Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 1 | /* |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 2 | * Arm64 crypto extension support functions |
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 |
Jerry Yu | b28d55b | 2023-03-14 10:36:47 +0800 | [diff] [blame] | 39 | #define MBEDTLS_NEED_TARGET_OPTIONS |
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 | ec9be84 | 2023-03-14 10:42:47 +0800 | [diff] [blame^] | 51 | #if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_NEED_TARGET_OPTIONS) |
| 52 | # if defined(__clang__) |
| 53 | # if __clang_major__ < 4 |
| 54 | # error "A more recent Clang is required for MBEDTLS_AESCE_C" |
| 55 | # endif |
| 56 | # pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function) |
| 57 | # define MBEDTLS_POP_TARGET_PRAGMA |
| 58 | # elif defined(__GNUC__) |
| 59 | # if __GNUC__ < 6 |
| 60 | # error "A more recent GCC is required for MBEDTLS_AESCE_C" |
| 61 | # endif |
| 62 | # pragma GCC push_options |
| 63 | # pragma GCC target ("arch=armv8-a+crypto") |
| 64 | # define MBEDTLS_POP_TARGET_PRAGMA |
| 65 | # else |
| 66 | # error "Only GCC and Clang supported for MBEDTLS_AESCE_C" |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 67 | # endif |
Jerry Yu | ec9be84 | 2023-03-14 10:42:47 +0800 | [diff] [blame^] | 68 | #endif /* !__ARM_FEATURE_AES || MBEDTLS_NEED_TARGET_OPTIONS */ |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 69 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 70 | #include <arm_neon.h> |
| 71 | |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 72 | #if defined(__linux__) |
| 73 | #include <asm/hwcap.h> |
| 74 | #include <sys/auxv.h> |
| 75 | #endif |
| 76 | |
| 77 | /* |
| 78 | * AES instruction support detection routine |
| 79 | */ |
| 80 | int mbedtls_aesce_has_support(void) |
| 81 | { |
| 82 | #if defined(__linux__) |
| 83 | unsigned long auxval = getauxval(AT_HWCAP); |
| 84 | return (auxval & (HWCAP_ASIMD | HWCAP_AES)) == |
| 85 | (HWCAP_ASIMD | HWCAP_AES); |
| 86 | #else |
Jerry Yu | ba1e78f | 2023-02-24 11:18:16 +0800 | [diff] [blame] | 87 | /* Assume AES instructions are supported. */ |
Jerry Yu | b95c776 | 2023-01-10 16:59:51 +0800 | [diff] [blame] | 88 | return 1; |
| 89 | #endif |
| 90 | } |
| 91 | |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 92 | static uint8x16_t aesce_encrypt_block(uint8x16_t block, |
| 93 | unsigned char *keys, |
| 94 | int rounds) |
| 95 | { |
| 96 | for (int i = 0; i < rounds - 1; i++) { |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 97 | /* AES AddRoundKey, SubBytes, ShiftRows (in this order). |
| 98 | * AddRoundKey adds the round key for the previous round. */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 99 | block = vaeseq_u8(block, vld1q_u8(keys + i * 16)); |
| 100 | /* AES mix columns */ |
| 101 | block = vaesmcq_u8(block); |
| 102 | } |
| 103 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 104 | /* AES AddRoundKey for the previous round. |
| 105 | * SubBytes, ShiftRows for the final round. */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 106 | block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16)); |
| 107 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 108 | /* Final round: no MixColumns */ |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 109 | |
| 110 | /* Final AddRoundKey */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 111 | block = veorq_u8(block, vld1q_u8(keys + rounds * 16)); |
| 112 | |
| 113 | return block; |
| 114 | } |
| 115 | |
| 116 | static uint8x16_t aesce_decrypt_block(uint8x16_t block, |
| 117 | unsigned char *keys, |
| 118 | int rounds) |
| 119 | { |
| 120 | |
| 121 | for (int i = 0; i < rounds - 1; i++) { |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 122 | /* AES AddRoundKey, SubBytes, ShiftRows */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 123 | block = vaesdq_u8(block, vld1q_u8(keys + i * 16)); |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 124 | /* AES inverse MixColumns for the next round. |
| 125 | * |
| 126 | * This means that we switch the order of the inverse AddRoundKey and |
| 127 | * inverse MixColumns operations. We have to do this as AddRoundKey is |
| 128 | * done in an atomic instruction together with the inverses of SubBytes |
| 129 | * and ShiftRows. |
| 130 | * |
| 131 | * It works because MixColumns is a linear operation over GF(2^8) and |
| 132 | * AddRoundKey is an exclusive or, which is equivalent to addition over |
| 133 | * GF(2^8). (The inverse of MixColumns needs to be applied to the |
| 134 | * affected round keys separately which has been done when the |
| 135 | * decryption round keys were calculated.) */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 136 | block = vaesimcq_u8(block); |
| 137 | } |
| 138 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 139 | /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the |
| 140 | * last full round. */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 141 | block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16)); |
| 142 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 143 | /* Inverse AddRoundKey for inverting the initial round key addition. */ |
Jerry Yu | 2bb3d81 | 2023-01-10 17:38:26 +0800 | [diff] [blame] | 144 | block = veorq_u8(block, vld1q_u8(keys + rounds * 16)); |
| 145 | |
| 146 | return block; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * AES-ECB block en(de)cryption |
| 151 | */ |
| 152 | int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx, |
| 153 | int mode, |
| 154 | const unsigned char input[16], |
| 155 | unsigned char output[16]) |
| 156 | { |
| 157 | uint8x16_t block = vld1q_u8(&input[0]); |
| 158 | unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset); |
| 159 | |
| 160 | if (mode == MBEDTLS_AES_ENCRYPT) { |
| 161 | block = aesce_encrypt_block(block, keys, ctx->nr); |
| 162 | } else { |
| 163 | block = aesce_decrypt_block(block, keys, ctx->nr); |
| 164 | } |
| 165 | vst1q_u8(&output[0], block); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Jerry Yu | e096da1 | 2023-01-10 17:07:01 +0800 | [diff] [blame] | 170 | /* |
| 171 | * Compute decryption round keys from encryption round keys |
| 172 | */ |
| 173 | void mbedtls_aesce_inverse_key(unsigned char *invkey, |
| 174 | const unsigned char *fwdkey, |
| 175 | int nr) |
| 176 | { |
| 177 | int i, j; |
| 178 | j = nr; |
| 179 | vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16)); |
| 180 | for (i = 1, j--; j > 0; i++, j--) { |
| 181 | vst1q_u8(invkey + i * 16, |
| 182 | vaesimcq_u8(vld1q_u8(fwdkey + j * 16))); |
| 183 | } |
| 184 | vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16)); |
| 185 | |
| 186 | } |
| 187 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 188 | static inline uint32_t aes_rot_word(uint32_t word) |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 189 | { |
| 190 | return (word << (32 - 8)) | (word >> 8); |
| 191 | } |
| 192 | |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 193 | static inline uint32_t aes_sub_word(uint32_t in) |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 194 | { |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 195 | uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in)); |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 196 | uint8x16_t zero = vdupq_n_u8(0); |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 197 | |
| 198 | /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields |
| 199 | * the correct result as ShiftRows doesn't change the first row. */ |
| 200 | v = vaeseq_u8(zero, v); |
| 201 | return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0); |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 205 | * Key expansion function |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 206 | */ |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 207 | static void aesce_setkey_enc(unsigned char *rk, |
| 208 | const unsigned char *key, |
| 209 | const size_t key_bit_length) |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 210 | { |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 211 | static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10, |
| 212 | 0x20, 0x40, 0x80, 0x1b, 0x36 }; |
Jerry Yu | 947bf96 | 2023-02-23 11:07:57 +0800 | [diff] [blame] | 213 | /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf |
| 214 | * - Section 5, Nr = Nk + 6 |
| 215 | * - Section 5.2, the key expansion size is Nb*(Nr+1) |
| 216 | */ |
| 217 | const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */ |
| 218 | const size_t round_key_len_in_words = 4; /* Nb */ |
| 219 | const size_t round_keys_needed = key_len_in_words + 6; /* Nr */ |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 220 | const size_t key_expansion_size_in_words = |
Jerry Yu | 947bf96 | 2023-02-23 11:07:57 +0800 | [diff] [blame] | 221 | round_key_len_in_words * (round_keys_needed + 1); /* Nb*(Nr+1) */ |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 222 | const uint32_t *rko_end = (uint32_t *) rk + key_expansion_size_in_words; |
Jerry Yu | c8bcdc8 | 2023-02-21 14:49:02 +0800 | [diff] [blame] | 223 | |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 224 | memcpy(rk, key, key_len_in_words * 4); |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 225 | |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 226 | for (uint32_t *rki = (uint32_t *) rk; |
| 227 | rki + key_len_in_words < rko_end; |
| 228 | rki += key_len_in_words) { |
| 229 | |
Jerry Yu | fac5a54 | 2023-02-23 10:13:40 +0800 | [diff] [blame] | 230 | size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words; |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 231 | uint32_t *rko; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 232 | rko = rki + key_len_in_words; |
| 233 | 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] | 234 | rko[0] ^= rcon[iteration] ^ rki[0]; |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 235 | rko[1] = rko[0] ^ rki[1]; |
| 236 | rko[2] = rko[1] ^ rki[2]; |
| 237 | rko[3] = rko[2] ^ rki[3]; |
Jerry Yu | fac5a54 | 2023-02-23 10:13:40 +0800 | [diff] [blame] | 238 | if (rko + key_len_in_words > rko_end) { |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 239 | /* Do not write overflow words.*/ |
| 240 | continue; |
| 241 | } |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 242 | switch (key_bit_length) { |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 243 | case 128: |
| 244 | break; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 245 | case 192: |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 246 | rko[4] = rko[3] ^ rki[4]; |
| 247 | rko[5] = rko[4] ^ rki[5]; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 248 | break; |
| 249 | case 256: |
Jerry Yu | 3304c20 | 2023-02-22 14:37:11 +0800 | [diff] [blame] | 250 | rko[4] = aes_sub_word(rko[3]) ^ rki[4]; |
| 251 | rko[5] = rko[4] ^ rki[5]; |
| 252 | rko[6] = rko[5] ^ rki[6]; |
| 253 | rko[7] = rko[6] ^ rki[7]; |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 254 | break; |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Key expansion, wrapper |
| 261 | */ |
| 262 | int mbedtls_aesce_setkey_enc(unsigned char *rk, |
| 263 | const unsigned char *key, |
| 264 | size_t bits) |
| 265 | { |
| 266 | switch (bits) { |
Jerry Yu | baae401 | 2023-02-21 15:26:13 +0800 | [diff] [blame] | 267 | case 128: |
| 268 | case 192: |
| 269 | case 256: |
Jerry Yu | ba1e78f | 2023-02-24 11:18:16 +0800 | [diff] [blame] | 270 | aesce_setkey_enc(rk, key, bits); |
| 271 | break; |
| 272 | default: |
| 273 | return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH; |
Jerry Yu | 3f2fb71 | 2023-01-10 17:05:42 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Jerry Yu | 48b999c | 2023-03-03 15:51:07 +0800 | [diff] [blame] | 279 | |
| 280 | #if defined(MBEDTLS_POP_TARGET_PRAGMA) |
| 281 | #if defined(__clang__) |
| 282 | #pragma clang attribute pop |
| 283 | #elif defined(__GNUC__) |
| 284 | #pragma GCC pop_options |
| 285 | #endif |
| 286 | #undef MBEDTLS_POP_TARGET_PRAGMA |
| 287 | #endif |
| 288 | |
Jerry Yu | 4923131 | 2023-01-10 16:57:21 +0800 | [diff] [blame] | 289 | #endif /* MBEDTLS_HAVE_ARM64 */ |
| 290 | |
| 291 | #endif /* MBEDTLS_AESCE_C */ |