blob: 29a4ce0183b2812c2e7b6e6ad3c9d9dbf3b41512 [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
20#include <string.h>
21#include "common.h"
22
23#if defined(MBEDTLS_AESCE_C)
24
25#include "aesce.h"
26
27#if defined(MBEDTLS_HAVE_ARM64)
28
29#if defined(__clang__)
30# if __clang_major__ < 4
Jerry Yub2783f62023-02-13 18:03:25 +080031# error "A more recent Clang is required for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080032# endif
33#elif defined(__GNUC__)
34# if __GNUC__ < 6
Jerry Yub2783f62023-02-13 18:03:25 +080035# error "A more recent GCC is required for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080036# endif
37#else
Jerry Yub2783f62023-02-13 18:03:25 +080038# error "Only GCC and Clang supported for MBEDTLS_AESCE_C"
Jerry Yu49231312023-01-10 16:57:21 +080039#endif
40
41#if !defined(__ARM_FEATURE_CRYPTO)
42# error "`crypto` feature moddifier MUST be enabled for MBEDTLS_AESCE_C."
43# error "Typical option for GCC and Clang is `-march=armv8-a+crypto`."
44#endif /* !__ARM_FEATURE_CRYPTO */
45
46#include <arm_neon.h>
47
Jerry Yub95c7762023-01-10 16:59:51 +080048#if defined(__linux__)
49#include <asm/hwcap.h>
50#include <sys/auxv.h>
51#endif
52
53/*
54 * AES instruction support detection routine
55 */
56int mbedtls_aesce_has_support(void)
57{
58#if defined(__linux__)
59 unsigned long auxval = getauxval(AT_HWCAP);
60 return (auxval & (HWCAP_ASIMD | HWCAP_AES)) ==
61 (HWCAP_ASIMD | HWCAP_AES);
62#else
63 /* Suppose aes instructions are supported. */
64 return 1;
65#endif
66}
67
Jerry Yu2bb3d812023-01-10 17:38:26 +080068static uint8x16_t aesce_encrypt_block(uint8x16_t block,
69 unsigned char *keys,
70 int rounds)
71{
72 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +080073 /* AES AddRoundKey, SubBytes, ShiftRows (in this order).
74 * AddRoundKey adds the round key for the previous round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +080075 block = vaeseq_u8(block, vld1q_u8(keys + i * 16));
76 /* AES mix columns */
77 block = vaesmcq_u8(block);
78 }
79
Jerry Yuc8bcdc82023-02-21 14:49:02 +080080 /* AES AddRoundKey for the previous round.
81 * SubBytes, ShiftRows for the final round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +080082 block = vaeseq_u8(block, vld1q_u8(keys + (rounds -1) * 16));
83
Jerry Yuc8bcdc82023-02-21 14:49:02 +080084 /* Final round: no MixColumns */
Jerry Yu2bb3d812023-01-10 17:38:26 +080085 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
86
87 return block;
88}
89
90static uint8x16_t aesce_decrypt_block(uint8x16_t block,
91 unsigned char *keys,
92 int rounds)
93{
94
95 for (int i = 0; i < rounds - 1; i++) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +080096 /* AES AddRoundKey, SubBytes, ShiftRows */
Jerry Yu2bb3d812023-01-10 17:38:26 +080097 block = vaesdq_u8(block, vld1q_u8(keys + i * 16));
Jerry Yuc8bcdc82023-02-21 14:49:02 +080098 /* AES inverse MixColumns for the next round.
99 *
100 * This means that we switch the order of the inverse AddRoundKey and
101 * inverse MixColumns operations. We have to do this as AddRoundKey is
102 * done in an atomic instruction together with the inverses of SubBytes
103 * and ShiftRows.
104 *
105 * It works because MixColumns is a linear operation over GF(2^8) and
106 * AddRoundKey is an exclusive or, which is equivalent to addition over
107 * GF(2^8). (The inverse of MixColumns needs to be applied to the
108 * affected round keys separately which has been done when the
109 * decryption round keys were calculated.) */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800110 block = vaesimcq_u8(block);
111 }
112
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800113 /* The inverses of AES AddRoundKey, SubBytes, ShiftRows finishing up the
114 * last full round. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800115 block = vaesdq_u8(block, vld1q_u8(keys + (rounds - 1) * 16));
116
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800117 /* Inverse AddRoundKey for inverting the initial round key addition. */
Jerry Yu2bb3d812023-01-10 17:38:26 +0800118 block = veorq_u8(block, vld1q_u8(keys + rounds * 16));
119
120 return block;
121}
122
123/*
124 * AES-ECB block en(de)cryption
125 */
126int mbedtls_aesce_crypt_ecb(mbedtls_aes_context *ctx,
127 int mode,
128 const unsigned char input[16],
129 unsigned char output[16])
130{
131 uint8x16_t block = vld1q_u8(&input[0]);
132 unsigned char *keys = (unsigned char *) (ctx->buf + ctx->rk_offset);
133
134 if (mode == MBEDTLS_AES_ENCRYPT) {
135 block = aesce_encrypt_block(block, keys, ctx->nr);
136 } else {
137 block = aesce_decrypt_block(block, keys, ctx->nr);
138 }
139 vst1q_u8(&output[0], block);
140
141 return 0;
142}
143
Jerry Yu3f2fb712023-01-10 17:05:42 +0800144
Jerry Yue096da12023-01-10 17:07:01 +0800145/*
146 * Compute decryption round keys from encryption round keys
147 */
148void mbedtls_aesce_inverse_key(unsigned char *invkey,
149 const unsigned char *fwdkey,
150 int nr)
151{
152 int i, j;
153 j = nr;
154 vst1q_u8(invkey, vld1q_u8(fwdkey + j * 16));
155 for (i = 1, j--; j > 0; i++, j--) {
156 vst1q_u8(invkey + i * 16,
157 vaesimcq_u8(vld1q_u8(fwdkey + j * 16)));
158 }
159 vst1q_u8(invkey + i * 16, vld1q_u8(fwdkey + j * 16));
160
161}
162
Jerry Yu3f2fb712023-01-10 17:05:42 +0800163static uint8_t const rcon[] = { 0x01, 0x02, 0x04, 0x08, 0x10,
164 0x20, 0x40, 0x80, 0x1b, 0x36 };
165
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800166static inline uint32_t aes_rot_word(uint32_t word)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800167{
168 return (word << (32 - 8)) | (word >> 8);
169}
170
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800171static inline uint32_t aes_sub_word(uint32_t in)
Jerry Yu3f2fb712023-01-10 17:05:42 +0800172{
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800173 uint8x16_t v = vreinterpretq_u8_u32(vdupq_n_u32(in));
Jerry Yu3f2fb712023-01-10 17:05:42 +0800174 uint8x16_t zero = vdupq_n_u8(0);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800175
176 /* vaeseq_u8 does both SubBytes and ShiftRows. Taking the first row yields
177 * the correct result as ShiftRows doesn't change the first row. */
178 v = vaeseq_u8(zero, v);
179 return vgetq_lane_u32(vreinterpretq_u32_u8(v), 0);
Jerry Yu3f2fb712023-01-10 17:05:42 +0800180}
181
182/*
183 * Key expansion, 128-bit case
184 */
185static void aesce_setkey_enc_128(unsigned char *rk,
186 const unsigned char *key)
187{
188 uint32_t *rki;
189 uint32_t *rko;
190 uint32_t *rk_u32 = (uint32_t *) rk;
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800191
Jerry Yu3f2fb712023-01-10 17:05:42 +0800192 memcpy(rk, key, (128 / 8));
193
194 for (size_t i = 0; i < sizeof(rcon); i++) {
195 rki = rk_u32 + i * (128 / 32);
196 rko = rki + (128 / 32);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800197 rko[0] = aes_rot_word(aes_sub_word(rki[(128 / 32) - 1])) ^ rcon[i] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800198 rko[1] = rko[0] ^ rki[1];
199 rko[2] = rko[1] ^ rki[2];
200 rko[3] = rko[2] ^ rki[3];
201 }
202}
203
204/*
205 * Key expansion, 192-bit case
206 */
207static void aesce_setkey_enc_192(unsigned char *rk,
208 const unsigned char *key)
209{
210 uint32_t *rki;
211 uint32_t *rko;
212 uint32_t *rk_u32 = (uint32_t *) rk;
213 memcpy(rk, key, (192 / 8));
214
215 for (size_t i = 0; i < 8; i++) {
216 rki = rk_u32 + i * (192 / 32);
217 rko = rki + (192 / 32);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800218 rko[0] = aes_rot_word(aes_sub_word(rki[(192 / 32) - 1])) ^ rcon[i] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800219 rko[1] = rko[0] ^ rki[1];
220 rko[2] = rko[1] ^ rki[2];
221 rko[3] = rko[2] ^ rki[3];
222 if (i < 7) {
223 rko[4] = rko[3] ^ rki[4];
224 rko[5] = rko[4] ^ rki[5];
225 }
226 }
227}
228
229/*
230 * Key expansion, 256-bit case
231 */
232static void aesce_setkey_enc_256(unsigned char *rk,
233 const unsigned char *key)
234{
235 uint32_t *rki;
236 uint32_t *rko;
237 uint32_t *rk_u32 = (uint32_t *) rk;
238 memcpy(rk, key, (256 / 8));
239
240 for (size_t i = 0; i < 7; i++) {
241 rki = rk_u32 + i * (256 / 32);
242 rko = rki + (256 / 32);
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800243 rko[0] = aes_rot_word(aes_sub_word(rki[(256 / 32) - 1])) ^ rcon[i] ^ rki[0];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800244 rko[1] = rko[0] ^ rki[1];
245 rko[2] = rko[1] ^ rki[2];
246 rko[3] = rko[2] ^ rki[3];
247 if (i < 6) {
Jerry Yuc8bcdc82023-02-21 14:49:02 +0800248 rko[4] = aes_sub_word(rko[3]) ^ rki[4];
Jerry Yu3f2fb712023-01-10 17:05:42 +0800249 rko[5] = rko[4] ^ rki[5];
250 rko[6] = rko[5] ^ rki[6];
251 rko[7] = rko[6] ^ rki[7];
252 }
253 }
254}
255
256/*
257 * Key expansion, wrapper
258 */
259int mbedtls_aesce_setkey_enc(unsigned char *rk,
260 const unsigned char *key,
261 size_t bits)
262{
263 switch (bits) {
264 case 128: aesce_setkey_enc_128(rk, key); break;
265 case 192: aesce_setkey_enc_192(rk, key); break;
266 case 256: aesce_setkey_enc_256(rk, key); break;
267 default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
268 }
269
270 return 0;
271}
272
Jerry Yu49231312023-01-10 16:57:21 +0800273#endif /* MBEDTLS_HAVE_ARM64 */
274
275#endif /* MBEDTLS_AESCE_C */