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
64 #ifdef GC_USE_RIJNDAEL
65 # include "rijndael-api-fst.h"
83 randomize (int level, char *data, size_t datalen)
93 device = NAME_OF_NONCE_DEVICE;
97 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
101 device = NAME_OF_RANDOM_DEVICE;
105 fd = open (device, O_RDONLY);
107 return GC_RANDOM_ERROR;
113 tmp = read (fd, data, datalen);
117 int save_errno = errno;
120 return GC_RANDOM_ERROR;
125 while (len < datalen);
129 return GC_RANDOM_ERROR;
135 gc_nonce (char *data, size_t datalen)
137 return randomize (0, data, datalen);
141 gc_pseudo_random (char *data, size_t datalen)
143 return randomize (1, data, datalen);
147 gc_random (char *data, size_t datalen)
149 return randomize (2, data, datalen);
152 /* Memory allocation. */
155 gc_set_allocators (gc_malloc_t func_malloc,
156 gc_malloc_t secure_malloc,
157 gc_secure_check_t secure_check,
158 gc_realloc_t func_realloc, gc_free_t func_free)
164 typedef struct _gc_cipher_ctx {
168 arctwo_context arctwoContext;
170 #ifdef GC_USE_ARCFOUR
171 arcfour_context arcfourContext;
176 #ifdef GC_USE_RIJNDAEL
177 rijndaelKeyInstance aesEncKey;
178 rijndaelKeyInstance aesDecKey;
179 rijndaelCipherInstance aesContext;
184 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
185 gc_cipher_handle * outhandle)
190 ctx = calloc (sizeof (*ctx), 1);
205 rc = GC_INVALID_CIPHER;
210 #ifdef GC_USE_ARCFOUR
219 rc = GC_INVALID_CIPHER;
232 rc = GC_INVALID_CIPHER;
237 #ifdef GC_USE_RIJNDAEL
248 rc = GC_INVALID_CIPHER;
254 rc = GC_INVALID_CIPHER;
266 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
268 _gc_cipher_ctx *ctx = handle;
274 arctwo_setkey (&ctx->arctwoContext, keylen, key);
278 #ifdef GC_USE_ARCFOUR
281 arcfour_setkey (&ctx->arcfourContext, key, keylen);
288 return GC_INVALID_CIPHER;
289 des_setkey (&ctx->desContext, key);
293 #ifdef GC_USE_RIJNDAEL
300 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
302 for (i = 0; i < keylen; i++)
303 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
305 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
306 keylen * 8, keyMaterial);
308 return GC_INVALID_CIPHER;
310 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
311 keylen * 8, keyMaterial);
313 return GC_INVALID_CIPHER;
315 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
317 return GC_INVALID_CIPHER;
323 return GC_INVALID_CIPHER;
330 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
332 _gc_cipher_ctx *ctx = handle;
336 #ifdef GC_USE_RIJNDAEL
343 /* Doesn't use IV. */
350 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
352 for (i = 0; i < ivlen; i++)
353 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
355 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
358 return GC_INVALID_CIPHER;
363 return GC_INVALID_CIPHER;
369 return GC_INVALID_CIPHER;
376 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
378 _gc_cipher_ctx *ctx = handle;
384 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
388 #ifdef GC_USE_ARCFOUR
391 arcfour_stream (&ctx->arcfourContext, data, data, len);
397 for (; len >= 8; len -= 8, data += 8)
398 des_ecb_encrypt (&ctx->desContext, data, data);
402 #ifdef GC_USE_RIJNDAEL
409 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
410 data, 8 * len, data);
412 return GC_INVALID_CIPHER;
418 return GC_INVALID_CIPHER;
425 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
427 _gc_cipher_ctx *ctx = handle;
433 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
437 #ifdef GC_USE_ARCFOUR
440 arcfour_stream (&ctx->arcfourContext, data, data, len);
446 for (; len >= 8; len -= 8, data += 8)
447 des_ecb_decrypt (&ctx->desContext, data, data);
451 #ifdef GC_USE_RIJNDAEL
458 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
459 data, 8 * len, data);
461 return GC_INVALID_CIPHER;
467 return GC_INVALID_CIPHER;
474 gc_cipher_close (gc_cipher_handle handle)
476 _gc_cipher_ctx *ctx = handle;
487 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
493 md4_buffer (in, inlen, resbuf);
499 md5_buffer (in, inlen, resbuf);
505 sha1_buffer (in, inlen, resbuf);
510 return GC_INVALID_HASH;
518 gc_md4 (const void *in, size_t inlen, void *resbuf)
520 md4_buffer (in, inlen, resbuf);
527 gc_md5 (const void *in, size_t inlen, void *resbuf)
529 md5_buffer (in, inlen, resbuf);
536 gc_sha1 (const void *in, size_t inlen, void *resbuf)
538 sha1_buffer (in, inlen, resbuf);
543 #ifdef GC_USE_HMAC_MD5
545 gc_hmac_md5 (const void *key, size_t keylen,
546 const void *in, size_t inlen, char *resbuf)
548 hmac_md5 (key, keylen, in, inlen, resbuf);
553 #ifdef GC_USE_HMAC_SHA1
555 gc_hmac_sha1 (const void *key, size_t keylen,
556 const void *in, size_t inlen, char *resbuf)
558 hmac_sha1 (key, keylen, in, inlen, resbuf);