1 |
werner |
46 |
/* md.c - message digest wrapper |
2 |
|
|
* Copyright (C) 2003, 2004 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of GPGLIB. |
5 |
|
|
* |
6 |
|
|
* GPGLIB 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 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* GPGLIB 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. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with GPGLIB; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <windows.h> |
27 |
|
|
#include <stdio.h> |
28 |
|
|
#include <malloc.h> |
29 |
|
|
#include <sys/types.h> |
30 |
|
|
|
31 |
|
|
#include "openpgp.h" |
32 |
|
|
#include "md.h" |
33 |
|
|
|
34 |
|
|
struct gpgm_md_s { |
35 |
|
|
void * ctx; |
36 |
|
|
int algo; |
37 |
|
|
void (*write)( void *ctx, unsigned char *inbuf, size_t inlen ); |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
gpg_md_t |
42 |
|
|
gpg_md_open( int algo ) |
43 |
|
|
{ |
44 |
|
|
gpg_md_t md; |
45 |
|
|
|
46 |
twoaday |
439 |
if (algo != MD_SHA1 && algo != MD_MD5 && algo != MD_RMD160) |
47 |
werner |
46 |
return NULL; |
48 |
|
|
md = calloc (1, sizeof * md); |
49 |
twoaday |
439 |
if (!md) |
50 |
werner |
46 |
return NULL; |
51 |
twoaday |
439 |
switch (algo) { |
52 |
werner |
46 |
case MD_MD5: |
53 |
|
|
md->ctx = calloc( 1, sizeof (MD5_CONTEXT) ); |
54 |
|
|
if( !md->ctx ) { |
55 |
|
|
free( md ); |
56 |
|
|
return NULL; |
57 |
|
|
} |
58 |
|
|
md5_init( (MD5_CONTEXT *)md->ctx ); |
59 |
|
|
md->write = md5_write; |
60 |
|
|
break; |
61 |
|
|
|
62 |
|
|
case MD_SHA1: |
63 |
|
|
md->ctx = calloc( 1, sizeof (SHA1_CONTEXT) ); |
64 |
|
|
if( !md->ctx ) { |
65 |
|
|
free( md ); |
66 |
|
|
return NULL; |
67 |
|
|
} |
68 |
|
|
sha1_init( (SHA1_CONTEXT *)md->ctx ); |
69 |
|
|
md->write = sha1_write; |
70 |
|
|
break; |
71 |
|
|
|
72 |
|
|
case MD_RMD160: |
73 |
|
|
md->ctx = calloc( 1, sizeof (RMD160_CONTEXT) ); |
74 |
|
|
if( !md->ctx ) { |
75 |
|
|
free( md ); |
76 |
|
|
return NULL; |
77 |
|
|
} |
78 |
|
|
rmd160_init( (RMD160_CONTEXT *)md->ctx ); |
79 |
|
|
md->write = rmd160_write; |
80 |
|
|
break; |
81 |
twoaday |
439 |
|
82 |
werner |
46 |
default: |
83 |
|
|
free( md ); |
84 |
|
|
return NULL; |
85 |
|
|
} |
86 |
|
|
md->algo = algo; |
87 |
|
|
return md; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
void |
92 |
|
|
gpg_md_close( gpg_md_t md ) |
93 |
|
|
{ |
94 |
|
|
if( md ) { |
95 |
|
|
md->algo = 0; |
96 |
|
|
md->write = NULL; |
97 |
|
|
free( md->ctx ); md->ctx = NULL; |
98 |
|
|
free( md ); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
void |
104 |
|
|
gpg_md_final( gpg_md_t md ) |
105 |
|
|
{ |
106 |
|
|
switch( md->algo ) { |
107 |
|
|
case MD_MD5 : md5_final( (MD5_CONTEXT *)md->ctx ); break; |
108 |
|
|
case MD_SHA1 : sha1_final( (SHA1_CONTEXT *) md->ctx ); break; |
109 |
|
|
case MD_RMD160: rmd160_final( (RMD160_CONTEXT *) md->ctx ); break; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
|
114 |
|
|
const unsigned char * |
115 |
|
|
gpg_md_read (gpg_md_t md) |
116 |
|
|
{ |
117 |
|
|
switch( md->algo ) { |
118 |
|
|
case MD_MD5 : return md5_read( (MD5_CONTEXT *)md->ctx ); break; |
119 |
|
|
case MD_SHA1 : return sha1_read( (SHA1_CONTEXT *)md->ctx ); break; |
120 |
|
|
case MD_RMD160: return rmd160_read( (RMD160_CONTEXT *)md->ctx ); break; |
121 |
|
|
} |
122 |
|
|
return NULL; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
void |
127 |
|
|
gpg_md_putc( gpg_md_t md, int c ) |
128 |
|
|
{ |
129 |
|
|
unsigned char buf[1]; |
130 |
|
|
buf[0] = c & 0xff; |
131 |
|
|
md->write( md->ctx, buf, 1 ); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
size_t |
136 |
|
|
_md_get_digest_len (gpg_md_t md) |
137 |
|
|
{ |
138 |
|
|
switch (md->algo) { |
139 |
|
|
case MD_MD5: |
140 |
|
|
return 16; |
141 |
|
|
case MD_SHA1: |
142 |
|
|
case MD_RMD160: |
143 |
|
|
return 20; |
144 |
|
|
} |
145 |
|
|
return 0; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
void |
150 |
|
|
gpg_md_write( gpg_md_t md, unsigned char *inbuf, size_t len ) |
151 |
|
|
{ |
152 |
|
|
if( md ) |
153 |
|
|
md->write( md->ctx, inbuf, len ); |
154 |
|
|
} |