Add gc-md4 and gc-md4-tests modules.
[gnulib.git] / lib / gc-gnulib.c
1 /* gc-gl-common.c --- Common gnulib internal crypto interface functions
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 internal functions. */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Get prototype. */
28 #include <gc.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 /* For randomize. */
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #ifdef GC_USE_MD4
41 # include "md4.h"
42 #endif
43 #ifdef GC_USE_MD5
44 # include "md5.h"
45 #endif
46 #ifdef GC_USE_SHA1
47 # include "sha1.h"
48 #endif
49 #ifdef GC_USE_HMAC_MD5
50 # include "hmac.h"
51 #endif
52
53 Gc_rc
54 gc_init (void)
55 {
56   return GC_OK;
57 }
58
59 void
60 gc_done (void)
61 {
62   return;
63 }
64
65 /* Randomness. */
66
67 static Gc_rc
68 randomize (int level, char *data, size_t datalen)
69 {
70   int fd;
71   const char *device;
72   size_t len = 0;
73   int rc;
74
75   switch (level)
76     {
77     case 0:
78       device = NAME_OF_NONCE_DEVICE;
79       break;
80
81     case 1:
82       device = NAME_OF_PSEUDO_RANDOM_DEVICE;
83       break;
84
85     default:
86       device = NAME_OF_RANDOM_DEVICE;
87       break;
88     }
89
90   fd = open (device, O_RDONLY);
91   if (fd < 0)
92     return GC_RANDOM_ERROR;
93
94   do
95     {
96       ssize_t tmp;
97
98       tmp = read (fd, data, datalen);
99
100       if (tmp < 0)
101         {
102           int save_errno = errno;
103           close (fd);
104           errno = save_errno;
105           return GC_RANDOM_ERROR;
106         }
107
108       len += tmp;
109     }
110   while (len < datalen);
111
112   rc = close (fd);
113   if (rc < 0)
114     return GC_RANDOM_ERROR;
115
116   return GC_OK;
117 }
118
119 Gc_rc
120 gc_nonce (char *data, size_t datalen)
121 {
122   return randomize (0, data, datalen);
123 }
124
125 Gc_rc
126 gc_pseudo_random (char *data, size_t datalen)
127 {
128   return randomize (1, data, datalen);
129 }
130
131 Gc_rc
132 gc_random (char *data, size_t datalen)
133 {
134   return randomize (2, data, datalen);
135 }
136
137 /* Memory allocation. */
138
139 void
140 gc_set_allocators (gc_malloc_t func_malloc,
141                    gc_malloc_t secure_malloc,
142                    gc_secure_check_t secure_check,
143                    gc_realloc_t func_realloc, gc_free_t func_free)
144 {
145   return;
146 }
147
148 /* Hashes. */
149
150 Gc_rc
151 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
152 {
153   switch (hash)
154     {
155 #ifdef GC_USE_MD4
156     case GC_MD4:
157       md4_buffer (in, inlen, resbuf);
158       break;
159 #endif
160
161 #ifdef GC_USE_MD5
162     case GC_MD5:
163       md5_buffer (in, inlen, resbuf);
164       break;
165 #endif
166
167 #ifdef GC_USE_SHA1
168     case GC_SHA1:
169       sha1_buffer (in, inlen, resbuf);
170       break;
171 #endif
172
173     default:
174       return GC_INVALID_HASH;
175     }
176
177   return GC_OK;
178 }
179
180 #ifdef GC_USE_MD4
181 Gc_rc
182 gc_md4 (const void *in, size_t inlen, void *resbuf)
183 {
184   md4_buffer (in, inlen, resbuf);
185   return GC_OK;
186 }
187 #endif
188
189 #ifdef GC_USE_MD5
190 Gc_rc
191 gc_md5 (const void *in, size_t inlen, void *resbuf)
192 {
193   md5_buffer (in, inlen, resbuf);
194   return GC_OK;
195 }
196 #endif
197
198 #ifdef GC_USE_SHA1
199 Gc_rc
200 gc_sha1 (const void *in, size_t inlen, void *resbuf)
201 {
202   sha1_buffer (in, inlen, resbuf);
203   return GC_OK;
204 }
205 #endif
206
207 #ifdef GC_USE_HMAC_MD5
208 Gc_rc
209 gc_hmac_md5 (const void *key, size_t keylen,
210              const void *in, size_t inlen, char *resbuf)
211 {
212   hmac_md5 (key, keylen, in, inlen, resbuf);
213   return GC_OK;
214 }
215 #endif
216
217 #ifdef GC_USE_HMAC_SHA1
218 Gc_rc
219 gc_hmac_sha1 (const void *key, size_t keylen,
220               const void *in, size_t inlen, char *resbuf)
221 {
222   hmac_sha1 (key, keylen, in, inlen, resbuf);
223   return GC_OK;
224 }
225 #endif