ffi-hydrogen 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 (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,383 @@
1
+ /*
2
+ * Based on Michael Hamburg's STROBE reference implementation.
3
+ * Copyright (c) 2015-2016 Cryptography Research, Inc.
4
+ * MIT License (MIT)
5
+ */
6
+
7
+ #if defined(__GNUC__) && defined(__SIZEOF_INT128__)
8
+ # define hydro_x25519_WBITS 64
9
+ #else
10
+ # define hydro_x25519_WBITS 32
11
+ #endif
12
+
13
+ #if hydro_x25519_WBITS == 64
14
+ typedef uint64_t hydro_x25519_limb_t;
15
+ typedef __uint128_t hydro_x25519_dlimb_t;
16
+ typedef __int128_t hydro_x25519_sdlimb_t;
17
+ # define hydro_x25519_eswap_limb(X) LOAD64_LE((const uint8_t *) &(X))
18
+ # define hydro_x25519_LIMB(x) x##ull
19
+ #elif hydro_x25519_WBITS == 32
20
+ typedef uint32_t hydro_x25519_limb_t;
21
+ typedef uint64_t hydro_x25519_dlimb_t;
22
+ typedef int64_t hydro_x25519_sdlimb_t;
23
+ # define hydro_x25519_eswap_limb(X) LOAD32_LE((const uint8_t *) &(X))
24
+ # define hydro_x25519_LIMB(x) (uint32_t)(x##ull), (uint32_t)((x##ull) >> 32)
25
+ #else
26
+ # error "Need to know hydro_x25519_WBITS"
27
+ #endif
28
+
29
+ #define hydro_x25519_NLIMBS (256 / hydro_x25519_WBITS)
30
+ typedef hydro_x25519_limb_t hydro_x25519_fe[hydro_x25519_NLIMBS];
31
+
32
+ typedef hydro_x25519_limb_t hydro_x25519_scalar_t[hydro_x25519_NLIMBS];
33
+
34
+ static const hydro_x25519_limb_t hydro_x25519_MONTGOMERY_FACTOR =
35
+ (hydro_x25519_limb_t) 0xd2b51da312547e1bull;
36
+
37
+ static const hydro_x25519_scalar_t hydro_x25519_sc_p = { hydro_x25519_LIMB(0x5812631a5cf5d3ed),
38
+ hydro_x25519_LIMB(0x14def9dea2f79cd6),
39
+ hydro_x25519_LIMB(0x0000000000000000),
40
+ hydro_x25519_LIMB(0x1000000000000000) };
41
+
42
+ static const hydro_x25519_scalar_t hydro_x25519_sc_r2 = { hydro_x25519_LIMB(0xa40611e3449c0f01),
43
+ hydro_x25519_LIMB(0xd00e1ba768859347),
44
+ hydro_x25519_LIMB(0xceec73d217f5be65),
45
+ hydro_x25519_LIMB(0x0399411b7c309a3d) };
46
+
47
+ static const uint8_t hydro_x25519_BASE_POINT[hydro_x25519_BYTES] = { 9 };
48
+
49
+ static const hydro_x25519_limb_t hydro_x25519_a24[1] = { 121665 };
50
+
51
+ static inline hydro_x25519_limb_t
52
+ hydro_x25519_umaal(hydro_x25519_limb_t *carry, hydro_x25519_limb_t acc, hydro_x25519_limb_t mand,
53
+ hydro_x25519_limb_t mier)
54
+ {
55
+ hydro_x25519_dlimb_t tmp = (hydro_x25519_dlimb_t) mand * mier + acc + *carry;
56
+
57
+ *carry = tmp >> hydro_x25519_WBITS;
58
+ return (hydro_x25519_limb_t) tmp;
59
+ }
60
+
61
+ static inline hydro_x25519_limb_t
62
+ hydro_x25519_adc(hydro_x25519_limb_t *carry, hydro_x25519_limb_t acc, hydro_x25519_limb_t mand)
63
+ {
64
+ hydro_x25519_dlimb_t total = (hydro_x25519_dlimb_t) *carry + acc + mand;
65
+
66
+ *carry = total >> hydro_x25519_WBITS;
67
+ return (hydro_x25519_limb_t) total;
68
+ }
69
+
70
+ static inline hydro_x25519_limb_t
71
+ hydro_x25519_adc0(hydro_x25519_limb_t *carry, hydro_x25519_limb_t acc)
72
+ {
73
+ hydro_x25519_dlimb_t total = (hydro_x25519_dlimb_t) *carry + acc;
74
+
75
+ *carry = total >> hydro_x25519_WBITS;
76
+ return (hydro_x25519_limb_t) total;
77
+ }
78
+
79
+ static void
80
+ hydro_x25519_propagate(hydro_x25519_fe x, hydro_x25519_limb_t over)
81
+ {
82
+ hydro_x25519_limb_t carry;
83
+ int i;
84
+
85
+ over = x[hydro_x25519_NLIMBS - 1] >> (hydro_x25519_WBITS - 1) | over << 1;
86
+ x[hydro_x25519_NLIMBS - 1] &= ~((hydro_x25519_limb_t) 1 << (hydro_x25519_WBITS - 1));
87
+ carry = over * 19;
88
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
89
+ x[i] = hydro_x25519_adc0(&carry, x[i]);
90
+ }
91
+ }
92
+
93
+ static void
94
+ hydro_x25519_add(hydro_x25519_fe out, const hydro_x25519_fe a, const hydro_x25519_fe b)
95
+ {
96
+ hydro_x25519_limb_t carry = 0;
97
+ int i;
98
+
99
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
100
+ out[i] = hydro_x25519_adc(&carry, a[i], b[i]);
101
+ }
102
+ hydro_x25519_propagate(out, carry);
103
+ }
104
+
105
+ static void
106
+ hydro_x25519_sub(hydro_x25519_fe out, const hydro_x25519_fe a, const hydro_x25519_fe b)
107
+ {
108
+ hydro_x25519_sdlimb_t carry = -38;
109
+ int i;
110
+
111
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
112
+ out[i] = carry = carry + a[i] - b[i];
113
+ carry >>= hydro_x25519_WBITS;
114
+ }
115
+ hydro_x25519_propagate(out, 1 + carry);
116
+ }
117
+
118
+ static void
119
+ hydro_x25519_swapin(hydro_x25519_limb_t *x, const uint8_t *in)
120
+ {
121
+ int i;
122
+
123
+ memcpy(x, in, sizeof(hydro_x25519_fe));
124
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
125
+ x[i] = hydro_x25519_eswap_limb(x[i]);
126
+ }
127
+ }
128
+
129
+ static void
130
+ hydro_x25519_swapout(uint8_t *out, hydro_x25519_limb_t *x)
131
+ {
132
+ int i;
133
+
134
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
135
+ x[i] = hydro_x25519_eswap_limb(x[i]);
136
+ }
137
+ memcpy(out, x, sizeof(hydro_x25519_fe));
138
+ }
139
+
140
+ static void
141
+ hydro_x25519_mul(hydro_x25519_fe out, const hydro_x25519_fe a, const hydro_x25519_fe b, int nb)
142
+ {
143
+ hydro_x25519_limb_t accum[2 * hydro_x25519_NLIMBS] = { 0 };
144
+ hydro_x25519_limb_t carry2;
145
+ int i, j;
146
+
147
+ for (i = 0; i < nb; i++) {
148
+ carry2 = 0;
149
+ hydro_x25519_limb_t mand = b[i];
150
+ for (j = 0; j < hydro_x25519_NLIMBS; j++) {
151
+ accum[i + j] = hydro_x25519_umaal(&carry2, accum[i + j], mand, a[j]);
152
+ }
153
+ accum[i + j] = carry2;
154
+ }
155
+ carry2 = 0;
156
+ for (j = 0; j < hydro_x25519_NLIMBS; j++) {
157
+ const hydro_x25519_limb_t mand = 38;
158
+
159
+ out[j] = hydro_x25519_umaal(&carry2, accum[j], mand, accum[j + hydro_x25519_NLIMBS]);
160
+ }
161
+ hydro_x25519_propagate(out, carry2);
162
+ }
163
+
164
+ static void
165
+ hydro_x25519_sqr(hydro_x25519_fe out, const hydro_x25519_fe a)
166
+ {
167
+ hydro_x25519_mul(out, a, a, hydro_x25519_NLIMBS);
168
+ }
169
+
170
+ static void
171
+ hydro_x25519_mul1(hydro_x25519_fe out, const hydro_x25519_fe a)
172
+ {
173
+ hydro_x25519_mul(out, a, out, hydro_x25519_NLIMBS);
174
+ }
175
+
176
+ static void
177
+ hydro_x25519_sqr1(hydro_x25519_fe a)
178
+ {
179
+ hydro_x25519_mul1(a, a);
180
+ }
181
+
182
+ static void
183
+ hydro_x25519_condswap(hydro_x25519_limb_t a[2 * hydro_x25519_NLIMBS],
184
+ hydro_x25519_limb_t b[2 * hydro_x25519_NLIMBS], hydro_x25519_limb_t doswap)
185
+ {
186
+ int i;
187
+
188
+ for (i = 0; i < 2 * hydro_x25519_NLIMBS; i++) {
189
+ hydro_x25519_limb_t xorv = (a[i] ^ b[i]) & doswap;
190
+ a[i] ^= xorv;
191
+ b[i] ^= xorv;
192
+ }
193
+ }
194
+
195
+ static int
196
+ hydro_x25519_canon(hydro_x25519_fe x)
197
+ {
198
+ hydro_x25519_sdlimb_t carry;
199
+ hydro_x25519_limb_t carry0 = 19;
200
+ hydro_x25519_limb_t res;
201
+ int i;
202
+
203
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
204
+ x[i] = hydro_x25519_adc0(&carry0, x[i]);
205
+ }
206
+ hydro_x25519_propagate(x, carry0);
207
+ carry = -19;
208
+ res = 0;
209
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
210
+ res |= x[i] = carry += x[i];
211
+ carry >>= hydro_x25519_WBITS;
212
+ }
213
+ return ((hydro_x25519_dlimb_t) res - 1) >> hydro_x25519_WBITS;
214
+ }
215
+
216
+ static void
217
+ hydro_x25519_ladder_part1(hydro_x25519_fe xs[5])
218
+ {
219
+ hydro_x25519_limb_t *x2 = xs[0], *z2 = xs[1], *x3 = xs[2], *z3 = xs[3], *t1 = xs[4];
220
+
221
+ hydro_x25519_add(t1, x2, z2); // t1 = A
222
+ hydro_x25519_sub(z2, x2, z2); // z2 = B
223
+ hydro_x25519_add(x2, x3, z3); // x2 = C
224
+ hydro_x25519_sub(z3, x3, z3); // z3 = D
225
+ hydro_x25519_mul1(z3, t1); // z3 = DA
226
+ hydro_x25519_mul1(x2, z2); // x3 = BC
227
+ hydro_x25519_add(x3, z3, x2); // x3 = DA+CB
228
+ hydro_x25519_sub(z3, z3, x2); // z3 = DA-CB
229
+ hydro_x25519_sqr1(t1); // t1 = AA
230
+ hydro_x25519_sqr1(z2); // z2 = BB
231
+ hydro_x25519_sub(x2, t1, z2); // x2 = E = AA-BB
232
+ hydro_x25519_mul(z2, x2, hydro_x25519_a24, // z2 = E*a24
233
+ sizeof(hydro_x25519_a24) / sizeof(hydro_x25519_a24[0]));
234
+ hydro_x25519_add(z2, z2, t1); // z2 = E*a24 + AA
235
+ }
236
+
237
+ static void
238
+ hydro_x25519_ladder_part2(hydro_x25519_fe xs[5], const hydro_x25519_fe x1)
239
+ {
240
+ hydro_x25519_limb_t *x2 = xs[0], *z2 = xs[1], *x3 = xs[2], *z3 = xs[3], *t1 = xs[4];
241
+
242
+ hydro_x25519_sqr1(z3); // z3 = (DA-CB)^2
243
+ hydro_x25519_mul1(z3, x1); // z3 = x1 * (DA-CB)^2
244
+ hydro_x25519_sqr1(x3); // x3 = (DA+CB)^2
245
+ hydro_x25519_mul1(z2, x2); // z2 = AA*(E*a24+AA)
246
+ hydro_x25519_sub(x2, t1, x2); // x2 = BB again
247
+ hydro_x25519_mul1(x2, t1); // x2 = AA*BB
248
+ }
249
+
250
+ static void
251
+ hydro_x25519_core(hydro_x25519_fe xs[5], const uint8_t scalar[hydro_x25519_BYTES],
252
+ const uint8_t *x1, bool clamp)
253
+ {
254
+ hydro_x25519_limb_t swap;
255
+ hydro_x25519_limb_t *x2 = xs[0], *x3 = xs[2], *z3 = xs[3];
256
+ hydro_x25519_fe x1i;
257
+ int i;
258
+
259
+ hydro_x25519_swapin(x1i, x1);
260
+ x1 = (const uint8_t *) x1i;
261
+ swap = 0;
262
+ mem_zero(xs, 4 * sizeof(hydro_x25519_fe));
263
+ x2[0] = z3[0] = 1;
264
+ memcpy(x3, x1, sizeof(hydro_x25519_fe));
265
+ for (i = 255; i >= 0; i--) {
266
+ uint8_t bytei = scalar[i / 8];
267
+ hydro_x25519_limb_t doswap;
268
+ hydro_x25519_fe x1_dup;
269
+
270
+ if (clamp) {
271
+ if (i / 8 == 0) {
272
+ bytei &= ~7;
273
+ } else if (i / 8 == hydro_x25519_BYTES - 1) {
274
+ bytei &= 0x7F;
275
+ bytei |= 0x40;
276
+ }
277
+ }
278
+ doswap = 1U + ~(hydro_x25519_limb_t)((bytei >> (i % 8)) & 1);
279
+ hydro_x25519_condswap(x2, x3, swap ^ doswap);
280
+ swap = doswap;
281
+ hydro_x25519_ladder_part1(xs);
282
+ memcpy(x1_dup, x1, sizeof x1_dup);
283
+ hydro_x25519_ladder_part2(xs, x1_dup);
284
+ }
285
+ hydro_x25519_condswap(x2, x3, swap);
286
+ }
287
+
288
+ static int
289
+ hydro_x25519_scalarmult(uint8_t out[hydro_x25519_BYTES], const uint8_t scalar[hydro_x25519_BYTES],
290
+ const uint8_t x1[hydro_x25519_BYTES], bool clamp)
291
+ {
292
+ hydro_x25519_fe xs[5];
293
+ hydro_x25519_limb_t *x2, *z2, *z3;
294
+ hydro_x25519_limb_t *prev;
295
+ int i;
296
+ int ret;
297
+
298
+ hydro_x25519_core(xs, scalar, x1, clamp);
299
+
300
+ /* Precomputed inversion chain */
301
+ x2 = xs[0];
302
+ z2 = xs[1];
303
+ z3 = xs[3];
304
+ prev = z2;
305
+
306
+ /* Raise to the p-2 = 0x7f..ffeb */
307
+ for (i = 253; i >= 0; i--) {
308
+ hydro_x25519_sqr(z3, prev);
309
+ prev = z3;
310
+ if (i >= 8 || (0xeb >> i & 1)) {
311
+ hydro_x25519_mul1(z3, z2);
312
+ }
313
+ }
314
+
315
+ /* Here prev = z3 */
316
+ /* x2 /= z2 */
317
+ hydro_x25519_mul1(x2, z3);
318
+ ret = hydro_x25519_canon(x2);
319
+ hydro_x25519_swapout(out, x2);
320
+
321
+ if (clamp == 0) {
322
+ return 0;
323
+ }
324
+ return ret;
325
+ }
326
+
327
+ static inline int
328
+ hydro_x25519_scalarmult_base(uint8_t pk[hydro_x25519_PUBLICKEYBYTES],
329
+ const uint8_t sk[hydro_x25519_SECRETKEYBYTES])
330
+ {
331
+ return hydro_x25519_scalarmult(pk, sk, hydro_x25519_BASE_POINT, 1);
332
+ }
333
+
334
+ static inline void
335
+ hydro_x25519_scalarmult_base_uniform(uint8_t pk[hydro_x25519_PUBLICKEYBYTES],
336
+ const uint8_t sk[hydro_x25519_SECRETKEYBYTES])
337
+ {
338
+ if (hydro_x25519_scalarmult(pk, sk, hydro_x25519_BASE_POINT, 0) != 0) {
339
+ abort();
340
+ }
341
+ }
342
+
343
+ static void
344
+ hydro_x25519_sc_montmul(hydro_x25519_scalar_t out, const hydro_x25519_scalar_t a,
345
+ const hydro_x25519_scalar_t b)
346
+ {
347
+ hydro_x25519_limb_t hic = 0;
348
+ int i, j;
349
+
350
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
351
+ hydro_x25519_limb_t carry = 0, carry2 = 0, mand = a[i],
352
+ mand2 = hydro_x25519_MONTGOMERY_FACTOR;
353
+
354
+ for (j = 0; j < hydro_x25519_NLIMBS; j++) {
355
+ hydro_x25519_limb_t acc = out[j];
356
+
357
+ acc = hydro_x25519_umaal(&carry, acc, mand, b[j]);
358
+ if (j == 0) {
359
+ mand2 *= acc;
360
+ }
361
+ acc = hydro_x25519_umaal(&carry2, acc, mand2, hydro_x25519_sc_p[j]);
362
+ if (j > 0) {
363
+ out[j - 1] = acc;
364
+ }
365
+ }
366
+
367
+ /* Add two carry registers and high carry */
368
+ out[hydro_x25519_NLIMBS - 1] = hydro_x25519_adc(&hic, carry, carry2);
369
+ }
370
+
371
+ /* Reduce */
372
+ hydro_x25519_sdlimb_t scarry = 0;
373
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
374
+ out[i] = scarry = scarry + out[i] - hydro_x25519_sc_p[i];
375
+ scarry >>= hydro_x25519_WBITS;
376
+ }
377
+ hydro_x25519_limb_t need_add = -(scarry + hic);
378
+
379
+ hydro_x25519_limb_t carry = 0;
380
+ for (i = 0; i < hydro_x25519_NLIMBS; i++) {
381
+ out[i] = hydro_x25519_umaal(&carry, out[i], need_add, hydro_x25519_sc_p[i]);
382
+ }
383
+ }
@@ -0,0 +1,10 @@
1
+ architectures=avr
2
+ author=Frank Denis <libhydrogen@pureftpd.org>
3
+ category=Other
4
+ includes=hydrogen.h
5
+ maintainer=Frank Denis <libhydrogen@pureftpd.org>
6
+ name=hydrogen-crypto
7
+ paragraph=Consistent high-level API, inspired by libsodium. Instead of low-level primitives, it exposes simple functions to solve common problems that cryptography can solve.
8
+ sentence=An easy-to-use, hard-to-misuse cryptographic library
9
+ url=https://github.com/jedisct1/libhydrogen
10
+ version=0.1
Binary file