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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* Copyright (C) 2000 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* GNUTLS 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.
*
* GNUTLS 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 <defines.h>
#include <gnutls_int.h>
#include <gnutls_hash_int.h>
/* This file handles all the internal functions that cope with hashes
* and hmacs. Currently it uses the functions provided by
* the gcrypt library that this can be easily changed.
*/
GNUTLS_HASH_HANDLE gnutls_hash_init(MACAlgorithm algorithm) {
GNUTLS_HASH_HANDLE ret;
switch (algorithm) {
case GNUTLS_MAC_NULL:
ret = GNUTLS_HASH_FAILED;
break;
case GNUTLS_MAC_SHA:
#ifdef USE_MHASH
ret = mhash_init( MHASH_SHA1);
#else
ret = gcry_md_open( GCRY_MD_SHA1, 0);
#endif
if (!ret) return GNUTLS_HASH_FAILED;
break;
case GNUTLS_MAC_MD5:
#ifdef USE_MHASH
ret = mhash_init( MHASH_MD5);
#else
ret = gcry_md_open( GCRY_MD_MD5, 0);
#endif
if (!ret) return GNUTLS_HASH_FAILED;
break;
default:
ret = GNUTLS_HASH_FAILED;
}
return ret;
}
int gnutls_hash_get_algo_len(MACAlgorithm algorithm) {
int ret;
switch (algorithm) {
case GNUTLS_MAC_NULL:
ret = 0;
break;
case GNUTLS_MAC_SHA:
#ifdef USE_MHASH
ret = mhash_get_block_size(MHASH_SHA1);
#else
ret = gcry_md_get_algo_dlen( GCRY_MD_SHA1);
#endif
break;
case GNUTLS_MAC_MD5:
#ifdef USE_MHASH
ret = mhash_get_block_size(MHASH_MD5);
#else
ret = gcry_md_get_algo_dlen( GCRY_MD_MD5);
#endif
break;
default:
ret = 0;
}
return ret;
}
int gnutls_hash(GNUTLS_HASH_HANDLE handle, void* text, int textlen) {
#ifdef USE_MHASH
mhash( handle, text, textlen);
#else
gcry_md_write( handle, text, textlen);
#endif
return 0;
}
void* gnutls_hash_deinit( GNUTLS_HASH_HANDLE handle) {
char* mac;
int maclen;
char* ret;
#ifdef USE_MHASH
ret = mhash_end(handle);
#else
maclen = gcry_md_get_algo_dlen(gcry_md_get_algo(handle));
ret = gnutls_malloc(maclen);
gcry_md_final(handle);
mac = gcry_md_read(handle,0);
memmove( ret, mac, maclen);
gcry_md_close(handle);
#endif
return ret;
}
GNUTLS_MAC_HANDLE _gnutls_hmac_init( MACAlgorithm algorithm, char* key, int keylen, int dp) {
GNUTLS_MAC_HANDLE ret;
switch (algorithm) {
case GNUTLS_MAC_NULL:
ret = GNUTLS_MAC_FAILED;
break;
case GNUTLS_MAC_SHA:
#ifdef USE_MHASH
if (dp==0) {
ret = mhash_hmac_init( MHASH_SHA1, key, keylen, 0);
} else {
ret = mhash_hmac_init_dp( MHASH_SHA1, key, keylen, 0);
}
#else
ret = gcry_md_open( GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
#endif
if (!ret) ret = GNUTLS_MAC_FAILED;
break;
case GNUTLS_MAC_MD5:
#ifdef USE_MHASH
if (dp==0) {
ret = mhash_hmac_init( MHASH_MD5, key, keylen, 0);
} else {
ret = mhash_hmac_init_dp( MHASH_MD5, key, keylen, 0);
}
#else
ret = gcry_md_open( GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
#endif
if (!ret) ret = GNUTLS_MAC_FAILED;
break;
default:
ret = GNUTLS_MAC_FAILED;
}
#ifndef USE_MHASH
if (ret!=GNUTLS_MAC_FAILED) gcry_md_setkey(ret, key, keylen);
#endif
return ret;
}
int gnutls_hmac_get_algo_len(MACAlgorithm algorithm) {
int ret;
switch (algorithm) {
case GNUTLS_MAC_NULL:
ret = 0;
break;
case GNUTLS_MAC_SHA:
#ifdef USE_MHASH
ret = mhash_get_block_size(MHASH_SHA1);
#else
ret = gcry_md_get_algo_dlen( GCRY_MD_SHA1);
#endif
break;
case GNUTLS_MAC_MD5:
#ifdef USE_MHASH
ret = mhash_get_block_size(MHASH_MD5);
#else
ret = gcry_md_get_algo_dlen( GCRY_MD_MD5);
#endif
break;
default:
ret = 0;
}
return ret;
}
int gnutls_hmac(GNUTLS_MAC_HANDLE handle, void* text, int textlen) {
#ifdef USE_MHASH
mhash( handle, text, textlen);
#else
gcry_md_write( handle, text, textlen);
#endif
return 0;
}
void* _gnutls_hmac_deinit( GNUTLS_MAC_HANDLE handle, int dp) {
char* mac;
int maclen;
char* ret;
#ifdef USE_MHASH
if (dp==0) {
ret = mhash_hmac_end(handle);
} else {
ret = mhash_hmac_end_dp(handle);
}
#else
maclen = gcry_md_get_algo_dlen(gcry_md_get_algo(handle));
ret = gnutls_malloc( maclen);
gcry_md_final(handle);
mac = gcry_md_read(handle,0);
memmove( ret, mac, maclen);
gcry_md_close(handle);
#endif
return ret;
}
|