gsasl  1.10.0
About: GNU SASL is an implementation of the Simple Authentication and Security Layer (SASL). Development version.
  Fossies Dox: gsasl-1.10.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

gc-gnulib.c
Go to the documentation of this file.
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
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
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
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:
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
207 gc_nonce (char *data, size_t datalen)
208 {
209  return randomize (0, data, datalen);
210 }
211 
212 Gc_rc
213 gc_pseudo_random (char *data, size_t datalen)
214 {
215  return randomize (1, data, datalen);
216 }
217 
218 Gc_rc
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
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 {
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
259 
260 Gc_rc
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:
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:
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:
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:
329  }
330  break;
331 #endif
332 
333  default:
335  }
336 
337  if (rc == GC_OK)
338  *outhandle = ctx;
339  else
340  free (ctx);
341 
342  return rc;
343 }
344 
345 Gc_rc
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
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
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
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
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 {
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
646 
647 Gc_rc
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:
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
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
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
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 *
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
902 {
903  _gc_hash_ctx *ctx = handle;
904 
905  free (ctx);
906 }
907 
908 Gc_rc
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
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
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
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
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
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
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
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
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
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
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
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
_W64 int ssize_t
Definition: ac-stdint.h:18
#define NAME_OF_NONCE_DEVICE
Definition: config.h:50
#define NAME_OF_PSEUDO_RANDOM_DEVICE
Definition: config.h:51
#define NAME_OF_RANDOM_DEVICE
Definition: config.h:52
Gc_rc gc_cipher_setiv(gc_cipher_handle handle, size_t ivlen, const char *iv)
Definition: gc-gnulib.c:410
struct _gc_hash_ctx _gc_hash_ctx
Gc_rc gc_hash_open(Gc_hash hash, Gc_hash_mode mode, gc_hash_handle *outhandle)
Definition: gc-gnulib.c:648
Gc_rc gc_hash_clone(gc_hash_handle handle, gc_hash_handle *outhandle)
Definition: gc-gnulib.c:722
#define MAX_DIGEST_SIZE
Definition: gc-gnulib.c:617
Gc_rc gc_cipher_open(Gc_cipher alg, Gc_cipher_mode mode, gc_cipher_handle *outhandle)
Definition: gc-gnulib.c:261
Gc_rc gc_hash_buffer(Gc_hash hash, const void *in, size_t inlen, char *resbuf)
Definition: gc-gnulib.c:909
const char * gc_hash_read(gc_hash_handle handle)
Definition: gc-gnulib.c:837
Gc_rc gc_md5(const void *in, size_t inlen, void *resbuf)
Definition: gc-gnulib.c:982
void gc_hash_close(gc_hash_handle handle)
Definition: gc-gnulib.c:901
Gc_rc gc_nonce(char *data, size_t datalen)
Definition: gc-gnulib.c:207
struct _gc_cipher_ctx _gc_cipher_ctx
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)
Definition: gc-gnulib.c:229
void gc_done(void)
Definition: gc-gnulib.c:126
Gc_rc gc_hmac_sha1(const void *key, size_t keylen, const void *in, size_t inlen, char *resbuf)
Definition: gc-gnulib.c:1037
void gc_hash_write(gc_hash_handle handle, size_t len, const char *data)
Definition: gc-gnulib.c:783
size_t gc_hash_digest_length(Gc_hash hash)
Definition: gc-gnulib.c:737
Gc_rc gc_cipher_encrypt_inline(gc_cipher_handle handle, size_t len, char *data)
Definition: gc-gnulib.c:464
Gc_rc gc_cipher_setkey(gc_cipher_handle handle, size_t keylen, const char *key)
Definition: gc-gnulib.c:346
Gc_rc gc_random(char *data, size_t datalen)
Definition: gc-gnulib.c:219
Gc_rc gc_cipher_close(gc_cipher_handle handle)
Definition: gc-gnulib.c:606
static Gc_rc randomize(int level, char *data, size_t datalen)
Definition: gc-gnulib.c:146
Gc_rc gc_cipher_decrypt_inline(gc_cipher_handle handle, size_t len, char *data)
Definition: gc-gnulib.c:534
Gc_rc gc_hmac_md5(const void *key, size_t keylen, const void *in, size_t inlen, char *resbuf)
Definition: gc-gnulib.c:1027
Gc_rc gc_init(void)
Definition: gc-gnulib.c:99
Gc_rc gc_sha1(const void *in, size_t inlen, void *resbuf)
Definition: gc-gnulib.c:991
Gc_rc gc_pseudo_random(char *data, size_t datalen)
Definition: gc-gnulib.c:213
Gc_rc gc_md2(const void *in, size_t inlen, void *resbuf)
Gc_hash
Definition: gc.h:41
@ GC_SM3
Definition: gc.h:51
@ GC_RMD160
Definition: gc.h:46
@ GC_MD5
Definition: gc.h:43
@ GC_SHA256
Definition: gc.h:47
@ GC_MD2
Definition: gc.h:45
@ GC_SHA1
Definition: gc.h:44
@ GC_MD4
Definition: gc.h:42
@ GC_SHA512
Definition: gc.h:49
Gc_rc gc_md4(const void *in, size_t inlen, void *resbuf)
#define GC_RMD160_DIGEST_SIZE
Definition: gc.h:67
void(* gc_free_t)(void *)
Definition: gc.h:111
Gc_rc gc_sha512(const void *in, size_t inlen, void *resbuf)
Gc_rc gc_hmac_sha512(const void *key, size_t keylen, const void *in, size_t inlen, char *resbuf)
Gc_rc gc_hmac_sha256(const void *key, size_t keylen, const void *in, size_t inlen, char *resbuf)
void * gc_cipher_handle
Definition: gc.h:101
#define GC_SM3_DIGEST_SIZE
Definition: gc.h:73
Gc_cipher
Definition: gc.h:79
@ GC_AES128
Definition: gc.h:80
@ GC_ARCTWO40
Definition: gc.h:87
@ GC_ARCFOUR40
Definition: gc.h:86
@ GC_AES192
Definition: gc.h:81
@ GC_DES
Definition: gc.h:84
@ GC_ARCFOUR128
Definition: gc.h:85
@ GC_AES256
Definition: gc.h:82
Gc_rc gc_sm3(const void *in, size_t inlen, void *resbuf)
#define GC_SHA512_DIGEST_SIZE
Definition: gc.h:71
Gc_hash_mode
Definition: gc.h:56
#define GC_SHA1_DIGEST_SIZE
Definition: gc.h:68
void * gc_hash_handle
Definition: gc.h:62
#define GC_SHA256_DIGEST_SIZE
Definition: gc.h:69
Gc_rc
Definition: gc.h:26
@ GC_OK
Definition: gc.h:27
@ GC_INVALID_CIPHER
Definition: gc.h:31
@ GC_MALLOC_ERROR
Definition: gc.h:28
@ GC_RANDOM_ERROR
Definition: gc.h:30
@ GC_INVALID_HASH
Definition: gc.h:32
int(* gc_secure_check_t)(const void *)
Definition: gc.h:109
#define GC_MD5_DIGEST_SIZE
Definition: gc.h:66
Gc_cipher_mode
Definition: gc.h:94
@ GC_CBC
Definition: gc.h:96
@ GC_STREAM
Definition: gc.h:97
@ GC_ECB
Definition: gc.h:95
void *(* gc_malloc_t)(size_t n)
Definition: gc.h:108
void *(* gc_realloc_t)(void *p, size_t n)
Definition: gc.h:110
#define GC_MD2_DIGEST_SIZE
Definition: gc.h:64
Gc_rc gc_sha256(const void *in, size_t inlen, void *resbuf)
#define GC_MD4_DIGEST_SIZE
Definition: gc.h:65
#define O_CLOEXEC
Definition: fcntl.in.h:291
int open(const char *filename, int flags,...)
Definition: open.c:59
#define NULL
Definition: stddef.in.h:72
static gss_OID_desc tmp
Definition: gss-extra.c:38
int hmac_md5(const void *key, size_t keylen, const void *in, size_t inlen, void *resbuf)
Definition: hmac.c:59
int hmac_sha1(const void *key, size_t keylen, const void *in, size_t inlen, void *restrict resbuf)
int hmac_sha512(const void *key, size_t keylen, const void *in, size_t inlen, void *restrict resbuf)
int hmac_sha256(const void *key, size_t keylen, const void *in, size_t inlen, void *restrict resbuf)
int rc
Definition: error.c:42
void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
Definition: md5.c:243
void * md5_buffer(const char *buffer, size_t len, void *resblock)
Definition: md5.c:227
void md5_init_ctx(struct md5_ctx *ctx)
Definition: md5.c:76
void * md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
Definition: md5.c:113
void sha1_process_bytes(const void *buffer, size_t len, struct sha1_ctx *ctx)
Definition: sha1.c:230
void * sha1_buffer(const char *buffer, size_t len, void *resblock)
Definition: sha1.c:215
void * sha1_finish_ctx(struct sha1_ctx *ctx, void *resbuf)
Definition: sha1.c:101
void sha1_init_ctx(struct sha1_ctx *ctx)
Definition: sha1.c:62
void * sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf)
Definition: sha256.c:158
void * sha256_buffer(const char *buffer, size_t len, void *resblock)
Definition: sha256.c:279
void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx)
Definition: sha256.c:309
void sha256_init_ctx(struct sha256_ctx *ctx)
Definition: sha256.c:63
Gc_cipher_mode mode
Definition: gc-gnulib.c:242
Gc_cipher alg
Definition: gc-gnulib.c:241
char hash[64]
Definition: gc-gnulib.c:623
struct md5_ctx md5Context
Definition: gc-gnulib.c:631
struct sha1_ctx sha1Context
Definition: gc-gnulib.c:634
Gc_hash_mode mode
Definition: gc-gnulib.c:622
Gc_hash alg
Definition: gc-gnulib.c:621
Definition: md5.h:70
Definition: sha1.h:41