Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 1 | /** |
| 2 | * \file aesce.h |
| 3 | * |
Dave Rodgman | f918d42 | 2023-03-17 17:52:23 +0000 | [diff] [blame] | 4 | * \brief Support hardware AES acceleration on Armv8-A processors with |
Dave Rodgman | ece803b | 2023-10-08 20:24:48 +0100 | [diff] [blame] | 5 | * the Armv8-A Cryptographic Extension. |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 12 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 13 | */ |
| 14 | #ifndef MBEDTLS_AESCE_H |
| 15 | #define MBEDTLS_AESCE_H |
| 16 | |
| 17 | #include "mbedtls/build_info.h" |
| 18 | |
| 19 | #include "mbedtls/aes.h" |
| 20 | |
Jerry Yu | 07d28d8 | 2023-03-20 18:12:36 +0800 | [diff] [blame] | 21 | |
Dave Rodgman | 9fd1b52 | 2023-10-10 15:23:44 +0100 | [diff] [blame] | 22 | #if defined(MBEDTLS_AESCE_C) && defined(MBEDTLS_ARCH_IS_ARMV8_A) && defined(__ARM_NEON) |
Jerry Yu | 72fd0bd | 2023-08-18 16:31:01 +0800 | [diff] [blame] | 23 | |
| 24 | #define MBEDTLS_AESCE_HAVE_CODE |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 29 | |
Dave Rodgman | 4566132 | 2023-08-04 12:31:58 +0100 | [diff] [blame] | 30 | #if defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) |
| 31 | |
Dave Rodgman | b30adce | 2023-08-04 12:52:51 +0100 | [diff] [blame] | 32 | extern signed char mbedtls_aesce_has_support_result; |
Dave Rodgman | 4566132 | 2023-08-04 12:31:58 +0100 | [diff] [blame] | 33 | |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 34 | /** |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 35 | * \brief Internal function to detect the crypto extension in CPUs. |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 36 | * |
| 37 | * \return 1 if CPU has support for the feature, 0 otherwise |
| 38 | */ |
Dave Rodgman | 4566132 | 2023-08-04 12:31:58 +0100 | [diff] [blame] | 39 | int mbedtls_aesce_has_support_impl(void); |
Jerry Yu | 0d4f4e5 | 2023-03-31 14:32:47 +0800 | [diff] [blame] | 40 | |
Dave Rodgman | f2249ec | 2023-08-04 14:27:58 +0100 | [diff] [blame] | 41 | #define MBEDTLS_AESCE_HAS_SUPPORT() (mbedtls_aesce_has_support_result == -1 ? \ |
Dave Rodgman | 4566132 | 2023-08-04 12:31:58 +0100 | [diff] [blame] | 42 | mbedtls_aesce_has_support_impl() : \ |
| 43 | mbedtls_aesce_has_support_result) |
| 44 | |
| 45 | #else /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */ |
| 46 | |
| 47 | /* If we are not on Linux, we can't detect support so assume that it's supported. |
| 48 | * Similarly, assume support if MBEDTLS_AES_USE_HARDWARE_ONLY is set. |
| 49 | */ |
Dave Rodgman | f2249ec | 2023-08-04 14:27:58 +0100 | [diff] [blame] | 50 | #define MBEDTLS_AESCE_HAS_SUPPORT() 1 |
Dave Rodgman | 4566132 | 2023-08-04 12:31:58 +0100 | [diff] [blame] | 51 | |
| 52 | #endif /* defined(__linux__) && !defined(MBEDTLS_AES_USE_HARDWARE_ONLY) */ |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 53 | |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 54 | /** |
| 55 | * \brief Internal AES-ECB block encryption and decryption |
| 56 | * |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 57 | * \warning This assumes that the context specifies either 10, 12 or 14 |
| 58 | * rounds and will behave incorrectly if this is not the case. |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 59 | * |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 60 | * \param ctx AES context |
| 61 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT |
| 62 | * \param input 16-byte input block |
| 63 | * \param output 16-byte output block |
| 64 | * |
| 65 | * \return 0 on success (cannot fail) |
| 66 | */ |
| 67 | int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx, |
| 68 | int mode, |
| 69 | const unsigned char input[16], |
| 70 | unsigned char output[16]); |
| 71 | |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 72 | /** |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 73 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) |
| 74 | * |
| 75 | * \note This function is only for internal use by other library |
| 76 | * functions; you must not call it directly. |
| 77 | * |
| 78 | * \param c Result |
| 79 | * \param a First operand |
| 80 | * \param b Second operand |
| 81 | * |
| 82 | * \note Both operands and result are bit strings interpreted as |
| 83 | * elements of GF(2^128) as per the GCM spec. |
| 84 | */ |
| 85 | void mbedtls_aesce_gcm_mult(unsigned char c[16], |
| 86 | const unsigned char a[16], |
| 87 | const unsigned char b[16]); |
| 88 | |
| 89 | |
Yanray Wang | b67b474 | 2023-10-31 17:10:32 +0800 | [diff] [blame] | 90 | #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT) |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 91 | /** |
Jerry Yu | e096da1 | 2023-01-10 17:07:01 +0800 | [diff] [blame] | 92 | * \brief Internal round key inversion. This function computes |
| 93 | * decryption round keys from the encryption round keys. |
| 94 | * |
| 95 | * \param invkey Round keys for the equivalent inverse cipher |
| 96 | * \param fwdkey Original round keys (for encryption) |
| 97 | * \param nr Number of rounds (that is, number of round keys minus one) |
| 98 | */ |
| 99 | void mbedtls_aesce_inverse_key(unsigned char *invkey, |
| 100 | const unsigned char *fwdkey, |
| 101 | int nr); |
Yanray Wang | b67b474 | 2023-10-31 17:10:32 +0800 | [diff] [blame] | 102 | #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */ |
Jerry Yu | e096da1 | 2023-01-10 17:07:01 +0800 | [diff] [blame] | 103 | |
| 104 | /** |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 105 | * \brief Internal key expansion for encryption |
| 106 | * |
| 107 | * \param rk Destination buffer where the round keys are written |
| 108 | * \param key Encryption key |
| 109 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 110 | * |
| 111 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 112 | */ |
| 113 | int mbedtls_aesce_setkey_enc(unsigned char *rk, |
| 114 | const unsigned char *key, |
| 115 | size_t bits); |
| 116 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 117 | #ifdef __cplusplus |
| 118 | } |
| 119 | #endif |
| 120 | |
Dave Rodgman | ece803b | 2023-10-08 20:24:48 +0100 | [diff] [blame] | 121 | #else |
| 122 | |
Dave Rodgman | 9fd1b52 | 2023-10-10 15:23:44 +0100 | [diff] [blame] | 123 | #if defined(MBEDTLS_AES_USE_HARDWARE_ONLY) && defined(MBEDTLS_ARCH_IS_ARMV8_A) |
Dave Rodgman | ece803b | 2023-10-08 20:24:48 +0100 | [diff] [blame] | 124 | #error "AES hardware acceleration not supported on this platform" |
| 125 | #endif |
| 126 | |
Dave Rodgman | 9fd1b52 | 2023-10-10 15:23:44 +0100 | [diff] [blame] | 127 | #endif /* MBEDTLS_AESCE_C && MBEDTLS_ARCH_IS_ARMV8_A && __ARM_NEON */ |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 128 | |
| 129 | #endif /* MBEDTLS_AESCE_H */ |