Debian Codegraph @

cipher/rijndael-armv8-ce.c

raw
1 /* ARMv8 Crypto Extension AES for Libgcrypt
2 * Copyright (C) 2016, 2022 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h> /* for memcmp() */
25
26 #include "types.h" /* for byte and u32 typedefs */
27 #include "g10lib.h"
28 #include "cipher.h"
29 #include "bufhelp.h"
30 #include "rijndael-internal.h"
31 #include "./cipher-internal.h"
32
33
34 #ifdef USE_ARM_CE
35
36
37 typedef struct u128_s { u32 a, b, c, d; } u128_t;
38
39 extern u32 _gcry_aes_sbox4_armv8_ce(u32 in4b);
40 extern void _gcry_aes_invmixcol_armv8_ce(u128_t *dst, const u128_t *src);
41
42 extern unsigned int _gcry_aes_enc_armv8_ce(const void *keysched, byte *dst,
43 const byte *src,
44 unsigned int nrounds);
45 extern unsigned int _gcry_aes_dec_armv8_ce(const void *keysched, byte *dst,
46 const byte *src,
47 unsigned int nrounds);
48
49 extern void _gcry_aes_cbc_enc_armv8_ce (const void *keysched,
50 unsigned char *outbuf,
51 const unsigned char *inbuf,
52 unsigned char *iv, size_t nblocks,
53 int cbc_mac, unsigned int nrounds);
54 extern void _gcry_aes_cbc_dec_armv8_ce (const void *keysched,
55 unsigned char *outbuf,
56 const unsigned char *inbuf,
57 unsigned char *iv, size_t nblocks,
58 unsigned int nrounds);
59
60 extern void _gcry_aes_cfb_enc_armv8_ce (const void *keysched,
61 unsigned char *outbuf,
62 const unsigned char *inbuf,
63 unsigned char *iv, size_t nblocks,
64 unsigned int nrounds);
65 extern void _gcry_aes_cfb_dec_armv8_ce (const void *keysched,
66 unsigned char *outbuf,
67 const unsigned char *inbuf,
68 unsigned char *iv, size_t nblocks,
69 unsigned int nrounds);
70
71 extern void _gcry_aes_ctr_enc_armv8_ce (const void *keysched,
72 unsigned char *outbuf,
73 const unsigned char *inbuf,
74 unsigned char *iv, size_t nblocks,
75 unsigned int nrounds);
76
77 extern void _gcry_aes_ctr32le_enc_armv8_ce (const void *keysched,
78 unsigned char *outbuf,
79 const unsigned char *inbuf,
80 unsigned char *iv, size_t nblocks,
81 unsigned int nrounds);
82
83 extern size_t _gcry_aes_ocb_enc_armv8_ce (const void *keysched,
84 unsigned char *outbuf,
85 const unsigned char *inbuf,
86 unsigned char *offset,
87 unsigned char *checksum,
88 unsigned char *L_table,
89 size_t nblocks,
90 unsigned int nrounds,
91 unsigned int blkn);
92 extern size_t _gcry_aes_ocb_dec_armv8_ce (const void *keysched,
93 unsigned char *outbuf,
94 const unsigned char *inbuf,
95 unsigned char *offset,
96 unsigned char *checksum,
97 unsigned char *L_table,
98 size_t nblocks,
99 unsigned int nrounds,
100 unsigned int blkn);
101 extern size_t _gcry_aes_ocb_auth_armv8_ce (const void *keysched,
102 const unsigned char *abuf,
103 unsigned char *offset,
104 unsigned char *checksum,
105 unsigned char *L_table,
106 size_t nblocks,
107 unsigned int nrounds,
108 unsigned int blkn);
109 extern void _gcry_aes_xts_enc_armv8_ce (const void *keysched,
110 unsigned char *outbuf,
111 const unsigned char *inbuf,
112 unsigned char *tweak,
113 size_t nblocks, unsigned int nrounds);
114 extern void _gcry_aes_xts_dec_armv8_ce (const void *keysched,
115 unsigned char *outbuf,
116 const unsigned char *inbuf,
117 unsigned char *tweak,
118 size_t nblocks, unsigned int nrounds);
119 extern void _gcry_aes_ecb_enc_armv8_ce (const void *keysched,
120 unsigned char *outbuf,
121 const unsigned char *inbuf,
122 size_t nblocks, unsigned int nrounds);
123 extern void _gcry_aes_ecb_dec_armv8_ce (const void *keysched,
124 unsigned char *outbuf,
125 const unsigned char *inbuf,
126 size_t nblocks, unsigned int nrounds);
127
128
129 void
130 _gcry_aes_armv8_ce_setkey (RIJNDAEL_context *ctx, const byte *key)
131 {
132 unsigned int rounds = ctx->rounds;
133 unsigned int KC = rounds - 6;
134 u32 *W_u32 = ctx->keyschenc32b;
135 unsigned int i, j;
136 u32 W_prev;
137 byte rcon = 1;
138
139 for (i = 0; i < KC; i += 2)
140 {
141 W_u32[i + 0] = buf_get_le32(key + i * 4 + 0);
142 W_u32[i + 1] = buf_get_le32(key + i * 4 + 4);
143 }
144
145 for (i = KC, j = KC, W_prev = W_u32[KC - 1];
146 i < 4 * (rounds + 1);
147 i += 2, j += 2)
148 {
149 u32 temp0 = W_prev;
150 u32 temp1;
151
152 if (j == KC)
153 {
154 j = 0;
155 temp0 = _gcry_aes_sbox4_armv8_ce(rol(temp0, 24)) ^ rcon;
156 rcon = ((rcon << 1) ^ (-(rcon >> 7) & 0x1b)) & 0xff;
157 }
158 else if (KC == 8 && j == 4)
159 {
160 temp0 = _gcry_aes_sbox4_armv8_ce(temp0);
161 }
162
163 temp1 = W_u32[i - KC + 0];
164
165 W_u32[i + 0] = temp0 ^ temp1;
166 W_u32[i + 1] = W_u32[i - KC + 1] ^ temp0 ^ temp1;
167 W_prev = W_u32[i + 1];
168 }
169 }
170
171 /* Make a decryption key from an encryption key. */
172 void
173 _gcry_aes_armv8_ce_prepare_decryption (RIJNDAEL_context *ctx)
174 {
175 u128_t *ekey = (u128_t *)(void *)ctx->keyschenc;
176 u128_t *dkey = (u128_t *)(void *)ctx->keyschdec;
177 int rounds = ctx->rounds;
178 int rr;
179 int r;
180
181 #define DO_AESIMC() _gcry_aes_invmixcol_armv8_ce(&dkey[r], &ekey[rr])
182
183 dkey[0] = ekey[rounds];
184 r = 1;
185 rr = rounds-1;
186 DO_AESIMC(); r++; rr--; /* round 1 */
187 DO_AESIMC(); r++; rr--; /* round 2 */
188 DO_AESIMC(); r++; rr--; /* round 3 */
189 DO_AESIMC(); r++; rr--; /* round 4 */
190 DO_AESIMC(); r++; rr--; /* round 5 */
191 DO_AESIMC(); r++; rr--; /* round 6 */
192 DO_AESIMC(); r++; rr--; /* round 7 */
193 DO_AESIMC(); r++; rr--; /* round 8 */
194 DO_AESIMC(); r++; rr--; /* round 9 */
195 if (rounds >= 12)
196 {
197 if (rounds > 12)
198 {
199 DO_AESIMC(); r++; rr--; /* round 10 */
200 DO_AESIMC(); r++; rr--; /* round 11 */
201 }
202
203 DO_AESIMC(); r++; rr--; /* round 12 / 10 */
204 DO_AESIMC(); r++; rr--; /* round 13 / 11 */
205 }
206
207 dkey[r] = ekey[0];
208
209 #undef DO_AESIMC
210 }
211
212 unsigned int
213 _gcry_aes_armv8_ce_encrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
214 const unsigned char *src)
215 {
216 const void *keysched = ctx->keyschenc32;
217 unsigned int nrounds = ctx->rounds;
218
219 return _gcry_aes_enc_armv8_ce(keysched, dst, src, nrounds);
220 }
221
222 unsigned int
223 _gcry_aes_armv8_ce_decrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
224 const unsigned char *src)
225 {
226 const void *keysched = ctx->keyschdec32;
227 unsigned int nrounds = ctx->rounds;
228
229 return _gcry_aes_dec_armv8_ce(keysched, dst, src, nrounds);
230 }
231
232 void
233 _gcry_aes_armv8_ce_cbc_enc (const RIJNDAEL_context *ctx, unsigned char *iv,
234 unsigned char *outbuf, const unsigned char *inbuf,
235 size_t nblocks, int cbc_mac)
236 {
237 const void *keysched = ctx->keyschenc32;
238 unsigned int nrounds = ctx->rounds;
239
240 _gcry_aes_cbc_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, cbc_mac,
241 nrounds);
242 }
243
244 void
245 _gcry_aes_armv8_ce_cbc_dec (RIJNDAEL_context *ctx, unsigned char *iv,
246 unsigned char *outbuf, const unsigned char *inbuf,
247 size_t nblocks)
248 {
249 const void *keysched = ctx->keyschdec32;
250 unsigned int nrounds = ctx->rounds;
251
252 if ( !ctx->decryption_prepared )
253 {
254 _gcry_aes_armv8_ce_prepare_decryption ( ctx );
255 ctx->decryption_prepared = 1;
256 }
257
258 _gcry_aes_cbc_dec_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
259 }
260
261 void
262 _gcry_aes_armv8_ce_cfb_enc (RIJNDAEL_context *ctx, unsigned char *iv,
263 unsigned char *outbuf, const unsigned char *inbuf,
264 size_t nblocks)
265 {
266 const void *keysched = ctx->keyschenc32;
267 unsigned int nrounds = ctx->rounds;
268
269 _gcry_aes_cfb_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
270 }
271
272 void
273 _gcry_aes_armv8_ce_cfb_dec (RIJNDAEL_context *ctx, unsigned char *iv,
274 unsigned char *outbuf, const unsigned char *inbuf,
275 size_t nblocks)
276 {
277 const void *keysched = ctx->keyschenc32;
278 unsigned int nrounds = ctx->rounds;
279
280 _gcry_aes_cfb_dec_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
281 }
282
283 void
284 _gcry_aes_armv8_ce_ctr_enc (RIJNDAEL_context *ctx, unsigned char *iv,
285 unsigned char *outbuf, const unsigned char *inbuf,
286 size_t nblocks)
287 {
288 const void *keysched = ctx->keyschenc32;
289 unsigned int nrounds = ctx->rounds;
290
291 _gcry_aes_ctr_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
292 }
293
294 void
295 _gcry_aes_armv8_ce_ctr32le_enc (RIJNDAEL_context *ctx, unsigned char *iv,
296 unsigned char *outbuf,
297 const unsigned char *inbuf, size_t nblocks)
298 {
299 const void *keysched = ctx->keyschenc32;
300 unsigned int nrounds = ctx->rounds;
301
302 _gcry_aes_ctr32le_enc_armv8_ce(keysched, outbuf, inbuf, iv, nblocks, nrounds);
303 }
304
305 size_t
306 _gcry_aes_armv8_ce_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
307 const void *inbuf_arg, size_t nblocks,
308 int encrypt)
309 {
310 RIJNDAEL_context *ctx = (void *)&c->context.c;
311 const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
312 unsigned char *outbuf = outbuf_arg;
313 const unsigned char *inbuf = inbuf_arg;
314 unsigned int nrounds = ctx->rounds;
315 u64 blkn = c->u_mode.ocb.data_nblocks;
316
317 if ( !encrypt && !ctx->decryption_prepared )
318 {
319 _gcry_aes_armv8_ce_prepare_decryption ( ctx );
320 ctx->decryption_prepared = 1;
321 }
322
323 c->u_mode.ocb.data_nblocks = blkn + nblocks;
324
325 if (encrypt)
326 return _gcry_aes_ocb_enc_armv8_ce (keysched, outbuf, inbuf,
327 c->u_iv.iv, c->u_ctr.ctr,
328 c->u_mode.ocb.L[0], nblocks, nrounds,
329 (unsigned int)blkn);
330 else
331 return _gcry_aes_ocb_dec_armv8_ce (keysched, outbuf, inbuf,
332 c->u_iv.iv, c->u_ctr.ctr,
333 c->u_mode.ocb.L[0], nblocks, nrounds,
334 (unsigned int)blkn);
335 }
336
337 size_t
338 _gcry_aes_armv8_ce_ocb_auth (gcry_cipher_hd_t c, void *abuf_arg,
339 size_t nblocks)
340 {
341 RIJNDAEL_context *ctx = (void *)&c->context.c;
342 const void *keysched = ctx->keyschenc32;
343 const unsigned char *abuf = abuf_arg;
344 unsigned int nrounds = ctx->rounds;
345 u64 blkn = c->u_mode.ocb.aad_nblocks;
346
347 c->u_mode.ocb.aad_nblocks = blkn + nblocks;
348
349 return _gcry_aes_ocb_auth_armv8_ce (keysched, abuf, c->u_mode.ocb.aad_offset,
350 c->u_mode.ocb.aad_sum, c->u_mode.ocb.L[0],
351 nblocks, nrounds, (unsigned int)blkn);
352 }
353
354 void
355 _gcry_aes_armv8_ce_xts_crypt (RIJNDAEL_context *ctx, unsigned char *tweak,
356 unsigned char *outbuf, const unsigned char *inbuf,
357 size_t nblocks, int encrypt)
358 {
359 const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
360 unsigned int nrounds = ctx->rounds;
361
362 if ( !encrypt && !ctx->decryption_prepared )
363 {
364 _gcry_aes_armv8_ce_prepare_decryption ( ctx );
365 ctx->decryption_prepared = 1;
366 }
367
368 if (encrypt)
369 _gcry_aes_xts_enc_armv8_ce (keysched, outbuf, inbuf, tweak,
370 nblocks, nrounds);
371 else
372 _gcry_aes_xts_dec_armv8_ce (keysched, outbuf, inbuf, tweak,
373 nblocks, nrounds);
374 }
375
376 void
377 _gcry_aes_armv8_ce_ecb_crypt (void *context, void *outbuf,
378 const void *inbuf, size_t nblocks,
379 int encrypt)
380 {
381 RIJNDAEL_context *ctx = context;
382 const void *keysched = encrypt ? ctx->keyschenc32 : ctx->keyschdec32;
383 unsigned int nrounds = ctx->rounds;
384
385 if ( !encrypt && !ctx->decryption_prepared )
386 {
387 _gcry_aes_armv8_ce_prepare_decryption ( ctx );
388 ctx->decryption_prepared = 1;
389 }
390
391 if (encrypt)
392 _gcry_aes_ecb_enc_armv8_ce (keysched, outbuf, inbuf, nblocks, nrounds);
393 else
394 _gcry_aes_ecb_dec_armv8_ce (keysched, outbuf, inbuf, nblocks, nrounds);
395 }
396 #endif /* USE_ARM_CE */