1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006 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. */
36 # include <sys/types.h>
37 # include <sys/stat.h>
55 #ifdef GC_USE_HMAC_MD5
69 #ifdef GC_USE_RIJNDAEL
70 # include "rijndael-api-fst.h"
90 randomize (int level, char *data, size_t datalen)
100 device = NAME_OF_NONCE_DEVICE;
104 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
108 device = NAME_OF_RANDOM_DEVICE;
112 fd = open (device, O_RDONLY);
114 return GC_RANDOM_ERROR;
120 tmp = read (fd, data, datalen);
124 int save_errno = errno;
127 return GC_RANDOM_ERROR;
132 while (len < datalen);
136 return GC_RANDOM_ERROR;
142 gc_nonce (char *data, size_t datalen)
144 return randomize (0, data, datalen);
148 gc_pseudo_random (char *data, size_t datalen)
150 return randomize (1, data, datalen);
154 gc_random (char *data, size_t datalen)
156 return randomize (2, data, datalen);
161 /* Memory allocation. */
164 gc_set_allocators (gc_malloc_t func_malloc,
165 gc_malloc_t secure_malloc,
166 gc_secure_check_t secure_check,
167 gc_realloc_t func_realloc, gc_free_t func_free)
173 typedef struct _gc_cipher_ctx {
177 arctwo_context arctwoContext;
178 char arctwoIV[ARCTWO_BLOCK_SIZE];
180 #ifdef GC_USE_ARCFOUR
181 arcfour_context arcfourContext;
186 #ifdef GC_USE_RIJNDAEL
187 rijndaelKeyInstance aesEncKey;
188 rijndaelKeyInstance aesDecKey;
189 rijndaelCipherInstance aesContext;
194 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
195 gc_cipher_handle * outhandle)
200 ctx = calloc (sizeof (*ctx), 1);
202 return GC_MALLOC_ERROR;
218 rc = GC_INVALID_CIPHER;
223 #ifdef GC_USE_ARCFOUR
232 rc = GC_INVALID_CIPHER;
245 rc = GC_INVALID_CIPHER;
250 #ifdef GC_USE_RIJNDAEL
261 rc = GC_INVALID_CIPHER;
267 rc = GC_INVALID_CIPHER;
279 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
281 _gc_cipher_ctx *ctx = handle;
287 arctwo_setkey (&ctx->arctwoContext, keylen, key);
291 #ifdef GC_USE_ARCFOUR
294 arcfour_setkey (&ctx->arcfourContext, key, keylen);
301 return GC_INVALID_CIPHER;
302 des_setkey (&ctx->desContext, key);
306 #ifdef GC_USE_RIJNDAEL
313 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
315 for (i = 0; i < keylen; i++)
316 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
318 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
319 keylen * 8, keyMaterial);
321 return GC_INVALID_CIPHER;
323 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
324 keylen * 8, keyMaterial);
326 return GC_INVALID_CIPHER;
328 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
330 return GC_INVALID_CIPHER;
336 return GC_INVALID_CIPHER;
343 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
345 _gc_cipher_ctx *ctx = handle;
351 if (ivlen != ARCTWO_BLOCK_SIZE)
352 return GC_INVALID_CIPHER;
353 memcpy (ctx->arctwoIV, iv, ivlen);
357 #ifdef GC_USE_RIJNDAEL
364 /* Doesn't use IV. */
371 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
373 for (i = 0; i < ivlen; i++)
374 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
376 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
379 return GC_INVALID_CIPHER;
384 return GC_INVALID_CIPHER;
390 return GC_INVALID_CIPHER;
397 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
399 _gc_cipher_ctx *ctx = handle;
408 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
412 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
413 data += ARCTWO_BLOCK_SIZE)
416 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
417 data[i] ^= ctx->arctwoIV[i];
418 arctwo_encrypt (&ctx->arctwoContext, data, data,
420 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
425 return GC_INVALID_CIPHER;
430 #ifdef GC_USE_ARCFOUR
433 arcfour_stream (&ctx->arcfourContext, data, data, len);
439 for (; len >= 8; len -= 8, data += 8)
440 des_ecb_encrypt (&ctx->desContext, data, data);
444 #ifdef GC_USE_RIJNDAEL
451 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
452 data, 8 * len, data);
454 return GC_INVALID_CIPHER;
460 return GC_INVALID_CIPHER;
467 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
469 _gc_cipher_ctx *ctx = handle;
478 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
482 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
483 data += ARCTWO_BLOCK_SIZE)
485 char tmpIV[ARCTWO_BLOCK_SIZE];
487 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
488 arctwo_decrypt (&ctx->arctwoContext, data, data,
490 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
491 data[i] ^= ctx->arctwoIV[i];
492 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
497 return GC_INVALID_CIPHER;
502 #ifdef GC_USE_ARCFOUR
505 arcfour_stream (&ctx->arcfourContext, data, data, len);
511 for (; len >= 8; len -= 8, data += 8)
512 des_ecb_decrypt (&ctx->desContext, data, data);
516 #ifdef GC_USE_RIJNDAEL
523 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
524 data, 8 * len, data);
526 return GC_INVALID_CIPHER;
532 return GC_INVALID_CIPHER;
539 gc_cipher_close (gc_cipher_handle handle)
541 _gc_cipher_ctx *ctx = handle;
551 #define MAX_DIGEST_SIZE 20
553 typedef struct _gc_hash_ctx {
556 char hash[MAX_DIGEST_SIZE];
558 struct md2_ctx md2Context;
561 struct md4_ctx md4Context;
564 struct md5_ctx md5Context;
567 struct sha1_ctx sha1Context;
572 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
577 ctx = calloc (sizeof (*ctx), 1);
586 md2_init_ctx (&ctx->md2Context);
592 md4_init_ctx (&ctx->md4Context);
598 md5_init_ctx (&ctx->md5Context);
604 sha1_init_ctx (&ctx->sha1Context);
609 rc = GC_INVALID_HASH;
619 rc = GC_INVALID_HASH;
632 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
634 _gc_hash_ctx *in = handle;
637 *outhandle = out = calloc (sizeof (*out), 1);
639 return GC_MALLOC_ERROR;
641 memcpy (out, in, sizeof (*out));
647 gc_hash_digest_length (Gc_hash hash)
654 len = GC_MD2_DIGEST_SIZE;
658 len = GC_MD4_DIGEST_SIZE;
662 len = GC_MD5_DIGEST_SIZE;
666 len = GC_RMD160_DIGEST_SIZE;
670 len = GC_SHA1_DIGEST_SIZE;
681 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
683 _gc_hash_ctx *ctx = handle;
689 md2_process_bytes (data, len, &ctx->md2Context);
695 md4_process_bytes (data, len, &ctx->md4Context);
701 md5_process_bytes (data, len, &ctx->md5Context);
707 sha1_process_bytes (data, len, &ctx->sha1Context);
717 gc_hash_read (gc_hash_handle handle)
719 _gc_hash_ctx *ctx = handle;
720 const char *ret = NULL;
726 md2_finish_ctx (&ctx->md2Context, ctx->hash);
733 md4_finish_ctx (&ctx->md4Context, ctx->hash);
740 md5_finish_ctx (&ctx->md5Context, ctx->hash);
747 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
760 gc_hash_close (gc_hash_handle handle)
762 _gc_hash_ctx *ctx = handle;
768 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
774 md2_buffer (in, inlen, resbuf);
780 md4_buffer (in, inlen, resbuf);
786 md5_buffer (in, inlen, resbuf);
792 sha1_buffer (in, inlen, resbuf);
797 return GC_INVALID_HASH;
805 gc_md2 (const void *in, size_t inlen, void *resbuf)
807 md2_buffer (in, inlen, resbuf);
814 gc_md4 (const void *in, size_t inlen, void *resbuf)
816 md4_buffer (in, inlen, resbuf);
823 gc_md5 (const void *in, size_t inlen, void *resbuf)
825 md5_buffer (in, inlen, resbuf);
832 gc_sha1 (const void *in, size_t inlen, void *resbuf)
834 sha1_buffer (in, inlen, resbuf);
839 #ifdef GC_USE_HMAC_MD5
841 gc_hmac_md5 (const void *key, size_t keylen,
842 const void *in, size_t inlen, char *resbuf)
844 hmac_md5 (key, keylen, in, inlen, resbuf);
849 #ifdef GC_USE_HMAC_SHA1
851 gc_hmac_sha1 (const void *key, size_t keylen,
852 const void *in, size_t inlen, char *resbuf)
854 hmac_sha1 (key, keylen, in, inlen, resbuf);