1 /* gc-gl-common.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
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.
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.
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
21 /* Note: This file is only built if GC uses internal functions. */
35 #include <sys/types.h>
49 #ifdef GC_USE_HMAC_MD5
68 randomize (int level, char *data, size_t datalen)
78 device = NAME_OF_NONCE_DEVICE;
82 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
86 device = NAME_OF_RANDOM_DEVICE;
90 fd = open (device, O_RDONLY);
92 return GC_RANDOM_ERROR;
98 tmp = read (fd, data, datalen);
102 int save_errno = errno;
105 return GC_RANDOM_ERROR;
110 while (len < datalen);
114 return GC_RANDOM_ERROR;
120 gc_nonce (char *data, size_t datalen)
122 return randomize (0, data, datalen);
126 gc_pseudo_random (char *data, size_t datalen)
128 return randomize (1, data, datalen);
132 gc_random (char *data, size_t datalen)
134 return randomize (2, data, datalen);
137 /* Memory allocation. */
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)
151 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
157 md4_buffer (in, inlen, resbuf);
163 md5_buffer (in, inlen, resbuf);
169 sha1_buffer (in, inlen, resbuf);
174 return GC_INVALID_HASH;
182 gc_md4 (const void *in, size_t inlen, void *resbuf)
184 md4_buffer (in, inlen, resbuf);
191 gc_md5 (const void *in, size_t inlen, void *resbuf)
193 md5_buffer (in, inlen, resbuf);
200 gc_sha1 (const void *in, size_t inlen, void *resbuf)
202 sha1_buffer (in, inlen, resbuf);
207 #ifdef GC_USE_HMAC_MD5
209 gc_hmac_md5 (const void *key, size_t keylen,
210 const void *in, size_t inlen, char *resbuf)
212 hmac_md5 (key, keylen, in, inlen, resbuf);
217 #ifdef GC_USE_HMAC_SHA1
219 gc_hmac_sha1 (const void *key, size_t keylen,
220 const void *in, size_t inlen, char *resbuf)
222 hmac_sha1 (key, keylen, in, inlen, resbuf);