xcrypt 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +106 -0
  3. data/ext/libxcrypt/AUTHORS +38 -0
  4. data/ext/libxcrypt/COPYING.LIB +502 -0
  5. data/ext/libxcrypt/ChangeLog +239 -0
  6. data/ext/libxcrypt/INSTALL +380 -0
  7. data/ext/libxcrypt/LICENSING +152 -0
  8. data/ext/libxcrypt/Makefile.am +704 -0
  9. data/ext/libxcrypt/Makefile.in +4110 -0
  10. data/ext/libxcrypt/NEWS +630 -0
  11. data/ext/libxcrypt/README +1 -0
  12. data/ext/libxcrypt/README.md +179 -0
  13. data/ext/libxcrypt/THANKS +13 -0
  14. data/ext/libxcrypt/TODO +1 -0
  15. data/ext/libxcrypt/TODO.md +100 -0
  16. data/ext/libxcrypt/aclocal.m4 +2617 -0
  17. data/ext/libxcrypt/autogen.sh +33 -0
  18. data/ext/libxcrypt/autom4te.cache/output.0 +19884 -0
  19. data/ext/libxcrypt/autom4te.cache/output.1 +19884 -0
  20. data/ext/libxcrypt/autom4te.cache/output.2 +19884 -0
  21. data/ext/libxcrypt/autom4te.cache/output.3 +19885 -0
  22. data/ext/libxcrypt/autom4te.cache/requests +714 -0
  23. data/ext/libxcrypt/autom4te.cache/traces.0 +4088 -0
  24. data/ext/libxcrypt/autom4te.cache/traces.1 +1060 -0
  25. data/ext/libxcrypt/autom4te.cache/traces.2 +4088 -0
  26. data/ext/libxcrypt/autom4te.cache/traces.3 +1060 -0
  27. data/ext/libxcrypt/build-aux/ci/ci-log-dependency-versions +79 -0
  28. data/ext/libxcrypt/build-aux/ci/ci-log-logfiles +22 -0
  29. data/ext/libxcrypt/build-aux/ci/clang-gcov-wrapper +2 -0
  30. data/ext/libxcrypt/build-aux/ci/configure-wrapper +10 -0
  31. data/ext/libxcrypt/build-aux/ci/summarize-coverage +24 -0
  32. data/ext/libxcrypt/build-aux/m4/ax_append_compile_flags.m4 +46 -0
  33. data/ext/libxcrypt/build-aux/m4/ax_append_flag.m4 +50 -0
  34. data/ext/libxcrypt/build-aux/m4/ax_check_compile_flag.m4 +53 -0
  35. data/ext/libxcrypt/build-aux/m4/ax_check_vscript.m4 +142 -0
  36. data/ext/libxcrypt/build-aux/m4/ax_gcc_func_attribute.m4 +246 -0
  37. data/ext/libxcrypt/build-aux/m4/ax_require_defined.m4 +37 -0
  38. data/ext/libxcrypt/build-aux/m4/ax_valgrind_check.m4 +239 -0
  39. data/ext/libxcrypt/build-aux/m4/libtool.m4 +8488 -0
  40. data/ext/libxcrypt/build-aux/m4/ltoptions.m4 +467 -0
  41. data/ext/libxcrypt/build-aux/m4/ltsugar.m4 +124 -0
  42. data/ext/libxcrypt/build-aux/m4/ltversion.m4 +24 -0
  43. data/ext/libxcrypt/build-aux/m4/lt~obsolete.m4 +99 -0
  44. data/ext/libxcrypt/build-aux/m4/pkg_compat.m4 +88 -0
  45. data/ext/libxcrypt/build-aux/m4/zw_alignment.m4 +90 -0
  46. data/ext/libxcrypt/build-aux/m4/zw_automodern.m4 +307 -0
  47. data/ext/libxcrypt/build-aux/m4/zw_detect_asan.m4 +24 -0
  48. data/ext/libxcrypt/build-aux/m4/zw_endianness.m4 +152 -0
  49. data/ext/libxcrypt/build-aux/m4/zw_ld_wrap.m4 +47 -0
  50. data/ext/libxcrypt/build-aux/m4/zw_prog_perl.m4 +40 -0
  51. data/ext/libxcrypt/build-aux/m4/zw_simple_warnings.m4 +150 -0
  52. data/ext/libxcrypt/build-aux/m4/zw_static_assert.m4 +68 -0
  53. data/ext/libxcrypt/build-aux/m4-autogen/compile +364 -0
  54. data/ext/libxcrypt/build-aux/m4-autogen/config.guess +1815 -0
  55. data/ext/libxcrypt/build-aux/m4-autogen/config.sub +2354 -0
  56. data/ext/libxcrypt/build-aux/m4-autogen/depcomp +792 -0
  57. data/ext/libxcrypt/build-aux/m4-autogen/install-sh +541 -0
  58. data/ext/libxcrypt/build-aux/m4-autogen/ltmain.sh +11524 -0
  59. data/ext/libxcrypt/build-aux/m4-autogen/missing +236 -0
  60. data/ext/libxcrypt/build-aux/m4-autogen/test-driver +160 -0
  61. data/ext/libxcrypt/build-aux/scripts/BuildCommon.pm +712 -0
  62. data/ext/libxcrypt/build-aux/scripts/check-perlcritic-config +76 -0
  63. data/ext/libxcrypt/build-aux/scripts/compute-symver-floor +116 -0
  64. data/ext/libxcrypt/build-aux/scripts/expand-selected-hashes +80 -0
  65. data/ext/libxcrypt/build-aux/scripts/gen-crypt-h +131 -0
  66. data/ext/libxcrypt/build-aux/scripts/gen-crypt-hashes-h +141 -0
  67. data/ext/libxcrypt/build-aux/scripts/gen-crypt-symbol-vers-h +150 -0
  68. data/ext/libxcrypt/build-aux/scripts/gen-libcrypt-map +67 -0
  69. data/ext/libxcrypt/build-aux/scripts/move-if-change +84 -0
  70. data/ext/libxcrypt/build-aux/scripts/skip-if-exec-format-error +78 -0
  71. data/ext/libxcrypt/codecov.yml +4 -0
  72. data/ext/libxcrypt/config.h.in +303 -0
  73. data/ext/libxcrypt/configure +19885 -0
  74. data/ext/libxcrypt/configure.ac +549 -0
  75. data/ext/libxcrypt/doc/crypt.3 +512 -0
  76. data/ext/libxcrypt/doc/crypt.5 +343 -0
  77. data/ext/libxcrypt/doc/crypt_checksalt.3 +106 -0
  78. data/ext/libxcrypt/doc/crypt_gensalt.3 +285 -0
  79. data/ext/libxcrypt/doc/crypt_gensalt_ra.3 +1 -0
  80. data/ext/libxcrypt/doc/crypt_gensalt_rn.3 +1 -0
  81. data/ext/libxcrypt/doc/crypt_preferred_method.3 +68 -0
  82. data/ext/libxcrypt/doc/crypt_r.3 +1 -0
  83. data/ext/libxcrypt/doc/crypt_ra.3 +1 -0
  84. data/ext/libxcrypt/doc/crypt_rn.3 +1 -0
  85. data/ext/libxcrypt/lib/alg-des-tables.c +3858 -0
  86. data/ext/libxcrypt/lib/alg-des.c +269 -0
  87. data/ext/libxcrypt/lib/alg-des.h +74 -0
  88. data/ext/libxcrypt/lib/alg-gost3411-2012-const.h +313 -0
  89. data/ext/libxcrypt/lib/alg-gost3411-2012-core.c +238 -0
  90. data/ext/libxcrypt/lib/alg-gost3411-2012-core.h +51 -0
  91. data/ext/libxcrypt/lib/alg-gost3411-2012-hmac.c +78 -0
  92. data/ext/libxcrypt/lib/alg-gost3411-2012-hmac.h +46 -0
  93. data/ext/libxcrypt/lib/alg-gost3411-2012-precalc.h +1426 -0
  94. data/ext/libxcrypt/lib/alg-gost3411-2012-ref.h +67 -0
  95. data/ext/libxcrypt/lib/alg-hmac-sha1.c +140 -0
  96. data/ext/libxcrypt/lib/alg-hmac-sha1.h +35 -0
  97. data/ext/libxcrypt/lib/alg-md4.c +270 -0
  98. data/ext/libxcrypt/lib/alg-md4.h +43 -0
  99. data/ext/libxcrypt/lib/alg-md5.c +291 -0
  100. data/ext/libxcrypt/lib/alg-md5.h +43 -0
  101. data/ext/libxcrypt/lib/alg-sha1.c +288 -0
  102. data/ext/libxcrypt/lib/alg-sha1.h +34 -0
  103. data/ext/libxcrypt/lib/alg-sha256.c +630 -0
  104. data/ext/libxcrypt/lib/alg-sha256.h +123 -0
  105. data/ext/libxcrypt/lib/alg-sha512.c +311 -0
  106. data/ext/libxcrypt/lib/alg-sha512.h +81 -0
  107. data/ext/libxcrypt/lib/alg-sm3-hmac.c +113 -0
  108. data/ext/libxcrypt/lib/alg-sm3-hmac.h +42 -0
  109. data/ext/libxcrypt/lib/alg-sm3.c +449 -0
  110. data/ext/libxcrypt/lib/alg-sm3.h +63 -0
  111. data/ext/libxcrypt/lib/alg-yescrypt-common.c +713 -0
  112. data/ext/libxcrypt/lib/alg-yescrypt-opt.c +1568 -0
  113. data/ext/libxcrypt/lib/alg-yescrypt-platform.c +106 -0
  114. data/ext/libxcrypt/lib/alg-yescrypt.h +360 -0
  115. data/ext/libxcrypt/lib/byteorder.h +164 -0
  116. data/ext/libxcrypt/lib/crypt-bcrypt.c +1061 -0
  117. data/ext/libxcrypt/lib/crypt-des-obsolete.c +215 -0
  118. data/ext/libxcrypt/lib/crypt-des.c +491 -0
  119. data/ext/libxcrypt/lib/crypt-gensalt-static.c +40 -0
  120. data/ext/libxcrypt/lib/crypt-gost-yescrypt.c +182 -0
  121. data/ext/libxcrypt/lib/crypt-md5.c +232 -0
  122. data/ext/libxcrypt/lib/crypt-nthash.c +134 -0
  123. data/ext/libxcrypt/lib/crypt-obsolete.h +40 -0
  124. data/ext/libxcrypt/lib/crypt-pbkdf1-sha1.c +260 -0
  125. data/ext/libxcrypt/lib/crypt-port.h +514 -0
  126. data/ext/libxcrypt/lib/crypt-scrypt.c +247 -0
  127. data/ext/libxcrypt/lib/crypt-sha256.c +308 -0
  128. data/ext/libxcrypt/lib/crypt-sha512.c +323 -0
  129. data/ext/libxcrypt/lib/crypt-sm3-yescrypt.c +189 -0
  130. data/ext/libxcrypt/lib/crypt-sm3.c +308 -0
  131. data/ext/libxcrypt/lib/crypt-static.c +44 -0
  132. data/ext/libxcrypt/lib/crypt-sunmd5.c +314 -0
  133. data/ext/libxcrypt/lib/crypt-yescrypt.c +177 -0
  134. data/ext/libxcrypt/lib/crypt.c +421 -0
  135. data/ext/libxcrypt/lib/crypt.h.in +249 -0
  136. data/ext/libxcrypt/lib/gen-des-tables.c +363 -0
  137. data/ext/libxcrypt/lib/hashes.conf +59 -0
  138. data/ext/libxcrypt/lib/libcrypt.map.in +48 -0
  139. data/ext/libxcrypt/lib/libcrypt.minver +97 -0
  140. data/ext/libxcrypt/lib/libxcrypt.pc.in +15 -0
  141. data/ext/libxcrypt/lib/util-base64.c +26 -0
  142. data/ext/libxcrypt/lib/util-gensalt-sha.c +88 -0
  143. data/ext/libxcrypt/lib/util-get-random-bytes.c +154 -0
  144. data/ext/libxcrypt/lib/util-make-failure-token.c +48 -0
  145. data/ext/libxcrypt/lib/util-xbzero.c +43 -0
  146. data/ext/libxcrypt/lib/util-xstrcpy.c +42 -0
  147. data/ext/libxcrypt/lib/xcrypt.h.in +58 -0
  148. data/ext/libxcrypt/libxcrypt.spec.rpkg +481 -0
  149. data/ext/libxcrypt/rpkg.conf +2 -0
  150. data/ext/libxcrypt/rpkg.macros +86 -0
  151. data/ext/libxcrypt/test/TestCommon.pm +326 -0
  152. data/ext/libxcrypt/test/alg-des.c +80 -0
  153. data/ext/libxcrypt/test/alg-gost3411-2012-hmac.c +90 -0
  154. data/ext/libxcrypt/test/alg-gost3411-2012.c +191 -0
  155. data/ext/libxcrypt/test/alg-hmac-sha1.c +187 -0
  156. data/ext/libxcrypt/test/alg-md4.c +111 -0
  157. data/ext/libxcrypt/test/alg-md5.c +134 -0
  158. data/ext/libxcrypt/test/alg-pbkdf-hmac-sha256.c +269 -0
  159. data/ext/libxcrypt/test/alg-sha1.c +111 -0
  160. data/ext/libxcrypt/test/alg-sha256.c +141 -0
  161. data/ext/libxcrypt/test/alg-sha512.c +170 -0
  162. data/ext/libxcrypt/test/alg-sm3-hmac.c +149 -0
  163. data/ext/libxcrypt/test/alg-sm3.c +168 -0
  164. data/ext/libxcrypt/test/alg-yescrypt.c +466 -0
  165. data/ext/libxcrypt/test/badsalt.c +726 -0
  166. data/ext/libxcrypt/test/badsetting.c +350 -0
  167. data/ext/libxcrypt/test/byteorder.c +254 -0
  168. data/ext/libxcrypt/test/checksalt.c +265 -0
  169. data/ext/libxcrypt/test/compile-strong-alias.c +43 -0
  170. data/ext/libxcrypt/test/crypt-badargs.c +392 -0
  171. data/ext/libxcrypt/test/crypt-gost-yescrypt.c +149 -0
  172. data/ext/libxcrypt/test/crypt-nested-call.c +180 -0
  173. data/ext/libxcrypt/test/crypt-sm3-yescrypt.c +149 -0
  174. data/ext/libxcrypt/test/crypt-too-long-phrase.c +157 -0
  175. data/ext/libxcrypt/test/des-cases.h +196 -0
  176. data/ext/libxcrypt/test/des-obsolete.c +206 -0
  177. data/ext/libxcrypt/test/des-obsolete_r.c +207 -0
  178. data/ext/libxcrypt/test/explicit-bzero.c +334 -0
  179. data/ext/libxcrypt/test/gensalt-bcrypt_x.c +54 -0
  180. data/ext/libxcrypt/test/gensalt-extradata.c +246 -0
  181. data/ext/libxcrypt/test/gensalt-nested-call.c +126 -0
  182. data/ext/libxcrypt/test/gensalt-nthash.c +65 -0
  183. data/ext/libxcrypt/test/gensalt.c +599 -0
  184. data/ext/libxcrypt/test/getrandom-fallbacks.c +295 -0
  185. data/ext/libxcrypt/test/getrandom-interface.c +211 -0
  186. data/ext/libxcrypt/test/ka-table-gen.py +945 -0
  187. data/ext/libxcrypt/test/ka-table.inc +5849 -0
  188. data/ext/libxcrypt/test/ka-tester.c +240 -0
  189. data/ext/libxcrypt/test/preferred-method.c +133 -0
  190. data/ext/libxcrypt/test/short-outbuf.c +119 -0
  191. data/ext/libxcrypt/test/special-char-salt.c +1160 -0
  192. data/ext/libxcrypt/test/symbols-compat.pl +137 -0
  193. data/ext/libxcrypt/test/symbols-renames.pl +107 -0
  194. data/ext/libxcrypt/test/symbols-static.pl +87 -0
  195. data/ext/xcrypt/xcrypt.c +9 -0
  196. data/lib/xcrypt/ffi.rb +76 -0
  197. data/lib/xcrypt/version.rb +5 -0
  198. data/lib/xcrypt.rb +89 -0
  199. metadata +267 -0
