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_MD4;
230 gcryalg = GCRY_MD_MD5;
234 gcryalg = GCRY_MD_SHA1;
238 gcryalg = GCRY_MD_RMD160;
242 return GC_INVALID_HASH;
252 gcrymode = GCRY_MD_FLAG_HMAC;
256 return GC_INVALID_HASH;
259 err = gcry_md_open ((gcry_md_hd_t *) outhandle, gcryalg, gcrymode);
260 if (gcry_err_code (err))
261 return GC_INVALID_HASH;
267 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
271 err = gcry_md_copy ((gcry_md_hd_t *) outhandle, (gcry_md_hd_t) handle);
273 return GC_INVALID_HASH;
279 gc_hash_digest_length (Gc_hash hash)
286 gcryalg = GCRY_MD_MD4;
290 gcryalg = GCRY_MD_MD5;
294 gcryalg = GCRY_MD_SHA1;
298 gcryalg = GCRY_MD_RMD160;
305 return gcry_md_get_algo_dlen (gcryalg);
309 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
311 gcry_md_setkey ((gcry_md_hd_t) handle, key, len);
315 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
317 gcry_md_write ((gcry_md_hd_t) handle, data, len);
321 gc_hash_read (gc_hash_handle handle)
325 gcry_md_final ((gcry_md_hd_t) handle);
326 digest = gcry_md_read ((gcry_md_hd_t) handle, 0);
332 gc_hash_close (gc_hash_handle handle)
334 gcry_md_close ((gcry_md_hd_t) handle);
338 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
346 gcryalg = GCRY_MD_MD4;
352 gcryalg = GCRY_MD_MD5;
358 gcryalg = GCRY_MD_SHA1;
364 gcryalg = GCRY_MD_RMD160;
369 return GC_INVALID_HASH;
372 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
377 /* One-call interface. */
381 gc_md4 (const void *in, size_t inlen, void *resbuf)
383 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
388 assert (outlen == GC_MD4_DIGEST_SIZE);
390 err = gcry_md_open (&hd, GCRY_MD_MD4, 0);
391 if (err != GPG_ERR_NO_ERROR)
392 return GC_INVALID_HASH;
394 gcry_md_write (hd, in, inlen);
396 p = gcry_md_read (hd, GCRY_MD_MD4);
400 return GC_INVALID_HASH;
403 memcpy (resbuf, p, outlen);
413 gc_md5 (const void *in, size_t inlen, void *resbuf)
415 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
420 assert (outlen == GC_MD5_DIGEST_SIZE);
422 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
423 if (err != GPG_ERR_NO_ERROR)
424 return GC_INVALID_HASH;
426 gcry_md_write (hd, in, inlen);
428 p = gcry_md_read (hd, GCRY_MD_MD5);
432 return GC_INVALID_HASH;
435 memcpy (resbuf, p, outlen);
445 gc_sha1 (const void *in, size_t inlen, void *resbuf)
447 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
452 assert (outlen == GC_SHA1_DIGEST_SIZE);
454 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
455 if (err != GPG_ERR_NO_ERROR)
456 return GC_INVALID_HASH;
458 gcry_md_write (hd, in, inlen);
460 p = gcry_md_read (hd, GCRY_MD_SHA1);
464 return GC_INVALID_HASH;
467 memcpy (resbuf, p, outlen);
475 #ifdef GC_USE_HMAC_MD5
477 gc_hmac_md5 (const void *key, size_t keylen,
478 const void *in, size_t inlen, char *resbuf)
480 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
487 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
488 if (err != GPG_ERR_NO_ERROR)
489 return GC_INVALID_HASH;
491 err = gcry_md_setkey (mdh, key, keylen);
492 if (err != GPG_ERR_NO_ERROR)
495 return GC_INVALID_HASH;
498 gcry_md_write (mdh, in, inlen);
500 hash = gcry_md_read (mdh, GCRY_MD_MD5);
504 return GC_INVALID_HASH;
507 memcpy (resbuf, hash, hlen);
515 #ifdef GC_USE_HMAC_SHA1
517 gc_hmac_sha1 (const void *key, size_t keylen,
518 const void *in, size_t inlen, char *resbuf)
520 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
525 assert (hlen == GC_SHA1_DIGEST_SIZE);
527 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
528 if (err != GPG_ERR_NO_ERROR)
529 return GC_INVALID_HASH;
531 err = gcry_md_setkey (mdh, key, keylen);
532 if (err != GPG_ERR_NO_ERROR)
535 return GC_INVALID_HASH;
538 gcry_md_write (mdh, in, inlen);
540 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
544 return GC_INVALID_HASH;
547 memcpy (resbuf, hash, hlen);