root/maint/gnulib/lib/gc-gnulib.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. gc_init
  2. gc_done
  3. randomize
  4. gc_nonce
  5. gc_pseudo_random
  6. gc_random
  7. gc_set_allocators
  8. gc_cipher_open
  9. gc_cipher_setkey
  10. gc_cipher_setiv
  11. gc_cipher_encrypt_inline
  12. gc_cipher_decrypt_inline
  13. gc_cipher_close
  14. gc_hash_open
  15. gc_hash_clone
  16. gc_hash_digest_length
  17. gc_hash_write
  18. gc_hash_read
  19. gc_hash_close
  20. gc_hash_buffer
  21. gc_md2
  22. gc_md4
  23. gc_md5
  24. gc_sha1
  25. gc_sha256
  26. gc_sha512
  27. gc_sm3
  28. gc_hmac_md5
  29. gc_hmac_sha1
  30. gc_hmac_sha256
  31. gc_hmac_sha512

   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
   6  * published by the Free Software Foundation; either version 2.1 of the
   7  * License, or (at your option) any later version.
   8  *
   9  * This file is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  * GNU 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 program.  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 <errno.h>
  33 #include <sys/random.h>
  34 #endif
  35 
  36 /* Hashes. */
  37 #if GNULIB_GC_MD2
  38 # include "md2.h"
  39 #endif
  40 #if GNULIB_GC_MD4
  41 # include "md4.h"
  42 #endif
  43 #if GNULIB_GC_MD5
  44 # include "md5.h"
  45 #endif
  46 #if GNULIB_GC_SHA1
  47 # include "sha1.h"
  48 #endif
  49 #if GNULIB_GC_SHA256
  50 # include "sha256.h"
  51 #endif
  52 #if GNULIB_GC_SHA512
  53 # include "sha512.h"
  54 #endif
  55 #if GNULIB_GC_SM3
  56 # include "sm3.h"
  57 #endif
  58 #if GNULIB_GC_HMAC_MD5 || GNULIB_GC_HMAC_SHA1 || GNULIB_GC_HMAC_SHA256 || GNULIB_GC_HMAC_SHA512
  59 # include "hmac.h"
  60 #endif
  61 
  62 /* Ciphers. */
  63 #if GNULIB_GC_ARCFOUR
  64 # include "arcfour.h"
  65 #endif
  66 #if GNULIB_GC_ARCTWO
  67 # include "arctwo.h"
  68 #endif
  69 #if GNULIB_GC_DES
  70 # include "des.h"
  71 #endif
  72 #if GNULIB_GC_RIJNDAEL
  73 # include "rijndael-api-fst.h"
  74 #endif
  75 
  76 Gc_rc
  77 gc_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79   return GC_OK;
  80 }
  81 
  82 void
  83 gc_done (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85   return;
  86 }
  87 
  88 #if GNULIB_GC_RANDOM
  89 
  90 /* Overwrite BUFFER with random data, under the control of getrandom
  91    FLAGS.  BUFFER contains LENGTH bytes.  Inspired by getentropy,
  92    however LENGTH is not restricted to 256.  Return 0 on success, -1
  93    (setting errno) on failure.  */
  94 static int
  95 randomize (void *buffer, size_t length, unsigned int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
  96 {
  97   char *buf = buffer;
  98 
  99   for (;;)
 100     {
 101       ssize_t bytes;
 102       if (length == 0)
 103         return GC_OK;
 104       while ((bytes = getrandom (buf, length, flags)) < 0)
 105         if (errno != EINTR)
 106           return GC_RANDOM_ERROR;
 107       if (bytes == 0)
 108         break;
 109       buf += bytes;
 110       length -= bytes;
 111     }
 112 
 113   return GC_RANDOM_ERROR;
 114 }
 115 
 116 Gc_rc
 117 gc_nonce (char *data, size_t datalen)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119   return randomize (data, datalen, 0);
 120 }
 121 
 122 Gc_rc
 123 gc_pseudo_random (char *data, size_t datalen)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125   return randomize (data, datalen, 0);
 126 }
 127 
 128 Gc_rc
 129 gc_random (char *data, size_t datalen)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131   return randomize (data, datalen, GRND_RANDOM);
 132 }
 133 
 134 #endif
 135 
 136 /* Memory allocation. */
 137 
 138 void
 139 gc_set_allocators (gc_malloc_t func_malloc,
     /* [previous][next][first][last][top][bottom][index][help] */
 140                    gc_malloc_t secure_malloc,
 141                    gc_secure_check_t secure_check,
 142                    gc_realloc_t func_realloc, gc_free_t func_free)
 143 {
 144   return;
 145 }
 146 
 147 /* Ciphers. */
 148 
 149 typedef struct _gc_cipher_ctx
 150 {
 151   Gc_cipher alg;
 152   Gc_cipher_mode mode;
 153 #if GNULIB_GC_ARCTWO
 154   arctwo_context arctwoContext;
 155   char arctwoIV[ARCTWO_BLOCK_SIZE];
 156 #endif
 157 #if GNULIB_GC_ARCFOUR
 158   arcfour_context arcfourContext;
 159 #endif
 160 #if GNULIB_GC_DES
 161   gl_des_ctx desContext;
 162 #endif
 163 #if GNULIB_GC_RIJNDAEL
 164   rijndaelKeyInstance aesEncKey;
 165   rijndaelKeyInstance aesDecKey;
 166   rijndaelCipherInstance aesContext;
 167 #endif
 168 } _gc_cipher_ctx;
 169 
 170 Gc_rc
 171 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
     /* [previous][next][first][last][top][bottom][index][help] */
 172                 gc_cipher_handle * outhandle)
 173 {
 174   _gc_cipher_ctx *ctx;
 175   Gc_rc rc = GC_OK;
 176 
 177   ctx = calloc (sizeof (*ctx), 1);
 178   if (!ctx)
 179     return GC_MALLOC_ERROR;
 180 
 181   ctx->alg = alg;
 182   ctx->mode = mode;
 183 
 184   switch (alg)
 185     {
 186 #if GNULIB_GC_ARCTWO
 187     case GC_ARCTWO40:
 188       switch (mode)
 189         {
 190         case GC_ECB:
 191         case GC_CBC:
 192           break;
 193 
 194         default:
 195           rc = GC_INVALID_CIPHER;
 196         }
 197       break;
 198 #endif
 199 
 200 #if GNULIB_GC_ARCFOUR
 201     case GC_ARCFOUR128:
 202     case GC_ARCFOUR40:
 203       switch (mode)
 204         {
 205         case GC_STREAM:
 206           break;
 207 
 208         default:
 209           rc = GC_INVALID_CIPHER;
 210         }
 211       break;
 212 #endif
 213 
 214 #if GNULIB_GC_DES
 215     case GC_DES:
 216       switch (mode)
 217         {
 218         case GC_ECB:
 219           break;
 220 
 221         default:
 222           rc = GC_INVALID_CIPHER;
 223         }
 224       break;
 225 #endif
 226 
 227 #if GNULIB_GC_RIJNDAEL
 228     case GC_AES128:
 229     case GC_AES192:
 230     case GC_AES256:
 231       switch (mode)
 232         {
 233         case GC_ECB:
 234         case GC_CBC:
 235           break;
 236 
 237         default:
 238           rc = GC_INVALID_CIPHER;
 239         }
 240       break;
 241 #endif
 242 
 243     default:
 244       rc = GC_INVALID_CIPHER;
 245     }
 246 
 247   if (rc == GC_OK)
 248     *outhandle = ctx;
 249   else
 250     free (ctx);
 251 
 252   return rc;
 253 }
 254 
 255 Gc_rc
 256 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258   _gc_cipher_ctx *ctx = handle;
 259 
 260   switch (ctx->alg)
 261     {
 262 #if GNULIB_GC_ARCTWO
 263     case GC_ARCTWO40:
 264       arctwo_setkey (&ctx->arctwoContext, keylen, key);
 265       break;
 266 #endif
 267 
 268 #if GNULIB_GC_ARCFOUR
 269     case GC_ARCFOUR128:
 270     case GC_ARCFOUR40:
 271       arcfour_setkey (&ctx->arcfourContext, key, keylen);
 272       break;
 273 #endif
 274 
 275 #if GNULIB_GC_DES
 276     case GC_DES:
 277       if (keylen != 8)
 278         return GC_INVALID_CIPHER;
 279       gl_des_setkey (&ctx->desContext, key);
 280       break;
 281 #endif
 282 
 283 #if GNULIB_GC_RIJNDAEL
 284     case GC_AES128:
 285     case GC_AES192:
 286     case GC_AES256:
 287       {
 288         rijndael_rc rc;
 289         size_t i;
 290         char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
 291 
 292         for (i = 0; i < keylen; i++)
 293           sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
 294 
 295         rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
 296                               keylen * 8, keyMaterial);
 297         if (rc < 0)
 298           return GC_INVALID_CIPHER;
 299 
 300         rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
 301                               keylen * 8, keyMaterial);
 302         if (rc < 0)
 303           return GC_INVALID_CIPHER;
 304 
 305         rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
 306         if (rc < 0)
 307           return GC_INVALID_CIPHER;
 308       }
 309       break;
 310 #endif
 311 
 312     default:
 313       return GC_INVALID_CIPHER;
 314     }
 315 
 316   return GC_OK;
 317 }
 318 
 319 Gc_rc
 320 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322   _gc_cipher_ctx *ctx = handle;
 323 
 324   switch (ctx->alg)
 325     {
 326 #if GNULIB_GC_ARCTWO
 327     case GC_ARCTWO40:
 328       if (ivlen != ARCTWO_BLOCK_SIZE)
 329         return GC_INVALID_CIPHER;
 330       memcpy (ctx->arctwoIV, iv, ivlen);
 331       break;
 332 #endif
 333 
 334 #if GNULIB_GC_RIJNDAEL
 335     case GC_AES128:
 336     case GC_AES192:
 337     case GC_AES256:
 338       switch (ctx->mode)
 339         {
 340         case GC_ECB:
 341           /* Doesn't use IV. */
 342           break;
 343 
 344         case GC_CBC:
 345           {
 346             rijndael_rc rc;
 347             size_t i;
 348             char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
 349 
 350             for (i = 0; i < ivlen; i++)
 351               sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
 352 
 353             rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
 354                                      ivMaterial);
 355             if (rc < 0)
 356               return GC_INVALID_CIPHER;
 357           }
 358           break;
 359 
 360         default:
 361           return GC_INVALID_CIPHER;
 362         }
 363       break;
 364 #endif
 365 
 366     default:
 367       return GC_INVALID_CIPHER;
 368     }
 369 
 370   return GC_OK;
 371 }
 372 
 373 Gc_rc
 374 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 375 {
 376   _gc_cipher_ctx *ctx = handle;
 377 
 378   switch (ctx->alg)
 379     {
 380 #if GNULIB_GC_ARCTWO
 381     case GC_ARCTWO40:
 382       switch (ctx->mode)
 383         {
 384         case GC_ECB:
 385           arctwo_encrypt (&ctx->arctwoContext, data, data, len);
 386           break;
 387 
 388         case GC_CBC:
 389           for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
 390                data += ARCTWO_BLOCK_SIZE)
 391             {
 392               size_t i;
 393               for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
 394                 data[i] ^= ctx->arctwoIV[i];
 395               arctwo_encrypt (&ctx->arctwoContext, data, data,
 396                               ARCTWO_BLOCK_SIZE);
 397               memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
 398             }
 399           break;
 400 
 401         default:
 402           return GC_INVALID_CIPHER;
 403         }
 404       break;
 405 #endif
 406 
 407 #if GNULIB_GC_ARCFOUR
 408     case GC_ARCFOUR128:
 409     case GC_ARCFOUR40:
 410       arcfour_stream (&ctx->arcfourContext, data, data, len);
 411       break;
 412 #endif
 413 
 414 #if GNULIB_GC_DES
 415     case GC_DES:
 416       for (; len >= 8; len -= 8, data += 8)
 417         gl_des_ecb_encrypt (&ctx->desContext, data, data);
 418       break;
 419 #endif
 420 
 421 #if GNULIB_GC_RIJNDAEL
 422     case GC_AES128:
 423     case GC_AES192:
 424     case GC_AES256:
 425       {
 426         int nblocks;
 427 
 428         nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
 429                                         data, 8 * len, data);
 430         if (nblocks < 0)
 431           return GC_INVALID_CIPHER;
 432       }
 433       break;
 434 #endif
 435 
 436     default:
 437       return GC_INVALID_CIPHER;
 438     }
 439 
 440   return GC_OK;
 441 }
 442 
 443 Gc_rc
 444 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 445 {
 446   _gc_cipher_ctx *ctx = handle;
 447 
 448   switch (ctx->alg)
 449     {
 450 #if GNULIB_GC_ARCTWO
 451     case GC_ARCTWO40:
 452       switch (ctx->mode)
 453         {
 454         case GC_ECB:
 455           arctwo_decrypt (&ctx->arctwoContext, data, data, len);
 456           break;
 457 
 458         case GC_CBC:
 459           for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
 460                data += ARCTWO_BLOCK_SIZE)
 461             {
 462               char tmpIV[ARCTWO_BLOCK_SIZE];
 463               size_t i;
 464               memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
 465               arctwo_decrypt (&ctx->arctwoContext, data, data,
 466                               ARCTWO_BLOCK_SIZE);
 467               for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
 468                 data[i] ^= ctx->arctwoIV[i];
 469               memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
 470             }
 471           break;
 472 
 473         default:
 474           return GC_INVALID_CIPHER;
 475         }
 476       break;
 477 #endif
 478 
 479 #if GNULIB_GC_ARCFOUR
 480     case GC_ARCFOUR128:
 481     case GC_ARCFOUR40:
 482       arcfour_stream (&ctx->arcfourContext, data, data, len);
 483       break;
 484 #endif
 485 
 486 #if GNULIB_GC_DES
 487     case GC_DES:
 488       for (; len >= 8; len -= 8, data += 8)
 489         gl_des_ecb_decrypt (&ctx->desContext, data, data);
 490       break;
 491 #endif
 492 
 493 #if GNULIB_GC_RIJNDAEL
 494     case GC_AES128:
 495     case GC_AES192:
 496     case GC_AES256:
 497       {
 498         int nblocks;
 499 
 500         nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
 501                                         data, 8 * len, data);
 502         if (nblocks < 0)
 503           return GC_INVALID_CIPHER;
 504       }
 505       break;
 506 #endif
 507 
 508     default:
 509       return GC_INVALID_CIPHER;
 510     }
 511 
 512   return GC_OK;
 513 }
 514 
 515 Gc_rc
 516 gc_cipher_close (gc_cipher_handle handle)
     /* [previous][next][first][last][top][bottom][index][help] */
 517 {
 518   _gc_cipher_ctx *ctx = handle;
 519 
 520   free (ctx);
 521 
 522   return GC_OK;
 523 }
 524 
 525 /* Hashes. */
 526 
 527 #define MAX_DIGEST_SIZE 64
 528 
 529 typedef struct _gc_hash_ctx
 530 {
 531   Gc_hash alg;
 532   Gc_hash_mode mode;
 533   char hash[MAX_DIGEST_SIZE];
 534 #if GNULIB_GC_MD2
 535   struct md2_ctx md2Context;
 536 #endif
 537 #if GNULIB_GC_MD4
 538   struct md4_ctx md4Context;
 539 #endif
 540 #if GNULIB_GC_MD5
 541   struct md5_ctx md5Context;
 542 #endif
 543 #if GNULIB_GC_SHA1
 544   struct sha1_ctx sha1Context;
 545 #endif
 546 #if GNULIB_GC_SHA256
 547   struct sha256_ctx sha256Context;
 548 #endif
 549 #if GNULIB_GC_SHA512
 550   struct sha512_ctx sha512Context;
 551 #endif
 552 #if GNULIB_GC_SM3
 553   struct sm3_ctx sm3Context;
 554 #endif
 555 } _gc_hash_ctx;
 556 
 557 Gc_rc
 558 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
     /* [previous][next][first][last][top][bottom][index][help] */
 559 {
 560   _gc_hash_ctx *ctx;
 561   Gc_rc rc = GC_OK;
 562 
 563   if (mode != 0)
 564     return GC_INVALID_HASH;
 565 
 566   ctx = calloc (sizeof (*ctx), 1);
 567   if (!ctx)
 568     return GC_MALLOC_ERROR;
 569 
 570   ctx->alg = hash;
 571   ctx->mode = mode;
 572 
 573   switch (hash)
 574     {
 575 #if GNULIB_GC_MD2
 576     case GC_MD2:
 577       /* Not needed, because ctx is already zero-initialized.  */
 578       /*md2_init_ctx (&ctx->md2Context);*/
 579       break;
 580 #endif
 581 
 582 #if GNULIB_GC_MD4
 583     case GC_MD4:
 584       md4_init_ctx (&ctx->md4Context);
 585       break;
 586 #endif
 587 
 588 #if GNULIB_GC_MD5
 589     case GC_MD5:
 590       md5_init_ctx (&ctx->md5Context);
 591       break;
 592 #endif
 593 
 594 #if GNULIB_GC_SHA1
 595     case GC_SHA1:
 596       sha1_init_ctx (&ctx->sha1Context);
 597       break;
 598 #endif
 599 
 600 #if GNULIB_GC_SHA256
 601     case GC_SHA256:
 602       sha256_init_ctx (&ctx->sha256Context);
 603       break;
 604 #endif
 605 
 606 #if GNULIB_GC_SHA512
 607     case GC_SHA512:
 608       sha512_init_ctx (&ctx->sha512Context);
 609       break;
 610 #endif
 611 
 612 #if GNULIB_GC_SM3
 613     case GC_SM3:
 614       sm3_init_ctx (&ctx->sm3Context);
 615       break;
 616 #endif
 617 
 618     default:
 619       rc = GC_INVALID_HASH;
 620       break;
 621     }
 622 
 623   if (rc == GC_OK)
 624     *outhandle = ctx;
 625   else
 626     free (ctx);
 627 
 628   return rc;
 629 }
 630 
 631 Gc_rc
 632 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
     /* [previous][next][first][last][top][bottom][index][help] */
 633 {
 634   _gc_hash_ctx *in = handle;
 635   _gc_hash_ctx *out;
 636 
 637   *outhandle = out = calloc (sizeof (*out), 1);
 638   if (!out)
 639     return GC_MALLOC_ERROR;
 640 
 641   memcpy (out, in, sizeof (*out));
 642 
 643   return GC_OK;
 644 }
 645 
 646 size_t
 647 gc_hash_digest_length (Gc_hash hash)
     /* [previous][next][first][last][top][bottom][index][help] */
 648 {
 649   size_t len;
 650 
 651   switch (hash)
 652     {
 653     case GC_MD2:
 654       len = GC_MD2_DIGEST_SIZE;
 655       break;
 656 
 657     case GC_MD4:
 658       len = GC_MD4_DIGEST_SIZE;
 659       break;
 660 
 661     case GC_MD5:
 662       len = GC_MD5_DIGEST_SIZE;
 663       break;
 664 
 665     case GC_RMD160:
 666       len = GC_RMD160_DIGEST_SIZE;
 667       break;
 668 
 669     case GC_SHA1:
 670       len = GC_SHA1_DIGEST_SIZE;
 671       break;
 672 
 673     case GC_SHA256:
 674       len = GC_SHA256_DIGEST_SIZE;
 675       break;
 676 
 677     case GC_SHA512:
 678       len = GC_SHA512_DIGEST_SIZE;
 679       break;
 680 
 681     case GC_SM3:
 682       len = GC_SM3_DIGEST_SIZE;
 683       break;
 684 
 685     default:
 686       return 0;
 687     }
 688 
 689   return len;
 690 }
 691 
 692 void
 693 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 694 {
 695   _gc_hash_ctx *ctx = handle;
 696 
 697   switch (ctx->alg)
 698     {
 699 #if GNULIB_GC_MD2
 700     case GC_MD2:
 701       md2_process_bytes (data, len, &ctx->md2Context);
 702       break;
 703 #endif
 704 
 705 #if GNULIB_GC_MD4
 706     case GC_MD4:
 707       md4_process_bytes (data, len, &ctx->md4Context);
 708       break;
 709 #endif
 710 
 711 #if GNULIB_GC_MD5
 712     case GC_MD5:
 713       md5_process_bytes (data, len, &ctx->md5Context);
 714       break;
 715 #endif
 716 
 717 #if GNULIB_GC_SHA1
 718     case GC_SHA1:
 719       sha1_process_bytes (data, len, &ctx->sha1Context);
 720       break;
 721 #endif
 722 
 723 #if GNULIB_GC_SHA256
 724     case GC_SHA256:
 725       sha256_process_bytes (data, len, &ctx->sha256Context);
 726       break;
 727 #endif
 728 
 729 #if GNULIB_GC_SHA512
 730     case GC_SHA512:
 731       sha512_process_bytes (data, len, &ctx->sha512Context);
 732       break;
 733 #endif
 734 
 735 #if GNULIB_GC_SM3
 736     case GC_SM3:
 737       sm3_process_bytes (data, len, &ctx->sm3Context);
 738       break;
 739 #endif
 740 
 741     default:
 742       break;
 743     }
 744 }
 745 
 746 const char *
 747 gc_hash_read (gc_hash_handle handle)
     /* [previous][next][first][last][top][bottom][index][help] */
 748 {
 749   _gc_hash_ctx *ctx = handle;
 750   const char *ret = NULL;
 751 
 752   switch (ctx->alg)
 753     {
 754 #if GNULIB_GC_MD2
 755     case GC_MD2:
 756       md2_finish_ctx (&ctx->md2Context, ctx->hash);
 757       ret = ctx->hash;
 758       break;
 759 #endif
 760 
 761 #if GNULIB_GC_MD4
 762     case GC_MD4:
 763       md4_finish_ctx (&ctx->md4Context, ctx->hash);
 764       ret = ctx->hash;
 765       break;
 766 #endif
 767 
 768 #if GNULIB_GC_MD5
 769     case GC_MD5:
 770       md5_finish_ctx (&ctx->md5Context, ctx->hash);
 771       ret = ctx->hash;
 772       break;
 773 #endif
 774 
 775 #if GNULIB_GC_SHA1
 776     case GC_SHA1:
 777       sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
 778       ret = ctx->hash;
 779       break;
 780 #endif
 781 
 782 #if GNULIB_GC_SHA256
 783     case GC_SHA256:
 784       sha256_finish_ctx (&ctx->sha256Context, ctx->hash);
 785       ret = ctx->hash;
 786       break;
 787 #endif
 788 
 789 #if GNULIB_GC_SHA512
 790     case GC_SHA512:
 791       sha512_finish_ctx (&ctx->sha512Context, ctx->hash);
 792       ret = ctx->hash;
 793       break;
 794 #endif
 795 
 796 #if GNULIB_GC_SM3
 797     case GC_SM3:
 798       sm3_finish_ctx (&ctx->sm3Context, ctx->hash);
 799       ret = ctx->hash;
 800       break;
 801 #endif
 802 
 803     default:
 804       return NULL;
 805     }
 806 
 807   return ret;
 808 }
 809 
 810 void
 811 gc_hash_close (gc_hash_handle handle)
     /* [previous][next][first][last][top][bottom][index][help] */
 812 {
 813   _gc_hash_ctx *ctx = handle;
 814 
 815   free (ctx);
 816 }
 817 
 818 Gc_rc
 819 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 820 {
 821   switch (hash)
 822     {
 823 #if GNULIB_GC_MD2
 824     case GC_MD2:
 825       md2_buffer (in, inlen, resbuf);
 826       break;
 827 #endif
 828 
 829 #if GNULIB_GC_MD4
 830     case GC_MD4:
 831       md4_buffer (in, inlen, resbuf);
 832       break;
 833 #endif
 834 
 835 #if GNULIB_GC_MD5
 836     case GC_MD5:
 837       md5_buffer (in, inlen, resbuf);
 838       break;
 839 #endif
 840 
 841 #if GNULIB_GC_SHA1
 842     case GC_SHA1:
 843       sha1_buffer (in, inlen, resbuf);
 844       break;
 845 #endif
 846 
 847 #if GNULIB_GC_SHA256
 848     case GC_SHA256:
 849       sha256_buffer (in, inlen, resbuf);
 850       break;
 851 #endif
 852 
 853 #if GNULIB_GC_SHA512
 854     case GC_SHA512:
 855       sha512_buffer (in, inlen, resbuf);
 856       break;
 857 #endif
 858 
 859 #if GNULIB_GC_SM3
 860     case GC_SM3:
 861       sm3_buffer (in, inlen, resbuf);
 862       break;
 863 #endif
 864 
 865     default:
 866       return GC_INVALID_HASH;
 867     }
 868 
 869   return GC_OK;
 870 }
 871 
 872 #if GNULIB_GC_MD2
 873 Gc_rc
 874 gc_md2 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 875 {
 876   md2_buffer (in, inlen, resbuf);
 877   return GC_OK;
 878 }
 879 #endif
 880 
 881 #if GNULIB_GC_MD4
 882 Gc_rc
 883 gc_md4 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 884 {
 885   md4_buffer (in, inlen, resbuf);
 886   return GC_OK;
 887 }
 888 #endif
 889 
 890 #if GNULIB_GC_MD5
 891 Gc_rc
 892 gc_md5 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 893 {
 894   md5_buffer (in, inlen, resbuf);
 895   return GC_OK;
 896 }
 897 #endif
 898 
 899 #if GNULIB_GC_SHA1
 900 Gc_rc
 901 gc_sha1 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 902 {
 903   sha1_buffer (in, inlen, resbuf);
 904   return GC_OK;
 905 }
 906 #endif
 907 
 908 #if GNULIB_GC_SHA256
 909 Gc_rc
 910 gc_sha256 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 911 {
 912   sha256_buffer (in, inlen, resbuf);
 913   return GC_OK;
 914 }
 915 #endif
 916 
 917 #if GNULIB_GC_SHA512
 918 Gc_rc
 919 gc_sha512 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 920 {
 921   sha512_buffer (in, inlen, resbuf);
 922   return GC_OK;
 923 }
 924 #endif
 925 
 926 #if GNULIB_GC_SM3
 927 Gc_rc
 928 gc_sm3 (const void *in, size_t inlen, void *resbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 929 {
 930   sm3_buffer (in, inlen, resbuf);
 931   return GC_OK;
 932 }
 933 #endif
 934 
 935 #if GNULIB_GC_HMAC_MD5
 936 Gc_rc
 937 gc_hmac_md5 (const void *key, size_t keylen,
     /* [previous][next][first][last][top][bottom][index][help] */
 938              const void *in, size_t inlen, char *resbuf)
 939 {
 940   hmac_md5 (key, keylen, in, inlen, resbuf);
 941   return GC_OK;
 942 }
 943 #endif
 944 
 945 #if GNULIB_GC_HMAC_SHA1
 946 Gc_rc
 947 gc_hmac_sha1 (const void *key, size_t keylen,
     /* [previous][next][first][last][top][bottom][index][help] */
 948               const void *in, size_t inlen, char *resbuf)
 949 {
 950   hmac_sha1 (key, keylen, in, inlen, resbuf);
 951   return GC_OK;
 952 }
 953 #endif
 954 
 955 #if GNULIB_GC_HMAC_SHA256
 956 Gc_rc
 957 gc_hmac_sha256 (const void *key, size_t keylen,
     /* [previous][next][first][last][top][bottom][index][help] */
 958                 const void *in, size_t inlen, char *resbuf)
 959 {
 960   hmac_sha256 (key, keylen, in, inlen, resbuf);
 961   return GC_OK;
 962 }
 963 #endif
 964 
 965 #if GNULIB_GC_HMAC_SHA512
 966 Gc_rc
 967 gc_hmac_sha512 (const void *key, size_t keylen,
     /* [previous][next][first][last][top][bottom][index][help] */
 968                 const void *in, size_t inlen, char *resbuf)
 969 {
 970   hmac_sha512 (key, keylen, in, inlen, resbuf);
 971   return GC_OK;
 972 }
 973 #endif

/* [previous][next][first][last][top][bottom][index][help] */