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>
53 #ifdef GC_USE_HMAC_MD5
67 #ifdef GC_USE_RIJNDAEL
68 # include "rijndael-api-fst.h"
86 randomize (int level, char *data, size_t datalen)
96 device = NAME_OF_NONCE_DEVICE;
100 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
104 device = NAME_OF_RANDOM_DEVICE;
108 fd = open (device, O_RDONLY);
110 return GC_RANDOM_ERROR;
116 tmp = read (fd, data, datalen);
120 int save_errno = errno;
123 return GC_RANDOM_ERROR;
128 while (len < datalen);
132 return GC_RANDOM_ERROR;
138 gc_nonce (char *data, size_t datalen)
140 return randomize (0, data, datalen);
144 gc_pseudo_random (char *data, size_t datalen)
146 return randomize (1, data, datalen);
150 gc_random (char *data, size_t datalen)
152 return randomize (2, data, datalen);
155 /* Memory allocation. */
158 gc_set_allocators (gc_malloc_t func_malloc,
159 gc_malloc_t secure_malloc,
160 gc_secure_check_t secure_check,
161 gc_realloc_t func_realloc, gc_free_t func_free)
167 typedef struct _gc_cipher_ctx {
171 arctwo_context arctwoContext;
172 char arctwoIV[ARCTWO_BLOCK_SIZE];
174 #ifdef GC_USE_ARCFOUR
175 arcfour_context arcfourContext;
180 #ifdef GC_USE_RIJNDAEL
181 rijndaelKeyInstance aesEncKey;
182 rijndaelKeyInstance aesDecKey;
183 rijndaelCipherInstance aesContext;
188 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
189 gc_cipher_handle * outhandle)
194 ctx = calloc (sizeof (*ctx), 1);
210 rc = GC_INVALID_CIPHER;
215 #ifdef GC_USE_ARCFOUR
224 rc = GC_INVALID_CIPHER;
237 rc = GC_INVALID_CIPHER;
242 #ifdef GC_USE_RIJNDAEL
253 rc = GC_INVALID_CIPHER;
259 rc = GC_INVALID_CIPHER;
271 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
273 _gc_cipher_ctx *ctx = handle;
279 arctwo_setkey (&ctx->arctwoContext, keylen, key);
283 #ifdef GC_USE_ARCFOUR
286 arcfour_setkey (&ctx->arcfourContext, key, keylen);
293 return GC_INVALID_CIPHER;
294 des_setkey (&ctx->desContext, key);
298 #ifdef GC_USE_RIJNDAEL
305 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
307 for (i = 0; i < keylen; i++)
308 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
310 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
311 keylen * 8, keyMaterial);
313 return GC_INVALID_CIPHER;
315 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
316 keylen * 8, keyMaterial);
318 return GC_INVALID_CIPHER;
320 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
322 return GC_INVALID_CIPHER;
328 return GC_INVALID_CIPHER;
335 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
337 _gc_cipher_ctx *ctx = handle;
343 if (ivlen != ARCTWO_BLOCK_SIZE)
344 return GC_INVALID_CIPHER;
345 memcpy (ctx->arctwoIV, iv, ivlen);
349 #ifdef GC_USE_RIJNDAEL
356 /* Doesn't use IV. */
363 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
365 for (i = 0; i < ivlen; i++)
366 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
368 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
371 return GC_INVALID_CIPHER;
376 return GC_INVALID_CIPHER;
382 return GC_INVALID_CIPHER;
389 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
391 _gc_cipher_ctx *ctx = handle;
400 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
404 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
405 data += ARCTWO_BLOCK_SIZE)
408 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
409 data[i] ^= ctx->arctwoIV[i];
410 arctwo_encrypt (&ctx->arctwoContext, data, data,
412 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
417 return GC_INVALID_CIPHER;
422 #ifdef GC_USE_ARCFOUR
425 arcfour_stream (&ctx->arcfourContext, data, data, len);
431 for (; len >= 8; len -= 8, data += 8)
432 des_ecb_encrypt (&ctx->desContext, data, data);
436 #ifdef GC_USE_RIJNDAEL
443 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
444 data, 8 * len, data);
446 return GC_INVALID_CIPHER;
452 return GC_INVALID_CIPHER;
459 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
461 _gc_cipher_ctx *ctx = handle;
470 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
474 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
475 data += ARCTWO_BLOCK_SIZE)
477 char tmpIV[ARCTWO_BLOCK_SIZE];
479 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
480 arctwo_decrypt (&ctx->arctwoContext, data, data,
482 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
483 data[i] ^= ctx->arctwoIV[i];
484 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
489 return GC_INVALID_CIPHER;
494 #ifdef GC_USE_ARCFOUR
497 arcfour_stream (&ctx->arcfourContext, data, data, len);
503 for (; len >= 8; len -= 8, data += 8)
504 des_ecb_decrypt (&ctx->desContext, data, data);
508 #ifdef GC_USE_RIJNDAEL
515 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
516 data, 8 * len, data);
518 return GC_INVALID_CIPHER;
524 return GC_INVALID_CIPHER;
531 gc_cipher_close (gc_cipher_handle handle)
533 _gc_cipher_ctx *ctx = handle;
543 #define MAX_DIGEST_SIZE 20
545 typedef struct _gc_hash_ctx {
548 char hash[MAX_DIGEST_SIZE];
550 struct md2_ctx md2Context;
553 struct md4_ctx md4Context;
556 struct md5_ctx md5Context;
559 struct sha1_ctx sha1Context;
564 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
569 ctx = calloc (sizeof (*ctx), 1);
578 md2_init_ctx (&ctx->md2Context);
584 md4_init_ctx (&ctx->md4Context);
590 md5_init_ctx (&ctx->md5Context);
596 sha1_init_ctx (&ctx->sha1Context);
601 rc = GC_INVALID_HASH;
611 rc = GC_INVALID_HASH;
624 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
626 _gc_hash_ctx *in = handle;
629 *outhandle = out = calloc (sizeof (*out), 1);
631 return GC_MALLOC_ERROR;
633 memcpy (out, in, sizeof (*out));
639 gc_hash_digest_length (Gc_hash hash)
646 len = GC_MD2_DIGEST_SIZE;
650 len = GC_MD4_DIGEST_SIZE;
654 len = GC_MD5_DIGEST_SIZE;
658 len = GC_RMD160_DIGEST_SIZE;
662 len = GC_SHA1_DIGEST_SIZE;
673 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
675 _gc_hash_ctx *ctx = handle;
681 md2_process_bytes (data, len, &ctx->md2Context);
687 md4_process_bytes (data, len, &ctx->md4Context);
693 md5_process_bytes (data, len, &ctx->md5Context);
699 sha1_process_bytes (data, len, &ctx->sha1Context);
709 gc_hash_read (gc_hash_handle handle)
711 _gc_hash_ctx *ctx = handle;
712 const char *ret = NULL;
718 md2_finish_ctx (&ctx->md2Context, ctx->hash);
725 md4_finish_ctx (&ctx->md4Context, ctx->hash);
732 md5_finish_ctx (&ctx->md5Context, ctx->hash);
739 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
752 gc_hash_close (gc_hash_handle handle)
754 _gc_hash_ctx *ctx = handle;
760 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
766 md2_buffer (in, inlen, resbuf);
772 md4_buffer (in, inlen, resbuf);
778 md5_buffer (in, inlen, resbuf);
784 sha1_buffer (in, inlen, resbuf);
789 return GC_INVALID_HASH;
797 gc_md2 (const void *in, size_t inlen, void *resbuf)
799 md2_buffer (in, inlen, resbuf);
806 gc_md4 (const void *in, size_t inlen, void *resbuf)
808 md4_buffer (in, inlen, resbuf);
815 gc_md5 (const void *in, size_t inlen, void *resbuf)
817 md5_buffer (in, inlen, resbuf);
824 gc_sha1 (const void *in, size_t inlen, void *resbuf)
826 sha1_buffer (in, inlen, resbuf);
831 #ifdef GC_USE_HMAC_MD5
833 gc_hmac_md5 (const void *key, size_t keylen,
834 const void *in, size_t inlen, char *resbuf)
836 hmac_md5 (key, keylen, in, inlen, resbuf);
841 #ifdef GC_USE_HMAC_SHA1
843 gc_hmac_sha1 (const void *key, size_t keylen,
844 const void *in, size_t inlen, char *resbuf)
846 hmac_sha1 (key, keylen, in, inlen, resbuf);