1997-12-01 11:33:23 +01:00
|
|
|
/* md.c - message digest dispatcher
|
1998-02-24 19:50:46 +01:00
|
|
|
* Copyright (C) 1998 Free Software Foundation, Inc.
|
1997-12-01 11:33:23 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* This file is part of GNUPG.
|
1997-12-01 11:33:23 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* GNUPG is free software; you can redistribute it and/or modify
|
1997-12-01 11:33:23 +01: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 19:50:46 +01:00
|
|
|
* GNUPG is distributed in the hope that it will be useful,
|
1997-12-01 11:33:23 +01: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1998-02-03 13:09:20 +01:00
|
|
|
#include <string.h>
|
1997-12-01 11:33:23 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
#include "errors.h"
|
|
|
|
|
1998-01-12 11:18:17 +01: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 11:33:23 +01:00
|
|
|
md_open( int algo, int secure )
|
|
|
|
{
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE hd;
|
1997-12-01 11:33:23 +01:00
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
hd = secure ? m_alloc_secure_clear( sizeof *hd )
|
|
|
|
: m_alloc_clear( sizeof *hd );
|
1998-02-12 00:22:09 +01:00
|
|
|
hd->secure = secure;
|
1998-01-12 11:18:17 +01:00
|
|
|
if( algo )
|
|
|
|
md_enable( hd, algo );
|
1998-03-09 22:44:06 +01:00
|
|
|
fast_random_poll();
|
1997-12-01 11:33:23 +01:00
|
|
|
return hd;
|
|
|
|
}
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
void
|
|
|
|
md_enable( MD_HANDLE h, int algo )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-01-12 11:18:17 +01:00
|
|
|
if( algo == DIGEST_ALGO_MD5 ) {
|
1998-02-27 18:51:28 +01:00
|
|
|
if( !h->use_md5 )
|
|
|
|
md5_init( &h->md5 );
|
1998-01-12 11:18:17 +01:00
|
|
|
h->use_md5 = 1;
|
|
|
|
}
|
|
|
|
else if( algo == DIGEST_ALGO_RMD160 ) {
|
1998-02-27 18:51:28 +01:00
|
|
|
if( !h->use_rmd160 )
|
|
|
|
rmd160_init( &h->rmd160 );
|
1998-01-12 11:18:17 +01:00
|
|
|
h->use_rmd160 = 1;
|
|
|
|
}
|
|
|
|
else if( algo == DIGEST_ALGO_SHA1 ) {
|
1998-02-27 18:51:28 +01:00
|
|
|
if( !h->use_sha1 )
|
|
|
|
sha1_init( &h->sha1 );
|
1998-01-12 11:18:17 +01:00
|
|
|
h->use_sha1 = 1;
|
|
|
|
}
|
1998-04-30 16:06:01 +02:00
|
|
|
#ifdef WITH_TIGER_HASH
|
|
|
|
else if( algo == DIGEST_ALGO_TIGER ) {
|
|
|
|
if( !h->use_tiger )
|
|
|
|
tiger_init( &h->tiger );
|
|
|
|
h->use_tiger = 1;
|
|
|
|
}
|
|
|
|
#endif
|
1997-12-01 11:33:23 +01:00
|
|
|
else
|
1998-01-12 11:18:17 +01:00
|
|
|
log_bug("md_enable(%d)", algo );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE
|
|
|
|
md_copy( MD_HANDLE a )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-01-12 11:18:17 +01:00
|
|
|
MD_HANDLE b;
|
|
|
|
|
1998-02-12 00:22:09 +01:00
|
|
|
b = a->secure ? m_alloc_secure( sizeof *b )
|
|
|
|
: m_alloc( sizeof *b );
|
1998-01-12 11:18:17 +01:00
|
|
|
memcpy( b, a, sizeof *a );
|
|
|
|
return b;
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
void
|
1998-01-12 11:18:17 +01:00
|
|
|
md_close(MD_HANDLE a)
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
|
|
|
if( !a )
|
|
|
|
return;
|
1998-05-13 19:53:36 +02:00
|
|
|
if( a->debug )
|
|
|
|
md_stop_debug(a);
|
1997-12-01 11:33:23 +01:00
|
|
|
m_free(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-01-12 11:18:17 +01:00
|
|
|
md_write( MD_HANDLE a, byte *inbuf, size_t inlen)
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-02-18 14:58:46 +01:00
|
|
|
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-01-12 11:18:17 +01:00
|
|
|
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 );
|
|
|
|
}
|
1998-04-30 16:06:01 +02:00
|
|
|
#ifdef WITH_TIGER_HASH
|
|
|
|
if( a->use_tiger ) {
|
|
|
|
tiger_write( &a->tiger, a->buffer, a->bufcount );
|
|
|
|
tiger_write( &a->tiger, inbuf, inlen );
|
|
|
|
}
|
|
|
|
#endif
|
1998-01-12 11:18:17 +01:00
|
|
|
if( a->use_md5 ) {
|
|
|
|
md5_write( &a->md5, a->buffer, a->bufcount );
|
|
|
|
md5_write( &a->md5, inbuf, inlen );
|
|
|
|
}
|
|
|
|
a->bufcount = 0;
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
|
1997-12-01 11:33:23 +01:00
|
|
|
void
|
1998-01-12 11:18:17 +01:00
|
|
|
md_final(MD_HANDLE a)
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-01-12 11:18:17 +01:00
|
|
|
if( a->bufcount )
|
|
|
|
md_write( a, NULL, 0 );
|
1998-04-30 16:06:01 +02:00
|
|
|
if( a->use_rmd160 )
|
1998-01-12 11:18:17 +01:00
|
|
|
rmd160_final( &a->rmd160 );
|
|
|
|
if( a->use_sha1 )
|
|
|
|
sha1_final( &a->sha1 );
|
1998-04-30 16:06:01 +02:00
|
|
|
#ifdef WITH_TIGER_HASH
|
|
|
|
if( a->use_tiger )
|
|
|
|
tiger_final( &a->tiger );
|
|
|
|
#endif
|
1998-01-12 11:18:17 +01:00
|
|
|
if( a->use_md5 )
|
|
|
|
md5_final( &a->md5 );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
/****************
|
|
|
|
* if ALGO is null get the digest for the used algo (which should be only one)
|
|
|
|
*/
|
1997-12-01 11:33:23 +01:00
|
|
|
byte *
|
1998-01-12 11:18:17 +01:00
|
|
|
md_read( MD_HANDLE a, int algo )
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
1998-01-12 11:18:17 +01:00
|
|
|
if( !algo ) {
|
|
|
|
if( a->use_rmd160 )
|
|
|
|
return rmd160_read( &a->rmd160 );
|
|
|
|
if( a->use_sha1 )
|
|
|
|
return sha1_read( &a->sha1 );
|
1998-04-30 16:06:01 +02:00
|
|
|
#ifdef WITH_TIGER_HASH
|
|
|
|
if( a->use_tiger )
|
|
|
|
return tiger_read( &a->tiger );
|
|
|
|
#endif
|
1998-01-12 11:18:17 +01:00
|
|
|
if( a->use_md5 )
|
|
|
|
return md5_read( &a->md5 );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
1998-01-12 11:18:17 +01:00
|
|
|
else {
|
|
|
|
if( algo == DIGEST_ALGO_RMD160 )
|
|
|
|
return rmd160_read( &a->rmd160 );
|
|
|
|
if( algo == DIGEST_ALGO_SHA1 )
|
|
|
|
return sha1_read( &a->sha1 );
|
1998-04-30 16:06:01 +02:00
|
|
|
#ifdef WITH_TIGER_HASH
|
|
|
|
if( algo == DIGEST_ALGO_TIGER )
|
|
|
|
return tiger_read( &a->tiger );
|
|
|
|
#endif
|
1998-01-12 11:18:17 +01:00
|
|
|
if( algo == DIGEST_ALGO_MD5 )
|
|
|
|
return md5_read( &a->md5 );
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
1998-01-16 22:15:24 +01:00
|
|
|
BUG();
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|
|
|
|
|
1998-01-12 11:18:17 +01:00
|
|
|
int
|
|
|
|
md_get_algo( MD_HANDLE a )
|
|
|
|
{
|
|
|
|
if( a->use_rmd160 )
|
|
|
|
return DIGEST_ALGO_RMD160;
|
|
|
|
if( a->use_sha1 )
|
|
|
|
return DIGEST_ALGO_SHA1;
|
1998-04-30 16:06:01 +02:00
|
|
|
#ifdef WITH_TIGER_HASH
|
|
|
|
if( a->use_tiger )
|
|
|
|
return DIGEST_ALGO_TIGER;
|
|
|
|
#endif
|
1998-01-12 11:18:17 +01:00
|
|
|
if( a->use_md5 )
|
|
|
|
return DIGEST_ALGO_MD5;
|
|
|
|
return 0;
|
|
|
|
}
|
1997-12-01 11:33:23 +01:00
|
|
|
|
1998-03-19 16:27:29 +01:00
|
|
|
/****************
|
|
|
|
* Return the length of the digest
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
md_digest_length( int algo )
|
|
|
|
{
|
|
|
|
switch( algo ) {
|
1998-04-30 16:06:01 +02:00
|
|
|
case DIGEST_ALGO_TIGER:
|
|
|
|
return 24;
|
1998-03-19 16:27:29 +01:00
|
|
|
case DIGEST_ALGO_RMD160:
|
|
|
|
case DIGEST_ALGO_SHA1:
|
|
|
|
return 20;
|
|
|
|
default:
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-12 15:39:08 +01:00
|
|
|
|
1998-05-26 15:38:00 +02:00
|
|
|
/* fixme: put the oids in a table and add a mode to enumerate the OIDs
|
|
|
|
* to make g10/sig-check.c more portable */
|
1998-02-12 15:39:08 +01:00
|
|
|
const byte *
|
|
|
|
md_asn_oid( int algo, size_t *asnlen, size_t *mdlen )
|
|
|
|
{
|
1998-05-26 15:38:00 +02:00
|
|
|
size_t alen;
|
1998-02-12 15:39:08 +01:00
|
|
|
byte *p;
|
|
|
|
|
|
|
|
if( algo == DIGEST_ALGO_MD5 ) {
|
|
|
|
static byte asn[18] = /* Object ID is 1.2.840.113549.2.5 */
|
|
|
|
{ 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
|
|
|
|
0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
|
1998-05-26 15:38:00 +02:00
|
|
|
alen = DIM(asn); p = asn;
|
1998-02-12 15:39:08 +01:00
|
|
|
}
|
|
|
|
else if( algo == DIGEST_ALGO_RMD160 ) {
|
|
|
|
static byte asn[15] = /* Object ID is 1.3.36.3.2.1 */
|
|
|
|
{ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x24, 0x03,
|
|
|
|
0x02, 0x01, 0x05, 0x00, 0x04, 0x14 };
|
1998-05-26 15:38:00 +02:00
|
|
|
alen = DIM(asn); p = asn;
|
1998-02-12 15:39:08 +01:00
|
|
|
}
|
1998-04-30 16:06:01 +02:00
|
|
|
else if( algo == DIGEST_ALGO_TIGER ) {
|
1998-05-26 15:38:00 +02:00
|
|
|
/* 40: SEQUENCE {
|
|
|
|
* 12: SEQUENCE {
|
|
|
|
* 8: OCTET STRING :54 49 47 45 52 31 39 32
|
|
|
|
* 0: NULL
|
|
|
|
* : }
|
|
|
|
* 24: OCTET STRING
|
|
|
|
* : }
|
|
|
|
*
|
|
|
|
* By replacing the 5th byte (0x04) with 0x16 we would have;
|
|
|
|
* 8: IA5String 'TIGER192'
|
|
|
|
*/
|
|
|
|
static byte asn[18] =
|
|
|
|
{ 0x30, 0x28, 0x30, 0x0c, 0x04, 0x08, 0x54, 0x49, 0x47,
|
|
|
|
0x45, 0x52, 0x31, 0x39, 0x32, 0x05, 0x00, 0x04, 0x18 };
|
|
|
|
alen = DIM(asn); p = asn;
|
1998-04-30 16:06:01 +02:00
|
|
|
}
|
1998-02-12 15:39:08 +01:00
|
|
|
else if( algo == DIGEST_ALGO_SHA1 ) {
|
1998-05-26 15:38:00 +02:00
|
|
|
static byte asn[15] = /* Object ID is 1.3.14.3.2.26 */
|
1998-02-12 15:39:08 +01:00
|
|
|
{ 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
|
|
|
|
0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
|
1998-05-26 15:38:00 +02:00
|
|
|
alen = DIM(asn); p = asn;
|
1998-02-12 15:39:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
log_bug("md_asn_oid(%d)", algo );
|
|
|
|
|
|
|
|
if( asnlen )
|
|
|
|
*asnlen = alen;
|
|
|
|
if( mdlen )
|
1998-05-26 15:38:00 +02:00
|
|
|
*mdlen = p[alen-1];
|
1998-02-12 15:39:08 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-13 19:53:36 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|