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 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x14, 0xDE, 0xF9, 0xDE, 0xA2, 0xF7, 0x9C, 0xD6,
88 0x58, 0x12, 0x63, 0x1A, 0x5C, 0xF5, 0xD3, 0xED
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 cswap(uint16_t *a
, uint16_t *b
, uint32_t ctl
)
113 for (i
= 0; i
< 18; i
++) {
118 tw
= ctl
& (aw
^ bw
);
125 c255_add(uint16_t *d
, const uint16_t *a
, const uint16_t *b
)
130 memcpy(t
, a
, sizeof t
);
131 ctl
= br_i15_add(t
, b
, 1);
132 ctl
|= NOT(br_i15_sub(t
, C255_P
, 0));
133 br_i15_sub(t
, C255_P
, ctl
);
134 memcpy(d
, t
, sizeof t
);
138 c255_sub(uint16_t *d
, const uint16_t *a
, const uint16_t *b
)
142 memcpy(t
, a
, sizeof t
);
143 br_i15_add(t
, C255_P
, br_i15_sub(t
, b
, 1));
144 memcpy(d
, t
, sizeof t
);
148 c255_mul(uint16_t *d
, const uint16_t *a
, const uint16_t *b
)
152 br_i15_montymul(t
, a
, b
, C255_P
, P0I
);
153 memcpy(d
, t
, sizeof t
);
157 byteswap(unsigned char *G
)
161 for (i
= 0; i
< 16; i
++) {
171 api_mul(unsigned char *G
, size_t Glen
,
172 const unsigned char *kb
, size_t kblen
, int curve
)
174 uint16_t x1
[18], x2
[18], x3
[18], z2
[18], z3
[18];
175 uint16_t a
[18], aa
[18], b
[18], bb
[18];
176 uint16_t c
[18], d
[18], e
[18], da
[18], cb
[18];
184 * Points are encoded over exactly 32 bytes. Multipliers must fit
185 * in 32 bytes as well.
186 * RFC 7748 mandates that the high bit of the last point byte must
187 * be ignored/cleared.
189 if (Glen
!= 32 || kblen
> 32) {
195 * Byteswap the point encoding, because it uses little-endian, and
196 * the generic decoding routine uses big-endian.
201 * Initialise variables x1, x2, z2, x3 and z3. We set all of them
202 * into Montgomery representation.
204 br_i15_decode_reduce(a
, G
, 32, C255_P
);
205 br_i15_montymul(x1
, a
, C255_R2
, C255_P
, P0I
);
206 memcpy(x3
, x1
, sizeof x1
);
207 br_i15_zero(z2
, C255_P
[0]);
208 memcpy(x2
, z2
, sizeof z2
);
210 memcpy(z3
, x2
, sizeof x2
);
212 memcpy(k
, kb
, kblen
);
213 memset(k
+ kblen
, 0, (sizeof k
) - kblen
);
219 print_int_mont("x1", x1);
223 for (i
= 254; i
>= 0; i
--) {
226 kt
= (k
[i
>> 3] >> (i
& 7)) & 1;
233 print_int_mont("x2", x2);
234 print_int_mont("z2", z2);
235 print_int_mont("x3", x3);
236 print_int_mont("z3", z3);
250 print_int_mont("a ", a);
251 print_int_mont("aa", aa);
252 print_int_mont("b ", b);
253 print_int_mont("bb", bb);
254 print_int_mont("e ", e);
255 print_int_mont("c ", c);
256 print_int_mont("d ", d);
257 print_int_mont("da", da);
258 print_int_mont("cb", cb);
261 c255_add(x3
, da
, cb
);
262 c255_mul(x3
, x3
, x3
);
263 c255_sub(z3
, da
, cb
);
264 c255_mul(z3
, z3
, z3
);
265 c255_mul(z3
, z3
, x1
);
266 c255_mul(x2
, aa
, bb
);
267 c255_mul(z2
, C255_A24
, e
);
268 c255_add(z2
, z2
, aa
);
272 print_int_mont("x2", x2);
273 print_int_mont("z2", z2);
274 print_int_mont("x3", x3);
275 print_int_mont("z3", z3);
282 * Inverse z2 with a modular exponentiation. This is a simple
283 * square-and-multiply algorithm; we mutualise most non-squarings
284 * since the exponent contains almost only ones.
286 memcpy(a
, z2
, sizeof z2
);
287 for (i
= 0; i
< 15; i
++) {
291 memcpy(b
, a
, sizeof a
);
292 for (i
= 0; i
< 14; i
++) {
295 for (j
= 0; j
< 16; j
++) {
300 for (i
= 14; i
>= 0; i
--) {
302 if ((0xFFEB >> i
) & 1) {
307 br_i15_from_monty(x2
, C255_P
, P0I
);
308 br_i15_encode(G
, 32, x2
);
314 api_mulgen(unsigned char *R
,
315 const unsigned char *x
, size_t xlen
, int curve
)
317 const unsigned char *G
;
320 G
= api_generator(curve
, &Glen
);
322 api_mul(R
, Glen
, x
, xlen
, curve
);
327 api_muladd(unsigned char *A
, const unsigned char *B
, size_t len
,
328 const unsigned char *x
, size_t xlen
,
329 const unsigned char *y
, size_t ylen
, int curve
)
332 * We don't implement this method, since it is used for ECDSA
333 * only, and there is no ECDSA over Curve25519 (which instead
347 /* see bearssl_ec.h */
348 const br_ec_impl br_ec_c25519_i15
= {
349 (uint32_t)0x20000000,