aesni.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
  32. ( defined(__amd64__) || defined(__x86_64__) ) && \
  33. ! defined(MBEDTLS_HAVE_X86_64)
  34. #define MBEDTLS_HAVE_X86_64
  35. #endif
  36. #if defined(MBEDTLS_HAVE_X86_64)
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /**
  41. * \brief Internal function to detect the AES-NI feature in CPUs.
  42. *
  43. * \note This function is only for internal use by other library
  44. * functions; you must not call it directly.
  45. *
  46. * \param what The feature to detect
  47. * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
  48. *
  49. * \return 1 if CPU has support for the feature, 0 otherwise
  50. */
  51. int mbedtls_aesni_has_support( unsigned int what );
  52. /**
  53. * \brief Internal AES-NI AES-ECB block encryption and decryption
  54. *
  55. * \note This function is only for internal use by other library
  56. * functions; you must not call it directly.
  57. *
  58. * \param ctx AES context
  59. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  60. * \param input 16-byte input block
  61. * \param output 16-byte output block
  62. *
  63. * \return 0 on success (cannot fail)
  64. */
  65. int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
  66. int mode,
  67. const unsigned char input[16],
  68. unsigned char output[16] );
  69. /**
  70. * \brief Internal GCM multiplication: c = a * b in GF(2^128)
  71. *
  72. * \note This function is only for internal use by other library
  73. * functions; you must not call it directly.
  74. *
  75. * \param c Result
  76. * \param a First operand
  77. * \param b Second operand
  78. *
  79. * \note Both operands and result are bit strings interpreted as
  80. * elements of GF(2^128) as per the GCM spec.
  81. */
  82. void mbedtls_aesni_gcm_mult( unsigned char c[16],
  83. const unsigned char a[16],
  84. const unsigned char b[16] );
  85. /**
  86. * \brief Internal round key inversion. This function computes
  87. * decryption round keys from the encryption round keys.
  88. *
  89. * \note This function is only for internal use by other library
  90. * functions; you must not call it directly.
  91. *
  92. * \param invkey Round keys for the equivalent inverse cipher
  93. * \param fwdkey Original round keys (for encryption)
  94. * \param nr Number of rounds (that is, number of round keys minus one)
  95. */
  96. void mbedtls_aesni_inverse_key( unsigned char *invkey,
  97. const unsigned char *fwdkey,
  98. int nr );
  99. /**
  100. * \brief Internal key expansion for encryption
  101. *
  102. * \note This function is only for internal use by other library
  103. * functions; you must not call it directly.
  104. *
  105. * \param rk Destination buffer where the round keys are written
  106. * \param key Encryption key
  107. * \param bits Key size in bits (must be 128, 192 or 256)
  108. *
  109. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  110. */
  111. int mbedtls_aesni_setkey_enc( unsigned char *rk,
  112. const unsigned char *key,
  113. size_t bits );
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* MBEDTLS_HAVE_X86_64 */
  118. #endif /* MBEDTLS_AESNI_H */