Update bibliography.
[gnutls.git] / lgl / gc-gnulib.c
blob27fafe452bfc3229213803a66ccea5d822180332
1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
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
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 #ifdef GNULIB_GC_RANDOM
77 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
78 # include <wincrypt.h>
79 HCRYPTPROV g_hProv = 0;
80 # endif
81 #endif
83 Gc_rc
84 gc_init (void)
86 #ifdef GNULIB_GC_RANDOM
87 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
88 if(g_hProv)
89 CryptReleaseContext(g_hProv, 0);
90 CryptAcquireContext(&g_hProv, NULL, NULL, PROV_RSA_FULL, 0);
91 # endif
92 #endif
94 return GC_OK;
97 void
98 gc_done (void)
100 #ifdef GNULIB_GC_RANDOM
101 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
102 if(g_hProv)
104 CryptReleaseContext(g_hProv, 0);
105 g_hProv = 0;
107 # endif
108 #endif
110 return;
113 #ifdef GNULIB_GC_RANDOM
115 /* Randomness. */
117 static Gc_rc
118 randomize (int level, char *data, size_t datalen)
120 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
121 if(!g_hProv)
122 return GC_RANDOM_ERROR;
123 CryptGenRandom(g_hProv, (DWORD)datalen, data);
124 #else
125 int fd;
126 const char *device;
127 size_t len = 0;
128 int rc;
130 switch (level)
132 case 0:
133 device = NAME_OF_NONCE_DEVICE;
134 break;
136 case 1:
137 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
138 break;
140 default:
141 device = NAME_OF_RANDOM_DEVICE;
142 break;
145 if (strcmp (device, "no") == 0)
146 return GC_RANDOM_ERROR;
148 fd = open (device, O_RDONLY);
149 if (fd < 0)
150 return GC_RANDOM_ERROR;
154 ssize_t tmp;
156 tmp = read (fd, data, datalen);
158 if (tmp < 0)
160 int save_errno = errno;
161 close (fd);
162 errno = save_errno;
163 return GC_RANDOM_ERROR;
166 len += tmp;
168 while (len < datalen);
170 rc = close (fd);
171 if (rc < 0)
172 return GC_RANDOM_ERROR;
173 #endif
175 return GC_OK;
178 Gc_rc
179 gc_nonce (char *data, size_t datalen)
181 return randomize (0, data, datalen);
184 Gc_rc
185 gc_pseudo_random (char *data, size_t datalen)
187 return randomize (1, data, datalen);
190 Gc_rc
191 gc_random (char *data, size_t datalen)
193 return randomize (2, data, datalen);
196 #endif
198 /* Memory allocation. */
200 void
201 gc_set_allocators (gc_malloc_t func_malloc,
202 gc_malloc_t secure_malloc,
203 gc_secure_check_t secure_check,
204 gc_realloc_t func_realloc, gc_free_t func_free)
206 return;
208 /* Ciphers. */
210 typedef struct _gc_cipher_ctx {
211 Gc_cipher alg;
212 Gc_cipher_mode mode;
213 #ifdef GNULIB_GC_ARCTWO
214 arctwo_context arctwoContext;
215 char arctwoIV[ARCTWO_BLOCK_SIZE];
216 #endif
217 #ifdef GNULIB_GC_ARCFOUR
218 arcfour_context arcfourContext;
219 #endif
220 #ifdef GNULIB_GC_DES
221 gl_des_ctx desContext;
222 #endif
223 #ifdef GNULIB_GC_RIJNDAEL
224 rijndaelKeyInstance aesEncKey;
225 rijndaelKeyInstance aesDecKey;
226 rijndaelCipherInstance aesContext;
227 #endif
228 } _gc_cipher_ctx;
230 Gc_rc
231 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
232 gc_cipher_handle * outhandle)
234 _gc_cipher_ctx *ctx;
235 Gc_rc rc = GC_OK;
237 ctx = calloc (sizeof (*ctx), 1);
238 if (!ctx)
239 return GC_MALLOC_ERROR;
241 ctx->alg = alg;
242 ctx->mode = mode;
244 switch (alg)
246 #ifdef GNULIB_GC_ARCTWO
247 case GC_ARCTWO40:
248 switch (mode)
250 case GC_ECB:
251 case GC_CBC:
252 break;
254 default:
255 rc = GC_INVALID_CIPHER;
257 break;
258 #endif
260 #ifdef GNULIB_GC_ARCFOUR
261 case GC_ARCFOUR128:
262 case GC_ARCFOUR40:
263 switch (mode)
265 case GC_STREAM:
266 break;
268 default:
269 rc = GC_INVALID_CIPHER;
271 break;
272 #endif
274 #ifdef GNULIB_GC_DES
275 case GC_DES:
276 switch (mode)
278 case GC_ECB:
279 break;
281 default:
282 rc = GC_INVALID_CIPHER;
284 break;
285 #endif
287 #ifdef GNULIB_GC_RIJNDAEL
288 case GC_AES128:
289 case GC_AES192:
290 case GC_AES256:
291 switch (mode)
293 case GC_ECB:
294 case GC_CBC:
295 break;
297 default:
298 rc = GC_INVALID_CIPHER;
300 break;
301 #endif
303 default:
304 rc = GC_INVALID_CIPHER;
307 if (rc == GC_OK)
308 *outhandle = ctx;
309 else
310 free (ctx);
312 return rc;
315 Gc_rc
316 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
318 _gc_cipher_ctx *ctx = handle;
320 switch (ctx->alg)
322 #ifdef GNULIB_GC_ARCTWO
323 case GC_ARCTWO40:
324 arctwo_setkey (&ctx->arctwoContext, keylen, key);
325 break;
326 #endif
328 #ifdef GNULIB_GC_ARCFOUR
329 case GC_ARCFOUR128:
330 case GC_ARCFOUR40:
331 arcfour_setkey (&ctx->arcfourContext, key, keylen);
332 break;
333 #endif
335 #ifdef GNULIB_GC_DES
336 case GC_DES:
337 if (keylen != 8)
338 return GC_INVALID_CIPHER;
339 gl_des_setkey (&ctx->desContext, key);
340 break;
341 #endif
343 #ifdef GNULIB_GC_RIJNDAEL
344 case GC_AES128:
345 case GC_AES192:
346 case GC_AES256:
348 rijndael_rc rc;
349 size_t i;
350 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
352 for (i = 0; i < keylen; i++)
353 sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
355 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
356 keylen * 8, keyMaterial);
357 if (rc < 0)
358 return GC_INVALID_CIPHER;
360 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
361 keylen * 8, keyMaterial);
362 if (rc < 0)
363 return GC_INVALID_CIPHER;
365 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
366 if (rc < 0)
367 return GC_INVALID_CIPHER;
369 break;
370 #endif
372 default:
373 return GC_INVALID_CIPHER;
376 return GC_OK;
379 Gc_rc
380 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
382 _gc_cipher_ctx *ctx = handle;
384 switch (ctx->alg)
386 #ifdef GNULIB_GC_ARCTWO
387 case GC_ARCTWO40:
388 if (ivlen != ARCTWO_BLOCK_SIZE)
389 return GC_INVALID_CIPHER;
390 memcpy (ctx->arctwoIV, iv, ivlen);
391 break;
392 #endif
394 #ifdef GNULIB_GC_RIJNDAEL
395 case GC_AES128:
396 case GC_AES192:
397 case GC_AES256:
398 switch (ctx->mode)
400 case GC_ECB:
401 /* Doesn't use IV. */
402 break;
404 case GC_CBC:
406 rijndael_rc rc;
407 size_t i;
408 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
410 for (i = 0; i < ivlen; i++)
411 sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
413 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
414 ivMaterial);
415 if (rc < 0)
416 return GC_INVALID_CIPHER;
418 break;
420 default:
421 return GC_INVALID_CIPHER;
423 break;
424 #endif
426 default:
427 return GC_INVALID_CIPHER;
430 return GC_OK;
433 Gc_rc
434 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
436 _gc_cipher_ctx *ctx = handle;
438 switch (ctx->alg)
440 #ifdef GNULIB_GC_ARCTWO
441 case GC_ARCTWO40:
442 switch (ctx->mode)
444 case GC_ECB:
445 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
446 break;
448 case GC_CBC:
449 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
450 data += ARCTWO_BLOCK_SIZE)
452 size_t i;
453 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
454 data[i] ^= ctx->arctwoIV[i];
455 arctwo_encrypt (&ctx->arctwoContext, data, data,
456 ARCTWO_BLOCK_SIZE);
457 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
459 break;
461 default:
462 return GC_INVALID_CIPHER;
464 break;
465 #endif
467 #ifdef GNULIB_GC_ARCFOUR
468 case GC_ARCFOUR128:
469 case GC_ARCFOUR40:
470 arcfour_stream (&ctx->arcfourContext, data, data, len);
471 break;
472 #endif
474 #ifdef GNULIB_GC_DES
475 case GC_DES:
476 for (; len >= 8; len -= 8, data += 8)
477 gl_des_ecb_encrypt (&ctx->desContext, data, data);
478 break;
479 #endif
481 #ifdef GNULIB_GC_RIJNDAEL
482 case GC_AES128:
483 case GC_AES192:
484 case GC_AES256:
486 int nblocks;
488 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
489 data, 8 * len, data);
490 if (nblocks < 0)
491 return GC_INVALID_CIPHER;
493 break;
494 #endif
496 default:
497 return GC_INVALID_CIPHER;
500 return GC_OK;
503 Gc_rc
504 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
506 _gc_cipher_ctx *ctx = handle;
508 switch (ctx->alg)
510 #ifdef GNULIB_GC_ARCTWO
511 case GC_ARCTWO40:
512 switch (ctx->mode)
514 case GC_ECB:
515 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
516 break;
518 case GC_CBC:
519 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
520 data += ARCTWO_BLOCK_SIZE)
522 char tmpIV[ARCTWO_BLOCK_SIZE];
523 size_t i;
524 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
525 arctwo_decrypt (&ctx->arctwoContext, data, data,
526 ARCTWO_BLOCK_SIZE);
527 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
528 data[i] ^= ctx->arctwoIV[i];
529 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
531 break;
533 default:
534 return GC_INVALID_CIPHER;
536 break;
537 #endif
539 #ifdef GNULIB_GC_ARCFOUR
540 case GC_ARCFOUR128:
541 case GC_ARCFOUR40:
542 arcfour_stream (&ctx->arcfourContext, data, data, len);
543 break;
544 #endif
546 #ifdef GNULIB_GC_DES
547 case GC_DES:
548 for (; len >= 8; len -= 8, data += 8)
549 gl_des_ecb_decrypt (&ctx->desContext, data, data);
550 break;
551 #endif
553 #ifdef GNULIB_GC_RIJNDAEL
554 case GC_AES128:
555 case GC_AES192:
556 case GC_AES256:
558 int nblocks;
560 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
561 data, 8 * len, data);
562 if (nblocks < 0)
563 return GC_INVALID_CIPHER;
565 break;
566 #endif
568 default:
569 return GC_INVALID_CIPHER;
572 return GC_OK;
575 Gc_rc
576 gc_cipher_close (gc_cipher_handle handle)
578 _gc_cipher_ctx *ctx = handle;
580 free (ctx);
582 return GC_OK;
585 /* Hashes. */
587 #define MAX_DIGEST_SIZE 20
589 typedef struct _gc_hash_ctx {
590 Gc_hash alg;
591 Gc_hash_mode mode;
592 char hash[MAX_DIGEST_SIZE];
593 #ifdef GNULIB_GC_MD2
594 struct md2_ctx md2Context;
595 #endif
596 #ifdef GNULIB_GC_MD4
597 struct md4_ctx md4Context;
598 #endif
599 #ifdef GNULIB_GC_MD5
600 struct md5_ctx md5Context;
601 #endif
602 #ifdef GNULIB_GC_SHA1
603 struct sha1_ctx sha1Context;
604 #endif
605 } _gc_hash_ctx;
607 Gc_rc
608 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
610 _gc_hash_ctx *ctx;
611 Gc_rc rc = GC_OK;
613 ctx = calloc (sizeof (*ctx), 1);
614 if (!ctx)
615 return GC_MALLOC_ERROR;
617 ctx->alg = hash;
618 ctx->mode = mode;
620 switch (hash)
622 #ifdef GNULIB_GC_MD2
623 case GC_MD2:
624 md2_init_ctx (&ctx->md2Context);
625 break;
626 #endif
628 #ifdef GNULIB_GC_MD4
629 case GC_MD4:
630 md4_init_ctx (&ctx->md4Context);
631 break;
632 #endif
634 #ifdef GNULIB_GC_MD5
635 case GC_MD5:
636 md5_init_ctx (&ctx->md5Context);
637 break;
638 #endif
640 #ifdef GNULIB_GC_SHA1
641 case GC_SHA1:
642 sha1_init_ctx (&ctx->sha1Context);
643 break;
644 #endif
646 default:
647 rc = GC_INVALID_HASH;
648 break;
651 switch (mode)
653 case 0:
654 break;
656 default:
657 rc = GC_INVALID_HASH;
658 break;
661 if (rc == GC_OK)
662 *outhandle = ctx;
663 else
664 free (ctx);
666 return rc;
669 Gc_rc
670 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
672 _gc_hash_ctx *in = handle;
673 _gc_hash_ctx *out;
675 *outhandle = out = calloc (sizeof (*out), 1);
676 if (!out)
677 return GC_MALLOC_ERROR;
679 memcpy (out, in, sizeof (*out));
681 return GC_OK;
684 size_t
685 gc_hash_digest_length (Gc_hash hash)
687 size_t len;
689 switch (hash)
691 case GC_MD2:
692 len = GC_MD2_DIGEST_SIZE;
693 break;
695 case GC_MD4:
696 len = GC_MD4_DIGEST_SIZE;
697 break;
699 case GC_MD5:
700 len = GC_MD5_DIGEST_SIZE;
701 break;
703 case GC_RMD160:
704 len = GC_RMD160_DIGEST_SIZE;
705 break;
707 case GC_SHA1:
708 len = GC_SHA1_DIGEST_SIZE;
709 break;
711 default:
712 return 0;
715 return len;
718 void
719 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
721 _gc_hash_ctx *ctx = handle;
723 switch (ctx->alg)
725 #ifdef GNULIB_GC_MD2
726 case GC_MD2:
727 md2_process_bytes (data, len, &ctx->md2Context);
728 break;
729 #endif
731 #ifdef GNULIB_GC_MD4
732 case GC_MD4:
733 md4_process_bytes (data, len, &ctx->md4Context);
734 break;
735 #endif
737 #ifdef GNULIB_GC_MD5
738 case GC_MD5:
739 md5_process_bytes (data, len, &ctx->md5Context);
740 break;
741 #endif
743 #ifdef GNULIB_GC_SHA1
744 case GC_SHA1:
745 sha1_process_bytes (data, len, &ctx->sha1Context);
746 break;
747 #endif
749 default:
750 break;
754 const char *
755 gc_hash_read (gc_hash_handle handle)
757 _gc_hash_ctx *ctx = handle;
758 const char *ret = NULL;
760 switch (ctx->alg)
762 #ifdef GNULIB_GC_MD2
763 case GC_MD2:
764 md2_finish_ctx (&ctx->md2Context, ctx->hash);
765 ret = ctx->hash;
766 break;
767 #endif
769 #ifdef GNULIB_GC_MD4
770 case GC_MD4:
771 md4_finish_ctx (&ctx->md4Context, ctx->hash);
772 ret = ctx->hash;
773 break;
774 #endif
776 #ifdef GNULIB_GC_MD5
777 case GC_MD5:
778 md5_finish_ctx (&ctx->md5Context, ctx->hash);
779 ret = ctx->hash;
780 break;
781 #endif
783 #ifdef GNULIB_GC_SHA1
784 case GC_SHA1:
785 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
786 ret = ctx->hash;
787 break;
788 #endif
790 default:
791 return NULL;
794 return ret;
797 void
798 gc_hash_close (gc_hash_handle handle)
800 _gc_hash_ctx *ctx = handle;
802 free (ctx);
805 Gc_rc
806 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
808 switch (hash)
810 #ifdef GNULIB_GC_MD2
811 case GC_MD2:
812 md2_buffer (in, inlen, resbuf);
813 break;
814 #endif
816 #ifdef GNULIB_GC_MD4
817 case GC_MD4:
818 md4_buffer (in, inlen, resbuf);
819 break;
820 #endif
822 #ifdef GNULIB_GC_MD5
823 case GC_MD5:
824 md5_buffer (in, inlen, resbuf);
825 break;
826 #endif
828 #ifdef GNULIB_GC_SHA1
829 case GC_SHA1:
830 sha1_buffer (in, inlen, resbuf);
831 break;
832 #endif
834 default:
835 return GC_INVALID_HASH;
838 return GC_OK;
841 #ifdef GNULIB_GC_MD2
842 Gc_rc
843 gc_md2 (const void *in, size_t inlen, void *resbuf)
845 md2_buffer (in, inlen, resbuf);
846 return GC_OK;
848 #endif
850 #ifdef GNULIB_GC_MD4
851 Gc_rc
852 gc_md4 (const void *in, size_t inlen, void *resbuf)
854 md4_buffer (in, inlen, resbuf);
855 return GC_OK;
857 #endif
859 #ifdef GNULIB_GC_MD5
860 Gc_rc
861 gc_md5 (const void *in, size_t inlen, void *resbuf)
863 md5_buffer (in, inlen, resbuf);
864 return GC_OK;
866 #endif
868 #ifdef GNULIB_GC_SHA1
869 Gc_rc
870 gc_sha1 (const void *in, size_t inlen, void *resbuf)
872 sha1_buffer (in, inlen, resbuf);
873 return GC_OK;
875 #endif
877 #ifdef GNULIB_GC_HMAC_MD5
878 Gc_rc
879 gc_hmac_md5 (const void *key, size_t keylen,
880 const void *in, size_t inlen, char *resbuf)
882 hmac_md5 (key, keylen, in, inlen, resbuf);
883 return GC_OK;
885 #endif
887 #ifdef GNULIB_GC_HMAC_SHA1
888 Gc_rc
889 gc_hmac_sha1 (const void *key, size_t keylen,
890 const void *in, size_t inlen, char *resbuf)
892 hmac_sha1 (key, keylen, in, inlen, resbuf);
893 return GC_OK;
895 #endif