1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Simon Josefsson
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, 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 Lesser 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_control (GCRYCTL_DISABLE_SECMEM
, NULL
, 0))
52 if (gcry_check_version (GCRYPT_VERSION
) == NULL
)
55 err
= gcry_control (GCRYCTL_INITIALIZATION_FINISHED
, NULL
, 0);
56 if (err
!= GPG_ERR_NO_ERROR
)
69 #ifdef GNULIB_GC_RANDOM
74 gc_nonce (char *data
, size_t datalen
)
76 gcry_create_nonce ((unsigned char *) data
, datalen
);
81 gc_pseudo_random (char *data
, size_t datalen
)
83 gcry_randomize ((unsigned char *) data
, datalen
, GCRY_STRONG_RANDOM
);
88 gc_random (char *data
, size_t datalen
)
90 gcry_randomize ((unsigned char *) data
, datalen
, GCRY_VERY_STRONG_RANDOM
);
96 /* Memory allocation. */
99 gc_set_allocators (gc_malloc_t func_malloc
,
100 gc_malloc_t secure_malloc
,
101 gc_secure_check_t secure_check
,
102 gc_realloc_t func_realloc
, gc_free_t func_free
)
104 gcry_set_allocation_handler (func_malloc
, secure_malloc
, secure_check
,
105 func_realloc
, func_free
);
111 gc_cipher_open (Gc_cipher alg
, Gc_cipher_mode mode
,
112 gc_cipher_handle
* outhandle
)
114 int gcryalg
, gcrymode
;
120 gcryalg
= GCRY_CIPHER_RIJNDAEL
;
124 gcryalg
= GCRY_CIPHER_RIJNDAEL
;
128 gcryalg
= GCRY_CIPHER_RIJNDAEL256
;
132 gcryalg
= GCRY_CIPHER_3DES
;
136 gcryalg
= GCRY_CIPHER_DES
;
141 gcryalg
= GCRY_CIPHER_ARCFOUR
;
145 gcryalg
= GCRY_CIPHER_RFC2268_40
;
148 #ifdef ENABLE_CAMELLIA
150 gcryalg
= GCRY_CIPHER_CAMELLIA128
;
154 gcryalg
= GCRY_CIPHER_CAMELLIA256
;
159 return GC_INVALID_CIPHER
;
165 gcrymode
= GCRY_CIPHER_MODE_ECB
;
169 gcrymode
= GCRY_CIPHER_MODE_CBC
;
173 gcrymode
= GCRY_CIPHER_MODE_STREAM
;
177 return GC_INVALID_CIPHER
;
180 err
= gcry_cipher_open ((gcry_cipher_hd_t
*) outhandle
,
181 gcryalg
, gcrymode
, 0);
182 if (gcry_err_code (err
))
183 return GC_INVALID_CIPHER
;
189 gc_cipher_setkey (gc_cipher_handle handle
, size_t keylen
, const char *key
)
193 err
= gcry_cipher_setkey ((gcry_cipher_hd_t
) handle
, key
, keylen
);
194 if (gcry_err_code (err
))
195 return GC_INVALID_CIPHER
;
201 gc_cipher_setiv (gc_cipher_handle handle
, size_t ivlen
, const char *iv
)
205 err
= gcry_cipher_setiv ((gcry_cipher_hd_t
) handle
, iv
, ivlen
);
206 if (gcry_err_code (err
))
207 return GC_INVALID_CIPHER
;
213 gc_cipher_encrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
215 if (gcry_cipher_encrypt ((gcry_cipher_hd_t
) handle
,
216 data
, len
, NULL
, len
) != 0)
217 return GC_INVALID_CIPHER
;
223 gc_cipher_decrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
225 if (gcry_cipher_decrypt ((gcry_cipher_hd_t
) handle
,
226 data
, len
, NULL
, len
) != 0)
227 return GC_INVALID_CIPHER
;
233 gc_cipher_close (gc_cipher_handle handle
)
235 gcry_cipher_close (handle
);
242 typedef struct _gc_hash_ctx
{
247 char hash
[GC_MD2_DIGEST_SIZE
];
248 struct md2_ctx md2Context
;
253 gc_hash_open (Gc_hash hash
, Gc_hash_mode mode
, gc_hash_handle
* outhandle
)
256 int gcryalg
= 0, gcrymode
= 0;
260 ctx
= calloc (sizeof (*ctx
), 1);
262 return GC_MALLOC_ERROR
;
270 gcryalg
= GCRY_MD_NONE
;
274 gcryalg
= GCRY_MD_MD4
;
278 gcryalg
= GCRY_MD_MD5
;
282 gcryalg
= GCRY_MD_SHA1
;
286 gcryalg
= GCRY_MD_SHA256
;
290 gcryalg
= GCRY_MD_SHA384
;
294 gcryalg
= GCRY_MD_SHA512
;
297 #ifdef ENABLE_CAMELLIA
299 gcryalg
= GCRY_MD_SHA224
;
304 gcryalg
= GCRY_MD_RMD160
;
308 rc
= GC_INVALID_HASH
;
318 gcrymode
= GCRY_MD_FLAG_HMAC
;
322 rc
= GC_INVALID_HASH
;
325 if (rc
== GC_OK
&& gcryalg
!= GCRY_MD_NONE
)
327 err
= gcry_md_open (&ctx
->gch
, gcryalg
, gcrymode
);
328 if (gcry_err_code (err
))
329 rc
= GC_INVALID_HASH
;
341 gc_hash_clone (gc_hash_handle handle
, gc_hash_handle
* outhandle
)
343 _gc_hash_ctx
*in
= handle
;
347 *outhandle
= out
= calloc (sizeof (*out
), 1);
349 return GC_MALLOC_ERROR
;
351 memcpy (out
, in
, sizeof (*out
));
353 err
= gcry_md_copy (&out
->gch
, in
->gch
);
357 return GC_INVALID_HASH
;
364 gc_hash_digest_length (Gc_hash hash
)
371 len
= GC_MD2_DIGEST_SIZE
;
375 len
= GC_MD4_DIGEST_SIZE
;
379 len
= GC_MD5_DIGEST_SIZE
;
383 len
= GC_RMD160_DIGEST_SIZE
;
387 len
= GC_SHA1_DIGEST_SIZE
;
391 len
= GC_SHA256_DIGEST_SIZE
;
395 len
= GC_SHA384_DIGEST_SIZE
;
399 len
= GC_SHA512_DIGEST_SIZE
;
402 #ifdef ENABLE_CAMELLIA
404 len
= GC_SHA224_DIGEST_SIZE
;
416 gc_hash_hmac_setkey (gc_hash_handle handle
, size_t len
, const char *key
)
418 _gc_hash_ctx
*ctx
= handle
;
420 if (ctx
->alg
!= GC_MD2
)
422 gcry_md_setkey (ctx
->gch
, key
, len
);
426 gc_hash_write (gc_hash_handle handle
, size_t len
, const char *data
)
428 _gc_hash_ctx
*ctx
= handle
;
431 if (ctx
->alg
== GC_MD2
)
432 md2_process_bytes (data
, len
, &ctx
->md2Context
);
435 gcry_md_write (ctx
->gch
, data
, len
);
439 gc_hash_read (gc_hash_handle handle
)
441 _gc_hash_ctx
*ctx
= handle
;
445 if (ctx
->alg
== GC_MD2
)
447 md2_finish_ctx (&ctx
->md2Context
, ctx
->hash
);
453 gcry_md_final (ctx
->gch
);
454 digest
= gcry_md_read (ctx
->gch
, 0);
461 gc_hash_close (gc_hash_handle handle
)
463 _gc_hash_ctx
*ctx
= handle
;
466 if (ctx
->alg
!= GC_MD2
)
468 gcry_md_close (ctx
->gch
);
474 gc_hash_buffer (Gc_hash hash
, const void *in
, size_t inlen
, char *resbuf
)
482 md2_buffer (in
, inlen
, resbuf
);
489 gcryalg
= GCRY_MD_MD4
;
495 gcryalg
= GCRY_MD_MD5
;
499 #ifdef GNULIB_GC_SHA1
501 gcryalg
= GCRY_MD_SHA1
;
505 #ifdef GNULIB_GC_SHA256
507 gcryalg
= GCRY_MD_SHA256
;
511 #ifdef GNULIB_GC_SHA384
513 gcryalg
= GCRY_MD_SHA384
;
517 #ifdef GNULIB_GC_SHA512
519 gcryalg
= GCRY_MD_SHA512
;
523 #ifdef GNULIB_GC_SHA224
525 gcryalg
= GCRY_MD_SHA224
;
529 #ifdef GNULIB_GC_RMD160
531 gcryalg
= GCRY_MD_RMD160
;
536 return GC_INVALID_HASH
;
539 gcry_md_hash_buffer (gcryalg
, resbuf
, in
, inlen
);
544 /* One-call interface. */
548 gc_md2 (const void *in
, size_t inlen
, void *resbuf
)
550 md2_buffer (in
, inlen
, resbuf
);
557 gc_md4 (const void *in
, size_t inlen
, void *resbuf
)
559 size_t outlen
= gcry_md_get_algo_dlen (GCRY_MD_MD4
);
564 assert (outlen
== GC_MD4_DIGEST_SIZE
);
566 err
= gcry_md_open (&hd
, GCRY_MD_MD4
, 0);
567 if (err
!= GPG_ERR_NO_ERROR
)
568 return GC_INVALID_HASH
;
570 gcry_md_write (hd
, in
, inlen
);
572 p
= gcry_md_read (hd
, GCRY_MD_MD4
);
576 return GC_INVALID_HASH
;
579 memcpy (resbuf
, p
, outlen
);
589 gc_md5 (const void *in
, size_t inlen
, void *resbuf
)
591 size_t outlen
= gcry_md_get_algo_dlen (GCRY_MD_MD5
);
596 assert (outlen
== GC_MD5_DIGEST_SIZE
);
598 err
= gcry_md_open (&hd
, GCRY_MD_MD5
, 0);
599 if (err
!= GPG_ERR_NO_ERROR
)
600 return GC_INVALID_HASH
;
602 gcry_md_write (hd
, in
, inlen
);
604 p
= gcry_md_read (hd
, GCRY_MD_MD5
);
608 return GC_INVALID_HASH
;
611 memcpy (resbuf
, p
, outlen
);
619 #ifdef GNULIB_GC_SHA1
621 gc_sha1 (const void *in
, size_t inlen
, void *resbuf
)
623 size_t outlen
= gcry_md_get_algo_dlen (GCRY_MD_SHA1
);
628 assert (outlen
== GC_SHA1_DIGEST_SIZE
);
630 err
= gcry_md_open (&hd
, GCRY_MD_SHA1
, 0);
631 if (err
!= GPG_ERR_NO_ERROR
)
632 return GC_INVALID_HASH
;
634 gcry_md_write (hd
, in
, inlen
);
636 p
= gcry_md_read (hd
, GCRY_MD_SHA1
);
640 return GC_INVALID_HASH
;
643 memcpy (resbuf
, p
, outlen
);
651 #ifdef GNULIB_GC_HMAC_MD5
653 gc_hmac_md5 (const void *key
, size_t keylen
,
654 const void *in
, size_t inlen
, char *resbuf
)
656 size_t hlen
= gcry_md_get_algo_dlen (GCRY_MD_MD5
);
663 err
= gcry_md_open (&mdh
, GCRY_MD_MD5
, GCRY_MD_FLAG_HMAC
);
664 if (err
!= GPG_ERR_NO_ERROR
)
665 return GC_INVALID_HASH
;
667 err
= gcry_md_setkey (mdh
, key
, keylen
);
668 if (err
!= GPG_ERR_NO_ERROR
)
671 return GC_INVALID_HASH
;
674 gcry_md_write (mdh
, in
, inlen
);
676 hash
= gcry_md_read (mdh
, GCRY_MD_MD5
);
680 return GC_INVALID_HASH
;
683 memcpy (resbuf
, hash
, hlen
);
691 #ifdef GNULIB_GC_HMAC_SHA1
693 gc_hmac_sha1 (const void *key
, size_t keylen
,
694 const void *in
, size_t inlen
, char *resbuf
)
696 size_t hlen
= gcry_md_get_algo_dlen (GCRY_MD_SHA1
);
701 assert (hlen
== GC_SHA1_DIGEST_SIZE
);
703 err
= gcry_md_open (&mdh
, GCRY_MD_SHA1
, GCRY_MD_FLAG_HMAC
);
704 if (err
!= GPG_ERR_NO_ERROR
)
705 return GC_INVALID_HASH
;
707 err
= gcry_md_setkey (mdh
, key
, keylen
);
708 if (err
!= GPG_ERR_NO_ERROR
)
711 return GC_INVALID_HASH
;
714 gcry_md_write (mdh
, in
, inlen
);
716 hash
= gcry_md_read (mdh
, GCRY_MD_SHA1
);
720 return GC_INVALID_HASH
;
723 memcpy (resbuf
, hash
, hlen
);