1 /* gc-gnulib.c --- Common gnulib internal crypto interface functions
2  * Copyright (C) 2002-2021 Free Software Foundation, Inc.
3  *
4  * This file is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1, 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this file; if not, see <https://www.gnu.org/licenses/>.
16  *
17  */
18 
19 /* Note: This file is only built if GC uses internal functions. */
20 
21 #include <config.h>
22 
23 /* Get prototype. */
24 #include "gc.h"
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
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
37 
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
63 
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
77 
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
91 
92 #if defined _WIN32 && ! defined __CYGWIN__
93 /* Don't assume that UNICODE is not defined.  */
94 # undef CryptAcquireContext
95 # define CryptAcquireContext CryptAcquireContextA
96 #endif
97 
98 Gc_rc
gc_init(void)99 gc_init (void)
100 {
101 #if GNULIB_GC_RANDOM
102 # if defined _WIN32 && ! defined __CYGWIN__
103   if (g_hProv)
104     CryptReleaseContext (g_hProv, 0);
105 
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 */
109 
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))
113     {
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;
118     }
119 # endif
120 #endif
121 
122   return GC_OK;
123 }
124 
125 void
gc_done(void)126 gc_done (void)
127 {
128 #if GNULIB_GC_RANDOM
129 # if defined _WIN32 && ! defined __CYGWIN__
130   if (g_hProv)
131     {
132       CryptReleaseContext (g_hProv, 0);
133       g_hProv = 0;
134     }
135 # endif
136 #endif
137 
138   return;
139 }
140 
141 #if GNULIB_GC_RANDOM
142 
143 /* Randomness. */
144 
145 static Gc_rc
randomize(int level,char * data,size_t datalen)146 randomize (int level, char *data, size_t datalen)
147 {
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;
157 
158   switch (level)
159     {
160     case 0:
161       device = NAME_OF_NONCE_DEVICE;
162       break;
163 
164     case 1:
165       device = NAME_OF_PSEUDO_RANDOM_DEVICE;
166       break;
167 
168     default:
169       device = NAME_OF_RANDOM_DEVICE;
170       break;
171     }
172 
173   if (strcmp (device, "no") == 0)
174     return GC_RANDOM_ERROR;
175 
176   fd = open (device, O_RDONLY | O_CLOEXEC);
177   if (fd < 0)
178     return GC_RANDOM_ERROR;
179 
180   do
181     {
182       ssize_t tmp;
183 
184       tmp = read (fd, data, datalen);
185 
186       if (tmp < 0)
187         {
188           int save_errno = errno;
189           close (fd);
190           errno = save_errno;
191           return GC_RANDOM_ERROR;
192         }
193 
194       len += tmp;
195     }
196   while (len < datalen);
197 
198   rc = close (fd);
199   if (rc < 0)
200     return GC_RANDOM_ERROR;
201 #endif
202 
203   return GC_OK;
204 }
205 
206 Gc_rc
gc_nonce(char * data,size_t datalen)207 gc_nonce (char *data, size_t datalen)
208 {
209   return randomize (0, data, datalen);
210 }
211 
212 Gc_rc
gc_pseudo_random(char * data,size_t datalen)213 gc_pseudo_random (char *data, size_t datalen)
214 {
215   return randomize (1, data, datalen);
216 }
217 
218 Gc_rc
gc_random(char * data,size_t datalen)219 gc_random (char *data, size_t datalen)
220 {
221   return randomize (2, data, datalen);
222 }
223 
224 #endif
225 
226 /* Memory allocation. */
227 
228 void
gc_set_allocators(gc_malloc_t func_malloc,gc_malloc_t secure_malloc,gc_secure_check_t secure_check,gc_realloc_t func_realloc,gc_free_t func_free)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)
233 {
234   return;
235 }
236 
237 /* Ciphers. */
238 
239 typedef struct _gc_cipher_ctx
240 {
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;
259 
260 Gc_rc
gc_cipher_open(Gc_cipher alg,Gc_cipher_mode mode,gc_cipher_handle * outhandle)261 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
262                 gc_cipher_handle * outhandle)
263 {
264   _gc_cipher_ctx *ctx;
265   Gc_rc rc = GC_OK;
266 
267   ctx = calloc (sizeof (*ctx), 1);
268   if (!ctx)
269     return GC_MALLOC_ERROR;
270 
271   ctx->alg = alg;
272   ctx->mode = mode;
273 
274   switch (alg)
275     {
276 #if GNULIB_GC_ARCTWO
277     case GC_ARCTWO40:
278       switch (mode)
279         {
280         case GC_ECB:
281         case GC_CBC:
282           break;
283 
284         default:
285           rc = GC_INVALID_CIPHER;
286         }
287       break;
288 #endif
289 
290 #if GNULIB_GC_ARCFOUR
291     case GC_ARCFOUR128:
292     case GC_ARCFOUR40:
293       switch (mode)
294         {
295         case GC_STREAM:
296           break;
297 
298         default:
299           rc = GC_INVALID_CIPHER;
300         }
301       break;
302 #endif
303 
304 #if GNULIB_GC_DES
305     case GC_DES:
306       switch (mode)
307         {
308         case GC_ECB:
309           break;
310 
311         default:
312           rc = GC_INVALID_CIPHER;
313         }
314       break;
315 #endif
316 
317 #if GNULIB_GC_RIJNDAEL
318     case GC_AES128:
319     case GC_AES192:
320     case GC_AES256:
321       switch (mode)
322         {
323         case GC_ECB:
324         case GC_CBC:
325           break;
326 
327         default:
328           rc = GC_INVALID_CIPHER;
329         }
330       break;
331 #endif
332 
333     default:
334       rc = GC_INVALID_CIPHER;
335     }
336 
337   if (rc == GC_OK)
338     *outhandle = ctx;
339   else
340     free (ctx);
341 
342   return rc;
343 }
344 
345 Gc_rc
gc_cipher_setkey(gc_cipher_handle handle,size_t keylen,const char * key)346 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
347 {
348   _gc_cipher_ctx *ctx = handle;
349 
350   switch (ctx->alg)
351     {
352 #if GNULIB_GC_ARCTWO
353     case GC_ARCTWO40:
354       arctwo_setkey (&ctx->arctwoContext, keylen, key);
355       break;
356 #endif
357 
358 #if GNULIB_GC_ARCFOUR
359     case GC_ARCFOUR128:
360     case GC_ARCFOUR40:
361       arcfour_setkey (&ctx->arcfourContext, key, keylen);
362       break;
363 #endif
364 
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
372 
373 #if GNULIB_GC_RIJNDAEL
374     case GC_AES128:
375     case GC_AES192:
376     case GC_AES256:
377       {
378         rijndael_rc rc;
379         size_t i;
380         char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
381 
382         for (i = 0; i < keylen; i++)
383           sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
384 
385         rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
386                               keylen * 8, keyMaterial);
387         if (rc < 0)
388           return GC_INVALID_CIPHER;
389 
390         rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
391                               keylen * 8, keyMaterial);
392         if (rc < 0)
393           return GC_INVALID_CIPHER;
394 
395         rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
396         if (rc < 0)
397           return GC_INVALID_CIPHER;
398       }
399       break;
400 #endif
401 
402     default:
403       return GC_INVALID_CIPHER;
404     }
405 
406   return GC_OK;
407 }
408 
409 Gc_rc
gc_cipher_setiv(gc_cipher_handle handle,size_t ivlen,const char * iv)410 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
411 {
412   _gc_cipher_ctx *ctx = handle;
413 
414   switch (ctx->alg)
415     {
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
423 
424 #if GNULIB_GC_RIJNDAEL
425     case GC_AES128:
426     case GC_AES192:
427     case GC_AES256:
428       switch (ctx->mode)
429         {
430         case GC_ECB:
431           /* Doesn't use IV. */
432           break;
433 
434         case GC_CBC:
435           {
436             rijndael_rc rc;
437             size_t i;
438             char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
439 
440             for (i = 0; i < ivlen; i++)
441               sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
442 
443             rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
444                                      ivMaterial);
445             if (rc < 0)
446               return GC_INVALID_CIPHER;
447           }
448           break;
449 
450         default:
451           return GC_INVALID_CIPHER;
452         }
453       break;
454 #endif
455 
456     default:
457       return GC_INVALID_CIPHER;
458     }
459 
460   return GC_OK;
461 }
462 
463 Gc_rc
gc_cipher_encrypt_inline(gc_cipher_handle handle,size_t len,char * data)464 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
465 {
466   _gc_cipher_ctx *ctx = handle;
467 
468   switch (ctx->alg)
469     {
470 #if GNULIB_GC_ARCTWO
471     case GC_ARCTWO40:
472       switch (ctx->mode)
473         {
474         case GC_ECB:
475           arctwo_encrypt (&ctx->arctwoContext, data, data, len);
476           break;
477 
478         case GC_CBC:
479           for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
480                data += ARCTWO_BLOCK_SIZE)
481             {
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);
488             }
489           break;
490 
491         default:
492           return GC_INVALID_CIPHER;
493         }
494       break;
495 #endif
496 
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
503 
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
510 
511 #if GNULIB_GC_RIJNDAEL
512     case GC_AES128:
513     case GC_AES192:
514     case GC_AES256:
515       {
516         int nblocks;
517 
518         nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
519                                         data, 8 * len, data);
520         if (nblocks < 0)
521           return GC_INVALID_CIPHER;
522       }
523       break;
524 #endif
525 
526     default:
527       return GC_INVALID_CIPHER;
528     }
529 
530   return GC_OK;
531 }
532 
533 Gc_rc
gc_cipher_decrypt_inline(gc_cipher_handle handle,size_t len,char * data)534 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
535 {
536   _gc_cipher_ctx *ctx = handle;
537 
538   switch (ctx->alg)
539     {
540 #if GNULIB_GC_ARCTWO
541     case GC_ARCTWO40:
542       switch (ctx->mode)
543         {
544         case GC_ECB:
545           arctwo_decrypt (&ctx->arctwoContext, data, data, len);
546           break;
547 
548         case GC_CBC:
549           for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
550                data += ARCTWO_BLOCK_SIZE)
551             {
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);
560             }
561           break;
562 
563         default:
564           return GC_INVALID_CIPHER;
565         }
566       break;
567 #endif
568 
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
575 
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
582 
583 #if GNULIB_GC_RIJNDAEL
584     case GC_AES128:
585     case GC_AES192:
586     case GC_AES256:
587       {
588         int nblocks;
589 
590         nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
591                                         data, 8 * len, data);
592         if (nblocks < 0)
593           return GC_INVALID_CIPHER;
594       }
595       break;
596 #endif
597 
598     default:
599       return GC_INVALID_CIPHER;
600     }
601 
602   return GC_OK;
603 }
604 
605 Gc_rc
gc_cipher_close(gc_cipher_handle handle)606 gc_cipher_close (gc_cipher_handle handle)
607 {
608   _gc_cipher_ctx *ctx = handle;
609 
610   free (ctx);
611 
612   return GC_OK;
613 }
614 
615 /* Hashes. */
616 
617 #define MAX_DIGEST_SIZE 64
618 
619 typedef struct _gc_hash_ctx
620 {
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;
646 
647 Gc_rc
gc_hash_open(Gc_hash hash,Gc_hash_mode mode,gc_hash_handle * outhandle)648 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
649 {
650   _gc_hash_ctx *ctx;
651   Gc_rc rc = GC_OK;
652 
653   if (mode != 0)
654     return GC_INVALID_HASH;
655 
656   ctx = calloc (sizeof (*ctx), 1);
657   if (!ctx)
658     return GC_MALLOC_ERROR;
659 
660   ctx->alg = hash;
661   ctx->mode = mode;
662 
663   switch (hash)
664     {
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
671 
672 #if GNULIB_GC_MD4
673     case GC_MD4:
674       md4_init_ctx (&ctx->md4Context);
675       break;
676 #endif
677 
678 #if GNULIB_GC_MD5
679     case GC_MD5:
680       md5_init_ctx (&ctx->md5Context);
681       break;
682 #endif
683 
684 #if GNULIB_GC_SHA1
685     case GC_SHA1:
686       sha1_init_ctx (&ctx->sha1Context);
687       break;
688 #endif
689 
690 #if GNULIB_GC_SHA256
691     case GC_SHA256:
692       sha256_init_ctx (&ctx->sha256Context);
693       break;
694 #endif
695 
696 #if GNULIB_GC_SHA512
697     case GC_SHA512:
698       sha512_init_ctx (&ctx->sha512Context);
699       break;
700 #endif
701 
702 #if GNULIB_GC_SM3
703     case GC_SM3:
704       sm3_init_ctx (&ctx->sm3Context);
705       break;
706 #endif
707 
708     default:
709       rc = GC_INVALID_HASH;
710       break;
711     }
712 
713   if (rc == GC_OK)
714     *outhandle = ctx;
715   else
716     free (ctx);
717 
718   return rc;
719 }
720 
721 Gc_rc
gc_hash_clone(gc_hash_handle handle,gc_hash_handle * outhandle)722 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
723 {
724   _gc_hash_ctx *in = handle;
725   _gc_hash_ctx *out;
726 
727   *outhandle = out = calloc (sizeof (*out), 1);
728   if (!out)
729     return GC_MALLOC_ERROR;
730 
731   memcpy (out, in, sizeof (*out));
732 
733   return GC_OK;
734 }
735 
736 size_t
gc_hash_digest_length(Gc_hash hash)737 gc_hash_digest_length (Gc_hash hash)
738 {
739   size_t len;
740 
741   switch (hash)
742     {
743     case GC_MD2:
744       len = GC_MD2_DIGEST_SIZE;
745       break;
746 
747     case GC_MD4:
748       len = GC_MD4_DIGEST_SIZE;
749       break;
750 
751     case GC_MD5:
752       len = GC_MD5_DIGEST_SIZE;
753       break;
754 
755     case GC_RMD160:
756       len = GC_RMD160_DIGEST_SIZE;
757       break;
758 
759     case GC_SHA1:
760       len = GC_SHA1_DIGEST_SIZE;
761       break;
762 
763     case GC_SHA256:
764       len = GC_SHA256_DIGEST_SIZE;
765       break;
766 
767     case GC_SHA512:
768       len = GC_SHA512_DIGEST_SIZE;
769       break;
770 
771     case GC_SM3:
772       len = GC_SM3_DIGEST_SIZE;
773       break;
774 
775     default:
776       return 0;
777     }
778 
779   return len;
780 }
781 
782 void
gc_hash_write(gc_hash_handle handle,size_t len,const char * data)783 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
784 {
785   _gc_hash_ctx *ctx = handle;
786 
787   switch (ctx->alg)
788     {
789 #if GNULIB_GC_MD2
790     case GC_MD2:
791       md2_process_bytes (data, len, &ctx->md2Context);
792       break;
793 #endif
794 
795 #if GNULIB_GC_MD4
796     case GC_MD4:
797       md4_process_bytes (data, len, &ctx->md4Context);
798       break;
799 #endif
800 
801 #if GNULIB_GC_MD5
802     case GC_MD5:
803       md5_process_bytes (data, len, &ctx->md5Context);
804       break;
805 #endif
806 
807 #if GNULIB_GC_SHA1
808     case GC_SHA1:
809       sha1_process_bytes (data, len, &ctx->sha1Context);
810       break;
811 #endif
812 
813 #if GNULIB_GC_SHA256
814     case GC_SHA256:
815       sha256_process_bytes (data, len, &ctx->sha256Context);
816       break;
817 #endif
818 
819 #if GNULIB_GC_SHA512
820     case GC_SHA512:
821       sha512_process_bytes (data, len, &ctx->sha512Context);
822       break;
823 #endif
824 
825 #if GNULIB_GC_SM3
826     case GC_SM3:
827       sm3_process_bytes (data, len, &ctx->sm3Context);
828       break;
829 #endif
830 
831     default:
832       break;
833     }
834 }
835 
836 const char *
gc_hash_read(gc_hash_handle handle)837 gc_hash_read (gc_hash_handle handle)
838 {
839   _gc_hash_ctx *ctx = handle;
840   const char *ret = NULL;
841 
842   switch (ctx->alg)
843     {
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
850 
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
857 
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
864 
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
871 
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
878 
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
885 
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
892 
893     default:
894       return NULL;
895     }
896 
897   return ret;
898 }
899 
900 void
gc_hash_close(gc_hash_handle handle)901 gc_hash_close (gc_hash_handle handle)
902 {
903   _gc_hash_ctx *ctx = handle;
904 
905   free (ctx);
906 }
907 
908 Gc_rc
gc_hash_buffer(Gc_hash hash,const void * in,size_t inlen,char * resbuf)909 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
910 {
911   switch (hash)
912     {
913 #if GNULIB_GC_MD2
914     case GC_MD2:
915       md2_buffer (in, inlen, resbuf);
916       break;
917 #endif
918 
919 #if GNULIB_GC_MD4
920     case GC_MD4:
921       md4_buffer (in, inlen, resbuf);
922       break;
923 #endif
924 
925 #if GNULIB_GC_MD5
926     case GC_MD5:
927       md5_buffer (in, inlen, resbuf);
928       break;
929 #endif
930 
931 #if GNULIB_GC_SHA1
932     case GC_SHA1:
933       sha1_buffer (in, inlen, resbuf);
934       break;
935 #endif
936 
937 #if GNULIB_GC_SHA256
938     case GC_SHA256:
939       sha256_buffer (in, inlen, resbuf);
940       break;
941 #endif
942 
943 #if GNULIB_GC_SHA512
944     case GC_SHA512:
945       sha512_buffer (in, inlen, resbuf);
946       break;
947 #endif
948 
949 #if GNULIB_GC_SM3
950     case GC_SM3:
951       sm3_buffer (in, inlen, resbuf);
952       break;
953 #endif
954 
955     default:
956       return GC_INVALID_HASH;
957     }
958 
959   return GC_OK;
960 }
961 
962 #if GNULIB_GC_MD2
963 Gc_rc
gc_md2(const void * in,size_t inlen,void * resbuf)964 gc_md2 (const void *in, size_t inlen, void *resbuf)
965 {
966   md2_buffer (in, inlen, resbuf);
967   return GC_OK;
968 }
969 #endif
970 
971 #if GNULIB_GC_MD4
972 Gc_rc
gc_md4(const void * in,size_t inlen,void * resbuf)973 gc_md4 (const void *in, size_t inlen, void *resbuf)
974 {
975   md4_buffer (in, inlen, resbuf);
976   return GC_OK;
977 }
978 #endif
979 
980 #if GNULIB_GC_MD5
981 Gc_rc
gc_md5(const void * in,size_t inlen,void * resbuf)982 gc_md5 (const void *in, size_t inlen, void *resbuf)
983 {
984   md5_buffer (in, inlen, resbuf);
985   return GC_OK;
986 }
987 #endif
988 
989 #if GNULIB_GC_SHA1
990 Gc_rc
gc_sha1(const void * in,size_t inlen,void * resbuf)991 gc_sha1 (const void *in, size_t inlen, void *resbuf)
992 {
993   sha1_buffer (in, inlen, resbuf);
994   return GC_OK;
995 }
996 #endif
997 
998 #if GNULIB_GC_SHA256
999 Gc_rc
gc_sha256(const void * in,size_t inlen,void * resbuf)1000 gc_sha256 (const void *in, size_t inlen, void *resbuf)
1001 {
1002   sha256_buffer (in, inlen, resbuf);
1003   return GC_OK;
1004 }
1005 #endif
1006 
1007 #if GNULIB_GC_SHA512
1008 Gc_rc
gc_sha512(const void * in,size_t inlen,void * resbuf)1009 gc_sha512 (const void *in, size_t inlen, void *resbuf)
1010 {
1011   sha512_buffer (in, inlen, resbuf);
1012   return GC_OK;
1013 }
1014 #endif
1015 
1016 #if GNULIB_GC_SM3
1017 Gc_rc
gc_sm3(const void * in,size_t inlen,void * resbuf)1018 gc_sm3 (const void *in, size_t inlen, void *resbuf)
1019 {
1020   sm3_buffer (in, inlen, resbuf);
1021   return GC_OK;
1022 }
1023 #endif
1024 
1025 #if GNULIB_GC_HMAC_MD5
1026 Gc_rc
gc_hmac_md5(const void * key,size_t keylen,const void * in,size_t inlen,char * resbuf)1027 gc_hmac_md5 (const void *key, size_t keylen,
1028              const void *in, size_t inlen, char *resbuf)
1029 {
1030   hmac_md5 (key, keylen, in, inlen, resbuf);
1031   return GC_OK;
1032 }
1033 #endif
1034 
1035 #if GNULIB_GC_HMAC_SHA1
1036 Gc_rc
gc_hmac_sha1(const void * key,size_t keylen,const void * in,size_t inlen,char * resbuf)1037 gc_hmac_sha1 (const void *key, size_t keylen,
1038               const void *in, size_t inlen, char *resbuf)
1039 {
1040   hmac_sha1 (key, keylen, in, inlen, resbuf);
1041   return GC_OK;
1042 }
1043 #endif
1044 
1045 #if GNULIB_GC_HMAC_SHA256
1046 Gc_rc
gc_hmac_sha256(const void * key,size_t keylen,const void * in,size_t inlen,char * resbuf)1047 gc_hmac_sha256 (const void *key, size_t keylen,
1048                 const void *in, size_t inlen, char *resbuf)
1049 {
1050   hmac_sha256 (key, keylen, in, inlen, resbuf);
1051   return GC_OK;
1052 }
1053 #endif
1054 
1055 #if GNULIB_GC_HMAC_SHA512
1056 Gc_rc
gc_hmac_sha512(const void * key,size_t keylen,const void * in,size_t inlen,char * resbuf)1057 gc_hmac_sha512 (const void *key, size_t keylen,
1058                 const void *in, size_t inlen, char *resbuf)
1059 {
1060   hmac_sha512 (key, keylen, in, inlen, resbuf);
1061   return GC_OK;
1062 }
1063 #endif
1064