Debian Codegraph @

cipher/rijndael.c

raw
1 /* Rijndael (AES) for GnuPG
2 * Copyright (C) 2000, 2001, 2002, 2003, 2007,
3 * 2008, 2011, 2012 Free Software Foundation, Inc.
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************
20 * The code here is based on the optimized implementation taken from
21 * http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ on Oct 2, 2000,
22 * which carries this notice:
23 *------------------------------------------
24 * rijndael-alg-fst.c v2.3 April '2000
25 *
26 * Optimised ANSI C code
27 *
28 * authors: v1.0: Antoon Bosselaers
29 * v2.0: Vincent Rijmen
30 * v2.3: Paulo Barreto
31 *
32 * This code is placed in the public domain.
33 *------------------------------------------
34 *
35 * The SP800-38a document is available at:
36 * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
37 *
38 */
39
40 #include <config.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h> /* for memcmp() */
44
45 #include "types.h" /* for byte and u32 typedefs */
46 #include "g10lib.h"
47 #include "cipher.h"
48 #include "bufhelp.h"
49 #include "hwf-common.h"
50 #include "rijndael-internal.h"
51 #include "./cipher-internal.h"
52
53
54 #ifdef USE_AMD64_ASM
55 /* AMD64 assembly implementations of AES */
56 extern unsigned int _gcry_aes_amd64_encrypt_block(const void *keysched_enc,
57 unsigned char *out,
58 const unsigned char *in,
59 int rounds,
60 const void *encT);
61
62 extern unsigned int _gcry_aes_amd64_decrypt_block(const void *keysched_dec,
63 unsigned char *out,
64 const unsigned char *in,
65 int rounds,
66 const void *decT);
67 #endif /*USE_AMD64_ASM*/
68
69 #ifdef USE_AESNI
70 /* AES-NI (AMD64 & i386) accelerated implementations of AES */
71 extern void _gcry_aes_aesni_do_setkey(RIJNDAEL_context *ctx, const byte *key);
72 extern void _gcry_aes_aesni_prepare_decryption(RIJNDAEL_context *ctx);
73
74 extern unsigned int _gcry_aes_aesni_encrypt (const RIJNDAEL_context *ctx,
75 unsigned char *dst,
76 const unsigned char *src);
77 extern unsigned int _gcry_aes_aesni_decrypt (const RIJNDAEL_context *ctx,
78 unsigned char *dst,
79 const unsigned char *src);
80 extern void _gcry_aes_aesni_cfb_enc (void *context, unsigned char *iv,
81 void *outbuf_arg, const void *inbuf_arg,
82 size_t nblocks);
83 extern void _gcry_aes_aesni_cbc_enc (void *context, unsigned char *iv,
84 void *outbuf_arg, const void *inbuf_arg,
85 size_t nblocks, int cbc_mac);
86 extern void _gcry_aes_aesni_ctr_enc (void *context, unsigned char *ctr,
87 void *outbuf_arg, const void *inbuf_arg,
88 size_t nblocks);
89 extern void _gcry_aes_aesni_ctr32le_enc (void *context, unsigned char *ctr,
90 void *outbuf_arg,
91 const void *inbuf_arg, size_t nblocks);
92 extern void _gcry_aes_aesni_cfb_dec (void *context, unsigned char *iv,
93 void *outbuf_arg, const void *inbuf_arg,
94 size_t nblocks);
95 extern void _gcry_aes_aesni_cbc_dec (void *context, unsigned char *iv,
96 void *outbuf_arg, const void *inbuf_arg,
97 size_t nblocks);
98 extern size_t _gcry_aes_aesni_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
99 const void *inbuf_arg, size_t nblocks,
100 int encrypt);
101 extern size_t _gcry_aes_aesni_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
102 size_t nblocks);
103 extern void _gcry_aes_aesni_xts_crypt (void *context, unsigned char *tweak,
104 void *outbuf_arg, const void *inbuf_arg,
105 size_t nblocks, int encrypt);
106 extern void _gcry_aes_aesni_ecb_crypt (void *context, void *outbuf_arg,
107 const void *inbuf_arg, size_t nblocks,
108 int encrypt);
109 #endif
110
111 #if defined(USE_VAES_I386) || defined(USE_VAES)
112 /* VAES (i386/AMD64) accelerated implementation of AES */
113
114 extern void _gcry_aes_vaes_cfb_dec (void *context, unsigned char *iv,
115 void *outbuf_arg, const void *inbuf_arg,
116 size_t nblocks);
117 extern void _gcry_aes_vaes_cbc_dec (void *context, unsigned char *iv,
118 void *outbuf_arg, const void *inbuf_arg,
119 size_t nblocks);
120 extern void _gcry_aes_vaes_ctr_enc (void *context, unsigned char *ctr,
121 void *outbuf_arg, const void *inbuf_arg,
122 size_t nblocks);
123 extern void _gcry_aes_vaes_ctr32le_enc (void *context, unsigned char *ctr,
124 void *outbuf_arg, const void *inbuf_arg,
125 size_t nblocks);
126 extern size_t _gcry_aes_vaes_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
127 const void *inbuf_arg, size_t nblocks,
128 int encrypt);
129 extern size_t _gcry_aes_vaes_ocb_auth (gcry_cipher_hd_t c,
130 const void *inbuf_arg,
131 size_t nblocks);
132 extern void _gcry_aes_vaes_xts_crypt (void *context, unsigned char *tweak,
133 void *outbuf_arg, const void *inbuf_arg,
134 size_t nblocks, int encrypt);
135 extern void _gcry_aes_vaes_ecb_crypt (void *context, void *outbuf_arg,
136 const void *inbuf_arg, size_t nblocks,
137 int encrypt);
138 #endif
139
140 #ifdef USE_SSSE3
141 /* SSSE3 (AMD64) vector permutation implementation of AES */
142 extern void _gcry_aes_ssse3_do_setkey(RIJNDAEL_context *ctx, const byte *key);
143 extern void _gcry_aes_ssse3_prepare_decryption(RIJNDAEL_context *ctx);
144
145 extern unsigned int _gcry_aes_ssse3_encrypt (const RIJNDAEL_context *ctx,
146 unsigned char *dst,
147 const unsigned char *src);
148 extern unsigned int _gcry_aes_ssse3_decrypt (const RIJNDAEL_context *ctx,
149 unsigned char *dst,
150 const unsigned char *src);
151 extern void _gcry_aes_ssse3_cfb_enc (void *context, unsigned char *iv,
152 void *outbuf_arg, const void *inbuf_arg,
153 size_t nblocks);
154 extern void _gcry_aes_ssse3_cbc_enc (void *context, unsigned char *iv,
155 void *outbuf_arg, const void *inbuf_arg,
156 size_t nblocks,
157 int cbc_mac);
158 extern void _gcry_aes_ssse3_ctr_enc (void *context, unsigned char *ctr,
159 void *outbuf_arg, const void *inbuf_arg,
160 size_t nblocks);
161 extern void _gcry_aes_ssse3_cfb_dec (void *context, unsigned char *iv,
162 void *outbuf_arg, const void *inbuf_arg,
163 size_t nblocks);
164 extern void _gcry_aes_ssse3_cbc_dec (void *context, unsigned char *iv,
165 void *outbuf_arg, const void *inbuf_arg,
166 size_t nblocks);
167 extern size_t _gcry_aes_ssse3_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
168 const void *inbuf_arg, size_t nblocks,
169 int encrypt);
170 extern size_t _gcry_aes_ssse3_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
171 size_t nblocks);
172 #endif
173
174 #ifdef USE_VP_AARCH64
175 /* AArch64 vector permutation implementation of AES */
176 extern void _gcry_aes_vp_aarch64_do_setkey(RIJNDAEL_context *ctx,
177 const byte *key);
178 extern void _gcry_aes_vp_aarch64_prepare_decryption(RIJNDAEL_context *ctx);
179
180 extern unsigned int _gcry_aes_vp_aarch64_encrypt (const RIJNDAEL_context *ctx,
181 unsigned char *dst,
182 const unsigned char *src);
183 extern unsigned int _gcry_aes_vp_aarch64_decrypt (const RIJNDAEL_context *ctx,
184 unsigned char *dst,
185 const unsigned char *src);
186 extern void _gcry_aes_vp_aarch64_cfb_enc (void *context, unsigned char *iv,
187 void *outbuf_arg,
188 const void *inbuf_arg,
189 size_t nblocks);
190 extern void _gcry_aes_vp_aarch64_cbc_enc (void *context, unsigned char *iv,
191 void *outbuf_arg,
192 const void *inbuf_arg,
193 size_t nblocks,
194 int cbc_mac);
195 extern void _gcry_aes_vp_aarch64_ctr_enc (void *context, unsigned char *ctr,
196 void *outbuf_arg,
197 const void *inbuf_arg,
198 size_t nblocks);
199 extern void _gcry_aes_vp_aarch64_ctr32le_enc (void *context, unsigned char *ctr,
200 void *outbuf_arg,
201 const void *inbuf_arg,
202 size_t nblocks);
203 extern void _gcry_aes_vp_aarch64_cfb_dec (void *context, unsigned char *iv,
204 void *outbuf_arg,
205 const void *inbuf_arg,
206 size_t nblocks);
207 extern void _gcry_aes_vp_aarch64_cbc_dec (void *context, unsigned char *iv,
208 void *outbuf_arg,
209 const void *inbuf_arg,
210 size_t nblocks);
211 extern size_t _gcry_aes_vp_aarch64_ocb_crypt (gcry_cipher_hd_t c,
212 void *outbuf_arg,
213 const void *inbuf_arg,
214 size_t nblocks,
215 int encrypt);
216 extern size_t _gcry_aes_vp_aarch64_ocb_auth (gcry_cipher_hd_t c,
217 const void *abuf_arg,
218 size_t nblocks);
219 extern void _gcry_aes_vp_aarch64_ecb_crypt (void *context, void *outbuf_arg,
220 const void *inbuf_arg,
221 size_t nblocks, int encrypt);
222 extern void _gcry_aes_vp_aarch64_xts_crypt (void *context, unsigned char *tweak,
223 void *outbuf_arg,
224 const void *inbuf_arg,
225 size_t nblocks, int encrypt);
226 #endif
227
228 #ifdef USE_VP_RISCV
229 /* RISC-V vector permutation implementation of AES */
230 extern int _gcry_aes_vp_riscv_setup_acceleration(RIJNDAEL_context *ctx);
231
232 extern void _gcry_aes_vp_riscv_do_setkey(RIJNDAEL_context *ctx,
233 const byte *key);
234 extern void _gcry_aes_vp_riscv_prepare_decryption(RIJNDAEL_context *ctx);
235
236 extern unsigned int _gcry_aes_vp_riscv_encrypt (const RIJNDAEL_context *ctx,
237 unsigned char *dst,
238 const unsigned char *src);
239 extern unsigned int _gcry_aes_vp_riscv_decrypt (const RIJNDAEL_context *ctx,
240 unsigned char *dst,
241 const unsigned char *src);
242 extern void _gcry_aes_vp_riscv_cfb_enc (void *context, unsigned char *iv,
243 void *outbuf_arg,
244 const void *inbuf_arg,
245 size_t nblocks);
246 extern void _gcry_aes_vp_riscv_cbc_enc (void *context, unsigned char *iv,
247 void *outbuf_arg,
248 const void *inbuf_arg,
249 size_t nblocks,
250 int cbc_mac);
251 extern void _gcry_aes_vp_riscv_ctr_enc (void *context, unsigned char *ctr,
252 void *outbuf_arg,
253 const void *inbuf_arg,
254 size_t nblocks);
255 extern void _gcry_aes_vp_riscv_ctr32le_enc (void *context, unsigned char *ctr,
256 void *outbuf_arg,
257 const void *inbuf_arg,
258 size_t nblocks);
259 extern void _gcry_aes_vp_riscv_cfb_dec (void *context, unsigned char *iv,
260 void *outbuf_arg,
261 const void *inbuf_arg,
262 size_t nblocks);
263 extern void _gcry_aes_vp_riscv_cbc_dec (void *context, unsigned char *iv,
264 void *outbuf_arg,
265 const void *inbuf_arg,
266 size_t nblocks);
267 extern size_t _gcry_aes_vp_riscv_ocb_crypt (gcry_cipher_hd_t c,
268 void *outbuf_arg,
269 const void *inbuf_arg,
270 size_t nblocks,
271 int encrypt);
272 extern size_t _gcry_aes_vp_riscv_ocb_auth (gcry_cipher_hd_t c,
273 const void *abuf_arg,
274 size_t nblocks);
275 extern void _gcry_aes_vp_riscv_ecb_crypt (void *context, void *outbuf_arg,
276 const void *inbuf_arg,
277 size_t nblocks, int encrypt);
278 extern void _gcry_aes_vp_riscv_xts_crypt (void *context, unsigned char *tweak,
279 void *outbuf_arg,
280 const void *inbuf_arg,
281 size_t nblocks, int encrypt);
282 #endif
283
284 #ifdef USE_RISCV_V_CRYPTO
285 /* RISC-V vector cryptography extension implementation of AES */
286 extern int
287 _gcry_aes_riscv_zvkned_setup_acceleration (RIJNDAEL_context *ctx);
288
289 extern void
290 _gcry_aes_riscv_zvkned_setkey (RIJNDAEL_context *ctx, const byte *key);
291 extern void
292 _gcry_aes_riscv_zvkned_prepare_decryption (RIJNDAEL_context *ctx);
293
294 extern unsigned int
295 _gcry_aes_riscv_zvkned_encrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
296 const unsigned char *src);
297 extern unsigned int
298 _gcry_aes_riscv_zvkned_decrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
299 const unsigned char *src);
300 extern void
301 _gcry_aes_riscv_zvkned_cfb_enc (void *context, unsigned char *iv,
302 void *outbuf_arg, const void *inbuf_arg,
303 size_t nblocks);
304 extern void
305 _gcry_aes_riscv_zvkned_cbc_enc (void *context, unsigned char *iv,
306 void *outbuf_arg, const void *inbuf_arg,
307 size_t nblocks, int cbc_mac);
308 extern void
309 _gcry_aes_riscv_zvkned_ctr_enc (void *context, unsigned char *ctr,
310 void *outbuf_arg, const void *inbuf_arg,
311 size_t nblocks);
312 extern void
313 _gcry_aes_riscv_zvkned_ctr32le_enc (void *context, unsigned char *ctr,
314 void *outbuf_arg, const void *inbuf_arg,
315 size_t nblocks);
316 extern void
317 _gcry_aes_riscv_zvkned_cfb_dec (void *context, unsigned char *iv,
318 void *outbuf_arg, const void *inbuf_arg,
319 size_t nblocks);
320 extern void
321 _gcry_aes_riscv_zvkned_cbc_dec (void *context, unsigned char *iv,
322 void *outbuf_arg, const void *inbuf_arg,
323 size_t nblocks);
324 extern size_t
325 _gcry_aes_riscv_zvkned_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
326 const void *inbuf_arg, size_t nblocks,
327 int encrypt);
328 extern size_t
329 _gcry_aes_riscv_zvkned_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
330 size_t nblocks);
331 extern void
332 _gcry_aes_riscv_zvkned_ecb_crypt (void *context, void *outbuf_arg,
333 const void *inbuf_arg, size_t nblocks,
334 int encrypt);
335 extern void
336 _gcry_aes_riscv_zvkned_xts_crypt (void *context, unsigned char *tweak,
337 void *outbuf_arg, const void *inbuf_arg,
338 size_t nblocks, int encrypt);
339 #endif
340
341 #ifdef USE_PADLOCK
342 extern unsigned int _gcry_aes_padlock_encrypt (const RIJNDAEL_context *ctx,
343 unsigned char *bx,
344 const unsigned char *ax);
345 extern unsigned int _gcry_aes_padlock_decrypt (const RIJNDAEL_context *ctx,
346 unsigned char *bx,
347 const unsigned char *ax);
348 extern void _gcry_aes_padlock_prepare_decryption (RIJNDAEL_context *ctx);
349 #endif
350
351 #ifdef USE_ARM_ASM
352 /* ARM assembly implementations of AES */
353 extern unsigned int _gcry_aes_arm_encrypt_block(const void *keysched_enc,
354 unsigned char *out,
355 const unsigned char *in,
356 int rounds,
357 const void *encT);
358
359 extern unsigned int _gcry_aes_arm_decrypt_block(const void *keysched_dec,
360 unsigned char *out,
361 const unsigned char *in,
362 int rounds,
363 const void *decT);
364 #endif /*USE_ARM_ASM*/
365
366 #ifdef USE_ARM_CE
367 /* ARMv8 Crypto Extension implementations of AES */
368 extern void _gcry_aes_armv8_ce_setkey(RIJNDAEL_context *ctx, const byte *key);
369 extern void _gcry_aes_armv8_ce_prepare_decryption(RIJNDAEL_context *ctx);
370
371 extern unsigned int _gcry_aes_armv8_ce_encrypt(const RIJNDAEL_context *ctx,
372 unsigned char *dst,
373 const unsigned char *src);
374 extern unsigned int _gcry_aes_armv8_ce_decrypt(const RIJNDAEL_context *ctx,
375 unsigned char *dst,
376 const unsigned char *src);
377
378 extern void _gcry_aes_armv8_ce_cfb_enc (void *context, unsigned char *iv,
379 void *outbuf_arg, const void *inbuf_arg,
380 size_t nblocks);
381 extern void _gcry_aes_armv8_ce_cbc_enc (void *context, unsigned char *iv,
382 void *outbuf_arg, const void *inbuf_arg,
383 size_t nblocks,
384 int cbc_mac);
385 extern void _gcry_aes_armv8_ce_ctr_enc (void *context, unsigned char *ctr,
386 void *outbuf_arg, const void *inbuf_arg,
387 size_t nblocks);
388 extern void _gcry_aes_armv8_ce_ctr32le_enc (void *context, unsigned char *ctr,
389 void *outbuf_arg,
390 const void *inbuf_arg,
391 size_t nblocks);
392 extern void _gcry_aes_armv8_ce_cfb_dec (void *context, unsigned char *iv,
393 void *outbuf_arg, const void *inbuf_arg,
394 size_t nblocks);
395 extern void _gcry_aes_armv8_ce_cbc_dec (void *context, unsigned char *iv,
396 void *outbuf_arg, const void *inbuf_arg,
397 size_t nblocks);
398 extern size_t _gcry_aes_armv8_ce_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
399 const void *inbuf_arg, size_t nblocks,
400 int encrypt);
401 extern size_t _gcry_aes_armv8_ce_ocb_auth (gcry_cipher_hd_t c,
402 const void *abuf_arg, size_t nblocks);
403 extern void _gcry_aes_armv8_ce_xts_crypt (void *context, unsigned char *tweak,
404 void *outbuf_arg,
405 const void *inbuf_arg,
406 size_t nblocks, int encrypt);
407 extern void _gcry_aes_armv8_ce_ecb_crypt (void *context, void *outbuf_arg,
408 const void *inbuf_arg, size_t nblocks,
409 int encrypt);
410 #endif /*USE_ARM_ASM*/
411
412 #ifdef USE_PPC_CRYPTO
413 /* PowerPC Crypto implementations of AES */
414 extern void _gcry_aes_ppc8_setkey(RIJNDAEL_context *ctx, const byte *key);
415 extern void _gcry_aes_ppc8_prepare_decryption(RIJNDAEL_context *ctx);
416
417 extern unsigned int _gcry_aes_ppc8_encrypt(const RIJNDAEL_context *ctx,
418 unsigned char *dst,
419 const unsigned char *src);
420 extern unsigned int _gcry_aes_ppc8_decrypt(const RIJNDAEL_context *ctx,
421 unsigned char *dst,
422 const unsigned char *src);
423
424 extern void _gcry_aes_ppc8_ecb_crypt (void *context, void *outbuf_arg,
425 const void *inbuf_arg, size_t nblocks,
426 int encrypt);
427
428 extern void _gcry_aes_ppc8_cfb_enc (void *context, unsigned char *iv,
429 void *outbuf_arg, const void *inbuf_arg,
430 size_t nblocks);
431 extern void _gcry_aes_ppc8_cbc_enc (void *context, unsigned char *iv,
432 void *outbuf_arg, const void *inbuf_arg,
433 size_t nblocks, int cbc_mac);
434 extern void _gcry_aes_ppc8_ctr_enc (void *context, unsigned char *ctr,
435 void *outbuf_arg, const void *inbuf_arg,
436 size_t nblocks);
437 extern void _gcry_aes_ppc8_cfb_dec (void *context, unsigned char *iv,
438 void *outbuf_arg, const void *inbuf_arg,
439 size_t nblocks);
440 extern void _gcry_aes_ppc8_cbc_dec (void *context, unsigned char *iv,
441 void *outbuf_arg, const void *inbuf_arg,
442 size_t nblocks);
443
444 extern size_t _gcry_aes_ppc8_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
445 const void *inbuf_arg, size_t nblocks,
446 int encrypt);
447 extern size_t _gcry_aes_ppc8_ocb_auth (gcry_cipher_hd_t c,
448 const void *abuf_arg, size_t nblocks);
449
450 extern void _gcry_aes_ppc8_xts_crypt (void *context, unsigned char *tweak,
451 void *outbuf_arg,
452 const void *inbuf_arg,
453 size_t nblocks, int encrypt);
454
455 extern void _gcry_aes_ppc8_ctr32le_enc (void *context, unsigned char *ctr,
456 void *outbuf_arg, const void *inbuf_arg,
457 size_t nblocks);
458 #endif /*USE_PPC_CRYPTO*/
459
460 #ifdef USE_PPC_CRYPTO_WITH_PPC9LE
461 /* Power9 little-endian crypto implementations of AES */
462 extern unsigned int _gcry_aes_ppc9le_encrypt(const RIJNDAEL_context *ctx,
463 unsigned char *dst,
464 const unsigned char *src);
465 extern unsigned int _gcry_aes_ppc9le_decrypt(const RIJNDAEL_context *ctx,
466 unsigned char *dst,
467 const unsigned char *src);
468
469 extern void _gcry_aes_ppc9le_ecb_crypt (void *context, void *outbuf_arg,
470 const void *inbuf_arg, size_t nblocks,
471 int encrypt);
472
473 extern void _gcry_aes_ppc9le_cfb_enc (void *context, unsigned char *iv,
474 void *outbuf_arg, const void *inbuf_arg,
475 size_t nblocks);
476 extern void _gcry_aes_ppc9le_cbc_enc (void *context, unsigned char *iv,
477 void *outbuf_arg, const void *inbuf_arg,
478 size_t nblocks, int cbc_mac);
479 extern void _gcry_aes_ppc9le_ctr_enc (void *context, unsigned char *ctr,
480 void *outbuf_arg, const void *inbuf_arg,
481 size_t nblocks);
482 extern void _gcry_aes_ppc9le_cfb_dec (void *context, unsigned char *iv,
483 void *outbuf_arg, const void *inbuf_arg,
484 size_t nblocks);
485 extern void _gcry_aes_ppc9le_cbc_dec (void *context, unsigned char *iv,
486 void *outbuf_arg, const void *inbuf_arg,
487 size_t nblocks);
488
489 extern size_t _gcry_aes_ppc9le_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
490 const void *inbuf_arg, size_t nblocks,
491 int encrypt);
492 extern size_t _gcry_aes_ppc9le_ocb_auth (gcry_cipher_hd_t c,
493 const void *abuf_arg, size_t nblocks);
494
495 extern void _gcry_aes_ppc9le_xts_crypt (void *context, unsigned char *tweak,
496 void *outbuf_arg,
497 const void *inbuf_arg,
498 size_t nblocks, int encrypt);
499
500 extern void _gcry_aes_ppc9le_ctr32le_enc (void *context, unsigned char *ctr,
501 void *outbuf_arg,
502 const void *inbuf_arg,
503 size_t nblocks);
504
505 extern size_t _gcry_aes_p10le_gcm_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
506 const void *inbuf_arg,
507 size_t nblocks, int encrypt);
508 #endif /*USE_PPC_CRYPTO_WITH_PPC9LE*/
509
510 #ifdef USE_S390X_CRYPTO
511 /* zSeries crypto implementations of AES */
512 extern int _gcry_aes_s390x_setup_acceleration(RIJNDAEL_context *ctx,
513 unsigned int keylen,
514 unsigned int hwfeatures,
515 cipher_bulk_ops_t *bulk_ops);
516 extern void _gcry_aes_s390x_setkey(RIJNDAEL_context *ctx, const byte *key);
517 extern void _gcry_aes_s390x_prepare_decryption(RIJNDAEL_context *ctx);
518
519 extern unsigned int _gcry_aes_s390x_encrypt(const RIJNDAEL_context *ctx,
520 unsigned char *dst,
521 const unsigned char *src);
522 extern unsigned int _gcry_aes_s390x_decrypt(const RIJNDAEL_context *ctx,
523 unsigned char *dst,
524 const unsigned char *src);
525
526 #endif /*USE_S390X_CRYPTO*/
527
528 static unsigned int do_encrypt (const RIJNDAEL_context *ctx, unsigned char *bx,
529 const unsigned char *ax);
530 static unsigned int do_decrypt (const RIJNDAEL_context *ctx, unsigned char *bx,
531 const unsigned char *ax);
532
533 static void _gcry_aes_cfb_enc (void *context, unsigned char *iv,
534 void *outbuf, const void *inbuf,
535 size_t nblocks);
536 static void _gcry_aes_cfb_dec (void *context, unsigned char *iv,
537 void *outbuf_arg, const void *inbuf_arg,
538 size_t nblocks);
539 static void _gcry_aes_cbc_enc (void *context, unsigned char *iv,
540 void *outbuf_arg, const void *inbuf_arg,
541 size_t nblocks, int cbc_mac);
542 static void _gcry_aes_cbc_dec (void *context, unsigned char *iv,
543 void *outbuf_arg, const void *inbuf_arg,
544 size_t nblocks);
545 static void _gcry_aes_ctr_enc (void *context, unsigned char *ctr,
546 void *outbuf_arg, const void *inbuf_arg,
547 size_t nblocks);
548 static size_t _gcry_aes_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
549 const void *inbuf_arg, size_t nblocks,
550 int encrypt);
551 static size_t _gcry_aes_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
552 size_t nblocks);
553 static void _gcry_aes_xts_crypt (void *context, unsigned char *tweak,
554 void *outbuf_arg, const void *inbuf_arg,
555 size_t nblocks, int encrypt);
556
557
558 /* All the numbers. */
559 #include "rijndael-tables.h"
560
561
562
563
564 /* Function prototypes. */
565 static const char *selftest(void);
566 static void prepare_decryption(RIJNDAEL_context *ctx);
567
568
569
570 /* Prefetching for encryption/decryption tables. */
571 static inline void prefetch_table(const volatile byte *tab, size_t len)
572 {
573 size_t i;
574
575 for (i = 0; len - i >= 8 * 32; i += 8 * 32)
576 {
577 (void)tab[i + 0 * 32];
578 (void)tab[i + 1 * 32];
579 (void)tab[i + 2 * 32];
580 (void)tab[i + 3 * 32];
581 (void)tab[i + 4 * 32];
582 (void)tab[i + 5 * 32];
583 (void)tab[i + 6 * 32];
584 (void)tab[i + 7 * 32];
585 }
586 for (; i < len; i += 32)
587 {
588 (void)tab[i];
589 }
590
591 (void)tab[len - 1];
592 }
593
594 static void prefetch_enc(void)
595 {
596 /* Modify counters to trigger copy-on-write and unsharing if physical pages
597 * of look-up table are shared between processes. Modifying counters also
598 * causes checksums for pages to change and hint same-page merging algorithm
599 * that these pages are frequently changing. */
600 enc_tables.counter_head++;
601 enc_tables.counter_tail++;
602
603 /* Prefetch look-up tables to cache. */
604 prefetch_table((const void *)&enc_tables, sizeof(enc_tables));
605 }
606
607 static void prefetch_dec(void)
608 {
609 /* Modify counters to trigger copy-on-write and unsharing if physical pages
610 * of look-up table are shared between processes. Modifying counters also
611 * causes checksums for pages to change and hint same-page merging algorithm
612 * that these pages are frequently changing. */
613 dec_tables.counter_head++;
614 dec_tables.counter_tail++;
615
616 /* Prefetch look-up tables to cache. */
617 prefetch_table((const void *)&dec_tables, sizeof(dec_tables));
618 }
619
620
621
622 static inline u32
623 sbox4(u32 inb4)
624 {
625 u32 out;
626 out = (encT[(inb4 >> 0) & 0xffU] & 0xff00U) >> 8;
627 out |= (encT[(inb4 >> 8) & 0xffU] & 0xff00U) >> 0;
628 out |= (encT[(inb4 >> 16) & 0xffU] & 0xff0000U) << 0;
629 out |= (encT[(inb4 >> 24) & 0xffU] & 0xff0000U) << 8;
630 return out;
631 }
632
633 /* Perform the key setup. */
634 static gcry_err_code_t
635 do_setkey (RIJNDAEL_context *ctx, const byte *key, const unsigned keylen,
636 cipher_bulk_ops_t *bulk_ops)
637 {
638 static int initialized = 0;
639 static const char *selftest_failed = 0;
640 void (*hw_setkey)(RIJNDAEL_context *ctx, const byte *key) = NULL;
641 int rounds;
642 unsigned int KC;
643 unsigned int hwfeatures;
644
645 /* The on-the-fly self tests are only run in non-fips mode. In fips
646 mode explicit self-tests are required. Actually the on-the-fly
647 self-tests are not fully thread-safe and it might happen that a
648 failed self-test won't get noticed in another thread.
649
650 FIXME: We might want to have a central registry of succeeded
651 self-tests. */
652 if (!fips_mode () && !initialized)
653 {
654 initialized = 1;
655 selftest_failed = selftest ();
656 if (selftest_failed)
657 log_error ("%s\n", selftest_failed );
658 }
659 if (selftest_failed)
660 return GPG_ERR_SELFTEST_FAILED;
661
662 if( keylen == 128/8 )
663 {
664 rounds = 10;
665 KC = 4;
666 }
667 else if ( keylen == 192/8 )
668 {
669 rounds = 12;
670 KC = 6;
671 }
672 else if ( keylen == 256/8 )
673 {
674 rounds = 14;
675 KC = 8;
676 }
677 else
678 return GPG_ERR_INV_KEYLEN;
679
680 ctx->rounds = rounds;
681 hwfeatures = _gcry_get_hw_features ();
682
683 ctx->decryption_prepared = 0;
684
685 /* Setup default bulk encryption routines. */
686 memset (bulk_ops, 0, sizeof(*bulk_ops));
687 bulk_ops->cfb_enc = _gcry_aes_cfb_enc;
688 bulk_ops->cfb_dec = _gcry_aes_cfb_dec;
689 bulk_ops->cbc_enc = _gcry_aes_cbc_enc;
690 bulk_ops->cbc_dec = _gcry_aes_cbc_dec;
691 bulk_ops->ctr_enc = _gcry_aes_ctr_enc;
692 bulk_ops->ocb_crypt = _gcry_aes_ocb_crypt;
693 bulk_ops->ocb_auth = _gcry_aes_ocb_auth;
694 bulk_ops->xts_crypt = _gcry_aes_xts_crypt;
695
696 (void)hwfeatures;
697
698 if (0)
699 {
700 ;
701 }
702 #ifdef USE_AESNI
703 else if (hwfeatures & HWF_INTEL_AESNI)
704 {
705 hw_setkey = _gcry_aes_aesni_do_setkey;
706 ctx->encrypt_fn = _gcry_aes_aesni_encrypt;
707 ctx->decrypt_fn = _gcry_aes_aesni_decrypt;
708 ctx->prefetch_enc_fn = NULL;
709 ctx->prefetch_dec_fn = NULL;
710 ctx->prepare_decryption = _gcry_aes_aesni_prepare_decryption;
711 ctx->use_avx = !!(hwfeatures & HWF_INTEL_AVX);
712 ctx->use_avx2 = !!(hwfeatures & HWF_INTEL_AVX2);
713
714 /* Setup AES-NI bulk encryption routines. */
715 bulk_ops->cfb_enc = _gcry_aes_aesni_cfb_enc;
716 bulk_ops->cfb_dec = _gcry_aes_aesni_cfb_dec;
717 bulk_ops->cbc_enc = _gcry_aes_aesni_cbc_enc;
718 bulk_ops->cbc_dec = _gcry_aes_aesni_cbc_dec;
719 bulk_ops->ctr_enc = _gcry_aes_aesni_ctr_enc;
720 bulk_ops->ctr32le_enc = _gcry_aes_aesni_ctr32le_enc;
721 bulk_ops->ocb_crypt = _gcry_aes_aesni_ocb_crypt;
722 bulk_ops->ocb_auth = _gcry_aes_aesni_ocb_auth;
723 bulk_ops->xts_crypt = _gcry_aes_aesni_xts_crypt;
724 bulk_ops->ecb_crypt = _gcry_aes_aesni_ecb_crypt;
725
726 #ifdef USE_VAES
727 if ((hwfeatures & HWF_INTEL_VAES_VPCLMUL) &&
728 (hwfeatures & HWF_INTEL_AVX2))
729 {
730 #ifdef USE_VAES_AVX512
731 ctx->use_vaes_avx512 = !!(hwfeatures & HWF_INTEL_AVX512);
732 #endif
733
734 /* Setup VAES bulk encryption routines. */
735 bulk_ops->cfb_dec = _gcry_aes_vaes_cfb_dec;
736 bulk_ops->cbc_dec = _gcry_aes_vaes_cbc_dec;
737 bulk_ops->ctr_enc = _gcry_aes_vaes_ctr_enc;
738 bulk_ops->ctr32le_enc = _gcry_aes_vaes_ctr32le_enc;
739 bulk_ops->ocb_crypt = _gcry_aes_vaes_ocb_crypt;
740 bulk_ops->ocb_auth = _gcry_aes_vaes_ocb_auth;
741 bulk_ops->xts_crypt = _gcry_aes_vaes_xts_crypt;
742 bulk_ops->ecb_crypt = _gcry_aes_vaes_ecb_crypt;
743 }
744 #endif
745 #ifdef USE_VAES_I386
746 if ((hwfeatures & HWF_INTEL_VAES_VPCLMUL) &&
747 (hwfeatures & HWF_INTEL_AVX2))
748 {
749 /* Setup VAES bulk encryption routines. */
750 bulk_ops->cfb_dec = _gcry_aes_vaes_cfb_dec;
751 bulk_ops->cbc_dec = _gcry_aes_vaes_cbc_dec;
752 bulk_ops->ctr_enc = _gcry_aes_vaes_ctr_enc;
753 bulk_ops->ctr32le_enc = _gcry_aes_vaes_ctr32le_enc;
754 bulk_ops->ocb_crypt = _gcry_aes_vaes_ocb_crypt;
755 bulk_ops->ocb_auth = _gcry_aes_vaes_ocb_auth;
756 bulk_ops->xts_crypt = _gcry_aes_vaes_xts_crypt;
757 bulk_ops->ecb_crypt = _gcry_aes_vaes_ecb_crypt;
758 }
759 #endif
760 }
761 #endif
762 #ifdef USE_PADLOCK
763 else if ((hwfeatures & HWF_PADLOCK_AES) && keylen == 128/8)
764 {
765 ctx->encrypt_fn = _gcry_aes_padlock_encrypt;
766 ctx->decrypt_fn = _gcry_aes_padlock_decrypt;
767 ctx->prefetch_enc_fn = NULL;
768 ctx->prefetch_dec_fn = NULL;
769 ctx->prepare_decryption = _gcry_aes_padlock_prepare_decryption;
770 memcpy (ctx->padlockkey, key, keylen);
771 }
772 #endif
773 #ifdef USE_SSSE3
774 else if (hwfeatures & HWF_INTEL_SSSE3)
775 {
776 hw_setkey = _gcry_aes_ssse3_do_setkey;
777 ctx->encrypt_fn = _gcry_aes_ssse3_encrypt;
778 ctx->decrypt_fn = _gcry_aes_ssse3_decrypt;
779 ctx->prefetch_enc_fn = NULL;
780 ctx->prefetch_dec_fn = NULL;
781 ctx->prepare_decryption = _gcry_aes_ssse3_prepare_decryption;
782
783 /* Setup SSSE3 bulk encryption routines. */
784 bulk_ops->cfb_enc = _gcry_aes_ssse3_cfb_enc;
785 bulk_ops->cfb_dec = _gcry_aes_ssse3_cfb_dec;
786 bulk_ops->cbc_enc = _gcry_aes_ssse3_cbc_enc;
787 bulk_ops->cbc_dec = _gcry_aes_ssse3_cbc_dec;
788 bulk_ops->ctr_enc = _gcry_aes_ssse3_ctr_enc;
789 bulk_ops->ocb_crypt = _gcry_aes_ssse3_ocb_crypt;
790 bulk_ops->ocb_auth = _gcry_aes_ssse3_ocb_auth;
791 }
792 #endif
793 #ifdef USE_ARM_CE
794 else if (hwfeatures & HWF_ARM_AES)
795 {
796 hw_setkey = _gcry_aes_armv8_ce_setkey;
797 ctx->encrypt_fn = _gcry_aes_armv8_ce_encrypt;
798 ctx->decrypt_fn = _gcry_aes_armv8_ce_decrypt;
799 ctx->prefetch_enc_fn = NULL;
800 ctx->prefetch_dec_fn = NULL;
801 ctx->prepare_decryption = _gcry_aes_armv8_ce_prepare_decryption;
802
803 /* Setup ARM-CE bulk encryption routines. */
804 bulk_ops->cfb_enc = _gcry_aes_armv8_ce_cfb_enc;
805 bulk_ops->cfb_dec = _gcry_aes_armv8_ce_cfb_dec;
806 bulk_ops->cbc_enc = _gcry_aes_armv8_ce_cbc_enc;
807 bulk_ops->cbc_dec = _gcry_aes_armv8_ce_cbc_dec;
808 bulk_ops->ctr_enc = _gcry_aes_armv8_ce_ctr_enc;
809 bulk_ops->ctr32le_enc = _gcry_aes_armv8_ce_ctr32le_enc;
810 bulk_ops->ocb_crypt = _gcry_aes_armv8_ce_ocb_crypt;
811 bulk_ops->ocb_auth = _gcry_aes_armv8_ce_ocb_auth;
812 bulk_ops->xts_crypt = _gcry_aes_armv8_ce_xts_crypt;
813 bulk_ops->ecb_crypt = _gcry_aes_armv8_ce_ecb_crypt;
814 }
815 #endif
816 #ifdef USE_VP_AARCH64
817 else if (hwfeatures & HWF_ARM_NEON)
818 {
819 hw_setkey = _gcry_aes_vp_aarch64_do_setkey;
820 ctx->encrypt_fn = _gcry_aes_vp_aarch64_encrypt;
821 ctx->decrypt_fn = _gcry_aes_vp_aarch64_decrypt;
822 ctx->prefetch_enc_fn = NULL;
823 ctx->prefetch_dec_fn = NULL;
824 ctx->prepare_decryption = _gcry_aes_vp_aarch64_prepare_decryption;
825
826 /* Setup vector permute AArch64 bulk encryption routines. */
827 bulk_ops->cfb_enc = _gcry_aes_vp_aarch64_cfb_enc;
828 bulk_ops->cfb_dec = _gcry_aes_vp_aarch64_cfb_dec;
829 bulk_ops->cbc_enc = _gcry_aes_vp_aarch64_cbc_enc;
830 bulk_ops->cbc_dec = _gcry_aes_vp_aarch64_cbc_dec;
831 bulk_ops->ctr_enc = _gcry_aes_vp_aarch64_ctr_enc;
832 bulk_ops->ctr32le_enc = _gcry_aes_vp_aarch64_ctr32le_enc;
833 bulk_ops->ocb_crypt = _gcry_aes_vp_aarch64_ocb_crypt;
834 bulk_ops->ocb_auth = _gcry_aes_vp_aarch64_ocb_auth;
835 bulk_ops->ecb_crypt = _gcry_aes_vp_aarch64_ecb_crypt;
836 bulk_ops->xts_crypt = _gcry_aes_vp_aarch64_xts_crypt;
837 }
838 #endif
839 #ifdef USE_RISCV_V_CRYPTO
840 else if ((hwfeatures & HWF_RISCV_IMAFDC)
841 && (hwfeatures & HWF_RISCV_B) /* Mandatory in RVA23U64 */
842 && (hwfeatures & HWF_RISCV_V) /* Mandatory in RVA23U64 */
843 && (hwfeatures & HWF_RISCV_ZVKNED) /* Optional in RVA23U64 */
844 && _gcry_aes_riscv_zvkned_setup_acceleration(ctx))
845 {
846 hw_setkey = _gcry_aes_riscv_zvkned_setkey;
847 ctx->encrypt_fn = _gcry_aes_riscv_zvkned_encrypt;
848 ctx->decrypt_fn = _gcry_aes_riscv_zvkned_decrypt;
849 ctx->prefetch_enc_fn = NULL;
850 ctx->prefetch_dec_fn = NULL;
851 ctx->prepare_decryption = _gcry_aes_riscv_zvkned_prepare_decryption;
852
853 /* Setup RISC-V vector cryptography bulk encryption routines. */
854 bulk_ops->cfb_enc = _gcry_aes_riscv_zvkned_cfb_enc;
855 bulk_ops->cfb_dec = _gcry_aes_riscv_zvkned_cfb_dec;
856 bulk_ops->cbc_enc = _gcry_aes_riscv_zvkned_cbc_enc;
857 bulk_ops->cbc_dec = _gcry_aes_riscv_zvkned_cbc_dec;
858 bulk_ops->ctr_enc = _gcry_aes_riscv_zvkned_ctr_enc;
859 bulk_ops->ctr32le_enc = _gcry_aes_riscv_zvkned_ctr32le_enc;
860 bulk_ops->ocb_crypt = _gcry_aes_riscv_zvkned_ocb_crypt;
861 bulk_ops->ocb_auth = _gcry_aes_riscv_zvkned_ocb_auth;
862 bulk_ops->ecb_crypt = _gcry_aes_riscv_zvkned_ecb_crypt;
863 bulk_ops->xts_crypt = _gcry_aes_riscv_zvkned_xts_crypt;
864 }
865 #endif
866 #ifdef USE_VP_RISCV
867 else if ((hwfeatures & HWF_RISCV_IMAFDC)
868 && (hwfeatures & HWF_RISCV_B) /* Mandatory in RVA22U64 */
869 && (hwfeatures & HWF_RISCV_V) /* Optional in RVA22U64 */
870 && _gcry_aes_vp_riscv_setup_acceleration(ctx))
871 {
872 hw_setkey = _gcry_aes_vp_riscv_do_setkey;
873 ctx->encrypt_fn = _gcry_aes_vp_riscv_encrypt;
874 ctx->decrypt_fn = _gcry_aes_vp_riscv_decrypt;
875 ctx->prefetch_enc_fn = NULL;
876 ctx->prefetch_dec_fn = NULL;
877 ctx->prepare_decryption = _gcry_aes_vp_riscv_prepare_decryption;
878
879 /* Setup vector permute RISC-V bulk encryption routines. */
880 bulk_ops->cfb_enc = _gcry_aes_vp_riscv_cfb_enc;
881 bulk_ops->cfb_dec = _gcry_aes_vp_riscv_cfb_dec;
882 bulk_ops->cbc_enc = _gcry_aes_vp_riscv_cbc_enc;
883 bulk_ops->cbc_dec = _gcry_aes_vp_riscv_cbc_dec;
884 bulk_ops->ctr_enc = _gcry_aes_vp_riscv_ctr_enc;
885 bulk_ops->ctr32le_enc = _gcry_aes_vp_riscv_ctr32le_enc;
886 bulk_ops->ocb_crypt = _gcry_aes_vp_riscv_ocb_crypt;
887 bulk_ops->ocb_auth = _gcry_aes_vp_riscv_ocb_auth;
888 bulk_ops->ecb_crypt = _gcry_aes_vp_riscv_ecb_crypt;
889 bulk_ops->xts_crypt = _gcry_aes_vp_riscv_xts_crypt;
890 }
891 #endif
892 #ifdef USE_PPC_CRYPTO_WITH_PPC9LE
893 else if ((hwfeatures & HWF_PPC_VCRYPTO) && (hwfeatures & HWF_PPC_ARCH_3_00))
894 {
895 hw_setkey = _gcry_aes_ppc8_setkey;
896 ctx->encrypt_fn = _gcry_aes_ppc9le_encrypt;
897 ctx->decrypt_fn = _gcry_aes_ppc9le_decrypt;
898 ctx->prefetch_enc_fn = NULL;
899 ctx->prefetch_dec_fn = NULL;
900 ctx->prepare_decryption = _gcry_aes_ppc8_prepare_decryption;
901
902 /* Setup PPC9LE bulk encryption routines. */
903 bulk_ops->ecb_crypt = _gcry_aes_ppc9le_ecb_crypt;
904 bulk_ops->cfb_enc = _gcry_aes_ppc9le_cfb_enc;
905 bulk_ops->cfb_dec = _gcry_aes_ppc9le_cfb_dec;
906 bulk_ops->cbc_enc = _gcry_aes_ppc9le_cbc_enc;
907 bulk_ops->cbc_dec = _gcry_aes_ppc9le_cbc_dec;
908 bulk_ops->ctr_enc = _gcry_aes_ppc9le_ctr_enc;
909 bulk_ops->ocb_crypt = _gcry_aes_ppc9le_ocb_crypt;
910 bulk_ops->ocb_auth = _gcry_aes_ppc9le_ocb_auth;
911 bulk_ops->xts_crypt = _gcry_aes_ppc9le_xts_crypt;
912 bulk_ops->ctr32le_enc = _gcry_aes_ppc9le_ctr32le_enc;
913 if (hwfeatures & HWF_PPC_ARCH_3_10) /* for P10 */
914 bulk_ops->gcm_crypt = _gcry_aes_p10le_gcm_crypt;
915 # ifdef ENABLE_FORCE_SOFT_HWFEATURES
916 /* HWF_PPC_ARCH_3_10 above is used as soft HW-feature indicator for P10.
917 * Actual implementation works with HWF_PPC_ARCH_3_00 also. */
918 if (hwfeatures & HWF_PPC_ARCH_3_00)
919 bulk_ops->gcm_crypt = _gcry_aes_p10le_gcm_crypt;
920 # endif
921 }
922 #endif
923 #ifdef USE_PPC_CRYPTO
924 else if (hwfeatures & HWF_PPC_VCRYPTO)
925 {
926 hw_setkey = _gcry_aes_ppc8_setkey;
927 ctx->encrypt_fn = _gcry_aes_ppc8_encrypt;
928 ctx->decrypt_fn = _gcry_aes_ppc8_decrypt;
929 ctx->prefetch_enc_fn = NULL;
930 ctx->prefetch_dec_fn = NULL;
931 ctx->prepare_decryption = _gcry_aes_ppc8_prepare_decryption;
932
933 /* Setup PPC8 bulk encryption routines. */
934 bulk_ops->ecb_crypt = _gcry_aes_ppc8_ecb_crypt;
935 bulk_ops->cfb_enc = _gcry_aes_ppc8_cfb_enc;
936 bulk_ops->cfb_dec = _gcry_aes_ppc8_cfb_dec;
937 bulk_ops->cbc_enc = _gcry_aes_ppc8_cbc_enc;
938 bulk_ops->cbc_dec = _gcry_aes_ppc8_cbc_dec;
939 bulk_ops->ctr_enc = _gcry_aes_ppc8_ctr_enc;
940 bulk_ops->ocb_crypt = _gcry_aes_ppc8_ocb_crypt;
941 bulk_ops->ocb_auth = _gcry_aes_ppc8_ocb_auth;
942 bulk_ops->xts_crypt = _gcry_aes_ppc8_xts_crypt;
943 bulk_ops->ctr32le_enc = _gcry_aes_ppc8_ctr32le_enc;
944 }
945 #endif
946 #ifdef USE_S390X_CRYPTO
947 else if (_gcry_aes_s390x_setup_acceleration (ctx, keylen, hwfeatures,
948 bulk_ops))
949 {
950 hw_setkey = _gcry_aes_s390x_setkey;
951 ctx->encrypt_fn = _gcry_aes_s390x_encrypt;
952 ctx->decrypt_fn = _gcry_aes_s390x_decrypt;
953 ctx->prefetch_enc_fn = NULL;
954 ctx->prefetch_dec_fn = NULL;
955 ctx->prepare_decryption = _gcry_aes_s390x_prepare_decryption;
956 }
957 #endif
958 else
959 {
960 ctx->encrypt_fn = do_encrypt;
961 ctx->decrypt_fn = do_decrypt;
962 ctx->prefetch_enc_fn = prefetch_enc;
963 ctx->prefetch_dec_fn = prefetch_dec;
964 ctx->prepare_decryption = prepare_decryption;
965 }
966
967 /* NB: We don't yet support Padlock hardware key generation. */
968
969 if (hw_setkey)
970 {
971 hw_setkey (ctx, key);
972 }
973 else
974 {
975 u32 W_prev;
976 u32 *W_u32 = ctx->keyschenc32b;
977 byte rcon = 1;
978 unsigned int i, j;
979
980 prefetch_enc();
981
982 for (i = 0; i < KC; i += 2)
983 {
984 W_u32[i + 0] = buf_get_le32(key + i * 4 + 0);
985 W_u32[i + 1] = buf_get_le32(key + i * 4 + 4);
986 }
987
988 for (i = KC, j = KC, W_prev = W_u32[KC - 1];
989 i < 4 * (rounds + 1);
990 i += 2, j += 2)
991 {
992 u32 temp0 = W_prev;
993 u32 temp1;
994
995 if (j == KC)
996 {
997 j = 0;
998 temp0 = sbox4(rol(temp0, 24)) ^ rcon;
999 rcon = ((rcon << 1) ^ (ct_ulong_gen_mask(rcon >> 7) & 0x1b)) & 0xff;
1000 }
1001 else if (KC == 8 && j == 4)
1002 {
1003 temp0 = sbox4(temp0);
1004 }
1005
1006 temp1 = W_u32[i - KC + 0];
1007
1008 W_u32[i + 0] = temp0 ^ temp1;
1009 W_u32[i + 1] = W_u32[i - KC + 1] ^ temp0 ^ temp1;
1010 W_prev = W_u32[i + 1];
1011 }
1012 }
1013
1014 return 0;
1015 }
1016
1017
1018 static gcry_err_code_t
1019 rijndael_setkey (void *context, const byte *key, const unsigned keylen,
1020 cipher_bulk_ops_t *bulk_ops)
1021 {
1022 RIJNDAEL_context *ctx = context;
1023 return do_setkey (ctx, key, keylen, bulk_ops);
1024 }
1025
1026
1027 /* Make a decryption key from an encryption key. */
1028 static void
1029 prepare_decryption( RIJNDAEL_context *ctx )
1030 {
1031 const byte *sbox = ((const byte *)encT) + 1;
1032 int r;
1033
1034 prefetch_enc();
1035 prefetch_dec();
1036
1037 ctx->keyschdec32[0][0] = ctx->keyschenc32[0][0];
1038 ctx->keyschdec32[0][1] = ctx->keyschenc32[0][1];
1039 ctx->keyschdec32[0][2] = ctx->keyschenc32[0][2];
1040 ctx->keyschdec32[0][3] = ctx->keyschenc32[0][3];
1041
1042 for (r = 1; r < ctx->rounds; r++)
1043 {
1044 u32 *wi = ctx->keyschenc32[r];
1045 u32 *wo = ctx->keyschdec32[r];
1046 u32 wt;
1047
1048 wt = wi[0];
1049 wo[0] = rol(decT[sbox[(byte)(wt >> 0) * 4]], 8 * 0)
1050 ^ rol(decT[sbox[(byte)(wt >> 8) * 4]], 8 * 1)
1051 ^ rol(decT[sbox[(byte)(wt >> 16) * 4]], 8 * 2)
1052 ^ rol(decT[sbox[(byte)(wt >> 24) * 4]], 8 * 3);
1053
1054 wt = wi[1];
1055 wo[1] = rol(decT[sbox[(byte)(wt >> 0) * 4]], 8 * 0)
1056 ^ rol(decT[sbox[(byte)(wt >> 8) * 4]], 8 * 1)
1057 ^ rol(decT[sbox[(byte)(wt >> 16) * 4]], 8 * 2)
1058 ^ rol(decT[sbox[(byte)(wt >> 24) * 4]], 8 * 3);
1059
1060 wt = wi[2];
1061 wo[2] = rol(decT[sbox[(byte)(wt >> 0) * 4]], 8 * 0)
1062 ^ rol(decT[sbox[(byte)(wt >> 8) * 4]], 8 * 1)
1063 ^ rol(decT[sbox[(byte)(wt >> 16) * 4]], 8 * 2)
1064 ^ rol(decT[sbox[(byte)(wt >> 24) * 4]], 8 * 3);
1065
1066 wt = wi[3];
1067 wo[3] = rol(decT[sbox[(byte)(wt >> 0) * 4]], 8 * 0)
1068 ^ rol(decT[sbox[(byte)(wt >> 8) * 4]], 8 * 1)
1069 ^ rol(decT[sbox[(byte)(wt >> 16) * 4]], 8 * 2)
1070 ^ rol(decT[sbox[(byte)(wt >> 24) * 4]], 8 * 3);
1071 }
1072
1073 ctx->keyschdec32[r][0] = ctx->keyschenc32[r][0];
1074 ctx->keyschdec32[r][1] = ctx->keyschenc32[r][1];
1075 ctx->keyschdec32[r][2] = ctx->keyschenc32[r][2];
1076 ctx->keyschdec32[r][3] = ctx->keyschenc32[r][3];
1077 }
1078
1079
1080 #if !defined(USE_ARM_ASM) && !defined(USE_AMD64_ASM)
1081 /* Encrypt one block. A and B may be the same. */
1082 static unsigned int
1083 do_encrypt_fn (const RIJNDAEL_context *ctx, unsigned char *b,
1084 const unsigned char *a)
1085 {
1086 #define rk (ctx->keyschenc32)
1087 const byte *sbox = ((const byte *)encT) + 1;
1088 int rounds = ctx->rounds;
1089 int r;
1090 u32 sa[4];
1091 u32 sb[4];
1092
1093 sb[0] = buf_get_le32(a + 0);
1094 sb[1] = buf_get_le32(a + 4);
1095 sb[2] = buf_get_le32(a + 8);
1096 sb[3] = buf_get_le32(a + 12);
1097
1098 sa[0] = sb[0] ^ rk[0][0];
1099 sa[1] = sb[1] ^ rk[0][1];
1100 sa[2] = sb[2] ^ rk[0][2];
1101 sa[3] = sb[3] ^ rk[0][3];
1102
1103 sb[0] = rol(encT[(byte)(sa[0] >> (0 * 8))], (0 * 8));
1104 sb[3] = rol(encT[(byte)(sa[0] >> (1 * 8))], (1 * 8));
1105 sb[2] = rol(encT[(byte)(sa[0] >> (2 * 8))], (2 * 8));
1106 sb[1] = rol(encT[(byte)(sa[0] >> (3 * 8))], (3 * 8));
1107 sa[0] = rk[1][0] ^ sb[0];
1108
1109 sb[1] ^= rol(encT[(byte)(sa[1] >> (0 * 8))], (0 * 8));
1110 sa[0] ^= rol(encT[(byte)(sa[1] >> (1 * 8))], (1 * 8));
1111 sb[3] ^= rol(encT[(byte)(sa[1] >> (2 * 8))], (2 * 8));
1112 sb[2] ^= rol(encT[(byte)(sa[1] >> (3 * 8))], (3 * 8));
1113 sa[1] = rk[1][1] ^ sb[1];
1114
1115 sb[2] ^= rol(encT[(byte)(sa[2] >> (0 * 8))], (0 * 8));
1116 sa[1] ^= rol(encT[(byte)(sa[2] >> (1 * 8))], (1 * 8));
1117 sa[0] ^= rol(encT[(byte)(sa[2] >> (2 * 8))], (2 * 8));
1118 sb[3] ^= rol(encT[(byte)(sa[2] >> (3 * 8))], (3 * 8));
1119 sa[2] = rk[1][2] ^ sb[2];
1120
1121 sb[3] ^= rol(encT[(byte)(sa[3] >> (0 * 8))], (0 * 8));
1122 sa[2] ^= rol(encT[(byte)(sa[3] >> (1 * 8))], (1 * 8));
1123 sa[1] ^= rol(encT[(byte)(sa[3] >> (2 * 8))], (2 * 8));
1124 sa[0] ^= rol(encT[(byte)(sa[3] >> (3 * 8))], (3 * 8));
1125 sa[3] = rk[1][3] ^ sb[3];
1126
1127 for (r = 2; r < rounds; r++)
1128 {
1129 sb[0] = rol(encT[(byte)(sa[0] >> (0 * 8))], (0 * 8));
1130 sb[3] = rol(encT[(byte)(sa[0] >> (1 * 8))], (1 * 8));
1131 sb[2] = rol(encT[(byte)(sa[0] >> (2 * 8))], (2 * 8));
1132 sb[1] = rol(encT[(byte)(sa[0] >> (3 * 8))], (3 * 8));
1133 sa[0] = rk[r][0] ^ sb[0];
1134
1135 sb[1] ^= rol(encT[(byte)(sa[1] >> (0 * 8))], (0 * 8));
1136 sa[0] ^= rol(encT[(byte)(sa[1] >> (1 * 8))], (1 * 8));
1137 sb[3] ^= rol(encT[(byte)(sa[1] >> (2 * 8))], (2 * 8));
1138 sb[2] ^= rol(encT[(byte)(sa[1] >> (3 * 8))], (3 * 8));
1139 sa[1] = rk[r][1] ^ sb[1];
1140
1141 sb[2] ^= rol(encT[(byte)(sa[2] >> (0 * 8))], (0 * 8));
1142 sa[1] ^= rol(encT[(byte)(sa[2] >> (1 * 8))], (1 * 8));
1143 sa[0] ^= rol(encT[(byte)(sa[2] >> (2 * 8))], (2 * 8));
1144 sb[3] ^= rol(encT[(byte)(sa[2] >> (3 * 8))], (3 * 8));
1145 sa[2] = rk[r][2] ^ sb[2];
1146
1147 sb[3] ^= rol(encT[(byte)(sa[3] >> (0 * 8))], (0 * 8));
1148 sa[2] ^= rol(encT[(byte)(sa[3] >> (1 * 8))], (1 * 8));
1149 sa[1] ^= rol(encT[(byte)(sa[3] >> (2 * 8))], (2 * 8));
1150 sa[0] ^= rol(encT[(byte)(sa[3] >> (3 * 8))], (3 * 8));
1151 sa[3] = rk[r][3] ^ sb[3];
1152
1153 r++;
1154
1155 sb[0] = rol(encT[(byte)(sa[0] >> (0 * 8))], (0 * 8));
1156 sb[3] = rol(encT[(byte)(sa[0] >> (1 * 8))], (1 * 8));
1157 sb[2] = rol(encT[(byte)(sa[0] >> (2 * 8))], (2 * 8));
1158 sb[1] = rol(encT[(byte)(sa[0] >> (3 * 8))], (3 * 8));
1159 sa[0] = rk[r][0] ^ sb[0];
1160
1161 sb[1] ^= rol(encT[(byte)(sa[1] >> (0 * 8))], (0 * 8));
1162 sa[0] ^= rol(encT[(byte)(sa[1] >> (1 * 8))], (1 * 8));
1163 sb[3] ^= rol(encT[(byte)(sa[1] >> (2 * 8))], (2 * 8));
1164 sb[2] ^= rol(encT[(byte)(sa[1] >> (3 * 8))], (3 * 8));
1165 sa[1] = rk[r][1] ^ sb[1];
1166
1167 sb[2] ^= rol(encT[(byte)(sa[2] >> (0 * 8))], (0 * 8));
1168 sa[1] ^= rol(encT[(byte)(sa[2] >> (1 * 8))], (1 * 8));
1169 sa[0] ^= rol(encT[(byte)(sa[2] >> (2 * 8))], (2 * 8));
1170 sb[3] ^= rol(encT[(byte)(sa[2] >> (3 * 8))], (3 * 8));
1171 sa[2] = rk[r][2] ^ sb[2];
1172
1173 sb[3] ^= rol(encT[(byte)(sa[3] >> (0 * 8))], (0 * 8));
1174 sa[2] ^= rol(encT[(byte)(sa[3] >> (1 * 8))], (1 * 8));
1175 sa[1] ^= rol(encT[(byte)(sa[3] >> (2 * 8))], (2 * 8));
1176 sa[0] ^= rol(encT[(byte)(sa[3] >> (3 * 8))], (3 * 8));
1177 sa[3] = rk[r][3] ^ sb[3];
1178 }
1179
1180 /* Last round is special. */
1181
1182 sb[0] = ((u32)sbox[(byte)(sa[0] >> (0 * 8)) * 4]) << (0 * 8);
1183 sb[3] = ((u32)sbox[(byte)(sa[0] >> (1 * 8)) * 4]) << (1 * 8);
1184 sb[2] = ((u32)sbox[(byte)(sa[0] >> (2 * 8)) * 4]) << (2 * 8);
1185 sb[1] = ((u32)sbox[(byte)(sa[0] >> (3 * 8)) * 4]) << (3 * 8);
1186 sa[0] = rk[r][0] ^ sb[0];
1187
1188 sb[1] ^= ((u32)sbox[(byte)(sa[1] >> (0 * 8)) * 4]) << (0 * 8);
1189 sa[0] ^= ((u32)sbox[(byte)(sa[1] >> (1 * 8)) * 4]) << (1 * 8);
1190 sb[3] ^= ((u32)sbox[(byte)(sa[1] >> (2 * 8)) * 4]) << (2 * 8);
1191 sb[2] ^= ((u32)sbox[(byte)(sa[1] >> (3 * 8)) * 4]) << (3 * 8);
1192 sa[1] = rk[r][1] ^ sb[1];
1193
1194 sb[2] ^= ((u32)sbox[(byte)(sa[2] >> (0 * 8)) * 4]) << (0 * 8);
1195 sa[1] ^= ((u32)sbox[(byte)(sa[2] >> (1 * 8)) * 4]) << (1 * 8);
1196 sa[0] ^= ((u32)sbox[(byte)(sa[2] >> (2 * 8)) * 4]) << (2 * 8);
1197 sb[3] ^= ((u32)sbox[(byte)(sa[2] >> (3 * 8)) * 4]) << (3 * 8);
1198 sa[2] = rk[r][2] ^ sb[2];
1199
1200 sb[3] ^= ((u32)sbox[(byte)(sa[3] >> (0 * 8)) * 4]) << (0 * 8);
1201 sa[2] ^= ((u32)sbox[(byte)(sa[3] >> (1 * 8)) * 4]) << (1 * 8);
1202 sa[1] ^= ((u32)sbox[(byte)(sa[3] >> (2 * 8)) * 4]) << (2 * 8);
1203 sa[0] ^= ((u32)sbox[(byte)(sa[3] >> (3 * 8)) * 4]) << (3 * 8);
1204 sa[3] = rk[r][3] ^ sb[3];
1205
1206 buf_put_le32(b + 0, sa[0]);
1207 buf_put_le32(b + 4, sa[1]);
1208 buf_put_le32(b + 8, sa[2]);
1209 buf_put_le32(b + 12, sa[3]);
1210 #undef rk
1211
1212 return (56 + 2*sizeof(int));
1213 }
1214 #endif /*!USE_ARM_ASM && !USE_AMD64_ASM*/
1215
1216
1217 static unsigned int
1218 do_encrypt (const RIJNDAEL_context *ctx,
1219 unsigned char *bx, const unsigned char *ax)
1220 {
1221 #ifdef USE_AMD64_ASM
1222 return _gcry_aes_amd64_encrypt_block(ctx->keyschenc, bx, ax, ctx->rounds,
1223 enc_tables.T);
1224 #elif defined(USE_ARM_ASM)
1225 return _gcry_aes_arm_encrypt_block(ctx->keyschenc, bx, ax, ctx->rounds,
1226 enc_tables.T);
1227 #else
1228 return do_encrypt_fn (ctx, bx, ax);
1229 #endif /* !USE_ARM_ASM && !USE_AMD64_ASM*/
1230 }
1231
1232
1233 static unsigned int
1234 rijndael_encrypt (void *context, byte *b, const byte *a)
1235 {
1236 RIJNDAEL_context *ctx = context;
1237
1238 if (ctx->prefetch_enc_fn)
1239 ctx->prefetch_enc_fn();
1240
1241 return ctx->encrypt_fn (ctx, b, a);
1242 }
1243
1244
1245 /* Bulk encryption of complete blocks in CFB mode. Caller needs to
1246 make sure that IV is aligned on an unsigned long boundary. This
1247 function is only intended for the bulk encryption feature of
1248 cipher.c. */
1249 static void
1250 _gcry_aes_cfb_enc (void *context, unsigned char *iv,
1251 void *outbuf_arg, const void *inbuf_arg,
1252 size_t nblocks)
1253 {
1254 RIJNDAEL_context *ctx = context;
1255 unsigned char *outbuf = outbuf_arg;
1256 const unsigned char *inbuf = inbuf_arg;
1257 unsigned int burn_depth = 0;
1258 rijndael_cryptfn_t encrypt_fn = ctx->encrypt_fn;
1259
1260 if (ctx->prefetch_enc_fn)
1261 ctx->prefetch_enc_fn();
1262
1263 for ( ;nblocks; nblocks-- )
1264 {
1265 /* Encrypt the IV. */
1266 burn_depth = encrypt_fn (ctx, iv, iv);
1267 /* XOR the input with the IV and store input into IV. */
1268 cipher_block_xor_2dst(outbuf, iv, inbuf, BLOCKSIZE);
1269 outbuf += BLOCKSIZE;
1270 inbuf += BLOCKSIZE;
1271 }
1272
1273 if (burn_depth)
1274 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1275 }
1276
1277
1278 /* Bulk encryption of complete blocks in CBC mode. Caller needs to
1279 make sure that IV is aligned on an unsigned long boundary. This
1280 function is only intended for the bulk encryption feature of
1281 cipher.c. */
1282 static void
1283 _gcry_aes_cbc_enc (void *context, unsigned char *iv,
1284 void *outbuf_arg, const void *inbuf_arg,
1285 size_t nblocks, int cbc_mac)
1286 {
1287 RIJNDAEL_context *ctx = context;
1288 unsigned char *outbuf = outbuf_arg;
1289 const unsigned char *inbuf = inbuf_arg;
1290 unsigned char *last_iv;
1291 unsigned int burn_depth = 0;
1292 rijndael_cryptfn_t encrypt_fn = ctx->encrypt_fn;
1293
1294 if (ctx->prefetch_enc_fn)
1295 ctx->prefetch_enc_fn();
1296
1297 last_iv = iv;
1298
1299 for ( ;nblocks; nblocks-- )
1300 {
1301 cipher_block_xor(outbuf, inbuf, last_iv, BLOCKSIZE);
1302
1303 burn_depth = encrypt_fn (ctx, outbuf, outbuf);
1304
1305 last_iv = outbuf;
1306 inbuf += BLOCKSIZE;
1307 if (!cbc_mac)
1308 outbuf += BLOCKSIZE;
1309 }
1310
1311 if (last_iv != iv)
1312 cipher_block_cpy (iv, last_iv, BLOCKSIZE);
1313
1314 if (burn_depth)
1315 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1316 }
1317
1318
1319 /* Bulk encryption of complete blocks in CTR mode. Caller needs to
1320 make sure that CTR is aligned on a 16 byte boundary if AESNI; the
1321 minimum alignment is for an u32. This function is only intended
1322 for the bulk encryption feature of cipher.c. CTR is expected to be
1323 of size BLOCKSIZE. */
1324 static void
1325 _gcry_aes_ctr_enc (void *context, unsigned char *ctr,
1326 void *outbuf_arg, const void *inbuf_arg,
1327 size_t nblocks)
1328 {
1329 RIJNDAEL_context *ctx = context;
1330 unsigned char *outbuf = outbuf_arg;
1331 const unsigned char *inbuf = inbuf_arg;
1332 unsigned int burn_depth = 0;
1333 union { unsigned char x1[16] ATTR_ALIGNED_16; u32 x32[4]; } tmp;
1334 rijndael_cryptfn_t encrypt_fn = ctx->encrypt_fn;
1335
1336 if (ctx->prefetch_enc_fn)
1337 ctx->prefetch_enc_fn();
1338
1339 for ( ;nblocks; nblocks-- )
1340 {
1341 /* Encrypt the counter. */
1342 burn_depth = encrypt_fn (ctx, tmp.x1, ctr);
1343 /* XOR the input with the encrypted counter and store in output. */
1344 cipher_block_xor(outbuf, tmp.x1, inbuf, BLOCKSIZE);
1345 outbuf += BLOCKSIZE;
1346 inbuf += BLOCKSIZE;
1347 /* Increment the counter. */
1348 cipher_block_add(ctr, 1, BLOCKSIZE);
1349 }
1350
1351 wipememory(&tmp, sizeof(tmp));
1352
1353 if (burn_depth)
1354 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1355 }
1356
1357
1358
1359 #if !defined(USE_ARM_ASM) && !defined(USE_AMD64_ASM)
1360 /* Decrypt one block. A and B may be the same. */
1361 static unsigned int
1362 do_decrypt_fn (const RIJNDAEL_context *ctx, unsigned char *b,
1363 const unsigned char *a)
1364 {
1365 #define rk (ctx->keyschdec32)
1366 int rounds = ctx->rounds;
1367 int r;
1368 u32 sa[4];
1369 u32 sb[4];
1370
1371 sb[0] = buf_get_le32(a + 0);
1372 sb[1] = buf_get_le32(a + 4);
1373 sb[2] = buf_get_le32(a + 8);
1374 sb[3] = buf_get_le32(a + 12);
1375
1376 sa[0] = sb[0] ^ rk[rounds][0];
1377 sa[1] = sb[1] ^ rk[rounds][1];
1378 sa[2] = sb[2] ^ rk[rounds][2];
1379 sa[3] = sb[3] ^ rk[rounds][3];
1380
1381 for (r = rounds - 1; r > 1; r--)
1382 {
1383 sb[0] = rol(decT[(byte)(sa[0] >> (0 * 8))], (0 * 8));
1384 sb[1] = rol(decT[(byte)(sa[0] >> (1 * 8))], (1 * 8));
1385 sb[2] = rol(decT[(byte)(sa[0] >> (2 * 8))], (2 * 8));
1386 sb[3] = rol(decT[(byte)(sa[0] >> (3 * 8))], (3 * 8));
1387 sa[0] = rk[r][0] ^ sb[0];
1388
1389 sb[1] ^= rol(decT[(byte)(sa[1] >> (0 * 8))], (0 * 8));
1390 sb[2] ^= rol(decT[(byte)(sa[1] >> (1 * 8))], (1 * 8));
1391 sb[3] ^= rol(decT[(byte)(sa[1] >> (2 * 8))], (2 * 8));
1392 sa[0] ^= rol(decT[(byte)(sa[1] >> (3 * 8))], (3 * 8));
1393 sa[1] = rk[r][1] ^ sb[1];
1394
1395 sb[2] ^= rol(decT[(byte)(sa[2] >> (0 * 8))], (0 * 8));
1396 sb[3] ^= rol(decT[(byte)(sa[2] >> (1 * 8))], (1 * 8));
1397 sa[0] ^= rol(decT[(byte)(sa[2] >> (2 * 8))], (2 * 8));
1398 sa[1] ^= rol(decT[(byte)(sa[2] >> (3 * 8))], (3 * 8));
1399 sa[2] = rk[r][2] ^ sb[2];
1400
1401 sb[3] ^= rol(decT[(byte)(sa[3] >> (0 * 8))], (0 * 8));
1402 sa[0] ^= rol(decT[(byte)(sa[3] >> (1 * 8))], (1 * 8));
1403 sa[1] ^= rol(decT[(byte)(sa[3] >> (2 * 8))], (2 * 8));
1404 sa[2] ^= rol(decT[(byte)(sa[3] >> (3 * 8))], (3 * 8));
1405 sa[3] = rk[r][3] ^ sb[3];
1406
1407 r--;
1408
1409 sb[0] = rol(decT[(byte)(sa[0] >> (0 * 8))], (0 * 8));
1410 sb[1] = rol(decT[(byte)(sa[0] >> (1 * 8))], (1 * 8));
1411 sb[2] = rol(decT[(byte)(sa[0] >> (2 * 8))], (2 * 8));
1412 sb[3] = rol(decT[(byte)(sa[0] >> (3 * 8))], (3 * 8));
1413 sa[0] = rk[r][0] ^ sb[0];
1414
1415 sb[1] ^= rol(decT[(byte)(sa[1] >> (0 * 8))], (0 * 8));
1416 sb[2] ^= rol(decT[(byte)(sa[1] >> (1 * 8))], (1 * 8));
1417 sb[3] ^= rol(decT[(byte)(sa[1] >> (2 * 8))], (2 * 8));
1418 sa[0] ^= rol(decT[(byte)(sa[1] >> (3 * 8))], (3 * 8));
1419 sa[1] = rk[r][1] ^ sb[1];
1420
1421 sb[2] ^= rol(decT[(byte)(sa[2] >> (0 * 8))], (0 * 8));
1422 sb[3] ^= rol(decT[(byte)(sa[2] >> (1 * 8))], (1 * 8));
1423 sa[0] ^= rol(decT[(byte)(sa[2] >> (2 * 8))], (2 * 8));
1424 sa[1] ^= rol(decT[(byte)(sa[2] >> (3 * 8))], (3 * 8));
1425 sa[2] = rk[r][2] ^ sb[2];
1426
1427 sb[3] ^= rol(decT[(byte)(sa[3] >> (0 * 8))], (0 * 8));
1428 sa[0] ^= rol(decT[(byte)(sa[3] >> (1 * 8))], (1 * 8));
1429 sa[1] ^= rol(decT[(byte)(sa[3] >> (2 * 8))], (2 * 8));
1430 sa[2] ^= rol(decT[(byte)(sa[3] >> (3 * 8))], (3 * 8));
1431 sa[3] = rk[r][3] ^ sb[3];
1432 }
1433
1434 sb[0] = rol(decT[(byte)(sa[0] >> (0 * 8))], (0 * 8));
1435 sb[1] = rol(decT[(byte)(sa[0] >> (1 * 8))], (1 * 8));
1436 sb[2] = rol(decT[(byte)(sa[0] >> (2 * 8))], (2 * 8));
1437 sb[3] = rol(decT[(byte)(sa[0] >> (3 * 8))], (3 * 8));
1438 sa[0] = rk[1][0] ^ sb[0];
1439
1440 sb[1] ^= rol(decT[(byte)(sa[1] >> (0 * 8))], (0 * 8));
1441 sb[2] ^= rol(decT[(byte)(sa[1] >> (1 * 8))], (1 * 8));
1442 sb[3] ^= rol(decT[(byte)(sa[1] >> (2 * 8))], (2 * 8));
1443 sa[0] ^= rol(decT[(byte)(sa[1] >> (3 * 8))], (3 * 8));
1444 sa[1] = rk[1][1] ^ sb[1];
1445
1446 sb[2] ^= rol(decT[(byte)(sa[2] >> (0 * 8))], (0 * 8));
1447 sb[3] ^= rol(decT[(byte)(sa[2] >> (1 * 8))], (1 * 8));
1448 sa[0] ^= rol(decT[(byte)(sa[2] >> (2 * 8))], (2 * 8));
1449 sa[1] ^= rol(decT[(byte)(sa[2] >> (3 * 8))], (3 * 8));
1450 sa[2] = rk[1][2] ^ sb[2];
1451
1452 sb[3] ^= rol(decT[(byte)(sa[3] >> (0 * 8))], (0 * 8));
1453 sa[0] ^= rol(decT[(byte)(sa[3] >> (1 * 8))], (1 * 8));
1454 sa[1] ^= rol(decT[(byte)(sa[3] >> (2 * 8))], (2 * 8));
1455 sa[2] ^= rol(decT[(byte)(sa[3] >> (3 * 8))], (3 * 8));
1456 sa[3] = rk[1][3] ^ sb[3];
1457
1458 /* Last round is special. */
1459 sb[0] = (u32)inv_sbox[(byte)(sa[0] >> (0 * 8))] << (0 * 8);
1460 sb[1] = (u32)inv_sbox[(byte)(sa[0] >> (1 * 8))] << (1 * 8);
1461 sb[2] = (u32)inv_sbox[(byte)(sa[0] >> (2 * 8))] << (2 * 8);
1462 sb[3] = (u32)inv_sbox[(byte)(sa[0] >> (3 * 8))] << (3 * 8);
1463 sa[0] = sb[0] ^ rk[0][0];
1464
1465 sb[1] ^= (u32)inv_sbox[(byte)(sa[1] >> (0 * 8))] << (0 * 8);
1466 sb[2] ^= (u32)inv_sbox[(byte)(sa[1] >> (1 * 8))] << (1 * 8);
1467 sb[3] ^= (u32)inv_sbox[(byte)(sa[1] >> (2 * 8))] << (2 * 8);
1468 sa[0] ^= (u32)inv_sbox[(byte)(sa[1] >> (3 * 8))] << (3 * 8);
1469 sa[1] = sb[1] ^ rk[0][1];
1470
1471 sb[2] ^= (u32)inv_sbox[(byte)(sa[2] >> (0 * 8))] << (0 * 8);
1472 sb[3] ^= (u32)inv_sbox[(byte)(sa[2] >> (1 * 8))] << (1 * 8);
1473 sa[0] ^= (u32)inv_sbox[(byte)(sa[2] >> (2 * 8))] << (2 * 8);
1474 sa[1] ^= (u32)inv_sbox[(byte)(sa[2] >> (3 * 8))] << (3 * 8);
1475 sa[2] = sb[2] ^ rk[0][2];
1476
1477 sb[3] ^= (u32)inv_sbox[(byte)(sa[3] >> (0 * 8))] << (0 * 8);
1478 sa[0] ^= (u32)inv_sbox[(byte)(sa[3] >> (1 * 8))] << (1 * 8);
1479 sa[1] ^= (u32)inv_sbox[(byte)(sa[3] >> (2 * 8))] << (2 * 8);
1480 sa[2] ^= (u32)inv_sbox[(byte)(sa[3] >> (3 * 8))] << (3 * 8);
1481 sa[3] = sb[3] ^ rk[0][3];
1482
1483 buf_put_le32(b + 0, sa[0]);
1484 buf_put_le32(b + 4, sa[1]);
1485 buf_put_le32(b + 8, sa[2]);
1486 buf_put_le32(b + 12, sa[3]);
1487 #undef rk
1488
1489 return (56+2*sizeof(int));
1490 }
1491 #endif /*!USE_ARM_ASM && !USE_AMD64_ASM*/
1492
1493
1494 /* Decrypt one block. AX and BX may be the same. */
1495 static unsigned int
1496 do_decrypt (const RIJNDAEL_context *ctx, unsigned char *bx,
1497 const unsigned char *ax)
1498 {
1499 #ifdef USE_AMD64_ASM
1500 return _gcry_aes_amd64_decrypt_block(ctx->keyschdec, bx, ax, ctx->rounds,
1501 dec_tables.T);
1502 #elif defined(USE_ARM_ASM)
1503 return _gcry_aes_arm_decrypt_block(ctx->keyschdec, bx, ax, ctx->rounds,
1504 dec_tables.T);
1505 #else
1506 return do_decrypt_fn (ctx, bx, ax);
1507 #endif /*!USE_ARM_ASM && !USE_AMD64_ASM*/
1508 }
1509
1510
1511 static inline void
1512 check_decryption_preparation (RIJNDAEL_context *ctx)
1513 {
1514 if ( !ctx->decryption_prepared )
1515 {
1516 ctx->prepare_decryption ( ctx );
1517 ctx->decryption_prepared = 1;
1518 }
1519 }
1520
1521
1522 static unsigned int
1523 rijndael_decrypt (void *context, byte *b, const byte *a)
1524 {
1525 RIJNDAEL_context *ctx = context;
1526
1527 check_decryption_preparation (ctx);
1528
1529 if (ctx->prefetch_dec_fn)
1530 ctx->prefetch_dec_fn();
1531
1532 return ctx->decrypt_fn (ctx, b, a);
1533 }
1534
1535
1536 /* Bulk decryption of complete blocks in CFB mode. Caller needs to
1537 make sure that IV is aligned on an unsigned long boundary. This
1538 function is only intended for the bulk encryption feature of
1539 cipher.c. */
1540 static void
1541 _gcry_aes_cfb_dec (void *context, unsigned char *iv,
1542 void *outbuf_arg, const void *inbuf_arg,
1543 size_t nblocks)
1544 {
1545 RIJNDAEL_context *ctx = context;
1546 unsigned char *outbuf = outbuf_arg;
1547 const unsigned char *inbuf = inbuf_arg;
1548 unsigned int burn_depth = 0;
1549 rijndael_cryptfn_t encrypt_fn = ctx->encrypt_fn;
1550
1551 if (ctx->prefetch_enc_fn)
1552 ctx->prefetch_enc_fn();
1553
1554 for ( ;nblocks; nblocks-- )
1555 {
1556 burn_depth = encrypt_fn (ctx, iv, iv);
1557 cipher_block_xor_n_copy(outbuf, iv, inbuf, BLOCKSIZE);
1558 outbuf += BLOCKSIZE;
1559 inbuf += BLOCKSIZE;
1560 }
1561
1562 if (burn_depth)
1563 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1564 }
1565
1566
1567 /* Bulk decryption of complete blocks in CBC mode. Caller needs to
1568 make sure that IV is aligned on an unsigned long boundary. This
1569 function is only intended for the bulk encryption feature of
1570 cipher.c. */
1571 static void
1572 _gcry_aes_cbc_dec (void *context, unsigned char *iv,
1573 void *outbuf_arg, const void *inbuf_arg,
1574 size_t nblocks)
1575 {
1576 RIJNDAEL_context *ctx = context;
1577 unsigned char *outbuf = outbuf_arg;
1578 const unsigned char *inbuf = inbuf_arg;
1579 unsigned int burn_depth = 0;
1580 unsigned char savebuf[BLOCKSIZE] ATTR_ALIGNED_16;
1581 rijndael_cryptfn_t decrypt_fn = ctx->decrypt_fn;
1582
1583 check_decryption_preparation (ctx);
1584
1585 if (ctx->prefetch_dec_fn)
1586 ctx->prefetch_dec_fn();
1587
1588 for ( ;nblocks; nblocks-- )
1589 {
1590 /* INBUF is needed later and it may be identical to OUTBUF, so store
1591 the intermediate result to SAVEBUF. */
1592
1593 burn_depth = decrypt_fn (ctx, savebuf, inbuf);
1594
1595 cipher_block_xor_n_copy_2(outbuf, savebuf, iv, inbuf, BLOCKSIZE);
1596 inbuf += BLOCKSIZE;
1597 outbuf += BLOCKSIZE;
1598 }
1599
1600 wipememory(savebuf, sizeof(savebuf));
1601
1602 if (burn_depth)
1603 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1604 }
1605
1606
1607
1608 /* Bulk encryption/decryption of complete blocks in OCB mode. */
1609 static size_t
1610 _gcry_aes_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
1611 const void *inbuf_arg, size_t nblocks, int encrypt)
1612 {
1613 RIJNDAEL_context *ctx = (void *)&c->context.c;
1614 unsigned char *outbuf = outbuf_arg;
1615 const unsigned char *inbuf = inbuf_arg;
1616 unsigned int burn_depth = 0;
1617
1618 if (encrypt)
1619 {
1620 union { unsigned char x1[16] ATTR_ALIGNED_16; u32 x32[4]; } l_tmp;
1621 rijndael_cryptfn_t encrypt_fn = ctx->encrypt_fn;
1622
1623 if (ctx->prefetch_enc_fn)
1624 ctx->prefetch_enc_fn();
1625
1626 for ( ;nblocks; nblocks-- )
1627 {
1628 u64 i = ++c->u_mode.ocb.data_nblocks;
1629 const unsigned char *l = ocb_get_l(c, i);
1630
1631 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1632 cipher_block_xor_1 (c->u_iv.iv, l, BLOCKSIZE);
1633 cipher_block_cpy (l_tmp.x1, inbuf, BLOCKSIZE);
1634 /* Checksum_i = Checksum_{i-1} xor P_i */
1635 cipher_block_xor_1 (c->u_ctr.ctr, l_tmp.x1, BLOCKSIZE);
1636 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
1637 cipher_block_xor_1 (l_tmp.x1, c->u_iv.iv, BLOCKSIZE);
1638 burn_depth = encrypt_fn (ctx, l_tmp.x1, l_tmp.x1);
1639 cipher_block_xor_1 (l_tmp.x1, c->u_iv.iv, BLOCKSIZE);
1640 cipher_block_cpy (outbuf, l_tmp.x1, BLOCKSIZE);
1641
1642 inbuf += BLOCKSIZE;
1643 outbuf += BLOCKSIZE;
1644 }
1645 }
1646 else
1647 {
1648 union { unsigned char x1[16] ATTR_ALIGNED_16; u32 x32[4]; } l_tmp;
1649 rijndael_cryptfn_t decrypt_fn = ctx->decrypt_fn;
1650
1651 check_decryption_preparation (ctx);
1652
1653 if (ctx->prefetch_dec_fn)
1654 ctx->prefetch_dec_fn();
1655
1656 for ( ;nblocks; nblocks-- )
1657 {
1658 u64 i = ++c->u_mode.ocb.data_nblocks;
1659 const unsigned char *l = ocb_get_l(c, i);
1660
1661 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1662 cipher_block_xor_1 (c->u_iv.iv, l, BLOCKSIZE);
1663 cipher_block_cpy (l_tmp.x1, inbuf, BLOCKSIZE);
1664 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
1665 cipher_block_xor_1 (l_tmp.x1, c->u_iv.iv, BLOCKSIZE);
1666 burn_depth = decrypt_fn (ctx, l_tmp.x1, l_tmp.x1);
1667 cipher_block_xor_1 (l_tmp.x1, c->u_iv.iv, BLOCKSIZE);
1668 /* Checksum_i = Checksum_{i-1} xor P_i */
1669 cipher_block_xor_1 (c->u_ctr.ctr, l_tmp.x1, BLOCKSIZE);
1670 cipher_block_cpy (outbuf, l_tmp.x1, BLOCKSIZE);
1671
1672 inbuf += BLOCKSIZE;
1673 outbuf += BLOCKSIZE;
1674 }
1675 }
1676
1677 if (burn_depth)
1678 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1679
1680 return 0;
1681 }
1682
1683
1684 /* Bulk authentication of complete blocks in OCB mode. */
1685 static size_t
1686 _gcry_aes_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg, size_t nblocks)
1687 {
1688 RIJNDAEL_context *ctx = (void *)&c->context.c;
1689 const unsigned char *abuf = abuf_arg;
1690 unsigned int burn_depth = 0;
1691 union { unsigned char x1[16] ATTR_ALIGNED_16; u32 x32[4]; } l_tmp;
1692 rijndael_cryptfn_t encrypt_fn = ctx->encrypt_fn;
1693
1694 if (ctx->prefetch_enc_fn)
1695 ctx->prefetch_enc_fn();
1696
1697 for ( ;nblocks; nblocks-- )
1698 {
1699 u64 i = ++c->u_mode.ocb.aad_nblocks;
1700 const unsigned char *l = ocb_get_l(c, i);
1701
1702 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1703 cipher_block_xor_1 (c->u_mode.ocb.aad_offset, l, BLOCKSIZE);
1704 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
1705 cipher_block_xor (l_tmp.x1, c->u_mode.ocb.aad_offset, abuf,
1706 BLOCKSIZE);
1707 burn_depth = encrypt_fn (ctx, l_tmp.x1, l_tmp.x1);
1708 cipher_block_xor_1 (c->u_mode.ocb.aad_sum, l_tmp.x1, BLOCKSIZE);
1709
1710 abuf += BLOCKSIZE;
1711 }
1712
1713 wipememory(&l_tmp, sizeof(l_tmp));
1714
1715 if (burn_depth)
1716 _gcry_burn_stack (burn_depth + 4 * sizeof(void *));
1717
1718 return 0;
1719 }
1720
1721
1722 /* Bulk encryption/decryption of complete blocks in XTS mode. */
1723 static void
1724 _gcry_aes_xts_crypt (void *context, unsigned char *tweak,
1725 void *outbuf_arg, const void *inbuf_arg,
1726 size_t nblocks, int encrypt)
1727 {
1728 RIJNDAEL_context *ctx = context;
1729 unsigned char *outbuf = outbuf_arg;
1730 const unsigned char *inbuf = inbuf_arg;
1731 unsigned int burn_depth = 0;
1732 rijndael_cryptfn_t crypt_fn;
1733 u64 tweak_lo, tweak_hi, tweak_next_lo, tweak_next_hi, tmp_lo, tmp_hi, carry;
1734
1735 if (encrypt)
1736 {
1737 if (ctx->prefetch_enc_fn)
1738 ctx->prefetch_enc_fn();
1739
1740 crypt_fn = ctx->encrypt_fn;
1741 }
1742 else
1743 {
1744 check_decryption_preparation (ctx);
1745
1746 if (ctx->prefetch_dec_fn)
1747 ctx->prefetch_dec_fn();
1748
1749 crypt_fn = ctx->decrypt_fn;
1750 }
1751
1752 tweak_next_lo = buf_get_le64 (tweak + 0);
1753 tweak_next_hi = buf_get_le64 (tweak + 8);
1754
1755 while (nblocks)
1756 {
1757 tweak_lo = tweak_next_lo;
1758 tweak_hi = tweak_next_hi;
1759
1760 /* Xor-Encrypt/Decrypt-Xor block. */
1761 tmp_lo = buf_get_le64 (inbuf + 0) ^ tweak_lo;
1762 tmp_hi = buf_get_le64 (inbuf + 8) ^ tweak_hi;
1763
1764 buf_put_le64 (outbuf + 0, tmp_lo);
1765 buf_put_le64 (outbuf + 8, tmp_hi);
1766
1767 /* Generate next tweak. */
1768 carry = ct_ulong_gen_mask(tweak_next_hi >> 63) & 0x87;
1769 tweak_next_hi = (tweak_next_hi << 1) + (tweak_next_lo >> 63);
1770 tweak_next_lo = (tweak_next_lo << 1) ^ carry;
1771
1772 burn_depth = crypt_fn (ctx, outbuf, outbuf);
1773
1774 buf_put_le64 (outbuf + 0, buf_get_le64 (outbuf + 0) ^ tweak_lo);
1775 buf_put_le64 (outbuf + 8, buf_get_le64 (outbuf + 8) ^ tweak_hi);
1776
1777 outbuf += GCRY_XTS_BLOCK_LEN;
1778 inbuf += GCRY_XTS_BLOCK_LEN;
1779 nblocks--;
1780 }
1781
1782 buf_put_le64 (tweak + 0, tweak_next_lo);
1783 buf_put_le64 (tweak + 8, tweak_next_hi);
1784
1785 if (burn_depth)
1786 _gcry_burn_stack (burn_depth + 5 * sizeof(void *));
1787 }
1788
1789
1790 /* Run the self-tests for AES 128. Returns NULL on success. */
1791 static const char*
1792 selftest_basic_128 (void)
1793 {
1794 RIJNDAEL_context *ctx;
1795 unsigned char ctxmem[sizeof(*ctx) + 16];
1796 unsigned char scratch[16];
1797 cipher_bulk_ops_t bulk_ops;
1798
1799 /* The test vectors are from the AES supplied ones; more or less
1800 randomly taken from ecb_tbl.txt (I=42,81,14) */
1801 #if 1
1802 static const unsigned char plaintext_128[16] =
1803 {
1804 0x01,0x4B,0xAF,0x22,0x78,0xA6,0x9D,0x33,
1805 0x1D,0x51,0x80,0x10,0x36,0x43,0xE9,0x9A
1806 };
1807 static const unsigned char key_128[16] =
1808 {
1809 0xE8,0xE9,0xEA,0xEB,0xED,0xEE,0xEF,0xF0,
1810 0xF2,0xF3,0xF4,0xF5,0xF7,0xF8,0xF9,0xFA
1811 };
1812 static const unsigned char ciphertext_128[16] =
1813 {
1814 0x67,0x43,0xC3,0xD1,0x51,0x9A,0xB4,0xF2,
1815 0xCD,0x9A,0x78,0xAB,0x09,0xA5,0x11,0xBD
1816 };
1817 #else
1818 /* Test vectors from fips-197, appendix C. */
1819 # warning debug test vectors in use
1820 static const unsigned char plaintext_128[16] =
1821 {
1822 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
1823 0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff
1824 };
1825 static const unsigned char key_128[16] =
1826 {
1827 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
1828 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
1829 /* 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, */
1830 /* 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c */
1831 };
1832 static const unsigned char ciphertext_128[16] =
1833 {
1834 0x69,0xc4,0xe0,0xd8,0x6a,0x7b,0x04,0x30,
1835 0xd8,0xcd,0xb7,0x80,0x70,0xb4,0xc5,0x5a
1836 };
1837 #endif
1838
1839 ctx = (void *)(ctxmem + ((16 - ((uintptr_t)ctxmem & 15)) & 15));
1840
1841 rijndael_setkey (ctx, key_128, sizeof (key_128), &bulk_ops);
1842 rijndael_encrypt (ctx, scratch, plaintext_128);
1843 if (memcmp (scratch, ciphertext_128, sizeof (ciphertext_128)))
1844 {
1845 return "AES-128 test encryption failed.";
1846 }
1847 rijndael_decrypt (ctx, scratch, scratch);
1848 if (memcmp (scratch, plaintext_128, sizeof (plaintext_128)))
1849 return "AES-128 test decryption failed.";
1850
1851 return NULL;
1852 }
1853
1854 /* Run the self-tests for AES 192. Returns NULL on success. */
1855 static const char*
1856 selftest_basic_192 (void)
1857 {
1858 RIJNDAEL_context *ctx;
1859 unsigned char ctxmem[sizeof(*ctx) + 16];
1860 unsigned char scratch[16];
1861 cipher_bulk_ops_t bulk_ops;
1862
1863 static unsigned char plaintext_192[16] =
1864 {
1865 0x76,0x77,0x74,0x75,0xF1,0xF2,0xF3,0xF4,
1866 0xF8,0xF9,0xE6,0xE7,0x77,0x70,0x71,0x72
1867 };
1868 static unsigned char key_192[24] =
1869 {
1870 0x04,0x05,0x06,0x07,0x09,0x0A,0x0B,0x0C,
1871 0x0E,0x0F,0x10,0x11,0x13,0x14,0x15,0x16,
1872 0x18,0x19,0x1A,0x1B,0x1D,0x1E,0x1F,0x20
1873 };
1874 static const unsigned char ciphertext_192[16] =
1875 {
1876 0x5D,0x1E,0xF2,0x0D,0xCE,0xD6,0xBC,0xBC,
1877 0x12,0x13,0x1A,0xC7,0xC5,0x47,0x88,0xAA
1878 };
1879
1880 ctx = (void *)(ctxmem + ((16 - ((uintptr_t)ctxmem & 15)) & 15));
1881
1882 rijndael_setkey (ctx, key_192, sizeof(key_192), &bulk_ops);
1883 rijndael_encrypt (ctx, scratch, plaintext_192);
1884 if (memcmp (scratch, ciphertext_192, sizeof (ciphertext_192)))
1885 {
1886 return "AES-192 test encryption failed.";
1887 }
1888 rijndael_decrypt (ctx, scratch, scratch);
1889 if (memcmp (scratch, plaintext_192, sizeof (plaintext_192)))
1890 return "AES-192 test decryption failed.";
1891
1892 return NULL;
1893 }
1894
1895
1896 /* Run the self-tests for AES 256. Returns NULL on success. */
1897 static const char*
1898 selftest_basic_256 (void)
1899 {
1900 RIJNDAEL_context *ctx;
1901 unsigned char ctxmem[sizeof(*ctx) + 16];
1902 unsigned char scratch[16];
1903 cipher_bulk_ops_t bulk_ops;
1904
1905 static unsigned char plaintext_256[16] =
1906 {
1907 0x06,0x9A,0x00,0x7F,0xC7,0x6A,0x45,0x9F,
1908 0x98,0xBA,0xF9,0x17,0xFE,0xDF,0x95,0x21
1909 };
1910 static unsigned char key_256[32] =
1911 {
1912 0x08,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x10,
1913 0x12,0x13,0x14,0x15,0x17,0x18,0x19,0x1A,
1914 0x1C,0x1D,0x1E,0x1F,0x21,0x22,0x23,0x24,
1915 0x26,0x27,0x28,0x29,0x2B,0x2C,0x2D,0x2E
1916 };
1917 static const unsigned char ciphertext_256[16] =
1918 {
1919 0x08,0x0E,0x95,0x17,0xEB,0x16,0x77,0x71,
1920 0x9A,0xCF,0x72,0x80,0x86,0x04,0x0A,0xE3
1921 };
1922
1923 ctx = (void *)(ctxmem + ((16 - ((uintptr_t)ctxmem & 15)) & 15));
1924
1925 rijndael_setkey (ctx, key_256, sizeof(key_256), &bulk_ops);
1926 rijndael_encrypt (ctx, scratch, plaintext_256);
1927 if (memcmp (scratch, ciphertext_256, sizeof (ciphertext_256)))
1928 {
1929 return "AES-256 test encryption failed.";
1930 }
1931 rijndael_decrypt (ctx, scratch, scratch);
1932 if (memcmp (scratch, plaintext_256, sizeof (plaintext_256)))
1933 return "AES-256 test decryption failed.";
1934
1935 return NULL;
1936 }
1937
1938
1939 /* Run all the self-tests and return NULL on success. This function
1940 is used for the on-the-fly self-tests. */
1941 static const char *
1942 selftest (void)
1943 {
1944 const char *r;
1945
1946 if ( (r = selftest_basic_128 ())
1947 || (r = selftest_basic_192 ())
1948 || (r = selftest_basic_256 ()) )
1949 return r;
1950
1951 return r;
1952 }
1953
1954
1955 /* SP800-38a.pdf for AES-128. */
1956 static const char *
1957 selftest_fips_128_38a (int requested_mode)
1958 {
1959 static const struct tv
1960 {
1961 int mode;
1962 const unsigned char key[16];
1963 const unsigned char iv[16];
1964 struct
1965 {
1966 const unsigned char input[16];
1967 const unsigned char output[16];
1968 } data[4];
1969 } tv[2] =
1970 {
1971 {
1972 GCRY_CIPHER_MODE_CFB, /* F.3.13, CFB128-AES128 */
1973 { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
1974 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
1975 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1976 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
1977 {
1978 { { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
1979 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a },
1980 { 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20,
1981 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a } },
1982
1983 { { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
1984 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
1985 { 0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f,
1986 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b } },
1987
1988 { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
1989 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
1990 { 0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40,
1991 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf } },
1992
1993 { { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
1994 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
1995 { 0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e,
1996 0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6 } }
1997 }
1998 },
1999 {
2000 GCRY_CIPHER_MODE_OFB,
2001 { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
2002 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
2003 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2004 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
2005 {
2006 { { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
2007 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a },
2008 { 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20,
2009 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a } },
2010
2011 { { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
2012 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
2013 { 0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03,
2014 0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25 } },
2015
2016 { { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
2017 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
2018 { 0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6,
2019 0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc } },
2020
2021 { { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
2022 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
2023 { 0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78,
2024 0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e } },
2025 }
2026 }
2027 };
2028 unsigned char scratch[16];
2029 gpg_error_t err;
2030 int tvi, idx;
2031 gcry_cipher_hd_t hdenc = NULL;
2032 gcry_cipher_hd_t hddec = NULL;
2033
2034 #define Fail(a) do { \
2035 _gcry_cipher_close (hdenc); \
2036 _gcry_cipher_close (hddec); \
2037 return a; \
2038 } while (0)
2039
2040 gcry_assert (sizeof tv[0].data[0].input == sizeof scratch);
2041 gcry_assert (sizeof tv[0].data[0].output == sizeof scratch);
2042
2043 for (tvi=0; tvi < DIM (tv); tvi++)
2044 if (tv[tvi].mode == requested_mode)
2045 break;
2046 if (tvi == DIM (tv))
2047 Fail ("no test data for this mode");
2048
2049 err = _gcry_cipher_open (&hdenc, GCRY_CIPHER_AES, tv[tvi].mode, 0);
2050 if (err)
2051 Fail ("open");
2052 err = _gcry_cipher_open (&hddec, GCRY_CIPHER_AES, tv[tvi].mode, 0);
2053 if (err)
2054 Fail ("open");
2055 err = _gcry_cipher_setkey (hdenc, tv[tvi].key, sizeof tv[tvi].key);
2056 if (!err)
2057 err = _gcry_cipher_setkey (hddec, tv[tvi].key, sizeof tv[tvi].key);
2058 if (err)
2059 Fail ("set key");
2060 err = _gcry_cipher_setiv (hdenc, tv[tvi].iv, sizeof tv[tvi].iv);
2061 if (!err)
2062 err = _gcry_cipher_setiv (hddec, tv[tvi].iv, sizeof tv[tvi].iv);
2063 if (err)
2064 Fail ("set IV");
2065 for (idx=0; idx < DIM (tv[tvi].data); idx++)
2066 {
2067 err = _gcry_cipher_encrypt (hdenc, scratch, sizeof scratch,
2068 tv[tvi].data[idx].input,
2069 sizeof tv[tvi].data[idx].input);
2070 if (err)
2071 Fail ("encrypt command");
2072 if (memcmp (scratch, tv[tvi].data[idx].output, sizeof scratch))
2073 Fail ("encrypt mismatch");
2074 err = _gcry_cipher_decrypt (hddec, scratch, sizeof scratch,
2075 tv[tvi].data[idx].output,
2076 sizeof tv[tvi].data[idx].output);
2077 if (err)
2078 Fail ("decrypt command");
2079 if (memcmp (scratch, tv[tvi].data[idx].input, sizeof scratch))
2080 Fail ("decrypt mismatch");
2081 }
2082
2083 #undef Fail
2084 _gcry_cipher_close (hdenc);
2085 _gcry_cipher_close (hddec);
2086 return NULL;
2087 }
2088
2089
2090 /* Complete selftest for AES-128 with all modes and driver code. */
2091 static gpg_err_code_t
2092 selftest_fips_128 (int extended, selftest_report_func_t report)
2093 {
2094 const char *what;
2095 const char *errtxt;
2096
2097 what = "low-level";
2098 errtxt = selftest_basic_128 ();
2099 if (errtxt)
2100 goto failed;
2101
2102 if (extended)
2103 {
2104 what = "cfb";
2105 errtxt = selftest_fips_128_38a (GCRY_CIPHER_MODE_CFB);
2106 if (errtxt)
2107 goto failed;
2108
2109 what = "ofb";
2110 errtxt = selftest_fips_128_38a (GCRY_CIPHER_MODE_OFB);
2111 if (errtxt)
2112 goto failed;
2113 }
2114
2115 return 0; /* Succeeded. */
2116
2117 failed:
2118 if (report)
2119 report ("cipher", GCRY_CIPHER_AES128, what, errtxt);
2120 return GPG_ERR_SELFTEST_FAILED;
2121 }
2122
2123 /* Complete selftest for AES-192. */
2124 static gpg_err_code_t
2125 selftest_fips_192 (int extended, selftest_report_func_t report)
2126 {
2127 const char *what;
2128 const char *errtxt;
2129
2130 (void)extended; /* No extended tests available. */
2131
2132 what = "low-level";
2133 errtxt = selftest_basic_192 ();
2134 if (errtxt)
2135 goto failed;
2136
2137
2138 return 0; /* Succeeded. */
2139
2140 failed:
2141 if (report)
2142 report ("cipher", GCRY_CIPHER_AES192, what, errtxt);
2143 return GPG_ERR_SELFTEST_FAILED;
2144 }
2145
2146
2147 /* Complete selftest for AES-256. */
2148 static gpg_err_code_t
2149 selftest_fips_256 (int extended, selftest_report_func_t report)
2150 {
2151 const char *what;
2152 const char *errtxt;
2153
2154 (void)extended; /* No extended tests available. */
2155
2156 what = "low-level";
2157 errtxt = selftest_basic_256 ();
2158 if (errtxt)
2159 goto failed;
2160
2161 return 0; /* Succeeded. */
2162
2163 failed:
2164 if (report)
2165 report ("cipher", GCRY_CIPHER_AES256, what, errtxt);
2166 return GPG_ERR_SELFTEST_FAILED;
2167 }
2168
2169
2170
2171 /* Run a full self-test for ALGO and return 0 on success. */
2172 static gpg_err_code_t
2173 run_selftests (int algo, int extended, selftest_report_func_t report)
2174 {
2175 gpg_err_code_t ec;
2176
2177 switch (algo)
2178 {
2179 case GCRY_CIPHER_AES128:
2180 ec = selftest_fips_128 (extended, report);
2181 break;
2182 case GCRY_CIPHER_AES192:
2183 ec = selftest_fips_192 (extended, report);
2184 break;
2185 case GCRY_CIPHER_AES256:
2186 ec = selftest_fips_256 (extended, report);
2187 break;
2188 default:
2189 ec = GPG_ERR_CIPHER_ALGO;
2190 break;
2191
2192 }
2193 return ec;
2194 }
2195
2196
2197
2198
2199 static const char *rijndael_names[] =
2200 {
2201 "RIJNDAEL",
2202 "AES128",
2203 "AES-128",
2204 NULL
2205 };
2206
2207 static const gcry_cipher_oid_spec_t rijndael_oids[] =
2208 {
2209 { "2.16.840.1.101.3.4.1.1", GCRY_CIPHER_MODE_ECB },
2210 { "2.16.840.1.101.3.4.1.2", GCRY_CIPHER_MODE_CBC },
2211 { "2.16.840.1.101.3.4.1.3", GCRY_CIPHER_MODE_OFB },
2212 { "2.16.840.1.101.3.4.1.4", GCRY_CIPHER_MODE_CFB },
2213 { "2.16.840.1.101.3.4.1.6", GCRY_CIPHER_MODE_GCM },
2214 { "2.16.840.1.101.3.4.1.7", GCRY_CIPHER_MODE_CCM },
2215 { NULL }
2216 };
2217
2218 gcry_cipher_spec_t _gcry_cipher_spec_aes =
2219 {
2220 GCRY_CIPHER_AES, {0, 1},
2221 "AES", rijndael_names, rijndael_oids, 16, 128,
2222 sizeof (RIJNDAEL_context),
2223 rijndael_setkey, rijndael_encrypt, rijndael_decrypt,
2224 NULL, NULL,
2225 run_selftests
2226 };
2227
2228
2229 static const char *rijndael192_names[] =
2230 {
2231 "RIJNDAEL192",
2232 "AES-192",
2233 NULL
2234 };
2235
2236 static const gcry_cipher_oid_spec_t rijndael192_oids[] =
2237 {
2238 { "2.16.840.1.101.3.4.1.21", GCRY_CIPHER_MODE_ECB },
2239 { "2.16.840.1.101.3.4.1.22", GCRY_CIPHER_MODE_CBC },
2240 { "2.16.840.1.101.3.4.1.23", GCRY_CIPHER_MODE_OFB },
2241 { "2.16.840.1.101.3.4.1.24", GCRY_CIPHER_MODE_CFB },
2242 { "2.16.840.1.101.3.4.1.26", GCRY_CIPHER_MODE_GCM },
2243 { "2.16.840.1.101.3.4.1.27", GCRY_CIPHER_MODE_CCM },
2244 { NULL }
2245 };
2246
2247 gcry_cipher_spec_t _gcry_cipher_spec_aes192 =
2248 {
2249 GCRY_CIPHER_AES192, {0, 1},
2250 "AES192", rijndael192_names, rijndael192_oids, 16, 192,
2251 sizeof (RIJNDAEL_context),
2252 rijndael_setkey, rijndael_encrypt, rijndael_decrypt,
2253 NULL, NULL,
2254 run_selftests
2255 };
2256
2257
2258 static const char *rijndael256_names[] =
2259 {
2260 "RIJNDAEL256",
2261 "AES-256",
2262 NULL
2263 };
2264
2265 static const gcry_cipher_oid_spec_t rijndael256_oids[] =
2266 {
2267 { "2.16.840.1.101.3.4.1.41", GCRY_CIPHER_MODE_ECB },
2268 { "2.16.840.1.101.3.4.1.42", GCRY_CIPHER_MODE_CBC },
2269 { "2.16.840.1.101.3.4.1.43", GCRY_CIPHER_MODE_OFB },
2270 { "2.16.840.1.101.3.4.1.44", GCRY_CIPHER_MODE_CFB },
2271 { "2.16.840.1.101.3.4.1.46", GCRY_CIPHER_MODE_GCM },
2272 { "2.16.840.1.101.3.4.1.47", GCRY_CIPHER_MODE_CCM },
2273 { NULL }
2274 };
2275
2276 gcry_cipher_spec_t _gcry_cipher_spec_aes256 =
2277 {
2278 GCRY_CIPHER_AES256, {0, 1},
2279 "AES256", rijndael256_names, rijndael256_oids, 16, 256,
2280 sizeof (RIJNDAEL_context),
2281 rijndael_setkey, rijndael_encrypt, rijndael_decrypt,
2282 NULL, NULL,
2283 run_selftests
2284 };