Debian Codegraph @

cipher/chacha20.c

raw
1 /* chacha20.c - Bernstein's ChaCha20 cipher
2 * Copyright (C) 2014,2017-2019 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * For a description of the algorithm, see:
20 * http://cr.yp.to/chacha.html
21 */
22
23 /*
24 * Based on D. J. Bernstein reference implementation at
25 * http://cr.yp.to/chacha.html:
26 *
27 * chacha-regs.c version 20080118
28 * D. J. Bernstein
29 * Public domain.
30 */
31
32 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "types.h"
37 #include "g10lib.h"
38 #include "cipher.h"
39 #include "hwf-common.h"
40 #include "cipher-internal.h"
41 #include "bufhelp.h"
42
43
44 #define CHACHA20_MIN_KEY_SIZE 16 /* Bytes. */
45 #define CHACHA20_MAX_KEY_SIZE 32 /* Bytes. */
46 #define CHACHA20_BLOCK_SIZE 64 /* Bytes. */
47 #define CHACHA20_MIN_IV_SIZE 8 /* Bytes. */
48 #define CHACHA20_MAX_IV_SIZE 12 /* Bytes. */
49 #define CHACHA20_CTR_SIZE 16 /* Bytes. */
50
51
52 /* USE_SSSE3 indicates whether to compile with Intel SSSE3 code. */
53 #undef USE_SSSE3
54 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_SSSE3) && \
55 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
56 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
57 # define USE_SSSE3 1
58 #endif
59
60 /* USE_AVX2 indicates whether to compile with Intel AVX2 code. */
61 #undef USE_AVX2
62 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX2) && \
63 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
64 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
65 # define USE_AVX2 1
66 #endif
67
68 /* USE_AVX512 indicates whether to compile with Intel AVX512 code. */
69 #undef USE_AVX512
70 #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX512) && \
71 (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
72 defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
73 # define USE_AVX512 1
74 #endif
75
76 /* USE_ARMV7_NEON indicates whether to enable ARMv7 NEON assembly code. */
77 #undef USE_ARMV7_NEON
78 #ifdef ENABLE_NEON_SUPPORT
79 # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
80 && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
81 && defined(HAVE_GCC_INLINE_ASM_NEON)
82 # define USE_ARMV7_NEON 1
83 # endif
84 #endif
85
86 /* USE_AARCH64_SIMD indicates whether to enable ARMv8 SIMD assembly
87 * code. */
88 #undef USE_AARCH64_SIMD
89 #ifdef ENABLE_NEON_SUPPORT
90 # if defined(__AARCH64EL__) \
91 && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \
92 && defined(HAVE_GCC_INLINE_ASM_AARCH64_NEON)
93 # define USE_AARCH64_SIMD 1
94 # endif
95 #endif
96
97 /* USE_PPC_VEC indicates whether to enable PowerPC vector
98 * accelerated code. */
99 #undef USE_PPC_VEC
100 #ifdef ENABLE_PPC_CRYPTO_SUPPORT
101 # if defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
102 defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC)
103 # if __GNUC__ >= 4
104 # define USE_PPC_VEC 1
105 # endif
106 # endif
107 #endif
108
109 /* USE_S390X_VX indicates whether to enable zSeries code. */
110 #undef USE_S390X_VX
111 #if defined (__s390x__) && __GNUC__ >= 4 && __ARCH__ >= 9
112 # if defined(HAVE_GCC_INLINE_ASM_S390X_VX)
113 # define USE_S390X_VX 1
114 # endif /* USE_S390X_VX */
115 #endif
116
117 /* USE_RISCV_V indicates whether to enable RISC-V vector extension code. */
118 #undef USE_RISCV_V
119 #if defined (__riscv) && defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS)
120 # define USE_RISCV_V 1
121 #endif
122
123 /* Assembly implementations use SystemV ABI, ABI conversion and additional
124 * stack to store XMM6-XMM15 needed on Win64. */
125 #undef ASM_FUNC_ABI
126 #undef ASM_EXTRA_STACK
127 #if defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)
128 # define ASM_FUNC_ABI __attribute__((sysv_abi))
129 #else
130 # define ASM_FUNC_ABI
131 #endif
132
133
134 typedef struct CHACHA20_context_s
135 {
136 u32 input[16];
137 unsigned char pad[CHACHA20_BLOCK_SIZE];
138 unsigned int unused; /* bytes in the pad. */
139 #if defined(USE_SSSE3) || defined(USE_AVX512) || defined(USE_AVX2)
140 unsigned int skip_one_block_hw_impl:1;
141 #endif
142 unsigned int use_ssse3:1;
143 unsigned int use_avx2:1;
144 unsigned int use_avx512:1;
145 unsigned int use_neon:1;
146 unsigned int use_ppc:1;
147 unsigned int use_p9:1;
148 unsigned int use_p10:1;
149 unsigned int use_s390x:1;
150 unsigned int use_riscv_v:1;
151 } CHACHA20_context_t;
152
153
154 #ifdef USE_SSSE3
155
156 unsigned int _gcry_chacha20_amd64_ssse3_blocks4(u32 *state, byte *dst,
157 const byte *src,
158 size_t nblks) ASM_FUNC_ABI;
159
160 unsigned int _gcry_chacha20_amd64_ssse3_blocks1(u32 *state, byte *dst,
161 const byte *src,
162 size_t nblks) ASM_FUNC_ABI;
163
164 unsigned int _gcry_chacha20_poly1305_amd64_ssse3_blocks4(
165 u32 *state, byte *dst, const byte *src, size_t nblks,
166 void *poly1305_state, const byte *poly1305_src) ASM_FUNC_ABI;
167
168 unsigned int _gcry_chacha20_poly1305_amd64_ssse3_blocks1(
169 u32 *state, byte *dst, const byte *src, size_t nblks,
170 void *poly1305_state, const byte *poly1305_src) ASM_FUNC_ABI;
171
172 #endif /* USE_SSSE3 */
173
174 #ifdef USE_AVX2
175
176 unsigned int _gcry_chacha20_amd64_avx2_blocks8(u32 *state, byte *dst,
177 const byte *src,
178 size_t nblks) ASM_FUNC_ABI;
179
180 unsigned int _gcry_chacha20_poly1305_amd64_avx2_blocks8(
181 u32 *state, byte *dst, const byte *src, size_t nblks,
182 void *poly1305_state, const byte *poly1305_src) ASM_FUNC_ABI;
183
184 #endif /* USE_AVX2 */
185
186 #ifdef USE_AVX512
187
188 unsigned int _gcry_chacha20_amd64_avx512_blocks(u32 *state, byte *dst,
189 const byte *src,
190 size_t nblks) ASM_FUNC_ABI;
191
192 #endif /* USE_AVX2 */
193
194 #ifdef USE_PPC_VEC
195
196 #ifndef WORDS_BIGENDIAN
197 unsigned int _gcry_chacha20_p10le_8x(u32 *state, byte *dst,
198 const byte *src,
199 size_t len);
200 #endif
201
202 unsigned int _gcry_chacha20_ppc8_blocks4(u32 *state, byte *dst,
203 const byte *src,
204 size_t nblks);
205
206 unsigned int _gcry_chacha20_ppc8_blocks1(u32 *state, byte *dst,
207 const byte *src,
208 size_t nblks);
209
210 unsigned int _gcry_chacha20_ppc9_blocks4(u32 *state, byte *dst,
211 const byte *src,
212 size_t nblks);
213
214 unsigned int _gcry_chacha20_ppc9_blocks1(u32 *state, byte *dst,
215 const byte *src,
216 size_t nblks);
217
218 #undef USE_PPC_VEC_POLY1305
219 #if SIZEOF_UNSIGNED_LONG == 8
220 #define USE_PPC_VEC_POLY1305 1
221 unsigned int _gcry_chacha20_poly1305_ppc8_blocks4(
222 u32 *state, byte *dst, const byte *src, size_t nblks,
223 POLY1305_STATE *st, const byte *poly1305_src);
224
225 unsigned int _gcry_chacha20_poly1305_ppc9_blocks4(
226 u32 *state, byte *dst, const byte *src, size_t nblks,
227 POLY1305_STATE *st, const byte *poly1305_src);
228 #endif /* SIZEOF_UNSIGNED_LONG == 8 */
229
230 #endif /* USE_PPC_VEC */
231
232 #ifdef USE_S390X_VX
233
234 unsigned int _gcry_chacha20_s390x_vx_blocks8(u32 *state, byte *dst,
235 const byte *src, size_t nblks);
236
237 unsigned int _gcry_chacha20_s390x_vx_blocks4_2_1(u32 *state, byte *dst,
238 const byte *src, size_t nblks);
239
240 #undef USE_S390X_VX_POLY1305
241 #if SIZEOF_UNSIGNED_LONG == 8
242 #define USE_S390X_VX_POLY1305 1
243 unsigned int _gcry_chacha20_poly1305_s390x_vx_blocks8(
244 u32 *state, byte *dst, const byte *src, size_t nblks,
245 POLY1305_STATE *st, const byte *poly1305_src);
246
247 unsigned int _gcry_chacha20_poly1305_s390x_vx_blocks4_2_1(
248 u32 *state, byte *dst, const byte *src, size_t nblks,
249 POLY1305_STATE *st, const byte *poly1305_src);
250 #endif /* SIZEOF_UNSIGNED_LONG == 8 */
251
252 #endif /* USE_S390X_VX */
253
254 #ifdef USE_ARMV7_NEON
255
256 unsigned int _gcry_chacha20_armv7_neon_blocks4(u32 *state, byte *dst,
257 const byte *src,
258 size_t nblks);
259
260 #endif /* USE_ARMV7_NEON */
261
262 #ifdef USE_AARCH64_SIMD
263
264 unsigned int _gcry_chacha20_aarch64_blocks4(u32 *state, byte *dst,
265 const byte *src, size_t nblks);
266
267 unsigned int _gcry_chacha20_poly1305_aarch64_blocks4(
268 u32 *state, byte *dst, const byte *src, size_t nblks,
269 void *poly1305_state, const byte *poly1305_src);
270
271 #endif /* USE_AARCH64_SIMD */
272
273 #ifdef USE_RISCV_V
274
275 unsigned int _gcry_chacha20_riscv_v_blocks(u32 *state, byte *dst,
276 const byte *src,
277 size_t nblks);
278
279 unsigned int _gcry_chacha20_riscv_v_check_hw(void);
280
281 #endif /* USE_RISCV_V */
282
283
284 static const char *selftest (void);
285
286
287 #define ROTATE(v,c) (rol(v,c))
288 #define XOR(v,w) ((v) ^ (w))
289 #define PLUS(v,w) ((u32)((v) + (w)))
290 #define PLUSONE(v) (PLUS((v),1))
291
292 #define QUARTERROUND(a,b,c,d) \
293 a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
294 c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
295 a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
296 c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
297
298 #define BUF_XOR_LE32(dst, src, offset, x) \
299 buf_put_le32((dst) + (offset), buf_get_le32((src) + (offset)) ^ (x))
300
301 static unsigned int
302 do_chacha20_blocks (u32 *input, byte *dst, const byte *src, size_t nblks)
303 {
304 u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
305 unsigned int i;
306
307 while (nblks)
308 {
309 x0 = input[0];
310 x1 = input[1];
311 x2 = input[2];
312 x3 = input[3];
313 x4 = input[4];
314 x5 = input[5];
315 x6 = input[6];
316 x7 = input[7];
317 x8 = input[8];
318 x9 = input[9];
319 x10 = input[10];
320 x11 = input[11];
321 x12 = input[12];
322 x13 = input[13];
323 x14 = input[14];
324 x15 = input[15];
325
326 for (i = 20; i > 0; i -= 2)
327 {
328 QUARTERROUND(x0, x4, x8, x12)
329 QUARTERROUND(x1, x5, x9, x13)
330 QUARTERROUND(x2, x6, x10, x14)
331 QUARTERROUND(x3, x7, x11, x15)
332 QUARTERROUND(x0, x5, x10, x15)
333 QUARTERROUND(x1, x6, x11, x12)
334 QUARTERROUND(x2, x7, x8, x13)
335 QUARTERROUND(x3, x4, x9, x14)
336 }
337
338 x0 = PLUS(x0, input[0]);
339 x1 = PLUS(x1, input[1]);
340 x2 = PLUS(x2, input[2]);
341 x3 = PLUS(x3, input[3]);
342 x4 = PLUS(x4, input[4]);
343 x5 = PLUS(x5, input[5]);
344 x6 = PLUS(x6, input[6]);
345 x7 = PLUS(x7, input[7]);
346 x8 = PLUS(x8, input[8]);
347 x9 = PLUS(x9, input[9]);
348 x10 = PLUS(x10, input[10]);
349 x11 = PLUS(x11, input[11]);
350 x12 = PLUS(x12, input[12]);
351 x13 = PLUS(x13, input[13]);
352 x14 = PLUS(x14, input[14]);
353 x15 = PLUS(x15, input[15]);
354
355 input[12] = PLUSONE(input[12]);
356 input[13] = PLUS(input[13], !input[12]);
357
358 BUF_XOR_LE32(dst, src, 0, x0);
359 BUF_XOR_LE32(dst, src, 4, x1);
360 BUF_XOR_LE32(dst, src, 8, x2);
361 BUF_XOR_LE32(dst, src, 12, x3);
362 BUF_XOR_LE32(dst, src, 16, x4);
363 BUF_XOR_LE32(dst, src, 20, x5);
364 BUF_XOR_LE32(dst, src, 24, x6);
365 BUF_XOR_LE32(dst, src, 28, x7);
366 BUF_XOR_LE32(dst, src, 32, x8);
367 BUF_XOR_LE32(dst, src, 36, x9);
368 BUF_XOR_LE32(dst, src, 40, x10);
369 BUF_XOR_LE32(dst, src, 44, x11);
370 BUF_XOR_LE32(dst, src, 48, x12);
371 BUF_XOR_LE32(dst, src, 52, x13);
372 BUF_XOR_LE32(dst, src, 56, x14);
373 BUF_XOR_LE32(dst, src, 60, x15);
374
375 src += CHACHA20_BLOCK_SIZE;
376 dst += CHACHA20_BLOCK_SIZE;
377 nblks--;
378 }
379
380 /* burn_stack */
381 return (17 * sizeof(u32) + 6 * sizeof(void *));
382 }
383
384
385 static unsigned int
386 chacha20_blocks (CHACHA20_context_t *ctx, byte *dst, const byte *src,
387 size_t nblks)
388 {
389 unsigned int nburn, burn = 0;
390 #if defined(USE_SSSE3) || defined(USE_AVX512)
391 size_t gen_nblks = 0;
392
393 if (ctx->skip_one_block_hw_impl)
394 {
395 gen_nblks = nblks % 2;
396 nblks = nblks - gen_nblks;
397 }
398 #endif
399
400 #ifdef USE_AVX512
401 if (nblks && ctx->use_avx512)
402 {
403 if (gen_nblks == 0)
404 return _gcry_chacha20_amd64_avx512_blocks(ctx->input, dst, src, nblks);
405
406 burn = _gcry_chacha20_amd64_avx512_blocks(ctx->input, dst, src, nblks);
407 dst += CHACHA20_BLOCK_SIZE * nblks;
408 src += CHACHA20_BLOCK_SIZE * nblks;
409 nblks = 0;
410 }
411 #endif
412
413 #ifdef USE_SSSE3
414 if (nblks && ctx->use_ssse3)
415 {
416 if (gen_nblks == 0)
417 return _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, dst, src, nblks);
418
419 burn = _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, dst, src, nblks);
420 dst += CHACHA20_BLOCK_SIZE * nblks;
421 src += CHACHA20_BLOCK_SIZE * nblks;
422 nblks = 0;
423 }
424 #endif
425
426 #if defined(USE_SSSE3) || defined(USE_AVX512)
427 nblks += gen_nblks;
428 if (nblks == 0)
429 return burn;
430 #endif
431
432 #ifdef USE_PPC_VEC
433 if (ctx->use_ppc)
434 {
435 if (ctx->use_p9)
436 return _gcry_chacha20_ppc9_blocks1(ctx->input, dst, src, nblks);
437 else
438 return _gcry_chacha20_ppc8_blocks1(ctx->input, dst, src, nblks);
439 }
440 #endif
441
442 #ifdef USE_S390X_VX
443 if (ctx->use_s390x)
444 {
445 return _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, dst, src, nblks);
446 }
447 #endif
448
449 #ifdef USE_RISCV_V
450 if (ctx->use_riscv_v)
451 {
452 return _gcry_chacha20_riscv_v_blocks(ctx->input, dst, src, nblks);
453 }
454 #endif
455
456 nburn = do_chacha20_blocks (ctx->input, dst, src, nblks);
457 return nburn > burn ? nburn : burn;
458 }
459
460
461 static void
462 chacha20_keysetup (CHACHA20_context_t *ctx, const byte *key,
463 unsigned int keylen)
464 {
465 static const char sigma[16] _GCRY_GCC_ATTR_NONSTRING = "expand 32-byte k";
466 static const char tau[16] _GCRY_GCC_ATTR_NONSTRING = "expand 16-byte k";
467 const char *constants;
468
469 ctx->input[4] = buf_get_le32(key + 0);
470 ctx->input[5] = buf_get_le32(key + 4);
471 ctx->input[6] = buf_get_le32(key + 8);
472 ctx->input[7] = buf_get_le32(key + 12);
473 if (keylen == CHACHA20_MAX_KEY_SIZE) /* 256 bits */
474 {
475 key += 16;
476 constants = sigma;
477 }
478 else /* 128 bits */
479 {
480 constants = tau;
481 }
482 ctx->input[8] = buf_get_le32(key + 0);
483 ctx->input[9] = buf_get_le32(key + 4);
484 ctx->input[10] = buf_get_le32(key + 8);
485 ctx->input[11] = buf_get_le32(key + 12);
486 ctx->input[0] = buf_get_le32(constants + 0);
487 ctx->input[1] = buf_get_le32(constants + 4);
488 ctx->input[2] = buf_get_le32(constants + 8);
489 ctx->input[3] = buf_get_le32(constants + 12);
490 }
491
492
493 static void
494 chacha20_ivsetup (CHACHA20_context_t * ctx, const byte *iv, size_t ivlen)
495 {
496 if (ivlen == CHACHA20_CTR_SIZE)
497 {
498 ctx->input[12] = buf_get_le32 (iv + 0);
499 ctx->input[13] = buf_get_le32 (iv + 4);
500 ctx->input[14] = buf_get_le32 (iv + 8);
501 ctx->input[15] = buf_get_le32 (iv + 12);
502 }
503 else if (ivlen == CHACHA20_MAX_IV_SIZE)
504 {
505 ctx->input[12] = 0;
506 ctx->input[13] = buf_get_le32 (iv + 0);
507 ctx->input[14] = buf_get_le32 (iv + 4);
508 ctx->input[15] = buf_get_le32 (iv + 8);
509 }
510 else if (ivlen == CHACHA20_MIN_IV_SIZE)
511 {
512 ctx->input[12] = 0;
513 ctx->input[13] = 0;
514 ctx->input[14] = buf_get_le32 (iv + 0);
515 ctx->input[15] = buf_get_le32 (iv + 4);
516 }
517 else
518 {
519 ctx->input[12] = 0;
520 ctx->input[13] = 0;
521 ctx->input[14] = 0;
522 ctx->input[15] = 0;
523 }
524 }
525
526
527 static void
528 chacha20_setiv (void *context, const byte *iv, size_t ivlen)
529 {
530 CHACHA20_context_t *ctx = (CHACHA20_context_t *) context;
531
532 /* draft-nir-cfrg-chacha20-poly1305-02 defines 96-bit and 64-bit nonce. */
533 if (iv && ivlen != CHACHA20_MAX_IV_SIZE && ivlen != CHACHA20_MIN_IV_SIZE
534 && ivlen != CHACHA20_CTR_SIZE)
535 log_info ("WARNING: chacha20_setiv: bad ivlen=%u\n", (u32) ivlen);
536
537 if (iv && (ivlen == CHACHA20_MAX_IV_SIZE || ivlen == CHACHA20_MIN_IV_SIZE
538 || ivlen == CHACHA20_CTR_SIZE))
539 chacha20_ivsetup (ctx, iv, ivlen);
540 else
541 chacha20_ivsetup (ctx, NULL, 0);
542
543 /* Reset the unused pad bytes counter. */
544 ctx->unused = 0;
545 }
546
547
548 static gcry_err_code_t
549 chacha20_do_setkey (CHACHA20_context_t *ctx,
550 const byte *key, unsigned int keylen)
551 {
552 static int initialized;
553 static const char *selftest_failed;
554 unsigned int features = _gcry_get_hw_features ();
555
556 if (!initialized)
557 {
558 initialized = 1;
559 selftest_failed = selftest ();
560 if (selftest_failed)
561 log_error ("CHACHA20 selftest failed (%s)\n", selftest_failed);
562 }
563 if (selftest_failed)
564 return GPG_ERR_SELFTEST_FAILED;
565
566 if (keylen != CHACHA20_MAX_KEY_SIZE && keylen != CHACHA20_MIN_KEY_SIZE)
567 return GPG_ERR_INV_KEYLEN;
568
569 #ifdef USE_SSSE3
570 ctx->use_ssse3 = (features & HWF_INTEL_SSSE3) != 0;
571 #endif
572 #ifdef USE_AVX512
573 ctx->use_avx512 = (features & HWF_INTEL_AVX512) != 0;
574 #endif
575 #ifdef USE_AVX2
576 ctx->use_avx2 = (features & HWF_INTEL_AVX2) != 0;
577 #endif
578 #if defined(USE_SSSE3) || defined(USE_AVX512) || defined(USE_AVX2)
579 /* If CPU prefers GPR over scalar integer vector implementation, use
580 * generic C chacha20 for single block non-parallel operations. */
581 ctx->skip_one_block_hw_impl =
582 !!_gcry_hwf_x86_cpu_details()->prefer_gpr_over_scalar_int_vector;
583 #endif
584 #ifdef USE_ARMV7_NEON
585 ctx->use_neon = (features & HWF_ARM_NEON) != 0;
586 #endif
587 #ifdef USE_AARCH64_SIMD
588 ctx->use_neon = (features & HWF_ARM_NEON) != 0;
589 #endif
590 #ifdef USE_PPC_VEC
591 ctx->use_ppc = (features & HWF_PPC_ARCH_2_07) != 0;
592 ctx->use_p9 = (features & HWF_PPC_ARCH_3_00) != 0;
593 # ifndef WORDS_BIGENDIAN
594 ctx->use_p10 = (features & HWF_PPC_ARCH_3_10) != 0;
595 # ifdef ENABLE_FORCE_SOFT_HWFEATURES
596 /* HWF_PPC_ARCH_3_10 above is used as soft HW-feature indicator for P10.
597 * Actual implementation works with HWF_PPC_ARCH_3_00 also. */
598 ctx->use_p10 |= (features & HWF_PPC_ARCH_3_00) != 0;
599 # endif
600 # endif
601 #endif
602 #ifdef USE_S390X_VX
603 ctx->use_s390x = (features & HWF_S390X_VX) != 0;
604 #endif
605 #ifdef USE_RISCV_V
606 ctx->use_riscv_v = (features & HWF_RISCV_IMAFDC)
607 && (features & HWF_RISCV_B) /* Mandatory in RVA22U64 */
608 && (features & HWF_RISCV_V) /* Optional in RVA22U64 */
609 && _gcry_chacha20_riscv_v_check_hw();
610 #endif
611
612 (void)features;
613
614 chacha20_keysetup (ctx, key, keylen);
615
616 /* We default to a zero nonce. */
617 chacha20_setiv (ctx, NULL, 0);
618
619 return 0;
620 }
621
622
623 static gcry_err_code_t
624 chacha20_setkey (void *context, const byte *key, unsigned int keylen,
625 cipher_bulk_ops_t *bulk_ops)
626 {
627 CHACHA20_context_t *ctx = (CHACHA20_context_t *) context;
628 gcry_err_code_t rc = chacha20_do_setkey (ctx, key, keylen);
629 (void)bulk_ops;
630 _gcry_burn_stack (4 + sizeof (void *) + 4 * sizeof (void *));
631 return rc;
632 }
633
634
635 static unsigned int
636 do_chacha20_encrypt_stream_tail (CHACHA20_context_t *ctx, byte *outbuf,
637 const byte *inbuf, size_t length)
638 {
639 static const unsigned char zero_pad[CHACHA20_BLOCK_SIZE] = { 0, };
640 unsigned int nburn, burn = 0;
641
642 #ifdef USE_AVX512
643 if (ctx->use_avx512 && length >= CHACHA20_BLOCK_SIZE)
644 {
645 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
646
647 if (ctx->skip_one_block_hw_impl)
648 nblocks -= nblocks % 2;
649
650 if (nblocks)
651 {
652 nburn = _gcry_chacha20_amd64_avx512_blocks(ctx->input, outbuf, inbuf,
653 nblocks);
654 burn = nburn > burn ? nburn : burn;
655 length -= nblocks * CHACHA20_BLOCK_SIZE;
656 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
657 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
658 }
659 }
660 #endif
661
662 #ifdef USE_AVX2
663 if (ctx->use_avx2 && length >= CHACHA20_BLOCK_SIZE * 8)
664 {
665 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
666 nblocks -= nblocks % 8;
667 nburn = _gcry_chacha20_amd64_avx2_blocks8(ctx->input, outbuf, inbuf,
668 nblocks);
669 burn = nburn > burn ? nburn : burn;
670 length -= nblocks * CHACHA20_BLOCK_SIZE;
671 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
672 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
673 }
674 #endif
675
676 #ifdef USE_SSSE3
677 if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE * 4)
678 {
679 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
680 nblocks -= nblocks % 4;
681 nburn = _gcry_chacha20_amd64_ssse3_blocks4(ctx->input, outbuf, inbuf,
682 nblocks);
683 burn = nburn > burn ? nburn : burn;
684 length -= nblocks * CHACHA20_BLOCK_SIZE;
685 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
686 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
687 }
688 #endif
689
690 #ifdef USE_ARMV7_NEON
691 if (ctx->use_neon && length >= CHACHA20_BLOCK_SIZE * 4)
692 {
693 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
694 nblocks -= nblocks % 4;
695 nburn = _gcry_chacha20_armv7_neon_blocks4(ctx->input, outbuf, inbuf,
696 nblocks);
697 burn = nburn > burn ? nburn : burn;
698 length -= nblocks * CHACHA20_BLOCK_SIZE;
699 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
700 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
701 }
702 #endif
703
704 #ifdef USE_AARCH64_SIMD
705 if (ctx->use_neon && length >= CHACHA20_BLOCK_SIZE * 4)
706 {
707 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
708 nblocks -= nblocks % 4;
709 nburn = _gcry_chacha20_aarch64_blocks4(ctx->input, outbuf, inbuf,
710 nblocks);
711 burn = nburn > burn ? nburn : burn;
712 length -= nblocks * CHACHA20_BLOCK_SIZE;
713 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
714 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
715 }
716 #endif
717
718 #ifdef USE_PPC_VEC
719 if (ctx->use_ppc && length >= CHACHA20_BLOCK_SIZE * 4)
720 {
721 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
722 nblocks -= nblocks % 4;
723 if (0)
724 {}
725 #ifndef WORDS_BIGENDIAN
726 /*
727 * A workaround to skip counter overflow. This is rare.
728 */
729 else if (ctx->use_p10 && nblocks >= 8
730 && ((u64)ctx->input[12] + nblocks) <= 0xffffffffU)
731 {
732 size_t len = nblocks * CHACHA20_BLOCK_SIZE;
733 nburn = _gcry_chacha20_p10le_8x(ctx->input, outbuf, inbuf, len);
734 }
735 #endif
736 else if (ctx->use_p9)
737 {
738 nburn = _gcry_chacha20_ppc9_blocks4(ctx->input, outbuf, inbuf,
739 nblocks);
740 }
741 else
742 {
743 nburn = _gcry_chacha20_ppc8_blocks4(ctx->input, outbuf, inbuf,
744 nblocks);
745 }
746 burn = nburn > burn ? nburn : burn;
747 length -= nblocks * CHACHA20_BLOCK_SIZE;
748 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
749 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
750 }
751 #endif
752
753 #ifdef USE_S390X_VX
754 if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE * 8)
755 {
756 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
757 nblocks -= nblocks % 8;
758 nburn = _gcry_chacha20_s390x_vx_blocks8(ctx->input, outbuf, inbuf,
759 nblocks);
760 burn = nburn > burn ? nburn : burn;
761 length -= nblocks * CHACHA20_BLOCK_SIZE;
762 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
763 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
764 }
765 #endif
766
767 if (length >= CHACHA20_BLOCK_SIZE)
768 {
769 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
770 nburn = chacha20_blocks(ctx, outbuf, inbuf, nblocks);
771 burn = nburn > burn ? nburn : burn;
772 length %= CHACHA20_BLOCK_SIZE;
773 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
774 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
775 }
776
777 if (length > 0)
778 {
779 nburn = chacha20_blocks(ctx, ctx->pad, zero_pad, 1);
780 burn = nburn > burn ? nburn : burn;
781
782 buf_xor (outbuf, inbuf, ctx->pad, length);
783 ctx->unused = CHACHA20_BLOCK_SIZE - length;
784 }
785
786 if (burn)
787 burn += 5 * sizeof(void *);
788
789 return burn;
790 }
791
792
793 static void
794 chacha20_encrypt_stream (void *context, byte *outbuf, const byte *inbuf,
795 size_t length)
796 {
797 CHACHA20_context_t *ctx = (CHACHA20_context_t *) context;
798 unsigned int nburn, burn = 0;
799
800 if (!length)
801 return;
802
803 if (ctx->unused)
804 {
805 unsigned char *p = ctx->pad;
806 size_t n;
807
808 gcry_assert (ctx->unused < CHACHA20_BLOCK_SIZE);
809
810 n = ctx->unused;
811 if (n > length)
812 n = length;
813
814 buf_xor (outbuf, inbuf, p + CHACHA20_BLOCK_SIZE - ctx->unused, n);
815 length -= n;
816 outbuf += n;
817 inbuf += n;
818 ctx->unused -= n;
819
820 if (!length)
821 return;
822 gcry_assert (!ctx->unused);
823 }
824
825 nburn = do_chacha20_encrypt_stream_tail (ctx, outbuf, inbuf, length);
826 burn = nburn > burn ? nburn : burn;
827
828 if (burn)
829 _gcry_burn_stack (burn);
830 }
831
832
833 gcry_err_code_t
834 _gcry_chacha20_poly1305_encrypt(gcry_cipher_hd_t c, byte *outbuf,
835 const byte *inbuf, size_t length)
836 {
837 CHACHA20_context_t *ctx = (void *) &c->context.c;
838 unsigned int nburn, burn = 0;
839 byte *authptr = NULL;
840
841 if (!length)
842 return 0;
843
844 if (ctx->unused)
845 {
846 unsigned char *p = ctx->pad;
847 size_t n;
848
849 gcry_assert (ctx->unused < CHACHA20_BLOCK_SIZE);
850
851 n = ctx->unused;
852 if (n > length)
853 n = length;
854
855 buf_xor (outbuf, inbuf, p + CHACHA20_BLOCK_SIZE - ctx->unused, n);
856 nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, outbuf, n);
857 burn = nburn > burn ? nburn : burn;
858 length -= n;
859 outbuf += n;
860 inbuf += n;
861 ctx->unused -= n;
862
863 if (!length)
864 {
865 if (burn)
866 _gcry_burn_stack (burn);
867
868 return 0;
869 }
870 gcry_assert (!ctx->unused);
871 }
872
873 gcry_assert (c->u_mode.poly1305.ctx.leftover == 0);
874
875 if (0)
876 { }
877 #ifdef USE_AVX512
878 else if (ctx->use_avx512)
879 {
880 /* Skip stitched chacha20-poly1305 for AVX512. */
881 authptr = NULL;
882 }
883 #endif
884 #ifdef USE_AVX2
885 else if (ctx->use_avx2 && length >= CHACHA20_BLOCK_SIZE * 8)
886 {
887 nburn = _gcry_chacha20_amd64_avx2_blocks8(ctx->input, outbuf, inbuf, 8);
888 burn = nburn > burn ? nburn : burn;
889
890 authptr = outbuf;
891 length -= 8 * CHACHA20_BLOCK_SIZE;
892 outbuf += 8 * CHACHA20_BLOCK_SIZE;
893 inbuf += 8 * CHACHA20_BLOCK_SIZE;
894 }
895 #endif
896 #ifdef USE_SSSE3
897 else if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE * 4)
898 {
899 nburn = _gcry_chacha20_amd64_ssse3_blocks4(ctx->input, outbuf, inbuf, 4);
900 burn = nburn > burn ? nburn : burn;
901
902 authptr = outbuf;
903 length -= 4 * CHACHA20_BLOCK_SIZE;
904 outbuf += 4 * CHACHA20_BLOCK_SIZE;
905 inbuf += 4 * CHACHA20_BLOCK_SIZE;
906 }
907 else if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE * 2)
908 {
909 nburn = _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, outbuf, inbuf, 2);
910 burn = nburn > burn ? nburn : burn;
911
912 authptr = outbuf;
913 length -= 2 * CHACHA20_BLOCK_SIZE;
914 outbuf += 2 * CHACHA20_BLOCK_SIZE;
915 inbuf += 2 * CHACHA20_BLOCK_SIZE;
916 }
917 else if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE)
918 {
919 nburn = _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, outbuf, inbuf, 1);
920 burn = nburn > burn ? nburn : burn;
921
922 authptr = outbuf;
923 length -= 1 * CHACHA20_BLOCK_SIZE;
924 outbuf += 1 * CHACHA20_BLOCK_SIZE;
925 inbuf += 1 * CHACHA20_BLOCK_SIZE;
926 }
927 #endif
928 #ifdef USE_AARCH64_SIMD
929 else if (ctx->use_neon && length >= CHACHA20_BLOCK_SIZE * 4)
930 {
931 nburn = _gcry_chacha20_aarch64_blocks4(ctx->input, outbuf, inbuf, 4);
932 burn = nburn > burn ? nburn : burn;
933
934 authptr = outbuf;
935 length -= 4 * CHACHA20_BLOCK_SIZE;
936 outbuf += 4 * CHACHA20_BLOCK_SIZE;
937 inbuf += 4 * CHACHA20_BLOCK_SIZE;
938 }
939 #endif
940 #ifdef USE_PPC_VEC_POLY1305
941 else if (ctx->use_ppc && ctx->use_p10)
942 {
943 /* Skip stitched chacha20-poly1305 for P10. */
944 authptr = NULL;
945 }
946 else if (ctx->use_ppc && length >= CHACHA20_BLOCK_SIZE * 4)
947 {
948 if (ctx->use_p9)
949 nburn = _gcry_chacha20_ppc9_blocks4(ctx->input, outbuf, inbuf, 4);
950 else
951 nburn = _gcry_chacha20_ppc8_blocks4(ctx->input, outbuf, inbuf, 4);
952 burn = nburn > burn ? nburn : burn;
953
954 authptr = outbuf;
955 length -= 4 * CHACHA20_BLOCK_SIZE;
956 outbuf += 4 * CHACHA20_BLOCK_SIZE;
957 inbuf += 4 * CHACHA20_BLOCK_SIZE;
958 }
959 #endif
960 #ifdef USE_S390X_VX_POLY1305
961 else if (ctx->use_s390x && length >= 2 * CHACHA20_BLOCK_SIZE * 8)
962 {
963 nburn = _gcry_chacha20_s390x_vx_blocks8(ctx->input, outbuf, inbuf, 8);
964 burn = nburn > burn ? nburn : burn;
965
966 authptr = outbuf;
967 length -= 8 * CHACHA20_BLOCK_SIZE;
968 outbuf += 8 * CHACHA20_BLOCK_SIZE;
969 inbuf += 8 * CHACHA20_BLOCK_SIZE;
970 }
971 else if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE * 4)
972 {
973 nburn = _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, outbuf, inbuf, 4);
974 burn = nburn > burn ? nburn : burn;
975
976 authptr = outbuf;
977 length -= 4 * CHACHA20_BLOCK_SIZE;
978 outbuf += 4 * CHACHA20_BLOCK_SIZE;
979 inbuf += 4 * CHACHA20_BLOCK_SIZE;
980 }
981 else if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE * 2)
982 {
983 nburn = _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, outbuf, inbuf, 2);
984 burn = nburn > burn ? nburn : burn;
985
986 authptr = outbuf;
987 length -= 2 * CHACHA20_BLOCK_SIZE;
988 outbuf += 2 * CHACHA20_BLOCK_SIZE;
989 inbuf += 2 * CHACHA20_BLOCK_SIZE;
990 }
991 else if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE)
992 {
993 nburn = _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, outbuf, inbuf, 1);
994 burn = nburn > burn ? nburn : burn;
995
996 authptr = outbuf;
997 length -= 1 * CHACHA20_BLOCK_SIZE;
998 outbuf += 1 * CHACHA20_BLOCK_SIZE;
999 inbuf += 1 * CHACHA20_BLOCK_SIZE;
1000 }
1001 #endif
1002
1003 if (authptr)
1004 {
1005 size_t authoffset = outbuf - authptr;
1006
1007 #ifdef USE_AVX2
1008 if (ctx->use_avx2 &&
1009 length >= 8 * CHACHA20_BLOCK_SIZE &&
1010 authoffset >= 8 * CHACHA20_BLOCK_SIZE)
1011 {
1012 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1013 nblocks -= nblocks % 8;
1014
1015 nburn = _gcry_chacha20_poly1305_amd64_avx2_blocks8(
1016 ctx->input, outbuf, inbuf, nblocks,
1017 &c->u_mode.poly1305.ctx.state, authptr);
1018 burn = nburn > burn ? nburn : burn;
1019
1020 length -= nblocks * CHACHA20_BLOCK_SIZE;
1021 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1022 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1023 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1024 }
1025 #endif
1026
1027 #ifdef USE_SSSE3
1028 if (ctx->use_ssse3)
1029 {
1030 if (length >= 4 * CHACHA20_BLOCK_SIZE &&
1031 authoffset >= 4 * CHACHA20_BLOCK_SIZE)
1032 {
1033 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1034 nblocks -= nblocks % 4;
1035
1036 nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks4(
1037 ctx->input, outbuf, inbuf, nblocks,
1038 &c->u_mode.poly1305.ctx.state, authptr);
1039 burn = nburn > burn ? nburn : burn;
1040
1041 length -= nblocks * CHACHA20_BLOCK_SIZE;
1042 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1043 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1044 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1045 }
1046
1047 if (length >= CHACHA20_BLOCK_SIZE &&
1048 authoffset >= CHACHA20_BLOCK_SIZE)
1049 {
1050 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1051
1052 nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks1(
1053 ctx->input, outbuf, inbuf, nblocks,
1054 &c->u_mode.poly1305.ctx.state, authptr);
1055 burn = nburn > burn ? nburn : burn;
1056
1057 length -= nblocks * CHACHA20_BLOCK_SIZE;
1058 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1059 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1060 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1061 }
1062 }
1063 #endif
1064
1065 #ifdef USE_AARCH64_SIMD
1066 if (ctx->use_neon &&
1067 length >= 4 * CHACHA20_BLOCK_SIZE &&
1068 authoffset >= 4 * CHACHA20_BLOCK_SIZE)
1069 {
1070 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1071 nblocks -= nblocks % 4;
1072
1073 nburn = _gcry_chacha20_poly1305_aarch64_blocks4(
1074 ctx->input, outbuf, inbuf, nblocks,
1075 &c->u_mode.poly1305.ctx.state, authptr);
1076 burn = nburn > burn ? nburn : burn;
1077
1078 length -= nblocks * CHACHA20_BLOCK_SIZE;
1079 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1080 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1081 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1082 }
1083 #endif
1084
1085 #ifdef USE_PPC_VEC_POLY1305
1086 if (ctx->use_ppc &&
1087 length >= 4 * CHACHA20_BLOCK_SIZE &&
1088 authoffset >= 4 * CHACHA20_BLOCK_SIZE)
1089 {
1090 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1091 nblocks -= nblocks % 4;
1092
1093 if (ctx->use_p9)
1094 nburn = _gcry_chacha20_poly1305_ppc9_blocks4(
1095 ctx->input, outbuf, inbuf, nblocks,
1096 &c->u_mode.poly1305.ctx.state, authptr);
1097 else
1098 nburn = _gcry_chacha20_poly1305_ppc8_blocks4(
1099 ctx->input, outbuf, inbuf, nblocks,
1100 &c->u_mode.poly1305.ctx.state, authptr);
1101 burn = nburn > burn ? nburn : burn;
1102
1103 length -= nblocks * CHACHA20_BLOCK_SIZE;
1104 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1105 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1106 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1107 }
1108 #endif
1109
1110 #ifdef USE_S390X_VX_POLY1305
1111 if (ctx->use_s390x)
1112 {
1113 if (length >= 8 * CHACHA20_BLOCK_SIZE &&
1114 authoffset >= 8 * CHACHA20_BLOCK_SIZE)
1115 {
1116 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1117 nblocks -= nblocks % 8;
1118
1119 burn = _gcry_chacha20_poly1305_s390x_vx_blocks8(
1120 ctx->input, outbuf, inbuf, nblocks,
1121 &c->u_mode.poly1305.ctx.state, authptr);
1122 burn = nburn > burn ? nburn : burn;
1123
1124 length -= nblocks * CHACHA20_BLOCK_SIZE;
1125 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1126 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1127 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1128 }
1129
1130 if (length >= CHACHA20_BLOCK_SIZE &&
1131 authoffset >= CHACHA20_BLOCK_SIZE)
1132 {
1133 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1134
1135 burn = _gcry_chacha20_poly1305_s390x_vx_blocks4_2_1(
1136 ctx->input, outbuf, inbuf, nblocks,
1137 &c->u_mode.poly1305.ctx.state, authptr);
1138 burn = nburn > burn ? nburn : burn;
1139
1140 length -= nblocks * CHACHA20_BLOCK_SIZE;
1141 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1142 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1143 authptr += nblocks * CHACHA20_BLOCK_SIZE;
1144 }
1145 }
1146 #endif
1147
1148 if (authoffset > 0)
1149 {
1150 _gcry_poly1305_update (&c->u_mode.poly1305.ctx, authptr, authoffset);
1151 authptr += authoffset;
1152 authoffset = 0;
1153 }
1154
1155 gcry_assert(authptr == outbuf);
1156 }
1157
1158 while (length)
1159 {
1160 size_t currlen = length;
1161
1162 /* Since checksumming is done after encryption, process input in 24KiB
1163 * chunks to keep data loaded in L1 cache for checksumming. However
1164 * only do splitting if input is large enough so that last chunks does
1165 * not end up being short. */
1166 if (currlen > 32 * 1024)
1167 currlen = 24 * 1024;
1168
1169 nburn = do_chacha20_encrypt_stream_tail (ctx, outbuf, inbuf, currlen);
1170 burn = nburn > burn ? nburn : burn;
1171
1172 nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, outbuf,
1173 currlen);
1174 burn = nburn > burn ? nburn : burn;
1175
1176 outbuf += currlen;
1177 inbuf += currlen;
1178 length -= currlen;
1179 }
1180
1181 if (burn)
1182 _gcry_burn_stack (burn);
1183
1184 return 0;
1185 }
1186
1187
1188 gcry_err_code_t
1189 _gcry_chacha20_poly1305_decrypt(gcry_cipher_hd_t c, byte *outbuf,
1190 const byte *inbuf, size_t length)
1191 {
1192 CHACHA20_context_t *ctx = (void *) &c->context.c;
1193 unsigned int nburn, burn = 0;
1194 #if defined(USE_AVX512) || defined(USE_PPC_VEC_POLY1305) \
1195 || defined(USE_AVX2) || defined(USE_SSSE3) || defined(USE_AARCH64_SIMD) \
1196 || defined(USE_S390X_VX_POLY1305)
1197 int skip_stitched = 0;
1198 #endif
1199
1200 if (!length)
1201 return 0;
1202
1203 if (ctx->unused)
1204 {
1205 unsigned char *p = ctx->pad;
1206 size_t n;
1207
1208 gcry_assert (ctx->unused < CHACHA20_BLOCK_SIZE);
1209
1210 n = ctx->unused;
1211 if (n > length)
1212 n = length;
1213
1214 nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, inbuf, n);
1215 burn = nburn > burn ? nburn : burn;
1216 buf_xor (outbuf, inbuf, p + CHACHA20_BLOCK_SIZE - ctx->unused, n);
1217 length -= n;
1218 outbuf += n;
1219 inbuf += n;
1220 ctx->unused -= n;
1221
1222 if (!length)
1223 {
1224 if (burn)
1225 _gcry_burn_stack (burn);
1226
1227 return 0;
1228 }
1229 gcry_assert (!ctx->unused);
1230 }
1231
1232 gcry_assert (c->u_mode.poly1305.ctx.leftover == 0);
1233
1234 #ifdef USE_AVX512
1235 if (ctx->use_avx512)
1236 {
1237 /* Skip stitched chacha20-poly1305 for AVX512. */
1238 skip_stitched = 1;
1239 }
1240 #endif
1241 #ifdef USE_PPC_VEC_POLY1305
1242 if (ctx->use_ppc && ctx->use_p10)
1243 {
1244 /* Skip stitched chacha20-poly1305 for P10. */
1245 skip_stitched = 1;
1246 }
1247 #endif
1248
1249 #ifdef USE_AVX2
1250 if (!skip_stitched && ctx->use_avx2 && length >= 8 * CHACHA20_BLOCK_SIZE)
1251 {
1252 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1253 nblocks -= nblocks % 8;
1254
1255 nburn = _gcry_chacha20_poly1305_amd64_avx2_blocks8(
1256 ctx->input, outbuf, inbuf, nblocks,
1257 &c->u_mode.poly1305.ctx.state, inbuf);
1258 burn = nburn > burn ? nburn : burn;
1259
1260 length -= nblocks * CHACHA20_BLOCK_SIZE;
1261 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1262 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1263 }
1264 #endif
1265
1266 #ifdef USE_SSSE3
1267 if (!skip_stitched && ctx->use_ssse3)
1268 {
1269 if (length >= 4 * CHACHA20_BLOCK_SIZE)
1270 {
1271 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1272 nblocks -= nblocks % 4;
1273
1274 nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks4(
1275 ctx->input, outbuf, inbuf, nblocks,
1276 &c->u_mode.poly1305.ctx.state, inbuf);
1277 burn = nburn > burn ? nburn : burn;
1278
1279 length -= nblocks * CHACHA20_BLOCK_SIZE;
1280 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1281 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1282 }
1283
1284 if (length >= CHACHA20_BLOCK_SIZE)
1285 {
1286 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1287
1288 nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks1(
1289 ctx->input, outbuf, inbuf, nblocks,
1290 &c->u_mode.poly1305.ctx.state, inbuf);
1291 burn = nburn > burn ? nburn : burn;
1292
1293 length -= nblocks * CHACHA20_BLOCK_SIZE;
1294 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1295 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1296 }
1297 }
1298 #endif
1299
1300 #ifdef USE_AARCH64_SIMD
1301 if (!skip_stitched && ctx->use_neon && length >= 4 * CHACHA20_BLOCK_SIZE)
1302 {
1303 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1304 nblocks -= nblocks % 4;
1305
1306 nburn = _gcry_chacha20_poly1305_aarch64_blocks4(
1307 ctx->input, outbuf, inbuf, nblocks,
1308 &c->u_mode.poly1305.ctx.state, inbuf);
1309 burn = nburn > burn ? nburn : burn;
1310
1311 length -= nblocks * CHACHA20_BLOCK_SIZE;
1312 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1313 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1314 }
1315 #endif
1316
1317 #ifdef USE_PPC_VEC_POLY1305
1318 /* skip stitch for p10 */
1319 if (!skip_stitched && ctx->use_ppc && length >= 4 * CHACHA20_BLOCK_SIZE)
1320 {
1321 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1322 nblocks -= nblocks % 4;
1323
1324 if (ctx->use_p9)
1325 nburn = _gcry_chacha20_poly1305_ppc9_blocks4(
1326 ctx->input, outbuf, inbuf, nblocks,
1327 &c->u_mode.poly1305.ctx.state, inbuf);
1328 else
1329 nburn = _gcry_chacha20_poly1305_ppc8_blocks4(
1330 ctx->input, outbuf, inbuf, nblocks,
1331 &c->u_mode.poly1305.ctx.state, inbuf);
1332 burn = nburn > burn ? nburn : burn;
1333
1334 length -= nblocks * CHACHA20_BLOCK_SIZE;
1335 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1336 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1337 }
1338 #endif
1339
1340 #ifdef USE_S390X_VX_POLY1305
1341 if (!skip_stitched && ctx->use_s390x)
1342 {
1343 if (length >= 8 * CHACHA20_BLOCK_SIZE)
1344 {
1345 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1346 nblocks -= nblocks % 8;
1347
1348 nburn = _gcry_chacha20_poly1305_s390x_vx_blocks8(
1349 ctx->input, outbuf, inbuf, nblocks,
1350 &c->u_mode.poly1305.ctx.state, inbuf);
1351 burn = nburn > burn ? nburn : burn;
1352
1353 length -= nblocks * CHACHA20_BLOCK_SIZE;
1354 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1355 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1356 }
1357
1358 if (length >= CHACHA20_BLOCK_SIZE)
1359 {
1360 size_t nblocks = length / CHACHA20_BLOCK_SIZE;
1361
1362 nburn = _gcry_chacha20_poly1305_s390x_vx_blocks4_2_1(
1363 ctx->input, outbuf, inbuf, nblocks,
1364 &c->u_mode.poly1305.ctx.state, inbuf);
1365 burn = nburn > burn ? nburn : burn;
1366
1367 length -= nblocks * CHACHA20_BLOCK_SIZE;
1368 outbuf += nblocks * CHACHA20_BLOCK_SIZE;
1369 inbuf += nblocks * CHACHA20_BLOCK_SIZE;
1370 }
1371 }
1372 #endif
1373
1374 while (length)
1375 {
1376 size_t currlen = length;
1377
1378 /* Since checksumming is done before decryption, process input in 24KiB
1379 * chunks to keep data loaded in L1 cache for decryption. However only
1380 * do splitting if input is large enough so that last chunks does not
1381 * end up being short. */
1382 if (currlen > 32 * 1024)
1383 currlen = 24 * 1024;
1384
1385 nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, inbuf,
1386 currlen);
1387 burn = nburn > burn ? nburn : burn;
1388
1389 nburn = do_chacha20_encrypt_stream_tail (ctx, outbuf, inbuf, currlen);
1390 burn = nburn > burn ? nburn : burn;
1391
1392 outbuf += currlen;
1393 inbuf += currlen;
1394 length -= currlen;
1395 }
1396
1397 if (burn)
1398 _gcry_burn_stack (burn);
1399
1400 return 0;
1401 }
1402
1403
1404 static const char *
1405 selftest (void)
1406 {
1407 byte ctxbuf[sizeof(CHACHA20_context_t) + 15];
1408 CHACHA20_context_t *ctx;
1409 byte scratch[127 + 1];
1410 byte buf[512 + 64 + 4];
1411 int i;
1412
1413 /* From draft-strombergson-chacha-test-vectors */
1414 static byte key_1[] = {
1415 0xc4, 0x6e, 0xc1, 0xb1, 0x8c, 0xe8, 0xa8, 0x78,
1416 0x72, 0x5a, 0x37, 0xe7, 0x80, 0xdf, 0xb7, 0x35,
1417 0x1f, 0x68, 0xed, 0x2e, 0x19, 0x4c, 0x79, 0xfb,
1418 0xc6, 0xae, 0xbe, 0xe1, 0xa6, 0x67, 0x97, 0x5d
1419 };
1420 static const byte nonce_1[] =
1421 { 0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21 };
1422 static const byte plaintext_1[127] = {
1423 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1424 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1425 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1426 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1427 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1428 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1429 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1430 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1431 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1432 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1433 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1434 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1435 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1436 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1437 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1439 };
1440 static const byte ciphertext_1[127] = {
1441 0xf6, 0x3a, 0x89, 0xb7, 0x5c, 0x22, 0x71, 0xf9,
1442 0x36, 0x88, 0x16, 0x54, 0x2b, 0xa5, 0x2f, 0x06,
1443 0xed, 0x49, 0x24, 0x17, 0x92, 0x30, 0x2b, 0x00,
1444 0xb5, 0xe8, 0xf8, 0x0a, 0xe9, 0xa4, 0x73, 0xaf,
1445 0xc2, 0x5b, 0x21, 0x8f, 0x51, 0x9a, 0xf0, 0xfd,
1446 0xd4, 0x06, 0x36, 0x2e, 0x8d, 0x69, 0xde, 0x7f,
1447 0x54, 0xc6, 0x04, 0xa6, 0xe0, 0x0f, 0x35, 0x3f,
1448 0x11, 0x0f, 0x77, 0x1b, 0xdc, 0xa8, 0xab, 0x92,
1449 0xe5, 0xfb, 0xc3, 0x4e, 0x60, 0xa1, 0xd9, 0xa9,
1450 0xdb, 0x17, 0x34, 0x5b, 0x0a, 0x40, 0x27, 0x36,
1451 0x85, 0x3b, 0xf9, 0x10, 0xb0, 0x60, 0xbd, 0xf1,
1452 0xf8, 0x97, 0xb6, 0x29, 0x0f, 0x01, 0xd1, 0x38,
1453 0xae, 0x2c, 0x4c, 0x90, 0x22, 0x5b, 0xa9, 0xea,
1454 0x14, 0xd5, 0x18, 0xf5, 0x59, 0x29, 0xde, 0xa0,
1455 0x98, 0xca, 0x7a, 0x6c, 0xcf, 0xe6, 0x12, 0x27,
1456 0x05, 0x3c, 0x84, 0xe4, 0x9a, 0x4a, 0x33
1457 };
1458
1459 /* 16-byte alignment required for amd64 implementation. */
1460 ctx = (CHACHA20_context_t *)((uintptr_t)(ctxbuf + 15) & ~(uintptr_t)15);
1461
1462 chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
1463 chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
1464 scratch[sizeof (scratch) - 1] = 0;
1465 chacha20_encrypt_stream (ctx, scratch, plaintext_1, sizeof plaintext_1);
1466 if (memcmp (scratch, ciphertext_1, sizeof ciphertext_1))
1467 return "ChaCha20 encryption test 1 failed.";
1468 if (scratch[sizeof (scratch) - 1])
1469 return "ChaCha20 wrote too much.";
1470 chacha20_setkey (ctx, key_1, sizeof (key_1), NULL);
1471 chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
1472 chacha20_encrypt_stream (ctx, scratch, scratch, sizeof plaintext_1);
1473 if (memcmp (scratch, plaintext_1, sizeof plaintext_1))
1474 return "ChaCha20 decryption test 1 failed.";
1475
1476 for (i = 0; i < sizeof buf; i++)
1477 buf[i] = i;
1478 chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
1479 chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
1480 /*encrypt */
1481 chacha20_encrypt_stream (ctx, buf, buf, sizeof buf);
1482 /*decrypt */
1483 chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
1484 chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
1485 chacha20_encrypt_stream (ctx, buf, buf, 1);
1486 chacha20_encrypt_stream (ctx, buf + 1, buf + 1, (sizeof buf) - 1 - 1);
1487 chacha20_encrypt_stream (ctx, buf + (sizeof buf) - 1,
1488 buf + (sizeof buf) - 1, 1);
1489 for (i = 0; i < sizeof buf; i++)
1490 if (buf[i] != (byte) i)
1491 return "ChaCha20 encryption test 2 failed.";
1492
1493 chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
1494 chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
1495 /* encrypt */
1496 for (i = 0; i < sizeof buf; i++)
1497 chacha20_encrypt_stream (ctx, &buf[i], &buf[i], 1);
1498 /* decrypt */
1499 chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
1500 chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
1501 chacha20_encrypt_stream (ctx, buf, buf, sizeof buf);
1502 for (i = 0; i < sizeof buf; i++)
1503 if (buf[i] != (byte) i)
1504 return "ChaCha20 encryption test 3 failed.";
1505
1506 return NULL;
1507 }
1508
1509
1510 gcry_cipher_spec_t _gcry_cipher_spec_chacha20 = {
1511 GCRY_CIPHER_CHACHA20,
1512 {0, 0}, /* flags */
1513 "CHACHA20", /* name */
1514 NULL, /* aliases */
1515 NULL, /* oids */
1516 1, /* blocksize in bytes. */
1517 CHACHA20_MAX_KEY_SIZE * 8, /* standard key length in bits. */
1518 sizeof (CHACHA20_context_t),
1519 chacha20_setkey,
1520 NULL,
1521 NULL,
1522 chacha20_encrypt_stream,
1523 chacha20_encrypt_stream,
1524 NULL,
1525 NULL,
1526 chacha20_setiv
1527 };