2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * Parameters for the field:
29 * - field modulus p = 2^255-19
30 * - R^2 mod p (R = 2^(15k) for the smallest k such that R >= p)
33 static const uint16_t C255_P[] = {
35 0x7FED, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
36 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
42 static const uint16_t C255_R2[] = {
44 0x0169, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
45 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
53 print_int_mont(const char *name, const uint16_t *x)
56 unsigned char tmp[32];
59 printf("%s = ", name);
60 memcpy(y, x, sizeof y);
61 br_i15_from_monty(y, C255_P, P0I);
62 br_i15_encode(tmp, sizeof tmp, y);
63 for (u = 0; u < sizeof tmp; u ++) {
64 printf("%02X", tmp[u]);
70 static const uint16_t C255_A24[] = {
72 0x45D3, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
73 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
77 static const unsigned char GEN[] = {
78 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
84 static const unsigned char ORDER[] = {
85 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
86 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
87 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
88 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
91 static const unsigned char *
92 api_generator(int curve, size_t *len)
99 static const unsigned char *
100 api_order(int curve, size_t *len)
108 api_xoff(int curve, size_t *len)
116 cswap(uint16_t *a, uint16_t *b, uint32_t ctl)
121 for (i = 0; i < 18; i ++) {
126 tw = ctl & (aw ^ bw);
133 c255_add(uint16_t *d, const uint16_t *a, const uint16_t *b)
138 memcpy(t, a, sizeof t);
139 ctl = br_i15_add(t, b, 1);
140 ctl |= NOT(br_i15_sub(t, C255_P, 0));
141 br_i15_sub(t, C255_P, ctl);
142 memcpy(d, t, sizeof t);
146 c255_sub(uint16_t *d, const uint16_t *a, const uint16_t *b)
150 memcpy(t, a, sizeof t);
151 br_i15_add(t, C255_P, br_i15_sub(t, b, 1));
152 memcpy(d, t, sizeof t);
156 c255_mul(uint16_t *d, const uint16_t *a, const uint16_t *b)
160 br_i15_montymul(t, a, b, C255_P, P0I);
161 memcpy(d, t, sizeof t);
165 byteswap(unsigned char *G)
169 for (i = 0; i < 16; i ++) {
179 api_mul(unsigned char *G, size_t Glen,
180 const unsigned char *kb, size_t kblen, int curve)
182 #define ILEN (18 * sizeof(uint16_t))
185 * The a[] and b[] arrays have an extra word to allow for
186 * decoding without using br_i15_decode_reduce().
188 uint16_t x1[18], x2[18], x3[18], z2[18], z3[18];
189 uint16_t a[19], aa[18], b[19], bb[18];
190 uint16_t c[18], d[18], e[18], da[18], cb[18];
198 * Points are encoded over exactly 32 bytes. Multipliers must fit
199 * in 32 bytes as well.
200 * RFC 7748 mandates that the high bit of the last point byte must
201 * be ignored/cleared.
203 if (Glen != 32 || kblen > 32) {
209 * Byteswap the point encoding, because it uses little-endian, and
210 * the generic decoding routine uses big-endian.
215 * Decode the point ('u' coordinate). This should be reduced
216 * modulo p, but we prefer to avoid the dependency on
217 * br_i15_decode_reduce(). Instead, we use br_i15_decode_mod()
218 * with a synthetic modulus of value 2^255 (this must work
219 * since G was truncated to 255 bits), then use a conditional
220 * subtraction. We use br_i15_decode_mod() and not
221 * br_i15_decode(), because the ec_prime_i15 implementation uses
222 * the former but not the latter.
223 * br_i15_decode_reduce(a, G, 32, C255_P);
225 br_i15_zero(b, 0x111);
227 br_i15_decode_mod(a, G, 32, b);
229 br_i15_sub(a, C255_P, NOT(br_i15_sub(a, C255_P, 0)));
232 * Initialise variables x1, x2, z2, x3 and z3. We set all of them
233 * into Montgomery representation.
235 br_i15_montymul(x1, a, C255_R2, C255_P, P0I);
236 memcpy(x3, x1, ILEN);
237 br_i15_zero(z2, C255_P[0]);
238 memcpy(x2, z2, ILEN);
240 memcpy(z3, x2, ILEN);
242 memset(k, 0, (sizeof k) - kblen);
243 memcpy(k + (sizeof k) - kblen, kb, kblen);
249 print_int_mont("x1", x1);
253 for (i = 254; i >= 0; i --) {
256 kt = (k[31 - (i >> 3)] >> (i & 7)) & 1;
263 print_int_mont("x2", x2);
264 print_int_mont("z2", z2);
265 print_int_mont("x3", x3);
266 print_int_mont("z3", z3);
280 print_int_mont("a ", a);
281 print_int_mont("aa", aa);
282 print_int_mont("b ", b);
283 print_int_mont("bb", bb);
284 print_int_mont("e ", e);
285 print_int_mont("c ", c);
286 print_int_mont("d ", d);
287 print_int_mont("da", da);
288 print_int_mont("cb", cb);
291 c255_add(x3, da, cb);
292 c255_mul(x3, x3, x3);
293 c255_sub(z3, da, cb);
294 c255_mul(z3, z3, z3);
295 c255_mul(z3, z3, x1);
296 c255_mul(x2, aa, bb);
297 c255_mul(z2, C255_A24, e);
298 c255_add(z2, z2, aa);
302 print_int_mont("x2", x2);
303 print_int_mont("z2", z2);
304 print_int_mont("x3", x3);
305 print_int_mont("z3", z3);
312 * Inverse z2 with a modular exponentiation. This is a simple
313 * square-and-multiply algorithm; we mutualise most non-squarings
314 * since the exponent contains almost only ones.
317 for (i = 0; i < 15; i ++) {
322 for (i = 0; i < 14; i ++) {
325 for (j = 0; j < 16; j ++) {
330 for (i = 14; i >= 0; i --) {
332 if ((0xFFEB >> i) & 1) {
339 * To avoid a dependency on br_i15_from_monty(), we use a
340 * Montgomery multiplication with 1.
341 * memcpy(x2, b, ILEN);
342 * br_i15_from_monty(x2, C255_P, P0I);
344 br_i15_zero(a, C255_P[0]);
346 br_i15_montymul(x2, a, b, C255_P, P0I);
348 br_i15_encode(G, 32, x2);
356 api_mulgen(unsigned char *R,
357 const unsigned char *x, size_t xlen, int curve)
359 const unsigned char *G;
362 G = api_generator(curve, &Glen);
364 api_mul(R, Glen, x, xlen, curve);
369 api_muladd(unsigned char *A, const unsigned char *B, size_t len,
370 const unsigned char *x, size_t xlen,
371 const unsigned char *y, size_t ylen, int curve)
374 * We don't implement this method, since it is used for ECDSA
375 * only, and there is no ECDSA over Curve25519 (which instead
389 /* see bearssl_ec.h */
390 const br_ec_impl br_ec_c25519_i15 = {
391 (uint32_t)0x20000000,