ffi-hydrogen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
@@ -0,0 +1,317 @@
1
+ #ifndef hydrogen_H
2
+ #define hydrogen_H
3
+
4
+ #include <stdbool.h>
5
+ #include <stdint.h>
6
+ #include <stdlib.h>
7
+
8
+ #ifdef __cplusplus
9
+ # ifdef __GNUC__
10
+ # pragma GCC diagnostic ignored "-Wlong-long"
11
+ # endif
12
+ extern "C" {
13
+ #endif
14
+
15
+ #if defined(__clang__) || defined(__GNUC__)
16
+ # define _hydro_attr_(X) __attribute__(X)
17
+ #else
18
+ # define _hydro_attr_(X)
19
+ #endif
20
+ #define _hydro_attr_deprecated_ _hydro_attr_((deprecated))
21
+ #define _hydro_attr_malloc_ _hydro_attr_((malloc))
22
+ #define _hydro_attr_noinline_ _hydro_attr_((noinline))
23
+ #define _hydro_attr_noreturn_ _hydro_attr_((noreturn))
24
+ #define _hydro_attr_warn_unused_result_ _hydro_attr_((warn_unused_result))
25
+ #define _hydro_attr_weak_ _hydro_attr_((weak))
26
+
27
+ #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
28
+ # define _hydro_attr_aligned_(X) __declspec(align(X))
29
+ #elif defined(__clang__) || defined(__GNUC__)
30
+ # define _hydro_attr_aligned_(X) _hydro_attr_((aligned(X)))
31
+ #else
32
+ # define _hydro_attr_aligned_(X)
33
+ #endif
34
+
35
+ #define HYDRO_VERSION_MAJOR 1
36
+ #define HYDRO_VERSION_MINOR 0
37
+
38
+ int hydro_init(void);
39
+
40
+ /* ---------------- */
41
+
42
+ #define hydro_random_SEEDBYTES 32
43
+
44
+ uint32_t hydro_random_u32(void);
45
+
46
+ uint32_t hydro_random_uniform(const uint32_t upper_bound);
47
+
48
+ void hydro_random_buf(void *out, size_t out_len);
49
+
50
+ void hydro_random_buf_deterministic(void *out, size_t out_len,
51
+ const uint8_t seed[hydro_random_SEEDBYTES]);
52
+
53
+ void hydro_random_ratchet(void);
54
+
55
+ void hydro_random_reseed(void);
56
+
57
+ /* ---------------- */
58
+
59
+ #define hydro_hash_BYTES 32
60
+ #define hydro_hash_BYTES_MAX 65535
61
+ #define hydro_hash_BYTES_MIN 16
62
+ #define hydro_hash_CONTEXTBYTES 8
63
+ #define hydro_hash_KEYBYTES 32
64
+
65
+ typedef struct hydro_hash_state {
66
+ uint32_t state[12];
67
+ uint8_t buf_off;
68
+ uint8_t align[3];
69
+ } hydro_hash_state;
70
+
71
+ void hydro_hash_keygen(uint8_t key[hydro_hash_KEYBYTES]);
72
+
73
+ int hydro_hash_init(hydro_hash_state *state, const char ctx[hydro_hash_CONTEXTBYTES],
74
+ const uint8_t key[hydro_hash_KEYBYTES]);
75
+
76
+ int hydro_hash_update(hydro_hash_state *state, const void *in_, size_t in_len);
77
+
78
+ int hydro_hash_final(hydro_hash_state *state, uint8_t *out, size_t out_len);
79
+
80
+ int hydro_hash_hash(uint8_t *out, size_t out_len, const void *in_, size_t in_len,
81
+ const char ctx[hydro_hash_CONTEXTBYTES],
82
+ const uint8_t key[hydro_hash_KEYBYTES]);
83
+
84
+ /* ---------------- */
85
+
86
+ #define hydro_secretbox_CONTEXTBYTES 8
87
+ #define hydro_secretbox_HEADERBYTES (20 + 16)
88
+ #define hydro_secretbox_KEYBYTES 32
89
+ #define hydro_secretbox_PROBEBYTES 16
90
+
91
+ void hydro_secretbox_keygen(uint8_t key[hydro_secretbox_KEYBYTES]);
92
+
93
+ int hydro_secretbox_encrypt(uint8_t *c, const void *m_, size_t mlen, uint64_t msg_id,
94
+ const char ctx[hydro_secretbox_CONTEXTBYTES],
95
+ const uint8_t key[hydro_secretbox_KEYBYTES]);
96
+
97
+ int hydro_secretbox_decrypt(void *m_, const uint8_t *c, size_t clen, uint64_t msg_id,
98
+ const char ctx[hydro_secretbox_CONTEXTBYTES],
99
+ const uint8_t key[hydro_secretbox_KEYBYTES])
100
+ _hydro_attr_warn_unused_result_;
101
+
102
+ void hydro_secretbox_probe_create(uint8_t probe[hydro_secretbox_PROBEBYTES], const uint8_t *c,
103
+ size_t c_len, const char ctx[hydro_secretbox_CONTEXTBYTES],
104
+ const uint8_t key[hydro_secretbox_KEYBYTES]);
105
+
106
+ int hydro_secretbox_probe_verify(const uint8_t probe[hydro_secretbox_PROBEBYTES], const uint8_t *c,
107
+ size_t c_len, const char ctx[hydro_secretbox_CONTEXTBYTES],
108
+ const uint8_t key[hydro_secretbox_KEYBYTES])
109
+ _hydro_attr_warn_unused_result_;
110
+
111
+ /* ---------------- */
112
+
113
+ #define hydro_kdf_CONTEXTBYTES 8
114
+ #define hydro_kdf_KEYBYTES 32
115
+ #define hydro_kdf_BYTES_MAX 65535
116
+ #define hydro_kdf_BYTES_MIN 16
117
+
118
+ void hydro_kdf_keygen(uint8_t key[hydro_kdf_KEYBYTES]);
119
+
120
+ int hydro_kdf_derive_from_key(uint8_t *subkey, size_t subkey_len, uint64_t subkey_id,
121
+ const char ctx[hydro_kdf_CONTEXTBYTES],
122
+ const uint8_t key[hydro_kdf_KEYBYTES]);
123
+
124
+ /* ---------------- */
125
+
126
+ #define hydro_sign_BYTES 64
127
+ #define hydro_sign_CONTEXTBYTES 8
128
+ #define hydro_sign_PUBLICKEYBYTES 32
129
+ #define hydro_sign_SECRETKEYBYTES 64
130
+ #define hydro_sign_SEEDBYTES 32
131
+
132
+ typedef struct hydro_sign_state {
133
+ hydro_hash_state hash_st;
134
+ } hydro_sign_state;
135
+
136
+ typedef struct hydro_sign_keypair {
137
+ uint8_t pk[hydro_sign_PUBLICKEYBYTES];
138
+ uint8_t sk[hydro_sign_SECRETKEYBYTES];
139
+ } hydro_sign_keypair;
140
+
141
+ void hydro_sign_keygen(hydro_sign_keypair *kp);
142
+
143
+ void hydro_sign_keygen_deterministic(hydro_sign_keypair *kp,
144
+ const uint8_t seed[hydro_sign_SEEDBYTES]);
145
+
146
+ int hydro_sign_init(hydro_sign_state *state, const char ctx[hydro_sign_CONTEXTBYTES]);
147
+
148
+ int hydro_sign_update(hydro_sign_state *state, const void *m_, size_t mlen);
149
+
150
+ int hydro_sign_final_create(hydro_sign_state *state, uint8_t csig[hydro_sign_BYTES],
151
+ const uint8_t sk[hydro_sign_SECRETKEYBYTES]);
152
+
153
+ int hydro_sign_final_verify(hydro_sign_state *state, const uint8_t csig[hydro_sign_BYTES],
154
+ const uint8_t pk[hydro_sign_PUBLICKEYBYTES])
155
+ _hydro_attr_warn_unused_result_;
156
+
157
+ int hydro_sign_create(uint8_t csig[hydro_sign_BYTES], const void *m_, size_t mlen,
158
+ const char ctx[hydro_sign_CONTEXTBYTES],
159
+ const uint8_t sk[hydro_sign_SECRETKEYBYTES]);
160
+
161
+ int hydro_sign_verify(const uint8_t csig[hydro_sign_BYTES], const void *m_, size_t mlen,
162
+ const char ctx[hydro_sign_CONTEXTBYTES],
163
+ const uint8_t pk[hydro_sign_PUBLICKEYBYTES]) _hydro_attr_warn_unused_result_;
164
+
165
+ /* ---------------- */
166
+
167
+ #define hydro_kx_SESSIONKEYBYTES 32
168
+ #define hydro_kx_PUBLICKEYBYTES 32
169
+ #define hydro_kx_SECRETKEYBYTES 32
170
+ #define hydro_kx_PSKBYTES 32
171
+ #define hydro_kx_SEEDBYTES 32
172
+
173
+ typedef struct hydro_kx_keypair {
174
+ uint8_t pk[hydro_kx_PUBLICKEYBYTES];
175
+ uint8_t sk[hydro_kx_SECRETKEYBYTES];
176
+ } hydro_kx_keypair;
177
+
178
+ typedef struct hydro_kx_session_keypair {
179
+ uint8_t rx[hydro_kx_SESSIONKEYBYTES];
180
+ uint8_t tx[hydro_kx_SESSIONKEYBYTES];
181
+ } hydro_kx_session_keypair;
182
+
183
+ typedef struct hydro_kx_state {
184
+ hydro_kx_keypair eph_kp;
185
+ uint8_t h[32];
186
+ uint8_t ck[32];
187
+ uint8_t k[32];
188
+ } hydro_kx_state;
189
+
190
+ void hydro_kx_keygen(hydro_kx_keypair *static_kp);
191
+
192
+ void hydro_kx_keygen_deterministic(hydro_kx_keypair *static_kp,
193
+ const uint8_t seed[hydro_kx_SEEDBYTES]);
194
+
195
+ /* NOISE_N */
196
+
197
+ #define hydro_kx_N_PACKET1BYTES 32
198
+
199
+ int hydro_kx_n_1(hydro_kx_session_keypair *kp, uint8_t packet1[hydro_kx_N_PACKET1BYTES],
200
+ const uint8_t psk[hydro_kx_PSKBYTES],
201
+ const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES]);
202
+
203
+ int hydro_kx_n_2(hydro_kx_session_keypair *kp, const uint8_t packet1[hydro_kx_N_PACKET1BYTES],
204
+ const uint8_t psk[hydro_kx_PSKBYTES], const hydro_kx_keypair *static_kp);
205
+
206
+ /* NOISE_KK */
207
+
208
+ #define hydro_kx_KK_PACKET1BYTES 32
209
+ #define hydro_kx_KK_PACKET2BYTES 32
210
+
211
+ int hydro_kx_kk_1(hydro_kx_state *state, uint8_t packet1[hydro_kx_KK_PACKET1BYTES],
212
+ const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES],
213
+ const hydro_kx_keypair *static_kp);
214
+
215
+ int hydro_kx_kk_2(hydro_kx_session_keypair *kp, uint8_t packet2[hydro_kx_KK_PACKET2BYTES],
216
+ const uint8_t packet1[hydro_kx_KK_PACKET1BYTES],
217
+ const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES],
218
+ const hydro_kx_keypair *static_kp);
219
+
220
+ int hydro_kx_kk_3(hydro_kx_state *state, hydro_kx_session_keypair *kp,
221
+ const uint8_t packet2[hydro_kx_KK_PACKET2BYTES],
222
+ const uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES]);
223
+
224
+ /* NOISE_XX */
225
+
226
+ #define hydro_kx_XX_PACKET1BYTES 32
227
+ #define hydro_kx_XX_PACKET2BYTES 80
228
+ #define hydro_kx_XX_PACKET3BYTES 48
229
+
230
+ int hydro_kx_xx_1(hydro_kx_state *state, uint8_t packet1[hydro_kx_XX_PACKET1BYTES],
231
+ const uint8_t psk[hydro_kx_PSKBYTES]);
232
+
233
+ int hydro_kx_xx_2(hydro_kx_state *state, uint8_t packet2[hydro_kx_XX_PACKET2BYTES],
234
+ const uint8_t packet1[hydro_kx_XX_PACKET1BYTES],
235
+ const uint8_t psk[hydro_kx_PSKBYTES], const hydro_kx_keypair *static_kp);
236
+
237
+ int hydro_kx_xx_3(hydro_kx_state *state, hydro_kx_session_keypair *kp,
238
+ uint8_t packet3[hydro_kx_XX_PACKET3BYTES],
239
+ uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES],
240
+ const uint8_t packet2[hydro_kx_XX_PACKET2BYTES],
241
+ const uint8_t psk[hydro_kx_PSKBYTES], const hydro_kx_keypair *static_kp);
242
+
243
+ int hydro_kx_xx_4(hydro_kx_state *state, hydro_kx_session_keypair *kp,
244
+ uint8_t peer_static_pk[hydro_kx_PUBLICKEYBYTES],
245
+ const uint8_t packet3[hydro_kx_XX_PACKET3BYTES],
246
+ const uint8_t psk[hydro_kx_PSKBYTES]);
247
+
248
+ /* ---------------- */
249
+
250
+ #define hydro_pwhash_CONTEXTBYTES 8
251
+ #define hydro_pwhash_MASTERKEYBYTES 32
252
+ #define hydro_pwhash_STOREDBYTES 128
253
+
254
+ void hydro_pwhash_keygen(uint8_t master_key[hydro_pwhash_MASTERKEYBYTES]);
255
+
256
+ int hydro_pwhash_deterministic(uint8_t *h, size_t h_len, const char *passwd, size_t passwd_len,
257
+ const char ctx[hydro_pwhash_CONTEXTBYTES],
258
+ const uint8_t master_key[hydro_pwhash_MASTERKEYBYTES],
259
+ uint64_t opslimit, size_t memlimit, uint8_t threads);
260
+
261
+ int hydro_pwhash_create(uint8_t stored[hydro_pwhash_STOREDBYTES], const char *passwd,
262
+ size_t passwd_len, const uint8_t master_key[hydro_pwhash_MASTERKEYBYTES],
263
+ uint64_t opslimit, size_t memlimit, uint8_t threads);
264
+
265
+ int hydro_pwhash_verify(const uint8_t stored[hydro_pwhash_STOREDBYTES], const char *passwd,
266
+ size_t passwd_len, const uint8_t master_key[hydro_pwhash_MASTERKEYBYTES],
267
+ uint64_t opslimit_max, size_t memlimit_max, uint8_t threads_max);
268
+
269
+ int hydro_pwhash_derive_static_key(uint8_t *static_key, size_t static_key_len,
270
+ const uint8_t stored[hydro_pwhash_STOREDBYTES],
271
+ const char *passwd, size_t passwd_len,
272
+ const char ctx[hydro_pwhash_CONTEXTBYTES],
273
+ const uint8_t master_key[hydro_pwhash_MASTERKEYBYTES],
274
+ uint64_t opslimit_max, size_t memlimit_max, uint8_t threads_max);
275
+
276
+ int hydro_pwhash_reencrypt(uint8_t stored[hydro_pwhash_STOREDBYTES],
277
+ const uint8_t master_key[hydro_pwhash_MASTERKEYBYTES],
278
+ const uint8_t new_master_key[hydro_pwhash_MASTERKEYBYTES]);
279
+
280
+ int hydro_pwhash_upgrade(uint8_t stored[hydro_pwhash_STOREDBYTES],
281
+ const uint8_t master_key[hydro_pwhash_MASTERKEYBYTES], uint64_t opslimit,
282
+ size_t memlimit, uint8_t threads);
283
+
284
+ /* ---------------- */
285
+
286
+ void hydro_memzero(void *pnt, size_t len);
287
+
288
+ void hydro_increment(uint8_t *n, size_t len);
289
+
290
+ bool hydro_equal(const void *b1_, const void *b2_, size_t len);
291
+
292
+ int hydro_compare(const uint8_t *b1_, const uint8_t *b2_, size_t len);
293
+
294
+ char *hydro_bin2hex(char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len);
295
+
296
+ int hydro_hex2bin(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len,
297
+ const char *ignore, const char **hex_end_p);
298
+
299
+ int hydro_pad(unsigned char *buf, size_t unpadded_buflen, size_t blocksize, size_t max_buflen);
300
+
301
+ int hydro_unpad(const unsigned char *buf, size_t padded_buflen, size_t blocksize);
302
+
303
+ /* ---------------- */
304
+
305
+ #define HYDRO_HWTYPE_ATMEGA328 1
306
+
307
+ #ifndef HYDRO_HWTYPE
308
+ # ifdef __AVR__
309
+ # define HYDRO_HWTYPE HYDRO_HWTYPE_ATMEGA328
310
+ # endif
311
+ #endif
312
+
313
+ #ifdef __cplusplus
314
+ }
315
+ #endif
316
+
317
+ #endif
@@ -0,0 +1,316 @@
1
+ #include <assert.h>
2
+ #include <errno.h>
3
+ #include <limits.h>
4
+ #include <stdbool.h>
5
+ #include <stdint.h>
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+
9
+ #include "../hydrogen.h"
10
+
11
+ #if !defined(__unix__) && (defined(__APPLE__) || defined(__linux__))
12
+ # define __unix__ 1
13
+ #endif
14
+ #ifndef __GNUC__
15
+ # define __restrict__
16
+ #endif
17
+
18
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
19
+ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
20
+ # define NATIVE_BIG_ENDIAN
21
+ #endif
22
+ #ifndef NATIVE_BIG_ENDIAN
23
+ # ifndef NATIVE_LITTLE_ENDIAN
24
+ # define NATIVE_LITTLE_ENDIAN
25
+ # endif
26
+ #endif
27
+
28
+ #ifndef TLS
29
+ # if defined(_WIN32) && !defined(__GNUC__)
30
+ # define TLS __declspec(thread)
31
+ # elif (defined(__clang__) || defined(__GNUC__)) && defined(__unix__)
32
+ # define TLS __thread
33
+ # else
34
+ # define TLS
35
+ # endif
36
+ #endif
37
+
38
+ #ifndef SIZE_MAX
39
+ # define SIZE_MAX ((size_t) -1)
40
+ #endif
41
+
42
+ #define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
43
+
44
+ #define ROTL32(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
45
+ #define ROTL64(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
46
+ #define ROTR32(x, b) (uint32_t)(((x) >> (b)) | ((x) << (32 - (b))))
47
+ #define ROTR64(x, b) (uint64_t)(((x) >> (b)) | ((x) << (64 - (b))))
48
+
49
+ #define LOAD64_LE(SRC) load64_le(SRC)
50
+ static inline uint64_t
51
+ load64_le(const uint8_t src[8])
52
+ {
53
+ #ifdef NATIVE_LITTLE_ENDIAN
54
+ uint64_t w;
55
+ memcpy(&w, src, sizeof w);
56
+ return w;
57
+ #else
58
+ uint64_t w = (uint64_t) src[0];
59
+ w |= (uint64_t) src[1] << 8;
60
+ w |= (uint64_t) src[2] << 16;
61
+ w |= (uint64_t) src[3] << 24;
62
+ w |= (uint64_t) src[4] << 32;
63
+ w |= (uint64_t) src[5] << 40;
64
+ w |= (uint64_t) src[6] << 48;
65
+ w |= (uint64_t) src[7] << 56;
66
+ return w;
67
+ #endif
68
+ }
69
+
70
+ #define STORE64_LE(DST, W) store64_le((DST), (W))
71
+ static inline void
72
+ store64_le(uint8_t dst[8], uint64_t w)
73
+ {
74
+ #ifdef NATIVE_LITTLE_ENDIAN
75
+ memcpy(dst, &w, sizeof w);
76
+ #else
77
+ dst[0] = (uint8_t) w;
78
+ w >>= 8;
79
+ dst[1] = (uint8_t) w;
80
+ w >>= 8;
81
+ dst[2] = (uint8_t) w;
82
+ w >>= 8;
83
+ dst[3] = (uint8_t) w;
84
+ w >>= 8;
85
+ dst[4] = (uint8_t) w;
86
+ w >>= 8;
87
+ dst[5] = (uint8_t) w;
88
+ w >>= 8;
89
+ dst[6] = (uint8_t) w;
90
+ w >>= 8;
91
+ dst[7] = (uint8_t) w;
92
+ #endif
93
+ }
94
+
95
+ #define LOAD32_LE(SRC) load32_le(SRC)
96
+ static inline uint32_t
97
+ load32_le(const uint8_t src[4])
98
+ {
99
+ #ifdef NATIVE_LITTLE_ENDIAN
100
+ uint32_t w;
101
+ memcpy(&w, src, sizeof w);
102
+ return w;
103
+ #else
104
+ uint32_t w = (uint32_t) src[0];
105
+ w |= (uint32_t) src[1] << 8;
106
+ w |= (uint32_t) src[2] << 16;
107
+ w |= (uint32_t) src[3] << 24;
108
+ return w;
109
+ #endif
110
+ }
111
+
112
+ #define STORE32_LE(DST, W) store32_le((DST), (W))
113
+ static inline void
114
+ store32_le(uint8_t dst[4], uint32_t w)
115
+ {
116
+ #ifdef NATIVE_LITTLE_ENDIAN
117
+ memcpy(dst, &w, sizeof w);
118
+ #else
119
+ dst[0] = (uint8_t) w;
120
+ w >>= 8;
121
+ dst[1] = (uint8_t) w;
122
+ w >>= 8;
123
+ dst[2] = (uint8_t) w;
124
+ w >>= 8;
125
+ dst[3] = (uint8_t) w;
126
+ #endif
127
+ }
128
+
129
+ #define LOAD16_LE(SRC) load16_le(SRC)
130
+ static inline uint16_t
131
+ load16_le(const uint8_t src[2])
132
+ {
133
+ #ifdef NATIVE_LITTLE_ENDIAN
134
+ uint16_t w;
135
+ memcpy(&w, src, sizeof w);
136
+ return w;
137
+ #else
138
+ uint16_t w = (uint16_t) src[0];
139
+ w |= (uint16_t) src[1] << 8;
140
+ return w;
141
+ #endif
142
+ }
143
+
144
+ #define STORE16_LE(DST, W) store16_le((DST), (W))
145
+ static inline void
146
+ store16_le(uint8_t dst[2], uint16_t w)
147
+ {
148
+ #ifdef NATIVE_LITTLE_ENDIAN
149
+ memcpy(dst, &w, sizeof w);
150
+ #else
151
+ dst[0] = (uint8_t) w;
152
+ w >>= 8;
153
+ dst[1] = (uint8_t) w;
154
+ #endif
155
+ }
156
+
157
+ /* ----- */
158
+
159
+ #define LOAD64_BE(SRC) load64_be(SRC)
160
+ static inline uint64_t
161
+ load64_be(const uint8_t src[8])
162
+ {
163
+ #ifdef NATIVE_BIG_ENDIAN
164
+ uint64_t w;
165
+ memcpy(&w, src, sizeof w);
166
+ return w;
167
+ #else
168
+ uint64_t w = (uint64_t) src[7];
169
+ w |= (uint64_t) src[6] << 8;
170
+ w |= (uint64_t) src[5] << 16;
171
+ w |= (uint64_t) src[4] << 24;
172
+ w |= (uint64_t) src[3] << 32;
173
+ w |= (uint64_t) src[2] << 40;
174
+ w |= (uint64_t) src[1] << 48;
175
+ w |= (uint64_t) src[0] << 56;
176
+ return w;
177
+ #endif
178
+ }
179
+
180
+ #define STORE64_BE(DST, W) store64_be((DST), (W))
181
+ static inline void
182
+ store64_be(uint8_t dst[8], uint64_t w)
183
+ {
184
+ #ifdef NATIVE_BIG_ENDIAN
185
+ memcpy(dst, &w, sizeof w);
186
+ #else
187
+ dst[7] = (uint8_t) w;
188
+ w >>= 8;
189
+ dst[6] = (uint8_t) w;
190
+ w >>= 8;
191
+ dst[5] = (uint8_t) w;
192
+ w >>= 8;
193
+ dst[4] = (uint8_t) w;
194
+ w >>= 8;
195
+ dst[3] = (uint8_t) w;
196
+ w >>= 8;
197
+ dst[2] = (uint8_t) w;
198
+ w >>= 8;
199
+ dst[1] = (uint8_t) w;
200
+ w >>= 8;
201
+ dst[0] = (uint8_t) w;
202
+ #endif
203
+ }
204
+
205
+ #define LOAD32_BE(SRC) load32_be(SRC)
206
+ static inline uint32_t
207
+ load32_be(const uint8_t src[4])
208
+ {
209
+ #ifdef NATIVE_BIG_ENDIAN
210
+ uint32_t w;
211
+ memcpy(&w, src, sizeof w);
212
+ return w;
213
+ #else
214
+ uint32_t w = (uint32_t) src[3];
215
+ w |= (uint32_t) src[2] << 8;
216
+ w |= (uint32_t) src[1] << 16;
217
+ w |= (uint32_t) src[0] << 24;
218
+ return w;
219
+ #endif
220
+ }
221
+
222
+ #define STORE32_BE(DST, W) store32_be((DST), (W))
223
+ static inline void
224
+ store32_be(uint8_t dst[4], uint32_t w)
225
+ {
226
+ #ifdef NATIVE_BIG_ENDIAN
227
+ memcpy(dst, &w, sizeof w);
228
+ #else
229
+ dst[3] = (uint8_t) w;
230
+ w >>= 8;
231
+ dst[2] = (uint8_t) w;
232
+ w >>= 8;
233
+ dst[1] = (uint8_t) w;
234
+ w >>= 8;
235
+ dst[0] = (uint8_t) w;
236
+ #endif
237
+ }
238
+
239
+ #define LOAD16_BE(SRC) load16_be(SRC)
240
+ static inline uint16_t
241
+ load16_be(const uint8_t src[2])
242
+ {
243
+ #ifdef NATIVE_BIG_ENDIAN
244
+ uint16_t w;
245
+ memcpy(&w, src, sizeof w);
246
+ return w;
247
+ #else
248
+ uint16_t w = (uint16_t) src[1];
249
+ w |= (uint16_t) src[0] << 8;
250
+ return w;
251
+ #endif
252
+ }
253
+
254
+ #define STORE16_BE(DST, W) store16_be((DST), (W))
255
+ static inline void
256
+ store16_be(uint8_t dst[2], uint16_t w)
257
+ {
258
+ #ifdef NATIVE_BIG_ENDIAN
259
+ memcpy(dst, &w, sizeof w);
260
+ #else
261
+ dst[1] = (uint8_t) w;
262
+ w >>= 8;
263
+ dst[0] = (uint8_t) w;
264
+ #endif
265
+ }
266
+
267
+ static inline void
268
+ mem_cpy(void *__restrict__ dst_, const void *__restrict__ src_, size_t n)
269
+ {
270
+ unsigned char * dst = (unsigned char *) dst_;
271
+ const unsigned char *src = (const unsigned char *) src_;
272
+ size_t i;
273
+
274
+ for (i = 0; i < n; i++) {
275
+ dst[i] = src[i];
276
+ }
277
+ }
278
+
279
+ static inline void
280
+ mem_zero(void *dst_, size_t n)
281
+ {
282
+ unsigned char *dst = (unsigned char *) dst_;
283
+ size_t i;
284
+
285
+ for (i = 0; i < n; i++) {
286
+ dst[i] = 0;
287
+ }
288
+ }
289
+
290
+ static inline void
291
+ mem_xor(void *__restrict__ dst_, const void *__restrict__ src_, size_t n)
292
+ {
293
+ unsigned char * dst = (unsigned char *) dst_;
294
+ const unsigned char *src = (const unsigned char *) src_;
295
+ size_t i;
296
+
297
+ for (i = 0; i < n; i++) {
298
+ dst[i] ^= src[i];
299
+ }
300
+ }
301
+
302
+ static inline void
303
+ mem_xor2(void *__restrict__ dst_, const void *__restrict__ src1_, const void *__restrict__ src2_,
304
+ size_t n)
305
+ {
306
+ unsigned char * dst = (unsigned char *) dst_;
307
+ const unsigned char *src1 = (const unsigned char *) src1_;
308
+ const unsigned char *src2 = (const unsigned char *) src2_;
309
+ size_t i;
310
+
311
+ for (i = 0; i < n; i++) {
312
+ dst[i] = src1[i] ^ src2[i];
313
+ }
314
+ }
315
+
316
+ static const uint8_t zero[64] = { 0 };