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