blob: 4cbe8b02299766ed0cc5dcc86f454abcbdfd9e38 [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
Jerry Yu61c4cfa2023-04-26 11:06:51 +080051/* Compiler version checks. */
52#if defined(__clang__) && (__clang_major__ < 4)
53# error "Minimum version of Clang for MBEDTLS_AESCE_C is 4.0."
54#elif defined(__GNUC__) && (__GNUC__ < 6)
55# error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0."
56#elif defined(_MSC_VER) && (_MSC_VER < 1929)
57/* TODO: We haven't verified MSVC from 1920 to 1928. If someone verified that,
58 * please update this and document of `MBEDTLS_AESCE_C` in
59 * `mbedtls_config.h`. */
60# error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2."
61#endif
62
Dave Rodgmandb6ab242023-03-14 16:03:57 +000063#if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
Jerry Yuec9be842023-03-14 10:42:47 +080064# if defined(__clang__)
Jerry Yuec9be842023-03-14 10:42:47 +080065# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
66# define MBEDTLS_POP_TARGET_PRAGMA
67# elif defined(__GNUC__)
Jerry Yuec9be842023-03-14 10:42:47 +080068# pragma GCC push_options
69# pragma GCC target ("arch=armv8-a+crypto")
70# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu07d28d82023-03-20 18:12:36 +080071# elif defined(_MSC_VER)
Jerry Yu61c4cfa2023-04-26 11:06:51 +080072# error "Required feature(__ARM_FEATURE_AES) is not enabled."
Jerry Yu49231312023-01-10 16:57:21 +080073# endif
Dave Rodgmandb6ab242023-03-14 16:03:57 +000074#endif /* !__ARM_FEATURE_AES || MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */
Jerry Yu49231312023-01-10 16:57:21 +080075
Jerry Yu49231312023-01-10 16:57:21 +080076#include <arm_neon.h>
77
Jerry Yub95c7762023-01-10 16:59:51 +080078#if defined(__linux__)
79#include <asm/hwcap.h>
80#include <sys/auxv.h>
81#endif
82
83/*
84 * AES instruction support detection routine
85 */
86int mbedtls_aesce_has_support(void)
87{
88#if defined(__linux__)
89 unsigned long auxval = getauxval(AT_HWCAP);
90 return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
91 (HWCAP_ASIMD | HWCAP_AES);
92#else
Jerry Yuba1e78f2023-02-24 11:18:16 +080093 /* Assume AES instructions are supported. */
Jerry Yub95c7762023-01-10 16:59:51 +080094 return 1;
95#endif
96}
97
Jerry Yu2bb3d812023-01-10 17:38:26 +080098static uint8x16_t aesce_encrypt_block(uint8x16_t block,
99 unsigned char *keys,
100 int rounds)
101{
102 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800103 /* AES AddRoundKey, SubBytes, ShiftRows (in this order).
104 * AddRoundKey adds the round key for the previous round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800105 block = vaeseq_u8(block, vld1q_u8(keys + i * 16));
106 /* AES mix columns */
107 block = vaesmcq_u8(block);
108 }
109
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800110 /* AES AddRoundKey for the previous round.
111 * SubBytes, ShiftRows for the final round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800112 block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16));
113
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800114 /* Final round: no MixColumns */
Jerry Yu3304c202023-02-22 14:37:11 +0800115
116 /* Final AddRoundKey */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800117 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
118
119 return block;
120}
121
122static uint8x16_t aesce_decrypt_block(uint8x16_t block,
123 unsigned char *keys,
124 int rounds)
125{
126
127 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800128 /* AES AddRoundKey, SubBytes, ShiftRows */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800129 block = vaesdq_u8(block, vld1q_u8(keys + i * 16));
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800130 /* AES inverse MixColumns for the next round.
131 *
132 * This means that we switch the order of the inverse AddRoundKey and
133 * inverse MixColumns operations. We have to do this as AddRoundKey is
134 * done in an atomic instruction together with the inverses of SubBytes
135 * and ShiftRows.
136 *
137 * It works because MixColumns is a linear operation over GF(2^8) and
138 * AddRoundKey is an exclusive or, which is equivalent to addition over
139 * GF(2^8). (The inverse of MixColumns needs to be applied to the
140 * affected round keys separately which has been done when the
141 * decryption round keys were calculated.) */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800142 block = vaesimcq_u8(block);
143 }
144
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800145 /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
146 * last full round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800147 block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16));
148
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800149 /* Inverse AddRoundKey for inverting the initial round key addition. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800150 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
151
152 return block;
153}
154
155/*
156 * AES-ECB block en(de)cryption
157 */
158int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
159 int mode,
160 const unsigned char input[16],
161 unsigned char output[16])
162{
163 uint8x16_t block = vld1q_u8(&input[0]);
164 unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
165
166 if (mode == MBEDTLS_AES_ENCRYPT) {
167 block = aesce_encrypt_block(block, keys, ctx->nr);
168 } else {
169 block = aesce_decrypt_block(block, keys, ctx->nr);
170 }
171 vst1q_u8(&output[0], block);
172
173 return 0;
174}
175
Jerry Yue096da12023-01-10 17:07:01 +0800176/*
177 * Compute decryption round keys from encryption round keys
178 */
179void mbedtls_aesce_inverse_key(unsigned char *invkey,
180 const unsigned char *fwdkey,
181 int nr)
182{
183 int i, j;
184 j = nr;
185 vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
186 for (i = 1, j--; j > 0; i++, j--) {
187 vst1q_u8(invkey + i * 16,
188 vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
189 }
190 vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
191
192}
193
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800194static inline uint32_t aes_rot_word(uint32_t word)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800195{
196 return (word << (32 - 8)) | (word >> 8);
197}
198
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800199static inline uint32_t aes_sub_word(uint32_t in)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800200{
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800201 uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
Jerry Yu3f2fb712023-01-10 17:05:42 +0800202 uint8x16_t zero = vdupq_n_u8(0);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800203
204 /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
205 * the correct result as ShiftRows doesn't change the first row. */
206 v = vaeseq_u8(zero, v);
207 return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800208}
209
210/*
Jerry Yubaae4012023-02-21 15:26:13 +0800211 * Key expansion function
Jerry Yu3f2fb712023-01-10 17:05:42 +0800212 */
Jerry Yubaae4012023-02-21 15:26:13 +0800213static void aesce_setkey_enc(unsigned char *rk,
214 const unsigned char *key,
215 const size_t key_bit_length)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800216{
Jerry Yubaae4012023-02-21 15:26:13 +0800217 static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
218 0x20, 0x40, 0x80, 0x1b, 0x36 };
Jerry Yu947bf962023-02-23 11:07:57 +0800219 /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
220 * - Section 5, Nr = Nk + 6
Jerry Yu2c266512023-03-01 11:18:20 +0800221 * - Section 5.2, the length of round keys is Nb*(Nr+1)
Jerry Yu947bf962023-02-23 11:07:57 +0800222 */
223 const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */
224 const size_t round_key_len_in_words = 4; /* Nb */
Jerry Yu2c266512023-03-01 11:18:20 +0800225 const size_t rounds_needed = key_len_in_words + 6; /* Nr */
226 const size_t round_keys_len_in_words =
227 round_key_len_in_words * (rounds_needed + 1); /* Nb*(Nr+1) */
228 const uint32_t *rko_end = (uint32_t *) rk + round_keys_len_in_words;
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800229
Jerry Yu3304c202023-02-22 14:37:11 +0800230 memcpy(rk, key, key_len_in_words * 4);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800231
Jerry Yu3304c202023-02-22 14:37:11 +0800232 for (uint32_t *rki = (uint32_t *) rk;
233 rki + key_len_in_words < rko_end;
234 rki += key_len_in_words) {
235
Jerry Yufac5a542023-02-23 10:13:40 +0800236 size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words;
Jerry Yu3304c202023-02-22 14:37:11 +0800237 uint32_t *rko;
Jerry Yubaae4012023-02-21 15:26:13 +0800238 rko = rki + key_len_in_words;
239 rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
Jerry Yu3304c202023-02-22 14:37:11 +0800240 rko[0] ^= rcon[iteration] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800241 rko[1] = rko[0] ^ rki[1];
242 rko[2] = rko[1] ^ rki[2];
243 rko[3] = rko[2] ^ rki[3];
Jerry Yufac5a542023-02-23 10:13:40 +0800244 if (rko + key_len_in_words > rko_end) {
Jerry Yu3304c202023-02-22 14:37:11 +0800245 /* Do not write overflow words.*/
246 continue;
247 }
Jerry Yubaae4012023-02-21 15:26:13 +0800248 switch (key_bit_length) {
Jerry Yu3304c202023-02-22 14:37:11 +0800249 case 128:
250 break;
Jerry Yubaae4012023-02-21 15:26:13 +0800251 case 192:
Jerry Yu3304c202023-02-22 14:37:11 +0800252 rko[4] = rko[3] ^ rki[4];
253 rko[5] = rko[4] ^ rki[5];
Jerry Yubaae4012023-02-21 15:26:13 +0800254 break;
255 case 256:
Jerry Yu3304c202023-02-22 14:37:11 +0800256 rko[4] = aes_sub_word(rko[3]) ^ rki[4];
257 rko[5] = rko[4] ^ rki[5];
258 rko[6] = rko[5] ^ rki[6];
259 rko[7] = rko[6] ^ rki[7];
Jerry Yubaae4012023-02-21 15:26:13 +0800260 break;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800261 }
262 }
263}
264
265/*
266 * Key expansion, wrapper
267 */
268int mbedtls_aesce_setkey_enc(unsigned char *rk,
269 const unsigned char *key,
270 size_t bits)
271{
272 switch (bits) {
Jerry Yubaae4012023-02-21 15:26:13 +0800273 case 128:
274 case 192:
275 case 256:
Jerry Yuba1e78f2023-02-24 11:18:16 +0800276 aesce_setkey_enc(rk, key, bits);
277 break;
278 default:
279 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800280 }
281
282 return 0;
283}
284
Jerry Yudf87a122023-01-10 18:17:15 +0800285#if defined(MBEDTLS_GCM_C)
286
Jerry Yu132d0cb2023-03-02 17:35:53 +0800287#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ == 5
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800288/* Some intrinsics are not available for GCC 5.X. */
Jerry Yu132d0cb2023-03-02 17:35:53 +0800289#define vreinterpretq_p64_u8(a) ((poly64x2_t) a)
290#define vreinterpretq_u8_p128(a) ((uint8x16_t) a)
291static inline poly64_t vget_low_p64(poly64x2_t __a)
292{
293 uint64x2_t tmp = (uint64x2_t) (__a);
294 uint64x1_t lo = vcreate_u64(vgetq_lane_u64(tmp, 0));
295 return (poly64_t) (lo);
296}
297#endif /* !__clang__ && __GNUC__ && __GNUC__ == 5*/
298
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800299/* vmull_p64/vmull_high_p64 wrappers.
300 *
301 * Older compilers miss some intrinsic functions for `poly*_t`. We use
302 * uint8x16_t and uint8x16x3_t as input/output parameters.
303 */
Jerry Yu9db4b1f2023-03-21 16:56:43 +0800304#if defined(__GNUC__) && !defined(__clang__)
305/* GCC reports incompatible type error without cast. GCC think poly64_t and
306 * poly64x1_t are different, that is different with MSVC and Clang. */
307#define MBEDTLS_VMULL_P64(a, b) vmull_p64((poly64_t) a, (poly64_t) b)
308#else
309/* MSVC reports `error C2440: 'type cast'` with cast. Clang does not report
310 * error with/without cast. And I think poly64_t and poly64x1_t are same, no
311 * cast for clang also. */
312#define MBEDTLS_VMULL_P64(a, b) vmull_p64(a, b)
313#endif
Jerry Yudf87a122023-01-10 18:17:15 +0800314static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b)
315{
Jerry Yu9db4b1f2023-03-21 16:56:43 +0800316
Jerry Yudf87a122023-01-10 18:17:15 +0800317 return vreinterpretq_u8_p128(
Jerry Yu9db4b1f2023-03-21 16:56:43 +0800318 MBEDTLS_VMULL_P64(
319 vget_low_p64(vreinterpretq_p64_u8(a)),
320 vget_low_p64(vreinterpretq_p64_u8(b))
321 ));
Jerry Yudf87a122023-01-10 18:17:15 +0800322}
323
324static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b)
325{
326 return vreinterpretq_u8_p128(
327 vmull_high_p64(vreinterpretq_p64_u8(a),
328 vreinterpretq_p64_u8(b)));
329}
330
Jerry Yuf0526a92023-03-14 15:00:29 +0800331/* GHASH does 128b polynomial multiplication on block in GF(2^128) defined by
Jerry Yu49b43672023-03-13 10:09:34 +0800332 * `x^128 + x^7 + x^2 + x + 1`.
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800333 *
334 * Arm64 only has 64b->128b polynomial multipliers, we need to do 4 64b
335 * multiplies to generate a 128b.
336 *
337 * `poly_mult_128` executes polynomial multiplication and outputs 256b that
338 * represented by 3 128b due to code size optimization.
339 *
340 * Output layout:
341 * | | | |
342 * |------------|-------------|-------------|
343 * | ret.val[0] | h3:h2:00:00 | high 128b |
Jerry Yu8f810602023-03-14 17:28:52 +0800344 * | ret.val[1] | :m2:m1:00 | middle 128b |
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800345 * | ret.val[2] | : :l1:l0 | low 128b |
346 */
Jerry Yudf87a122023-01-10 18:17:15 +0800347static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b)
348{
349 uint8x16x3_t ret;
Jerry Yu8f810602023-03-14 17:28:52 +0800350 uint8x16_t h, m, l; /* retval high/middle/low */
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800351 uint8x16_t c, d, e;
352
353 h = pmull_high(a, b); /* h3:h2:00:00 = a1*b1 */
354 l = pmull_low(a, b); /* : :l1:l0 = a0*b0 */
355 c = vextq_u8(b, b, 8); /* :c1:c0 = b0:b1 */
356 d = pmull_high(a, c); /* :d2:d1:00 = a1*b0 */
357 e = pmull_low(a, c); /* :e2:e1:00 = a0*b1 */
358 m = veorq_u8(d, e); /* :m2:m1:00 = d + e */
359
360 ret.val[0] = h;
361 ret.val[1] = m;
362 ret.val[2] = l;
Jerry Yudf87a122023-01-10 18:17:15 +0800363 return ret;
364}
365
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800366/*
367 * Modulo reduction.
368 *
369 * See: https://www.researchgate.net/publication/285612706_Implementing_GCM_on_ARMv8
370 *
371 * Section 4.3
372 *
373 * Modular reduction is slightly more complex. Write the GCM modulus as f(z) =
374 * 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 +0800375 * consider that z^128 ≡r(z) (mod z^128 +r(z)), allowing us to write the 256-bit
376 * operand to be reduced as a(z) = h(z)z^128 +l(z)≡h(z)r(z) + l(z). That is, we
377 * 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 +0800378 * the result is still larger than 128 bits, we reduce again.
379 */
380static inline uint8x16_t poly_mult_reduce(uint8x16x3_t input)
Jerry Yudf87a122023-01-10 18:17:15 +0800381{
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800382 uint8x16_t const ZERO = vdupq_n_u8(0);
Jerry Yu8b6df3f2023-03-21 16:59:13 +0800383
Jerry Yudf87a122023-01-10 18:17:15 +0800384 uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87));
Jerry Yu8b6df3f2023-03-21 16:59:13 +0800385#if defined(__GNUC__)
386 /* use 'asm' as an optimisation barrier to prevent loading MODULO from
387 * memory. It is for GNUC compatible compilers.
388 */
Jerry Yudf87a122023-01-10 18:17:15 +0800389 asm ("" : "+w" (r));
Jerry Yu8b6df3f2023-03-21 16:59:13 +0800390#endif
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800391 uint8x16_t const MODULO = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8));
Jerry Yu8f810602023-03-14 17:28:52 +0800392 uint8x16_t h, m, l; /* input high/middle/low 128b */
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800393 uint8x16_t c, d, e, f, g, n, o;
394 h = input.val[0]; /* h3:h2:00:00 */
395 m = input.val[1]; /* :m2:m1:00 */
396 l = input.val[2]; /* : :l1:l0 */
397 c = pmull_high(h, MODULO); /* :c2:c1:00 = reduction of h3 */
398 d = pmull_low(h, MODULO); /* : :d1:d0 = reduction of h2 */
399 e = veorq_u8(c, m); /* :e2:e1:00 = m2:m1:00 + c2:c1:00 */
400 f = pmull_high(e, MODULO); /* : :f1:f0 = reduction of e2 */
401 g = vextq_u8(ZERO, e, 8); /* : :g1:00 = e1:00 */
402 n = veorq_u8(d, l); /* : :n1:n0 = d1:d0 + l1:l0 */
403 o = veorq_u8(n, f); /* o1:o0 = f1:f0 + n1:n0 */
404 return veorq_u8(o, g); /* = o1:o0 + g1:00 */
Jerry Yudf87a122023-01-10 18:17:15 +0800405}
406
407/*
408 * GCM multiplication: c = a times b in GF(2^128)
409 */
410void mbedtls_aesce_gcm_mult(unsigned char c[16],
411 const unsigned char a[16],
412 const unsigned char b[16])
413{
414 uint8x16_t va, vb, vc;
415 va = vrbitq_u8(vld1q_u8(&a[0]));
416 vb = vrbitq_u8(vld1q_u8(&b[0]));
417 vc = vrbitq_u8(poly_mult_reduce(poly_mult_128(va, vb)));
418 vst1q_u8(&c[0], vc);
419}
420
421#endif /* MBEDTLS_GCM_C */
Jerry Yu48b999c2023-03-03 15:51:07 +0800422
423#if defined(MBEDTLS_POP_TARGET_PRAGMA)
424#if defined(__clang__)
425#pragma clang attribute pop
426#elif defined(__GNUC__)
427#pragma GCC pop_options
428#endif
429#undef MBEDTLS_POP_TARGET_PRAGMA
430#endif
431
Jerry Yu49231312023-01-10 16:57:21 +0800432#endif /* MBEDTLS_HAVE_ARM64 */
433
434#endif /* MBEDTLS_AESCE_C */