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
52 #ifdef GC_USE_RIJNDAEL
53 # include "rijndael-api-fst.h"
71 randomize (int level, char *data, size_t datalen)
81 device = NAME_OF_NONCE_DEVICE;
85 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
89 device = NAME_OF_RANDOM_DEVICE;
93 fd = open (device, O_RDONLY);
95 return GC_RANDOM_ERROR;
101 tmp = read (fd, data, datalen);
105 int save_errno = errno;
108 return GC_RANDOM_ERROR;
113 while (len < datalen);
117 return GC_RANDOM_ERROR;
123 gc_nonce (char *data, size_t datalen)
125 return randomize (0, data, datalen);
129 gc_pseudo_random (char *data, size_t datalen)
131 return randomize (1, data, datalen);
135 gc_random (char *data, size_t datalen)
137 return randomize (2, data, datalen);
140 /* Memory allocation. */
143 gc_set_allocators (gc_malloc_t func_malloc,
144 gc_malloc_t secure_malloc,
145 gc_secure_check_t secure_check,
146 gc_realloc_t func_realloc, gc_free_t func_free)
152 typedef struct _gc_cipher_ctx {
155 #ifdef GC_USE_RIJNDAEL
156 rijndaelKeyInstance aesEncKey;
157 rijndaelKeyInstance aesDecKey;
158 rijndaelCipherInstance aesContext;
163 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
164 gc_cipher_handle * outhandle)
169 ctx = calloc (sizeof (*ctx), 1);
176 #ifdef GC_USE_RIJNDAEL
187 rc = GC_INVALID_CIPHER;
193 rc = GC_INVALID_CIPHER;
205 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
207 _gc_cipher_ctx *ctx = handle;
211 #ifdef GC_USE_RIJNDAEL
218 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
220 for (i = 0; i < keylen; i++)
221 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
223 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
224 keylen * 8, keyMaterial);
226 return GC_INVALID_CIPHER;
228 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
229 keylen * 8, keyMaterial);
231 return GC_INVALID_CIPHER;
233 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
235 return GC_INVALID_CIPHER;
241 return GC_INVALID_CIPHER;
248 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
250 _gc_cipher_ctx *ctx = handle;
254 #ifdef GC_USE_RIJNDAEL
261 /* Doesn't use IV. */
268 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
270 for (i = 0; i < ivlen; i++)
271 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
273 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
276 return GC_INVALID_CIPHER;
281 return GC_INVALID_CIPHER;
287 return GC_INVALID_CIPHER;
294 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
296 _gc_cipher_ctx *ctx = handle;
300 #ifdef GC_USE_RIJNDAEL
307 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
308 data, 8 * len, data);
310 return GC_INVALID_CIPHER;
316 return GC_INVALID_CIPHER;
323 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
325 _gc_cipher_ctx *ctx = handle;
329 #ifdef GC_USE_RIJNDAEL
336 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
337 data, 8 * len, data);
339 return GC_INVALID_CIPHER;
345 return GC_INVALID_CIPHER;
352 gc_cipher_close (gc_cipher_handle handle)
354 _gc_cipher_ctx *ctx = handle;
365 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
371 md4_buffer (in, inlen, resbuf);
377 md5_buffer (in, inlen, resbuf);
383 sha1_buffer (in, inlen, resbuf);
388 return GC_INVALID_HASH;
396 gc_md4 (const void *in, size_t inlen, void *resbuf)
398 md4_buffer (in, inlen, resbuf);
405 gc_md5 (const void *in, size_t inlen, void *resbuf)
407 md5_buffer (in, inlen, resbuf);
414 gc_sha1 (const void *in, size_t inlen, void *resbuf)
416 sha1_buffer (in, inlen, resbuf);
421 #ifdef GC_USE_HMAC_MD5
423 gc_hmac_md5 (const void *key, size_t keylen,
424 const void *in, size_t inlen, char *resbuf)
426 hmac_md5 (key, keylen, in, inlen, resbuf);
431 #ifdef GC_USE_HMAC_SHA1
433 gc_hmac_sha1 (const void *key, size_t keylen,
434 const void *in, size_t inlen, char *resbuf)
436 hmac_sha1 (key, keylen, in, inlen, resbuf);