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>
50 #ifdef GC_USE_HMAC_MD5
61 #ifdef GC_USE_RIJNDAEL
62 # include "rijndael-api-fst.h"
80 randomize (int level, char *data, size_t datalen)
90 device = NAME_OF_NONCE_DEVICE;
94 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
98 device = NAME_OF_RANDOM_DEVICE;
102 fd = open (device, O_RDONLY);
104 return GC_RANDOM_ERROR;
110 tmp = read (fd, data, datalen);
114 int save_errno = errno;
117 return GC_RANDOM_ERROR;
122 while (len < datalen);
126 return GC_RANDOM_ERROR;
132 gc_nonce (char *data, size_t datalen)
134 return randomize (0, data, datalen);
138 gc_pseudo_random (char *data, size_t datalen)
140 return randomize (1, data, datalen);
144 gc_random (char *data, size_t datalen)
146 return randomize (2, data, datalen);
149 /* Memory allocation. */
152 gc_set_allocators (gc_malloc_t func_malloc,
153 gc_malloc_t secure_malloc,
154 gc_secure_check_t secure_check,
155 gc_realloc_t func_realloc, gc_free_t func_free)
161 typedef struct _gc_cipher_ctx {
165 arctwo_context arctwoContext;
167 #ifdef GC_USE_ARCFOUR
168 arcfour_context arcfourContext;
170 #ifdef GC_USE_RIJNDAEL
171 rijndaelKeyInstance aesEncKey;
172 rijndaelKeyInstance aesDecKey;
173 rijndaelCipherInstance aesContext;
178 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
179 gc_cipher_handle * outhandle)
184 ctx = calloc (sizeof (*ctx), 1);
199 rc = GC_INVALID_CIPHER;
204 #ifdef GC_USE_ARCFOUR
213 rc = GC_INVALID_CIPHER;
218 #ifdef GC_USE_RIJNDAEL
229 rc = GC_INVALID_CIPHER;
235 rc = GC_INVALID_CIPHER;
247 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
249 _gc_cipher_ctx *ctx = handle;
255 arctwo_setkey (&ctx->arctwoContext, keylen, key);
259 #ifdef GC_USE_ARCFOUR
262 arcfour_setkey (&ctx->arcfourContext, key, keylen);
266 #ifdef GC_USE_RIJNDAEL
273 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
275 for (i = 0; i < keylen; i++)
276 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
278 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
279 keylen * 8, keyMaterial);
281 return GC_INVALID_CIPHER;
283 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
284 keylen * 8, keyMaterial);
286 return GC_INVALID_CIPHER;
288 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
290 return GC_INVALID_CIPHER;
296 return GC_INVALID_CIPHER;
303 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
305 _gc_cipher_ctx *ctx = handle;
309 #ifdef GC_USE_RIJNDAEL
316 /* Doesn't use IV. */
323 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
325 for (i = 0; i < ivlen; i++)
326 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
328 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
331 return GC_INVALID_CIPHER;
336 return GC_INVALID_CIPHER;
342 return GC_INVALID_CIPHER;
349 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
351 _gc_cipher_ctx *ctx = handle;
357 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
361 #ifdef GC_USE_ARCFOUR
364 arcfour_stream (&ctx->arcfourContext, data, data, len);
368 #ifdef GC_USE_RIJNDAEL
375 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
376 data, 8 * len, data);
378 return GC_INVALID_CIPHER;
384 return GC_INVALID_CIPHER;
391 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
393 _gc_cipher_ctx *ctx = handle;
399 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
403 #ifdef GC_USE_ARCFOUR
406 arcfour_stream (&ctx->arcfourContext, data, data, len);
410 #ifdef GC_USE_RIJNDAEL
417 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
418 data, 8 * len, data);
420 return GC_INVALID_CIPHER;
426 return GC_INVALID_CIPHER;
433 gc_cipher_close (gc_cipher_handle handle)
435 _gc_cipher_ctx *ctx = handle;
446 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
452 md4_buffer (in, inlen, resbuf);
458 md5_buffer (in, inlen, resbuf);
464 sha1_buffer (in, inlen, resbuf);
469 return GC_INVALID_HASH;
477 gc_md4 (const void *in, size_t inlen, void *resbuf)
479 md4_buffer (in, inlen, resbuf);
486 gc_md5 (const void *in, size_t inlen, void *resbuf)
488 md5_buffer (in, inlen, resbuf);
495 gc_sha1 (const void *in, size_t inlen, void *resbuf)
497 sha1_buffer (in, inlen, resbuf);
502 #ifdef GC_USE_HMAC_MD5
504 gc_hmac_md5 (const void *key, size_t keylen,
505 const void *in, size_t inlen, char *resbuf)
507 hmac_md5 (key, keylen, in, inlen, resbuf);
512 #ifdef GC_USE_HMAC_SHA1
514 gc_hmac_sha1 (const void *key, size_t keylen,
515 const void *in, size_t inlen, char *resbuf)
517 hmac_sha1 (key, keylen, in, inlen, resbuf);