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