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,265 @@
1
+ /* Copyright (C) 2018-2021 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
+ #include <stdio.h>
21
+
22
+ struct testcase
23
+ {
24
+ const char *prefix;
25
+ const int exp_prefix;
26
+ const int exp_gensalt;
27
+ const int exp_crypt;
28
+ };
29
+
30
+ static const struct testcase testcases[] =
31
+ {
32
+ #if INCLUDE_descrypt || INCLUDE_bigcrypt
33
+ { "", CRYPT_SALT_INVALID, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
34
+ { "..", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
35
+ { "MN", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
36
+ #else
37
+ { "", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
38
+ { "..", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
39
+ { "MN", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
40
+ #endif
41
+ #if INCLUDE_bsdicrypt
42
+ { "_", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
43
+ #else
44
+ { "_", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
45
+ #endif
46
+ #if INCLUDE_md5crypt
47
+ { "$1$", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
48
+ #else
49
+ { "$1$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
50
+ #endif
51
+ #if INCLUDE_nt
52
+ { "$3$", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
53
+ #else
54
+ { "$3$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
55
+ #endif
56
+ #if INCLUDE_sunmd5
57
+ { "$md5", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
58
+ #else
59
+ { "$md5", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
60
+ #endif
61
+ #if INCLUDE_sha1crypt
62
+ { "$sha1", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
63
+ #else
64
+ { "$sha1", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
65
+ #endif
66
+ #if INCLUDE_sha256crypt
67
+ { "$5$", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
68
+ #else
69
+ { "$5$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
70
+ #endif
71
+ #if INCLUDE_sha512crypt
72
+ { "$6$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
73
+ #else
74
+ { "$6$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
75
+ #endif
76
+ #if INCLUDE_sm3crypt
77
+ { "$sm3$", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_METHOD_LEGACY },
78
+ #else
79
+ { "$sm3$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
80
+ #endif
81
+ #if INCLUDE_bcrypt
82
+ { "$2b$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
83
+ #else
84
+ { "$2b$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
85
+ #endif
86
+ #if INCLUDE_bcrypt_a
87
+ { "$2a$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
88
+ #else
89
+ { "$2a$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
90
+ #endif
91
+ #if INCLUDE_bcrypt_x
92
+ { "$2x$", CRYPT_SALT_METHOD_LEGACY, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
93
+ #else
94
+ { "$2x$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
95
+ #endif
96
+ #if INCLUDE_bcrypt_y
97
+ { "$2y$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
98
+ #else
99
+ { "$2y$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
100
+ #endif
101
+ #if INCLUDE_yescrypt
102
+ { "$y$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
103
+ #else
104
+ { "$y$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
105
+ #endif
106
+ #if INCLUDE_scrypt
107
+ { "$7$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
108
+ #else
109
+ { "$7$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
110
+ #endif
111
+ #if INCLUDE_gost_yescrypt
112
+ { "$gy$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
113
+ #else
114
+ { "$gy$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
115
+ #endif
116
+ #if INCLUDE_sm3_yescrypt
117
+ { "$sm3y$", CRYPT_SALT_OK, CRYPT_SALT_OK, CRYPT_SALT_OK },
118
+ #else
119
+ { "$sm3y$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
120
+ #endif
121
+
122
+ /* All of these are invalid. */
123
+ { "$@", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
124
+ { "%A", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
125
+ { "A%", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
126
+ { "$2$", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
127
+ { "*0", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
128
+ { "*1", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
129
+ { " ", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
130
+ { "!!", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
131
+ { "**", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
132
+ { "::", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
133
+ { ";;", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
134
+ { "\\\\", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
135
+ { "\x01\x01", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
136
+ { "\x19\x19", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
137
+ { "\x20\x20", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
138
+ { "\x7f\x7f", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
139
+ { "\xfe\xfe", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
140
+ { "\xff\xff", CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
141
+ #if defined HASH_ALGORITHM_DEFAULT
142
+ { NULL, CRYPT_SALT_INVALID, CRYPT_SALT_OK, CRYPT_SALT_OK },
143
+ #else
144
+ { NULL, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID, CRYPT_SALT_INVALID },
145
+ #endif
146
+ };
147
+
148
+ int
149
+ main (void)
150
+ {
151
+ char gs_out[CRYPT_GENSALT_OUTPUT_SIZE] = "";
152
+ const char *phr = "police saying freeze";
153
+ struct crypt_data cd;
154
+ const size_t gs_len = CRYPT_GENSALT_OUTPUT_SIZE;
155
+
156
+ int status = 0;
157
+ int retval = 0;
158
+
159
+ for (size_t i = 0; i < ARRAY_SIZE (testcases); i++)
160
+ {
161
+ /* crypt_checksalt on prefix. */
162
+ retval = crypt_checksalt (testcases[i].prefix);
163
+ if (retval == testcases[i].exp_prefix)
164
+ printf ("PASS (prefix): %s, result: %d\n",
165
+ testcases[i].prefix, retval);
166
+ else
167
+ {
168
+ status = 1;
169
+ printf ("FAIL (prefix): %s, expected: %d, got: %d\n",
170
+ testcases[i].prefix,
171
+ testcases[i].exp_prefix, retval);
172
+ continue;
173
+ }
174
+
175
+ /* crypt_checksalt on gensalt output. */
176
+ crypt_gensalt_rn (testcases[i].prefix, 0, NULL, 0,
177
+ gs_out, (int) gs_len);
178
+ retval = crypt_checksalt (gs_out);
179
+ if (retval == testcases[i].exp_gensalt)
180
+ printf ("PASS (gensalt): %s, result: %d\n",
181
+ gs_out, retval);
182
+ else
183
+ {
184
+ status = 1;
185
+ printf ("FAIL (gensalt): %s, expected: %d, got: %d\n",
186
+ gs_out, testcases[i].exp_gensalt, retval);
187
+ continue;
188
+ }
189
+
190
+ /* crypt_checksalt on crypt output. */
191
+ crypt_r (phr, gs_out, &cd);
192
+ retval = crypt_checksalt (cd.output);
193
+ if (retval == testcases[i].exp_crypt)
194
+ printf ("PASS (crypt): %s, result: %d\n",
195
+ cd.output, retval);
196
+ else
197
+ {
198
+ status = 1;
199
+ printf ("FAIL (crypt): %s, expected: %d, got: %d\n",
200
+ cd.output, testcases[i].exp_crypt, retval);
201
+ }
202
+
203
+ #if INCLUDE_descrypt && INCLUDE_bigcrypt
204
+
205
+ /* Test bigcrypt as well. */
206
+ if (testcases[i].prefix && strlen (testcases[i].prefix) == 2)
207
+ {
208
+ /* Prefix must be at least 14 bytes. */
209
+ char bigcrypt_prefix[CRYPT_GENSALT_OUTPUT_SIZE];
210
+ const char *pad = "............";
211
+ memcpy (bigcrypt_prefix, testcases[i].prefix, 2);
212
+ strncpy (bigcrypt_prefix + 2, pad, gs_len - 2);
213
+
214
+ /* crypt_checksalt on prefix. */
215
+ retval = crypt_checksalt (bigcrypt_prefix);
216
+ if (retval == testcases[i].exp_prefix)
217
+ printf ("PASS (prefix): %s, result: %d\n",
218
+ bigcrypt_prefix, retval);
219
+ else
220
+ {
221
+ status = 1;
222
+ printf ("FAIL (prefix): %s, expected: %d, got: %d\n",
223
+ bigcrypt_prefix,
224
+ testcases[i].exp_prefix, retval);
225
+ continue;
226
+ }
227
+
228
+ /* crypt_checksalt on gensalt output. */
229
+ crypt_gensalt_rn (bigcrypt_prefix, 0, NULL, 0,
230
+ gs_out, (int) gs_len);
231
+
232
+ /* Add 12 trailing bytes. */
233
+ strncpy (gs_out + 2, pad, gs_len - 2);
234
+
235
+ retval = crypt_checksalt (gs_out);
236
+ if (retval == testcases[i].exp_gensalt)
237
+ printf ("PASS (gensalt): %s, result: %d\n",
238
+ gs_out, retval);
239
+ else
240
+ {
241
+ status = 1;
242
+ printf ("FAIL (gensalt): %s, expected: %d, got: %d\n",
243
+ gs_out, testcases[i].exp_gensalt, retval);
244
+ continue;
245
+ }
246
+
247
+ /* crypt_checksalt on crypt output. */
248
+ crypt_r (phr, gs_out, &cd);
249
+ retval = crypt_checksalt (cd.output);
250
+ if (retval == testcases[i].exp_crypt)
251
+ printf ("PASS (crypt): %s, result: %d\n",
252
+ cd.output, retval);
253
+ else
254
+ {
255
+ status = 1;
256
+ printf ("FAIL (crypt): %s, expected: %d, got: %d\n",
257
+ cd.output, testcases[i].exp_crypt, retval);
258
+ }
259
+ }
260
+ #endif
261
+
262
+ }
263
+
264
+ return status;
265
+ }
@@ -0,0 +1,43 @@
1
+ /* Copyright (C) 2018 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
+ /* Simple compile test for our macro definition of strong_alias().
20
+ The sole purpose of this test is the fact some platforms do not
21
+ support strong aliases, some don't support aliases at all.
22
+ We test it just in case we may need this macro on those platforms
23
+ some time in the future. */
24
+
25
+ #include "crypt-port.h"
26
+
27
+ /* Prototype */
28
+ int addition (int, int);
29
+
30
+ int addition (int a, int b)
31
+ {
32
+ return a + b;
33
+ }
34
+ strong_alias (addition, add);
35
+
36
+ int
37
+ main (void)
38
+ {
39
+ int a = 1;
40
+ int b = -1;
41
+
42
+ return add (a, b);
43
+ }
@@ -0,0 +1,392 @@
1
+ /* Test passing invalid arguments to crypt*().
2
+
3
+ Written by Zack Weinberg <zackw at panix.com> in 2018.
4
+ To the extent possible under law, Zack Weinberg has waived all
5
+ copyright and related or neighboring rights to this work.
6
+
7
+ See https://creativecommons.org/publicdomain/zero/1.0/ for further
8
+ details. */
9
+
10
+ #include "crypt-port.h"
11
+
12
+ #include <errno.h>
13
+ #include <setjmp.h>
14
+ #include <stdio.h>
15
+ #include <string.h>
16
+ #include <signal.h>
17
+ #include <sys/mman.h>
18
+
19
+ /* The behavior tested below should be consistent for all hashing
20
+ methods. */
21
+ static const char *settings[] =
22
+ {
23
+ #if INCLUDE_descrypt || INCLUDE_bigcrypt
24
+ "Mp",
25
+ #endif
26
+ #if INCLUDE_bsdicrypt
27
+ "_J9..MJHn",
28
+ #endif
29
+ #if INCLUDE_md5crypt
30
+ "$1$MJHnaAke",
31
+ #endif
32
+ #if INCLUDE_nt
33
+ "$3$",
34
+ #endif
35
+ #if INCLUDE_sunmd5
36
+ /* exercise all paths of the bug-compatibility logic */
37
+ "$md5,rounds=55349$BPm.fm03$",
38
+ "$md5,rounds=55349$BPm.fm03$x",
39
+ "$md5,rounds=55349$BPm.fm03$$",
40
+ "$md5,rounds=55349$BPm.fm03$$x",
41
+ "$md5$BPm.fm03$",
42
+ "$md5$BPm.fm03$x",
43
+ "$md5$BPm.fm03$$",
44
+ "$md5$BPm.fm03$$x",
45
+ #endif
46
+ #if INCLUDE_sm3crypt
47
+ "$sm3$MJHnaAkegEVYHsFK",
48
+ "$sm3$rounds=10191$MJHnaAkegEVYHsFK",
49
+ #endif
50
+ #if INCLUDE_sha1crypt
51
+ "$sha1$248488$ggu.H673kaZ5$",
52
+ #endif
53
+ #if INCLUDE_sha256crypt
54
+ "$5$MJHnaAkegEVYHsFK",
55
+ "$5$rounds=10191$MJHnaAkegEVYHsFK",
56
+ #endif
57
+ #if INCLUDE_sha512crypt
58
+ "$6$MJHnaAkegEVYHsFK",
59
+ "$6$rounds=10191$MJHnaAkegEVYHsFK",
60
+ #endif
61
+ #if INCLUDE_bcrypt_a
62
+ "$2a$05$UBVLHeMpJ/QQCv3XqJx8zO",
63
+ #endif
64
+ #if INCLUDE_bcrypt
65
+ "$2b$05$UBVLHeMpJ/QQCv3XqJx8zO",
66
+ #endif
67
+ #if INCLUDE_bcrypt_x
68
+ "$2x$05$UBVLHeMpJ/QQCv3XqJx8zO",
69
+ #endif
70
+ #if INCLUDE_bcrypt_y
71
+ "$2y$05$UBVLHeMpJ/QQCv3XqJx8zO",
72
+ #endif
73
+ #if INCLUDE_yescrypt
74
+ "$y$j9T$MJHnaAkegEVYHsFKkmfzJ1",
75
+ #endif
76
+ #if INCLUDE_scrypt
77
+ "$7$CU..../....MJHnaAkegEVYHsFKkmfzJ1",
78
+ #endif
79
+ #if INCLUDE_gost_yescrypt
80
+ "$gy$j9T$MJHnaAkegEVYHsFKkmfzJ1",
81
+ #endif
82
+ #if INCLUDE_sm3_yescrypt
83
+ "$sm3y$j9T$MJHnaAkegEVYHsFKkmfzJ1",
84
+ #endif
85
+ };
86
+
87
+ /* In some of the tests below, a segmentation fault is the expected result. */
88
+ static sigjmp_buf env;
89
+ static void
90
+ segv_handler (int sig)
91
+ {
92
+ siglongjmp (env, sig);
93
+ }
94
+
95
+ static bool error_occurred;
96
+
97
+ #ifndef XCRYPT_USE_ASAN /* see comments in do_tests */
98
+ static void
99
+ expect_no_fault (const char *tag,
100
+ const char *phrase, const char *setting, const char *expect,
101
+ void (*testfn) (const char *, const char *,
102
+ const char *, const char *))
103
+ {
104
+ int rv = sigsetjmp (env, 1);
105
+ if (!rv)
106
+ testfn (tag, phrase, setting, expect);
107
+ else
108
+ {
109
+ printf ("FAIL: %s: Unexpected %s\n", tag, strsignal (rv));
110
+ error_occurred = 1;
111
+ }
112
+ }
113
+ #endif
114
+
115
+ static void
116
+ expect_a_fault (const char *tag,
117
+ const char *phrase, const char *setting, const char *expect,
118
+ void (*testfn) (const char *, const char *,
119
+ const char *, const char *))
120
+ {
121
+ int rv = sigsetjmp (env, 1);
122
+ if (!rv)
123
+ {
124
+ testfn (tag, phrase, setting, expect);
125
+ printf ("ERROR: %s: No signal occurred\n", tag);
126
+ error_occurred = true;
127
+ }
128
+ }
129
+
130
+ static void
131
+ check (const char *tag, const char *expect, const char *got)
132
+ {
133
+ int err = errno;
134
+ if ((got == 0 && expect != 0)
135
+ || (got != 0 && expect == 0)
136
+ || (got != 0 && expect != 0 && strcmp(got, expect) != 0))
137
+ {
138
+ printf ("FAIL: %s: exp '%s' got '%s'\n",
139
+ tag, expect ? expect : "(nil)",
140
+ got ? got : "(nil)");
141
+ error_occurred = true;
142
+ }
143
+ if ((expect == 0 || expect[0] == '*') && err != EINVAL)
144
+ {
145
+ printf ("FAIL: %s: exp '%s' got '%s'\n",
146
+ tag, strerror (EINVAL), strerror (err));
147
+ error_occurred = true;
148
+ }
149
+ }
150
+
151
+ static void
152
+ test_crypt (const char *tag,
153
+ const char *phrase, const char *setting, const char *expect)
154
+ {
155
+ char *got = crypt (phrase, setting);
156
+ check (tag, expect, got);
157
+ }
158
+
159
+ static void
160
+ test_crypt_r (const char *tag,
161
+ const char *phrase, const char *setting, const char *expect)
162
+ {
163
+ struct crypt_data data;
164
+ memset (&data, 0, sizeof data);
165
+ char *got = crypt_r (phrase, setting, &data);
166
+ check (tag, expect, got);
167
+ }
168
+
169
+ static void
170
+ test_crypt_rn (const char *tag,
171
+ const char *phrase, const char *setting, const char *expect)
172
+ {
173
+ struct crypt_data data;
174
+ memset (&data, 0, sizeof data);
175
+
176
+ char *got = crypt_rn (phrase, setting, &data, (int) sizeof data);
177
+ check (tag, expect, got);
178
+ }
179
+
180
+ static void
181
+ test_crypt_ra (const char *tag,
182
+ const char *phrase, const char *setting, const char *expect)
183
+ {
184
+ /* cheat - crypt_ra doesn't actually care whether its scratch area
185
+ is on the heap as long as it's big enough */
186
+ struct crypt_data data;
187
+ memset (&data, 0, sizeof data);
188
+ void *datap = &data;
189
+ int datas = (int) sizeof data;
190
+
191
+ char *got = crypt_ra (phrase, setting, &datap, &datas);
192
+ check (tag, expect, got);
193
+ }
194
+
195
+ #if ENABLE_FAILURE_TOKENS
196
+ # define FT0 "*0"
197
+ # define FT1 "*1"
198
+ #else
199
+ # define FT0 0
200
+ # define FT1 0
201
+ #endif
202
+
203
+ /* PAGE should point to PAGESIZE bytes of read-write memory followed
204
+ by another PAGESIZE bytes of inaccessible memory. */
205
+
206
+ static void
207
+ do_tests(char *page, size_t pagesize)
208
+ {
209
+ static const char phrase[] =
210
+ "the ritual question of how much is two plus two";
211
+
212
+ /* This copy operation intentionally omits the NUL; 'p1' points to a
213
+ sequence of nonzero bytes followed immediately by inaccessible
214
+ memory. */
215
+ memcpy (page + pagesize - (sizeof phrase - 1), phrase, sizeof phrase - 1);
216
+ const char *p1 = page + pagesize - (sizeof phrase - 1);
217
+ const char *p2 = page + pagesize;
218
+ size_t i;
219
+
220
+ /* Our crypt*() functions return NULL / a failure token, with errno set
221
+ to EINVAL, when either the setting or the phrase argument is NULL.
222
+ ASan's interceptors for crypt*() instead crash the program when either
223
+ argument is NULL -- this is arguably a better choice, but for
224
+ compatibility's sake we can't change what our functions do. There is
225
+ no way to disable interception of specific functions as far as I can
226
+ tell. Therefore, these tests are skipped when compiled with ASan. */
227
+ #ifndef XCRYPT_USE_ASAN
228
+ /* When SETTING is null, it shouldn't matter what PHRASE is. */
229
+ expect_no_fault ("0.0.crypt", 0, 0, FT0, test_crypt);
230
+ expect_no_fault ("0.0.crypt_r", 0, 0, FT0, test_crypt_r);
231
+ expect_no_fault ("0.0.crypt_rn", 0, 0, 0, test_crypt_rn);
232
+ expect_no_fault ("0.0.crypt_ra", 0, 0, 0, test_crypt_ra);
233
+
234
+ expect_no_fault ("''.0.crypt", "", 0, FT0, test_crypt);
235
+ expect_no_fault ("''.0.crypt_r", "", 0, FT0, test_crypt_r);
236
+ expect_no_fault ("''.0.crypt_rn", "", 0, 0, test_crypt_rn);
237
+ expect_no_fault ("''.0.crypt_ra", "", 0, 0, test_crypt_ra);
238
+
239
+ expect_no_fault ("ph.0.crypt", phrase, 0, FT0, test_crypt);
240
+ expect_no_fault ("ph.0.crypt_r", phrase, 0, FT0, test_crypt_r);
241
+ expect_no_fault ("ph.0.crypt_rn", phrase, 0, 0, test_crypt_rn);
242
+ expect_no_fault ("ph.0.crypt_ra", phrase, 0, 0, test_crypt_ra);
243
+
244
+ expect_no_fault ("p1.0.crypt", p1, 0, FT0, test_crypt);
245
+ expect_no_fault ("p1.0.crypt_r", p1, 0, FT0, test_crypt_r);
246
+ expect_no_fault ("p1.0.crypt_rn", p1, 0, 0, test_crypt_rn);
247
+ expect_no_fault ("p1.0.crypt_ra", p1, 0, 0, test_crypt_ra);
248
+
249
+ expect_no_fault ("p2.0.crypt", p2, 0, FT0, test_crypt);
250
+ expect_no_fault ("p2.0.crypt_r", p2, 0, FT0, test_crypt_r);
251
+ expect_no_fault ("p2.0.crypt_rn", p2, 0, 0, test_crypt_rn);
252
+ expect_no_fault ("p2.0.crypt_ra", p2, 0, 0, test_crypt_ra);
253
+
254
+ /* Conversely, when PHRASE is null,
255
+ it shouldn't matter what SETTING is... */
256
+ expect_no_fault ("0.''.crypt", 0, "", FT0, test_crypt);
257
+ expect_no_fault ("0.''.crypt_r", 0, "", FT0, test_crypt_r);
258
+ expect_no_fault ("0.''.crypt_rn", 0, "", 0, test_crypt_rn);
259
+ expect_no_fault ("0.''.crypt_ra", 0, "", 0, test_crypt_ra);
260
+
261
+ expect_no_fault ("0.'*'.crypt", 0, "*", FT0, test_crypt);
262
+ expect_no_fault ("0.'*'.crypt_r", 0, "*", FT0, test_crypt_r);
263
+ expect_no_fault ("0.'*'.crypt_rn", 0, "*", 0, test_crypt_rn);
264
+ expect_no_fault ("0.'*'.crypt_ra", 0, "*", 0, test_crypt_ra);
265
+
266
+ expect_no_fault ("0.'*0'.crypt", 0, "*0", FT1, test_crypt);
267
+ expect_no_fault ("0.'*0'.crypt_r", 0, "*0", FT1, test_crypt_r);
268
+ expect_no_fault ("0.'*0'.crypt_rn", 0, "*0", 0, test_crypt_rn);
269
+ expect_no_fault ("0.'*0'.crypt_ra", 0, "*0", 0, test_crypt_ra);
270
+
271
+ expect_no_fault ("0.'*1'.crypt", 0, "*1", FT0, test_crypt);
272
+ expect_no_fault ("0.'*1'.crypt_r", 0, "*1", FT0, test_crypt_r);
273
+ expect_no_fault ("0.'*1'.crypt_rn", 0, "*1", 0, test_crypt_rn);
274
+ expect_no_fault ("0.'*1'.crypt_ra", 0, "*1", 0, test_crypt_ra);
275
+
276
+ expect_no_fault ("0.p1.crypt", 0, p1, FT0, test_crypt);
277
+ expect_no_fault ("0.p1.crypt_r", 0, p1, FT0, test_crypt_r);
278
+ expect_no_fault ("0.p1.crypt_rn", 0, p1, 0, test_crypt_rn);
279
+ expect_no_fault ("0.p1.crypt_ra", 0, p1, 0, test_crypt_ra);
280
+
281
+ /* ... except for the case where SETTING is nonnull but there are
282
+ fewer than 2 readable characters at SETTING, in which case we'll
283
+ crash before we get to the null check in do_crypt. This is a
284
+ bug, but it's impractical to fix without breaking the property
285
+ that 'crypt' _never_ creates a failure token that is equal to the
286
+ setting string, which is more important than this corner case. */
287
+ expect_a_fault ("0.p2.crypt", 0, p2, FT0, test_crypt);
288
+ expect_a_fault ("0.p2.crypt_r", 0, p2, FT0, test_crypt_r);
289
+ expect_a_fault ("0.p2.crypt_rn", 0, p2, 0, test_crypt_rn);
290
+ expect_a_fault ("0.p2.crypt_ra", 0, p2, 0, test_crypt_ra);
291
+ #endif /* no ASan */
292
+
293
+ /* When SETTING is valid, passing an invalid string as PHRASE should
294
+ crash reliably. */
295
+ for (i = 0; i < ARRAY_SIZE (settings); i++)
296
+ {
297
+ snprintf (page, pagesize, "p1.'%s'.crypt", settings[i]);
298
+ expect_a_fault (page, p1, settings[i], FT0, test_crypt);
299
+
300
+ snprintf (page, pagesize, "p1.'%s'.crypt_r", settings[i]);
301
+ expect_a_fault (page, p1, settings[i], FT0, test_crypt_r);
302
+
303
+ snprintf (page, pagesize, "p1.'%s'.crypt_rn", settings[i]);
304
+ expect_a_fault (page, p1, settings[i], 0, test_crypt_rn);
305
+
306
+ snprintf (page, pagesize, "p1.'%s'.crypt_ra", settings[i]);
307
+ expect_a_fault (page, p1, settings[i], 0, test_crypt_ra);
308
+
309
+ snprintf (page, pagesize, "p2.'%s'.crypt", settings[i]);
310
+ expect_a_fault (page, p2, settings[i], FT0, test_crypt);
311
+
312
+ snprintf (page, pagesize, "p2.'%s'.crypt_r", settings[i]);
313
+ expect_a_fault (page, p2, settings[i], FT0, test_crypt_r);
314
+
315
+ snprintf (page, pagesize, "p2.'%s'.crypt_rn", settings[i]);
316
+ expect_a_fault (page, p2, settings[i], 0, test_crypt_rn);
317
+
318
+ snprintf (page, pagesize, "p2.'%s'.crypt_ra", settings[i]);
319
+ expect_a_fault (page, p2, settings[i], 0, test_crypt_ra);
320
+ }
321
+
322
+ /* Conversely, when PHRASE is valid, passing an invalid string as SETTING
323
+ should crash reliably. */
324
+ expect_a_fault ("ph.p2.crypt", phrase, p2, FT0, test_crypt);
325
+ expect_a_fault ("ph.p2.crypt_r", phrase, p2, FT0, test_crypt_r);
326
+ expect_a_fault ("ph.p2.crypt_rn", phrase, p2, 0, test_crypt_rn);
327
+ expect_a_fault ("ph.p2.crypt_ra", phrase, p2, 0, test_crypt_ra);
328
+
329
+ for (i = 0; i < ARRAY_SIZE (settings); i++)
330
+ {
331
+ p1 = memcpy (page + pagesize - strlen (settings[i]),
332
+ settings[i], strlen (settings[i]));
333
+
334
+ snprintf (page, pagesize, "ph.'%s'.crypt", settings[i]);
335
+ expect_a_fault (page, phrase, p1, FT0, test_crypt);
336
+
337
+ snprintf (page, pagesize, "ph.'%s'.crypt_r", settings[i]);
338
+ expect_a_fault (page, phrase, p1, FT0, test_crypt_r);
339
+
340
+ snprintf (page, pagesize, "ph.'%s'.crypt_rn", settings[i]);
341
+ expect_a_fault (page, phrase, p1, 0, test_crypt_rn);
342
+
343
+ snprintf (page, pagesize, "ph.'%s'.crypt_ra", settings[i]);
344
+ expect_a_fault (page, phrase, p1, 0, test_crypt_ra);
345
+ }
346
+ }
347
+
348
+ int
349
+ main (void)
350
+ {
351
+ /* Set up a two-page region whose first page is read-write and
352
+ whose second page is inaccessible. */
353
+ long pagesize_l = sysconf (_SC_PAGESIZE);
354
+ if (pagesize_l < 256)
355
+ {
356
+ printf ("ERROR: pagesize of %ld is too small\n", pagesize_l);
357
+ return 99;
358
+ }
359
+
360
+ size_t pagesize = (size_t) pagesize_l;
361
+ char *page = mmap (0, pagesize * 2, PROT_READ|PROT_WRITE,
362
+ MAP_PRIVATE|MAP_ANON, -1, 0);
363
+ if (page == MAP_FAILED)
364
+ {
365
+ perror ("mmap");
366
+ return 99;
367
+ }
368
+ // coverity[overflow_sink]
369
+ memset (page, 'x', pagesize * 2);
370
+ if (mprotect (page + pagesize, pagesize, PROT_NONE))
371
+ {
372
+ perror ("mprotect");
373
+ return 99;
374
+ }
375
+
376
+ struct sigaction sa, os, ob;
377
+ sigfillset (&sa.sa_mask);
378
+ sa.sa_flags = SA_RESTART;
379
+ sa.sa_handler = segv_handler;
380
+ if (sigaction (SIGBUS, &sa, &ob) || sigaction (SIGSEGV, &sa, &os))
381
+ {
382
+ perror ("sigaction");
383
+ return 1;
384
+ }
385
+
386
+ do_tests (page, pagesize);
387
+
388
+ sigaction (SIGBUS, &ob, 0);
389
+ sigaction (SIGSEGV, &os, 0);
390
+
391
+ return error_occurred;
392
+ }