aesni.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * \file aesni.h
  3. *
  4. * \brief AES-NI for hardware AES acceleration on some Intel processors
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_AESNI_H
  25. #define MBEDTLS_AESNI_H
  26. #include "aes.h"
  27. #define MBEDTLS_AESNI_AES 0x02000000u
  28. #define MBEDTLS_AESNI_CLMUL 0x00000002u
  29. #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \
  30. ( defined(__amd64__) || defined(__x86_64__) ) && \
  31. ! defined(MBEDTLS_HAVE_X86_64)
  32. #define MBEDTLS_HAVE_X86_64
  33. #endif
  34. #if defined(MBEDTLS_HAVE_X86_64)
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * \brief AES-NI features detection routine
  40. *
  41. * \param what The feature to detect
  42. * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL)
  43. *
  44. * \return 1 if CPU has support for the feature, 0 otherwise
  45. */
  46. int mbedtls_aesni_has_support( unsigned int what );
  47. /**
  48. * \brief AES-NI AES-ECB block en(de)cryption
  49. *
  50. * \param ctx AES context
  51. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  52. * \param input 16-byte input block
  53. * \param output 16-byte output block
  54. *
  55. * \return 0 on success (cannot fail)
  56. */
  57. int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
  58. int mode,
  59. const unsigned char input[16],
  60. unsigned char output[16] );
  61. /**
  62. * \brief GCM multiplication: c = a * b in GF(2^128)
  63. *
  64. * \param c Result
  65. * \param a First operand
  66. * \param b Second operand
  67. *
  68. * \note Both operands and result are bit strings interpreted as
  69. * elements of GF(2^128) as per the GCM spec.
  70. */
  71. void mbedtls_aesni_gcm_mult( unsigned char c[16],
  72. const unsigned char a[16],
  73. const unsigned char b[16] );
  74. /**
  75. * \brief Compute decryption round keys from encryption round keys
  76. *
  77. * \param invkey Round keys for the equivalent inverse cipher
  78. * \param fwdkey Original round keys (for encryption)
  79. * \param nr Number of rounds (that is, number of round keys minus one)
  80. */
  81. void mbedtls_aesni_inverse_key( unsigned char *invkey,
  82. const unsigned char *fwdkey, int nr );
  83. /**
  84. * \brief Perform key expansion (for encryption)
  85. *
  86. * \param rk Destination buffer where the round keys are written
  87. * \param key Encryption key
  88. * \param bits Key size in bits (must be 128, 192 or 256)
  89. *
  90. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  91. */
  92. int mbedtls_aesni_setkey_enc( unsigned char *rk,
  93. const unsigned char *key,
  94. size_t bits );
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif /* MBEDTLS_HAVE_X86_64 */
  99. #endif /* MBEDTLS_AESNI_H */