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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
107 /* Ciphers. */
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
108
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
109 Gc_rc
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
110 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
111 gc_cipher_handle * outhandle)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
112 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
113 int gcryalg, gcrymode;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
114 gcry_error_t err;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
115
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
116 switch (alg)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
117 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
118 case GC_AES128:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
119 gcryalg = GCRY_CIPHER_RIJNDAEL;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
120 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
121
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
122 case GC_AES192:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
123 gcryalg = GCRY_CIPHER_RIJNDAEL;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
124 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
125
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
126 case GC_AES256:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
127 gcryalg = GCRY_CIPHER_RIJNDAEL256;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
128 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
129
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
130 case GC_3DES:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
131 gcryalg = GCRY_CIPHER_3DES;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
132 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
133
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
134 case GC_DES:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
135 gcryalg = GCRY_CIPHER_DES;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
136 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
137
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
138 case GC_ARCFOUR128:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
139 case GC_ARCFOUR40:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
140 gcryalg = GCRY_CIPHER_ARCFOUR;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
141 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
142
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
143 case GC_ARCTWO40:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
144 gcryalg = GCRY_CIPHER_RFC2268_40;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
145 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
146
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
147 default:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
148 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
149 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
150
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
151 switch (mode)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
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
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
157 case GC_CBC:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
158 gcrymode = GCRY_CIPHER_MODE_CBC;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
159 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
160
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
161 case GC_STREAM:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
162 gcrymode = GCRY_CIPHER_MODE_STREAM;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
163 break;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
164
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
165 default:
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
166 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
167 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
168
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
169 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
170 gcryalg, gcrymode, 0);
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
171 if (gcry_err_code (err))
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
172 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
173
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
174 return GC_OK;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
175 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
176
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
177 Gc_rc
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
178 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
179 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
180 gcry_error_t err;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
181
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
182 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
183 if (gcry_err_code (err))
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
184 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
185
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
186 return GC_OK;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
187 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
188
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
189 Gc_rc
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
190 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
191 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
192 gcry_error_t err;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
193
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
194 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
195 if (gcry_err_code (err))
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
196 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
197
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
198 return GC_OK;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
199 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
200
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
201 Gc_rc
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
202 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
203 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
204 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
205 data, len, NULL, len) != 0)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
206 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
207
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
208 return GC_OK;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
209 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
210
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
211 Gc_rc
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
212 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
213 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
214 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
215 data, len, NULL, len) != 0)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
216 return GC_INVALID_CIPHER;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
217
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
218 return GC_OK;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
219 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
220
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
221 Gc_rc
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
222 gc_cipher_close (gc_cipher_handle handle)
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
223 {
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
224 gcry_cipher_close (handle);
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
225
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
226 return GC_OK;
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
227 }
1cc68a6a8398 Add ciphers.
Simon Josefsson <simon@josefsson.org>
parents: 6367
diff changeset
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
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
452 #ifdef GC_USE_SHA1
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
453 case GC_SHA1:
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
454 gcryalg = GCRY_MD_SHA1;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
455 break;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
456 #endif
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
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
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
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
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
550 gc_sha1 (const void *in, size_t inlen, void *resbuf)
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
551 {
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
552 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
553 gcry_md_hd_t hd;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
554 gpg_error_t err;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
555 unsigned char *p;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
556
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
557 assert (outlen == GC_SHA1_DIGEST_SIZE);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
558
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
559 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
560 if (err != GPG_ERR_NO_ERROR)
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
561 return GC_INVALID_HASH;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
562
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
563 gcry_md_write (hd, in, inlen);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
564
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
565 p = gcry_md_read (hd, GCRY_MD_SHA1);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
566 if (p == NULL)
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
567 {
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
568 gcry_md_close (hd);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
569 return GC_INVALID_HASH;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
570 }
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
571
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
572 memcpy (resbuf, p, outlen);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
573
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
574 gcry_md_close (hd);
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
575
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
576 return GC_OK;
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
577 }
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
578 #endif
d3733bac8f2d Add gc-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents: 6363
diff changeset
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
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
619
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
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
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
622 gc_hmac_sha1 (const void *key, size_t keylen,
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
623 const void *in, size_t inlen, char *resbuf)
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
624 {
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
625 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
626 gcry_md_hd_t mdh;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
627 unsigned char *hash;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
628 gpg_error_t err;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
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
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
631
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
632 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
633 if (err != GPG_ERR_NO_ERROR)
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
634 return GC_INVALID_HASH;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
635
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
636 err = gcry_md_setkey (mdh, key, keylen);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
637 if (err != GPG_ERR_NO_ERROR)
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
638 {
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
639 gcry_md_close (mdh);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
640 return GC_INVALID_HASH;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
641 }
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
642
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
643 gcry_md_write (mdh, in, inlen);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
644
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
645 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
646 if (hash == NULL)
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
647 {
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
648 gcry_md_close (mdh);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
649 return GC_INVALID_HASH;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
650 }
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
651
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
652 memcpy (resbuf, hash, hlen);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
653
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
654 gcry_md_close (mdh);
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
655
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
656 return GC_OK;
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
657 }
22f41fec10ee Add gc-hmac-sha1.
Simon Josefsson <simon@josefsson.org>
parents: 6365
diff changeset
658 #endif