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,
49 static const uint16_t C255_A24
[] = {
51 0x45D3, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
52 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
56 static const unsigned char GEN
[] = {
57 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
63 static const unsigned char ORDER
[] = {
64 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x14, 0xDE, 0xF9, 0xDE, 0xA2, 0xF7, 0x9C, 0xD6,
67 0x58, 0x12, 0x63, 0x1A, 0x5C, 0xF5, 0xD3, 0xED
70 static const unsigned char *
71 api_generator(int curve
, size_t *len
)
78 static const unsigned char *
79 api_order(int curve
, size_t *len
)
87 cswap(uint16_t *a
, uint16_t *b
, uint32_t ctl
)
92 for (i
= 0; i
< 18; i
++) {
104 c255_add(uint16_t *d
, const uint16_t *a
, const uint16_t *b
)
109 memcpy(t
, a
, sizeof t
);
110 ctl
= br_i15_add(t
, b
, 1);
111 ctl
|= NOT(br_i15_sub(t
, C255_P
, 0));
112 br_i15_sub(t
, C255_P
, ctl
);
113 memcpy(d
, t
, sizeof t
);
117 c255_sub(uint16_t *d
, const uint16_t *a
, const uint16_t *b
)
121 memcpy(t
, a
, sizeof t
);
122 br_i15_add(t
, C255_P
, br_i15_sub(t
, b
, 1));
123 memcpy(d
, t
, sizeof t
);
127 c255_mul(uint16_t *d
, const uint16_t *a
, const uint16_t *b
)
131 br_i15_montymul(t
, a
, b
, C255_P
, P0I
);
132 memcpy(d
, t
, sizeof t
);
136 byteswap(unsigned char *G
)
140 for (i
= 0; i
< 16; i
++) {
150 api_mul(unsigned char *G
, size_t Glen
,
151 const unsigned char *kb
, size_t kblen
, int curve
)
153 uint16_t x1
[18], x2
[18], x3
[18], z2
[18], z3
[18];
154 uint16_t a
[18], aa
[18], b
[18], bb
[18];
155 uint16_t c
[18], d
[18], e
[18], da
[18], cb
[18];
163 * Points are encoded over exactly 32 bytes. Multipliers must fit
164 * in 32 bytes as well.
165 * RFC 7748 mandates that the high bit of the last point byte must
166 * be ignored/cleared.
168 if (Glen
!= 32 || kblen
> 32) {
174 * Byteswap the point encoding, because it uses little-endian, and
175 * the generic decoding routine uses big-endian.
180 * Initialise variables x1, x2, z2, x3 and z3. We set all of them
181 * into Montgomery representation.
183 br_i15_decode_reduce(a
, G
, 32, C255_P
);
184 br_i15_montymul(x1
, a
, C255_R2
, C255_P
, P0I
);
185 memcpy(x3
, x1
, sizeof x1
);
186 br_i15_zero(z2
, C255_P
[0]);
187 memcpy(x2
, z2
, sizeof z2
);
189 memcpy(z3
, x2
, sizeof x2
);
191 memcpy(k
, kb
, kblen
);
192 memset(k
+ kblen
, 0, (sizeof k
) - kblen
);
198 for (i
= 254; i
>= 0; i
--) {
201 kt
= (k
[i
>> 3] >> (i
& 7)) & 1;
216 c255_add(x3
, da
, cb
);
217 c255_mul(x3
, x3
, x3
);
218 c255_sub(z3
, da
, cb
);
219 c255_mul(z3
, z3
, z3
);
220 c255_mul(z3
, z3
, x1
);
221 c255_mul(x2
, aa
, bb
);
222 c255_mul(z2
, C255_A24
, e
);
223 c255_add(z2
, z2
, aa
);
230 * Inverse z2 with a modular exponentiation. This is a simple
231 * square-and-multiply algorithm; we mutualise most non-squarings
232 * since the exponent contains almost only ones.
234 memcpy(a
, z2
, sizeof z2
);
235 for (i
= 0; i
< 15; i
++) {
239 memcpy(b
, a
, sizeof a
);
240 for (i
= 0; i
< 14; i
++) {
243 for (j
= 0; j
< 16; j
++) {
248 for (i
= 14; i
>= 0; i
--) {
250 if ((0xFFEB >> i
) & 1) {
255 br_i15_from_monty(x2
, C255_P
, P0I
);
256 br_i15_encode(G
, 32, x2
);
262 api_mulgen(unsigned char *R
,
263 const unsigned char *x
, size_t xlen
, int curve
)
265 const unsigned char *G
;
268 G
= api_generator(curve
, &Glen
);
270 api_mul(R
, Glen
, x
, xlen
, curve
);
275 api_muladd(unsigned char *A
, const unsigned char *B
, size_t len
,
276 const unsigned char *x
, size_t xlen
,
277 const unsigned char *y
, size_t ylen
, int curve
)
280 * We don't implement this method, since it is used for ECDSA
281 * only, and there is no ECDSA over Curve25519 (which instead
295 /* see bearssl_ec.h */
296 const br_ec_impl br_ec_c25519_i15
= {
297 (uint32_t)0x20000000,