aesni.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 OR GPL-2.0-or-later
  12. */
  13. #ifndef MBEDTLS_AESNI_H
  14. #define MBEDTLS_AESNI_H
  15. #include "mbedtls/build_info.h"
  16. #include "mbedtls/aes.h"
  17. #define MBEDTLS_AESNI_AES 0x02000000u
  18. #define MBEDTLS_AESNI_CLMUL 0x00000002u
  19. #if defined(MBEDTLS_AESNI_C) && \
  20. (defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_X86))
  21. /* Can we do AESNI with intrinsics?
  22. * (Only implemented with certain compilers, only for certain targets.)
  23. */
  24. #undef MBEDTLS_AESNI_HAVE_INTRINSICS
  25. #if defined(_MSC_VER) && !defined(__clang__)
  26. /* Visual Studio supports AESNI intrinsics since VS 2008 SP1. We only support
  27. * VS 2013 and up for other reasons anyway, so no need to check the version. */
  28. #define MBEDTLS_AESNI_HAVE_INTRINSICS
  29. #endif
  30. /* GCC-like compilers: currently, we only support intrinsics if the requisite
  31. * target flag is enabled when building the library (e.g. `gcc -mpclmul -msse2`
  32. * or `clang -maes -mpclmul`). */
  33. #if (defined(__GNUC__) || defined(__clang__)) && defined(__AES__) && defined(__PCLMUL__)
  34. #define MBEDTLS_AESNI_HAVE_INTRINSICS
  35. #endif
  36. /* For 32-bit, we only support intrinsics */
  37. #if defined(MBEDTLS_ARCH_IS_X86) && (defined(__GNUC__) || defined(__clang__))
  38. #define MBEDTLS_AESNI_HAVE_INTRINSICS
  39. #endif
  40. /* Choose the implementation of AESNI, if one is available.
  41. *
  42. * Favor the intrinsics-based implementation if it's available, for better
  43. * maintainability.
  44. * Performance is about the same (see #7380).
  45. * In the long run, we will likely remove the assembly implementation. */
  46. #if defined(MBEDTLS_AESNI_HAVE_INTRINSICS)
  47. #define MBEDTLS_AESNI_HAVE_CODE 2 // via intrinsics
  48. #elif defined(MBEDTLS_HAVE_ASM) && \
  49. (defined(__GNUC__) || defined(__clang__)) && defined(MBEDTLS_ARCH_IS_X64)
  50. /* Can we do AESNI with inline assembly?
  51. * (Only implemented with gas syntax, only for 64-bit.)
  52. */
  53. #define MBEDTLS_AESNI_HAVE_CODE 1 // via assembly
  54. #else
  55. #error "MBEDTLS_AESNI_C defined, but neither intrinsics nor assembly available"
  56. #endif
  57. #if defined(MBEDTLS_AESNI_HAVE_CODE)
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * \brief Internal function to detect the AES-NI feature in CPUs.
  63. *
  64. * \note This function is only for internal use by other library
  65. * functions; you must not call it directly.
  66. *
  67. * \param what The feature to detect
  68. * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
  69. *
  70. * \return 1 if CPU has support for the feature, 0 otherwise
  71. */
  72. #if !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
  73. int mbedtls_aesni_has_support(unsigned int what);
  74. #else
  75. #define mbedtls_aesni_has_support(what) 1
  76. #endif
  77. /**
  78. * \brief Internal AES-NI AES-ECB block encryption and decryption
  79. *
  80. * \note This function is only for internal use by other library
  81. * functions; you must not call it directly.
  82. *
  83. * \param ctx AES context
  84. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  85. * \param input 16-byte input block
  86. * \param output 16-byte output block
  87. *
  88. * \return 0 on success (cannot fail)
  89. */
  90. int mbedtls_aesni_crypt_ecb(mbedtls_aes_context *ctx,
  91. int mode,
  92. const unsigned char input[16],
  93. unsigned char output[16]);
  94. /**
  95. * \brief Internal GCM multiplication: c = a * b in GF(2^128)
  96. *
  97. * \note This function is only for internal use by other library
  98. * functions; you must not call it directly.
  99. *
  100. * \param c Result
  101. * \param a First operand
  102. * \param b Second operand
  103. *
  104. * \note Both operands and result are bit strings interpreted as
  105. * elements of GF(2^128) as per the GCM spec.
  106. */
  107. void mbedtls_aesni_gcm_mult(unsigned char c[16],
  108. const unsigned char a[16],
  109. const unsigned char b[16]);
  110. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  111. /**
  112. * \brief Internal round key inversion. This function computes
  113. * decryption round keys from the encryption round keys.
  114. *
  115. * \note This function is only for internal use by other library
  116. * functions; you must not call it directly.
  117. *
  118. * \param invkey Round keys for the equivalent inverse cipher
  119. * \param fwdkey Original round keys (for encryption)
  120. * \param nr Number of rounds (that is, number of round keys minus one)
  121. */
  122. void mbedtls_aesni_inverse_key(unsigned char *invkey,
  123. const unsigned char *fwdkey,
  124. int nr);
  125. #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
  126. /**
  127. * \brief Internal key expansion for encryption
  128. *
  129. * \note This function is only for internal use by other library
  130. * functions; you must not call it directly.
  131. *
  132. * \param rk Destination buffer where the round keys are written
  133. * \param key Encryption key
  134. * \param bits Key size in bits (must be 128, 192 or 256)
  135. *
  136. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  137. */
  138. int mbedtls_aesni_setkey_enc(unsigned char *rk,
  139. const unsigned char *key,
  140. size_t bits);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif /* MBEDTLS_AESNI_HAVE_CODE */
  145. #endif /* MBEDTLS_AESNI_C && (MBEDTLS_ARCH_IS_X64 || MBEDTLS_ARCH_IS_X86) */
  146. #endif /* MBEDTLS_AESNI_H */