1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 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. */
32 #ifdef GNULIB_GC_RANDOM
34 # include <sys/types.h>
35 # include <sys/stat.h>
53 #if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1)
58 #ifdef GNULIB_GC_ARCFOUR
61 #ifdef GNULIB_GC_ARCTWO
67 #ifdef GNULIB_GC_RIJNDAEL
68 # include "rijndael-api-fst.h"
71 /* The results of open() in this file are not used with fchdir,
72 therefore save some unnecessary work in fchdir.c. */
76 #ifdef GNULIB_GC_RANDOM
77 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
78 # include <wincrypt.h>
79 HCRYPTPROV g_hProv = 0;
86 #ifdef GNULIB_GC_RANDOM
87 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
89 CryptReleaseContext(g_hProv, 0);
90 CryptAcquireContext(&g_hProv, NULL, NULL, PROV_RSA_FULL, 0);
100 #ifdef GNULIB_GC_RANDOM
101 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
104 CryptReleaseContext(g_hProv, 0);
113 #ifdef GNULIB_GC_RANDOM
118 randomize (int level, char *data, size_t datalen)
120 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
122 return GC_RANDOM_ERROR;
123 CryptGenRandom(g_hProv, (DWORD)datalen, data);
133 device = NAME_OF_NONCE_DEVICE;
137 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
141 device = NAME_OF_RANDOM_DEVICE;
145 if (strcmp (device, "no") == 0)
146 return GC_RANDOM_ERROR;
148 fd = open (device, O_RDONLY);
150 return GC_RANDOM_ERROR;
156 tmp = read (fd, data, datalen);
160 int save_errno = errno;
163 return GC_RANDOM_ERROR;
168 while (len < datalen);
172 return GC_RANDOM_ERROR;
179 gc_nonce (char *data, size_t datalen)
181 return randomize (0, data, datalen);
185 gc_pseudo_random (char *data, size_t datalen)
187 return randomize (1, data, datalen);
191 gc_random (char *data, size_t datalen)
193 return randomize (2, data, datalen);
198 /* Memory allocation. */
201 gc_set_allocators (gc_malloc_t func_malloc,
202 gc_malloc_t secure_malloc,
203 gc_secure_check_t secure_check,
204 gc_realloc_t func_realloc, gc_free_t func_free)
210 typedef struct _gc_cipher_ctx {
213 #ifdef GNULIB_GC_ARCTWO
214 arctwo_context arctwoContext;
215 char arctwoIV[ARCTWO_BLOCK_SIZE];
217 #ifdef GNULIB_GC_ARCFOUR
218 arcfour_context arcfourContext;
221 gl_des_ctx desContext;
223 #ifdef GNULIB_GC_RIJNDAEL
224 rijndaelKeyInstance aesEncKey;
225 rijndaelKeyInstance aesDecKey;
226 rijndaelCipherInstance aesContext;
231 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
232 gc_cipher_handle * outhandle)
237 ctx = calloc (sizeof (*ctx), 1);
239 return GC_MALLOC_ERROR;
246 #ifdef GNULIB_GC_ARCTWO
255 rc = GC_INVALID_CIPHER;
260 #ifdef GNULIB_GC_ARCFOUR
269 rc = GC_INVALID_CIPHER;
282 rc = GC_INVALID_CIPHER;
287 #ifdef GNULIB_GC_RIJNDAEL
298 rc = GC_INVALID_CIPHER;
304 rc = GC_INVALID_CIPHER;
316 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
318 _gc_cipher_ctx *ctx = handle;
322 #ifdef GNULIB_GC_ARCTWO
324 arctwo_setkey (&ctx->arctwoContext, keylen, key);
328 #ifdef GNULIB_GC_ARCFOUR
331 arcfour_setkey (&ctx->arcfourContext, key, keylen);
338 return GC_INVALID_CIPHER;
339 gl_des_setkey (&ctx->desContext, key);
343 #ifdef GNULIB_GC_RIJNDAEL
350 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
352 for (i = 0; i < keylen; i++)
353 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
355 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
356 keylen * 8, keyMaterial);
358 return GC_INVALID_CIPHER;
360 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
361 keylen * 8, keyMaterial);
363 return GC_INVALID_CIPHER;
365 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
367 return GC_INVALID_CIPHER;
373 return GC_INVALID_CIPHER;
380 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
382 _gc_cipher_ctx *ctx = handle;
386 #ifdef GNULIB_GC_ARCTWO
388 if (ivlen != ARCTWO_BLOCK_SIZE)
389 return GC_INVALID_CIPHER;
390 memcpy (ctx->arctwoIV, iv, ivlen);
394 #ifdef GNULIB_GC_RIJNDAEL
401 /* Doesn't use IV. */
408 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
410 for (i = 0; i < ivlen; i++)
411 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
413 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
416 return GC_INVALID_CIPHER;
421 return GC_INVALID_CIPHER;
427 return GC_INVALID_CIPHER;
434 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
436 _gc_cipher_ctx *ctx = handle;
440 #ifdef GNULIB_GC_ARCTWO
445 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
449 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
450 data += ARCTWO_BLOCK_SIZE)
453 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
454 data[i] ^= ctx->arctwoIV[i];
455 arctwo_encrypt (&ctx->arctwoContext, data, data,
457 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
462 return GC_INVALID_CIPHER;
467 #ifdef GNULIB_GC_ARCFOUR
470 arcfour_stream (&ctx->arcfourContext, data, data, len);
476 for (; len >= 8; len -= 8, data += 8)
477 gl_des_ecb_encrypt (&ctx->desContext, data, data);
481 #ifdef GNULIB_GC_RIJNDAEL
488 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
489 data, 8 * len, data);
491 return GC_INVALID_CIPHER;
497 return GC_INVALID_CIPHER;
504 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
506 _gc_cipher_ctx *ctx = handle;
510 #ifdef GNULIB_GC_ARCTWO
515 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
519 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
520 data += ARCTWO_BLOCK_SIZE)
522 char tmpIV[ARCTWO_BLOCK_SIZE];
524 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
525 arctwo_decrypt (&ctx->arctwoContext, data, data,
527 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
528 data[i] ^= ctx->arctwoIV[i];
529 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
534 return GC_INVALID_CIPHER;
539 #ifdef GNULIB_GC_ARCFOUR
542 arcfour_stream (&ctx->arcfourContext, data, data, len);
548 for (; len >= 8; len -= 8, data += 8)
549 gl_des_ecb_decrypt (&ctx->desContext, data, data);
553 #ifdef GNULIB_GC_RIJNDAEL
560 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
561 data, 8 * len, data);
563 return GC_INVALID_CIPHER;
569 return GC_INVALID_CIPHER;
576 gc_cipher_close (gc_cipher_handle handle)
578 _gc_cipher_ctx *ctx = handle;
587 #define MAX_DIGEST_SIZE 20
589 typedef struct _gc_hash_ctx {
592 char hash[MAX_DIGEST_SIZE];
594 struct md2_ctx md2Context;
597 struct md4_ctx md4Context;
600 struct md5_ctx md5Context;
602 #ifdef GNULIB_GC_SHA1
603 struct sha1_ctx sha1Context;
608 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
613 ctx = calloc (sizeof (*ctx), 1);
615 return GC_MALLOC_ERROR;
624 md2_init_ctx (&ctx->md2Context);
630 md4_init_ctx (&ctx->md4Context);
636 md5_init_ctx (&ctx->md5Context);
640 #ifdef GNULIB_GC_SHA1
642 sha1_init_ctx (&ctx->sha1Context);
647 rc = GC_INVALID_HASH;
657 rc = GC_INVALID_HASH;
670 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
672 _gc_hash_ctx *in = handle;
675 *outhandle = out = calloc (sizeof (*out), 1);
677 return GC_MALLOC_ERROR;
679 memcpy (out, in, sizeof (*out));
685 gc_hash_digest_length (Gc_hash hash)
692 len = GC_MD2_DIGEST_SIZE;
696 len = GC_MD4_DIGEST_SIZE;
700 len = GC_MD5_DIGEST_SIZE;
704 len = GC_RMD160_DIGEST_SIZE;
708 len = GC_SHA1_DIGEST_SIZE;
719 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
721 _gc_hash_ctx *ctx = handle;
727 md2_process_bytes (data, len, &ctx->md2Context);
733 md4_process_bytes (data, len, &ctx->md4Context);
739 md5_process_bytes (data, len, &ctx->md5Context);
743 #ifdef GNULIB_GC_SHA1
745 sha1_process_bytes (data, len, &ctx->sha1Context);
755 gc_hash_read (gc_hash_handle handle)
757 _gc_hash_ctx *ctx = handle;
758 const char *ret = NULL;
764 md2_finish_ctx (&ctx->md2Context, ctx->hash);
771 md4_finish_ctx (&ctx->md4Context, ctx->hash);
778 md5_finish_ctx (&ctx->md5Context, ctx->hash);
783 #ifdef GNULIB_GC_SHA1
785 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
798 gc_hash_close (gc_hash_handle handle)
800 _gc_hash_ctx *ctx = handle;
806 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
812 md2_buffer (in, inlen, resbuf);
818 md4_buffer (in, inlen, resbuf);
824 md5_buffer (in, inlen, resbuf);
828 #ifdef GNULIB_GC_SHA1
830 sha1_buffer (in, inlen, resbuf);
835 return GC_INVALID_HASH;
843 gc_md2 (const void *in, size_t inlen, void *resbuf)
845 md2_buffer (in, inlen, resbuf);
852 gc_md4 (const void *in, size_t inlen, void *resbuf)
854 md4_buffer (in, inlen, resbuf);
861 gc_md5 (const void *in, size_t inlen, void *resbuf)
863 md5_buffer (in, inlen, resbuf);
868 #ifdef GNULIB_GC_SHA1
870 gc_sha1 (const void *in, size_t inlen, void *resbuf)
872 sha1_buffer (in, inlen, resbuf);
877 #ifdef GNULIB_GC_HMAC_MD5
879 gc_hmac_md5 (const void *key, size_t keylen,
880 const void *in, size_t inlen, char *resbuf)
882 hmac_md5 (key, keylen, in, inlen, resbuf);
887 #ifdef GNULIB_GC_HMAC_SHA1
889 gc_hmac_sha1 (const void *key, size_t keylen,
890 const void *in, size_t inlen, char *resbuf)
892 hmac_sha1 (key, keylen, in, inlen, resbuf);