Activated Curve25519 support for ECDHE cipher suites.
[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 /* obsolete
50 #include <stdio.h>
51 #include <stdlib.h>
52 static void
53 print_int_mont(const char *name, const uint16_t *x)
54 {
55 uint16_t y[18];
56 unsigned char tmp[32];
57 size_t u;
58
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]);
65 }
66 printf("\n");
67 }
68 */
69
70 static const uint16_t C255_A24[] = {
71 0x0110,
72 0x45D3, 0x0046, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
73 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
74 0x0000
75 };
76
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
82 };
83
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
89 };
90
91 static const unsigned char *
92 api_generator(int curve, size_t *len)
93 {
94 (void)curve;
95 *len = 32;
96 return GEN;
97 }
98
99 static const unsigned char *
100 api_order(int curve, size_t *len)
101 {
102 (void)curve;
103 *len = 32;
104 return ORDER;
105 }
106
107 static size_t
108 api_xoff(int curve, size_t *len)
109 {
110 (void)curve;
111 *len = 32;
112 return 0;
113 }
114
115 static void
116 cswap(uint16_t *a, uint16_t *b, uint32_t ctl)
117 {
118 int i;
119
120 ctl = -ctl;
121 for (i = 0; i < 18; i ++) {
122 uint32_t aw, bw, tw;
123
124 aw = a[i];
125 bw = b[i];
126 tw = ctl & (aw ^ bw);
127 a[i] = aw ^ tw;
128 b[i] = bw ^ tw;
129 }
130 }
131
132 static void
133 c255_add(uint16_t *d, const uint16_t *a, const uint16_t *b)
134 {
135 uint32_t ctl;
136 uint16_t t[18];
137
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);
143 }
144
145 static void
146 c255_sub(uint16_t *d, const uint16_t *a, const uint16_t *b)
147 {
148 uint16_t t[18];
149
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);
153 }
154
155 static void
156 c255_mul(uint16_t *d, const uint16_t *a, const uint16_t *b)
157 {
158 uint16_t t[18];
159
160 br_i15_montymul(t, a, b, C255_P, P0I);
161 memcpy(d, t, sizeof t);
162 }
163
164 static void
165 byteswap(unsigned char *G)
166 {
167 int i;
168
169 for (i = 0; i < 16; i ++) {
170 unsigned char t;
171
172 t = G[i];
173 G[i] = G[31 - i];
174 G[31 - i] = t;
175 }
176 }
177
178 static uint32_t
179 api_mul(unsigned char *G, size_t Glen,
180 const unsigned char *kb, size_t kblen, int curve)
181 {
182 uint16_t x1[18], x2[18], x3[18], z2[18], z3[18];
183 uint16_t a[18], aa[18], b[18], bb[18];
184 uint16_t c[18], d[18], e[18], da[18], cb[18];
185 unsigned char k[32];
186 uint32_t swap;
187 int i;
188
189 (void)curve;
190
191 /*
192 * Points are encoded over exactly 32 bytes. Multipliers must fit
193 * in 32 bytes as well.
194 * RFC 7748 mandates that the high bit of the last point byte must
195 * be ignored/cleared.
196 */
197 if (Glen != 32 || kblen > 32) {
198 return 0;
199 }
200 G[31] &= 0x7F;
201
202 /*
203 * Byteswap the point encoding, because it uses little-endian, and
204 * the generic decoding routine uses big-endian.
205 */
206 byteswap(G);
207
208 /*
209 * Initialise variables x1, x2, z2, x3 and z3. We set all of them
210 * into Montgomery representation.
211 */
212 br_i15_decode_reduce(a, G, 32, C255_P);
213 br_i15_montymul(x1, a, C255_R2, C255_P, P0I);
214 memcpy(x3, x1, sizeof x1);
215 br_i15_zero(z2, C255_P[0]);
216 memcpy(x2, z2, sizeof z2);
217 x2[1] = 19;
218 memcpy(z3, x2, sizeof x2);
219
220 memcpy(k, kb, kblen);
221 memset(k + kblen, 0, (sizeof k) - kblen);
222 k[0] &= 0xF8;
223 k[31] &= 0x7F;
224 k[31] |= 0x40;
225
226 /* obsolete
227 print_int_mont("x1", x1);
228 */
229
230 swap = 0;
231 for (i = 254; i >= 0; i --) {
232 uint32_t kt;
233
234 kt = (k[i >> 3] >> (i & 7)) & 1;
235 swap ^= kt;
236 cswap(x2, x3, swap);
237 cswap(z2, z3, swap);
238 swap = kt;
239
240 /* obsolete
241 print_int_mont("x2", x2);
242 print_int_mont("z2", z2);
243 print_int_mont("x3", x3);
244 print_int_mont("z3", z3);
245 */
246
247 c255_add(a, x2, z2);
248 c255_mul(aa, a, a);
249 c255_sub(b, x2, z2);
250 c255_mul(bb, b, b);
251 c255_sub(e, aa, bb);
252 c255_add(c, x3, z3);
253 c255_sub(d, x3, z3);
254 c255_mul(da, d, a);
255 c255_mul(cb, c, b);
256
257 /* obsolete
258 print_int_mont("a ", a);
259 print_int_mont("aa", aa);
260 print_int_mont("b ", b);
261 print_int_mont("bb", bb);
262 print_int_mont("e ", e);
263 print_int_mont("c ", c);
264 print_int_mont("d ", d);
265 print_int_mont("da", da);
266 print_int_mont("cb", cb);
267 */
268
269 c255_add(x3, da, cb);
270 c255_mul(x3, x3, x3);
271 c255_sub(z3, da, cb);
272 c255_mul(z3, z3, z3);
273 c255_mul(z3, z3, x1);
274 c255_mul(x2, aa, bb);
275 c255_mul(z2, C255_A24, e);
276 c255_add(z2, z2, aa);
277 c255_mul(z2, e, z2);
278
279 /* obsolete
280 print_int_mont("x2", x2);
281 print_int_mont("z2", z2);
282 print_int_mont("x3", x3);
283 print_int_mont("z3", z3);
284 */
285 }
286 cswap(x2, x3, swap);
287 cswap(z2, z3, swap);
288
289 /*
290 * Inverse z2 with a modular exponentiation. This is a simple
291 * square-and-multiply algorithm; we mutualise most non-squarings
292 * since the exponent contains almost only ones.
293 */
294 memcpy(a, z2, sizeof z2);
295 for (i = 0; i < 15; i ++) {
296 c255_mul(a, a, a);
297 c255_mul(a, a, z2);
298 }
299 memcpy(b, a, sizeof a);
300 for (i = 0; i < 14; i ++) {
301 int j;
302
303 for (j = 0; j < 16; j ++) {
304 c255_mul(b, b, b);
305 }
306 c255_mul(b, b, a);
307 }
308 for (i = 14; i >= 0; i --) {
309 c255_mul(b, b, b);
310 if ((0xFFEB >> i) & 1) {
311 c255_mul(b, z2, b);
312 }
313 }
314 c255_mul(x2, x2, b);
315 br_i15_from_monty(x2, C255_P, P0I);
316 br_i15_encode(G, 32, x2);
317 byteswap(G);
318 return 1;
319 }
320
321 static size_t
322 api_mulgen(unsigned char *R,
323 const unsigned char *x, size_t xlen, int curve)
324 {
325 const unsigned char *G;
326 size_t Glen;
327
328 G = api_generator(curve, &Glen);
329 memcpy(R, G, Glen);
330 api_mul(R, Glen, x, xlen, curve);
331 return Glen;
332 }
333
334 static uint32_t
335 api_muladd(unsigned char *A, const unsigned char *B, size_t len,
336 const unsigned char *x, size_t xlen,
337 const unsigned char *y, size_t ylen, int curve)
338 {
339 /*
340 * We don't implement this method, since it is used for ECDSA
341 * only, and there is no ECDSA over Curve25519 (which instead
342 * uses EdDSA).
343 */
344 (void)A;
345 (void)B;
346 (void)len;
347 (void)x;
348 (void)xlen;
349 (void)y;
350 (void)ylen;
351 (void)curve;
352 return 0;
353 }
354
355 /* see bearssl_ec.h */
356 const br_ec_impl br_ec_c25519_i15 = {
357 (uint32_t)0x20000000,
358 &api_generator,
359 &api_order,
360 &api_xoff,
361 &api_mul,
362 &api_mulgen,
363 &api_muladd
364 };