79560ae9f6197be721501c22670e197a86f887d6
[BearSSL] / src / ec / ec_c25519_i15.c
1 /*
2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 /*
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)
31 */
32
33 static const uint16_t C255_P[] = {
34 0x0110,
35 0x7FED, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
36 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF,
37 0x7FFF
38 };
39
40 #define P0I 0x4A1B
41
42 static const uint16_t C255_R2[] = {
43 0x0110,
44 0x0169, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
45 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
46 0x0000
47 };
48
49 static const uint16_t C255_A24[] = {
50 0x0110,
51 0x45D3, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
52 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
53 0x0000
54 };
55
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
61 };
62
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
68 };
69
70 static const unsigned char *
71 api_generator(int curve, size_t *len)
72 {
73 (void)curve;
74 *len = 32;
75 return GEN;
76 }
77
78 static const unsigned char *
79 api_order(int curve, size_t *len)
80 {
81 (void)curve;
82 *len = 32;
83 return ORDER;
84 }
85
86 static void
87 cswap(uint16_t *a, uint16_t *b, uint32_t ctl)
88 {
89 int i;
90
91 ctl = -ctl;
92 for (i = 0; i < 18; i ++) {
93 uint32_t aw, bw, tw;
94
95 aw = a[i];
96 bw = b[i];
97 tw = ctl & (aw ^ bw);
98 a[i] = aw ^ tw;
99 b[i] = bw ^ tw;
100 }
101 }
102
103 static void
104 c255_add(uint16_t *d, const uint16_t *a, const uint16_t *b)
105 {
106 uint32_t ctl;
107 uint16_t t[18];
108
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);
114 }
115
116 static void
117 c255_sub(uint16_t *d, const uint16_t *a, const uint16_t *b)
118 {
119 uint16_t t[18];
120
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);
124 }
125
126 static void
127 c255_mul(uint16_t *d, const uint16_t *a, const uint16_t *b)
128 {
129 uint16_t t[18];
130
131 br_i15_montymul(t, a, b, C255_P, P0I);
132 memcpy(d, t, sizeof t);
133 }
134
135 static void
136 byteswap(unsigned char *G)
137 {
138 int i;
139
140 for (i = 0; i < 16; i ++) {
141 unsigned char t;
142
143 t = G[i];
144 G[i] = G[31 - i];
145 G[31 - i] = t;
146 }
147 }
148
149 static uint32_t
150 api_mul(unsigned char *G, size_t Glen,
151 const unsigned char *kb, size_t kblen, int curve)
152 {
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];
156 unsigned char k[32];
157 uint32_t swap;
158 int i;
159
160 (void)curve;
161
162 /*
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.
167 */
168 if (Glen != 32 || kblen > 32) {
169 return 0;
170 }
171 G[31] &= 0x7F;
172
173 /*
174 * Byteswap the point encoding, because it uses little-endian, and
175 * the generic decoding routine uses big-endian.
176 */
177 byteswap(G);
178
179 /*
180 * Initialise variables x1, x2, z2, x3 and z3. We set all of them
181 * into Montgomery representation.
182 */
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);
188 x2[1] = 19;
189 memcpy(z3, x2, sizeof x2);
190
191 memcpy(k, kb, kblen);
192 memset(k + kblen, 0, (sizeof k) - kblen);
193 k[0] &= 0xF8;
194 k[31] &= 0x7F;
195 k[31] |= 0x40;
196
197 swap = 0;
198 for (i = 254; i >= 0; i --) {
199 uint32_t kt;
200
201 kt = (k[i >> 3] >> (i & 7)) & 1;
202 swap ^= kt;
203 cswap(x2, x3, swap);
204 cswap(z2, z3, swap);
205 swap = kt;
206
207 c255_add(a, x2, z2);
208 c255_mul(aa, a, a);
209 c255_sub(b, x2, z2);
210 c255_mul(bb, b, b);
211 c255_sub(e, aa, bb);
212 c255_add(c, x3, z3);
213 c255_sub(d, x3, z3);
214 c255_mul(da, d, a);
215 c255_mul(cb, c, b);
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);
224 c255_mul(z2, e, z2);
225 }
226 cswap(x2, x3, swap);
227 cswap(z2, z3, swap);
228
229 /*
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.
233 */
234 memcpy(a, z2, sizeof z2);
235 for (i = 0; i < 15; i ++) {
236 c255_mul(a, a, a);
237 c255_mul(a, a, z2);
238 }
239 memcpy(b, a, sizeof a);
240 for (i = 0; i < 14; i ++) {
241 int j;
242
243 for (j = 0; j < 16; j ++) {
244 c255_mul(b, b, b);
245 }
246 c255_mul(b, b, a);
247 }
248 for (i = 14; i >= 0; i --) {
249 c255_mul(b, b, b);
250 if ((0xFFEB >> i) & 1) {
251 c255_mul(b, z2, b);
252 }
253 }
254 c255_mul(x2, x2, b);
255 br_i15_from_monty(x2, C255_P, P0I);
256 br_i15_encode(G, 32, x2);
257 byteswap(G);
258 return 1;
259 }
260
261 static size_t
262 api_mulgen(unsigned char *R,
263 const unsigned char *x, size_t xlen, int curve)
264 {
265 const unsigned char *G;
266 size_t Glen;
267
268 G = api_generator(curve, &Glen);
269 memcpy(R, G, Glen);
270 api_mul(R, Glen, x, xlen, curve);
271 return Glen;
272 }
273
274 static uint32_t
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)
278 {
279 /*
280 * We don't implement this method, since it is used for ECDSA
281 * only, and there is no ECDSA over Curve25519 (which instead
282 * uses EdDSA).
283 */
284 (void)A;
285 (void)B;
286 (void)len;
287 (void)x;
288 (void)xlen;
289 (void)y;
290 (void)ylen;
291 (void)curve;
292 return 0;
293 }
294
295 /* see bearssl_ec.h */
296 const br_ec_impl br_ec_c25519_i15 = {
297 (uint32_t)0x20000000,
298 &api_generator,
299 &api_order,
300 &api_mul,
301 &api_mulgen,
302 &api_muladd
303 };