glob: fix heap buffer overflow
[gnulib.git] / lib / gc-gnulib.c
blob61d5d980a6f7287059cc7871cf614202afa25430
1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002-2017 Free Software Foundation, Inc.
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this file; if not, see <https://www.gnu.org/licenses/>.
19 /* Note: This file is only built if GC uses internal functions. */
21 #include <config.h>
23 /* Get prototype. */
24 #include "gc.h"
26 #include <stdlib.h>
27 #include <string.h>
29 /* For randomize. */
30 #ifdef GNULIB_GC_RANDOM
31 # include <unistd.h>
32 # include <sys/types.h>
33 # include <sys/stat.h>
34 # include <fcntl.h>
35 # include <errno.h>
36 #endif
38 /* Hashes. */
39 #ifdef GNULIB_GC_MD2
40 # include "md2.h"
41 #endif
42 #ifdef GNULIB_GC_MD4
43 # include "md4.h"
44 #endif
45 #ifdef GNULIB_GC_MD5
46 # include "md5.h"
47 #endif
48 #ifdef GNULIB_GC_SHA1
49 # include "sha1.h"
50 #endif
51 #if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1) || defined(GNULIB_GC_HMAC_SHA256) || defined(GNULIB_GC_HMAC_SHA512)
52 # include "hmac.h"
53 #endif
55 /* Ciphers. */
56 #ifdef GNULIB_GC_ARCFOUR
57 # include "arcfour.h"
58 #endif
59 #ifdef GNULIB_GC_ARCTWO
60 # include "arctwo.h"
61 #endif
62 #ifdef GNULIB_GC_DES
63 # include "des.h"
64 #endif
65 #ifdef GNULIB_GC_RIJNDAEL
66 # include "rijndael-api-fst.h"
67 #endif
69 #ifdef GNULIB_GC_RANDOM
70 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
71 # include <windows.h>
72 # include <wincrypt.h>
73 HCRYPTPROV g_hProv = 0;
74 # ifndef PROV_INTEL_SEC
75 # define PROV_INTEL_SEC 22
76 # endif
77 # ifndef CRYPT_VERIFY_CONTEXT
78 # define CRYPT_VERIFY_CONTEXT 0xF0000000
79 # endif
80 # endif
81 #endif
83 Gc_rc
84 gc_init (void)
86 #ifdef GNULIB_GC_RANDOM
87 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
88 if (g_hProv)
89 CryptReleaseContext (g_hProv, 0);
91 /* There is no need to create a container for just random data, so
92 we can use CRYPT_VERIFY_CONTEXT (one call) see:
93 http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */
95 /* We first try to use the Intel PIII RNG if drivers are present */
96 if (!CryptAcquireContext (&g_hProv, NULL, NULL,
97 PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT))
99 /* not a PIII or no drivers available, use default RSA CSP */
100 if (!CryptAcquireContext (&g_hProv, NULL, NULL,
101 PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT))
102 return GC_RANDOM_ERROR;
104 # endif
105 #endif
107 return GC_OK;
110 void
111 gc_done (void)
113 #ifdef GNULIB_GC_RANDOM
114 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
115 if (g_hProv)
117 CryptReleaseContext (g_hProv, 0);
118 g_hProv = 0;
120 # endif
121 #endif
123 return;
126 #ifdef GNULIB_GC_RANDOM
128 /* Randomness. */
130 static Gc_rc
131 randomize (int level, char *data, size_t datalen)
133 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
134 if (!g_hProv)
135 return GC_RANDOM_ERROR;
136 CryptGenRandom (g_hProv, (DWORD) datalen, data);
137 #else
138 int fd;
139 const char *device;
140 size_t len = 0;
141 int rc;
143 switch (level)
145 case 0:
146 device = NAME_OF_NONCE_DEVICE;
147 break;
149 case 1:
150 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
151 break;
153 default:
154 device = NAME_OF_RANDOM_DEVICE;
155 break;
158 if (strcmp (device, "no") == 0)
159 return GC_RANDOM_ERROR;
161 fd = open (device, O_RDONLY);
162 if (fd < 0)
163 return GC_RANDOM_ERROR;
167 ssize_t tmp;
169 tmp = read (fd, data, datalen);
171 if (tmp < 0)
173 int save_errno = errno;
174 close (fd);
175 errno = save_errno;
176 return GC_RANDOM_ERROR;
179 len += tmp;
181 while (len < datalen);
183 rc = close (fd);
184 if (rc < 0)
185 return GC_RANDOM_ERROR;
186 #endif
188 return GC_OK;
191 Gc_rc
192 gc_nonce (char *data, size_t datalen)
194 return randomize (0, data, datalen);
197 Gc_rc
198 gc_pseudo_random (char *data, size_t datalen)
200 return randomize (1, data, datalen);
203 Gc_rc
204 gc_random (char *data, size_t datalen)
206 return randomize (2, data, datalen);
209 #endif
211 /* Memory allocation. */
213 void
214 gc_set_allocators (gc_malloc_t func_malloc,
215 gc_malloc_t secure_malloc,
216 gc_secure_check_t secure_check,
217 gc_realloc_t func_realloc, gc_free_t func_free)
219 return;
222 /* Ciphers. */
224 typedef struct _gc_cipher_ctx
226 Gc_cipher alg;
227 Gc_cipher_mode mode;
228 #ifdef GNULIB_GC_ARCTWO
229 arctwo_context arctwoContext;
230 char arctwoIV[ARCTWO_BLOCK_SIZE];
231 #endif
232 #ifdef GNULIB_GC_ARCFOUR
233 arcfour_context arcfourContext;
234 #endif
235 #ifdef GNULIB_GC_DES
236 gl_des_ctx desContext;
237 #endif
238 #ifdef GNULIB_GC_RIJNDAEL
239 rijndaelKeyInstance aesEncKey;
240 rijndaelKeyInstance aesDecKey;
241 rijndaelCipherInstance aesContext;
242 #endif
243 } _gc_cipher_ctx;
245 Gc_rc
246 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
247 gc_cipher_handle * outhandle)
249 _gc_cipher_ctx *ctx;
250 Gc_rc rc = GC_OK;
252 ctx = calloc (sizeof (*ctx), 1);
253 if (!ctx)
254 return GC_MALLOC_ERROR;
256 ctx->alg = alg;
257 ctx->mode = mode;
259 switch (alg)
261 #ifdef GNULIB_GC_ARCTWO
262 case GC_ARCTWO40:
263 switch (mode)
265 case GC_ECB:
266 case GC_CBC:
267 break;
269 default:
270 rc = GC_INVALID_CIPHER;
272 break;
273 #endif
275 #ifdef GNULIB_GC_ARCFOUR
276 case GC_ARCFOUR128:
277 case GC_ARCFOUR40:
278 switch (mode)
280 case GC_STREAM:
281 break;
283 default:
284 rc = GC_INVALID_CIPHER;
286 break;
287 #endif
289 #ifdef GNULIB_GC_DES
290 case GC_DES:
291 switch (mode)
293 case GC_ECB:
294 break;
296 default:
297 rc = GC_INVALID_CIPHER;
299 break;
300 #endif
302 #ifdef GNULIB_GC_RIJNDAEL
303 case GC_AES128:
304 case GC_AES192:
305 case GC_AES256:
306 switch (mode)
308 case GC_ECB:
309 case GC_CBC:
310 break;
312 default:
313 rc = GC_INVALID_CIPHER;
315 break;
316 #endif
318 default:
319 rc = GC_INVALID_CIPHER;
322 if (rc == GC_OK)
323 *outhandle = ctx;
324 else
325 free (ctx);
327 return rc;
330 Gc_rc
331 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
333 _gc_cipher_ctx *ctx = handle;
335 switch (ctx->alg)
337 #ifdef GNULIB_GC_ARCTWO
338 case GC_ARCTWO40:
339 arctwo_setkey (&ctx->arctwoContext, keylen, key);
340 break;
341 #endif
343 #ifdef GNULIB_GC_ARCFOUR
344 case GC_ARCFOUR128:
345 case GC_ARCFOUR40:
346 arcfour_setkey (&ctx->arcfourContext, key, keylen);
347 break;
348 #endif
350 #ifdef GNULIB_GC_DES
351 case GC_DES:
352 if (keylen != 8)
353 return GC_INVALID_CIPHER;
354 gl_des_setkey (&ctx->desContext, key);
355 break;
356 #endif
358 #ifdef GNULIB_GC_RIJNDAEL
359 case GC_AES128:
360 case GC_AES192:
361 case GC_AES256:
363 rijndael_rc rc;
364 size_t i;
365 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
367 for (i = 0; i < keylen; i++)
368 sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
370 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
371 keylen * 8, keyMaterial);
372 if (rc < 0)
373 return GC_INVALID_CIPHER;
375 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
376 keylen * 8, keyMaterial);
377 if (rc < 0)
378 return GC_INVALID_CIPHER;
380 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
381 if (rc < 0)
382 return GC_INVALID_CIPHER;
384 break;
385 #endif
387 default:
388 return GC_INVALID_CIPHER;
391 return GC_OK;
394 Gc_rc
395 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
397 _gc_cipher_ctx *ctx = handle;
399 switch (ctx->alg)
401 #ifdef GNULIB_GC_ARCTWO
402 case GC_ARCTWO40:
403 if (ivlen != ARCTWO_BLOCK_SIZE)
404 return GC_INVALID_CIPHER;
405 memcpy (ctx->arctwoIV, iv, ivlen);
406 break;
407 #endif
409 #ifdef GNULIB_GC_RIJNDAEL
410 case GC_AES128:
411 case GC_AES192:
412 case GC_AES256:
413 switch (ctx->mode)
415 case GC_ECB:
416 /* Doesn't use IV. */
417 break;
419 case GC_CBC:
421 rijndael_rc rc;
422 size_t i;
423 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
425 for (i = 0; i < ivlen; i++)
426 sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
428 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
429 ivMaterial);
430 if (rc < 0)
431 return GC_INVALID_CIPHER;
433 break;
435 default:
436 return GC_INVALID_CIPHER;
438 break;
439 #endif
441 default:
442 return GC_INVALID_CIPHER;
445 return GC_OK;
448 Gc_rc
449 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
451 _gc_cipher_ctx *ctx = handle;
453 switch (ctx->alg)
455 #ifdef GNULIB_GC_ARCTWO
456 case GC_ARCTWO40:
457 switch (ctx->mode)
459 case GC_ECB:
460 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
461 break;
463 case GC_CBC:
464 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
465 data += ARCTWO_BLOCK_SIZE)
467 size_t i;
468 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
469 data[i] ^= ctx->arctwoIV[i];
470 arctwo_encrypt (&ctx->arctwoContext, data, data,
471 ARCTWO_BLOCK_SIZE);
472 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
474 break;
476 default:
477 return GC_INVALID_CIPHER;
479 break;
480 #endif
482 #ifdef GNULIB_GC_ARCFOUR
483 case GC_ARCFOUR128:
484 case GC_ARCFOUR40:
485 arcfour_stream (&ctx->arcfourContext, data, data, len);
486 break;
487 #endif
489 #ifdef GNULIB_GC_DES
490 case GC_DES:
491 for (; len >= 8; len -= 8, data += 8)
492 gl_des_ecb_encrypt (&ctx->desContext, data, data);
493 break;
494 #endif
496 #ifdef GNULIB_GC_RIJNDAEL
497 case GC_AES128:
498 case GC_AES192:
499 case GC_AES256:
501 int nblocks;
503 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
504 data, 8 * len, data);
505 if (nblocks < 0)
506 return GC_INVALID_CIPHER;
508 break;
509 #endif
511 default:
512 return GC_INVALID_CIPHER;
515 return GC_OK;
518 Gc_rc
519 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
521 _gc_cipher_ctx *ctx = handle;
523 switch (ctx->alg)
525 #ifdef GNULIB_GC_ARCTWO
526 case GC_ARCTWO40:
527 switch (ctx->mode)
529 case GC_ECB:
530 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
531 break;
533 case GC_CBC:
534 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
535 data += ARCTWO_BLOCK_SIZE)
537 char tmpIV[ARCTWO_BLOCK_SIZE];
538 size_t i;
539 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
540 arctwo_decrypt (&ctx->arctwoContext, data, data,
541 ARCTWO_BLOCK_SIZE);
542 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
543 data[i] ^= ctx->arctwoIV[i];
544 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
546 break;
548 default:
549 return GC_INVALID_CIPHER;
551 break;
552 #endif
554 #ifdef GNULIB_GC_ARCFOUR
555 case GC_ARCFOUR128:
556 case GC_ARCFOUR40:
557 arcfour_stream (&ctx->arcfourContext, data, data, len);
558 break;
559 #endif
561 #ifdef GNULIB_GC_DES
562 case GC_DES:
563 for (; len >= 8; len -= 8, data += 8)
564 gl_des_ecb_decrypt (&ctx->desContext, data, data);
565 break;
566 #endif
568 #ifdef GNULIB_GC_RIJNDAEL
569 case GC_AES128:
570 case GC_AES192:
571 case GC_AES256:
573 int nblocks;
575 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
576 data, 8 * len, data);
577 if (nblocks < 0)
578 return GC_INVALID_CIPHER;
580 break;
581 #endif
583 default:
584 return GC_INVALID_CIPHER;
587 return GC_OK;
590 Gc_rc
591 gc_cipher_close (gc_cipher_handle handle)
593 _gc_cipher_ctx *ctx = handle;
595 free (ctx);
597 return GC_OK;
600 /* Hashes. */
602 #define MAX_DIGEST_SIZE 20
604 typedef struct _gc_hash_ctx
606 Gc_hash alg;
607 Gc_hash_mode mode;
608 char hash[MAX_DIGEST_SIZE];
609 #ifdef GNULIB_GC_MD2
610 struct md2_ctx md2Context;
611 #endif
612 #ifdef GNULIB_GC_MD4
613 struct md4_ctx md4Context;
614 #endif
615 #ifdef GNULIB_GC_MD5
616 struct md5_ctx md5Context;
617 #endif
618 #ifdef GNULIB_GC_SHA1
619 struct sha1_ctx sha1Context;
620 #endif
621 } _gc_hash_ctx;
623 Gc_rc
624 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
626 _gc_hash_ctx *ctx;
627 Gc_rc rc = GC_OK;
629 if (mode != 0)
630 return GC_INVALID_HASH;
632 ctx = calloc (sizeof (*ctx), 1);
633 if (!ctx)
634 return GC_MALLOC_ERROR;
636 ctx->alg = hash;
637 ctx->mode = mode;
639 switch (hash)
641 #ifdef GNULIB_GC_MD2
642 case GC_MD2:
643 md2_init_ctx (&ctx->md2Context);
644 break;
645 #endif
647 #ifdef GNULIB_GC_MD4
648 case GC_MD4:
649 md4_init_ctx (&ctx->md4Context);
650 break;
651 #endif
653 #ifdef GNULIB_GC_MD5
654 case GC_MD5:
655 md5_init_ctx (&ctx->md5Context);
656 break;
657 #endif
659 #ifdef GNULIB_GC_SHA1
660 case GC_SHA1:
661 sha1_init_ctx (&ctx->sha1Context);
662 break;
663 #endif
665 default:
666 rc = GC_INVALID_HASH;
667 break;
670 if (rc == GC_OK)
671 *outhandle = ctx;
672 else
673 free (ctx);
675 return rc;
678 Gc_rc
679 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
681 _gc_hash_ctx *in = handle;
682 _gc_hash_ctx *out;
684 *outhandle = out = calloc (sizeof (*out), 1);
685 if (!out)
686 return GC_MALLOC_ERROR;
688 memcpy (out, in, sizeof (*out));
690 return GC_OK;
693 size_t
694 gc_hash_digest_length (Gc_hash hash)
696 size_t len;
698 switch (hash)
700 case GC_MD2:
701 len = GC_MD2_DIGEST_SIZE;
702 break;
704 case GC_MD4:
705 len = GC_MD4_DIGEST_SIZE;
706 break;
708 case GC_MD5:
709 len = GC_MD5_DIGEST_SIZE;
710 break;
712 case GC_RMD160:
713 len = GC_RMD160_DIGEST_SIZE;
714 break;
716 case GC_SHA1:
717 len = GC_SHA1_DIGEST_SIZE;
718 break;
720 default:
721 return 0;
724 return len;
727 void
728 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
730 _gc_hash_ctx *ctx = handle;
732 switch (ctx->alg)
734 #ifdef GNULIB_GC_MD2
735 case GC_MD2:
736 md2_process_bytes (data, len, &ctx->md2Context);
737 break;
738 #endif
740 #ifdef GNULIB_GC_MD4
741 case GC_MD4:
742 md4_process_bytes (data, len, &ctx->md4Context);
743 break;
744 #endif
746 #ifdef GNULIB_GC_MD5
747 case GC_MD5:
748 md5_process_bytes (data, len, &ctx->md5Context);
749 break;
750 #endif
752 #ifdef GNULIB_GC_SHA1
753 case GC_SHA1:
754 sha1_process_bytes (data, len, &ctx->sha1Context);
755 break;
756 #endif
758 default:
759 break;
763 const char *
764 gc_hash_read (gc_hash_handle handle)
766 _gc_hash_ctx *ctx = handle;
767 const char *ret = NULL;
769 switch (ctx->alg)
771 #ifdef GNULIB_GC_MD2
772 case GC_MD2:
773 md2_finish_ctx (&ctx->md2Context, ctx->hash);
774 ret = ctx->hash;
775 break;
776 #endif
778 #ifdef GNULIB_GC_MD4
779 case GC_MD4:
780 md4_finish_ctx (&ctx->md4Context, ctx->hash);
781 ret = ctx->hash;
782 break;
783 #endif
785 #ifdef GNULIB_GC_MD5
786 case GC_MD5:
787 md5_finish_ctx (&ctx->md5Context, ctx->hash);
788 ret = ctx->hash;
789 break;
790 #endif
792 #ifdef GNULIB_GC_SHA1
793 case GC_SHA1:
794 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
795 ret = ctx->hash;
796 break;
797 #endif
799 default:
800 return NULL;
803 return ret;
806 void
807 gc_hash_close (gc_hash_handle handle)
809 _gc_hash_ctx *ctx = handle;
811 free (ctx);
814 Gc_rc
815 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
817 switch (hash)
819 #ifdef GNULIB_GC_MD2
820 case GC_MD2:
821 md2_buffer (in, inlen, resbuf);
822 break;
823 #endif
825 #ifdef GNULIB_GC_MD4
826 case GC_MD4:
827 md4_buffer (in, inlen, resbuf);
828 break;
829 #endif
831 #ifdef GNULIB_GC_MD5
832 case GC_MD5:
833 md5_buffer (in, inlen, resbuf);
834 break;
835 #endif
837 #ifdef GNULIB_GC_SHA1
838 case GC_SHA1:
839 sha1_buffer (in, inlen, resbuf);
840 break;
841 #endif
843 default:
844 return GC_INVALID_HASH;
847 return GC_OK;
850 #ifdef GNULIB_GC_MD2
851 Gc_rc
852 gc_md2 (const void *in, size_t inlen, void *resbuf)
854 md2_buffer (in, inlen, resbuf);
855 return GC_OK;
857 #endif
859 #ifdef GNULIB_GC_MD4
860 Gc_rc
861 gc_md4 (const void *in, size_t inlen, void *resbuf)
863 md4_buffer (in, inlen, resbuf);
864 return GC_OK;
866 #endif
868 #ifdef GNULIB_GC_MD5
869 Gc_rc
870 gc_md5 (const void *in, size_t inlen, void *resbuf)
872 md5_buffer (in, inlen, resbuf);
873 return GC_OK;
875 #endif
877 #ifdef GNULIB_GC_SHA1
878 Gc_rc
879 gc_sha1 (const void *in, size_t inlen, void *resbuf)
881 sha1_buffer (in, inlen, resbuf);
882 return GC_OK;
884 #endif
886 #ifdef GNULIB_GC_HMAC_MD5
887 Gc_rc
888 gc_hmac_md5 (const void *key, size_t keylen,
889 const void *in, size_t inlen, char *resbuf)
891 hmac_md5 (key, keylen, in, inlen, resbuf);
892 return GC_OK;
894 #endif
896 #ifdef GNULIB_GC_HMAC_SHA1
897 Gc_rc
898 gc_hmac_sha1 (const void *key, size_t keylen,
899 const void *in, size_t inlen, char *resbuf)
901 hmac_sha1 (key, keylen, in, inlen, resbuf);
902 return GC_OK;
904 #endif
906 #ifdef GNULIB_GC_HMAC_SHA256
907 Gc_rc
908 gc_hmac_sha256 (const void *key, size_t keylen,
909 const void *in, size_t inlen, char *resbuf)
911 hmac_sha256 (key, keylen, in, inlen, resbuf);
912 return GC_OK;
914 #endif
916 #ifdef GNULIB_GC_HMAC_SHA512
917 Gc_rc
918 gc_hmac_sha512 (const void *key, size_t keylen,
919 const void *in, size_t inlen, char *resbuf)
921 hmac_sha512 (key, keylen, in, inlen, resbuf);
922 return GC_OK;
924 #endif