1 /* md.c - message digest dispatcher
2 * Copyright (C) 1998, 1999, 2002, 2003, 2006 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 3 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, see <http://www.gnu.org/licenses/>.
29 #include "algorithms.h"
33 * This structure is used for the list of available algorithms
34 * and for the list of algorithms in MD_HANDLE.
36 struct md_digest_list_s {
37 struct md_digest_list_s *next;
43 void (*init)( void *c );
44 void (*write)( void *c, byte *buf, size_t nbytes );
45 void (*final)( void *c );
46 byte *(*read)( void *c );
47 size_t contextsize; /* allocate this amount of context */
48 PROPERLY_ALIGNED_TYPE context;
51 static struct md_digest_list_s *digest_list;
54 static struct md_digest_list_s *
55 new_list_item (int algo,
56 const char *(*get_info)( int, size_t*,byte**, int*, int*,
58 void (**)(void*,byte*,size_t),
59 void (**)(void*),byte *(**)(void*)))
61 struct md_digest_list_s *r;
63 r = xmalloc_clear (sizeof *r );
65 r->name = (*get_info)( algo, &r->contextsize,
66 &r->asnoid, &r->asnlen, &r->mdlen,
67 &r->init, &r->write, &r->final, &r->read );
75 r->next = digest_list;
84 Load all available hash algorithms and return true. Subsequent
88 load_digest_module (void)
90 static int initialized = 0;
96 /* We load them in reverse order so that the most
97 frequently used are the first in the list. */
99 if (!new_list_item (DIGEST_ALGO_SHA512, sha512_get_info))
101 if (!new_list_item (DIGEST_ALGO_SHA384, sha384_get_info))
105 if (!new_list_item (DIGEST_ALGO_SHA256, sha256_get_info))
107 if (!new_list_item (DIGEST_ALGO_SHA224, sha224_get_info))
110 if (!new_list_item (DIGEST_ALGO_MD5, md5_get_info))
112 if (!new_list_item (DIGEST_ALGO_RMD160, rmd160_get_info))
114 if (!new_list_item (DIGEST_ALGO_SHA1, sha1_get_info))
122 * Map a string to the digest algo */
124 string_to_digest_algo( const char *string )
126 struct md_digest_list_s *r;
129 for(r = digest_list; r; r = r->next )
130 if( !ascii_strcasecmp( r->name, string ) )
132 } while( !r && load_digest_module () );
134 /* Didn't find it, so try the Hx format */
135 if(string[0]=='H' || string[0]=='h')
142 val=strtol(string,&endptr,10);
143 if(*string!='\0' && *endptr=='\0' && check_digest_algo(val)==0)
151 * Map a digest algo to a string
154 digest_algo_to_string( int algo )
156 struct md_digest_list_s *r;
159 for(r = digest_list; r; r = r->next )
160 if( r->algo == algo )
162 } while( !r && load_digest_module () );
168 check_digest_algo( int algo )
170 struct md_digest_list_s *r;
173 for(r = digest_list; r; r = r->next )
174 if( r->algo == algo )
176 } while( !r && load_digest_module () );
177 return G10ERR_DIGEST_ALGO;
183 * Open a message digest handle for use with algorithm ALGO.
184 * More algorithms may be added by md_enable(). The initial algorithm
188 md_open( int algo, int secure )
194 bufsize = 512 - sizeof( *hd );
195 hd = xmalloc_secure_clear( sizeof *hd + bufsize );
198 bufsize = 1024 - sizeof( *hd );
199 hd = xmalloc_clear( sizeof *hd + bufsize );
202 hd->bufsize = bufsize+1; /* hd has already one byte allocated */
205 md_enable( hd, algo );
211 md_enable( MD_HANDLE h, int algo )
213 struct md_digest_list_s *r, *ac;
215 for( ac=h->list; ac; ac = ac->next )
216 if( ac->algo == algo )
217 return ; /* already enabled */
218 /* find the algorithm */
220 for(r = digest_list; r; r = r->next )
221 if( r->algo == algo )
223 } while( !r && load_digest_module () );
225 log_error("md_enable: algorithm %d not available\n", algo );
228 /* and allocate a new list entry */
229 ac = h->secure? xmalloc_secure( sizeof *ac + r->contextsize
230 - sizeof(r->context) )
231 : xmalloc( sizeof *ac + r->contextsize
232 - sizeof(r->context) );
236 /* and init this instance */
237 (*ac->init)( &ac->context.c );
242 md_copy( MD_HANDLE a )
245 struct md_digest_list_s *ar, *br;
248 md_write( a, NULL, 0 );
249 b = a->secure ? xmalloc_secure( sizeof *b + a->bufsize - 1 )
250 : xmalloc( sizeof *b + a->bufsize - 1 );
251 memcpy( b, a, sizeof *a + a->bufsize - 1 );
254 /* and now copy the complete list of algorithms */
255 /* I know that the copied list is reversed, but that doesn't matter */
256 for( ar=a->list; ar; ar = ar->next ) {
257 br = a->secure ? xmalloc_secure( sizeof *br + ar->contextsize
258 - sizeof(ar->context) )
259 : xmalloc( sizeof *br + ar->contextsize
260 - sizeof(ar->context) );
261 memcpy( br, ar, sizeof(*br) + ar->contextsize
262 - sizeof(ar->context) );
268 md_start_debug( b, "unknown" );
274 * Reset all contexts and discard any buffered stuff. This may be used
275 * instead of a md_close(); md_open().
278 md_reset( MD_HANDLE a )
280 struct md_digest_list_s *r;
282 a->bufcount = a->finalized = 0;
283 for( r=a->list; r; r = r->next ) {
284 memset( r->context.c, 0, r->contextsize );
285 (*r->init)( &r->context.c );
291 md_close(MD_HANDLE a)
293 struct md_digest_list_s *r, *r2;
299 for(r=a->list; r; r = r2 ) {
308 md_write( MD_HANDLE a, const byte *inbuf, size_t inlen)
310 struct md_digest_list_s *r;
313 if( a->bufcount && fwrite(a->buffer, a->bufcount, 1, a->debug ) != 1 )
315 if( inlen && fwrite(inbuf, inlen, 1, a->debug ) != 1 )
318 for(r=a->list; r; r = r->next ) {
319 (*r->write)( &r->context.c, a->buffer, a->bufcount );
320 /* Fixme: all ->write fnc should take a const byte* */
321 (*r->write)( &r->context.c, (byte*)inbuf, inlen );
329 md_final(MD_HANDLE a)
331 struct md_digest_list_s *r;
337 md_write( a, NULL, 0 );
339 for(r=a->list; r; r = r->next ) {
340 (*r->final)( &r->context.c );
347 * if ALGO is null get the digest for the used algo (which should be only one)
350 md_read( MD_HANDLE a, int algo )
352 struct md_digest_list_s *r;
354 if( !algo ) { /* return the first algorithm */
357 log_debug("more than algorithm in md_read(0)\n");
358 return (*r->read)( &r->context.c );
362 for(r=a->list; r; r = r->next )
363 if( r->algo == algo )
364 return (*r->read)( &r->context.c );
372 * This function combines md_final and md_read but keeps the context
373 * intact. This function can be used to calculate intermediate
374 * digests. The digest is copied into buffer and the digestlength is
375 * returned. If buffer is NULL only the needed size for buffer is returned.
376 * buflen gives the max size of buffer. If the buffer is too shourt to
377 * hold the complete digest, the buffer is filled with as many bytes are
378 * possible and this value is returned.
381 md_digest( MD_HANDLE a, int algo, byte *buffer, int buflen )
383 struct md_digest_list_s *r = NULL;
388 md_write( a, NULL, 0 );
390 if( !algo ) { /* return digest for the first algorithm */
391 if( (r=a->list) && r->next )
392 log_debug("more than algorithm in md_digest(0)\n");
395 for(r=a->list; r; r = r->next )
396 if( r->algo == algo )
405 /* I don't want to change the interface, so I simply work on a copy
406 * the context (extra overhead - should be fixed)*/
407 context = a->secure ? xmalloc_secure( r->contextsize )
408 : xmalloc( r->contextsize );
409 memcpy( context, r->context.c, r->contextsize );
410 (*r->final)( context );
411 digest = (*r->read)( context );
413 if( buflen > r->mdlen )
415 memcpy( buffer, digest, buflen );
423 md_get_algo( MD_HANDLE a )
425 struct md_digest_list_s *r;
429 log_error("WARNING: more than algorithm in md_get_algo()\n");
435 /* Returns true if a given algo is in use in a md */
437 md_algo_present( MD_HANDLE a, int algo )
439 struct md_digest_list_s *r=a->list;
453 * Return the length of the digest
456 md_digest_length( int algo )
458 struct md_digest_list_s *r;
461 for(r = digest_list; r; r = r->next ) {
462 if( r->algo == algo )
465 } while( !r && load_digest_module () );
466 log_error("WARNING: no length for md algo %d\n", algo);
471 /* Hmmm: add a mode to enumerate the OIDs
472 * to make g10/sig-check.c more portable */
474 md_asn_oid( int algo, size_t *asnlen, size_t *mdlen )
476 struct md_digest_list_s *r;
479 for(r = digest_list; r; r = r->next ) {
480 if( r->algo == algo ) {
488 } while( !r && load_digest_module () );
489 log_bug("no asn for md algo %d\n", algo);
495 md_start_debug( MD_HANDLE md, const char *suffix )
501 log_debug("Oops: md debug already started\n");
505 sprintf(buf, "dbgmd-%05d" EXTSEP_S "%.10s", idx, suffix );
506 md->debug = fopen(buf, "wb");
508 log_debug("md debug: can't open %s\n", buf );
512 md_stop_debug( MD_HANDLE md )
516 md_write( md, NULL, 0 );
520 #ifdef HAVE_U64_TYPEDEF
521 { /* a kludge to pull in the __muldi3 for Solaris */
522 volatile u32 a = (u32)(ulong)md;