findprog-in: Relicense under LGPLv2+.
[gnulib.git] / lib / gc-gnulib.c
blob84f42e43f8ecb79913c8b0df3cecf00e7cd8174d
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. */
21 #include <config.h>
23 /* Get prototype. */
24 #include "gc.h"
26 #include <stdlib.h>
27 #include <string.h>
29 /* For randomize. */
30 #if 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 #if GNULIB_GC_MD2
40 # include "md2.h"
41 #endif
42 #if GNULIB_GC_MD4
43 # include "md4.h"
44 #endif
45 #if GNULIB_GC_MD5
46 # include "md5.h"
47 #endif
48 #if GNULIB_GC_SHA1
49 # include "sha1.h"
50 #endif
51 #if GNULIB_GC_SHA256
52 # include "sha256.h"
53 #endif
54 #if GNULIB_GC_SHA512
55 # include "sha512.h"
56 #endif
57 #if GNULIB_GC_SM3
58 # include "sm3.h"
59 #endif
60 #if GNULIB_GC_HMAC_MD5 || GNULIB_GC_HMAC_SHA1 || GNULIB_GC_HMAC_SHA256 || GNULIB_GC_HMAC_SHA512
61 # include "hmac.h"
62 #endif
64 /* Ciphers. */
65 #if GNULIB_GC_ARCFOUR
66 # include "arcfour.h"
67 #endif
68 #if GNULIB_GC_ARCTWO
69 # include "arctwo.h"
70 #endif
71 #if GNULIB_GC_DES
72 # include "des.h"
73 #endif
74 #if GNULIB_GC_RIJNDAEL
75 # include "rijndael-api-fst.h"
76 #endif
78 #if GNULIB_GC_RANDOM
79 # if defined _WIN32 && ! defined __CYGWIN__
80 # include <windows.h>
81 # include <wincrypt.h>
82 HCRYPTPROV g_hProv = 0;
83 # ifndef PROV_INTEL_SEC
84 # define PROV_INTEL_SEC 22
85 # endif
86 # ifndef CRYPT_VERIFY_CONTEXT
87 # define CRYPT_VERIFY_CONTEXT 0xF0000000
88 # endif
89 # endif
90 #endif
92 #if defined _WIN32 && ! defined __CYGWIN__
93 /* Don't assume that UNICODE is not defined. */
94 # undef CryptAcquireContext
95 # define CryptAcquireContext CryptAcquireContextA
96 #endif
98 Gc_rc
99 gc_init (void)
101 #if GNULIB_GC_RANDOM
102 # if defined _WIN32 && ! defined __CYGWIN__
103 if (g_hProv)
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;
119 # endif
120 #endif
122 return GC_OK;
125 void
126 gc_done (void)
128 #if GNULIB_GC_RANDOM
129 # if defined _WIN32 && ! defined __CYGWIN__
130 if (g_hProv)
132 CryptReleaseContext (g_hProv, 0);
133 g_hProv = 0;
135 # endif
136 #endif
138 return;
141 #if GNULIB_GC_RANDOM
143 /* Randomness. */
145 static Gc_rc
146 randomize (int level, char *data, size_t datalen)
148 #if defined _WIN32 && ! defined __CYGWIN__
149 if (!g_hProv)
150 return GC_RANDOM_ERROR;
151 CryptGenRandom (g_hProv, (DWORD) datalen, data);
152 #else
153 int fd;
154 const char *device;
155 size_t len = 0;
156 int rc;
158 switch (level)
160 case 0:
161 device = NAME_OF_NONCE_DEVICE;
162 break;
164 case 1:
165 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
166 break;
168 default:
169 device = NAME_OF_RANDOM_DEVICE;
170 break;
173 if (strcmp (device, "no") == 0)
174 return GC_RANDOM_ERROR;
176 fd = open (device, O_RDONLY | O_CLOEXEC);
177 if (fd < 0)
178 return GC_RANDOM_ERROR;
182 ssize_t tmp;
184 tmp = read (fd, data, datalen);
186 if (tmp < 0)
188 int save_errno = errno;
189 close (fd);
190 errno = save_errno;
191 return GC_RANDOM_ERROR;
194 len += tmp;
196 while (len < datalen);
198 rc = close (fd);
199 if (rc < 0)
200 return GC_RANDOM_ERROR;
201 #endif
203 return GC_OK;
206 Gc_rc
207 gc_nonce (char *data, size_t datalen)
209 return randomize (0, data, datalen);
212 Gc_rc
213 gc_pseudo_random (char *data, size_t datalen)
215 return randomize (1, data, datalen);
218 Gc_rc
219 gc_random (char *data, size_t datalen)
221 return randomize (2, data, datalen);
224 #endif
226 /* Memory allocation. */
228 void
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)
234 return;
237 /* Ciphers. */
239 typedef struct _gc_cipher_ctx
241 Gc_cipher alg;
242 Gc_cipher_mode mode;
243 #if GNULIB_GC_ARCTWO
244 arctwo_context arctwoContext;
245 char arctwoIV[ARCTWO_BLOCK_SIZE];
246 #endif
247 #if GNULIB_GC_ARCFOUR
248 arcfour_context arcfourContext;
249 #endif
250 #if GNULIB_GC_DES
251 gl_des_ctx desContext;
252 #endif
253 #if GNULIB_GC_RIJNDAEL
254 rijndaelKeyInstance aesEncKey;
255 rijndaelKeyInstance aesDecKey;
256 rijndaelCipherInstance aesContext;
257 #endif
258 } _gc_cipher_ctx;
260 Gc_rc
261 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
262 gc_cipher_handle * outhandle)
264 _gc_cipher_ctx *ctx;
265 Gc_rc rc = GC_OK;
267 ctx = calloc (sizeof (*ctx), 1);
268 if (!ctx)
269 return GC_MALLOC_ERROR;
271 ctx->alg = alg;
272 ctx->mode = mode;
274 switch (alg)
276 #if GNULIB_GC_ARCTWO
277 case GC_ARCTWO40:
278 switch (mode)
280 case GC_ECB:
281 case GC_CBC:
282 break;
284 default:
285 rc = GC_INVALID_CIPHER;
287 break;
288 #endif
290 #if GNULIB_GC_ARCFOUR
291 case GC_ARCFOUR128:
292 case GC_ARCFOUR40:
293 switch (mode)
295 case GC_STREAM:
296 break;
298 default:
299 rc = GC_INVALID_CIPHER;
301 break;
302 #endif
304 #if GNULIB_GC_DES
305 case GC_DES:
306 switch (mode)
308 case GC_ECB:
309 break;
311 default:
312 rc = GC_INVALID_CIPHER;
314 break;
315 #endif
317 #if GNULIB_GC_RIJNDAEL
318 case GC_AES128:
319 case GC_AES192:
320 case GC_AES256:
321 switch (mode)
323 case GC_ECB:
324 case GC_CBC:
325 break;
327 default:
328 rc = GC_INVALID_CIPHER;
330 break;
331 #endif
333 default:
334 rc = GC_INVALID_CIPHER;
337 if (rc == GC_OK)
338 *outhandle = ctx;
339 else
340 free (ctx);
342 return rc;
345 Gc_rc
346 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
348 _gc_cipher_ctx *ctx = handle;
350 switch (ctx->alg)
352 #if GNULIB_GC_ARCTWO
353 case GC_ARCTWO40:
354 arctwo_setkey (&ctx->arctwoContext, keylen, key);
355 break;
356 #endif
358 #if GNULIB_GC_ARCFOUR
359 case GC_ARCFOUR128:
360 case GC_ARCFOUR40:
361 arcfour_setkey (&ctx->arcfourContext, key, keylen);
362 break;
363 #endif
365 #if GNULIB_GC_DES
366 case GC_DES:
367 if (keylen != 8)
368 return GC_INVALID_CIPHER;
369 gl_des_setkey (&ctx->desContext, key);
370 break;
371 #endif
373 #if GNULIB_GC_RIJNDAEL
374 case GC_AES128:
375 case GC_AES192:
376 case GC_AES256:
378 rijndael_rc rc;
379 size_t i;
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);
387 if (rc < 0)
388 return GC_INVALID_CIPHER;
390 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
391 keylen * 8, keyMaterial);
392 if (rc < 0)
393 return GC_INVALID_CIPHER;
395 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
396 if (rc < 0)
397 return GC_INVALID_CIPHER;
399 break;
400 #endif
402 default:
403 return GC_INVALID_CIPHER;
406 return GC_OK;
409 Gc_rc
410 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
412 _gc_cipher_ctx *ctx = handle;
414 switch (ctx->alg)
416 #if GNULIB_GC_ARCTWO
417 case GC_ARCTWO40:
418 if (ivlen != ARCTWO_BLOCK_SIZE)
419 return GC_INVALID_CIPHER;
420 memcpy (ctx->arctwoIV, iv, ivlen);
421 break;
422 #endif
424 #if GNULIB_GC_RIJNDAEL
425 case GC_AES128:
426 case GC_AES192:
427 case GC_AES256:
428 switch (ctx->mode)
430 case GC_ECB:
431 /* Doesn't use IV. */
432 break;
434 case GC_CBC:
436 rijndael_rc rc;
437 size_t i;
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,
444 ivMaterial);
445 if (rc < 0)
446 return GC_INVALID_CIPHER;
448 break;
450 default:
451 return GC_INVALID_CIPHER;
453 break;
454 #endif
456 default:
457 return GC_INVALID_CIPHER;
460 return GC_OK;
463 Gc_rc
464 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
466 _gc_cipher_ctx *ctx = handle;
468 switch (ctx->alg)
470 #if GNULIB_GC_ARCTWO
471 case GC_ARCTWO40:
472 switch (ctx->mode)
474 case GC_ECB:
475 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
476 break;
478 case GC_CBC:
479 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
480 data += ARCTWO_BLOCK_SIZE)
482 size_t i;
483 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
484 data[i] ^= ctx->arctwoIV[i];
485 arctwo_encrypt (&ctx->arctwoContext, data, data,
486 ARCTWO_BLOCK_SIZE);
487 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
489 break;
491 default:
492 return GC_INVALID_CIPHER;
494 break;
495 #endif
497 #if GNULIB_GC_ARCFOUR
498 case GC_ARCFOUR128:
499 case GC_ARCFOUR40:
500 arcfour_stream (&ctx->arcfourContext, data, data, len);
501 break;
502 #endif
504 #if GNULIB_GC_DES
505 case GC_DES:
506 for (; len >= 8; len -= 8, data += 8)
507 gl_des_ecb_encrypt (&ctx->desContext, data, data);
508 break;
509 #endif
511 #if GNULIB_GC_RIJNDAEL
512 case GC_AES128:
513 case GC_AES192:
514 case GC_AES256:
516 int nblocks;
518 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
519 data, 8 * len, data);
520 if (nblocks < 0)
521 return GC_INVALID_CIPHER;
523 break;
524 #endif
526 default:
527 return GC_INVALID_CIPHER;
530 return GC_OK;
533 Gc_rc
534 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
536 _gc_cipher_ctx *ctx = handle;
538 switch (ctx->alg)
540 #if GNULIB_GC_ARCTWO
541 case GC_ARCTWO40:
542 switch (ctx->mode)
544 case GC_ECB:
545 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
546 break;
548 case GC_CBC:
549 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
550 data += ARCTWO_BLOCK_SIZE)
552 char tmpIV[ARCTWO_BLOCK_SIZE];
553 size_t i;
554 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
555 arctwo_decrypt (&ctx->arctwoContext, data, data,
556 ARCTWO_BLOCK_SIZE);
557 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
558 data[i] ^= ctx->arctwoIV[i];
559 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
561 break;
563 default:
564 return GC_INVALID_CIPHER;
566 break;
567 #endif
569 #if GNULIB_GC_ARCFOUR
570 case GC_ARCFOUR128:
571 case GC_ARCFOUR40:
572 arcfour_stream (&ctx->arcfourContext, data, data, len);
573 break;
574 #endif
576 #if GNULIB_GC_DES
577 case GC_DES:
578 for (; len >= 8; len -= 8, data += 8)
579 gl_des_ecb_decrypt (&ctx->desContext, data, data);
580 break;
581 #endif
583 #if GNULIB_GC_RIJNDAEL
584 case GC_AES128:
585 case GC_AES192:
586 case GC_AES256:
588 int nblocks;
590 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
591 data, 8 * len, data);
592 if (nblocks < 0)
593 return GC_INVALID_CIPHER;
595 break;
596 #endif
598 default:
599 return GC_INVALID_CIPHER;
602 return GC_OK;
605 Gc_rc
606 gc_cipher_close (gc_cipher_handle handle)
608 _gc_cipher_ctx *ctx = handle;
610 free (ctx);
612 return GC_OK;
615 /* Hashes. */
617 #define MAX_DIGEST_SIZE 64
619 typedef struct _gc_hash_ctx
621 Gc_hash alg;
622 Gc_hash_mode mode;
623 char hash[MAX_DIGEST_SIZE];
624 #if GNULIB_GC_MD2
625 struct md2_ctx md2Context;
626 #endif
627 #if GNULIB_GC_MD4
628 struct md4_ctx md4Context;
629 #endif
630 #if GNULIB_GC_MD5
631 struct md5_ctx md5Context;
632 #endif
633 #if GNULIB_GC_SHA1
634 struct sha1_ctx sha1Context;
635 #endif
636 #if GNULIB_GC_SHA256
637 struct sha256_ctx sha256Context;
638 #endif
639 #if GNULIB_GC_SHA512
640 struct sha512_ctx sha512Context;
641 #endif
642 #if GNULIB_GC_SM3
643 struct sm3_ctx sm3Context;
644 #endif
645 } _gc_hash_ctx;
647 Gc_rc
648 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
650 _gc_hash_ctx *ctx;
651 Gc_rc rc = GC_OK;
653 if (mode != 0)
654 return GC_INVALID_HASH;
656 ctx = calloc (sizeof (*ctx), 1);
657 if (!ctx)
658 return GC_MALLOC_ERROR;
660 ctx->alg = hash;
661 ctx->mode = mode;
663 switch (hash)
665 #if GNULIB_GC_MD2
666 case GC_MD2:
667 /* Not needed, because ctx is already zero-initialized. */
668 /*md2_init_ctx (&ctx->md2Context);*/
669 break;
670 #endif
672 #if GNULIB_GC_MD4
673 case GC_MD4:
674 md4_init_ctx (&ctx->md4Context);
675 break;
676 #endif
678 #if GNULIB_GC_MD5
679 case GC_MD5:
680 md5_init_ctx (&ctx->md5Context);
681 break;
682 #endif
684 #if GNULIB_GC_SHA1
685 case GC_SHA1:
686 sha1_init_ctx (&ctx->sha1Context);
687 break;
688 #endif
690 #if GNULIB_GC_SHA256
691 case GC_SHA256:
692 sha256_init_ctx (&ctx->sha256Context);
693 break;
694 #endif
696 #if GNULIB_GC_SHA512
697 case GC_SHA512:
698 sha512_init_ctx (&ctx->sha512Context);
699 break;
700 #endif
702 #if GNULIB_GC_SM3
703 case GC_SM3:
704 sm3_init_ctx (&ctx->sm3Context);
705 break;
706 #endif
708 default:
709 rc = GC_INVALID_HASH;
710 break;
713 if (rc == GC_OK)
714 *outhandle = ctx;
715 else
716 free (ctx);
718 return rc;
721 Gc_rc
722 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
724 _gc_hash_ctx *in = handle;
725 _gc_hash_ctx *out;
727 *outhandle = out = calloc (sizeof (*out), 1);
728 if (!out)
729 return GC_MALLOC_ERROR;
731 memcpy (out, in, sizeof (*out));
733 return GC_OK;
736 size_t
737 gc_hash_digest_length (Gc_hash hash)
739 size_t len;
741 switch (hash)
743 case GC_MD2:
744 len = GC_MD2_DIGEST_SIZE;
745 break;
747 case GC_MD4:
748 len = GC_MD4_DIGEST_SIZE;
749 break;
751 case GC_MD5:
752 len = GC_MD5_DIGEST_SIZE;
753 break;
755 case GC_RMD160:
756 len = GC_RMD160_DIGEST_SIZE;
757 break;
759 case GC_SHA1:
760 len = GC_SHA1_DIGEST_SIZE;
761 break;
763 case GC_SHA256:
764 len = GC_SHA256_DIGEST_SIZE;
765 break;
767 case GC_SHA512:
768 len = GC_SHA512_DIGEST_SIZE;
769 break;
771 case GC_SM3:
772 len = GC_SM3_DIGEST_SIZE;
773 break;
775 default:
776 return 0;
779 return len;
782 void
783 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
785 _gc_hash_ctx *ctx = handle;
787 switch (ctx->alg)
789 #if GNULIB_GC_MD2
790 case GC_MD2:
791 md2_process_bytes (data, len, &ctx->md2Context);
792 break;
793 #endif
795 #if GNULIB_GC_MD4
796 case GC_MD4:
797 md4_process_bytes (data, len, &ctx->md4Context);
798 break;
799 #endif
801 #if GNULIB_GC_MD5
802 case GC_MD5:
803 md5_process_bytes (data, len, &ctx->md5Context);
804 break;
805 #endif
807 #if GNULIB_GC_SHA1
808 case GC_SHA1:
809 sha1_process_bytes (data, len, &ctx->sha1Context);
810 break;
811 #endif
813 #if GNULIB_GC_SHA256
814 case GC_SHA256:
815 sha256_process_bytes (data, len, &ctx->sha256Context);
816 break;
817 #endif
819 #if GNULIB_GC_SHA512
820 case GC_SHA512:
821 sha512_process_bytes (data, len, &ctx->sha512Context);
822 break;
823 #endif
825 #if GNULIB_GC_SM3
826 case GC_SM3:
827 sm3_process_bytes (data, len, &ctx->sm3Context);
828 break;
829 #endif
831 default:
832 break;
836 const char *
837 gc_hash_read (gc_hash_handle handle)
839 _gc_hash_ctx *ctx = handle;
840 const char *ret = NULL;
842 switch (ctx->alg)
844 #if GNULIB_GC_MD2
845 case GC_MD2:
846 md2_finish_ctx (&ctx->md2Context, ctx->hash);
847 ret = ctx->hash;
848 break;
849 #endif
851 #if GNULIB_GC_MD4
852 case GC_MD4:
853 md4_finish_ctx (&ctx->md4Context, ctx->hash);
854 ret = ctx->hash;
855 break;
856 #endif
858 #if GNULIB_GC_MD5
859 case GC_MD5:
860 md5_finish_ctx (&ctx->md5Context, ctx->hash);
861 ret = ctx->hash;
862 break;
863 #endif
865 #if GNULIB_GC_SHA1
866 case GC_SHA1:
867 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
868 ret = ctx->hash;
869 break;
870 #endif
872 #if GNULIB_GC_SHA256
873 case GC_SHA256:
874 sha256_finish_ctx (&ctx->sha256Context, ctx->hash);
875 ret = ctx->hash;
876 break;
877 #endif
879 #if GNULIB_GC_SHA512
880 case GC_SHA512:
881 sha512_finish_ctx (&ctx->sha512Context, ctx->hash);
882 ret = ctx->hash;
883 break;
884 #endif
886 #if GNULIB_GC_SM3
887 case GC_SM3:
888 sm3_finish_ctx (&ctx->sm3Context, ctx->hash);
889 ret = ctx->hash;
890 break;
891 #endif
893 default:
894 return NULL;
897 return ret;
900 void
901 gc_hash_close (gc_hash_handle handle)
903 _gc_hash_ctx *ctx = handle;
905 free (ctx);
908 Gc_rc
909 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
911 switch (hash)
913 #if GNULIB_GC_MD2
914 case GC_MD2:
915 md2_buffer (in, inlen, resbuf);
916 break;
917 #endif
919 #if GNULIB_GC_MD4
920 case GC_MD4:
921 md4_buffer (in, inlen, resbuf);
922 break;
923 #endif
925 #if GNULIB_GC_MD5
926 case GC_MD5:
927 md5_buffer (in, inlen, resbuf);
928 break;
929 #endif
931 #if GNULIB_GC_SHA1
932 case GC_SHA1:
933 sha1_buffer (in, inlen, resbuf);
934 break;
935 #endif
937 #if GNULIB_GC_SHA256
938 case GC_SHA256:
939 sha256_buffer (in, inlen, resbuf);
940 break;
941 #endif
943 #if GNULIB_GC_SHA512
944 case GC_SHA512:
945 sha512_buffer (in, inlen, resbuf);
946 break;
947 #endif
949 #if GNULIB_GC_SM3
950 case GC_SM3:
951 sm3_buffer (in, inlen, resbuf);
952 break;
953 #endif
955 default:
956 return GC_INVALID_HASH;
959 return GC_OK;
962 #if GNULIB_GC_MD2
963 Gc_rc
964 gc_md2 (const void *in, size_t inlen, void *resbuf)
966 md2_buffer (in, inlen, resbuf);
967 return GC_OK;
969 #endif
971 #if GNULIB_GC_MD4
972 Gc_rc
973 gc_md4 (const void *in, size_t inlen, void *resbuf)
975 md4_buffer (in, inlen, resbuf);
976 return GC_OK;
978 #endif
980 #if GNULIB_GC_MD5
981 Gc_rc
982 gc_md5 (const void *in, size_t inlen, void *resbuf)
984 md5_buffer (in, inlen, resbuf);
985 return GC_OK;
987 #endif
989 #if GNULIB_GC_SHA1
990 Gc_rc
991 gc_sha1 (const void *in, size_t inlen, void *resbuf)
993 sha1_buffer (in, inlen, resbuf);
994 return GC_OK;
996 #endif
998 #if GNULIB_GC_SHA256
999 Gc_rc
1000 gc_sha256 (const void *in, size_t inlen, void *resbuf)
1002 sha256_buffer (in, inlen, resbuf);
1003 return GC_OK;
1005 #endif
1007 #if GNULIB_GC_SHA512
1008 Gc_rc
1009 gc_sha512 (const void *in, size_t inlen, void *resbuf)
1011 sha512_buffer (in, inlen, resbuf);
1012 return GC_OK;
1014 #endif
1016 #if GNULIB_GC_SM3
1017 Gc_rc
1018 gc_sm3 (const void *in, size_t inlen, void *resbuf)
1020 sm3_buffer (in, inlen, resbuf);
1021 return GC_OK;
1023 #endif
1025 #if GNULIB_GC_HMAC_MD5
1026 Gc_rc
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);
1031 return GC_OK;
1033 #endif
1035 #if GNULIB_GC_HMAC_SHA1
1036 Gc_rc
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);
1041 return GC_OK;
1043 #endif
1045 #if GNULIB_GC_HMAC_SHA256
1046 Gc_rc
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);
1051 return GC_OK;
1053 #endif
1055 #if GNULIB_GC_HMAC_SHA512
1056 Gc_rc
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);
1061 return GC_OK;
1063 #endif