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