diff options
author | Werner Koch <wk@gnupg.org> | 1998-01-12 11:18:17 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 1998-01-12 11:18:17 +0100 |
commit | ed3609258828942808702a07ef2986d7328efa3f (patch) | |
tree | 185f17c055b38fee37bc0090789dcf96a1535c8c /cipher/md.c | |
parent | patchlevel 2 (diff) | |
download | gnupg2-ed3609258828942808702a07ef2986d7328efa3f.tar.xz gnupg2-ed3609258828942808702a07ef2986d7328efa3f.zip |
started with trust stuff
Diffstat (limited to 'cipher/md.c')
-rw-r--r-- | cipher/md.c | 197 |
1 files changed, 93 insertions, 104 deletions
diff --git a/cipher/md.c b/cipher/md.c index 67f5356a8..e88a45c77 100644 --- a/cipher/md.c +++ b/cipher/md.c @@ -27,146 +27,135 @@ #include "errors.h" -int -md_okay( int algo ) -{ - return check_digest_algo( algo ); -} - - -MD_HANDLE * +/**************** + * Open a message digest handle for use with algorithm ALGO. + * More algorithms may be added by md_enable(). The initial algorithm + * may be 0. + */ +MD_HANDLE md_open( int algo, int secure ) { - MD_HANDLE *hd; - - hd = m_alloc( sizeof *hd + 19 ); - hd->algo = algo; - hd->datalen = 0; - if( algo == DIGEST_ALGO_MD5 ) - hd->u.md5 = md5_open( secure ); - else if( algo == DIGEST_ALGO_RMD160 ) - hd->u.rmd= rmd160_open( secure ); - else if( algo == DIGEST_ALGO_SHA1 ) - hd->u.sha1 = sha1_open( secure ); - else - return NULL; + MD_HANDLE hd; + hd = secure ? m_alloc_secure_clear( sizeof *hd ) + : m_alloc_clear( sizeof *hd ); + if( algo ) + md_enable( hd, algo ); return hd; } - -MD_HANDLE * -md_copy( MD_HANDLE *a ) +void +md_enable( MD_HANDLE h, int algo ) { - MD_HANDLE *hd; - - hd = m_alloc( sizeof *hd + 19 ); - hd->algo = a->algo; - hd->datalen = 0; - if( a->algo == DIGEST_ALGO_MD5 ) - hd->u.md5 = md5_copy( a->u.md5 ); - else if( a->algo == DIGEST_ALGO_RMD160 ) - hd->u.rmd= rmd160_copy( a->u.rmd ); - else if( a->algo == DIGEST_ALGO_SHA1 ) - hd->u.sha1= sha1_copy( a->u.sha1 ); + if( algo == DIGEST_ALGO_MD5 ) { + md5_init( &h->md5 ); + h->use_md5 = 1; + } + else if( algo == DIGEST_ALGO_RMD160 ) { + rmd160_init( &h->rmd160 ); + h->use_rmd160 = 1; + } + else if( algo == DIGEST_ALGO_SHA1 ) { + sha1_init( &h->sha1 ); + h->use_sha1 = 1; + } else - log_bug(NULL); - return hd; + log_bug("md_enable(%d)", algo ); } -/* used for a BAD Kludge in rmd160.c, md5.c */ -MD_HANDLE * -md_makecontainer( int algo ) +MD_HANDLE +md_copy( MD_HANDLE a ) { - MD_HANDLE *hd; - - hd = m_alloc( sizeof *hd + 19 ); - hd->algo = algo; - hd->datalen = 0; - if( algo == DIGEST_ALGO_MD5 ) - ; - else if( algo == DIGEST_ALGO_RMD160 ) - ; - else if( algo == DIGEST_ALGO_SHA1 ) - ; - else - log_bug(NULL); - return hd; + MD_HANDLE b; + + b = m_is_secure(a)? m_alloc_secure( sizeof *b ) + : m_alloc( sizeof *b ); + memcpy( b, a, sizeof *a ); + return b; } + void -md_close(MD_HANDLE *a) +md_close(MD_HANDLE a) { if( !a ) return; - if( a->algo == DIGEST_ALGO_MD5 ) - md5_close( a->u.md5 ); - else if( a->algo == DIGEST_ALGO_RMD160 ) - rmd160_close( a->u.rmd ); - else if( a->algo == DIGEST_ALGO_SHA1 ) - sha1_close( a->u.sha1 ); - else - log_bug(NULL); m_free(a); } void -md_write( MD_HANDLE *a, byte *inbuf, size_t inlen) +md_write( MD_HANDLE a, byte *inbuf, size_t inlen) { - if( a->algo == DIGEST_ALGO_MD5 ) - md5_write( a->u.md5, inbuf, inlen ); - else if( a->algo == DIGEST_ALGO_RMD160 ) - rmd160_write( a->u.rmd, inbuf, inlen ); - else if( a->algo == DIGEST_ALGO_SHA1 ) - sha1_write( a->u.sha1, inbuf, inlen ); - else - log_bug(NULL); + if( a->use_rmd160 ) { + rmd160_write( &a->rmd160, a->buffer, a->bufcount ); + rmd160_write( &a->rmd160, inbuf, inlen ); + } + if( a->use_sha1 ) { + sha1_write( &a->sha1, a->buffer, a->bufcount ); + sha1_write( &a->sha1, inbuf, inlen ); + } + if( a->use_md5 ) { + md5_write( &a->md5, a->buffer, a->bufcount ); + md5_write( &a->md5, inbuf, inlen ); + } + a->bufcount = 0; } + void -md_putchar( MD_HANDLE *a, int c ) +md_final(MD_HANDLE a) { - if( a->algo == DIGEST_ALGO_MD5 ) - md5_putchar( a->u.md5, c ); - else if( a->algo == DIGEST_ALGO_RMD160 ) - rmd160_putchar( a->u.rmd, c ); - else if( a->algo == DIGEST_ALGO_SHA1 ) - sha1_putchar( a->u.sha1, c ); - else - log_bug(NULL); + if( a->bufcount ) + md_write( a, NULL, 0 ); + if( a->use_rmd160 ) { + byte *p; + rmd160_final( &a->rmd160 ); + p = rmd160_read( &a->rmd160 ); + } + if( a->use_sha1 ) + sha1_final( &a->sha1 ); + if( a->use_md5 ) + md5_final( &a->md5 ); } +/**************** + * if ALGO is null get the digest for the used algo (which should be only one) + */ byte * -md_final(MD_HANDLE *a) +md_read( MD_HANDLE a, int algo ) { - if( a->algo == DIGEST_ALGO_MD5 ) { - if( !a->datalen ) { - md5_final( a->u.md5 ); - memcpy(a->data, md5_read( a->u.md5 ), 16); - a->datalen = 16; - } - return a->data; + if( !algo ) { + if( a->use_rmd160 ) + return rmd160_read( &a->rmd160 ); + if( a->use_sha1 ) + return sha1_read( &a->sha1 ); + if( a->use_md5 ) + return md5_read( &a->md5 ); } - else if( a->algo == DIGEST_ALGO_RMD160 ) { - if( !a->datalen ) { - memcpy(a->data, rmd160_final( a->u.rmd ), 20 ); - a->datalen = 20; - } - return a->data; + else { + if( algo == DIGEST_ALGO_RMD160 ) + return rmd160_read( &a->rmd160 ); + if( algo == DIGEST_ALGO_SHA1 ) + return sha1_read( &a->sha1 ); + if( algo == DIGEST_ALGO_MD5 ) + return md5_read( &a->md5 ); } - else if( a->algo == DIGEST_ALGO_SHA1 ) { - if( !a->datalen ) { - memcpy(a->data, sha1_final( a->u.sha1 ), 20 ); - a->datalen = 20; - } - return a->data; - } - else - log_bug(NULL); + log_bug(NULL); } +int +md_get_algo( MD_HANDLE a ) +{ + if( a->use_rmd160 ) + return DIGEST_ALGO_RMD160; + if( a->use_sha1 ) + return DIGEST_ALGO_SHA1; + if( a->use_md5 ) + return DIGEST_ALGO_MD5; + return 0; +} |