floor, ceil, trunc, truncf, truncl: Defeat GCC optimizations.
[gnulib.git] / lib / gc-libgcrypt.c
blob140d4bcaa4d59ad153e5aae6e03ddaf7192ee94a
1 /* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2 * Copyright (C) 2002-2018 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 Libgcrypt. */
21 #include <config.h>
23 /* Get prototype. */
24 #include "gc.h"
26 #include <stdlib.h>
27 #include <string.h>
29 /* Get libgcrypt API. */
30 #include <gcrypt.h>
31 #ifdef GNULIB_GC_MD2
32 # include "md2.h"
33 #endif
35 #include <assert.h>
37 #ifndef MIN_GCRYPT_VERSION
38 # define MIN_GCRYPT_VERSION "1.4.4"
39 #endif
41 /* Initialization. */
43 Gc_rc
44 gc_init (void)
46 gcry_error_t err;
48 err = gcry_control (GCRYCTL_ANY_INITIALIZATION_P);
49 if (err == GPG_ERR_NO_ERROR)
51 if (gcry_control (GCRYCTL_DISABLE_SECMEM, NULL, 0))
52 return GC_INIT_ERROR;
54 if (gcry_check_version (MIN_GCRYPT_VERSION) == NULL)
55 return GC_INIT_ERROR;
57 err = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
58 if (err != GPG_ERR_NO_ERROR)
59 return GC_INIT_ERROR;
62 return GC_OK;
65 void
66 gc_done (void)
68 return;
71 #ifdef GNULIB_GC_RANDOM
73 /* Randomness. */
75 Gc_rc
76 gc_nonce (char *data, size_t datalen)
78 gcry_create_nonce ((unsigned char *) data, datalen);
79 return GC_OK;
82 Gc_rc
83 gc_pseudo_random (char *data, size_t datalen)
85 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
86 return GC_OK;
89 Gc_rc
90 gc_random (char *data, size_t datalen)
92 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
93 return GC_OK;
96 #endif
98 /* Memory allocation. */
100 void
101 gc_set_allocators (gc_malloc_t func_malloc,
102 gc_malloc_t secure_malloc,
103 gc_secure_check_t secure_check,
104 gc_realloc_t func_realloc, gc_free_t func_free)
106 gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
107 func_realloc, func_free);
110 /* Ciphers. */
112 Gc_rc
113 gc_cipher_open (Gc_cipher alg, Gc_cipher_mode mode,
114 gc_cipher_handle * outhandle)
116 int gcryalg, gcrymode;
117 gcry_error_t err;
119 switch (alg)
121 case GC_AES128:
122 gcryalg = GCRY_CIPHER_RIJNDAEL;
123 break;
125 case GC_AES192:
126 gcryalg = GCRY_CIPHER_RIJNDAEL;
127 break;
129 case GC_AES256:
130 gcryalg = GCRY_CIPHER_RIJNDAEL256;
131 break;
133 case GC_3DES:
134 gcryalg = GCRY_CIPHER_3DES;
135 break;
137 case GC_DES:
138 gcryalg = GCRY_CIPHER_DES;
139 break;
141 case GC_ARCFOUR128:
142 case GC_ARCFOUR40:
143 gcryalg = GCRY_CIPHER_ARCFOUR;
144 break;
146 case GC_ARCTWO40:
147 gcryalg = GCRY_CIPHER_RFC2268_40;
148 break;
150 #ifdef HAVE_CAMELLIA
151 case GC_CAMELLIA128:
152 gcryalg = GCRY_CIPHER_CAMELLIA128;
153 break;
155 case GC_CAMELLIA256:
156 gcryalg = GCRY_CIPHER_CAMELLIA256;
157 break;
158 #endif
160 default:
161 return GC_INVALID_CIPHER;
164 switch (mode)
166 case GC_ECB:
167 gcrymode = GCRY_CIPHER_MODE_ECB;
168 break;
170 case GC_CBC:
171 gcrymode = GCRY_CIPHER_MODE_CBC;
172 break;
174 case GC_STREAM:
175 gcrymode = GCRY_CIPHER_MODE_STREAM;
176 break;
178 default:
179 return GC_INVALID_CIPHER;
182 err = gcry_cipher_open ((gcry_cipher_hd_t *) outhandle,
183 gcryalg, gcrymode, 0);
184 if (gcry_err_code (err))
185 return GC_INVALID_CIPHER;
187 return GC_OK;
190 Gc_rc
191 gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key)
193 gcry_error_t err;
195 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
196 if (gcry_err_code (err))
197 return GC_INVALID_CIPHER;
199 return GC_OK;
202 Gc_rc
203 gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv)
205 gcry_error_t err;
207 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
208 if (gcry_err_code (err))
209 return GC_INVALID_CIPHER;
211 return GC_OK;
214 Gc_rc
215 gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data)
217 if (gcry_cipher_encrypt ((gcry_cipher_hd_t) handle,
218 data, len, NULL, len) != 0)
219 return GC_INVALID_CIPHER;
221 return GC_OK;
224 Gc_rc
225 gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data)
227 if (gcry_cipher_decrypt ((gcry_cipher_hd_t) handle,
228 data, len, NULL, len) != 0)
229 return GC_INVALID_CIPHER;
231 return GC_OK;
234 Gc_rc
235 gc_cipher_close (gc_cipher_handle handle)
237 gcry_cipher_close (handle);
239 return GC_OK;
242 /* Hashes. */
244 typedef struct _gc_hash_ctx {
245 Gc_hash alg;
246 Gc_hash_mode mode;
247 gcry_md_hd_t gch;
248 #ifdef GNULIB_GC_MD2
249 char hash[GC_MD2_DIGEST_SIZE];
250 struct md2_ctx md2Context;
251 #endif
252 } _gc_hash_ctx;
254 Gc_rc
255 gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle)
257 _gc_hash_ctx *ctx;
258 int gcryalg = 0, gcrymode = 0;
259 gcry_error_t err;
260 Gc_rc rc = GC_OK;
262 ctx = calloc (sizeof (*ctx), 1);
263 if (!ctx)
264 return GC_MALLOC_ERROR;
266 ctx->alg = hash;
267 ctx->mode = mode;
269 switch (hash)
271 case GC_MD2:
272 gcryalg = GCRY_MD_NONE;
273 break;
275 case GC_MD4:
276 gcryalg = GCRY_MD_MD4;
277 break;
279 case GC_MD5:
280 gcryalg = GCRY_MD_MD5;
281 break;
283 case GC_SHA1:
284 gcryalg = GCRY_MD_SHA1;
285 break;
287 case GC_SHA256:
288 gcryalg = GCRY_MD_SHA256;
289 break;
291 case GC_SHA384:
292 gcryalg = GCRY_MD_SHA384;
293 break;
295 case GC_SHA512:
296 gcryalg = GCRY_MD_SHA512;
297 break;
299 case GC_SHA224:
300 gcryalg = GCRY_MD_SHA224;
301 break;
303 case GC_RMD160:
304 gcryalg = GCRY_MD_RMD160;
305 break;
307 case GC_SM3:
308 gcryalg = GCRY_MD_SM3;
309 break;
311 default:
312 rc = GC_INVALID_HASH;
315 switch (mode)
317 case GC_NULL:
318 gcrymode = 0;
319 break;
321 case GC_HMAC:
322 gcrymode = GCRY_MD_FLAG_HMAC;
323 break;
325 default:
326 rc = GC_INVALID_HASH;
329 if (rc == GC_OK && gcryalg != GCRY_MD_NONE)
331 err = gcry_md_open (&ctx->gch, gcryalg, gcrymode);
332 if (gcry_err_code (err))
333 rc = GC_INVALID_HASH;
336 if (rc == GC_OK)
337 *outhandle = ctx;
338 else
339 free (ctx);
341 return rc;
344 Gc_rc
345 gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle)
347 _gc_hash_ctx *in = handle;
348 _gc_hash_ctx *out;
349 int err;
351 *outhandle = out = calloc (sizeof (*out), 1);
352 if (!out)
353 return GC_MALLOC_ERROR;
355 memcpy (out, in, sizeof (*out));
357 err = gcry_md_copy (&out->gch, in->gch);
358 if (err)
360 free (out);
361 return GC_INVALID_HASH;
364 return GC_OK;
367 size_t
368 gc_hash_digest_length (Gc_hash hash)
370 size_t len;
372 switch (hash)
374 case GC_MD2:
375 len = GC_MD2_DIGEST_SIZE;
376 break;
378 case GC_MD4:
379 len = GC_MD4_DIGEST_SIZE;
380 break;
382 case GC_MD5:
383 len = GC_MD5_DIGEST_SIZE;
384 break;
386 case GC_RMD160:
387 len = GC_RMD160_DIGEST_SIZE;
388 break;
390 case GC_SHA1:
391 len = GC_SHA1_DIGEST_SIZE;
392 break;
394 case GC_SHA256:
395 len = GC_SHA256_DIGEST_SIZE;
396 break;
398 case GC_SHA384:
399 len = GC_SHA384_DIGEST_SIZE;
400 break;
402 case GC_SHA512:
403 len = GC_SHA512_DIGEST_SIZE;
404 break;
406 case GC_SHA224:
407 len = GC_SHA224_DIGEST_SIZE;
408 break;
410 case GC_SM3:
411 len = GC_SM3_DIGEST_SIZE;
412 break;
414 default:
415 return 0;
418 return len;
421 void
422 gc_hash_hmac_setkey (gc_hash_handle handle, size_t len, const char *key)
424 _gc_hash_ctx *ctx = handle;
425 #ifdef GNULIB_GC_MD2
426 if (ctx->alg != GC_MD2)
427 #endif
428 gcry_md_setkey (ctx->gch, key, len);
431 void
432 gc_hash_write (gc_hash_handle handle, size_t len, const char *data)
434 _gc_hash_ctx *ctx = handle;
436 #ifdef GNULIB_GC_MD2
437 if (ctx->alg == GC_MD2)
438 md2_process_bytes (data, len, &ctx->md2Context);
439 else
440 #endif
441 gcry_md_write (ctx->gch, data, len);
444 const char *
445 gc_hash_read (gc_hash_handle handle)
447 _gc_hash_ctx *ctx = handle;
448 const char *digest;
450 #ifdef GNULIB_GC_MD2
451 if (ctx->alg == GC_MD2)
453 md2_finish_ctx (&ctx->md2Context, ctx->hash);
454 digest = ctx->hash;
456 else
457 #endif
459 gcry_md_final (ctx->gch);
460 digest = (const char *) gcry_md_read (ctx->gch, 0);
463 return digest;
466 void
467 gc_hash_close (gc_hash_handle handle)
469 _gc_hash_ctx *ctx = handle;
471 #ifdef GNULIB_GC_MD2
472 if (ctx->alg != GC_MD2)
473 #endif
474 gcry_md_close (ctx->gch);
476 free (ctx);
479 Gc_rc
480 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
482 int gcryalg;
484 switch (hash)
486 #ifdef GNULIB_GC_MD2
487 case GC_MD2:
488 md2_buffer (in, inlen, resbuf);
489 return GC_OK;
490 break;
491 #endif
493 #ifdef GNULIB_GC_MD4
494 case GC_MD4:
495 gcryalg = GCRY_MD_MD4;
496 break;
497 #endif
499 #ifdef GNULIB_GC_MD5
500 case GC_MD5:
501 gcryalg = GCRY_MD_MD5;
502 break;
503 #endif
505 #ifdef GNULIB_GC_SHA1
506 case GC_SHA1:
507 gcryalg = GCRY_MD_SHA1;
508 break;
509 #endif
511 #ifdef GNULIB_GC_SHA256
512 case GC_SHA256:
513 gcryalg = GCRY_MD_SHA256;
514 break;
515 #endif
517 #ifdef GNULIB_GC_SHA384
518 case GC_SHA384:
519 gcryalg = GCRY_MD_SHA384;
520 break;
521 #endif
523 #ifdef GNULIB_GC_SHA512
524 case GC_SHA512:
525 gcryalg = GCRY_MD_SHA512;
526 break;
527 #endif
529 #ifdef GNULIB_GC_SHA224
530 case GC_SHA224:
531 gcryalg = GCRY_MD_SHA224;
532 break;
533 #endif
535 #ifdef GNULIB_GC_RMD160
536 case GC_RMD160:
537 gcryalg = GCRY_MD_RMD160;
538 break;
539 #endif
541 #ifdef GNULIB_GC_SM3
542 case GC_SM3:
543 gcryalg = GCRY_MD_SM3;
544 break;
545 #endif
547 default:
548 return GC_INVALID_HASH;
551 gcry_md_hash_buffer (gcryalg, resbuf, in, inlen);
553 return GC_OK;
556 /* One-call interface. */
558 #ifdef GNULIB_GC_MD2
559 Gc_rc
560 gc_md2 (const void *in, size_t inlen, void *resbuf)
562 md2_buffer (in, inlen, resbuf);
563 return GC_OK;
565 #endif
567 #ifdef GNULIB_GC_MD4
568 Gc_rc
569 gc_md4 (const void *in, size_t inlen, void *resbuf)
571 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
572 gcry_md_hd_t hd;
573 gpg_error_t err;
574 unsigned char *p;
576 assert (outlen == GC_MD4_DIGEST_SIZE);
578 err = gcry_md_open (&hd, GCRY_MD_MD4, 0);
579 if (err != GPG_ERR_NO_ERROR)
580 return GC_INVALID_HASH;
582 gcry_md_write (hd, in, inlen);
584 p = gcry_md_read (hd, GCRY_MD_MD4);
585 if (p == NULL)
587 gcry_md_close (hd);
588 return GC_INVALID_HASH;
591 memcpy (resbuf, p, outlen);
593 gcry_md_close (hd);
595 return GC_OK;
597 #endif
599 #ifdef GNULIB_GC_MD5
600 Gc_rc
601 gc_md5 (const void *in, size_t inlen, void *resbuf)
603 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
604 gcry_md_hd_t hd;
605 gpg_error_t err;
606 unsigned char *p;
608 assert (outlen == GC_MD5_DIGEST_SIZE);
610 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
611 if (err != GPG_ERR_NO_ERROR)
612 return GC_INVALID_HASH;
614 gcry_md_write (hd, in, inlen);
616 p = gcry_md_read (hd, GCRY_MD_MD5);
617 if (p == NULL)
619 gcry_md_close (hd);
620 return GC_INVALID_HASH;
623 memcpy (resbuf, p, outlen);
625 gcry_md_close (hd);
627 return GC_OK;
629 #endif
631 #ifdef GNULIB_GC_SHA1
632 Gc_rc
633 gc_sha1 (const void *in, size_t inlen, void *resbuf)
635 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
636 gcry_md_hd_t hd;
637 gpg_error_t err;
638 unsigned char *p;
640 assert (outlen == GC_SHA1_DIGEST_SIZE);
642 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
643 if (err != GPG_ERR_NO_ERROR)
644 return GC_INVALID_HASH;
646 gcry_md_write (hd, in, inlen);
648 p = gcry_md_read (hd, GCRY_MD_SHA1);
649 if (p == NULL)
651 gcry_md_close (hd);
652 return GC_INVALID_HASH;
655 memcpy (resbuf, p, outlen);
657 gcry_md_close (hd);
659 return GC_OK;
661 #endif
663 #ifdef GNULIB_GC_SM3
664 Gc_rc
665 gc_sm3 (const void *in, size_t inlen, void *resbuf)
667 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SM3);
668 gcry_md_hd_t hd;
669 gpg_error_t err;
670 unsigned char *p;
672 assert (outlen == GC_SM3_DIGEST_SIZE);
674 err = gcry_md_open (&hd, GCRY_MD_SM3, 0);
675 if (err != GPG_ERR_NO_ERROR)
676 return GC_INVALID_HASH;
678 gcry_md_write (hd, in, inlen);
680 p = gcry_md_read (hd, GCRY_MD_SM3);
681 if (p == NULL)
683 gcry_md_close (hd);
684 return GC_INVALID_HASH;
687 memcpy (resbuf, p, outlen);
689 gcry_md_close (hd);
691 return GC_OK;
693 #endif
695 #ifdef GNULIB_GC_HMAC_MD5
696 Gc_rc
697 gc_hmac_md5 (const void *key, size_t keylen,
698 const void *in, size_t inlen, char *resbuf)
700 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
701 gcry_md_hd_t mdh;
702 unsigned char *hash;
703 gpg_error_t err;
705 assert (hlen == GC_MD5_DIGEST_SIZE);
707 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
708 if (err != GPG_ERR_NO_ERROR)
709 return GC_INVALID_HASH;
711 err = gcry_md_setkey (mdh, key, keylen);
712 if (err != GPG_ERR_NO_ERROR)
714 gcry_md_close (mdh);
715 return GC_INVALID_HASH;
718 gcry_md_write (mdh, in, inlen);
720 hash = gcry_md_read (mdh, GCRY_MD_MD5);
721 if (hash == NULL)
723 gcry_md_close (mdh);
724 return GC_INVALID_HASH;
727 memcpy (resbuf, hash, hlen);
729 gcry_md_close (mdh);
731 return GC_OK;
733 #endif
735 #ifdef GNULIB_GC_HMAC_SHA1
736 Gc_rc
737 gc_hmac_sha1 (const void *key, size_t keylen,
738 const void *in, size_t inlen, char *resbuf)
740 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
741 gcry_md_hd_t mdh;
742 unsigned char *hash;
743 gpg_error_t err;
745 assert (hlen == GC_SHA1_DIGEST_SIZE);
747 err = gcry_md_open (&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
748 if (err != GPG_ERR_NO_ERROR)
749 return GC_INVALID_HASH;
751 err = gcry_md_setkey (mdh, key, keylen);
752 if (err != GPG_ERR_NO_ERROR)
754 gcry_md_close (mdh);
755 return GC_INVALID_HASH;
758 gcry_md_write (mdh, in, inlen);
760 hash = gcry_md_read (mdh, GCRY_MD_SHA1);
761 if (hash == NULL)
763 gcry_md_close (mdh);
764 return GC_INVALID_HASH;
767 memcpy (resbuf, hash, hlen);
769 gcry_md_close (mdh);
771 return GC_OK;
773 #endif
775 #ifdef GNULIB_GC_HMAC_SHA256
776 Gc_rc
777 gc_hmac_sha256 (const void *key, size_t keylen,
778 const void *in, size_t inlen, char *resbuf)
780 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA256);
781 gcry_md_hd_t mdh;
782 unsigned char *hash;
783 gpg_error_t err;
785 assert (hlen == GC_SHA256_DIGEST_SIZE);
787 err = gcry_md_open (&mdh, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
788 if (err != GPG_ERR_NO_ERROR)
789 return GC_INVALID_HASH;
791 err = gcry_md_setkey (mdh, key, keylen);
792 if (err != GPG_ERR_NO_ERROR)
794 gcry_md_close (mdh);
795 return GC_INVALID_HASH;
798 gcry_md_write (mdh, in, inlen);
800 hash = gcry_md_read (mdh, GCRY_MD_SHA256);
801 if (hash == NULL)
803 gcry_md_close (mdh);
804 return GC_INVALID_HASH;
807 memcpy (resbuf, hash, hlen);
809 gcry_md_close (mdh);
811 return GC_OK;
813 #endif
815 #ifdef GNULIB_GC_HMAC_SHA512
816 Gc_rc
817 gc_hmac_sha512 (const void *key, size_t keylen,
818 const void *in, size_t inlen, char *resbuf)
820 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA512);
821 gcry_md_hd_t mdh;
822 unsigned char *hash;
823 gpg_error_t err;
825 assert (hlen == GC_SHA512_DIGEST_SIZE);
827 err = gcry_md_open (&mdh, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC);
828 if (err != GPG_ERR_NO_ERROR)
829 return GC_INVALID_HASH;
831 err = gcry_md_setkey (mdh, key, keylen);
832 if (err != GPG_ERR_NO_ERROR)
834 gcry_md_close (mdh);
835 return GC_INVALID_HASH;
838 gcry_md_write (mdh, in, inlen);
840 hash = gcry_md_read (mdh, GCRY_MD_SHA512);
841 if (hash == NULL)
843 gcry_md_close (mdh);
844 return GC_INVALID_HASH;
847 memcpy (resbuf, hash, hlen);
849 gcry_md_close (mdh);
851 return GC_OK;
853 #endif