1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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. */
31 /* Get libgcrypt API. */
46 err
= gcry_control (GCRYCTL_ANY_INITIALIZATION_P
);
47 if (err
== GPG_ERR_NO_ERROR
)
49 if (gcry_check_version (GCRYPT_VERSION
) == NULL
)
52 err
= gcry_control (GCRYCTL_INITIALIZATION_FINISHED
, NULL
, 0);
53 if (err
!= GPG_ERR_NO_ERROR
)
66 #ifdef GNULIB_GC_RANDOM
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
);
93 /* Memory allocation. */
96 gc_set_allocators (gc_malloc_t func_malloc
,
97 gc_malloc_t secure_malloc
,
98 gc_secure_check_t secure_check
,
99 gc_realloc_t func_realloc
, gc_free_t func_free
)
101 gcry_set_allocation_handler (func_malloc
, secure_malloc
, secure_check
,
102 func_realloc
, func_free
);
108 gc_cipher_open (Gc_cipher alg
, Gc_cipher_mode mode
,
109 gc_cipher_handle
* outhandle
)
111 int gcryalg
, gcrymode
;
117 gcryalg
= GCRY_CIPHER_RIJNDAEL
;
121 gcryalg
= GCRY_CIPHER_RIJNDAEL
;
125 gcryalg
= GCRY_CIPHER_RIJNDAEL256
;
129 gcryalg
= GCRY_CIPHER_3DES
;
133 gcryalg
= GCRY_CIPHER_DES
;
138 gcryalg
= GCRY_CIPHER_ARCFOUR
;
142 gcryalg
= GCRY_CIPHER_RFC2268_40
;
146 return GC_INVALID_CIPHER
;
152 gcrymode
= GCRY_CIPHER_MODE_ECB
;
156 gcrymode
= GCRY_CIPHER_MODE_CBC
;
160 gcrymode
= GCRY_CIPHER_MODE_STREAM
;
164 return GC_INVALID_CIPHER
;
167 err
= gcry_cipher_open ((gcry_cipher_hd_t
*) outhandle
,
168 gcryalg
, gcrymode
, 0);
169 if (gcry_err_code (err
))
170 return GC_INVALID_CIPHER
;
176 gc_cipher_setkey (gc_cipher_handle handle
, size_t keylen
, const char *key
)
180 err
= gcry_cipher_setkey ((gcry_cipher_hd_t
) handle
, key
, keylen
);
181 if (gcry_err_code (err
))
182 return GC_INVALID_CIPHER
;
188 gc_cipher_setiv (gc_cipher_handle handle
, size_t ivlen
, const char *iv
)
192 err
= gcry_cipher_setiv ((gcry_cipher_hd_t
) handle
, iv
, ivlen
);
193 if (gcry_err_code (err
))
194 return GC_INVALID_CIPHER
;
200 gc_cipher_encrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
202 if (gcry_cipher_encrypt ((gcry_cipher_hd_t
) handle
,
203 data
, len
, NULL
, len
) != 0)
204 return GC_INVALID_CIPHER
;
210 gc_cipher_decrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
212 if (gcry_cipher_decrypt ((gcry_cipher_hd_t
) handle
,
213 data
, len
, NULL
, len
) != 0)
214 return GC_INVALID_CIPHER
;
220 gc_cipher_close (gc_cipher_handle handle
)
222 gcry_cipher_close (handle
);
229 typedef struct _gc_hash_ctx
{
234 char hash
[GC_MD2_DIGEST_SIZE
];
235 struct md2_ctx md2Context
;
240 gc_hash_open (Gc_hash hash
, Gc_hash_mode mode
, gc_hash_handle
* outhandle
)
243 int gcryalg
= 0, gcrymode
= 0;
247 ctx
= calloc (sizeof (*ctx
), 1);
249 return GC_MALLOC_ERROR
;
257 gcryalg
= GCRY_MD_NONE
;
261 gcryalg
= GCRY_MD_MD4
;
265 gcryalg
= GCRY_MD_MD5
;
269 gcryalg
= GCRY_MD_SHA1
;
273 gcryalg
= GCRY_MD_SHA256
;
277 gcryalg
= GCRY_MD_SHA384
;
281 gcryalg
= GCRY_MD_SHA512
;
285 gcryalg
= GCRY_MD_RMD160
;
289 rc
= GC_INVALID_HASH
;
299 gcrymode
= GCRY_MD_FLAG_HMAC
;
303 rc
= GC_INVALID_HASH
;
306 if (rc
== GC_OK
&& gcryalg
!= GCRY_MD_NONE
)
308 err
= gcry_md_open (&ctx
->gch
, gcryalg
, gcrymode
);
309 if (gcry_err_code (err
))
310 rc
= GC_INVALID_HASH
;
322 gc_hash_clone (gc_hash_handle handle
, gc_hash_handle
* outhandle
)
324 _gc_hash_ctx
*in
= handle
;
328 *outhandle
= out
= calloc (sizeof (*out
), 1);
330 return GC_MALLOC_ERROR
;
332 memcpy (out
, in
, sizeof (*out
));
334 err
= gcry_md_copy (&out
->gch
, in
->gch
);
338 return GC_INVALID_HASH
;
345 gc_hash_digest_length (Gc_hash hash
)
352 len
= GC_MD2_DIGEST_SIZE
;
356 len
= GC_MD4_DIGEST_SIZE
;
360 len
= GC_MD5_DIGEST_SIZE
;
364 len
= GC_RMD160_DIGEST_SIZE
;
368 len
= GC_SHA1_DIGEST_SIZE
;
372 len
= GC_SHA256_DIGEST_SIZE
;
376 len
= GC_SHA384_DIGEST_SIZE
;
380 len
= GC_SHA512_DIGEST_SIZE
;
391 gc_hash_hmac_setkey (gc_hash_handle handle
, size_t len
, const char *key
)
393 _gc_hash_ctx
*ctx
= handle
;
395 if (ctx
->alg
!= GC_MD2
)
397 gcry_md_setkey (ctx
->gch
, key
, len
);
401 gc_hash_write (gc_hash_handle handle
, size_t len
, const char *data
)
403 _gc_hash_ctx
*ctx
= handle
;
406 if (ctx
->alg
== GC_MD2
)
407 md2_process_bytes (data
, len
, &ctx
->md2Context
);
410 gcry_md_write (ctx
->gch
, data
, len
);
414 gc_hash_read (gc_hash_handle handle
)
416 _gc_hash_ctx
*ctx
= handle
;
420 if (ctx
->alg
== GC_MD2
)
422 md2_finish_ctx (&ctx
->md2Context
, ctx
->hash
);
428 gcry_md_final (ctx
->gch
);
429 digest
= gcry_md_read (ctx
->gch
, 0);
436 gc_hash_close (gc_hash_handle handle
)
438 _gc_hash_ctx
*ctx
= handle
;
441 if (ctx
->alg
!= GC_MD2
)
443 gcry_md_close (ctx
->gch
);
449 gc_hash_buffer (Gc_hash hash
, const void *in
, size_t inlen
, char *resbuf
)
457 md2_buffer (in
, inlen
, resbuf
);
464 gcryalg
= GCRY_MD_MD4
;
470 gcryalg
= GCRY_MD_MD5
;
474 #ifdef GNULIB_GC_SHA1
476 gcryalg
= GCRY_MD_SHA1
;
480 #ifdef GNULIB_GC_SHA256
482 gcryalg
= GCRY_MD_SHA256
;
486 #ifdef GNULIB_GC_SHA384
488 gcryalg
= GCRY_MD_SHA384
;
492 #ifdef GNULIB_GC_SHA512
494 gcryalg
= GCRY_MD_SHA512
;
498 #ifdef GNULIB_GC_RMD160
500 gcryalg
= GCRY_MD_RMD160
;
505 return GC_INVALID_HASH
;
508 gcry_md_hash_buffer (gcryalg
, resbuf
, in
, inlen
);
513 /* One-call interface. */
517 gc_md2 (const void *in
, size_t inlen
, void *resbuf
)
519 md2_buffer (in
, inlen
, resbuf
);
526 gc_md4 (const void *in
, size_t inlen
, void *resbuf
)
528 size_t outlen
= gcry_md_get_algo_dlen (GCRY_MD_MD4
);
533 assert (outlen
== GC_MD4_DIGEST_SIZE
);
535 err
= gcry_md_open (&hd
, GCRY_MD_MD4
, 0);
536 if (err
!= GPG_ERR_NO_ERROR
)
537 return GC_INVALID_HASH
;
539 gcry_md_write (hd
, in
, inlen
);
541 p
= gcry_md_read (hd
, GCRY_MD_MD4
);
545 return GC_INVALID_HASH
;
548 memcpy (resbuf
, p
, outlen
);
558 gc_md5 (const void *in
, size_t inlen
, void *resbuf
)
560 size_t outlen
= gcry_md_get_algo_dlen (GCRY_MD_MD5
);
565 assert (outlen
== GC_MD5_DIGEST_SIZE
);
567 err
= gcry_md_open (&hd
, GCRY_MD_MD5
, 0);
568 if (err
!= GPG_ERR_NO_ERROR
)
569 return GC_INVALID_HASH
;
571 gcry_md_write (hd
, in
, inlen
);
573 p
= gcry_md_read (hd
, GCRY_MD_MD5
);
577 return GC_INVALID_HASH
;
580 memcpy (resbuf
, p
, outlen
);
588 #ifdef GNULIB_GC_SHA1
590 gc_sha1 (const void *in
, size_t inlen
, void *resbuf
)
592 size_t outlen
= gcry_md_get_algo_dlen (GCRY_MD_SHA1
);
597 assert (outlen
== GC_SHA1_DIGEST_SIZE
);
599 err
= gcry_md_open (&hd
, GCRY_MD_SHA1
, 0);
600 if (err
!= GPG_ERR_NO_ERROR
)
601 return GC_INVALID_HASH
;
603 gcry_md_write (hd
, in
, inlen
);
605 p
= gcry_md_read (hd
, GCRY_MD_SHA1
);
609 return GC_INVALID_HASH
;
612 memcpy (resbuf
, p
, outlen
);
620 #ifdef GNULIB_GC_HMAC_MD5
622 gc_hmac_md5 (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_MD5
);
632 err
= gcry_md_open (&mdh
, GCRY_MD_MD5
, 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_MD5
);
649 return GC_INVALID_HASH
;
652 memcpy (resbuf
, hash
, hlen
);
660 #ifdef GNULIB_GC_HMAC_SHA1
662 gc_hmac_sha1 (const void *key
, size_t keylen
,
663 const void *in
, size_t inlen
, char *resbuf
)
665 size_t hlen
= gcry_md_get_algo_dlen (GCRY_MD_SHA1
);
670 assert (hlen
== GC_SHA1_DIGEST_SIZE
);
672 err
= gcry_md_open (&mdh
, GCRY_MD_SHA1
, GCRY_MD_FLAG_HMAC
);
673 if (err
!= GPG_ERR_NO_ERROR
)
674 return GC_INVALID_HASH
;
676 err
= gcry_md_setkey (mdh
, key
, keylen
);
677 if (err
!= GPG_ERR_NO_ERROR
)
680 return GC_INVALID_HASH
;
683 gcry_md_write (mdh
, in
, inlen
);
685 hash
= gcry_md_read (mdh
, GCRY_MD_SHA1
);
689 return GC_INVALID_HASH
;
692 memcpy (resbuf
, hash
, hlen
);