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,177 @@
1
+ /* Copyright (C) 2018 vt@altlinux.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 "alg-yescrypt.h"
21
+
22
+ #include <errno.h>
23
+
24
+ #if INCLUDE_yescrypt || INCLUDE_scrypt
25
+
26
+ /* For use in scratch space by crypt_yescrypt_rn(). */
27
+ typedef struct
28
+ {
29
+ yescrypt_local_t local;
30
+ uint8_t outbuf[CRYPT_OUTPUT_SIZE];
31
+ uint8_t *retval;
32
+ } crypt_yescrypt_internal_t;
33
+
34
+ static_assert (sizeof (crypt_yescrypt_internal_t) <= ALG_SPECIFIC_SIZE,
35
+ "ALG_SPECIFIC_SIZE is too small for YESCRYPT.");
36
+
37
+ void
38
+ crypt_yescrypt_rn (const char *phrase, size_t phr_size,
39
+ const char *setting, size_t set_size,
40
+ uint8_t *output, size_t o_size,
41
+ void *scratch, size_t s_size)
42
+ {
43
+ #if !INCLUDE_scrypt
44
+
45
+ /* If scrypt is disabled fail when called with its prefix. */
46
+ if (!strncmp (setting, "$7$", 3))
47
+ {
48
+ errno = EINVAL;
49
+ return;
50
+ }
51
+
52
+ #endif /* !INCLUDE_scrypt */
53
+
54
+ #if !INCLUDE_yescrypt
55
+
56
+ /* If yescrypt is disabled fail when called with its prefix. */
57
+ if (!strncmp (setting, "$y$", 3))
58
+ {
59
+ errno = EINVAL;
60
+ return;
61
+ }
62
+
63
+ #endif /* !INCLUDE_yescrypt */
64
+
65
+ if (o_size < set_size + 1 + 43 + 1 ||
66
+ CRYPT_OUTPUT_SIZE < set_size + 1 + 43 + 1 ||
67
+ s_size < sizeof (crypt_yescrypt_internal_t))
68
+ {
69
+ errno = ERANGE;
70
+ return;
71
+ }
72
+
73
+ crypt_yescrypt_internal_t *intbuf = scratch;
74
+
75
+ if (yescrypt_init_local (&intbuf->local))
76
+ return;
77
+
78
+ intbuf->retval = yescrypt_r (NULL, &intbuf->local,
79
+ (const uint8_t *)phrase, phr_size,
80
+ (const uint8_t *)setting, NULL,
81
+ intbuf->outbuf, o_size);
82
+
83
+ if (!intbuf->retval)
84
+ errno = EINVAL;
85
+
86
+ if (yescrypt_free_local (&intbuf->local) || !intbuf->retval)
87
+ return;
88
+
89
+ strcpy_or_abort (output, o_size, intbuf->outbuf);
90
+ return;
91
+ }
92
+
93
+ #endif /* INCLUDE_yescrypt || INCLUDE_scrypt */
94
+
95
+ #if INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_sm3_yescrypt
96
+
97
+ /*
98
+ * As OUTPUT is initialized with a failure token before gensalt_yescrypt_rn
99
+ * is called, in case of an error we could just set an appropriate errno
100
+ * and return.
101
+ * Since O_SIZE is guaranteed to be greater than 2, we may fill OUTPUT
102
+ * with a short failure token when need.
103
+ */
104
+ void
105
+ gensalt_yescrypt_rn (unsigned long count,
106
+ const uint8_t *rbytes, size_t nrbytes,
107
+ uint8_t *output, size_t o_size)
108
+ {
109
+ /* Up to 512 bits (64 bytes) of entropy for computing the salt portion
110
+ of the MCF-setting are supported. */
111
+ nrbytes = (nrbytes > 64 ? 64 : nrbytes);
112
+
113
+ if (o_size < 3 + 8 * 6 + 1 + BASE64_LEN (nrbytes) + 1 ||
114
+ CRYPT_GENSALT_OUTPUT_SIZE < 3 + 8 * 6 + 1 + BASE64_LEN (nrbytes) + 1)
115
+ {
116
+ errno = ERANGE;
117
+ return;
118
+ }
119
+
120
+ if (count > 11 || nrbytes < 16)
121
+ {
122
+ errno = EINVAL;
123
+ return;
124
+ }
125
+
126
+ /* Temporary buffer for operation. The buffer is guaranteed to be
127
+ large enough to hold the maximum size of the generated salt. */
128
+ uint8_t outbuf[CRYPT_GENSALT_OUTPUT_SIZE];
129
+
130
+ yescrypt_params_t params =
131
+ {
132
+ .flags = YESCRYPT_DEFAULTS,
133
+ .p = 1,
134
+ };
135
+
136
+ /* Valid cost parameters are from 1 to 11. The default is 5.
137
+ These are used to set yescrypt's 'N' and 'r' parameters as
138
+ follows:
139
+ N (block count) is specified in units of r (block size,
140
+ adjustable in steps of 128 bytes).
141
+
142
+ 128 bytes * r = size of each memory block
143
+
144
+ 128 bytes * r * N = total amount of memory used for hashing
145
+ in N blocks of r * 128 bytes.
146
+
147
+ The author of yescrypt recommends in the documentation to use
148
+ r=8 (a block size of 1 KiB) for total sizes of 2 MiB and less,
149
+ and r=32 (a block size of 4KiB) above that.
150
+ This has to do with the typical per-core last-level cache sizes
151
+ of current CPUs. */
152
+
153
+ if (count == 0)
154
+ count = 5;
155
+
156
+ if (count < 3)
157
+ {
158
+ params.r = 8; // N in 1KiB
159
+ params.N = 1ULL << (count + 9); // 1 -> 1024, 2 -> 2048
160
+ }
161
+ else
162
+ {
163
+ params.r = 32; // N in 4KiB
164
+ params.N = 1ULL << (count + 7); // 3 -> 1024, 4 -> 2048, ... 11 -> 262144
165
+ }
166
+
167
+ if (!yescrypt_encode_params_r (&params, rbytes, nrbytes, outbuf, o_size))
168
+ {
169
+ errno = ERANGE;
170
+ return;
171
+ }
172
+
173
+ strcpy_or_abort (output, o_size, outbuf);
174
+ return;
175
+ }
176
+
177
+ #endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_sm3_yescrypt */
@@ -0,0 +1,421 @@
1
+ /* High-level libcrypt interfaces.
2
+
3
+ Copyright 2007-2017 Thorsten Kukuk and Zack Weinberg
4
+ Copyright 2018-2025 Björn Esser
5
+
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public License
8
+ as published by the Free Software Foundation; either version 2.1 of
9
+ the License, or (at your option) any later version.
10
+
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, see
18
+ <https://www.gnu.org/licenses/>. */
19
+
20
+ #include "crypt-port.h"
21
+
22
+ #include <errno.h>
23
+ #include <stdlib.h>
24
+
25
+ /* The internal storage area within struct crypt_data is used as
26
+ follows. We don't know what alignment the algorithm modules will
27
+ need for their scratch data, so give it the maximum natural
28
+ alignment. Note that the C11 alignas() specifier can't be applied
29
+ directly to a struct type, but it can be applied to the first field
30
+ of a struct, which effectively forces alignment of the entire
31
+ struct, since the first field must always have offset 0. */
32
+ struct crypt_internal
33
+ {
34
+ /* Alignment critical data members. */
35
+ char alignas (alignof (max_align_t)) alg_specific[ALG_SPECIFIC_SIZE];
36
+ /* Not alignment critical data members. */
37
+ char output[CRYPT_OUTPUT_SIZE];
38
+ };
39
+
40
+ static_assert(sizeof (struct crypt_internal) + alignof (struct crypt_internal)
41
+ <= CRYPT_DATA_INTERNAL_SIZE,
42
+ "crypt_data.internal is too small for crypt_internal");
43
+
44
+ /* struct crypt_data is allocated by application code and contains
45
+ only char-typed fields, so its 'internal' field may not be
46
+ sufficiently aligned. */
47
+ static inline struct crypt_internal *
48
+ get_internal (struct crypt_data *data)
49
+ {
50
+ uintptr_t internalp = (uintptr_t) data->internal;
51
+ const uintptr_t align = alignof (struct crypt_internal);
52
+ internalp = (internalp + align - 1) & ~(align - 1);
53
+ return (struct crypt_internal *)internalp;
54
+ }
55
+
56
+ typedef void (*crypt_fn) (const char *phrase, size_t phr_size,
57
+ const char *setting, size_t set_size,
58
+ uint8_t *output, size_t out_size,
59
+ void *scratch, size_t scr_size);
60
+
61
+ typedef void (*gensalt_fn) (unsigned long count,
62
+ const uint8_t *rbytes, size_t nrbytes,
63
+ uint8_t *output, size_t output_size);
64
+
65
+ struct hashfn
66
+ {
67
+ const char *prefix;
68
+ size_t plen;
69
+ crypt_fn crypt;
70
+ gensalt_fn gensalt;
71
+ /* The type of this field is unsigned char to ensure that it cannot
72
+ be set larger than the size of an internal buffer in crypt_gensalt_rn. */
73
+ unsigned char nrbytes;
74
+ unsigned char is_strong;
75
+ };
76
+
77
+ static const struct hashfn hash_algorithms[] =
78
+ {
79
+ HASH_ALGORITHM_TABLE_ENTRIES
80
+ };
81
+
82
+ #if INCLUDE_descrypt || INCLUDE_bigcrypt
83
+ static int
84
+ is_des_salt_char (char c)
85
+ {
86
+ return ((c >= 'a' && c <= 'z') ||
87
+ (c >= 'A' && c <= 'Z') ||
88
+ (c >= '0' && c <= '9') ||
89
+ c == '.' || c == '/');
90
+ }
91
+ #endif
92
+
93
+ static const struct hashfn *
94
+ get_hashfn (const char *setting)
95
+ {
96
+ const struct hashfn *h;
97
+ for (h = hash_algorithms; h->prefix; h++)
98
+ {
99
+ if (h->plen > 0)
100
+ {
101
+ if (!strncmp (setting, h->prefix, h->plen))
102
+ return h;
103
+ }
104
+ #if INCLUDE_descrypt || INCLUDE_bigcrypt
105
+ else
106
+ {
107
+ if (setting[0] == '\0' ||
108
+ (is_des_salt_char (setting[0]) && is_des_salt_char (setting[1])))
109
+ return h;
110
+ }
111
+ #endif
112
+ }
113
+ return 0;
114
+ }
115
+
116
+ /* Check a setting string for generic validity, according to the rule
117
+ stated in crypt(5):
118
+
119
+ "Hashed passphrases are always entirely printable ASCII, and do
120
+ not contain any whitespace or the characters ':', ';', '*', '!',
121
+ or '\\'. (These characters are used as delimiters and special
122
+ markers in the passwd(5) and shadow(5) files.)"
123
+
124
+ There is a precautionary case for rejecting additional ASCII
125
+ punctuation, particularly other characters often given syntactic
126
+ significance in configuration files, such as " ' and #. However,
127
+ this check didn't used to exist at all, and some of the hash
128
+ function implementations don't restrict the set of byte values they
129
+ they will accept in their setting strings (particularly in the salt
130
+ component) either. Thus, to maintain compatibility with the widest
131
+ variety of existing hashed passphrases, we are only enforcing the
132
+ documented rule for now.
133
+
134
+ See <https://github.com/besser82/libxcrypt/issues/135> for
135
+ additional discussion. */
136
+ static int
137
+ check_badsalt_chars (const char *setting)
138
+ {
139
+ size_t i;
140
+
141
+ for (i = 0; setting[i] != '\0'; i++)
142
+ if ((unsigned char) setting[i] <= 0x20 ||
143
+ (unsigned char) setting[i] >= 0x7f)
144
+ return 1;
145
+
146
+ return strcspn (setting, "!*:;\\") != i;
147
+ }
148
+
149
+ static void
150
+ do_crypt (const char *phrase, const char *setting, struct crypt_data *data)
151
+ {
152
+ struct crypt_internal *cint = get_internal (data);
153
+ memset (cint->output, 0, sizeof cint->output);
154
+ make_failure_token (setting, cint->output, sizeof cint->output);
155
+
156
+ if (!phrase || !setting)
157
+ {
158
+ errno = EINVAL;
159
+ goto out;
160
+ }
161
+ /* Do these strlen() calls before reading prefixes of either
162
+ 'phrase' or 'setting', so we get a predictable crash if they are
163
+ not valid strings. */
164
+ size_t phr_size = strlen (phrase);
165
+ size_t set_size = strlen (setting);
166
+ if (phr_size >= CRYPT_MAX_PASSPHRASE_SIZE)
167
+ {
168
+ errno = ERANGE;
169
+ goto out;
170
+ }
171
+ if (check_badsalt_chars (setting))
172
+ {
173
+ errno = EINVAL;
174
+ goto out;
175
+ }
176
+
177
+ const struct hashfn *h = get_hashfn (setting);
178
+ if (!h)
179
+ {
180
+ /* Unrecognized hash algorithm */
181
+ errno = EINVAL;
182
+ goto out;
183
+ }
184
+
185
+ h->crypt (phrase, phr_size, setting, set_size,
186
+ (unsigned char *) cint->output, sizeof cint->output,
187
+ cint->alg_specific, sizeof cint->alg_specific);
188
+
189
+ out:
190
+ strcpy_or_abort (data->output, sizeof data->output, cint->output);
191
+ explicit_bzero (data->internal, sizeof data->internal);
192
+ explicit_bzero (data->reserved, sizeof data->reserved);
193
+ data->initialized = 0;
194
+ }
195
+
196
+ #if INCLUDE_crypt_rn
197
+ char *
198
+ crypt_rn (const char *phrase, const char *setting, void *data, int size)
199
+ {
200
+ if (size < (int) sizeof (struct crypt_data))
201
+ {
202
+ errno = ERANGE;
203
+ make_failure_token (setting, data, size);
204
+ return 0;
205
+ }
206
+
207
+ struct crypt_data *p = data;
208
+ do_crypt (phrase, setting, p);
209
+ return p->output[0] == '*' ? 0 : p->output;
210
+ }
211
+ SYMVER_crypt_rn;
212
+ #endif
213
+
214
+ #if INCLUDE_crypt_ra
215
+ char *
216
+ crypt_ra (const char *phrase, const char *setting, void **data, int *size)
217
+ {
218
+ struct crypt_data *p = NULL;
219
+
220
+ /* Short-circuit, if possible. */
221
+ if (*data && *size >= (int) sizeof (struct crypt_data))
222
+ {
223
+ p = *data;
224
+ do_crypt (phrase, setting, p);
225
+ return p->output[0] == '*' ? 0 : p->output;
226
+ }
227
+
228
+ /* Allocate new memory for struct crypt_data. */
229
+ p = malloc (sizeof (struct crypt_data));
230
+ if (!p)
231
+ {
232
+ /* ERRNO is set by malloc. */
233
+ if (*data)
234
+ make_failure_token (setting, *data, *size);
235
+ return 0;
236
+ }
237
+ memset (p, 0, sizeof (struct crypt_data));
238
+
239
+ do_crypt (phrase, setting, p);
240
+
241
+ /* Zeroize memory, if needed, before assigning
242
+ the new memory location to the data pointer. */
243
+ if (*data && *size > 0)
244
+ explicit_bzero (*data, (size_t) *size);
245
+ free (*data);
246
+ *data = p;
247
+ *size = sizeof (struct crypt_data);
248
+
249
+ return p->output[0] == '*' ? 0 : p->output;
250
+ }
251
+ SYMVER_crypt_ra;
252
+ #endif
253
+
254
+ #if INCLUDE_crypt_r
255
+ char *
256
+ crypt_r (const char *phrase, const char *setting, struct crypt_data *data)
257
+ {
258
+ do_crypt (phrase, setting, data);
259
+ #if ENABLE_FAILURE_TOKENS
260
+ return data->output;
261
+ #else
262
+ return data->output[0] == '*' ? 0 : data->output;
263
+ #endif
264
+ }
265
+ SYMVER_crypt_r;
266
+ #endif
267
+
268
+ /* For code compatibility with older versions (v3.1.1 and earlier). */
269
+ #if INCLUDE_crypt_r && INCLUDE_xcrypt_r
270
+ strong_alias (crypt_r, xcrypt_r);
271
+ SYMVER_xcrypt_r;
272
+ #endif
273
+
274
+ #if INCLUDE_crypt_gensalt_rn
275
+ char *
276
+ crypt_gensalt_rn (const char *prefix, unsigned long count,
277
+ const char *rbytes, int nrbytes, char *output,
278
+ int output_size)
279
+ {
280
+ /* Individual gensalt functions will check for adequate space for
281
+ their own breed of setting, but the shortest possible one is
282
+ three bytes (DES two-character salt + NUL terminator) and we
283
+ also want to rule out negative numbers early. */
284
+ if (output_size < 3)
285
+ {
286
+ errno = ERANGE;
287
+ make_failure_token (prefix, output, output_size);
288
+ return 0;
289
+ }
290
+
291
+ char outbuf[CRYPT_GENSALT_OUTPUT_SIZE];
292
+ unsigned char internal_nrbytes = 0;
293
+ memset (outbuf, 0, sizeof outbuf);
294
+ make_failure_token (prefix, outbuf, sizeof outbuf);
295
+
296
+ /* If the prefix is 0, that means to use the current best default.
297
+ Note that this is different from the behavior when the prefix is
298
+ "", which selects DES. HASH_ALGORITHM_DEFAULT is not defined when
299
+ the current default algorithm was disabled at configure time. */
300
+ if (!prefix)
301
+ {
302
+ #if defined HASH_ALGORITHM_DEFAULT
303
+ prefix = HASH_ALGORITHM_DEFAULT;
304
+ #else
305
+ errno = EINVAL;
306
+ goto out;
307
+ #endif
308
+ }
309
+
310
+ const struct hashfn *h = get_hashfn (prefix);
311
+ if (!h)
312
+ {
313
+ errno = EINVAL;
314
+ goto out;
315
+ }
316
+
317
+ char internal_rbytes[UCHAR_MAX] = "\0";
318
+ /* typeof (internal_nrbytes) == typeof (h->nrbytes). */
319
+
320
+ /* If rbytes is 0, read random bytes from the operating system if
321
+ possible. */
322
+ if (!rbytes)
323
+ {
324
+ if (!get_random_bytes (internal_rbytes, h->nrbytes))
325
+ goto out;
326
+
327
+ rbytes = internal_rbytes;
328
+ nrbytes = internal_nrbytes = h->nrbytes;
329
+ }
330
+
331
+ h->gensalt (count,
332
+ (const unsigned char *) rbytes, (size_t) nrbytes,
333
+ (unsigned char *) outbuf,
334
+ MIN ((size_t) output_size, sizeof outbuf));
335
+
336
+ out:
337
+ strcpy_or_abort (output, (size_t) output_size, outbuf);
338
+ explicit_bzero (outbuf, sizeof outbuf);
339
+
340
+ if (internal_nrbytes)
341
+ explicit_bzero (internal_rbytes, internal_nrbytes);
342
+
343
+ return output[0] == '*' ? 0 : output;
344
+ }
345
+ SYMVER_crypt_gensalt_rn;
346
+ #endif
347
+
348
+ /* For code compatibility with older versions (v3.1.1 and earlier). */
349
+ #if INCLUDE_crypt_gensalt_rn && INCLUDE_crypt_gensalt_r
350
+ strong_alias (crypt_gensalt_rn, crypt_gensalt_r);
351
+ SYMVER_crypt_gensalt_r;
352
+ #endif
353
+
354
+ /* For code compatibility with older versions (v3.1.1 and earlier). */
355
+ #if INCLUDE_crypt_gensalt_rn && INCLUDE_xcrypt_gensalt_r
356
+ strong_alias (crypt_gensalt_rn, xcrypt_gensalt_r);
357
+ SYMVER_xcrypt_gensalt_r;
358
+ #endif
359
+
360
+ #if INCLUDE_crypt_gensalt_ra
361
+ char *
362
+ crypt_gensalt_ra (const char *prefix, unsigned long count,
363
+ const char *rbytes, int nrbytes)
364
+ {
365
+ char *output = malloc (CRYPT_GENSALT_OUTPUT_SIZE);
366
+ if (!output)
367
+ return 0;
368
+
369
+ char *result = crypt_gensalt_rn (prefix, count, rbytes, nrbytes, output,
370
+ CRYPT_GENSALT_OUTPUT_SIZE);
371
+ if (result == 0)
372
+ free (output);
373
+ return result;
374
+ }
375
+ SYMVER_crypt_gensalt_ra;
376
+ #endif
377
+
378
+ #if INCLUDE_crypt_checksalt
379
+ static_assert(CRYPT_SALT_OK == 0, "CRYPT_SALT_OK does not equal zero");
380
+
381
+ int
382
+ crypt_checksalt (const char *setting)
383
+ {
384
+ int retval = CRYPT_SALT_INVALID;
385
+
386
+ if (!setting || /* NULL string */
387
+ setting[0] == '\0' || /* empty passphrase */
388
+ check_badsalt_chars (setting)) /* bad salt chars */
389
+ goto end;
390
+
391
+ const struct hashfn *h = get_hashfn (setting);
392
+
393
+ if (h)
394
+ {
395
+ retval = CRYPT_SALT_OK;
396
+
397
+ if (h->is_strong == 0)
398
+ {
399
+ retval = CRYPT_SALT_METHOD_LEGACY;
400
+ goto end;
401
+ }
402
+ }
403
+
404
+ end:
405
+ return retval;
406
+ }
407
+ SYMVER_crypt_checksalt;
408
+ #endif
409
+
410
+ #if INCLUDE_crypt_preferred_method
411
+ const char *
412
+ crypt_preferred_method (void)
413
+ {
414
+ #if defined HASH_ALGORITHM_DEFAULT
415
+ return HASH_ALGORITHM_DEFAULT;
416
+ #else
417
+ return NULL;
418
+ #endif
419
+ }
420
+ SYMVER_crypt_preferred_method;
421
+ #endif