Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/gc-libgcrypt.c @ 6683:b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
author | Simon Josefsson <simon@josefsson.org> |
---|---|
date | Tue, 07 Mar 2006 13:17:10 +0000 (2006-03-07) |
parents | 13e8dcd2e4d7 |
children | a88f85e4728f |
rev | line source |
---|---|
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC. |
6683
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Simon Josefsson |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
3 * |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
4 * This file is free software; you can redistribute it and/or modify |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
6 * by the Free Software Foundation; either version 2, or (at your |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
7 * option) any later version. |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
8 * |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
9 * This file is distributed in the hope that it will be useful, but |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
10 * WITHOUT ANY WARRANTY; without even the implied warranty of |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
12 * General Public License for more details. |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
13 * |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
14 * You should have received a copy of the GNU General Public License |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
15 * along with this file; if not, write to the Free Software |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
17 * 02110-1301, USA. |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
18 * |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
19 */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
20 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
21 /* Note: This file is only built if GC uses Libgcrypt. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
22 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
24 # include <config.h> |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
25 #endif |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
26 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
27 /* Get prototype. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
28 #include "gc.h" |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
29 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
30 #include <stdlib.h> |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
31 #include <string.h> |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
32 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
33 /* Get libgcrypt API. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
34 #include <gcrypt.h> |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
35 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
36 # include "md2.h" |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
37 #endif |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
38 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
39 #include <assert.h> |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
40 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
41 /* Initialization. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
42 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
43 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
44 gc_init (void) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
45 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
46 gcry_error_t err; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
47 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
48 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
49 if (err == GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
50 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
51 if (gcry_check_version (GCRYPT_VERSION) == NULL) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
52 return GC_INIT_ERROR; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
53 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
54 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
55 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
56 return GC_INIT_ERROR; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
57 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
58 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
59 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
60 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
61 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
62 void |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
63 gc_done (void) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
64 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
65 return; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
66 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
67 |
6683
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
68 #ifdef GC_USE_RANDOM |
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
69 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
70 /* Randomness. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
71 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
72 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
73 gc_nonce (char *data, size_t datalen) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
74 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
75 gcry_create_nonce ((unsigned char *) data, datalen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
76 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
77 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
78 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
79 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
80 gc_pseudo_random (char *data, size_t datalen) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
81 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
82 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
83 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
84 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
85 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
86 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
87 gc_random (char *data, size_t datalen) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
88 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
89 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
90 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
91 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
92 |
6683
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
93 #endif |
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
94 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
95 /* Memory allocation. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
96 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
97 void |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
98 gc_set_allocators (gc_malloc_t func_malloc, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
99 gc_malloc_t secure_malloc, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
100 gc_secure_check_t secure_check, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
101 gc_realloc_t func_realloc, gc_free_t func_free) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
102 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
103 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
104 func_realloc, func_free); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
105 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
106 |
6383 | 107 /* Ciphers. */ |
108 | |
109 Gc_rc | |
110 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode, | |
111 gc_cipher_handle * outhandle) | |
112 { | |
113 int gcryalg, gcrymode; | |
114 gcry_error_t err; | |
115 | |
116 switch (alg) | |
117 { | |
118 case GC_AES128: | |
119 gcryalg = GCRY_CIPHER_RIJNDAEL; | |
120 break; | |
121 | |
122 case GC_AES192: | |
123 gcryalg = GCRY_CIPHER_RIJNDAEL; | |
124 break; | |
125 | |
126 case GC_AES256: | |
127 gcryalg = GCRY_CIPHER_RIJNDAEL256; | |
128 break; | |
129 | |
130 case GC_3DES: | |
131 gcryalg = GCRY_CIPHER_3DES; | |
132 break; | |
133 | |
134 case GC_DES: | |
135 gcryalg = GCRY_CIPHER_DES; | |
136 break; | |
137 | |
138 case GC_ARCFOUR128: | |
139 case GC_ARCFOUR40: | |
140 gcryalg = GCRY_CIPHER_ARCFOUR; | |
141 break; | |
142 | |
143 case GC_ARCTWO40: | |
144 gcryalg = GCRY_CIPHER_RFC2268_40; | |
145 break; | |
146 | |
147 default: | |
148 return GC_INVALID_CIPHER; | |
149 } | |
150 | |
151 switch (mode) | |
152 { | |
6401
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
153 case GC_ECB: |
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
154 gcrymode = GCRY_CIPHER_MODE_ECB; |
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
155 break; |
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
156 |
6383 | 157 case GC_CBC: |
158 gcrymode = GCRY_CIPHER_MODE_CBC; | |
159 break; | |
160 | |
161 case GC_STREAM: | |
162 gcrymode = GCRY_CIPHER_MODE_STREAM; | |
163 break; | |
164 | |
165 default: | |
166 return GC_INVALID_CIPHER; | |
167 } | |
168 | |
169 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle, | |
170 gcryalg, gcrymode, 0); | |
171 if (gcry_err_code (err)) | |
172 return GC_INVALID_CIPHER; | |
173 | |
174 return GC_OK; | |
175 } | |
176 | |
177 Gc_rc | |
178 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) | |
179 { | |
180 gcry_error_t err; | |
181 | |
182 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen); | |
183 if (gcry_err_code (err)) | |
184 return GC_INVALID_CIPHER; | |
185 | |
186 return GC_OK; | |
187 } | |
188 | |
189 Gc_rc | |
190 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv) | |
191 { | |
192 gcry_error_t err; | |
193 | |
194 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen); | |
195 if (gcry_err_code (err)) | |
196 return GC_INVALID_CIPHER; | |
197 | |
198 return GC_OK; | |
199 } | |
200 | |
201 Gc_rc | |
202 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) | |
203 { | |
204 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle, | |
205 data, len, NULL, len) != 0) | |
206 return GC_INVALID_CIPHER; | |
207 | |
208 return GC_OK; | |
209 } | |
210 | |
211 Gc_rc | |
212 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data) | |
213 { | |
214 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle, | |
215 data, len, NULL, len) != 0) | |
216 return GC_INVALID_CIPHER; | |
217 | |
218 return GC_OK; | |
219 } | |
220 | |
221 Gc_rc | |
222 gc_cipher_close (gc_cipher_handle handle) | |
223 { | |
224 gcry_cipher_close (handle); | |
225 | |
226 return GC_OK; | |
227 } | |
228 | |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
229 /* Hashes. */ |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
230 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
231 typedef struct _gc_hash_ctx { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
232 Gc_hash alg; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
233 Gc_hash_mode mode; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
234 gcry_md_hd_t gch; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
235 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
236 char hash[GC_MD2_DIGEST_SIZE]; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
237 struct md2_ctx md2Context; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
238 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
239 } _gc_hash_ctx; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
240 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
241 Gc_rc |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
242 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
243 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
244 _gc_hash_ctx *ctx; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
245 int gcryalg, gcrymode; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
246 gcry_error_t err; |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
247 Gc_rc rc = GC_OK; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
248 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
249 ctx = calloc (sizeof (*ctx), 1); |
6432
13e8dcd2e4d7
* gc-gnulib.c, gc-libgcrypt.c: Check calloc return value.
Simon Josefsson <simon@josefsson.org>
parents:
6431
diff
changeset
|
250 if (!ctx) |
13e8dcd2e4d7
* gc-gnulib.c, gc-libgcrypt.c: Check calloc return value.
Simon Josefsson <simon@josefsson.org>
parents:
6431
diff
changeset
|
251 return GC_MALLOC_ERROR; |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
252 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
253 ctx->alg = hash; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
254 ctx->mode = mode; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
255 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
256 switch (hash) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
257 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
258 case GC_MD2: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
259 gcryalg = GCRY_MD_NONE; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
260 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
261 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
262 case GC_MD4: |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
263 gcryalg = GCRY_MD_MD4; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
264 break; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
265 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
266 case GC_MD5: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
267 gcryalg = GCRY_MD_MD5; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
268 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
269 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
270 case GC_SHA1: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
271 gcryalg = GCRY_MD_SHA1; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
272 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
273 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
274 case GC_RMD160: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
275 gcryalg = GCRY_MD_RMD160; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
276 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
277 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
278 default: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
279 rc = GC_INVALID_HASH; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
280 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
281 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
282 switch (mode) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
283 { |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
284 case 0: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
285 gcrymode = 0; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
286 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
287 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
288 case GC_HMAC: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
289 gcrymode = GCRY_MD_FLAG_HMAC; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
290 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
291 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
292 default: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
293 rc = GC_INVALID_HASH; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
294 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
295 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
296 if (rc == GC_OK && gcryalg != GCRY_MD_NONE) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
297 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
298 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
299 if (gcry_err_code (err)) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
300 rc = GC_INVALID_HASH; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
301 } |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
302 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
303 if (rc == GC_OK) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
304 *outhandle = ctx; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
305 else |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
306 free (ctx); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
307 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
308 return rc; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
309 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
310 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
311 Gc_rc |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
312 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
313 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
314 _gc_hash_ctx *in = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
315 _gc_hash_ctx *out; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
316 int err; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
317 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
318 *outhandle = out = calloc (sizeof (*out), 1); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
319 if (!out) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
320 return GC_MALLOC_ERROR; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
321 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
322 memcpy (out, in, sizeof (*out)); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
323 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
324 err = gcry_md_copy (&out->gch, in->gch); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
325 if (err) |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
326 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
327 free (out); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
328 return GC_INVALID_HASH; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
329 } |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
330 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
331 return GC_OK; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
332 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
333 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
334 size_t |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
335 gc_hash_digest_length (Gc_hash hash) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
336 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
337 size_t len; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
338 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
339 switch (hash) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
340 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
341 case GC_MD2: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
342 len = GC_MD2_DIGEST_SIZE; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
343 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
344 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
345 case GC_MD4: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
346 len = GC_MD4_DIGEST_SIZE; |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
347 break; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
348 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
349 case GC_MD5: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
350 len = GC_MD5_DIGEST_SIZE; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
351 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
352 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
353 case GC_RMD160: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
354 len = GC_RMD160_DIGEST_SIZE; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
355 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
356 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
357 case GC_SHA1: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
358 len = GC_SHA1_DIGEST_SIZE; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
359 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
360 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
361 default: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
362 return 0; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
363 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
364 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
365 return len; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
366 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
367 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
368 void |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
369 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
370 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
371 _gc_hash_ctx *ctx = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
372 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
373 if (ctx->alg != GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
374 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
375 gcry_md_setkey (ctx->gch, key, len); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
376 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
377 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
378 void |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
379 gc_hash_write (gc_hash_handle handle, size_t len, const char *data) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
380 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
381 _gc_hash_ctx *ctx = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
382 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
383 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
384 if (ctx->alg == GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
385 md2_process_bytes (data, len, &ctx->md2Context); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
386 else |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
387 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
388 gcry_md_write (ctx->gch, data, len); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
389 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
390 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
391 const char * |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
392 gc_hash_read (gc_hash_handle handle) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
393 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
394 _gc_hash_ctx *ctx = handle; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
395 const char *digest; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
396 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
397 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
398 if (ctx->alg == GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
399 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
400 md2_finish_ctx (&ctx->md2Context, ctx->hash); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
401 digest = ctx->hash; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
402 } |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
403 else |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
404 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
405 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
406 gcry_md_final (ctx->gch); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
407 digest = gcry_md_read (ctx->gch, 0); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
408 } |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
409 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
410 return digest; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
411 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
412 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
413 void |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
414 gc_hash_close (gc_hash_handle handle) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
415 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
416 _gc_hash_ctx *ctx = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
417 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
418 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
419 if (ctx->alg != GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
420 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
421 gcry_md_close (ctx->gch); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
422 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
423 free (ctx); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
424 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
425 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
426 Gc_rc |
6362
2bcfc491052b
* gc-libgcrypt.c (gc_md5): Fix assert call.
Simon Josefsson <simon@josefsson.org>
parents:
6349
diff
changeset
|
427 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
428 { |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
429 int gcryalg; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
430 |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
431 switch (hash) |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
432 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
433 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
434 case GC_MD2: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
435 md2_buffer (in, inlen, resbuf); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
436 return GC_OK; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
437 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
438 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
439 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
440 #ifdef GC_USE_MD4 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
441 case GC_MD4: |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
442 gcryalg = GCRY_MD_MD4; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
443 break; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
444 #endif |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
445 |
6363
bae11f33bf3c
Split parts of the gc module into gc-md5 and gc-hmac-md5 modules.
Simon Josefsson <simon@josefsson.org>
parents:
6362
diff
changeset
|
446 #ifdef GC_USE_MD5 |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
447 case GC_MD5: |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
448 gcryalg = GCRY_MD_MD5; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
449 break; |
6363
bae11f33bf3c
Split parts of the gc module into gc-md5 and gc-hmac-md5 modules.
Simon Josefsson <simon@josefsson.org>
parents:
6362
diff
changeset
|
450 #endif |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
451 |
6365 | 452 #ifdef GC_USE_SHA1 |
453 case GC_SHA1: | |
454 gcryalg = GCRY_MD_SHA1; | |
455 break; | |
456 #endif | |
457 | |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
458 #ifdef GC_USE_RMD160 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
459 case GC_RMD160: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
460 gcryalg = GCRY_MD_RMD160; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
461 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
462 #endif |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
463 |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
464 default: |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
465 return GC_INVALID_HASH; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
466 } |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
467 |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
468 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen); |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
469 |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
470 return GC_OK; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
471 } |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
472 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
473 /* One-call interface. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
474 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
475 #ifdef GC_USE_MD2 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
476 Gc_rc |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
477 gc_md2 (const void *in, size_t inlen, void *resbuf) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
478 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
479 md2_buffer (in, inlen, resbuf); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
480 return GC_OK; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
481 } |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
482 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
483 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
484 #ifdef GC_USE_MD4 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
485 Gc_rc |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
486 gc_md4 (const void *in, size_t inlen, void *resbuf) |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
487 { |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
488 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
489 gcry_md_hd_t hd; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
490 gpg_error_t err; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
491 unsigned char *p; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
492 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
493 assert (outlen == GC_MD4_DIGEST_SIZE); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
494 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
495 err = gcry_md_open (&hd, GCRY_MD_MD4, 0); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
496 if (err != GPG_ERR_NO_ERROR) |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
497 return GC_INVALID_HASH; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
498 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
499 gcry_md_write (hd, in, inlen); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
500 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
501 p = gcry_md_read (hd, GCRY_MD_MD4); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
502 if (p == NULL) |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
503 { |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
504 gcry_md_close (hd); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
505 return GC_INVALID_HASH; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
506 } |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
507 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
508 memcpy (resbuf, p, outlen); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
509 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
510 gcry_md_close (hd); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
511 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
512 return GC_OK; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
513 } |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
514 #endif |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
515 |
6363
bae11f33bf3c
Split parts of the gc module into gc-md5 and gc-hmac-md5 modules.
Simon Josefsson <simon@josefsson.org>
parents:
6362
diff
changeset
|
516 #ifdef GC_USE_MD5 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
517 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
518 gc_md5 (const void *in, size_t inlen, void *resbuf) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
519 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
520 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
521 gcry_md_hd_t hd; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
522 gpg_error_t err; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
523 unsigned char *p; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
524 |
6362
2bcfc491052b
* gc-libgcrypt.c (gc_md5): Fix assert call.
Simon Josefsson <simon@josefsson.org>
parents:
6349
diff
changeset
|
525 assert (outlen == GC_MD5_DIGEST_SIZE); |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
526 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
527 err = gcry_md_open (&hd, GCRY_MD_MD5, 0); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
528 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
529 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
530 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
531 gcry_md_write (hd, in, inlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
532 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
533 p = gcry_md_read (hd, GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
534 if (p == NULL) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
535 { |
6362
2bcfc491052b
* gc-libgcrypt.c (gc_md5): Fix assert call.
Simon Josefsson <simon@josefsson.org>
parents:
6349
diff
changeset
|
536 gcry_md_close (hd); |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
537 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
538 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
539 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
540 memcpy (resbuf, p, outlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
541 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
542 gcry_md_close (hd); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
543 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
544 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
545 } |
6363
bae11f33bf3c
Split parts of the gc module into gc-md5 and gc-hmac-md5 modules.
Simon Josefsson <simon@josefsson.org>
parents:
6362
diff
changeset
|
546 #endif |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
547 |
6365 | 548 #ifdef GC_USE_SHA1 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
549 Gc_rc |
6365 | 550 gc_sha1 (const void *in, size_t inlen, void *resbuf) |
551 { | |
552 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1); | |
553 gcry_md_hd_t hd; | |
554 gpg_error_t err; | |
555 unsigned char *p; | |
556 | |
557 assert (outlen == GC_SHA1_DIGEST_SIZE); | |
558 | |
559 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0); | |
560 if (err != GPG_ERR_NO_ERROR) | |
561 return GC_INVALID_HASH; | |
562 | |
563 gcry_md_write (hd, in, inlen); | |
564 | |
565 p = gcry_md_read (hd, GCRY_MD_SHA1); | |
566 if (p == NULL) | |
567 { | |
568 gcry_md_close (hd); | |
569 return GC_INVALID_HASH; | |
570 } | |
571 | |
572 memcpy (resbuf, p, outlen); | |
573 | |
574 gcry_md_close (hd); | |
575 | |
576 return GC_OK; | |
577 } | |
578 #endif | |
579 | |
6363
bae11f33bf3c
Split parts of the gc module into gc-md5 and gc-hmac-md5 modules.
Simon Josefsson <simon@josefsson.org>
parents:
6362
diff
changeset
|
580 #ifdef GC_USE_HMAC_MD5 |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
581 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
582 gc_hmac_md5 (const void *key, size_t keylen, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
583 const void *in, size_t inlen, char *resbuf) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
584 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
585 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
586 gcry_md_hd_t mdh; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
587 unsigned char *hash; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
588 gpg_error_t err; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
589 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
590 assert (hlen == 16); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
591 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
592 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
593 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
594 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
595 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
596 err = gcry_md_setkey (mdh, key, keylen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
597 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
598 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
599 gcry_md_close (mdh); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
600 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
601 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
602 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
603 gcry_md_write (mdh, in, inlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
604 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
605 hash = gcry_md_read (mdh, GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
606 if (hash == NULL) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
607 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
608 gcry_md_close (mdh); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
609 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
610 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
611 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
612 memcpy (resbuf, hash, hlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
613 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
614 gcry_md_close (mdh); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
615 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
616 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
617 } |
6363
bae11f33bf3c
Split parts of the gc module into gc-md5 and gc-hmac-md5 modules.
Simon Josefsson <simon@josefsson.org>
parents:
6362
diff
changeset
|
618 #endif |
6366 | 619 |
620 #ifdef GC_USE_HMAC_SHA1 | |
6367
69cadd43d0f4
* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
Simon Josefsson <simon@josefsson.org>
parents:
6366
diff
changeset
|
621 Gc_rc |
6366 | 622 gc_hmac_sha1 (const void *key, size_t keylen, |
623 const void *in, size_t inlen, char *resbuf) | |
624 { | |
625 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1); | |
626 gcry_md_hd_t mdh; | |
627 unsigned char *hash; | |
628 gpg_error_t err; | |
629 | |
6387
92889893ef1f
* gc-libgcrypt.c (gc_hmac_sha1): Fix assert.
Simon Josefsson <simon@josefsson.org>
parents:
6384
diff
changeset
|
630 assert (hlen == GC_SHA1_DIGEST_SIZE); |
6366 | 631 |
632 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC); | |
633 if (err != GPG_ERR_NO_ERROR) | |
634 return GC_INVALID_HASH; | |
635 | |
636 err = gcry_md_setkey (mdh, key, keylen); | |
637 if (err != GPG_ERR_NO_ERROR) | |
638 { | |
639 gcry_md_close (mdh); | |
640 return GC_INVALID_HASH; | |
641 } | |
642 | |
643 gcry_md_write (mdh, in, inlen); | |
644 | |
645 hash = gcry_md_read (mdh, GCRY_MD_SHA1); | |
646 if (hash == NULL) | |
647 { | |
648 gcry_md_close (mdh); | |
649 return GC_INVALID_HASH; | |
650 } | |
651 | |
652 memcpy (resbuf, hash, hlen); | |
653 | |
654 gcry_md_close (mdh); | |
655 | |
656 return GC_OK; | |
657 } | |
658 #endif |