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;
169 char arctwoIV[ARCTWO_BLOCK_SIZE];
171 #ifdef GC_USE_ARCFOUR
172 arcfour_context arcfourContext;
177 #ifdef GC_USE_RIJNDAEL
178 rijndaelKeyInstance aesEncKey;
179 rijndaelKeyInstance aesDecKey;
180 rijndaelCipherInstance aesContext;
185 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
186 gc_cipher_handle * outhandle)
191 ctx = calloc (sizeof (*ctx), 1);
207 rc = GC_INVALID_CIPHER;
212 #ifdef GC_USE_ARCFOUR
221 rc = GC_INVALID_CIPHER;
234 rc = GC_INVALID_CIPHER;
239 #ifdef GC_USE_RIJNDAEL
250 rc = GC_INVALID_CIPHER;
256 rc = GC_INVALID_CIPHER;
268 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
270 _gc_cipher_ctx *ctx = handle;
276 arctwo_setkey (&ctx->arctwoContext, keylen, key);
280 #ifdef GC_USE_ARCFOUR
283 arcfour_setkey (&ctx->arcfourContext, key, keylen);
290 return GC_INVALID_CIPHER;
291 des_setkey (&ctx->desContext, key);
295 #ifdef GC_USE_RIJNDAEL
302 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
304 for (i = 0; i < keylen; i++)
305 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
307 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
308 keylen * 8, keyMaterial);
310 return GC_INVALID_CIPHER;
312 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
313 keylen * 8, keyMaterial);
315 return GC_INVALID_CIPHER;
317 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
319 return GC_INVALID_CIPHER;
325 return GC_INVALID_CIPHER;
332 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
334 _gc_cipher_ctx *ctx = handle;
340 if (ivlen != ARCTWO_BLOCK_SIZE)
341 return GC_INVALID_CIPHER;
342 memcpy (ctx->arctwoIV, iv, ivlen);
346 #ifdef GC_USE_RIJNDAEL
353 /* Doesn't use IV. */
360 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
362 for (i = 0; i < ivlen; i++)
363 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
365 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
368 return GC_INVALID_CIPHER;
373 return GC_INVALID_CIPHER;
379 return GC_INVALID_CIPHER;
386 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
388 _gc_cipher_ctx *ctx = handle;
397 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
401 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
402 data += ARCTWO_BLOCK_SIZE)
405 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
406 data[i] ^= ctx->arctwoIV[i];
407 arctwo_encrypt (&ctx->arctwoContext, data, data,
409 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
414 return GC_INVALID_CIPHER;
419 #ifdef GC_USE_ARCFOUR
422 arcfour_stream (&ctx->arcfourContext, data, data, len);
428 for (; len >= 8; len -= 8, data += 8)
429 des_ecb_encrypt (&ctx->desContext, data, data);
433 #ifdef GC_USE_RIJNDAEL
440 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
441 data, 8 * len, data);
443 return GC_INVALID_CIPHER;
449 return GC_INVALID_CIPHER;
456 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
458 _gc_cipher_ctx *ctx = handle;
467 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
471 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
472 data += ARCTWO_BLOCK_SIZE)
474 char tmpIV[ARCTWO_BLOCK_SIZE];
476 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
477 arctwo_decrypt (&ctx->arctwoContext, data, data,
479 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
480 data[i] ^= ctx->arctwoIV[i];
481 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
486 return GC_INVALID_CIPHER;
491 #ifdef GC_USE_ARCFOUR
494 arcfour_stream (&ctx->arcfourContext, data, data, len);
500 for (; len >= 8; len -= 8, data += 8)
501 des_ecb_decrypt (&ctx->desContext, data, data);
505 #ifdef GC_USE_RIJNDAEL
512 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
513 data, 8 * len, data);
515 return GC_INVALID_CIPHER;
521 return GC_INVALID_CIPHER;
528 gc_cipher_close (gc_cipher_handle handle)
530 _gc_cipher_ctx *ctx = handle;
541 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
547 md4_buffer (in, inlen, resbuf);
553 md5_buffer (in, inlen, resbuf);
559 sha1_buffer (in, inlen, resbuf);
564 return GC_INVALID_HASH;
572 gc_md4 (const void *in, size_t inlen, void *resbuf)
574 md4_buffer (in, inlen, resbuf);
581 gc_md5 (const void *in, size_t inlen, void *resbuf)
583 md5_buffer (in, inlen, resbuf);
590 gc_sha1 (const void *in, size_t inlen, void *resbuf)
592 sha1_buffer (in, inlen, resbuf);
597 #ifdef GC_USE_HMAC_MD5
599 gc_hmac_md5 (const void *key, size_t keylen,
600 const void *in, size_t inlen, char *resbuf)
602 hmac_md5 (key, keylen, in, inlen, resbuf);
607 #ifdef GC_USE_HMAC_SHA1
609 gc_hmac_sha1 (const void *key, size_t keylen,
610 const void *in, size_t inlen, char *resbuf)
612 hmac_sha1 (key, keylen, in, inlen, resbuf);