blob: f0ca274df05ef20199124068cc5d5ea08ee07e78 [file] [log] [blame]
Jerry Yu49231312023-01-10 16:57:21 +08001/*
Dave Rodgmanf918d422023-03-17 17:52:23 +00002 * Armv8-A Cryptographic Extension support functions for Aarch64
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
Jerry Yu07d28d82023-03-20 18:12:36 +080065# elif defined(_MSC_VER)
Jerry Yuec9be842023-03-14 10:42:47 +080066# else
Jerry Yu07d28d82023-03-20 18:12:36 +080067# error "Only MSVC, GCC and Clang supported for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080068# endif
Dave Rodgmandb6ab242023-03-14 16:03:57 +000069#endif /* !__ARM_FEATURE_AES || MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */
Jerry Yu49231312023-01-10 16:57:21 +080070
Jerry Yu49231312023-01-10 16:57:21 +080071#include <arm_neon.h>
72
Jerry Yub95c7762023-01-10 16:59:51 +080073#if defined(__linux__)
74#include <asm/hwcap.h>
75#include <sys/auxv.h>
76#endif
77
78/*
79 * AES instruction support detection routine
80 */
81int mbedtls_aesce_has_support(void)
82{
83#if defined(__linux__)
84 unsigned long auxval = getauxval(AT_HWCAP);
85 return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
86 (HWCAP_ASIMD | HWCAP_AES);
87#else
Jerry Yuba1e78f2023-02-24 11:18:16 +080088 /* Assume AES instructions are supported. */
Jerry Yub95c7762023-01-10 16:59:51 +080089 return 1;
90#endif
91}
92
Jerry Yu2bb3d812023-01-10 17:38:26 +080093static uint8x16_t aesce_encrypt_block(uint8x16_t block,
94 unsigned char *keys,
95 int rounds)
96{
97 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +080098 /* AES AddRoundKey, SubBytes, ShiftRows (in this order).
99 * AddRoundKey adds the round key for the previous round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800100 block = vaeseq_u8(block, vld1q_u8(keys + i * 16));
101 /* AES mix columns */
102 block = vaesmcq_u8(block);
103 }
104
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800105 /* AES AddRoundKey for the previous round.
106 * SubBytes, ShiftRows for the final round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800107 block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16));
108
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800109 /* Final round: no MixColumns */
Jerry Yu3304c202023-02-22 14:37:11 +0800110
111 /* Final AddRoundKey */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800112 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
113
114 return block;
115}
116
117static uint8x16_t aesce_decrypt_block(uint8x16_t block,
118 unsigned char *keys,
119 int rounds)
120{
121
122 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800123 /* AES AddRoundKey, SubBytes, ShiftRows */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800124 block = vaesdq_u8(block, vld1q_u8(keys + i * 16));
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800125 /* AES inverse MixColumns for the next round.
126 *
127 * This means that we switch the order of the inverse AddRoundKey and
128 * inverse MixColumns operations. We have to do this as AddRoundKey is
129 * done in an atomic instruction together with the inverses of SubBytes
130 * and ShiftRows.
131 *
132 * It works because MixColumns is a linear operation over GF(2^8) and
133 * AddRoundKey is an exclusive or, which is equivalent to addition over
134 * GF(2^8). (The inverse of MixColumns needs to be applied to the
135 * affected round keys separately which has been done when the
136 * decryption round keys were calculated.) */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800137 block = vaesimcq_u8(block);
138 }
139
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800140 /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
141 * last full round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800142 block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16));
143
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800144 /* Inverse AddRoundKey for inverting the initial round key addition. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800145 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
146
147 return block;
148}
149
150/*
151 * AES-ECB block en(de)cryption
152 */
153int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
154 int mode,
155 const unsigned char input[16],
156 unsigned char output[16])
157{
158 uint8x16_t block = vld1q_u8(&input[0]);
159 unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
160
161 if (mode == MBEDTLS_AES_ENCRYPT) {
162 block = aesce_encrypt_block(block, keys, ctx->nr);
163 } else {
164 block = aesce_decrypt_block(block, keys, ctx->nr);
165 }
166 vst1q_u8(&output[0], block);
167
168 return 0;
169}
170
Jerry Yue096da12023-01-10 17:07:01 +0800171/*
172 * Compute decryption round keys from encryption round keys
173 */
174void mbedtls_aesce_inverse_key(unsigned char *invkey,
175 const unsigned char *fwdkey,
176 int nr)
177{
178 int i, j;
179 j = nr;
180 vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
181 for (i = 1, j--; j > 0; i++, j--) {
182 vst1q_u8(invkey + i * 16,
183 vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
184 }
185 vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
186
187}
188
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800189static inline uint32_t aes_rot_word(uint32_t word)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800190{
191 return (word << (32 - 8)) | (word >> 8);
192}
193
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800194static inline uint32_t aes_sub_word(uint32_t in)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800195{
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800196 uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
Jerry Yu3f2fb712023-01-10 17:05:42 +0800197 uint8x16_t zero = vdupq_n_u8(0);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800198
199 /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
200 * the correct result as ShiftRows doesn't change the first row. */
201 v = vaeseq_u8(zero, v);
202 return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800203}
204
205/*
Jerry Yubaae4012023-02-21 15:26:13 +0800206 * Key expansion function
Jerry Yu3f2fb712023-01-10 17:05:42 +0800207 */
Jerry Yubaae4012023-02-21 15:26:13 +0800208static void aesce_setkey_enc(unsigned char *rk,
209 const unsigned char *key,
210 const size_t key_bit_length)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800211{
Jerry Yubaae4012023-02-21 15:26:13 +0800212 static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
213 0x20, 0x40, 0x80, 0x1b, 0x36 };
Jerry Yu947bf962023-02-23 11:07:57 +0800214 /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
215 * - Section 5, Nr = Nk + 6
Jerry Yu2c266512023-03-01 11:18:20 +0800216 * - Section 5.2, the length of round keys is Nb*(Nr+1)
Jerry Yu947bf962023-02-23 11:07:57 +0800217 */
218 const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */
219 const size_t round_key_len_in_words = 4; /* Nb */
Jerry Yu2c266512023-03-01 11:18:20 +0800220 const size_t rounds_needed = key_len_in_words + 6; /* Nr */
221 const size_t round_keys_len_in_words =
222 round_key_len_in_words * (rounds_needed + 1); /* Nb*(Nr+1) */
223 const uint32_t *rko_end = (uint32_t *) rk + round_keys_len_in_words;
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800224
Jerry Yu3304c202023-02-22 14:37:11 +0800225 memcpy(rk, key, key_len_in_words * 4);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800226
Jerry Yu3304c202023-02-22 14:37:11 +0800227 for (uint32_t *rki = (uint32_t *) rk;
228 rki + key_len_in_words < rko_end;
229 rki += key_len_in_words) {
230
Jerry Yufac5a542023-02-23 10:13:40 +0800231 size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words;
Jerry Yu3304c202023-02-22 14:37:11 +0800232 uint32_t *rko;
Jerry Yubaae4012023-02-21 15:26:13 +0800233 rko = rki + key_len_in_words;
234 rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
Jerry Yu3304c202023-02-22 14:37:11 +0800235 rko[0] ^= rcon[iteration] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800236 rko[1] = rko[0] ^ rki[1];
237 rko[2] = rko[1] ^ rki[2];
238 rko[3] = rko[2] ^ rki[3];
Jerry Yufac5a542023-02-23 10:13:40 +0800239 if (rko + key_len_in_words > rko_end) {
Jerry Yu3304c202023-02-22 14:37:11 +0800240 /* Do not write overflow words.*/
241 continue;
242 }
Jerry Yubaae4012023-02-21 15:26:13 +0800243 switch (key_bit_length) {
Jerry Yu3304c202023-02-22 14:37:11 +0800244 case 128:
245 break;
Jerry Yubaae4012023-02-21 15:26:13 +0800246 case 192:
Jerry Yu3304c202023-02-22 14:37:11 +0800247 rko[4] = rko[3] ^ rki[4];
248 rko[5] = rko[4] ^ rki[5];
Jerry Yubaae4012023-02-21 15:26:13 +0800249 break;
250 case 256:
Jerry Yu3304c202023-02-22 14:37:11 +0800251 rko[4] = aes_sub_word(rko[3]) ^ rki[4];
252 rko[5] = rko[4] ^ rki[5];
253 rko[6] = rko[5] ^ rki[6];
254 rko[7] = rko[6] ^ rki[7];
Jerry Yubaae4012023-02-21 15:26:13 +0800255 break;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800256 }
257 }
258}
259
260/*
261 * Key expansion, wrapper
262 */
263int mbedtls_aesce_setkey_enc(unsigned char *rk,
264 const unsigned char *key,
265 size_t bits)
266{
267 switch (bits) {
Jerry Yubaae4012023-02-21 15:26:13 +0800268 case 128:
269 case 192:
270 case 256:
Jerry Yuba1e78f2023-02-24 11:18:16 +0800271 aesce_setkey_enc(rk, key, bits);
272 break;
273 default:
274 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800275 }
276
277 return 0;
278}
279
Jerry Yudf87a122023-01-10 18:17:15 +0800280#if defined(MBEDTLS_GCM_C)
281
Jerry Yu132d0cb2023-03-02 17:35:53 +0800282#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ == 5
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800283/* Some intrinsics are not available for GCC 5.X. */
Jerry Yu132d0cb2023-03-02 17:35:53 +0800284#define vreinterpretq_p64_u8(a) ((poly64x2_t) a)
285#define vreinterpretq_u8_p128(a) ((uint8x16_t) a)
286static inline poly64_t vget_low_p64(poly64x2_t __a)
287{
288 uint64x2_t tmp = (uint64x2_t) (__a);
289 uint64x1_t lo = vcreate_u64(vgetq_lane_u64(tmp, 0));
290 return (poly64_t) (lo);
291}
292#endif /* !__clang__ && __GNUC__ && __GNUC__ == 5*/
293
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800294/* vmull_p64/vmull_high_p64 wrappers.
295 *
296 * Older compilers miss some intrinsic functions for `poly*_t`. We use
297 * uint8x16_t and uint8x16x3_t as input/output parameters.
298 */
Jerry Yudf87a122023-01-10 18:17:15 +0800299static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b)
300{
301 return vreinterpretq_u8_p128(
302 vmull_p64(
303 (poly64_t) vget_low_p64(vreinterpretq_p64_u8(a)),
304 (poly64_t) vget_low_p64(vreinterpretq_p64_u8(b))));
305}
306
307static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b)
308{
309 return vreinterpretq_u8_p128(
310 vmull_high_p64(vreinterpretq_p64_u8(a),
311 vreinterpretq_p64_u8(b)));
312}
313
Jerry Yuf0526a92023-03-14 15:00:29 +0800314/* GHASH does 128b polynomial multiplication on block in GF(2^128) defined by
Jerry Yu49b43672023-03-13 10:09:34 +0800315 * `x^128 + x^7 + x^2 + x + 1`.
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800316 *
317 * Arm64 only has 64b->128b polynomial multipliers, we need to do 4 64b
318 * multiplies to generate a 128b.
319 *
320 * `poly_mult_128` executes polynomial multiplication and outputs 256b that
321 * represented by 3 128b due to code size optimization.
322 *
323 * Output layout:
324 * | | | |
325 * |------------|-------------|-------------|
326 * | ret.val[0] | h3:h2:00:00 | high 128b |
Jerry Yu8f810602023-03-14 17:28:52 +0800327 * | ret.val[1] | :m2:m1:00 | middle 128b |
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800328 * | ret.val[2] | : :l1:l0 | low 128b |
329 */
Jerry Yudf87a122023-01-10 18:17:15 +0800330static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b)
331{
332 uint8x16x3_t ret;
Jerry Yu8f810602023-03-14 17:28:52 +0800333 uint8x16_t h, m, l; /* retval high/middle/low */
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800334 uint8x16_t c, d, e;
335
336 h = pmull_high(a, b); /* h3:h2:00:00 = a1*b1 */
337 l = pmull_low(a, b); /* : :l1:l0 = a0*b0 */
338 c = vextq_u8(b, b, 8); /* :c1:c0 = b0:b1 */
339 d = pmull_high(a, c); /* :d2:d1:00 = a1*b0 */
340 e = pmull_low(a, c); /* :e2:e1:00 = a0*b1 */
341 m = veorq_u8(d, e); /* :m2:m1:00 = d + e */
342
343 ret.val[0] = h;
344 ret.val[1] = m;
345 ret.val[2] = l;
Jerry Yudf87a122023-01-10 18:17:15 +0800346 return ret;
347}
348
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800349/*
350 * Modulo reduction.
351 *
352 * See: https://www.researchgate.net/publication/285612706_Implementing_GCM_on_ARMv8
353 *
354 * Section 4.3
355 *
356 * Modular reduction is slightly more complex. Write the GCM modulus as f(z) =
357 * z^128 +r(z), where r(z) = z^7+z^2+z+ 1. The well known approach is to
Jerry Yube4fdef2023-03-15 14:50:42 +0800358 * consider that z^128 ≡r(z) (mod z^128 +r(z)), allowing us to write the 256-bit
359 * operand to be reduced as a(z) = h(z)z^128 +l(z)≡h(z)r(z) + l(z). That is, we
360 * simply multiply the higher part of the operand by r(z) and add it to l(z). If
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800361 * the result is still larger than 128 bits, we reduce again.
362 */
363static inline uint8x16_t poly_mult_reduce(uint8x16x3_t input)
Jerry Yudf87a122023-01-10 18:17:15 +0800364{
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800365 uint8x16_t const ZERO = vdupq_n_u8(0);
366 /* use 'asm' as an optimisation barrier to prevent loading MODULO from memory */
Jerry Yudf87a122023-01-10 18:17:15 +0800367 uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87));
368 asm ("" : "+w" (r));
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800369 uint8x16_t const MODULO = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8));
Jerry Yu8f810602023-03-14 17:28:52 +0800370 uint8x16_t h, m, l; /* input high/middle/low 128b */
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800371 uint8x16_t c, d, e, f, g, n, o;
372 h = input.val[0]; /* h3:h2:00:00 */
373 m = input.val[1]; /* :m2:m1:00 */
374 l = input.val[2]; /* : :l1:l0 */
375 c = pmull_high(h, MODULO); /* :c2:c1:00 = reduction of h3 */
376 d = pmull_low(h, MODULO); /* : :d1:d0 = reduction of h2 */
377 e = veorq_u8(c, m); /* :e2:e1:00 = m2:m1:00 + c2:c1:00 */
378 f = pmull_high(e, MODULO); /* : :f1:f0 = reduction of e2 */
379 g = vextq_u8(ZERO, e, 8); /* : :g1:00 = e1:00 */
380 n = veorq_u8(d, l); /* : :n1:n0 = d1:d0 + l1:l0 */
381 o = veorq_u8(n, f); /* o1:o0 = f1:f0 + n1:n0 */
382 return veorq_u8(o, g); /* = o1:o0 + g1:00 */
Jerry Yudf87a122023-01-10 18:17:15 +0800383}
384
385/*
386 * GCM multiplication: c = a times b in GF(2^128)
387 */
388void mbedtls_aesce_gcm_mult(unsigned char c[16],
389 const unsigned char a[16],
390 const unsigned char b[16])
391{
392 uint8x16_t va, vb, vc;
393 va = vrbitq_u8(vld1q_u8(&a[0]));
394 vb = vrbitq_u8(vld1q_u8(&b[0]));
395 vc = vrbitq_u8(poly_mult_reduce(poly_mult_128(va, vb)));
396 vst1q_u8(&c[0], vc);
397}
398
399#endif /* MBEDTLS_GCM_C */
Jerry Yu48b999c2023-03-03 15:51:07 +0800400
401#if defined(MBEDTLS_POP_TARGET_PRAGMA)
402#if defined(__clang__)
403#pragma clang attribute pop
404#elif defined(__GNUC__)
405#pragma GCC pop_options
406#endif
407#undef MBEDTLS_POP_TARGET_PRAGMA
408#endif
409
Jerry Yu49231312023-01-10 16:57:21 +0800410#endif /* MBEDTLS_HAVE_ARM64 */
411
412#endif /* MBEDTLS_AESCE_C */