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,168 @@
1
+ /*
2
+ * Copyright (C) 2024 Tianjia Zhang
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ * 3. Neither the name of the author nor the names of other contributors
14
+ * may be used to endorse or promote products derived from this software
15
+ * without specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
+ * SUCH DAMAGE.
28
+ */
29
+
30
+ #include "crypt-port.h"
31
+ #include "alg-sm3.h"
32
+
33
+ #include <stdio.h>
34
+
35
+ #if INCLUDE_sm3crypt
36
+
37
+ static const struct
38
+ {
39
+ const char *input;
40
+ const char result[33];
41
+ } tests[] =
42
+ {
43
+ /* Test vectors from OSCCA GM/T 0004-2012: appendix A. */
44
+ {
45
+ "abc",
46
+ "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2"
47
+ "\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0"
48
+ },
49
+ {
50
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
51
+ "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1\xea"
52
+ "\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b\x8c\x05"
53
+ },
54
+ {
55
+ "",
56
+ "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8E\x61\x19\x48\x31\xe8\x1a\x8f"
57
+ "\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82\xaa\x2b"
58
+ },
59
+ {
60
+ "a",
61
+ "\x62\x34\x76\xac\x18\xf6\x5a\x29\x09\xe4\x3c\x7f\xec\x61\xb4\x9c"
62
+ "\x7e\x76\x4a\x91\xa1\x8c\xcb\x82\xf1\x91\x7a\x29\xc8\x6c\x5e\x88"
63
+ },
64
+ {
65
+ "message digest",
66
+ "\xc5\x22\xa9\x42\xe8\x9b\xd8\x0d\x97\xdd\x66\x6e\x7a\x55\x31\xb3"
67
+ "\x61\x88\xc9\x81\x71\x49\xe9\xb2\x58\xdf\xe5\x1e\xce\x98\xed\x77"
68
+ },
69
+ {
70
+ "abcdefghijklmnopqrstuvwxyz",
71
+ "\xb8\x0f\xe9\x7a\x4d\xa2\x4a\xfc\x27\x75\x64\xf6\x6a\x35\x9e\xf4"
72
+ "\x40\x46\x2a\xd2\x8d\xcc\x6d\x63\xad\xb2\x4d\x5c\x20\xa6\x15\x95"
73
+ },
74
+ {
75
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
76
+ "\x29\x71\xd1\x0c\x88\x42\xb7\x0c\x97\x9e\x55\x06\x34\x80\xc5\x0b"
77
+ "\xac\xff\xd9\x0e\x98\xe2\xe6\x0d\x25\x12\xab\x8a\xbf\xdf\xce\xc5"
78
+ },
79
+ {
80
+ "123456789012345678901234567890123456789012345678901234567890"
81
+ "12345678901234567890",
82
+ "\xad\x81\x80\x53\x21\xf3\xe6\x9d\x25\x12\x35\xbf\x88\x6a\x56\x48"
83
+ "\x44\x87\x3b\x56\xdd\x7d\xde\x40\x0f\x05\x5b\x7d\xde\x39\x30\x7a"
84
+ }
85
+ };
86
+
87
+
88
+ static void
89
+ report_failure(int n, const char *tag,
90
+ const char expected[32], uint8_t actual[32])
91
+ {
92
+ int i;
93
+ printf ("FAIL: test %d (%s):\n exp:", n, tag);
94
+ for (i = 0; i < 32; i++)
95
+ {
96
+ if (i % 4 == 0)
97
+ putchar (' ');
98
+ printf ("%02x", (unsigned int)(unsigned char)expected[i]);
99
+ }
100
+ printf ("\n got:");
101
+ for (i = 0; i < 32; i++)
102
+ {
103
+ if (i % 4 == 0)
104
+ putchar (' ');
105
+ printf ("%02x", (unsigned int)(unsigned char)actual[i]);
106
+ }
107
+ putchar ('\n');
108
+ putchar ('\n');
109
+ }
110
+
111
+ int
112
+ main (void)
113
+ {
114
+ sm3_ctx ctx;
115
+ uint8_t sum[32];
116
+ int result = 0;
117
+ int cnt;
118
+ int i;
119
+
120
+ for (cnt = 0; cnt < (int) ARRAY_SIZE (tests); ++cnt)
121
+ {
122
+ sm3_buf (tests[cnt].input, strlen (tests[cnt].input), sum);
123
+ if (memcmp (tests[cnt].result, sum, 32) != 0)
124
+ {
125
+ report_failure (cnt, "all at once", tests[cnt].result, sum);
126
+ result = 1;
127
+ }
128
+
129
+ sm3_init (&ctx);
130
+ for (i = 0; tests[cnt].input[i] != '\0'; ++i)
131
+ sm3_update (&ctx, &tests[cnt].input[i], 1);
132
+ sm3_final (sum, &ctx);
133
+ if (memcmp (tests[cnt].result, sum, 32) != 0)
134
+ {
135
+ report_failure (cnt, "byte by byte", tests[cnt].result, sum);
136
+ result = 1;
137
+ }
138
+ }
139
+
140
+ /* Test vector from FIPS 180-2: appendix B.3. */
141
+ char buf[1000];
142
+ memset (buf, 'a', sizeof (buf));
143
+ sm3_init (&ctx);
144
+ for (i = 0; i < 1000; ++i)
145
+ sm3_update (&ctx, buf, sizeof (buf));
146
+ sm3_final (sum, &ctx);
147
+ static const char expected[33] =
148
+ "\xc8\xaa\xf8\x94\x29\x55\x40\x29\xe2\x31\x94\x1a\x2a\xcc\x0a\xd6"
149
+ "\x1f\xf2\xa5\xac\xd8\xfa\xdd\x25\x84\x7a\x3a\x73\x2b\x3b\x02\xc3";
150
+
151
+ if (memcmp (expected, sum, 32) != 0)
152
+ {
153
+ report_failure (cnt, "block by block", expected, sum);
154
+ result = 1;
155
+ }
156
+
157
+ return result;
158
+ }
159
+
160
+ #else
161
+
162
+ int
163
+ main (void)
164
+ {
165
+ return 77; /* UNSUPPORTED */
166
+ }
167
+
168
+ #endif
@@ -0,0 +1,466 @@
1
+ /*-
2
+ * Copyright 2013-2018 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
+ #include "crypt-port.h"
22
+
23
+ #if INCLUDE_yescrypt || INCLUDE_scrypt || INCLUDE_gost_yescrypt || \
24
+ INCLUDE_sm3_yescrypt
25
+
26
+ #pragma GCC diagnostic ignored "-Wconversion"
27
+ #pragma GCC diagnostic ignored "-Wsign-conversion"
28
+ #pragma GCC diagnostic ignored "-Wcast-qual"
29
+
30
+ #include <stdio.h>
31
+ #include <string.h>
32
+
33
+ #define YESCRYPT_FLAGS YESCRYPT_DEFAULTS
34
+ #if 1
35
+ #define YESCRYPT_P 11
36
+ #define YESCRYPT_PROM 8
37
+ #else
38
+ #define YESCRYPT_P 1
39
+ #define YESCRYPT_PROM 1
40
+ #endif
41
+
42
+ #undef TEST_PBKDF2_SHA256
43
+ #define TEST_SCRYPT
44
+ #define TEST_YESCRYPT_KDF
45
+ #define TEST_YESCRYPT_ENCODING
46
+ #define TEST_ROM
47
+ #define TEST_ROM_PREALLOC
48
+
49
+ static int retval = 0;
50
+
51
+ #ifdef TEST_ROM_PREALLOC
52
+ #include <stdlib.h> /* for malloc() */
53
+ #endif
54
+
55
+ #ifdef TEST_PBKDF2_SHA256
56
+ #include <assert.h>
57
+
58
+ #include "alg-sha256.h"
59
+
60
+ static void print_PBKDF2_SHA256_raw(const char *passwd, size_t passwdlen,
61
+ const char *salt, size_t saltlen, uint64_t c, size_t dkLen)
62
+ {
63
+ uint8_t dk[64];
64
+ size_t i;
65
+
66
+ assert(dkLen <= sizeof(dk));
67
+
68
+ /* XXX This prints the strings truncated at first NUL */
69
+ printf("PBKDF2_SHA256(\"%s\", \"%s\", %llu, %llu) =",
70
+ passwd, salt, (unsigned long long)c, (unsigned long long)dkLen);
71
+
72
+ PBKDF2_SHA256((const uint8_t *) passwd, passwdlen,
73
+ (const uint8_t *) salt, saltlen, c, dk, dkLen);
74
+
75
+ for (i = 0; i < dkLen; i++)
76
+ printf(" %02x", dk[i]);
77
+ puts("");
78
+ }
79
+
80
+ static void print_PBKDF2_SHA256(const char *passwd,
81
+ const char *salt, uint64_t c, size_t dkLen)
82
+ {
83
+ print_PBKDF2_SHA256_raw(passwd, strlen(passwd), salt, strlen(salt), c,
84
+ dkLen);
85
+ }
86
+ #endif
87
+
88
+ #if defined(TEST_SCRYPT) || defined(TEST_YESCRYPT_ENCODING)
89
+ #include "alg-yescrypt.h"
90
+ #endif
91
+
92
+ #ifdef TEST_SCRYPT
93
+ static void print_scrypt(const char *passwd, const char *salt,
94
+ uint64_t N, uint32_t r, uint32_t p)
95
+ {
96
+ uint8_t dk[64];
97
+ size_t i;
98
+
99
+ printf("scrypt(\"%s\", \"%s\", %llu, %u, %u) =",
100
+ passwd, salt, (unsigned long long)N, r, p);
101
+
102
+ if (crypto_scrypt((const uint8_t *) passwd, strlen(passwd),
103
+ (const uint8_t *) salt, strlen(salt), N, r, p, dk, sizeof(dk))) {
104
+ puts(" FAILED");
105
+ retval = 1;
106
+ return;
107
+ }
108
+
109
+ for (i = 0; i < sizeof(dk); i++)
110
+ printf(" %02x", dk[i]);
111
+ puts("");
112
+ }
113
+ #endif
114
+
115
+ #ifdef TEST_YESCRYPT_KDF
116
+ static void print_yescrypt(const char *passwd, const char *salt,
117
+ yescrypt_flags_t flags,
118
+ uint64_t N, uint32_t r, uint32_t p, uint32_t t, uint32_t g,
119
+ uint32_t dklen)
120
+ {
121
+ yescrypt_local_t local;
122
+ yescrypt_params_t params = {flags, N, r, p, t, g, 0};
123
+ uint8_t dk[64];
124
+ uint32_t i;
125
+
126
+ #if 1
127
+ /* Don't test hash upgrades */
128
+ if (g)
129
+ return;
130
+ #endif
131
+
132
+ if (dklen > sizeof(dk) || yescrypt_init_local(&local)) {
133
+ puts("FAILED");
134
+ retval = 1;
135
+ return;
136
+ }
137
+
138
+ printf("yescrypt(\"%s\", \"%s\", %u, %llu, %u, %u, %u, %u) =",
139
+ passwd, salt, flags, (unsigned long long)N, r, p, t, g);
140
+
141
+ if (yescrypt_kdf(NULL, &local,
142
+ (const uint8_t *) passwd, strlen(passwd),
143
+ (const uint8_t *) salt, strlen(salt), &params, dk, dklen)) {
144
+ yescrypt_free_local(&local);
145
+ puts(" FAILED");
146
+ retval = 1;
147
+ return;
148
+ }
149
+
150
+ yescrypt_free_local(&local);
151
+
152
+ for (i = 0; i < dklen; i++)
153
+ printf(" %02x", dk[i]);
154
+ puts("");
155
+ }
156
+ #endif
157
+
158
+ int main(void)
159
+ {
160
+ int i;
161
+
162
+ setvbuf(stdout, NULL, _IOLBF, 0);
163
+
164
+ #ifdef TEST_PBKDF2_SHA256
165
+ print_PBKDF2_SHA256("password", "salt", 1, 20);
166
+ print_PBKDF2_SHA256("password", "salt", 2, 20);
167
+ print_PBKDF2_SHA256("password", "salt", 4096, 20);
168
+ print_PBKDF2_SHA256("password", "salt", 16777216, 20);
169
+ print_PBKDF2_SHA256("passwordPASSWORDpassword",
170
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096, 25);
171
+ print_PBKDF2_SHA256_raw("pass\0word", 9, "sa\0lt", 5, 4096, 16);
172
+ #if 0
173
+ print_PBKDF2_SHA256("password", "salt", 1, 32);
174
+ print_PBKDF2_SHA256("password", "salt", 2, 32);
175
+ print_PBKDF2_SHA256("password", "salt", 4096, 32);
176
+ print_PBKDF2_SHA256("password", "salt", 16777216, 32);
177
+ print_PBKDF2_SHA256("passwordPASSWORDpassword",
178
+ "saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096, 40);
179
+ print_PBKDF2_SHA256("password", "salt", 4096, 16);
180
+ print_PBKDF2_SHA256("password", "salt", 1, 20);
181
+ print_PBKDF2_SHA256("password", "salt", 2, 20);
182
+ print_PBKDF2_SHA256("password", "salt", 4096, 20);
183
+ print_PBKDF2_SHA256("password", "salt", 16777216, 20);
184
+ print_PBKDF2_SHA256("password", "salt", 4096, 25);
185
+ print_PBKDF2_SHA256("password", "salt", 4096, 16);
186
+ #endif
187
+ #endif
188
+
189
+ #ifdef TEST_SCRYPT
190
+ print_scrypt("", "", 16, 1, 1);
191
+ print_scrypt("password", "NaCl", 1024, 8, 16);
192
+ print_scrypt("pleaseletmein", "SodiumChloride", 16384, 8, 1);
193
+ print_scrypt("pleaseletmein", "SodiumChloride", 1048576, 8, 1);
194
+ #endif
195
+
196
+ #ifdef TEST_YESCRYPT_KDF
197
+ print_yescrypt("", "", 0, 16, 1, 1, 0, 0, 64);
198
+ print_yescrypt("", "", 0, 16, 1, 1, 0, 0, 8);
199
+ print_yescrypt("", "", 0, 4, 1, 1, 0, 0, 64);
200
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 0, 0, 64);
201
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 0, 0, 8);
202
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 1, 0, 64);
203
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 2, 0, 64);
204
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 3, 0, 64);
205
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 3, 0, 33);
206
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 3, 0, 32);
207
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 3, 0, 31);
208
+ print_yescrypt("", "", YESCRYPT_WORM, 4, 1, 1, 3, 0, 1);
209
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 0, 0, 64);
210
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 0, 0, 4);
211
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 1, 0, 64);
212
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 1, 0, 33);
213
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 1, 0, 32);
214
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 1, 0, 31);
215
+ print_yescrypt("", "", YESCRYPT_DEFAULTS, 4, 1, 1, 1, 0, 1);
216
+ for (i = 0; i <= 6; i++)
217
+ print_yescrypt("p", "s", YESCRYPT_DEFAULTS, 16, 8, 1, i + 10, i, 40);
218
+ for (i = 0; i <= 6; i++)
219
+ print_yescrypt("p", "s", YESCRYPT_WORM, 16, 8, 1, i + 10, i, 40);
220
+ for (i = 0; i <= 6; i++)
221
+ print_yescrypt("p", "s", YESCRYPT_DEFAULTS, 16, 8, 1, 0, i, 40);
222
+ for (i = 0; i <= 6; i++)
223
+ print_yescrypt("p", "s", YESCRYPT_WORM, 16, 8, 1, 0, i, 40);
224
+ for (i = 0; i <= 2; i++)
225
+ print_yescrypt("p", "s", YESCRYPT_DEFAULTS, 16, 8, 1, 0, i, 32);
226
+ for (i = 0; i <= 2; i++)
227
+ print_yescrypt("p", "s", YESCRYPT_DEFAULTS, 16, 8, 1, 0, i, 8);
228
+ #endif
229
+
230
+ #ifdef TEST_YESCRYPT_ENCODING
231
+ {
232
+ uint8_t *setting;
233
+ yescrypt_binary_t key = {.uc={
234
+ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
235
+ 17,18,19,20,21,22,23,24,25,26,27,28,255,128,64,32}};
236
+
237
+ for (i = 0; i < 18; i++) {
238
+ uint32_t N_log2 = (i < 14) ? (16 - i) : 2;
239
+ uint32_t r = (i < 8) ? (8 - i) : (1 + (i & 1));
240
+ uint32_t p = (i & 1) ? 1 : YESCRYPT_P;
241
+ yescrypt_flags_t flags = YESCRYPT_DEFAULTS;
242
+ if ((int)p - (i / 2) > 1)
243
+ p -= i / 2;
244
+ if (i & 2) {
245
+ flags = YESCRYPT_WORM;
246
+ } else {
247
+ while ((1ULL << N_log2) / p <= 3)
248
+ N_log2++;
249
+ }
250
+ yescrypt_params_t params =
251
+ {flags, (uint64_t)1 << N_log2, r, p, 0, 0, 0};
252
+ setting = yescrypt_encode_params(&params,
253
+ (const uint8_t *)"WZaPV7LSUEKMo34.", 16 - (i & 15));
254
+ if (setting && i == 0)
255
+ printf("'%s'\n", (char *)setting);
256
+ if (!setting) {
257
+ printf("%d yescrypt_encode_params() = NULL\n", i);
258
+ retval = 1;
259
+ }
260
+ if (setting) {
261
+ uint8_t *hash = yescrypt(
262
+ (const uint8_t *)"pleaseletmein", setting);
263
+ printf("Plaintext: '%s'\n", (char *)hash);
264
+ hash = (uint8_t *)strdup((char *)hash);
265
+ if (!hash || strcmp(
266
+ (char *)hash, (char *)yescrypt(
267
+ (const uint8_t *)"pleaseletmein", hash))) {
268
+ puts("Validation FAILED");
269
+ retval = 1;
270
+ }
271
+ uint8_t *orig = (uint8_t *)strdup((char *)hash);
272
+ if (!yescrypt_reencrypt(hash, NULL, &key)) {
273
+ printf("%d yescrypt_reencrypt() = NULL\n", i);
274
+ retval = 1;
275
+ }
276
+ printf("Encrypted: '%s'\n", (char *)hash);
277
+ yescrypt_local_t local;
278
+ if (yescrypt_init_local(&local)) {
279
+ puts("yescrypt_init_local() FAILED");
280
+ retval = 1;
281
+ }
282
+ uint8_t buf[128];
283
+ if (strcmp((char *)hash, (char *)yescrypt_r(
284
+ NULL, &local,
285
+ (const uint8_t *)"pleaseletmein", 13,
286
+ hash, &key, buf, sizeof(buf)))) {
287
+ puts("Validation of encrypted FAILED");
288
+ retval = 1;
289
+ }
290
+ if (!strcmp((char *)hash, (char *)yescrypt_r(
291
+ NULL, &local,
292
+ (const uint8_t *)"pleaseletmein", 13,
293
+ hash, NULL, buf, sizeof(buf)))) {
294
+ puts("Validation of encrypted "
295
+ "unexpectedly succeeded");
296
+ retval = 1;
297
+ }
298
+ if (!strcmp((char *)orig, (char *)yescrypt_r(
299
+ NULL, &local,
300
+ (const uint8_t *)"pleaseletmein", 13,
301
+ orig, &key, buf, sizeof(buf)))) {
302
+ puts("Validation of unencrypted "
303
+ "unexpectedly succeeded");
304
+ retval = 1;
305
+ }
306
+ yescrypt_free_local(&local);
307
+ if (!yescrypt_reencrypt(hash, &key, NULL)) {
308
+ printf("%d yescrypt_reencrypt() = NULL\n", i);
309
+ retval = 1;
310
+ }
311
+ if (strcmp((char *)hash, (char *)orig)) {
312
+ puts("Decryption FAILED");
313
+ retval = 1;
314
+ }
315
+ free(orig);
316
+ free(hash);
317
+ }
318
+ }
319
+
320
+ printf("'%s'\n", (char *)yescrypt(
321
+ (const uint8_t *)"pleaseletmein",
322
+ (const uint8_t *)"$7$C6..../....SodiumChloride"));
323
+
324
+ printf("'%s'\n", (char *)yescrypt(
325
+ (const uint8_t *)"pleaseletmein",
326
+ (const uint8_t *)"$7$06..../....SodiumChloride"));
327
+
328
+ #ifdef TEST_ROM
329
+ uint64_t rom_bytes = 256 * (1024ULL*1024);
330
+ uint64_t ram_bytes = 2 * (1024ULL*1024);
331
+ uint32_t r;
332
+ uint64_t NROM_log2, N_log2;
333
+ yescrypt_shared_t shared;
334
+ yescrypt_local_t local;
335
+
336
+ NROM_log2 = 0;
337
+ while (((rom_bytes >> NROM_log2) & 0xff) == 0)
338
+ NROM_log2++;
339
+ r = rom_bytes >> (7 + NROM_log2);
340
+ while (r < 5 && NROM_log2 > 0) {
341
+ r <<= 1;
342
+ NROM_log2--;
343
+ }
344
+ rom_bytes = (uint64_t)r << (7 + NROM_log2);
345
+
346
+ N_log2 = 0;
347
+ while (((uint64_t)r << (7 + N_log2)) < ram_bytes)
348
+ N_log2++;
349
+ ram_bytes = (uint64_t)r << (7 + N_log2);
350
+
351
+ printf("r=%u N=2^%u NROM=2^%u\n", r,
352
+ (unsigned int)N_log2, (unsigned int)NROM_log2);
353
+
354
+ printf("Will use %.2f KiB ROM\n", rom_bytes / 1024.0);
355
+ printf(" %.2f KiB RAM\n", ram_bytes / 1024.0);
356
+
357
+ printf("Initializing ROM ...");
358
+ fflush(stdout);
359
+ yescrypt_params_t rom_params = { YESCRYPT_DEFAULTS,
360
+ 0, r, YESCRYPT_PROM, 0, 0, (uint64_t)1 << NROM_log2 };
361
+ if (yescrypt_init_shared(&shared,
362
+ (uint8_t *)"local param", 12, &rom_params)) {
363
+ puts(" FAILED");
364
+ retval = 1;
365
+ }
366
+ yescrypt_binary_t *digest = yescrypt_digest_shared(&shared);
367
+ printf(" DONE (%02x%02x%02x%02x)\n",
368
+ digest->uc[0], digest->uc[1], digest->uc[2], digest->uc[3]);
369
+
370
+ if (yescrypt_init_local(&local)) {
371
+ puts("FAILED");
372
+ retval = 1;
373
+ }
374
+
375
+ yescrypt_params_t params = rom_params;
376
+ params.flags = YESCRYPT_FLAGS;
377
+ params.N = (uint64_t)1 << N_log2;
378
+ params.p = YESCRYPT_P;
379
+ setting = yescrypt_encode_params(&params,
380
+ (const uint8_t *)"WZaPV7LSUEKMo34.", 16);
381
+ printf("'%s'\n", (char *)setting);
382
+
383
+ uint8_t hash[128];
384
+
385
+ printf("'%s'\n", (char *)yescrypt_r(&shared, &local,
386
+ (const uint8_t *)"pleaseletmein", 13, setting, NULL,
387
+ hash, sizeof(hash)));
388
+
389
+ #ifdef TEST_ROM_PREALLOC
390
+ yescrypt_free_shared(&shared);
391
+
392
+ shared.aligned_size = ((uint64_t)1 << NROM_log2) * 128 * r;
393
+ shared.base_size = shared.aligned_size + 63;
394
+ uint8_t *where = shared.base = malloc(shared.base_size);
395
+ where += 63;
396
+ where = shared.aligned = where - ((uintptr_t)where & 63);
397
+
398
+ printf("Initializing ROM in preallocated memory ...");
399
+ fflush(stdout);
400
+ rom_params.flags |= YESCRYPT_SHARED_PREALLOCATED;
401
+ if (yescrypt_init_shared(&shared,
402
+ (uint8_t *)"local param", 12, &rom_params)) {
403
+ puts(" FAILED");
404
+ retval = 1;
405
+ }
406
+ digest = yescrypt_digest_shared(&shared);
407
+ printf(" DONE (%02x%02x%02x%02x)\n",
408
+ digest->uc[0], digest->uc[1], digest->uc[2], digest->uc[3]);
409
+
410
+ if ((void *)where != shared.aligned) {
411
+ puts("YESCRYPT_SHARED_PREALLOCATED failed");
412
+ retval = 1;
413
+ }
414
+
415
+ #endif
416
+
417
+ printf("'%s'\n", (char *)yescrypt_r(&shared, &local,
418
+ (const uint8_t *)"pleaseletmein", 13, setting, NULL,
419
+ hash, sizeof(hash)));
420
+
421
+ printf("'%s'\n", (char *)yescrypt_r(&shared, &local,
422
+ (const uint8_t *)"pleaseletmeIn", 13, setting, NULL,
423
+ hash, sizeof(hash)));
424
+
425
+ setting = yescrypt_encode_params(&params,
426
+ (const uint8_t *)"WZaPV7LSUEIMo34.", 16);
427
+
428
+ printf("'%s'\n", (char *)yescrypt_r(&shared, &local,
429
+ (const uint8_t *)"pleaseletmein", 13, setting, NULL,
430
+ hash, sizeof(hash)));
431
+
432
+ printf("'%s'\n", (char *)yescrypt_r(&shared, &local,
433
+ (const uint8_t *)"pleaseletmeIn", 13, setting, NULL,
434
+ hash, sizeof(hash)));
435
+
436
+ params.N = 4;
437
+ params.NROM *= params.r;
438
+ params.r = 1;
439
+ params.p = 1;
440
+ setting = yescrypt_encode_params(&params,
441
+ (const uint8_t *)"WZaPV7LSUEKMo34.", 16);
442
+
443
+ printf("'%s'\n", (char *)yescrypt_r(&shared, &local,
444
+ (const uint8_t *)"pleaseletmein", 13, setting, NULL,
445
+ hash, sizeof(hash)));
446
+
447
+ free(shared.base); /* has been malloc'ed */
448
+ shared.base = NULL;
449
+ yescrypt_free_shared(&shared);
450
+ #endif
451
+ }
452
+ #endif
453
+
454
+ return retval;
455
+ }
456
+
457
+ #else
458
+
459
+ int
460
+ main (void)
461
+ {
462
+ return 77; /* UNSUPPORTED */
463
+ }
464
+
465
+ #endif /* INCLUDE_yescrypt || INCLUDE_scrypt || INCLUDE_gost_yescrypt ||
466
+ INCLUDE_sm3_yescrypt */