Add.
[gnutls.git] / lgl / gc-gnulib.c
blobce4ff029c252dd0be810e1bb1d681845d1c15077
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 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
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 gl_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 gl_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 gl_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 gl_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 free (ctx);
551 return GC_OK;
554 /* Hashes. */
556 #define MAX_DIGEST_SIZE 20
558 typedef struct _gc_hash_ctx {
559 Gc_hash alg;
560 Gc_hash_mode mode;
561 char hash[MAX_DIGEST_SIZE];
562 #ifdef GNULIB_GC_MD2
563 struct md2_ctx md2Context;
564 #endif
565 #ifdef GNULIB_GC_MD4
566 struct md4_ctx md4Context;
567 #endif
568 #ifdef GNULIB_GC_MD5
569 struct md5_ctx md5Context;
570 #endif
571 #ifdef GNULIB_GC_SHA1
572 struct sha1_ctx sha1Context;
573 #endif
574 } _gc_hash_ctx;
576 Gc_rc
577 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
579 _gc_hash_ctx *ctx;
580 Gc_rc rc = GC_OK;
582 ctx = calloc (sizeof (*ctx), 1);
583 if (!ctx)
584 return GC_MALLOC_ERROR;
586 ctx->alg = hash;
587 ctx->mode = mode;
589 switch (hash)
591 #ifdef GNULIB_GC_MD2
592 case GC_MD2:
593 md2_init_ctx (&ctx->md2Context);
594 break;
595 #endif
597 #ifdef GNULIB_GC_MD4
598 case GC_MD4:
599 md4_init_ctx (&ctx->md4Context);
600 break;
601 #endif
603 #ifdef GNULIB_GC_MD5
604 case GC_MD5:
605 md5_init_ctx (&ctx->md5Context);
606 break;
607 #endif
609 #ifdef GNULIB_GC_SHA1
610 case GC_SHA1:
611 sha1_init_ctx (&ctx->sha1Context);
612 break;
613 #endif
615 default:
616 rc = GC_INVALID_HASH;
617 break;
620 switch (mode)
622 case 0:
623 break;
625 default:
626 rc = GC_INVALID_HASH;
627 break;
630 if (rc == GC_OK)
631 *outhandle = ctx;
632 else
633 free (ctx);
635 return rc;
638 Gc_rc
639 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
641 _gc_hash_ctx *in = handle;
642 _gc_hash_ctx *out;
644 *outhandle = out = calloc (sizeof (*out), 1);
645 if (!out)
646 return GC_MALLOC_ERROR;
648 memcpy (out, in, sizeof (*out));
650 return GC_OK;
653 size_t
654 gc_hash_digest_length (Gc_hash hash)
656 size_t len;
658 switch (hash)
660 case GC_MD2:
661 len = GC_MD2_DIGEST_SIZE;
662 break;
664 case GC_MD4:
665 len = GC_MD4_DIGEST_SIZE;
666 break;
668 case GC_MD5:
669 len = GC_MD5_DIGEST_SIZE;
670 break;
672 case GC_RMD160:
673 len = GC_RMD160_DIGEST_SIZE;
674 break;
676 case GC_SHA1:
677 len = GC_SHA1_DIGEST_SIZE;
678 break;
680 default:
681 return 0;
684 return len;
687 void
688 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
690 _gc_hash_ctx *ctx = handle;
692 switch (ctx->alg)
694 #ifdef GNULIB_GC_MD2
695 case GC_MD2:
696 md2_process_bytes (data, len, &ctx->md2Context);
697 break;
698 #endif
700 #ifdef GNULIB_GC_MD4
701 case GC_MD4:
702 md4_process_bytes (data, len, &ctx->md4Context);
703 break;
704 #endif
706 #ifdef GNULIB_GC_MD5
707 case GC_MD5:
708 md5_process_bytes (data, len, &ctx->md5Context);
709 break;
710 #endif
712 #ifdef GNULIB_GC_SHA1
713 case GC_SHA1:
714 sha1_process_bytes (data, len, &ctx->sha1Context);
715 break;
716 #endif
718 default:
719 break;
723 const char *
724 gc_hash_read (gc_hash_handle handle)
726 _gc_hash_ctx *ctx = handle;
727 const char *ret = NULL;
729 switch (ctx->alg)
731 #ifdef GNULIB_GC_MD2
732 case GC_MD2:
733 md2_finish_ctx (&ctx->md2Context, ctx->hash);
734 ret = ctx->hash;
735 break;
736 #endif
738 #ifdef GNULIB_GC_MD4
739 case GC_MD4:
740 md4_finish_ctx (&ctx->md4Context, ctx->hash);
741 ret = ctx->hash;
742 break;
743 #endif
745 #ifdef GNULIB_GC_MD5
746 case GC_MD5:
747 md5_finish_ctx (&ctx->md5Context, ctx->hash);
748 ret = ctx->hash;
749 break;
750 #endif
752 #ifdef GNULIB_GC_SHA1
753 case GC_SHA1:
754 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
755 ret = ctx->hash;
756 break;
757 #endif
759 default:
760 return NULL;
763 return ret;
766 void
767 gc_hash_close (gc_hash_handle handle)
769 _gc_hash_ctx *ctx = handle;
771 free (ctx);
774 Gc_rc
775 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
777 switch (hash)
779 #ifdef GNULIB_GC_MD2
780 case GC_MD2:
781 md2_buffer (in, inlen, resbuf);
782 break;
783 #endif
785 #ifdef GNULIB_GC_MD4
786 case GC_MD4:
787 md4_buffer (in, inlen, resbuf);
788 break;
789 #endif
791 #ifdef GNULIB_GC_MD5
792 case GC_MD5:
793 md5_buffer (in, inlen, resbuf);
794 break;
795 #endif
797 #ifdef GNULIB_GC_SHA1
798 case GC_SHA1:
799 sha1_buffer (in, inlen, resbuf);
800 break;
801 #endif
803 default:
804 return GC_INVALID_HASH;
807 return GC_OK;
810 #ifdef GNULIB_GC_MD2
811 Gc_rc
812 gc_md2 (const void *in, size_t inlen, void *resbuf)
814 md2_buffer (in, inlen, resbuf);
815 return GC_OK;
817 #endif
819 #ifdef GNULIB_GC_MD4
820 Gc_rc
821 gc_md4 (const void *in, size_t inlen, void *resbuf)
823 md4_buffer (in, inlen, resbuf);
824 return GC_OK;
826 #endif
828 #ifdef GNULIB_GC_MD5
829 Gc_rc
830 gc_md5 (const void *in, size_t inlen, void *resbuf)
832 md5_buffer (in, inlen, resbuf);
833 return GC_OK;
835 #endif
837 #ifdef GNULIB_GC_SHA1
838 Gc_rc
839 gc_sha1 (const void *in, size_t inlen, void *resbuf)
841 sha1_buffer (in, inlen, resbuf);
842 return GC_OK;
844 #endif
846 #ifdef GNULIB_GC_HMAC_MD5
847 Gc_rc
848 gc_hmac_md5 (const void *key, size_t keylen,
849 const void *in, size_t inlen, char *resbuf)
851 hmac_md5 (key, keylen, in, inlen, resbuf);
852 return GC_OK;
854 #endif
856 #ifdef GNULIB_GC_HMAC_SHA1
857 Gc_rc
858 gc_hmac_sha1 (const void *key, size_t keylen,
859 const void *in, size_t inlen, char *resbuf)
861 hmac_sha1 (key, keylen, in, inlen, resbuf);
862 return GC_OK;
864 #endif