Mercurial > hg > octave-kai > gnulib-hg
annotate lib/gc-libgcrypt.c @ 8696:54215b13787d
2007-04-16 Simon Josefsson <simon@josefsson.org>
* lib/gc-libgcrypt.c (gc_hash_open): Shut up compiler warnings.
author | Simon Josefsson <simon@josefsson.org> |
---|---|
date | Mon, 16 Apr 2007 13:14:21 +0000 |
parents | 38b4f50a2b13 |
children | e6944904022b |
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. |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 |
7584
a88f85e4728f
* lib/arcfour.c: Assume config.h.
Eric Blake <ebb9@byu.net>
parents:
6683
diff
changeset
|
23 #include <config.h> |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
24 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
25 /* Get prototype. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
26 #include "gc.h" |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
27 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
28 #include <stdlib.h> |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
29 #include <string.h> |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
30 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
31 /* Get libgcrypt API. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
32 #include <gcrypt.h> |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
33 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
34 # include "md2.h" |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
35 #endif |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
36 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
37 #include <assert.h> |
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 /* Initialization. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
40 |
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
|
41 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
42 gc_init (void) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
43 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
44 gcry_error_t err; |
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 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
47 if (err == GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
48 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
49 if (gcry_check_version (GCRYPT_VERSION) == NULL) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
50 return GC_INIT_ERROR; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
51 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
52 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
53 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
54 return GC_INIT_ERROR; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
55 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
56 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
57 return GC_OK; |
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 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
60 void |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
61 gc_done (void) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
62 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
63 return; |
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 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
66 #ifdef GNULIB_GC_RANDOM |
6683
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
67 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
68 /* Randomness. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
69 |
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
|
70 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
71 gc_nonce (char *data, size_t datalen) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
72 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
73 gcry_create_nonce ((unsigned char *) data, datalen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
74 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
75 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
76 |
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
|
77 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
78 gc_pseudo_random (char *data, size_t datalen) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
79 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
80 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
81 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
82 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
83 |
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
|
84 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
85 gc_random (char *data, size_t datalen) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
86 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
87 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
88 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
89 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
90 |
6683
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
91 #endif |
b834cf4a531f
Split off gc-random from gc, and only warn on missing devices.
Simon Josefsson <simon@josefsson.org>
parents:
6432
diff
changeset
|
92 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
93 /* Memory allocation. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
94 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
95 void |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
96 gc_set_allocators (gc_malloc_t func_malloc, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
97 gc_malloc_t secure_malloc, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
98 gc_secure_check_t secure_check, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
99 gc_realloc_t func_realloc, gc_free_t func_free) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
100 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
101 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
102 func_realloc, func_free); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
103 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
104 |
6383 | 105 /* Ciphers. */ |
106 | |
107 Gc_rc | |
108 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode, | |
109 gc_cipher_handle * outhandle) | |
110 { | |
111 int gcryalg, gcrymode; | |
112 gcry_error_t err; | |
113 | |
114 switch (alg) | |
115 { | |
116 case GC_AES128: | |
117 gcryalg = GCRY_CIPHER_RIJNDAEL; | |
118 break; | |
119 | |
120 case GC_AES192: | |
121 gcryalg = GCRY_CIPHER_RIJNDAEL; | |
122 break; | |
123 | |
124 case GC_AES256: | |
125 gcryalg = GCRY_CIPHER_RIJNDAEL256; | |
126 break; | |
127 | |
128 case GC_3DES: | |
129 gcryalg = GCRY_CIPHER_3DES; | |
130 break; | |
131 | |
132 case GC_DES: | |
133 gcryalg = GCRY_CIPHER_DES; | |
134 break; | |
135 | |
136 case GC_ARCFOUR128: | |
137 case GC_ARCFOUR40: | |
138 gcryalg = GCRY_CIPHER_ARCFOUR; | |
139 break; | |
140 | |
141 case GC_ARCTWO40: | |
142 gcryalg = GCRY_CIPHER_RFC2268_40; | |
143 break; | |
144 | |
145 default: | |
146 return GC_INVALID_CIPHER; | |
147 } | |
148 | |
149 switch (mode) | |
150 { | |
6401
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
151 case GC_ECB: |
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
152 gcrymode = GCRY_CIPHER_MODE_ECB; |
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
153 break; |
3679e6b8d7cf
* gc-libgcrypt.c (gc_cipher_open): Handle ECB.
Simon Josefsson <simon@josefsson.org>
parents:
6394
diff
changeset
|
154 |
6383 | 155 case GC_CBC: |
156 gcrymode = GCRY_CIPHER_MODE_CBC; | |
157 break; | |
158 | |
159 case GC_STREAM: | |
160 gcrymode = GCRY_CIPHER_MODE_STREAM; | |
161 break; | |
162 | |
163 default: | |
164 return GC_INVALID_CIPHER; | |
165 } | |
166 | |
167 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle, | |
168 gcryalg, gcrymode, 0); | |
169 if (gcry_err_code (err)) | |
170 return GC_INVALID_CIPHER; | |
171 | |
172 return GC_OK; | |
173 } | |
174 | |
175 Gc_rc | |
176 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) | |
177 { | |
178 gcry_error_t err; | |
179 | |
180 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen); | |
181 if (gcry_err_code (err)) | |
182 return GC_INVALID_CIPHER; | |
183 | |
184 return GC_OK; | |
185 } | |
186 | |
187 Gc_rc | |
188 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv) | |
189 { | |
190 gcry_error_t err; | |
191 | |
192 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen); | |
193 if (gcry_err_code (err)) | |
194 return GC_INVALID_CIPHER; | |
195 | |
196 return GC_OK; | |
197 } | |
198 | |
199 Gc_rc | |
200 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) | |
201 { | |
202 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle, | |
203 data, len, NULL, len) != 0) | |
204 return GC_INVALID_CIPHER; | |
205 | |
206 return GC_OK; | |
207 } | |
208 | |
209 Gc_rc | |
210 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data) | |
211 { | |
212 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle, | |
213 data, len, NULL, len) != 0) | |
214 return GC_INVALID_CIPHER; | |
215 | |
216 return GC_OK; | |
217 } | |
218 | |
219 Gc_rc | |
220 gc_cipher_close (gc_cipher_handle handle) | |
221 { | |
222 gcry_cipher_close (handle); | |
223 | |
224 return GC_OK; | |
225 } | |
226 | |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
227 /* Hashes. */ |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
228 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
229 typedef struct _gc_hash_ctx { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
230 Gc_hash alg; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
231 Gc_hash_mode mode; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
232 gcry_md_hd_t gch; |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
233 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
234 char hash[GC_MD2_DIGEST_SIZE]; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
235 struct md2_ctx md2Context; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
236 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
237 } _gc_hash_ctx; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
238 |
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
|
239 Gc_rc |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
240 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
|
241 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
242 _gc_hash_ctx *ctx; |
8696
54215b13787d
2007-04-16 Simon Josefsson <simon@josefsson.org>
Simon Josefsson <simon@josefsson.org>
parents:
8109
diff
changeset
|
243 int gcryalg = 0, gcrymode = 0; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
244 gcry_error_t err; |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
245 Gc_rc rc = GC_OK; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
246 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
247 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
|
248 if (!ctx) |
13e8dcd2e4d7
* gc-gnulib.c, gc-libgcrypt.c: Check calloc return value.
Simon Josefsson <simon@josefsson.org>
parents:
6431
diff
changeset
|
249 return GC_MALLOC_ERROR; |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
250 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
251 ctx->alg = hash; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
252 ctx->mode = mode; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
253 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
254 switch (hash) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
255 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
256 case GC_MD2: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
257 gcryalg = GCRY_MD_NONE; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
258 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
259 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
260 case GC_MD4: |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
261 gcryalg = GCRY_MD_MD4; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
262 break; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
263 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
264 case GC_MD5: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
265 gcryalg = GCRY_MD_MD5; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
266 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
267 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
268 case GC_SHA1: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
269 gcryalg = GCRY_MD_SHA1; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
270 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
271 |
8109
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
272 case GC_SHA256: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
273 gcryalg = GCRY_MD_SHA256; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
274 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
275 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
276 case GC_SHA384: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
277 gcryalg = GCRY_MD_SHA384; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
278 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
279 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
280 case GC_SHA512: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
281 gcryalg = GCRY_MD_SHA512; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
282 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
283 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
284 case GC_RMD160: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
285 gcryalg = GCRY_MD_RMD160; |
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 default: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
289 rc = GC_INVALID_HASH; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
290 } |
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 switch (mode) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
293 { |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
294 case 0: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
295 gcrymode = 0; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
296 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
297 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
298 case GC_HMAC: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
299 gcrymode = GCRY_MD_FLAG_HMAC; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
300 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
301 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
302 default: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
303 rc = GC_INVALID_HASH; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
304 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
305 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
306 if (rc == GC_OK && gcryalg != GCRY_MD_NONE) |
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 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
309 if (gcry_err_code (err)) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
310 rc = GC_INVALID_HASH; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
311 } |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
312 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
313 if (rc == GC_OK) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
314 *outhandle = ctx; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
315 else |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
316 free (ctx); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
317 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
318 return rc; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
319 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
320 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
321 Gc_rc |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
322 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
|
323 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
324 _gc_hash_ctx *in = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
325 _gc_hash_ctx *out; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
326 int err; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
327 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
328 *outhandle = out = calloc (sizeof (*out), 1); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
329 if (!out) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
330 return GC_MALLOC_ERROR; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
331 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
332 memcpy (out, in, sizeof (*out)); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
333 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
334 err = gcry_md_copy (&out->gch, in->gch); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
335 if (err) |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
336 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
337 free (out); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
338 return GC_INVALID_HASH; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
339 } |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
340 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
341 return GC_OK; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
342 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
343 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
344 size_t |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
345 gc_hash_digest_length (Gc_hash hash) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
346 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
347 size_t len; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
348 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
349 switch (hash) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
350 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
351 case GC_MD2: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
352 len = GC_MD2_DIGEST_SIZE; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
353 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
354 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
355 case GC_MD4: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
356 len = GC_MD4_DIGEST_SIZE; |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
357 break; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
358 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
359 case GC_MD5: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
360 len = GC_MD5_DIGEST_SIZE; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
361 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
362 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
363 case GC_RMD160: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
364 len = GC_RMD160_DIGEST_SIZE; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
365 break; |
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 case GC_SHA1: |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
368 len = GC_SHA1_DIGEST_SIZE; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
369 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
370 |
8109
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
371 case GC_SHA256: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
372 len = GC_SHA256_DIGEST_SIZE; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
373 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
374 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
375 case GC_SHA384: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
376 len = GC_SHA384_DIGEST_SIZE; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
377 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
378 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
379 case GC_SHA512: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
380 len = GC_SHA512_DIGEST_SIZE; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
381 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
382 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
383 default: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
384 return 0; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
385 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
386 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
387 return len; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
388 } |
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 void |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
391 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
|
392 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
393 _gc_hash_ctx *ctx = handle; |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
394 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
395 if (ctx->alg != GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
396 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
397 gcry_md_setkey (ctx->gch, key, len); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
398 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
399 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
400 void |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
401 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
|
402 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
403 _gc_hash_ctx *ctx = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
404 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
405 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
406 if (ctx->alg == GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
407 md2_process_bytes (data, len, &ctx->md2Context); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
408 else |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
409 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
410 gcry_md_write (ctx->gch, data, len); |
6384
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 const char * |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
414 gc_hash_read (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; |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
417 const char *digest; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
418 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
419 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
420 if (ctx->alg == GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
421 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
422 md2_finish_ctx (&ctx->md2Context, ctx->hash); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
423 digest = ctx->hash; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
424 } |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
425 else |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
426 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
427 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
428 gcry_md_final (ctx->gch); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
429 digest = gcry_md_read (ctx->gch, 0); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
430 } |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
431 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
432 return digest; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
433 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
434 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
435 void |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
436 gc_hash_close (gc_hash_handle handle) |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
437 { |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
438 _gc_hash_ctx *ctx = handle; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
439 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
440 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
441 if (ctx->alg != GC_MD2) |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
442 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
443 gcry_md_close (ctx->gch); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
444 |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
445 free (ctx); |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
446 } |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
447 |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
448 Gc_rc |
6362
2bcfc491052b
* gc-libgcrypt.c (gc_md5): Fix assert call.
Simon Josefsson <simon@josefsson.org>
parents:
6349
diff
changeset
|
449 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
|
450 { |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
451 int gcryalg; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
452 |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
453 switch (hash) |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
454 { |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
455 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
456 case GC_MD2: |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
457 md2_buffer (in, inlen, resbuf); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
458 return GC_OK; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
459 break; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
460 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
461 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
462 #ifdef GNULIB_GC_MD4 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
463 case GC_MD4: |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
464 gcryalg = GCRY_MD_MD4; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
465 break; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
466 #endif |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
467 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
468 #ifdef GNULIB_GC_MD5 |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
469 case GC_MD5: |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
470 gcryalg = GCRY_MD_MD5; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
471 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
|
472 #endif |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
473 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
474 #ifdef GNULIB_GC_SHA1 |
6365 | 475 case GC_SHA1: |
476 gcryalg = GCRY_MD_SHA1; | |
477 break; | |
478 #endif | |
479 | |
8109
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
480 #ifdef GNULIB_GC_SHA256 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
481 case GC_SHA256: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
482 gcryalg = GCRY_MD_SHA256; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
483 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
484 #endif |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
485 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
486 #ifdef GNULIB_GC_SHA384 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
487 case GC_SHA384: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
488 gcryalg = GCRY_MD_SHA384; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
489 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
490 #endif |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
491 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
492 #ifdef GNULIB_GC_SHA512 |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
493 case GC_SHA512: |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
494 gcryalg = GCRY_MD_SHA512; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
495 break; |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
496 #endif |
38b4f50a2b13
lib/gc.h, lib/gc-libgcrypt.c: Support SHA-256/384/512.
Simon Josefsson <simon@josefsson.org>
parents:
7796
diff
changeset
|
497 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
498 #ifdef GNULIB_GC_RMD160 |
6384
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
499 case GC_RMD160: |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
500 gcryalg = GCRY_MD_RMD160; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
501 break; |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
502 #endif |
a4b7c6c3f5bc
Add more hash functions.
Simon Josefsson <simon@josefsson.org>
parents:
6383
diff
changeset
|
503 |
6349
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
504 default: |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
505 return GC_INVALID_HASH; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
506 } |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
507 |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
508 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
|
509 |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
510 return GC_OK; |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
511 } |
c195d0c75e25
2005-10-08 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents:
6348
diff
changeset
|
512 |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
513 /* One-call interface. */ |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
514 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
515 #ifdef GNULIB_GC_MD2 |
6431
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
516 Gc_rc |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
517 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
|
518 { |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
519 md2_buffer (in, inlen, resbuf); |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
520 return GC_OK; |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
521 } |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
522 #endif |
076accb971ef
Add MD2 and hash fixes.
Simon Josefsson <simon@josefsson.org>
parents:
6401
diff
changeset
|
523 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
524 #ifdef GNULIB_GC_MD4 |
6394
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
525 Gc_rc |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
526 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
|
527 { |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
528 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
|
529 gcry_md_hd_t hd; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
530 gpg_error_t err; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
531 unsigned char *p; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
532 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
533 assert (outlen == GC_MD4_DIGEST_SIZE); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
534 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
535 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
|
536 if (err != GPG_ERR_NO_ERROR) |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
537 return GC_INVALID_HASH; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
538 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
539 gcry_md_write (hd, in, inlen); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
540 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
541 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
|
542 if (p == NULL) |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
543 { |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
544 gcry_md_close (hd); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
545 return GC_INVALID_HASH; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
546 } |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
547 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
548 memcpy (resbuf, p, outlen); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
549 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
550 gcry_md_close (hd); |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
551 |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
552 return GC_OK; |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
553 } |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
554 #endif |
1ddc29532d18
Add gc-md4 and gc-md4-tests modules.
Simon Josefsson <simon@josefsson.org>
parents:
6387
diff
changeset
|
555 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
556 #ifdef GNULIB_GC_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
|
557 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
558 gc_md5 (const void *in, size_t inlen, void *resbuf) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
559 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
560 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
561 gcry_md_hd_t hd; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
562 gpg_error_t err; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
563 unsigned char *p; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
564 |
6362
2bcfc491052b
* gc-libgcrypt.c (gc_md5): Fix assert call.
Simon Josefsson <simon@josefsson.org>
parents:
6349
diff
changeset
|
565 assert (outlen == GC_MD5_DIGEST_SIZE); |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
566 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
567 err = gcry_md_open (&hd, GCRY_MD_MD5, 0); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
568 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
569 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
570 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
571 gcry_md_write (hd, in, inlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
572 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
573 p = gcry_md_read (hd, GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
574 if (p == NULL) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
575 { |
6362
2bcfc491052b
* gc-libgcrypt.c (gc_md5): Fix assert call.
Simon Josefsson <simon@josefsson.org>
parents:
6349
diff
changeset
|
576 gcry_md_close (hd); |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
577 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
578 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
579 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
580 memcpy (resbuf, p, outlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
581 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
582 gcry_md_close (hd); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
583 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
584 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
585 } |
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
|
586 #endif |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
587 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
588 #ifdef GNULIB_GC_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
|
589 Gc_rc |
6365 | 590 gc_sha1 (const void *in, size_t inlen, void *resbuf) |
591 { | |
592 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1); | |
593 gcry_md_hd_t hd; | |
594 gpg_error_t err; | |
595 unsigned char *p; | |
596 | |
597 assert (outlen == GC_SHA1_DIGEST_SIZE); | |
598 | |
599 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0); | |
600 if (err != GPG_ERR_NO_ERROR) | |
601 return GC_INVALID_HASH; | |
602 | |
603 gcry_md_write (hd, in, inlen); | |
604 | |
605 p = gcry_md_read (hd, GCRY_MD_SHA1); | |
606 if (p == NULL) | |
607 { | |
608 gcry_md_close (hd); | |
609 return GC_INVALID_HASH; | |
610 } | |
611 | |
612 memcpy (resbuf, p, outlen); | |
613 | |
614 gcry_md_close (hd); | |
615 | |
616 return GC_OK; | |
617 } | |
618 #endif | |
619 | |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
620 #ifdef GNULIB_GC_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
|
621 Gc_rc |
6348
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
622 gc_hmac_md5 (const void *key, size_t keylen, |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
623 const void *in, size_t inlen, char *resbuf) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
624 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
625 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
626 gcry_md_hd_t mdh; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
627 unsigned char *hash; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
628 gpg_error_t err; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
629 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
630 assert (hlen == 16); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
631 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
632 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
|
633 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
634 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
635 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
636 err = gcry_md_setkey (mdh, key, keylen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
637 if (err != GPG_ERR_NO_ERROR) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
638 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
639 gcry_md_close (mdh); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
640 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
641 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
642 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
643 gcry_md_write (mdh, in, inlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
644 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
645 hash = gcry_md_read (mdh, GCRY_MD_MD5); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
646 if (hash == NULL) |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
647 { |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
648 gcry_md_close (mdh); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
649 return GC_INVALID_HASH; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
650 } |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
651 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
652 memcpy (resbuf, hash, hlen); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
653 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
654 gcry_md_close (mdh); |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
655 |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
656 return GC_OK; |
55961a93440c
Add generic crypto module.
Simon Josefsson <simon@josefsson.org>
parents:
diff
changeset
|
657 } |
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
|
658 #endif |
6366 | 659 |
7796
1eae086746a0
Move module indicator macros from *.m4 files to the module descriptions.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
660 #ifdef GNULIB_GC_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
|
661 Gc_rc |
6366 | 662 gc_hmac_sha1 (const void *key, size_t keylen, |
663 const void *in, size_t inlen, char *resbuf) | |
664 { | |
665 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1); | |
666 gcry_md_hd_t mdh; | |
667 unsigned char *hash; | |
668 gpg_error_t err; | |
669 | |
6387
92889893ef1f
* gc-libgcrypt.c (gc_hmac_sha1): Fix assert.
Simon Josefsson <simon@josefsson.org>
parents:
6384
diff
changeset
|
670 assert (hlen == GC_SHA1_DIGEST_SIZE); |
6366 | 671 |
672 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC); | |
673 if (err != GPG_ERR_NO_ERROR) | |
674 return GC_INVALID_HASH; | |
675 | |
676 err = gcry_md_setkey (mdh, key, keylen); | |
677 if (err != GPG_ERR_NO_ERROR) | |
678 { | |
679 gcry_md_close (mdh); | |
680 return GC_INVALID_HASH; | |
681 } | |
682 | |
683 gcry_md_write (mdh, in, inlen); | |
684 | |
685 hash = gcry_md_read (mdh, GCRY_MD_SHA1); | |
686 if (hash == NULL) | |
687 { | |
688 gcry_md_close (mdh); | |
689 return GC_INVALID_HASH; | |
690 } | |
691 | |
692 memcpy (resbuf, hash, hlen); | |
693 | |
694 gcry_md_close (mdh); | |
695 | |
696 return GC_OK; | |
697 } | |
698 #endif |