1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
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 Libgcrypt. */
31 /* Get libgcrypt API. */
46 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
47 if (err == GPG_ERR_NO_ERROR)
49 if (gcry_control (GCRYCTL_DISABLE_SECMEM, NULL, 0))
52 if (gcry_check_version (GCRYPT_VERSION) == NULL)
55 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
56 if (err != GPG_ERR_NO_ERROR)
69 #ifdef GNULIB_GC_RANDOM
74 gc_nonce (char *data, size_t datalen)
76 gcry_create_nonce ((unsigned char *) data, datalen);
81 gc_pseudo_random (char *data, size_t datalen)
83 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
88 gc_random (char *data, size_t datalen)
90 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
96 /* Memory allocation. */
99 gc_set_allocators (gc_malloc_t func_malloc,
100 gc_malloc_t secure_malloc,
101 gc_secure_check_t secure_check,
102 gc_realloc_t func_realloc, gc_free_t func_free)
104 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
105 func_realloc, func_free);
111 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
112 gc_cipher_handle * outhandle)
114 int gcryalg, gcrymode;
120 gcryalg = GCRY_CIPHER_RIJNDAEL;
124 gcryalg = GCRY_CIPHER_RIJNDAEL;
128 gcryalg = GCRY_CIPHER_RIJNDAEL256;
132 gcryalg = GCRY_CIPHER_3DES;
136 gcryalg = GCRY_CIPHER_DES;
141 gcryalg = GCRY_CIPHER_ARCFOUR;
145 gcryalg = GCRY_CIPHER_RFC2268_40;
150 gcryalg = GCRY_CIPHER_CAMELLIA128;
154 gcryalg = GCRY_CIPHER_CAMELLIA256;
159 return GC_INVALID_CIPHER;
165 gcrymode = GCRY_CIPHER_MODE_ECB;
169 gcrymode = GCRY_CIPHER_MODE_CBC;
173 gcrymode = GCRY_CIPHER_MODE_STREAM;
177 return GC_INVALID_CIPHER;
180 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
181 gcryalg, gcrymode, 0);
182 if (gcry_err_code (err))
183 return GC_INVALID_CIPHER;
189 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
193 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
194 if (gcry_err_code (err))
195 return GC_INVALID_CIPHER;
201 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
205 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
206 if (gcry_err_code (err))
207 return GC_INVALID_CIPHER;
213 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
215 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
216 data, len, NULL, len) != 0)
217 return GC_INVALID_CIPHER;
223 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
225 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
226 data, len, NULL, len) != 0)
227 return GC_INVALID_CIPHER;
233 gc_cipher_close (gc_cipher_handle handle)
235 gcry_cipher_close (handle);
242 typedef struct _gc_hash_ctx {
247 char hash[GC_MD2_DIGEST_SIZE];
248 struct md2_ctx md2Context;
253 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
256 int gcryalg = 0, gcrymode = 0;
260 ctx = calloc (sizeof (*ctx), 1);
262 return GC_MALLOC_ERROR;
270 gcryalg = GCRY_MD_NONE;
274 gcryalg = GCRY_MD_MD4;
278 gcryalg = GCRY_MD_MD5;
282 gcryalg = GCRY_MD_SHA1;
286 gcryalg = GCRY_MD_SHA256;
290 gcryalg = GCRY_MD_SHA384;
294 gcryalg = GCRY_MD_SHA512;
298 gcryalg = GCRY_MD_RMD160;
302 rc = GC_INVALID_HASH;
312 gcrymode = GCRY_MD_FLAG_HMAC;
316 rc = GC_INVALID_HASH;
319 if (rc == GC_OK && gcryalg != GCRY_MD_NONE)
321 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode);
322 if (gcry_err_code (err))
323 rc = GC_INVALID_HASH;
335 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
337 _gc_hash_ctx *in = handle;
341 *outhandle = out = calloc (sizeof (*out), 1);
343 return GC_MALLOC_ERROR;
345 memcpy (out, in, sizeof (*out));
347 err = gcry_md_copy (&out->gch, in->gch);
351 return GC_INVALID_HASH;
358 gc_hash_digest_length (Gc_hash hash)
365 len = GC_MD2_DIGEST_SIZE;
369 len = GC_MD4_DIGEST_SIZE;
373 len = GC_MD5_DIGEST_SIZE;
377 len = GC_RMD160_DIGEST_SIZE;
381 len = GC_SHA1_DIGEST_SIZE;
385 len = GC_SHA256_DIGEST_SIZE;
389 len = GC_SHA384_DIGEST_SIZE;
393 len = GC_SHA512_DIGEST_SIZE;
404 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
406 _gc_hash_ctx *ctx = handle;
408 if (ctx->alg != GC_MD2)
410 gcry_md_setkey (ctx->gch, key, len);
414 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
416 _gc_hash_ctx *ctx = handle;
419 if (ctx->alg == GC_MD2)
420 md2_process_bytes (data, len, &ctx->md2Context);
423 gcry_md_write (ctx->gch, data, len);
427 gc_hash_read (gc_hash_handle handle)
429 _gc_hash_ctx *ctx = handle;
433 if (ctx->alg == GC_MD2)
435 md2_finish_ctx (&ctx->md2Context, ctx->hash);
441 gcry_md_final (ctx->gch);
442 digest = gcry_md_read (ctx->gch, 0);
449 gc_hash_close (gc_hash_handle handle)
451 _gc_hash_ctx *ctx = handle;
454 if (ctx->alg != GC_MD2)
456 gcry_md_close (ctx->gch);
462 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
470 md2_buffer (in, inlen, resbuf);
477 gcryalg = GCRY_MD_MD4;
483 gcryalg = GCRY_MD_MD5;
487 #ifdef GNULIB_GC_SHA1
489 gcryalg = GCRY_MD_SHA1;
493 #ifdef GNULIB_GC_SHA256
495 gcryalg = GCRY_MD_SHA256;
499 #ifdef GNULIB_GC_SHA384
501 gcryalg = GCRY_MD_SHA384;
505 #ifdef GNULIB_GC_SHA512
507 gcryalg = GCRY_MD_SHA512;
511 #ifdef GNULIB_GC_RMD160
513 gcryalg = GCRY_MD_RMD160;
518 return GC_INVALID_HASH;
521 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
526 /* One-call interface. */
530 gc_md2 (const void *in, size_t inlen, void *resbuf)
532 md2_buffer (in, inlen, resbuf);
539 gc_md4 (const void *in, size_t inlen, void *resbuf)
541 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
546 assert (outlen == GC_MD4_DIGEST_SIZE);
548 err = gcry_md_open (&hd, GCRY_MD_MD4, 0);
549 if (err != GPG_ERR_NO_ERROR)
550 return GC_INVALID_HASH;
552 gcry_md_write (hd, in, inlen);
554 p = gcry_md_read (hd, GCRY_MD_MD4);
558 return GC_INVALID_HASH;
561 memcpy (resbuf, p, outlen);
571 gc_md5 (const void *in, size_t inlen, void *resbuf)
573 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
578 assert (outlen == GC_MD5_DIGEST_SIZE);
580 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
581 if (err != GPG_ERR_NO_ERROR)
582 return GC_INVALID_HASH;
584 gcry_md_write (hd, in, inlen);
586 p = gcry_md_read (hd, GCRY_MD_MD5);
590 return GC_INVALID_HASH;
593 memcpy (resbuf, p, outlen);
601 #ifdef GNULIB_GC_SHA1
603 gc_sha1 (const void *in, size_t inlen, void *resbuf)
605 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
610 assert (outlen == GC_SHA1_DIGEST_SIZE);
612 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
613 if (err != GPG_ERR_NO_ERROR)
614 return GC_INVALID_HASH;
616 gcry_md_write (hd, in, inlen);
618 p = gcry_md_read (hd, GCRY_MD_SHA1);
622 return GC_INVALID_HASH;
625 memcpy (resbuf, p, outlen);
633 #ifdef GNULIB_GC_HMAC_MD5
635 gc_hmac_md5 (const void *key, size_t keylen,
636 const void *in, size_t inlen, char *resbuf)
638 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
645 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
646 if (err != GPG_ERR_NO_ERROR)
647 return GC_INVALID_HASH;
649 err = gcry_md_setkey (mdh, key, keylen);
650 if (err != GPG_ERR_NO_ERROR)
653 return GC_INVALID_HASH;
656 gcry_md_write (mdh, in, inlen);
658 hash = gcry_md_read (mdh, GCRY_MD_MD5);
662 return GC_INVALID_HASH;
665 memcpy (resbuf, hash, hlen);
673 #ifdef GNULIB_GC_HMAC_SHA1
675 gc_hmac_sha1 (const void *key, size_t keylen,
676 const void *in, size_t inlen, char *resbuf)
678 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
683 assert (hlen == GC_SHA1_DIGEST_SIZE);
685 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
686 if (err != GPG_ERR_NO_ERROR)
687 return GC_INVALID_HASH;
689 err = gcry_md_setkey (mdh, key, keylen);
690 if (err != GPG_ERR_NO_ERROR)
693 return GC_INVALID_HASH;
696 gcry_md_write (mdh, in, inlen);
698 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
702 return GC_INVALID_HASH;
705 memcpy (resbuf, hash, hlen);