aesce.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * \file aesce.h
  3. *
  4. * \brief Support hardware AES acceleration on Armv8-A processors with
  5. * the Armv8-A Cryptographic Extension.
  6. *
  7. * \warning These functions are only for internal use by other library
  8. * functions; you must not call them directly.
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. */
  14. #ifndef MBEDTLS_AESCE_H
  15. #define MBEDTLS_AESCE_H
  16. #include "mbedtls/build_info.h"
  17. #include "common.h"
  18. #include "mbedtls/aes.h"
  19. #if defined(MBEDTLS_AESCE_C) \
  20. && defined(MBEDTLS_ARCH_IS_ARMV8_A) && defined(MBEDTLS_HAVE_NEON_INTRINSICS) \
  21. && (defined(MBEDTLS_COMPILER_IS_GCC) || defined(__clang__) || defined(MSC_VER))
  22. /* MBEDTLS_AESCE_HAVE_CODE is defined if we have a suitable target platform, and a
  23. * potentially suitable compiler (compiler version & flags are not checked when defining
  24. * this). */
  25. #define MBEDTLS_AESCE_HAVE_CODE
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY)
  30. extern signed char mbedtls_aesce_has_support_result;
  31. /**
  32. * \brief Internal function to detect the crypto extension in CPUs.
  33. *
  34. * \return 1 if CPU has support for the feature, 0 otherwise
  35. */
  36. int mbedtls_aesce_has_support_impl(void);
  37. #define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \
  38. mbedtls_aesce_has_support_impl() : \
  39. mbedtls_aesce_has_support_result)
  40. #else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
  41. /* If we are not on Linux, we can't detect support so assume that it's supported.
  42. * Similarly, assume support if MBEDTLS_AES_USE_HARDWARE_ONLY is set.
  43. */
  44. #define MBEDTLS_AESCE_HAS_SUPPORT() 1
  45. #endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */
  46. /**
  47. * \brief Internal AES-ECB block encryption and decryption
  48. *
  49. * \warning This assumes that the context specifies either 10, 12 or 14
  50. * rounds and will behave incorrectly if this is not the case.
  51. *
  52. * \param ctx AES context
  53. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  54. * \param input 16-byte input block
  55. * \param output 16-byte output block
  56. *
  57. * \return 0 on success (cannot fail)
  58. */
  59. int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
  60. int mode,
  61. const unsigned char input[16],
  62. unsigned char output[16]);
  63. /**
  64. * \brief Internal GCM multiplication: c = a * b in GF(2^128)
  65. *
  66. * \note This function is only for internal use by other library
  67. * functions; you must not call it directly.
  68. *
  69. * \param c Result
  70. * \param a First operand
  71. * \param b Second operand
  72. *
  73. * \note Both operands and result are bit strings interpreted as
  74. * elements of GF(2^128) as per the GCM spec.
  75. */
  76. void mbedtls_aesce_gcm_mult(unsigned char c[16],
  77. const unsigned char a[16],
  78. const unsigned char b[16]);
  79. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  80. /**
  81. * \brief Internal round key inversion. This function computes
  82. * decryption round keys from the encryption round keys.
  83. *
  84. * \param invkey Round keys for the equivalent inverse cipher
  85. * \param fwdkey Original round keys (for encryption)
  86. * \param nr Number of rounds (that is, number of round keys minus one)
  87. */
  88. void mbedtls_aesce_inverse_key(unsigned char *invkey,
  89. const unsigned char *fwdkey,
  90. int nr);
  91. #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
  92. /**
  93. * \brief Internal key expansion for encryption
  94. *
  95. * \param rk Destination buffer where the round keys are written
  96. * \param key Encryption key
  97. * \param bits Key size in bits (must be 128, 192 or 256)
  98. *
  99. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  100. */
  101. int mbedtls_aesce_setkey_enc(unsigned char *rk,
  102. const unsigned char *key,
  103. size_t bits);
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #else
  108. #if defined(MBEDTLS_AES_USE_HARDWARE_ONLY) && defined(MBEDTLS_ARCH_IS_ARMV8_A)
  109. #error "AES hardware acceleration not supported on this platform / compiler"
  110. #endif
  111. #endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARMV8_A && MBEDTLS_HAVE_NEON_INTRINSICS &&
  112. (MBEDTLS_COMPILER_IS_GCC || __clang__ || MSC_VER) */
  113. #endif /* MBEDTLS_AESCE_H */