1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-23 10:29:58 +01:00
gnupg/cipher/md.c

428 lines
9.1 KiB
C
Raw Normal View History

1997-12-01 10:33:23 +00:00
/* md.c - message digest dispatcher
1998-02-24 18:50:46 +00:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-12-01 10:33:23 +00:00
*
1998-02-24 18:50:46 +00:00
* This file is part of GNUPG.
1997-12-01 10:33:23 +00:00
*
1998-02-24 18:50:46 +00:00
* GNUPG is free software; you can redistribute it and/or modify
1997-12-01 10:33:23 +00:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
1998-02-24 18:50:46 +00:00
* GNUPG is distributed in the hope that it will be useful,
1997-12-01 10:33:23 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
1998-06-16 15:13:28 +00:00
#define DEFINES_MD_HANDLE 1
1997-12-01 10:33:23 +00:00
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
1998-02-03 12:09:20 +00:00
#include <string.h>
1997-12-01 10:33:23 +00:00
#include <errno.h>
#include "util.h"
#include "cipher.h"
#include "errors.h"
1998-06-16 15:13:28 +00:00
#include "dynload.h"
#include "md5.h"
#include "sha1.h"
#include "rmd.h"
1997-12-01 10:33:23 +00:00
1998-06-13 17:00:02 +00:00
1998-06-16 15:13:28 +00:00
/****************
* This structure is used for the list of available algorithms
* and for the list of algorithms in MD_HANDLE.
*/
struct md_digest_list_s {
struct md_digest_list_s *next;
const char *name;
int algo;
byte *asnoid;
int asnlen;
int mdlen;
void (*init)( void *c );
void (*write)( void *c, byte *buf, size_t nbytes );
void (*final)( void *c );
byte *(*read)( void *c );
size_t contextsize; /* allocate this amount of context */
char context[1];
};
static struct md_digest_list_s *digest_list;
static struct md_digest_list_s *
new_list_item( int algo,
const char *(*get_info)( int, size_t*,byte**, int*, int*,
void (**)(void*),
void (**)(void*,byte*,size_t),
void (**)(void*),byte *(**)(void*)) )
{
struct md_digest_list_s *r;
r = m_alloc_clear( sizeof *r );
r->algo = algo,
r->name = (*get_info)( algo, &r->contextsize,
&r->asnoid, &r->asnlen, &r->mdlen,
&r->init, &r->write, &r->final, &r->read );
if( !r->name ) {
m_free(r);
r = NULL;
}
return r;
}
1998-06-13 17:00:02 +00:00
1998-06-16 15:13:28 +00:00
/****************
* Put the static entries into the table.
*/
static void
setup_digest_list()
{
struct md_digest_list_s *r;
r = new_list_item( DIGEST_ALGO_MD5, md5_get_info );
if( r ) { r->next = digest_list; digest_list = r; }
1998-06-13 17:00:02 +00:00
1998-06-16 15:13:28 +00:00
r = new_list_item( DIGEST_ALGO_RMD160, rmd160_get_info );
if( r ) { r->next = digest_list; digest_list = r; }
r = new_list_item( DIGEST_ALGO_SHA1, sha1_get_info );
if( r ) { r->next = digest_list; digest_list = r; }
}
/****************
* Try to load all modules and return true if new modules are available
*/
static int
load_digest_modules()
{
static int done = 0;
static int initialized = 0;
struct md_digest_list_s *r;
void *context = NULL;
int algo;
int any = 0;
const char *(*get_info)( int, size_t*,byte**, int*, int*,
void (**)(void*),
void (**)(void*,byte*,size_t),
void (**)(void*),byte *(**)(void*));
if( !initialized ) {
setup_digest_list(); /* load static modules on the first call */
initialized = 1;
return 1;
}
if( done )
return 0;
done = 1;
while( enum_gnupgext_digests( &context, &algo, &get_info ) ) {
for(r=digest_list; r; r = r->next )
if( r->algo == algo )
break;
if( r ) {
log_info("skipping digest %d: already loaded\n", algo );
continue;
}
r = new_list_item( algo, get_info );
if( ! r ) {
log_info("skipping digest %d: no name\n", algo );
continue;
}
/* put it into the list */
if( g10_opt_verbose > 1 )
log_info("loaded digest %d\n", algo);
r->next = digest_list;
digest_list = r;
any = 1;
}
enum_gnupgext_digests( &context, NULL, NULL );
return any;
}
1998-06-13 17:00:02 +00:00
/****************
* Map a string to the digest algo
*/
int
string_to_digest_algo( const char *string )
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
1998-06-13 17:00:02 +00:00
1998-06-16 15:13:28 +00:00
do {
for(r = digest_list; r; r = r->next )
if( !stricmp( r->name, string ) )
return r->algo;
} while( !r && load_digest_modules() );
1998-06-13 17:00:02 +00:00
return 0;
}
/****************
* Map a digest algo to a string
*/
const char *
digest_algo_to_string( int algo )
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
1998-06-13 17:00:02 +00:00
1998-06-16 15:13:28 +00:00
do {
for(r = digest_list; r; r = r->next )
if( r->algo == algo )
return r->name;
} while( !r && load_digest_modules() );
1998-06-13 17:00:02 +00:00
return NULL;
}
int
check_digest_algo( int algo )
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
do {
for(r = digest_list; r; r = r->next )
if( r->algo == algo )
return 0;
} while( !r && load_digest_modules() );
return G10ERR_DIGEST_ALGO;
1998-06-13 17:00:02 +00:00
}
1998-01-12 10:18:17 +00:00
/****************
* 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
1997-12-01 10:33:23 +00:00
md_open( int algo, int secure )
{
1998-01-12 10:18:17 +00:00
MD_HANDLE hd;
hd = secure ? m_alloc_secure_clear( sizeof *hd )
: m_alloc_clear( sizeof *hd );
1998-02-11 23:22:09 +00:00
hd->secure = secure;
1998-01-12 10:18:17 +00:00
if( algo )
md_enable( hd, algo );
1998-03-09 21:44:06 +00:00
fast_random_poll();
1997-12-01 10:33:23 +00:00
return hd;
}
1998-01-12 10:18:17 +00:00
void
md_enable( MD_HANDLE h, int algo )
1997-12-01 10:33:23 +00:00
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r, *ac;
for( ac=h->list; ac; ac = ac->next )
if( ac->algo == algo )
return ; /* already enabled */
/* find the algorithm */
do {
for(r = digest_list; r; r = r->next )
if( r->algo == algo )
break;
} while( !r && load_digest_modules() );
if( !r ) {
log_error("md_enable: algorithm %d not available\n", algo );
return;
1998-01-12 10:18:17 +00:00
}
1998-06-16 15:13:28 +00:00
/* and allocate a new list entry */
ac = h->secure? m_alloc_secure( sizeof *ac + r->contextsize )
: m_alloc( sizeof *ac + r->contextsize );
*ac = *r;
ac->next = h->list;
h->list = ac;
/* and init this instance */
(*ac->init)( &ac->context );
1997-12-01 10:33:23 +00:00
}
1998-01-12 10:18:17 +00:00
MD_HANDLE
md_copy( MD_HANDLE a )
1997-12-01 10:33:23 +00:00
{
1998-01-12 10:18:17 +00:00
MD_HANDLE b;
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *ar, *br;
1998-01-12 10:18:17 +00:00
1998-02-11 23:22:09 +00:00
b = a->secure ? m_alloc_secure( sizeof *b )
: m_alloc( sizeof *b );
1998-01-12 10:18:17 +00:00
memcpy( b, a, sizeof *a );
1998-06-16 15:13:28 +00:00
b->list = NULL;
/* and now copy the compelte list of algorithms */
/* I know that the copied list is reversed, but that doesn't matter */
for( ar=a->list; ar; ar = ar->next ) {
br = a->secure ? m_alloc_secure( sizeof *br + ar->contextsize )
: m_alloc( sizeof *br + ar->contextsize );
memcpy( br, ar, sizeof(*br) + ar->contextsize );
br->next = b->list;
b->list = br;
}
1998-01-12 10:18:17 +00:00
return b;
1997-12-01 10:33:23 +00:00
}
1998-01-12 10:18:17 +00:00
1997-12-01 10:33:23 +00:00
void
1998-01-12 10:18:17 +00:00
md_close(MD_HANDLE a)
1997-12-01 10:33:23 +00:00
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r, *r2;
1997-12-01 10:33:23 +00:00
if( !a )
return;
1998-05-13 17:53:36 +00:00
if( a->debug )
md_stop_debug(a);
1998-06-16 15:13:28 +00:00
for(r=a->list; r; r = r2 ) {
r2 = r->next;
m_free(r);
}
1997-12-01 10:33:23 +00:00
m_free(a);
}
void
1998-01-12 10:18:17 +00:00
md_write( MD_HANDLE a, byte *inbuf, size_t inlen)
1997-12-01 10:33:23 +00:00
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
if( a->debug ) {
if( a->bufcount && fwrite(a->buffer, a->bufcount, 1, a->debug ) != 1 )
BUG();
if( inlen && fwrite(inbuf, inlen, 1, a->debug ) != 1 )
BUG();
}
1998-06-16 15:13:28 +00:00
for(r=a->list; r; r = r->next ) {
(*r->write)( &r->context, a->buffer, a->bufcount );
(*r->write)( &r->context, inbuf, inlen );
1998-01-12 10:18:17 +00:00
}
a->bufcount = 0;
1997-12-01 10:33:23 +00:00
}
1998-01-12 10:18:17 +00:00
1997-12-01 10:33:23 +00:00
void
1998-01-12 10:18:17 +00:00
md_final(MD_HANDLE a)
1997-12-01 10:33:23 +00:00
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
1998-01-12 10:18:17 +00:00
if( a->bufcount )
md_write( a, NULL, 0 );
1998-06-16 15:13:28 +00:00
1998-06-25 10:19:08 +00:00
for(r=a->list; r; r = r->next ) {
1998-06-16 15:13:28 +00:00
(*r->final)( &r->context );
1998-06-25 10:19:08 +00:00
}
1997-12-01 10:33:23 +00:00
}
1998-01-12 10:18:17 +00:00
/****************
* if ALGO is null get the digest for the used algo (which should be only one)
*/
1997-12-01 10:33:23 +00:00
byte *
1998-01-12 10:18:17 +00:00
md_read( MD_HANDLE a, int algo )
1997-12-01 10:33:23 +00:00
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
if( !algo ) { /* return the first algorithm */
if( (r=a->list) ) {
if( r->next )
log_error("warning: more than algorithm in md_read(0)\n");
return (*r->read)( &r->context );
}
1997-12-01 10:33:23 +00:00
}
1998-01-12 10:18:17 +00:00
else {
1998-06-16 15:13:28 +00:00
for(r=a->list; r; r = r->next )
if( r->algo == algo )
return (*r->read)( &r->context );
1997-12-01 10:33:23 +00:00
}
1998-01-16 21:15:24 +00:00
BUG();
1998-06-16 15:13:28 +00:00
return NULL;
1997-12-01 10:33:23 +00:00
}
1998-01-12 10:18:17 +00:00
int
md_get_algo( MD_HANDLE a )
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
if( (r=a->list) ) {
if( r->next )
log_error("warning: more than algorithm in md_get_algo()\n");
return r->algo;
}
1998-01-12 10:18:17 +00:00
return 0;
}
1997-12-01 10:33:23 +00:00
1998-03-19 15:27:29 +00:00
/****************
* Return the length of the digest
*/
int
md_digest_length( int algo )
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
do {
for(r = digest_list; r; r = r->next ) {
if( r->algo == algo )
return r->mdlen;
}
} while( !r && load_digest_modules() );
log_error("warning: no length for md algo %d\n", algo);
return 0;
1998-03-19 15:27:29 +00:00
}
1998-02-12 14:39:08 +00:00
1998-06-16 15:13:28 +00:00
/* fixme: add a mode to enumerate the OIDs
* to make g10/sig-check.c more portable */
1998-02-12 14:39:08 +00:00
const byte *
md_asn_oid( int algo, size_t *asnlen, size_t *mdlen )
{
1998-06-16 15:13:28 +00:00
struct md_digest_list_s *r;
do {
for(r = digest_list; r; r = r->next ) {
if( r->algo == algo ) {
if( asnlen )
*asnlen = r->asnlen;
if( mdlen )
*mdlen = r->mdlen;
return r->asnoid;
}
}
} while( !r && load_digest_modules() );
log_bug("warning: no asn for md algo %d\n", algo);
return NULL;
1998-02-12 14:39:08 +00:00
}
1998-05-13 17:53:36 +00:00
void
md_start_debug( MD_HANDLE md, const char *suffix )
{
static int index=0;
char buf[25];
if( md->debug ) {
log_debug("Oops: md debug already started\n");
return;
}
index++;
sprintf(buf, "dbgmd-%05d.%.10s", index, suffix );
md->debug = fopen(buf, "w");
if( !md->debug )
log_debug("md debug: can't open %s\n", buf );
}
void
md_stop_debug( MD_HANDLE md )
{
if( md->debug ) {
fclose(md->debug);
md->debug = NULL;
}
}