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,215 @@
1
+ /*
2
+ * FreeSec: libcrypt for NetBSD
3
+ *
4
+ * Copyright (c) 1994 David Burren
5
+ * All rights reserved.
6
+ *
7
+ * Adapted for FreeBSD-2.0 by Geoffrey M. Rehmet
8
+ * this file should now *only* export crypt(), in order to make
9
+ * binaries of libcrypt exportable from the USA
10
+ *
11
+ * Adapted for FreeBSD-4.0 by Mark R V Murray
12
+ * this file should now *only* export crypt_des(), in order to make
13
+ * a module that can be optionally included in libcrypt.
14
+ *
15
+ * Adapted for libxcrypt by Zack Weinberg, 2017
16
+ * see notes in des.c
17
+ *
18
+ * Adapted for libxcrypt by Björn Esser, 2019
19
+ * add function-stubs simply setting error to ENOSYS.
20
+ *
21
+ * Redistribution and use in source and binary forms, with or without
22
+ * modification, are permitted provided that the following conditions
23
+ * are met:
24
+ * 1. Redistributions of source code must retain the above copyright
25
+ * notice, this list of conditions and the following disclaimer.
26
+ * 2. Redistributions in binary form must reproduce the above copyright
27
+ * notice, this list of conditions and the following disclaimer in the
28
+ * documentation and/or other materials provided with the distribution.
29
+ * 3. Neither the name of the author nor the names of other contributors
30
+ * may be used to endorse or promote products derived from this software
31
+ * without specific prior written permission.
32
+ *
33
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43
+ * SUCH DAMAGE.
44
+ *
45
+ * This is an original implementation of the DES and the crypt(3) interfaces
46
+ * by David Burren <davidb@werj.com.au>.
47
+ */
48
+
49
+ /* Obsolete DES symmetric cipher API - not to be used in new code. */
50
+
51
+ #include "crypt-port.h"
52
+ #include "crypt-obsolete.h"
53
+ #include "alg-des.h"
54
+ #include <errno.h>
55
+
56
+ #if (INCLUDE_encrypt || INCLUDE_encrypt_r || INCLUDE_setkey || INCLUDE_setkey_r) && \
57
+ !ENABLE_OBSOLETE_API_ENOSYS
58
+
59
+ static_assert(sizeof (struct des_ctx) + alignof (struct des_ctx)
60
+ <= CRYPT_DATA_INTERNAL_SIZE,
61
+ "crypt_data.internal is too small for struct des_ctx");
62
+
63
+ /* struct crypt_data is allocated by application code and contains
64
+ only char-typed fields, so its 'internal' field may not be
65
+ sufficiently aligned. */
66
+ static inline struct des_ctx *
67
+ get_des_ctx (struct crypt_data *data)
68
+ {
69
+ uintptr_t internalp = (uintptr_t) data->internal;
70
+ const uintptr_t align = alignof (struct des_ctx);
71
+ internalp = (internalp + align - 1) & ~(align - 1);
72
+ return (struct des_ctx *)internalp;
73
+ }
74
+
75
+ /* For reasons lost in the mists of time, these functions operate on
76
+ 64-*byte* arrays, each of which should be either 0 or 1 - only the
77
+ low bit of each byte is examined. The DES primitives, much more
78
+ sensibly, operate on 8-byte/64-*bit* arrays. */
79
+
80
+ static void
81
+ unpack_bits (char bytev[64], const unsigned char bitv[8])
82
+ {
83
+ unsigned char c;
84
+ for (int i = 0; i < 8; i++)
85
+ {
86
+ c = bitv[i];
87
+ for (int j = 0; j < 8; j++)
88
+ bytev[i*8 + j] = (char)((c & (0x01 << (7 - j))) != 0);
89
+ }
90
+ }
91
+
92
+ static void
93
+ pack_bits (unsigned char bitv[8], const char bytev[64])
94
+ {
95
+ unsigned int c;
96
+ for (int i = 0; i < 8; i++)
97
+ {
98
+ c = 0;
99
+ for (int j = 0; j < 8; j++)
100
+ {
101
+ c <<= 1;
102
+ c |= ((unsigned char)bytev[i*8 + j] & 0x01u);
103
+ }
104
+ bitv[i] = (unsigned char)c;
105
+ }
106
+ }
107
+ #endif
108
+
109
+ /* Initialize DATA with a DES key, KEY, represented as a byte vector. */
110
+ #if (INCLUDE_setkey_r || INCLUDE_setkey) && !ENABLE_OBSOLETE_API_ENOSYS
111
+ static void
112
+ do_setkey_r (const char *key, struct des_ctx *ctx)
113
+ {
114
+ memset (ctx, 0, sizeof (struct des_ctx));
115
+ des_set_salt (ctx, 0);
116
+
117
+ unsigned char bkey[8];
118
+ pack_bits (bkey, key);
119
+ des_set_key (ctx, bkey);
120
+ }
121
+ #endif
122
+
123
+ #if INCLUDE_setkey_r
124
+ void
125
+ setkey_r (ARG_UNUSED (const char *key), ARG_UNUSED (struct crypt_data *data))
126
+ {
127
+ #if ENABLE_OBSOLETE_API_ENOSYS
128
+ /* This function is not supported in this configuration. */
129
+ errno = ENOSYS;
130
+ #else
131
+ do_setkey_r (key, get_des_ctx (data));
132
+ #endif
133
+ }
134
+ SYMVER_setkey_r;
135
+ #endif
136
+
137
+ /* Encrypt or decrypt one DES block, BLOCK, using the key schedule in
138
+ DATA. BLOCK is processed in place. */
139
+ #if (INCLUDE_encrypt_r || INCLUDE_encrypt) && !ENABLE_OBSOLETE_API_ENOSYS
140
+ static void
141
+ do_encrypt_r (char *block, int edflag, struct des_ctx *ctx)
142
+ {
143
+ unsigned char bin[8], bout[8];
144
+ pack_bits (bin, block);
145
+ des_crypt_block (ctx, bout, bin, 1, edflag != 0);
146
+ unpack_bits (block, bout);
147
+ }
148
+ #endif
149
+
150
+ #if INCLUDE_encrypt_r
151
+ void
152
+ encrypt_r (char *block, ARG_UNUSED (int edflag),
153
+ ARG_UNUSED (struct crypt_data *data))
154
+ {
155
+ #if ENABLE_OBSOLETE_API_ENOSYS
156
+ /* Make sure sensitive data is erased in case
157
+ case get_random_bytes() fails. */
158
+ explicit_bzero(block, 64);
159
+
160
+ /* Overwrite sensitive data with random data. */
161
+ get_random_bytes(block, 64);
162
+
163
+ /* This function is not supported in this configuration. */
164
+ errno = ENOSYS;
165
+ #else
166
+ do_encrypt_r (block, edflag, get_des_ctx (data));
167
+ #endif
168
+ }
169
+ SYMVER_encrypt_r;
170
+ #endif
171
+
172
+ /* Even-more-deprecated-than-the-above nonreentrant versions.
173
+ These use a separate state object from the main library's
174
+ nonreentrant crypt(). Unlike with crypt() vs crypt_r(),
175
+ these do not get their own file because they're not compiled
176
+ into the static library anyway. */
177
+
178
+ #if (INCLUDE_setkey || INCLUDE_encrypt) && !ENABLE_OBSOLETE_API_ENOSYS
179
+ static struct des_ctx nr_encrypt_ctx;
180
+ #endif
181
+
182
+ #if INCLUDE_setkey
183
+ void
184
+ setkey (ARG_UNUSED (const char *key))
185
+ {
186
+ #if ENABLE_OBSOLETE_API_ENOSYS
187
+ /* This function is not supported in this configuration. */
188
+ errno = ENOSYS;
189
+ #else
190
+ do_setkey_r (key, &nr_encrypt_ctx);
191
+ #endif
192
+ }
193
+ SYMVER_setkey;
194
+ #endif
195
+
196
+ #if INCLUDE_encrypt
197
+ void
198
+ encrypt (char *block, ARG_UNUSED (int edflag))
199
+ {
200
+ #if ENABLE_OBSOLETE_API_ENOSYS
201
+ /* Make sure sensitive data is erased in case
202
+ case get_random_bytes() fails. */
203
+ explicit_bzero(block, 64);
204
+
205
+ /* Overwrite sensitive data with random data. */
206
+ get_random_bytes(block, 64);
207
+
208
+ /* This function is not supported in this configuration. */
209
+ errno = ENOSYS;
210
+ #else
211
+ do_encrypt_r (block, edflag, &nr_encrypt_ctx);
212
+ #endif
213
+ }
214
+ SYMVER_encrypt;
215
+ #endif
@@ -0,0 +1,491 @@
1
+ /*
2
+ * FreeSec: libcrypt for NetBSD
3
+ *
4
+ * Copyright (c) 1994 David Burren
5
+ * All rights reserved.
6
+ *
7
+ * Adapted for FreeBSD-2.0 by Geoffrey M. Rehmet
8
+ * this file should now *only* export crypt(), in order to make
9
+ * binaries of libcrypt exportable from the USA
10
+ *
11
+ * Adapted for FreeBSD-4.0 by Mark R V Murray
12
+ * this file should now *only* export crypt_des(), in order to make
13
+ * a module that can be optionally included in libcrypt.
14
+ *
15
+ * Adapted for libxcrypt by Zack Weinberg, 2017
16
+ * see notes in des.c
17
+ *
18
+ * Redistribution and use in source and binary forms, with or without
19
+ * modification, are permitted provided that the following conditions
20
+ * are met:
21
+ * 1. Redistributions of source code must retain the above copyright
22
+ * notice, this list of conditions and the following disclaimer.
23
+ * 2. Redistributions in binary form must reproduce the above copyright
24
+ * notice, this list of conditions and the following disclaimer in the
25
+ * documentation and/or other materials provided with the distribution.
26
+ * 3. Neither the name of the author nor the names of other contributors
27
+ * may be used to endorse or promote products derived from this software
28
+ * without specific prior written permission.
29
+ *
30
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
+ * SUCH DAMAGE.
41
+ *
42
+ * This is an original implementation of the DES and the crypt(3) interfaces
43
+ * by David Burren <davidb@werj.com.au>.
44
+ */
45
+
46
+ #include "crypt-port.h"
47
+ #include "alg-des.h"
48
+
49
+ #include <errno.h>
50
+
51
+ #if INCLUDE_descrypt || INCLUDE_bsdicrypt || INCLUDE_bigcrypt
52
+
53
+ #define DES_TRD_OUTPUT_LEN 14 /* SShhhhhhhhhhh0 */
54
+ #define DES_EXT_OUTPUT_LEN 21 /* _CCCCSSSShhhhhhhhhhh0 */
55
+ #define DES_BIG_OUTPUT_LEN ((16*11) + 2 + 1) /* SS (hhhhhhhhhhh){1,16} 0 */
56
+
57
+ #define DES_MAX_OUTPUT_LEN \
58
+ MAX (DES_TRD_OUTPUT_LEN, MAX (DES_EXT_OUTPUT_LEN, DES_BIG_OUTPUT_LEN))
59
+
60
+ static_assert (DES_MAX_OUTPUT_LEN <= CRYPT_OUTPUT_SIZE,
61
+ "CRYPT_OUTPUT_SIZE is too small for DES");
62
+
63
+ /* A des_buffer holds all of the sensitive intermediate data used by
64
+ crypt_des_*. */
65
+
66
+ struct des_buffer
67
+ {
68
+ struct des_ctx ctx;
69
+ uint8_t keybuf[8];
70
+ uint8_t pkbuf[8];
71
+ };
72
+
73
+ static_assert (sizeof (struct des_buffer) <= ALG_SPECIFIC_SIZE,
74
+ "ALG_SPECIFIC_SIZE is too small for DES");
75
+
76
+
77
+ static inline int
78
+ ascii_to_bin(char ch)
79
+ {
80
+ if (ch > 'z')
81
+ return -1;
82
+ if (ch >= 'a')
83
+ return ch - 'a' + 38;
84
+ if (ch > 'Z')
85
+ return -1;
86
+ if (ch >= 'A')
87
+ return ch - 'A' + 12;
88
+ if (ch > '9')
89
+ return -1;
90
+ if (ch >= '.')
91
+ return ch - '.';
92
+ return -1;
93
+ }
94
+
95
+ /* Generate an 11-character DES password hash into the buffer at
96
+ OUTPUT, and nul-terminate it. The salt and key have already been
97
+ set. The plaintext is 64 bits of zeroes, and the raw ciphertext is
98
+ written to cbuf[]. */
99
+ static void
100
+ des_gen_hash (struct des_ctx *ctx, uint32_t count, uint8_t *output,
101
+ uint8_t cbuf[8])
102
+ {
103
+ uint8_t plaintext[8];
104
+ memset (plaintext, 0, 8);
105
+ des_crypt_block (ctx, cbuf, plaintext, count, false);
106
+
107
+ /* Now encode the result. */
108
+ const uint8_t *sptr = cbuf;
109
+ const uint8_t *end = sptr + 8;
110
+ unsigned int c1, c2;
111
+
112
+ do
113
+ {
114
+ c1 = *sptr++;
115
+ *output++ = ascii64[c1 >> 2];
116
+ c1 = (c1 & 0x03) << 4;
117
+ if (end - sptr <= 0)
118
+ {
119
+ *output++ = ascii64[c1];
120
+ break;
121
+ }
122
+
123
+ c2 = *sptr++;
124
+ c1 |= c2 >> 4;
125
+ *output++ = ascii64[c1];
126
+ c1 = (c2 & 0x0f) << 2;
127
+ if (end - sptr <= 0)
128
+ {
129
+ *output++ = ascii64[c1];
130
+ break;
131
+ }
132
+
133
+ c2 = *sptr++;
134
+ c1 |= c2 >> 6;
135
+ *output++ = ascii64[c1];
136
+ *output++ = ascii64[c2 & 0x3f];
137
+ }
138
+ while (end - sptr > 0);
139
+ *output = '\0';
140
+ }
141
+ #endif
142
+
143
+ #if INCLUDE_descrypt
144
+ /* The original UNIX DES-based password hash, no extensions. */
145
+ void
146
+ crypt_descrypt_rn (const char *phrase, size_t ARG_UNUSED (phr_size),
147
+ const char *setting, size_t ARG_UNUSED (set_size),
148
+ uint8_t *output, size_t out_size,
149
+ void *scratch, size_t scr_size)
150
+ {
151
+ /* This shouldn't ever happen, but... */
152
+ if (out_size < DES_TRD_OUTPUT_LEN || scr_size < sizeof (struct des_buffer))
153
+ {
154
+ errno = ERANGE;
155
+ return;
156
+ }
157
+
158
+ struct des_buffer *buf = scratch;
159
+ struct des_ctx *ctx = &buf->ctx;
160
+ uint32_t salt = 0;
161
+ uint8_t *keybuf = buf->keybuf, *pkbuf = buf->pkbuf;
162
+ uint8_t *cp = output;
163
+ int i;
164
+
165
+ /* "old"-style: setting - 2 bytes of salt, phrase - up to 8 characters.
166
+ Note: ascii_to_bin maps all byte values outside the ascii64
167
+ alphabet to -1. Do not read past the end of the string. */
168
+ i = ascii_to_bin (setting[0]);
169
+ if (i < 0)
170
+ {
171
+ errno = EINVAL;
172
+ return;
173
+ }
174
+ salt = (unsigned int)i;
175
+ i = ascii_to_bin (setting[1]);
176
+ if (i < 0)
177
+ {
178
+ errno = EINVAL;
179
+ return;
180
+ }
181
+ salt |= ((unsigned int)i << 6);
182
+
183
+ /* Write the canonical form of the salt to the output buffer. We do
184
+ this instead of copying from the setting because the setting
185
+ might be catastrophically malformed (e.g. a 0- or 1-byte string;
186
+ this could plausibly happen if e.g. login(8) doesn't special-case
187
+ "*" or "!" in the password database). */
188
+ *cp++ = ascii64[salt & 0x3f];
189
+ *cp++ = ascii64[(salt >> 6) & 0x3f];
190
+
191
+ /* Copy the first 8 characters of the password into keybuf, shifting
192
+ each character up by 1 bit and padding on the right with zeroes. */
193
+ for (i = 0; i < 8; i++)
194
+ {
195
+ keybuf[i] = (uint8_t)(*phrase << 1);
196
+ if (*phrase)
197
+ phrase++;
198
+ }
199
+
200
+ des_set_key (ctx, keybuf);
201
+ des_set_salt (ctx, salt);
202
+ des_gen_hash (ctx, 25, cp, pkbuf);
203
+ }
204
+ #endif
205
+
206
+ #if INCLUDE_bigcrypt
207
+ /* This algorithm is algorithm 0 (default) shipped with the C2 secure
208
+ implementation of Digital UNIX.
209
+
210
+ Disclaimer: This work is not based on the source code to Digital
211
+ UNIX, nor am I (Andy Phillips) connected to Digital Equipment Corp,
212
+ in any way other than as a customer. This code is based on
213
+ published interfaces and reasonable guesswork.
214
+
215
+ Description: The cleartext is divided into blocks of 8 characters
216
+ or less. Each block is encrypted using the standard UNIX libc crypt
217
+ function. The result of the encryption for one block provides the
218
+ salt for the succeeding block. The output is simply the
219
+ concatenation of all the blocks. Up to 16 blocks are supported
220
+ (that is, the password can be no more than 128 characters long).
221
+
222
+ Andy Phillips <atp@mssl.ucl.ac.uk> */
223
+ void
224
+ crypt_bigcrypt_rn (const char *phrase, size_t phr_size,
225
+ const char *setting, size_t set_size,
226
+ uint8_t *output, size_t out_size,
227
+ void *scratch, size_t scr_size)
228
+ {
229
+ /* descrypt and bigcrypt generate identical hashes when the phrase
230
+ contains no more than 8 characters. When the phrase is longer
231
+ than 8 characters, descrypt would truncate it, and bigcrypt would
232
+ generate a different, longer hash. Therefore, when we encounter
233
+ a phrase longer than 8 characters together with a short setting
234
+ string, the appropriate behavior depends on whether descrypt is
235
+ enabled. When it is, forward to descrypt. When it isn't, reject
236
+ the operation. */
237
+ if (phr_size > 8 && set_size <= 13)
238
+ {
239
+ #if INCLUDE_descrypt
240
+ crypt_descrypt_rn (phrase, phr_size, setting, set_size,
241
+ output, out_size, scratch, scr_size);
242
+ #else
243
+ errno = EINVAL;
244
+ #endif
245
+ return;
246
+ }
247
+
248
+ /* This shouldn't ever happen, but... */
249
+ if (out_size < DES_BIG_OUTPUT_LEN || scr_size < sizeof (struct des_buffer))
250
+ {
251
+ errno = ERANGE;
252
+ return;
253
+ }
254
+
255
+ struct des_buffer *buf = scratch;
256
+ struct des_ctx *ctx = &buf->ctx;
257
+ uint32_t salt = 0;
258
+ uint8_t *keybuf = buf->keybuf, *pkbuf = buf->pkbuf;
259
+ uint8_t *cp = output;
260
+ int i, seg;
261
+
262
+ /* The setting string is exactly the same as for a traditional DES
263
+ hash. */
264
+ i = ascii_to_bin (setting[0]);
265
+ if (i < 0)
266
+ {
267
+ errno = EINVAL;
268
+ return;
269
+ }
270
+ salt = (unsigned int)i;
271
+ i = ascii_to_bin (setting[1]);
272
+ if (i < 0)
273
+ {
274
+ errno = EINVAL;
275
+ return;
276
+ }
277
+ salt |= ((unsigned int)i << 6);
278
+
279
+ *cp++ = ascii64[salt & 0x3f];
280
+ *cp++ = ascii64[(salt >> 6) & 0x3f];
281
+
282
+ for (seg = 0; seg < 16; seg++)
283
+ {
284
+ /* Copy and shift each block as for the traditional DES. */
285
+ for (i = 0; i < 8; i++)
286
+ {
287
+ keybuf[i] = (uint8_t)(*phrase << 1);
288
+ if (*phrase)
289
+ phrase++;
290
+ }
291
+
292
+ des_set_key (ctx, keybuf);
293
+ des_set_salt (ctx, salt);
294
+ des_gen_hash (ctx, 25, cp, pkbuf);
295
+
296
+ if (*phrase == 0)
297
+ break;
298
+
299
+ /* change the salt (1st 2 chars of previous block) - this was found
300
+ by dowsing - no need to check for invalid characters here */
301
+ salt = (unsigned int)ascii_to_bin ((char)cp[0]);
302
+ salt |= (unsigned int)ascii_to_bin ((char)cp[1]) << 6;
303
+ cp += 11;
304
+ }
305
+ }
306
+ #endif
307
+
308
+ #if INCLUDE_bsdicrypt
309
+ /* crypt_rn() entry point for BSD-style extended DES hashes. These
310
+ permit long passwords and have more salt and a controllable iteration
311
+ count, but are still unacceptably weak by modern standards. */
312
+ void
313
+ crypt_bsdicrypt_rn (const char *phrase, size_t ARG_UNUSED (phr_size),
314
+ const char *setting, size_t set_size,
315
+ uint8_t *output, size_t out_size,
316
+ void *scratch, size_t scr_size)
317
+ {
318
+ /* This shouldn't ever happen, but... */
319
+ if (out_size < DES_EXT_OUTPUT_LEN || scr_size < sizeof (struct des_buffer))
320
+ {
321
+ errno = ERANGE;
322
+ return;
323
+ }
324
+
325
+ /* If this is true, this function shouldn't have been called.
326
+ Setting must be at least 9 bytes long, byte 10+ is ignored. */
327
+ if (*setting != '_' || set_size < 9)
328
+ {
329
+ errno = EINVAL;
330
+ return;
331
+ }
332
+
333
+ struct des_buffer *buf = scratch;
334
+ struct des_ctx *ctx = &buf->ctx;
335
+ uint32_t count = 0, salt = 0;
336
+ uint8_t *keybuf = buf->keybuf, *pkbuf = buf->pkbuf;
337
+ uint8_t *cp = output;
338
+ int i, x;
339
+
340
+ /* "new"-style DES hash:
341
+ setting - underscore, 4 bytes of count, 4 bytes of salt
342
+ phrase - unlimited characters
343
+ */
344
+ for (i = 1; i < 5; i++)
345
+ {
346
+ x = ascii_to_bin(setting[i]);
347
+ if (x < 0)
348
+ {
349
+ errno = EINVAL;
350
+ return;
351
+ }
352
+ count |= (unsigned int)x << ((i - 1) * 6);
353
+ }
354
+
355
+ for (i = 5; i < 9; i++)
356
+ {
357
+ x = ascii_to_bin(setting[i]);
358
+ if (x < 0)
359
+ {
360
+ errno = EINVAL;
361
+ return;
362
+ }
363
+ salt |= (unsigned int)x << ((i - 5) * 6);
364
+ }
365
+
366
+ memcpy (cp, setting, 9);
367
+ cp += 9;
368
+
369
+ /* Fold passwords longer than 8 bytes into a single DES key using a
370
+ procedure similar to a Merkle-Dåmgard hash construction. Each
371
+ block is shifted and padded, as for the traditional hash, then
372
+ XORed with the output of the previous round (IV all bits zero),
373
+ set as the DES key, and encrypted to produce the round output.
374
+ The salt is zero throughout this procedure. */
375
+ des_set_salt (ctx, 0);
376
+ memset (pkbuf, 0, 8);
377
+ for (;;)
378
+ {
379
+ for (i = 0; i < 8; i++)
380
+ {
381
+ keybuf[i] = (uint8_t)(pkbuf[i] ^ (*phrase << 1));
382
+ if (*phrase)
383
+ phrase++;
384
+ }
385
+ des_set_key (ctx, keybuf);
386
+ if (*phrase == 0)
387
+ break;
388
+ des_crypt_block (ctx, pkbuf, keybuf, 1, false);
389
+ }
390
+
391
+ /* Proceed as for the traditional DES hash. */
392
+ des_set_salt (ctx, salt);
393
+ des_gen_hash (ctx, count, cp, pkbuf);
394
+ }
395
+ #endif
396
+
397
+ #if INCLUDE_descrypt || INCLUDE_bigcrypt
398
+ void
399
+ gensalt_descrypt_rn (unsigned long count,
400
+ const uint8_t *rbytes, size_t nrbytes,
401
+ uint8_t *output, size_t output_size)
402
+ {
403
+ if (output_size < 3)
404
+ {
405
+ errno = ERANGE;
406
+ return;
407
+ }
408
+
409
+ if (nrbytes < 2 || count != 0)
410
+ {
411
+ errno = EINVAL;
412
+ return;
413
+ }
414
+
415
+ output[0] = ascii64[(unsigned int) rbytes[0] & 0x3f];
416
+ output[1] = ascii64[(unsigned int) rbytes[1] & 0x3f];
417
+ output[2] = '\0';
418
+ }
419
+ #if INCLUDE_bigcrypt
420
+ void
421
+ gensalt_bigcrypt_rn (unsigned long count,
422
+ const uint8_t *rbytes, size_t nrbytes,
423
+ uint8_t *output, size_t output_size)
424
+ {
425
+ #if !INCLUDE_descrypt
426
+ /* We need descrypt + 12 bytes. */
427
+ if (output_size < 3 + 12)
428
+ {
429
+ errno = ERANGE;
430
+ return;
431
+ }
432
+ #endif
433
+
434
+ /* Same setting string as descrypt, but... */
435
+ gensalt_descrypt_rn (count, rbytes, nrbytes, output, output_size);
436
+
437
+ #if !INCLUDE_descrypt
438
+ /* ... add 12 trailing dummy characters, which makes the string too
439
+ long to be a descrypt setting, thus bigcrypt will be used. */
440
+ strcpy_or_abort (output + 2, output_size - 2, "............");
441
+ #endif
442
+ }
443
+ #endif
444
+ #endif
445
+
446
+ #if INCLUDE_bsdicrypt
447
+ void
448
+ gensalt_bsdicrypt_rn (unsigned long count,
449
+ const uint8_t *rbytes, size_t nrbytes,
450
+ uint8_t *output, size_t output_size)
451
+ {
452
+ if (output_size < 1 + 4 + 4 + 1)
453
+ {
454
+ errno = ERANGE;
455
+ return;
456
+ }
457
+ if (nrbytes < 3)
458
+ {
459
+ errno = EINVAL;
460
+ return;
461
+ }
462
+
463
+ if (count == 0)
464
+ count = 725;
465
+ if (count > 0xffffff)
466
+ count = 0xffffff;
467
+
468
+ /* Even iteration counts make it easier to detect weak DES keys from a look
469
+ at the hash, so they should be avoided. */
470
+ count |= 1;
471
+
472
+ unsigned long value =
473
+ ((unsigned long) (unsigned char) rbytes[0] << 0) |
474
+ ((unsigned long) (unsigned char) rbytes[1] << 8) |
475
+ ((unsigned long) (unsigned char) rbytes[2] << 16);
476
+
477
+ output[0] = '_';
478
+
479
+ output[1] = ascii64[(count >> 0) & 0x3f];
480
+ output[2] = ascii64[(count >> 6) & 0x3f];
481
+ output[3] = ascii64[(count >> 12) & 0x3f];
482
+ output[4] = ascii64[(count >> 18) & 0x3f];
483
+
484
+ output[5] = ascii64[(value >> 0) & 0x3f];
485
+ output[6] = ascii64[(value >> 6) & 0x3f];
486
+ output[7] = ascii64[(value >> 12) & 0x3f];
487
+ output[8] = ascii64[(value >> 18) & 0x3f];
488
+
489
+ output[9] = '\0';
490
+ }
491
+ #endif