1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
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 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)
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);
91 /* Memory allocation. */
94 gc_set_allocators (gc_malloc_t func_malloc,
95 gc_malloc_t secure_malloc,
96 gc_secure_check_t secure_check,
97 gc_realloc_t func_realloc, gc_free_t func_free)
99 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
100 func_realloc, func_free);
106 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
107 gc_cipher_handle * outhandle)
109 int gcryalg, gcrymode;
115 gcryalg = GCRY_CIPHER_RIJNDAEL;
119 gcryalg = GCRY_CIPHER_RIJNDAEL;
123 gcryalg = GCRY_CIPHER_RIJNDAEL256;
127 gcryalg = GCRY_CIPHER_3DES;
131 gcryalg = GCRY_CIPHER_DES;
136 gcryalg = GCRY_CIPHER_ARCFOUR;
140 gcryalg = GCRY_CIPHER_RFC2268_40;
144 return GC_INVALID_CIPHER;
150 gcrymode = GCRY_CIPHER_MODE_ECB;
154 gcrymode = GCRY_CIPHER_MODE_CBC;
158 gcrymode = GCRY_CIPHER_MODE_STREAM;
162 return GC_INVALID_CIPHER;
165 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
166 gcryalg, gcrymode, 0);
167 if (gcry_err_code (err))
168 return GC_INVALID_CIPHER;
174 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
178 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
179 if (gcry_err_code (err))
180 return GC_INVALID_CIPHER;
186 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
190 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
191 if (gcry_err_code (err))
192 return GC_INVALID_CIPHER;
198 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
200 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
201 data, len, NULL, len) != 0)
202 return GC_INVALID_CIPHER;
208 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
210 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
211 data, len, NULL, len) != 0)
212 return GC_INVALID_CIPHER;
218 gc_cipher_close (gc_cipher_handle handle)
220 gcry_cipher_close (handle);
227 typedef struct _gc_hash_ctx {
232 char hash[GC_MD2_DIGEST_SIZE];
233 struct md2_ctx md2Context;
238 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
241 int gcryalg, gcrymode;
245 ctx = calloc (sizeof (*ctx), 1);
253 gcryalg = GCRY_MD_NONE;
257 gcryalg = GCRY_MD_MD4;
261 gcryalg = GCRY_MD_MD5;
265 gcryalg = GCRY_MD_SHA1;
269 gcryalg = GCRY_MD_RMD160;
273 rc = GC_INVALID_HASH;
283 gcrymode = GCRY_MD_FLAG_HMAC;
287 rc = GC_INVALID_HASH;
290 if (rc == GC_OK && gcryalg != GCRY_MD_NONE)
292 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode);
293 if (gcry_err_code (err))
294 rc = GC_INVALID_HASH;
306 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
308 _gc_hash_ctx *in = handle;
312 *outhandle = out = calloc (sizeof (*out), 1);
314 return GC_MALLOC_ERROR;
316 memcpy (out, in, sizeof (*out));
318 err = gcry_md_copy (&out->gch, in->gch);
322 return GC_INVALID_HASH;
329 gc_hash_digest_length (Gc_hash hash)
336 len = GC_MD2_DIGEST_SIZE;
340 len = GC_MD4_DIGEST_SIZE;
344 len = GC_MD5_DIGEST_SIZE;
348 len = GC_RMD160_DIGEST_SIZE;
352 len = GC_SHA1_DIGEST_SIZE;
363 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
365 _gc_hash_ctx *ctx = handle;
367 if (ctx->alg != GC_MD2)
369 gcry_md_setkey (ctx->gch, key, len);
373 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
375 _gc_hash_ctx *ctx = handle;
378 if (ctx->alg == GC_MD2)
379 md2_process_bytes (data, len, &ctx->md2Context);
382 gcry_md_write (ctx->gch, data, len);
386 gc_hash_read (gc_hash_handle handle)
388 _gc_hash_ctx *ctx = handle;
392 if (ctx->alg == GC_MD2)
394 md2_finish_ctx (&ctx->md2Context, ctx->hash);
400 gcry_md_final (ctx->gch);
401 digest = gcry_md_read (ctx->gch, 0);
408 gc_hash_close (gc_hash_handle handle)
410 _gc_hash_ctx *ctx = handle;
413 if (ctx->alg != GC_MD2)
415 gcry_md_close (ctx->gch);
421 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
429 md2_buffer (in, inlen, resbuf);
436 gcryalg = GCRY_MD_MD4;
442 gcryalg = GCRY_MD_MD5;
448 gcryalg = GCRY_MD_SHA1;
454 gcryalg = GCRY_MD_RMD160;
459 return GC_INVALID_HASH;
462 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
467 /* One-call interface. */
471 gc_md2 (const void *in, size_t inlen, void *resbuf)
473 md2_buffer (in, inlen, resbuf);
480 gc_md4 (const void *in, size_t inlen, void *resbuf)
482 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
487 assert (outlen == GC_MD4_DIGEST_SIZE);
489 err = gcry_md_open (&hd, GCRY_MD_MD4, 0);
490 if (err != GPG_ERR_NO_ERROR)
491 return GC_INVALID_HASH;
493 gcry_md_write (hd, in, inlen);
495 p = gcry_md_read (hd, GCRY_MD_MD4);
499 return GC_INVALID_HASH;
502 memcpy (resbuf, p, outlen);
512 gc_md5 (const void *in, size_t inlen, void *resbuf)
514 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
519 assert (outlen == GC_MD5_DIGEST_SIZE);
521 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
522 if (err != GPG_ERR_NO_ERROR)
523 return GC_INVALID_HASH;
525 gcry_md_write (hd, in, inlen);
527 p = gcry_md_read (hd, GCRY_MD_MD5);
531 return GC_INVALID_HASH;
534 memcpy (resbuf, p, outlen);
544 gc_sha1 (const void *in, size_t inlen, void *resbuf)
546 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
551 assert (outlen == GC_SHA1_DIGEST_SIZE);
553 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
554 if (err != GPG_ERR_NO_ERROR)
555 return GC_INVALID_HASH;
557 gcry_md_write (hd, in, inlen);
559 p = gcry_md_read (hd, GCRY_MD_SHA1);
563 return GC_INVALID_HASH;
566 memcpy (resbuf, p, outlen);
574 #ifdef GC_USE_HMAC_MD5
576 gc_hmac_md5 (const void *key, size_t keylen,
577 const void *in, size_t inlen, char *resbuf)
579 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
586 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
587 if (err != GPG_ERR_NO_ERROR)
588 return GC_INVALID_HASH;
590 err = gcry_md_setkey (mdh, key, keylen);
591 if (err != GPG_ERR_NO_ERROR)
594 return GC_INVALID_HASH;
597 gcry_md_write (mdh, in, inlen);
599 hash = gcry_md_read (mdh, GCRY_MD_MD5);
603 return GC_INVALID_HASH;
606 memcpy (resbuf, hash, hlen);
614 #ifdef GC_USE_HMAC_SHA1
616 gc_hmac_sha1 (const void *key, size_t keylen,
617 const void *in, size_t inlen, char *resbuf)
619 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
624 assert (hlen == GC_SHA1_DIGEST_SIZE);
626 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
627 if (err != GPG_ERR_NO_ERROR)
628 return GC_INVALID_HASH;
630 err = gcry_md_setkey (mdh, key, keylen);
631 if (err != GPG_ERR_NO_ERROR)
634 return GC_INVALID_HASH;
637 gcry_md_write (mdh, in, inlen);
639 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
643 return GC_INVALID_HASH;
646 memcpy (resbuf, hash, hlen);