1
1
/* md.c - message digest dispatcher
2
* Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
2
* Copyright (C) 1998, 1999, 2002, 2003, 2006 Free Software Foundation, Inc.
4
4
* This file is part of Libgcrypt.
58
58
{ &_gcry_digest_spec_sha256, GCRY_MD_SHA256 },
59
{ &_gcry_digest_spec_sha224, GCRY_MD_SHA224 },
61
62
{ &_gcry_digest_spec_sha512, GCRY_MD_SHA512 },
125
130
static gcry_err_code_t md_enable (gcry_md_hd_t hd, int algo);
126
131
static gcry_err_code_t md_copy (gcry_md_hd_t a, gcry_md_hd_t *b);
127
132
static void md_close (gcry_md_hd_t a);
128
static void md_write (gcry_md_hd_t a, byte *inbuf, size_t inlen);
133
static void md_write (gcry_md_hd_t a, const void *inbuf, size_t inlen);
129
134
static void md_final(gcry_md_hd_t a);
130
135
static byte *md_read( gcry_md_hd_t a, int algo );
131
136
static int md_get_algo( gcry_md_hd_t a );
132
137
static int md_digest_length( int algo );
133
138
static const byte *md_asn_oid( int algo, size_t *asnlen, size_t *mdlen );
134
static void md_start_debug( gcry_md_hd_t a, char *suffix );
135
static void md_stop_debug( gcry_md_hd_t a );
139
static void md_start_debug ( gcry_md_hd_t a, const char *suffix );
140
static void md_stop_debug ( gcry_md_hd_t a );
298
303
ret = search_oid (string, &algorithm, NULL);
301
/* Not found, search for an acording diget name. */
306
/* Not found, search a matching digest name. */
302
307
digest = gcry_md_lookup_name (string);
429
ctx->macpads = gcry_malloc_secure (128);
434
if ( (GCRY_MD_SHA384 == algo) || (GCRY_MD_SHA512 == algo) ) {
435
ctx->macpads_Bsize = 128;
436
ctx->macpads = gcry_malloc_secure (2*(ctx->macpads_Bsize));
438
ctx->macpads_Bsize = 64;
439
ctx->macpads = gcry_malloc_secure (2*(ctx->macpads_Bsize));
430
441
if (! ctx->macpads)
443
err = gpg_err_code_from_errno (errno);
433
err = gpg_err_code_from_errno (errno);
591
b->macpads = gcry_malloc_secure (128);
602
b->macpads = gcry_malloc_secure (2*(a->macpads_Bsize));
592
603
if (! b->macpads)
605
err = gpg_err_code_from_errno (errno);
595
err = gpg_err_code_from_errno (errno);
598
memcpy (b->macpads, a->macpads, 128);
609
memcpy (b->macpads, a->macpads, (2*(a->macpads_Bsize)));
602
613
/* Copy the complete list of algorithms. The copied list is
603
614
reversed, but that doesn't matter. */
605
for (ar = a->list; ar; ar = ar->next)
608
br = gcry_xmalloc_secure (sizeof *br
609
+ ar->digest->contextsize
610
- sizeof(ar->context));
612
br = gcry_xmalloc (sizeof *br
613
+ ar->digest->contextsize
614
- sizeof (ar->context));
616
sizeof (*br) + ar->digest->contextsize - sizeof (ar->context));
620
/* Add a reference to the module. */
621
ath_mutex_lock (&digests_registered_lock);
622
_gcry_module_use (br->module);
623
ath_mutex_unlock (&digests_registered_lock);
617
for (ar = a->list; ar; ar = ar->next)
620
br = gcry_malloc_secure (sizeof *br
621
+ ar->digest->contextsize
622
- sizeof(ar->context));
624
br = gcry_malloc (sizeof *br
625
+ ar->digest->contextsize
626
- sizeof (ar->context));
629
err = gpg_err_code_from_errno (errno);
634
memcpy (br, ar, (sizeof (*br) + ar->digest->contextsize
635
- sizeof (ar->context)));
639
/* Add a reference to the module. */
640
ath_mutex_lock (&digests_registered_lock);
641
_gcry_module_use (br->module);
642
ath_mutex_unlock (&digests_registered_lock);
646
if (a->debug && !err)
627
647
md_start_debug (bhd, "unknown");
658
678
(*r->digest->init) (&r->context.c);
660
680
if (a->ctx->macpads)
661
md_write (a, a->ctx->macpads, 64); /* inner pad */
681
md_write (a, a->ctx->macpads, a->ctx->macpads_Bsize); /* inner pad */
722
742
gcry_md_write (gcry_md_hd_t hd, const void *inbuf, size_t inlen)
724
md_write (hd, (unsigned char *) inbuf, inlen);
744
md_write (hd, inbuf, inlen);
753
773
_gcry_fatal_error (err, NULL);
754
md_write (om, a->ctx->macpads+64, 64);
774
md_write (om, (a->ctx->macpads)+(a->ctx->macpads_Bsize), a->ctx->macpads_Bsize);
755
775
md_write (om, p, dlen);
757
777
/* Replace our digest with the mac (they have the same size). */
782
802
assert ( keylen <= 64 );
785
memset ( hd->ctx->macpads, 0, 128 );
805
memset ( hd->ctx->macpads, 0, 2*(hd->ctx->macpads_Bsize) );
786
806
ipad = hd->ctx->macpads;
787
opad = hd->ctx->macpads+64;
807
opad = (hd->ctx->macpads)+(hd->ctx->macpads_Bsize);
788
808
memcpy ( ipad, key, keylen );
789
809
memcpy ( opad, key, keylen );
790
for (i=0; i < 64; i++ )
810
for (i=0; i < (hd->ctx->macpads_Bsize); i++ )
811
830
case GCRYCTL_SET_KEY:
812
rc = gcry_err_code (gcry_md_setkey (hd, buf, buflen));
831
rc = gcry_err_code (gcry_md_setkey (hd, buffer, buflen));
814
833
case GCRYCTL_START_DUMP:
815
md_start_debug (hd, (char*)buf);
834
md_start_debug (hd, buffer);
817
836
case GCRYCTL_STOP_DUMP:
837
md_stop_debug ( hd );
821
840
rc = GPG_ERR_INV_OP;
840
859
return gcry_error (rc);
862
/* The new debug interface. If SUFFIX is a string it creates an debug
863
file for the context HD. IF suffix is NULL, the file is closed and
864
debugging is stopped. */
866
gcry_md_debug (gcry_md_hd_t hd, const char *suffix)
869
md_start_debug (hd, suffix);
844
876
/****************
845
877
* if ALGO is null get the digest for the used algo (which should be only one)
1084
1121
case GCRYCTL_GET_ASNOID:
1086
const char unsigned *asn;
1089
asn = md_asn_oid (algo, &asnlen, NULL);
1090
if (buffer && (*nbytes >= asnlen))
1122
/* We need to check that the algo is available because
1123
md_asn_oid would otherwise raise an assertion. */
1124
err = check_digest_algo (algo);
1127
const char unsigned *asn;
1130
asn = md_asn_oid (algo, &asnlen, NULL);
1131
if (buffer && (*nbytes >= asnlen))
1092
1133
memcpy (buffer, asn, asnlen);
1093
1134
*nbytes = asnlen;
1095
else if ((! buffer) && nbytes)
1100
err = GPG_ERR_TOO_SHORT;
1102
err = GPG_ERR_INV_ARG;
1136
else if (!buffer && nbytes)
1141
err = GPG_ERR_TOO_SHORT;
1143
err = GPG_ERR_INV_ARG;
1108
1149
err = GPG_ERR_INV_OP;
1116
md_start_debug( gcry_md_hd_t md, char *suffix )
1157
md_start_debug ( gcry_md_hd_t md, const char *suffix )
1118
1159
static int idx=0;
1121
if( md->ctx->debug ) {
1122
log_debug("Oops: md debug already started\n");
1162
if ( md->ctx->debug )
1164
log_debug("Oops: md debug already started\n");
1126
sprintf(buf, "dbgmd-%05d.%.10s", idx, suffix );
1168
snprintf (buf, DIM(buf)-1, "dbgmd-%05d.%.10s", idx, suffix );
1127
1169
md->ctx->debug = fopen(buf, "w");
1128
if( !md->ctx->debug )
1170
if ( !md->ctx->debug )
1129
1171
log_debug("md debug: can't open %s\n", buf );
1133
1175
md_stop_debug( gcry_md_hd_t md )
1135
if( md->ctx->debug ) {
1137
md_write( md, NULL, 0 );
1138
fclose(md->ctx->debug);
1139
md->ctx->debug = NULL;
1177
if ( md->ctx->debug )
1180
md_write ( md, NULL, 0 );
1181
fclose (md->ctx->debug);
1182
md->ctx->debug = NULL;
1141
1185
#ifdef HAVE_U64_TYPEDEF
1142
1186
{ /* a kludge to pull in the __muldi3 for Solaris */
1143
1187
volatile u32 a = (u32)(ulong)md;
1156
1200
* Returns 1 when the handle works on secured memory
1157
1201
* otherwise 0 is returned. There is no error return.
1158
1202
* GCRYCTL_IS_ALGO_ENABLED:
1159
* Returns 1 if the algo is enanled for that handle.
1203
* Returns 1 if the algo is enabled for that handle.
1160
1204
* The algo must be passed as the address of an int.