aesni.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * \file aesni.h
  3. *
  4. * \brief AES-NI for hardware AES acceleration on some Intel processors
  5. *
  6. * \warning These functions are only for internal use by other library
  7. * functions; you must not call them directly.
  8. */
  9. /*
  10. * Copyright The Mbed TLS Contributors
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #ifndef MBEDTLS_AESNI_H
  26. #define MBEDTLS_AESNI_H
  27. #include "mbedtls/build_info.h"
  28. #include "mbedtls/aes.h"
  29. #define MBEDTLS_AESNI_AES 0x02000000u
  30. #define MBEDTLS_AESNI_CLMUL 0x00000002u
  31. /* Can we do AESNI with inline assembly?
  32. * (Only implemented with gas syntax, only for 64-bit.)
  33. */
  34. #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
  35. (defined(__amd64__) || defined(__x86_64__)) && \
  36. !defined(MBEDTLS_HAVE_X86_64)
  37. #define MBEDTLS_HAVE_X86_64
  38. #endif
  39. #if defined(MBEDTLS_AESNI_C)
  40. /* Can we do AESNI with intrinsics?
  41. * (Only implemented with certain compilers, only for certain targets.)
  42. */
  43. #undef MBEDTLS_AESNI_HAVE_INTRINSICS
  44. #if defined(_MSC_VER)
  45. /* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
  46. * VS 2013 and up for other reasons anyway, so no need to check the version. */
  47. #define MBEDTLS_AESNI_HAVE_INTRINSICS
  48. #endif
  49. /* GCC-like compilers: currently, we only support intrinsics if the requisite
  50. * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
  51. * or `clang -maes -mpclmul`). */
  52. #if defined(__GNUC__) && defined(__AES__) && defined(__PCLMUL__)
  53. #define MBEDTLS_AESNI_HAVE_INTRINSICS
  54. #endif
  55. /* Choose the implementation of AESNI, if one is available. */
  56. #undef MBEDTLS_AESNI_HAVE_CODE
  57. /* To minimize disruption when releasing the intrinsics-based implementation,
  58. * favor the assembly-based implementation if it's available. We intend to
  59. * revise this in a later release of Mbed TLS 3.x. In the long run, we will
  60. * likely remove the assembly implementation. */
  61. #if defined(MBEDTLS_HAVE_X86_64)
  62. #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
  63. #elif defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
  64. #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
  65. #endif
  66. #if defined(MBEDTLS_AESNI_HAVE_CODE)
  67. #ifdef __cplusplus
  68. extern "C" {
  69. #endif
  70. /**
  71. * \brief Internal function to detect the AES-NI feature in CPUs.
  72. *
  73. * \note This function is only for internal use by other library
  74. * functions; you must not call it directly.
  75. *
  76. * \param what The feature to detect
  77. * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
  78. *
  79. * \return 1 if CPU has support for the feature, 0 otherwise
  80. */
  81. int mbedtls_aesni_has_support(unsigned int what);
  82. /**
  83. * \brief Internal AES-NI AES-ECB block encryption and decryption
  84. *
  85. * \note This function is only for internal use by other library
  86. * functions; you must not call it directly.
  87. *
  88. * \param ctx AES context
  89. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  90. * \param input 16-byte input block
  91. * \param output 16-byte output block
  92. *
  93. * \return 0 on success (cannot fail)
  94. */
  95. int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
  96. int mode,
  97. const unsigned char input[16],
  98. unsigned char output[16]);
  99. /**
  100. * \brief Internal GCM multiplication: c = a * b in GF(2^128)
  101. *
  102. * \note This function is only for internal use by other library
  103. * functions; you must not call it directly.
  104. *
  105. * \param c Result
  106. * \param a First operand
  107. * \param b Second operand
  108. *
  109. * \note Both operands and result are bit strings interpreted as
  110. * elements of GF(2^128) as per the GCM spec.
  111. */
  112. void mbedtls_aesni_gcm_mult(unsigned char c[16],
  113. const unsigned char a[16],
  114. const unsigned char b[16]);
  115. /**
  116. * \brief Internal round key inversion. This function computes
  117. * decryption round keys from the encryption round keys.
  118. *
  119. * \note This function is only for internal use by other library
  120. * functions; you must not call it directly.
  121. *
  122. * \param invkey Round keys for the equivalent inverse cipher
  123. * \param fwdkey Original round keys (for encryption)
  124. * \param nr Number of rounds (that is, number of round keys minus one)
  125. */
  126. void mbedtls_aesni_inverse_key(unsigned char *invkey,
  127. const unsigned char *fwdkey,
  128. int nr);
  129. /**
  130. * \brief Internal key expansion for encryption
  131. *
  132. * \note This function is only for internal use by other library
  133. * functions; you must not call it directly.
  134. *
  135. * \param rk Destination buffer where the round keys are written
  136. * \param key Encryption key
  137. * \param bits Key size in bits (must be 128, 192 or 256)
  138. *
  139. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  140. */
  141. int mbedtls_aesni_setkey_enc(unsigned char *rk,
  142. const unsigned char *key,
  143. size_t bits);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif /* MBEDTLS_AESNI_HAVE_CODE */
  148. #endif /* MBEDTLS_AESNI_C */
  149. #endif /* MBEDTLS_AESNI_H */