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