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 | #if !defined(MBEDTLS_HAVE_ARM64) |
Jerry Yu | 61c4cfa | 2023-04-26 11:06:51 +0800 | [diff] [blame] | 34 | #if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) |
Jerry Yu | 07d28d8 | 2023-03-20 18:12:36 +0800 | [diff] [blame] | 35 | #define MBEDTLS_HAVE_ARM64 |
| 36 | #endif |
| 37 | #endif |
| 38 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 39 | #if defined(MBEDTLS_HAVE_ARM64) |
| 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 44 | |
| 45 | /** |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 46 | * \brief Internal function to detect the crypto extension in CPUs. |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 47 | * |
| 48 | * \return 1 if CPU has support for the feature, 0 otherwise |
| 49 | */ |
| 50 | int mbedtls_aesce_has_support(void); |
| 51 | |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 52 | /** |
| 53 | * \brief Internal AES-ECB block encryption and decryption |
| 54 | * |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame^] | 55 | * \warning This assumes that the context specifies either 10, 12 or 14 |
| 56 | * rounds and will behave incorrectly if this is not the case. |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 57 | * |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 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_aesce_crypt_ecb(mbedtls_aes_context *ctx, |
| 66 | int mode, |
| 67 | const unsigned char input[16], |
| 68 | unsigned char output[16]); |
| 69 | |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 70 | /** |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 71 | * \brief Internal GCM multiplication: c = a * b in GF(2^128) |
| 72 | * |
| 73 | * \note This function is only for internal use by other library |
| 74 | * functions; you must not call it directly. |
| 75 | * |
| 76 | * \param c Result |
| 77 | * \param a First operand |
| 78 | * \param b Second operand |
| 79 | * |
| 80 | * \note Both operands and result are bit strings interpreted as |
| 81 | * elements of GF(2^128) as per the GCM spec. |
| 82 | */ |
| 83 | void mbedtls_aesce_gcm_mult(unsigned char c[16], |
| 84 | const unsigned char a[16], |
| 85 | const unsigned char b[16]); |
| 86 | |
| 87 | |
| 88 | /** |
Jerry Yu | e096da1 | 2023-01-10 17:07:01 +0800 | [diff] [blame] | 89 | * \brief Internal round key inversion. This function computes |
| 90 | * decryption round keys from the encryption round keys. |
| 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_aesce_inverse_key(unsigned char *invkey, |
| 97 | const unsigned char *fwdkey, |
| 98 | int nr); |
| 99 | |
| 100 | /** |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 101 | * \brief Internal key expansion for encryption |
| 102 | * |
| 103 | * \param rk Destination buffer where the round keys are written |
| 104 | * \param key Encryption key |
| 105 | * \param bits Key size in bits (must be 128, 192 or 256) |
| 106 | * |
| 107 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH |
| 108 | */ |
| 109 | int mbedtls_aesce_setkey_enc(unsigned char *rk, |
| 110 | const unsigned char *key, |
| 111 | size_t bits); |
| 112 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 113 | #ifdef __cplusplus |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | #endif /* MBEDTLS_HAVE_ARM64 */ |
| 118 | |
| 119 | #endif /* MBEDTLS_AESCE_H */ |