This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathSMP.patch
More file actions
1182 lines (1126 loc) · 37.3 KB
/
SMP.patch
File metadata and controls
1182 lines (1126 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/NEWS b/NEWS
index a06199e7..745dd5b4 100644
--- a/NEWS
+++ b/NEWS
@@ -397,7 +397,7 @@ Noteworthy changes in version 1.9.3 (2021-04-19) [C23/A3/R3]
- Make keygrip computation work for compressed points. [#4961]
-* Performance:
+ * Performance:
- Add x86_64 VAES/AVX2 accelerated implementation of Camellia.
[0e7e60241a]
diff --git a/autogen.sh b/autogen.sh
index 9b361581..8aaa998b 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -505,8 +505,8 @@ info "Running $ACLOCAL ${aclocal_flags} ..."
$ACLOCAL ${aclocal_flags}
info "Running autoheader..."
$AUTOHEADER
-info "Running automake --gnu ..."
-$AUTOMAKE --gnu;
+info "Running automake --gnu --add-missing ..."
+$AUTOMAKE --gnu --add-missing;
info "Running autoconf${FORCE} ..."
$AUTOCONF${FORCE}
diff --git a/build-aux/ltmain.sh b/build-aux/ltmain.sh
index 859599aa..bafa8fbb 100644
--- a/build-aux/ltmain.sh
+++ b/build-aux/ltmain.sh
@@ -5686,8 +5686,8 @@ func_mode_link ()
*-*-cygwin* | *-*-mingw* | *-*-pw32* | *-*-os2* | *-*-darwin* | *-cegcc*)
# The PATH hackery in wrapper scripts is required on Windows
# and Darwin in order for the loader to find any dlls it needs.
- func_warning "\`-no-install' is ignored for $host"
- func_warning "assuming \`-no-fast-install' instead"
+ # func_warning "\`-no-install' is ignored for $host"
+ # func_warning "assuming \`-no-fast-install' instead"
fast_install=no
;;
*) no_install=yes ;;
diff --git a/cipher/bithelp.h b/cipher/bithelp.h
index 7793ce7c..78578768 100644
--- a/cipher/bithelp.h
+++ b/cipher/bithelp.h
@@ -44,6 +44,8 @@ static inline u64 rol64(u64 x, int n)
provided helpers. */
#ifdef HAVE_BUILTIN_BSWAP32
# define _gcry_bswap32 __builtin_bswap32
+#elif defined(_MSC_VER)
+# define _gcry_bswap32 _byteswap_ulong
#else
static inline u32
_gcry_bswap32(u32 x)
@@ -54,6 +56,8 @@ _gcry_bswap32(u32 x)
#ifdef HAVE_BUILTIN_BSWAP64
# define _gcry_bswap64 __builtin_bswap64
+#elif defined(_MSC_VER)
+# define _gcry_bswap64 _byteswap_uint64
#else
static inline u64
_gcry_bswap64(u64 x)
@@ -84,6 +88,13 @@ _gcry_ctz (unsigned int x)
{
#if defined (HAVE_BUILTIN_CTZ)
return x ? __builtin_ctz (x) : 8 * sizeof (x);
+#elif defined(_MSC_VER)
+ if (x) {
+ unsigned long ret;
+ _BitScanForward(&ret, x);
+ return ret;
+ }
+ return 8 * sizeof(x);
#else
/* See
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup
@@ -111,6 +122,13 @@ _gcry_ctz64(u64 x)
#elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8
#warning hello
return x ? __builtin_ctz (x) : 8 * sizeof (x);
+#elif defined(_MSC_VER) && defined(_WIN64)
+ if (x) {
+ unsigned long ret;
+ _BitScanForward64(&ret, x);
+ return (int)ret;
+ }
+ return 8 * sizeof(x);
#else
if ((x & 0xffffffff))
return _gcry_ctz (x);
diff --git a/cipher/cipher-internal.h b/cipher/cipher-internal.h
index cd8ff788..dac54ec1 100644
--- a/cipher/cipher-internal.h
+++ b/cipher/cipher-internal.h
@@ -66,7 +66,7 @@
#undef GCM_USE_INTEL_PCLMUL
#if defined(ENABLE_PCLMUL_SUPPORT) && defined(GCM_USE_TABLES)
# if ((defined(__i386__) && SIZEOF_UNSIGNED_LONG == 4) || defined(__x86_64__))
-# if __GNUC__ >= 4
+# if (__GNUC__ >= 4) || defined(__INTEL_COMPILER)
# define GCM_USE_INTEL_PCLMUL 1
# endif
# endif
diff --git a/cipher/cipher-poly1305.c b/cipher/cipher-poly1305.c
index c76dd9a4..0c28e652 100644
--- a/cipher/cipher-poly1305.c
+++ b/cipher/cipher-poly1305.c
@@ -71,7 +71,7 @@ poly1305_fill_bytecounts (gcry_cipher_hd_t c)
static void
poly1305_do_padding (gcry_cipher_hd_t c, u32 ctr[2])
{
- static const byte zero_padding_buf[15] = {};
+ static const byte zero_padding_buf[15] = {0};
u32 padding_count;
/* Padding to 16 byte boundary. */
diff --git a/cipher/des.c b/cipher/des.c
index 4b3f9a1e..fc17c8af 100644
--- a/cipher/des.c
+++ b/cipher/des.c
@@ -139,7 +139,7 @@
# define ATTR_ALIGNED_16
#endif
-#if defined(__GNUC__) && defined(__GNU_LIBRARY__)
+#if (defined(__GNUC__) && defined(__GNU_LIBRARY__)) || defined(_MSC_VER)
# define working_memcmp memcmp
#else
/*
diff --git a/cipher/pubkey-util.c b/cipher/pubkey-util.c
index 68defea6..244dd5d4 100644
--- a/cipher/pubkey-util.c
+++ b/cipher/pubkey-util.c
@@ -957,7 +957,10 @@ _gcry_pk_util_data_to_mpi (gcry_sexp_t input, gcry_mpi_t *ret_mpi,
void *random_override = NULL;
size_t random_override_len = 0;
- if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen )
+ /* The RSA PKCS#1.5 encryption is no longer supported by FIPS */
+ if (fips_mode ())
+ rc = GPG_ERR_INV_FLAG;
+ else if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen )
rc = GPG_ERR_INV_OBJ;
else
{
@@ -1089,7 +1092,10 @@ _gcry_pk_util_data_to_mpi (gcry_sexp_t input, gcry_mpi_t *ret_mpi,
const void * value;
size_t valuelen;
- if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen )
+ /* The RSA OAEP encryption requires some more assurances in FIPS */
+ if (fips_mode ())
+ rc = GPG_ERR_INV_FLAG;
+ else if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen )
rc = GPG_ERR_INV_OBJ;
else
{
diff --git a/cipher/pubkey.c b/cipher/pubkey.c
index 214bd611..f4c17d2d 100644
--- a/cipher/pubkey.c
+++ b/cipher/pubkey.c
@@ -672,6 +672,30 @@ _gcry_pk_verify_md (gcry_sexp_t s_sig, const char *tmpl, gcry_md_hd_t hd_orig,
gcry_sexp_t s_data = NULL;
gcry_error_t err;
gcry_md_hd_t hd;
+ const char *s;
+ char *hash_name;
+
+ /* Check if it has fixed hash name or %s */
+ s = strstr (tmpl, "(hash ");
+ if (s == NULL)
+ return GPG_ERR_DIGEST_ALGO;
+
+ s += 6;
+ if (!strncmp (s, "%s", 2))
+ hash_name = NULL;
+ else
+ {
+ const char *p;
+
+ for (p = s; *p && *p != ' '; p++)
+ ;
+
+ hash_name = xtrymalloc (p - s + 1);
+ if (!hash_name)
+ return gpg_error_from_syserror ();
+ memcpy (hash_name, s, p - s);
+ hash_name[p - s] = 0;
+ }
if (!hd_orig)
hd = NULL;
diff --git a/cipher/rsa.c b/cipher/rsa.c
index c7a809f4..bbf7eaed 100644
--- a/cipher/rsa.c
+++ b/cipher/rsa.c
@@ -1468,6 +1468,12 @@ rsa_decrypt (gcry_sexp_t *r_plain, gcry_sexp_t s_data, gcry_sexp_t keyparms)
rc = GPG_ERR_INV_DATA;
goto leave;
}
+ if (fips_mode () && (ctx.encoding == PUBKEY_ENC_PKCS1 ||
+ ctx.encoding == PUBKEY_ENC_OAEP))
+ {
+ rc = GPG_ERR_INV_FLAG;
+ goto leave;
+ }
/* Extract the key. */
rc = sexp_extract_param (keyparms, NULL, "nedp?q?u?",
diff --git a/cipher/stribog.c b/cipher/stribog.c
index 21e385ae..f894bcf6 100644
--- a/cipher/stribog.c
+++ b/cipher/stribog.c
@@ -1286,7 +1286,7 @@ static void
stribog_final (void *context)
{
STRIBOG_CONTEXT *hd = context;
- u64 Z[8] = {};
+ u64 Z[8] = {0};
int i;
/* PAD. It does not count towards message length */
diff --git a/compat/compat.c b/compat/compat.c
index 7684e4f3..86745fc6 100644
--- a/compat/compat.c
+++ b/compat/compat.c
@@ -30,6 +30,8 @@ _gcry_compat_identification (void)
static const char blurb[] =
"\n\n"
"This is Libgcrypt " PACKAGE_VERSION " - The GNU Crypto Library\n"
+ "Copyright (C) 2012-2022 g10 Code GmbH\n"
+ "Copyright (C) 2013-2022 Jussi Kivilinna\n"
"Copyright (C) 2000-2018 Free Software Foundation, Inc.\n"
"Copyright (C) 2012-2024 g10 Code GmbH\n"
"Copyright (C) 2013-2024 Jussi Kivilinna\n"
diff --git a/configure.ac b/configure.ac
index 1d06ca3b..43ad0712 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1281,7 +1281,8 @@ AC_CACHE_CHECK([whether GCC assembler supports for CFI directives],
".cfi_restore_state\n\t"
".long 0\n\t"
".cfi_endproc\n\t"
- );]])],
+ );
+ void asmfunc(void)]])],
[gcry_cv_gcc_asm_cfi_directives=yes])])
if test "$gcry_cv_gcc_asm_cfi_directives" = "yes" ; then
AC_DEFINE(HAVE_GCC_ASM_CFI_DIRECTIVES,1,
diff --git a/mpi/ec.c b/mpi/ec.c
index 2f8a25a4..11100fa4 100644
--- a/mpi/ec.c
+++ b/mpi/ec.c
@@ -30,6 +30,12 @@
#include "ec-context.h"
#include "ec-internal.h"
+#ifdef HAVE_W32_SYSTEM
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+# define getenv(x) NULL
+# endif
+#endif
+
extern void reverse_buffer (unsigned char *buffer, unsigned int length);
#define point_init(a) _gcry_mpi_point_init ((a))
diff --git a/mpi/generic/mpi-asm-defs.h b/mpi/generic/mpi-asm-defs.h
index e607806e..111859c3 100644
--- a/mpi/generic/mpi-asm-defs.h
+++ b/mpi/generic/mpi-asm-defs.h
@@ -1,7 +1,7 @@
/* This file defines some basic constants for the MPI machinery.
* AMD64 compiled for the x32 ABI is special and thus we can't use the
* standard values for this ABI. */
-#if __GNUC__ >= 3 && defined(__x86_64__) && defined(__ILP32__)
+#if (__GNUC__ >= 3 && defined(__x86_64__) && defined(__ILP32__)) || (defined(_WIN64) || defined(_MSC_VER) && (defined(__x86_64__) || defined(__x86_64) || defined(_M_X64)))
#define BYTES_PER_MPI_LIMB 8
#else
#define BYTES_PER_MPI_LIMB (SIZEOF_UNSIGNED_LONG)
diff --git a/mpi/longlong.h b/mpi/longlong.h
index 21bd1a7e..ceed070d 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -108,7 +108,7 @@ SPDX-License-Identifier: LGPL-2.1-or-later
#ifdef __riscos__
#pragma continue_after_hash_error
#else /* !__riscos__ */
-#if defined (__GNUC__) && !defined (NO_ASM)
+#if ( defined (__GNUC__) || defined(__INTEL_COMPILER) ) && !defined (NO_ASM)
/* We sometimes need to clobber "cc" with gcc2, but that would not be
understood by gcc1. Use cpp to avoid major code duplication. */
@@ -1745,6 +1745,14 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
# elif defined (HAVE_BUILTIN_CLZ) && SIZEOF_UNSIGNED_INT * 8 == W_TYPE_SIZE
# define count_leading_zeros(count, x) (count = __builtin_clz(x))
# undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+# elif defined (_MSC_VER) && SIZEOF_UNSIGNED_INT * 8 == W_TYPE_SIZE
+static inline int msvc_clz(unsigned int x) { unsigned int count; _BitScanReverse(&count, x); return 31 - count; }
+# define count_leading_zeros(count, x) (count = msvc_clz(x))
+# undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+# elif defined (_MSC_VER) && (defined(_WIN64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64)) && SIZEOF_UNSIGNED_LONG_LONG * 8 == W_TYPE_SIZE
+static inline int msvc_clz(unsigned long long x) { unsigned int count; _BitScanReverse64(&count, x); return 63 - count; }
+# define count_leading_zeros(count, x) (count = msvc_clz(x))
+# undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
# endif
#endif
@@ -1755,6 +1763,12 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
# elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT * 8 == W_TYPE_SIZE
# define count_trailing_zeros(count, x) (count = __builtin_ctz(x))
# undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+# elif defined (_MSC_VER) && SIZEOF_UNSIGNED_INT * 8 == W_TYPE_SIZE
+# define count_trailing_zeros(count, x) (_BitScanForward(&count, x))
+# undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+# elif defined (_MSC_VER) && (defined(_WIN64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64)) && SIZEOF_UNSIGNED_LONG_LONG * 8 == W_TYPE_SIZE
+# define count_trailing_zeros(count, x) (_BitScanForward64(&count, x))
+# undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
# endif
#endif
diff --git a/random/rand-internal.h b/random/rand-internal.h
index 3d7a582f..3bfa695c 100644
--- a/random/rand-internal.h
+++ b/random/rand-internal.h
@@ -129,6 +129,15 @@ int _gcry_rndw32ce_gather_random (void (*add) (const void *, size_t,
void _gcry_rndw32ce_gather_random_fast (void (*add)(const void*, size_t,
enum random_origins),
enum random_origins origin );
+/*-- rndw32uwp.cpp --*/
+int _gcry_rnduwp_gather_random (void (*add) (const void *, size_t,
+ enum random_origins),
+ enum random_origins origin,
+ size_t length, int level );
+
+void _gcry_rnduwp_gather_random_fast (void (*add) (const void *, size_t,
+ enum random_origins),
+ enum random_origins origin );
/*-- rndjent.c --*/
size_t _gcry_rndjent_poll (void (*add)(const void*,
diff --git a/random/random-csprng.c b/random/random-csprng.c
index 4f34acc0..6c3e2a08 100644
--- a/random/random-csprng.c
+++ b/random/random-csprng.c
@@ -37,10 +37,14 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
+#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
+#endif
#include <sys/types.h>
#include <sys/stat.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#include <fcntl.h>
#include <time.h>
#ifdef HAVE_GETHRTIME
@@ -52,8 +56,21 @@
#ifdef HAVE_GETRUSAGE
#include <sys/resource.h>
#endif
-#ifdef __MINGW32__
+#ifdef HAVE_W32_SYSTEM
#include <process.h>
+#include <io.h>
+# ifndef S_IRUSR
+# define S_IRUSR _S_IREAD
+# endif
+# ifndef S_IS_IWUSRRUSR
+# define S_IWUSR _S_IWRITE
+# endif
+# ifndef S_ISREG
+# define S_ISREG(m) (((m)& S_IFMT) == S_IFREG)
+# endif
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+# define getpid() GetCurrentProcessId()
+# endif
#endif
#ifdef HAVE_W32_SYSTEM
#include <windows.h>
@@ -1175,7 +1192,11 @@ getfnc_gather_random (void))(void (*)(const void*, size_t,
#endif
#if USE_RNDW32
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+ fnc = _gcry_rnduwp_gather_random;
+# else
fnc = _gcry_rndw32_gather_random;
+# endif
return fnc;
#endif
@@ -1197,7 +1218,11 @@ getfnc_fast_random_poll (void))( void (*)(const void*, size_t,
enum random_origins)
{
#if USE_RNDW32
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+ return _gcry_rnduwp_gather_random_fast;
+# else
return _gcry_rndw32_gather_random_fast;
+# endif
#endif
#if USE_RNDW32CE
return _gcry_rndw32ce_gather_random_fast;
diff --git a/random/random-drbg.c b/random/random-drbg.c
index cad364ab..7ed75682 100644
--- a/random/random-drbg.c
+++ b/random/random-drbg.c
@@ -149,7 +149,9 @@
#include <config.h>
#include <string.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#include <stdint.h>
#include "g10lib.h"
@@ -157,6 +159,14 @@
#include "rand-internal.h"
#include "../cipher/bufhelp.h"
+#ifdef HAVE_W32_SYSTEM
+# include <process.h>
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+# include <windows.h>
+# define getpid() GetCurrentProcessId()
+# endif
+#endif
+
/******************************************************************
@@ -622,12 +632,16 @@ drbg_get_entropy (drbg_state_t drbg, unsigned char *buffer,
rc = _gcry_rndunix_gather_random (drbg_read_cb, 0, len,
GCRY_VERY_STRONG_RANDOM);
#elif USE_RNDW32
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+ rc = _gcry_rnduwp_gather_random(drbg_read_cb, 0, len, GCRY_VERY_STRONG_RANDOM);
+# else
do
{
rc = _gcry_rndw32_gather_random (drbg_read_cb, 0, len,
GCRY_VERY_STRONG_RANDOM);
}
while (rc >= 0 && read_cb_len < read_cb_size);
+# endif
#else
rc = -1;
#endif
diff --git a/random/random-system.c b/random/random-system.c
index a1d17ad0..330a89a6 100644
--- a/random/random-system.c
+++ b/random/random-system.c
@@ -27,7 +27,9 @@
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h>
#endif
@@ -154,11 +156,15 @@ get_random (void *buffer, size_t length, int level)
#elif USE_RNDUNIX
rc = _gcry_rndunix_gather_random (read_cb, 0, length, level);
#elif USE_RNDW32
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+ rc = _gcry_rnduwp_gather_random(read_cb, 0, length, level);
+# else
do
{
rc = _gcry_rndw32_gather_random (read_cb, 0, length, level);
}
while (rc >= 0 && read_cb_len < read_cb_size);
+# endif
#else
rc = -1;
#endif
diff --git a/random/random.c b/random/random.c
index c0435d7b..cc8b6e26 100644
--- a/random/random.c
+++ b/random/random.c
@@ -28,7 +28,9 @@
#include <errno.h>
#include <time.h>
#include <sys/types.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#ifdef HAVE_SYSLOG
# include <syslog.h>
#endif /*HAVE_SYSLOG*/
@@ -42,6 +44,13 @@
/* The name of a file used to globally configure the RNG. */
#define RANDOM_CONF_FILE "/etc/gcrypt/random.conf"
+#ifdef HAVE_W32_SYSTEM
+# include <process.h>
+# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
+# include <windows.h>
+# define getpid() GetCurrentProcessId()
+# endif
+#endif
/* If not NULL a progress function called from certain places and the
opaque value passed along. Registered by
diff --git a/random/rndgetentropy.c b/random/rndgetentropy.c
index a6f3c4ab..bf9640ec 100644
--- a/random/rndgetentropy.c
+++ b/random/rndgetentropy.c
@@ -25,7 +25,9 @@
#include <errno.h>
#include <sys/types.h>
#include <string.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#ifdef HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
diff --git a/random/rndjent.c b/random/rndjent.c
index 0468c7cb..09413425 100644
--- a/random/rndjent.c
+++ b/random/rndjent.c
@@ -43,7 +43,9 @@
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#include <errno.h>
#ifndef EOPNOTSUPP
# define EOPNOTSUPP ENOSYS
diff --git a/random/rndw32.c b/random/rndw32.c
index fd979ab9..81fec61d 100644
--- a/random/rndw32.c
+++ b/random/rndw32.c
@@ -141,8 +141,8 @@ typedef DWORD (WINAPI *NTPOWERINFORMATION)
/* Type definitions for function pointers to call CryptoAPI functions. */
typedef BOOL (WINAPI *CRYPTACQUIRECONTEXT)(HCRYPTPROV *phProv,
- LPCTSTR pszContainer,
- LPCTSTR pszProvider,
+ LPCSTR pszContainer,
+ LPCSTR pszProvider,
DWORD dwProvType,
DWORD dwFlags);
typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,
@@ -260,7 +260,7 @@ init_system_rng (void)
system_rng_available = 0;
hRNGProv = NULL;
- hAdvAPI32 = GetModuleHandle ("AdvAPI32.dll");
+ hAdvAPI32 = GetModuleHandleW (L"AdvAPI32.dll");
if (!hAdvAPI32)
return;
@@ -338,7 +338,7 @@ read_mbm_data (void (*add)(const void*, size_t, enum random_origins),
HANDLE hMBMData;
SharedData *mbmDataPtr;
- hMBMData = OpenFileMapping (FILE_MAP_READ, FALSE, "$M$B$M$5$S$D$" );
+ hMBMData = OpenFileMappingW (FILE_MAP_READ, FALSE, L"$M$B$M$5$S$D$" );
if (hMBMData)
{
mbmDataPtr = (SharedData*)MapViewOfFile (hMBMData, FILE_MAP_READ,0,0,0);
@@ -439,7 +439,7 @@ registry_poll (void (*add)(const void*, size_t, enum random_origins),
if ( debug_me )
log_debug ("rndw32#slow_gatherer_nt: get perf data\n" );
- status = RegQueryValueEx (HKEY_PERFORMANCE_DATA, "Global", NULL,
+ status = RegQueryValueExW (HKEY_PERFORMANCE_DATA, L"Global", NULL,
NULL, (LPBYTE) pPerfData, &dwSize);
if (status == ERROR_SUCCESS)
{
@@ -502,8 +502,8 @@ slow_gatherer ( void (*add)(const void*, size_t, enum random_origins),
if ( debug_me )
log_debug ("rndw32#slow_gatherer: init toolkit\n" );
/* Find out whether this is an NT server or workstation if necessary */
- if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
- "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
+ if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
+ L"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
BYTE szValue[32 + 8];
@@ -512,7 +512,7 @@ slow_gatherer ( void (*add)(const void*, size_t, enum random_origins),
if ( debug_me )
log_debug ("rndw32#slow_gatherer: check product options\n" );
- status = RegQueryValueEx (hKey, "ProductType", 0, NULL,
+ status = RegQueryValueExW (hKey, L"ProductType", 0, NULL,
szValue, &dwSize);
if (status == ERROR_SUCCESS && stricmp ((char*)szValue, "WinNT"))
{
@@ -531,7 +531,7 @@ slow_gatherer ( void (*add)(const void*, size_t, enum random_origins),
/* readPnPData (); - we have not implemented that. */
/* Initialize the NetAPI32 function pointers if necessary */
- hNetAPI32 = LoadLibrary ("NETAPI32.DLL");
+ hNetAPI32 = LoadLibraryW (L"NETAPI32.DLL");
if (hNetAPI32)
{
if (debug_me)
@@ -552,7 +552,7 @@ slow_gatherer ( void (*add)(const void*, size_t, enum random_origins),
}
/* Initialize the NT kernel native API function pointers if necessary */
- hNTAPI = GetModuleHandle ("NTDll.dll");
+ hNTAPI = GetModuleHandleW (L"NTDll.dll");
if (hNTAPI)
{
/* Get a pointer to the NT native information query functions */
@@ -600,12 +600,12 @@ slow_gatherer ( void (*add)(const void*, size_t, enum random_origins),
for (drive_no = 0; drive_no < 100 ; drive_no++)
{
char diskPerformance[SIZEOF_DISK_PERFORMANCE_STRUCT + 8];
- char szDevice[50];
+ wchar_t szDevice[50];
/* Check whether we can access this device. */
- snprintf (szDevice, sizeof szDevice, "\\\\.\\PhysicalDrive%d",
- drive_no);
- hDevice = CreateFile (szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
+ _snwprintf (szDevice, sizeof(szDevice) / sizeof(wchar_t), L"\\\\.\\PhysicalDrive%d",
+ drive_no);
+ hDevice = CreateFileW (szDevice, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
break; /* No more drives. */
diff --git a/random/rndw32uwp.cpp b/random/rndw32uwp.cpp
new file mode 100644
index 00000000..4741a13f
--- /dev/null
+++ b/random/rndw32uwp.cpp
@@ -0,0 +1,57 @@
+/* rndw32uwp - W32 entropy gatherer (UWP)
+ *
+ * This file is part of Libgcrypt.
+ *
+ * Libgcrypt is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * Libgcrypt is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "types.h"
+#include "g10lib.h"
+#include "windows.h"
+
+using namespace Platform;
+using namespace Windows::Security::Cryptography;
+using namespace Windows::Security::Cryptography::Certificates;
+using namespace Windows::Storage::Streams;
+
+extern "C" {
+int
+_gcry_rnduwp_gather_random(void(*add)(const void*, size_t,
+ enum random_origins),
+ enum random_origins origin,
+ size_t length, int level)
+{
+ if (!level)
+ return 0;
+
+ IBuffer^ data = CryptographicBuffer::GenerateRandom(length);
+ Array<unsigned char>^ data2;
+ CryptographicBuffer::CopyToByteArray(data, &data2);
+ (*add)(data2->Data, length, origin);
+
+ return 0;
+}
+
+void
+_gcry_rnduwp_gather_random_fast(void(*add)(const void*, size_t,
+ enum random_origins),
+ enum random_origins origin)
+{
+ size_t size = 20 * sizeof(intptr_t) + 2 * sizeof(POINT) + sizeof(MEMORYSTATUS)
+ + 8 * sizeof(FILETIME) + sizeof(LARGE_INTEGER);
+
+ _gcry_rnduwp_gather_random(add, origin, size, 1);
+}
+}
\ No newline at end of file
diff --git a/src/context.c b/src/context.c
index de0a183d..f95c8e6d 100644
--- a/src/context.c
+++ b/src/context.c
@@ -22,7 +22,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#include "g10lib.h"
#include "mpi.h"
diff --git a/src/fips.c b/src/fips.c
index cf91baa8..2271e374 100644
--- a/src/fips.c
+++ b/src/fips.c
@@ -21,7 +21,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#include <string.h>
#ifdef ENABLE_HMAC_BINARY_CHECK
# include <dlfcn.h>
@@ -32,6 +34,12 @@
#ifdef HAVE_SYSLOG
# include <syslog.h>
#endif /*HAVE_SYSLOG*/
+#ifdef HAVE_W32_SYSTEM
+# include <io.h>
+# ifndef F_OK
+# define F_OK 0
+# endif
+#endif
/* The name of the file used to force libgcrypt into fips mode. */
#define FIPS_FORCE_FILE "/etc/gcrypt/fips_enabled"
@@ -365,7 +373,6 @@ _gcry_fips_indicator_cipher (va_list arg_ptr)
case GCRY_CIPHER_MODE_OFB:
case GCRY_CIPHER_MODE_CTR:
case GCRY_CIPHER_MODE_CCM:
- case GCRY_CIPHER_MODE_GCM:
case GCRY_CIPHER_MODE_XTS:
case GCRY_CIPHER_MODE_AESWRAP:
return GPG_ERR_NO_ERROR;
diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in
index 9cad7a46..0e96998b 100644
--- a/src/gcrypt.h.in
+++ b/src/gcrypt.h.in
@@ -1,4 +1,6 @@
/* gcrypt.h - GNU Cryptographic Library Interface -*- c -*-
+ * Copyright (C) 2012-2023 g10 Code GmbH
+ * Copyright (C) 2013-2023 Jussi Kivilinna
* Copyright (C) 1998-2018 Free Software Foundation, Inc.
* Copyright (C) 2012-2024 g10 Code GmbH
*
@@ -34,7 +36,8 @@
#if defined _WIN32 || defined __WIN32__
# ifndef __GNUC__
- typedef long ssize_t;
+# include <basetsd.h>
+ typedef SSIZE_T ssize_t;
typedef int pid_t;
# endif /*!__GNUC__*/
#endif /*_WIN32*/
diff --git a/src/global.c b/src/global.c
index 593ea406..bd5055ae 100644
--- a/src/global.c
+++ b/src/global.c
@@ -29,7 +29,9 @@
#include <ctype.h>
#include <limits.h>
#include <errno.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#ifdef HAVE_SYSLOG
# include <syslog.h>
#endif /*HAVE_SYSLOG*/
diff --git a/src/hwf-x86.c b/src/hwf-x86.c
index bda14d9d..04137c5f 100644
--- a/src/hwf-x86.c
+++ b/src/hwf-x86.c
@@ -23,7 +23,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#include "g10lib.h"
#include "hwf-common.h"
@@ -36,7 +38,7 @@
features. */
#undef HAS_X86_CPUID
-#if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && defined (__GNUC__)
+#if defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4 && ( defined (__GNUC__) || defined(__INTEL_COMPILER) )
# define HAS_X86_CPUID 1
#ifdef HAVE_GCC_ASM_CFI_DIRECTIVES
@@ -76,9 +78,9 @@ is_cpuid_available(void)
"popf\n\t"
CFI_POP4
"xorl %%eax, %%ecx\n\t" /* Compare flags against saved flags. */
- "jz .Lno_cpuid%=\n\t" /* Toggling did not work, thus no CPUID. */
+ "jz 1f\n\t" /* Toggling did not work, thus no CPUID. */
"movl $1, %0\n" /* Worked. true -> HAS_CPUID. */
- ".Lno_cpuid%=:\n\t"
+ "1:\n\t"
: "+r" (has_cpuid)
:
: "%eax", "%ecx", "cc", "memory"
@@ -131,7 +133,7 @@ get_xgetbv(void)
#endif /* i386 && GNUC */
-#if defined (__x86_64__) && defined (__GNUC__)
+#if defined (__x86_64__) && ( defined (__GNUC__) || defined(__INTEL_COMPILER) )
# define HAS_X86_CPUID 1
static int
diff --git a/src/hwfeatures.c b/src/hwfeatures.c
index b11cadef..2afa6168 100644
--- a/src/hwfeatures.c
+++ b/src/hwfeatures.c
@@ -24,7 +24,9 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#ifdef HAVE_SYSLOG
# include <syslog.h>
#endif /*HAVE_SYSLOG*/
diff --git a/src/misc.c b/src/misc.c
index b1e8eb1c..5cc4ae20 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -24,7 +24,12 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+#ifdef HAVE_W32_SYSTEM
+# include <io.h>
+#endif
#include "g10lib.h"
#include "secmem.h"
diff --git a/src/mpicalc.c b/src/mpicalc.c
index 0903e0a4..ca413cf4 100644
--- a/src/mpicalc.c
+++ b/src/mpicalc.c
@@ -85,26 +85,40 @@ print_mpi (gcry_mpi_t a)
static void
-do_add (void)
+do_add (int usemod)
{
- if (stackidx < 2)
+ if (stackidx < (usemod?3:2))
{
fputs ("stack underflow\n", stderr);
return;
}
- mpi_add (stack[stackidx - 2], stack[stackidx - 2], stack[stackidx - 1]);
+ if (usemod)
+ {
+ mpi_addm (stack[stackidx - 3], stack[stackidx - 3],
+ stack[stackidx - 2], stack[stackidx - 1]);
+ stackidx--;
+ }
+ else
+ mpi_add (stack[stackidx - 2], stack[stackidx - 2], stack[stackidx - 1]);
stackidx--;
}
static void
-do_sub (void)
+do_sub (int usemod)
{
- if (stackidx < 2)
+ if (stackidx < (usemod?3:2))
{
fputs ("stack underflow\n", stderr);
return;
}
- mpi_sub (stack[stackidx - 2], stack[stackidx - 2], stack[stackidx - 1]);
+ if (usemod)
+ {
+ mpi_subm (stack[stackidx - 3], stack[stackidx - 3],
+ stack[stackidx - 2], stack[stackidx - 1]);
+ stackidx--;
+ }
+ else
+ mpi_sub (stack[stackidx - 2], stack[stackidx - 2], stack[stackidx - 1]);
stackidx--;
}
@@ -328,6 +342,7 @@ print_help (void)
"r reverse [0] := [1], [1] := [0] {0}\n"
"b # of bits [0] := nbits([0]) {0}\n"
"P prime check [0] := is_prime([0])?1:0 {0}\n"
+ "M use mod for next '+' and '-'\n"
"c clear stack\n"
"p print top item\n"
"f print the stack\n"
@@ -348,6 +363,7 @@ main (int argc, char **argv)
int state = 0;
char strbuf[4096];
int stridx = 0;
+ int usemod = 0;
if (argc)
{
@@ -460,7 +476,8 @@ main (int argc, char **argv)
else
{
ungetc (c, stdin);
- do_add ();
+ do_add (usemod);
+ usemod = 0;
}
break;
case '-':
@@ -480,7 +497,8 @@ main (int argc, char **argv)
else
{
ungetc (c, stdin);
- do_sub ();
+ do_sub (usemod);
+ usemod = 0;
}
break;
case '*':
@@ -547,6 +565,9 @@ main (int argc, char **argv)
case 'P':
do_primecheck ();
break;
+ case 'M':
+ usemod = 1;
+ break;
case 'c':
for (i = 0; i < stackidx; i++)
{
diff --git a/src/secmem.c b/src/secmem.c
index 4e1d2991..b7232a22 100644
--- a/src/secmem.c
+++ b/src/secmem.c
@@ -25,7 +25,9 @@
#include <string.h>
#include <errno.h>
#include <stdarg.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
#include <stddef.h>
#if defined(HAVE_MLOCK) || defined(HAVE_MMAP)
diff --git a/src/versioninfo.rc.in b/src/versioninfo.rc.in
index f87d0d05..929f9ccc 100644
--- a/src/versioninfo.rc.in
+++ b/src/versioninfo.rc.in
@@ -39,7 +39,7 @@ BEGIN
VALUE "FileDescription", "Libgcrypt - The GNU Crypto Library\0"
VALUE "FileVersion", "@LIBGCRYPT_LT_CURRENT@.@LIBGCRYPT_LT_AGE@.@LIBGCRYPT_LT_REVISION@.@BUILD_REVISION@\0"
VALUE "InternalName", "libgcrypt\0"
- VALUE "LegalCopyright", "Copyright © 2021 g10 Code GmbH\0"
+ VALUE "LegalCopyright", "Copyright © 2023 g10 Code GmbH\0"
VALUE "LegalTrademarks", "\0"
VALUE "OriginalFilename", "libgcrypt.dll\0"
VALUE "PrivateBuild", "\0"
diff --git a/tests/basic.c b/tests/basic.c
index 72c65b58..999d5001 100644
--- a/tests/basic.c
+++ b/tests/basic.c
@@ -17441,35 +17441,40 @@ check_pubkey_crypt (int n, gcry_sexp_t skey, gcry_sexp_t pkey, int algo,
NULL,
0,