Crypto++ 8.2
Free C&
donna_32.cpp
1// donna_32.cpp - written and placed in public domain by Jeffrey Walton
2// Crypto++ specific implementation wrapped around Andrew
3// Moon's public domain curve25519-donna and ed25519-donna,
4// https://github.com/floodyberry/curve25519-donna and
5// https://github.com/floodyberry/ed25519-donna.
6
7// The curve25519 and ed25519 source files multiplex different repos and
8// architectures using namespaces. The repos are Andrew Moon's
9// curve25519-donna and ed25519-donna. The architectures are 32-bit, 64-bit
10// and SSE. For example, 32-bit x25519 uses symbols from Donna::X25519 and
11// Donna::Arch32.
12
13// A fair amount of duplication happens below, but we could not directly
14// use curve25519 for both x25519 and ed25519. A close examination reveals
15// slight differences in the implementation. For example, look at the
16// two curve25519_sub functions.
17
18// If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
19// https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
20
21#include "pch.h"
22
23#include "config.h"
24#include "donna.h"
25#include "secblock.h"
26#include "sha.h"
27#include "misc.h"
28#include "cpu.h"
29
30#include <istream>
31#include <sstream>
32
33#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
34# pragma GCC diagnostic ignored "-Wunused-function"
35#endif
36
37// Squash MS LNK4221 and libtool warnings
38extern const char DONNA32_FNAME[] = __FILE__;
39
40#if defined(CRYPTOPP_CURVE25519_32BIT)
41
42#include "donna_32.h"
43
44ANONYMOUS_NAMESPACE_BEGIN
45
46using CryptoPP::byte;
47using CryptoPP::word32;
48using CryptoPP::GetWord;
49using CryptoPP::PutWord;
50using CryptoPP::LITTLE_ENDIAN_ORDER;
51
52inline word32 U8TO32_LE(const byte* p)
53{
54 return GetWord<word32>(false, LITTLE_ENDIAN_ORDER, p);
55}
56
57inline void U32TO8_LE(byte* p, word32 w)
58{
59 PutWord(false, LITTLE_ENDIAN_ORDER, p, w);
60}
61
62ANONYMOUS_NAMESPACE_END
63
64NAMESPACE_BEGIN(CryptoPP)
65NAMESPACE_BEGIN(Donna)
66NAMESPACE_BEGIN(X25519)
67ANONYMOUS_NAMESPACE_BEGIN
68
69using CryptoPP::byte;
70using CryptoPP::word32;
71using CryptoPP::sword32;
72using CryptoPP::word64;
73using CryptoPP::sword64;
74
75using CryptoPP::GetBlock;
76using CryptoPP::LittleEndian;
77
78// Bring in all the symbols from the 32-bit header
79using namespace CryptoPP::Donna::Arch32;
80
81/* out = in */
82inline void
83curve25519_copy(bignum25519 out, const bignum25519 in) {
84 out[0] = in[0]; out[1] = in[1];
85 out[2] = in[2]; out[3] = in[3];
86 out[4] = in[4]; out[5] = in[5];
87 out[6] = in[6]; out[7] = in[7];
88 out[8] = in[8]; out[9] = in[9];
89}
90
91/* out = a + b */
92inline void
93curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
94 out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
95 out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
96 out[4] = a[4] + b[4]; out[5] = a[5] + b[5];
97 out[6] = a[6] + b[6]; out[7] = a[7] + b[7];
98 out[8] = a[8] + b[8]; out[9] = a[9] + b[9];
99}
100
101/* out = a - b */
102inline void
103curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
104 word32 c;
105 out[0] = 0x7ffffda + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
106 out[1] = 0x3fffffe + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
107 out[2] = 0x7fffffe + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
108 out[3] = 0x3fffffe + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
109 out[4] = 0x7fffffe + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
110 out[5] = 0x3fffffe + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
111 out[6] = 0x7fffffe + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
112 out[7] = 0x3fffffe + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
113 out[8] = 0x7fffffe + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
114 out[9] = 0x3fffffe + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
115 out[0] += 19 * c;
116}
117
118/* out = in * scalar */
119inline void
120curve25519_scalar_product(bignum25519 out, const bignum25519 in, const word32 scalar) {
121 word64 a;
122 word32 c;
123 a = mul32x32_64(in[0], scalar); out[0] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
124 a = mul32x32_64(in[1], scalar) + c; out[1] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
125 a = mul32x32_64(in[2], scalar) + c; out[2] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
126 a = mul32x32_64(in[3], scalar) + c; out[3] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
127 a = mul32x32_64(in[4], scalar) + c; out[4] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
128 a = mul32x32_64(in[5], scalar) + c; out[5] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
129 a = mul32x32_64(in[6], scalar) + c; out[6] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
130 a = mul32x32_64(in[7], scalar) + c; out[7] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
131 a = mul32x32_64(in[8], scalar) + c; out[8] = (word32)a & reduce_mask_26; c = (word32)(a >> 26);
132 a = mul32x32_64(in[9], scalar) + c; out[9] = (word32)a & reduce_mask_25; c = (word32)(a >> 25);
133 out[0] += c * 19;
134}
135
136/* out = a * b */
137inline void
138curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b) {
139 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
140 word32 s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
141 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
142 word32 p;
143
144 r0 = b[0]; r1 = b[1]; r2 = b[2]; r3 = b[3]; r4 = b[4];
145 r5 = b[5]; r6 = b[6]; r7 = b[7]; r8 = b[8]; r9 = b[9];
146
147 s0 = a[0]; s1 = a[1]; s2 = a[2]; s3 = a[3]; s4 = a[4];
148 s5 = a[5]; s6 = a[6]; s7 = a[7]; s8 = a[8]; s9 = a[9];
149
150 m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
151 m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
152 m5 = mul32x32_64(r0, s5) + mul32x32_64(r1, s4) + mul32x32_64(r2, s3) + mul32x32_64(r3, s2) + mul32x32_64(r4, s1) + mul32x32_64(r5, s0);
153 m7 = mul32x32_64(r0, s7) + mul32x32_64(r1, s6) + mul32x32_64(r2, s5) + mul32x32_64(r3, s4) + mul32x32_64(r4, s3) + mul32x32_64(r5, s2) + mul32x32_64(r6, s1) + mul32x32_64(r7, s0);
154 m9 = mul32x32_64(r0, s9) + mul32x32_64(r1, s8) + mul32x32_64(r2, s7) + mul32x32_64(r3, s6) + mul32x32_64(r4, s5) + mul32x32_64(r5, s4) + mul32x32_64(r6, s3) + mul32x32_64(r7, s2) + mul32x32_64(r8, s1) + mul32x32_64(r9, s0);
155
156 r1 *= 2; r3 *= 2; r5 *= 2; r7 *= 2;
157
158 m0 = mul32x32_64(r0, s0);
159 m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
160 m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
161 m6 = mul32x32_64(r0, s6) + mul32x32_64(r1, s5) + mul32x32_64(r2, s4) + mul32x32_64(r3, s3) + mul32x32_64(r4, s2) + mul32x32_64(r5, s1) + mul32x32_64(r6, s0);
162 m8 = mul32x32_64(r0, s8) + mul32x32_64(r1, s7) + mul32x32_64(r2, s6) + mul32x32_64(r3, s5) + mul32x32_64(r4, s4) + mul32x32_64(r5, s3) + mul32x32_64(r6, s2) + mul32x32_64(r7, s1) + mul32x32_64(r8, s0);
163
164 r1 *= 19; r2 *= 19;
165 r3 = (r3 / 2) * 19;
166 r4 *= 19;
167 r5 = (r5 / 2) * 19;
168 r6 *= 19;
169 r7 = (r7 / 2) * 19;
170 r8 *= 19; r9 *= 19;
171
172 m1 += (mul32x32_64(r9, s2) + mul32x32_64(r8, s3) + mul32x32_64(r7, s4) + mul32x32_64(r6, s5) + mul32x32_64(r5, s6) + mul32x32_64(r4, s7) + mul32x32_64(r3, s8) + mul32x32_64(r2, s9));
173 m3 += (mul32x32_64(r9, s4) + mul32x32_64(r8, s5) + mul32x32_64(r7, s6) + mul32x32_64(r6, s7) + mul32x32_64(r5, s8) + mul32x32_64(r4, s9));
174 m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
175 m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
176
177 r3 *= 2; r5 *= 2; r7 *= 2; r9 *= 2;
178
179 m0 += (mul32x32_64(r9, s1) + mul32x32_64(r8, s2) + mul32x32_64(r7, s3) + mul32x32_64(r6, s4) + mul32x32_64(r5, s5) + mul32x32_64(r4, s6) + mul32x32_64(r3, s7) + mul32x32_64(r2, s8) + mul32x32_64(r1, s9));
180 m2 += (mul32x32_64(r9, s3) + mul32x32_64(r8, s4) + mul32x32_64(r7, s5) + mul32x32_64(r6, s6) + mul32x32_64(r5, s7) + mul32x32_64(r4, s8) + mul32x32_64(r3, s9));
181 m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
182 m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
183 m8 += (mul32x32_64(r9, s9));
184
185 r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
186 m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
187 m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
188 m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
189 m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
190 m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
191 m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
192 m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
193 m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
194 m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
195 m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
196 r1 += p;
197
198 out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
199 out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
200}
201
202/* out = in * in */
203inline void
204curve25519_square(bignum25519 out, const bignum25519 in) {
205 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
206 word32 d6,d7,d8,d9;
207 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
208 word32 p;
209
210 r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
211 r5 = in[5]; r6 = in[6]; r7 = in[7]; r8 = in[8]; r9 = in[9];
212
213 m0 = mul32x32_64(r0, r0);
214 r0 *= 2;
215 m1 = mul32x32_64(r0, r1);
216 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
217 r1 *= 2;
218 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
219 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
220 r2 *= 2;
221 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
222 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
223 r3 *= 2;
224 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
225 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
226 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
227
228 d6 = r6 * 19; d7 = r7 * 2 * 19;
229 d8 = r8 * 19; d9 = r9 * 2 * 19;
230
231 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
232 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
233 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
234 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
235 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
236 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
237 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
238 m7 += (mul32x32_64(d9, r8 ));
239 m8 += (mul32x32_64(d9, r9 ));
240
241 r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
242 m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
243 m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
244 m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
245 m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
246 m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
247 m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
248 m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
249 m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
250 m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
251 m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
252 r1 += p;
253
254 out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
255 out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
256}
257
258/* out = in^(2 * count) */
259void
260curve25519_square_times(bignum25519 out, const bignum25519 in, int count) {
261 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
262 word32 d6,d7,d8,d9;
263 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
264 word32 p;
265
266 r0 = in[0]; r1 = in[1]; r2 = in[2]; r3 = in[3]; r4 = in[4];
267 r5 = in[5]; r6 = in[6]; r7 = in[7]; r8 = in[8]; r9 = in[9];
268
269 do {
270 m0 = mul32x32_64(r0, r0);
271 r0 *= 2;
272 m1 = mul32x32_64(r0, r1);
273 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
274 r1 *= 2;
275 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
276 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
277 r2 *= 2;
278 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
279 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
280 r3 *= 2;
281 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
282 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
283 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
284
285 d6 = r6 * 19; d7 = r7 * 2 * 19;
286 d8 = r8 * 19; d9 = r9 * 2 * 19;
287
288 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
289 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
290 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
291 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
292 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
293 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
294 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
295 m7 += (mul32x32_64(d9, r8 ));
296 m8 += (mul32x32_64(d9, r9 ));
297
298 r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
299 m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
300 m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
301 m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
302 m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
303 m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
304 m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
305 m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
306 m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
307 m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
308 m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
309 r1 += p;
310 } while (--count);
311
312 out[0] = r0; out[1] = r1; out[2] = r2; out[3] = r3; out[4] = r4;
313 out[5] = r5; out[6] = r6; out[7] = r7; out[8] = r8; out[9] = r9;
314}
315
316/* Take a little-endian, 32-byte number and expand it into polynomial form */
317void
318curve25519_expand(bignum25519 out, const byte in[32]) {
319 word32 x0,x1,x2,x3,x4,x5,x6,x7;
321 block(x0)(x1)(x2)(x3)(x4)(x5)(x6)(x7);
322
323 out[0] = ( x0 ) & reduce_mask_26;
324 out[1] = ((((word64)x1 << 32) | x0) >> 26) & reduce_mask_25;
325 out[2] = ((((word64)x2 << 32) | x1) >> 19) & reduce_mask_26;
326 out[3] = ((((word64)x3 << 32) | x2) >> 13) & reduce_mask_25;
327 out[4] = (( x3) >> 6) & reduce_mask_26;
328 out[5] = ( x4 ) & reduce_mask_25;
329 out[6] = ((((word64)x5 << 32) | x4) >> 25) & reduce_mask_26;
330 out[7] = ((((word64)x6 << 32) | x5) >> 19) & reduce_mask_25;
331 out[8] = ((((word64)x7 << 32) | x6) >> 12) & reduce_mask_26;
332 out[9] = (( x7) >> 6) & reduce_mask_25; /* ignore the top bit */
333}
334
335/* Take a fully reduced polynomial form number and contract it into a little-endian, 32-byte array */
336void
337curve25519_contract(byte out[32], const bignum25519 in) {
338 bignum25519 f;
339 curve25519_copy(f, in);
340
341 #define carry_pass() \
342 f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
343 f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
344 f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
345 f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
346 f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
347 f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
348 f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
349 f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
350 f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
351
352 #define carry_pass_full() \
353 carry_pass() \
354 f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
355
356 #define carry_pass_final() \
357 carry_pass() \
358 f[9] &= reduce_mask_25;
359
360 carry_pass_full()
361 carry_pass_full()
362
363 /* now t is between 0 and 2^255-1, properly carried. */
364 /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
365 f[0] += 19;
366 carry_pass_full()
367
368 /* now between 19 and 2^255-1 in both cases, and offset by 19. */
369 f[0] += (1 << 26) - 19;
370 f[1] += (1 << 25) - 1;
371 f[2] += (1 << 26) - 1;
372 f[3] += (1 << 25) - 1;
373 f[4] += (1 << 26) - 1;
374 f[5] += (1 << 25) - 1;
375 f[6] += (1 << 26) - 1;
376 f[7] += (1 << 25) - 1;
377 f[8] += (1 << 26) - 1;
378 f[9] += (1 << 25) - 1;
379
380 /* now between 2^255 and 2^256-20, and offset by 2^255. */
381 carry_pass_final()
382
383 #undef carry_pass
384 #undef carry_full
385 #undef carry_final
386
387 f[1] <<= 2;
388 f[2] <<= 3;
389 f[3] <<= 5;
390 f[4] <<= 6;
391 f[6] <<= 1;
392 f[7] <<= 3;
393 f[8] <<= 4;
394 f[9] <<= 6;
395
396 #define F(i, s) \
397 out[s+0] |= (byte)( f[i] & 0xff); \
398 out[s+1] = (byte)((f[i] >> 8) & 0xff); \
399 out[s+2] = (byte)((f[i] >> 16) & 0xff); \
400 out[s+3] = (byte)((f[i] >> 24) & 0xff);
401
402 out[0] = out[16] = 0;
403 F(0,0); F(1,3);
404 F(2,6); F(3,9);
405 F(4,12); F(5,16);
406 F(6,19); F(7,22);
407 F(8,25); F(9,28);
408 #undef F
409}
410
411inline void
412curve25519_swap_conditional(bignum25519 x, bignum25519 qpx, word32 iswap) {
413 const word32 swap = (word32)(-(sword32)iswap);
414 word32 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
415
416 x0 = swap & (x[0] ^ qpx[0]); x[0] ^= x0; qpx[0] ^= x0;
417 x1 = swap & (x[1] ^ qpx[1]); x[1] ^= x1; qpx[1] ^= x1;
418 x2 = swap & (x[2] ^ qpx[2]); x[2] ^= x2; qpx[2] ^= x2;
419 x3 = swap & (x[3] ^ qpx[3]); x[3] ^= x3; qpx[3] ^= x3;
420 x4 = swap & (x[4] ^ qpx[4]); x[4] ^= x4; qpx[4] ^= x4;
421 x5 = swap & (x[5] ^ qpx[5]); x[5] ^= x5; qpx[5] ^= x5;
422 x6 = swap & (x[6] ^ qpx[6]); x[6] ^= x6; qpx[6] ^= x6;
423 x7 = swap & (x[7] ^ qpx[7]); x[7] ^= x7; qpx[7] ^= x7;
424 x8 = swap & (x[8] ^ qpx[8]); x[8] ^= x8; qpx[8] ^= x8;
425 x9 = swap & (x[9] ^ qpx[9]); x[9] ^= x9; qpx[9] ^= x9;
426}
427
428/*
429 * In: b = 2^5 - 2^0
430 * Out: b = 2^250 - 2^0
431 */
432void
433curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
434 ALIGN(16) bignum25519 t0,c;
435
436 /* 2^5 - 2^0 */ /* b */
437 /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
438 /* 2^10 - 2^0 */ curve25519_mul(b, t0, b);
439 /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
440 /* 2^20 - 2^0 */ curve25519_mul(c, t0, b);
441 /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
442 /* 2^40 - 2^0 */ curve25519_mul(t0, t0, c);
443 /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
444 /* 2^50 - 2^0 */ curve25519_mul(b, t0, b);
445 /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
446 /* 2^100 - 2^0 */ curve25519_mul(c, t0, b);
447 /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
448 /* 2^200 - 2^0 */ curve25519_mul(t0, t0, c);
449 /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
450 /* 2^250 - 2^0 */ curve25519_mul(b, t0, b);
451}
452
453/*
454 * z^(p - 2) = z(2^255 - 21)
455 */
456void
457curve25519_recip(bignum25519 out, const bignum25519 z) {
458 ALIGN(16) bignum25519 a, t0, b;
459
460 /* 2 */ curve25519_square(a, z); /* a = 2 */
461 /* 8 */ curve25519_square_times(t0, a, 2);
462 /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
463 /* 11 */ curve25519_mul(a, b, a); /* a = 11 */
464 /* 22 */ curve25519_square(t0, a);
465 /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
466 /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
467 /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
468 /* 2^255 - 21 */ curve25519_mul(out, b, a);
469}
470
471ANONYMOUS_NAMESPACE_END
472NAMESPACE_END // X25519
473NAMESPACE_END // Donna
474NAMESPACE_END // CryptoPP
475
476//******************************* ed25519 *******************************//
477
478NAMESPACE_BEGIN(CryptoPP)
479NAMESPACE_BEGIN(Donna)
480NAMESPACE_BEGIN(Ed25519)
481ANONYMOUS_NAMESPACE_BEGIN
482
483using CryptoPP::byte;
484using CryptoPP::word32;
485using CryptoPP::sword32;
486using CryptoPP::word64;
487using CryptoPP::sword64;
488
489using CryptoPP::GetBlock;
491
492using CryptoPP::SHA512;
493
494// Bring in all the symbols from the 32-bit header
495using namespace CryptoPP::Donna::Arch32;
496
497/* out = in */
498inline void
499curve25519_copy(bignum25519 out, const bignum25519 in) {
500 out[0] = in[0]; out[1] = in[1];
501 out[2] = in[2]; out[3] = in[3];
502 out[4] = in[4]; out[5] = in[5];
503 out[6] = in[6]; out[7] = in[7];
504 out[8] = in[8]; out[9] = in[9];
505}
506
507/* out = a + b */
508inline void
509curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
510 out[0] = a[0] + b[0]; out[1] = a[1] + b[1];
511 out[2] = a[2] + b[2]; out[3] = a[3] + b[3];
512 out[4] = a[4] + b[4]; out[5] = a[5] + b[5];
513 out[6] = a[6] + b[6]; out[7] = a[7] + b[7];
514 out[8] = a[8] + b[8]; out[9] = a[9] + b[9];
515}
516
517inline void
518curve25519_add_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
519 word32 c;
520 out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
521 out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
522 out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
523 out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
524 out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
525 out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
526 out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
527 out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
528 out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
529 out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
530 out[0] += 19 * c;
531}
532
533inline void
534curve25519_add_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
535 word32 c;
536 out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
537 out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
538 out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
539 out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
540 out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
541 out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
542 out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
543 out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
544 out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
545 out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
546 out[0] += 19 * c;
547}
548
549/* out = a - b */
550inline void
551curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
552 word32 c;
553 out[0] = twoP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
554 out[1] = twoP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
555 out[2] = twoP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
556 out[3] = twoP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
557 out[4] = twoP2468 + a[4] - b[4] + c;
558 out[5] = twoP13579 + a[5] - b[5] ;
559 out[6] = twoP2468 + a[6] - b[6] ;
560 out[7] = twoP13579 + a[7] - b[7] ;
561 out[8] = twoP2468 + a[8] - b[8] ;
562 out[9] = twoP13579 + a[9] - b[9] ;
563}
564
565/* out = a - b, where a is the result of a basic op (add,sub) */
566inline void
567curve25519_sub_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
568 word32 c;
569 out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
570 out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
571 out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
572 out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
573 out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
574 out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
575 out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
576 out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
577 out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
578 out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
579 out[0] += 19 * c;
580}
581
582inline void
583curve25519_sub_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
584 word32 c;
585 out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
586 out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
587 out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
588 out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
589 out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
590 out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
591 out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
592 out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
593 out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
594 out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
595 out[0] += 19 * c;
596}
597
598/* out = -a */
599inline void
600curve25519_neg(bignum25519 out, const bignum25519 a) {
601 word32 c;
602 out[0] = twoP0 - a[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
603 out[1] = twoP13579 - a[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
604 out[2] = twoP2468 - a[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
605 out[3] = twoP13579 - a[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
606 out[4] = twoP2468 - a[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
607 out[5] = twoP13579 - a[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
608 out[6] = twoP2468 - a[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
609 out[7] = twoP13579 - a[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
610 out[8] = twoP2468 - a[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
611 out[9] = twoP13579 - a[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
612 out[0] += 19 * c;
613}
614
615/* out = a * b */
616void
617curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b) {
618 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
619 word32 s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
620 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
621 word32 p;
622
623 r0 = b[0]; r1 = b[1];
624 r2 = b[2]; r3 = b[3];
625 r4 = b[4]; r5 = b[5];
626 r6 = b[6]; r7 = b[7];
627 r8 = b[8]; r9 = b[9];
628
629 s0 = a[0]; s1 = a[1];
630 s2 = a[2]; s3 = a[3];
631 s4 = a[4]; s5 = a[5];
632 s6 = a[6]; s7 = a[7];
633 s8 = a[8]; s9 = a[9];
634
635 m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
636 m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
637 m5 = mul32x32_64(r0, s5) + mul32x32_64(r1, s4) + mul32x32_64(r2, s3) + mul32x32_64(r3, s2) + mul32x32_64(r4, s1) + mul32x32_64(r5, s0);
638 m7 = mul32x32_64(r0, s7) + mul32x32_64(r1, s6) + mul32x32_64(r2, s5) + mul32x32_64(r3, s4) + mul32x32_64(r4, s3) + mul32x32_64(r5, s2) + mul32x32_64(r6, s1) + mul32x32_64(r7, s0);
639 m9 = mul32x32_64(r0, s9) + mul32x32_64(r1, s8) + mul32x32_64(r2, s7) + mul32x32_64(r3, s6) + mul32x32_64(r4, s5) + mul32x32_64(r5, s4) + mul32x32_64(r6, s3) + mul32x32_64(r7, s2) + mul32x32_64(r8, s1) + mul32x32_64(r9, s0);
640
641 r1 *= 2; r3 *= 2;
642 r5 *= 2; r7 *= 2;
643
644 m0 = mul32x32_64(r0, s0);
645 m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
646 m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
647 m6 = mul32x32_64(r0, s6) + mul32x32_64(r1, s5) + mul32x32_64(r2, s4) + mul32x32_64(r3, s3) + mul32x32_64(r4, s2) + mul32x32_64(r5, s1) + mul32x32_64(r6, s0);
648 m8 = mul32x32_64(r0, s8) + mul32x32_64(r1, s7) + mul32x32_64(r2, s6) + mul32x32_64(r3, s5) + mul32x32_64(r4, s4) + mul32x32_64(r5, s3) + mul32x32_64(r6, s2) + mul32x32_64(r7, s1) + mul32x32_64(r8, s0);
649
650 r1 *= 19; r2 *= 19;
651 r3 = (r3 / 2) * 19;
652 r4 *= 19;
653 r5 = (r5 / 2) * 19;
654 r6 *= 19;
655 r7 = (r7 / 2) * 19;
656 r8 *= 19; r9 *= 19;
657
658 m1 += (mul32x32_64(r9, s2) + mul32x32_64(r8, s3) + mul32x32_64(r7, s4) + mul32x32_64(r6, s5) + mul32x32_64(r5, s6) + mul32x32_64(r4, s7) + mul32x32_64(r3, s8) + mul32x32_64(r2, s9));
659 m3 += (mul32x32_64(r9, s4) + mul32x32_64(r8, s5) + mul32x32_64(r7, s6) + mul32x32_64(r6, s7) + mul32x32_64(r5, s8) + mul32x32_64(r4, s9));
660 m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
661 m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
662
663 r3 *= 2; r5 *= 2;
664 r7 *= 2; r9 *= 2;
665
666 m0 += (mul32x32_64(r9, s1) + mul32x32_64(r8, s2) + mul32x32_64(r7, s3) + mul32x32_64(r6, s4) + mul32x32_64(r5, s5) + mul32x32_64(r4, s6) + mul32x32_64(r3, s7) + mul32x32_64(r2, s8) + mul32x32_64(r1, s9));
667 m2 += (mul32x32_64(r9, s3) + mul32x32_64(r8, s4) + mul32x32_64(r7, s5) + mul32x32_64(r6, s6) + mul32x32_64(r5, s7) + mul32x32_64(r4, s8) + mul32x32_64(r3, s9));
668 m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
669 m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
670 m8 += (mul32x32_64(r9, s9));
671
672 r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
673 m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
674 m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
675 m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
676 m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
677 m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
678 m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
679 m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
680 m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
681 m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
682 m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
683 r1 += p;
684
685 out[0] = r0; out[1] = r1;
686 out[2] = r2; out[3] = r3;
687 out[4] = r4; out[5] = r5;
688 out[6] = r6; out[7] = r7;
689 out[8] = r8; out[9] = r9;
690}
691
692/* out = in*in */
693void
694curve25519_square(bignum25519 out, const bignum25519 in) {
695 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
696 word32 d6,d7,d8,d9;
697 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
698 word32 p;
699
700 r0 = in[0]; r1 = in[1];
701 r2 = in[2]; r3 = in[3];
702 r4 = in[4]; r5 = in[5];
703 r6 = in[6]; r7 = in[7];
704 r8 = in[8]; r9 = in[9];
705
706 m0 = mul32x32_64(r0, r0);
707 r0 *= 2;
708 m1 = mul32x32_64(r0, r1);
709 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
710 r1 *= 2;
711 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
712 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
713 r2 *= 2;
714 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
715 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
716 r3 *= 2;
717 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
718 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
719 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
720
721 d6 = r6 * 19;
722 d7 = r7 * 2 * 19;
723 d8 = r8 * 19;
724 d9 = r9 * 2 * 19;
725
726 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
727 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
728 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
729 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
730 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
731 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
732 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
733 m7 += (mul32x32_64(d9, r8 ));
734 m8 += (mul32x32_64(d9, r9 ));
735
736 r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
737 m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
738 m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
739 m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
740 m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
741 m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
742 m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
743 m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
744 m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
745 m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
746 m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
747 r1 += p;
748
749 out[0] = r0; out[1] = r1;
750 out[2] = r2; out[3] = r3;
751 out[4] = r4; out[5] = r5;
752 out[6] = r6; out[7] = r7;
753 out[8] = r8; out[9] = r9;
754}
755
756/* out = in ^ (2 * count) */
757void
758curve25519_square_times(bignum25519 out, const bignum25519 in, int count) {
759 word32 r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
760 word32 d6,d7,d8,d9,p;
761 word64 m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
762
763 r0 = in[0]; r1 = in[1];
764 r2 = in[2]; r3 = in[3];
765 r4 = in[4]; r5 = in[5];
766 r6 = in[6]; r7 = in[7];
767 r8 = in[8]; r9 = in[9];
768
769 do {
770 m0 = mul32x32_64(r0, r0);
771 r0 *= 2;
772 m1 = mul32x32_64(r0, r1);
773 m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
774 r1 *= 2;
775 m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
776 m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
777 r2 *= 2;
778 m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
779 m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
780 r3 *= 2;
781 m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
782 m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
783 m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
784
785 d6 = r6 * 19;
786 d7 = r7 * 2 * 19;
787 d8 = r8 * 19;
788 d9 = r9 * 2 * 19;
789
790 m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
791 m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
792 m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
793 m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
794 m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
795 m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
796 m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
797 m7 += (mul32x32_64(d9, r8 ));
798 m8 += (mul32x32_64(d9, r9 ));
799
800 r0 = (word32)m0 & reduce_mask_26; c = (m0 >> 26);
801 m1 += c; r1 = (word32)m1 & reduce_mask_25; c = (m1 >> 25);
802 m2 += c; r2 = (word32)m2 & reduce_mask_26; c = (m2 >> 26);
803 m3 += c; r3 = (word32)m3 & reduce_mask_25; c = (m3 >> 25);
804 m4 += c; r4 = (word32)m4 & reduce_mask_26; c = (m4 >> 26);
805 m5 += c; r5 = (word32)m5 & reduce_mask_25; c = (m5 >> 25);
806 m6 += c; r6 = (word32)m6 & reduce_mask_26; c = (m6 >> 26);
807 m7 += c; r7 = (word32)m7 & reduce_mask_25; c = (m7 >> 25);
808 m8 += c; r8 = (word32)m8 & reduce_mask_26; c = (m8 >> 26);
809 m9 += c; r9 = (word32)m9 & reduce_mask_25; p = (word32)(m9 >> 25);
810 m0 = r0 + mul32x32_64(p,19); r0 = (word32)m0 & reduce_mask_26; p = (word32)(m0 >> 26);
811 r1 += p;
812 } while (--count);
813
814 out[0] = r0; out[1] = r1;
815 out[2] = r2; out[3] = r3;
816 out[4] = r4; out[5] = r5;
817 out[6] = r6; out[7] = r7;
818 out[8] = r8; out[9] = r9;
819}
820
821/* Take a little-endian, 32-byte number and expand it into polynomial form */
822void
823curve25519_expand(bignum25519 out, const byte in[32]) {
824 word32 x0,x1,x2,x3,x4,x5,x6,x7;
826 block(x0)(x1)(x2)(x3)(x4)(x5)(x6)(x7);
827
828 out[0] = ( x0 ) & 0x3ffffff;
829 out[1] = ((((word64)x1 << 32) | x0) >> 26) & 0x1ffffff;
830 out[2] = ((((word64)x2 << 32) | x1) >> 19) & 0x3ffffff;
831 out[3] = ((((word64)x3 << 32) | x2) >> 13) & 0x1ffffff;
832 out[4] = (( x3) >> 6) & 0x3ffffff;
833 out[5] = ( x4 ) & 0x1ffffff;
834 out[6] = ((((word64)x5 << 32) | x4) >> 25) & 0x3ffffff;
835 out[7] = ((((word64)x6 << 32) | x5) >> 19) & 0x1ffffff;
836 out[8] = ((((word64)x7 << 32) | x6) >> 12) & 0x3ffffff;
837 out[9] = (( x7) >> 6) & 0x1ffffff;
838}
839
840/* Take a fully reduced polynomial form number and contract it into a
841 * little-endian, 32-byte array
842 */
843void
844curve25519_contract(byte out[32], const bignum25519 in) {
845 bignum25519 f;
846 curve25519_copy(f, in);
847
848 #define carry_pass() \
849 f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
850 f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
851 f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
852 f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
853 f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
854 f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
855 f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
856 f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
857 f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
858
859 #define carry_pass_full() \
860 carry_pass() \
861 f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
862
863 #define carry_pass_final() \
864 carry_pass() \
865 f[9] &= reduce_mask_25;
866
867 carry_pass_full()
868 carry_pass_full()
869
870 /* now t is between 0 and 2^255-1, properly carried. */
871 /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
872 f[0] += 19;
873 carry_pass_full()
874
875 /* now between 19 and 2^255-1 in both cases, and offset by 19. */
876 f[0] += (reduce_mask_26 + 1) - 19;
877 f[1] += (reduce_mask_25 + 1) - 1;
878 f[2] += (reduce_mask_26 + 1) - 1;
879 f[3] += (reduce_mask_25 + 1) - 1;
880 f[4] += (reduce_mask_26 + 1) - 1;
881 f[5] += (reduce_mask_25 + 1) - 1;
882 f[6] += (reduce_mask_26 + 1) - 1;
883 f[7] += (reduce_mask_25 + 1) - 1;
884 f[8] += (reduce_mask_26 + 1) - 1;
885 f[9] += (reduce_mask_25 + 1) - 1;
886
887 /* now between 2^255 and 2^256-20, and offset by 2^255. */
888 carry_pass_final()
889
890 #undef carry_pass
891 #undef carry_full
892 #undef carry_final
893
894 f[1] <<= 2; f[2] <<= 3;
895 f[3] <<= 5; f[4] <<= 6;
896 f[6] <<= 1; f[7] <<= 3;
897 f[8] <<= 4; f[9] <<= 6;
898
899 #define F(i, s) \
900 out[s+0] |= (byte)( f[i] & 0xff); \
901 out[s+1] = (byte)((f[i] >> 8) & 0xff); \
902 out[s+2] = (byte)((f[i] >> 16) & 0xff); \
903 out[s+3] = (byte)((f[i] >> 24) & 0xff);
904
905 out[0] = out[16] = 0;
906 F(0,0); F(1,3);
907 F(2,6); F(3,9);
908 F(4,12); F(5,16);
909 F(6,19); F(7,22);
910 F(8,25); F(9,28);
911 #undef F
912}
913
914/* out = (flag) ? in : out */
915inline void
916curve25519_move_conditional_bytes(byte out[96], const byte in[96], word32 flag) {
917 const word32 nb = flag - 1, b = ~nb;
918 const word32 *inl = (const word32 *)in;
919 word32 *outl = (word32 *)out;
920 outl[0] = (outl[0] & nb) | (inl[0] & b);
921 outl[1] = (outl[1] & nb) | (inl[1] & b);
922 outl[2] = (outl[2] & nb) | (inl[2] & b);
923 outl[3] = (outl[3] & nb) | (inl[3] & b);
924 outl[4] = (outl[4] & nb) | (inl[4] & b);
925 outl[5] = (outl[5] & nb) | (inl[5] & b);
926 outl[6] = (outl[6] & nb) | (inl[6] & b);
927 outl[7] = (outl[7] & nb) | (inl[7] & b);
928 outl[8] = (outl[8] & nb) | (inl[8] & b);
929 outl[9] = (outl[9] & nb) | (inl[9] & b);
930 outl[10] = (outl[10] & nb) | (inl[10] & b);
931 outl[11] = (outl[11] & nb) | (inl[11] & b);
932 outl[12] = (outl[12] & nb) | (inl[12] & b);
933 outl[13] = (outl[13] & nb) | (inl[13] & b);
934 outl[14] = (outl[14] & nb) | (inl[14] & b);
935 outl[15] = (outl[15] & nb) | (inl[15] & b);
936 outl[16] = (outl[16] & nb) | (inl[16] & b);
937 outl[17] = (outl[17] & nb) | (inl[17] & b);
938 outl[18] = (outl[18] & nb) | (inl[18] & b);
939 outl[19] = (outl[19] & nb) | (inl[19] & b);
940 outl[20] = (outl[20] & nb) | (inl[20] & b);
941 outl[21] = (outl[21] & nb) | (inl[21] & b);
942 outl[22] = (outl[22] & nb) | (inl[22] & b);
943 outl[23] = (outl[23] & nb) | (inl[23] & b);
944}
945
946/* if (iswap) swap(a, b) */
947inline void
948curve25519_swap_conditional(bignum25519 a, bignum25519 b, word32 iswap) {
949 const word32 swap = (word32)(-(sword32)iswap);
950 word32 x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
951
952 x0 = swap & (a[0] ^ b[0]); a[0] ^= x0; b[0] ^= x0;
953 x1 = swap & (a[1] ^ b[1]); a[1] ^= x1; b[1] ^= x1;
954 x2 = swap & (a[2] ^ b[2]); a[2] ^= x2; b[2] ^= x2;
955 x3 = swap & (a[3] ^ b[3]); a[3] ^= x3; b[3] ^= x3;
956 x4 = swap & (a[4] ^ b[4]); a[4] ^= x4; b[4] ^= x4;
957 x5 = swap & (a[5] ^ b[5]); a[5] ^= x5; b[5] ^= x5;
958 x6 = swap & (a[6] ^ b[6]); a[6] ^= x6; b[6] ^= x6;
959 x7 = swap & (a[7] ^ b[7]); a[7] ^= x7; b[7] ^= x7;
960 x8 = swap & (a[8] ^ b[8]); a[8] ^= x8; b[8] ^= x8;
961 x9 = swap & (a[9] ^ b[9]); a[9] ^= x9; b[9] ^= x9;
962}
963
964/*
965 * In: b = 2^5 - 2^0
966 * Out: b = 2^250 - 2^0
967 */
968void
969curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
970 ALIGN(16) bignum25519 t0,c;
971
972 /* 2^5 - 2^0 */ /* b */
973 /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
974 /* 2^10 - 2^0 */ curve25519_mul(b, t0, b);
975 /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
976 /* 2^20 - 2^0 */ curve25519_mul(c, t0, b);
977 /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
978 /* 2^40 - 2^0 */ curve25519_mul(t0, t0, c);
979 /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
980 /* 2^50 - 2^0 */ curve25519_mul(b, t0, b);
981 /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
982 /* 2^100 - 2^0 */ curve25519_mul(c, t0, b);
983 /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
984 /* 2^200 - 2^0 */ curve25519_mul(t0, t0, c);
985 /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
986 /* 2^250 - 2^0 */ curve25519_mul(b, t0, b);
987}
988
989/*
990 * z^(p - 2) = z(2^255 - 21)
991 */
992void
993curve25519_recip(bignum25519 out, const bignum25519 z) {
994 ALIGN(16) bignum25519 a,t0,b;
995
996 /* 2 */ curve25519_square_times(a, z, 1); /* a = 2 */
997 /* 8 */ curve25519_square_times(t0, a, 2);
998 /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
999 /* 11 */ curve25519_mul(a, b, a); /* a = 11 */
1000 /* 22 */ curve25519_square_times(t0, a, 1);
1001 /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
1002 /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
1003 /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
1004 /* 2^255 - 21 */ curve25519_mul(out, b, a);
1005}
1006
1007/*
1008 * z^((p-5)/8) = z^(2^252 - 3)
1009 */
1010void
1011curve25519_pow_two252m3(bignum25519 two252m3, const bignum25519 z) {
1012 ALIGN(16) bignum25519 b,c,t0;
1013
1014 /* 2 */ curve25519_square_times(c, z, 1); /* c = 2 */
1015 /* 8 */ curve25519_square_times(t0, c, 2); /* t0 = 8 */
1016 /* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
1017 /* 11 */ curve25519_mul(c, b, c); /* c = 11 */
1018 /* 22 */ curve25519_square_times(t0, c, 1);
1019 /* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
1020 /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
1021 /* 2^252 - 2^2 */ curve25519_square_times(b, b, 2);
1022 /* 2^252 - 3 */ curve25519_mul(two252m3, b, z);
1023}
1024
1025inline void
1026ed25519_hash(byte *hash, const byte *in, size_t inlen) {
1027 SHA512().CalculateDigest(hash, in, inlen);
1028}
1029
1030inline void
1031ed25519_extsk(hash_512bits extsk, const byte sk[32]) {
1032 ed25519_hash(extsk, sk, 32);
1033 extsk[0] &= 248;
1034 extsk[31] &= 127;
1035 extsk[31] |= 64;
1036}
1037
1038void
1039UpdateFromStream(HashTransformation& hash, std::istream& stream)
1040{
1041 SecByteBlock block(4096);
1042 while (stream.read((char*)block.begin(), block.size()))
1043 hash.Update(block, block.size());
1044
1045 std::streamsize rem = stream.gcount();
1046 if (rem)
1047 hash.Update(block, (size_t)rem);
1048
1049 block.SetMark(0);
1050}
1051
1052void
1053ed25519_hram(hash_512bits hram, const byte RS[64], const byte pk[32], const byte *m, size_t mlen) {
1054 SHA512 hash;
1055 hash.Update(RS, 32);
1056 hash.Update(pk, 32);
1057 hash.Update(m, mlen);
1058 hash.Final(hram);
1059}
1060
1061void
1062ed25519_hram(hash_512bits hram, const byte RS[64], const byte pk[32], std::istream& stream) {
1063 SHA512 hash;
1064 hash.Update(RS, 32);
1065 hash.Update(pk, 32);
1066 UpdateFromStream(hash, stream);
1067 hash.Final(hram);
1068}
1069
1070inline bignum256modm_element_t
1071lt_modm(bignum256modm_element_t a, bignum256modm_element_t b) {
1072 return (a - b) >> 31;
1073}
1074
1075/* see HAC, Alg. 14.42 Step 4 */
1076void
1077reduce256_modm(bignum256modm r) {
1078 bignum256modm t;
1079 bignum256modm_element_t b = 0, pb, mask;
1080
1081 /* t = r - m */
1082 pb = 0;
1083 pb += modm_m[0]; b = lt_modm(r[0], pb); t[0] = (r[0] - pb + (b << 30)); pb = b;
1084 pb += modm_m[1]; b = lt_modm(r[1], pb); t[1] = (r[1] - pb + (b << 30)); pb = b;
1085 pb += modm_m[2]; b = lt_modm(r[2], pb); t[2] = (r[2] - pb + (b << 30)); pb = b;
1086 pb += modm_m[3]; b = lt_modm(r[3], pb); t[3] = (r[3] - pb + (b << 30)); pb = b;
1087 pb += modm_m[4]; b = lt_modm(r[4], pb); t[4] = (r[4] - pb + (b << 30)); pb = b;
1088 pb += modm_m[5]; b = lt_modm(r[5], pb); t[5] = (r[5] - pb + (b << 30)); pb = b;
1089 pb += modm_m[6]; b = lt_modm(r[6], pb); t[6] = (r[6] - pb + (b << 30)); pb = b;
1090 pb += modm_m[7]; b = lt_modm(r[7], pb); t[7] = (r[7] - pb + (b << 30)); pb = b;
1091 pb += modm_m[8]; b = lt_modm(r[8], pb); t[8] = (r[8] - pb + (b << 16));
1092
1093 /* keep r if r was smaller than m */
1094 mask = b - 1;
1095 r[0] ^= mask & (r[0] ^ t[0]);
1096 r[1] ^= mask & (r[1] ^ t[1]);
1097 r[2] ^= mask & (r[2] ^ t[2]);
1098 r[3] ^= mask & (r[3] ^ t[3]);
1099 r[4] ^= mask & (r[4] ^ t[4]);
1100 r[5] ^= mask & (r[5] ^ t[5]);
1101 r[6] ^= mask & (r[6] ^ t[6]);
1102 r[7] ^= mask & (r[7] ^ t[7]);
1103 r[8] ^= mask & (r[8] ^ t[8]);
1104}
1105
1106/* Barrett reduction, see HAC, Alg. 14.42 */
1107void
1108barrett_reduce256_modm(bignum256modm r, const bignum256modm q1, const bignum256modm r1) {
1109 bignum256modm q3, r2;
1110 word64 c;
1111 bignum256modm_element_t f, b, pb;
1112
1113 /* q1 = x >> 248 = 264 bits = 9 30 bit elements
1114 q2 = mu * q1
1115 q3 = (q2 / 256(32+1)) = q2 / (2^8)^(32+1) = q2 >> 264
1116 */
1117 c = mul32x32_64(modm_mu[0], q1[7]) + mul32x32_64(modm_mu[1], q1[6]) + mul32x32_64(modm_mu[2], q1[5]) + mul32x32_64(modm_mu[3], q1[4]) + mul32x32_64(modm_mu[4], q1[3]) + mul32x32_64(modm_mu[5], q1[2]) + mul32x32_64(modm_mu[6], q1[1]) + mul32x32_64(modm_mu[7], q1[0]);
1118 c >>= 30;
1119 c += mul32x32_64(modm_mu[0], q1[8]) + mul32x32_64(modm_mu[1], q1[7]) + mul32x32_64(modm_mu[2], q1[6]) + mul32x32_64(modm_mu[3], q1[5]) + mul32x32_64(modm_mu[4], q1[4]) + mul32x32_64(modm_mu[5], q1[3]) + mul32x32_64(modm_mu[6], q1[2]) + mul32x32_64(modm_mu[7], q1[1]) + mul32x32_64(modm_mu[8], q1[0]);
1120 f = (bignum256modm_element_t)c; q3[0] = (f >> 24) & 0x3f; c >>= 30;
1121 c += mul32x32_64(modm_mu[1], q1[8]) + mul32x32_64(modm_mu[2], q1[7]) + mul32x32_64(modm_mu[3], q1[6]) + mul32x32_64(modm_mu[4], q1[5]) + mul32x32_64(modm_mu[5], q1[4]) + mul32x32_64(modm_mu[6], q1[3]) + mul32x32_64(modm_mu[7], q1[2]) + mul32x32_64(modm_mu[8], q1[1]);
1122 f = (bignum256modm_element_t)c; q3[0] |= (f << 6) & 0x3fffffff; q3[1] = (f >> 24) & 0x3f; c >>= 30;
1123 c += mul32x32_64(modm_mu[2], q1[8]) + mul32x32_64(modm_mu[3], q1[7]) + mul32x32_64(modm_mu[4], q1[6]) + mul32x32_64(modm_mu[5], q1[5]) + mul32x32_64(modm_mu[6], q1[4]) + mul32x32_64(modm_mu[7], q1[3]) + mul32x32_64(modm_mu[8], q1[2]);
1124 f = (bignum256modm_element_t)c; q3[1] |= (f << 6) & 0x3fffffff; q3[2] = (f >> 24) & 0x3f; c >>= 30;
1125 c += mul32x32_64(modm_mu[3], q1[8]) + mul32x32_64(modm_mu[4], q1[7]) + mul32x32_64(modm_mu[5], q1[6]) + mul32x32_64(modm_mu[6], q1[5]) + mul32x32_64(modm_mu[7], q1[4]) + mul32x32_64(modm_mu[8], q1[3]);
1126 f = (bignum256modm_element_t)c; q3[2] |= (f << 6) & 0x3fffffff; q3[3] = (f >> 24) & 0x3f; c >>= 30;
1127 c += mul32x32_64(modm_mu[4], q1[8]) + mul32x32_64(modm_mu[5], q1[7]) + mul32x32_64(modm_mu[6], q1[6]) + mul32x32_64(modm_mu[7], q1[5]) + mul32x32_64(modm_mu[8], q1[4]);
1128 f = (bignum256modm_element_t)c; q3[3] |= (f << 6) & 0x3fffffff; q3[4] = (f >> 24) & 0x3f; c >>= 30;
1129 c += mul32x32_64(modm_mu[5], q1[8]) + mul32x32_64(modm_mu[6], q1[7]) + mul32x32_64(modm_mu[7], q1[6]) + mul32x32_64(modm_mu[8], q1[5]);
1130 f = (bignum256modm_element_t)c; q3[4] |= (f << 6) & 0x3fffffff; q3[5] = (f >> 24) & 0x3f; c >>= 30;
1131 c += mul32x32_64(modm_mu[6], q1[8]) + mul32x32_64(modm_mu[7], q1[7]) + mul32x32_64(modm_mu[8], q1[6]);
1132 f = (bignum256modm_element_t)c; q3[5] |= (f << 6) & 0x3fffffff; q3[6] = (f >> 24) & 0x3f; c >>= 30;
1133 c += mul32x32_64(modm_mu[7], q1[8]) + mul32x32_64(modm_mu[8], q1[7]);
1134 f = (bignum256modm_element_t)c; q3[6] |= (f << 6) & 0x3fffffff; q3[7] = (f >> 24) & 0x3f; c >>= 30;
1135 c += mul32x32_64(modm_mu[8], q1[8]);
1136 f = (bignum256modm_element_t)c; q3[7] |= (f << 6) & 0x3fffffff; q3[8] = (bignum256modm_element_t)(c >> 24);
1137
1138 /* r1 = (x mod 256^(32+1)) = x mod (2^8)(31+1) = x & ((1 << 264) - 1)
1139 r2 = (q3 * m) mod (256^(32+1)) = (q3 * m) & ((1 << 264) - 1)
1140 */
1141 c = mul32x32_64(modm_m[0], q3[0]);
1142 r2[0] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1143 c += mul32x32_64(modm_m[0], q3[1]) + mul32x32_64(modm_m[1], q3[0]);
1144 r2[1] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1145 c += mul32x32_64(modm_m[0], q3[2]) + mul32x32_64(modm_m[1], q3[1]) + mul32x32_64(modm_m[2], q3[0]);
1146 r2[2] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1147 c += mul32x32_64(modm_m[0], q3[3]) + mul32x32_64(modm_m[1], q3[2]) + mul32x32_64(modm_m[2], q3[1]) + mul32x32_64(modm_m[3], q3[0]);
1148 r2[3] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1149 c += mul32x32_64(modm_m[0], q3[4]) + mul32x32_64(modm_m[1], q3[3]) + mul32x32_64(modm_m[2], q3[2]) + mul32x32_64(modm_m[3], q3[1]) + mul32x32_64(modm_m[4], q3[0]);
1150 r2[4] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1151 c += mul32x32_64(modm_m[0], q3[5]) + mul32x32_64(modm_m[1], q3[4]) + mul32x32_64(modm_m[2], q3[3]) + mul32x32_64(modm_m[3], q3[2]) + mul32x32_64(modm_m[4], q3[1]) + mul32x32_64(modm_m[5], q3[0]);
1152 r2[5] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1153 c += mul32x32_64(modm_m[0], q3[6]) + mul32x32_64(modm_m[1], q3[5]) + mul32x32_64(modm_m[2], q3[4]) + mul32x32_64(modm_m[3], q3[3]) + mul32x32_64(modm_m[4], q3[2]) + mul32x32_64(modm_m[5], q3[1]) + mul32x32_64(modm_m[6], q3[0]);
1154 r2[6] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1155 c += mul32x32_64(modm_m[0], q3[7]) + mul32x32_64(modm_m[1], q3[6]) + mul32x32_64(modm_m[2], q3[5]) + mul32x32_64(modm_m[3], q3[4]) + mul32x32_64(modm_m[4], q3[3]) + mul32x32_64(modm_m[5], q3[2]) + mul32x32_64(modm_m[6], q3[1]) + mul32x32_64(modm_m[7], q3[0]);
1156 r2[7] = (bignum256modm_element_t)(c & 0x3fffffff); c >>= 30;
1157 c += mul32x32_64(modm_m[0], q3[8]) + mul32x32_64(modm_m[1], q3[7]) + mul32x32_64(modm_m[2], q3[6]) + mul32x32_64(modm_m[3], q3[5]) + mul32x32_64(modm_m[4], q3[4]) + mul32x32_64(modm_m[5], q3[3]) + mul32x32_64(modm_m[6], q3[2]) + mul32x32_64(modm_m[7], q3[1]) + mul32x32_64(modm_m[8], q3[0]);
1158 r2[8] = (bignum256modm_element_t)(c & 0xffffff);
1159
1160 /* r = r1 - r2
1161 if (r < 0) r += (1 << 264) */
1162 pb = 0;
1163 pb += r2[0]; b = lt_modm(r1[0], pb); r[0] = (r1[0] - pb + (b << 30)); pb = b;
1164 pb += r2[1]; b = lt_modm(r1[1], pb); r[1] = (r1[1] - pb + (b << 30)); pb = b;
1165 pb += r2[2]; b = lt_modm(r1[2], pb); r[2] = (r1[2] - pb + (b << 30)); pb = b;
1166 pb += r2[3]; b = lt_modm(r1[3], pb); r[3] = (r1[3] - pb + (b << 30)); pb = b;
1167 pb += r2[4]; b = lt_modm(r1[4], pb); r[4] = (r1[4] - pb + (b << 30)); pb = b;
1168 pb += r2[5]; b = lt_modm(r1[5], pb); r[5] = (r1[5] - pb + (b << 30)); pb = b;
1169 pb += r2[6]; b = lt_modm(r1[6], pb); r[6] = (r1[6] - pb + (b << 30)); pb = b;
1170 pb += r2[7]; b = lt_modm(r1[7], pb); r[7] = (r1[7] - pb + (b << 30)); pb = b;
1171 pb += r2[8]; b = lt_modm(r1[8], pb); r[8] = (r1[8] - pb + (b << 24));
1172
1173 reduce256_modm(r);
1174 reduce256_modm(r);
1175}
1176
1177/* addition modulo m */
1178void
1179add256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y) {
1180 bignum256modm_element_t c;
1181
1182 c = x[0] + y[0]; r[0] = c & 0x3fffffff; c >>= 30;
1183 c += x[1] + y[1]; r[1] = c & 0x3fffffff; c >>= 30;
1184 c += x[2] + y[2]; r[2] = c & 0x3fffffff; c >>= 30;
1185 c += x[3] + y[3]; r[3] = c & 0x3fffffff; c >>= 30;
1186 c += x[4] + y[4]; r[4] = c & 0x3fffffff; c >>= 30;
1187 c += x[5] + y[5]; r[5] = c & 0x3fffffff; c >>= 30;
1188 c += x[6] + y[6]; r[6] = c & 0x3fffffff; c >>= 30;
1189 c += x[7] + y[7]; r[7] = c & 0x3fffffff; c >>= 30;
1190 c += x[8] + y[8]; r[8] = c;
1191
1192 reduce256_modm(r);
1193}
1194
1195/* multiplication modulo m */
1196void
1197mul256_modm(bignum256modm r, const bignum256modm x, const bignum256modm y) {
1198 bignum256modm r1, q1;
1199 word64 c;
1200 bignum256modm_element_t f;
1201
1202 c = mul32x32_64(x[0], y[0]);
1203 f = (bignum256modm_element_t)c; r1[0] = (f & 0x3fffffff); c >>= 30;
1204 c += mul32x32_64(x[0], y[1]) + mul32x32_64(x[1], y[0]);
1205 f = (bignum256modm_element_t)c; r1[1] = (f & 0x3fffffff); c >>= 30;
1206 c += mul32x32_64(x[0], y[2]) + mul32x32_64(x[1], y[1]) + mul32x32_64(x[2], y[0]);
1207 f = (bignum256modm_element_t)c; r1[2] = (f & 0x3fffffff); c >>= 30;
1208 c += mul32x32_64(x[0], y[3]) + mul32x32_64(x[1], y[2]) + mul32x32_64(x[2], y[1]) + mul32x32_64(x[3], y[0]);
1209 f = (bignum256modm_element_t)c; r1[3] = (f & 0x3fffffff); c >>= 30;
1210 c += mul32x32_64(x[0], y[4]) + mul32x32_64(x[1], y[3]) + mul32x32_64(x[2], y[2]) + mul32x32_64(x[3], y[1]) + mul32x32_64(x[4], y[0]);
1211 f = (bignum256modm_element_t)c; r1[4] = (f & 0x3fffffff); c >>= 30;
1212 c += mul32x32_64(x[0], y[5]) + mul32x32_64(x[1], y[4]) + mul32x32_64(x[2], y[3]) + mul32x32_64(x[3], y[2]) + mul32x32_64(x[4], y[1]) + mul32x32_64(x[5], y[0]);
1213 f = (bignum256modm_element_t)c; r1[5] = (f & 0x3fffffff); c >>= 30;
1214 c += mul32x32_64(x[0], y[6]) + mul32x32_64(x[1], y[5]) + mul32x32_64(x[2], y[4]) + mul32x32_64(x[3], y[3]) + mul32x32_64(x[4], y[2]) + mul32x32_64(x[5], y[1]) + mul32x32_64(x[6], y[0]);
1215 f = (bignum256modm_element_t)c; r1[6] = (f & 0x3fffffff); c >>= 30;
1216 c += mul32x32_64(x[0], y[7]) + mul32x32_64(x[1], y[6]) + mul32x32_64(x[2], y[5]) + mul32x32_64(x[3], y[4]) + mul32x32_64(x[4], y[3]) + mul32x32_64(x[5], y[2]) + mul32x32_64(x[6], y[1]) + mul32x32_64(x[7], y[0]);
1217 f = (bignum256modm_element_t)c; r1[7] = (f & 0x3fffffff); c >>= 30;
1218 c += mul32x32_64(x[0], y[8]) + mul32x32_64(x[1], y[7]) + mul32x32_64(x[2], y[6]) + mul32x32_64(x[3], y[5]) + mul32x32_64(x[4], y[4]) + mul32x32_64(x[5], y[3]) + mul32x32_64(x[6], y[2]) + mul32x32_64(x[7], y[1]) + mul32x32_64(x[8], y[0]);
1219 f = (bignum256modm_element_t)c; r1[8] = (f & 0x00ffffff); q1[0] = (f >> 8) & 0x3fffff; c >>= 30;
1220 c += mul32x32_64(x[1], y[8]) + mul32x32_64(x[2], y[7]) + mul32x32_64(x[3], y[6]) + mul32x32_64(x[4], y[5]) + mul32x32_64(x[5], y[4]) + mul32x32_64(x[6], y[3]) + mul32x32_64(x[7], y[2]) + mul32x32_64(x[8], y[1]);
1221 f = (bignum256modm_element_t)c; q1[0] = (q1[0] | (f << 22)) & 0x3fffffff; q1[1] = (f >> 8) & 0x3fffff; c >>= 30;
1222 c += mul32x32_64(x[2], y[8]) + mul32x32_64(x[3], y[7]) + mul32x32_64(x[4], y[6]) + mul32x32_64(x[5], y[5]) + mul32x32_64(x[6], y[4]) + mul32x32_64(x[7], y[3]) + mul32x32_64(x[8], y[2]);
1223 f = (bignum256modm_element_t)c; q1[1] = (q1[1] | (f << 22)) & 0x3fffffff; q1[2] = (f >> 8) & 0x3fffff; c >>= 30;
1224 c += mul32x32_64(x[3], y[8]) + mul32x32_64(x[4], y[7]) + mul32x32_64(x[5], y[6]) + mul32x32_64(x[6], y[5]) + mul32x32_64(x[7], y[4]) + mul32x32_64(x[8], y[3]);
1225 f = (bignum256modm_element_t)c; q1[2] = (q1[2] | (f << 22)) & 0x3fffffff; q1[3] = (f >> 8) & 0x3fffff; c >>= 30;
1226 c += mul32x32_64(x[4], y[8]) + mul32x32_64(x[5], y[7]) + mul32x32_64(x[6], y[6]) + mul32x32_64(x[7], y[5]) + mul32x32_64(x[8], y[4]);
1227 f = (bignum256modm_element_t)c; q1[3] = (q1[3] | (f << 22)) & 0x3fffffff; q1[4] = (f >> 8) & 0x3fffff; c >>= 30;
1228 c += mul32x32_64(x[5], y[8]) + mul32x32_64(x[6], y[7]) + mul32x32_64(x[7], y[6]) + mul32x32_64(x[8], y[5]);
1229 f = (bignum256modm_element_t)c; q1[4] = (q1[4] | (f << 22)) & 0x3fffffff; q1[5] = (f >> 8) & 0x3fffff; c >>= 30;
1230 c += mul32x32_64(x[6], y[8]) + mul32x32_64(x[7], y[7]) + mul32x32_64(x[8], y[6]);
1231 f = (bignum256modm_element_t)c; q1[5] = (q1[5] | (f << 22)) & 0x3fffffff; q1[6] = (f >> 8) & 0x3fffff; c >>= 30;
1232 c += mul32x32_64(x[7], y[8]) + mul32x32_64(x[8], y[7]);
1233 f = (bignum256modm_element_t)c; q1[6] = (q1[6] | (f << 22)) & 0x3fffffff; q1[7] = (f >> 8) & 0x3fffff; c >>= 30;
1234 c += mul32x32_64(x[8], y[8]);
1235 f = (bignum256modm_element_t)c; q1[7] = (q1[7] | (f << 22)) & 0x3fffffff; q1[8] = (f >> 8) & 0x3fffff;
1236
1237 barrett_reduce256_modm(r, q1, r1);
1238}
1239
1240void
1241expand256_modm(bignum256modm out, const byte *in, size_t len) {
1242 byte work[64] = {0};
1243 bignum256modm_element_t x[16];
1244 bignum256modm q1;
1245
1246 memcpy(work, in, len);
1247 x[0] = U8TO32_LE(work + 0);
1248 x[1] = U8TO32_LE(work + 4);
1249 x[2] = U8TO32_LE(work + 8);
1250 x[3] = U8TO32_LE(work + 12);
1251 x[4] = U8TO32_LE(work + 16);
1252 x[5] = U8TO32_LE(work + 20);
1253 x[6] = U8TO32_LE(work + 24);
1254 x[7] = U8TO32_LE(work + 28);
1255 x[8] = U8TO32_LE(work + 32);
1256 x[9] = U8TO32_LE(work + 36);
1257 x[10] = U8TO32_LE(work + 40);
1258 x[11] = U8TO32_LE(work + 44);
1259 x[12] = U8TO32_LE(work + 48);
1260 x[13] = U8TO32_LE(work + 52);
1261 x[14] = U8TO32_LE(work + 56);
1262 x[15] = U8TO32_LE(work + 60);
1263
1264 /* r1 = (x mod 256^(32+1)) = x mod (2^8)(31+1) = x & ((1 << 264) - 1) */
1265 out[0] = ( x[0]) & 0x3fffffff;
1266 out[1] = ((x[ 0] >> 30) | (x[ 1] << 2)) & 0x3fffffff;
1267 out[2] = ((x[ 1] >> 28) | (x[ 2] << 4)) & 0x3fffffff;
1268 out[3] = ((x[ 2] >> 26) | (x[ 3] << 6)) & 0x3fffffff;
1269 out[4] = ((x[ 3] >> 24) | (x[ 4] << 8)) & 0x3fffffff;
1270 out[5] = ((x[ 4] >> 22) | (x[ 5] << 10)) & 0x3fffffff;
1271 out[6] = ((x[ 5] >> 20) | (x[ 6] << 12)) & 0x3fffffff;
1272 out[7] = ((x[ 6] >> 18) | (x[ 7] << 14)) & 0x3fffffff;
1273 out[8] = ((x[ 7] >> 16) | (x[ 8] << 16)) & 0x00ffffff;
1274
1275 /* 8*31 = 248 bits, no need to reduce */
1276 if (len < 32)
1277 return;
1278
1279 /* q1 = x >> 248 = 264 bits = 9 30 bit elements */
1280 q1[0] = ((x[ 7] >> 24) | (x[ 8] << 8)) & 0x3fffffff;
1281 q1[1] = ((x[ 8] >> 22) | (x[ 9] << 10)) & 0x3fffffff;
1282 q1[2] = ((x[ 9] >> 20) | (x[10] << 12)) & 0x3fffffff;
1283 q1[3] = ((x[10] >> 18) | (x[11] << 14)) & 0x3fffffff;
1284 q1[4] = ((x[11] >> 16) | (x[12] << 16)) & 0x3fffffff;
1285 q1[5] = ((x[12] >> 14) | (x[13] << 18)) & 0x3fffffff;
1286 q1[6] = ((x[13] >> 12) | (x[14] << 20)) & 0x3fffffff;
1287 q1[7] = ((x[14] >> 10) | (x[15] << 22)) & 0x3fffffff;
1288 q1[8] = ((x[15] >> 8) );
1289
1290 barrett_reduce256_modm(out, q1, out);
1291}
1292
1293void
1294expand_raw256_modm(bignum256modm out, const byte in[32]) {
1295 bignum256modm_element_t x[8];
1296
1297 x[0] = U8TO32_LE(in + 0);
1298 x[1] = U8TO32_LE(in + 4);
1299 x[2] = U8TO32_LE(in + 8);
1300 x[3] = U8TO32_LE(in + 12);
1301 x[4] = U8TO32_LE(in + 16);
1302 x[5] = U8TO32_LE(in + 20);
1303 x[6] = U8TO32_LE(in + 24);
1304 x[7] = U8TO32_LE(in + 28);
1305
1306 out[0] = ( x[0]) & 0x3fffffff;
1307 out[1] = ((x[ 0] >> 30) | (x[ 1] << 2)) & 0x3fffffff;
1308 out[2] = ((x[ 1] >> 28) | (x[ 2] << 4)) & 0x3fffffff;
1309 out[3] = ((x[ 2] >> 26) | (x[ 3] << 6)) & 0x3fffffff;
1310 out[4] = ((x[ 3] >> 24) | (x[ 4] << 8)) & 0x3fffffff;
1311 out[5] = ((x[ 4] >> 22) | (x[ 5] << 10)) & 0x3fffffff;
1312 out[6] = ((x[ 5] >> 20) | (x[ 6] << 12)) & 0x3fffffff;
1313 out[7] = ((x[ 6] >> 18) | (x[ 7] << 14)) & 0x3fffffff;
1314 out[8] = ((x[ 7] >> 16) ) & 0x0000ffff;
1315}
1316
1317void
1318contract256_modm(byte out[32], const bignum256modm in) {
1319 U32TO8_LE(out + 0, (in[0] ) | (in[1] << 30));
1320 U32TO8_LE(out + 4, (in[1] >> 2) | (in[2] << 28));
1321 U32TO8_LE(out + 8, (in[2] >> 4) | (in[3] << 26));
1322 U32TO8_LE(out + 12, (in[3] >> 6) | (in[4] << 24));
1323 U32TO8_LE(out + 16, (in[4] >> 8) | (in[5] << 22));
1324 U32TO8_LE(out + 20, (in[5] >> 10) | (in[6] << 20));
1325 U32TO8_LE(out + 24, (in[6] >> 12) | (in[7] << 18));
1326 U32TO8_LE(out + 28, (in[7] >> 14) | (in[8] << 16));
1327}
1328
1329void
1330contract256_window4_modm(signed char r[64], const bignum256modm in) {
1331 char carry;
1332 signed char *quads = r;
1333 bignum256modm_element_t i, j, v;
1334
1335 for (i = 0; i < 8; i += 2) {
1336 v = in[i];
1337 for (j = 0; j < 7; j++) {
1338 *quads++ = (v & 15);
1339 v >>= 4;
1340 }
1341 v |= (in[i+1] << 2);
1342 for (j = 0; j < 8; j++) {
1343 *quads++ = (v & 15);
1344 v >>= 4;
1345 }
1346 }
1347
1348 v = in[8];
1349 *quads++ = (v & 15); v >>= 4;
1350 *quads++ = (v & 15); v >>= 4;
1351 *quads++ = (v & 15); v >>= 4;
1352 *quads++ = (v & 15); v >>= 4;
1353
1354 /* making it signed */
1355 carry = 0;
1356 for(i = 0; i < 63; i++) {
1357 r[i] += carry;
1358 r[i+1] += (r[i] >> 4);
1359 r[i] &= 15;
1360 carry = (r[i] >> 3);
1361 r[i] -= (carry << 4);
1362 }
1363 r[63] += carry;
1364}
1365
1366void
1367contract256_slidingwindow_modm(signed char r[256], const bignum256modm s, int windowsize) {
1368 int i,j,k,b;
1369 int m = (1 << (windowsize - 1)) - 1, soplen = 256;
1370 signed char *bits = r;
1371 bignum256modm_element_t v;
1372
1373 /* first put the binary expansion into r */
1374 for (i = 0; i < 8; i++) {
1375 v = s[i];
1376 for (j = 0; j < 30; j++, v >>= 1)
1377 *bits++ = (v & 1);
1378 }
1379 v = s[8];
1380 for (j = 0; j < 16; j++, v >>= 1)
1381 *bits++ = (v & 1);
1382
1383 /* Making it sliding window */
1384 for (j = 0; j < soplen; j++) {
1385 if (!r[j])
1386 continue;
1387
1388 for (b = 1; (b < (soplen - j)) && (b <= 6); b++) {
1389 if ((r[j] + (r[j + b] << b)) <= m) {
1390 r[j] += r[j + b] << b;
1391 r[j + b] = 0;
1392 } else if ((r[j] - (r[j + b] << b)) >= -m) {
1393 r[j] -= r[j + b] << b;
1394 for (k = j + b; k < soplen; k++) {
1395 if (!r[k]) {
1396 r[k] = 1;
1397 break;
1398 }
1399 r[k] = 0;
1400 }
1401 } else if (r[j + b]) {
1402 break;
1403 }
1404 }
1405 }
1406}
1407
1408inline void
1409ge25519_p1p1_to_partial(ge25519 *r, const ge25519_p1p1 *p) {
1410 curve25519_mul(r->x, p->x, p->t);
1411 curve25519_mul(r->y, p->y, p->z);
1412 curve25519_mul(r->z, p->z, p->t);
1413}
1414
1415inline void
1416ge25519_p1p1_to_full(ge25519 *r, const ge25519_p1p1 *p) {
1417 curve25519_mul(r->x, p->x, p->t);
1418 curve25519_mul(r->y, p->y, p->z);
1419 curve25519_mul(r->z, p->z, p->t);
1420 curve25519_mul(r->t, p->x, p->y);
1421}
1422
1423void
1424ge25519_full_to_pniels(ge25519_pniels *p, const ge25519 *r) {
1425 curve25519_sub(p->ysubx, r->y, r->x);
1426 curve25519_add(p->xaddy, r->y, r->x);
1427 curve25519_copy(p->z, r->z);
1428 curve25519_mul(p->t2d, r->t, ge25519_ec2d);
1429}
1430
1431void
1432ge25519_add_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519 *q) {
1433 bignum25519 a,b,c,d,t,u;
1434
1435 curve25519_sub(a, p->y, p->x);
1436 curve25519_add(b, p->y, p->x);
1437 curve25519_sub(t, q->y, q->x);
1438 curve25519_add(u, q->y, q->x);
1439 curve25519_mul(a, a, t);
1440 curve25519_mul(b, b, u);
1441 curve25519_mul(c, p->t, q->t);
1442 curve25519_mul(c, c, ge25519_ec2d);
1443 curve25519_mul(d, p->z, q->z);
1444 curve25519_add(d, d, d);
1445 curve25519_sub(r->x, b, a);
1446 curve25519_add(r->y, b, a);
1447 curve25519_add_after_basic(r->z, d, c);
1448 curve25519_sub_after_basic(r->t, d, c);
1449}
1450
1451void
1452ge25519_double_p1p1(ge25519_p1p1 *r, const ge25519 *p) {
1453 bignum25519 a,b,c;
1454
1455 curve25519_square(a, p->x);
1456 curve25519_square(b, p->y);
1457 curve25519_square(c, p->z);
1458 curve25519_add_reduce(c, c, c);
1459 curve25519_add(r->x, p->x, p->y);
1460 curve25519_square(r->x, r->x);
1461 curve25519_add(r->y, b, a);
1462 curve25519_sub(r->z, b, a);
1463 curve25519_sub_after_basic(r->x, r->x, r->y);
1464 curve25519_sub_after_basic(r->t, c, r->z);
1465}
1466
1467void
1468ge25519_nielsadd2_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_niels *q, byte signbit) {
1469 const bignum25519 *qb = (const bignum25519 *)q;
1470 bignum25519 *rb = (bignum25519 *)r;
1471 bignum25519 a,b,c;
1472
1473 curve25519_sub(a, p->y, p->x);
1474 curve25519_add(b, p->y, p->x);
1475 curve25519_mul(a, a, qb[signbit]); /* x for +, y for - */
1476 curve25519_mul(r->x, b, qb[signbit^1]); /* y for +, x for - */
1477 curve25519_add(r->y, r->x, a);
1478 curve25519_sub(r->x, r->x, a);
1479 curve25519_mul(c, p->t, q->t2d);
1480 curve25519_add_reduce(r->t, p->z, p->z);
1481 curve25519_copy(r->z, r->t);
1482 curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
1483 curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
1484}
1485
1486void
1487ge25519_pnielsadd_p1p1(ge25519_p1p1 *r, const ge25519 *p, const ge25519_pniels *q, byte signbit) {
1488 const bignum25519 *qb = (const bignum25519 *)q;
1489 bignum25519 *rb = (bignum25519 *)r;
1490 bignum25519 a,b,c;
1491
1492 curve25519_sub(a, p->y, p->x);
1493 curve25519_add(b, p->y, p->x);
1494 curve25519_mul(a, a, qb[signbit]); /* ysubx for +, xaddy for - */
1495 curve25519_mul(r->x, b, qb[signbit^1]); /* xaddy for +, ysubx for - */
1496 curve25519_add(r->y, r->x, a);
1497 curve25519_sub(r->x, r->x, a);
1498 curve25519_mul(c, p->t, q->t2d);
1499 curve25519_mul(r->t, p->z, q->z);
1500 curve25519_add_reduce(r->t, r->t, r->t);
1501 curve25519_copy(r->z, r->t);
1502 curve25519_add(rb[2+signbit], rb[2+signbit], c); /* z for +, t for - */
1503 curve25519_sub(rb[2+(signbit^1)], rb[2+(signbit^1)], c); /* t for +, z for - */
1504}
1505
1506void
1507ge25519_double_partial(ge25519 *r, const ge25519 *p) {
1508 ge25519_p1p1 t;
1509 ge25519_double_p1p1(&t, p);
1510 ge25519_p1p1_to_partial(r, &t);
1511}
1512
1513void
1514ge25519_double(ge25519 *r, const ge25519 *p) {
1515 ge25519_p1p1 t;
1516 ge25519_double_p1p1(&t, p);
1517 ge25519_p1p1_to_full(r, &t);
1518}
1519
1520void
1521ge25519_add(ge25519 *r, const ge25519 *p, const ge25519 *q) {
1522 ge25519_p1p1 t;
1523 ge25519_add_p1p1(&t, p, q);
1524 ge25519_p1p1_to_full(r, &t);
1525}
1526
1527void
1528ge25519_nielsadd2(ge25519 *r, const ge25519_niels *q) {
1529 bignum25519 a,b,c,e,f,g,h;
1530
1531 curve25519_sub(a, r->y, r->x);
1532 curve25519_add(b, r->y, r->x);
1533 curve25519_mul(a, a, q->ysubx);
1534 curve25519_mul(e, b, q->xaddy);
1535 curve25519_add(h, e, a);
1536 curve25519_sub(e, e, a);
1537 curve25519_mul(c, r->t, q->t2d);
1538 curve25519_add(f, r->z, r->z);
1539 curve25519_add_after_basic(g, f, c);
1540 curve25519_sub_after_basic(f, f, c);
1541 curve25519_mul(r->x, e, f);
1542 curve25519_mul(r->y, h, g);
1543 curve25519_mul(r->z, g, f);
1544 curve25519_mul(r->t, e, h);
1545}
1546
1547void
1548ge25519_pnielsadd(ge25519_pniels *r, const ge25519 *p, const ge25519_pniels *q) {
1549 bignum25519 a,b,c,x,y,z,t;
1550
1551 curve25519_sub(a, p->y, p->x);
1552 curve25519_add(b, p->y, p->x);
1553 curve25519_mul(a, a, q->ysubx);
1554 curve25519_mul(x, b, q->xaddy);
1555 curve25519_add(y, x, a);
1556 curve25519_sub(x, x, a);
1557 curve25519_mul(c, p->t, q->t2d);
1558 curve25519_mul(t, p->z, q->z);
1559 curve25519_add(t, t, t);
1560 curve25519_add_after_basic(z, t, c);
1561 curve25519_sub_after_basic(t, t, c);
1562 curve25519_mul(r->xaddy, x, t);
1563 curve25519_mul(r->ysubx, y, z);
1564 curve25519_mul(r->z, z, t);
1565 curve25519_mul(r->t2d, x, y);
1566 curve25519_copy(y, r->ysubx);
1567 curve25519_sub(r->ysubx, r->ysubx, r->xaddy);
1568 curve25519_add(r->xaddy, r->xaddy, y);
1569 curve25519_mul(r->t2d, r->t2d, ge25519_ec2d);
1570}
1571
1572void
1573ge25519_pack(byte r[32], const ge25519 *p) {
1574 bignum25519 tx, ty, zi;
1575 byte parity[32];
1576 curve25519_recip(zi, p->z);
1577 curve25519_mul(tx, p->x, zi);
1578 curve25519_mul(ty, p->y, zi);
1579 curve25519_contract(r, ty);
1580 curve25519_contract(parity, tx);
1581 r[31] ^= ((parity[0] & 1) << 7);
1582}
1583
1584int
1585ed25519_verify(const byte *x, const byte *y, size_t len) {
1586 size_t differentbits = 0;
1587 while (len--)
1588 differentbits |= (*x++ ^ *y++);
1589 return (int) (1 & ((differentbits - 1) >> 8));
1590}
1591
1592int
1593ge25519_unpack_negative_vartime(ge25519 *r, const byte p[32]) {
1594 const byte zero[32] = {0};
1595 const bignum25519 one = {1};
1596 byte parity = p[31] >> 7;
1597 byte check[32];
1598 bignum25519 t, root, num, den, d3;
1599
1600 curve25519_expand(r->y, p);
1601 curve25519_copy(r->z, one);
1602 curve25519_square(num, r->y); /* x = y^2 */
1603 curve25519_mul(den, num, ge25519_ecd); /* den = dy^2 */
1604 curve25519_sub_reduce(num, num, r->z); /* x = y^1 - 1 */
1605 curve25519_add(den, den, r->z); /* den = dy^2 + 1 */
1606
1607 /* Computation of sqrt(num/den) */
1608 /* 1.: computation of num^((p-5)/8)*den^((7p-35)/8) = (num*den^7)^((p-5)/8) */
1609 curve25519_square(t, den);
1610 curve25519_mul(d3, t, den);
1611 curve25519_square(r->x, d3);
1612 curve25519_mul(r->x, r->x, den);
1613 curve25519_mul(r->x, r->x, num);
1614 curve25519_pow_two252m3(r->x, r->x);
1615
1616 /* 2. computation of r->x = num * den^3 * (num*den^7)^((p-5)/8) */
1617 curve25519_mul(r->x, r->x, d3);
1618 curve25519_mul(r->x, r->x, num);
1619
1620 /* 3. Check if either of the roots works: */
1621 curve25519_square(t, r->x);
1622 curve25519_mul(t, t, den);
1623 curve25519_sub_reduce(root, t, num);
1624 curve25519_contract(check, root);
1625 if (!ed25519_verify(check, zero, 32)) {
1626 curve25519_add_reduce(t, t, num);
1627 curve25519_contract(check, t);
1628 if (!ed25519_verify(check, zero, 32))
1629 return 0;
1630 curve25519_mul(r->x, r->x, ge25519_sqrtneg1);
1631 }
1632
1633 curve25519_contract(check, r->x);
1634 if ((check[0] & 1) == parity) {
1635 curve25519_copy(t, r->x);
1636 curve25519_neg(r->x, t);
1637 }
1638 curve25519_mul(r->t, r->x, r->y);
1639 return 1;
1640}
1641
1642/* computes [s1]p1 + [s2]basepoint */
1643void
1644ge25519_double_scalarmult_vartime(ge25519 *r, const ge25519 *p1, const bignum256modm s1, const bignum256modm s2) {
1645 signed char slide1[256], slide2[256];
1646 ge25519_pniels pre1[S1_TABLE_SIZE];
1647 ge25519 d1;
1648 ge25519_p1p1 t;
1649 sword32 i;
1650
1651 contract256_slidingwindow_modm(slide1, s1, S1_SWINDOWSIZE);
1652 contract256_slidingwindow_modm(slide2, s2, S2_SWINDOWSIZE);
1653
1654 ge25519_double(&d1, p1);
1655 ge25519_full_to_pniels(pre1, p1);
1656 for (i = 0; i < S1_TABLE_SIZE - 1; i++)
1657 ge25519_pnielsadd(&pre1[i+1], &d1, &pre1[i]);
1658
1659 /* set neutral */
1660 memset(r, 0, sizeof(ge25519));
1661 r->y[0] = 1;
1662 r->z[0] = 1;
1663
1664 i = 255;
1665 while ((i >= 0) && !(slide1[i] | slide2[i]))
1666 i--;
1667
1668 for (; i >= 0; i--) {
1669 ge25519_double_p1p1(&t, r);
1670
1671 if (slide1[i]) {
1672 ge25519_p1p1_to_full(r, &t);
1673 ge25519_pnielsadd_p1p1(&t, r, &pre1[abs(slide1[i]) / 2], (byte)slide1[i] >> 7);
1674 }
1675
1676 if (slide2[i]) {
1677 ge25519_p1p1_to_full(r, &t);
1678 ge25519_nielsadd2_p1p1(&t, r, &ge25519_niels_sliding_multiples[abs(slide2[i]) / 2], (byte)slide2[i] >> 7);
1679 }
1680
1681 ge25519_p1p1_to_partial(r, &t);
1682 }
1683}
1684
1685#if !defined(HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS)
1686
1687word32
1688ge25519_windowb_equal(word32 b, word32 c) {
1689 return ((b ^ c) - 1) >> 31;
1690}
1691
1692void
1693ge25519_scalarmult_base_choose_niels(ge25519_niels *t, const byte table[256][96], word32 pos, signed char b) {
1694 bignum25519 neg;
1695 word32 sign = (word32)((byte)b >> 7);
1696 word32 mask = ~(sign - 1);
1697 word32 u = (b + mask) ^ mask;
1698 word32 i;
1699
1700 /* ysubx, xaddy, t2d in packed form. initialize to ysubx = 1, xaddy = 1, t2d = 0 */
1701 byte packed[96] = {0};
1702 packed[0] = 1;
1703 packed[32] = 1;
1704
1705 for (i = 0; i < 8; i++)
1706 curve25519_move_conditional_bytes(packed, table[(pos * 8) + i], ge25519_windowb_equal(u, i + 1));
1707
1708 /* expand in to t */
1709 curve25519_expand(t->ysubx, packed + 0);
1710 curve25519_expand(t->xaddy, packed + 32);
1711 curve25519_expand(t->t2d , packed + 64);
1712
1713 /* adjust for sign */
1714 curve25519_swap_conditional(t->ysubx, t->xaddy, sign);
1715 curve25519_neg(neg, t->t2d);
1716 curve25519_swap_conditional(t->t2d, neg, sign);
1717}
1718
1719#endif /* HAVE_GE25519_SCALARMULT_BASE_CHOOSE_NIELS */
1720
1721/* computes [s]basepoint */
1722void
1723ge25519_scalarmult_base_niels(ge25519 *r, const byte basepoint_table[256][96], const bignum256modm s) {
1724 signed char b[64];
1725 word32 i;
1726 ge25519_niels t;
1727
1728 contract256_window4_modm(b, s);
1729
1730 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[1]);
1731 curve25519_sub_reduce(r->x, t.xaddy, t.ysubx);
1732 curve25519_add_reduce(r->y, t.xaddy, t.ysubx);
1733 memset(r->z, 0, sizeof(bignum25519));
1734 curve25519_copy(r->t, t.t2d);
1735 r->z[0] = 2;
1736 for (i = 3; i < 64; i += 2) {
1737 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1738 ge25519_nielsadd2(r, &t);
1739 }
1740 ge25519_double_partial(r, r);
1741 ge25519_double_partial(r, r);
1742 ge25519_double_partial(r, r);
1743 ge25519_double(r, r);
1744 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, 0, b[0]);
1745 curve25519_mul(t.t2d, t.t2d, ge25519_ecd);
1746 ge25519_nielsadd2(r, &t);
1747 for(i = 2; i < 64; i += 2) {
1748 ge25519_scalarmult_base_choose_niels(&t, basepoint_table, i / 2, b[i]);
1749 ge25519_nielsadd2(r, &t);
1750 }
1751}
1752
1753ANONYMOUS_NAMESPACE_END
1754NAMESPACE_END // Ed25519
1755NAMESPACE_END // Donna
1756NAMESPACE_END // CryptoPP
1757
1758//***************************** curve25519 *****************************//
1759
1760NAMESPACE_BEGIN(CryptoPP)
1761NAMESPACE_BEGIN(Donna)
1762
1763int curve25519_mult_CXX(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
1764{
1765 using namespace CryptoPP::Donna::X25519;
1766
1768 for (size_t i = 0; i < 32; ++i)
1769 e[i] = secretKey[i];
1770 e[0] &= 0xf8; e[31] &= 0x7f; e[31] |= 0x40;
1771
1772 bignum25519 nqpqx = {1}, nqpqz = {0}, nqz = {1}, nqx;
1773 bignum25519 q, qx, qpqx, qqx, zzz, zmone;
1774 size_t bit, lastbit;
1775
1776 curve25519_expand(q, othersKey);
1777 curve25519_copy(nqx, q);
1778
1779 /* bit 255 is always 0, and bit 254 is always 1, so skip bit 255 and
1780 start pre-swapped on bit 254 */
1781 lastbit = 1;
1782
1783 /* we are doing bits 254..3 in the loop, but are swapping in bits 253..2 */
1784 for (int i = 253; i >= 2; i--) {
1785 curve25519_add(qx, nqx, nqz);
1786 curve25519_sub(nqz, nqx, nqz);
1787 curve25519_add(qpqx, nqpqx, nqpqz);
1788 curve25519_sub(nqpqz, nqpqx, nqpqz);
1789 curve25519_mul(nqpqx, qpqx, nqz);
1790 curve25519_mul(nqpqz, qx, nqpqz);
1791 curve25519_add(qqx, nqpqx, nqpqz);
1792 curve25519_sub(nqpqz, nqpqx, nqpqz);
1793 curve25519_square(nqpqz, nqpqz);
1794 curve25519_square(nqpqx, qqx);
1795 curve25519_mul(nqpqz, nqpqz, q);
1796 curve25519_square(qx, qx);
1797 curve25519_square(nqz, nqz);
1798 curve25519_mul(nqx, qx, nqz);
1799 curve25519_sub(nqz, qx, nqz);
1800 curve25519_scalar_product(zzz, nqz, 121665);
1801 curve25519_add(zzz, zzz, qx);
1802 curve25519_mul(nqz, nqz, zzz);
1803
1804 bit = (e[i/8] >> (i & 7)) & 1;
1805 curve25519_swap_conditional(nqx, nqpqx, bit ^ lastbit);
1806 curve25519_swap_conditional(nqz, nqpqz, bit ^ lastbit);
1807 lastbit = bit;
1808 }
1809
1810 /* the final 3 bits are always zero, so we only need to double */
1811 for (int i = 0; i < 3; i++) {
1812 curve25519_add(qx, nqx, nqz);
1813 curve25519_sub(nqz, nqx, nqz);
1814 curve25519_square(qx, qx);
1815 curve25519_square(nqz, nqz);
1816 curve25519_mul(nqx, qx, nqz);
1817 curve25519_sub(nqz, qx, nqz);
1818 curve25519_scalar_product(zzz, nqz, 121665);
1819 curve25519_add(zzz, zzz, qx);
1820 curve25519_mul(nqz, nqz, zzz);
1821 }
1822
1823 curve25519_recip(zmone, nqz);
1824 curve25519_mul(nqz, nqx, zmone);
1825 curve25519_contract(sharedKey, nqz);
1826
1827 return 0;
1828}
1829
1830int curve25519_mult(byte publicKey[32], const byte secretKey[32])
1831{
1832 using namespace CryptoPP::Donna::X25519;
1833
1834#if (CRYPTOPP_CURVE25519_SSE2)
1835 if (HasSSE2())
1836 return curve25519_mult_SSE2(publicKey, secretKey, basePoint);
1837 else
1838#endif
1839
1840 return curve25519_mult_CXX(publicKey, secretKey, basePoint);
1841}
1842
1843int curve25519_mult(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
1844{
1845#if (CRYPTOPP_CURVE25519_SSE2)
1846 if (HasSSE2())
1847 return curve25519_mult_SSE2(sharedKey, secretKey, othersKey);
1848 else
1849#endif
1850
1851 return curve25519_mult_CXX(sharedKey, secretKey, othersKey);
1852}
1853
1854NAMESPACE_END // Donna
1855NAMESPACE_END // CryptoPP
1856
1857//******************************* ed25519 *******************************//
1858
1859NAMESPACE_BEGIN(CryptoPP)
1860NAMESPACE_BEGIN(Donna)
1861
1862int
1863ed25519_publickey_CXX(byte publicKey[32], const byte secretKey[32])
1864{
1865 using namespace CryptoPP::Donna::Ed25519;
1866
1867 bignum256modm a;
1868 ALIGN(16) ge25519 A;
1869 hash_512bits extsk;
1870
1871 /* A = aB */
1872 ed25519_extsk(extsk, secretKey);
1873 expand256_modm(a, extsk, 32);
1874 ge25519_scalarmult_base_niels(&A, ge25519_niels_base_multiples, a);
1875 ge25519_pack(publicKey, &A);
1876
1877 return 0;
1878}
1879
1880int
1881ed25519_publickey(byte publicKey[32], const byte secretKey[32])
1882{
1883 return ed25519_publickey_CXX(publicKey, secretKey);
1884}
1885
1886int
1887ed25519_sign_CXX(std::istream& stream, const byte sk[32], const byte pk[32], byte RS[64])
1888{
1889 using namespace CryptoPP::Donna::Ed25519;
1890
1891 bignum256modm r, S, a;
1892 ALIGN(16) ge25519 R;
1893 hash_512bits extsk, hashr, hram;
1894
1895 // Unfortunately we need to read the stream twice. The fisrt time calculates
1896 // 'r = H(aExt[32..64], m)'. The second time calculates 'S = H(R,A,m)'. There
1897 // is a data dependency due to hashing 'RS' with 'R = [r]B' that does not
1898 // allow us to read the stream once.
1899 std::streampos where = stream.tellg();
1900
1901 ed25519_extsk(extsk, sk);
1902
1903 /* r = H(aExt[32..64], m) */
1904 SHA512 hash;
1905 hash.Update(extsk + 32, 32);
1906 UpdateFromStream(hash, stream);
1907 hash.Final(hashr);
1908 expand256_modm(r, hashr, 64);
1909
1910 /* R = rB */
1911 ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
1912 ge25519_pack(RS, &R);
1913
1914 // Reset stream for the second digest
1915 stream.clear();
1916 stream.seekg(where);
1917
1918 /* S = H(R,A,m).. */
1919 ed25519_hram(hram, RS, pk, stream);
1920 expand256_modm(S, hram, 64);
1921
1922 /* S = H(R,A,m)a */
1923 expand256_modm(a, extsk, 32);
1924 mul256_modm(S, S, a);
1925
1926 /* S = (r + H(R,A,m)a) */
1927 add256_modm(S, S, r);
1928
1929 /* S = (r + H(R,A,m)a) mod L */
1930 contract256_modm(RS + 32, S);
1931
1932 return 0;
1933}
1934
1935int
1936ed25519_sign_CXX(const byte *m, size_t mlen, const byte sk[32], const byte pk[32], byte RS[64])
1937{
1938 using namespace CryptoPP::Donna::Ed25519;
1939
1940 bignum256modm r, S, a;
1941 ALIGN(16) ge25519 R;
1942 hash_512bits extsk, hashr, hram;
1943
1944 ed25519_extsk(extsk, sk);
1945
1946 /* r = H(aExt[32..64], m) */
1947 SHA512 hash;
1948 hash.Update(extsk + 32, 32);
1949 hash.Update(m, mlen);
1950 hash.Final(hashr);
1951 expand256_modm(r, hashr, 64);
1952
1953 /* R = rB */
1954 ge25519_scalarmult_base_niels(&R, ge25519_niels_base_multiples, r);
1955 ge25519_pack(RS, &R);
1956
1957 /* S = H(R,A,m).. */
1958 ed25519_hram(hram, RS, pk, m, mlen);
1959 expand256_modm(S, hram, 64);
1960
1961 /* S = H(R,A,m)a */
1962 expand256_modm(a, extsk, 32);
1963 mul256_modm(S, S, a);
1964
1965 /* S = (r + H(R,A,m)a) */
1966 add256_modm(S, S, r);
1967
1968 /* S = (r + H(R,A,m)a) mod L */
1969 contract256_modm(RS + 32, S);
1970
1971 return 0;
1972}
1973
1974int
1975ed25519_sign(std::istream& stream, const byte secretKey[32], const byte publicKey[32],
1976 byte signature[64])
1977{
1978 return ed25519_sign_CXX(stream, secretKey, publicKey, signature);
1979}
1980
1981int
1982ed25519_sign(const byte* message, size_t messageLength, const byte secretKey[32],
1983 const byte publicKey[32], byte signature[64])
1984{
1985 return ed25519_sign_CXX(message, messageLength, secretKey, publicKey, signature);
1986}
1987
1988int
1989ed25519_sign_open_CXX(std::istream& stream, const byte pk[32], const byte RS[64]) {
1990
1991 using namespace CryptoPP::Donna::Ed25519;
1992
1993 ALIGN(16) ge25519 R, A;
1994 hash_512bits hash;
1995 bignum256modm hram, S;
1996 byte checkR[32];
1997
1998 if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
1999 return -1;
2000
2001 /* hram = H(R,A,m) */
2002 ed25519_hram(hash, RS, pk, stream);
2003 expand256_modm(hram, hash, 64);
2004
2005 /* S */
2006 expand256_modm(S, RS + 32, 32);
2007
2008 /* SB - H(R,A,m)A */
2009 ge25519_double_scalarmult_vartime(&R, &A, hram, S);
2010 ge25519_pack(checkR, &R);
2011
2012 /* check that R = SB - H(R,A,m)A */
2013 return ed25519_verify(RS, checkR, 32) ? 0 : -1;
2014}
2015
2016int
2017ed25519_sign_open_CXX(const byte *m, size_t mlen, const byte pk[32], const byte RS[64]) {
2018
2019 using namespace CryptoPP::Donna::Ed25519;
2020
2021 ALIGN(16) ge25519 R, A;
2022 hash_512bits hash;
2023 bignum256modm hram, S;
2024 byte checkR[32];
2025
2026 if ((RS[63] & 224) || !ge25519_unpack_negative_vartime(&A, pk))
2027 return -1;
2028
2029 /* hram = H(R,A,m) */
2030 ed25519_hram(hash, RS, pk, m, mlen);
2031 expand256_modm(hram, hash, 64);
2032
2033 /* S */
2034 expand256_modm(S, RS + 32, 32);
2035
2036 /* SB - H(R,A,m)A */
2037 ge25519_double_scalarmult_vartime(&R, &A, hram, S);
2038 ge25519_pack(checkR, &R);
2039
2040 /* check that R = SB - H(R,A,m)A */
2041 return ed25519_verify(RS, checkR, 32) ? 0 : -1;
2042}
2043
2044int
2045ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
2046{
2047 return ed25519_sign_open_CXX(message, messageLength, publicKey, signature);
2048}
2049
2050int
2051ed25519_sign_open(std::istream& stream, const byte publicKey[32], const byte signature[64])
2052{
2053 return ed25519_sign_open_CXX(stream, publicKey, signature);
2054}
2055
2056NAMESPACE_END // Donna
2057NAMESPACE_END // CryptoPP
2058
2059#endif // CRYPTOPP_CURVE25519_32BIT
Fixed size stack-based SecBlock.
Definition: secblock.h:1078
Access a block of memory.
Definition: misc.h:2455
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1085
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
SHA-512 message digest.
Definition: sha.h:142
SecBlock<byte> typedef.
Definition: secblock.h:1058
Library configuration file.
Functions for CPU features and intrinsics.
bool HasSSE2()
Determines SSE2 availability.
Definition: cpu.h:116
@ LITTLE_ENDIAN_ORDER
byte order is little-endian
Definition: cryptlib.h:145
int ed25519_sign_open(const byte *message, size_t messageLength, const byte publicKey[32], const byte signature[64])
Verifies a signature on a message.
int ed25519_sign(const byte *message, size_t messageLength, const byte secretKey[32], const byte publicKey[32], byte signature[64])
Creates a signature on a message.
int ed25519_publickey(byte publicKey[32], const byte secretKey[32])
Creates a public key from a secret key.
int curve25519_mult(byte publicKey[32], const byte secretKey[32])
Generate a public key.
Utility functions for the Crypto++ library.
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
Definition: misc.h:2428
Crypto++ library namespace.
Precompiled header file.
Classes and functions for secure memory allocations.
Classes for SHA-1 and SHA-2 family of message digests.
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:136