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,106 @@
1
+ /*-
2
+ * Copyright 2013-2018,2022 Alexander Peslyak
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted.
7
+ *
8
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
9
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
12
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18
+ * SUCH DAMAGE.
19
+ */
20
+
21
+ #ifdef __unix__
22
+ #include <sys/mman.h>
23
+ #endif
24
+ #ifdef __linux__
25
+ #include <linux/mman.h> /* for MAP_HUGE_2MB */
26
+ #endif
27
+
28
+ #define HUGEPAGE_THRESHOLD (32 * 1024 * 1024)
29
+
30
+ #ifdef __x86_64__
31
+ #define HUGEPAGE_SIZE (2 * 1024 * 1024)
32
+ #else
33
+ #undef HUGEPAGE_SIZE
34
+ #endif
35
+
36
+ static void *alloc_region(yescrypt_region_t *region, size_t size)
37
+ {
38
+ size_t base_size = size;
39
+ uint8_t *base, *aligned;
40
+ #ifdef MAP_ANON
41
+ unsigned int flags =
42
+ #ifdef MAP_NOCORE
43
+ MAP_NOCORE |
44
+ #endif
45
+ MAP_ANON | MAP_PRIVATE;
46
+ #if defined(MAP_HUGETLB) && defined(MAP_HUGE_2MB) && defined(HUGEPAGE_SIZE)
47
+ size_t new_size = size;
48
+ const size_t hugepage_mask = (size_t)HUGEPAGE_SIZE - 1;
49
+ if (size >= HUGEPAGE_THRESHOLD && size + hugepage_mask >= size) {
50
+ flags |= MAP_HUGETLB | MAP_HUGE_2MB;
51
+ /*
52
+ * Linux's munmap() fails on MAP_HUGETLB mappings if size is not a multiple of
53
+ * huge page size, so let's round up to huge page size here.
54
+ */
55
+ new_size = size + hugepage_mask;
56
+ new_size &= ~hugepage_mask;
57
+ }
58
+ base = mmap(NULL, new_size, PROT_READ | PROT_WRITE, (int)flags, -1, 0);
59
+ if (base != MAP_FAILED) {
60
+ base_size = new_size;
61
+ } else if (flags & MAP_HUGETLB) {
62
+ flags &= ~(unsigned int)(MAP_HUGETLB | MAP_HUGE_2MB);
63
+ base = mmap(NULL, size, PROT_READ | PROT_WRITE, (int)flags, -1, 0);
64
+ }
65
+
66
+ #else
67
+ base = mmap(NULL, size, PROT_READ | PROT_WRITE, (int)flags, -1, 0);
68
+ #endif
69
+ if (base == MAP_FAILED)
70
+ base = NULL;
71
+ aligned = base;
72
+ #else /* mmap not available */
73
+ base = aligned = NULL;
74
+ if (size + 63 < size) {
75
+ errno = ENOMEM;
76
+ } else if ((base = malloc(size + 63)) != NULL) {
77
+ aligned = base + 63;
78
+ aligned -= (uintptr_t)aligned & 63;
79
+ }
80
+ #endif
81
+ region->base = base;
82
+ region->aligned = aligned;
83
+ region->base_size = base ? base_size : 0;
84
+ region->aligned_size = base ? size : 0;
85
+ return aligned;
86
+ }
87
+
88
+ static inline void init_region(yescrypt_region_t *region)
89
+ {
90
+ region->base = region->aligned = NULL;
91
+ region->base_size = region->aligned_size = 0;
92
+ }
93
+
94
+ static int free_region(yescrypt_region_t *region)
95
+ {
96
+ if (region->base) {
97
+ #ifdef MAP_ANON
98
+ if (munmap(region->base, region->base_size))
99
+ return -1;
100
+ #else
101
+ free(region->base);
102
+ #endif
103
+ }
104
+ init_region(region);
105
+ return 0;
106
+ }
@@ -0,0 +1,360 @@
1
+ /*-
2
+ * Copyright 2009 Colin Percival
3
+ * Copyright 2013-2018 Alexander Peslyak
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE.
26
+ *
27
+ * This file was originally written by Colin Percival as part of the Tarsnap
28
+ * online backup system.
29
+ */
30
+ #ifndef _YESCRYPT_H_
31
+ #define _YESCRYPT_H_
32
+
33
+ #include "crypt-port.h"
34
+
35
+ #include <stdint.h>
36
+ #include <stdlib.h> /* for size_t */
37
+
38
+ #ifdef __cplusplus
39
+ extern "C" {
40
+ #endif
41
+
42
+ /**
43
+ * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
44
+ * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
45
+ * p, buflen) and write the result into buf. The parameters r, p, and buflen
46
+ * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
47
+ * must be a power of 2 greater than 1.
48
+ *
49
+ * Return 0 on success; or -1 on error.
50
+ *
51
+ * MT-safe as long as buf is local to the thread.
52
+ */
53
+ extern int crypto_scrypt(const uint8_t *passwd, size_t passwdlen,
54
+ const uint8_t *salt, size_t saltlen,
55
+ uint64_t N, uint32_t r, uint32_t p, uint8_t *buf, size_t buflen);
56
+
57
+ /**
58
+ * Internal type used by the memory allocator. Please do not use it directly.
59
+ * Use yescrypt_shared_t and yescrypt_local_t as appropriate instead, since
60
+ * they might differ from each other in a future version.
61
+ */
62
+ typedef struct {
63
+ void *base, *aligned;
64
+ size_t base_size, aligned_size;
65
+ } yescrypt_region_t;
66
+
67
+ /**
68
+ * Types for shared (ROM) and thread-local (RAM) data structures.
69
+ */
70
+ typedef yescrypt_region_t yescrypt_shared_t;
71
+ typedef yescrypt_region_t yescrypt_local_t;
72
+
73
+ /**
74
+ * Two 64-bit tags placed 48 bytes to the end of a ROM in host byte endianness
75
+ * (and followed by 32 bytes of the ROM digest).
76
+ */
77
+ #define YESCRYPT_ROM_TAG1 0x7470797263736579 /* "yescrypt" */
78
+ #define YESCRYPT_ROM_TAG2 0x687361684d4f522d /* "-ROMhash" */
79
+
80
+ /**
81
+ * Type and possible values for the flags argument of yescrypt_kdf(),
82
+ * yescrypt_encode_params_r(), yescrypt_encode_params(). Most of these may be
83
+ * OR'ed together, except that YESCRYPT_WORM stands on its own.
84
+ * Please refer to the description of yescrypt_kdf() below for the meaning of
85
+ * these flags.
86
+ */
87
+ typedef uint32_t yescrypt_flags_t;
88
+ /* Public */
89
+ #define YESCRYPT_WORM 1
90
+ #define YESCRYPT_RW 0x002
91
+ #define YESCRYPT_ROUNDS_3 0x000
92
+ #define YESCRYPT_ROUNDS_6 0x004
93
+ #define YESCRYPT_GATHER_1 0x000
94
+ #define YESCRYPT_GATHER_2 0x008
95
+ #define YESCRYPT_GATHER_4 0x010
96
+ #define YESCRYPT_GATHER_8 0x018
97
+ #define YESCRYPT_SIMPLE_1 0x000
98
+ #define YESCRYPT_SIMPLE_2 0x020
99
+ #define YESCRYPT_SIMPLE_4 0x040
100
+ #define YESCRYPT_SIMPLE_8 0x060
101
+ #define YESCRYPT_SBOX_6K 0x000
102
+ #define YESCRYPT_SBOX_12K 0x080
103
+ #define YESCRYPT_SBOX_24K 0x100
104
+ #define YESCRYPT_SBOX_48K 0x180
105
+ #define YESCRYPT_SBOX_96K 0x200
106
+ #define YESCRYPT_SBOX_192K 0x280
107
+ #define YESCRYPT_SBOX_384K 0x300
108
+ #define YESCRYPT_SBOX_768K 0x380
109
+ /* Only valid for yescrypt_init_shared() */
110
+ #define YESCRYPT_SHARED_PREALLOCATED 0x10000
111
+ #ifdef YESCRYPT_INTERNAL
112
+ /* Private */
113
+ #define YESCRYPT_MODE_MASK 0x003
114
+ #define YESCRYPT_RW_FLAVOR_MASK 0x3fc
115
+ #define YESCRYPT_INIT_SHARED 0x01000000
116
+ #define YESCRYPT_ALLOC_ONLY 0x08000000
117
+ #define YESCRYPT_PREHASH 0x10000000
118
+ #endif
119
+
120
+ #define YESCRYPT_RW_DEFAULTS \
121
+ (YESCRYPT_RW | \
122
+ YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | \
123
+ YESCRYPT_SBOX_12K)
124
+
125
+ #define YESCRYPT_DEFAULTS YESCRYPT_RW_DEFAULTS
126
+
127
+ #ifdef YESCRYPT_INTERNAL
128
+ #define YESCRYPT_KNOWN_FLAGS \
129
+ (YESCRYPT_MODE_MASK | YESCRYPT_RW_FLAVOR_MASK | \
130
+ YESCRYPT_SHARED_PREALLOCATED | \
131
+ YESCRYPT_INIT_SHARED | YESCRYPT_ALLOC_ONLY | YESCRYPT_PREHASH)
132
+ #endif
133
+
134
+ /**
135
+ * yescrypt parameters combined into one struct. N, r, p are the same as in
136
+ * classic scrypt, except that the meaning of p changes when YESCRYPT_RW is
137
+ * set. flags, t, g, NROM are special to yescrypt.
138
+ */
139
+ typedef struct {
140
+ yescrypt_flags_t flags;
141
+ uint64_t N;
142
+ uint32_t r, p, t, g;
143
+ uint64_t NROM;
144
+ } yescrypt_params_t;
145
+
146
+ /**
147
+ * A 256-bit yescrypt hash, or a hash encryption key (which may itself have
148
+ * been derived as a yescrypt hash of a human-specified key string).
149
+ */
150
+ typedef union {
151
+ unsigned char uc[32];
152
+ uint64_t u64[4];
153
+ } yescrypt_binary_t;
154
+
155
+ /**
156
+ * yescrypt_init_shared(shared, seed, seedlen, params):
157
+ * Optionally allocate memory for and initialize the shared (ROM) data
158
+ * structure. The parameters flags, NROM, r, p, and t specify how the ROM is
159
+ * to be initialized, and seed and seedlen specify the initial seed affecting
160
+ * the data with which the ROM is filled.
161
+ *
162
+ * Return 0 on success; or -1 on error.
163
+ *
164
+ * If bit YESCRYPT_SHARED_PREALLOCATED in flags is set, then memory for the
165
+ * ROM is assumed to have been preallocated by the caller, with shared->aligned
166
+ * being the start address of the ROM and shared->aligned_size being its size
167
+ * (which must be sufficient for NROM, r, p). This may be used e.g. when the
168
+ * ROM is to be placed in a SysV shared memory segment allocated by the caller.
169
+ *
170
+ * MT-safe as long as shared is local to the thread.
171
+ */
172
+ extern int yescrypt_init_shared(yescrypt_shared_t *shared,
173
+ const uint8_t *seed, size_t seedlen, const yescrypt_params_t *params);
174
+
175
+ /**
176
+ * yescrypt_digest_shared(shared):
177
+ * Extract the previously stored message digest of the provided yescrypt ROM.
178
+ *
179
+ * Return pointer to the message digest on success; or NULL on error.
180
+ *
181
+ * MT-unsafe.
182
+ */
183
+ extern yescrypt_binary_t *yescrypt_digest_shared(yescrypt_shared_t *shared);
184
+
185
+ /**
186
+ * yescrypt_free_shared(shared):
187
+ * Free memory that had been allocated with yescrypt_init_shared().
188
+ *
189
+ * Return 0 on success; or -1 on error.
190
+ *
191
+ * MT-safe as long as shared is local to the thread.
192
+ */
193
+ extern int yescrypt_free_shared(yescrypt_shared_t *shared);
194
+
195
+ /**
196
+ * yescrypt_init_local(local):
197
+ * Initialize the thread-local (RAM) data structure. Actual memory allocation
198
+ * is currently fully postponed until a call to yescrypt_kdf() or yescrypt_r().
199
+ *
200
+ * Return 0 on success; or -1 on error.
201
+ *
202
+ * MT-safe as long as local is local to the thread.
203
+ */
204
+ extern int yescrypt_init_local(yescrypt_local_t *local);
205
+
206
+ /**
207
+ * yescrypt_free_local(local):
208
+ * Free memory that may have been allocated for an initialized thread-local
209
+ * (RAM) data structure.
210
+ *
211
+ * Return 0 on success; or -1 on error.
212
+ *
213
+ * MT-safe as long as local is local to the thread.
214
+ */
215
+ extern int yescrypt_free_local(yescrypt_local_t *local);
216
+
217
+ /**
218
+ * yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen, params,
219
+ * buf, buflen):
220
+ * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
221
+ * p, buflen), or a revision of scrypt as requested by flags and shared, and
222
+ * write the result into buf. The parameters N, r, p, and buflen must satisfy
223
+ * the same conditions as with crypto_scrypt(). t controls computation time
224
+ * while not affecting peak memory usage (t = 0 is optimal unless higher N*r
225
+ * is not affordable while higher t is). g controls hash upgrades (g = 0 for
226
+ * no upgrades so far). shared and flags may request special modes. local is
227
+ * the thread-local data structure, allowing to preserve and reuse a memory
228
+ * allocation across calls, thereby reducing processing overhead.
229
+ *
230
+ * Return 0 on success; or -1 on error.
231
+ *
232
+ * Classic scrypt is available by setting shared = NULL, flags = 0, and t = 0.
233
+ *
234
+ * Setting YESCRYPT_WORM enables only minimal deviations from classic scrypt:
235
+ * support for the t parameter, and pre- and post-hashing.
236
+ *
237
+ * Setting YESCRYPT_RW fully enables yescrypt. As a side effect of differences
238
+ * between the algorithms, it also prevents p > 1 from growing the threads'
239
+ * combined processing time and memory allocation (like it did with classic
240
+ * scrypt and YESCRYPT_WORM), treating p as a divider rather than a multiplier.
241
+ *
242
+ * Passing a shared structure, with ROM contents previously computed by
243
+ * yescrypt_init_shared(), enables the use of ROM and requires YESCRYPT_RW.
244
+ *
245
+ * In order to allow for initialization of the ROM to be split into a separate
246
+ * program (or separate invocation of the same program), the shared->aligned
247
+ * and shared->aligned_size fields may optionally be set by the caller directly
248
+ * (e.g., to a mapped SysV shm segment), without using yescrypt_init_shared().
249
+ *
250
+ * local must be initialized with yescrypt_init_local().
251
+ *
252
+ * MT-safe as long as local and buf are local to the thread.
253
+ */
254
+ extern int yescrypt_kdf(const yescrypt_shared_t *shared,
255
+ yescrypt_local_t *local,
256
+ const uint8_t *passwd, size_t passwdlen,
257
+ const uint8_t *salt, size_t saltlen,
258
+ const yescrypt_params_t *params,
259
+ uint8_t *buf, size_t buflen);
260
+
261
+ /**
262
+ * yescrypt_r(shared, local, passwd, passwdlen, setting, key, buf, buflen):
263
+ * Compute and encode an scrypt or enhanced scrypt hash of passwd given the
264
+ * parameters and salt value encoded in setting. If shared is not NULL, a ROM
265
+ * is used and YESCRYPT_RW is required. Otherwise, whether to compute classic
266
+ * scrypt, YESCRYPT_WORM (a slight deviation from classic scrypt), or
267
+ * YESCRYPT_RW (time-memory tradeoff discouraging modification) is determined
268
+ * by the setting string. shared (if not NULL) and local must be initialized
269
+ * as described above for yescrypt_kdf(). buf must be large enough (as
270
+ * indicated by buflen) to hold the encoded hash string.
271
+ *
272
+ * Return the encoded hash string on success; or NULL on error.
273
+ *
274
+ * MT-safe as long as local and buf are local to the thread.
275
+ */
276
+ extern uint8_t *yescrypt_r(const yescrypt_shared_t *shared,
277
+ yescrypt_local_t *local,
278
+ const uint8_t *passwd, size_t passwdlen,
279
+ const uint8_t *setting,
280
+ const yescrypt_binary_t *key,
281
+ uint8_t *buf, size_t buflen);
282
+
283
+ /**
284
+ * yescrypt(passwd, setting):
285
+ * Compute and encode an scrypt or enhanced scrypt hash of passwd given the
286
+ * parameters and salt value encoded in setting. Whether to compute classic
287
+ * scrypt, YESCRYPT_WORM (a slight deviation from classic scrypt), or
288
+ * YESCRYPT_RW (time-memory tradeoff discouraging modification) is determined
289
+ * by the setting string.
290
+ *
291
+ * Return the encoded hash string on success; or NULL on error.
292
+ *
293
+ * This is a crypt(3)-like interface, which is simpler to use than
294
+ * yescrypt_r(), but it is not MT-safe, it does not allow for the use of a ROM,
295
+ * and it is slower than yescrypt_r() for repeated calls because it allocates
296
+ * and frees memory on each call.
297
+ *
298
+ * MT-unsafe.
299
+ */
300
+ extern uint8_t *yescrypt(const uint8_t *passwd, const uint8_t *setting);
301
+
302
+ /**
303
+ * yescrypt_reencrypt(hash, from_key, to_key):
304
+ * Re-encrypt a yescrypt hash from one key to another. Either key may be NULL
305
+ * to indicate unencrypted hash. The encoded hash string is modified in-place.
306
+ *
307
+ * Return the hash pointer on success; or NULL on error (in which case the hash
308
+ * string is left unmodified).
309
+ *
310
+ * MT-safe as long as hash is local to the thread.
311
+ */
312
+ extern uint8_t *yescrypt_reencrypt(uint8_t *hash,
313
+ const yescrypt_binary_t *from_key,
314
+ const yescrypt_binary_t *to_key);
315
+
316
+ /**
317
+ * yescrypt_encode_params_r(params, src, srclen, buf, buflen):
318
+ * Generate a setting string for use with yescrypt_r() and yescrypt() by
319
+ * encoding into it the parameters flags, N, r, p, t, g, and a salt given by
320
+ * src (of srclen bytes). buf must be large enough (as indicated by buflen)
321
+ * to hold the setting string.
322
+ *
323
+ * Return the setting string on success; or NULL on error.
324
+ *
325
+ * MT-safe as long as buf is local to the thread.
326
+ */
327
+ extern uint8_t *yescrypt_encode_params_r(const yescrypt_params_t *params,
328
+ const uint8_t *src, size_t srclen,
329
+ uint8_t *buf, size_t buflen);
330
+
331
+ /**
332
+ * yescrypt_encode_params(params, src, srclen):
333
+ * Generate a setting string for use with yescrypt_r() and yescrypt(). This
334
+ * function is the same as yescrypt_encode_params_r() except that it uses a
335
+ * static buffer and thus is not MT-safe.
336
+ *
337
+ * Return the setting string on success; or NULL on error.
338
+ *
339
+ * MT-unsafe.
340
+ */
341
+ extern uint8_t *yescrypt_encode_params(const yescrypt_params_t *params,
342
+ const uint8_t *src, size_t srclen);
343
+
344
+ #ifdef YESCRYPT_INTERNAL
345
+
346
+ #define decode64 yescrypt_decode64
347
+ #define encode64 yescrypt_encode64
348
+
349
+ extern const uint8_t *decode64(uint8_t *dst, size_t *dstlen,
350
+ const uint8_t *src, size_t srclen);
351
+ extern uint8_t *encode64(uint8_t *dst, size_t dstlen,
352
+ const uint8_t *src, size_t srclen);
353
+
354
+ #endif
355
+
356
+ #ifdef __cplusplus
357
+ }
358
+ #endif
359
+
360
+ #endif /* !_YESCRYPT_H_ */
@@ -0,0 +1,164 @@
1
+ /* Functions to copy data between possibly-unaligned byte buffers
2
+ and machine integers, fixing the endianness.
3
+
4
+ Written by Zack Weinberg <zackw at panix.com> in 2017.
5
+ To the extent possible under law, Zack Weinberg has waived all
6
+ copyright and related or neighboring rights to this work.
7
+
8
+ See https://creativecommons.org/publicdomain/zero/1.0/ for further
9
+ details. */
10
+
11
+ #ifndef _CRYPT_BYTEORDER_H
12
+ #define _CRYPT_BYTEORDER_H 1
13
+
14
+ static inline uint32_t
15
+ le32_to_cpu (const unsigned char *buf)
16
+ {
17
+ return ((((uint32_t)buf[0]) << 0) |
18
+ (((uint32_t)buf[1]) << 8) |
19
+ (((uint32_t)buf[2]) << 16) |
20
+ (((uint32_t)buf[3]) << 24) );
21
+ }
22
+
23
+ static inline uint32_t
24
+ be32_to_cpu (const unsigned char *buf)
25
+ {
26
+ return ((((uint32_t)buf[0]) << 24) |
27
+ (((uint32_t)buf[1]) << 16) |
28
+ (((uint32_t)buf[2]) << 8) |
29
+ (((uint32_t)buf[3]) << 0) );
30
+ }
31
+
32
+ static inline uint64_t
33
+ le64_to_cpu (const unsigned char *buf)
34
+ {
35
+ return ((((uint64_t)buf[0]) << 0) |
36
+ (((uint64_t)buf[1]) << 8) |
37
+ (((uint64_t)buf[2]) << 16) |
38
+ (((uint64_t)buf[3]) << 24) |
39
+ (((uint64_t)buf[4]) << 32) |
40
+ (((uint64_t)buf[5]) << 40) |
41
+ (((uint64_t)buf[6]) << 48) |
42
+ (((uint64_t)buf[7]) << 56) );
43
+ }
44
+
45
+ static inline uint64_t
46
+ be64_to_cpu (const unsigned char *buf)
47
+ {
48
+ return ((((uint64_t)buf[0]) << 56) |
49
+ (((uint64_t)buf[1]) << 48) |
50
+ (((uint64_t)buf[2]) << 40) |
51
+ (((uint64_t)buf[3]) << 32) |
52
+ (((uint64_t)buf[4]) << 24) |
53
+ (((uint64_t)buf[5]) << 16) |
54
+ (((uint64_t)buf[6]) << 8) |
55
+ (((uint64_t)buf[7]) << 0) );
56
+ }
57
+
58
+ static inline void
59
+ cpu_to_le32 (unsigned char *buf, uint32_t n)
60
+ {
61
+ buf[0] = (unsigned char)((n & 0x000000FFu) >> 0);
62
+ buf[1] = (unsigned char)((n & 0x0000FF00u) >> 8);
63
+ buf[2] = (unsigned char)((n & 0x00FF0000u) >> 16);
64
+ buf[3] = (unsigned char)((n & 0xFF000000u) >> 24);
65
+ }
66
+
67
+ static inline void
68
+ cpu_to_be32 (unsigned char *buf, uint32_t n)
69
+ {
70
+ buf[0] = (unsigned char)((n & 0xFF000000u) >> 24);
71
+ buf[1] = (unsigned char)((n & 0x00FF0000u) >> 16);
72
+ buf[2] = (unsigned char)((n & 0x0000FF00u) >> 8);
73
+ buf[3] = (unsigned char)((n & 0x000000FFu) >> 0);
74
+ }
75
+
76
+ static inline void
77
+ cpu_to_le64 (unsigned char *buf, uint64_t n)
78
+ {
79
+ buf[0] = (unsigned char)((n & 0x00000000000000FFull) >> 0);
80
+ buf[1] = (unsigned char)((n & 0x000000000000FF00ull) >> 8);
81
+ buf[2] = (unsigned char)((n & 0x0000000000FF0000ull) >> 16);
82
+ buf[3] = (unsigned char)((n & 0x00000000FF000000ull) >> 24);
83
+ buf[4] = (unsigned char)((n & 0x000000FF00000000ull) >> 32);
84
+ buf[5] = (unsigned char)((n & 0x0000FF0000000000ull) >> 40);
85
+ buf[6] = (unsigned char)((n & 0x00FF000000000000ull) >> 48);
86
+ buf[7] = (unsigned char)((n & 0xFF00000000000000ull) >> 56);
87
+ }
88
+
89
+ static inline void
90
+ cpu_to_be64 (unsigned char *buf, uint64_t n)
91
+ {
92
+ buf[0] = (unsigned char)((n & 0xFF00000000000000ull) >> 56);
93
+ buf[1] = (unsigned char)((n & 0x00FF000000000000ull) >> 48);
94
+ buf[2] = (unsigned char)((n & 0x0000FF0000000000ull) >> 40);
95
+ buf[3] = (unsigned char)((n & 0x000000FF00000000ull) >> 32);
96
+ buf[4] = (unsigned char)((n & 0x00000000FF000000ull) >> 24);
97
+ buf[5] = (unsigned char)((n & 0x0000000000FF0000ull) >> 16);
98
+ buf[6] = (unsigned char)((n & 0x000000000000FF00ull) >> 8);
99
+ buf[7] = (unsigned char)((n & 0x00000000000000FFull) >> 0);
100
+ }
101
+
102
+ /* Template: Define a function named cpu_to_<END><BITS>_vect that
103
+ takes a vector SRC of LEN integers, each of type uint<BITS>_t, and
104
+ writes them to the buffer DST in the endianness defined by END.
105
+ Caution: LEN is the number of vector elements, not the total size
106
+ of the buffers. */
107
+ #define VECTOR_CPU_TO(end, bits) VECTOR_CPU_TO_(end##bits, uint##bits##_t)
108
+ #define VECTOR_CPU_TO_(prim, stype) \
109
+ static inline void \
110
+ cpu_to_##prim##_vect(uint8_t *dst, const stype *src, size_t len) \
111
+ { \
112
+ while (len) \
113
+ { \
114
+ cpu_to_##prim(dst, *src); \
115
+ src += 1; \
116
+ dst += sizeof(stype); \
117
+ len -= 1; \
118
+ } \
119
+ } struct _swallow_semicolon
120
+
121
+ /* Template: Define a function named <END><BITS>_to_cpu_vect that
122
+ reads a vector of LEN integers, each of type uint<BITS>_t, from the
123
+ buffer SRC, in the endianness defined by END, and writes them to
124
+ the vector DST. Caution: LEN is the number of vector elements, not
125
+ the total size of the buffers. */
126
+ #define VECTOR_TO_CPU(end, bits) VECTOR_TO_CPU_(end##bits, uint##bits##_t)
127
+ #define VECTOR_TO_CPU_(prim, dtype) \
128
+ static inline void \
129
+ prim##_to_cpu_vect(dtype *dst, const uint8_t *src, size_t len) \
130
+ { \
131
+ while (len) \
132
+ { \
133
+ *dst = prim##_to_cpu(src); \
134
+ src += sizeof(dtype); \
135
+ dst += 1; \
136
+ len -= 1; \
137
+ } \
138
+ } struct _swallow_semicolon
139
+
140
+ /* These are the vectorized endianness-conversion functions that are
141
+ presently used. Add more as necessary. */
142
+ VECTOR_CPU_TO(be,32);
143
+ VECTOR_CPU_TO(be,64);
144
+ VECTOR_TO_CPU(be,32);
145
+ VECTOR_TO_CPU(be,64);
146
+
147
+ /* Alternative names used in code derived from Colin Percival's
148
+ cryptography libraries. */
149
+ #define le32enc cpu_to_le32
150
+ #define le32dec le32_to_cpu
151
+ #define le64enc cpu_to_le64
152
+ #define le64dec le64_to_cpu
153
+
154
+ #define be32enc cpu_to_be32
155
+ #define be32dec be32_to_cpu
156
+ #define be64enc cpu_to_be64
157
+ #define be64dec be64_to_cpu
158
+
159
+ #define be32enc_vect cpu_to_be32_vect
160
+ #define be32dec_vect be32_to_cpu_vect
161
+ #define be64enc_vect cpu_to_be64_vect
162
+ #define be64dec_vect be64_to_cpu_vect
163
+
164
+ #endif /* byteorder.h */