1
/* md.c - message digest dispatcher
2
* Copyright (C) 1998, 1999, 2002, 2003 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30
#include "algorithms.h"
34
* This structure is used for the list of available algorithms
35
* and for the list of algorithms in MD_HANDLE.
37
struct md_digest_list_s {
38
struct md_digest_list_s *next;
44
void (*init)( void *c );
45
void (*write)( void *c, byte *buf, size_t nbytes );
46
void (*final)( void *c );
47
byte *(*read)( void *c );
48
size_t contextsize; /* allocate this amount of context */
49
PROPERLY_ALIGNED_TYPE context;
52
static struct md_digest_list_s *digest_list;
55
static struct md_digest_list_s *
56
new_list_item (int algo,
57
const char *(*get_info)( int, size_t*,byte**, int*, int*,
59
void (**)(void*,byte*,size_t),
60
void (**)(void*),byte *(**)(void*)))
62
struct md_digest_list_s *r;
64
r = m_alloc_clear (sizeof *r );
66
r->name = (*get_info)( algo, &r->contextsize,
67
&r->asnoid, &r->asnlen, &r->mdlen,
68
&r->init, &r->write, &r->final, &r->read );
76
r->next = digest_list;
85
Load all available hash algorithms and return true. Subsequent
89
load_digest_module (void)
91
static int initialized = 0;
97
/* We load them in reverse order so that the most
98
frequently used are the first in the list. */
100
if (!new_list_item (DIGEST_ALGO_SHA512, sha512_get_info))
102
if (!new_list_item (DIGEST_ALGO_SHA384, sha384_get_info))
106
if (!new_list_item (DIGEST_ALGO_SHA256, sha256_get_info))
109
if (!new_list_item (DIGEST_ALGO_MD5, md5_get_info))
111
if (!new_list_item (DIGEST_ALGO_RMD160, rmd160_get_info))
113
if (!new_list_item (DIGEST_ALGO_SHA1, sha1_get_info))
121
* Map a string to the digest algo */
123
string_to_digest_algo( const char *string )
125
struct md_digest_list_s *r;
128
for(r = digest_list; r; r = r->next )
129
if( !ascii_strcasecmp( r->name, string ) )
131
} while( !r && load_digest_module () );
133
/* Didn't find it, so try the Hx format */
134
if(string[0]=='H' || string[0]=='h')
141
val=strtol(string,&endptr,10);
142
if(*string!='\0' && *endptr=='\0' && check_digest_algo(val)==0)
150
* Map a digest algo to a string
153
digest_algo_to_string( int algo )
155
struct md_digest_list_s *r;
158
for(r = digest_list; r; r = r->next )
159
if( r->algo == algo )
161
} while( !r && load_digest_module () );
167
check_digest_algo( int algo )
169
struct md_digest_list_s *r;
172
for(r = digest_list; r; r = r->next )
173
if( r->algo == algo )
175
} while( !r && load_digest_module () );
176
return G10ERR_DIGEST_ALGO;
182
* Open a message digest handle for use with algorithm ALGO.
183
* More algorithms may be added by md_enable(). The initial algorithm
187
md_open( int algo, int secure )
193
bufsize = 512 - sizeof( *hd );
194
hd = m_alloc_secure_clear( sizeof *hd + bufsize );
197
bufsize = 1024 - sizeof( *hd );
198
hd = m_alloc_clear( sizeof *hd + bufsize );
201
hd->bufsize = bufsize+1; /* hd has already one byte allocated */
204
md_enable( hd, algo );
210
md_enable( MD_HANDLE h, int algo )
212
struct md_digest_list_s *r, *ac;
214
for( ac=h->list; ac; ac = ac->next )
215
if( ac->algo == algo )
216
return ; /* already enabled */
217
/* find the algorithm */
219
for(r = digest_list; r; r = r->next )
220
if( r->algo == algo )
222
} while( !r && load_digest_module () );
224
log_error("md_enable: algorithm %d not available\n", algo );
227
/* and allocate a new list entry */
228
ac = h->secure? m_alloc_secure( sizeof *ac + r->contextsize
229
- sizeof(r->context) )
230
: m_alloc( sizeof *ac + r->contextsize
231
- sizeof(r->context) );
235
/* and init this instance */
236
(*ac->init)( &ac->context.c );
241
md_copy( MD_HANDLE a )
244
struct md_digest_list_s *ar, *br;
247
md_write( a, NULL, 0 );
248
b = a->secure ? m_alloc_secure( sizeof *b + a->bufsize - 1 )
249
: m_alloc( sizeof *b + a->bufsize - 1 );
250
memcpy( b, a, sizeof *a + a->bufsize - 1 );
253
/* and now copy the complete list of algorithms */
254
/* I know that the copied list is reversed, but that doesn't matter */
255
for( ar=a->list; ar; ar = ar->next ) {
256
br = a->secure ? m_alloc_secure( sizeof *br + ar->contextsize
257
- sizeof(ar->context) )
258
: m_alloc( sizeof *br + ar->contextsize
259
- sizeof(ar->context) );
260
memcpy( br, ar, sizeof(*br) + ar->contextsize
261
- sizeof(ar->context) );
267
md_start_debug( b, "unknown" );
273
* Reset all contexts and discard any buffered stuff. This may be used
274
* instead of a md_close(); md_open().
277
md_reset( MD_HANDLE a )
279
struct md_digest_list_s *r;
281
a->bufcount = a->finalized = 0;
282
for( r=a->list; r; r = r->next ) {
283
memset( r->context.c, 0, r->contextsize );
284
(*r->init)( &r->context.c );
290
md_close(MD_HANDLE a)
292
struct md_digest_list_s *r, *r2;
298
for(r=a->list; r; r = r2 ) {
307
md_write( MD_HANDLE a, const byte *inbuf, size_t inlen)
309
struct md_digest_list_s *r;
312
if( a->bufcount && fwrite(a->buffer, a->bufcount, 1, a->debug ) != 1 )
314
if( inlen && fwrite(inbuf, inlen, 1, a->debug ) != 1 )
317
for(r=a->list; r; r = r->next ) {
318
(*r->write)( &r->context.c, a->buffer, a->bufcount );
319
/* Fixme: all ->write fnc should take a const byte* */
320
(*r->write)( &r->context.c, (byte*)inbuf, inlen );
328
md_final(MD_HANDLE a)
330
struct md_digest_list_s *r;
336
md_write( a, NULL, 0 );
338
for(r=a->list; r; r = r->next ) {
339
(*r->final)( &r->context.c );
346
* if ALGO is null get the digest for the used algo (which should be only one)
349
md_read( MD_HANDLE a, int algo )
351
struct md_digest_list_s *r;
353
if( !algo ) { /* return the first algorithm */
356
log_debug("more than algorithm in md_read(0)\n");
357
return (*r->read)( &r->context.c );
361
for(r=a->list; r; r = r->next )
362
if( r->algo == algo )
363
return (*r->read)( &r->context.c );
371
* This function combines md_final and md_read but keeps the context
372
* intact. This function can be used to calculate intermediate
373
* digests. The digest is copied into buffer and the digestlength is
374
* returned. If buffer is NULL only the needed size for buffer is returned.
375
* buflen gives the max size of buffer. If the buffer is too shourt to
376
* hold the complete digest, the buffer is filled with as many bytes are
377
* possible and this value is returned.
380
md_digest( MD_HANDLE a, int algo, byte *buffer, int buflen )
382
struct md_digest_list_s *r = NULL;
387
md_write( a, NULL, 0 );
389
if( !algo ) { /* return digest for the first algorithm */
390
if( (r=a->list) && r->next )
391
log_debug("more than algorithm in md_digest(0)\n");
394
for(r=a->list; r; r = r->next )
395
if( r->algo == algo )
404
/* I don't want to change the interface, so I simply work on a copy
405
* the context (extra overhead - should be fixed)*/
406
context = a->secure ? m_alloc_secure( r->contextsize )
407
: m_alloc( r->contextsize );
408
memcpy( context, r->context.c, r->contextsize );
409
(*r->final)( context );
410
digest = (*r->read)( context );
412
if( buflen > r->mdlen )
414
memcpy( buffer, digest, buflen );
422
md_get_algo( MD_HANDLE a )
424
struct md_digest_list_s *r;
428
log_error("WARNING: more than algorithm in md_get_algo()\n");
434
/* Returns true if a given algo is in use in a md */
436
md_algo_present( MD_HANDLE a, int algo )
438
struct md_digest_list_s *r=a->list;
452
* Return the length of the digest
455
md_digest_length( int algo )
457
struct md_digest_list_s *r;
460
for(r = digest_list; r; r = r->next ) {
461
if( r->algo == algo )
464
} while( !r && load_digest_module () );
465
log_error("WARNING: no length for md algo %d\n", algo);
470
/* Hmmm: add a mode to enumerate the OIDs
471
* to make g10/sig-check.c more portable */
473
md_asn_oid( int algo, size_t *asnlen, size_t *mdlen )
475
struct md_digest_list_s *r;
478
for(r = digest_list; r; r = r->next ) {
479
if( r->algo == algo ) {
487
} while( !r && load_digest_module () );
488
log_bug("no asn for md algo %d\n", algo);
494
md_start_debug( MD_HANDLE md, const char *suffix )
500
log_debug("Oops: md debug already started\n");
504
sprintf(buf, "dbgmd-%05d" EXTSEP_S "%.10s", idx, suffix );
505
md->debug = fopen(buf, "wb");
507
log_debug("md debug: can't open %s\n", buf );
511
md_stop_debug( MD_HANDLE md )
515
md_write( md, NULL, 0 );
519
#ifdef HAVE_U64_TYPEDEF
520
{ /* a kludge to pull in the __muldi3 for Solaris */
521
volatile u32 a = (u32)(ulong)md;