Update.
[shishi.git] / gl / gc-gnulib.c
blobf414f433ef5c4ea72386fa692486bb315aa83408
1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
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 internal functions. */
23 #include <config.h>
25 /* Get prototype. */
26 #include "gc.h"
28 #include <stdlib.h>
29 #include <string.h>
31 /* For randomize. */
32 #ifdef GNULIB_GC_RANDOM
33 # include <unistd.h>
34 # include <sys/types.h>
35 # include <sys/stat.h>
36 # include <fcntl.h>
37 # include <errno.h>
38 #endif
40 /* Hashes. */
41 #ifdef GNULIB_GC_MD2
42 # include "md2.h"
43 #endif
44 #ifdef GNULIB_GC_MD4
45 # include "md4.h"
46 #endif
47 #ifdef GNULIB_GC_MD5
48 # include "md5.h"
49 #endif
50 #ifdef GNULIB_GC_SHA1
51 # include "sha1.h"
52 #endif
53 #if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1)
54 # include "hmac.h"
55 #endif
57 /* Ciphers. */
58 #ifdef GNULIB_GC_ARCFOUR
59 # include "arcfour.h"
60 #endif
61 #ifdef GNULIB_GC_ARCTWO
62 # include "arctwo.h"
63 #endif
64 #ifdef GNULIB_GC_DES
65 # include "des.h"
66 #endif
67 #ifdef GNULIB_GC_RIJNDAEL
68 # include "rijndael-api-fst.h"
69 #endif
71 /* The results of open() in this file are not used with fchdir,
72 therefore save some unnecessary work in fchdir.c. */
73 #undef open
74 #undef close
76 Gc_rc
77 gc_init (void)
79 return GC_OK;
82 void
83 gc_done (void)
85 return;
88 #ifdef GNULIB_GC_RANDOM
90 /* Randomness. */
92 static Gc_rc
93 randomize (int level, char *data, size_t datalen)
95 int fd;
96 const char *device;
97 size_t len = 0;
98 int rc;
100 switch (level)
102 case 0:
103 device = NAME_OF_NONCE_DEVICE;
104 break;
106 case 1:
107 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
108 break;
110 default:
111 device = NAME_OF_RANDOM_DEVICE;
112 break;
115 if (strcmp (device, "no") == 0)
116 return GC_RANDOM_ERROR;
118 fd = open (device, O_RDONLY);
119 if (fd < 0)
120 return GC_RANDOM_ERROR;
124 ssize_t tmp;
126 tmp = read (fd, data, datalen);
128 if (tmp < 0)
130 int save_errno = errno;
131 close (fd);
132 errno = save_errno;
133 return GC_RANDOM_ERROR;
136 len += tmp;
138 while (len < datalen);
140 rc = close (fd);
141 if (rc < 0)
142 return GC_RANDOM_ERROR;
144 return GC_OK;
147 Gc_rc
148 gc_nonce (char *data, size_t datalen)
150 return randomize (0, data, datalen);
153 Gc_rc
154 gc_pseudo_random (char *data, size_t datalen)
156 return randomize (1, data, datalen);
159 Gc_rc
160 gc_random (char *data, size_t datalen)
162 return randomize (2, data, datalen);
165 #endif
167 /* Memory allocation. */
169 void
170 gc_set_allocators (gc_malloc_t func_malloc,
171 gc_malloc_t secure_malloc,
172 gc_secure_check_t secure_check,
173 gc_realloc_t func_realloc, gc_free_t func_free)
175 return;
177 /* Ciphers. */
179 typedef struct _gc_cipher_ctx {
180 Gc_cipher alg;
181 Gc_cipher_mode mode;
182 #ifdef GNULIB_GC_ARCTWO
183 arctwo_context arctwoContext;
184 char arctwoIV[ARCTWO_BLOCK_SIZE];
185 #endif
186 #ifdef GNULIB_GC_ARCFOUR
187 arcfour_context arcfourContext;
188 #endif
189 #ifdef GNULIB_GC_DES
190 des_ctx desContext;
191 #endif
192 #ifdef GNULIB_GC_RIJNDAEL
193 rijndaelKeyInstance aesEncKey;
194 rijndaelKeyInstance aesDecKey;
195 rijndaelCipherInstance aesContext;
196 #endif
197 } _gc_cipher_ctx;
199 Gc_rc
200 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
201 gc_cipher_handle * outhandle)
203 _gc_cipher_ctx *ctx;
204 Gc_rc rc = GC_OK;
206 ctx = calloc (sizeof (*ctx), 1);
207 if (!ctx)
208 return GC_MALLOC_ERROR;
210 ctx->alg = alg;
211 ctx->mode = mode;
213 switch (alg)
215 #ifdef GNULIB_GC_ARCTWO
216 case GC_ARCTWO40:
217 switch (mode)
219 case GC_ECB:
220 case GC_CBC:
221 break;
223 default:
224 rc = GC_INVALID_CIPHER;
226 break;
227 #endif
229 #ifdef GNULIB_GC_ARCFOUR
230 case GC_ARCFOUR128:
231 case GC_ARCFOUR40:
232 switch (mode)
234 case GC_STREAM:
235 break;
237 default:
238 rc = GC_INVALID_CIPHER;
240 break;
241 #endif
243 #ifdef GNULIB_GC_DES
244 case GC_DES:
245 switch (mode)
247 case GC_ECB:
248 break;
250 default:
251 rc = GC_INVALID_CIPHER;
253 break;
254 #endif
256 #ifdef GNULIB_GC_RIJNDAEL
257 case GC_AES128:
258 case GC_AES192:
259 case GC_AES256:
260 switch (mode)
262 case GC_ECB:
263 case GC_CBC:
264 break;
266 default:
267 rc = GC_INVALID_CIPHER;
269 break;
270 #endif
272 default:
273 rc = GC_INVALID_CIPHER;
276 if (rc == GC_OK)
277 *outhandle = ctx;
278 else
279 free (ctx);
281 return rc;
284 Gc_rc
285 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
287 _gc_cipher_ctx *ctx = handle;
289 switch (ctx->alg)
291 #ifdef GNULIB_GC_ARCTWO
292 case GC_ARCTWO40:
293 arctwo_setkey (&ctx->arctwoContext, keylen, key);
294 break;
295 #endif
297 #ifdef GNULIB_GC_ARCFOUR
298 case GC_ARCFOUR128:
299 case GC_ARCFOUR40:
300 arcfour_setkey (&ctx->arcfourContext, key, keylen);
301 break;
302 #endif
304 #ifdef GNULIB_GC_DES
305 case GC_DES:
306 if (keylen != 8)
307 return GC_INVALID_CIPHER;
308 des_setkey (&ctx->desContext, key);
309 break;
310 #endif
312 #ifdef GNULIB_GC_RIJNDAEL
313 case GC_AES128:
314 case GC_AES192:
315 case GC_AES256:
317 rijndael_rc rc;
318 size_t i;
319 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
321 for (i = 0; i < keylen; i++)
322 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
324 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
325 keylen * 8, keyMaterial);
326 if (rc < 0)
327 return GC_INVALID_CIPHER;
329 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
330 keylen * 8, keyMaterial);
331 if (rc < 0)
332 return GC_INVALID_CIPHER;
334 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
335 if (rc < 0)
336 return GC_INVALID_CIPHER;
338 break;
339 #endif
341 default:
342 return GC_INVALID_CIPHER;
345 return GC_OK;
348 Gc_rc
349 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
351 _gc_cipher_ctx *ctx = handle;
353 switch (ctx->alg)
355 #ifdef GNULIB_GC_ARCTWO
356 case GC_ARCTWO40:
357 if (ivlen != ARCTWO_BLOCK_SIZE)
358 return GC_INVALID_CIPHER;
359 memcpy (ctx->arctwoIV, iv, ivlen);
360 break;
361 #endif
363 #ifdef GNULIB_GC_RIJNDAEL
364 case GC_AES128:
365 case GC_AES192:
366 case GC_AES256:
367 switch (ctx->mode)
369 case GC_ECB:
370 /* Doesn't use IV. */
371 break;
373 case GC_CBC:
375 rijndael_rc rc;
376 size_t i;
377 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
379 for (i = 0; i < ivlen; i++)
380 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
382 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
383 ivMaterial);
384 if (rc < 0)
385 return GC_INVALID_CIPHER;
387 break;
389 default:
390 return GC_INVALID_CIPHER;
392 break;
393 #endif
395 default:
396 return GC_INVALID_CIPHER;
399 return GC_OK;
402 Gc_rc
403 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
405 _gc_cipher_ctx *ctx = handle;
407 switch (ctx->alg)
409 #ifdef GNULIB_GC_ARCTWO
410 case GC_ARCTWO40:
411 switch (ctx->mode)
413 case GC_ECB:
414 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
415 break;
417 case GC_CBC:
418 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
419 data += ARCTWO_BLOCK_SIZE)
421 size_t i;
422 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
423 data[i] ^= ctx->arctwoIV[i];
424 arctwo_encrypt (&ctx->arctwoContext, data, data,
425 ARCTWO_BLOCK_SIZE);
426 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
428 break;
430 default:
431 return GC_INVALID_CIPHER;
433 break;
434 #endif
436 #ifdef GNULIB_GC_ARCFOUR
437 case GC_ARCFOUR128:
438 case GC_ARCFOUR40:
439 arcfour_stream (&ctx->arcfourContext, data, data, len);
440 break;
441 #endif
443 #ifdef GNULIB_GC_DES
444 case GC_DES:
445 for (; len >= 8; len -= 8, data += 8)
446 des_ecb_encrypt (&ctx->desContext, data, data);
447 break;
448 #endif
450 #ifdef GNULIB_GC_RIJNDAEL
451 case GC_AES128:
452 case GC_AES192:
453 case GC_AES256:
455 int nblocks;
457 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
458 data, 8 * len, data);
459 if (nblocks < 0)
460 return GC_INVALID_CIPHER;
462 break;
463 #endif
465 default:
466 return GC_INVALID_CIPHER;
469 return GC_OK;
472 Gc_rc
473 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
475 _gc_cipher_ctx *ctx = handle;
477 switch (ctx->alg)
479 #ifdef GNULIB_GC_ARCTWO
480 case GC_ARCTWO40:
481 switch (ctx->mode)
483 case GC_ECB:
484 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
485 break;
487 case GC_CBC:
488 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
489 data += ARCTWO_BLOCK_SIZE)
491 char tmpIV[ARCTWO_BLOCK_SIZE];
492 size_t i;
493 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
494 arctwo_decrypt (&ctx->arctwoContext, data, data,
495 ARCTWO_BLOCK_SIZE);
496 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
497 data[i] ^= ctx->arctwoIV[i];
498 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
500 break;
502 default:
503 return GC_INVALID_CIPHER;
505 break;
506 #endif
508 #ifdef GNULIB_GC_ARCFOUR
509 case GC_ARCFOUR128:
510 case GC_ARCFOUR40:
511 arcfour_stream (&ctx->arcfourContext, data, data, len);
512 break;
513 #endif
515 #ifdef GNULIB_GC_DES
516 case GC_DES:
517 for (; len >= 8; len -= 8, data += 8)
518 des_ecb_decrypt (&ctx->desContext, data, data);
519 break;
520 #endif
522 #ifdef GNULIB_GC_RIJNDAEL
523 case GC_AES128:
524 case GC_AES192:
525 case GC_AES256:
527 int nblocks;
529 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
530 data, 8 * len, data);
531 if (nblocks < 0)
532 return GC_INVALID_CIPHER;
534 break;
535 #endif
537 default:
538 return GC_INVALID_CIPHER;
541 return GC_OK;
544 Gc_rc
545 gc_cipher_close (gc_cipher_handle handle)
547 _gc_cipher_ctx *ctx = handle;
549 if (ctx)
550 free (ctx);
552 return GC_OK;
555 /* Hashes. */
557 #define MAX_DIGEST_SIZE 20
559 typedef struct _gc_hash_ctx {
560 Gc_hash alg;
561 Gc_hash_mode mode;
562 char hash[MAX_DIGEST_SIZE];
563 #ifdef GNULIB_GC_MD2
564 struct md2_ctx md2Context;
565 #endif
566 #ifdef GNULIB_GC_MD4
567 struct md4_ctx md4Context;
568 #endif
569 #ifdef GNULIB_GC_MD5
570 struct md5_ctx md5Context;
571 #endif
572 #ifdef GNULIB_GC_SHA1
573 struct sha1_ctx sha1Context;
574 #endif
575 } _gc_hash_ctx;
577 Gc_rc
578 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
580 _gc_hash_ctx *ctx;
581 Gc_rc rc = GC_OK;
583 ctx = calloc (sizeof (*ctx), 1);
585 ctx->alg = hash;
586 ctx->mode = mode;
588 switch (hash)
590 #ifdef GNULIB_GC_MD2
591 case GC_MD2:
592 md2_init_ctx (&ctx->md2Context);
593 break;
594 #endif
596 #ifdef GNULIB_GC_MD4
597 case GC_MD4:
598 md4_init_ctx (&ctx->md4Context);
599 break;
600 #endif
602 #ifdef GNULIB_GC_MD5
603 case GC_MD5:
604 md5_init_ctx (&ctx->md5Context);
605 break;
606 #endif
608 #ifdef GNULIB_GC_SHA1
609 case GC_SHA1:
610 sha1_init_ctx (&ctx->sha1Context);
611 break;
612 #endif
614 default:
615 rc = GC_INVALID_HASH;
616 break;
619 switch (mode)
621 case 0:
622 break;
624 default:
625 rc = GC_INVALID_HASH;
626 break;
629 if (rc == GC_OK)
630 *outhandle = ctx;
631 else
632 free (ctx);
634 return rc;
637 Gc_rc
638 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
640 _gc_hash_ctx *in = handle;
641 _gc_hash_ctx *out;
643 *outhandle = out = calloc (sizeof (*out), 1);
644 if (!out)
645 return GC_MALLOC_ERROR;
647 memcpy (out, in, sizeof (*out));
649 return GC_OK;
652 size_t
653 gc_hash_digest_length (Gc_hash hash)
655 size_t len;
657 switch (hash)
659 case GC_MD2:
660 len = GC_MD2_DIGEST_SIZE;
661 break;
663 case GC_MD4:
664 len = GC_MD4_DIGEST_SIZE;
665 break;
667 case GC_MD5:
668 len = GC_MD5_DIGEST_SIZE;
669 break;
671 case GC_RMD160:
672 len = GC_RMD160_DIGEST_SIZE;
673 break;
675 case GC_SHA1:
676 len = GC_SHA1_DIGEST_SIZE;
677 break;
679 default:
680 return 0;
683 return len;
686 void
687 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
689 _gc_hash_ctx *ctx = handle;
691 switch (ctx->alg)
693 #ifdef GNULIB_GC_MD2
694 case GC_MD2:
695 md2_process_bytes (data, len, &ctx->md2Context);
696 break;
697 #endif
699 #ifdef GNULIB_GC_MD4
700 case GC_MD4:
701 md4_process_bytes (data, len, &ctx->md4Context);
702 break;
703 #endif
705 #ifdef GNULIB_GC_MD5
706 case GC_MD5:
707 md5_process_bytes (data, len, &ctx->md5Context);
708 break;
709 #endif
711 #ifdef GNULIB_GC_SHA1
712 case GC_SHA1:
713 sha1_process_bytes (data, len, &ctx->sha1Context);
714 break;
715 #endif
717 default:
718 break;
722 const char *
723 gc_hash_read (gc_hash_handle handle)
725 _gc_hash_ctx *ctx = handle;
726 const char *ret = NULL;
728 switch (ctx->alg)
730 #ifdef GNULIB_GC_MD2
731 case GC_MD2:
732 md2_finish_ctx (&ctx->md2Context, ctx->hash);
733 ret = ctx->hash;
734 break;
735 #endif
737 #ifdef GNULIB_GC_MD4
738 case GC_MD4:
739 md4_finish_ctx (&ctx->md4Context, ctx->hash);
740 ret = ctx->hash;
741 break;
742 #endif
744 #ifdef GNULIB_GC_MD5
745 case GC_MD5:
746 md5_finish_ctx (&ctx->md5Context, ctx->hash);
747 ret = ctx->hash;
748 break;
749 #endif
751 #ifdef GNULIB_GC_SHA1
752 case GC_SHA1:
753 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
754 ret = ctx->hash;
755 break;
756 #endif
758 default:
759 return NULL;
762 return ret;
765 void
766 gc_hash_close (gc_hash_handle handle)
768 _gc_hash_ctx *ctx = handle;
770 free (ctx);
773 Gc_rc
774 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
776 switch (hash)
778 #ifdef GNULIB_GC_MD2
779 case GC_MD2:
780 md2_buffer (in, inlen, resbuf);
781 break;
782 #endif
784 #ifdef GNULIB_GC_MD4
785 case GC_MD4:
786 md4_buffer (in, inlen, resbuf);
787 break;
788 #endif
790 #ifdef GNULIB_GC_MD5
791 case GC_MD5:
792 md5_buffer (in, inlen, resbuf);
793 break;
794 #endif
796 #ifdef GNULIB_GC_SHA1
797 case GC_SHA1:
798 sha1_buffer (in, inlen, resbuf);
799 break;
800 #endif
802 default:
803 return GC_INVALID_HASH;
806 return GC_OK;
809 #ifdef GNULIB_GC_MD2
810 Gc_rc
811 gc_md2 (const void *in, size_t inlen, void *resbuf)
813 md2_buffer (in, inlen, resbuf);
814 return GC_OK;
816 #endif
818 #ifdef GNULIB_GC_MD4
819 Gc_rc
820 gc_md4 (const void *in, size_t inlen, void *resbuf)
822 md4_buffer (in, inlen, resbuf);
823 return GC_OK;
825 #endif
827 #ifdef GNULIB_GC_MD5
828 Gc_rc
829 gc_md5 (const void *in, size_t inlen, void *resbuf)
831 md5_buffer (in, inlen, resbuf);
832 return GC_OK;
834 #endif
836 #ifdef GNULIB_GC_SHA1
837 Gc_rc
838 gc_sha1 (const void *in, size_t inlen, void *resbuf)
840 sha1_buffer (in, inlen, resbuf);
841 return GC_OK;
843 #endif
845 #ifdef GNULIB_GC_HMAC_MD5
846 Gc_rc
847 gc_hmac_md5 (const void *key, size_t keylen,
848 const void *in, size_t inlen, char *resbuf)
850 hmac_md5 (key, keylen, in, inlen, resbuf);
851 return GC_OK;
853 #endif
855 #ifdef GNULIB_GC_HMAC_SHA1
856 Gc_rc
857 gc_hmac_sha1 (const void *key, size_t keylen,
858 const void *in, size_t inlen, char *resbuf)
860 hmac_sha1 (key, keylen, in, inlen, resbuf);
861 return GC_OK;
863 #endif