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,726 @@
1
+ /* Test rejection of ill-formed password hashes.
2
+ Copyright (C) 2012-2018 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
+ <http://www.gnu.org/licenses/>. */
18
+
19
+ #include "crypt-port.h"
20
+ #undef yescrypt
21
+
22
+ #include <errno.h>
23
+ #include <setjmp.h>
24
+ #include <stdio.h>
25
+ #include <string.h>
26
+ #include <signal.h>
27
+ #include <sys/mman.h>
28
+
29
+ /* If VERBOSE is true, passing testcases will be printed out as well
30
+ as failing ones. */
31
+ static bool verbose = false;
32
+
33
+ /* All hashes are hashes of this passphrase, an infamous error message
34
+ used for some forgotten can't-happen condition in Unix V6; see
35
+ <https://wiki.tuhs.org/doku.php?id=anecdotes:values_of_beta>. */
36
+ static const char phrase[] = "values of β will give rise to dom!";
37
+
38
+ /* Correct setting strings, from which we derive incorrect ones by
39
+ replacing one character at a time with a character that cannot
40
+ appear in a valid passphrase (namely ':') and/or truncating the
41
+ string. */
42
+ struct valid_setting;
43
+
44
+ /* Type of functions to use in is_valid_trunc. */
45
+ typedef bool (*valid_trunc_p)(const struct valid_setting *original,
46
+ const char *truncated);
47
+
48
+ struct valid_setting
49
+ {
50
+ /* Human-readable name for this test */
51
+ const char *tag;
52
+
53
+ /* The setting string */
54
+ const char *setting;
55
+
56
+ /* Length of the actual setting, within the setting string. This is
57
+ usually equal to strlen(setting), but a couple of the strings are
58
+ padded on the right for hash-specific reasons. */
59
+ size_t setting_len;
60
+
61
+ /* Given a truncation of a valid setting string, decide whether the
62
+ truncation is also valid. */
63
+ valid_trunc_p is_valid_trunc;
64
+
65
+ /* Numeric parameter for is_valid_trunc; usually the length of a
66
+ subfield of the setting. */
67
+ uint16_t is_valid_trunc_param;
68
+
69
+ /* Whether support for this hash was compiled into the library. */
70
+ bool enabled;
71
+
72
+ };
73
+
74
+ /* is_valid_trunc functions -- forward declarations */
75
+
76
+ static bool vt_never(const struct valid_setting *, const char *);
77
+ static bool vt_varsuffix(const struct valid_setting *, const char *);
78
+ static bool vt_sunmd5(const struct valid_setting *, const char *);
79
+ static bool vt_sm3(const struct valid_setting *, const char *);
80
+ static bool vt_sha2gnu(const struct valid_setting *, const char *);
81
+ static bool vt_yescrypt(const struct valid_setting *, const char *);
82
+
83
+ /* shorthands for use in valid_cases */
84
+
85
+ #define V_( hash, setting, vt, vp) \
86
+ { #hash, setting, sizeof setting - 1, vt, vp, INCLUDE_##hash }
87
+ #define Vp_( hash, setting, vt, vp) \
88
+ { #hash, setting, vp, vt, vp, INCLUDE_##hash }
89
+ #define Vt_( hash, tag, setting, vt, vp) \
90
+ { #hash " (" #tag ")", setting, sizeof setting - 1, vt, vp, INCLUDE_##hash }
91
+ #define Vtp_(hash, tag, setting, vt, vp) \
92
+ { #hash " (" #tag ")", setting, vp, vt, vp, INCLUDE_##hash }
93
+
94
+ #define V( hash, setting) V_( hash, setting, vt_never, 0)
95
+ #define Vn( hash, vt, setting) V_( hash, setting, vt_##vt, 0)
96
+ #define Vp( hash, sl, setting) Vp_( hash, setting, vt_varsuffix, sl)
97
+ #define Vv( hash, sl, setting) V_( hash, setting, vt_varsuffix, sl)
98
+ #define Vt( hash, tag, setting) Vt_( hash, tag, setting, vt_never, 0)
99
+ #define Vtn(hash, tag, vt, setting) Vt_( hash, tag, setting, vt_##vt, 0)
100
+ #define Vtp(hash, tag, sl, setting) Vtp_(hash, tag, setting, vt_varsuffix, sl)
101
+ #define Vtv(hash, tag, sl, setting) Vt_( hash, tag, setting, vt_varsuffix, sl)
102
+
103
+ /* Each of these is a valid setting string for some algorithm,
104
+ from which we will derive many invalid setting strings.
105
+ This is an expensive test, so where possible, the number of
106
+ "rounds" of the hash function has been set abnormally low. */
107
+ static const struct valid_setting valid_cases[] =
108
+ {
109
+ V (descrypt, "Mp" ),
110
+ /* bigcrypt is extra special:
111
+ this salt is a valid descrypt salt when bigcrypt isn't enabled
112
+ but descrypt is;
113
+ truncations down to 2 are valid when descrypt is enabled, but
114
+ if *only* bigcrypt is enabled, then truncations can only
115
+ go down to 14. */
116
+ {
117
+ INCLUDE_bigcrypt ? "bigcrypt" : "descrypt (padded)",
118
+ "Mp............", 2, vt_varsuffix,
119
+ INCLUDE_descrypt ? 2 : 14,
120
+ INCLUDE_descrypt || INCLUDE_bigcrypt
121
+ },
122
+ V (bsdicrypt, "_J9..MJHn" ),
123
+ Vv (md5crypt, 3, "$1$MJHnaAke$" ),
124
+ Vtn(sunmd5, plain, sunmd5, "$md5$1xMeE.at$" ),
125
+ Vtn(sunmd5, rounds, sunmd5, "$md5,rounds=123$1xMeE.at$" ),
126
+ Vt (nt, plain, "$3$" ),
127
+ Vtp(nt, fake salt, 3, "$3$__not_used__c809a450df09a3" ),
128
+ Vv (sha1crypt, 11, "$sha1$123$GGXpNqoJvglVTkGU$" ),
129
+ Vtn(sha256crypt, plain, sha2gnu, "$5$MJHnaAkegEVYHsFK$" ),
130
+ Vtn(sha256crypt, rounds, sha2gnu, "$5$rounds=1000$MJHnaAkegEVYHsFK$" ),
131
+ Vtn(sha512crypt, plain, sha2gnu, "$6$MJHnaAkegEVYHsFK$" ),
132
+ Vtn(sha512crypt, rounds, sha2gnu, "$6$rounds=1000$MJHnaAkegEVYHsFK$" ),
133
+ Vtn(sm3crypt, plain, sm3, "$sm3$MJHnaAkegEVYHsFK$" ),
134
+ Vtn(sm3crypt, rounds, sm3, "$sm3$rounds=1000$MJHnaAkegEVYHsFK$" ),
135
+ V (bcrypt, "$2b$04$UBVLHeMpJ/QQCv3XqJx8zO" ),
136
+ V (bcrypt_a, "$2a$04$UBVLHeMpJ/QQCv3XqJx8zO" ),
137
+ V (bcrypt_x, "$2x$04$UBVLHeMpJ/QQCv3XqJx8zO" ),
138
+ V (bcrypt_y, "$2y$04$UBVLHeMpJ/QQCv3XqJx8zO" ),
139
+ Vv (scrypt, 14, "$7$C6..../....SodiumChloride$" ),
140
+ Vn (yescrypt, yescrypt, "$y$j9T$PKXc3hCOSyMqdaEQArI62/$" ),
141
+ Vn (gost_yescrypt, yescrypt, "$gy$j9T$PKXc3hCOSyMqdaEQArI62/$" ),
142
+ Vn (sm3_yescrypt, yescrypt, "$sm3y$j9T$PKXc3hCOSyMqdaEQArI62/$" ),
143
+ };
144
+
145
+ #undef V_
146
+ #undef Vp_
147
+ #undef Vt_
148
+ #undef Vtp_
149
+
150
+ #undef V
151
+ #undef Vn
152
+ #undef Vp
153
+ #undef Vv
154
+ #undef Vt
155
+ #undef Vtn
156
+ #undef Vtp
157
+ #undef Vtv
158
+
159
+ /* Additional tests of manually constructed, invalid setting
160
+ strings. */
161
+ struct invalid_setting
162
+ {
163
+ const char *tag;
164
+ const char *setting;
165
+ };
166
+ static const struct invalid_setting invalid_cases[] =
167
+ {
168
+ /* These strings are invalid regardless of the algorithm. */
169
+ { "too short 1", "/" },
170
+ { "too short 2", "M" },
171
+ { "too short 3", "$" },
172
+ { "too short 4", "_" },
173
+ { "too short 5", "." },
174
+ { "invalid char :", ":" },
175
+ { "invalid char ;", ";" },
176
+ { "invalid char *", "*" },
177
+ { "invalid char !", "!" },
178
+ { "invalid char \\", "\\" },
179
+ { "invalid char SPC", " " },
180
+ { "invalid char TAB", "\t" },
181
+ { "invalid char ^M", "\r" },
182
+ { "invalid char ^J", "\n" },
183
+ { "invalid char ^L", "\f" },
184
+ { "invalid char ^A", "\001" },
185
+ { "invalid char DEL", "\177" },
186
+ { "failure token 1", "*0" },
187
+ { "failure token 2", "*1" },
188
+ { "unsupported algorithm", "$un$upp0rt3d$" },
189
+ { "empty string", "" },
190
+
191
+ /* These strings are invalid for specific algorithms, in ways
192
+ that the generic error generator cannot produce. */
193
+ { "sunmd5 absent rounds", "$md5,rounds=$1xMeE.at$" },
194
+ { "sunmd5 low rounds", "$md5,rounds=0$1xMeE.at$" },
195
+ { "sunmd5 octal rounds", "$md5,rounds=012$1xMeE.at$" },
196
+ { "sunmd5 high rounds", "$md5,rounds=4294967296$1xMeE.at$" },
197
+ { "sha256 absent rounds", "$5$rounds=$MJHnaAkegEVYHsFK$" },
198
+ { "sha256 low rounds", "$5$rounds=0$MJHnaAkegEVYHsFK$" },
199
+ { "sha256 octal rounds", "$5$rounds=0100$MJHnaAkegEVYHsFK$" },
200
+ { "sha256 high rounds", "$5$rounds=4294967295$MJHnaAkegEVYHsFK$" },
201
+ { "sha512 absent rounds", "$6$rounds=$MJHnaAkegEVYHsFK$" },
202
+ { "sha512 low rounds", "$6$rounds=0$MJHnaAkegEVYHsFK$" },
203
+ { "sha512 octal rounds", "$6$rounds=0100$MJHnaAkegEVYHsFK$" },
204
+ { "sha512 high rounds", "$6$rounds=4294967295$MJHnaAkegEVYHsFK$" },
205
+ { "sm3 absent rounds", "$sm3$rounds=$MJHnaAkegEVYHsFK$" },
206
+ { "sm3 low rounds", "$sm3$rounds=0$MJHnaAkegEVYHsFK$" },
207
+ { "sm3 octal rounds", "$sm3$rounds=0100$MJHnaAkegEVYHsFK$" },
208
+ { "sm3 high rounds", "$sm3$rounds=4294967295$MJHnaAkegEVYHsFK$" },
209
+ { "bcrypt no subtype", "$2$04$UBVLHeMpJ/QQCv3XqJx8zO" },
210
+ { "bcrypt_b low rounds", "$2b$03$UBVLHeMpJ/QQCv3XqJx8zO" },
211
+ { "bcrypt_b high rounds", "$2b$32$UBVLHeMpJ/QQCv3XqJx8zO" },
212
+ { "bcrypt_a low rounds", "$2a$03$UBVLHeMpJ/QQCv3XqJx8zO" },
213
+ { "bcrypt_a high rounds", "$2a$32$UBVLHeMpJ/QQCv3XqJx8zO" },
214
+ { "bcrypt_x low rounds", "$2x$03$UBVLHeMpJ/QQCv3XqJx8zO" },
215
+ { "bcrypt_x high rounds", "$2x$32$UBVLHeMpJ/QQCv3XqJx8zO" },
216
+ { "bcrypt_y low rounds", "$2y$03$UBVLHeMpJ/QQCv3XqJx8zO" },
217
+ { "bcrypt_y low rounds", "$2y$32$UBVLHeMpJ/QQCv3XqJx8zO" },
218
+ { "yescrypt short params", "$y$j9$PKXc3hCOSyMqdaEQArI62/$" },
219
+ { "gost-yescrypt short params", "$gy$j9$PKXc3hCOSyMqdaEQArI62/$" },
220
+ { "sm3-yescrypt short params", "$sm3y$j9$PKXc3hCOSyMqdaEQArI62/$" },
221
+ };
222
+
223
+ /* is_valid_trunc functions -- definitions.
224
+ Note: these only need to be correct for the patterns we actually test. */
225
+
226
+ /* All truncations of this setting string are invalid. */
227
+ static bool
228
+ vt_never(const struct valid_setting * ARG_UNUSED(original),
229
+ const char * ARG_UNUSED(truncated))
230
+ {
231
+ return false;
232
+ }
233
+
234
+ /* This setting string has a variable-length suffix; truncations are
235
+ valid as long as the result has at least `is_valid_trunc_param'
236
+ characters. */
237
+ static bool
238
+ vt_varsuffix(const struct valid_setting *original,
239
+ const char *truncated)
240
+ {
241
+ return strlen(truncated) >= original->is_valid_trunc_param;
242
+ }
243
+
244
+ /* Special validity rule for sunmd5, sha256crypt, and sha512crypt: ... */
245
+ static bool
246
+ vt_roundseq(const char *truncated, size_t minlen, size_t roundslen,
247
+ const char *roundstag1, const char *roundstag2)
248
+ {
249
+ /* ... the setting cannot be valid if it's shorter than 'minlen'
250
+ characters ... */
251
+ if (strlen(truncated) < minlen)
252
+ return false;
253
+
254
+ /* ... if it begins with roundstag1 or roundstag2 then a sequence of
255
+ digits must follow, then a dollar sign; roundstag2 may be null;
256
+ ... */
257
+ if (!strncmp(truncated, roundstag1, roundslen)
258
+ || (roundstag2 && !strncmp(truncated, roundstag2, roundslen)))
259
+ {
260
+ size_t i = roundslen;
261
+ while (truncated[i] >= '0' && truncated[i] <= '9')
262
+ i++;
263
+ if (truncated[i] != '$')
264
+ return false;
265
+ }
266
+
267
+ /* ... otherwise it's ok. */
268
+ return true;
269
+ }
270
+
271
+ /* Special validity rule for sunmd5. */
272
+ static bool
273
+ vt_sunmd5(const struct valid_setting *ARG_UNUSED(original),
274
+ const char *truncated)
275
+ {
276
+ return vt_roundseq(truncated, strlen("$md5$"), strlen("$md5,rounds="),
277
+ "$md5,rounds=", 0);
278
+ }
279
+
280
+ /* Special validity rule for sm3. */
281
+ static bool
282
+ vt_sm3(const struct valid_setting *ARG_UNUSED(original),
283
+ const char *truncated)
284
+ {
285
+ return vt_roundseq(truncated, strlen("$sm3$"), strlen("$sm3$rounds="),
286
+ "$sm3$rounds=", 0);
287
+ }
288
+
289
+ /* Special validity rule for sha256crypt and sha512crypt. */
290
+ static bool
291
+ vt_sha2gnu(const struct valid_setting *ARG_UNUSED(original),
292
+ const char *truncated)
293
+ {
294
+ return vt_roundseq(truncated, strlen("$5$"), strlen("$5$rounds="),
295
+ "$5$rounds=", "$6$rounds=");
296
+ }
297
+
298
+ /* Special validity rule for yescrypt and gost_yescrypt: ... */
299
+ static bool
300
+ vt_yescrypt(const struct valid_setting *ARG_UNUSED(original),
301
+ const char *truncated)
302
+ {
303
+ /* ... the setting string must begin with "$y$j9T$" or "$gy$j9T$"
304
+ (other introductory sequences are possible but those are the
305
+ only ones we use); ... */
306
+ size_t y_intro_len = strlen("$y$j9T$");
307
+ size_t gy_intro_len = strlen("$gy$j9T$");
308
+ size_t sm3y_intro_len = strlen("$sm3y$j9T$");
309
+ size_t intro_len;
310
+ if (!strncmp(truncated, "$y$j9T$", y_intro_len))
311
+ intro_len = y_intro_len;
312
+ else if (!strncmp(truncated, "$gy$j9T$", gy_intro_len))
313
+ intro_len = gy_intro_len;
314
+ else if (!strncmp(truncated, "$sm3y$j9T$", sm3y_intro_len))
315
+ intro_len = sm3y_intro_len;
316
+ else
317
+ return false;
318
+
319
+ /* ... and the remainder must be one of these lengths. (I do not
320
+ see a pattern.) */
321
+ switch (strlen(truncated) - intro_len)
322
+ {
323
+ case 0:
324
+ case 4:
325
+ case 7:
326
+ case 8:
327
+ case 12:
328
+ case 16:
329
+ case 20:
330
+ case 22:
331
+ case 23:
332
+ return true;
333
+ default:
334
+ return false;
335
+ }
336
+ }
337
+
338
+
339
+ /* Some of the test setting strings contain unprintable characters,
340
+ which we print as hex escapes. For readability, whenever we print
341
+ out a setting string we pad it on the right with spaces to the
342
+ length of the longest setting string we have. (There is always
343
+ something after that on the line.) */
344
+ static size_t longest_setting;
345
+
346
+ static void
347
+ print_setting (const char *setting, bool pad)
348
+ {
349
+ size_t n = 0;
350
+ for (; *setting; setting++)
351
+ {
352
+ unsigned int c = (unsigned int)(unsigned char) *setting;
353
+ if (0x20 <= c && c <= 0x7e)
354
+ {
355
+ putchar ((int)c);
356
+ n += 1;
357
+ }
358
+ else
359
+ {
360
+ printf ("\\x%02x", c);
361
+ n += 4;
362
+ }
363
+ }
364
+ if (!pad)
365
+ return;
366
+ while (n < longest_setting)
367
+ {
368
+ putchar (' ');
369
+ n += 1;
370
+ }
371
+ }
372
+
373
+ static size_t
374
+ measure_setting (const char *setting)
375
+ {
376
+ size_t n = 0;
377
+ for (; *setting; setting++)
378
+ {
379
+ unsigned int c = (unsigned int)(unsigned char) *setting;
380
+ if (0x20 <= c && c <= 0x7e)
381
+ n += 1;
382
+ else
383
+ n += 4;
384
+ }
385
+ return n;
386
+ }
387
+
388
+ static void
389
+ measure_settings (void)
390
+ {
391
+ size_t ls = 0;
392
+ for (size_t i = 0; i < ARRAY_SIZE (valid_cases); i++)
393
+ ls = MAX (ls, measure_setting(valid_cases[i].setting));
394
+
395
+ for (size_t i = 0; i < ARRAY_SIZE (invalid_cases); i++)
396
+ ls = MAX (ls, measure_setting(invalid_cases[i].setting));
397
+
398
+ longest_setting = ls;
399
+ }
400
+
401
+ static void
402
+ print_result (const char *result, const char *setting,
403
+ const char *tag, bool expected_valid)
404
+ {
405
+ printf ("%s: ", result);
406
+ print_setting (setting, true);
407
+ printf (" (%s, %s)", tag, expected_valid ? "valid" : "invalid");
408
+ }
409
+
410
+ /* Part of what we're testing, is whether any of the hashing methods
411
+ can read past the end of a properly terminated C string that
412
+ happens to contain an invalid setting. We do this by placing the
413
+ invalid setting right next to a page of inaccessible memory and
414
+ trapping the fault. */
415
+ static volatile sig_atomic_t signal_loop = 0;
416
+ static sigjmp_buf env;
417
+ static void
418
+ segv_handler (int sig)
419
+ {
420
+ if (signal_loop == 0)
421
+ {
422
+ signal_loop = 1;
423
+ siglongjmp (env, sig);
424
+ }
425
+ else
426
+ {
427
+ signal (sig, SIG_DFL);
428
+ raise (sig);
429
+ }
430
+ }
431
+
432
+ /* We use only crypt_rn in this test, because it only exercises the
433
+ error handling logic within the hashing methods, not the
434
+ higher-level error handling logic that varies slightly among the
435
+ entry points (that's all taken care of in crypt-badargs.c). We use
436
+ crypt_rn instead of crypt_r so that this test does not need to vary
437
+ any of its logic based on --enable-failure-tokens. */
438
+ static bool
439
+ test_one_setting (const char *setting, size_t l_setting,
440
+ const char *tag, bool expected_valid,
441
+ struct crypt_data *cd)
442
+ {
443
+ volatile bool fail = false;
444
+ signal_loop = 0;
445
+ int sig = sigsetjmp (env, 1);
446
+ if (!sig)
447
+ {
448
+ char *retval = crypt_rn (phrase, setting, cd, (int) sizeof *cd);
449
+ if (expected_valid)
450
+ {
451
+ if (!retval)
452
+ {
453
+ fail = true;
454
+ print_result ("FAIL", setting, tag, expected_valid);
455
+ puts(": returned NULL");
456
+ }
457
+ else if (retval != cd->output)
458
+ {
459
+ fail = true;
460
+ print_result ("FAIL", setting, tag, expected_valid);
461
+ printf(": returned %p, should be %p\n",
462
+ (const void *)retval, (const void *)cd->output);
463
+ }
464
+ else if (strncmp (retval, setting, l_setting))
465
+ {
466
+ fail = true;
467
+ print_result("FAIL", setting, tag, expected_valid);
468
+ fputs(": got non-matching ", stdout);
469
+ print_setting(retval, false);
470
+ putchar('\n');
471
+ }
472
+ }
473
+ else
474
+ {
475
+ if (retval)
476
+ {
477
+ fail = true;
478
+ print_result ("FAIL", setting, tag, expected_valid);
479
+ fputs(": expected NULL, got ", stdout);
480
+ print_setting (retval, false);
481
+ putchar('\n');
482
+ }
483
+ }
484
+ }
485
+ else
486
+ {
487
+ fail = true;
488
+ print_result("FAIL", setting, tag, expected_valid);
489
+ printf(": %s\n", strsignal (sig));
490
+ }
491
+
492
+ if (verbose && !fail)
493
+ {
494
+ print_result("PASS", setting, tag, expected_valid);
495
+ putchar('\n');
496
+ }
497
+
498
+ return fail;
499
+ }
500
+
501
+ static bool
502
+ test_one_valid(const struct valid_setting *tc,
503
+ char *page, size_t pagesize, struct crypt_data *cd)
504
+ {
505
+ /* Caution: tc->setting_len is _not_ always equal to strlen(tc->setting).
506
+ Sometimes it is smaller. */
507
+ size_t l_setting = strlen(tc->setting) + 1;
508
+ char *setting = page + pagesize - l_setting;
509
+ memcpy(setting, tc->setting, l_setting);
510
+
511
+ /* crypt_rn() using this setting, unmodified, is expected to
512
+ succeed, unless the hash function is disabled. */
513
+ if (test_one_setting (setting, tc->setting_len, tc->tag, tc->enabled, cd))
514
+ return true;
515
+
516
+ /* Rechecking the hash with the full output should also succeed.
517
+ In this subtest we expect to get the same _complete hash_
518
+ back out, not just the same setting. */
519
+ if (tc->enabled)
520
+ {
521
+ size_t l_hash = strlen (cd->output);
522
+ char *p = page + pagesize - (l_hash + 1);
523
+ assert (l_hash + 1 <= CRYPT_OUTPUT_SIZE);
524
+ memcpy (p, cd->output, l_hash + 1);
525
+
526
+ if (test_one_setting (p, l_hash, tc->tag, true, cd))
527
+ return true;
528
+
529
+ /* When crypt() is called with a complete hashed passphrase as the
530
+ setting string, the hashing method must not look at the hash
531
+ component of the setting _at all_. We test this by supplying a
532
+ string with one extra character, an A, which _could_ be part of
533
+ the hash component for all supported methods, but which is much
534
+ too short by itself. This should produce the same complete hash
535
+ as the previous test. (It has to be a character which _could_
536
+ appear, because the generic crypt() machinery rejects setting
537
+ strings containing invalid characters in any position.)
538
+
539
+ Super special case: Don't do this subtest for sunmd5,
540
+ because, due to a bug in its original implementation, the
541
+ first character after the end of the salt _does_ affect the
542
+ hash output. We have to preserve this bug for compatibility
543
+ with existing sunmd5 hashed passphrases. */
544
+ if (!INCLUDE_sunmd5 || strncmp(tc->setting, "$md5", 4))
545
+ {
546
+ p = page + pagesize - (l_hash + 1 + l_setting + 1);
547
+ memcpy (p, cd->output, l_hash + 1);
548
+
549
+ char *settingA = page + pagesize - (l_setting + 1);
550
+ memcpy(settingA, tc->setting, l_setting - 1);
551
+ settingA[l_setting - 1] = 'A';
552
+ settingA[l_setting - 0] = '\0';
553
+ if (test_one_setting (settingA, tc->setting_len, tc->tag, true, cd))
554
+ return true;
555
+ if (strcmp (cd->output, p))
556
+ {
557
+ print_result ("FAIL", settingA, tc->tag, true);
558
+ /* Since cd->output and p are both hashed passphrases, not
559
+ handcrafted invalid setting strings, we can safely print
560
+ them with %s. */
561
+ printf (": expected %s, got %s\n", p, cd->output);
562
+ return true;
563
+ }
564
+ else if (verbose)
565
+ {
566
+ print_result ("PASS", settingA, tc->tag, true);
567
+ printf (": got %s, as expected\n", cd->output);
568
+ }
569
+ }
570
+
571
+ /* Restore the original data at 'setting', as expected by code
572
+ below. */
573
+ memcpy(setting, tc->setting, l_setting);
574
+ }
575
+
576
+ /* The rest of the subtests in this function are logically independent. */
577
+ bool failed = false;
578
+
579
+ /* Replacing any one character of this setting with a ':', leaving
580
+ the rest of the string intact, should cause crypt_rn to fail. */
581
+ for (size_t i = 0; i < l_setting - 1; i++)
582
+ {
583
+ char saved = setting[i];
584
+ setting[i] = ':';
585
+ failed |= test_one_setting(setting, tc->setting_len, tc->tag, false, cd);
586
+ setting[i] = saved;
587
+ }
588
+
589
+ /* Chop off the last character of the setting string and test that.
590
+ Then, replace the new last character of the setting string with a
591
+ colon, and test that. (This is different from the earlier test
592
+ where we replaced each character in turn with a colon but kept
593
+ the rest of the string intact, because the hashing method might
594
+ be calling strlen() on the setting string.) Repeat these two
595
+ steps until we have just one character left, then stop.
596
+
597
+ For instance, if the original setting string is
598
+ $1$MJHnaAke$
599
+ then we test
600
+ $1$MJHnaAke
601
+ $1$MJHnaAk:
602
+ $1$MJHnaAk
603
+ $1$MJHnaA:
604
+ $1$MJHnaA
605
+ ...
606
+ $1
607
+ $:
608
+
609
+ ($1$MJHnaAke: would have been tested by the loop above. All the
610
+ single-character strings that can be a prefix of a setting string
611
+ from valid_cases---"$", "_", "M"---are tested by invalid_cases,
612
+ is ":".)
613
+
614
+ Up till this point l_setting has been _one more than_
615
+ strlen(setting), but in this loop it is more convenient to have
616
+ it be equal to strlen(setting). */
617
+ l_setting -= 1;
618
+
619
+ while (l_setting > 2)
620
+ {
621
+ memmove(setting + 1, setting, l_setting - 1);
622
+ setting += 1;
623
+ l_setting -= 1;
624
+ failed |= test_one_setting(setting, MIN (l_setting, tc->setting_len),
625
+ tc->tag,
626
+ tc->enabled
627
+ && tc->is_valid_trunc(tc, setting),
628
+ cd);
629
+ page[pagesize - 2] = ':';
630
+ failed |= test_one_setting(setting, l_setting, tc->tag, false, cd);
631
+ }
632
+
633
+ return failed;
634
+ }
635
+
636
+ static bool
637
+ test_one_invalid(const struct invalid_setting *tc,
638
+ char *page, size_t pagesize, struct crypt_data *cd)
639
+ {
640
+ size_t l_setting = strlen(tc->setting) + 1;
641
+ char *setting = page + pagesize - l_setting;
642
+ memcpy(setting, tc->setting, l_setting);
643
+ return test_one_setting(setting, l_setting - 1, tc->tag, false, cd);
644
+ }
645
+
646
+ static bool
647
+ do_tests(char *page, size_t pagesize)
648
+ {
649
+ bool failed = false;
650
+
651
+ struct crypt_data cd;
652
+ memset (&cd, 0, sizeof cd);
653
+
654
+ for (size_t i = 0; i < ARRAY_SIZE (valid_cases); i++)
655
+ failed |= test_one_valid (&valid_cases[i], page, pagesize, &cd);
656
+
657
+ for (size_t i = 0; i < ARRAY_SIZE (invalid_cases); i++)
658
+ failed |= test_one_invalid (&invalid_cases[i], page, pagesize, &cd);
659
+
660
+ return failed;
661
+ }
662
+
663
+ int
664
+ main (int argc, char **argv)
665
+ {
666
+ if (argc <= 1)
667
+ ;
668
+ else if (argc == 2
669
+ && (!strcmp(argv[1], "-v")
670
+ || !strcmp(argv[1], "--verbose")))
671
+ verbose = true;
672
+ else
673
+ {
674
+ fprintf(stderr, "usage: %s [-v | --verbose]\n", argv[0]);
675
+ return 99;
676
+ }
677
+
678
+ if (setvbuf(stdout, 0, _IOLBF, 0) || setvbuf(stderr, 0, _IOLBF, 0))
679
+ {
680
+ perror ("setvbuf");
681
+ return 99;
682
+ }
683
+
684
+ /* Set up a two-page region whose first page is read-write and
685
+ whose second page is inaccessible. */
686
+ long pagesize_l = sysconf (_SC_PAGESIZE);
687
+ if (pagesize_l < (long) CRYPT_OUTPUT_SIZE)
688
+ {
689
+ printf ("ERROR: pagesize of %ld is too small\n", pagesize_l);
690
+ return 99;
691
+ }
692
+
693
+ size_t pagesize = (size_t) pagesize_l;
694
+ char *page = mmap (0, pagesize * 2, PROT_READ|PROT_WRITE,
695
+ MAP_PRIVATE|MAP_ANON, -1, 0);
696
+ if (page == MAP_FAILED)
697
+ {
698
+ perror ("mmap");
699
+ return 99;
700
+ }
701
+ // coverity[overflow_sink]
702
+ memset (page, 'x', pagesize * 2);
703
+ if (mprotect (page + pagesize, pagesize, PROT_NONE))
704
+ {
705
+ perror ("mprotect");
706
+ return 99;
707
+ }
708
+
709
+ struct sigaction sa, os, ob;
710
+ sigfillset (&sa.sa_mask);
711
+ sa.sa_flags = SA_RESTART;
712
+ sa.sa_handler = segv_handler;
713
+ if (sigaction (SIGBUS, &sa, &ob) || sigaction (SIGSEGV, &sa, &os))
714
+ {
715
+ perror ("sigaction");
716
+ return 1;
717
+ }
718
+
719
+ measure_settings();
720
+ bool failed = do_tests (page, pagesize);
721
+
722
+ sigaction (SIGBUS, &ob, 0);
723
+ sigaction (SIGSEGV, &os, 0);
724
+
725
+ return failed;
726
+ }