blob: 6b493a27292c8f00acf434924fc0186e07a6a1db [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. */
Jerry Yudb368de2023-04-26 16:55:37 +080052#if defined(__clang__)
53# if __clang_major__ < 4
54# error "Minimum version of Clang for MBEDTLS_AESCE_C is 4.0."
55# endif
56#elif defined(__GNUC__)
57# if __GNUC__ < 6
58# error "Minimum version of GCC for MBEDTLS_AESCE_C is 6.0."
59# endif
60#elif defined(_MSC_VER)
Jerry Yu61c4cfa2023-04-26 11:06:51 +080061/* TODO: We haven't verified MSVC from 1920 to 1928. If someone verified that,
62 * please update this and document of `MBEDTLS_AESCE_C` in
63 * `mbedtls_config.h`. */
Jerry Yudb368de2023-04-26 16:55:37 +080064# if _MSC_VER < 1929
65# error "Minimum version of MSVC for MBEDTLS_AESCE_C is 2019 version 16.11.2."
66# endif
Jerry Yu61c4cfa2023-04-26 11:06:51 +080067#endif
68
Dave Rodgmandb6ab242023-03-14 16:03:57 +000069#if !defined(__ARM_FEATURE_AES) || defined(MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG)
Jerry Yuec9be842023-03-14 10:42:47 +080070# if defined(__clang__)
Jerry Yuec9be842023-03-14 10:42:47 +080071# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
72# define MBEDTLS_POP_TARGET_PRAGMA
73# elif defined(__GNUC__)
Jerry Yuec9be842023-03-14 10:42:47 +080074# pragma GCC push_options
75# pragma GCC target ("arch=armv8-a+crypto")
76# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu07d28d82023-03-20 18:12:36 +080077# elif defined(_MSC_VER)
Jerry Yu61c4cfa2023-04-26 11:06:51 +080078# error "Required feature(__ARM_FEATURE_AES) is not enabled."
Jerry Yu49231312023-01-10 16:57:21 +080079# endif
Dave Rodgmandb6ab242023-03-14 16:03:57 +000080#endif /* !__ARM_FEATURE_AES || MBEDTLS_ENABLE_ARM_CRYPTO_EXTENSIONS_COMPILER_FLAG */
Jerry Yu49231312023-01-10 16:57:21 +080081
Jerry Yu49231312023-01-10 16:57:21 +080082#include <arm_neon.h>
83
Jerry Yub95c7762023-01-10 16:59:51 +080084#if defined(__linux__)
85#include <asm/hwcap.h>
86#include <sys/auxv.h>
87#endif
88
89/*
90 * AES instruction support detection routine
91 */
92int mbedtls_aesce_has_support(void)
93{
94#if defined(__linux__)
95 unsigned long auxval = getauxval(AT_HWCAP);
96 return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
97 (HWCAP_ASIMD | HWCAP_AES);
98#else
Jerry Yuba1e78f2023-02-24 11:18:16 +080099 /* Assume AES instructions are supported. */
Jerry Yub95c7762023-01-10 16:59:51 +0800100 return 1;
101#endif
102}
103
Dave Rodgmanf88a68c2023-06-15 18:46:41 +0100104MBEDTLS_OPTIMIZE_ALWAYS
Jerry Yu2bb3d812023-01-10 17:38:26 +0800105static uint8x16_t aesce_encrypt_block(uint8x16_t block,
106 unsigned char *keys,
107 int rounds)
108{
Dave Rodgman96fdfb82023-06-15 16:21:31 +0100109 /* Assume either 10, 12 or 14 rounds */
110 if (rounds == 10) {
111 goto rounds_10;
Jerry Yu2bb3d812023-01-10 17:38:26 +0800112 }
Dave Rodgman96fdfb82023-06-15 16:21:31 +0100113 if (rounds == 12) {
114 goto rounds_12;
115 }
116 block = vaeseq_u8(block, vld1q_u8(keys));
117 block = vaesmcq_u8(block);
118 keys += 16;
119 block = vaeseq_u8(block, vld1q_u8(keys));
120 block = vaesmcq_u8(block);
121 keys += 16;
122rounds_12:
123 block = vaeseq_u8(block, vld1q_u8(keys));
124 block = vaesmcq_u8(block);
125 keys += 16;
126 block = vaeseq_u8(block, vld1q_u8(keys));
127 block = vaesmcq_u8(block);
128 keys += 16;
129rounds_10:
130 block = vaeseq_u8(block, vld1q_u8(keys));
131 block = vaesmcq_u8(block);
132 keys += 16;
133 block = vaeseq_u8(block, vld1q_u8(keys));
134 block = vaesmcq_u8(block);
135 keys += 16;
136 block = vaeseq_u8(block, vld1q_u8(keys));
137 block = vaesmcq_u8(block);
138 keys += 16;
139 block = vaeseq_u8(block, vld1q_u8(keys));
140 block = vaesmcq_u8(block);
141 keys += 16;
142 block = vaeseq_u8(block, vld1q_u8(keys));
143 block = vaesmcq_u8(block);
144 keys += 16;
145 block = vaeseq_u8(block, vld1q_u8(keys));
146 block = vaesmcq_u8(block);
147 keys += 16;
148 block = vaeseq_u8(block, vld1q_u8(keys));
149 block = vaesmcq_u8(block);
150 keys += 16;
151 block = vaeseq_u8(block, vld1q_u8(keys));
152 block = vaesmcq_u8(block);
153 keys += 16;
154 block = vaeseq_u8(block, vld1q_u8(keys));
155 block = vaesmcq_u8(block);
156 keys += 16;
Jerry Yu2bb3d812023-01-10 17:38:26 +0800157
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800158 /* AES AddRoundKey for the previous round.
159 * SubBytes, ShiftRows for the final round. */
Dave Rodgman96fdfb82023-06-15 16:21:31 +0100160 block = vaeseq_u8(block, vld1q_u8(keys));
161 keys += 16;
Jerry Yu2bb3d812023-01-10 17:38:26 +0800162
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800163 /* Final round: no MixColumns */
Jerry Yu3304c202023-02-22 14:37:11 +0800164
165 /* Final AddRoundKey */
Dave Rodgman96fdfb82023-06-15 16:21:31 +0100166 block = veorq_u8(block, vld1q_u8(keys));
Jerry Yu2bb3d812023-01-10 17:38:26 +0800167
168 return block;
169}
170
Dave Rodgmanf88a68c2023-06-15 18:46:41 +0100171MBEDTLS_OPTIMIZE_ALWAYS
Jerry Yu2bb3d812023-01-10 17:38:26 +0800172static uint8x16_t aesce_decrypt_block(uint8x16_t block,
173 unsigned char *keys,
174 int rounds)
175{
Dave Rodgman1c4451d2023-06-15 16:28:00 +0100176 /* Assume either 10, 12 or 14 rounds */
177 if (rounds == 10) {
178 goto rounds_10;
Jerry Yu2bb3d812023-01-10 17:38:26 +0800179 }
Dave Rodgman1c4451d2023-06-15 16:28:00 +0100180 if (rounds == 12) {
181 goto rounds_12;
182 }
183
184 /* AES AddRoundKey, SubBytes, ShiftRows */
185 block = vaesdq_u8(block, vld1q_u8(keys));
186 /* AES inverse MixColumns for the next round.
187 *
188 * This means that we switch the order of the inverse AddRoundKey and
189 * inverse MixColumns operations. We have to do this as AddRoundKey is
190 * done in an atomic instruction together with the inverses of SubBytes
191 * and ShiftRows.
192 *
193 * It works because MixColumns is a linear operation over GF(2^8) and
194 * AddRoundKey is an exclusive or, which is equivalent to addition over
195 * GF(2^8). (The inverse of MixColumns needs to be applied to the
196 * affected round keys separately which has been done when the
197 * decryption round keys were calculated.) */
198 block = vaesimcq_u8(block);
199 keys += 16;
200
201 block = vaesdq_u8(block, vld1q_u8(keys));
202 block = vaesimcq_u8(block);
203 keys += 16;
204rounds_12:
205 block = vaesdq_u8(block, vld1q_u8(keys));
206 block = vaesimcq_u8(block);
207 keys += 16;
208 block = vaesdq_u8(block, vld1q_u8(keys));
209 block = vaesimcq_u8(block);
210 keys += 16;
211rounds_10:
212 block = vaesdq_u8(block, vld1q_u8(keys));
213 block = vaesimcq_u8(block);
214 keys += 16;
215 block = vaesdq_u8(block, vld1q_u8(keys));
216 block = vaesimcq_u8(block);
217 keys += 16;
218 block = vaesdq_u8(block, vld1q_u8(keys));
219 block = vaesimcq_u8(block);
220 keys += 16;
221 block = vaesdq_u8(block, vld1q_u8(keys));
222 block = vaesimcq_u8(block);
223 keys += 16;
224 block = vaesdq_u8(block, vld1q_u8(keys));
225 block = vaesimcq_u8(block);
226 keys += 16;
227 block = vaesdq_u8(block, vld1q_u8(keys));
228 block = vaesimcq_u8(block);
229 keys += 16;
230 block = vaesdq_u8(block, vld1q_u8(keys));
231 block = vaesimcq_u8(block);
232 keys += 16;
233 block = vaesdq_u8(block, vld1q_u8(keys));
234 block = vaesimcq_u8(block);
235 keys += 16;
236 block = vaesdq_u8(block, vld1q_u8(keys));
237 block = vaesimcq_u8(block);
238 keys += 16;
Jerry Yu2bb3d812023-01-10 17:38:26 +0800239
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800240 /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
241 * last full round. */
Dave Rodgman1c4451d2023-06-15 16:28:00 +0100242 block = vaesdq_u8(block, vld1q_u8(keys));
243 keys += 16;
Jerry Yu2bb3d812023-01-10 17:38:26 +0800244
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800245 /* Inverse AddRoundKey for inverting the initial round key addition. */
Dave Rodgman1c4451d2023-06-15 16:28:00 +0100246 block = veorq_u8(block, vld1q_u8(keys));
Jerry Yu2bb3d812023-01-10 17:38:26 +0800247
248 return block;
249}
250
251/*
252 * AES-ECB block en(de)cryption
253 */
Dave Rodgmanf88a68c2023-06-15 18:46:41 +0100254MBEDTLS_OPTIMIZE_ALWAYS
Jerry Yu2bb3d812023-01-10 17:38:26 +0800255int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
256 int mode,
257 const unsigned char input[16],
258 unsigned char output[16])
259{
260 uint8x16_t block = vld1q_u8(&input[0]);
261 unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
262
263 if (mode == MBEDTLS_AES_ENCRYPT) {
264 block = aesce_encrypt_block(block, keys, ctx->nr);
265 } else {
266 block = aesce_decrypt_block(block, keys, ctx->nr);
267 }
268 vst1q_u8(&output[0], block);
269
270 return 0;
271}
272
Jerry Yue096da12023-01-10 17:07:01 +0800273/*
274 * Compute decryption round keys from encryption round keys
275 */
276void mbedtls_aesce_inverse_key(unsigned char *invkey,
277 const unsigned char *fwdkey,
278 int nr)
279{
280 int i, j;
281 j = nr;
282 vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
283 for (i = 1, j--; j > 0; i++, j--) {
284 vst1q_u8(invkey + i * 16,
285 vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
286 }
287 vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
288
289}
290
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800291static inline uint32_t aes_rot_word(uint32_t word)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800292{
293 return (word << (32 - 8)) | (word >> 8);
294}
295
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800296static inline uint32_t aes_sub_word(uint32_t in)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800297{
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800298 uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
Jerry Yu3f2fb712023-01-10 17:05:42 +0800299 uint8x16_t zero = vdupq_n_u8(0);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800300
301 /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
302 * the correct result as ShiftRows doesn't change the first row. */
303 v = vaeseq_u8(zero, v);
304 return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800305}
306
307/*
Jerry Yubaae4012023-02-21 15:26:13 +0800308 * Key expansion function
Jerry Yu3f2fb712023-01-10 17:05:42 +0800309 */
Jerry Yubaae4012023-02-21 15:26:13 +0800310static void aesce_setkey_enc(unsigned char *rk,
311 const unsigned char *key,
312 const size_t key_bit_length)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800313{
Jerry Yubaae4012023-02-21 15:26:13 +0800314 static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
315 0x20, 0x40, 0x80, 0x1b, 0x36 };
Jerry Yu947bf962023-02-23 11:07:57 +0800316 /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
317 * - Section 5, Nr = Nk + 6
Jerry Yu2c266512023-03-01 11:18:20 +0800318 * - Section 5.2, the length of round keys is Nb*(Nr+1)
Jerry Yu947bf962023-02-23 11:07:57 +0800319 */
320 const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */
321 const size_t round_key_len_in_words = 4; /* Nb */
Jerry Yu2c266512023-03-01 11:18:20 +0800322 const size_t rounds_needed = key_len_in_words + 6; /* Nr */
323 const size_t round_keys_len_in_words =
324 round_key_len_in_words * (rounds_needed + 1); /* Nb*(Nr+1) */
325 const uint32_t *rko_end = (uint32_t *) rk + round_keys_len_in_words;
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800326
Jerry Yu3304c202023-02-22 14:37:11 +0800327 memcpy(rk, key, key_len_in_words * 4);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800328
Jerry Yu3304c202023-02-22 14:37:11 +0800329 for (uint32_t *rki = (uint32_t *) rk;
330 rki + key_len_in_words < rko_end;
331 rki += key_len_in_words) {
332
Jerry Yufac5a542023-02-23 10:13:40 +0800333 size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words;
Jerry Yu3304c202023-02-22 14:37:11 +0800334 uint32_t *rko;
Jerry Yubaae4012023-02-21 15:26:13 +0800335 rko = rki + key_len_in_words;
336 rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
Jerry Yu3304c202023-02-22 14:37:11 +0800337 rko[0] ^= rcon[iteration] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800338 rko[1] = rko[0] ^ rki[1];
339 rko[2] = rko[1] ^ rki[2];
340 rko[3] = rko[2] ^ rki[3];
Jerry Yufac5a542023-02-23 10:13:40 +0800341 if (rko + key_len_in_words > rko_end) {
Jerry Yu3304c202023-02-22 14:37:11 +0800342 /* Do not write overflow words.*/
343 continue;
344 }
Yanray Wange2bc1582023-05-08 10:28:53 +0800345#if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Jerry Yubaae4012023-02-21 15:26:13 +0800346 switch (key_bit_length) {
Jerry Yu3304c202023-02-22 14:37:11 +0800347 case 128:
348 break;
Jerry Yubaae4012023-02-21 15:26:13 +0800349 case 192:
Jerry Yu3304c202023-02-22 14:37:11 +0800350 rko[4] = rko[3] ^ rki[4];
351 rko[5] = rko[4] ^ rki[5];
Jerry Yubaae4012023-02-21 15:26:13 +0800352 break;
353 case 256:
Jerry Yu3304c202023-02-22 14:37:11 +0800354 rko[4] = aes_sub_word(rko[3]) ^ rki[4];
355 rko[5] = rko[4] ^ rki[5];
356 rko[6] = rko[5] ^ rki[6];
357 rko[7] = rko[6] ^ rki[7];
Jerry Yubaae4012023-02-21 15:26:13 +0800358 break;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800359 }
Yanray Wange2bc1582023-05-08 10:28:53 +0800360#endif /* !MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Jerry Yu3f2fb712023-01-10 17:05:42 +0800361 }
362}
363
364/*
365 * Key expansion, wrapper
366 */
367int mbedtls_aesce_setkey_enc(unsigned char *rk,
368 const unsigned char *key,
369 size_t bits)
370{
371 switch (bits) {
Jerry Yubaae4012023-02-21 15:26:13 +0800372 case 128:
373 case 192:
374 case 256:
Jerry Yuba1e78f2023-02-24 11:18:16 +0800375 aesce_setkey_enc(rk, key, bits);
376 break;
377 default:
378 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800379 }
380
381 return 0;
382}
383
Jerry Yudf87a122023-01-10 18:17:15 +0800384#if defined(MBEDTLS_GCM_C)
385
Jerry Yu132d0cb2023-03-02 17:35:53 +0800386#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ == 5
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800387/* Some intrinsics are not available for GCC 5.X. */
Jerry Yu132d0cb2023-03-02 17:35:53 +0800388#define vreinterpretq_p64_u8(a) ((poly64x2_t) a)
389#define vreinterpretq_u8_p128(a) ((uint8x16_t) a)
390static inline poly64_t vget_low_p64(poly64x2_t __a)
391{
392 uint64x2_t tmp = (uint64x2_t) (__a);
393 uint64x1_t lo = vcreate_u64(vgetq_lane_u64(tmp, 0));
394 return (poly64_t) (lo);
395}
396#endif /* !__clang__ && __GNUC__ && __GNUC__ == 5*/
397
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800398/* vmull_p64/vmull_high_p64 wrappers.
399 *
400 * Older compilers miss some intrinsic functions for `poly*_t`. We use
401 * uint8x16_t and uint8x16x3_t as input/output parameters.
402 */
Jerry Yu9db4b1f2023-03-21 16:56:43 +0800403#if defined(__GNUC__) && !defined(__clang__)
404/* GCC reports incompatible type error without cast. GCC think poly64_t and
405 * poly64x1_t are different, that is different with MSVC and Clang. */
406#define MBEDTLS_VMULL_P64(a, b) vmull_p64((poly64_t) a, (poly64_t) b)
407#else
408/* MSVC reports `error C2440: 'type cast'` with cast. Clang does not report
409 * error with/without cast. And I think poly64_t and poly64x1_t are same, no
410 * cast for clang also. */
411#define MBEDTLS_VMULL_P64(a, b) vmull_p64(a, b)
412#endif
Jerry Yudf87a122023-01-10 18:17:15 +0800413static inline uint8x16_t pmull_low(uint8x16_t a, uint8x16_t b)
414{
Jerry Yu9db4b1f2023-03-21 16:56:43 +0800415
Jerry Yudf87a122023-01-10 18:17:15 +0800416 return vreinterpretq_u8_p128(
Jerry Yu9db4b1f2023-03-21 16:56:43 +0800417 MBEDTLS_VMULL_P64(
418 vget_low_p64(vreinterpretq_p64_u8(a)),
419 vget_low_p64(vreinterpretq_p64_u8(b))
420 ));
Jerry Yudf87a122023-01-10 18:17:15 +0800421}
422
423static inline uint8x16_t pmull_high(uint8x16_t a, uint8x16_t b)
424{
425 return vreinterpretq_u8_p128(
426 vmull_high_p64(vreinterpretq_p64_u8(a),
427 vreinterpretq_p64_u8(b)));
428}
429
Jerry Yuf0526a92023-03-14 15:00:29 +0800430/* GHASH does 128b polynomial multiplication on block in GF(2^128) defined by
Jerry Yu49b43672023-03-13 10:09:34 +0800431 * `x^128 + x^7 + x^2 + x + 1`.
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800432 *
433 * Arm64 only has 64b->128b polynomial multipliers, we need to do 4 64b
434 * multiplies to generate a 128b.
435 *
436 * `poly_mult_128` executes polynomial multiplication and outputs 256b that
437 * represented by 3 128b due to code size optimization.
438 *
439 * Output layout:
440 * | | | |
441 * |------------|-------------|-------------|
442 * | ret.val[0] | h3:h2:00:00 | high 128b |
Jerry Yu8f810602023-03-14 17:28:52 +0800443 * | ret.val[1] | :m2:m1:00 | middle 128b |
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800444 * | ret.val[2] | : :l1:l0 | low 128b |
445 */
Jerry Yudf87a122023-01-10 18:17:15 +0800446static inline uint8x16x3_t poly_mult_128(uint8x16_t a, uint8x16_t b)
447{
448 uint8x16x3_t ret;
Jerry Yu8f810602023-03-14 17:28:52 +0800449 uint8x16_t h, m, l; /* retval high/middle/low */
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800450 uint8x16_t c, d, e;
451
452 h = pmull_high(a, b); /* h3:h2:00:00 = a1*b1 */
453 l = pmull_low(a, b); /* : :l1:l0 = a0*b0 */
454 c = vextq_u8(b, b, 8); /* :c1:c0 = b0:b1 */
455 d = pmull_high(a, c); /* :d2:d1:00 = a1*b0 */
456 e = pmull_low(a, c); /* :e2:e1:00 = a0*b1 */
457 m = veorq_u8(d, e); /* :m2:m1:00 = d + e */
458
459 ret.val[0] = h;
460 ret.val[1] = m;
461 ret.val[2] = l;
Jerry Yudf87a122023-01-10 18:17:15 +0800462 return ret;
463}
464
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800465/*
466 * Modulo reduction.
467 *
468 * See: https://www.researchgate.net/publication/285612706_Implementing_GCM_on_ARMv8
469 *
470 * Section 4.3
471 *
472 * Modular reduction is slightly more complex. Write the GCM modulus as f(z) =
473 * 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 +0800474 * consider that z^128 ≡r(z) (mod z^128 +r(z)), allowing us to write the 256-bit
475 * operand to be reduced as a(z) = h(z)z^128 +l(z)≡h(z)r(z) + l(z). That is, we
476 * 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 +0800477 * the result is still larger than 128 bits, we reduce again.
478 */
479static inline uint8x16_t poly_mult_reduce(uint8x16x3_t input)
Jerry Yudf87a122023-01-10 18:17:15 +0800480{
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800481 uint8x16_t const ZERO = vdupq_n_u8(0);
Jerry Yu8b6df3f2023-03-21 16:59:13 +0800482
Jerry Yudf87a122023-01-10 18:17:15 +0800483 uint64x2_t r = vreinterpretq_u64_u8(vdupq_n_u8(0x87));
Jerry Yu8b6df3f2023-03-21 16:59:13 +0800484#if defined(__GNUC__)
485 /* use 'asm' as an optimisation barrier to prevent loading MODULO from
486 * memory. It is for GNUC compatible compilers.
487 */
Jerry Yudf87a122023-01-10 18:17:15 +0800488 asm ("" : "+w" (r));
Jerry Yu8b6df3f2023-03-21 16:59:13 +0800489#endif
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800490 uint8x16_t const MODULO = vreinterpretq_u8_u64(vshrq_n_u64(r, 64 - 8));
Jerry Yu8f810602023-03-14 17:28:52 +0800491 uint8x16_t h, m, l; /* input high/middle/low 128b */
Jerry Yu1ac7f6b2023-03-07 15:44:59 +0800492 uint8x16_t c, d, e, f, g, n, o;
493 h = input.val[0]; /* h3:h2:00:00 */
494 m = input.val[1]; /* :m2:m1:00 */
495 l = input.val[2]; /* : :l1:l0 */
496 c = pmull_high(h, MODULO); /* :c2:c1:00 = reduction of h3 */
497 d = pmull_low(h, MODULO); /* : :d1:d0 = reduction of h2 */
498 e = veorq_u8(c, m); /* :e2:e1:00 = m2:m1:00 + c2:c1:00 */
499 f = pmull_high(e, MODULO); /* : :f1:f0 = reduction of e2 */
500 g = vextq_u8(ZERO, e, 8); /* : :g1:00 = e1:00 */
501 n = veorq_u8(d, l); /* : :n1:n0 = d1:d0 + l1:l0 */
502 o = veorq_u8(n, f); /* o1:o0 = f1:f0 + n1:n0 */
503 return veorq_u8(o, g); /* = o1:o0 + g1:00 */
Jerry Yudf87a122023-01-10 18:17:15 +0800504}
505
506/*
507 * GCM multiplication: c = a times b in GF(2^128)
508 */
509void mbedtls_aesce_gcm_mult(unsigned char c[16],
510 const unsigned char a[16],
511 const unsigned char b[16])
512{
513 uint8x16_t va, vb, vc;
514 va = vrbitq_u8(vld1q_u8(&a[0]));
515 vb = vrbitq_u8(vld1q_u8(&b[0]));
516 vc = vrbitq_u8(poly_mult_reduce(poly_mult_128(va, vb)));
517 vst1q_u8(&c[0], vc);
518}
519
520#endif /* MBEDTLS_GCM_C */
Jerry Yu48b999c2023-03-03 15:51:07 +0800521
522#if defined(MBEDTLS_POP_TARGET_PRAGMA)
523#if defined(__clang__)
524#pragma clang attribute pop
525#elif defined(__GNUC__)
526#pragma GCC pop_options
527#endif
528#undef MBEDTLS_POP_TARGET_PRAGMA
529#endif
530
Jerry Yu49231312023-01-10 16:57:21 +0800531#endif /* MBEDTLS_HAVE_ARM64 */
532
533#endif /* MBEDTLS_AESCE_C */