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