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^(31k) for the smallest k such that R >= p)
33 static const uint32_t C255_P
[] = {
35 0x7FFFFFED, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF,
36 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x0000007F
39 #define P0I 0x286BCA1B
41 static const uint32_t C255_R2
[] = {
43 0x00000000, 0x02D20000, 0x00000000, 0x00000000, 0x00000000,
44 0x00000000, 0x00000000, 0x00000000, 0x00000000
47 static const uint32_t C255_A24
[] = {
49 0x53000000, 0x0000468B, 0x00000000, 0x00000000, 0x00000000,
50 0x00000000, 0x00000000, 0x00000000, 0x00000000
57 print_int_mont(const char *name, const uint32_t *x)
60 unsigned char tmp[32];
63 printf("%s = ", name);
64 memcpy(y, x, sizeof y);
65 br_i31_from_monty(y, C255_P, P0I);
66 br_i31_encode(tmp, sizeof tmp, y);
67 for (u = 0; u < sizeof tmp; u ++) {
68 printf("%02X", tmp[u]);
74 static const unsigned char GEN
[] = {
75 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
81 static const unsigned char ORDER
[] = {
82 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
83 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
84 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
85 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
88 static const unsigned char *
89 api_generator(int curve
, size_t *len
)
96 static const unsigned char *
97 api_order(int curve
, size_t *len
)
105 api_xoff(int curve
, size_t *len
)
113 cswap(uint32_t *a
, uint32_t *b
, uint32_t ctl
)
118 for (i
= 0; i
< 10; i
++) {
123 tw
= ctl
& (aw
^ bw
);
130 c255_add(uint32_t *d
, const uint32_t *a
, const uint32_t *b
)
135 memcpy(t
, a
, sizeof t
);
136 ctl
= br_i31_add(t
, b
, 1);
137 ctl
|= NOT(br_i31_sub(t
, C255_P
, 0));
138 br_i31_sub(t
, C255_P
, ctl
);
139 memcpy(d
, t
, sizeof t
);
143 c255_sub(uint32_t *d
, const uint32_t *a
, const uint32_t *b
)
147 memcpy(t
, a
, sizeof t
);
148 br_i31_add(t
, C255_P
, br_i31_sub(t
, b
, 1));
149 memcpy(d
, t
, sizeof t
);
153 c255_mul(uint32_t *d
, const uint32_t *a
, const uint32_t *b
)
157 br_i31_montymul(t
, a
, b
, C255_P
, P0I
);
158 memcpy(d
, t
, sizeof t
);
162 byteswap(unsigned char *G
)
166 for (i
= 0; i
< 16; i
++) {
176 api_mul(unsigned char *G
, size_t Glen
,
177 const unsigned char *kb
, size_t kblen
, int curve
)
179 uint32_t x1
[10], x2
[10], x3
[10], z2
[10], z3
[10];
180 uint32_t a
[10], aa
[10], b
[10], bb
[10];
181 uint32_t c
[10], d
[10], e
[10], da
[10], cb
[10];
189 * Points are encoded over exactly 32 bytes. Multipliers must fit
190 * in 32 bytes as well.
191 * RFC 7748 mandates that the high bit of the last point byte must
192 * be ignored/cleared.
194 if (Glen
!= 32 || kblen
> 32) {
200 * Byteswap the point encoding, because it uses little-endian, and
201 * the generic decoding routine uses big-endian.
206 * Decode the point ('u' coordinate). This should be reduced
207 * modulo p, but we prefer to avoid the dependency on
208 * br_i31_decode_reduce(). Instead, we use br_i31_decode_mod()
209 * with a synthetic modulus of value 2^255 (this must work
210 * since G was truncated to 255 bits), then use a conditional
211 * subtraction. We use br_i31_decode_mod() and not
212 * br_i31_decode(), because the ec_prime_i31 implementation uses
213 * the former but not the latter.
214 * br_i31_decode_reduce(a, G, 32, C255_P);
216 br_i31_zero(b
, 0x108);
218 br_i31_decode_mod(a
, G
, 32, b
);
220 br_i31_sub(a
, C255_P
, NOT(br_i31_sub(a
, C255_P
, 0)));
223 * Initialise variables x1, x2, z2, x3 and z3. We set all of them
224 * into Montgomery representation.
226 br_i31_montymul(x1
, a
, C255_R2
, C255_P
, P0I
);
227 memcpy(x3
, x1
, sizeof x1
);
228 br_i31_zero(z2
, C255_P
[0]);
229 memcpy(x2
, z2
, sizeof z2
);
231 memcpy(z3
, x2
, sizeof x2
);
233 memcpy(k
, kb
, kblen
);
234 memset(k
+ kblen
, 0, (sizeof k
) - kblen
);
240 print_int_mont("x1", x1);
244 for (i
= 254; i
>= 0; i
--) {
247 kt
= (k
[i
>> 3] >> (i
& 7)) & 1;
254 print_int_mont("x2", x2);
255 print_int_mont("z2", z2);
256 print_int_mont("x3", x3);
257 print_int_mont("z3", z3);
271 print_int_mont("a ", a);
272 print_int_mont("aa", aa);
273 print_int_mont("b ", b);
274 print_int_mont("bb", bb);
275 print_int_mont("e ", e);
276 print_int_mont("c ", c);
277 print_int_mont("d ", d);
278 print_int_mont("da", da);
279 print_int_mont("cb", cb);
282 c255_add(x3
, da
, cb
);
283 c255_mul(x3
, x3
, x3
);
284 c255_sub(z3
, da
, cb
);
285 c255_mul(z3
, z3
, z3
);
286 c255_mul(z3
, z3
, x1
);
287 c255_mul(x2
, aa
, bb
);
288 c255_mul(z2
, C255_A24
, e
);
289 c255_add(z2
, z2
, aa
);
293 print_int_mont("x2", x2);
294 print_int_mont("z2", z2);
295 print_int_mont("x3", x3);
296 print_int_mont("z3", z3);
303 * Inverse z2 with a modular exponentiation. This is a simple
304 * square-and-multiply algorithm; we mutualise most non-squarings
305 * since the exponent contains almost only ones.
307 memcpy(a
, z2
, sizeof z2
);
308 for (i
= 0; i
< 15; i
++) {
312 memcpy(b
, a
, sizeof a
);
313 for (i
= 0; i
< 14; i
++) {
316 for (j
= 0; j
< 16; j
++) {
321 for (i
= 14; i
>= 0; i
--) {
323 if ((0xFFEB >> i
) & 1) {
330 * To avoid a dependency on br_i31_from_monty(), we use
331 * a Montgomery multiplication with 1.
332 * memcpy(x2, b, sizeof b);
333 * br_i31_from_monty(x2, C255_P, P0I);
335 br_i31_zero(a
, C255_P
[0]);
337 br_i31_montymul(x2
, a
, b
, C255_P
, P0I
);
339 br_i31_encode(G
, 32, x2
);
345 api_mulgen(unsigned char *R
,
346 const unsigned char *x
, size_t xlen
, int curve
)
348 const unsigned char *G
;
351 G
= api_generator(curve
, &Glen
);
353 api_mul(R
, Glen
, x
, xlen
, curve
);
358 api_muladd(unsigned char *A
, const unsigned char *B
, size_t len
,
359 const unsigned char *x
, size_t xlen
,
360 const unsigned char *y
, size_t ylen
, int curve
)
363 * We don't implement this method, since it is used for ECDSA
364 * only, and there is no ECDSA over Curve25519 (which instead
378 /* see bearssl_ec.h */
379 const br_ec_impl br_ec_c25519_i31
= {
380 (uint32_t)0x20000000,