* gc-gnulib.c [GC_USE_HMAC_SHA1]: include hmac.h for hmac_sha1.
[gnulib.git] / lib / gc-gnulib.c
1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
2  * Copyright (C) 2002, 2003, 2004, 2005, 2006  Simon Josefsson
3  *
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.
8  *
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.
13  *
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.
18  *
19  */
20
21 /* Note: This file is only built if GC uses internal functions. */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Get prototype. */
28 #include "gc.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 /* For randomize. */
34 #ifdef GC_USE_RANDOM
35 # include <unistd.h>
36 # include <sys/types.h>
37 # include <sys/stat.h>
38 # include <fcntl.h>
39 # include <errno.h>
40 #endif
41
42 /* Hashes. */
43 #ifdef GC_USE_MD2
44 # include "md2.h"
45 #endif
46 #ifdef GC_USE_MD4
47 # include "md4.h"
48 #endif
49 #ifdef GC_USE_MD5
50 # include "md5.h"
51 #endif
52 #ifdef GC_USE_SHA1
53 # include "sha1.h"
54 #endif
55 #if defined(GC_USE_HMAC_MD5) || defined(GC_USE_HMAC_SHA1)
56 # include "hmac.h"
57 #endif
58
59 /* Ciphers. */
60 #ifdef GC_USE_ARCFOUR
61 # include "arcfour.h"
62 #endif
63 #ifdef GC_USE_ARCTWO
64 # include "arctwo.h"
65 #endif
66 #ifdef GC_USE_DES
67 # include "des.h"
68 #endif
69 #ifdef GC_USE_RIJNDAEL
70 # include "rijndael-api-fst.h"
71 #endif
72
73 Gc_rc
74 gc_init (void)
75 {
76   return GC_OK;
77 }
78
79 void
80 gc_done (void)
81 {
82   return;
83 }
84
85 #ifdef GC_USE_RANDOM
86
87 /* Randomness. */
88
89 static Gc_rc
90 randomize (int level, char *data, size_t datalen)
91 {
92   int fd;
93   const char *device;
94   size_t len = 0;
95   int rc;
96
97   switch (level)
98     {
99     case 0:
100       device = NAME_OF_NONCE_DEVICE;
101       break;
102
103     case 1:
104       device = NAME_OF_PSEUDO_RANDOM_DEVICE;
105       break;
106
107     default:
108       device = NAME_OF_RANDOM_DEVICE;
109       break;
110     }
111
112   if (strcmp (device, "no") == 0)
113     return GC_RANDOM_ERROR;
114
115   fd = open (device, O_RDONLY);
116   if (fd < 0)
117     return GC_RANDOM_ERROR;
118
119   do
120     {
121       ssize_t tmp;
122
123       tmp = read (fd, data, datalen);
124
125       if (tmp < 0)
126         {
127           int save_errno = errno;
128           close (fd);
129           errno = save_errno;
130           return GC_RANDOM_ERROR;
131         }
132
133       len += tmp;
134     }
135   while (len < datalen);
136
137   rc = close (fd);
138   if (rc < 0)
139     return GC_RANDOM_ERROR;
140
141   return GC_OK;
142 }
143
144 Gc_rc
145 gc_nonce (char *data, size_t datalen)
146 {
147   return randomize (0, data, datalen);
148 }
149
150 Gc_rc
151 gc_pseudo_random (char *data, size_t datalen)
152 {
153   return randomize (1, data, datalen);
154 }
155
156 Gc_rc
157 gc_random (char *data, size_t datalen)
158 {
159   return randomize (2, data, datalen);
160 }
161
162 #endif
163
164 /* Memory allocation. */
165
166 void
167 gc_set_allocators (gc_malloc_t func_malloc,
168                    gc_malloc_t secure_malloc,
169                    gc_secure_check_t secure_check,
170                    gc_realloc_t func_realloc, gc_free_t func_free)
171 {
172   return;
173 }
174 /* Ciphers. */
175
176 typedef struct _gc_cipher_ctx {
177   Gc_cipher alg;
178   Gc_cipher_mode mode;
179 #ifdef GC_USE_ARCTWO
180   arctwo_context arctwoContext;
181   char arctwoIV[ARCTWO_BLOCK_SIZE];
182 #endif
183 #ifdef GC_USE_ARCFOUR
184   arcfour_context arcfourContext;
185 #endif
186 #ifdef GC_USE_DES
187   des_ctx desContext;
188 #endif
189 #ifdef GC_USE_RIJNDAEL
190   rijndaelKeyInstance aesEncKey;
191   rijndaelKeyInstance aesDecKey;
192   rijndaelCipherInstance aesContext;
193 #endif
194 } _gc_cipher_ctx;
195
196 Gc_rc
197 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
198                 gc_cipher_handle * outhandle)
199 {
200   _gc_cipher_ctx *ctx;
201   Gc_rc rc = GC_OK;
202
203   ctx = calloc (sizeof (*ctx), 1);
204   if (!ctx)
205     return GC_MALLOC_ERROR;
206
207   ctx->alg = alg;
208   ctx->mode = mode;
209
210   switch (alg)
211     {
212 #ifdef GC_USE_ARCTWO
213     case GC_ARCTWO40:
214       switch (mode)
215         {
216         case GC_ECB:
217         case GC_CBC:
218           break;
219
220         default:
221           rc = GC_INVALID_CIPHER;
222         }
223       break;
224 #endif
225
226 #ifdef GC_USE_ARCFOUR
227     case GC_ARCFOUR128:
228     case GC_ARCFOUR40:
229       switch (mode)
230         {
231         case GC_STREAM:
232           break;
233
234         default:
235           rc = GC_INVALID_CIPHER;
236         }
237       break;
238 #endif
239
240 #ifdef GC_USE_DES
241     case GC_DES:
242       switch (mode)
243         {
244         case GC_ECB:
245           break;
246
247         default:
248           rc = GC_INVALID_CIPHER;
249         }
250       break;
251 #endif
252
253 #ifdef GC_USE_RIJNDAEL
254     case GC_AES128:
255     case GC_AES192:
256     case GC_AES256:
257       switch (mode)
258         {
259         case GC_ECB:
260         case GC_CBC:
261           break;
262
263         default:
264           rc = GC_INVALID_CIPHER;
265         }
266       break;
267 #endif
268
269     default:
270       rc = GC_INVALID_CIPHER;
271     }
272
273   if (rc == GC_OK)
274     *outhandle = ctx;
275   else
276     free (ctx);
277
278   return rc;
279 }
280
281 Gc_rc
282 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
283 {
284   _gc_cipher_ctx *ctx = handle;
285
286   switch (ctx->alg)
287     {
288 #ifdef GC_USE_ARCTWO
289     case GC_ARCTWO40:
290       arctwo_setkey (&ctx->arctwoContext, keylen, key);
291       break;
292 #endif
293
294 #ifdef GC_USE_ARCFOUR
295     case GC_ARCFOUR128:
296     case GC_ARCFOUR40:
297       arcfour_setkey (&ctx->arcfourContext, key, keylen);
298       break;
299 #endif
300
301 #ifdef GC_USE_DES
302     case GC_DES:
303       if (keylen != 8)
304         return GC_INVALID_CIPHER;
305       des_setkey (&ctx->desContext, key);
306       break;
307 #endif
308
309 #ifdef GC_USE_RIJNDAEL
310     case GC_AES128:
311     case GC_AES192:
312     case GC_AES256:
313       {
314         rijndael_rc rc;
315         size_t i;
316         char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
317
318         for (i = 0; i < keylen; i++)
319           sprintf (&keyMaterial[2*i], "%02x", key[i] & 0xFF);
320
321         rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
322                               keylen * 8, keyMaterial);
323         if (rc < 0)
324           return GC_INVALID_CIPHER;
325
326         rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
327                               keylen * 8, keyMaterial);
328         if (rc < 0)
329           return GC_INVALID_CIPHER;
330
331         rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
332         if (rc < 0)
333           return GC_INVALID_CIPHER;
334       }
335       break;
336 #endif
337
338     default:
339       return GC_INVALID_CIPHER;
340     }
341
342   return GC_OK;
343 }
344
345 Gc_rc
346 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
347 {
348   _gc_cipher_ctx *ctx = handle;
349
350   switch (ctx->alg)
351     {
352 #ifdef GC_USE_ARCTWO
353     case GC_ARCTWO40:
354       if (ivlen != ARCTWO_BLOCK_SIZE)
355         return GC_INVALID_CIPHER;
356       memcpy (ctx->arctwoIV, iv, ivlen);
357       break;
358 #endif
359
360 #ifdef GC_USE_RIJNDAEL
361     case GC_AES128:
362     case GC_AES192:
363     case GC_AES256:
364       switch (ctx->mode)
365         {
366         case GC_ECB:
367           /* Doesn't use IV. */
368           break;
369
370         case GC_CBC:
371           {
372             rijndael_rc rc;
373             size_t i;
374             char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
375
376             for (i = 0; i < ivlen; i++)
377               sprintf (&ivMaterial[2*i], "%02x", iv[i] & 0xFF);
378
379             rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
380                                      ivMaterial);
381             if (rc < 0)
382               return GC_INVALID_CIPHER;
383           }
384           break;
385
386         default:
387           return GC_INVALID_CIPHER;
388         }
389       break;
390 #endif
391
392     default:
393       return GC_INVALID_CIPHER;
394     }
395
396   return GC_OK;
397 }
398
399 Gc_rc
400 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
401 {
402   _gc_cipher_ctx *ctx = handle;
403
404   switch (ctx->alg)
405     {
406 #ifdef GC_USE_ARCTWO
407     case GC_ARCTWO40:
408       switch (ctx->mode)
409         {
410         case GC_ECB:
411           arctwo_encrypt (&ctx->arctwoContext, data, data, len);
412           break;
413
414         case GC_CBC:
415           for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
416                  data += ARCTWO_BLOCK_SIZE)
417             {
418               size_t i;
419               for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
420                 data[i] ^= ctx->arctwoIV[i];
421               arctwo_encrypt (&ctx->arctwoContext, data, data,
422                               ARCTWO_BLOCK_SIZE);
423               memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
424             }
425             break;
426
427         default:
428           return GC_INVALID_CIPHER;
429         }
430       break;
431 #endif
432
433 #ifdef GC_USE_ARCFOUR
434     case GC_ARCFOUR128:
435     case GC_ARCFOUR40:
436       arcfour_stream (&ctx->arcfourContext, data, data, len);
437       break;
438 #endif
439
440 #ifdef GC_USE_DES
441     case GC_DES:
442       for (; len >= 8; len -= 8, data += 8)
443         des_ecb_encrypt (&ctx->desContext, data, data);
444       break;
445 #endif
446
447 #ifdef GC_USE_RIJNDAEL
448     case GC_AES128:
449     case GC_AES192:
450     case GC_AES256:
451       {
452         int nblocks;
453
454         nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
455                                         data, 8 * len, data);
456         if (nblocks < 0)
457           return GC_INVALID_CIPHER;
458       }
459       break;
460 #endif
461
462     default:
463       return GC_INVALID_CIPHER;
464     }
465
466   return GC_OK;
467 }
468
469 Gc_rc
470 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
471 {
472   _gc_cipher_ctx *ctx = handle;
473
474   switch (ctx->alg)
475     {
476 #ifdef GC_USE_ARCTWO
477     case GC_ARCTWO40:
478       switch (ctx->mode)
479         {
480         case GC_ECB:
481           arctwo_decrypt (&ctx->arctwoContext, data, data, len);
482           break;
483
484         case GC_CBC:
485           for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
486                  data += ARCTWO_BLOCK_SIZE)
487             {
488               char tmpIV[ARCTWO_BLOCK_SIZE];
489               size_t i;
490               memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
491               arctwo_decrypt (&ctx->arctwoContext, data, data,
492                               ARCTWO_BLOCK_SIZE);
493               for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
494                 data[i] ^= ctx->arctwoIV[i];
495               memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
496             }
497           break;
498
499         default:
500           return GC_INVALID_CIPHER;
501         }
502       break;
503 #endif
504
505 #ifdef GC_USE_ARCFOUR
506     case GC_ARCFOUR128:
507     case GC_ARCFOUR40:
508       arcfour_stream (&ctx->arcfourContext, data, data, len);
509       break;
510 #endif
511
512 #ifdef GC_USE_DES
513     case GC_DES:
514       for (; len >= 8; len -= 8, data += 8)
515         des_ecb_decrypt (&ctx->desContext, data, data);
516       break;
517 #endif
518
519 #ifdef GC_USE_RIJNDAEL
520     case GC_AES128:
521     case GC_AES192:
522     case GC_AES256:
523       {
524         int nblocks;
525
526         nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
527                                         data, 8 * len, data);
528         if (nblocks < 0)
529           return GC_INVALID_CIPHER;
530       }
531       break;
532 #endif
533
534     default:
535       return GC_INVALID_CIPHER;
536     }
537
538   return GC_OK;
539 }
540
541 Gc_rc
542 gc_cipher_close (gc_cipher_handle handle)
543 {
544   _gc_cipher_ctx *ctx = handle;
545
546   if (ctx)
547     free (ctx);
548
549   return GC_OK;
550 }
551
552 /* Hashes. */
553
554 #define MAX_DIGEST_SIZE 20
555
556 typedef struct _gc_hash_ctx {
557   Gc_hash alg;
558   Gc_hash_mode mode;
559   char hash[MAX_DIGEST_SIZE];
560 #ifdef GC_USE_MD2
561   struct md2_ctx md2Context;
562 #endif
563 #ifdef GC_USE_MD4
564   struct md4_ctx md4Context;
565 #endif
566 #ifdef GC_USE_MD5
567   struct md5_ctx md5Context;
568 #endif
569 #ifdef GC_USE_SHA1
570   struct sha1_ctx sha1Context;
571 #endif
572 } _gc_hash_ctx;
573
574 Gc_rc
575 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
576 {
577   _gc_hash_ctx *ctx;
578   Gc_rc rc = GC_OK;
579
580   ctx = calloc (sizeof (*ctx), 1);
581
582   ctx->alg = hash;
583   ctx->mode = mode;
584
585   switch (hash)
586     {
587 #ifdef GC_USE_MD2
588     case GC_MD2:
589       md2_init_ctx (&ctx->md2Context);
590       break;
591 #endif
592
593 #ifdef GC_USE_MD4
594     case GC_MD4:
595       md4_init_ctx (&ctx->md4Context);
596       break;
597 #endif
598
599 #ifdef GC_USE_MD5
600     case GC_MD5:
601       md5_init_ctx (&ctx->md5Context);
602       break;
603 #endif
604
605 #ifdef GC_USE_SHA1
606     case GC_SHA1:
607       sha1_init_ctx (&ctx->sha1Context);
608       break;
609 #endif
610
611     default:
612       rc = GC_INVALID_HASH;
613       break;
614     }
615
616   switch (mode)
617     {
618     case 0:
619       break;
620
621     default:
622       rc = GC_INVALID_HASH;
623       break;
624     }
625
626   if (rc == GC_OK)
627     *outhandle = ctx;
628   else
629     free (ctx);
630
631   return rc;
632 }
633
634 Gc_rc
635 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
636 {
637   _gc_hash_ctx *in = handle;
638   _gc_hash_ctx *out;
639
640   *outhandle = out = calloc (sizeof (*out), 1);
641   if (!out)
642     return GC_MALLOC_ERROR;
643
644   memcpy (out, in, sizeof (*out));
645
646   return GC_OK;
647 }
648
649 size_t
650 gc_hash_digest_length (Gc_hash hash)
651 {
652   size_t len;
653
654   switch (hash)
655     {
656     case GC_MD2:
657       len = GC_MD2_DIGEST_SIZE;
658       break;
659
660     case GC_MD4:
661       len = GC_MD4_DIGEST_SIZE;
662       break;
663
664     case GC_MD5:
665       len = GC_MD5_DIGEST_SIZE;
666       break;
667
668     case GC_RMD160:
669       len = GC_RMD160_DIGEST_SIZE;
670       break;
671
672     case GC_SHA1:
673       len = GC_SHA1_DIGEST_SIZE;
674       break;
675
676     default:
677       return 0;
678     }
679
680   return len;
681 }
682
683 void
684 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
685 {
686   _gc_hash_ctx *ctx = handle;
687
688   switch (ctx->alg)
689     {
690 #ifdef GC_USE_MD2
691     case GC_MD2:
692       md2_process_bytes (data, len, &ctx->md2Context);
693       break;
694 #endif
695
696 #ifdef GC_USE_MD4
697     case GC_MD4:
698       md4_process_bytes (data, len, &ctx->md4Context);
699       break;
700 #endif
701
702 #ifdef GC_USE_MD5
703     case GC_MD5:
704       md5_process_bytes (data, len, &ctx->md5Context);
705       break;
706 #endif
707
708 #ifdef GC_USE_SHA1
709     case GC_SHA1:
710       sha1_process_bytes (data, len, &ctx->sha1Context);
711       break;
712 #endif
713
714     default:
715       break;
716     }
717 }
718
719 const char *
720 gc_hash_read (gc_hash_handle handle)
721 {
722   _gc_hash_ctx *ctx = handle;
723   const char *ret = NULL;
724
725   switch (ctx->alg)
726     {
727 #ifdef GC_USE_MD2
728     case GC_MD2:
729       md2_finish_ctx (&ctx->md2Context, ctx->hash);
730       ret = ctx->hash;
731       break;
732 #endif
733
734 #ifdef GC_USE_MD4
735     case GC_MD4:
736       md4_finish_ctx (&ctx->md4Context, ctx->hash);
737       ret = ctx->hash;
738       break;
739 #endif
740
741 #ifdef GC_USE_MD5
742     case GC_MD5:
743       md5_finish_ctx (&ctx->md5Context, ctx->hash);
744       ret = ctx->hash;
745       break;
746 #endif
747
748 #ifdef GC_USE_SHA1
749     case GC_SHA1:
750       sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
751       ret = ctx->hash;
752       break;
753 #endif
754
755     default:
756       return NULL;
757     }
758
759   return ret;
760 }
761
762 void
763 gc_hash_close (gc_hash_handle handle)
764 {
765   _gc_hash_ctx *ctx = handle;
766
767   free (ctx);
768 }
769
770 Gc_rc
771 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
772 {
773   switch (hash)
774     {
775 #ifdef GC_USE_MD2
776     case GC_MD2:
777       md2_buffer (in, inlen, resbuf);
778       break;
779 #endif
780
781 #ifdef GC_USE_MD4
782     case GC_MD4:
783       md4_buffer (in, inlen, resbuf);
784       break;
785 #endif
786
787 #ifdef GC_USE_MD5
788     case GC_MD5:
789       md5_buffer (in, inlen, resbuf);
790       break;
791 #endif
792
793 #ifdef GC_USE_SHA1
794     case GC_SHA1:
795       sha1_buffer (in, inlen, resbuf);
796       break;
797 #endif
798
799     default:
800       return GC_INVALID_HASH;
801     }
802
803   return GC_OK;
804 }
805
806 #ifdef GC_USE_MD2
807 Gc_rc
808 gc_md2 (const void *in, size_t inlen, void *resbuf)
809 {
810   md2_buffer (in, inlen, resbuf);
811   return GC_OK;
812 }
813 #endif
814
815 #ifdef GC_USE_MD4
816 Gc_rc
817 gc_md4 (const void *in, size_t inlen, void *resbuf)
818 {
819   md4_buffer (in, inlen, resbuf);
820   return GC_OK;
821 }
822 #endif
823
824 #ifdef GC_USE_MD5
825 Gc_rc
826 gc_md5 (const void *in, size_t inlen, void *resbuf)
827 {
828   md5_buffer (in, inlen, resbuf);
829   return GC_OK;
830 }
831 #endif
832
833 #ifdef GC_USE_SHA1
834 Gc_rc
835 gc_sha1 (const void *in, size_t inlen, void *resbuf)
836 {
837   sha1_buffer (in, inlen, resbuf);
838   return GC_OK;
839 }
840 #endif
841
842 #ifdef GC_USE_HMAC_MD5
843 Gc_rc
844 gc_hmac_md5 (const void *key, size_t keylen,
845              const void *in, size_t inlen, char *resbuf)
846 {
847   hmac_md5 (key, keylen, in, inlen, resbuf);
848   return GC_OK;
849 }
850 #endif
851
852 #ifdef GC_USE_HMAC_SHA1
853 Gc_rc
854 gc_hmac_sha1 (const void *key, size_t keylen,
855               const void *in, size_t inlen, char *resbuf)
856 {
857   hmac_sha1 (key, keylen, in, inlen, resbuf);
858   return GC_OK;
859 }
860 #endif