Debian Codegraph @

cipher/cipher.c

raw
1 /* cipher.c - cipher dispatcher
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 * 2005, 2007, 2008, 2009, 2011 Free Software Foundation, Inc.
4 * Copyright (C) 2013 g10 Code GmbH
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "g10lib.h"
29 #include "../src/gcrypt-testapi.h"
30 #include "cipher.h"
31 #include "./cipher-internal.h"
32
33
34 /* This is the list of the default ciphers, which are included in
35 libgcrypt. */
36 static gcry_cipher_spec_t * const cipher_list[] =
37 {
38 #if USE_BLOWFISH
39 &_gcry_cipher_spec_blowfish,
40 #endif
41 #if USE_DES
42 &_gcry_cipher_spec_des,
43 &_gcry_cipher_spec_tripledes,
44 #endif
45 #if USE_ARCFOUR
46 &_gcry_cipher_spec_arcfour,
47 #endif
48 #if USE_CAST5
49 &_gcry_cipher_spec_cast5,
50 #endif
51 #if USE_AES
52 &_gcry_cipher_spec_aes,
53 &_gcry_cipher_spec_aes192,
54 &_gcry_cipher_spec_aes256,
55 #endif
56 #if USE_TWOFISH
57 &_gcry_cipher_spec_twofish,
58 &_gcry_cipher_spec_twofish128,
59 #endif
60 #if USE_SERPENT
61 &_gcry_cipher_spec_serpent128,
62 &_gcry_cipher_spec_serpent192,
63 &_gcry_cipher_spec_serpent256,
64 #endif
65 #if USE_RFC2268
66 &_gcry_cipher_spec_rfc2268_40,
67 &_gcry_cipher_spec_rfc2268_128,
68 #endif
69 #if USE_SEED
70 &_gcry_cipher_spec_seed,
71 #endif
72 #if USE_CAMELLIA
73 &_gcry_cipher_spec_camellia128,
74 &_gcry_cipher_spec_camellia192,
75 &_gcry_cipher_spec_camellia256,
76 #endif
77 #if USE_IDEA
78 &_gcry_cipher_spec_idea,
79 #endif
80 #if USE_SALSA20
81 &_gcry_cipher_spec_salsa20,
82 &_gcry_cipher_spec_salsa20r12,
83 #endif
84 #if USE_GOST28147
85 &_gcry_cipher_spec_gost28147,
86 &_gcry_cipher_spec_gost28147_mesh,
87 #endif
88 #if USE_CHACHA20
89 &_gcry_cipher_spec_chacha20,
90 #endif
91 #if USE_SM4
92 &_gcry_cipher_spec_sm4,
93 #endif
94 #if USE_ARIA
95 &_gcry_cipher_spec_aria128,
96 &_gcry_cipher_spec_aria192,
97 &_gcry_cipher_spec_aria256,
98 #endif
99 NULL
100 };
101
102 /* Cipher implementations starting with index 0 (enum gcry_cipher_algos) */
103 static gcry_cipher_spec_t * const cipher_list_algo0[] =
104 {
105 NULL, /* GCRY_CIPHER_NONE */
106 #if USE_IDEA
107 &_gcry_cipher_spec_idea,
108 #else
109 NULL,
110 #endif
111 #if USE_DES
112 &_gcry_cipher_spec_tripledes,
113 #else
114 NULL,
115 #endif
116 #if USE_CAST5
117 &_gcry_cipher_spec_cast5,
118 #else
119 NULL,
120 #endif
121 #if USE_BLOWFISH
122 &_gcry_cipher_spec_blowfish,
123 #else
124 NULL,
125 #endif
126 NULL, /* GCRY_CIPHER_SAFER_SK128 */
127 NULL, /* GCRY_CIPHER_DES_SK */
128 #if USE_AES
129 &_gcry_cipher_spec_aes,
130 &_gcry_cipher_spec_aes192,
131 &_gcry_cipher_spec_aes256,
132 #else
133 NULL,
134 NULL,
135 NULL,
136 #endif
137 #if USE_TWOFISH
138 &_gcry_cipher_spec_twofish
139 #else
140 NULL
141 #endif
142 };
143
144 /* Cipher implementations starting with index 301 (enum gcry_cipher_algos) */
145 static gcry_cipher_spec_t * const cipher_list_algo301[] =
146 {
147 #if USE_ARCFOUR
148 &_gcry_cipher_spec_arcfour,
149 #else
150 NULL,
151 #endif
152 #if USE_DES
153 &_gcry_cipher_spec_des,
154 #else
155 NULL,
156 #endif
157 #if USE_TWOFISH
158 &_gcry_cipher_spec_twofish128,
159 #else
160 NULL,
161 #endif
162 #if USE_SERPENT
163 &_gcry_cipher_spec_serpent128,
164 &_gcry_cipher_spec_serpent192,
165 &_gcry_cipher_spec_serpent256,
166 #else
167 NULL,
168 NULL,
169 NULL,
170 #endif
171 #if USE_RFC2268
172 &_gcry_cipher_spec_rfc2268_40,
173 &_gcry_cipher_spec_rfc2268_128,
174 #else
175 NULL,
176 NULL,
177 #endif
178 #if USE_SEED
179 &_gcry_cipher_spec_seed,
180 #else
181 NULL,
182 #endif
183 #if USE_CAMELLIA
184 &_gcry_cipher_spec_camellia128,
185 &_gcry_cipher_spec_camellia192,
186 &_gcry_cipher_spec_camellia256,
187 #else
188 NULL,
189 NULL,
190 NULL,
191 #endif
192 #if USE_SALSA20
193 &_gcry_cipher_spec_salsa20,
194 &_gcry_cipher_spec_salsa20r12,
195 #else
196 NULL,
197 NULL,
198 #endif
199 #if USE_GOST28147
200 &_gcry_cipher_spec_gost28147,
201 #else
202 NULL,
203 #endif
204 #if USE_CHACHA20
205 &_gcry_cipher_spec_chacha20,
206 #else
207 NULL,
208 #endif
209 #if USE_GOST28147
210 &_gcry_cipher_spec_gost28147_mesh,
211 #else
212 NULL,
213 #endif
214 #if USE_SM4
215 &_gcry_cipher_spec_sm4,
216 #else
217 NULL,
218 #endif
219 #if USE_ARIA
220 &_gcry_cipher_spec_aria128,
221 &_gcry_cipher_spec_aria192,
222 &_gcry_cipher_spec_aria256
223 #else
224 NULL,
225 NULL,
226 NULL
227 #endif
228 };
229
230
231 static void _gcry_cipher_setup_mode_ops(gcry_cipher_hd_t c, int mode);
232
233
234 static int
235 map_algo (int algo)
236 {
237 return algo;
238 }
239
240
241 /* Return the spec structure for the cipher algorithm ALGO. For
242 an unknown algorithm NULL is returned. */
243 static gcry_cipher_spec_t *
244 spec_from_algo (int algo)
245 {
246 gcry_cipher_spec_t *spec = NULL;
247
248 algo = map_algo (algo);
249
250 if (algo >= 0 && algo < DIM(cipher_list_algo0))
251 spec = cipher_list_algo0[algo];
252 else if (algo >= 301 && algo < 301 + DIM(cipher_list_algo301))
253 spec = cipher_list_algo301[algo - 301];
254
255 if (spec)
256 gcry_assert (spec->algo == algo);
257
258 return spec;
259 }
260
261
262 /* Lookup a cipher's spec by its name. */
263 static gcry_cipher_spec_t *
264 spec_from_name (const char *name)
265 {
266 gcry_cipher_spec_t *spec;
267 int idx;
268 const char **aliases;
269
270 for (idx=0; (spec = cipher_list[idx]); idx++)
271 {
272 if (!stricmp (name, spec->name))
273 return spec;
274 if (spec->aliases)
275 {
276 for (aliases = spec->aliases; *aliases; aliases++)
277 if (!stricmp (name, *aliases))
278 return spec;
279 }
280 }
281
282 return NULL;
283 }
284
285
286 /* Lookup a cipher's spec by its OID. */
287 static gcry_cipher_spec_t *
288 spec_from_oid (const char *oid)
289 {
290 gcry_cipher_spec_t *spec;
291 const gcry_cipher_oid_spec_t *oid_specs;
292 int idx, j;
293
294 for (idx=0; (spec = cipher_list[idx]); idx++)
295 {
296 oid_specs = spec->oids;
297 if (oid_specs)
298 {
299 for (j = 0; oid_specs[j].oid; j++)
300 if (!stricmp (oid, oid_specs[j].oid))
301 return spec;
302 }
303 }
304
305 return NULL;
306 }
307
308
309 /* Locate the OID in the oid table and return the spec or NULL if not
310 found. An optional "oid." or "OID." prefix in OID is ignored, the
311 OID is expected to be in standard IETF dotted notation. A pointer
312 to the OID specification of the module implementing this algorithm
313 is return in OID_SPEC unless passed as NULL.*/
314 static gcry_cipher_spec_t *
315 search_oid (const char *oid, gcry_cipher_oid_spec_t *oid_spec)
316 {
317 gcry_cipher_spec_t *spec;
318 int i;
319
320 if (!oid)
321 return NULL;
322
323 if (!strncmp (oid, "oid.", 4) || !strncmp (oid, "OID.", 4))
324 oid += 4;
325
326 spec = spec_from_oid (oid);
327 if (spec && spec->oids)
328 {
329 for (i = 0; spec->oids[i].oid; i++)
330 if (!stricmp (oid, spec->oids[i].oid))
331 {
332 if (oid_spec)
333 *oid_spec = spec->oids[i];
334 return spec;
335 }
336 }
337
338 return NULL;
339 }
340
341
342 /* Map STRING to the cipher algorithm identifier. Returns the
343 algorithm ID of the cipher for the given name or 0 if the name is
344 not known. It is valid to pass NULL for STRING which results in a
345 return value of 0. */
346 int
347 _gcry_cipher_map_name (const char *string)
348 {
349 gcry_cipher_spec_t *spec;
350
351 if (!string)
352 return 0;
353
354 /* If the string starts with a digit (optionally prefixed with
355 either "OID." or "oid."), we first look into our table of ASN.1
356 object identifiers to figure out the algorithm */
357
358 spec = search_oid (string, NULL);
359 if (spec)
360 return spec->algo;
361
362 spec = spec_from_name (string);
363 if (spec)
364 return spec->algo;
365
366 return 0;
367 }
368
369
370 /* Given a STRING with an OID in dotted decimal notation, this
371 function returns the cipher mode (GCRY_CIPHER_MODE_*) associated
372 with that OID or 0 if no mode is known. Passing NULL for string
373 yields a return value of 0. */
374 int
375 _gcry_cipher_mode_from_oid (const char *string)
376 {
377 gcry_cipher_spec_t *spec;
378 gcry_cipher_oid_spec_t oid_spec;
379
380 if (!string)
381 return 0;
382
383 spec = search_oid (string, &oid_spec);
384 if (spec)
385 return oid_spec.mode;
386
387 return 0;
388 }
389
390
391 /* Map the cipher algorithm identifier ALGORITHM to a string
392 representing this algorithm. This string is the default name as
393 used by Libgcrypt. A "?" is returned for an unknown algorithm.
394 NULL is never returned. */
395 const char *
396 _gcry_cipher_algo_name (int algorithm)
397 {
398 gcry_cipher_spec_t *spec;
399
400 spec = spec_from_algo (algorithm);
401 return spec? spec->name : "?";
402 }
403
404
405 /* Flag the cipher algorithm with the identifier ALGORITHM as
406 disabled. There is no error return, the function does nothing for
407 unknown algorithms. Disabled algorithms are virtually not
408 available in Libgcrypt. This is not thread safe and should thus be
409 called early. */
410 static void
411 disable_cipher_algo (int algo)
412 {
413 gcry_cipher_spec_t *spec = spec_from_algo (algo);
414
415 if (spec)
416 spec->flags.disabled = 1;
417 }
418
419
420 /* Return 0 if the cipher algorithm with identifier ALGORITHM is
421 available. Returns a basic error code value if it is not
422 available. */
423 static gcry_err_code_t
424 check_cipher_algo (int algorithm)
425 {
426 gcry_cipher_spec_t *spec;
427
428 spec = spec_from_algo (algorithm);
429 if (spec && !spec->flags.disabled && (spec->flags.fips || !fips_mode ()))
430 return 0;
431
432 return GPG_ERR_CIPHER_ALGO;
433 }
434
435
436 /* Return the standard length in bits of the key for the cipher
437 algorithm with the identifier ALGORITHM. */
438 static unsigned int
439 cipher_get_keylen (int algorithm)
440 {
441 gcry_cipher_spec_t *spec;
442 unsigned len = 0;
443
444 spec = spec_from_algo (algorithm);
445 if (spec)
446 {
447 len = spec->keylen;
448 if (!len)
449 log_bug ("cipher %d w/o key length\n", algorithm);
450 }
451
452 return len;
453 }
454
455
456 /* Return the block length of the cipher algorithm with the identifier
457 ALGORITHM. This function return 0 for an invalid algorithm. */
458 static unsigned int
459 cipher_get_blocksize (int algorithm)
460 {
461 gcry_cipher_spec_t *spec;
462 unsigned len = 0;
463
464 spec = spec_from_algo (algorithm);
465 if (spec)
466 {
467 len = spec->blocksize;
468 if (!len)
469 log_bug ("cipher %d w/o blocksize\n", algorithm);
470 }
471
472 return len;
473 }
474
475
476 /*
477 Open a cipher handle for use with cipher algorithm ALGORITHM, using
478 the cipher mode MODE (one of the GCRY_CIPHER_MODE_*) and return a
479 handle in HANDLE. Put NULL into HANDLE and return an error code if
480 something goes wrong. FLAGS may be used to modify the
481 operation. The defined flags are:
482
483 GCRY_CIPHER_SECURE: allocate all internal buffers in secure memory.
484 GCRY_CIPHER_ENABLE_SYNC: Enable the sync operation as used in OpenPGP.
485 GCRY_CIPHER_CBC_CTS: Enable CTS mode.
486 GCRY_CIPHER_CBC_MAC: Enable MAC mode.
487
488 Values for these flags may be combined using OR.
489 */
490 gcry_err_code_t
491 _gcry_cipher_open (gcry_cipher_hd_t *handle,
492 int algo, int mode, unsigned int flags)
493 {
494 gcry_err_code_t rc;
495 gcry_cipher_hd_t h = NULL;
496
497 if (mode >= GCRY_CIPHER_MODE_INTERNAL)
498 rc = GPG_ERR_INV_CIPHER_MODE;
499 else
500 rc = _gcry_cipher_open_internal (&h, algo, mode, flags);
501
502 *handle = rc ? NULL : h;
503
504 return rc;
505 }
506
507
508 /* Return an error if the give cipher mode is non-FIPS compliant. */
509 static gcry_err_code_t
510 cipher_modes_fips_compliance (enum gcry_cipher_modes mode)
511 {
512 switch (mode)
513 {
514 case GCRY_CIPHER_MODE_ECB:
515 case GCRY_CIPHER_MODE_CBC:
516 case GCRY_CIPHER_MODE_CFB:
517 case GCRY_CIPHER_MODE_CFB8:
518 case GCRY_CIPHER_MODE_OFB:
519 case GCRY_CIPHER_MODE_CTR:
520 case GCRY_CIPHER_MODE_CCM:
521 case GCRY_CIPHER_MODE_XTS:
522 case GCRY_CIPHER_MODE_AESWRAP:
523 return 0;
524 case GCRY_CIPHER_MODE_NONE:
525 case GCRY_CIPHER_MODE_STREAM:
526 case GCRY_CIPHER_MODE_GCM:
527 case GCRY_CIPHER_MODE_POLY1305:
528 case GCRY_CIPHER_MODE_OCB:
529 case GCRY_CIPHER_MODE_EAX:
530 case GCRY_CIPHER_MODE_SIV:
531 case GCRY_CIPHER_MODE_GCM_SIV:
532 break;
533 }
534 return GPG_ERR_NOT_SUPPORTED;
535 }
536
537
538 /* This is similar to cipher_modes_fips_compliance but only for the
539 * internal modes (i.e. CMAC). Return an error if the mode is
540 * non-FIPS compliant. */
541 static gcry_err_code_t
542 cipher_int_modes_fips_compliance (enum gcry_cipher_internal_modes mode)
543 {
544 switch (mode)
545 {
546 case GCRY_CIPHER_MODE_INTERNAL:
547 break;
548 case GCRY_CIPHER_MODE_CMAC:
549 return 0;
550 }
551 return GPG_ERR_NOT_SUPPORTED;
552 }
553
554
555 /* Return an error if the give cipher mode is non-FIPS compliant. The
556 * mode is not an enum here so that we can use it for real modes and
557 * for internal modes. */
558 gcry_err_code_t
559 _gcry_cipher_mode_fips_compliance (int mode)
560 {
561 if (mode >= GCRY_CIPHER_MODE_INTERNAL)
562 return cipher_int_modes_fips_compliance (mode);
563 else
564 return cipher_modes_fips_compliance (mode);
565 }
566
567
568 gcry_err_code_t
569 _gcry_cipher_open_internal (gcry_cipher_hd_t *handle,
570 int algo, int mode, unsigned int flags)
571 {
572 int secure = !!(flags & GCRY_CIPHER_SECURE);
573 gcry_cipher_spec_t *spec;
574 gcry_cipher_hd_t h = NULL;
575 gcry_err_code_t err;
576
577 /* If the application missed to call the random poll function, we do
578 it here to ensure that it is used once in a while. */
579 _gcry_fast_random_poll ();
580
581 spec = spec_from_algo (algo);
582 if (!spec)
583 err = GPG_ERR_CIPHER_ALGO;
584 else if (spec->flags.disabled)
585 err = GPG_ERR_CIPHER_ALGO;
586 else if (fips_mode ())
587 {
588 if (!spec->flags.fips)
589 {
590 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_CIPHER))
591 err = GPG_ERR_CIPHER_ALGO;
592 else
593 {
594 fips_service_indicator_mark_non_compliant ();
595 err = 0;
596 }
597 }
598 else if ((err = _gcry_cipher_mode_fips_compliance (mode)))
599 {
600 if (!fips_check_rejection (GCRY_FIPS_FLAG_REJECT_CIPHER_MODE))
601 {
602 fips_service_indicator_mark_non_compliant ();
603 err = 0;
604 }
605 }
606 }
607 else
608 err = 0;
609
610 /* check flags */
611 if ((! err)
612 && ((flags & ~(0
613 | GCRY_CIPHER_SECURE
614 | GCRY_CIPHER_ENABLE_SYNC
615 | GCRY_CIPHER_CBC_CTS
616 | GCRY_CIPHER_CBC_MAC
617 | GCRY_CIPHER_EXTENDED))
618 || ((flags & GCRY_CIPHER_CBC_CTS) && (flags & GCRY_CIPHER_CBC_MAC))))
619 err = GPG_ERR_CIPHER_ALGO;
620
621 /* check that a valid mode has been requested */
622 if (! err)
623 switch (mode)
624 {
625 case GCRY_CIPHER_MODE_ECB:
626 case GCRY_CIPHER_MODE_CBC:
627 case GCRY_CIPHER_MODE_CFB:
628 case GCRY_CIPHER_MODE_CFB8:
629 case GCRY_CIPHER_MODE_OFB:
630 case GCRY_CIPHER_MODE_CTR:
631 case GCRY_CIPHER_MODE_AESWRAP:
632 case GCRY_CIPHER_MODE_CMAC:
633 case GCRY_CIPHER_MODE_EAX:
634 if (!spec->encrypt || !spec->decrypt)
635 err = GPG_ERR_INV_CIPHER_MODE;
636 break;
637
638 case GCRY_CIPHER_MODE_CCM:
639 if (!spec->encrypt || !spec->decrypt)
640 err = GPG_ERR_INV_CIPHER_MODE;
641 else if (spec->blocksize != GCRY_CCM_BLOCK_LEN)
642 err = GPG_ERR_INV_CIPHER_MODE;
643 break;
644
645 case GCRY_CIPHER_MODE_XTS:
646 if (!spec->encrypt || !spec->decrypt)
647 err = GPG_ERR_INV_CIPHER_MODE;
648 else if (spec->blocksize != GCRY_XTS_BLOCK_LEN)
649 err = GPG_ERR_INV_CIPHER_MODE;
650 break;
651
652 case GCRY_CIPHER_MODE_GCM:
653 if (!spec->encrypt || !spec->decrypt)
654 err = GPG_ERR_INV_CIPHER_MODE;
655 else if (spec->blocksize != GCRY_GCM_BLOCK_LEN)
656 err = GPG_ERR_INV_CIPHER_MODE;
657 break;
658
659 case GCRY_CIPHER_MODE_SIV:
660 case GCRY_CIPHER_MODE_GCM_SIV:
661 if (!spec->encrypt || !spec->decrypt)
662 err = GPG_ERR_INV_CIPHER_MODE;
663 else if (spec->blocksize != GCRY_SIV_BLOCK_LEN)
664 err = GPG_ERR_INV_CIPHER_MODE;
665 break;
666
667 case GCRY_CIPHER_MODE_POLY1305:
668 if (!spec->stencrypt || !spec->stdecrypt || !spec->setiv)
669 err = GPG_ERR_INV_CIPHER_MODE;
670 else if (spec->algo != GCRY_CIPHER_CHACHA20)
671 err = GPG_ERR_INV_CIPHER_MODE;
672 break;
673
674 case GCRY_CIPHER_MODE_OCB:
675 /* Note that our implementation allows only for 128 bit block
676 length algorithms. Lower block lengths would be possible
677 but we do not implement them because they limit the
678 security too much. */
679 if (!spec->encrypt || !spec->decrypt)
680 err = GPG_ERR_INV_CIPHER_MODE;
681 else if (spec->blocksize != GCRY_OCB_BLOCK_LEN)
682 err = GPG_ERR_INV_CIPHER_MODE;
683 break;
684
685 case GCRY_CIPHER_MODE_STREAM:
686 if (!spec->stencrypt || !spec->stdecrypt)
687 err = GPG_ERR_INV_CIPHER_MODE;
688 break;
689
690 case GCRY_CIPHER_MODE_NONE:
691 /* This mode may be used for debugging. It copies the main
692 text verbatim to the ciphertext. We do not allow this in
693 fips mode or if no debug flag has been set. */
694 if (fips_mode () || !_gcry_get_debug_flag (0))
695 err = GPG_ERR_INV_CIPHER_MODE;
696 break;
697
698 default:
699 err = GPG_ERR_INV_CIPHER_MODE;
700 }
701
702 /* Perform selftest here and mark this with a flag in cipher_table?
703 No, we should not do this as it takes too long. Further it does
704 not make sense to exclude algorithms with failing selftests at
705 runtime: If a selftest fails there is something seriously wrong
706 with the system and thus we better die immediately. */
707
708 if (! err)
709 {
710 size_t size = (sizeof (*h)
711 + 2 * spec->contextsize
712 - sizeof (cipher_context_alignment_t)
713 #ifdef NEED_16BYTE_ALIGNED_CONTEXT
714 + 15 /* Space for leading alignment gap. */
715 #endif /*NEED_16BYTE_ALIGNED_CONTEXT*/
716 );
717
718 /* Space needed per mode. */
719 switch (mode)
720 {
721 case GCRY_CIPHER_MODE_XTS:
722 case GCRY_CIPHER_MODE_SIV:
723 /* Additional cipher context for tweak. */
724 size += 2 * spec->contextsize + 15;
725 break;
726
727 default:
728 break;
729 }
730
731 if (secure)
732 h = xtrycalloc_secure (1, size);
733 else
734 h = xtrycalloc (1, size);
735
736 if (! h)
737 err = gpg_err_code_from_syserror ();
738 else
739 {
740 size_t off = 0;
741 char *tc;
742
743 #ifdef NEED_16BYTE_ALIGNED_CONTEXT
744 if ( ((uintptr_t)h & 0x0f) )
745 {
746 /* The malloced block is not aligned on a 16 byte
747 boundary. Correct for this. */
748 off = 16 - ((uintptr_t)h & 0x0f);
749 h = (void*)((char*)h + off);
750 }
751 #endif /*NEED_16BYTE_ALIGNED_CONTEXT*/
752
753 h->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
754 h->actual_handle_size = size - off;
755 h->handle_offset = off;
756 h->spec = spec;
757 h->algo = algo;
758 h->mode = mode;
759 h->flags = flags;
760
761 /* Setup mode routines. */
762 _gcry_cipher_setup_mode_ops(h, mode);
763
764 /* Setup defaults depending on the mode. */
765 switch (mode)
766 {
767 case GCRY_CIPHER_MODE_OCB:
768 h->u_mode.ocb.taglen = 16; /* Bytes. */
769 break;
770
771 case GCRY_CIPHER_MODE_XTS:
772 tc = h->context.c + spec->contextsize * 2;
773 tc += (16 - (uintptr_t)tc % 16) % 16;
774 h->u_mode.xts.tweak_context = tc;
775 break;
776
777 case GCRY_CIPHER_MODE_SIV:
778 tc = h->context.c + spec->contextsize * 2;
779 tc += (16 - (uintptr_t)tc % 16) % 16;
780 h->u_mode.siv.ctr_context = tc;
781 break;
782
783 default:
784 break;
785 }
786 }
787 }
788
789 /* Done. */
790
791 *handle = err ? NULL : h;
792
793 return err;
794 }
795
796
797 /* Release all resources associated with the cipher handle H. H may be
798 NULL in which case this is a no-operation. */
799 void
800 _gcry_cipher_close (gcry_cipher_hd_t h)
801 {
802 size_t off;
803
804 if (!h)
805 return;
806
807 if ((h->magic != CTX_MAGIC_SECURE)
808 && (h->magic != CTX_MAGIC_NORMAL))
809 _gcry_fatal_error(GPG_ERR_INTERNAL,
810 "gcry_cipher_close: already closed/invalid handle");
811 else
812 h->magic = 0;
813
814 /* We always want to wipe out the memory even when the context has
815 been allocated in secure memory. The user might have disabled
816 secure memory or is using his own implementation which does not
817 do the wiping. To accomplish this we need to keep track of the
818 actual size of this structure because we have no way to known
819 how large the allocated area was when using a standard malloc. */
820 off = h->handle_offset;
821 wipememory (h, h->actual_handle_size);
822
823 xfree ((char*)h - off);
824 }
825
826
827 /* Set the key to be used for the encryption context C to KEY with
828 length KEYLEN. The length should match the required length. */
829 static gcry_err_code_t
830 cipher_setkey (gcry_cipher_hd_t c, byte *key, size_t keylen)
831 {
832 gcry_err_code_t rc;
833
834 if (c->mode == GCRY_CIPHER_MODE_XTS)
835 {
836 /* XTS uses two keys. */
837 if (keylen % 2)
838 return GPG_ERR_INV_KEYLEN;
839 keylen /= 2;
840
841 if (fips_mode ())
842 {
843 /* Reject key if subkeys Key_1 and Key_2 are equal.
844 See "Implementation Guidance for FIPS 140-2, A.9 XTS-AES
845 Key Generation Requirements" for details. */
846 if (buf_eq_const (key, key + keylen, keylen))
847 {
848 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_CIPHER))
849 return GPG_ERR_WEAK_KEY;
850 else
851 fips_service_indicator_mark_non_compliant ();
852 }
853 }
854 }
855 else if (c->mode == GCRY_CIPHER_MODE_SIV)
856 {
857 /* SIV uses two keys. */
858 if (keylen % 2)
859 return GPG_ERR_INV_KEYLEN;
860 keylen /= 2;
861 }
862
863 rc = c->spec->setkey (&c->context.c, key, keylen, &c->bulk);
864 if (!rc || (c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
865 {
866 int is_weak_key = (rc == GPG_ERR_WEAK_KEY);
867
868 /* Duplicate initial context. */
869 memcpy ((void *) ((char *) &c->context.c + c->spec->contextsize),
870 (void *) &c->context.c,
871 c->spec->contextsize);
872 c->marks.key = 1;
873
874 switch (c->mode)
875 {
876 case GCRY_CIPHER_MODE_CMAC:
877 rc = _gcry_cipher_cmac_set_subkeys (c);
878 break;
879
880 case GCRY_CIPHER_MODE_EAX:
881 rc = _gcry_cipher_eax_setkey (c);
882 break;
883
884 case GCRY_CIPHER_MODE_GCM:
885 _gcry_cipher_gcm_setkey (c);
886 break;
887
888 case GCRY_CIPHER_MODE_GCM_SIV:
889 rc = _gcry_cipher_gcm_siv_setkey (c, keylen);
890 if (rc && !(c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
891 c->marks.key = 0;
892 break;
893
894 case GCRY_CIPHER_MODE_OCB:
895 _gcry_cipher_ocb_setkey (c);
896 break;
897
898 case GCRY_CIPHER_MODE_POLY1305:
899 _gcry_cipher_poly1305_setkey (c);
900 break;
901
902 case GCRY_CIPHER_MODE_XTS:
903 /* Setup tweak cipher with second part of XTS key. */
904 rc = c->spec->setkey (c->u_mode.xts.tweak_context, key + keylen,
905 keylen, &c->bulk);
906 if (!rc || (c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
907 {
908 /* Duplicate initial tweak context. */
909 memcpy (c->u_mode.xts.tweak_context + c->spec->contextsize,
910 c->u_mode.xts.tweak_context, c->spec->contextsize);
911 }
912 else
913 c->marks.key = 0;
914 break;
915
916 case GCRY_CIPHER_MODE_SIV:
917 /* Setup CTR cipher with second part of SIV key. */
918 rc = _gcry_cipher_siv_setkey (c, key + keylen, keylen);
919 if (!rc || (c->marks.allow_weak_key && rc == GPG_ERR_WEAK_KEY))
920 {
921 /* Duplicate initial CTR context. */
922 memcpy (c->u_mode.siv.ctr_context + c->spec->contextsize,
923 c->u_mode.siv.ctr_context, c->spec->contextsize);
924 }
925 else
926 c->marks.key = 0;
927 break;
928
929 default:
930 break;
931 }
932
933 /* Restore "weak key" error-code in case mode specific setkey
934 * returned success. */
935 if (!rc && is_weak_key)
936 rc = GPG_ERR_WEAK_KEY;
937 }
938 else
939 c->marks.key = 0;
940
941 return rc;
942 }
943
944
945 /* Set the IV to be used for the encryption context C to IV with
946 length IVLEN. The length should match the required length. */
947 static gcry_err_code_t
948 cipher_setiv (gcry_cipher_hd_t c, const byte *iv, size_t ivlen)
949 {
950 /* If the cipher has its own IV handler, we use only this one. This
951 is currently used for stream ciphers requiring a nonce. */
952 if (c->spec->setiv)
953 {
954 c->spec->setiv (&c->context.c, iv, ivlen);
955 return 0;
956 }
957
958 memset (c->u_iv.iv, 0, c->spec->blocksize);
959 if (iv)
960 {
961 if (ivlen != c->spec->blocksize)
962 {
963 log_info ("WARNING: cipher_setiv: ivlen=%u blklen=%u\n",
964 (unsigned int)ivlen, (unsigned int)c->spec->blocksize);
965 fips_signal_error ("IV length does not match blocklength");
966 }
967 if (ivlen > c->spec->blocksize)
968 ivlen = c->spec->blocksize;
969 memcpy (c->u_iv.iv, iv, ivlen);
970 c->marks.iv = 1;
971 }
972 else
973 c->marks.iv = 0;
974 c->unused = 0;
975
976 return 0;
977 }
978
979
980 /* Reset the cipher context to the initial context. This is basically
981 the same as an release followed by a new. */
982 static void
983 cipher_reset (gcry_cipher_hd_t c)
984 {
985 unsigned int marks_key, marks_allow_weak_key;
986
987 marks_key = c->marks.key;
988 marks_allow_weak_key = c->marks.allow_weak_key;
989
990 memcpy (&c->context.c,
991 (char *) &c->context.c + c->spec->contextsize,
992 c->spec->contextsize);
993 memset (&c->marks, 0, sizeof c->marks);
994 memset (c->u_iv.iv, 0, c->spec->blocksize);
995 memset (c->lastiv, 0, c->spec->blocksize);
996 memset (c->u_ctr.ctr, 0, c->spec->blocksize);
997 c->unused = 0;
998
999 c->marks.key = marks_key;
1000 c->marks.allow_weak_key = marks_allow_weak_key;
1001
1002 switch (c->mode)
1003 {
1004 case GCRY_CIPHER_MODE_CMAC:
1005 _gcry_cmac_reset(&c->u_mode.cmac);
1006 break;
1007
1008 case GCRY_CIPHER_MODE_EAX:
1009 _gcry_cmac_reset(&c->u_mode.eax.cmac_header);
1010 _gcry_cmac_reset(&c->u_mode.eax.cmac_ciphertext);
1011 break;
1012
1013 case GCRY_CIPHER_MODE_GCM:
1014 case GCRY_CIPHER_MODE_GCM_SIV:
1015 /* Only clear head of u_mode, keep ghash_key and gcm_table. */
1016 {
1017 byte *u_mode_pos = (void *)&c->u_mode;
1018 byte *ghash_key_pos = c->u_mode.gcm.u_ghash_key.key;
1019 size_t u_mode_head_length = ghash_key_pos - u_mode_pos;
1020
1021 memset (&c->u_mode, 0, u_mode_head_length);
1022 }
1023 break;
1024
1025 case GCRY_CIPHER_MODE_POLY1305:
1026 memset (&c->u_mode.poly1305, 0, sizeof c->u_mode.poly1305);
1027 break;
1028
1029 case GCRY_CIPHER_MODE_CCM:
1030 memset (&c->u_mode.ccm, 0, sizeof c->u_mode.ccm);
1031 break;
1032
1033 case GCRY_CIPHER_MODE_OCB:
1034 {
1035 const size_t table_maxblks = 1 << OCB_L_TABLE_SIZE;
1036 byte *u_mode_head_pos = (void *)&c->u_mode.ocb;
1037 byte *u_mode_tail_pos = (void *)&c->u_mode.ocb.tag;
1038 size_t u_mode_head_length = u_mode_tail_pos - u_mode_head_pos;
1039 size_t u_mode_tail_length = sizeof(c->u_mode.ocb) - u_mode_head_length;
1040
1041 if (c->u_mode.ocb.aad_nblocks < table_maxblks)
1042 {
1043 /* Precalculated L-values are still ok after reset, no need
1044 * to clear. */
1045 memset (u_mode_tail_pos, 0, u_mode_tail_length);
1046 }
1047 else
1048 {
1049 /* Reinitialize L table. */
1050 memset (&c->u_mode.ocb, 0, sizeof(c->u_mode.ocb));
1051 _gcry_cipher_ocb_setkey (c);
1052 }
1053
1054 /* Setup default taglen. */
1055 c->u_mode.ocb.taglen = 16;
1056 }
1057 break;
1058
1059 case GCRY_CIPHER_MODE_XTS:
1060 memcpy (c->u_mode.xts.tweak_context,
1061 c->u_mode.xts.tweak_context + c->spec->contextsize,
1062 c->spec->contextsize);
1063 break;
1064
1065 case GCRY_CIPHER_MODE_SIV:
1066 /* Only clear head of u_mode, keep s2v_cmac and ctr_context. */
1067 {
1068 byte *u_mode_pos = (void *)&c->u_mode;
1069 byte *tail_pos = (void *)&c->u_mode.siv.s2v_cmac;
1070 size_t u_mode_head_length = tail_pos - u_mode_pos;
1071
1072 memset (&c->u_mode, 0, u_mode_head_length);
1073
1074 memcpy (c->u_mode.siv.ctr_context,
1075 c->u_mode.siv.ctr_context + c->spec->contextsize,
1076 c->spec->contextsize);
1077
1078 memcpy (c->u_mode.siv.s2v_d, c->u_mode.siv.s2v_zero_block,
1079 GCRY_SIV_BLOCK_LEN);
1080 }
1081 break;
1082
1083 default:
1084 break; /* u_mode unused by other modes. */
1085 }
1086 }
1087
1088
1089
1090 static gcry_err_code_t
1091 do_ecb_crypt (gcry_cipher_hd_t c, unsigned char *outbuf, size_t outbuflen,
1092 const unsigned char *inbuf, size_t inbuflen, int encrypt)
1093 {
1094 unsigned int blocksize = c->spec->blocksize;
1095 size_t n, nblocks;
1096
1097 if (outbuflen < inbuflen)
1098 return GPG_ERR_BUFFER_TOO_SHORT;
1099 if ((inbuflen % blocksize))
1100 return GPG_ERR_INV_LENGTH;
1101
1102 nblocks = inbuflen / blocksize;
1103
1104 if (nblocks == 0)
1105 return 0;
1106
1107 if (c->bulk.ecb_crypt)
1108 {
1109 c->bulk.ecb_crypt (&c->context.c, outbuf, inbuf, nblocks, encrypt);
1110 }
1111 else
1112 {
1113 gcry_cipher_encrypt_t crypt_fn =
1114 encrypt ? c->spec->encrypt : c->spec->decrypt;
1115 unsigned int burn = 0;
1116 unsigned int nburn;
1117
1118 for (n = 0; n < nblocks; n++)
1119 {
1120 nburn = crypt_fn (&c->context.c, outbuf, inbuf);
1121 burn = nburn > burn ? nburn : burn;
1122 inbuf += blocksize;
1123 outbuf += blocksize;
1124 }
1125
1126 if (burn > 0)
1127 _gcry_burn_stack (burn + 4 * sizeof(void *));
1128 }
1129
1130 return 0;
1131 }
1132
1133 static gcry_err_code_t
1134 do_ecb_encrypt (gcry_cipher_hd_t c,
1135 unsigned char *outbuf, size_t outbuflen,
1136 const unsigned char *inbuf, size_t inbuflen)
1137 {
1138 return do_ecb_crypt (c, outbuf, outbuflen, inbuf, inbuflen, 1);
1139 }
1140
1141 static gcry_err_code_t
1142 do_ecb_decrypt (gcry_cipher_hd_t c,
1143 unsigned char *outbuf, size_t outbuflen,
1144 const unsigned char *inbuf, size_t inbuflen)
1145 {
1146 return do_ecb_crypt (c, outbuf, outbuflen, inbuf, inbuflen, 0);
1147 }
1148
1149
1150 static gcry_err_code_t
1151 do_stream_encrypt (gcry_cipher_hd_t c,
1152 unsigned char *outbuf, size_t outbuflen,
1153 const unsigned char *inbuf, size_t inbuflen)
1154 {
1155 (void)outbuflen;
1156 c->spec->stencrypt (&c->context.c, outbuf, (void *)inbuf, inbuflen);
1157 return 0;
1158 }
1159
1160 static gcry_err_code_t
1161 do_stream_decrypt (gcry_cipher_hd_t c,
1162 unsigned char *outbuf, size_t outbuflen,
1163 const unsigned char *inbuf, size_t inbuflen)
1164 {
1165 (void)outbuflen;
1166 c->spec->stdecrypt (&c->context.c, outbuf, (void *)inbuf, inbuflen);
1167 return 0;
1168 }
1169
1170
1171 static gcry_err_code_t
1172 do_encrypt_none_unknown (gcry_cipher_hd_t c, byte *outbuf, size_t outbuflen,
1173 const byte *inbuf, size_t inbuflen)
1174 {
1175 gcry_err_code_t rc;
1176
1177 (void)outbuflen;
1178
1179 switch (c->mode)
1180 {
1181 case GCRY_CIPHER_MODE_CMAC:
1182 rc = GPG_ERR_INV_CIPHER_MODE;
1183 break;
1184
1185 case GCRY_CIPHER_MODE_NONE:
1186 if (fips_mode () || !_gcry_get_debug_flag (0))
1187 {
1188 fips_signal_error ("cipher mode NONE used");
1189 rc = GPG_ERR_INV_CIPHER_MODE;
1190 }
1191 else
1192 {
1193 if (inbuf != outbuf)
1194 memmove (outbuf, inbuf, inbuflen);
1195 rc = 0;
1196 }
1197 break;
1198
1199 default:
1200 log_fatal ("cipher_encrypt: invalid mode %d\n", c->mode );
1201 rc = GPG_ERR_INV_CIPHER_MODE;
1202 break;
1203 }
1204
1205 return rc;
1206 }
1207
1208 static gcry_err_code_t
1209 do_decrypt_none_unknown (gcry_cipher_hd_t c, byte *outbuf, size_t outbuflen,
1210 const byte *inbuf, size_t inbuflen)
1211 {
1212 gcry_err_code_t rc;
1213
1214 (void)outbuflen;
1215
1216 switch (c->mode)
1217 {
1218 case GCRY_CIPHER_MODE_CMAC:
1219 rc = GPG_ERR_INV_CIPHER_MODE;
1220 break;
1221
1222 case GCRY_CIPHER_MODE_NONE:
1223 if (fips_mode () || !_gcry_get_debug_flag (0))
1224 {
1225 fips_signal_error ("cipher mode NONE used");
1226 rc = GPG_ERR_INV_CIPHER_MODE;
1227 }
1228 else
1229 {
1230 if (inbuf != outbuf)
1231 memmove (outbuf, inbuf, inbuflen);
1232 rc = 0;
1233 }
1234 break;
1235
1236 default:
1237 log_fatal ("cipher_decrypt: invalid mode %d\n", c->mode );
1238 rc = GPG_ERR_INV_CIPHER_MODE;
1239 break;
1240 }
1241
1242 return rc;
1243 }
1244
1245
1246 /****************
1247 * Encrypt IN and write it to OUT. If IN is NULL, in-place encryption has
1248 * been requested.
1249 */
1250 gcry_err_code_t
1251 _gcry_cipher_encrypt (gcry_cipher_hd_t h, void *out, size_t outsize,
1252 const void *in, size_t inlen)
1253 {
1254 gcry_err_code_t rc;
1255
1256 if (!in) /* Caller requested in-place encryption. */
1257 {
1258 in = out;
1259 inlen = outsize;
1260 }
1261
1262 if (h->mode != GCRY_CIPHER_MODE_NONE && !h->marks.key)
1263 {
1264 log_error ("cipher_encrypt: key not set\n");
1265 return GPG_ERR_MISSING_KEY;
1266 }
1267
1268 rc = h->mode_ops.encrypt (h, out, outsize, in, inlen);
1269
1270 /* Failsafe: Make sure that the plaintext will never make it into
1271 OUT if the encryption returned an error. */
1272 if (rc && out)
1273 memset (out, 0x42, outsize);
1274
1275 return rc;
1276 }
1277
1278
1279 /****************
1280 * Decrypt IN and write it to OUT. If IN is NULL, in-place encryption has
1281 * been requested.
1282 */
1283 gcry_err_code_t
1284 _gcry_cipher_decrypt (gcry_cipher_hd_t h, void *out, size_t outsize,
1285 const void *in, size_t inlen)
1286 {
1287 if (!in) /* Caller requested in-place encryption. */
1288 {
1289 in = out;
1290 inlen = outsize;
1291 }
1292
1293 if (h->mode != GCRY_CIPHER_MODE_NONE && !h->marks.key)
1294 {
1295 log_error ("cipher_decrypt: key not set\n");
1296 return GPG_ERR_MISSING_KEY;
1297 }
1298
1299 return h->mode_ops.decrypt (h, out, outsize, in, inlen);
1300 }
1301
1302
1303 /****************
1304 * Used for PGP's somewhat strange CFB mode. Only works if
1305 * the corresponding flag is set.
1306 */
1307 static void
1308 cipher_sync (gcry_cipher_hd_t c)
1309 {
1310 if ((c->flags & GCRY_CIPHER_ENABLE_SYNC) && c->unused)
1311 {
1312 memmove (c->u_iv.iv + c->unused,
1313 c->u_iv.iv, c->spec->blocksize - c->unused);
1314 memcpy (c->u_iv.iv,
1315 c->lastiv + c->spec->blocksize - c->unused, c->unused);
1316 c->unused = 0;
1317 }
1318 }
1319
1320
1321 gcry_err_code_t
1322 _gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen)
1323 {
1324 return cipher_setkey (hd, (void*)key, keylen);
1325 }
1326
1327
1328 gcry_err_code_t
1329 _gcry_cipher_setiv (gcry_cipher_hd_t c, const void *iv, size_t ivlen)
1330 {
1331 if (c->mode == GCRY_CIPHER_MODE_GCM)
1332 {
1333 c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 0;
1334
1335 if (fips_mode ())
1336 {
1337 /* Direct invocation of GCM setiv in FIPS mode disables encryption. */
1338 c->u_mode.gcm.disallow_encryption_because_of_setiv_in_fips_mode = 1;
1339 }
1340 }
1341
1342 return c->mode_ops.setiv (c, iv, ivlen);
1343 }
1344
1345
1346 /* Set counter for CTR mode. (CTR,CTRLEN) must denote a buffer of
1347 block size length, or (NULL,0) to set the CTR to the all-zero
1348 block. */
1349 gpg_err_code_t
1350 _gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen)
1351 {
1352 if (ctr && ctrlen == hd->spec->blocksize)
1353 {
1354 memcpy (hd->u_ctr.ctr, ctr, hd->spec->blocksize);
1355 hd->unused = 0;
1356 }
1357 else if (!ctr || !ctrlen)
1358 {
1359 memset (hd->u_ctr.ctr, 0, hd->spec->blocksize);
1360 hd->unused = 0;
1361 }
1362 else
1363 return GPG_ERR_INV_ARG;
1364
1365 return 0;
1366 }
1367
1368 gpg_err_code_t
1369 _gcry_cipher_getctr (gcry_cipher_hd_t hd, void *ctr, size_t ctrlen)
1370 {
1371 if (ctr && ctrlen == hd->spec->blocksize)
1372 memcpy (ctr, hd->u_ctr.ctr, hd->spec->blocksize);
1373 else
1374 return GPG_ERR_INV_ARG;
1375
1376 return 0;
1377 }
1378
1379
1380 gcry_err_code_t
1381 _gcry_cipher_setup_geniv (gcry_cipher_hd_t hd, int method,
1382 const void *fixed_iv, size_t fixed_iv_len,
1383 const void *dyn_iv, size_t dyn_iv_len)
1384 {
1385 gcry_err_code_t rc = 0;
1386
1387 if (method != GCRY_CIPHER_GENIV_METHOD_CONCAT)
1388 return GPG_ERR_INV_ARG;
1389
1390 if (fixed_iv_len + dyn_iv_len > MAX_BLOCKSIZE)
1391 return GPG_ERR_INV_ARG;
1392
1393 hd->aead.geniv_method = GCRY_CIPHER_GENIV_METHOD_CONCAT;
1394 hd->aead.fixed_iv_len = fixed_iv_len;
1395 hd->aead.dynamic_iv_len = dyn_iv_len;
1396 memset (hd->aead.fixed, 0, MAX_BLOCKSIZE);
1397 memset (hd->aead.dynamic, 0, MAX_BLOCKSIZE);
1398 memcpy (hd->aead.fixed, fixed_iv, fixed_iv_len);
1399 memcpy (hd->aead.dynamic, dyn_iv, dyn_iv_len);
1400
1401 return rc;
1402 }
1403
1404
1405 gcry_err_code_t
1406 _gcry_cipher_geniv (gcry_cipher_hd_t hd, void *iv, size_t iv_len)
1407 {
1408 gcry_err_code_t rc = 0;
1409 int i;
1410
1411 if (hd->aead.geniv_method != GCRY_CIPHER_GENIV_METHOD_CONCAT)
1412 return GPG_ERR_INV_ARG;
1413
1414 if (iv_len != hd->aead.fixed_iv_len + hd->aead.dynamic_iv_len)
1415 return GPG_ERR_INV_ARG;
1416
1417 memcpy (iv, hd->aead.fixed, hd->aead.fixed_iv_len);
1418 memcpy ((byte *)iv+hd->aead.fixed_iv_len,
1419 hd->aead.dynamic, hd->aead.dynamic_iv_len);
1420 rc = hd->mode_ops.setiv (hd, iv, iv_len);
1421
1422 for (i = hd->aead.dynamic_iv_len; i > 0; i--)
1423 if (++hd->aead.dynamic[i - 1] != 0)
1424 break;
1425
1426 return rc;
1427 }
1428
1429
1430 gcry_err_code_t
1431 _gcry_cipher_authenticate (gcry_cipher_hd_t hd, const void *abuf,
1432 size_t abuflen)
1433 {
1434 gcry_err_code_t rc;
1435
1436 if (hd->mode_ops.authenticate)
1437 {
1438 rc = hd->mode_ops.authenticate (hd, abuf, abuflen);
1439 }
1440 else
1441 {
1442 log_error ("gcry_cipher_authenticate: invalid mode %d\n", hd->mode);
1443 rc = GPG_ERR_INV_CIPHER_MODE;
1444 }
1445
1446 return rc;
1447 }
1448
1449
1450 gcry_err_code_t
1451 _gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag, size_t taglen)
1452 {
1453 gcry_err_code_t rc;
1454
1455 if (hd->mode_ops.get_tag)
1456 {
1457 rc = hd->mode_ops.get_tag (hd, outtag, taglen);
1458 }
1459 else
1460 {
1461 log_error ("gcry_cipher_gettag: invalid mode %d\n", hd->mode);
1462 rc = GPG_ERR_INV_CIPHER_MODE;
1463 }
1464
1465 return rc;
1466 }
1467
1468
1469 gcry_err_code_t
1470 _gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag, size_t taglen)
1471 {
1472 gcry_err_code_t rc;
1473
1474 if (hd->mode_ops.check_tag)
1475 {
1476 rc = hd->mode_ops.check_tag (hd, intag, taglen);
1477 }
1478 else
1479 {
1480 log_error ("gcry_cipher_checktag: invalid mode %d\n", hd->mode);
1481 rc = GPG_ERR_INV_CIPHER_MODE;
1482 }
1483
1484 return rc;
1485 }
1486
1487
1488
1489 static void
1490 _gcry_cipher_setup_mode_ops(gcry_cipher_hd_t c, int mode)
1491 {
1492 /* Setup encryption and decryption routines. */
1493 switch (mode)
1494 {
1495 case GCRY_CIPHER_MODE_STREAM:
1496 c->mode_ops.encrypt = do_stream_encrypt;
1497 c->mode_ops.decrypt = do_stream_decrypt;
1498 break;
1499
1500 case GCRY_CIPHER_MODE_ECB:
1501 c->mode_ops.encrypt = do_ecb_encrypt;
1502 c->mode_ops.decrypt = do_ecb_decrypt;
1503 break;
1504
1505 case GCRY_CIPHER_MODE_CBC:
1506 if (!(c->flags & GCRY_CIPHER_CBC_CTS))
1507 {
1508 c->mode_ops.encrypt = _gcry_cipher_cbc_encrypt;
1509 c->mode_ops.decrypt = _gcry_cipher_cbc_decrypt;
1510 }
1511 else
1512 {
1513 c->mode_ops.encrypt = _gcry_cipher_cbc_cts_encrypt;
1514 c->mode_ops.decrypt = _gcry_cipher_cbc_cts_decrypt;
1515 }
1516 break;
1517
1518 case GCRY_CIPHER_MODE_CFB:
1519 c->mode_ops.encrypt = _gcry_cipher_cfb_encrypt;
1520 c->mode_ops.decrypt = _gcry_cipher_cfb_decrypt;
1521 break;
1522
1523 case GCRY_CIPHER_MODE_CFB8:
1524 c->mode_ops.encrypt = _gcry_cipher_cfb8_encrypt;
1525 c->mode_ops.decrypt = _gcry_cipher_cfb8_decrypt;
1526 break;
1527
1528 case GCRY_CIPHER_MODE_OFB:
1529 c->mode_ops.encrypt = _gcry_cipher_ofb_encrypt;
1530 c->mode_ops.decrypt = _gcry_cipher_ofb_encrypt;
1531 break;
1532
1533 case GCRY_CIPHER_MODE_CTR:
1534 c->mode_ops.encrypt = _gcry_cipher_ctr_encrypt;
1535 c->mode_ops.decrypt = _gcry_cipher_ctr_encrypt;
1536 break;
1537
1538 case GCRY_CIPHER_MODE_AESWRAP:
1539 c->mode_ops.decrypt = _gcry_cipher_keywrap_decrypt_auto;
1540 if (!(c->flags & GCRY_CIPHER_EXTENDED))
1541 c->mode_ops.encrypt = _gcry_cipher_keywrap_encrypt;
1542 else
1543 c->mode_ops.encrypt = _gcry_cipher_keywrap_encrypt_padding;
1544 break;
1545
1546 case GCRY_CIPHER_MODE_CCM:
1547 c->mode_ops.encrypt = _gcry_cipher_ccm_encrypt;
1548 c->mode_ops.decrypt = _gcry_cipher_ccm_decrypt;
1549 break;
1550
1551 case GCRY_CIPHER_MODE_EAX:
1552 c->mode_ops.encrypt = _gcry_cipher_eax_encrypt;
1553 c->mode_ops.decrypt = _gcry_cipher_eax_decrypt;
1554 break;
1555
1556 case GCRY_CIPHER_MODE_GCM:
1557 c->mode_ops.encrypt = _gcry_cipher_gcm_encrypt;
1558 c->mode_ops.decrypt = _gcry_cipher_gcm_decrypt;
1559 break;
1560
1561 case GCRY_CIPHER_MODE_POLY1305:
1562 c->mode_ops.encrypt = _gcry_cipher_poly1305_encrypt;
1563 c->mode_ops.decrypt = _gcry_cipher_poly1305_decrypt;
1564 break;
1565
1566 case GCRY_CIPHER_MODE_OCB:
1567 c->mode_ops.encrypt = _gcry_cipher_ocb_encrypt;
1568 c->mode_ops.decrypt = _gcry_cipher_ocb_decrypt;
1569 break;
1570
1571 case GCRY_CIPHER_MODE_XTS:
1572 c->mode_ops.encrypt = _gcry_cipher_xts_encrypt;
1573 c->mode_ops.decrypt = _gcry_cipher_xts_decrypt;
1574 break;
1575
1576 case GCRY_CIPHER_MODE_SIV:
1577 c->mode_ops.encrypt = _gcry_cipher_siv_encrypt;
1578 c->mode_ops.decrypt = _gcry_cipher_siv_decrypt;
1579 break;
1580
1581 case GCRY_CIPHER_MODE_GCM_SIV:
1582 c->mode_ops.encrypt = _gcry_cipher_gcm_siv_encrypt;
1583 c->mode_ops.decrypt = _gcry_cipher_gcm_siv_decrypt;
1584 break;
1585
1586 default:
1587 c->mode_ops.encrypt = do_encrypt_none_unknown;
1588 c->mode_ops.decrypt = do_decrypt_none_unknown;
1589 break;
1590 }
1591
1592 /* Setup IV setting routine. */
1593 switch (mode)
1594 {
1595 case GCRY_CIPHER_MODE_CCM:
1596 c->mode_ops.setiv = _gcry_cipher_ccm_set_nonce;
1597 break;
1598
1599 case GCRY_CIPHER_MODE_EAX:
1600 c->mode_ops.setiv = _gcry_cipher_eax_set_nonce;
1601 break;
1602
1603 case GCRY_CIPHER_MODE_GCM:
1604 c->mode_ops.setiv = _gcry_cipher_gcm_setiv;
1605 break;
1606
1607 case GCRY_CIPHER_MODE_POLY1305:
1608 c->mode_ops.setiv = _gcry_cipher_poly1305_setiv;
1609 break;
1610
1611 case GCRY_CIPHER_MODE_OCB:
1612 c->mode_ops.setiv = _gcry_cipher_ocb_set_nonce;
1613 break;
1614
1615 case GCRY_CIPHER_MODE_SIV:
1616 c->mode_ops.setiv = _gcry_cipher_siv_set_nonce;
1617 break;
1618
1619 case GCRY_CIPHER_MODE_GCM_SIV:
1620 c->mode_ops.setiv = _gcry_cipher_gcm_siv_set_nonce;
1621 break;
1622
1623 default:
1624 c->mode_ops.setiv = cipher_setiv;
1625 break;
1626 }
1627
1628
1629 /* Setup authentication routines for AEAD modes. */
1630 switch (mode)
1631 {
1632 case GCRY_CIPHER_MODE_CCM:
1633 c->mode_ops.authenticate = _gcry_cipher_ccm_authenticate;
1634 c->mode_ops.get_tag = _gcry_cipher_ccm_get_tag;
1635 c->mode_ops.check_tag = _gcry_cipher_ccm_check_tag;
1636 break;
1637
1638 case GCRY_CIPHER_MODE_CMAC:
1639 c->mode_ops.authenticate = _gcry_cipher_cmac_authenticate;
1640 c->mode_ops.get_tag = _gcry_cipher_cmac_get_tag;
1641 c->mode_ops.check_tag = _gcry_cipher_cmac_check_tag;
1642 break;
1643
1644 case GCRY_CIPHER_MODE_EAX:
1645 c->mode_ops.authenticate = _gcry_cipher_eax_authenticate;
1646 c->mode_ops.get_tag = _gcry_cipher_eax_get_tag;
1647 c->mode_ops.check_tag = _gcry_cipher_eax_check_tag;
1648 break;
1649
1650 case GCRY_CIPHER_MODE_GCM:
1651 c->mode_ops.authenticate = _gcry_cipher_gcm_authenticate;
1652 c->mode_ops.get_tag = _gcry_cipher_gcm_get_tag;
1653 c->mode_ops.check_tag = _gcry_cipher_gcm_check_tag;
1654 break;
1655
1656 case GCRY_CIPHER_MODE_POLY1305:
1657 c->mode_ops.authenticate = _gcry_cipher_poly1305_authenticate;
1658 c->mode_ops.get_tag = _gcry_cipher_poly1305_get_tag;
1659 c->mode_ops.check_tag = _gcry_cipher_poly1305_check_tag;
1660 break;
1661
1662 case GCRY_CIPHER_MODE_OCB:
1663 c->mode_ops.authenticate = _gcry_cipher_ocb_authenticate;
1664 c->mode_ops.get_tag = _gcry_cipher_ocb_get_tag;
1665 c->mode_ops.check_tag = _gcry_cipher_ocb_check_tag;
1666 break;
1667
1668 case GCRY_CIPHER_MODE_SIV:
1669 c->mode_ops.authenticate = _gcry_cipher_siv_authenticate;
1670 c->mode_ops.get_tag = _gcry_cipher_siv_get_tag;
1671 c->mode_ops.check_tag = _gcry_cipher_siv_check_tag;
1672 break;
1673
1674 case GCRY_CIPHER_MODE_GCM_SIV:
1675 c->mode_ops.authenticate = _gcry_cipher_gcm_siv_authenticate;
1676 c->mode_ops.get_tag = _gcry_cipher_gcm_siv_get_tag;
1677 c->mode_ops.check_tag = _gcry_cipher_gcm_siv_check_tag;
1678 break;
1679
1680 default:
1681 c->mode_ops.authenticate = NULL;
1682 c->mode_ops.get_tag = NULL;
1683 c->mode_ops.check_tag = NULL;
1684 break;
1685 }
1686 }
1687
1688
1689 gcry_err_code_t
1690 _gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
1691 {
1692 gcry_err_code_t rc = 0;
1693
1694 switch (cmd)
1695 {
1696 case GCRYCTL_RESET:
1697 cipher_reset (h);
1698 break;
1699
1700 case GCRYCTL_FINALIZE:
1701 if (!h || buffer || buflen)
1702 return GPG_ERR_INV_ARG;
1703 h->marks.finalize = 1;
1704 break;
1705
1706 case GCRYCTL_CFB_SYNC:
1707 cipher_sync( h );
1708 break;
1709
1710 case GCRYCTL_SET_CBC_CTS:
1711 if (buflen)
1712 if (h->flags & GCRY_CIPHER_CBC_MAC)
1713 rc = GPG_ERR_INV_FLAG;
1714 else
1715 h->flags |= GCRY_CIPHER_CBC_CTS;
1716 else
1717 h->flags &= ~GCRY_CIPHER_CBC_CTS;
1718 break;
1719
1720 case GCRYCTL_SET_CBC_MAC:
1721 if (buflen)
1722 if (h->flags & GCRY_CIPHER_CBC_CTS)
1723 rc = GPG_ERR_INV_FLAG;
1724 else
1725 h->flags |= GCRY_CIPHER_CBC_MAC;
1726 else
1727 h->flags &= ~GCRY_CIPHER_CBC_MAC;
1728 break;
1729
1730 case GCRYCTL_SET_CCM_LENGTHS:
1731 {
1732 u64 params[3];
1733 size_t encryptedlen;
1734 size_t aadlen;
1735 size_t authtaglen;
1736
1737 if (h->mode != GCRY_CIPHER_MODE_CCM)
1738 return GPG_ERR_INV_CIPHER_MODE;
1739
1740 if (!buffer || buflen != 3 * sizeof(u64))
1741 return GPG_ERR_INV_ARG;
1742
1743 /* This command is used to pass additional length parameters needed
1744 by CCM mode to initialize CBC-MAC. */
1745 memcpy (params, buffer, sizeof(params));
1746 encryptedlen = params[0];
1747 aadlen = params[1];
1748 authtaglen = params[2];
1749
1750 rc = _gcry_cipher_ccm_set_lengths (h, encryptedlen, aadlen, authtaglen);
1751 }
1752 break;
1753
1754 case GCRYCTL_SET_DECRYPTION_TAG:
1755 {
1756 if (!buffer)
1757 return GPG_ERR_INV_ARG;
1758
1759 if (h->mode == GCRY_CIPHER_MODE_SIV)
1760 rc = _gcry_cipher_siv_set_decryption_tag (h, buffer, buflen);
1761 else if (h->mode == GCRY_CIPHER_MODE_GCM_SIV)
1762 rc = _gcry_cipher_gcm_siv_set_decryption_tag (h, buffer, buflen);
1763 else
1764 rc = GPG_ERR_INV_CIPHER_MODE;
1765 }
1766 break;
1767
1768 case GCRYCTL_SET_TAGLEN:
1769 if (!h || !buffer || buflen != sizeof(int) )
1770 return GPG_ERR_INV_ARG;
1771 switch (h->mode)
1772 {
1773 case GCRY_CIPHER_MODE_OCB:
1774 switch (*(int*)buffer)
1775 {
1776 case 8: case 12: case 16:
1777 h->u_mode.ocb.taglen = *(int*)buffer;
1778 break;
1779 default:
1780 rc = GPG_ERR_INV_LENGTH; /* Invalid tag length. */
1781 break;
1782 }
1783 break;
1784
1785 default:
1786 rc =GPG_ERR_INV_CIPHER_MODE;
1787 break;
1788 }
1789 break;
1790
1791 case GCRYCTL_DISABLE_ALGO:
1792 /* This command expects NULL for H and BUFFER to point to an
1793 integer with the algo number. */
1794 if( h || !buffer || buflen != sizeof(int) )
1795 return GPG_ERR_CIPHER_ALGO;
1796 disable_cipher_algo( *(int*)buffer );
1797 break;
1798
1799 case PRIV_CIPHERCTL_DISABLE_WEAK_KEY: /* (private) */
1800 if (h->spec->set_extra_info)
1801 rc = h->spec->set_extra_info
1802 (&h->context.c, CIPHER_INFO_NO_WEAK_KEY, NULL, 0);
1803 else
1804 rc = GPG_ERR_NOT_SUPPORTED;
1805 break;
1806
1807 case PRIV_CIPHERCTL_GET_INPUT_VECTOR: /* (private) */
1808 /* This is the input block as used in CFB and OFB mode which has
1809 initially been set as IV. The returned format is:
1810 1 byte Actual length of the block in bytes.
1811 n byte The block.
1812 If the provided buffer is too short, an error is returned. */
1813 if (buflen < (1 + h->spec->blocksize))
1814 rc = GPG_ERR_TOO_SHORT;
1815 else
1816 {
1817 unsigned char *ivp;
1818 unsigned char *dst = buffer;
1819 int n = h->unused;
1820
1821 if (!n)
1822 n = h->spec->blocksize;
1823 gcry_assert (n <= h->spec->blocksize);
1824 *dst++ = n;
1825 ivp = h->u_iv.iv + h->spec->blocksize - n;
1826 while (n--)
1827 *dst++ = *ivp++;
1828 }
1829 break;
1830
1831 case PRIV_CIPHERCTL_GET_COUNTER: /* (private) */
1832 /* This is the input block as used in CTR mode which has
1833 initially been set as IV. The returned format is:
1834 1 byte Actual length of the block in bytes.
1835 n byte The block.
1836 If the provided buffer is too short, an error is returned. */
1837 if (buflen < (1 + h->spec->blocksize))
1838 rc = GPG_ERR_TOO_SHORT;
1839 else
1840 {
1841 unsigned char *ctrp;
1842 unsigned char *dst = buffer;
1843 int n = h->unused;
1844
1845 if (!n)
1846 n = h->spec->blocksize;
1847 gcry_assert (n <= h->spec->blocksize);
1848 *dst++ = n;
1849 ctrp = h->u_ctr.ctr + h->spec->blocksize - n;
1850 while (n--)
1851 *dst++ = *ctrp++;
1852 }
1853 break;
1854
1855 case GCRYCTL_SET_SBOX:
1856 if (h->spec->set_extra_info)
1857 rc = h->spec->set_extra_info
1858 (&h->context.c, GCRYCTL_SET_SBOX, buffer, buflen);
1859 else
1860 rc = GPG_ERR_NOT_SUPPORTED;
1861 break;
1862
1863 case GCRYCTL_SET_ALLOW_WEAK_KEY:
1864 /* Expecting BUFFER to be NULL and buflen to be on/off flag (0 or 1). */
1865 if (!h || buffer || buflen > 1)
1866 return GPG_ERR_CIPHER_ALGO;
1867 h->marks.allow_weak_key = buflen ? 1 : 0;
1868 break;
1869
1870 default:
1871 rc = GPG_ERR_INV_OP;
1872 }
1873
1874 return rc;
1875 }
1876
1877
1878 /* Return information about the cipher handle H. CMD is the kind of
1879 * information requested.
1880 *
1881 * CMD may be one of:
1882 *
1883 * GCRYCTL_GET_TAGLEN:
1884 * Return the length of the tag for an AE algorithm mode. An
1885 * error is returned for modes which do not support a tag.
1886 * BUFFER must be given as NULL. On success the result is stored
1887 * at NBYTES. The taglen is returned in bytes.
1888 *
1889 * GCRYCTL_GET_KEYLEN:
1890 * Return the length of the key wrapped for AES-WRAP mode. The
1891 * length is encoded in big-endian 4 bytes, when the key is
1892 * unwrapped with KWP. Return 00 00 00 00, when the key is
1893 * unwrapped with KW.
1894 *
1895 * The function returns 0 on success or an error code.
1896 */
1897 gcry_err_code_t
1898 _gcry_cipher_info (gcry_cipher_hd_t h, int cmd, void *buffer, size_t *nbytes)
1899 {
1900 gcry_err_code_t rc = 0;
1901
1902 switch (cmd)
1903 {
1904 case GCRYCTL_GET_TAGLEN:
1905 if (!h || buffer || !nbytes)
1906 rc = GPG_ERR_INV_ARG;
1907 else
1908 {
1909 switch (h->mode)
1910 {
1911 case GCRY_CIPHER_MODE_OCB:
1912 *nbytes = h->u_mode.ocb.taglen;
1913 break;
1914
1915 case GCRY_CIPHER_MODE_CCM:
1916 *nbytes = h->u_mode.ccm.authlen;
1917 break;
1918
1919 case GCRY_CIPHER_MODE_EAX:
1920 *nbytes = h->spec->blocksize;
1921 break;
1922
1923 case GCRY_CIPHER_MODE_GCM:
1924 *nbytes = GCRY_GCM_BLOCK_LEN;
1925 break;
1926
1927 case GCRY_CIPHER_MODE_POLY1305:
1928 *nbytes = POLY1305_TAGLEN;
1929 break;
1930
1931 case GCRY_CIPHER_MODE_SIV:
1932 *nbytes = GCRY_SIV_BLOCK_LEN;
1933 break;
1934
1935 case GCRY_CIPHER_MODE_GCM_SIV:
1936 *nbytes = GCRY_SIV_BLOCK_LEN;
1937 break;
1938
1939 default:
1940 rc = GPG_ERR_INV_CIPHER_MODE;
1941 break;
1942 }
1943 }
1944 break;
1945
1946 case GCRYCTL_GET_KEYLEN:
1947 if (!h || !buffer || !nbytes)
1948 rc = GPG_ERR_INV_ARG;
1949 else
1950 {
1951 switch (h->mode)
1952 {
1953 case GCRY_CIPHER_MODE_AESWRAP:
1954 *nbytes = 4;
1955 memcpy (buffer, h->u_mode.wrap.plen, 4);
1956 break;
1957
1958 default:
1959 rc = GPG_ERR_INV_CIPHER_MODE;
1960 break;
1961 }
1962 }
1963 break;
1964
1965 default:
1966 rc = GPG_ERR_INV_OP;
1967 }
1968
1969 return rc;
1970 }
1971
1972 /* Return information about the given cipher algorithm ALGO.
1973
1974 WHAT select the kind of information returned:
1975
1976 GCRYCTL_GET_KEYLEN:
1977 Return the length of the key. If the algorithm ALGO
1978 supports multiple key lengths, the maximum supported key length
1979 is returned. The key length is returned as number of octets.
1980 BUFFER and NBYTES must be zero.
1981
1982 GCRYCTL_GET_BLKLEN:
1983 Return the blocklength of the algorithm ALGO counted in octets.
1984 BUFFER and NBYTES must be zero.
1985
1986 GCRYCTL_TEST_ALGO:
1987 Returns 0 if the specified algorithm ALGO is available for use.
1988 BUFFER and NBYTES must be zero.
1989
1990 Note: Because this function is in most cases used to return an
1991 integer value, we can make it easier for the caller to just look at
1992 the return value. The caller will in all cases consult the value
1993 and thereby detecting whether a error occurred or not (i.e. while
1994 checking the block size)
1995 */
1996 gcry_err_code_t
1997 _gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes)
1998 {
1999 gcry_err_code_t rc = 0;
2000 unsigned int ui;
2001
2002 switch (what)
2003 {
2004 case GCRYCTL_GET_KEYLEN:
2005 if (buffer || (! nbytes))
2006 rc = GPG_ERR_CIPHER_ALGO;
2007 else
2008 {
2009 ui = cipher_get_keylen (algo);
2010 if ((ui > 0) && (ui <= 512))
2011 *nbytes = (size_t) ui / 8;
2012 else
2013 /* The only reason for an error is an invalid algo. */
2014 rc = GPG_ERR_CIPHER_ALGO;
2015 }
2016 break;
2017
2018 case GCRYCTL_GET_BLKLEN:
2019 if (buffer || (! nbytes))
2020 rc = GPG_ERR_CIPHER_ALGO;
2021 else
2022 {
2023 ui = cipher_get_blocksize (algo);
2024 if ((ui > 0) && (ui < 10000))
2025 *nbytes = ui;
2026 else
2027 {
2028 /* The only reason is an invalid algo or a strange
2029 blocksize. */
2030 rc = GPG_ERR_CIPHER_ALGO;
2031 }
2032 }
2033 break;
2034
2035 case GCRYCTL_TEST_ALGO:
2036 if (buffer || nbytes)
2037 rc = GPG_ERR_INV_ARG;
2038 else
2039 rc = check_cipher_algo (algo);
2040 break;
2041
2042 default:
2043 rc = GPG_ERR_INV_OP;
2044 }
2045
2046 return rc;
2047 }
2048
2049
2050 /* This function returns length of the key for algorithm ALGO. If the
2051 algorithm supports multiple key lengths, the maximum supported key
2052 length is returned. On error 0 is returned. The key length is
2053 returned as number of octets.
2054
2055 This is a convenience functions which should be preferred over
2056 gcry_cipher_algo_info because it allows for proper type
2057 checking. */
2058 size_t
2059 _gcry_cipher_get_algo_keylen (int algo)
2060 {
2061 size_t n;
2062
2063 if (_gcry_cipher_algo_info (algo, GCRYCTL_GET_KEYLEN, NULL, &n))
2064 n = 0;
2065 return n;
2066 }
2067
2068
2069 /* This functions returns the blocklength of the algorithm ALGO
2070 counted in octets. On error 0 is returned.
2071
2072 This is a convenience functions which should be preferred over
2073 gcry_cipher_algo_info because it allows for proper type
2074 checking. */
2075 size_t
2076 _gcry_cipher_get_algo_blklen (int algo)
2077 {
2078 size_t n;
2079
2080 if (_gcry_cipher_algo_info( algo, GCRYCTL_GET_BLKLEN, NULL, &n))
2081 n = 0;
2082 return n;
2083 }
2084
2085
2086 /* Explicitly initialize this module. */
2087 gcry_err_code_t
2088 _gcry_cipher_init (void)
2089 {
2090 return 0;
2091 }
2092
2093
2094 /* Run the selftests for cipher algorithm ALGO with optional reporting
2095 function REPORT. */
2096 gpg_error_t
2097 _gcry_cipher_selftest (int algo, int extended, selftest_report_func_t report)
2098 {
2099 gcry_err_code_t ec = 0;
2100 gcry_cipher_spec_t *spec;
2101
2102 spec = spec_from_algo (algo);
2103 if (spec && !spec->flags.disabled
2104 && (spec->flags.fips || !fips_mode ())
2105 && spec->selftest)
2106 ec = spec->selftest (algo, extended, report);
2107 else
2108 {
2109 ec = GPG_ERR_CIPHER_ALGO;
2110 if (report)
2111 report ("cipher", algo, "module",
2112 spec && !spec->flags.disabled
2113 && (spec->flags.fips || !fips_mode ())?
2114 "no selftest available" :
2115 spec? "algorithm disabled" : "algorithm not found");
2116 }
2117
2118 return gpg_error (ec);
2119 }