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. */
30 /* Get libgcrypt API. */
42 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
43 if (err == GPG_ERR_NO_ERROR)
45 if (gcry_check_version (GCRYPT_VERSION) == NULL)
48 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
49 if (err != GPG_ERR_NO_ERROR)
65 gc_nonce (char *data, size_t datalen)
67 gcry_create_nonce ((unsigned char *) data, datalen);
72 gc_pseudo_random (char *data, size_t datalen)
74 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
79 gc_random (char *data, size_t datalen)
81 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
85 /* Memory allocation. */
88 gc_set_allocators (gc_malloc_t func_malloc,
89 gc_malloc_t secure_malloc,
90 gc_secure_check_t secure_check,
91 gc_realloc_t func_realloc, gc_free_t func_free)
93 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
94 func_realloc, func_free);
100 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
101 gc_cipher_handle * outhandle)
103 int gcryalg, gcrymode;
109 gcryalg = GCRY_CIPHER_RIJNDAEL;
113 gcryalg = GCRY_CIPHER_RIJNDAEL;
117 gcryalg = GCRY_CIPHER_RIJNDAEL256;
121 gcryalg = GCRY_CIPHER_3DES;
125 gcryalg = GCRY_CIPHER_DES;
130 gcryalg = GCRY_CIPHER_ARCFOUR;
134 gcryalg = GCRY_CIPHER_RFC2268_40;
138 return GC_INVALID_CIPHER;
144 gcrymode = GCRY_CIPHER_MODE_CBC;
148 gcrymode = GCRY_CIPHER_MODE_STREAM;
152 return GC_INVALID_CIPHER;
155 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
156 gcryalg, gcrymode, 0);
157 if (gcry_err_code (err))
158 return GC_INVALID_CIPHER;
164 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
168 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
169 if (gcry_err_code (err))
170 return GC_INVALID_CIPHER;
176 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
180 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
181 if (gcry_err_code (err))
182 return GC_INVALID_CIPHER;
188 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
190 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
191 data, len, NULL, len) != 0)
192 return GC_INVALID_CIPHER;
198 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
200 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
201 data, len, NULL, len) != 0)
202 return GC_INVALID_CIPHER;
208 gc_cipher_close (gc_cipher_handle handle)
210 gcry_cipher_close (handle);
218 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
220 int gcryalg, gcrymode;
226 gcryalg = GCRY_MD_MD5;
230 gcryalg = GCRY_MD_SHA1;
234 gcryalg = GCRY_MD_RMD160;
238 return GC_INVALID_HASH;
248 gcrymode = GCRY_MD_FLAG_HMAC;
252 return GC_INVALID_HASH;
255 err = gcry_md_open ((gcry_md_hd_t *) outhandle, gcryalg, gcrymode);
256 if (gcry_err_code (err))
257 return GC_INVALID_HASH;
263 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
267 err = gcry_md_copy ((gcry_md_hd_t *) outhandle, (gcry_md_hd_t) handle);
269 return GC_INVALID_HASH;
275 gc_hash_digest_length (Gc_hash hash)
282 gcryalg = GCRY_MD_MD5;
286 gcryalg = GCRY_MD_SHA1;
290 gcryalg = GCRY_MD_RMD160;
297 return gcry_md_get_algo_dlen (gcryalg);
301 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
303 gcry_md_setkey ((gcry_md_hd_t) handle, key, len);
307 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
309 gcry_md_write ((gcry_md_hd_t) handle, data, len);
313 gc_hash_read (gc_hash_handle handle)
317 gcry_md_final ((gcry_md_hd_t) handle);
318 digest = gcry_md_read ((gcry_md_hd_t) handle, 0);
324 gc_hash_close (gc_hash_handle handle)
326 gcry_md_close ((gcry_md_hd_t) handle);
330 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
338 gcryalg = GCRY_MD_MD5;
344 gcryalg = GCRY_MD_SHA1;
350 gcryalg = GCRY_MD_RMD160;
355 return GC_INVALID_HASH;
358 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
363 /* One-call interface. */
367 gc_md5 (const void *in, size_t inlen, void *resbuf)
369 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
374 assert (outlen == GC_MD5_DIGEST_SIZE);
376 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
377 if (err != GPG_ERR_NO_ERROR)
378 return GC_INVALID_HASH;
380 gcry_md_write (hd, in, inlen);
382 p = gcry_md_read (hd, GCRY_MD_MD5);
386 return GC_INVALID_HASH;
389 memcpy (resbuf, p, outlen);
399 gc_sha1 (const void *in, size_t inlen, void *resbuf)
401 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
406 assert (outlen == GC_SHA1_DIGEST_SIZE);
408 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
409 if (err != GPG_ERR_NO_ERROR)
410 return GC_INVALID_HASH;
412 gcry_md_write (hd, in, inlen);
414 p = gcry_md_read (hd, GCRY_MD_SHA1);
418 return GC_INVALID_HASH;
421 memcpy (resbuf, p, outlen);
429 #ifdef GC_USE_HMAC_MD5
431 gc_hmac_md5 (const void *key, size_t keylen,
432 const void *in, size_t inlen, char *resbuf)
434 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
441 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
442 if (err != GPG_ERR_NO_ERROR)
443 return GC_INVALID_HASH;
445 err = gcry_md_setkey (mdh, key, keylen);
446 if (err != GPG_ERR_NO_ERROR)
449 return GC_INVALID_HASH;
452 gcry_md_write (mdh, in, inlen);
454 hash = gcry_md_read (mdh, GCRY_MD_MD5);
458 return GC_INVALID_HASH;
461 memcpy (resbuf, hash, hlen);
469 #ifdef GC_USE_HMAC_SHA1
471 gc_hmac_sha1 (const void *key, size_t keylen,
472 const void *in, size_t inlen, char *resbuf)
474 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
481 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
482 if (err != GPG_ERR_NO_ERROR)
483 return GC_INVALID_HASH;
485 err = gcry_md_setkey (mdh, key, keylen);
486 if (err != GPG_ERR_NO_ERROR)
489 return GC_INVALID_HASH;
492 gcry_md_write (mdh, in, inlen);
494 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
498 return GC_INVALID_HASH;
501 memcpy (resbuf, hash, hlen);