1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006 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. */
33 /* Get libgcrypt API. */
48 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
49 if (err == GPG_ERR_NO_ERROR)
51 if (gcry_check_version (GCRYPT_VERSION) == NULL)
54 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
55 if (err != GPG_ERR_NO_ERROR)
73 gc_nonce (char *data, size_t datalen)
75 gcry_create_nonce ((unsigned char *) data, datalen);
80 gc_pseudo_random (char *data, size_t datalen)
82 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
87 gc_random (char *data, size_t datalen)
89 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
95 /* Memory allocation. */
98 gc_set_allocators (gc_malloc_t func_malloc,
99 gc_malloc_t secure_malloc,
100 gc_secure_check_t secure_check,
101 gc_realloc_t func_realloc, gc_free_t func_free)
103 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
104 func_realloc, func_free);
110 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
111 gc_cipher_handle * outhandle)
113 int gcryalg, gcrymode;
119 gcryalg = GCRY_CIPHER_RIJNDAEL;
123 gcryalg = GCRY_CIPHER_RIJNDAEL;
127 gcryalg = GCRY_CIPHER_RIJNDAEL256;
131 gcryalg = GCRY_CIPHER_3DES;
135 gcryalg = GCRY_CIPHER_DES;
140 gcryalg = GCRY_CIPHER_ARCFOUR;
144 gcryalg = GCRY_CIPHER_RFC2268_40;
148 return GC_INVALID_CIPHER;
154 gcrymode = GCRY_CIPHER_MODE_ECB;
158 gcrymode = GCRY_CIPHER_MODE_CBC;
162 gcrymode = GCRY_CIPHER_MODE_STREAM;
166 return GC_INVALID_CIPHER;
169 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
170 gcryalg, gcrymode, 0);
171 if (gcry_err_code (err))
172 return GC_INVALID_CIPHER;
178 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
182 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
183 if (gcry_err_code (err))
184 return GC_INVALID_CIPHER;
190 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
194 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
195 if (gcry_err_code (err))
196 return GC_INVALID_CIPHER;
202 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
204 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
205 data, len, NULL, len) != 0)
206 return GC_INVALID_CIPHER;
212 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
214 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
215 data, len, NULL, len) != 0)
216 return GC_INVALID_CIPHER;
222 gc_cipher_close (gc_cipher_handle handle)
224 gcry_cipher_close (handle);
231 typedef struct _gc_hash_ctx {
236 char hash[GC_MD2_DIGEST_SIZE];
237 struct md2_ctx md2Context;
242 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
245 int gcryalg, gcrymode;
249 ctx = calloc (sizeof (*ctx), 1);
251 return GC_MALLOC_ERROR;
259 gcryalg = GCRY_MD_NONE;
263 gcryalg = GCRY_MD_MD4;
267 gcryalg = GCRY_MD_MD5;
271 gcryalg = GCRY_MD_SHA1;
275 gcryalg = GCRY_MD_RMD160;
279 rc = GC_INVALID_HASH;
289 gcrymode = GCRY_MD_FLAG_HMAC;
293 rc = GC_INVALID_HASH;
296 if (rc == GC_OK && gcryalg != GCRY_MD_NONE)
298 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode);
299 if (gcry_err_code (err))
300 rc = GC_INVALID_HASH;
312 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
314 _gc_hash_ctx *in = handle;
318 *outhandle = out = calloc (sizeof (*out), 1);
320 return GC_MALLOC_ERROR;
322 memcpy (out, in, sizeof (*out));
324 err = gcry_md_copy (&out->gch, in->gch);
328 return GC_INVALID_HASH;
335 gc_hash_digest_length (Gc_hash hash)
342 len = GC_MD2_DIGEST_SIZE;
346 len = GC_MD4_DIGEST_SIZE;
350 len = GC_MD5_DIGEST_SIZE;
354 len = GC_RMD160_DIGEST_SIZE;
358 len = GC_SHA1_DIGEST_SIZE;
369 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
371 _gc_hash_ctx *ctx = handle;
373 if (ctx->alg != GC_MD2)
375 gcry_md_setkey (ctx->gch, key, len);
379 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
381 _gc_hash_ctx *ctx = handle;
384 if (ctx->alg == GC_MD2)
385 md2_process_bytes (data, len, &ctx->md2Context);
388 gcry_md_write (ctx->gch, data, len);
392 gc_hash_read (gc_hash_handle handle)
394 _gc_hash_ctx *ctx = handle;
398 if (ctx->alg == GC_MD2)
400 md2_finish_ctx (&ctx->md2Context, ctx->hash);
406 gcry_md_final (ctx->gch);
407 digest = gcry_md_read (ctx->gch, 0);
414 gc_hash_close (gc_hash_handle handle)
416 _gc_hash_ctx *ctx = handle;
419 if (ctx->alg != GC_MD2)
421 gcry_md_close (ctx->gch);
427 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
435 md2_buffer (in, inlen, resbuf);
442 gcryalg = GCRY_MD_MD4;
448 gcryalg = GCRY_MD_MD5;
454 gcryalg = GCRY_MD_SHA1;
460 gcryalg = GCRY_MD_RMD160;
465 return GC_INVALID_HASH;
468 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
473 /* One-call interface. */
477 gc_md2 (const void *in, size_t inlen, void *resbuf)
479 md2_buffer (in, inlen, resbuf);
486 gc_md4 (const void *in, size_t inlen, void *resbuf)
488 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
493 assert (outlen == GC_MD4_DIGEST_SIZE);
495 err = gcry_md_open (&hd, GCRY_MD_MD4, 0);
496 if (err != GPG_ERR_NO_ERROR)
497 return GC_INVALID_HASH;
499 gcry_md_write (hd, in, inlen);
501 p = gcry_md_read (hd, GCRY_MD_MD4);
505 return GC_INVALID_HASH;
508 memcpy (resbuf, p, outlen);
518 gc_md5 (const void *in, size_t inlen, void *resbuf)
520 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
525 assert (outlen == GC_MD5_DIGEST_SIZE);
527 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
528 if (err != GPG_ERR_NO_ERROR)
529 return GC_INVALID_HASH;
531 gcry_md_write (hd, in, inlen);
533 p = gcry_md_read (hd, GCRY_MD_MD5);
537 return GC_INVALID_HASH;
540 memcpy (resbuf, p, outlen);
550 gc_sha1 (const void *in, size_t inlen, void *resbuf)
552 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
557 assert (outlen == GC_SHA1_DIGEST_SIZE);
559 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
560 if (err != GPG_ERR_NO_ERROR)
561 return GC_INVALID_HASH;
563 gcry_md_write (hd, in, inlen);
565 p = gcry_md_read (hd, GCRY_MD_SHA1);
569 return GC_INVALID_HASH;
572 memcpy (resbuf, p, outlen);
580 #ifdef GC_USE_HMAC_MD5
582 gc_hmac_md5 (const void *key, size_t keylen,
583 const void *in, size_t inlen, char *resbuf)
585 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
592 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
593 if (err != GPG_ERR_NO_ERROR)
594 return GC_INVALID_HASH;
596 err = gcry_md_setkey (mdh, key, keylen);
597 if (err != GPG_ERR_NO_ERROR)
600 return GC_INVALID_HASH;
603 gcry_md_write (mdh, in, inlen);
605 hash = gcry_md_read (mdh, GCRY_MD_MD5);
609 return GC_INVALID_HASH;
612 memcpy (resbuf, hash, hlen);
620 #ifdef GC_USE_HMAC_SHA1
622 gc_hmac_sha1 (const void *key, size_t keylen,
623 const void *in, size_t inlen, char *resbuf)
625 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
630 assert (hlen == GC_SHA1_DIGEST_SIZE);
632 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
633 if (err != GPG_ERR_NO_ERROR)
634 return GC_INVALID_HASH;
636 err = gcry_md_setkey (mdh, key, keylen);
637 if (err != GPG_ERR_NO_ERROR)
640 return GC_INVALID_HASH;
643 gcry_md_write (mdh, in, inlen);
645 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
649 return GC_INVALID_HASH;
652 memcpy (resbuf, hash, hlen);