1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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_check_version (GCRYPT_VERSION) == NULL)
52 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
53 if (err != GPG_ERR_NO_ERROR)
66 #ifdef GNULIB_GC_RANDOM
71 gc_nonce (char *data, size_t datalen)
73 gcry_create_nonce ((unsigned char *) data, datalen);
78 gc_pseudo_random (char *data, size_t datalen)
80 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
85 gc_random (char *data, size_t datalen)
87 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
93 /* Memory allocation. */
96 gc_set_allocators (gc_malloc_t func_malloc,
97 gc_malloc_t secure_malloc,
98 gc_secure_check_t secure_check,
99 gc_realloc_t func_realloc, gc_free_t func_free)
101 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
102 func_realloc, func_free);
108 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
109 gc_cipher_handle * outhandle)
111 int gcryalg, gcrymode;
117 gcryalg = GCRY_CIPHER_RIJNDAEL;
121 gcryalg = GCRY_CIPHER_RIJNDAEL;
125 gcryalg = GCRY_CIPHER_RIJNDAEL256;
129 gcryalg = GCRY_CIPHER_3DES;
133 gcryalg = GCRY_CIPHER_DES;
138 gcryalg = GCRY_CIPHER_ARCFOUR;
142 gcryalg = GCRY_CIPHER_RFC2268_40;
147 gcryalg = GCRY_CIPHER_CAMELLIA128;
151 gcryalg = GCRY_CIPHER_CAMELLIA256;
156 return GC_INVALID_CIPHER;
162 gcrymode = GCRY_CIPHER_MODE_ECB;
166 gcrymode = GCRY_CIPHER_MODE_CBC;
170 gcrymode = GCRY_CIPHER_MODE_STREAM;
174 return GC_INVALID_CIPHER;
177 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
178 gcryalg, gcrymode, 0);
179 if (gcry_err_code (err))
180 return GC_INVALID_CIPHER;
186 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
190 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
191 if (gcry_err_code (err))
192 return GC_INVALID_CIPHER;
198 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
202 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
203 if (gcry_err_code (err))
204 return GC_INVALID_CIPHER;
210 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
212 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
213 data, len, NULL, len) != 0)
214 return GC_INVALID_CIPHER;
220 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
222 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
223 data, len, NULL, len) != 0)
224 return GC_INVALID_CIPHER;
230 gc_cipher_close (gc_cipher_handle handle)
232 gcry_cipher_close (handle);
239 typedef struct _gc_hash_ctx {
244 char hash[GC_MD2_DIGEST_SIZE];
245 struct md2_ctx md2Context;
250 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
253 int gcryalg = 0, gcrymode = 0;
257 ctx = calloc (sizeof (*ctx), 1);
259 return GC_MALLOC_ERROR;
267 gcryalg = GCRY_MD_NONE;
271 gcryalg = GCRY_MD_MD4;
275 gcryalg = GCRY_MD_MD5;
279 gcryalg = GCRY_MD_SHA1;
283 gcryalg = GCRY_MD_SHA256;
287 gcryalg = GCRY_MD_SHA384;
291 gcryalg = GCRY_MD_SHA512;
295 gcryalg = GCRY_MD_RMD160;
299 rc = GC_INVALID_HASH;
309 gcrymode = GCRY_MD_FLAG_HMAC;
313 rc = GC_INVALID_HASH;
316 if (rc == GC_OK && gcryalg != GCRY_MD_NONE)
318 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode);
319 if (gcry_err_code (err))
320 rc = GC_INVALID_HASH;
332 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
334 _gc_hash_ctx *in = handle;
338 *outhandle = out = calloc (sizeof (*out), 1);
340 return GC_MALLOC_ERROR;
342 memcpy (out, in, sizeof (*out));
344 err = gcry_md_copy (&out->gch, in->gch);
348 return GC_INVALID_HASH;
355 gc_hash_digest_length (Gc_hash hash)
362 len = GC_MD2_DIGEST_SIZE;
366 len = GC_MD4_DIGEST_SIZE;
370 len = GC_MD5_DIGEST_SIZE;
374 len = GC_RMD160_DIGEST_SIZE;
378 len = GC_SHA1_DIGEST_SIZE;
382 len = GC_SHA256_DIGEST_SIZE;
386 len = GC_SHA384_DIGEST_SIZE;
390 len = GC_SHA512_DIGEST_SIZE;
401 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
403 _gc_hash_ctx *ctx = handle;
405 if (ctx->alg != GC_MD2)
407 gcry_md_setkey (ctx->gch, key, len);
411 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
413 _gc_hash_ctx *ctx = handle;
416 if (ctx->alg == GC_MD2)
417 md2_process_bytes (data, len, &ctx->md2Context);
420 gcry_md_write (ctx->gch, data, len);
424 gc_hash_read (gc_hash_handle handle)
426 _gc_hash_ctx *ctx = handle;
430 if (ctx->alg == GC_MD2)
432 md2_finish_ctx (&ctx->md2Context, ctx->hash);
438 gcry_md_final (ctx->gch);
439 digest = gcry_md_read (ctx->gch, 0);
446 gc_hash_close (gc_hash_handle handle)
448 _gc_hash_ctx *ctx = handle;
451 if (ctx->alg != GC_MD2)
453 gcry_md_close (ctx->gch);
459 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
467 md2_buffer (in, inlen, resbuf);
474 gcryalg = GCRY_MD_MD4;
480 gcryalg = GCRY_MD_MD5;
484 #ifdef GNULIB_GC_SHA1
486 gcryalg = GCRY_MD_SHA1;
490 #ifdef GNULIB_GC_SHA256
492 gcryalg = GCRY_MD_SHA256;
496 #ifdef GNULIB_GC_SHA384
498 gcryalg = GCRY_MD_SHA384;
502 #ifdef GNULIB_GC_SHA512
504 gcryalg = GCRY_MD_SHA512;
508 #ifdef GNULIB_GC_RMD160
510 gcryalg = GCRY_MD_RMD160;
515 return GC_INVALID_HASH;
518 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
523 /* One-call interface. */
527 gc_md2 (const void *in, size_t inlen, void *resbuf)
529 md2_buffer (in, inlen, resbuf);
536 gc_md4 (const void *in, size_t inlen, void *resbuf)
538 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
543 assert (outlen == GC_MD4_DIGEST_SIZE);
545 err = gcry_md_open (&hd, GCRY_MD_MD4, 0);
546 if (err != GPG_ERR_NO_ERROR)
547 return GC_INVALID_HASH;
549 gcry_md_write (hd, in, inlen);
551 p = gcry_md_read (hd, GCRY_MD_MD4);
555 return GC_INVALID_HASH;
558 memcpy (resbuf, p, outlen);
568 gc_md5 (const void *in, size_t inlen, void *resbuf)
570 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
575 assert (outlen == GC_MD5_DIGEST_SIZE);
577 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
578 if (err != GPG_ERR_NO_ERROR)
579 return GC_INVALID_HASH;
581 gcry_md_write (hd, in, inlen);
583 p = gcry_md_read (hd, GCRY_MD_MD5);
587 return GC_INVALID_HASH;
590 memcpy (resbuf, p, outlen);
598 #ifdef GNULIB_GC_SHA1
600 gc_sha1 (const void *in, size_t inlen, void *resbuf)
602 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
607 assert (outlen == GC_SHA1_DIGEST_SIZE);
609 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
610 if (err != GPG_ERR_NO_ERROR)
611 return GC_INVALID_HASH;
613 gcry_md_write (hd, in, inlen);
615 p = gcry_md_read (hd, GCRY_MD_SHA1);
619 return GC_INVALID_HASH;
622 memcpy (resbuf, p, outlen);
630 #ifdef GNULIB_GC_HMAC_MD5
632 gc_hmac_md5 (const void *key, size_t keylen,
633 const void *in, size_t inlen, char *resbuf)
635 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
642 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
643 if (err != GPG_ERR_NO_ERROR)
644 return GC_INVALID_HASH;
646 err = gcry_md_setkey (mdh, key, keylen);
647 if (err != GPG_ERR_NO_ERROR)
650 return GC_INVALID_HASH;
653 gcry_md_write (mdh, in, inlen);
655 hash = gcry_md_read (mdh, GCRY_MD_MD5);
659 return GC_INVALID_HASH;
662 memcpy (resbuf, hash, hlen);
670 #ifdef GNULIB_GC_HMAC_SHA1
672 gc_hmac_sha1 (const void *key, size_t keylen,
673 const void *in, size_t inlen, char *resbuf)
675 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
680 assert (hlen == GC_SHA1_DIGEST_SIZE);
682 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
683 if (err != GPG_ERR_NO_ERROR)
684 return GC_INVALID_HASH;
686 err = gcry_md_setkey (mdh, key, keylen);
687 if (err != GPG_ERR_NO_ERROR)
690 return GC_INVALID_HASH;
693 gcry_md_write (mdh, in, inlen);
695 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
699 return GC_INVALID_HASH;
702 memcpy (resbuf, hash, hlen);