Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 1 | /* |
Dave Rodgman | f918d42 | 2023-03-17 17:52:23 +0000 | [diff] [blame] | 2 | * Armv8-A Cryptographic Extension support functions for Aarch64 |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
Jerry Yu | 48b999c | 2023-03-03 15:51:07 +0800 | [diff] [blame] | 20 | #if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO) && \ |
Jerry Yu | 6f86c19 | 2023-03-13 11:03:40 +0800 | [diff] [blame] | 21 | defined(__clang__) && __clang_major__ >= 4 |
Jerry Yu | 48b999c | 2023-03-03 15:51:07 +0800 | [diff] [blame] | 22 | /* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged. |
| 23 | * |
| 24 | * The intrinsic declaration are guarded by predefined ACLE macros in clang: |
| 25 | * these are normally only enabled by the -march option on the command line. |
| 26 | * By defining the macros ourselves we gain access to those declarations without |
| 27 | * requiring -march on the command line. |
| 28 | * |
| 29 | * `arm_neon.h` could be included by any header file, so we put these defines |
| 30 | * at the top of this file, before any includes. |
| 31 | */ |
| 32 | #define __ARM_FEATURE_CRYPTO 1 |
Jerry Yu | ae129c3 | 2023-03-03 15:55:56 +0800 | [diff] [blame] | 33 | /* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions |
| 34 | * |
Jerry Yu | 490bf08 | 2023-03-06 15:21:44 +0800 | [diff] [blame] | 35 | * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it |
| 36 | * for older compilers. |
Jerry Yu | ae129c3 | 2023-03-03 15:55:56 +0800 | [diff] [blame] | 37 | */ |
| 38 | #define __ARM_FEATURE_AES 1 |
Dave Rodgman | db6ab24 | 2023-03-14 16:03:57 +0000 | [diff] [blame] | 39 | #define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG |
Jerry Yu | 490bf08 | 2023-03-06 15:21:44 +0800 | [diff] [blame] | 40 | #endif |
Jerry Yu | 48b999c | 2023-03-03 15:51:07 +0800 | [diff] [blame] | 41 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include "common.h" |
| 44 | |
| 45 | #if defined(MBEDTLS_AESCE_C) |
| 46 | |
| 47 | #include "aesce.h" |
| 48 | |
| 49 | #if defined(MBEDTLS_HAVE_ARM64) |
| 50 | |
Jerry Yu | 61c4cfa | 2023-04-26 11:06:51 +0800 | [diff] [blame] | 51 | /* Compiler version checks. */ |
Jerry Yu | db368de | 2023-04-26 16:55:37 +0800 | [diff] [blame] | 52 | #if defined(__clang__) |
| 53 | # if __clang_major__ < 4 |
| 54 | # error "Minimum version of Clang for MBEDTLS_AESCE_C is 4.0." |
| 55 | # endif |
| 56 | #elif defined(__GNUC__) |
| 57 | # if __GNUC__ < 6 |
| 58 | # error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0." |
| 59 | # endif |
| 60 | #elif defined(_MSC_VER) |
Jerry Yu | 61c4cfa | 2023-04-26 11:06:51 +0800 | [diff] [blame] | 61 | /* TODO: We haven't verified MSVC from 1920 to 1928. If someone verified that, |
| 62 | * please update this and document of `MBEDTLS_AESCE_C` in |
| 63 | * `mbedtls_config.h`. */ |
Jerry Yu | db368de | 2023-04-26 16:55:37 +0800 | [diff] [blame] | 64 | # if _MSC_VER < 1929 |
| 65 | # error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2." |
| 66 | # endif |
Jerry Yu | 61c4cfa | 2023-04-26 11:06:51 +0800 | [diff] [blame] | 67 | #endif |
| 68 | |
Jerry Yu | 6b00f5a | 2023-05-04 16:30:21 +0800 | [diff] [blame] | 69 | #ifdef __ARM_NEON |
Jerry Yu | 08933d3 | 2023-04-27 18:28:00 +0800 | [diff] [blame] | 70 | #include <arm_neon.h> |
Jerry Yu | 6b00f5a | 2023-05-04 16:30:21 +0800 | [diff] [blame] | 71 | #else |
| 72 | #error "Target does not support NEON instructions" |
| 73 | #endif |
Jerry Yu | 08933d3 | 2023-04-27 18:28:00 +0800 | [diff] [blame] | 74 | |
Jerry Yu | 580e06f | 2023-04-28 17:42:40 +0800 | [diff] [blame] | 75 | #if !(defined(__ARM_FEATURE_CRYPTO) || defined(__ARM_FEATURE_AES)) || \ |
| 76 | defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG) |
Jerry Yu | ec9be84 | 2023-03-14 10:42:47 +0800 | [diff] [blame] | 77 | # if defined(__clang__) |
Jerry Yu | 08933d3 | 2023-04-27 18:28:00 +0800 | [diff] [blame] | 78 | # pragma clang attribute push (__attribute__((target("crypto,aes"))), apply_to=function) |
Jerry Yu | ec9be84 | 2023-03-14 10:42:47 +0800 | [diff] [blame] | 79 | # define MBEDTLS_POP_TARGET_PRAGMA |
| 80 | # elif defined(__GNUC__) |
Jerry Yu | ec9be84 | 2023-03-14 10:42:47 +0800 | [diff] [blame] | 81 | # pragma GCC push_options |
Beniamin Sandu | 471a975 | 2023-06-25 20:16:16 +0300 | [diff] [blame] | 82 | # pragma GCC target ("+crypto") |
Jerry Yu | ec9be84 | 2023-03-14 10:42:47 +0800 | [diff] [blame] | 83 | # define MBEDTLS_POP_TARGET_PRAGMA |
Jerry Yu | 07d28d8 | 2023-03-20 18:12:36 +0800 | [diff] [blame] | 84 | # elif defined(_MSC_VER) |
Jerry Yu | 61c4cfa | 2023-04-26 11:06:51 +0800 | [diff] [blame] | 85 | # error "Required feature(__ARM_FEATURE_AES) is not enabled." |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 86 | # endif |
Jerry Yu | 580e06f | 2023-04-28 17:42:40 +0800 | [diff] [blame] | 87 | #endif /* !(__ARM_FEATURE_CRYPTO || __ARM_FEATURE_AES) || |
| 88 | MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */ |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 89 | |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 90 | #if defined(__linux__) |
| 91 | #include <asm/hwcap.h> |
| 92 | #include <sys/auxv.h> |
| 93 | #endif |
| 94 | |
| 95 | /* |
| 96 | * AES instruction support detection routine |
| 97 | */ |
| 98 | int mbedtls_aesce_has_support(void) |
| 99 | { |
| 100 | #if defined(__linux__) |
| 101 | unsigned long auxval = getauxval(AT_HWCAP); |
| 102 | return (auxval & (HWCAP_ASIMD | HWCAP_AES)) == |
| 103 | (HWCAP_ASIMD | HWCAP_AES); |
| 104 | #else |
Jerry Yu | ba1e78f | 2023-02-24 11:18:16 +0800 | [diff] [blame] | 105 | /* Assume AES instructions are supported. */ |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 106 | return 1; |
| 107 | #endif |
| 108 | } |
| 109 | |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 110 | /* Single round of AESCE encryption */ |
| 111 | #define AESCE_ENCRYPT_ROUND \ |
| 112 | block = vaeseq_u8(block, vld1q_u8(keys)); \ |
| 113 | block = vaesmcq_u8(block); \ |
| 114 | keys += 16 |
| 115 | /* Two rounds of AESCE encryption */ |
| 116 | #define AESCE_ENCRYPT_ROUND_X2 AESCE_ENCRYPT_ROUND; AESCE_ENCRYPT_ROUND |
| 117 | |
Dave Rodgman | 9bb7e6f | 2023-06-16 09:41:21 +0100 | [diff] [blame] | 118 | MBEDTLS_OPTIMIZE_FOR_PERFORMANCE |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 119 | static uint8x16_t aesce_encrypt_block(uint8x16_t block, |
| 120 | unsigned char *keys, |
| 121 | int rounds) |
| 122 | { |
Dave Rodgman | 73b0c0b | 2023-06-16 14:48:14 +0100 | [diff] [blame] | 123 | /* 10, 12 or 14 rounds. Unroll loop. */ |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 124 | if (rounds == 10) { |
| 125 | goto rounds_10; |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 126 | } |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 127 | if (rounds == 12) { |
| 128 | goto rounds_12; |
| 129 | } |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 130 | AESCE_ENCRYPT_ROUND_X2; |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 131 | rounds_12: |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 132 | AESCE_ENCRYPT_ROUND_X2; |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 133 | rounds_10: |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 134 | AESCE_ENCRYPT_ROUND_X2; |
| 135 | AESCE_ENCRYPT_ROUND_X2; |
| 136 | AESCE_ENCRYPT_ROUND_X2; |
| 137 | AESCE_ENCRYPT_ROUND_X2; |
| 138 | AESCE_ENCRYPT_ROUND; |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 139 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 140 | /* AES AddRoundKey for the previous round. |
| 141 | * SubBytes, ShiftRows for the final round. */ |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 142 | block = vaeseq_u8(block, vld1q_u8(keys)); |
| 143 | keys += 16; |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 144 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 145 | /* Final round: no MixColumns */ |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 146 | |
| 147 | /* Final AddRoundKey */ |
Dave Rodgman | 96fdfb8 | 2023-06-15 16:21:31 +0100 | [diff] [blame] | 148 | block = veorq_u8(block, vld1q_u8(keys)); |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 149 | |
| 150 | return block; |
| 151 | } |
| 152 | |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 153 | /* Single round of AESCE decryption |
| 154 | * |
| 155 | * AES AddRoundKey, SubBytes, ShiftRows |
| 156 | * |
| 157 | * block = vaesdq_u8(block, vld1q_u8(keys)); |
| 158 | * |
| 159 | * AES inverse MixColumns for the next round. |
| 160 | * |
| 161 | * This means that we switch the order of the inverse AddRoundKey and |
| 162 | * inverse MixColumns operations. We have to do this as AddRoundKey is |
| 163 | * done in an atomic instruction together with the inverses of SubBytes |
| 164 | * and ShiftRows. |
| 165 | * |
| 166 | * It works because MixColumns is a linear operation over GF(2^8) and |
| 167 | * AddRoundKey is an exclusive or, which is equivalent to addition over |
| 168 | * GF(2^8). (The inverse of MixColumns needs to be applied to the |
| 169 | * affected round keys separately which has been done when the |
| 170 | * decryption round keys were calculated.) |
| 171 | * |
| 172 | * block = vaesimcq_u8(block); |
| 173 | */ |
| 174 | #define AESCE_DECRYPT_ROUND \ |
| 175 | block = vaesdq_u8(block, vld1q_u8(keys)); \ |
| 176 | block = vaesimcq_u8(block); \ |
| 177 | keys += 16 |
| 178 | /* Two rounds of AESCE decryption */ |
| 179 | #define AESCE_DECRYPT_ROUND_X2 AESCE_DECRYPT_ROUND; AESCE_DECRYPT_ROUND |
| 180 | |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 181 | static uint8x16_t aesce_decrypt_block(uint8x16_t block, |
| 182 | unsigned char *keys, |
| 183 | int rounds) |
| 184 | { |
Dave Rodgman | 73b0c0b | 2023-06-16 14:48:14 +0100 | [diff] [blame] | 185 | /* 10, 12 or 14 rounds. Unroll loop. */ |
Dave Rodgman | 1c4451d | 2023-06-15 16:28:00 +0100 | [diff] [blame] | 186 | if (rounds == 10) { |
| 187 | goto rounds_10; |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 188 | } |
Dave Rodgman | 1c4451d | 2023-06-15 16:28:00 +0100 | [diff] [blame] | 189 | if (rounds == 12) { |
| 190 | goto rounds_12; |
| 191 | } |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 192 | AESCE_DECRYPT_ROUND_X2; |
Dave Rodgman | 1c4451d | 2023-06-15 16:28:00 +0100 | [diff] [blame] | 193 | rounds_12: |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 194 | AESCE_DECRYPT_ROUND_X2; |
Dave Rodgman | 1c4451d | 2023-06-15 16:28:00 +0100 | [diff] [blame] | 195 | rounds_10: |
Dave Rodgman | 48fd2ab | 2023-06-16 09:36:50 +0100 | [diff] [blame] | 196 | AESCE_DECRYPT_ROUND_X2; |
| 197 | AESCE_DECRYPT_ROUND_X2; |
| 198 | AESCE_DECRYPT_ROUND_X2; |
| 199 | AESCE_DECRYPT_ROUND_X2; |
| 200 | AESCE_DECRYPT_ROUND; |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 201 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 202 | /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the |
| 203 | * last full round. */ |
Dave Rodgman | 1c4451d | 2023-06-15 16:28:00 +0100 | [diff] [blame] | 204 | block = vaesdq_u8(block, vld1q_u8(keys)); |
| 205 | keys += 16; |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 206 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 207 | /* Inverse AddRoundKey for inverting the initial round key addition. */ |
Dave Rodgman | 1c4451d | 2023-06-15 16:28:00 +0100 | [diff] [blame] | 208 | block = veorq_u8(block, vld1q_u8(keys)); |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 209 | |
| 210 | return block; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * AES-ECB block en(de)cryption |
| 215 | */ |
| 216 | int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx, |
| 217 | int mode, |
| 218 | const unsigned char input[16], |
| 219 | unsigned char output[16]) |
| 220 | { |
| 221 | uint8x16_t block = vld1q_u8(&input[0]); |
| 222 | unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset); |
| 223 | |
| 224 | if (mode == MBEDTLS_AES_ENCRYPT) { |
| 225 | block = aesce_encrypt_block(block, keys, ctx->nr); |
| 226 | } else { |
| 227 | block = aesce_decrypt_block(block, keys, ctx->nr); |
| 228 | } |
| 229 | vst1q_u8(&output[0], block); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jerry Yu | e096da1 | 2023-01-10 17:07:01 +0800 | [diff] [blame] | 234 | /* |
| 235 | * Compute decryption round keys from encryption round keys |
| 236 | */ |
| 237 | void mbedtls_aesce_inverse_key(unsigned char *invkey, |
| 238 | const unsigned char *fwdkey, |
| 239 | int nr) |
| 240 | { |
| 241 | int i, j; |
| 242 | j = nr; |
| 243 | vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16)); |
| 244 | for (i = 1, j--; j > 0; i++, j--) { |
| 245 | vst1q_u8(invkey + i * 16, |
| 246 | vaesimcq_u8(vld1q_u8(fwdkey + j * 16))); |
| 247 | } |
| 248 | vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16)); |
| 249 | |
| 250 | } |
| 251 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 252 | static inline uint32_t aes_rot_word(uint32_t word) |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 253 | { |
| 254 | return (word << (32 - 8)) | (word >> 8); |
| 255 | } |
| 256 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 257 | static inline uint32_t aes_sub_word(uint32_t in) |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 258 | { |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 259 | uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in)); |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 260 | uint8x16_t zero = vdupq_n_u8(0); |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 261 | |
| 262 | /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields |
| 263 | * the correct result as ShiftRows doesn't change the first row. */ |
| 264 | v = vaeseq_u8(zero, v); |
| 265 | return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0); |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 269 | * Key expansion function |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 270 | */ |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 271 | static void aesce_setkey_enc(unsigned char *rk, |
| 272 | const unsigned char *key, |
| 273 | const size_t key_bit_length) |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 274 | { |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 275 | static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10, |
| 276 | 0x20, 0x40, 0x80, 0x1b, 0x36 }; |
Jerry Yu | 947bf96 | 2023-02-23 11:07:57 +0800 | [diff] [blame] | 277 | /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf |
| 278 | * - Section 5, Nr = Nk + 6 |
Jerry Yu | 2c26651 | 2023-03-01 11:18:20 +0800 | [diff] [blame] | 279 | * - Section 5.2, the length of round keys is Nb*(Nr+1) |
Jerry Yu | 947bf96 | 2023-02-23 11:07:57 +0800 | [diff] [blame] | 280 | */ |
| 281 | const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */ |
| 282 | const size_t round_key_len_in_words = 4; /* Nb */ |
Jerry Yu | 2c26651 | 2023-03-01 11:18:20 +0800 | [diff] [blame] | 283 | const size_t rounds_needed = key_len_in_words + 6; /* Nr */ |
| 284 | const size_t round_keys_len_in_words = |
| 285 | round_key_len_in_words * (rounds_needed + 1); /* Nb*(Nr+1) */ |
| 286 | const uint32_t *rko_end = (uint32_t *) rk + round_keys_len_in_words; |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 287 | |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 288 | memcpy(rk, key, key_len_in_words * 4); |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 289 | |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 290 | for (uint32_t *rki = (uint32_t *) rk; |
| 291 | rki + key_len_in_words < rko_end; |
| 292 | rki += key_len_in_words) { |
| 293 | |
Jerry Yu | fac5a54 | 2023-02-23 10:13:40 +0800 | [diff] [blame] | 294 | size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words; |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 295 | uint32_t *rko; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 296 | rko = rki + key_len_in_words; |
| 297 | rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1])); |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 298 | rko[0] ^= rcon[iteration] ^ rki[0]; |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 299 | rko[1] = rko[0] ^ rki[1]; |
| 300 | rko[2] = rko[1] ^ rki[2]; |
| 301 | rko[3] = rko[2] ^ rki[3]; |
Jerry Yu | fac5a54 | 2023-02-23 10:13:40 +0800 | [diff] [blame] | 302 | if (rko + key_len_in_words > rko_end) { |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 303 | /* Do not write overflow words.*/ |
| 304 | continue; |
| 305 | } |
Yanray Wang | e2bc158 | 2023-05-08 10:28:53 +0800 | [diff] [blame] | 306 | #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 307 | switch (key_bit_length) { |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 308 | case 128: |
| 309 | break; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 310 | case 192: |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 311 | rko[4] = rko[3] ^ rki[4]; |
| 312 | rko[5] = rko[4] ^ rki[5]; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 313 | break; |
| 314 | case 256: |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 315 | rko[4] = aes_sub_word(rko[3]) ^ rki[4]; |
| 316 | rko[5] = rko[4] ^ rki[5]; |
| 317 | rko[6] = rko[5] ^ rki[6]; |
| 318 | rko[7] = rko[6] ^ rki[7]; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 319 | break; |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 320 | } |
Yanray Wang | e2bc158 | 2023-05-08 10:28:53 +0800 | [diff] [blame] | 321 | #endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */ |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Key expansion, wrapper |
| 327 | */ |
| 328 | int mbedtls_aesce_setkey_enc(unsigned char *rk, |
| 329 | const unsigned char *key, |
| 330 | size_t bits) |
| 331 | { |
| 332 | switch (bits) { |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 333 | case 128: |
| 334 | case 192: |
| 335 | case 256: |
Jerry Yu | ba1e78f | 2023-02-24 11:18:16 +0800 | [diff] [blame] | 336 | aesce_setkey_enc(rk, key, bits); |
| 337 | break; |
| 338 | default: |
| 339 | return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 345 | #if defined(MBEDTLS_GCM_C) |
| 346 | |
Jerry Yu | 132d0cb | 2023-03-02 17:35:53 +0800 | [diff] [blame] | 347 | #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ == 5 |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 348 | /* Some intrinsics are not available for GCC 5.X. */ |
Jerry Yu | 132d0cb | 2023-03-02 17:35:53 +0800 | [diff] [blame] | 349 | #define vreinterpretq_p64_u8(a) ((poly64x2_t) a) |
| 350 | #define vreinterpretq_u8_p128(a) ((uint8x16_t) a) |
| 351 | static inline poly64_t vget_low_p64(poly64x2_t __a) |
| 352 | { |
| 353 | uint64x2_t tmp = (uint64x2_t) (__a); |
| 354 | uint64x1_t lo = vcreate_u64(vgetq_lane_u64(tmp, 0)); |
| 355 | return (poly64_t) (lo); |
| 356 | } |
| 357 | #endif /* !__clang__ && __GNUC__ && __GNUC__ == 5*/ |
| 358 | |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 359 | /* vmull_p64/vmull_high_p64 wrappers. |
| 360 | * |
| 361 | * Older compilers miss some intrinsic functions for `poly*_t`. We use |
| 362 | * uint8x16_t and uint8x16x3_t as input/output parameters. |
| 363 | */ |
Jerry Yu | 9db4b1f | 2023-03-21 16:56:43 +0800 | [diff] [blame] | 364 | #if defined(__GNUC__) && !defined(__clang__) |
| 365 | /* GCC reports incompatible type error without cast. GCC think poly64_t and |
| 366 | * poly64x1_t are different, that is different with MSVC and Clang. */ |
| 367 | #define MBEDTLS_VMULL_P64(a, b) vmull_p64((poly64_t) a, (poly64_t) b) |
| 368 | #else |
| 369 | /* MSVC reports `error C2440: 'type cast'` with cast. Clang does not report |
| 370 | * error with/without cast. And I think poly64_t and poly64x1_t are same, no |
| 371 | * cast for clang also. */ |
| 372 | #define MBEDTLS_VMULL_P64(a, b) vmull_p64(a, b) |
| 373 | #endif |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 374 | static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b) |
| 375 | { |
Jerry Yu | 9db4b1f | 2023-03-21 16:56:43 +0800 | [diff] [blame] | 376 | |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 377 | return vreinterpretq_u8_p128( |
Jerry Yu | 9db4b1f | 2023-03-21 16:56:43 +0800 | [diff] [blame] | 378 | MBEDTLS_VMULL_P64( |
| 379 | vget_low_p64(vreinterpretq_p64_u8(a)), |
| 380 | vget_low_p64(vreinterpretq_p64_u8(b)) |
| 381 | )); |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b) |
| 385 | { |
| 386 | return vreinterpretq_u8_p128( |
| 387 | vmull_high_p64(vreinterpretq_p64_u8(a), |
| 388 | vreinterpretq_p64_u8(b))); |
| 389 | } |
| 390 | |
Jerry Yu | f0526a9 | 2023-03-14 15:00:29 +0800 | [diff] [blame] | 391 | /* GHASH does 128b polynomial multiplication on block in GF(2^128) defined by |
Jerry Yu | 49b4367 | 2023-03-13 10:09:34 +0800 | [diff] [blame] | 392 | * `x^128 + x^7 + x^2 + x + 1`. |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 393 | * |
| 394 | * Arm64 only has 64b->128b polynomial multipliers, we need to do 4 64b |
| 395 | * multiplies to generate a 128b. |
| 396 | * |
| 397 | * `poly_mult_128` executes polynomial multiplication and outputs 256b that |
| 398 | * represented by 3 128b due to code size optimization. |
| 399 | * |
| 400 | * Output layout: |
| 401 | * | | | | |
| 402 | * |------------|-------------|-------------| |
| 403 | * | ret.val[0] | h3:h2:00:00 | high 128b | |
Jerry Yu | 8f81060 | 2023-03-14 17:28:52 +0800 | [diff] [blame] | 404 | * | ret.val[1] | :m2:m1:00 | middle 128b | |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 405 | * | ret.val[2] | : :l1:l0 | low 128b | |
| 406 | */ |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 407 | static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b) |
| 408 | { |
| 409 | uint8x16x3_t ret; |
Jerry Yu | 8f81060 | 2023-03-14 17:28:52 +0800 | [diff] [blame] | 410 | uint8x16_t h, m, l; /* retval high/middle/low */ |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 411 | uint8x16_t c, d, e; |
| 412 | |
| 413 | h = pmull_high(a, b); /* h3:h2:00:00 = a1*b1 */ |
| 414 | l = pmull_low(a, b); /* : :l1:l0 = a0*b0 */ |
| 415 | c = vextq_u8(b, b, 8); /* :c1:c0 = b0:b1 */ |
| 416 | d = pmull_high(a, c); /* :d2:d1:00 = a1*b0 */ |
| 417 | e = pmull_low(a, c); /* :e2:e1:00 = a0*b1 */ |
| 418 | m = veorq_u8(d, e); /* :m2:m1:00 = d + e */ |
| 419 | |
| 420 | ret.val[0] = h; |
| 421 | ret.val[1] = m; |
| 422 | ret.val[2] = l; |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 426 | /* |
| 427 | * Modulo reduction. |
| 428 | * |
| 429 | * See: https://www.researchgate.net/publication/285612706_Implementing_GCM_on_ARMv8 |
| 430 | * |
| 431 | * Section 4.3 |
| 432 | * |
| 433 | * Modular reduction is slightly more complex. Write the GCM modulus as f(z) = |
| 434 | * z^128 +r(z), where r(z) = z^7+z^2+z+ 1. The well known approach is to |
Jerry Yu | be4fdef | 2023-03-15 14:50:42 +0800 | [diff] [blame] | 435 | * consider that z^128 ≡r(z) (mod z^128 +r(z)), allowing us to write the 256-bit |
| 436 | * operand to be reduced as a(z) = h(z)z^128 +l(z)≡h(z)r(z) + l(z). That is, we |
| 437 | * simply multiply the higher part of the operand by r(z) and add it to l(z). If |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 438 | * the result is still larger than 128 bits, we reduce again. |
| 439 | */ |
| 440 | static inline uint8x16_t poly_mult_reduce(uint8x16x3_t input) |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 441 | { |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 442 | uint8x16_t const ZERO = vdupq_n_u8(0); |
Jerry Yu | 8b6df3f | 2023-03-21 16:59:13 +0800 | [diff] [blame] | 443 | |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 444 | uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87)); |
Jerry Yu | 8b6df3f | 2023-03-21 16:59:13 +0800 | [diff] [blame] | 445 | #if defined(__GNUC__) |
| 446 | /* use 'asm' as an optimisation barrier to prevent loading MODULO from |
| 447 | * memory. It is for GNUC compatible compilers. |
| 448 | */ |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 449 | asm ("" : "+w" (r)); |
Jerry Yu | 8b6df3f | 2023-03-21 16:59:13 +0800 | [diff] [blame] | 450 | #endif |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 451 | uint8x16_t const MODULO = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8)); |
Jerry Yu | 8f81060 | 2023-03-14 17:28:52 +0800 | [diff] [blame] | 452 | uint8x16_t h, m, l; /* input high/middle/low 128b */ |
Jerry Yu | 1ac7f6b | 2023-03-07 15:44:59 +0800 | [diff] [blame] | 453 | uint8x16_t c, d, e, f, g, n, o; |
| 454 | h = input.val[0]; /* h3:h2:00:00 */ |
| 455 | m = input.val[1]; /* :m2:m1:00 */ |
| 456 | l = input.val[2]; /* : :l1:l0 */ |
| 457 | c = pmull_high(h, MODULO); /* :c2:c1:00 = reduction of h3 */ |
| 458 | d = pmull_low(h, MODULO); /* : :d1:d0 = reduction of h2 */ |
| 459 | e = veorq_u8(c, m); /* :e2:e1:00 = m2:m1:00 + c2:c1:00 */ |
| 460 | f = pmull_high(e, MODULO); /* : :f1:f0 = reduction of e2 */ |
| 461 | g = vextq_u8(ZERO, e, 8); /* : :g1:00 = e1:00 */ |
| 462 | n = veorq_u8(d, l); /* : :n1:n0 = d1:d0 + l1:l0 */ |
| 463 | o = veorq_u8(n, f); /* o1:o0 = f1:f0 + n1:n0 */ |
| 464 | return veorq_u8(o, g); /* = o1:o0 + g1:00 */ |
Jerry Yu | df87a12 | 2023-01-10 18:17:15 +0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* |
| 468 | * GCM multiplication: c = a times b in GF(2^128) |
| 469 | */ |
| 470 | void mbedtls_aesce_gcm_mult(unsigned char c[16], |
| 471 | const unsigned char a[16], |
| 472 | const unsigned char b[16]) |
| 473 | { |
| 474 | uint8x16_t va, vb, vc; |
| 475 | va = vrbitq_u8(vld1q_u8(&a[0])); |
| 476 | vb = vrbitq_u8(vld1q_u8(&b[0])); |
| 477 | vc = vrbitq_u8(poly_mult_reduce(poly_mult_128(va, vb))); |
| 478 | vst1q_u8(&c[0], vc); |
| 479 | } |
| 480 | |
| 481 | #endif /* MBEDTLS_GCM_C */ |
Jerry Yu | 48b999c | 2023-03-03 15:51:07 +0800 | [diff] [blame] | 482 | |
| 483 | #if defined(MBEDTLS_POP_TARGET_PRAGMA) |
| 484 | #if defined(__clang__) |
| 485 | #pragma clang attribute pop |
| 486 | #elif defined(__GNUC__) |
| 487 | #pragma GCC pop_options |
| 488 | #endif |
| 489 | #undef MBEDTLS_POP_TARGET_PRAGMA |
| 490 | #endif |
| 491 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 492 | #endif /* MBEDTLS_HAVE_ARM64 */ |
| 493 | |
| 494 | #endif /* MBEDTLS_AESCE_C */ |