@@ -0,0 +1,334 @@
1
+ /* Test that explicit_bzero block clears are not optimized out.
2
+ Copyright (C) 2016-2020 Free Software Foundation, Inc.
3
+ This file is part of the GNU C Library.
4
+
5
+ The GNU C Library is free software; you can redistribute it and/or
6
+ modify it under the terms of the GNU Lesser General Public
7
+ License as published by the Free Software Foundation; either
8
+ version 2.1 of the License, or (at your option) any later version.
9
+
10
+ The GNU C Library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public
16
+ License along with the GNU C Library; if not, see
17
+ <https://www.gnu.org/licenses/>. */
18
+
19
+ /* This test is conceptually based on a test designed by Matthew
20
+ Dempsky for the OpenBSD regression suite:
21
+ <openbsd>/src/regress/lib/libc/explicit_bzero/explicit_bzero.c.
22
+ The basic idea is, we have a function that contains a
23
+ block-clearing operation (not necessarily explicit_bzero), after
24
+ which the block is dead, in the compiler-jargon sense. Execute
25
+ that function while running on a user-allocated alternative
26
+ stack. Then we have another pointer to the memory region affected
27
+ by the block clear -- namely, the original allocation for the
28
+ alternative stack -- and can find out whether it actually happened.
29
+
30
+ The OpenBSD test uses sigaltstack and SIGUSR1 to get onto an
31
+ alternative stack. This causes a number of awkward problems; some
32
+ operating systems (e.g. Solaris and OSX) wipe the signal stack upon
33
+ returning to the normal stack, there's no way to be sure that other
34
+ processes running on the same system will not interfere, and the
35
+ signal stack is very small so it's not safe to call printf there.
36
+ This implementation instead uses the <ucontext.h> coroutine
37
+ interface. The coroutine stack is still too small to safely use
38
+ printf, but we know the OS won't erase it, so we can do all the
39
+ checks and printing from the normal stack. */
40
+
41
+ #include "crypt-port.h"
42
+
43
+ #if !defined USE_SWAPCONTEXT || defined XCRYPT_USE_ASAN
44
+ /* We can't do this test if we don't have the ucontext API.
45
+ Also ASAN may have some problems with using swapcontext(). */
46
+ int main(void)
47
+ {
48
+ return 77;
49
+ }
50
+ #else
51
+
52
+ #include <stdio.h>
53
+ #include <stdlib.h>
54
+ #include <ucontext.h>
55
+
56
+ #ifdef HAVE_VALGRIND_VALGRIND_H
57
+ # include <valgrind/valgrind.h>
58
+ # include <valgrind/memcheck.h>
59
+ #else
60
+ # define VALGRIND_STACK_REGISTER(start, end) do {} while (0)
61
+ # define VALGRIND_MAKE_MEM_DEFINED(addr, len) do {} while (0)
62
+ #endif
63
+
64
+ /* A byte pattern that is unlikely to occur by chance: the first 16
65
+ prime numbers (OEIS A000040). */
66
+ static const unsigned char test_pattern[16] =
67
+ {
68
+ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
69
+ };
70
+
71
+ /* Immediately after each subtest returns, we call swapcontext to get
72
+ back onto the main stack. That call might itself overwrite the
73
+ test pattern, so we fill a modest-sized buffer with copies of it
74
+ and check whether any of them survived. */
75
+
76
+ #define PATTERN_SIZE (sizeof test_pattern)
77
+ #define PATTERN_REPS 32
78
+ #define TEST_BUFFER_SIZE (PATTERN_SIZE * PATTERN_REPS)
79
+
80
+ /* There are three subtests, two of which are sanity checks.
81
+ Each test follows this sequence:
82
+
83
+ main coroutine
84
+ ---- --------
85
+ advance cur_subtest
86
+ swap
87
+ call setup function
88
+ prepare test buffer
89
+ swap
90
+ verify that buffer
91
+ was filled in
92
+ swap
93
+ possibly clear buffer
94
+ return
95
+ swap
96
+ check buffer again,
97
+ according to test
98
+ expectation
99
+
100
+ In the "no_clear" case, we don't do anything to the test buffer
101
+ between preparing it and letting it go out of scope, and we expect
102
+ to find it. This confirms that the test buffer does get filled in
103
+ and we can find it from the stack buffer. In the "ordinary_clear"
104
+ case, we clear it using memset. Depending on the target, the
105
+ compiler may not be able to apply dead store elimination to the
106
+ memset call, so the test does not fail if the memset is not
107
+ eliminated. Finally, the "explicit_clear" case uses explicit_bzero
108
+ and expects _not_ to find the test buffer, which is the real
109
+ test. */
110
+
111
+ static ucontext_t uc_main, uc_co;
112
+
113
+ static NO_INLINE int
114
+ use_test_buffer (unsigned char *buf)
115
+ {
116
+ unsigned int sum = 0;
117
+
118
+ for (unsigned int i = 0; i < PATTERN_REPS; i++)
119
+ sum += buf[i * PATTERN_SIZE];
120
+
121
+ return (sum == 2 * PATTERN_REPS) ? 0 : 1;
122
+ }
123
+
124
+ /* Always check the test buffer immediately after filling it; this
125
+ makes externally visible side effects depend on the buffer existing
126
+ and having been filled in. */
127
+ static inline void
128
+ prepare_test_buffer (unsigned char *buf)
129
+ {
130
+ for (unsigned int i = 0; i < PATTERN_REPS; i++)
131
+ memcpy (buf + i*PATTERN_SIZE, test_pattern, PATTERN_SIZE);
132
+
133
+ if (swapcontext (&uc_co, &uc_main))
134
+ abort ();
135
+
136
+ /* Force the compiler to really copy the pattern to buf. */
137
+ if (use_test_buffer (buf))
138
+ abort ();
139
+ }
140
+
141
+ static void
142
+ setup_no_clear (void)
143
+ {
144
+ unsigned char buf[TEST_BUFFER_SIZE];
145
+ prepare_test_buffer (buf);
146
+ }
147
+
148
+ static void
149
+ setup_ordinary_clear (void)
150
+ {
151
+ unsigned char buf[TEST_BUFFER_SIZE];
152
+ prepare_test_buffer (buf);
153
+ memset (buf, 0, TEST_BUFFER_SIZE);
154
+ }
155
+
156
+ static void
157
+ setup_explicit_clear (void)
158
+ {
159
+ unsigned char buf[TEST_BUFFER_SIZE];
160
+ prepare_test_buffer (buf);
161
+ explicit_bzero (buf, TEST_BUFFER_SIZE);
162
+ }
163
+
164
+ enum test_expectation
165
+ {
166
+ EXPECT_NONE = 1,
167
+ EXPECT_SOME,
168
+ EXPECT_ALL,
169
+ NO_EXPECTATIONS,
170
+ ARRAY_END = 0
171
+ };
172
+ struct subtest
173
+ {
174
+ void (*setup_subtest) (void);
175
+ const char *label;
176
+ enum test_expectation expected;
177
+ };
178
+ static const struct subtest *cur_subtest;
179
+
180
+ static const struct subtest subtests[] =
181
+ {
182
+ { setup_no_clear, "no clear", EXPECT_SOME },
183
+ /* The memset may happen or not, depending on compiler
184
+ optimizations. */
185
+ { setup_ordinary_clear, "ordinary clear", NO_EXPECTATIONS },
186
+ { setup_explicit_clear, "explicit clear", EXPECT_NONE },
187
+ { 0, 0, ARRAY_END }
188
+ };
189
+
190
+ static void
191
+ test_coroutine (void)
192
+ {
193
+ while (cur_subtest->setup_subtest)
194
+ {
195
+ cur_subtest->setup_subtest ();
196
+ if (swapcontext (&uc_co, &uc_main))
197
+ abort ();
198
+ }
199
+ }
200
+
201
+ /* All the code above this point runs on the coroutine stack.
202
+ All the code below this point runs on the main stack. */
203
+
204
+ static int test_status;
205
+ static unsigned char *co_stack_buffer;
206
+ static size_t co_stack_size;
207
+
208
+ static unsigned int
209
+ count_test_patterns (unsigned char *buf, size_t bufsiz)
210
+ {
211
+ VALGRIND_MAKE_MEM_DEFINED (buf, bufsiz);
212
+ unsigned char *first = memmem (buf, bufsiz, test_pattern, PATTERN_SIZE);
213
+ if (!first)
214
+ return 0;
215
+ unsigned int cnt = 0;
216
+ for (unsigned int i = 0; i < PATTERN_REPS; i++)
217
+ {
218
+ unsigned char *p = first + i*PATTERN_SIZE;
219
+ if (p + PATTERN_SIZE - buf > (ptrdiff_t)bufsiz)
220
+ break;
221
+ if (memcmp (p, test_pattern, PATTERN_SIZE) == 0)
222
+ cnt++;
223
+ }
224
+ return cnt;
225
+ }
226
+
227
+ static void
228
+ check_test_buffer (enum test_expectation expected,
229
+ const char *label, const char *stage)
230
+ {
231
+ unsigned int cnt = count_test_patterns (co_stack_buffer, co_stack_size);
232
+ switch (expected)
233
+ {
234
+ case EXPECT_NONE:
235
+ if (cnt == 0)
236
+ printf ("PASS: %s/%s: expected 0 got %u\n", label, stage, cnt);
237
+ else
238
+ {
239
+ printf ("FAIL: %s/%s: expected 0 got %u\n", label, stage, cnt);
240
+ test_status = 1;
241
+ }
242
+ break;
243
+
244
+ case EXPECT_SOME:
245
+ if (cnt > 0)
246
+ printf ("PASS: %s/%s: expected some got %u\n", label, stage, cnt);
247
+ else
248
+ {
249
+ printf ("FAIL: %s/%s: expected some got 0\n", label, stage);
250
+ test_status = 1;
251
+ }
252
+ break;
253
+
254
+ case EXPECT_ALL:
255
+ if (cnt == PATTERN_REPS)
256
+ printf ("PASS: %s/%s: expected %d got %u\n", label, stage,
257
+ PATTERN_REPS, cnt);
258
+ else
259
+ {
260
+ printf ("FAIL: %s/%s: expected %d got %u\n", label, stage,
261
+ PATTERN_REPS, cnt);
262
+ test_status = 1;
263
+ }
264
+ break;
265
+
266
+ case NO_EXPECTATIONS:
267
+ printf ("INFO: %s/%s: found %u patterns%s\n", label, stage, cnt,
268
+ cnt == 0 ? " (memset not eliminated)" : "");
269
+ break;
270
+
271
+ default:
272
+ printf ("ERROR: %s/%s: invalid value for 'expected' = %d\n",
273
+ label, stage, (int)expected);
274
+ test_status = 1;
275
+ }
276
+ }
277
+
278
+ static void
279
+ test_loop (void)
280
+ {
281
+ cur_subtest = subtests;
282
+ while (cur_subtest->setup_subtest)
283
+ {
284
+ if (swapcontext (&uc_main, &uc_co))
285
+ abort ();
286
+ check_test_buffer (EXPECT_ALL, cur_subtest->label, "prepare");
287
+ if (swapcontext (&uc_main, &uc_co))
288
+ abort ();
289
+ check_test_buffer (cur_subtest->expected, cur_subtest->label, "test");
290
+ cur_subtest++;
291
+ }
292
+ /* Terminate the coroutine. */
293
+ if (swapcontext (&uc_main, &uc_co))
294
+ abort ();
295
+ }
296
+
297
+ int
298
+ main (void)
299
+ {
300
+ size_t page_alignment = sizeof (void *);
301
+ long page_alignment_l = sysconf (_SC_PAGESIZE);
302
+ if (page_alignment_l > (long) sizeof (void *))
303
+ page_alignment = (size_t) page_alignment_l;
304
+
305
+ co_stack_size = (size_t) SIGSTKSZ + (size_t) TEST_BUFFER_SIZE;
306
+ if (co_stack_size < page_alignment * 4)
307
+ co_stack_size = page_alignment * 4;
308
+
309
+ void *p;
310
+ int err = posix_memalign (&p, page_alignment, co_stack_size);
311
+ if (err || !p)
312
+ {
313
+ printf ("ERROR: allocating alt stack: %s\n", strerror (err));
314
+ return 2;
315
+ }
316
+ co_stack_buffer = p;
317
+ memset (co_stack_buffer, 0, co_stack_size);
318
+ VALGRIND_STACK_REGISTER (co_stack_buffer, co_stack_buffer + co_stack_size);
319
+
320
+ if (getcontext (&uc_co))
321
+ {
322
+ printf ("ERROR: allocating coroutine context: %s\n", strerror (err));
323
+ return 2;
324
+ }
325
+ uc_co.uc_stack.ss_sp = co_stack_buffer;
326
+ uc_co.uc_stack.ss_size = co_stack_size;
327
+ uc_co.uc_link = &uc_main;
328
+ makecontext (&uc_co, test_coroutine, 0);
329
+
330
+ test_loop ();
331
+ return test_status;
332
+ }
333
+
334
+ #endif /* have ucontext.h || defined XCRYPT_USE_ASAN */
@@ -0,0 +1,54 @@
1
+ /* Copyright (C) 2025 Björn Esser <besser82@fedoraproject.org>
2
+ *
3
+ * Redistribution and use in source and binary forms, with or without
4
+ * modification, are permitted.
5
+ *
6
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
7
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
10
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
16
+ * SUCH DAMAGE.
17
+ */
18
+
19
+ #include "crypt-port.h"
20
+
21
+ #if INCLUDE_bcrypt_x
22
+
23
+ #include <errno.h>
24
+ #include <stdio.h>
25
+
26
+ int
27
+ main (void)
28
+ {
29
+ char *retval;
30
+
31
+ errno = 0;
32
+ retval = crypt_gensalt ("$2x$", 0, NULL, 0);
33
+
34
+ if (retval || errno != EINVAL)
35
+ {
36
+ fprintf (stderr, "gensalt: expected \"NULL\", got \"%s\" "
37
+ "with errno == %i, instead of %i.\n",
38
+ retval ? retval : "NULL", errno, EINVAL);
39
+
40
+ return 1;
41
+ }
42
+
43
+ return 0;
44
+ }
45
+
46
+ #else
47
+
48
+ int
49
+ main (void)
50
+ {
51
+ return 77; /* UNSUPPORTED */
52
+ }
53
+
54
+ #endif /* INCLUDE_bcrypt_x */
@@ -0,0 +1,246 @@
1
+ /* Test that the prefix argument to crypt_gensalt affects only the
2
+ choice of hashing method, not any of the parameters or the salt.
3
+
4
+ Written by Zack Weinberg <zackw at panix.com> in 2018.
5
+ To the extent possible under law, Zack Weinberg has waived all
6
+ copyright and related or neighboring rights to this work.
7
+
8
+ See https://creativecommons.org/publicdomain/zero/1.0/ for further
9
+ details. */
10
+
11
+ #include "crypt-port.h"
12
+
13
+ #include <errno.h>
14
+ #include <stdio.h>
15
+
16
+ /* Random bytes used when calling crypt_gensalt; for determinism, these
17
+ are fixed from run to run. */
18
+ #define N_RBYTES 64ul
19
+
20
+ static const char rbytes1[] =
21
+ "90idUkI2+mu2E/tMTViD418j2sPdEYq9LYq0yRW7RYhr4RqQ+oVzIIEcfJBqpf/D";
22
+
23
+ static const char rbytes2[] =
24
+ "sEwXQxrjBTEADauxCpvOQqq7iU9oq6uJ+Iux/fbhtLRj1MWgBFyo/t+nh/nzm0Kn";
25
+
26
+ static_assert(sizeof rbytes1 == N_RBYTES + 1, "rbytes1 is wrong length");
27
+ static_assert(sizeof rbytes2 == N_RBYTES + 1, "rbytes2 is wrong length");
28
+
29
+ struct testcase
30
+ {
31
+ const char *prefix;
32
+ unsigned long count1;
33
+ unsigned long count2;
34
+ };
35
+
36
+ /* This list should include one entry for each potentially-supported
37
+ hash prefix. If the hash method has tunable cost, set count1 and
38
+ count2 to two different nonzero values, within the supported cost
39
+ range. Neither value should equal the default cost. If the hash
40
+ method does not have tunable cost, set count1 and count2 to zero. */
41
+ static const struct testcase testcases[] =
42
+ {
43
+ #if INCLUDE_descrypt || INCLUDE_bigcrypt
44
+ { "", 0, 0 },
45
+ #endif
46
+ #if INCLUDE_bsdicrypt
47
+ { "_", 7019, 1120211 },
48
+ #endif
49
+ #if INCLUDE_nt
50
+ { "$3$", 0, 0 },
51
+ #endif
52
+ #if INCLUDE_md5crypt
53
+ { "$1$", 0, 0 },
54
+ #endif
55
+ #if INCLUDE_sunmd5
56
+ { "$md5", 7019, 1120211 },
57
+ #endif
58
+ #if INCLUDE_sm3crypt
59
+ { "$sm3$", 7019, 1120211 },
60
+ #endif
61
+ #if INCLUDE_sha1crypt
62
+ { "$sha1", 7019, 1120211 },
63
+ #endif
64
+ #if INCLUDE_sha256crypt
65
+ { "$5$", 7019, 1120211 },
66
+ #endif
67
+ #if INCLUDE_sha512crypt
68
+ { "$6$", 7019, 1120211 },
69
+ #endif
70
+ #if INCLUDE_bcrypt
71
+ { "$2b$", 7, 11 },
72
+ #endif
73
+ #if INCLUDE_bcrypt_y
74
+ { "$2y$", 7, 11 },
75
+ #endif
76
+ #if INCLUDE_bcrypt_a
77
+ { "$2a$", 7, 11 },
78
+ #endif
79
+ #if INCLUDE_scrypt
80
+ { "$7$", 7, 11, },
81
+ #endif
82
+ #if INCLUDE_yescrypt
83
+ { "$y$", 7, 11, },
84
+ #endif
85
+ #if INCLUDE_gost_yescrypt
86
+ { "$gy$", 7, 11, },
87
+ #endif
88
+ #if INCLUDE_sm3_yescrypt
89
+ { "$sm3y$", 7, 11, },
90
+ #endif
91
+ { 0, 0, 0, }
92
+ };
93
+
94
+ static int
95
+ do_crypt_gensalt(const char *prefix,
96
+ const char rbytes[MIN_SIZE(N_RBYTES)],
97
+ unsigned long count,
98
+ char outbuf[MIN_SIZE(CRYPT_GENSALT_OUTPUT_SIZE)])
99
+ {
100
+ /* Detect failure to NUL-terminate the output properly. */
101
+ static int ncalls = 0;
102
+ memset(outbuf, '!' + (ncalls % ('~' - '!' + 1)),
103
+ CRYPT_GENSALT_OUTPUT_SIZE - 1);
104
+ outbuf[CRYPT_GENSALT_OUTPUT_SIZE - 1] = 0;
105
+ ncalls++;
106
+
107
+ char *rv = crypt_gensalt_rn(prefix, count, rbytes, N_RBYTES,
108
+ outbuf, CRYPT_GENSALT_OUTPUT_SIZE);
109
+ if (rv == 0)
110
+ {
111
+ printf("ERROR: gensalt(%s, %lu, %c%c..., %lu, outbuf, %lu) = 0/%s\n",
112
+ prefix, count, rbytes[0], rbytes[1],
113
+ N_RBYTES, (unsigned long)CRYPT_GENSALT_OUTPUT_SIZE,
114
+ strerror(errno));
115
+ outbuf[0] = '*';
116
+ memset (outbuf+1, 0, CRYPT_GENSALT_OUTPUT_SIZE-1);
117
+ return 1;
118
+ }
119
+ else if (rv[0] == '*')
120
+ {
121
+ printf("ERROR: gensalt(%s, %lu, %c%c..., %lu, outbuf, %lu) = %s/%s\n",
122
+ prefix, count, rbytes[0], rbytes[1],
123
+ N_RBYTES, (unsigned long)CRYPT_GENSALT_OUTPUT_SIZE,
124
+ outbuf, strerror(errno));
125
+ outbuf[0] = '*';
126
+ memset (outbuf+1, 0, CRYPT_GENSALT_OUTPUT_SIZE-1);
127
+ return 1;
128
+ }
129
+ else
130
+ return 0;
131
+ }
132
+
133
+ static int
134
+ do_check_equal(const char *stst, const char *sref,
135
+ const char *prefix, const char rbytes[N_RBYTES],
136
+ unsigned long count, const char *setting)
137
+ {
138
+ if (!strcmp(stst, sref))
139
+ return 0;
140
+
141
+ printf("FAIL: expected %s\n"
142
+ " got %s\n"
143
+ " from %s, %lu, %c%c...\n"
144
+ " and %s\n",
145
+ sref, stst, prefix, count, rbytes[0], rbytes[1], setting);
146
+ return 1;
147
+ }
148
+
149
+ int
150
+ main(void)
151
+ {
152
+ int status = 0;
153
+ char sref[6][CRYPT_GENSALT_OUTPUT_SIZE];
154
+ char stst[CRYPT_GENSALT_OUTPUT_SIZE];
155
+
156
+ for (size_t i = 0; testcases[i].prefix; i++)
157
+ {
158
+ const char *prefix = testcases[i].prefix;
159
+ unsigned long count1 = testcases[i].count1;
160
+ unsigned long count2 = testcases[i].count2;
161
+ int ncases;
162
+
163
+ memset(sref, 0, sizeof sref);
164
+
165
+ /* If count1 and count2 are both nonzero, then they should also
166
+ be unequal, and we have six reference cases:
167
+ (0, count1, count2) x (rbytes1, rbytes2).
168
+ If count1 and count2 are both zero, then we only have two
169
+ reference cases: 0 x (rbytes1, rbytes2) (this happens when the
170
+ hash method doesn't have tunable cost).
171
+ It is incorrect for only one of count1 and count2 to be zero,
172
+ or for them to be equal but nonzero. */
173
+ if (count1 == 0 && count2 == 0)
174
+ {
175
+ ncases = 2;
176
+ status |= do_crypt_gensalt(prefix, rbytes1, 0, sref[0]);
177
+ status |= do_crypt_gensalt(prefix, rbytes2, 0, sref[1]);
178
+ }
179
+ else if (count1 != 0 && count2 != 0 && count1 != count2)
180
+ {
181
+ ncases = 6;
182
+ status |= do_crypt_gensalt(prefix, rbytes1, 0, sref[0]);
183
+ status |= do_crypt_gensalt(prefix, rbytes2, 0, sref[1]);
184
+ status |= do_crypt_gensalt(prefix, rbytes1, count1, sref[2]);
185
+ status |= do_crypt_gensalt(prefix, rbytes2, count1, sref[3]);
186
+ status |= do_crypt_gensalt(prefix, rbytes1, count2, sref[4]);
187
+ status |= do_crypt_gensalt(prefix, rbytes2, count2, sref[5]);
188
+ }
189
+ else
190
+ {
191
+ printf ("ERROR: %zu/%s: inappropriate count1=%lu count2=%lu\n",
192
+ i, prefix, count1, count2);
193
+ status = 1;
194
+ continue;
195
+ }
196
+
197
+ /* At this point, sref[0..ncases] are filled with setting
198
+ strings corresponding to different combinations of salt and
199
+ cost. If we reuse those strings as prefixes for crypt_gensalt,
200
+ none of the additional information should affect the output. */
201
+ for (int j = 0; j < ncases; j++)
202
+ {
203
+ if (sref[j][0] == '*')
204
+ continue; /* initial crypt_gensalt call failed */
205
+ if (count1 == 0 && count2 == 0)
206
+ {
207
+ status |= do_crypt_gensalt(sref[j], rbytes1, 0, stst);
208
+ status |= do_check_equal(stst, sref[0],
209
+ prefix, rbytes1, 0, sref[j]);
210
+
211
+ status |= do_crypt_gensalt(sref[j], rbytes2, 0, stst);
212
+ status |= do_check_equal(stst, sref[1],
213
+ prefix, rbytes2, 0, sref[j]);
214
+ }
215
+ else
216
+ {
217
+ status |= do_crypt_gensalt(sref[j], rbytes1, 0, stst);
218
+ status |= do_check_equal(stst, sref[0],
219
+ prefix, rbytes1, 0, sref[j]);
220
+
221
+ status |= do_crypt_gensalt(sref[j], rbytes2, 0, stst);
222
+ status |= do_check_equal(stst, sref[1],
223
+ prefix, rbytes2, 0, sref[j]);
224
+
225
+ status |= do_crypt_gensalt(sref[j], rbytes1, count1, stst);
226
+ status |= do_check_equal(stst, sref[2],
227
+ prefix, rbytes1, count1, sref[j]);
228
+
229
+ status |= do_crypt_gensalt(sref[j], rbytes2, count1, stst);
230
+ status |= do_check_equal(stst, sref[3],
231
+ prefix, rbytes2, count1, sref[j]);
232
+
233
+ status |= do_crypt_gensalt(sref[j], rbytes1, count2, stst);
234
+ status |= do_check_equal(stst, sref[4],
235
+ prefix, rbytes1, count2, sref[j]);
236
+
237
+ status |= do_crypt_gensalt(sref[j], rbytes2, count2, stst);
238
+ status |= do_check_equal(stst, sref[5],
239
+ prefix, rbytes2, count2, sref[j]);
240
+ }
241
+ }
242
+
243
+ }
244
+
245
+ return status;
246
+ }