blob: ca7c6928f16d1b4b0001bc583940f703eff384ba [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) && \
21 defined(__clang__) && __clang_major__ < 18 && __clang_major__ > 3
22/* 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
33#define NEED_TARGET_OPTIONS
34#endif /* __aarch64__ && __clang__ &&
35 !__ARM_FEATURE_CRYPTO && __clang_major__ < 18 && __clang_major__ > 3 */
36
Jerry Yu49231312023-01-10 16:57:21 +080037#include <string.h>
38#include "common.h"
39
40#if defined(MBEDTLS_AESCE_C)
41
42#include "aesce.h"
43
44#if defined(MBEDTLS_HAVE_ARM64)
45
46#if defined(__clang__)
47# if __clang_major__ < 4
Jerry Yub2783f62023-02-13 18:03:25 +080048# error "A more recent Clang is required for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080049# endif
Jerry Yu48b999c2023-03-03 15:51:07 +080050# pragma clang attribute push (__attribute__((target("crypto"))), apply_to=function)
51# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu49231312023-01-10 16:57:21 +080052#elif defined(__GNUC__)
53# if __GNUC__ < 6
Jerry Yub2783f62023-02-13 18:03:25 +080054# error "A more recent GCC is required for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080055# endif
Jerry Yu48b999c2023-03-03 15:51:07 +080056# pragma GCC push_options
57# pragma GCC target ("arch=armv8-a+crypto")
58# define MBEDTLS_POP_TARGET_PRAGMA
Jerry Yu49231312023-01-10 16:57:21 +080059#else
Jerry Yub2783f62023-02-13 18:03:25 +080060# error "Only GCC and Clang supported for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080061#endif
62
Jerry Yu49231312023-01-10 16:57:21 +080063#include <arm_neon.h>
64
Jerry Yub95c7762023-01-10 16:59:51 +080065#if defined(__linux__)
66#include <asm/hwcap.h>
67#include <sys/auxv.h>
68#endif
69
70/*
71 * AES instruction support detection routine
72 */
73int mbedtls_aesce_has_support(void)
74{
75#if defined(__linux__)
76 unsigned long auxval = getauxval(AT_HWCAP);
77 return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
78 (HWCAP_ASIMD | HWCAP_AES);
79#else
Jerry Yuba1e78f2023-02-24 11:18:16 +080080 /* Assume AES instructions are supported. */
Jerry Yub95c7762023-01-10 16:59:51 +080081 return 1;
82#endif
83}
84
Jerry Yu2bb3d812023-01-10 17:38:26 +080085static uint8x16_t aesce_encrypt_block(uint8x16_t block,
86 unsigned char *keys,
87 int rounds)
88{
89 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +080090 /* AES AddRoundKey, SubBytes, ShiftRows (in this order).
91 * AddRoundKey adds the round key for the previous round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +080092 block = vaeseq_u8(block, vld1q_u8(keys + i * 16));
93 /* AES mix columns */
94 block = vaesmcq_u8(block);
95 }
96
Jerry Yuc8bcdc82023-02-21 14:49:02 +080097 /* AES AddRoundKey for the previous round.
98 * SubBytes, ShiftRows for the final round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +080099 block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16));
100
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800101 /* Final round: no MixColumns */
Jerry Yu3304c202023-02-22 14:37:11 +0800102
103 /* Final AddRoundKey */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800104 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
105
106 return block;
107}
108
109static uint8x16_t aesce_decrypt_block(uint8x16_t block,
110 unsigned char *keys,
111 int rounds)
112{
113
114 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800115 /* AES AddRoundKey, SubBytes, ShiftRows */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800116 block = vaesdq_u8(block, vld1q_u8(keys + i * 16));
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800117 /* AES inverse MixColumns for the next round.
118 *
119 * This means that we switch the order of the inverse AddRoundKey and
120 * inverse MixColumns operations. We have to do this as AddRoundKey is
121 * done in an atomic instruction together with the inverses of SubBytes
122 * and ShiftRows.
123 *
124 * It works because MixColumns is a linear operation over GF(2^8) and
125 * AddRoundKey is an exclusive or, which is equivalent to addition over
126 * GF(2^8). (The inverse of MixColumns needs to be applied to the
127 * affected round keys separately which has been done when the
128 * decryption round keys were calculated.) */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800129 block = vaesimcq_u8(block);
130 }
131
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800132 /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
133 * last full round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800134 block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16));
135
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800136 /* Inverse AddRoundKey for inverting the initial round key addition. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800137 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
138
139 return block;
140}
141
142/*
143 * AES-ECB block en(de)cryption
144 */
145int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
146 int mode,
147 const unsigned char input[16],
148 unsigned char output[16])
149{
150 uint8x16_t block = vld1q_u8(&input[0]);
151 unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
152
153 if (mode == MBEDTLS_AES_ENCRYPT) {
154 block = aesce_encrypt_block(block, keys, ctx->nr);
155 } else {
156 block = aesce_decrypt_block(block, keys, ctx->nr);
157 }
158 vst1q_u8(&output[0], block);
159
160 return 0;
161}
162
Jerry Yue096da12023-01-10 17:07:01 +0800163/*
164 * Compute decryption round keys from encryption round keys
165 */
166void mbedtls_aesce_inverse_key(unsigned char *invkey,
167 const unsigned char *fwdkey,
168 int nr)
169{
170 int i, j;
171 j = nr;
172 vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
173 for (i = 1, j--; j > 0; i++, j--) {
174 vst1q_u8(invkey + i * 16,
175 vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
176 }
177 vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
178
179}
180
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800181static inline uint32_t aes_rot_word(uint32_t word)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800182{
183 return (word << (32 - 8)) | (word >> 8);
184}
185
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800186static inline uint32_t aes_sub_word(uint32_t in)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800187{
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800188 uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
Jerry Yu3f2fb712023-01-10 17:05:42 +0800189 uint8x16_t zero = vdupq_n_u8(0);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800190
191 /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
192 * the correct result as ShiftRows doesn't change the first row. */
193 v = vaeseq_u8(zero, v);
194 return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800195}
196
197/*
Jerry Yubaae4012023-02-21 15:26:13 +0800198 * Key expansion function
Jerry Yu3f2fb712023-01-10 17:05:42 +0800199 */
Jerry Yubaae4012023-02-21 15:26:13 +0800200static void aesce_setkey_enc(unsigned char *rk,
201 const unsigned char *key,
202 const size_t key_bit_length)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800203{
Jerry Yubaae4012023-02-21 15:26:13 +0800204 static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
205 0x20, 0x40, 0x80, 0x1b, 0x36 };
Jerry Yu947bf962023-02-23 11:07:57 +0800206 /* See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf
207 * - Section 5, Nr = Nk + 6
208 * - Section 5.2, the key expansion size is Nb*(Nr+1)
209 */
210 const uint32_t key_len_in_words = key_bit_length / 32; /* Nk */
211 const size_t round_key_len_in_words = 4; /* Nb */
212 const size_t round_keys_needed = key_len_in_words + 6; /* Nr */
Jerry Yu3304c202023-02-22 14:37:11 +0800213 const size_t key_expansion_size_in_words =
Jerry Yu947bf962023-02-23 11:07:57 +0800214 round_key_len_in_words * (round_keys_needed + 1); /* Nb*(Nr+1) */
Jerry Yu3304c202023-02-22 14:37:11 +0800215 const uint32_t *rko_end = (uint32_t *) rk + key_expansion_size_in_words;
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800216
Jerry Yu3304c202023-02-22 14:37:11 +0800217 memcpy(rk, key, key_len_in_words * 4);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800218
Jerry Yu3304c202023-02-22 14:37:11 +0800219 for (uint32_t *rki = (uint32_t *) rk;
220 rki + key_len_in_words < rko_end;
221 rki += key_len_in_words) {
222
Jerry Yufac5a542023-02-23 10:13:40 +0800223 size_t iteration = (rki - (uint32_t *) rk) / key_len_in_words;
Jerry Yu3304c202023-02-22 14:37:11 +0800224 uint32_t *rko;
Jerry Yubaae4012023-02-21 15:26:13 +0800225 rko = rki + key_len_in_words;
226 rko[0] = aes_rot_word(aes_sub_word(rki[key_len_in_words - 1]));
Jerry Yu3304c202023-02-22 14:37:11 +0800227 rko[0] ^= rcon[iteration] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800228 rko[1] = rko[0] ^ rki[1];
229 rko[2] = rko[1] ^ rki[2];
230 rko[3] = rko[2] ^ rki[3];
Jerry Yufac5a542023-02-23 10:13:40 +0800231 if (rko + key_len_in_words > rko_end) {
Jerry Yu3304c202023-02-22 14:37:11 +0800232 /* Do not write overflow words.*/
233 continue;
234 }
Jerry Yubaae4012023-02-21 15:26:13 +0800235 switch (key_bit_length) {
Jerry Yu3304c202023-02-22 14:37:11 +0800236 case 128:
237 break;
Jerry Yubaae4012023-02-21 15:26:13 +0800238 case 192:
Jerry Yu3304c202023-02-22 14:37:11 +0800239 rko[4] = rko[3] ^ rki[4];
240 rko[5] = rko[4] ^ rki[5];
Jerry Yubaae4012023-02-21 15:26:13 +0800241 break;
242 case 256:
Jerry Yu3304c202023-02-22 14:37:11 +0800243 rko[4] = aes_sub_word(rko[3]) ^ rki[4];
244 rko[5] = rko[4] ^ rki[5];
245 rko[6] = rko[5] ^ rki[6];
246 rko[7] = rko[6] ^ rki[7];
Jerry Yubaae4012023-02-21 15:26:13 +0800247 break;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800248 }
249 }
250}
251
252/*
253 * Key expansion, wrapper
254 */
255int mbedtls_aesce_setkey_enc(unsigned char *rk,
256 const unsigned char *key,
257 size_t bits)
258{
259 switch (bits) {
Jerry Yubaae4012023-02-21 15:26:13 +0800260 case 128:
261 case 192:
262 case 256:
Jerry Yuba1e78f2023-02-24 11:18:16 +0800263 aesce_setkey_enc(rk, key, bits);
264 break;
265 default:
266 return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
Jerry Yu3f2fb712023-01-10 17:05:42 +0800267 }
268
269 return 0;
270}
271
Jerry Yu48b999c2023-03-03 15:51:07 +0800272
273#if defined(MBEDTLS_POP_TARGET_PRAGMA)
274#if defined(__clang__)
275#pragma clang attribute pop
276#elif defined(__GNUC__)
277#pragma GCC pop_options
278#endif
279#undef MBEDTLS_POP_TARGET_PRAGMA
280#endif
281
Jerry Yu49231312023-01-10 16:57:21 +0800282#endif /* MBEDTLS_HAVE_ARM64 */
283
284#endif /* MBEDTLS_AESCE_C */