blob: 011c98997986b0778eac3a568f5398b2ec6e77e3 [file] [log] [blame]
Jerry Yu49231312023-01-10 16:57:21 +08001/*
Jerry Yuc8bcdc82023-02-21 14:49:02 +08002 * Arm64 crypto extension support functions
Jerry Yu49231312023-01-10 16:57:21 +08003 *
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 Yu48b999c2023-03-03 15:51:07 +080020#if defined(__aarch64__) && !defined(__ARM_FEATURE_CRYPTO) && \
Jerry Yu6f86c192023-03-13 11:03:40 +080021 defined(__clang__) && __clang_major__ >= 4
Jerry Yu48b999c2023-03-03 15:51:07 +080022/* 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 Yuae129c32023-03-03 15:55:56 +080033/* See: https://arm-software.github.io/acle/main/acle.html#cryptographic-extensions
34 *
Jerry Yu490bf082023-03-06 15:21:44 +080035 * `__ARM_FEATURE_CRYPTO` is deprecated, but we need to continue to specify it
36 * for older compilers.
Jerry Yuae129c32023-03-03 15:55:56 +080037 */
38#define __ARM_FEATURE_AES 1
Dave Rodgmandb6ab242023-03-14 16:03:57 +000039#define MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG
Jerry Yu490bf082023-03-06 15:21:44 +080040#endif
Jerry Yu48b999c2023-03-03 15:51:07 +080041
Jerry Yu49231312023-01-10 16:57:21 +080042#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
Dave Rodgmandb6ab242023-03-14 16:03:57 +000051#if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
Jerry Yuec9be842023-03-14 10:42:47 +080052# 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 Yu49231312023-01-10 16:57:21 +080067# endif
Dave Rodgmandb6ab242023-03-14 16:03:57 +000068#endif /* !__ARM_FEATURE_AES || MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */
Jerry Yu49231312023-01-10 16:57:21 +080069
Jerry Yu49231312023-01-10 16:57:21 +080070#include <arm_neon.h>
71
Jerry Yub95c7762023-01-10 16:59:51 +080072#if defined(__linux__)
73#include <asm/hwcap.h>
74#include <sys/auxv.h>
75#endif
76
77/*
78 * AES instruction support detection routine
79 */
80int 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 Yuba1e78f2023-02-24 11:18:16 +080087 /* Assume AES instructions are supported. */
Jerry Yub95c7762023-01-10 16:59:51 +080088 return 1;
89#endif
90}
91
Jerry Yu2bb3d812023-01-10 17:38:26 +080092static 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 Yuc8bcdc82023-02-21 14:49:02 +080097 /* AES AddRoundKey, SubBytes, ShiftRows (in this order).
98 * AddRoundKey adds the round key for the previous round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +080099 block = vaeseq_u8(block, vld1q_u8(keys + i * 16));
100 /* AES mix columns */
101 block = vaesmcq_u8(block);
102 }
103
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800104 /* AES AddRoundKey for the previous round.
105 * SubBytes, ShiftRows for the final round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800106 block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16));
107
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800108 /* Final round: no MixColumns */
Jerry Yu3304c202023-02-22 14:37:11 +0800109
110 /* Final AddRoundKey */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800111 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
112
113 return block;
114}
115
116static 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 Yuc8bcdc82023-02-21 14:49:02 +0800122 /* AES AddRoundKey, SubBytes, ShiftRows */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800123 block = vaesdq_u8(block, vld1q_u8(keys + i * 16));
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800124 /* 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 Yu2bb3d812023-01-10 17:38:26 +0800136 block = vaesimcq_u8(block);
137 }
138
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800139 /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
140 * last full round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800141 block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16));
142
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800143 /* Inverse AddRoundKey for inverting the initial round key addition. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800144 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
145
146 return block;
147}
148
149/*
150 * AES-ECB block en(de)cryption
151 */
152int 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 Yue096da12023-01-10 17:07:01 +0800170/*
171 * Compute decryption round keys from encryption round keys
172 */
173void 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 Yuc8bcdc82023-02-21 14:49:02 +0800188static inline uint32_t aes_rot_word(uint32_t word)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800189{
190 return (word << (32 - 8)) | (word >> 8);
191}
192
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800193static inline uint32_t aes_sub_word(uint32_t in)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800194{
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800195 uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
Jerry Yu3f2fb712023-01-10 17:05:42 +0800196 uint8x16_t zero = vdupq_n_u8(0);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800197
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 Yu3f2fb712023-01-10 17:05:42 +0800202}
203
204/*
Jerry Yubaae4012023-02-21 15:26:13 +0800205 * Key expansion function
Jerry Yu3f2fb712023-01-10 17:05:42 +0800206 */
Jerry Yubaae4012023-02-21 15:26:13 +0800207static void aesce_setkey_enc(unsigned char *rk,
208 const unsigned char *key,
209 const size_t key_bit_length)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800210{
Jerry Yubaae4012023-02-21 15:26:13 +0800211 static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
212 0x20, 0x40, 0x80, 0x1b, 0x36 };
Jerry Yu947bf962023-02-23 11:07:57 +0800213 /* 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 Yu3304c202023-02-22 14:37:11 +0800220 const size_t key_expansion_size_in_words =
Jerry Yu947bf962023-02-23 11:07:57 +0800221 round_key_len_in_words * (round_keys_needed + 1); /* Nb*(Nr+1) */
Jerry Yu3304c202023-02-22 14:37:11 +0800222 const uint32_t *rko_end = (uint32_t *) rk + key_expansion_size_in_words;
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800223
Jerry Yu3304c202023-02-22 14:37:11 +0800224 memcpy(rk, key, key_len_in_words * 4);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800225
Jerry Yu3304c202023-02-22 14:37:11 +0800226 for (uint32_t *rki = (uint32_t *) rk;
227 rki + key_len_in_words < rko_end;
228 rki += key_len_in_words) {
229
Jerry Yufac5a542023-02-23 10:13:40 +0800230 size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words;
Jerry Yu3304c202023-02-22 14:37:11 +0800231 uint32_t *rko;
Jerry Yubaae4012023-02-21 15:26:13 +0800232 rko = rki + key_len_in_words;
233 rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
Jerry Yu3304c202023-02-22 14:37:11 +0800234 rko[0] ^= rcon[iteration] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800235 rko[1] = rko[0] ^ rki[1];
236 rko[2] = rko[1] ^ rki[2];
237 rko[3] = rko[2] ^ rki[3];
Jerry Yufac5a542023-02-23 10:13:40 +0800238 if (rko + key_len_in_words > rko_end) {
Jerry Yu3304c202023-02-22 14:37:11 +0800239 /* Do not write overflow words.*/
240 continue;
241 }
Jerry Yubaae4012023-02-21 15:26:13 +0800242 switch (key_bit_length) {
Jerry Yu3304c202023-02-22 14:37:11 +0800243 case 128:
244 break;
Jerry Yubaae4012023-02-21 15:26:13 +0800245 case 192:
Jerry Yu3304c202023-02-22 14:37:11 +0800246 rko[4] = rko[3] ^ rki[4];
247 rko[5] = rko[4] ^ rki[5];
Jerry Yubaae4012023-02-21 15:26:13 +0800248 break;
249 case 256:
Jerry Yu3304c202023-02-22 14:37:11 +0800250 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 Yubaae4012023-02-21 15:26:13 +0800254 break;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800255 }
256 }
257}
258
259/*
260 * Key expansion, wrapper
261 */
262int mbedtls_aesce_setkey_enc(unsigned char *rk,
263 const unsigned char *key,
264 size_t bits)
265{
266 switch (bits) {
Jerry Yubaae4012023-02-21 15:26:13 +0800267 case 128:
268 case 192:
269 case 256:
Jerry Yuba1e78f2023-02-24 11:18:16 +0800270 aesce_setkey_enc(rk, key, bits);
271 break;
272 default:
273 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800274 }
275
276 return 0;
277}
278
Jerry Yudf87a122023-01-10 18:17:15 +0800279#if defined(MBEDTLS_GCM_C)
280
281static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b)
282{
283 return vreinterpretq_u8_p128(
284 vmull_p64(
285 (poly64_t) vget_low_p64(vreinterpretq_p64_u8(a)),
286 (poly64_t) vget_low_p64(vreinterpretq_p64_u8(b))));
287}
288
289static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b)
290{
291 return vreinterpretq_u8_p128(
292 vmull_high_p64(vreinterpretq_p64_u8(a),
293 vreinterpretq_p64_u8(b)));
294}
295
296static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b)
297{
298 uint8x16x3_t ret;
299 uint8x16_t c = vextq_u8(b, b, 8);
300 ret.val[0] = pmull_high(a, b); /* a1*b1 */
301 ret.val[1] = veorq_u8(pmull_high(a, c), /* a1*b0 + a0*b1 */
302 pmull_low(a, c));
303 ret.val[2] = pmull_low(a, b); /* a0*b0 */
304 return ret;
305}
306
307static inline uint8x16_t poly_mult_reduce(uint8x16x3_t a)
308{
309 uint8x16_t const Z = vdupq_n_u8(0);
310 /* use 'asm' as an optimisation barrier to prevent loading R from memory */
311 uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87));
312 asm ("" : "+w" (r));
313 uint8x16_t const R = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8));
314 uint8x16_t d = a.val[0]; /* d3:d2:00:00 */
315 uint8x16_t j = a.val[1]; /* j2:j1:00 */
316 uint8x16_t g = a.val[2]; /* g1:g0 = a0*b0 */
317 uint8x16_t h = pmull_high(d, R); /* h2:h1:00 = reduction of d3 */
318 uint8x16_t i = pmull_low(d, R); /* i1:i0 = reduction of d2 */
319 uint8x16_t k = veorq_u8(j, h); /* k2:k1:00 = j2:j1 + h2:h1 */
320 uint8x16_t l = pmull_high(k, R); /* l1:l0 = reduction of k2 */
321 uint8x16_t m = vextq_u8(Z, k, 8); /* m1:00 = k1:00 */
322 uint8x16_t n = veorq_u8(g, i); /* n1:n0 = g1:g0 + i1:i0 */
323 uint8x16_t o = veorq_u8(n, l); /* o1:o0 = l1:l0 + n1:n0 */
324 return veorq_u8(o, m); /* = o1:o0 + m1:00 */
325}
326
327/*
328 * GCM multiplication: c = a times b in GF(2^128)
329 */
330void mbedtls_aesce_gcm_mult(unsigned char c[16],
331 const unsigned char a[16],
332 const unsigned char b[16])
333{
334 uint8x16_t va, vb, vc;
335 va = vrbitq_u8(vld1q_u8(&a[0]));
336 vb = vrbitq_u8(vld1q_u8(&b[0]));
337 vc = vrbitq_u8(poly_mult_reduce(poly_mult_128(va, vb)));
338 vst1q_u8(&c[0], vc);
339}
340
341#endif /* MBEDTLS_GCM_C */
Jerry Yu48b999c2023-03-03 15:51:07 +0800342
343#if defined(MBEDTLS_POP_TARGET_PRAGMA)
344#if defined(__clang__)
345#pragma clang attribute pop
346#elif defined(__GNUC__)
347#pragma GCC pop_options
348#endif
349#undef MBEDTLS_POP_TARGET_PRAGMA
350#endif
351
Jerry Yu49231312023-01-10 16:57:21 +0800352#endif /* MBEDTLS_HAVE_ARM64 */
353
354#endif /* MBEDTLS_AESCE_C */