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