Debian Codegraph @

cipher/serpent.c

raw
1 /* serpent.c - Implementation of the Serpent encryption algorithm.
2 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
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 <https://www.gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdio.h>
25
26 #include "types.h"
27 #include "g10lib.h"
28 #include "cipher.h"
29 #include "bithelp.h"
30 #include "bufhelp.h"
31 #include "cipher-internal.h"
32 #include "bulkhelp.h"
33
34
35 /* USE_SSE2 indicates whether to compile with x86-64 SSE2 code. */
36 #undef USE_SSE2
37 #if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
38 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
39 # define USE_SSE2 1
40 #endif
41
42 /* USE_AVX2 indicates whether to compile with x86-64 AVX2 code. */
43 #undef USE_AVX2
44 #if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
45 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
46 # if defined(ENABLE_AVX2_SUPPORT)
47 # define USE_AVX2 1
48 # endif
49 #endif
50
51 /* USE_AVX512 indicates whether to compile with x86 AVX512 code. */
52 #undef USE_AVX512
53 #if (defined(__x86_64) || defined(__i386)) && \
54 defined(HAVE_COMPATIBLE_CC_X86_AVX512_INTRINSICS)
55 # if defined(ENABLE_AVX512_SUPPORT)
56 # define USE_AVX512 1
57 # endif
58 #endif
59
60 /* USE_NEON indicates whether to enable ARM NEON assembly code. */
61 #undef USE_NEON
62 #ifdef ENABLE_NEON_SUPPORT
63 # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
64 && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
65 && defined(HAVE_GCC_INLINE_ASM_NEON)
66 # define USE_NEON 1
67 # endif
68 #endif /*ENABLE_NEON_SUPPORT*/
69
70 /* Number of rounds per Serpent encrypt/decrypt operation. */
71 #define ROUNDS 32
72
73 /* Magic number, used during generating of the subkeys. */
74 #define PHI 0x9E3779B9
75
76 /* Serpent works on 128 bit blocks. */
77 typedef u32 serpent_block_t[4];
78
79 /* Serpent key, provided by the user. If the original key is shorter
80 than 256 bits, it is padded. */
81 typedef u32 serpent_key_t[8];
82
83 /* The key schedule consists of 33 128 bit subkeys. */
84 typedef u32 serpent_subkeys_t[ROUNDS + 1][4];
85
86 /* A Serpent context. */
87 typedef struct serpent_context
88 {
89 serpent_subkeys_t keys; /* Generated subkeys. */
90
91 #ifdef USE_AVX2
92 int use_avx2;
93 #endif
94 #ifdef USE_AVX512
95 int use_avx512;
96 #endif
97 #ifdef USE_NEON
98 int use_neon;
99 #endif
100 } serpent_context_t;
101
102
103 /* Assembly implementations use SystemV ABI, ABI conversion and additional
104 * stack to store XMM6-XMM15 needed on Win64. */
105 #undef ASM_FUNC_ABI
106 #if defined(USE_SSE2) || defined(USE_AVX2)
107 # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
108 # define ASM_FUNC_ABI __attribute__((sysv_abi))
109 # else
110 # define ASM_FUNC_ABI
111 # endif
112 #endif
113
114
115 #ifdef USE_SSE2
116 /* Assembler implementations of Serpent using SSE2. Process 8 block in
117 parallel.
118 */
119 extern void _gcry_serpent_sse2_ctr_enc(serpent_context_t *ctx,
120 unsigned char *out,
121 const unsigned char *in,
122 unsigned char *ctr) ASM_FUNC_ABI;
123
124 extern void _gcry_serpent_sse2_cbc_dec(serpent_context_t *ctx,
125 unsigned char *out,
126 const unsigned char *in,
127 unsigned char *iv) ASM_FUNC_ABI;
128
129 extern void _gcry_serpent_sse2_cfb_dec(serpent_context_t *ctx,
130 unsigned char *out,
131 const unsigned char *in,
132 unsigned char *iv) ASM_FUNC_ABI;
133
134 extern void _gcry_serpent_sse2_ocb_enc(serpent_context_t *ctx,
135 unsigned char *out,
136 const unsigned char *in,
137 unsigned char *offset,
138 unsigned char *checksum,
139 const u64 Ls[8]) ASM_FUNC_ABI;
140
141 extern void _gcry_serpent_sse2_ocb_dec(serpent_context_t *ctx,
142 unsigned char *out,
143 const unsigned char *in,
144 unsigned char *offset,
145 unsigned char *checksum,
146 const u64 Ls[8]) ASM_FUNC_ABI;
147
148 extern void _gcry_serpent_sse2_ocb_auth(serpent_context_t *ctx,
149 const unsigned char *abuf,
150 unsigned char *offset,
151 unsigned char *checksum,
152 const u64 Ls[8]) ASM_FUNC_ABI;
153
154 extern void _gcry_serpent_sse2_blk8(const serpent_context_t *c, byte *out,
155 const byte *in, int encrypt) ASM_FUNC_ABI;
156 #endif
157
158 #ifdef USE_AVX2
159 /* Assembler implementations of Serpent using AVX2. Process 16 block in
160 parallel.
161 */
162 extern void _gcry_serpent_avx2_ctr_enc(serpent_context_t *ctx,
163 unsigned char *out,
164 const unsigned char *in,
165 unsigned char *ctr) ASM_FUNC_ABI;
166
167 extern void _gcry_serpent_avx2_cbc_dec(serpent_context_t *ctx,
168 unsigned char *out,
169 const unsigned char *in,
170 unsigned char *iv) ASM_FUNC_ABI;
171
172 extern void _gcry_serpent_avx2_cfb_dec(serpent_context_t *ctx,
173 unsigned char *out,
174 const unsigned char *in,
175 unsigned char *iv) ASM_FUNC_ABI;
176
177 extern void _gcry_serpent_avx2_ocb_enc(serpent_context_t *ctx,
178 unsigned char *out,
179 const unsigned char *in,
180 unsigned char *offset,
181 unsigned char *checksum,
182 const u64 Ls[16]) ASM_FUNC_ABI;
183
184 extern void _gcry_serpent_avx2_ocb_dec(serpent_context_t *ctx,
185 unsigned char *out,
186 const unsigned char *in,
187 unsigned char *offset,
188 unsigned char *checksum,
189 const u64 Ls[16]) ASM_FUNC_ABI;
190
191 extern void _gcry_serpent_avx2_ocb_auth(serpent_context_t *ctx,
192 const unsigned char *abuf,
193 unsigned char *offset,
194 unsigned char *checksum,
195 const u64 Ls[16]) ASM_FUNC_ABI;
196
197 extern void _gcry_serpent_avx2_blk16(const serpent_context_t *c, byte *out,
198 const byte *in, int encrypt) ASM_FUNC_ABI;
199 #endif
200
201 #ifdef USE_AVX512
202 /* Assembler implementations of Serpent using AVX512. Processing 32 blocks in
203 parallel.
204 */
205 extern void _gcry_serpent_avx512_cbc_dec(const void *ctx,
206 unsigned char *out,
207 const unsigned char *in,
208 unsigned char *iv);
209
210 extern void _gcry_serpent_avx512_cfb_dec(const void *ctx,
211 unsigned char *out,
212 const unsigned char *in,
213 unsigned char *iv);
214
215 extern void _gcry_serpent_avx512_ctr_enc(const void *ctx,
216 unsigned char *out,
217 const unsigned char *in,
218 unsigned char *ctr);
219
220 extern void _gcry_serpent_avx512_ocb_crypt(const void *ctx,
221 unsigned char *out,
222 const unsigned char *in,
223 unsigned char *offset,
224 unsigned char *checksum,
225 const ocb_L_uintptr_t Ls[32],
226 int encrypt);
227
228 extern void _gcry_serpent_avx512_blk32(const void *c, byte *out,
229 const byte *in,
230 int encrypt);
231 #endif
232
233 #ifdef USE_NEON
234 /* Assembler implementations of Serpent using ARM NEON. Process 8 block in
235 parallel.
236 */
237 extern void _gcry_serpent_neon_ctr_enc(serpent_context_t *ctx,
238 unsigned char *out,
239 const unsigned char *in,
240 unsigned char *ctr);
241
242 extern void _gcry_serpent_neon_cbc_dec(serpent_context_t *ctx,
243 unsigned char *out,
244 const unsigned char *in,
245 unsigned char *iv);
246
247 extern void _gcry_serpent_neon_cfb_dec(serpent_context_t *ctx,
248 unsigned char *out,
249 const unsigned char *in,
250 unsigned char *iv);
251
252 extern void _gcry_serpent_neon_ocb_enc(serpent_context_t *ctx,
253 unsigned char *out,
254 const unsigned char *in,
255 unsigned char *offset,
256 unsigned char *checksum,
257 const void *Ls[8]);
258
259 extern void _gcry_serpent_neon_ocb_dec(serpent_context_t *ctx,
260 unsigned char *out,
261 const unsigned char *in,
262 unsigned char *offset,
263 unsigned char *checksum,
264 const void *Ls[8]);
265
266 extern void _gcry_serpent_neon_ocb_auth(serpent_context_t *ctx,
267 const unsigned char *abuf,
268 unsigned char *offset,
269 unsigned char *checksum,
270 const void *Ls[8]);
271
272 extern void _gcry_serpent_neon_blk8(const serpent_context_t *c, byte *out,
273 const byte *in, int encrypt);
274 #endif
275
276
277 /* Prototypes. */
278 static const char *serpent_test (void);
279
280 static void _gcry_serpent_ctr_enc (void *context, unsigned char *ctr,
281 void *outbuf_arg, const void *inbuf_arg,
282 size_t nblocks);
283 static void _gcry_serpent_cbc_dec (void *context, unsigned char *iv,
284 void *outbuf_arg, const void *inbuf_arg,
285 size_t nblocks);
286 static void _gcry_serpent_cfb_dec (void *context, unsigned char *iv,
287 void *outbuf_arg, const void *inbuf_arg,
288 size_t nblocks);
289 static size_t _gcry_serpent_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
290 const void *inbuf_arg, size_t nblocks,
291 int encrypt);
292 static size_t _gcry_serpent_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
293 size_t nblocks);
294 static void _gcry_serpent_xts_crypt (void *context, unsigned char *tweak,
295 void *outbuf_arg, const void *inbuf_arg,
296 size_t nblocks, int encrypt);
297 static void _gcry_serpent_ecb_crypt (void *context, void *outbuf_arg,
298 const void *inbuf_arg, size_t nblocks,
299 int encrypt);
300
301
302 /*
303 * These are the S-Boxes of Serpent from following research paper.
304 *
305 * D. A. Osvik, “Speeding up Serpent,” in Third AES Candidate Conference,
306 * (New York, New York, USA), p. 317–329, National Institute of Standards and
307 * Technology, 2000.
308 *
309 * Paper is also available at: http://www.ii.uib.no/~osvik/pub/aes3.pdf
310 *
311 */
312
313 #define SBOX0(r0, r1, r2, r3, w, x, y, z) \
314 { \
315 u32 r4; \
316 \
317 r3 ^= r0; r4 = r1; \
318 r1 &= r3; r4 ^= r2; \
319 r1 ^= r0; r0 |= r3; \
320 r0 ^= r4; r4 ^= r3; \
321 r3 ^= r2; r2 |= r1; \
322 r2 ^= r4; r4 = ~r4; \
323 r4 |= r1; r1 ^= r3; \
324 r1 ^= r4; r3 |= r0; \
325 r1 ^= r3; r4 ^= r3; \
326 \
327 w = r1; x = r4; y = r2; z = r0; \
328 }
329
330 #define SBOX0_INVERSE(r0, r1, r2, r3, w, x, y, z) \
331 { \
332 u32 r4; \
333 \
334 r2 = ~r2; r4 = r1; \
335 r1 |= r0; r4 = ~r4; \
336 r1 ^= r2; r2 |= r4; \
337 r1 ^= r3; r0 ^= r4; \
338 r2 ^= r0; r0 &= r3; \
339 r4 ^= r0; r0 |= r1; \
340 r0 ^= r2; r3 ^= r4; \
341 r2 ^= r1; r3 ^= r0; \
342 r3 ^= r1; \
343 r2 &= r3; \
344 r4 ^= r2; \
345 \
346 w = r0; x = r4; y = r1; z = r3; \
347 }
348
349 #define SBOX1(r0, r1, r2, r3, w, x, y, z) \
350 { \
351 u32 r4; \
352 \
353 r0 = ~r0; r2 = ~r2; \
354 r4 = r0; r0 &= r1; \
355 r2 ^= r0; r0 |= r3; \
356 r3 ^= r2; r1 ^= r0; \
357 r0 ^= r4; r4 |= r1; \
358 r1 ^= r3; r2 |= r0; \
359 r2 &= r4; r0 ^= r1; \
360 r1 &= r2; \
361 r1 ^= r0; r0 &= r2; \
362 r0 ^= r4; \
363 \
364 w = r2; x = r0; y = r3; z = r1; \
365 }
366
367 #define SBOX1_INVERSE(r0, r1, r2, r3, w, x, y, z) \
368 { \
369 u32 r4; \
370 \
371 r4 = r1; r1 ^= r3; \
372 r3 &= r1; r4 ^= r2; \
373 r3 ^= r0; r0 |= r1; \
374 r2 ^= r3; r0 ^= r4; \
375 r0 |= r2; r1 ^= r3; \
376 r0 ^= r1; r1 |= r3; \
377 r1 ^= r0; r4 = ~r4; \
378 r4 ^= r1; r1 |= r0; \
379 r1 ^= r0; \
380 r1 |= r4; \
381 r3 ^= r1; \
382 \
383 w = r4; x = r0; y = r3; z = r2; \
384 }
385
386 #define SBOX2(r0, r1, r2, r3, w, x, y, z) \
387 { \
388 u32 r4; \
389 \
390 r4 = r0; r0 &= r2; \
391 r0 ^= r3; r2 ^= r1; \
392 r2 ^= r0; r3 |= r4; \
393 r3 ^= r1; r4 ^= r2; \
394 r1 = r3; r3 |= r4; \
395 r3 ^= r0; r0 &= r1; \
396 r4 ^= r0; r1 ^= r3; \
397 r1 ^= r4; r4 = ~r4; \
398 \
399 w = r2; x = r3; y = r1; z = r4; \
400 }
401
402 #define SBOX2_INVERSE(r0, r1, r2, r3, w, x, y, z) \
403 { \
404 u32 r4; \
405 \
406 r2 ^= r3; r3 ^= r0; \
407 r4 = r3; r3 &= r2; \
408 r3 ^= r1; r1 |= r2; \
409 r1 ^= r4; r4 &= r3; \
410 r2 ^= r3; r4 &= r0; \
411 r4 ^= r2; r2 &= r1; \
412 r2 |= r0; r3 = ~r3; \
413 r2 ^= r3; r0 ^= r3; \
414 r0 &= r1; r3 ^= r4; \
415 r3 ^= r0; \
416 \
417 w = r1; x = r4; y = r2; z = r3; \
418 }
419
420 #define SBOX3(r0, r1, r2, r3, w, x, y, z) \
421 { \
422 u32 r4; \
423 \
424 r4 = r0; r0 |= r3; \
425 r3 ^= r1; r1 &= r4; \
426 r4 ^= r2; r2 ^= r3; \
427 r3 &= r0; r4 |= r1; \
428 r3 ^= r4; r0 ^= r1; \
429 r4 &= r0; r1 ^= r3; \
430 r4 ^= r2; r1 |= r0; \
431 r1 ^= r2; r0 ^= r3; \
432 r2 = r1; r1 |= r3; \
433 r1 ^= r0; \
434 \
435 w = r1; x = r2; y = r3; z = r4; \
436 }
437
438 #define SBOX3_INVERSE(r0, r1, r2, r3, w, x, y, z) \
439 { \
440 u32 r4; \
441 \
442 r4 = r2; r2 ^= r1; \
443 r0 ^= r2; r4 &= r2; \
444 r4 ^= r0; r0 &= r1; \
445 r1 ^= r3; r3 |= r4; \
446 r2 ^= r3; r0 ^= r3; \
447 r1 ^= r4; r3 &= r2; \
448 r3 ^= r1; r1 ^= r0; \
449 r1 |= r2; r0 ^= r3; \
450 r1 ^= r4; \
451 r0 ^= r1; \
452 \
453 w = r2; x = r1; y = r3; z = r0; \
454 }
455
456 #define SBOX4(r0, r1, r2, r3, w, x, y, z) \
457 { \
458 u32 r4; \
459 \
460 r1 ^= r3; r3 = ~r3; \
461 r2 ^= r3; r3 ^= r0; \
462 r4 = r1; r1 &= r3; \
463 r1 ^= r2; r4 ^= r3; \
464 r0 ^= r4; r2 &= r4; \
465 r2 ^= r0; r0 &= r1; \
466 r3 ^= r0; r4 |= r1; \
467 r4 ^= r0; r0 |= r3; \
468 r0 ^= r2; r2 &= r3; \
469 r0 = ~r0; r4 ^= r2; \
470 \
471 w = r1; x = r4; y = r0; z = r3; \
472 }
473
474 #define SBOX4_INVERSE(r0, r1, r2, r3, w, x, y, z) \
475 { \
476 u32 r4; \
477 \
478 r4 = r2; r2 &= r3; \
479 r2 ^= r1; r1 |= r3; \
480 r1 &= r0; r4 ^= r2; \
481 r4 ^= r1; r1 &= r2; \
482 r0 = ~r0; r3 ^= r4; \
483 r1 ^= r3; r3 &= r0; \
484 r3 ^= r2; r0 ^= r1; \
485 r2 &= r0; r3 ^= r0; \
486 r2 ^= r4; \
487 r2 |= r3; r3 ^= r0; \
488 r2 ^= r1; \
489 \
490 w = r0; x = r3; y = r2; z = r4; \
491 }
492
493 #define SBOX5(r0, r1, r2, r3, w, x, y, z) \
494 { \
495 u32 r4; \
496 \
497 r0 ^= r1; r1 ^= r3; \
498 r3 = ~r3; r4 = r1; \
499 r1 &= r0; r2 ^= r3; \
500 r1 ^= r2; r2 |= r4; \
501 r4 ^= r3; r3 &= r1; \
502 r3 ^= r0; r4 ^= r1; \
503 r4 ^= r2; r2 ^= r0; \
504 r0 &= r3; r2 = ~r2; \
505 r0 ^= r4; r4 |= r3; \
506 r2 ^= r4; \
507 \
508 w = r1; x = r3; y = r0; z = r2; \
509 }
510
511 #define SBOX5_INVERSE(r0, r1, r2, r3, w, x, y, z) \
512 { \
513 u32 r4; \
514 \
515 r1 = ~r1; r4 = r3; \
516 r2 ^= r1; r3 |= r0; \
517 r3 ^= r2; r2 |= r1; \
518 r2 &= r0; r4 ^= r3; \
519 r2 ^= r4; r4 |= r0; \
520 r4 ^= r1; r1 &= r2; \
521 r1 ^= r3; r4 ^= r2; \
522 r3 &= r4; r4 ^= r1; \
523 r3 ^= r4; r4 = ~r4; \
524 r3 ^= r0; \
525 \
526 w = r1; x = r4; y = r3; z = r2; \
527 }
528
529 #define SBOX6(r0, r1, r2, r3, w, x, y, z) \
530 { \
531 u32 r4; \
532 \
533 r2 = ~r2; r4 = r3; \
534 r3 &= r0; r0 ^= r4; \
535 r3 ^= r2; r2 |= r4; \
536 r1 ^= r3; r2 ^= r0; \
537 r0 |= r1; r2 ^= r1; \
538 r4 ^= r0; r0 |= r3; \
539 r0 ^= r2; r4 ^= r3; \
540 r4 ^= r0; r3 = ~r3; \
541 r2 &= r4; \
542 r2 ^= r3; \
543 \
544 w = r0; x = r1; y = r4; z = r2; \
545 }
546
547 #define SBOX6_INVERSE(r0, r1, r2, r3, w, x, y, z) \
548 { \
549 u32 r4; \
550 \
551 r0 ^= r2; r4 = r2; \
552 r2 &= r0; r4 ^= r3; \
553 r2 = ~r2; r3 ^= r1; \
554 r2 ^= r3; r4 |= r0; \
555 r0 ^= r2; r3 ^= r4; \
556 r4 ^= r1; r1 &= r3; \
557 r1 ^= r0; r0 ^= r3; \
558 r0 |= r2; r3 ^= r1; \
559 r4 ^= r0; \
560 \
561 w = r1; x = r2; y = r4; z = r3; \
562 }
563
564 #define SBOX7(r0, r1, r2, r3, w, x, y, z) \
565 { \
566 u32 r4; \
567 \
568 r4 = r1; r1 |= r2; \
569 r1 ^= r3; r4 ^= r2; \
570 r2 ^= r1; r3 |= r4; \
571 r3 &= r0; r4 ^= r2; \
572 r3 ^= r1; r1 |= r4; \
573 r1 ^= r0; r0 |= r4; \
574 r0 ^= r2; r1 ^= r4; \
575 r2 ^= r1; r1 &= r0; \
576 r1 ^= r4; r2 = ~r2; \
577 r2 |= r0; \
578 r4 ^= r2; \
579 \
580 w = r4; x = r3; y = r1; z = r0; \
581 }
582
583 #define SBOX7_INVERSE(r0, r1, r2, r3, w, x, y, z) \
584 { \
585 u32 r4; \
586 \
587 r4 = r2; r2 ^= r0; \
588 r0 &= r3; r4 |= r3; \
589 r2 = ~r2; r3 ^= r1; \
590 r1 |= r0; r0 ^= r2; \
591 r2 &= r4; r3 &= r4; \
592 r1 ^= r2; r2 ^= r0; \
593 r0 |= r2; r4 ^= r1; \
594 r0 ^= r3; r3 ^= r4; \
595 r4 |= r0; r3 ^= r2; \
596 r4 ^= r2; \
597 \
598 w = r3; x = r0; y = r1; z = r4; \
599 }
600
601 /* XOR BLOCK1 into BLOCK0. */
602 #define BLOCK_XOR(block0, block1) \
603 { \
604 block0[0] ^= block1[0]; \
605 block0[1] ^= block1[1]; \
606 block0[2] ^= block1[2]; \
607 block0[3] ^= block1[3]; \
608 }
609
610 /* Copy BLOCK_SRC to BLOCK_DST. */
611 #define BLOCK_COPY(block_dst, block_src) \
612 { \
613 block_dst[0] = block_src[0]; \
614 block_dst[1] = block_src[1]; \
615 block_dst[2] = block_src[2]; \
616 block_dst[3] = block_src[3]; \
617 }
618
619 /* Apply SBOX number WHICH to to the block found in ARRAY0, writing
620 the output to the block found in ARRAY1. */
621 #define SBOX(which, array0, array1) \
622 SBOX##which (array0[0], array0[1], array0[2], array0[3], \
623 array1[0], array1[1], array1[2], array1[3]);
624
625 /* Apply inverse SBOX number WHICH to to the block found in ARRAY0, writing
626 the output to the block found in ARRAY1. */
627 #define SBOX_INVERSE(which, array0, array1) \
628 SBOX##which##_INVERSE (array0[0], array0[1], array0[2], array0[3], \
629 array1[0], array1[1], array1[2], array1[3]);
630
631 /* Apply the linear transformation to BLOCK. */
632 #define LINEAR_TRANSFORMATION(block) \
633 { \
634 block[0] = rol (block[0], 13); \
635 block[2] = rol (block[2], 3); \
636 block[1] = block[1] ^ block[0] ^ block[2]; \
637 block[3] = block[3] ^ block[2] ^ (block[0] << 3); \
638 block[1] = rol (block[1], 1); \
639 block[3] = rol (block[3], 7); \
640 block[0] = block[0] ^ block[1] ^ block[3]; \
641 block[2] = block[2] ^ block[3] ^ (block[1] << 7); \
642 block[0] = rol (block[0], 5); \
643 block[2] = rol (block[2], 22); \
644 }
645
646 /* Apply the inverse linear transformation to BLOCK. */
647 #define LINEAR_TRANSFORMATION_INVERSE(block) \
648 { \
649 block[2] = ror (block[2], 22); \
650 block[0] = ror (block[0] , 5); \
651 block[2] = block[2] ^ block[3] ^ (block[1] << 7); \
652 block[0] = block[0] ^ block[1] ^ block[3]; \
653 block[3] = ror (block[3], 7); \
654 block[1] = ror (block[1], 1); \
655 block[3] = block[3] ^ block[2] ^ (block[0] << 3); \
656 block[1] = block[1] ^ block[0] ^ block[2]; \
657 block[2] = ror (block[2], 3); \
658 block[0] = ror (block[0], 13); \
659 }
660
661 /* Apply a Serpent round to BLOCK, using the SBOX number WHICH and the
662 subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary storage.
663 This macro increments `round'. */
664 #define ROUND(which, subkeys, block, block_tmp) \
665 { \
666 BLOCK_XOR (block, subkeys[round]); \
667 round++; \
668 SBOX (which, block, block_tmp); \
669 LINEAR_TRANSFORMATION (block_tmp); \
670 BLOCK_COPY (block, block_tmp); \
671 }
672
673 /* Apply the last Serpent round to BLOCK, using the SBOX number WHICH
674 and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary
675 storage. The result will be stored in BLOCK_TMP. This macro
676 increments `round'. */
677 #define ROUND_LAST(which, subkeys, block, block_tmp) \
678 { \
679 BLOCK_XOR (block, subkeys[round]); \
680 round++; \
681 SBOX (which, block, block_tmp); \
682 BLOCK_XOR (block_tmp, subkeys[round]); \
683 round++; \
684 }
685
686 /* Apply an inverse Serpent round to BLOCK, using the SBOX number
687 WHICH and the subkeys contained in SUBKEYS. Use BLOCK_TMP as
688 temporary storage. This macro increments `round'. */
689 #define ROUND_INVERSE(which, subkey, block, block_tmp) \
690 { \
691 LINEAR_TRANSFORMATION_INVERSE (block); \
692 SBOX_INVERSE (which, block, block_tmp); \
693 BLOCK_XOR (block_tmp, subkey[round]); \
694 round--; \
695 BLOCK_COPY (block, block_tmp); \
696 }
697
698 /* Apply the first Serpent round to BLOCK, using the SBOX number WHICH
699 and the subkeys contained in SUBKEYS. Use BLOCK_TMP as temporary
700 storage. The result will be stored in BLOCK_TMP. This macro
701 increments `round'. */
702 #define ROUND_FIRST_INVERSE(which, subkeys, block, block_tmp) \
703 { \
704 BLOCK_XOR (block, subkeys[round]); \
705 round--; \
706 SBOX_INVERSE (which, block, block_tmp); \
707 BLOCK_XOR (block_tmp, subkeys[round]); \
708 round--; \
709 }
710
711 /* Convert the user provided key KEY of KEY_LENGTH bytes into the
712 internally used format. */
713 static void
714 serpent_key_prepare (const byte *key, unsigned int key_length,
715 serpent_key_t key_prepared)
716 {
717 int i;
718
719 /* Copy key. */
720 key_length /= 4;
721 for (i = 0; i < key_length; i++)
722 key_prepared[i] = buf_get_le32 (key + i * 4);
723
724 if (i < 8)
725 {
726 /* Key must be padded according to the Serpent
727 specification. */
728 key_prepared[i] = 0x00000001;
729
730 for (i++; i < 8; i++)
731 key_prepared[i] = 0;
732 }
733 }
734
735 /* Derive the 33 subkeys from KEY and store them in SUBKEYS. */
736 static void
737 serpent_subkeys_generate (serpent_key_t key, serpent_subkeys_t subkeys)
738 {
739 u32 w[8]; /* The `prekey'. */
740 u32 ws[4];
741 u32 wt[4];
742
743 /* Initialize with key values. */
744 w[0] = key[0];
745 w[1] = key[1];
746 w[2] = key[2];
747 w[3] = key[3];
748 w[4] = key[4];
749 w[5] = key[5];
750 w[6] = key[6];
751 w[7] = key[7];
752
753 /* Expand to intermediate key using the affine recurrence. */
754 #define EXPAND_KEY4(wo, r) \
755 wo[0] = w[(r+0)%8] = \
756 rol (w[(r+0)%8] ^ w[(r+3)%8] ^ w[(r+5)%8] ^ w[(r+7)%8] ^ PHI ^ (r+0), 11); \
757 wo[1] = w[(r+1)%8] = \
758 rol (w[(r+1)%8] ^ w[(r+4)%8] ^ w[(r+6)%8] ^ w[(r+0)%8] ^ PHI ^ (r+1), 11); \
759 wo[2] = w[(r+2)%8] = \
760 rol (w[(r+2)%8] ^ w[(r+5)%8] ^ w[(r+7)%8] ^ w[(r+1)%8] ^ PHI ^ (r+2), 11); \
761 wo[3] = w[(r+3)%8] = \
762 rol (w[(r+3)%8] ^ w[(r+6)%8] ^ w[(r+0)%8] ^ w[(r+2)%8] ^ PHI ^ (r+3), 11);
763
764 #define EXPAND_KEY(r) \
765 EXPAND_KEY4(ws, (r)); \
766 EXPAND_KEY4(wt, (r + 4));
767
768 /* Calculate subkeys via S-Boxes, in bitslice mode. */
769 EXPAND_KEY (0); SBOX (3, ws, subkeys[0]); SBOX (2, wt, subkeys[1]);
770 EXPAND_KEY (8); SBOX (1, ws, subkeys[2]); SBOX (0, wt, subkeys[3]);
771 EXPAND_KEY (16); SBOX (7, ws, subkeys[4]); SBOX (6, wt, subkeys[5]);
772 EXPAND_KEY (24); SBOX (5, ws, subkeys[6]); SBOX (4, wt, subkeys[7]);
773 EXPAND_KEY (32); SBOX (3, ws, subkeys[8]); SBOX (2, wt, subkeys[9]);
774 EXPAND_KEY (40); SBOX (1, ws, subkeys[10]); SBOX (0, wt, subkeys[11]);
775 EXPAND_KEY (48); SBOX (7, ws, subkeys[12]); SBOX (6, wt, subkeys[13]);
776 EXPAND_KEY (56); SBOX (5, ws, subkeys[14]); SBOX (4, wt, subkeys[15]);
777 EXPAND_KEY (64); SBOX (3, ws, subkeys[16]); SBOX (2, wt, subkeys[17]);
778 EXPAND_KEY (72); SBOX (1, ws, subkeys[18]); SBOX (0, wt, subkeys[19]);
779 EXPAND_KEY (80); SBOX (7, ws, subkeys[20]); SBOX (6, wt, subkeys[21]);
780 EXPAND_KEY (88); SBOX (5, ws, subkeys[22]); SBOX (4, wt, subkeys[23]);
781 EXPAND_KEY (96); SBOX (3, ws, subkeys[24]); SBOX (2, wt, subkeys[25]);
782 EXPAND_KEY (104); SBOX (1, ws, subkeys[26]); SBOX (0, wt, subkeys[27]);
783 EXPAND_KEY (112); SBOX (7, ws, subkeys[28]); SBOX (6, wt, subkeys[29]);
784 EXPAND_KEY (120); SBOX (5, ws, subkeys[30]); SBOX (4, wt, subkeys[31]);
785 EXPAND_KEY4 (ws, 128); SBOX (3, ws, subkeys[32]);
786
787 wipememory (ws, sizeof (ws));
788 wipememory (wt, sizeof (wt));
789 wipememory (w, sizeof (w));
790 }
791
792 /* Initialize CONTEXT with the key KEY of KEY_LENGTH bits. */
793 static gcry_err_code_t
794 serpent_setkey_internal (serpent_context_t *context,
795 const byte *key, unsigned int key_length)
796 {
797 serpent_key_t key_prepared;
798
799 if (key_length > 32)
800 return GPG_ERR_INV_KEYLEN;
801
802 serpent_key_prepare (key, key_length, key_prepared);
803 serpent_subkeys_generate (key_prepared, context->keys);
804
805 #ifdef USE_AVX512
806 context->use_avx512 = 0;
807 if ((_gcry_get_hw_features () & HWF_INTEL_AVX512))
808 {
809 context->use_avx512 = 1;
810 }
811 #endif
812
813 #ifdef USE_AVX2
814 context->use_avx2 = 0;
815 if ((_gcry_get_hw_features () & HWF_INTEL_AVX2))
816 {
817 context->use_avx2 = 1;
818 }
819 #endif
820
821 #ifdef USE_NEON
822 context->use_neon = 0;
823 if ((_gcry_get_hw_features () & HWF_ARM_NEON))
824 {
825 context->use_neon = 1;
826 }
827 #endif
828
829 wipememory (key_prepared, sizeof(key_prepared));
830 return 0;
831 }
832
833 /* Initialize CTX with the key KEY of KEY_LENGTH bytes. */
834 static gcry_err_code_t
835 serpent_setkey (void *ctx,
836 const byte *key, unsigned int key_length,
837 cipher_bulk_ops_t *bulk_ops)
838 {
839 serpent_context_t *context = ctx;
840 static const char *serpent_test_ret;
841 static int serpent_init_done;
842 gcry_err_code_t ret = GPG_ERR_NO_ERROR;
843
844 if (! serpent_init_done)
845 {
846 /* Execute a self-test the first time, Serpent is used. */
847 serpent_init_done = 1;
848 serpent_test_ret = serpent_test ();
849 if (serpent_test_ret)
850 log_error ("Serpent test failure: %s\n", serpent_test_ret);
851 }
852
853 /* Setup bulk encryption routines. */
854 memset (bulk_ops, 0, sizeof(*bulk_ops));
855 bulk_ops->cbc_dec = _gcry_serpent_cbc_dec;
856 bulk_ops->cfb_dec = _gcry_serpent_cfb_dec;
857 bulk_ops->ctr_enc = _gcry_serpent_ctr_enc;
858 bulk_ops->ocb_crypt = _gcry_serpent_ocb_crypt;
859 bulk_ops->ocb_auth = _gcry_serpent_ocb_auth;
860 bulk_ops->xts_crypt = _gcry_serpent_xts_crypt;
861 bulk_ops->ecb_crypt = _gcry_serpent_ecb_crypt;
862
863 if (serpent_test_ret)
864 ret = GPG_ERR_SELFTEST_FAILED;
865 else
866 ret = serpent_setkey_internal (context, key, key_length);
867
868 return ret;
869 }
870
871 static void
872 serpent_encrypt_internal (serpent_context_t *context,
873 const byte *input, byte *output)
874 {
875 serpent_block_t b, b_next;
876 int round = 0;
877
878 b[0] = buf_get_le32 (input + 0);
879 b[1] = buf_get_le32 (input + 4);
880 b[2] = buf_get_le32 (input + 8);
881 b[3] = buf_get_le32 (input + 12);
882
883 ROUND (0, context->keys, b, b_next);
884 ROUND (1, context->keys, b, b_next);
885 ROUND (2, context->keys, b, b_next);
886 ROUND (3, context->keys, b, b_next);
887 ROUND (4, context->keys, b, b_next);
888 ROUND (5, context->keys, b, b_next);
889 ROUND (6, context->keys, b, b_next);
890 ROUND (7, context->keys, b, b_next);
891 ROUND (0, context->keys, b, b_next);
892 ROUND (1, context->keys, b, b_next);
893 ROUND (2, context->keys, b, b_next);
894 ROUND (3, context->keys, b, b_next);
895 ROUND (4, context->keys, b, b_next);
896 ROUND (5, context->keys, b, b_next);
897 ROUND (6, context->keys, b, b_next);
898 ROUND (7, context->keys, b, b_next);
899 ROUND (0, context->keys, b, b_next);
900 ROUND (1, context->keys, b, b_next);
901 ROUND (2, context->keys, b, b_next);
902 ROUND (3, context->keys, b, b_next);
903 ROUND (4, context->keys, b, b_next);
904 ROUND (5, context->keys, b, b_next);
905 ROUND (6, context->keys, b, b_next);
906 ROUND (7, context->keys, b, b_next);
907 ROUND (0, context->keys, b, b_next);
908 ROUND (1, context->keys, b, b_next);
909 ROUND (2, context->keys, b, b_next);
910 ROUND (3, context->keys, b, b_next);
911 ROUND (4, context->keys, b, b_next);
912 ROUND (5, context->keys, b, b_next);
913 ROUND (6, context->keys, b, b_next);
914
915 ROUND_LAST (7, context->keys, b, b_next);
916
917 buf_put_le32 (output + 0, b_next[0]);
918 buf_put_le32 (output + 4, b_next[1]);
919 buf_put_le32 (output + 8, b_next[2]);
920 buf_put_le32 (output + 12, b_next[3]);
921 }
922
923 static void
924 serpent_decrypt_internal (serpent_context_t *context,
925 const byte *input, byte *output)
926 {
927 serpent_block_t b, b_next;
928 int round = ROUNDS;
929
930 b_next[0] = buf_get_le32 (input + 0);
931 b_next[1] = buf_get_le32 (input + 4);
932 b_next[2] = buf_get_le32 (input + 8);
933 b_next[3] = buf_get_le32 (input + 12);
934
935 ROUND_FIRST_INVERSE (7, context->keys, b_next, b);
936
937 ROUND_INVERSE (6, context->keys, b, b_next);
938 ROUND_INVERSE (5, context->keys, b, b_next);
939 ROUND_INVERSE (4, context->keys, b, b_next);
940 ROUND_INVERSE (3, context->keys, b, b_next);
941 ROUND_INVERSE (2, context->keys, b, b_next);
942 ROUND_INVERSE (1, context->keys, b, b_next);
943 ROUND_INVERSE (0, context->keys, b, b_next);
944 ROUND_INVERSE (7, context->keys, b, b_next);
945 ROUND_INVERSE (6, context->keys, b, b_next);
946 ROUND_INVERSE (5, context->keys, b, b_next);
947 ROUND_INVERSE (4, context->keys, b, b_next);
948 ROUND_INVERSE (3, context->keys, b, b_next);
949 ROUND_INVERSE (2, context->keys, b, b_next);
950 ROUND_INVERSE (1, context->keys, b, b_next);
951 ROUND_INVERSE (0, context->keys, b, b_next);
952 ROUND_INVERSE (7, context->keys, b, b_next);
953 ROUND_INVERSE (6, context->keys, b, b_next);
954 ROUND_INVERSE (5, context->keys, b, b_next);
955 ROUND_INVERSE (4, context->keys, b, b_next);
956 ROUND_INVERSE (3, context->keys, b, b_next);
957 ROUND_INVERSE (2, context->keys, b, b_next);
958 ROUND_INVERSE (1, context->keys, b, b_next);
959 ROUND_INVERSE (0, context->keys, b, b_next);
960 ROUND_INVERSE (7, context->keys, b, b_next);
961 ROUND_INVERSE (6, context->keys, b, b_next);
962 ROUND_INVERSE (5, context->keys, b, b_next);
963 ROUND_INVERSE (4, context->keys, b, b_next);
964 ROUND_INVERSE (3, context->keys, b, b_next);
965 ROUND_INVERSE (2, context->keys, b, b_next);
966 ROUND_INVERSE (1, context->keys, b, b_next);
967 ROUND_INVERSE (0, context->keys, b, b_next);
968
969 buf_put_le32 (output + 0, b_next[0]);
970 buf_put_le32 (output + 4, b_next[1]);
971 buf_put_le32 (output + 8, b_next[2]);
972 buf_put_le32 (output + 12, b_next[3]);
973 }
974
975 static unsigned int
976 serpent_encrypt (void *ctx, byte *buffer_out, const byte *buffer_in)
977 {
978 serpent_context_t *context = ctx;
979
980 serpent_encrypt_internal (context, buffer_in, buffer_out);
981 return /*burn_stack*/ (2 * sizeof (serpent_block_t));
982 }
983
984 static unsigned int
985 serpent_decrypt (void *ctx, byte *buffer_out, const byte *buffer_in)
986 {
987 serpent_context_t *context = ctx;
988
989 serpent_decrypt_internal (context, buffer_in, buffer_out);
990 return /*burn_stack*/ (2 * sizeof (serpent_block_t));
991 }
992
993
994
995 /* Bulk encryption of complete blocks in CTR mode. This function is only
996 intended for the bulk encryption feature of cipher.c. CTR is expected to be
997 of size sizeof(serpent_block_t). */
998 static void
999 _gcry_serpent_ctr_enc(void *context, unsigned char *ctr,
1000 void *outbuf_arg, const void *inbuf_arg,
1001 size_t nblocks)
1002 {
1003 serpent_context_t *ctx = context;
1004 unsigned char *outbuf = outbuf_arg;
1005 const unsigned char *inbuf = inbuf_arg;
1006 unsigned char tmpbuf[sizeof(serpent_block_t)];
1007 int burn_stack_depth = 2 * sizeof (serpent_block_t);
1008
1009 #ifdef USE_AVX512
1010 if (ctx->use_avx512)
1011 {
1012 int did_use_avx512 = 0;
1013
1014 /* Process data in 32 block chunks. */
1015 while (nblocks >= 32)
1016 {
1017 _gcry_serpent_avx512_ctr_enc(ctx, outbuf, inbuf, ctr);
1018
1019 nblocks -= 32;
1020 outbuf += 32 * sizeof(serpent_block_t);
1021 inbuf += 32 * sizeof(serpent_block_t);
1022 did_use_avx512 = 1;
1023 }
1024
1025 if (did_use_avx512)
1026 {
1027 /* serpent-avx512 code does not use stack */
1028 if (nblocks == 0)
1029 burn_stack_depth = 0;
1030 }
1031
1032 /* Use generic/avx2/sse2 code to handle smaller chunks... */
1033 /* TODO: use caching instead? */
1034 }
1035 #endif
1036
1037 #ifdef USE_AVX2
1038 if (ctx->use_avx2)
1039 {
1040 int did_use_avx2 = 0;
1041
1042 /* Process data in 16 block chunks. */
1043 while (nblocks >= 16)
1044 {
1045 _gcry_serpent_avx2_ctr_enc(ctx, outbuf, inbuf, ctr);
1046
1047 nblocks -= 16;
1048 outbuf += 16 * sizeof(serpent_block_t);
1049 inbuf += 16 * sizeof(serpent_block_t);
1050 did_use_avx2 = 1;
1051 }
1052
1053 if (did_use_avx2)
1054 {
1055 /* serpent-avx2 assembly code does not use stack */
1056 if (nblocks == 0)
1057 burn_stack_depth = 0;
1058 }
1059
1060 /* Use generic/sse2 code to handle smaller chunks... */
1061 /* TODO: use caching instead? */
1062 }
1063 #endif
1064
1065 #ifdef USE_SSE2
1066 {
1067 int did_use_sse2 = 0;
1068
1069 /* Process data in 8 block chunks. */
1070 while (nblocks >= 8)
1071 {
1072 _gcry_serpent_sse2_ctr_enc(ctx, outbuf, inbuf, ctr);
1073
1074 nblocks -= 8;
1075 outbuf += 8 * sizeof(serpent_block_t);
1076 inbuf += 8 * sizeof(serpent_block_t);
1077 did_use_sse2 = 1;
1078 }
1079
1080 if (did_use_sse2)
1081 {
1082 /* serpent-sse2 assembly code does not use stack */
1083 if (nblocks == 0)
1084 burn_stack_depth = 0;
1085 }
1086
1087 /* Use generic code to handle smaller chunks... */
1088 /* TODO: use caching instead? */
1089 }
1090 #endif
1091
1092 #ifdef USE_NEON
1093 if (ctx->use_neon)
1094 {
1095 int did_use_neon = 0;
1096
1097 /* Process data in 8 block chunks. */
1098 while (nblocks >= 8)
1099 {
1100 _gcry_serpent_neon_ctr_enc(ctx, outbuf, inbuf, ctr);
1101
1102 nblocks -= 8;
1103 outbuf += 8 * sizeof(serpent_block_t);
1104 inbuf += 8 * sizeof(serpent_block_t);
1105 did_use_neon = 1;
1106 }
1107
1108 if (did_use_neon)
1109 {
1110 /* serpent-neon assembly code does not use stack */
1111 if (nblocks == 0)
1112 burn_stack_depth = 0;
1113 }
1114
1115 /* Use generic code to handle smaller chunks... */
1116 /* TODO: use caching instead? */
1117 }
1118 #endif
1119
1120 for ( ;nblocks; nblocks-- )
1121 {
1122 /* Encrypt the counter. */
1123 serpent_encrypt_internal(ctx, ctr, tmpbuf);
1124 /* XOR the input with the encrypted counter and store in output. */
1125 cipher_block_xor(outbuf, tmpbuf, inbuf, sizeof(serpent_block_t));
1126 outbuf += sizeof(serpent_block_t);
1127 inbuf += sizeof(serpent_block_t);
1128 /* Increment the counter. */
1129 cipher_block_add(ctr, 1, sizeof(serpent_block_t));
1130 }
1131
1132 wipememory(tmpbuf, sizeof(tmpbuf));
1133 _gcry_burn_stack(burn_stack_depth);
1134 }
1135
1136 /* Bulk decryption of complete blocks in CBC mode. This function is only
1137 intended for the bulk encryption feature of cipher.c. */
1138 static void
1139 _gcry_serpent_cbc_dec(void *context, unsigned char *iv,
1140 void *outbuf_arg, const void *inbuf_arg,
1141 size_t nblocks)
1142 {
1143 serpent_context_t *ctx = context;
1144 unsigned char *outbuf = outbuf_arg;
1145 const unsigned char *inbuf = inbuf_arg;
1146 unsigned char savebuf[sizeof(serpent_block_t)];
1147 int burn_stack_depth = 2 * sizeof (serpent_block_t);
1148
1149 #ifdef USE_AVX512
1150 if (ctx->use_avx512)
1151 {
1152 int did_use_avx512 = 0;
1153
1154 /* Process data in 32 block chunks. */
1155 while (nblocks >= 32)
1156 {
1157 _gcry_serpent_avx512_cbc_dec(ctx, outbuf, inbuf, iv);
1158
1159 nblocks -= 32;
1160 outbuf += 32 * sizeof(serpent_block_t);
1161 inbuf += 32 * sizeof(serpent_block_t);
1162 did_use_avx512 = 1;
1163 }
1164
1165 if (did_use_avx512)
1166 {
1167 /* serpent-avx512 code does not use stack */
1168 if (nblocks == 0)
1169 burn_stack_depth = 0;
1170 }
1171
1172 /* Use generic/avx2/sse2 code to handle smaller chunks... */
1173 }
1174 #endif
1175
1176 #ifdef USE_AVX2
1177 if (ctx->use_avx2)
1178 {
1179 int did_use_avx2 = 0;
1180
1181 /* Process data in 16 block chunks. */
1182 while (nblocks >= 16)
1183 {
1184 _gcry_serpent_avx2_cbc_dec(ctx, outbuf, inbuf, iv);
1185
1186 nblocks -= 16;
1187 outbuf += 16 * sizeof(serpent_block_t);
1188 inbuf += 16 * sizeof(serpent_block_t);
1189 did_use_avx2 = 1;
1190 }
1191
1192 if (did_use_avx2)
1193 {
1194 /* serpent-avx2 assembly code does not use stack */
1195 if (nblocks == 0)
1196 burn_stack_depth = 0;
1197 }
1198
1199 /* Use generic/sse2 code to handle smaller chunks... */
1200 }
1201 #endif
1202
1203 #ifdef USE_SSE2
1204 {
1205 int did_use_sse2 = 0;
1206
1207 /* Process data in 8 block chunks. */
1208 while (nblocks >= 8)
1209 {
1210 _gcry_serpent_sse2_cbc_dec(ctx, outbuf, inbuf, iv);
1211
1212 nblocks -= 8;
1213 outbuf += 8 * sizeof(serpent_block_t);
1214 inbuf += 8 * sizeof(serpent_block_t);
1215 did_use_sse2 = 1;
1216 }
1217
1218 if (did_use_sse2)
1219 {
1220 /* serpent-sse2 assembly code does not use stack */
1221 if (nblocks == 0)
1222 burn_stack_depth = 0;
1223 }
1224
1225 /* Use generic code to handle smaller chunks... */
1226 }
1227 #endif
1228
1229 #ifdef USE_NEON
1230 if (ctx->use_neon)
1231 {
1232 int did_use_neon = 0;
1233
1234 /* Process data in 8 block chunks. */
1235 while (nblocks >= 8)
1236 {
1237 _gcry_serpent_neon_cbc_dec(ctx, outbuf, inbuf, iv);
1238
1239 nblocks -= 8;
1240 outbuf += 8 * sizeof(serpent_block_t);
1241 inbuf += 8 * sizeof(serpent_block_t);
1242 did_use_neon = 1;
1243 }
1244
1245 if (did_use_neon)
1246 {
1247 /* serpent-neon assembly code does not use stack */
1248 if (nblocks == 0)
1249 burn_stack_depth = 0;
1250 }
1251
1252 /* Use generic code to handle smaller chunks... */
1253 }
1254 #endif
1255
1256 for ( ;nblocks; nblocks-- )
1257 {
1258 /* INBUF is needed later and it may be identical to OUTBUF, so store
1259 the intermediate result to SAVEBUF. */
1260 serpent_decrypt_internal (ctx, inbuf, savebuf);
1261
1262 cipher_block_xor_n_copy_2(outbuf, savebuf, iv, inbuf,
1263 sizeof(serpent_block_t));
1264 inbuf += sizeof(serpent_block_t);
1265 outbuf += sizeof(serpent_block_t);
1266 }
1267
1268 wipememory(savebuf, sizeof(savebuf));
1269 _gcry_burn_stack(burn_stack_depth);
1270 }
1271
1272 /* Bulk decryption of complete blocks in CFB mode. This function is only
1273 intended for the bulk encryption feature of cipher.c. */
1274 static void
1275 _gcry_serpent_cfb_dec(void *context, unsigned char *iv,
1276 void *outbuf_arg, const void *inbuf_arg,
1277 size_t nblocks)
1278 {
1279 serpent_context_t *ctx = context;
1280 unsigned char *outbuf = outbuf_arg;
1281 const unsigned char *inbuf = inbuf_arg;
1282 int burn_stack_depth = 2 * sizeof (serpent_block_t);
1283
1284 #ifdef USE_AVX512
1285 if (ctx->use_avx512)
1286 {
1287 int did_use_avx512 = 0;
1288
1289 /* Process data in 32 block chunks. */
1290 while (nblocks >= 32)
1291 {
1292 _gcry_serpent_avx512_cfb_dec(ctx, outbuf, inbuf, iv);
1293
1294 nblocks -= 32;
1295 outbuf += 32 * sizeof(serpent_block_t);
1296 inbuf += 32 * sizeof(serpent_block_t);
1297 did_use_avx512 = 1;
1298 }
1299
1300 if (did_use_avx512)
1301 {
1302 /* serpent-avx512 code does not use stack */
1303 if (nblocks == 0)
1304 burn_stack_depth = 0;
1305 }
1306
1307 /* Use generic/avx2/sse2 code to handle smaller chunks... */
1308 }
1309 #endif
1310
1311 #ifdef USE_AVX2
1312 if (ctx->use_avx2)
1313 {
1314 int did_use_avx2 = 0;
1315
1316 /* Process data in 16 block chunks. */
1317 while (nblocks >= 16)
1318 {
1319 _gcry_serpent_avx2_cfb_dec(ctx, outbuf, inbuf, iv);
1320
1321 nblocks -= 16;
1322 outbuf += 16 * sizeof(serpent_block_t);
1323 inbuf += 16 * sizeof(serpent_block_t);
1324 did_use_avx2 = 1;
1325 }
1326
1327 if (did_use_avx2)
1328 {
1329 /* serpent-avx2 assembly code does not use stack */
1330 if (nblocks == 0)
1331 burn_stack_depth = 0;
1332 }
1333
1334 /* Use generic/sse2 code to handle smaller chunks... */
1335 }
1336 #endif
1337
1338 #ifdef USE_SSE2
1339 {
1340 int did_use_sse2 = 0;
1341
1342 /* Process data in 8 block chunks. */
1343 while (nblocks >= 8)
1344 {
1345 _gcry_serpent_sse2_cfb_dec(ctx, outbuf, inbuf, iv);
1346
1347 nblocks -= 8;
1348 outbuf += 8 * sizeof(serpent_block_t);
1349 inbuf += 8 * sizeof(serpent_block_t);
1350 did_use_sse2 = 1;
1351 }
1352
1353 if (did_use_sse2)
1354 {
1355 /* serpent-sse2 assembly code does not use stack */
1356 if (nblocks == 0)
1357 burn_stack_depth = 0;
1358 }
1359
1360 /* Use generic code to handle smaller chunks... */
1361 }
1362 #endif
1363
1364 #ifdef USE_NEON
1365 if (ctx->use_neon)
1366 {
1367 int did_use_neon = 0;
1368
1369 /* Process data in 8 block chunks. */
1370 while (nblocks >= 8)
1371 {
1372 _gcry_serpent_neon_cfb_dec(ctx, outbuf, inbuf, iv);
1373
1374 nblocks -= 8;
1375 outbuf += 8 * sizeof(serpent_block_t);
1376 inbuf += 8 * sizeof(serpent_block_t);
1377 did_use_neon = 1;
1378 }
1379
1380 if (did_use_neon)
1381 {
1382 /* serpent-neon assembly code does not use stack */
1383 if (nblocks == 0)
1384 burn_stack_depth = 0;
1385 }
1386
1387 /* Use generic code to handle smaller chunks... */
1388 }
1389 #endif
1390
1391 for ( ;nblocks; nblocks-- )
1392 {
1393 serpent_encrypt_internal(ctx, iv, iv);
1394 cipher_block_xor_n_copy(outbuf, iv, inbuf, sizeof(serpent_block_t));
1395 outbuf += sizeof(serpent_block_t);
1396 inbuf += sizeof(serpent_block_t);
1397 }
1398
1399 _gcry_burn_stack(burn_stack_depth);
1400 }
1401
1402 /* Bulk encryption/decryption of complete blocks in OCB mode. */
1403 static size_t
1404 _gcry_serpent_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
1405 const void *inbuf_arg, size_t nblocks, int encrypt)
1406 {
1407 #if defined(USE_AVX512) || defined(USE_AVX2) || defined(USE_SSE2) \
1408 || defined(USE_NEON)
1409 serpent_context_t *ctx = (void *)&c->context.c;
1410 unsigned char *outbuf = outbuf_arg;
1411 const unsigned char *inbuf = inbuf_arg;
1412 int burn_stack_depth = 2 * sizeof (serpent_block_t);
1413 u64 blkn = c->u_mode.ocb.data_nblocks;
1414 #else
1415 (void)c;
1416 (void)outbuf_arg;
1417 (void)inbuf_arg;
1418 (void)encrypt;
1419 #endif
1420
1421 #ifdef USE_AVX512
1422 if (ctx->use_avx512)
1423 {
1424 int did_use_avx512 = 0;
1425 ocb_L_uintptr_t Ls[32];
1426 ocb_L_uintptr_t *l;
1427
1428 if (nblocks >= 32)
1429 {
1430 l = bulk_ocb_prepare_L_pointers_array_blk32 (c, Ls, blkn);
1431
1432 /* Process data in 32 block chunks. */
1433 while (nblocks >= 32)
1434 {
1435 blkn += 32;
1436 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 32);
1437
1438 _gcry_serpent_avx512_ocb_crypt(ctx, outbuf, inbuf, c->u_iv.iv,
1439 c->u_ctr.ctr, Ls, encrypt);
1440
1441 nblocks -= 32;
1442 outbuf += 32 * sizeof(serpent_block_t);
1443 inbuf += 32 * sizeof(serpent_block_t);
1444 did_use_avx512 = 1;
1445 }
1446 }
1447
1448 if (did_use_avx512)
1449 {
1450 /* serpent-avx512 code does not use stack */
1451 if (nblocks == 0)
1452 burn_stack_depth = 0;
1453 }
1454
1455 /* Use generic code to handle smaller chunks... */
1456 }
1457 #endif
1458
1459 #ifdef USE_AVX2
1460 if (ctx->use_avx2)
1461 {
1462 int did_use_avx2 = 0;
1463 u64 Ls[16];
1464 u64 *l;
1465
1466 if (nblocks >= 16)
1467 {
1468 l = bulk_ocb_prepare_L_pointers_array_blk16 (c, Ls, blkn);
1469
1470 /* Process data in 16 block chunks. */
1471 while (nblocks >= 16)
1472 {
1473 blkn += 16;
1474 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 16);
1475
1476 if (encrypt)
1477 _gcry_serpent_avx2_ocb_enc(ctx, outbuf, inbuf, c->u_iv.iv,
1478 c->u_ctr.ctr, Ls);
1479 else
1480 _gcry_serpent_avx2_ocb_dec(ctx, outbuf, inbuf, c->u_iv.iv,
1481 c->u_ctr.ctr, Ls);
1482
1483 nblocks -= 16;
1484 outbuf += 16 * sizeof(serpent_block_t);
1485 inbuf += 16 * sizeof(serpent_block_t);
1486 did_use_avx2 = 1;
1487 }
1488 }
1489
1490 if (did_use_avx2)
1491 {
1492 /* serpent-avx2 assembly code does not use stack */
1493 if (nblocks == 0)
1494 burn_stack_depth = 0;
1495 }
1496
1497 /* Use generic code to handle smaller chunks... */
1498 }
1499 #endif
1500
1501 #ifdef USE_SSE2
1502 {
1503 int did_use_sse2 = 0;
1504 u64 Ls[8];
1505 u64 *l;
1506
1507 if (nblocks >= 8)
1508 {
1509 l = bulk_ocb_prepare_L_pointers_array_blk8 (c, Ls, blkn);
1510
1511 /* Process data in 8 block chunks. */
1512 while (nblocks >= 8)
1513 {
1514 blkn += 8;
1515 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 8);
1516
1517 if (encrypt)
1518 _gcry_serpent_sse2_ocb_enc(ctx, outbuf, inbuf, c->u_iv.iv,
1519 c->u_ctr.ctr, Ls);
1520 else
1521 _gcry_serpent_sse2_ocb_dec(ctx, outbuf, inbuf, c->u_iv.iv,
1522 c->u_ctr.ctr, Ls);
1523
1524 nblocks -= 8;
1525 outbuf += 8 * sizeof(serpent_block_t);
1526 inbuf += 8 * sizeof(serpent_block_t);
1527 did_use_sse2 = 1;
1528 }
1529 }
1530
1531 if (did_use_sse2)
1532 {
1533 /* serpent-sse2 assembly code does not use stack */
1534 if (nblocks == 0)
1535 burn_stack_depth = 0;
1536 }
1537
1538 /* Use generic code to handle smaller chunks... */
1539 }
1540 #endif
1541
1542 #ifdef USE_NEON
1543 if (ctx->use_neon)
1544 {
1545 int did_use_neon = 0;
1546 uintptr_t Ls[8];
1547 uintptr_t *l;
1548
1549 if (nblocks >= 8)
1550 {
1551 l = bulk_ocb_prepare_L_pointers_array_blk8 (c, Ls, blkn);
1552
1553 /* Process data in 8 block chunks. */
1554 while (nblocks >= 8)
1555 {
1556 blkn += 8;
1557 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 8);
1558
1559 if (encrypt)
1560 _gcry_serpent_neon_ocb_enc(ctx, outbuf, inbuf, c->u_iv.iv,
1561 c->u_ctr.ctr, (const void **)Ls);
1562 else
1563 _gcry_serpent_neon_ocb_dec(ctx, outbuf, inbuf, c->u_iv.iv,
1564 c->u_ctr.ctr, (const void **)Ls);
1565
1566 nblocks -= 8;
1567 outbuf += 8 * sizeof(serpent_block_t);
1568 inbuf += 8 * sizeof(serpent_block_t);
1569 did_use_neon = 1;
1570 }
1571 }
1572
1573 if (did_use_neon)
1574 {
1575 /* serpent-neon assembly code does not use stack */
1576 if (nblocks == 0)
1577 burn_stack_depth = 0;
1578 }
1579
1580 /* Use generic code to handle smaller chunks... */
1581 }
1582 #endif
1583
1584 #if defined(USE_AVX512) || defined(USE_AVX2) || defined(USE_SSE2) \
1585 || defined(USE_NEON)
1586 c->u_mode.ocb.data_nblocks = blkn;
1587
1588 if (burn_stack_depth)
1589 _gcry_burn_stack (burn_stack_depth + 4 * sizeof(void *));
1590 #endif
1591
1592 return nblocks;
1593 }
1594
1595 /* Bulk authentication of complete blocks in OCB mode. */
1596 static size_t
1597 _gcry_serpent_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
1598 size_t nblocks)
1599 {
1600 #if defined(USE_AVX2) || defined(USE_SSE2) || defined(USE_NEON)
1601 serpent_context_t *ctx = (void *)&c->context.c;
1602 const unsigned char *abuf = abuf_arg;
1603 int burn_stack_depth = 2 * sizeof(serpent_block_t);
1604 u64 blkn = c->u_mode.ocb.aad_nblocks;
1605 #else
1606 (void)c;
1607 (void)abuf_arg;
1608 #endif
1609
1610 #ifdef USE_AVX2
1611 if (ctx->use_avx2)
1612 {
1613 int did_use_avx2 = 0;
1614 u64 Ls[16];
1615 u64 *l;
1616
1617 if (nblocks >= 16)
1618 {
1619 l = bulk_ocb_prepare_L_pointers_array_blk16 (c, Ls, blkn);
1620
1621 /* Process data in 16 block chunks. */
1622 while (nblocks >= 16)
1623 {
1624 blkn += 16;
1625 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 16);
1626
1627 _gcry_serpent_avx2_ocb_auth(ctx, abuf, c->u_mode.ocb.aad_offset,
1628 c->u_mode.ocb.aad_sum, Ls);
1629
1630 nblocks -= 16;
1631 abuf += 16 * sizeof(serpent_block_t);
1632 did_use_avx2 = 1;
1633 }
1634 }
1635
1636 if (did_use_avx2)
1637 {
1638 /* serpent-avx2 assembly code does not use stack */
1639 if (nblocks == 0)
1640 burn_stack_depth = 0;
1641 }
1642
1643 /* Use generic code to handle smaller chunks... */
1644 }
1645 #endif
1646
1647 #ifdef USE_SSE2
1648 {
1649 int did_use_sse2 = 0;
1650 u64 Ls[8];
1651 u64 *l;
1652
1653 if (nblocks >= 8)
1654 {
1655 l = bulk_ocb_prepare_L_pointers_array_blk8 (c, Ls, blkn);
1656
1657 /* Process data in 8 block chunks. */
1658 while (nblocks >= 8)
1659 {
1660 blkn += 8;
1661 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 8);
1662
1663 _gcry_serpent_sse2_ocb_auth(ctx, abuf, c->u_mode.ocb.aad_offset,
1664 c->u_mode.ocb.aad_sum, Ls);
1665
1666 nblocks -= 8;
1667 abuf += 8 * sizeof(serpent_block_t);
1668 did_use_sse2 = 1;
1669 }
1670 }
1671
1672 if (did_use_sse2)
1673 {
1674 /* serpent-avx2 assembly code does not use stack */
1675 if (nblocks == 0)
1676 burn_stack_depth = 0;
1677 }
1678
1679 /* Use generic code to handle smaller chunks... */
1680 }
1681 #endif
1682
1683 #ifdef USE_NEON
1684 if (ctx->use_neon)
1685 {
1686 int did_use_neon = 0;
1687 uintptr_t Ls[8];
1688 uintptr_t *l;
1689
1690 if (nblocks >= 8)
1691 {
1692 l = bulk_ocb_prepare_L_pointers_array_blk8 (c, Ls, blkn);
1693
1694 /* Process data in 8 block chunks. */
1695 while (nblocks >= 8)
1696 {
1697 blkn += 8;
1698 *l = (uintptr_t)(void *)ocb_get_l(c, blkn - blkn % 8);
1699
1700 _gcry_serpent_neon_ocb_auth(ctx, abuf, c->u_mode.ocb.aad_offset,
1701 c->u_mode.ocb.aad_sum,
1702 (const void **)Ls);
1703
1704 nblocks -= 8;
1705 abuf += 8 * sizeof(serpent_block_t);
1706 did_use_neon = 1;
1707 }
1708 }
1709
1710 if (did_use_neon)
1711 {
1712 /* serpent-neon assembly code does not use stack */
1713 if (nblocks == 0)
1714 burn_stack_depth = 0;
1715 }
1716
1717 /* Use generic code to handle smaller chunks... */
1718 }
1719 #endif
1720
1721 #if defined(USE_AVX2) || defined(USE_SSE2) || defined(USE_NEON)
1722 c->u_mode.ocb.aad_nblocks = blkn;
1723
1724 if (burn_stack_depth)
1725 _gcry_burn_stack (burn_stack_depth + 4 * sizeof(void *));
1726 #endif
1727
1728 return nblocks;
1729 }
1730
1731
1732 static unsigned int
1733 serpent_crypt_blk1_32(void *context, byte *out, const byte *in,
1734 size_t num_blks, int encrypt)
1735 {
1736 serpent_context_t *ctx = context;
1737 unsigned int burn, burn_stack_depth = 0;
1738
1739 #ifdef USE_AVX512
1740 if (num_blks == 32 && ctx->use_avx512)
1741 {
1742 _gcry_serpent_avx512_blk32 (ctx, out, in, encrypt);
1743 return 0;
1744 }
1745 #endif
1746
1747 #ifdef USE_AVX2
1748 while (num_blks == 16 && ctx->use_avx2)
1749 {
1750 _gcry_serpent_avx2_blk16 (ctx, out, in, encrypt);
1751 out += 16 * sizeof(serpent_block_t);
1752 in += 16 * sizeof(serpent_block_t);
1753 num_blks -= 16;
1754 }
1755 #endif
1756
1757 #ifdef USE_SSE2
1758 while (num_blks >= 8)
1759 {
1760 _gcry_serpent_sse2_blk8 (ctx, out, in, encrypt);
1761 out += 8 * sizeof(serpent_block_t);
1762 in += 8 * sizeof(serpent_block_t);
1763 num_blks -= 8;
1764 }
1765 #endif
1766
1767 #ifdef USE_NEON
1768 if (ctx->use_neon)
1769 {
1770 while (num_blks >= 8)
1771 {
1772 _gcry_serpent_neon_blk8 (ctx, out, in, encrypt);
1773 out += 8 * sizeof(serpent_block_t);
1774 in += 8 * sizeof(serpent_block_t);
1775 num_blks -= 8;
1776 }
1777 }
1778 #endif
1779
1780 while (num_blks >= 1)
1781 {
1782 if (encrypt)
1783 serpent_encrypt_internal((void *)ctx, in, out);
1784 else
1785 serpent_decrypt_internal((void *)ctx, in, out);
1786
1787 burn = 2 * sizeof(serpent_block_t);
1788 burn_stack_depth = (burn > burn_stack_depth) ? burn : burn_stack_depth;
1789 out += sizeof(serpent_block_t);
1790 in += sizeof(serpent_block_t);
1791 num_blks--;
1792 }
1793
1794 return burn_stack_depth;
1795 }
1796
1797 static unsigned int
1798 serpent_encrypt_blk1_32(void *ctx, byte *out, const byte *in,
1799 size_t num_blks)
1800 {
1801 return serpent_crypt_blk1_32 (ctx, out, in, num_blks, 1);
1802 }
1803
1804 static unsigned int
1805 serpent_decrypt_blk1_32(void *ctx, byte *out, const byte *in,
1806 size_t num_blks)
1807 {
1808 return serpent_crypt_blk1_32 (ctx, out, in, num_blks, 0);
1809 }
1810
1811
1812 /* Bulk encryption/decryption of complete blocks in XTS mode. */
1813 static void
1814 _gcry_serpent_xts_crypt (void *context, unsigned char *tweak, void *outbuf_arg,
1815 const void *inbuf_arg, size_t nblocks, int encrypt)
1816 {
1817 serpent_context_t *ctx = context;
1818 unsigned char *outbuf = outbuf_arg;
1819 const unsigned char *inbuf = inbuf_arg;
1820 int burn_stack_depth = 0;
1821
1822 /* Process remaining blocks. */
1823 if (nblocks)
1824 {
1825 unsigned char tmpbuf[32 * 16];
1826 unsigned int tmp_used = 16;
1827 size_t nburn;
1828
1829 nburn = bulk_xts_crypt_128(ctx, encrypt ? serpent_encrypt_blk1_32
1830 : serpent_decrypt_blk1_32,
1831 outbuf, inbuf, nblocks,
1832 tweak, tmpbuf, sizeof(tmpbuf) / 16,
1833 &tmp_used);
1834 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1835
1836 wipememory(tmpbuf, tmp_used);
1837 }
1838
1839 if (burn_stack_depth)
1840 _gcry_burn_stack(burn_stack_depth);
1841 }
1842
1843
1844 /* Bulk encryption/decryption in ECB mode. */
1845 static void
1846 _gcry_serpent_ecb_crypt (void *context, void *outbuf_arg, const void *inbuf_arg,
1847 size_t nblocks, int encrypt)
1848 {
1849 serpent_context_t *ctx = context;
1850 unsigned char *outbuf = outbuf_arg;
1851 const unsigned char *inbuf = inbuf_arg;
1852 int burn_stack_depth = 0;
1853
1854 /* Process remaining blocks. */
1855 if (nblocks)
1856 {
1857 size_t nburn;
1858
1859 nburn = bulk_ecb_crypt_128(ctx, encrypt ? serpent_encrypt_blk1_32
1860 : serpent_decrypt_blk1_32,
1861 outbuf, inbuf, nblocks, 32);
1862 burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
1863 }
1864
1865 if (burn_stack_depth)
1866 _gcry_burn_stack(burn_stack_depth);
1867 }
1868
1869
1870
1871 /* Serpent test. */
1872
1873 static const char *
1874 serpent_test (void)
1875 {
1876 serpent_context_t context;
1877 unsigned char scratch[16];
1878 unsigned int i;
1879
1880 static struct test
1881 {
1882 int key_length;
1883 unsigned char key[32] _GCRY_GCC_ATTR_NONSTRING;
1884 unsigned char text_plain[16] _GCRY_GCC_ATTR_NONSTRING;
1885 unsigned char text_cipher[16] _GCRY_GCC_ATTR_NONSTRING;
1886 } test_data[] =
1887 {
1888 {
1889 16,
1890 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1891 "\xD2\x9D\x57\x6F\xCE\xA3\xA3\xA7\xED\x90\x99\xF2\x92\x73\xD7\x8E",
1892 "\xB2\x28\x8B\x96\x8A\xE8\xB0\x86\x48\xD1\xCE\x96\x06\xFD\x99\x2D"
1893 },
1894 {
1895 24,
1896 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1897 "\x00\x00\x00\x00\x00\x00\x00\x00",
1898 "\xD2\x9D\x57\x6F\xCE\xAB\xA3\xA7\xED\x98\x99\xF2\x92\x7B\xD7\x8E",
1899 "\x13\x0E\x35\x3E\x10\x37\xC2\x24\x05\xE8\xFA\xEF\xB2\xC3\xC3\xE9"
1900 },
1901 {
1902 32,
1903 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1904 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1905 "\xD0\x95\x57\x6F\xCE\xA3\xE3\xA7\xED\x98\xD9\xF2\x90\x73\xD7\x8E",
1906 "\xB9\x0E\xE5\x86\x2D\xE6\x91\x68\xF2\xBD\xD5\x12\x5B\x45\x47\x2B"
1907 },
1908 {
1909 32,
1910 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1911 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
1912 "\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00",
1913 "\x20\x61\xA4\x27\x82\xBD\x52\xEC\x69\x1E\xC3\x83\xB0\x3B\xA7\x7C"
1914 },
1915 {
1916 0
1917 },
1918 };
1919
1920 for (i = 0; test_data[i].key_length; i++)
1921 {
1922 serpent_setkey_internal (&context, test_data[i].key,
1923 test_data[i].key_length);
1924 serpent_encrypt_internal (&context, test_data[i].text_plain, scratch);
1925
1926 if (memcmp (scratch, test_data[i].text_cipher, sizeof (serpent_block_t)))
1927 switch (test_data[i].key_length)
1928 {
1929 case 16:
1930 return "Serpent-128 test encryption failed.";
1931 case 24:
1932 return "Serpent-192 test encryption failed.";
1933 case 32:
1934 return "Serpent-256 test encryption failed.";
1935 }
1936
1937 serpent_decrypt_internal (&context, test_data[i].text_cipher, scratch);
1938 if (memcmp (scratch, test_data[i].text_plain, sizeof (serpent_block_t)))
1939 switch (test_data[i].key_length)
1940 {
1941 case 16:
1942 return "Serpent-128 test decryption failed.";
1943 case 24:
1944 return "Serpent-192 test decryption failed.";
1945 case 32:
1946 return "Serpent-256 test decryption failed.";
1947 }
1948 }
1949
1950 return NULL;
1951 }
1952
1953
1954 static const gcry_cipher_oid_spec_t serpent128_oids[] =
1955 {
1956 {"1.3.6.1.4.1.11591.13.2.1", GCRY_CIPHER_MODE_ECB },
1957 {"1.3.6.1.4.1.11591.13.2.2", GCRY_CIPHER_MODE_CBC },
1958 {"1.3.6.1.4.1.11591.13.2.3", GCRY_CIPHER_MODE_OFB },
1959 {"1.3.6.1.4.1.11591.13.2.4", GCRY_CIPHER_MODE_CFB },
1960 { NULL }
1961 };
1962
1963 static const gcry_cipher_oid_spec_t serpent192_oids[] =
1964 {
1965 {"1.3.6.1.4.1.11591.13.2.21", GCRY_CIPHER_MODE_ECB },
1966 {"1.3.6.1.4.1.11591.13.2.22", GCRY_CIPHER_MODE_CBC },
1967 {"1.3.6.1.4.1.11591.13.2.23", GCRY_CIPHER_MODE_OFB },
1968 {"1.3.6.1.4.1.11591.13.2.24", GCRY_CIPHER_MODE_CFB },
1969 { NULL }
1970 };
1971
1972 static const gcry_cipher_oid_spec_t serpent256_oids[] =
1973 {
1974 {"1.3.6.1.4.1.11591.13.2.41", GCRY_CIPHER_MODE_ECB },
1975 {"1.3.6.1.4.1.11591.13.2.42", GCRY_CIPHER_MODE_CBC },
1976 {"1.3.6.1.4.1.11591.13.2.43", GCRY_CIPHER_MODE_OFB },
1977 {"1.3.6.1.4.1.11591.13.2.44", GCRY_CIPHER_MODE_CFB },
1978 { NULL }
1979 };
1980
1981 static const char *serpent128_aliases[] =
1982 {
1983 "SERPENT",
1984 "SERPENT-128",
1985 NULL
1986 };
1987 static const char *serpent192_aliases[] =
1988 {
1989 "SERPENT-192",
1990 NULL
1991 };
1992 static const char *serpent256_aliases[] =
1993 {
1994 "SERPENT-256",
1995 NULL
1996 };
1997
1998 gcry_cipher_spec_t _gcry_cipher_spec_serpent128 =
1999 {
2000 GCRY_CIPHER_SERPENT128, {0, 0},
2001 "SERPENT128", serpent128_aliases, serpent128_oids, 16, 128,
2002 sizeof (serpent_context_t),
2003 serpent_setkey, serpent_encrypt, serpent_decrypt
2004 };
2005
2006 gcry_cipher_spec_t _gcry_cipher_spec_serpent192 =
2007 {
2008 GCRY_CIPHER_SERPENT192, {0, 0},
2009 "SERPENT192", serpent192_aliases, serpent192_oids, 16, 192,
2010 sizeof (serpent_context_t),
2011 serpent_setkey, serpent_encrypt, serpent_decrypt
2012 };
2013
2014 gcry_cipher_spec_t _gcry_cipher_spec_serpent256 =
2015 {
2016 GCRY_CIPHER_SERPENT256, {0, 0},
2017 "SERPENT256", serpent256_aliases, serpent256_oids, 16, 256,
2018 sizeof (serpent_context_t),
2019 serpent_setkey, serpent_encrypt, serpent_decrypt
2020 };