1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002-2020 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. */
32 # include <sys/types.h>
33 # include <sys/stat.h>
60 #if GNULIB_GC_HMAC_MD5 || GNULIB_GC_HMAC_SHA1 || GNULIB_GC_HMAC_SHA256 || GNULIB_GC_HMAC_SHA512
74 #if GNULIB_GC_RIJNDAEL
75 # include "rijndael-api-fst.h"
79 # if defined _WIN32 && ! defined __CYGWIN__
81 # include <wincrypt.h>
82 HCRYPTPROV g_hProv
= 0;
83 # ifndef PROV_INTEL_SEC
84 # define PROV_INTEL_SEC 22
86 # ifndef CRYPT_VERIFY_CONTEXT
87 # define CRYPT_VERIFY_CONTEXT 0xF0000000
92 #if defined _WIN32 && ! defined __CYGWIN__
93 /* Don't assume that UNICODE is not defined. */
94 # undef CryptAcquireContext
95 # define CryptAcquireContext CryptAcquireContextA
102 # if defined _WIN32 && ! defined __CYGWIN__
104 CryptReleaseContext (g_hProv
, 0);
106 /* There is no need to create a container for just random data, so
107 we can use CRYPT_VERIFY_CONTEXT (one call) see:
108 https://web.archive.org/web/20070314163712/http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */
110 /* We first try to use the Intel PIII RNG if drivers are present */
111 if (!CryptAcquireContext (&g_hProv
, NULL
, NULL
,
112 PROV_INTEL_SEC
, CRYPT_VERIFY_CONTEXT
))
114 /* not a PIII or no drivers available, use default RSA CSP */
115 if (!CryptAcquireContext (&g_hProv
, NULL
, NULL
,
116 PROV_RSA_FULL
, CRYPT_VERIFY_CONTEXT
))
117 return GC_RANDOM_ERROR
;
129 # if defined _WIN32 && ! defined __CYGWIN__
132 CryptReleaseContext (g_hProv
, 0);
146 randomize (int level
, char *data
, size_t datalen
)
148 #if defined _WIN32 && ! defined __CYGWIN__
150 return GC_RANDOM_ERROR
;
151 CryptGenRandom (g_hProv
, (DWORD
) datalen
, data
);
161 device
= NAME_OF_NONCE_DEVICE
;
165 device
= NAME_OF_PSEUDO_RANDOM_DEVICE
;
169 device
= NAME_OF_RANDOM_DEVICE
;
173 if (strcmp (device
, "no") == 0)
174 return GC_RANDOM_ERROR
;
176 fd
= open (device
, O_RDONLY
| O_CLOEXEC
);
178 return GC_RANDOM_ERROR
;
184 tmp
= read (fd
, data
, datalen
);
188 int save_errno
= errno
;
191 return GC_RANDOM_ERROR
;
196 while (len
< datalen
);
200 return GC_RANDOM_ERROR
;
207 gc_nonce (char *data
, size_t datalen
)
209 return randomize (0, data
, datalen
);
213 gc_pseudo_random (char *data
, size_t datalen
)
215 return randomize (1, data
, datalen
);
219 gc_random (char *data
, size_t datalen
)
221 return randomize (2, data
, datalen
);
226 /* Memory allocation. */
229 gc_set_allocators (gc_malloc_t func_malloc
,
230 gc_malloc_t secure_malloc
,
231 gc_secure_check_t secure_check
,
232 gc_realloc_t func_realloc
, gc_free_t func_free
)
239 typedef struct _gc_cipher_ctx
244 arctwo_context arctwoContext
;
245 char arctwoIV
[ARCTWO_BLOCK_SIZE
];
247 #if GNULIB_GC_ARCFOUR
248 arcfour_context arcfourContext
;
251 gl_des_ctx desContext
;
253 #if GNULIB_GC_RIJNDAEL
254 rijndaelKeyInstance aesEncKey
;
255 rijndaelKeyInstance aesDecKey
;
256 rijndaelCipherInstance aesContext
;
261 gc_cipher_open (Gc_cipher alg
, Gc_cipher_mode mode
,
262 gc_cipher_handle
* outhandle
)
267 ctx
= calloc (sizeof (*ctx
), 1);
269 return GC_MALLOC_ERROR
;
285 rc
= GC_INVALID_CIPHER
;
290 #if GNULIB_GC_ARCFOUR
299 rc
= GC_INVALID_CIPHER
;
312 rc
= GC_INVALID_CIPHER
;
317 #if GNULIB_GC_RIJNDAEL
328 rc
= GC_INVALID_CIPHER
;
334 rc
= GC_INVALID_CIPHER
;
346 gc_cipher_setkey (gc_cipher_handle handle
, size_t keylen
, const char *key
)
348 _gc_cipher_ctx
*ctx
= handle
;
354 arctwo_setkey (&ctx
->arctwoContext
, keylen
, key
);
358 #if GNULIB_GC_ARCFOUR
361 arcfour_setkey (&ctx
->arcfourContext
, key
, keylen
);
368 return GC_INVALID_CIPHER
;
369 gl_des_setkey (&ctx
->desContext
, key
);
373 #if GNULIB_GC_RIJNDAEL
380 char keyMaterial
[RIJNDAEL_MAX_KEY_SIZE
+ 1];
382 for (i
= 0; i
< keylen
; i
++)
383 sprintf (&keyMaterial
[2 * i
], "%02x", key
[i
] & 0xFF);
385 rc
= rijndaelMakeKey (&ctx
->aesEncKey
, RIJNDAEL_DIR_ENCRYPT
,
386 keylen
* 8, keyMaterial
);
388 return GC_INVALID_CIPHER
;
390 rc
= rijndaelMakeKey (&ctx
->aesDecKey
, RIJNDAEL_DIR_DECRYPT
,
391 keylen
* 8, keyMaterial
);
393 return GC_INVALID_CIPHER
;
395 rc
= rijndaelCipherInit (&ctx
->aesContext
, RIJNDAEL_MODE_ECB
, NULL
);
397 return GC_INVALID_CIPHER
;
403 return GC_INVALID_CIPHER
;
410 gc_cipher_setiv (gc_cipher_handle handle
, size_t ivlen
, const char *iv
)
412 _gc_cipher_ctx
*ctx
= handle
;
418 if (ivlen
!= ARCTWO_BLOCK_SIZE
)
419 return GC_INVALID_CIPHER
;
420 memcpy (ctx
->arctwoIV
, iv
, ivlen
);
424 #if GNULIB_GC_RIJNDAEL
431 /* Doesn't use IV. */
438 char ivMaterial
[2 * RIJNDAEL_MAX_IV_SIZE
+ 1];
440 for (i
= 0; i
< ivlen
; i
++)
441 sprintf (&ivMaterial
[2 * i
], "%02x", iv
[i
] & 0xFF);
443 rc
= rijndaelCipherInit (&ctx
->aesContext
, RIJNDAEL_MODE_CBC
,
446 return GC_INVALID_CIPHER
;
451 return GC_INVALID_CIPHER
;
457 return GC_INVALID_CIPHER
;
464 gc_cipher_encrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
466 _gc_cipher_ctx
*ctx
= handle
;
475 arctwo_encrypt (&ctx
->arctwoContext
, data
, data
, len
);
479 for (; len
>= ARCTWO_BLOCK_SIZE
; len
-= ARCTWO_BLOCK_SIZE
,
480 data
+= ARCTWO_BLOCK_SIZE
)
483 for (i
= 0; i
< ARCTWO_BLOCK_SIZE
; i
++)
484 data
[i
] ^= ctx
->arctwoIV
[i
];
485 arctwo_encrypt (&ctx
->arctwoContext
, data
, data
,
487 memcpy (ctx
->arctwoIV
, data
, ARCTWO_BLOCK_SIZE
);
492 return GC_INVALID_CIPHER
;
497 #if GNULIB_GC_ARCFOUR
500 arcfour_stream (&ctx
->arcfourContext
, data
, data
, len
);
506 for (; len
>= 8; len
-= 8, data
+= 8)
507 gl_des_ecb_encrypt (&ctx
->desContext
, data
, data
);
511 #if GNULIB_GC_RIJNDAEL
518 nblocks
= rijndaelBlockEncrypt (&ctx
->aesContext
, &ctx
->aesEncKey
,
519 data
, 8 * len
, data
);
521 return GC_INVALID_CIPHER
;
527 return GC_INVALID_CIPHER
;
534 gc_cipher_decrypt_inline (gc_cipher_handle handle
, size_t len
, char *data
)
536 _gc_cipher_ctx
*ctx
= handle
;
545 arctwo_decrypt (&ctx
->arctwoContext
, data
, data
, len
);
549 for (; len
>= ARCTWO_BLOCK_SIZE
; len
-= ARCTWO_BLOCK_SIZE
,
550 data
+= ARCTWO_BLOCK_SIZE
)
552 char tmpIV
[ARCTWO_BLOCK_SIZE
];
554 memcpy (tmpIV
, data
, ARCTWO_BLOCK_SIZE
);
555 arctwo_decrypt (&ctx
->arctwoContext
, data
, data
,
557 for (i
= 0; i
< ARCTWO_BLOCK_SIZE
; i
++)
558 data
[i
] ^= ctx
->arctwoIV
[i
];
559 memcpy (ctx
->arctwoIV
, tmpIV
, ARCTWO_BLOCK_SIZE
);
564 return GC_INVALID_CIPHER
;
569 #if GNULIB_GC_ARCFOUR
572 arcfour_stream (&ctx
->arcfourContext
, data
, data
, len
);
578 for (; len
>= 8; len
-= 8, data
+= 8)
579 gl_des_ecb_decrypt (&ctx
->desContext
, data
, data
);
583 #if GNULIB_GC_RIJNDAEL
590 nblocks
= rijndaelBlockDecrypt (&ctx
->aesContext
, &ctx
->aesDecKey
,
591 data
, 8 * len
, data
);
593 return GC_INVALID_CIPHER
;
599 return GC_INVALID_CIPHER
;
606 gc_cipher_close (gc_cipher_handle handle
)
608 _gc_cipher_ctx
*ctx
= handle
;
617 #define MAX_DIGEST_SIZE 64
619 typedef struct _gc_hash_ctx
623 char hash
[MAX_DIGEST_SIZE
];
625 struct md2_ctx md2Context
;
628 struct md4_ctx md4Context
;
631 struct md5_ctx md5Context
;
634 struct sha1_ctx sha1Context
;
637 struct sha256_ctx sha256Context
;
640 struct sha512_ctx sha512Context
;
643 struct sm3_ctx sm3Context
;
648 gc_hash_open (Gc_hash hash
, Gc_hash_mode mode
, gc_hash_handle
* outhandle
)
654 return GC_INVALID_HASH
;
656 ctx
= calloc (sizeof (*ctx
), 1);
658 return GC_MALLOC_ERROR
;
667 /* Not needed, because ctx is already zero-initialized. */
668 /*md2_init_ctx (&ctx->md2Context);*/
674 md4_init_ctx (&ctx
->md4Context
);
680 md5_init_ctx (&ctx
->md5Context
);
686 sha1_init_ctx (&ctx
->sha1Context
);
692 sha256_init_ctx (&ctx
->sha256Context
);
698 sha512_init_ctx (&ctx
->sha512Context
);
704 sm3_init_ctx (&ctx
->sm3Context
);
709 rc
= GC_INVALID_HASH
;
722 gc_hash_clone (gc_hash_handle handle
, gc_hash_handle
* outhandle
)
724 _gc_hash_ctx
*in
= handle
;
727 *outhandle
= out
= calloc (sizeof (*out
), 1);
729 return GC_MALLOC_ERROR
;
731 memcpy (out
, in
, sizeof (*out
));
737 gc_hash_digest_length (Gc_hash hash
)
744 len
= GC_MD2_DIGEST_SIZE
;
748 len
= GC_MD4_DIGEST_SIZE
;
752 len
= GC_MD5_DIGEST_SIZE
;
756 len
= GC_RMD160_DIGEST_SIZE
;
760 len
= GC_SHA1_DIGEST_SIZE
;
764 len
= GC_SHA256_DIGEST_SIZE
;
768 len
= GC_SHA512_DIGEST_SIZE
;
772 len
= GC_SM3_DIGEST_SIZE
;
783 gc_hash_write (gc_hash_handle handle
, size_t len
, const char *data
)
785 _gc_hash_ctx
*ctx
= handle
;
791 md2_process_bytes (data
, len
, &ctx
->md2Context
);
797 md4_process_bytes (data
, len
, &ctx
->md4Context
);
803 md5_process_bytes (data
, len
, &ctx
->md5Context
);
809 sha1_process_bytes (data
, len
, &ctx
->sha1Context
);
815 sha256_process_bytes (data
, len
, &ctx
->sha256Context
);
821 sha512_process_bytes (data
, len
, &ctx
->sha512Context
);
827 sm3_process_bytes (data
, len
, &ctx
->sm3Context
);
837 gc_hash_read (gc_hash_handle handle
)
839 _gc_hash_ctx
*ctx
= handle
;
840 const char *ret
= NULL
;
846 md2_finish_ctx (&ctx
->md2Context
, ctx
->hash
);
853 md4_finish_ctx (&ctx
->md4Context
, ctx
->hash
);
860 md5_finish_ctx (&ctx
->md5Context
, ctx
->hash
);
867 sha1_finish_ctx (&ctx
->sha1Context
, ctx
->hash
);
874 sha256_finish_ctx (&ctx
->sha256Context
, ctx
->hash
);
881 sha512_finish_ctx (&ctx
->sha512Context
, ctx
->hash
);
888 sm3_finish_ctx (&ctx
->sm3Context
, ctx
->hash
);
901 gc_hash_close (gc_hash_handle handle
)
903 _gc_hash_ctx
*ctx
= handle
;
909 gc_hash_buffer (Gc_hash hash
, const void *in
, size_t inlen
, char *resbuf
)
915 md2_buffer (in
, inlen
, resbuf
);
921 md4_buffer (in
, inlen
, resbuf
);
927 md5_buffer (in
, inlen
, resbuf
);
933 sha1_buffer (in
, inlen
, resbuf
);
939 sha256_buffer (in
, inlen
, resbuf
);
945 sha512_buffer (in
, inlen
, resbuf
);
951 sm3_buffer (in
, inlen
, resbuf
);
956 return GC_INVALID_HASH
;
964 gc_md2 (const void *in
, size_t inlen
, void *resbuf
)
966 md2_buffer (in
, inlen
, resbuf
);
973 gc_md4 (const void *in
, size_t inlen
, void *resbuf
)
975 md4_buffer (in
, inlen
, resbuf
);
982 gc_md5 (const void *in
, size_t inlen
, void *resbuf
)
984 md5_buffer (in
, inlen
, resbuf
);
991 gc_sha1 (const void *in
, size_t inlen
, void *resbuf
)
993 sha1_buffer (in
, inlen
, resbuf
);
1000 gc_sha256 (const void *in
, size_t inlen
, void *resbuf
)
1002 sha256_buffer (in
, inlen
, resbuf
);
1007 #if GNULIB_GC_SHA512
1009 gc_sha512 (const void *in
, size_t inlen
, void *resbuf
)
1011 sha512_buffer (in
, inlen
, resbuf
);
1018 gc_sm3 (const void *in
, size_t inlen
, void *resbuf
)
1020 sm3_buffer (in
, inlen
, resbuf
);
1025 #if GNULIB_GC_HMAC_MD5
1027 gc_hmac_md5 (const void *key
, size_t keylen
,
1028 const void *in
, size_t inlen
, char *resbuf
)
1030 hmac_md5 (key
, keylen
, in
, inlen
, resbuf
);
1035 #if GNULIB_GC_HMAC_SHA1
1037 gc_hmac_sha1 (const void *key
, size_t keylen
,
1038 const void *in
, size_t inlen
, char *resbuf
)
1040 hmac_sha1 (key
, keylen
, in
, inlen
, resbuf
);
1045 #if GNULIB_GC_HMAC_SHA256
1047 gc_hmac_sha256 (const void *key
, size_t keylen
,
1048 const void *in
, size_t inlen
, char *resbuf
)
1050 hmac_sha256 (key
, keylen
, in
, inlen
, resbuf
);
1055 #if GNULIB_GC_HMAC_SHA512
1057 gc_hmac_sha512 (const void *key
, size_t keylen
,
1058 const void *in
, size_t inlen
, char *resbuf
)
1060 hmac_sha512 (key
, keylen
, in
, inlen
, resbuf
);