* gc-libgcrypt.c (gc_md5): Fix assert call.
[gnulib.git] / lib / gc-libgcrypt.c
1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2  * Copyright (C) 2002, 2003, 2004, 2005  Simon Josefsson
3  *
4  * This file is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2, or (at your
7  * option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this file; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  */
20
21 /* Note: This file is only built if GC uses Libgcrypt. */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Get prototype. */
28 #include "gc.h"
29
30 /* Get libgcrypt API. */
31 #include <gcrypt.h>
32
33 #include <assert.h>
34
35 /* Initialization. */
36
37 int
38 gc_init (void)
39 {
40   gcry_error_t err;
41
42   err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
43   if (err == GPG_ERR_NO_ERROR)
44     {
45       if (gcry_check_version (GCRYPT_VERSION) == NULL)
46         return GC_INIT_ERROR;
47
48       err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
49       if (err != GPG_ERR_NO_ERROR)
50         return GC_INIT_ERROR;
51     }
52
53   return GC_OK;
54 }
55
56 void
57 gc_done (void)
58 {
59   return;
60 }
61
62 /* Randomness. */
63
64 int
65 gc_nonce (char *data, size_t datalen)
66 {
67   gcry_create_nonce ((unsigned char *) data, datalen);
68   return GC_OK;
69 }
70
71 int
72 gc_pseudo_random (char *data, size_t datalen)
73 {
74   gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
75   return GC_OK;
76 }
77
78 int
79 gc_random (char *data, size_t datalen)
80 {
81   gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
82   return GC_OK;
83 }
84
85 /* Memory allocation. */
86
87 void
88 gc_set_allocators (gc_malloc_t func_malloc,
89                    gc_malloc_t secure_malloc,
90                    gc_secure_check_t secure_check,
91                    gc_realloc_t func_realloc, gc_free_t func_free)
92 {
93   gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
94                                func_realloc, func_free);
95 }
96
97 /* Hashes. */
98
99 int
100 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
101 {
102   int gcryalg;
103
104   switch (hash)
105     {
106     case GC_MD5:
107       gcryalg = GCRY_MD_MD5;
108       break;
109
110     default:
111       return GC_INVALID_HASH;
112     }
113
114   gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
115
116   return GC_OK;
117 }
118
119 /* One-call interface. */
120
121 int
122 gc_md5 (const void *in, size_t inlen, void *resbuf)
123 {
124   size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
125   gcry_md_hd_t hd;
126   gpg_error_t err;
127   unsigned char *p;
128
129   assert (outlen == GC_MD5_DIGEST_SIZE);
130
131   err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
132   if (err != GPG_ERR_NO_ERROR)
133     return GC_INVALID_HASH;
134
135   gcry_md_write (hd, in, inlen);
136
137   p = gcry_md_read (hd, GCRY_MD_MD5);
138   if (p == NULL)
139     {
140       gcry_md_close (hd);
141       return GC_INVALID_HASH;
142     }
143
144   memcpy (resbuf, p, outlen);
145
146   gcry_md_close (hd);
147
148   return GC_OK;
149 }
150
151 int
152 gc_hmac_md5 (const void *key, size_t keylen,
153              const void *in, size_t inlen, char *resbuf)
154 {
155   size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
156   gcry_md_hd_t mdh;
157   unsigned char *hash;
158   gpg_error_t err;
159
160   assert (hlen == 16);
161
162   err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
163   if (err != GPG_ERR_NO_ERROR)
164     return GC_INVALID_HASH;
165
166   err = gcry_md_setkey (mdh, key, keylen);
167   if (err != GPG_ERR_NO_ERROR)
168     {
169       gcry_md_close (mdh);
170       return GC_INVALID_HASH;
171     }
172
173   gcry_md_write (mdh, in, inlen);
174
175   hash = gcry_md_read (mdh, GCRY_MD_MD5);
176   if (hash == NULL)
177     {
178       gcry_md_close (mdh);
179       return GC_INVALID_HASH;
180     }
181
182   memcpy (resbuf, hash, hlen);
183
184   gcry_md_close (mdh);
185
186   return GC_OK;
187 }