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. */
30 #ifdef GNULIB_GC_RANDOM
32 # include <sys/types.h>
33 # include <sys/stat.h>
51 #if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1) || defined(GNULIB_GC_HMAC_SHA256) || defined(GNULIB_GC_HMAC_SHA512)
56 #ifdef GNULIB_GC_ARCFOUR
59 #ifdef GNULIB_GC_ARCTWO
65 #ifdef GNULIB_GC_RIJNDAEL
66 # include "rijndael-api-fst.h"
69 #ifdef GNULIB_GC_RANDOM
70 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
72 # include <wincrypt.h>
73 HCRYPTPROV g_hProv
= 0;
74 # ifndef PROV_INTEL_SEC
75 # define PROV_INTEL_SEC 22
77 # ifndef CRYPT_VERIFY_CONTEXT
78 # define CRYPT_VERIFY_CONTEXT 0xF0000000
86 #ifdef GNULIB_GC_RANDOM
87 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
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
;
113 #ifdef GNULIB_GC_RANDOM
114 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
117 CryptReleaseContext (g_hProv
, 0);
126 #ifdef GNULIB_GC_RANDOM
131 randomize (int level
, char *data
, size_t datalen
)
133 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
135 return GC_RANDOM_ERROR
;
136 CryptGenRandom (g_hProv
, (DWORD
) datalen
, data
);
146 device
= NAME_OF_NONCE_DEVICE
;
150 device
= NAME_OF_PSEUDO_RANDOM_DEVICE
;
154 device
= NAME_OF_RANDOM_DEVICE
;
158 if (strcmp (device
, "no") == 0)
159 return GC_RANDOM_ERROR
;
161 fd
= open (device
, O_RDONLY
);
163 return GC_RANDOM_ERROR
;
169 tmp
= read (fd
, data
, datalen
);
173 int save_errno
= errno
;
176 return GC_RANDOM_ERROR
;
181 while (len
< datalen
);
185 return GC_RANDOM_ERROR
;
192 gc_nonce (char *data
, size_t datalen
)
194 return randomize (0, data
, datalen
);
198 gc_pseudo_random (char *data
, size_t datalen
)
200 return randomize (1, data
, datalen
);
204 gc_random (char *data
, size_t datalen
)
206 return randomize (2, data
, datalen
);
211 /* Memory allocation. */
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
)
224 typedef struct _gc_cipher_ctx
228 #ifdef GNULIB_GC_ARCTWO
229 arctwo_context arctwoContext
;
230 char arctwoIV
[ARCTWO_BLOCK_SIZE
];
232 #ifdef GNULIB_GC_ARCFOUR
233 arcfour_context arcfourContext
;
236 gl_des_ctx desContext
;
238 #ifdef GNULIB_GC_RIJNDAEL
239 rijndaelKeyInstance aesEncKey
;
240 rijndaelKeyInstance aesDecKey
;
241 rijndaelCipherInstance aesContext
;
246 gc_cipher_open (Gc_cipher alg
, Gc_cipher_mode mode
,
247 gc_cipher_handle
* outhandle
)
252 ctx
= calloc (sizeof (*ctx
), 1);
254 return GC_MALLOC_ERROR
;
261 #ifdef GNULIB_GC_ARCTWO
270 rc
= GC_INVALID_CIPHER
;
275 #ifdef GNULIB_GC_ARCFOUR
284 rc
= GC_INVALID_CIPHER
;
297 rc
= GC_INVALID_CIPHER
;
302 #ifdef GNULIB_GC_RIJNDAEL
313 rc
= GC_INVALID_CIPHER
;
319 rc
= GC_INVALID_CIPHER
;
331 gc_cipher_setkey (gc_cipher_handle handle
, size_t keylen
, const char *key
)
333 _gc_cipher_ctx
*ctx
= handle
;
337 #ifdef GNULIB_GC_ARCTWO
339 arctwo_setkey (&ctx
->arctwoContext
, keylen
, key
);
343 #ifdef GNULIB_GC_ARCFOUR
346 arcfour_setkey (&ctx
->arcfourContext
, key
, keylen
);
353 return GC_INVALID_CIPHER
;
354 gl_des_setkey (&ctx
->desContext
, key
);
358 #ifdef GNULIB_GC_RIJNDAEL
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
);
373 return GC_INVALID_CIPHER
;
375 rc
= rijndaelMakeKey (&ctx
->aesDecKey
, RIJNDAEL_DIR_DECRYPT
,
376 keylen
* 8, keyMaterial
);
378 return GC_INVALID_CIPHER
;
380 rc
= rijndaelCipherInit (&ctx
->aesContext
, RIJNDAEL_MODE_ECB
, NULL
);
382 return GC_INVALID_CIPHER
;
388 return GC_INVALID_CIPHER
;
395 gc_cipher_setiv (gc_cipher_handle handle
, size_t ivlen
, const char *iv
)
397 _gc_cipher_ctx
*ctx
= handle
;
401 #ifdef GNULIB_GC_ARCTWO
403 if (ivlen
!= ARCTWO_BLOCK_SIZE
)
404 return GC_INVALID_CIPHER
;
405 memcpy (ctx
->arctwoIV
, iv
, ivlen
);
409 #ifdef GNULIB_GC_RIJNDAEL
416 /* Doesn't use IV. */
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
,
431 return GC_INVALID_CIPHER
;
436 return GC_INVALID_CIPHER
;
442 return GC_INVALID_CIPHER
;
449 gc_cipher_encrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
451 _gc_cipher_ctx
*ctx
= handle
;
455 #ifdef GNULIB_GC_ARCTWO
460 arctwo_encrypt (&ctx
->arctwoContext
, data
, data
, len
);
464 for (; len
>= ARCTWO_BLOCK_SIZE
; len
-= ARCTWO_BLOCK_SIZE
,
465 data
+= ARCTWO_BLOCK_SIZE
)
468 for (i
= 0; i
< ARCTWO_BLOCK_SIZE
; i
++)
469 data
[i
] ^= ctx
->arctwoIV
[i
];
470 arctwo_encrypt (&ctx
->arctwoContext
, data
, data
,
472 memcpy (ctx
->arctwoIV
, data
, ARCTWO_BLOCK_SIZE
);
477 return GC_INVALID_CIPHER
;
482 #ifdef GNULIB_GC_ARCFOUR
485 arcfour_stream (&ctx
->arcfourContext
, data
, data
, len
);
491 for (; len
>= 8; len
-= 8, data
+= 8)
492 gl_des_ecb_encrypt (&ctx
->desContext
, data
, data
);
496 #ifdef GNULIB_GC_RIJNDAEL
503 nblocks
= rijndaelBlockEncrypt (&ctx
->aesContext
, &ctx
->aesEncKey
,
504 data
, 8 * len
, data
);
506 return GC_INVALID_CIPHER
;
512 return GC_INVALID_CIPHER
;
519 gc_cipher_decrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
521 _gc_cipher_ctx
*ctx
= handle
;
525 #ifdef GNULIB_GC_ARCTWO
530 arctwo_decrypt (&ctx
->arctwoContext
, data
, data
, len
);
534 for (; len
>= ARCTWO_BLOCK_SIZE
; len
-= ARCTWO_BLOCK_SIZE
,
535 data
+= ARCTWO_BLOCK_SIZE
)
537 char tmpIV
[ARCTWO_BLOCK_SIZE
];
539 memcpy (tmpIV
, data
, ARCTWO_BLOCK_SIZE
);
540 arctwo_decrypt (&ctx
->arctwoContext
, data
, data
,
542 for (i
= 0; i
< ARCTWO_BLOCK_SIZE
; i
++)
543 data
[i
] ^= ctx
->arctwoIV
[i
];
544 memcpy (ctx
->arctwoIV
, tmpIV
, ARCTWO_BLOCK_SIZE
);
549 return GC_INVALID_CIPHER
;
554 #ifdef GNULIB_GC_ARCFOUR
557 arcfour_stream (&ctx
->arcfourContext
, data
, data
, len
);
563 for (; len
>= 8; len
-= 8, data
+= 8)
564 gl_des_ecb_decrypt (&ctx
->desContext
, data
, data
);
568 #ifdef GNULIB_GC_RIJNDAEL
575 nblocks
= rijndaelBlockDecrypt (&ctx
->aesContext
, &ctx
->aesDecKey
,
576 data
, 8 * len
, data
);
578 return GC_INVALID_CIPHER
;
584 return GC_INVALID_CIPHER
;
591 gc_cipher_close (gc_cipher_handle handle
)
593 _gc_cipher_ctx
*ctx
= handle
;
602 #define MAX_DIGEST_SIZE 20
604 typedef struct _gc_hash_ctx
608 char hash
[MAX_DIGEST_SIZE
];
610 struct md2_ctx md2Context
;
613 struct md4_ctx md4Context
;
616 struct md5_ctx md5Context
;
618 #ifdef GNULIB_GC_SHA1
619 struct sha1_ctx sha1Context
;
624 gc_hash_open (Gc_hash hash
, Gc_hash_mode mode
, gc_hash_handle
* outhandle
)
630 return GC_INVALID_HASH
;
632 ctx
= calloc (sizeof (*ctx
), 1);
634 return GC_MALLOC_ERROR
;
643 md2_init_ctx (&ctx
->md2Context
);
649 md4_init_ctx (&ctx
->md4Context
);
655 md5_init_ctx (&ctx
->md5Context
);
659 #ifdef GNULIB_GC_SHA1
661 sha1_init_ctx (&ctx
->sha1Context
);
666 rc
= GC_INVALID_HASH
;
679 gc_hash_clone (gc_hash_handle handle
, gc_hash_handle
* outhandle
)
681 _gc_hash_ctx
*in
= handle
;
684 *outhandle
= out
= calloc (sizeof (*out
), 1);
686 return GC_MALLOC_ERROR
;
688 memcpy (out
, in
, sizeof (*out
));
694 gc_hash_digest_length (Gc_hash hash
)
701 len
= GC_MD2_DIGEST_SIZE
;
705 len
= GC_MD4_DIGEST_SIZE
;
709 len
= GC_MD5_DIGEST_SIZE
;
713 len
= GC_RMD160_DIGEST_SIZE
;
717 len
= GC_SHA1_DIGEST_SIZE
;
728 gc_hash_write (gc_hash_handle handle
, size_t len
, const char *data
)
730 _gc_hash_ctx
*ctx
= handle
;
736 md2_process_bytes (data
, len
, &ctx
->md2Context
);
742 md4_process_bytes (data
, len
, &ctx
->md4Context
);
748 md5_process_bytes (data
, len
, &ctx
->md5Context
);
752 #ifdef GNULIB_GC_SHA1
754 sha1_process_bytes (data
, len
, &ctx
->sha1Context
);
764 gc_hash_read (gc_hash_handle handle
)
766 _gc_hash_ctx
*ctx
= handle
;
767 const char *ret
= NULL
;
773 md2_finish_ctx (&ctx
->md2Context
, ctx
->hash
);
780 md4_finish_ctx (&ctx
->md4Context
, ctx
->hash
);
787 md5_finish_ctx (&ctx
->md5Context
, ctx
->hash
);
792 #ifdef GNULIB_GC_SHA1
794 sha1_finish_ctx (&ctx
->sha1Context
, ctx
->hash
);
807 gc_hash_close (gc_hash_handle handle
)
809 _gc_hash_ctx
*ctx
= handle
;
815 gc_hash_buffer (Gc_hash hash
, const void *in
, size_t inlen
, char *resbuf
)
821 md2_buffer (in
, inlen
, resbuf
);
827 md4_buffer (in
, inlen
, resbuf
);
833 md5_buffer (in
, inlen
, resbuf
);
837 #ifdef GNULIB_GC_SHA1
839 sha1_buffer (in
, inlen
, resbuf
);
844 return GC_INVALID_HASH
;
852 gc_md2 (const void *in
, size_t inlen
, void *resbuf
)
854 md2_buffer (in
, inlen
, resbuf
);
861 gc_md4 (const void *in
, size_t inlen
, void *resbuf
)
863 md4_buffer (in
, inlen
, resbuf
);
870 gc_md5 (const void *in
, size_t inlen
, void *resbuf
)
872 md5_buffer (in
, inlen
, resbuf
);
877 #ifdef GNULIB_GC_SHA1
879 gc_sha1 (const void *in
, size_t inlen
, void *resbuf
)
881 sha1_buffer (in
, inlen
, resbuf
);
886 #ifdef GNULIB_GC_HMAC_MD5
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
);
896 #ifdef GNULIB_GC_HMAC_SHA1
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
);
906 #ifdef GNULIB_GC_HMAC_SHA256
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
);
916 #ifdef GNULIB_GC_HMAC_SHA512
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
);