Debian Codegraph @

cipher/md.c

raw
1 /* md.c - message digest dispatcher
2 * Copyright (C) 1998, 1999, 2002, 2003, 2006,
3 * 2008 Free Software Foundation, Inc.
4 * Copyright (C) 2013, 2014 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 "cipher.h"
30
31
32 /* This is the list of the digest implementations included in
33 libgcrypt. */
34 static const gcry_md_spec_t * const digest_list[] =
35 {
36 #if USE_CRC
37 &_gcry_digest_spec_crc32,
38 &_gcry_digest_spec_crc32_rfc1510,
39 &_gcry_digest_spec_crc24_rfc2440,
40 #endif
41 #if USE_SHA1
42 &_gcry_digest_spec_sha1,
43 #endif
44 #if USE_SHA256
45 &_gcry_digest_spec_sha256,
46 &_gcry_digest_spec_sha224,
47 #endif
48 #if USE_SHA512
49 &_gcry_digest_spec_sha512,
50 &_gcry_digest_spec_sha384,
51 &_gcry_digest_spec_sha512_256,
52 &_gcry_digest_spec_sha512_224,
53 #endif
54 #if USE_SHA3
55 &_gcry_digest_spec_sha3_224,
56 &_gcry_digest_spec_sha3_256,
57 &_gcry_digest_spec_sha3_384,
58 &_gcry_digest_spec_sha3_512,
59 &_gcry_digest_spec_shake128,
60 &_gcry_digest_spec_shake256,
61 &_gcry_digest_spec_cshake128,
62 &_gcry_digest_spec_cshake256,
63 #endif
64 #if USE_GOST_R_3411_94
65 &_gcry_digest_spec_gost3411_94,
66 &_gcry_digest_spec_gost3411_cp,
67 #endif
68 #if USE_GOST_R_3411_12
69 &_gcry_digest_spec_stribog_256,
70 &_gcry_digest_spec_stribog_512,
71 #endif
72 #if USE_WHIRLPOOL
73 &_gcry_digest_spec_whirlpool,
74 #endif
75 #if USE_RMD160
76 &_gcry_digest_spec_rmd160,
77 #endif
78 #if USE_TIGER
79 &_gcry_digest_spec_tiger,
80 &_gcry_digest_spec_tiger1,
81 &_gcry_digest_spec_tiger2,
82 #endif
83 #if USE_MD5
84 &_gcry_digest_spec_md5,
85 #endif
86 #if USE_MD4
87 &_gcry_digest_spec_md4,
88 #endif
89 #if USE_MD2
90 &_gcry_digest_spec_md2,
91 #endif
92 #if USE_BLAKE2
93 &_gcry_digest_spec_blake2b_512,
94 &_gcry_digest_spec_blake2b_384,
95 &_gcry_digest_spec_blake2b_256,
96 &_gcry_digest_spec_blake2b_160,
97 &_gcry_digest_spec_blake2s_256,
98 &_gcry_digest_spec_blake2s_224,
99 &_gcry_digest_spec_blake2s_160,
100 &_gcry_digest_spec_blake2s_128,
101 #endif
102 #if USE_SM3
103 &_gcry_digest_spec_sm3,
104 #endif
105 NULL
106 };
107
108 /* Digest implementations starting with index 0 (enum gcry_md_algos) */
109 static const gcry_md_spec_t * const digest_list_algo0[] =
110 {
111 NULL, /* GCRY_MD_NONE */
112 #if USE_MD5
113 &_gcry_digest_spec_md5,
114 #else
115 NULL,
116 #endif
117 #if USE_SHA1
118 &_gcry_digest_spec_sha1,
119 #else
120 NULL,
121 #endif
122 #if USE_RMD160
123 &_gcry_digest_spec_rmd160,
124 #else
125 NULL,
126 #endif
127 NULL, /* Unused index 4 */
128 #if USE_MD2
129 &_gcry_digest_spec_md2,
130 #else
131 NULL,
132 #endif
133 #if USE_TIGER
134 &_gcry_digest_spec_tiger,
135 #else
136 NULL,
137 #endif
138 NULL, /* GCRY_MD_HAVAL */
139 #if USE_SHA256
140 &_gcry_digest_spec_sha256,
141 #else
142 NULL,
143 #endif
144 #if USE_SHA512
145 &_gcry_digest_spec_sha384,
146 &_gcry_digest_spec_sha512,
147 #else
148 NULL,
149 NULL,
150 #endif
151 #if USE_SHA256
152 &_gcry_digest_spec_sha224
153 #else
154 NULL
155 #endif
156 };
157
158 /* Digest implementations starting with index 301 (enum gcry_md_algos) */
159 static const gcry_md_spec_t * const digest_list_algo301[] =
160 {
161 #if USE_MD4
162 &_gcry_digest_spec_md4,
163 #else
164 NULL,
165 #endif
166 #if USE_CRC
167 &_gcry_digest_spec_crc32,
168 &_gcry_digest_spec_crc32_rfc1510,
169 &_gcry_digest_spec_crc24_rfc2440,
170 #else
171 NULL,
172 NULL,
173 NULL,
174 #endif
175 #if USE_WHIRLPOOL
176 &_gcry_digest_spec_whirlpool,
177 #else
178 NULL,
179 #endif
180 #if USE_TIGER
181 &_gcry_digest_spec_tiger1,
182 &_gcry_digest_spec_tiger2,
183 #else
184 NULL,
185 NULL,
186 #endif
187 #if USE_GOST_R_3411_94
188 &_gcry_digest_spec_gost3411_94,
189 #else
190 NULL,
191 #endif
192 #if USE_GOST_R_3411_12
193 &_gcry_digest_spec_stribog_256,
194 &_gcry_digest_spec_stribog_512,
195 #else
196 NULL,
197 NULL,
198 #endif
199 #if USE_GOST_R_3411_94
200 &_gcry_digest_spec_gost3411_cp,
201 #else
202 NULL,
203 #endif
204 #if USE_SHA3
205 &_gcry_digest_spec_sha3_224,
206 &_gcry_digest_spec_sha3_256,
207 &_gcry_digest_spec_sha3_384,
208 &_gcry_digest_spec_sha3_512,
209 &_gcry_digest_spec_shake128,
210 &_gcry_digest_spec_shake256,
211 #else
212 NULL,
213 NULL,
214 NULL,
215 NULL,
216 NULL,
217 NULL,
218 #endif
219 #if USE_BLAKE2
220 &_gcry_digest_spec_blake2b_512,
221 &_gcry_digest_spec_blake2b_384,
222 &_gcry_digest_spec_blake2b_256,
223 &_gcry_digest_spec_blake2b_160,
224 &_gcry_digest_spec_blake2s_256,
225 &_gcry_digest_spec_blake2s_224,
226 &_gcry_digest_spec_blake2s_160,
227 &_gcry_digest_spec_blake2s_128,
228 #else
229 NULL,
230 NULL,
231 NULL,
232 NULL,
233 NULL,
234 NULL,
235 NULL,
236 NULL,
237 #endif
238 #if USE_SM3
239 &_gcry_digest_spec_sm3,
240 #else
241 NULL,
242 #endif
243 #if USE_SHA512
244 &_gcry_digest_spec_sha512_256,
245 &_gcry_digest_spec_sha512_224,
246 #else
247 NULL,
248 NULL,
249 #endif
250 #if USE_SHA3
251 &_gcry_digest_spec_cshake128,
252 &_gcry_digest_spec_cshake256
253 #else
254 NULL,
255 NULL
256 #endif
257 };
258
259
260 typedef struct gcry_md_list
261 {
262 const gcry_md_spec_t *spec;
263 struct gcry_md_list *next;
264 size_t actual_struct_size; /* Allocated size of this structure. */
265 PROPERLY_ALIGNED_TYPE context[1];
266 } GcryDigestEntry;
267
268 /* This structure is put right after the gcry_md_hd_t buffer, so that
269 * only one memory block is needed. */
270 struct gcry_md_context
271 {
272 int magic;
273 struct {
274 unsigned int secure:1;
275 unsigned int finalized:1;
276 unsigned int bugemu1:1;
277 unsigned int hmac:1;
278 } flags;
279 size_t actual_handle_size; /* Allocated size of this handle. */
280 FILE *debug;
281 GcryDigestEntry *list;
282 };
283
284
285 #define CTX_MAGIC_NORMAL 0x11071961
286 #define CTX_MAGIC_SECURE 0x16917011
287
288 static gcry_err_code_t md_enable (gcry_md_hd_t hd, int algo);
289 static void md_close (gcry_md_hd_t a);
290 static void md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen);
291 static byte *md_read( gcry_md_hd_t a, int algo );
292 static int md_get_algo( gcry_md_hd_t a );
293 static int md_digest_length( int algo );
294 static void md_start_debug ( gcry_md_hd_t a, const char *suffix );
295 static void md_stop_debug ( gcry_md_hd_t a );
296
297
298
299 static int
300 map_algo (int algo)
301 {
302 return algo;
303 }
304
305
306 /* Return the spec structure for the hash algorithm ALGO. For an
307 unknown algorithm NULL is returned. */
308 static const gcry_md_spec_t *
309 spec_from_algo (int algo)
310 {
311 const gcry_md_spec_t *spec = NULL;
312
313 algo = map_algo (algo);
314
315 if (algo >= 0 && algo < DIM(digest_list_algo0))
316 spec = digest_list_algo0[algo];
317 else if (algo >= 301 && algo < 301 + DIM(digest_list_algo301))
318 spec = digest_list_algo301[algo - 301];
319
320 if (spec)
321 gcry_assert (spec->algo == algo);
322
323 return spec;
324 }
325
326
327 /* Lookup a hash's spec by its name. */
328 static const gcry_md_spec_t *
329 spec_from_name (const char *name)
330 {
331 const gcry_md_spec_t *spec;
332 int idx;
333
334 for (idx=0; (spec = digest_list[idx]); idx++)
335 {
336 if (!stricmp (name, spec->name))
337 return spec;
338 }
339
340 return NULL;
341 }
342
343
344 /* Lookup a hash's spec by its OID. */
345 static const gcry_md_spec_t *
346 spec_from_oid (const char *oid)
347 {
348 const gcry_md_spec_t *spec;
349 const gcry_md_oid_spec_t *oid_specs;
350 int idx, j;
351
352 for (idx=0; (spec = digest_list[idx]); idx++)
353 {
354 oid_specs = spec->oids;
355 if (oid_specs)
356 {
357 for (j = 0; oid_specs[j].oidstring; j++)
358 if (!stricmp (oid, oid_specs[j].oidstring))
359 return spec;
360 }
361 }
362
363 return NULL;
364 }
365
366
367 static const gcry_md_spec_t *
368 search_oid (const char *oid, gcry_md_oid_spec_t *oid_spec)
369 {
370 const gcry_md_spec_t *spec;
371 int i;
372
373 if (!oid)
374 return NULL;
375
376 if (!strncmp (oid, "oid.", 4) || !strncmp (oid, "OID.", 4))
377 oid += 4;
378
379 spec = spec_from_oid (oid);
380 if (spec && spec->oids)
381 {
382 for (i = 0; spec->oids[i].oidstring; i++)
383 if (!stricmp (oid, spec->oids[i].oidstring))
384 {
385 if (oid_spec)
386 *oid_spec = spec->oids[i];
387 return spec;
388 }
389 }
390
391 return NULL;
392 }
393
394
395 /****************
396 * Map a string to the digest algo
397 */
398 int
399 _gcry_md_map_name (const char *string)
400 {
401 const gcry_md_spec_t *spec;
402
403 if (!string)
404 return 0;
405
406 /* If the string starts with a digit (optionally prefixed with
407 either "OID." or "oid."), we first look into our table of ASN.1
408 object identifiers to figure out the algorithm */
409 spec = search_oid (string, NULL);
410 if (spec)
411 return spec->algo;
412
413 /* Not found, search a matching digest name. */
414 spec = spec_from_name (string);
415 if (spec)
416 return spec->algo;
417
418 return 0;
419 }
420
421
422 /****************
423 * This function simply returns the name of the algorithm or some constant
424 * string when there is no algo. It will never return NULL.
425 * Use the macro gcry_md_test_algo() to check whether the algorithm
426 * is valid.
427 */
428 const char *
429 _gcry_md_algo_name (int algorithm)
430 {
431 const gcry_md_spec_t *spec;
432
433 spec = spec_from_algo (algorithm);
434 return spec ? spec->name : "?";
435 }
436
437
438 static gcry_err_code_t
439 check_digest_algo_spec (int algo, const gcry_md_spec_t *spec)
440 {
441 int reject = 0;
442
443 if (spec->flags.disabled)
444 return GPG_ERR_DIGEST_ALGO;
445
446 if (!fips_mode ())
447 return 0;
448
449 if (spec->flags.fips)
450 return 0;
451
452 if (algo == GCRY_MD_MD5)
453 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_MD5);
454 else if (algo == GCRY_MD_SHA1)
455 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_SHA1);
456 else
457 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_OTHERS);
458
459 if (reject)
460 return GPG_ERR_DIGEST_ALGO;
461
462 fips_service_indicator_mark_non_compliant ();
463 return 0;
464 }
465
466 static gcry_err_code_t
467 check_digest_algo (int algo)
468 {
469 const gcry_md_spec_t *spec = spec_from_algo (algo);
470
471 if (!spec)
472 return GPG_ERR_DIGEST_ALGO;
473 else
474 return check_digest_algo_spec (algo, spec);
475 }
476
477
478 /****************
479 * Open a message digest handle for use with algorithm ALGO.
480 * More algorithms may be added by md_enable(). The initial algorithm
481 * may be 0.
482 */
483 static gcry_err_code_t
484 md_open (gcry_md_hd_t *h, int algo, unsigned int flags)
485 {
486 gcry_err_code_t err = 0;
487 int secure = !!(flags & GCRY_MD_FLAG_SECURE);
488 int hmac = !!(flags & GCRY_MD_FLAG_HMAC);
489 int bufsize = secure ? 512 : 1024;
490 gcry_md_hd_t hd;
491 size_t n;
492
493 /* Allocate a memory area to hold the caller visible buffer with it's
494 * control information and the data required by this module. Set the
495 * context pointer at the beginning to this area.
496 * We have to use this strange scheme because we want to hide the
497 * internal data but have a variable sized buffer.
498 *
499 * +---+------+---........------+-------------+
500 * !ctx! bctl ! buffer ! private !
501 * +---+------+---........------+-------------+
502 * ! ^
503 * !---------------------------!
504 *
505 * We have to make sure that private is well aligned.
506 */
507 n = offsetof (struct gcry_md_handle, buf) + bufsize;
508 n = ((n + sizeof (PROPERLY_ALIGNED_TYPE) - 1)
509 / sizeof (PROPERLY_ALIGNED_TYPE)) * sizeof (PROPERLY_ALIGNED_TYPE);
510
511 /* Allocate and set the Context pointer to the private data */
512 if (secure)
513 hd = xtrymalloc_secure (n + sizeof (struct gcry_md_context));
514 else
515 hd = xtrymalloc (n + sizeof (struct gcry_md_context));
516
517 if (! hd)
518 err = gpg_err_code_from_errno (errno);
519
520 if (! err)
521 {
522 struct gcry_md_context *ctx;
523
524 ctx = (void *) (hd->buf - offsetof (struct gcry_md_handle, buf) + n);
525 /* Setup the globally visible data (bctl in the diagram).*/
526 hd->ctx = ctx;
527 hd->bufsize = n - offsetof (struct gcry_md_handle, buf);
528 hd->bufpos = 0;
529
530 /* Initialize the private data. */
531 wipememory2 (ctx, 0, sizeof *ctx);
532 ctx->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL;
533 ctx->actual_handle_size = n + sizeof (struct gcry_md_context);
534 ctx->flags.secure = secure;
535 ctx->flags.hmac = hmac;
536 ctx->flags.bugemu1 = !!(flags & GCRY_MD_FLAG_BUGEMU1);
537 }
538
539 if (! err)
540 {
541 /* Hmmm, should we really do that? - yes [-wk] */
542 _gcry_fast_random_poll ();
543
544 if (algo)
545 {
546 err = md_enable (hd, algo);
547 if (err)
548 md_close (hd);
549 }
550 }
551
552 if (! err)
553 *h = hd;
554
555 return err;
556 }
557
558 /* Create a message digest object for algorithm ALGO. FLAGS may be
559 given as an bitwise OR of the gcry_md_flags values. ALGO may be
560 given as 0 if the algorithms to be used are later set using
561 gcry_md_enable. H is guaranteed to be a valid handle or NULL on
562 error. */
563 gcry_err_code_t
564 _gcry_md_open (gcry_md_hd_t *h, int algo, unsigned int flags)
565 {
566 gcry_err_code_t rc;
567 gcry_md_hd_t hd;
568
569 if ((flags & ~(GCRY_MD_FLAG_SECURE
570 | GCRY_MD_FLAG_HMAC
571 | GCRY_MD_FLAG_BUGEMU1)))
572 rc = GPG_ERR_INV_ARG;
573 else
574 rc = md_open (&hd, algo, flags);
575
576 if (!rc && fips_mode ())
577 {
578 GcryDigestEntry *entry = hd->ctx->list;
579 /* No ENTRY means that ALGO==0.
580 It's not yet known, if it's FIPS compliant or not. */
581 int is_compliant_algo = 1;
582
583 if (entry)
584 {
585 const gcry_md_spec_t *spec = entry->spec;
586 is_compliant_algo = spec->flags.fips;
587 }
588
589 if (!is_compliant_algo)
590 {
591 int reject = 0;
592
593 if (algo == GCRY_MD_MD5)
594 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_MD5);
595 else if (algo == GCRY_MD_SHA1)
596 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_SHA1);
597 else
598 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_OTHERS);
599
600 if (reject)
601 {
602 md_close (hd);
603 hd = NULL;
604 rc = GPG_ERR_DIGEST_ALGO;
605 }
606 else
607 fips_service_indicator_mark_non_compliant ();
608 }
609 }
610
611 *h = rc? NULL : hd;
612 return rc;
613 }
614
615
616
617 static gcry_err_code_t
618 md_enable (gcry_md_hd_t hd, int algorithm)
619 {
620 struct gcry_md_context *h = hd->ctx;
621 const gcry_md_spec_t *spec;
622 GcryDigestEntry *entry;
623 gcry_err_code_t err = 0;
624 int reject;
625
626 for (entry = h->list; entry; entry = entry->next)
627 if (entry->spec->algo == algorithm)
628 return 0; /* Already enabled */
629
630 if (algorithm == GCRY_MD_MD5)
631 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_MD5);
632 else if (algorithm == GCRY_MD_SHA1)
633 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_SHA1);
634 else
635 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_OTHERS);
636
637 spec = spec_from_algo (algorithm);
638 if (!spec)
639 {
640 log_debug ("md_enable: algorithm %d not available\n", algorithm);
641 err = GPG_ERR_DIGEST_ALGO;
642 }
643
644 if (!err && spec->flags.disabled)
645 err = GPG_ERR_DIGEST_ALGO;
646
647 /* Any non-FIPS algorithm should go this way */
648 if (!err && reject && !spec->flags.fips && fips_mode ())
649 err = GPG_ERR_DIGEST_ALGO;
650
651 if (!err && h->flags.hmac && spec->read == NULL)
652 {
653 /* Expandable output function cannot act as part of HMAC. */
654 err = GPG_ERR_DIGEST_ALGO;
655 }
656
657 if (!err)
658 {
659 size_t size = (sizeof (*entry)
660 + spec->contextsize * (h->flags.hmac? 3 : 1)
661 - sizeof (entry->context));
662
663 /* And allocate a new list entry. */
664 if (h->flags.secure)
665 entry = xtrymalloc_secure (size);
666 else
667 entry = xtrymalloc (size);
668
669 if (! entry)
670 err = gpg_err_code_from_errno (errno);
671 else
672 {
673 entry->spec = spec;
674 entry->next = h->list;
675 entry->actual_struct_size = size;
676 h->list = entry;
677
678 /* And init this instance. */
679 entry->spec->init (entry->context,
680 h->flags.bugemu1? GCRY_MD_FLAG_BUGEMU1:0);
681 }
682 }
683
684 return err;
685 }
686
687
688 gcry_err_code_t
689 _gcry_md_enable (gcry_md_hd_t hd, int algorithm)
690 {
691 gcry_err_code_t rc;
692
693 rc = md_enable (hd, algorithm);
694 if (!rc && fips_mode ())
695 {
696 GcryDigestEntry *entry = hd->ctx->list;
697 /* No ENTRY means, something goes wrong. */
698 int is_compliant_algo = 0;
699
700 if (entry)
701 {
702 const gcry_md_spec_t *spec = entry->spec;
703 is_compliant_algo = spec->flags.fips;
704 }
705
706 if (!is_compliant_algo)
707 {
708 int reject = 0;
709
710 if (algorithm == GCRY_MD_MD5)
711 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_MD5);
712 else if (algorithm == GCRY_MD_SHA1)
713 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_SHA1);
714 else
715 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_OTHERS);
716
717 if (reject)
718 rc = GPG_ERR_DIGEST_ALGO;
719 else
720 fips_service_indicator_mark_non_compliant ();
721 }
722 }
723
724 return rc;
725 }
726
727
728 static gcry_err_code_t
729 md_copy (gcry_md_hd_t ahd, gcry_md_hd_t *b_hd)
730 {
731 gcry_err_code_t rc = 0;
732 struct gcry_md_context *a = ahd->ctx;
733 struct gcry_md_context *b;
734 GcryDigestEntry *ar, *br;
735 gcry_md_hd_t bhd;
736 size_t n;
737 int is_compliant_algo = 1;
738 int reject = 0;
739
740 if (ahd->bufpos)
741 md_write (ahd, NULL, 0);
742
743 n = (char *) ahd->ctx - (char *) ahd;
744 if (a->flags.secure)
745 bhd = xtrymalloc_secure (n + sizeof (struct gcry_md_context));
746 else
747 bhd = xtrymalloc (n + sizeof (struct gcry_md_context));
748
749 if (!bhd)
750 {
751 rc = gpg_err_code_from_syserror ();
752 goto leave;
753 }
754
755 bhd->ctx = b = (void *) ((char *) bhd + n);
756 /* No need to copy the buffer due to the write above. */
757 gcry_assert (ahd->bufsize == (n - offsetof (struct gcry_md_handle, buf)));
758 bhd->bufsize = ahd->bufsize;
759 bhd->bufpos = 0;
760 gcry_assert (! ahd->bufpos);
761 memcpy (b, a, sizeof *a);
762 b->list = NULL;
763 b->debug = NULL;
764
765 if (!a->list)
766 is_compliant_algo = 0;
767
768 /* Copy the complete list of algorithms. The copied list is
769 reversed, but that doesn't matter. */
770 for (ar = a->list; ar; ar = ar->next)
771 {
772 const gcry_md_spec_t *spec = ar->spec;
773
774 if (a->flags.secure)
775 br = xtrymalloc_secure (ar->actual_struct_size);
776 else
777 br = xtrymalloc (ar->actual_struct_size);
778 if (!br)
779 {
780 rc = gpg_err_code_from_syserror ();
781 md_close (bhd);
782 goto leave;
783 }
784
785 if (!spec->flags.fips)
786 {
787 is_compliant_algo = 0;
788
789 if (spec->algo == GCRY_MD_MD5)
790 reject |= fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_MD5);
791 else if (spec->algo == GCRY_MD_SHA1)
792 reject = fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_SHA1);
793 else
794 reject |= fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MD_OTHERS);
795 }
796
797 memcpy (br, ar, ar->actual_struct_size);
798 br->next = b->list;
799 b->list = br;
800 }
801
802 if (a->debug)
803 md_start_debug (bhd, "unknown");
804
805 if (!is_compliant_algo && fips_mode ())
806 {
807 if (reject)
808 {
809 rc = GPG_ERR_DIGEST_ALGO;
810 md_close (bhd);
811 }
812 else
813 fips_service_indicator_mark_non_compliant ();
814 }
815
816 if (!rc)
817 *b_hd = bhd;
818
819 leave:
820 return rc;
821 }
822
823
824 gcry_err_code_t
825 _gcry_md_copy (gcry_md_hd_t *handle, gcry_md_hd_t hd)
826 {
827 gcry_err_code_t rc;
828
829 rc = md_copy (hd, handle);
830 if (rc)
831 *handle = NULL;
832 return rc;
833 }
834
835
836 /*
837 * Reset all contexts and discard any buffered stuff. This may be used
838 * instead of a md_close(); md_open().
839 */
840 void
841 _gcry_md_reset (gcry_md_hd_t a)
842 {
843 GcryDigestEntry *r;
844
845 /* Note: We allow this even in fips non operational mode. */
846
847 a->bufpos = a->ctx->flags.finalized = 0;
848
849 if (a->ctx->flags.hmac)
850 for (r = a->ctx->list; r; r = r->next)
851 {
852 memcpy (r->context, (char *)r->context + r->spec->contextsize,
853 r->spec->contextsize);
854 }
855 else
856 for (r = a->ctx->list; r; r = r->next)
857 {
858 memset (r->context, 0, r->spec->contextsize);
859 (*r->spec->init) (r->context,
860 a->ctx->flags.bugemu1? GCRY_MD_FLAG_BUGEMU1:0);
861 }
862 }
863
864
865 static void
866 md_close (gcry_md_hd_t a)
867 {
868 GcryDigestEntry *r, *r2;
869
870 if (! a)
871 return;
872 if (a->ctx->debug)
873 md_stop_debug (a);
874 for (r = a->ctx->list; r; r = r2)
875 {
876 r2 = r->next;
877 wipememory (r, r->actual_struct_size);
878 xfree (r);
879 }
880
881 wipememory (a, a->ctx->actual_handle_size);
882 xfree(a);
883 }
884
885
886 void
887 _gcry_md_close (gcry_md_hd_t hd)
888 {
889 /* Note: We allow this even in fips non operational mode. */
890 md_close (hd);
891 }
892
893
894 static void
895 md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen)
896 {
897 GcryDigestEntry *r;
898
899 if (a->ctx->debug)
900 {
901 if (a->bufpos && fwrite (a->buf, a->bufpos, 1, a->ctx->debug) != 1)
902 BUG();
903 if (inlen && fwrite (inbuf, inlen, 1, a->ctx->debug) != 1)
904 BUG();
905 }
906
907 for (r = a->ctx->list; r; r = r->next)
908 {
909 if (a->bufpos)
910 (*r->spec->write) (r->context, a->buf, a->bufpos);
911 (*r->spec->write) (r->context, inbuf, inlen);
912 }
913 a->bufpos = 0;
914 }
915
916
917 /* Note that this function may be used after finalize and read to keep
918 on writing to the transform function so to mitigate timing
919 attacks. */
920 void
921 _gcry_md_write (gcry_md_hd_t hd, const void *inbuf, size_t inlen)
922 {
923 md_write (hd, inbuf, inlen);
924 }
925
926
927 static void
928 md_final (gcry_md_hd_t a)
929 {
930 GcryDigestEntry *r;
931
932 if (a->ctx->flags.finalized)
933 return;
934
935 if (a->bufpos)
936 md_write (a, NULL, 0);
937
938 for (r = a->ctx->list; r; r = r->next)
939 (*r->spec->final) (r->context);
940
941 a->ctx->flags.finalized = 1;
942
943 if (!a->ctx->flags.hmac)
944 return;
945
946 for (r = a->ctx->list; r; r = r->next)
947 {
948 byte *p;
949 size_t dlen = r->spec->mdlen;
950 byte *hash;
951 gcry_err_code_t err;
952
953 if (r->spec->read == NULL)
954 continue;
955
956 p = r->spec->read (r->context);
957
958 if (a->ctx->flags.secure)
959 hash = xtrymalloc_secure (dlen);
960 else
961 hash = xtrymalloc (dlen);
962 if (!hash)
963 {
964 err = gpg_err_code_from_errno (errno);
965 _gcry_fatal_error (err, NULL);
966 }
967
968 memcpy (hash, p, dlen);
969 memcpy (r->context, (char *)r->context + r->spec->contextsize * 2,
970 r->spec->contextsize);
971 (*r->spec->write) (r->context, hash, dlen);
972 (*r->spec->final) (r->context);
973 xfree (hash);
974 }
975 }
976
977
978 static gcry_err_code_t
979 md_setkey (gcry_md_hd_t h, const unsigned char *key, size_t keylen)
980 {
981 gcry_err_code_t rc = 0;
982 GcryDigestEntry *r;
983 int algo_had_setkey = 0;
984
985 if (!h->ctx->list)
986 return GPG_ERR_DIGEST_ALGO; /* Might happen if no algo is enabled. */
987
988 if (h->ctx->flags.hmac)
989 return GPG_ERR_DIGEST_ALGO; /* Tried md_setkey for HMAC md. */
990
991 for (r = h->ctx->list; r; r = r->next)
992 {
993 switch (r->spec->algo)
994 {
995 #if USE_BLAKE2
996 /* TODO? add spec->init_with_key? */
997 case GCRY_MD_BLAKE2B_512:
998 case GCRY_MD_BLAKE2B_384:
999 case GCRY_MD_BLAKE2B_256:
1000 case GCRY_MD_BLAKE2B_160:
1001 case GCRY_MD_BLAKE2S_256:
1002 case GCRY_MD_BLAKE2S_224:
1003 case GCRY_MD_BLAKE2S_160:
1004 case GCRY_MD_BLAKE2S_128:
1005 algo_had_setkey = 1;
1006 memset (r->context, 0, r->spec->contextsize);
1007 rc = _gcry_blake2_init_with_key (r->context,
1008 h->ctx->flags.bugemu1
1009 ? GCRY_MD_FLAG_BUGEMU1:0,
1010 key, keylen, r->spec->algo);
1011 break;
1012 #endif
1013 default:
1014 rc = GPG_ERR_DIGEST_ALGO;
1015 break;
1016 }
1017
1018 if (rc)
1019 break;
1020 }
1021
1022 if (rc && !algo_had_setkey)
1023 {
1024 /* None of algorithms had setkey implementation, so contexts were not
1025 * modified. Just return error. */
1026 return rc;
1027 }
1028 else if (rc && algo_had_setkey)
1029 {
1030 /* Some of the contexts have been modified, but got error. Reset
1031 * all contexts. */
1032 _gcry_md_reset (h);
1033 return rc;
1034 }
1035
1036 /* Successful md_setkey implies reset. */
1037 h->bufpos = h->ctx->flags.finalized = 0;
1038
1039 return 0;
1040 }
1041
1042
1043 static gcry_err_code_t
1044 prepare_macpads (gcry_md_hd_t a, const unsigned char *key, size_t keylen)
1045 {
1046 GcryDigestEntry *r;
1047
1048 if (!a->ctx->list)
1049 return GPG_ERR_DIGEST_ALGO; /* Might happen if no algo is enabled. */
1050
1051 if (!a->ctx->flags.hmac)
1052 return GPG_ERR_DIGEST_ALGO; /* Tried prepare_macpads for non-HMAC md. */
1053
1054 for (r = a->ctx->list; r; r = r->next)
1055 {
1056 const unsigned char *k;
1057 size_t k_len;
1058 unsigned char *key_allocated = NULL;
1059 int macpad_Bsize;
1060 int i;
1061
1062 switch (r->spec->algo)
1063 {
1064 /* TODO: add spec->blocksize */
1065 case GCRY_MD_SHA3_224:
1066 macpad_Bsize = 1152 / 8;
1067 break;
1068 case GCRY_MD_SHA3_256:
1069 macpad_Bsize = 1088 / 8;
1070 break;
1071 case GCRY_MD_SHA3_384:
1072 macpad_Bsize = 832 / 8;
1073 break;
1074 case GCRY_MD_SHA3_512:
1075 macpad_Bsize = 576 / 8;
1076 break;
1077 case GCRY_MD_SHA384:
1078 case GCRY_MD_SHA512:
1079 case GCRY_MD_SHA512_256:
1080 case GCRY_MD_SHA512_224:
1081 case GCRY_MD_BLAKE2B_512:
1082 case GCRY_MD_BLAKE2B_384:
1083 case GCRY_MD_BLAKE2B_256:
1084 case GCRY_MD_BLAKE2B_160:
1085 macpad_Bsize = 128;
1086 break;
1087 case GCRY_MD_GOSTR3411_94:
1088 case GCRY_MD_GOSTR3411_CP:
1089 macpad_Bsize = 32;
1090 break;
1091 default:
1092 macpad_Bsize = 64;
1093 break;
1094 }
1095
1096 if ( keylen > macpad_Bsize )
1097 {
1098 k = key_allocated = xtrymalloc_secure (r->spec->mdlen);
1099 if (!k)
1100 return gpg_err_code_from_errno (errno);
1101 _gcry_md_hash_buffer (r->spec->algo, key_allocated, key, keylen);
1102 k_len = r->spec->mdlen;
1103 gcry_assert ( k_len <= macpad_Bsize );
1104 }
1105 else
1106 {
1107 k = key;
1108 k_len = keylen;
1109 }
1110
1111 (*r->spec->init) (r->context,
1112 a->ctx->flags.bugemu1? GCRY_MD_FLAG_BUGEMU1:0);
1113 a->bufpos = 0;
1114 for (i=0; i < k_len; i++ )
1115 _gcry_md_putc (a, k[i] ^ 0x36);
1116 for (; i < macpad_Bsize; i++ )
1117 _gcry_md_putc (a, 0x36);
1118 (*r->spec->write) (r->context, a->buf, a->bufpos);
1119 memcpy ((char *)r->context + r->spec->contextsize, r->context,
1120 r->spec->contextsize);
1121
1122 (*r->spec->init) (r->context,
1123 a->ctx->flags.bugemu1? GCRY_MD_FLAG_BUGEMU1:0);
1124 a->bufpos = 0;
1125 for (i=0; i < k_len; i++ )
1126 _gcry_md_putc (a, k[i] ^ 0x5c);
1127 for (; i < macpad_Bsize; i++ )
1128 _gcry_md_putc (a, 0x5c);
1129 (*r->spec->write) (r->context, a->buf, a->bufpos);
1130 memcpy ((char *)r->context + r->spec->contextsize*2, r->context,
1131 r->spec->contextsize);
1132
1133 xfree (key_allocated);
1134 }
1135
1136 a->bufpos = 0;
1137 return 0;
1138 }
1139
1140
1141 static gcry_err_code_t
1142 md_customize (gcry_md_hd_t h, void *buffer, size_t buflen)
1143 {
1144 gcry_err_code_t rc = 0;
1145 GcryDigestEntry *r;
1146 int algo_had_customize = 0;
1147
1148 if (!h->ctx->list)
1149 return GPG_ERR_DIGEST_ALGO; /* Might happen if no algo is enabled. */
1150
1151 for (r = h->ctx->list; r; r = r->next)
1152 {
1153 switch (r->spec->algo)
1154 {
1155 case GCRY_MD_CSHAKE128:
1156 case GCRY_MD_CSHAKE256:
1157 algo_had_customize = 1;
1158 if (buflen != sizeof (struct gcry_cshake_customization))
1159 rc = GPG_ERR_INV_ARG;
1160 else
1161 rc = _gcry_cshake_customize (r->context, buffer);
1162 break;
1163 default:
1164 rc = GPG_ERR_DIGEST_ALGO;
1165 break;
1166 }
1167
1168 if (rc)
1169 break;
1170 }
1171
1172 if (rc && !algo_had_customize)
1173 {
1174 /* None of algorithms had customize implementation, so contexts were not
1175 * modified. Just return error. */
1176 return rc;
1177 }
1178 else if (rc && algo_had_customize)
1179 {
1180 /* Some of the contexts have been modified, but got error. Reset
1181 * all contexts. */
1182 _gcry_md_reset (h);
1183 return rc;
1184 }
1185
1186 return 0;
1187 }
1188
1189
1190 gcry_err_code_t
1191 _gcry_md_ctl (gcry_md_hd_t hd, int cmd, void *buffer, size_t buflen)
1192 {
1193 gcry_err_code_t rc = 0;
1194
1195 (void)buflen; /* Currently not used. */
1196
1197 switch (cmd)
1198 {
1199 case GCRYCTL_FINALIZE:
1200 md_final (hd);
1201 break;
1202 case GCRYCTL_START_DUMP:
1203 md_start_debug (hd, buffer);
1204 break;
1205 case GCRYCTL_STOP_DUMP:
1206 md_stop_debug ( hd );
1207 break;
1208 case GCRYCTL_MD_CUSTOMIZE:
1209 rc = md_customize (hd, buffer, buflen);
1210 break;
1211 default:
1212 rc = GPG_ERR_INV_OP;
1213 }
1214 return rc;
1215 }
1216
1217
1218 gcry_err_code_t
1219 _gcry_md_setkey (gcry_md_hd_t hd, const void *key, size_t keylen)
1220 {
1221 gcry_err_code_t rc;
1222
1223 if (hd->ctx->flags.hmac)
1224 {
1225 rc = prepare_macpads (hd, key, keylen);
1226 if (!rc)
1227 _gcry_md_reset (hd);
1228 }
1229 else
1230 {
1231 rc = md_setkey (hd, key, keylen);
1232 }
1233
1234 return rc;
1235 }
1236
1237
1238 /* The new debug interface. If SUFFIX is a string it creates an debug
1239 file for the context HD. IF suffix is NULL, the file is closed and
1240 debugging is stopped. */
1241 void
1242 _gcry_md_debug (gcry_md_hd_t hd, const char *suffix)
1243 {
1244 if (suffix)
1245 md_start_debug (hd, suffix);
1246 else
1247 md_stop_debug (hd);
1248 }
1249
1250
1251 /****************
1252 * If ALGO is null get the digest for the used algo (which should be
1253 * only one)
1254 */
1255 static byte *
1256 md_read( gcry_md_hd_t a, int algo )
1257 {
1258 GcryDigestEntry *r = a->ctx->list;
1259
1260 if (! algo)
1261 {
1262 /* Return the first algorithm */
1263 if (r)
1264 {
1265 if (r->next)
1266 log_debug ("more than one algorithm in md_read(0)\n");
1267 if (r->spec->read)
1268 return r->spec->read (r->context);
1269 }
1270 }
1271 else
1272 {
1273 for (r = a->ctx->list; r; r = r->next)
1274 if (r->spec->algo == algo)
1275 {
1276 if (r->spec->read)
1277 return r->spec->read (r->context);
1278 break;
1279 }
1280 }
1281
1282 if (r && !r->spec->read)
1283 _gcry_fatal_error (GPG_ERR_DIGEST_ALGO,
1284 "requested algo has no fixed digest length");
1285 else
1286 _gcry_fatal_error (GPG_ERR_DIGEST_ALGO, "requested algo not in md context");
1287 return NULL;
1288 }
1289
1290
1291 /*
1292 * Read out the complete digest, this function implictly finalizes
1293 * the hash.
1294 */
1295 byte *
1296 _gcry_md_read (gcry_md_hd_t hd, int algo)
1297 {
1298 /* This function is expected to always return a digest, thus we
1299 can't return an error which we actually should do in
1300 non-operational state. */
1301 _gcry_md_ctl (hd, GCRYCTL_FINALIZE, NULL, 0);
1302 return md_read (hd, algo);
1303 }
1304
1305
1306 /****************
1307 * If ALGO is null get the digest for the used algo (which should be
1308 * only one)
1309 */
1310 static gcry_err_code_t
1311 md_extract(gcry_md_hd_t a, int algo, void *out, size_t outlen)
1312 {
1313 GcryDigestEntry *r = a->ctx->list;
1314
1315 if (!algo)
1316 {
1317 /* Return the first algorithm */
1318 if (r && r->spec->extract)
1319 {
1320 if (r->next)
1321 log_debug ("more than one algorithm in md_extract(0)\n");
1322
1323 return r->spec->extract (r->context, out, outlen);
1324 }
1325 }
1326 else
1327 {
1328 for (r = a->ctx->list; r; r = r->next)
1329 if (r->spec->algo == algo && r->spec->extract)
1330 {
1331 return r->spec->extract (r->context, out, outlen);
1332 }
1333 }
1334
1335 return GPG_ERR_DIGEST_ALGO;
1336 }
1337
1338
1339 /*
1340 * Expand the output from XOF class digest, this function implictly finalizes
1341 * the hash.
1342 */
1343 gcry_err_code_t
1344 _gcry_md_extract (gcry_md_hd_t hd, int algo, void *out, size_t outlen)
1345 {
1346 _gcry_md_ctl (hd, GCRYCTL_FINALIZE, NULL, 0);
1347 return md_extract (hd, algo, out, outlen);
1348 }
1349
1350
1351 /*
1352 * Read out an intermediate digest. Not yet functional.
1353 */
1354 gcry_err_code_t
1355 _gcry_md_get (gcry_md_hd_t hd, int algo, byte *buffer, int buflen)
1356 {
1357 (void)hd;
1358 (void)algo;
1359 (void)buffer;
1360 (void)buflen;
1361
1362 /*md_digest ... */
1363 fips_signal_error ("unimplemented function called");
1364 return GPG_ERR_INTERNAL;
1365 }
1366
1367
1368 /*
1369 * Shortcut function to hash a buffer with a given algo. The only
1370 * guaranteed supported algorithms are RIPE-MD160 and SHA-1. The
1371 * supplied digest buffer must be large enough to store the resulting
1372 * hash. No error is returned, the function will abort on an invalid
1373 * algo. DISABLED_ALGOS are ignored here. */
1374 void
1375 _gcry_md_hash_buffer (int algo, void *digest,
1376 const void *buffer, size_t length)
1377 {
1378 const gcry_md_spec_t *spec;
1379
1380 spec = spec_from_algo (algo);
1381 if (!spec)
1382 {
1383 log_debug ("md_hash_buffer: algorithm %d not available\n", algo);
1384 return;
1385 }
1386
1387 if (spec->hash_buffers != NULL)
1388 {
1389 gcry_buffer_t iov;
1390
1391 iov.size = 0;
1392 iov.data = (void *)buffer;
1393 iov.off = 0;
1394 iov.len = length;
1395
1396 if (spec->flags.disabled)
1397 log_bug ("gcry_md_hash_buffer failed for algo %d: %s",
1398 algo, gpg_strerror (gcry_error (GPG_ERR_DIGEST_ALGO)));
1399
1400 spec->hash_buffers (digest, spec->mdlen, &iov, 1);
1401 }
1402 else
1403 {
1404 /* For the others we do not have a fast function, so we use the
1405 normal functions. */
1406 gcry_md_hd_t h;
1407 gpg_err_code_t err;
1408
1409 err = md_open (&h, algo, 0);
1410 if (err)
1411 log_bug ("gcry_md_open failed for algo %d: %s",
1412 algo, gpg_strerror (gcry_error(err)));
1413 md_write (h, (byte *) buffer, length);
1414 md_final (h);
1415 memcpy (digest, md_read (h, algo), md_digest_length (algo));
1416 md_close (h);
1417 }
1418
1419 if (fips_mode ())
1420 {
1421 if (!spec->flags.fips)
1422 fips_service_indicator_mark_non_compliant ();
1423 }
1424 }
1425
1426
1427 /* Shortcut function to hash multiple buffers with a given algo. In
1428 contrast to gcry_md_hash_buffer, this function returns an error on
1429 invalid arguments or on other problems; disabled algorithms are
1430 _not_ ignored but flagged as an error.
1431
1432 The data to sign is taken from the array IOV which has IOVCNT items.
1433
1434 The only supported flag in FLAGS is GCRY_MD_FLAG_HMAC which turns
1435 this function into a HMAC function; the first item in IOV is then
1436 used as the key.
1437
1438 On success 0 is returned and resulting hash or HMAC is stored at
1439 DIGEST. DIGESTLEN may be given as -1, in which case DIGEST must
1440 have been provided by the caller with an appropriate length.
1441 DIGESTLEN may also be the appropriate length or, in case of XOF
1442 algorithms, DIGESTLEN indicates number bytes to extract from XOF
1443 to DIGEST. */
1444 gpg_err_code_t
1445 _gcry_md_hash_buffers_extract (int algo, unsigned int flags, void *digest,
1446 int digestlen, const gcry_buffer_t *iov,
1447 int iovcnt)
1448 {
1449 const gcry_md_spec_t *spec;
1450 int is_xof;
1451 int hmac;
1452
1453 if (!iov || iovcnt < 0)
1454 return GPG_ERR_INV_ARG;
1455 if (flags & ~(GCRY_MD_FLAG_HMAC))
1456 return GPG_ERR_INV_ARG;
1457
1458 hmac = !!(flags & GCRY_MD_FLAG_HMAC);
1459 if (hmac && iovcnt < 1)
1460 return GPG_ERR_INV_ARG;
1461
1462 spec = spec_from_algo (algo);
1463 if (!spec)
1464 {
1465 log_debug ("md_hash_buffers: algorithm %d not available\n", algo);
1466 return GPG_ERR_DIGEST_ALGO;
1467 }
1468
1469 is_xof = spec->extract != NULL;
1470 if (!is_xof && digestlen != -1 && digestlen != spec->mdlen)
1471 return GPG_ERR_DIGEST_ALGO;
1472
1473 if (digestlen == -1)
1474 digestlen = spec->mdlen;
1475
1476 if (!hmac && spec->hash_buffers)
1477 {
1478 if (spec->flags.disabled)
1479 return GPG_ERR_DIGEST_ALGO;
1480
1481 spec->hash_buffers (digest, digestlen, iov, iovcnt);
1482 }
1483 else
1484 {
1485 /* For the others we do not have a fast function, so we use the
1486 normal functions. */
1487 gcry_md_hd_t h;
1488 gpg_err_code_t rc;
1489
1490 rc = md_open (&h, algo, (hmac? GCRY_MD_FLAG_HMAC:0));
1491 if (rc)
1492 return rc;
1493
1494 if (hmac)
1495 {
1496 rc = _gcry_md_setkey (h,
1497 (const char*)iov[0].data + iov[0].off,
1498 iov[0].len);
1499 if (rc)
1500 {
1501 md_close (h);
1502 return rc;
1503 }
1504 iov++; iovcnt--;
1505 }
1506 for (;iovcnt; iov++, iovcnt--)
1507 md_write (h, (const char*)iov[0].data + iov[0].off, iov[0].len);
1508 md_final (h);
1509 if (digestlen == spec->mdlen)
1510 memcpy (digest, md_read (h, algo), spec->mdlen);
1511 else if (digestlen > 0)
1512 md_extract (h, algo, digest, digestlen);
1513 md_close (h);
1514 }
1515
1516 if (fips_mode ())
1517 {
1518 if (!spec->flags.fips)
1519 fips_service_indicator_mark_non_compliant ();
1520 }
1521
1522 return 0;
1523 }
1524
1525
1526 /* Shortcut function to hash multiple buffers with a given algo. In
1527 contrast to gcry_md_hash_buffer, this function returns an error on
1528 invalid arguments or on other problems; disabled algorithms are
1529 _not_ ignored but flagged as an error.
1530
1531 The data to sign is taken from the array IOV which has IOVCNT items.
1532
1533 The only supported flag in FLAGS is GCRY_MD_FLAG_HMAC which turns
1534 this function into a HMAC function; the first item in IOV is then
1535 used as the key.
1536
1537 On success 0 is returned and resulting hash or HMAC is stored at
1538 DIGEST which must have been provided by the caller with an
1539 appropriate length. */
1540 gpg_err_code_t
1541 _gcry_md_hash_buffers (int algo, unsigned int flags, void *digest,
1542 const gcry_buffer_t *iov, int iovcnt)
1543 {
1544 return _gcry_md_hash_buffers_extract(algo, flags, digest, -1, iov, iovcnt);
1545 }
1546
1547
1548 static int
1549 md_get_algo (gcry_md_hd_t a)
1550 {
1551 GcryDigestEntry *r = a->ctx->list;
1552
1553 if (r && r->next)
1554 {
1555 fips_signal_error ("possible usage error");
1556 log_error ("WARNING: more than one algorithm in md_get_algo()\n");
1557 }
1558 return r ? r->spec->algo : 0;
1559 }
1560
1561
1562 int
1563 _gcry_md_get_algo (gcry_md_hd_t hd)
1564 {
1565 return md_get_algo (hd);
1566 }
1567
1568
1569 /****************
1570 * Return the length of the digest
1571 */
1572 static int
1573 md_digest_length (int algorithm)
1574 {
1575 const gcry_md_spec_t *spec;
1576
1577 spec = spec_from_algo (algorithm);
1578 return spec? spec->mdlen : 0;
1579 }
1580
1581
1582 /****************
1583 * Return the length of the digest in bytes.
1584 * This function will return 0 in case of errors.
1585 */
1586 unsigned int
1587 _gcry_md_get_algo_dlen (int algorithm)
1588 {
1589 return md_digest_length (algorithm);
1590 }
1591
1592
1593 /* Hmmm: add a mode to enumerate the OIDs
1594 * to make g10/sig-check.c more portable */
1595 static const byte *
1596 md_asn_oid (int algorithm, size_t *asnlen, size_t *mdlen)
1597 {
1598 const gcry_md_spec_t *spec;
1599 const byte *asnoid = NULL;
1600
1601 spec = spec_from_algo (algorithm);
1602 if (spec)
1603 {
1604 if (asnlen)
1605 *asnlen = spec->asnlen;
1606 if (mdlen)
1607 *mdlen = spec->mdlen;
1608 asnoid = spec->asnoid;
1609 }
1610 else
1611 log_bug ("no ASN.1 OID for md algo %d\n", algorithm);
1612
1613 return asnoid;
1614 }
1615
1616
1617 /****************
1618 * Return information about the given cipher algorithm
1619 * WHAT select the kind of information returned:
1620 * GCRYCTL_TEST_ALGO:
1621 * Returns 0 when the specified algorithm is available for use.
1622 * buffer and nbytes must be zero.
1623 * GCRYCTL_GET_ASNOID:
1624 * Return the ASNOID of the algorithm in buffer. if buffer is NULL, only
1625 * the required length is returned.
1626 * GCRYCTL_SELFTEST
1627 * Helper for the regression tests - shall not be used by applications.
1628 *
1629 * Note: Because this function is in most cases used to return an
1630 * integer value, we can make it easier for the caller to just look at
1631 * the return value. The caller will in all cases consult the value
1632 * and thereby detecting whether a error occurred or not (i.e. while checking
1633 * the block size)
1634 */
1635 gcry_err_code_t
1636 _gcry_md_algo_info (int algo, int what, void *buffer, size_t *nbytes)
1637 {
1638 gcry_err_code_t rc;
1639
1640 switch (what)
1641 {
1642 case GCRYCTL_TEST_ALGO:
1643 if (buffer || nbytes)
1644 rc = GPG_ERR_INV_ARG;
1645 else
1646 rc = check_digest_algo (algo);
1647 break;
1648
1649 case GCRYCTL_GET_ASNOID:
1650 /* We need to check that the algo is available because
1651 md_asn_oid would otherwise raise an assertion. */
1652 rc = check_digest_algo (algo);
1653 if (!rc)
1654 {
1655 const char unsigned *asn;
1656 size_t asnlen;
1657
1658 asn = md_asn_oid (algo, &asnlen, NULL);
1659 if (buffer && (*nbytes >= asnlen))
1660 {
1661 memcpy (buffer, asn, asnlen);
1662 *nbytes = asnlen;
1663 }
1664 else if (!buffer && nbytes)
1665 *nbytes = asnlen;
1666 else
1667 {
1668 if (buffer)
1669 rc = GPG_ERR_TOO_SHORT;
1670 else
1671 rc = GPG_ERR_INV_ARG;
1672 }
1673 }
1674 break;
1675
1676 case GCRYCTL_SELFTEST:
1677 /* Helper function for the regression tests. */
1678 rc = gpg_err_code (_gcry_md_selftest (algo, nbytes? (int)*nbytes : 0,
1679 NULL));
1680 break;
1681
1682 default:
1683 rc = GPG_ERR_INV_OP;
1684 break;
1685 }
1686
1687 return rc;
1688 }
1689
1690
1691 static void
1692 md_start_debug ( gcry_md_hd_t md, const char *suffix )
1693 {
1694 static int idx=0;
1695 char buf[50];
1696
1697 if (fips_mode ())
1698 return;
1699
1700 if ( md->ctx->debug )
1701 {
1702 log_debug("Oops: md debug already started\n");
1703 return;
1704 }
1705 idx++;
1706 snprintf (buf, DIM(buf)-1, "dbgmd-%05d.%.10s", idx, suffix );
1707 md->ctx->debug = fopen(buf, "w");
1708 if ( !md->ctx->debug )
1709 log_debug("md debug: can't open %s\n", buf );
1710 }
1711
1712
1713 static void
1714 md_stop_debug( gcry_md_hd_t md )
1715 {
1716 if ( md->ctx->debug )
1717 {
1718 if ( md->bufpos )
1719 md_write ( md, NULL, 0 );
1720 fclose (md->ctx->debug);
1721 md->ctx->debug = NULL;
1722 }
1723
1724 { /* a kludge to pull in the __muldi3 for Solaris */
1725 volatile u32 a = (u32)(uintptr_t)md;
1726 volatile u64 b = 42;
1727 volatile u64 c;
1728 c = a * b;
1729 (void)c;
1730 }
1731 }
1732
1733
1734
1735 /*
1736 * Return information about the digest handle.
1737 * GCRYCTL_IS_SECURE:
1738 * Returns 1 when the handle works on secured memory
1739 * otherwise 0 is returned. There is no error return.
1740 * GCRYCTL_IS_ALGO_ENABLED:
1741 * Returns 1 if the algo is enabled for that handle.
1742 * The algo must be passed as the address of an int.
1743 */
1744 gcry_err_code_t
1745 _gcry_md_info (gcry_md_hd_t h, int cmd, void *buffer, size_t *nbytes)
1746 {
1747 gcry_err_code_t rc = 0;
1748
1749 switch (cmd)
1750 {
1751 case GCRYCTL_IS_SECURE:
1752 *nbytes = h->ctx->flags.secure;
1753 break;
1754
1755 case GCRYCTL_IS_ALGO_ENABLED:
1756 {
1757 GcryDigestEntry *r;
1758 int algo;
1759
1760 if ( !buffer || !nbytes || *nbytes != sizeof (int))
1761 rc = GPG_ERR_INV_ARG;
1762 else
1763 {
1764 algo = *(int*)buffer;
1765
1766 *nbytes = 0;
1767 for(r=h->ctx->list; r; r = r->next ) {
1768 if (r->spec->algo == algo)
1769 {
1770 *nbytes = 1;
1771 break;
1772 }
1773 }
1774 }
1775 break;
1776 }
1777
1778 default:
1779 rc = GPG_ERR_INV_OP;
1780 }
1781
1782 return rc;
1783 }
1784
1785
1786 /* Explicitly initialize this module. */
1787 gcry_err_code_t
1788 _gcry_md_init (void)
1789 {
1790 return 0;
1791 }
1792
1793
1794 int
1795 _gcry_md_is_secure (gcry_md_hd_t a)
1796 {
1797 size_t value;
1798
1799 if (_gcry_md_info (a, GCRYCTL_IS_SECURE, NULL, &value))
1800 value = 1; /* It seems to be better to assume secure memory on
1801 error. */
1802 return value;
1803 }
1804
1805
1806 int
1807 _gcry_md_is_enabled (gcry_md_hd_t a, int algo)
1808 {
1809 size_t value;
1810
1811 value = sizeof algo;
1812 if (_gcry_md_info (a, GCRYCTL_IS_ALGO_ENABLED, &algo, &value))
1813 value = 0;
1814 return value;
1815 }
1816
1817
1818 /* Run the selftests for digest algorithm ALGO with optional reporting
1819 function REPORT. */
1820 gpg_error_t
1821 _gcry_md_selftest (int algo, int extended, selftest_report_func_t report)
1822 {
1823 gcry_err_code_t ec = 0;
1824 const gcry_md_spec_t *spec;
1825
1826 spec = spec_from_algo (algo);
1827 if (spec && !check_digest_algo_spec (algo, spec) && spec->selftest)
1828 ec = spec->selftest (algo, extended, report);
1829 else
1830 {
1831 ec = (spec && spec->selftest) ? GPG_ERR_DIGEST_ALGO
1832 /* */ : GPG_ERR_NOT_IMPLEMENTED;
1833 if (report)
1834 report ("digest", algo, "module",
1835 spec && !spec->flags.disabled
1836 && (spec->flags.fips || !fips_mode ())?
1837 "no selftest available" :
1838 spec? "algorithm disabled" : "algorithm not found");
1839 }
1840
1841 return gpg_error (ec);
1842 }