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