Update.
[shishi.git] / gl / gc-libgcrypt.c
blobea0de060ac3ddbf25308d78f1f3e96e8f913f48c
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
17 * 02110-1301, USA.
21 /* Note: This file is only built if GC uses Libgcrypt. */
23 #include <config.h>
25 /* Get prototype. */
26 #include "gc.h"
28 #include <stdlib.h>
29 #include <string.h>
31 /* Get libgcrypt API. */
32 #include <gcrypt.h>
33 #ifdef GNULIB_GC_MD2
34 # include "md2.h"
35 #endif
37 #include <assert.h>
39 /* Initialization. */
41 Gc_rc
42 gc_init (void)
44 gcry_error_t err;
46 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
47 if (err == GPG_ERR_NO_ERROR)
49 if (gcry_check_version (GCRYPT_VERSION) == NULL)
50 return GC_INIT_ERROR;
52 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
53 if (err != GPG_ERR_NO_ERROR)
54 return GC_INIT_ERROR;
57 return GC_OK;
60 void
61 gc_done (void)
63 return;
66 #ifdef GNULIB_GC_RANDOM
68 /* Randomness. */
70 Gc_rc
71 gc_nonce (char *data, size_t datalen)
73 gcry_create_nonce ((unsigned char *) data, datalen);
74 return GC_OK;
77 Gc_rc
78 gc_pseudo_random (char *data, size_t datalen)
80 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
81 return GC_OK;
84 Gc_rc
85 gc_random (char *data, size_t datalen)
87 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
88 return GC_OK;
91 #endif
93 /* Memory allocation. */
95 void
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);
105 /* Ciphers. */
107 Gc_rc
108 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
109 gc_cipher_handle * outhandle)
111 int gcryalg, gcrymode;
112 gcry_error_t err;
114 switch (alg)
116 case GC_AES128:
117 gcryalg = GCRY_CIPHER_RIJNDAEL;
118 break;
120 case GC_AES192:
121 gcryalg = GCRY_CIPHER_RIJNDAEL;
122 break;
124 case GC_AES256:
125 gcryalg = GCRY_CIPHER_RIJNDAEL256;
126 break;
128 case GC_3DES:
129 gcryalg = GCRY_CIPHER_3DES;
130 break;
132 case GC_DES:
133 gcryalg = GCRY_CIPHER_DES;
134 break;
136 case GC_ARCFOUR128:
137 case GC_ARCFOUR40:
138 gcryalg = GCRY_CIPHER_ARCFOUR;
139 break;
141 case GC_ARCTWO40:
142 gcryalg = GCRY_CIPHER_RFC2268_40;
143 break;
145 default:
146 return GC_INVALID_CIPHER;
149 switch (mode)
151 case GC_ECB:
152 gcrymode = GCRY_CIPHER_MODE_ECB;
153 break;
155 case GC_CBC:
156 gcrymode = GCRY_CIPHER_MODE_CBC;
157 break;
159 case GC_STREAM:
160 gcrymode = GCRY_CIPHER_MODE_STREAM;
161 break;
163 default:
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;
172 return GC_OK;
175 Gc_rc
176 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
178 gcry_error_t err;
180 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
181 if (gcry_err_code (err))
182 return GC_INVALID_CIPHER;
184 return GC_OK;
187 Gc_rc
188 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
190 gcry_error_t err;
192 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
193 if (gcry_err_code (err))
194 return GC_INVALID_CIPHER;
196 return GC_OK;
199 Gc_rc
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;
206 return GC_OK;
209 Gc_rc
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;
216 return GC_OK;
219 Gc_rc
220 gc_cipher_close (gc_cipher_handle handle)
222 gcry_cipher_close (handle);
224 return GC_OK;
227 /* Hashes. */
229 typedef struct _gc_hash_ctx {
230 Gc_hash alg;
231 Gc_hash_mode mode;
232 gcry_md_hd_t gch;
233 #ifdef GNULIB_GC_MD2
234 char hash[GC_MD2_DIGEST_SIZE];
235 struct md2_ctx md2Context;
236 #endif
237 } _gc_hash_ctx;
239 Gc_rc
240 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
242 _gc_hash_ctx *ctx;
243 int gcryalg = 0, gcrymode = 0;
244 gcry_error_t err;
245 Gc_rc rc = GC_OK;
247 ctx = calloc (sizeof (*ctx), 1);
248 if (!ctx)
249 return GC_MALLOC_ERROR;
251 ctx->alg = hash;
252 ctx->mode = mode;
254 switch (hash)
256 case GC_MD2:
257 gcryalg = GCRY_MD_NONE;
258 break;
260 case GC_MD4:
261 gcryalg = GCRY_MD_MD4;
262 break;
264 case GC_MD5:
265 gcryalg = GCRY_MD_MD5;
266 break;
268 case GC_SHA1:
269 gcryalg = GCRY_MD_SHA1;
270 break;
272 case GC_SHA256:
273 gcryalg = GCRY_MD_SHA256;
274 break;
276 case GC_SHA384:
277 gcryalg = GCRY_MD_SHA384;
278 break;
280 case GC_SHA512:
281 gcryalg = GCRY_MD_SHA512;
282 break;
284 case GC_RMD160:
285 gcryalg = GCRY_MD_RMD160;
286 break;
288 default:
289 rc = GC_INVALID_HASH;
292 switch (mode)
294 case 0:
295 gcrymode = 0;
296 break;
298 case GC_HMAC:
299 gcrymode = GCRY_MD_FLAG_HMAC;
300 break;
302 default:
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;
313 if (rc == GC_OK)
314 *outhandle = ctx;
315 else
316 free (ctx);
318 return rc;
321 Gc_rc
322 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
324 _gc_hash_ctx *in = handle;
325 _gc_hash_ctx *out;
326 int err;
328 *outhandle = out = calloc (sizeof (*out), 1);
329 if (!out)
330 return GC_MALLOC_ERROR;
332 memcpy (out, in, sizeof (*out));
334 err = gcry_md_copy (&out->gch, in->gch);
335 if (err)
337 free (out);
338 return GC_INVALID_HASH;
341 return GC_OK;
344 size_t
345 gc_hash_digest_length (Gc_hash hash)
347 size_t len;
349 switch (hash)
351 case GC_MD2:
352 len = GC_MD2_DIGEST_SIZE;
353 break;
355 case GC_MD4:
356 len = GC_MD4_DIGEST_SIZE;
357 break;
359 case GC_MD5:
360 len = GC_MD5_DIGEST_SIZE;
361 break;
363 case GC_RMD160:
364 len = GC_RMD160_DIGEST_SIZE;
365 break;
367 case GC_SHA1:
368 len = GC_SHA1_DIGEST_SIZE;
369 break;
371 case GC_SHA256:
372 len = GC_SHA256_DIGEST_SIZE;
373 break;
375 case GC_SHA384:
376 len = GC_SHA384_DIGEST_SIZE;
377 break;
379 case GC_SHA512:
380 len = GC_SHA512_DIGEST_SIZE;
381 break;
383 default:
384 return 0;
387 return len;
390 void
391 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
393 _gc_hash_ctx *ctx = handle;
394 #ifdef GNULIB_GC_MD2
395 if (ctx->alg != GC_MD2)
396 #endif
397 gcry_md_setkey (ctx->gch, key, len);
400 void
401 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
403 _gc_hash_ctx *ctx = handle;
405 #ifdef GNULIB_GC_MD2
406 if (ctx->alg == GC_MD2)
407 md2_process_bytes (data, len, &ctx->md2Context);
408 else
409 #endif
410 gcry_md_write (ctx->gch, data, len);
413 const char *
414 gc_hash_read (gc_hash_handle handle)
416 _gc_hash_ctx *ctx = handle;
417 const char *digest;
419 #ifdef GNULIB_GC_MD2
420 if (ctx->alg == GC_MD2)
422 md2_finish_ctx (&ctx->md2Context, ctx->hash);
423 digest = ctx->hash;
425 else
426 #endif
428 gcry_md_final (ctx->gch);
429 digest = gcry_md_read (ctx->gch, 0);
432 return digest;
435 void
436 gc_hash_close (gc_hash_handle handle)
438 _gc_hash_ctx *ctx = handle;
440 #ifdef GNULIB_GC_MD2
441 if (ctx->alg != GC_MD2)
442 #endif
443 gcry_md_close (ctx->gch);
445 free (ctx);
448 Gc_rc
449 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
451 int gcryalg;
453 switch (hash)
455 #ifdef GNULIB_GC_MD2
456 case GC_MD2:
457 md2_buffer (in, inlen, resbuf);
458 return GC_OK;
459 break;
460 #endif
462 #ifdef GNULIB_GC_MD4
463 case GC_MD4:
464 gcryalg = GCRY_MD_MD4;
465 break;
466 #endif
468 #ifdef GNULIB_GC_MD5
469 case GC_MD5:
470 gcryalg = GCRY_MD_MD5;
471 break;
472 #endif
474 #ifdef GNULIB_GC_SHA1
475 case GC_SHA1:
476 gcryalg = GCRY_MD_SHA1;
477 break;
478 #endif
480 #ifdef GNULIB_GC_SHA256
481 case GC_SHA256:
482 gcryalg = GCRY_MD_SHA256;
483 break;
484 #endif
486 #ifdef GNULIB_GC_SHA384
487 case GC_SHA384:
488 gcryalg = GCRY_MD_SHA384;
489 break;
490 #endif
492 #ifdef GNULIB_GC_SHA512
493 case GC_SHA512:
494 gcryalg = GCRY_MD_SHA512;
495 break;
496 #endif
498 #ifdef GNULIB_GC_RMD160
499 case GC_RMD160:
500 gcryalg = GCRY_MD_RMD160;
501 break;
502 #endif
504 default:
505 return GC_INVALID_HASH;
508 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
510 return GC_OK;
513 /* One-call interface. */
515 #ifdef GNULIB_GC_MD2
516 Gc_rc
517 gc_md2 (const void *in, size_t inlen, void *resbuf)
519 md2_buffer (in, inlen, resbuf);
520 return GC_OK;
522 #endif
524 #ifdef GNULIB_GC_MD4
525 Gc_rc
526 gc_md4 (const void *in, size_t inlen, void *resbuf)
528 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
529 gcry_md_hd_t hd;
530 gpg_error_t err;
531 unsigned char *p;
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);
542 if (p == NULL)
544 gcry_md_close (hd);
545 return GC_INVALID_HASH;
548 memcpy (resbuf, p, outlen);
550 gcry_md_close (hd);
552 return GC_OK;
554 #endif
556 #ifdef GNULIB_GC_MD5
557 Gc_rc
558 gc_md5 (const void *in, size_t inlen, void *resbuf)
560 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
561 gcry_md_hd_t hd;
562 gpg_error_t err;
563 unsigned char *p;
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);
574 if (p == NULL)
576 gcry_md_close (hd);
577 return GC_INVALID_HASH;
580 memcpy (resbuf, p, outlen);
582 gcry_md_close (hd);
584 return GC_OK;
586 #endif
588 #ifdef GNULIB_GC_SHA1
589 Gc_rc
590 gc_sha1 (const void *in, size_t inlen, void *resbuf)
592 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
593 gcry_md_hd_t hd;
594 gpg_error_t err;
595 unsigned char *p;
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);
606 if (p == NULL)
608 gcry_md_close (hd);
609 return GC_INVALID_HASH;
612 memcpy (resbuf, p, outlen);
614 gcry_md_close (hd);
616 return GC_OK;
618 #endif
620 #ifdef GNULIB_GC_HMAC_MD5
621 Gc_rc
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);
626 gcry_md_hd_t mdh;
627 unsigned char *hash;
628 gpg_error_t err;
630 assert (hlen == 16);
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)
639 gcry_md_close (mdh);
640 return GC_INVALID_HASH;
643 gcry_md_write (mdh, in, inlen);
645 hash = gcry_md_read (mdh, GCRY_MD_MD5);
646 if (hash == NULL)
648 gcry_md_close (mdh);
649 return GC_INVALID_HASH;
652 memcpy (resbuf, hash, hlen);
654 gcry_md_close (mdh);
656 return GC_OK;
658 #endif
660 #ifdef GNULIB_GC_HMAC_SHA1
661 Gc_rc
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);
666 gcry_md_hd_t mdh;
667 unsigned char *hash;
668 gpg_error_t err;
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)
679 gcry_md_close (mdh);
680 return GC_INVALID_HASH;
683 gcry_md_write (mdh, in, inlen);
685 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
686 if (hash == NULL)
688 gcry_md_close (mdh);
689 return GC_INVALID_HASH;
692 memcpy (resbuf, hash, hlen);
694 gcry_md_close (mdh);
696 return GC_OK;
698 #endif