summaryrefslogtreecommitdiff
path: root/cipher/md.c
blob: e88a45c77c6041615afbd685bc7e7fd89868e84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* md.c  -  message digest dispatcher
 *	Copyright (c) 1997 by Werner Koch (dd9jn)
 *
 * This file is part of G10.
 *
 * G10 is free software; you can redistribute it and/or modify
 * 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.
 *
 * G10 is distributed in the hope that it will be useful,
 * 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>
#include <errno.h>
#include "util.h"
#include "cipher.h"
#include "errors.h"


/****************
 * 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 = secure ? m_alloc_secure_clear( sizeof *hd )
		: m_alloc_clear( sizeof *hd );
    if( algo )
	md_enable( hd, algo );
    return hd;
}

void
md_enable( MD_HANDLE h, int algo )
{
    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("md_enable(%d)", algo );
}


MD_HANDLE
md_copy( MD_HANDLE a )
{
    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)
{
    if( !a )
	return;
    m_free(a);
}


void
md_write( MD_HANDLE a, byte *inbuf, size_t inlen)
{
    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_final(MD_HANDLE a)
{
    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_read( MD_HANDLE a, int algo )
{
    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( 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 );
    }
    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;
}