Mercurial > hg > ucis.core
comparison NaCl/crypto_sign/edwards25519sha512batch.cs @ 20:c873e3dd73fe
Added NaCl cryptography code
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 15 Apr 2013 00:43:48 +0200 |
parents | |
children | 7e9d1cfcc562 |
comparison
equal
deleted
inserted
replaced
19:b9ef273964fd | 20:c873e3dd73fe |
---|---|
1 using System; | |
2 using UCIS.NaCl; | |
3 using System.Runtime.InteropServices; | |
4 | |
5 namespace UCIS.NaCl.crypto_sign { | |
6 public static class edwards25519sha512batch { | |
7 public const int SECRETKEYBYTES = 64; | |
8 public const int PUBLICKEYBYTES = 32; | |
9 public const int CRYPTO_BYTES = 64; | |
10 | |
11 /*Arithmetic modulo the group order n = 2^252 + 27742317777372353535851937790883648493 = 7237005577332262213973186563042994240857116359379907606001950938285454250989 */ | |
12 | |
13 unsafe struct sc25519 { | |
14 public fixed UInt32 v[32]; //crypto_uint32 v[32]; | |
15 | |
16 static UInt32[] m = new UInt32[32] {0xED, 0xD3, 0xF5, 0x5C, 0x1A, 0x63, 0x12, 0x58, 0xD6, 0x9C, 0xF7, 0xA2, 0xDE, 0xF9, 0xDE, 0x14, | |
17 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10}; | |
18 | |
19 static UInt32[] mu = new UInt32[33] {0x1B, 0x13, 0x2C, 0x0A, 0xA3, 0xE5, 0x9C, 0xED, 0xA7, 0x29, 0x63, 0x08, 0x5D, 0x21, 0x06, 0x21, | |
20 0xEB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F}; | |
21 | |
22 /* Reduce coefficients of r before calling reduce_add_sub */ | |
23 static unsafe void reduce_add_sub(sc25519* r) { | |
24 int i, b = 0, pb = 0, nb; | |
25 Byte* t = stackalloc Byte[32]; | |
26 | |
27 for (i = 0; i < 32; i++) { | |
28 b = (r->v[i] < pb + m[i]) ? 1 : 0; | |
29 t[i] = (Byte)(r->v[i] - pb - m[i] + b * 256); | |
30 pb = b; | |
31 } | |
32 nb = 1 - b; | |
33 for (i = 0; i < 32; i++) r->v[i] = (uint)(r->v[i] * b + t[i] * nb); | |
34 } | |
35 | |
36 /* Reduce coefficients of x before calling barrett_reduce */ | |
37 static unsafe void barrett_reduce(sc25519* r, UInt32* x) { // const crypto_uint32 x[64] | |
38 /* See HAC, Alg. 14.42 */ | |
39 UInt32* q2 = stackalloc UInt32[66]; // { 0 }; | |
40 for (int z = 0; z < 66; z++) q2[z] = 0; | |
41 UInt32* q3 = q2 + 33; | |
42 UInt32* r1 = stackalloc UInt32[33]; | |
43 UInt32* r2 = stackalloc UInt32[33]; // { 0 }; | |
44 for (int z = 0; z < 33; z++) r2[z] = 0; | |
45 UInt32 carry; | |
46 int b, pb = 0; | |
47 | |
48 for (int i = 0; i < 33; i++) | |
49 for (int j = 0; j < 33; j++) | |
50 if (i + j >= 31) q2[i + j] += mu[i] * x[j + 31]; | |
51 carry = q2[31] >> 8; | |
52 q2[32] += carry; | |
53 carry = q2[32] >> 8; | |
54 q2[33] += carry; | |
55 | |
56 for (int i = 0; i < 33; i++) r1[i] = x[i]; | |
57 for (int i = 0; i < 32; i++) | |
58 for (int j = 0; j < 33; j++) | |
59 if (i + j < 33) r2[i + j] += m[i] * q3[j]; | |
60 | |
61 for (int i = 0; i < 32; i++) { | |
62 carry = r2[i] >> 8; | |
63 r2[i + 1] += carry; | |
64 r2[i] &= 0xff; | |
65 } | |
66 | |
67 for (int i = 0; i < 32; i++) { | |
68 b = (r1[i] < pb + r2[i]) ? 1 : 0; | |
69 r->v[i] = (uint)(r1[i] - pb - r2[i] + b * 256); | |
70 pb = b; | |
71 } | |
72 | |
73 /* XXX: Can it really happen that r<0?, See HAC, Alg 14.42, Step 3 | |
74 * If so: Handle it here! | |
75 */ | |
76 | |
77 reduce_add_sub(r); | |
78 reduce_add_sub(r); | |
79 } | |
80 | |
81 /* | |
82 static int iszero(const sc25519 *x) | |
83 { | |
84 // Implement | |
85 return 0; | |
86 } | |
87 */ | |
88 | |
89 public static unsafe void sc25519_from32bytes(sc25519* r, Byte* x) { //const unsigned char x[32] | |
90 UInt32* t = stackalloc UInt32[64]; // { 0 }; | |
91 for (int i = 0; i < 32; i++) t[i] = x[i]; | |
92 for (int i = 32; i < 64; i++) t[i] = 0; | |
93 barrett_reduce(r, t); | |
94 } | |
95 | |
96 public static unsafe void sc25519_from64bytes(sc25519* r, Byte* x) { //const unsigned char x[64] | |
97 UInt32* t = stackalloc UInt32[64]; // { 0 }; | |
98 for (int i = 0; i < 64; i++) t[i] = x[i]; | |
99 barrett_reduce(r, t); | |
100 } | |
101 | |
102 /* XXX: What we actually want for crypto_group is probably just something like | |
103 * void sc25519_frombytes(sc25519 *r, const unsigned char *x, size_t xlen) | |
104 */ | |
105 | |
106 public static unsafe void sc25519_to32bytes(Byte* r, sc25519* x) { //unsigned char r[32] | |
107 for (int i = 0; i < 32; i++) r[i] = (Byte)x->v[i]; | |
108 } | |
109 | |
110 public static unsafe void sc25519_add(sc25519* r, sc25519* x, sc25519* y) { | |
111 for (int i = 0; i < 32; i++) r->v[i] = x->v[i] + y->v[i]; | |
112 for (int i = 0; i < 31; i++) { | |
113 uint carry = r->v[i] >> 8; | |
114 r->v[i + 1] += carry; | |
115 r->v[i] &= 0xff; | |
116 } | |
117 reduce_add_sub(r); | |
118 } | |
119 | |
120 public static unsafe void sc25519_mul(sc25519* r, sc25519* x, sc25519* y) { | |
121 UInt32* t = stackalloc UInt32[64]; | |
122 for (int i = 0; i < 64; i++) t[i] = 0; | |
123 | |
124 for (int i = 0; i < 32; i++) | |
125 for (int j = 0; j < 32; j++) | |
126 t[i + j] += x->v[i] * y->v[j]; | |
127 | |
128 /* Reduce coefficients */ | |
129 for (int i = 0; i < 63; i++) { | |
130 uint carry = t[i] >> 8; | |
131 t[i + 1] += carry; | |
132 t[i] &= 0xff; | |
133 } | |
134 | |
135 barrett_reduce(r, t); | |
136 } | |
137 | |
138 public static unsafe void sc25519_square(sc25519* r, sc25519* x) { | |
139 sc25519_mul(r, x, x); | |
140 } | |
141 } | |
142 struct ge25519 { | |
143 public fe25519 x; | |
144 public fe25519 y; | |
145 public fe25519 z; | |
146 public fe25519 t; | |
147 | |
148 struct ge25519_p1p1 { | |
149 public fe25519 x; | |
150 public fe25519 z; | |
151 public fe25519 y; | |
152 public fe25519 t; | |
153 } | |
154 struct ge25519_p2 { | |
155 public fe25519 x; | |
156 public fe25519 y; | |
157 public fe25519 z; | |
158 } | |
159 | |
160 /* Windowsize for fixed-window scalar multiplication */ | |
161 const int WINDOWSIZE = 2; //#define WINDOWSIZE 2 /* Should be 1,2, or 4 */ | |
162 const int WINDOWMASK = ((1 << WINDOWSIZE) - 1); //#define WINDOWMASK ((1<<WINDOWSIZE)-1) | |
163 | |
164 /* packed parameter d in the Edwards curve equation */ | |
165 static Byte[] ecd = new Byte[32] {0xA3, 0x78, 0x59, 0x13, 0xCA, 0x4D, 0xEB, 0x75, 0xAB, 0xD8, 0x41, 0x41, 0x4D, 0x0A, 0x70, 0x00, | |
166 0x98, 0xE8, 0x79, 0x77, 0x79, 0x40, 0xC7, 0x8C, 0x73, 0xFE, 0x6F, 0x2B, 0xEE, 0x6C, 0x03, 0x52}; | |
167 | |
168 /* Packed coordinates of the base point */ | |
169 static Byte[] ge25519_base_x = new Byte[32] {0x1A, 0xD5, 0x25, 0x8F, 0x60, 0x2D, 0x56, 0xC9, 0xB2, 0xA7, 0x25, 0x95, 0x60, 0xC7, 0x2C, 0x69, | |
170 0x5C, 0xDC, 0xD6, 0xFD, 0x31, 0xE2, 0xA4, 0xC0, 0xFE, 0x53, 0x6E, 0xCD, 0xD3, 0x36, 0x69, 0x21}; | |
171 static Byte[] ge25519_base_y = new Byte[32] {0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, | |
172 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}; | |
173 static Byte[] ge25519_base_z = new Byte[32] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
174 static Byte[] ge25519_base_t = new Byte[32] {0xA3, 0xDD, 0xB7, 0xA5, 0xB3, 0x8A, 0xDE, 0x6D, 0xF5, 0x52, 0x51, 0x77, 0x80, 0x9F, 0xF0, 0x20, | |
175 0x7D, 0xE3, 0xAB, 0x64, 0x8E, 0x4E, 0xEA, 0x66, 0x65, 0x76, 0x8B, 0xD7, 0x0F, 0x5F, 0x87, 0x67}; | |
176 | |
177 /* Packed coordinates of the neutral element */ | |
178 static Byte[] ge25519_neutral_x = new Byte[32]; // { 0 }; | |
179 static Byte[] ge25519_neutral_y = new Byte[32] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
180 static Byte[] ge25519_neutral_z = new Byte[32] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
181 static Byte[] ge25519_neutral_t = new Byte[32]; // { 0 }; | |
182 | |
183 static unsafe void p1p1_to_p2(ge25519_p2* r, ge25519_p1p1* p) { | |
184 fe25519.fe25519_mul(&r->x, &p->x, &p->t); | |
185 fe25519.fe25519_mul(&r->y, &p->y, &p->z); | |
186 fe25519.fe25519_mul(&r->z, &p->z, &p->t); | |
187 } | |
188 | |
189 static unsafe void p1p1_to_p3(ge25519* r, ge25519_p1p1* p) { | |
190 p1p1_to_p2((ge25519_p2*)r, p); | |
191 fe25519.fe25519_mul(&r->t, &p->x, &p->y); | |
192 } | |
193 | |
194 /* Constant-time version of: if(b) r = p */ | |
195 static unsafe void cmov_p3(ge25519* r, ge25519* p, Byte b) { | |
196 fe25519.fe25519_cmov(&r->x, &p->x, b); | |
197 fe25519.fe25519_cmov(&r->y, &p->y, b); | |
198 fe25519.fe25519_cmov(&r->z, &p->z, b); | |
199 fe25519.fe25519_cmov(&r->t, &p->t, b); | |
200 } | |
201 | |
202 /* See http://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#doubling-dbl-2008-hwcd */ | |
203 static unsafe void dbl_p1p1(ge25519_p1p1* r, ge25519_p2* p) { | |
204 fe25519 a, b, c, d; | |
205 fe25519.fe25519_square(&a, &p->x); | |
206 fe25519.fe25519_square(&b, &p->y); | |
207 fe25519.fe25519_square(&c, &p->z); | |
208 fe25519.fe25519_add(&c, &c, &c); | |
209 fe25519.fe25519_neg(&d, &a); | |
210 | |
211 fe25519.fe25519_add(&r->x, &p->x, &p->y); | |
212 fe25519.fe25519_square(&r->x, &r->x); | |
213 fe25519.fe25519_sub(&r->x, &r->x, &a); | |
214 fe25519.fe25519_sub(&r->x, &r->x, &b); | |
215 fe25519.fe25519_add(&r->z, &d, &b); | |
216 fe25519.fe25519_sub(&r->t, &r->z, &c); | |
217 fe25519.fe25519_sub(&r->y, &d, &b); | |
218 } | |
219 | |
220 static unsafe void add_p1p1(ge25519_p1p1* r, ge25519* p, ge25519* q) { | |
221 fe25519 a, b, c, d, t, fd; | |
222 fixed (Byte* ecdp = ecd) fe25519.fe25519_unpack(&fd, ecdp); | |
223 | |
224 fe25519.fe25519_sub(&a, &p->y, &p->x); // A = (Y1-X1)*(Y2-X2) | |
225 fe25519.fe25519_sub(&t, &q->y, &q->x); | |
226 fe25519.fe25519_mul(&a, &a, &t); | |
227 fe25519.fe25519_add(&b, &p->x, &p->y); // B = (Y1+X1)*(Y2+X2) | |
228 fe25519.fe25519_add(&t, &q->x, &q->y); | |
229 fe25519.fe25519_mul(&b, &b, &t); | |
230 fe25519.fe25519_mul(&c, &p->t, &q->t); //C = T1*k*T2 | |
231 fe25519.fe25519_mul(&c, &c, &fd); | |
232 fe25519.fe25519_add(&c, &c, &c); //XXX: Can save this addition by precomputing 2*ecd | |
233 fe25519.fe25519_mul(&d, &p->z, &q->z); //D = Z1*2*Z2 | |
234 fe25519.fe25519_add(&d, &d, &d); | |
235 fe25519.fe25519_sub(&r->x, &b, &a); // E = B-A | |
236 fe25519.fe25519_sub(&r->t, &d, &c); // F = D-C | |
237 fe25519.fe25519_add(&r->z, &d, &c); // G = D+C | |
238 fe25519.fe25519_add(&r->y, &b, &a); // H = B+A | |
239 } | |
240 | |
241 /* ******************************************************************** | |
242 * EXPORTED FUNCTIONS | |
243 ******************************************************************** */ | |
244 | |
245 /* return 0 on success, -1 otherwise */ | |
246 public unsafe static Boolean ge25519_unpack_vartime(ge25519* r, Byte* p) { //const unsigned char p[32] | |
247 Boolean ret; | |
248 fe25519 t, fd; | |
249 fe25519.fe25519_setone(&r->z); | |
250 fixed (Byte* ecdp = ecd) fe25519.fe25519_unpack(&fd, ecdp); | |
251 Byte par = (Byte)(p[31] >> 7); | |
252 fe25519.fe25519_unpack(&r->y, p); | |
253 fe25519.fe25519_square(&r->x, &r->y); | |
254 fe25519.fe25519_mul(&t, &r->x, &fd); | |
255 fe25519.fe25519_sub(&r->x, &r->x, &r->z); | |
256 fe25519.fe25519_add(&t, &r->z, &t); | |
257 fe25519.fe25519_invert(&t, &t); | |
258 fe25519.fe25519_mul(&r->x, &r->x, &t); | |
259 ret = fe25519.fe25519_sqrt_vartime(&r->x, &r->x, par); | |
260 fe25519.fe25519_mul(&r->t, &r->x, &r->y); | |
261 return ret; | |
262 } | |
263 | |
264 public static unsafe void ge25519_pack(Byte* r, ge25519* p) { //unsigned char r[32] | |
265 fe25519 tx, ty, zi; | |
266 fe25519.fe25519_invert(&zi, &p->z); | |
267 fe25519.fe25519_mul(&tx, &p->x, &zi); | |
268 fe25519.fe25519_mul(&ty, &p->y, &zi); | |
269 fe25519.fe25519_pack(r, &ty); | |
270 r[31] ^= (Byte)(fe25519.fe25519_getparity(&tx) << 7); | |
271 } | |
272 | |
273 public static unsafe void ge25519_add(ge25519* r, ge25519* p, ge25519* q) { | |
274 ge25519_p1p1 grp1p1; | |
275 add_p1p1(&grp1p1, p, q); | |
276 p1p1_to_p3(r, &grp1p1); | |
277 } | |
278 | |
279 public static unsafe void ge25519_double(ge25519* r, ge25519* p) { | |
280 ge25519_p1p1 grp1p1; | |
281 dbl_p1p1(&grp1p1, (ge25519_p2*)p); | |
282 p1p1_to_p3(r, &grp1p1); | |
283 } | |
284 | |
285 public static unsafe void ge25519_scalarmult(ge25519* r, ge25519* p, sc25519* s) { | |
286 int i, j, k; | |
287 ge25519 g; | |
288 fixed (Byte* ge25519_neutral_xp = ge25519_neutral_x) fe25519.fe25519_unpack(&g.x, ge25519_neutral_xp); | |
289 fixed (Byte* ge25519_neutral_yp = ge25519_neutral_y) fe25519.fe25519_unpack(&g.y, ge25519_neutral_yp); | |
290 fixed (Byte* ge25519_neutral_zp = ge25519_neutral_z) fe25519.fe25519_unpack(&g.z, ge25519_neutral_zp); | |
291 fixed (Byte* ge25519_neutral_tp = ge25519_neutral_t) fe25519.fe25519_unpack(&g.t, ge25519_neutral_tp); | |
292 | |
293 ge25519[] pre = new ge25519[(1 << WINDOWSIZE)]; | |
294 ge25519 t; | |
295 ge25519_p1p1 tp1p1; | |
296 Byte w; | |
297 Byte* sb = stackalloc Byte[32]; | |
298 sc25519.sc25519_to32bytes(sb, s); | |
299 | |
300 // Precomputation | |
301 pre[0] = g; | |
302 pre[1] = *p; | |
303 for (i = 2; i < (1 << WINDOWSIZE); i += 2) { | |
304 fixed (ge25519* prep = pre) { | |
305 dbl_p1p1(&tp1p1, (ge25519_p2*)(prep + i / 2)); | |
306 p1p1_to_p3(prep + i, &tp1p1); | |
307 add_p1p1(&tp1p1, prep + i, prep + 1); | |
308 p1p1_to_p3(prep + i + 1, &tp1p1); | |
309 } | |
310 } | |
311 | |
312 // Fixed-window scalar multiplication | |
313 for (i = 32; i > 0; i--) { | |
314 for (j = 8 - WINDOWSIZE; j >= 0; j -= WINDOWSIZE) { | |
315 for (k = 0; k < WINDOWSIZE - 1; k++) { | |
316 dbl_p1p1(&tp1p1, (ge25519_p2*)&g); | |
317 p1p1_to_p2((ge25519_p2*)&g, &tp1p1); | |
318 } | |
319 dbl_p1p1(&tp1p1, (ge25519_p2*)&g); | |
320 p1p1_to_p3(&g, &tp1p1); | |
321 // Cache-timing resistant loading of precomputed value: | |
322 w = (Byte)((sb[i - 1] >> j) & WINDOWMASK); | |
323 t = pre[0]; | |
324 for (k = 1; k < (1 << WINDOWSIZE); k++) | |
325 fixed (ge25519* prekp = &pre[k]) cmov_p3(&t, prekp, (k == w) ? (Byte)1 : (Byte)0); | |
326 | |
327 add_p1p1(&tp1p1, &g, &t); | |
328 if (j != 0) p1p1_to_p2((ge25519_p2*)&g, &tp1p1); | |
329 else p1p1_to_p3(&g, &tp1p1); /* convert to p3 representation at the end */ | |
330 } | |
331 } | |
332 r->x = g.x; | |
333 r->y = g.y; | |
334 r->z = g.z; | |
335 r->t = g.t; | |
336 } | |
337 | |
338 public unsafe static void ge25519_scalarmult_base(ge25519* r, sc25519* s) { | |
339 /* XXX: Better algorithm for known-base-point scalar multiplication */ | |
340 ge25519 t; | |
341 fixed (Byte* ge25519_base_xp = ge25519_base_x) fe25519.fe25519_unpack(&t.x, ge25519_base_xp); | |
342 fixed (Byte* ge25519_base_yp = ge25519_base_y) fe25519.fe25519_unpack(&t.y, ge25519_base_yp); | |
343 fixed (Byte* ge25519_base_zp = ge25519_base_z) fe25519.fe25519_unpack(&t.z, ge25519_base_zp); | |
344 fixed (Byte* ge25519_base_tp = ge25519_base_t) fe25519.fe25519_unpack(&t.t, ge25519_base_tp); | |
345 ge25519_scalarmult(r, &t, s); | |
346 } | |
347 } | |
348 unsafe struct fe25519 { | |
349 public fixed UInt32 v[32]; // crypto_uint32 v[32]; | |
350 | |
351 const int WINDOWSIZE = 4; //#define WINDOWSIZE 4 /* Should be 1,2, or 4 */ | |
352 const int WINDOWMASK = ((1 << WINDOWSIZE) - 1); //#define WINDOWMASK ((1<<WINDOWSIZE)-1) | |
353 | |
354 static unsafe void reduce_add_sub(fe25519* r) { | |
355 for (int rep = 0; rep < 4; rep++) { | |
356 UInt32 t = r->v[31] >> 7; | |
357 r->v[31] &= 127; | |
358 t *= 19; | |
359 r->v[0] += t; | |
360 for (int i = 0; i < 31; i++) { | |
361 t = r->v[i] >> 8; | |
362 r->v[i + 1] += t; | |
363 r->v[i] &= 255; | |
364 } | |
365 } | |
366 } | |
367 | |
368 unsafe static void reduce_mul(fe25519* r) { | |
369 for (int rep = 0; rep < 2; rep++) { | |
370 UInt32 t = r->v[31] >> 7; | |
371 r->v[31] &= 127; | |
372 t *= 19; | |
373 r->v[0] += t; | |
374 for (int i = 0; i < 31; i++) { | |
375 t = r->v[i] >> 8; | |
376 r->v[i + 1] += t; | |
377 r->v[i] &= 255; | |
378 } | |
379 } | |
380 } | |
381 | |
382 /* reduction modulo 2^255-19 */ | |
383 unsafe static void freeze(fe25519* r) { | |
384 UInt32 m = (r->v[31] == 127) ? 1u : 0; | |
385 for (int i = 30; i > 1; i--) | |
386 m *= (r->v[i] == 255) ? 1u : 0; | |
387 m *= (r->v[0] >= 237) ? 1u : 0; | |
388 | |
389 r->v[31] -= m * 127; | |
390 for (int i = 30; i > 0; i--) | |
391 r->v[i] -= m * 255; | |
392 r->v[0] -= m * 237; | |
393 } | |
394 | |
395 /*freeze input before calling isone*/ | |
396 unsafe static Boolean isone(fe25519* x) { | |
397 bool r = x->v[0] == 1; | |
398 for (int i = 1; i < 32; i++) r &= (x->v[i] == 0); | |
399 return r; | |
400 } | |
401 | |
402 /*freeze input before calling iszero*/ | |
403 unsafe static Boolean iszero(fe25519* x) { | |
404 bool r = (x->v[0] == 0); | |
405 for (int i = 1; i < 32; i++) r &= (x->v[i] == 0); | |
406 return r; | |
407 } | |
408 | |
409 | |
410 unsafe static Boolean issquare(fe25519* x) { | |
411 Byte[] e = new Byte[32] { 0xf6, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f }; /* (p-1)/2 */ | |
412 fe25519 t; | |
413 | |
414 fixed (Byte* ep = e) fe25519_pow(&t, x, ep); | |
415 freeze(&t); | |
416 return isone(&t) || iszero(&t); | |
417 } | |
418 | |
419 public static unsafe void fe25519_unpack(fe25519* r, Byte* x) { //const unsigned char x[32] | |
420 for (int i = 0; i < 32; i++) r->v[i] = x[i]; | |
421 r->v[31] &= 127; | |
422 } | |
423 | |
424 /* Assumes input x being reduced mod 2^255 */ | |
425 public static unsafe void fe25519_pack(Byte* r, fe25519* x) { //unsigned char r[32] | |
426 for (int i = 0; i < 32; i++) | |
427 r[i] = (byte)x->v[i]; | |
428 | |
429 /* freeze byte array */ | |
430 UInt32 m = (r[31] == 127) ? 1u : 0; /* XXX: some compilers might use branches; fix */ | |
431 for (int i = 30; i > 1; i--) | |
432 m *= (r[i] == 255) ? 1u : 0; | |
433 m *= (r[0] >= 237) ? 1u : 0; | |
434 r[31] -= (byte)(m * 127); | |
435 for (int i = 30; i > 0; i--) | |
436 r[i] -= (byte)(m * 255); | |
437 r[0] -= (byte)(m * 237); | |
438 } | |
439 | |
440 public static unsafe void fe25519_cmov(fe25519* r, fe25519* x, Byte b) { | |
441 Byte nb = (Byte)(1 - b); | |
442 for (int i = 0; i < 32; i++) r->v[i] = nb * r->v[i] + b * x->v[i]; | |
443 } | |
444 | |
445 public static unsafe Byte fe25519_getparity(fe25519* x) { | |
446 fe25519 t = new fe25519(); | |
447 for (int i = 0; i < 32; i++) t.v[i] = x->v[i]; | |
448 freeze(&t); | |
449 return (Byte)(t.v[0] & 1); | |
450 } | |
451 | |
452 public static unsafe void fe25519_setone(fe25519* r) { | |
453 r->v[0] = 1; | |
454 for (int i = 1; i < 32; i++) r->v[i] = 0; | |
455 } | |
456 | |
457 static unsafe void fe25519_setzero(fe25519* r) { | |
458 for (int i = 0; i < 32; i++) r->v[i] = 0; | |
459 } | |
460 | |
461 public static unsafe void fe25519_neg(fe25519* r, fe25519* x) { | |
462 fe25519 t = new fe25519(); | |
463 for (int i = 0; i < 32; i++) t.v[i] = x->v[i]; | |
464 fe25519_setzero(r); | |
465 fe25519_sub(r, r, &t); | |
466 } | |
467 | |
468 public static unsafe void fe25519_add(fe25519* r, fe25519* x, fe25519* y) { | |
469 for (int i = 0; i < 32; i++) r->v[i] = x->v[i] + y->v[i]; | |
470 reduce_add_sub(r); | |
471 } | |
472 | |
473 public static unsafe void fe25519_sub(fe25519* r, fe25519* x, fe25519* y) { | |
474 UInt32* t = stackalloc UInt32[32]; | |
475 t[0] = x->v[0] + 0x1da; | |
476 t[31] = x->v[31] + 0xfe; | |
477 for (int i = 1; i < 31; i++) t[i] = x->v[i] + 0x1fe; | |
478 for (int i = 0; i < 32; i++) r->v[i] = t[i] - y->v[i]; | |
479 reduce_add_sub(r); | |
480 } | |
481 | |
482 public static unsafe void fe25519_mul(fe25519* r, fe25519* x, fe25519* y) { | |
483 UInt32* t = stackalloc UInt32[63]; | |
484 for (int i = 0; i < 63; i++) t[i] = 0; | |
485 for (int i = 0; i < 32; i++) | |
486 for (int j = 0; j < 32; j++) | |
487 t[i + j] += x->v[i] * y->v[j]; | |
488 | |
489 for (int i = 32; i < 63; i++) | |
490 r->v[i - 32] = t[i - 32] + 38 * t[i]; | |
491 r->v[31] = t[31]; /* result now in r[0]...r[31] */ | |
492 | |
493 reduce_mul(r); | |
494 } | |
495 | |
496 public static unsafe void fe25519_square(fe25519* r, fe25519* x) { | |
497 fe25519_mul(r, x, x); | |
498 } | |
499 | |
500 /*XXX: Make constant time! */ | |
501 public static unsafe void fe25519_pow(fe25519* r, fe25519* x, Byte* e) { | |
502 /* | |
503 fe25519 g; | |
504 fe25519_setone(&g); | |
505 int i; | |
506 unsigned char j; | |
507 for(i=32;i>0;i--) | |
508 { | |
509 for(j=128;j>0;j>>=1) | |
510 { | |
511 fe25519_square(&g,&g); | |
512 if(e[i-1] & j) | |
513 fe25519_mul(&g,&g,x); | |
514 } | |
515 } | |
516 for(i=0;i<32;i++) r->v[i] = g.v[i]; | |
517 */ | |
518 fe25519 g; | |
519 fe25519_setone(&g); | |
520 fe25519[] pre = new fe25519[(1 << WINDOWSIZE)]; | |
521 fe25519 t; | |
522 Byte w; | |
523 | |
524 // Precomputation | |
525 fixed (fe25519* prep = pre) fe25519_setone(prep); | |
526 pre[1] = *x; | |
527 for (int i = 2; i < (1 << WINDOWSIZE); i += 2) { | |
528 fixed (fe25519* prep = pre) { | |
529 fe25519_square(prep + i, prep + i / 2); | |
530 fe25519_mul(prep + i + 1, prep + i, prep + 1); | |
531 } | |
532 } | |
533 | |
534 // Fixed-window scalar multiplication | |
535 for (int i = 32; i > 0; i--) { | |
536 for (int j = 8 - WINDOWSIZE; j >= 0; j -= WINDOWSIZE) { | |
537 for (int k = 0; k < WINDOWSIZE; k++) | |
538 fe25519_square(&g, &g); | |
539 // Cache-timing resistant loading of precomputed value: | |
540 w = (Byte)((e[i - 1] >> j) & WINDOWMASK); | |
541 t = pre[0]; | |
542 for (int k = 1; k < (1 << WINDOWSIZE); k++) fixed (fe25519* prekp = &pre[k]) fe25519_cmov(&t, prekp, (k == w) ? (Byte)1 : (Byte)0); | |
543 fe25519_mul(&g, &g, &t); | |
544 } | |
545 } | |
546 *r = g; | |
547 } | |
548 | |
549 /* Return 0 on success, 1 otherwise */ | |
550 public static unsafe Boolean fe25519_sqrt_vartime(fe25519* r, fe25519* x, Byte parity) { | |
551 /* See HAC, Alg. 3.37 */ | |
552 if (!issquare(x)) return true; | |
553 Byte[] e = new Byte[32] { 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f }; /* (p-1)/4 */ | |
554 Byte[] e2 = new Byte[32] { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f }; /* (p+3)/8 */ | |
555 Byte[] e3 = new Byte[32] { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f }; /* (p-5)/8 */ | |
556 fe25519 p = new fe25519(); // { { 0 } }; | |
557 fe25519 d; | |
558 fixed (Byte* ep = e) fe25519.fe25519_pow(&d, x, ep); | |
559 freeze(&d); | |
560 if (isone(&d)) | |
561 fixed (Byte* e2p = e2) fe25519.fe25519_pow(r, x, e2p); | |
562 else { | |
563 for (int i = 0; i < 32; i++) | |
564 d.v[i] = 4 * x->v[i]; | |
565 fixed (Byte* e3p = e3) fe25519.fe25519_pow(&d, &d, e3p); | |
566 for (int i = 0; i < 32; i++) | |
567 r->v[i] = 2 * x->v[i]; | |
568 fe25519_mul(r, r, &d); | |
569 } | |
570 freeze(r); | |
571 if ((r->v[0] & 1) != (parity & 1)) { | |
572 fe25519_sub(r, &p, r); | |
573 } | |
574 return false; | |
575 } | |
576 | |
577 public static unsafe void fe25519_invert(fe25519* r, fe25519* x) { | |
578 fe25519 z2; | |
579 fe25519 z9; | |
580 fe25519 z11; | |
581 fe25519 z2_5_0; | |
582 fe25519 z2_10_0; | |
583 fe25519 z2_20_0; | |
584 fe25519 z2_50_0; | |
585 fe25519 z2_100_0; | |
586 fe25519 t0; | |
587 fe25519 t1; | |
588 | |
589 /* 2 */ | |
590 fe25519_square(&z2, x); | |
591 /* 4 */ | |
592 fe25519_square(&t1, &z2); | |
593 /* 8 */ | |
594 fe25519_square(&t0, &t1); | |
595 /* 9 */ | |
596 fe25519_mul(&z9, &t0, x); | |
597 /* 11 */ | |
598 fe25519_mul(&z11, &z9, &z2); | |
599 /* 22 */ | |
600 fe25519_square(&t0, &z11); | |
601 /* 2^5 - 2^0 = 31 */ | |
602 fe25519_mul(&z2_5_0, &t0, &z9); | |
603 | |
604 /* 2^6 - 2^1 */ | |
605 fe25519_square(&t0, &z2_5_0); | |
606 /* 2^7 - 2^2 */ | |
607 fe25519_square(&t1, &t0); | |
608 /* 2^8 - 2^3 */ | |
609 fe25519_square(&t0, &t1); | |
610 /* 2^9 - 2^4 */ | |
611 fe25519_square(&t1, &t0); | |
612 /* 2^10 - 2^5 */ | |
613 fe25519_square(&t0, &t1); | |
614 /* 2^10 - 2^0 */ | |
615 fe25519_mul(&z2_10_0, &t0, &z2_5_0); | |
616 | |
617 /* 2^11 - 2^1 */ | |
618 fe25519_square(&t0, &z2_10_0); | |
619 /* 2^12 - 2^2 */ | |
620 fe25519_square(&t1, &t0); | |
621 /* 2^20 - 2^10 */ | |
622 for (int i = 2; i < 10; i += 2) { fe25519_square(&t0, &t1); fe25519_square(&t1, &t0); } | |
623 /* 2^20 - 2^0 */ | |
624 fe25519_mul(&z2_20_0, &t1, &z2_10_0); | |
625 | |
626 /* 2^21 - 2^1 */ | |
627 fe25519_square(&t0, &z2_20_0); | |
628 /* 2^22 - 2^2 */ | |
629 fe25519_square(&t1, &t0); | |
630 /* 2^40 - 2^20 */ | |
631 for (int i = 2; i < 20; i += 2) { fe25519_square(&t0, &t1); fe25519_square(&t1, &t0); } | |
632 /* 2^40 - 2^0 */ | |
633 fe25519_mul(&t0, &t1, &z2_20_0); | |
634 | |
635 /* 2^41 - 2^1 */ | |
636 fe25519_square(&t1, &t0); | |
637 /* 2^42 - 2^2 */ | |
638 fe25519_square(&t0, &t1); | |
639 /* 2^50 - 2^10 */ | |
640 for (int i = 2; i < 10; i += 2) { fe25519_square(&t1, &t0); fe25519_square(&t0, &t1); } | |
641 /* 2^50 - 2^0 */ | |
642 fe25519_mul(&z2_50_0, &t0, &z2_10_0); | |
643 | |
644 /* 2^51 - 2^1 */ | |
645 fe25519_square(&t0, &z2_50_0); | |
646 /* 2^52 - 2^2 */ | |
647 fe25519_square(&t1, &t0); | |
648 /* 2^100 - 2^50 */ | |
649 for (int i = 2; i < 50; i += 2) { fe25519_square(&t0, &t1); fe25519_square(&t1, &t0); } | |
650 /* 2^100 - 2^0 */ | |
651 fe25519_mul(&z2_100_0, &t1, &z2_50_0); | |
652 | |
653 /* 2^101 - 2^1 */ | |
654 fe25519_square(&t1, &z2_100_0); | |
655 /* 2^102 - 2^2 */ | |
656 fe25519_square(&t0, &t1); | |
657 /* 2^200 - 2^100 */ | |
658 for (int i = 2; i < 100; i += 2) { fe25519_square(&t1, &t0); fe25519_square(&t0, &t1); } | |
659 /* 2^200 - 2^0 */ | |
660 fe25519_mul(&t1, &t0, &z2_100_0); | |
661 | |
662 /* 2^201 - 2^1 */ | |
663 fe25519_square(&t0, &t1); | |
664 /* 2^202 - 2^2 */ | |
665 fe25519_square(&t1, &t0); | |
666 /* 2^250 - 2^50 */ | |
667 for (int i = 2; i < 50; i += 2) { fe25519_square(&t0, &t1); fe25519_square(&t1, &t0); } | |
668 /* 2^250 - 2^0 */ | |
669 fe25519_mul(&t0, &t1, &z2_50_0); | |
670 | |
671 /* 2^251 - 2^1 */ | |
672 fe25519_square(&t1, &t0); | |
673 /* 2^252 - 2^2 */ | |
674 fe25519_square(&t0, &t1); | |
675 /* 2^253 - 2^3 */ | |
676 fe25519_square(&t1, &t0); | |
677 /* 2^254 - 2^4 */ | |
678 fe25519_square(&t0, &t1); | |
679 /* 2^255 - 2^5 */ | |
680 fe25519_square(&t1, &t0); | |
681 /* 2^255 - 21 */ | |
682 fe25519_mul(r, &t1, &z11); | |
683 } | |
684 } | |
685 | |
686 public static unsafe void crypto_sign_keypair(out Byte[] pk, out Byte[] sk) { | |
687 sc25519 scsk; | |
688 ge25519 gepk; | |
689 | |
690 sk = new Byte[SECRETKEYBYTES]; | |
691 pk = new Byte[PUBLICKEYBYTES]; | |
692 randombytes.generate(sk); | |
693 fixed (Byte* skp = sk) crypto_hash.sha512.crypto_hash(skp, skp, 32); | |
694 sk[0] &= 248; | |
695 sk[31] &= 127; | |
696 sk[31] |= 64; | |
697 | |
698 fixed (Byte* skp = sk) sc25519.sc25519_from32bytes(&scsk, skp); | |
699 | |
700 ge25519.ge25519_scalarmult_base(&gepk, &scsk); | |
701 fixed (Byte* pkp = pk) ge25519.ge25519_pack(pkp, &gepk); | |
702 } | |
703 | |
704 public static unsafe Byte[] crypto_sign(Byte[] m, Byte[] sk) { | |
705 if (sk.Length != SECRETKEYBYTES) throw new ArgumentException("sk.Length != SECRETKEYBYTES"); | |
706 Byte[] sm = new Byte[m.Length + 64]; | |
707 UInt64 smlen; | |
708 fixed (Byte* smp = sm, mp = m, skp = sk) crypto_sign(smp, out smlen, mp, (ulong)m.Length, skp); | |
709 return sm; | |
710 } | |
711 public static unsafe void crypto_sign(Byte* sm, out UInt64 smlen, Byte* m, UInt64 mlen, Byte* sk) { | |
712 sc25519 sck, scs, scsk; | |
713 ge25519 ger; | |
714 Byte* r = stackalloc Byte[32]; | |
715 Byte* s = stackalloc Byte[32]; | |
716 Byte* hmg = stackalloc Byte[crypto_hash.sha512.BYTES]; | |
717 Byte* hmr = stackalloc Byte[crypto_hash.sha512.BYTES]; | |
718 | |
719 smlen = mlen + 64; | |
720 for (UInt64 i = 0; i < mlen; i++) sm[32 + i] = m[i]; | |
721 for (int i = 0; i < 32; i++) sm[i] = sk[32 + i]; | |
722 crypto_hash.sha512.crypto_hash(hmg, sm, mlen + 32); /* Generate k as h(m,sk[32],...,sk[63]) */ | |
723 | |
724 sc25519.sc25519_from64bytes(&sck, hmg); | |
725 ge25519.ge25519_scalarmult_base(&ger, &sck); | |
726 ge25519.ge25519_pack(r, &ger); | |
727 | |
728 for (int i = 0; i < 32; i++) sm[i] = r[i]; | |
729 | |
730 crypto_hash.sha512.crypto_hash(hmr, sm, mlen + 32); /* Compute h(m,r) */ | |
731 sc25519.sc25519_from64bytes(&scs, hmr); | |
732 sc25519.sc25519_mul(&scs, &scs, &sck); | |
733 | |
734 sc25519.sc25519_from32bytes(&scsk, sk); | |
735 sc25519.sc25519_add(&scs, &scs, &scsk); | |
736 | |
737 sc25519.sc25519_to32bytes(s, &scs); /* cat s */ | |
738 for (UInt64 i = 0; i < 32; i++) | |
739 sm[mlen + 32 + i] = s[i]; | |
740 } | |
741 | |
742 public static unsafe Byte[] crypto_sign_open(Byte[] sm, Byte[] pk) { | |
743 if (pk.Length != PUBLICKEYBYTES) throw new ArgumentException("pk.Length != PUBLICKEYBYTES"); | |
744 Byte[] m = new Byte[sm.Length - 64]; | |
745 UInt64 mlen; | |
746 fixed (Byte* smp = sm, mp = m, pkp = pk) { | |
747 if (crypto_sign_open(mp, out mlen, smp, (ulong)sm.Length, pkp) != 0) return null; | |
748 } | |
749 return m; | |
750 } | |
751 public static unsafe int crypto_sign_open(Byte* m, out UInt64 mlen, Byte* sm, UInt64 smlen, Byte* pk) { | |
752 mlen = 0; | |
753 if (smlen < 64) return -1; | |
754 | |
755 Byte* t1 = stackalloc Byte[32], t2 = stackalloc Byte[32]; | |
756 ge25519 get1, get2, gepk; | |
757 sc25519 schmr, scs; | |
758 Byte* hmr = stackalloc Byte[crypto_hash.sha512.BYTES]; | |
759 | |
760 if (ge25519.ge25519_unpack_vartime(&get1, sm)) return -1; | |
761 if (ge25519.ge25519_unpack_vartime(&gepk, pk)) return -1; | |
762 | |
763 crypto_hash.sha512.crypto_hash(hmr, sm, smlen - 32); | |
764 | |
765 sc25519.sc25519_from64bytes(&schmr, hmr); | |
766 ge25519.ge25519_scalarmult(&get1, &get1, &schmr); | |
767 ge25519.ge25519_add(&get1, &get1, &gepk); | |
768 ge25519.ge25519_pack(t1, &get1); | |
769 | |
770 sc25519.sc25519_from32bytes(&scs, &sm[smlen - 32]); | |
771 ge25519.ge25519_scalarmult_base(&get2, &scs); | |
772 ge25519.ge25519_pack(t2, &get2); | |
773 | |
774 if (m != null) for (UInt64 i = 0; i < smlen - 64; i++) m[i] = sm[i + 32]; | |
775 mlen = smlen - 64; | |
776 | |
777 return crypto_verify._32.crypto_verify(t1, t2); | |
778 } | |
779 } | |
780 } |