blob: 1608d37c42f41057b9d84b62c117a31d6235c1fd [file] [log] [blame]
Kenny Rootdb0850c2013-10-08 12:52:07 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Google Inc. nor the names of its contributors may
12 * be used to endorse or promote products derived from this software
13 * without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 * EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27// This is an implementation of the P256 elliptic curve group. It's written to
28// be portable 32-bit, although it's still constant-time.
29//
30// WARNING: Implementing these functions in a constant-time manner is far from
31// obvious. Be careful when touching this code.
32//
33// See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background.
34
35#include <assert.h>
36#include <stdint.h>
37#include <string.h>
38#include <stdio.h>
39
40#include "mincrypt/p256.h"
41
42const p256_int SECP256r1_n = // curve order
43 {{0xfc632551, 0xf3b9cac2, 0xa7179e84, 0xbce6faad, -1, -1, 0, -1}};
44
45const p256_int SECP256r1_p = // curve field size
46 {{-1, -1, -1, 0, 0, 0, 1, -1 }};
47
48const p256_int SECP256r1_b = // curve b
49 {{0x27d2604b, 0x3bce3c3e, 0xcc53b0f6, 0x651d06b0,
50 0x769886bc, 0xb3ebbd55, 0xaa3a93e7, 0x5ac635d8}};
51
52static const p256_int p256_one = P256_ONE;
53
54void p256_init(p256_int* a) {
55 memset(a, 0, sizeof(*a));
56}
57
58void p256_clear(p256_int* a) { p256_init(a); }
59
60int p256_get_bit(const p256_int* scalar, int bit) {
61 return (P256_DIGIT(scalar, bit / P256_BITSPERDIGIT)
62 >> (bit & (P256_BITSPERDIGIT - 1))) & 1;
63}
64
65int p256_is_zero(const p256_int* a) {
66 int i, result = 0;
67 for (i = 0; i < P256_NDIGITS; ++i) result |= P256_DIGIT(a, i);
68 return !result;
69}
70
71// top, c[] += a[] * b
72// Returns new top
73static p256_digit mulAdd(const p256_int* a,
74 p256_digit b,
75 p256_digit top,
76 p256_digit* c) {
77 int i;
78 p256_ddigit carry = 0;
79
80 for (i = 0; i < P256_NDIGITS; ++i) {
81 carry += *c;
82 carry += (p256_ddigit)P256_DIGIT(a, i) * b;
83 *c++ = (p256_digit)carry;
84 carry >>= P256_BITSPERDIGIT;
85 }
86 return top + (p256_digit)carry;
87}
88
89// top, c[] -= top_a, a[]
90static p256_digit subTop(p256_digit top_a,
91 const p256_digit* a,
92 p256_digit top_c,
93 p256_digit* c) {
94 int i;
95 p256_sddigit borrow = 0;
96
97 for (i = 0; i < P256_NDIGITS; ++i) {
98 borrow += *c;
99 borrow -= *a++;
100 *c++ = (p256_digit)borrow;
101 borrow >>= P256_BITSPERDIGIT;
102 }
103 borrow += top_c;
104 borrow -= top_a;
105 top_c = (p256_digit)borrow;
106 assert((borrow >> P256_BITSPERDIGIT) == 0);
107 return top_c;
108}
109
110// top, c[] -= MOD[] & mask (0 or -1)
111// returns new top.
112static p256_digit subM(const p256_int* MOD,
113 p256_digit top,
114 p256_digit* c,
115 p256_digit mask) {
116 int i;
117 p256_sddigit borrow = 0;
118 for (i = 0; i < P256_NDIGITS; ++i) {
119 borrow += *c;
120 borrow -= P256_DIGIT(MOD, i) & mask;
121 *c++ = (p256_digit)borrow;
122 borrow >>= P256_BITSPERDIGIT;
123 }
124 return top + (p256_digit)borrow;
125}
126
127// top, c[] += MOD[] & mask (0 or -1)
128// returns new top.
129static p256_digit addM(const p256_int* MOD,
130 p256_digit top,
131 p256_digit* c,
132 p256_digit mask) {
133 int i;
134 p256_ddigit carry = 0;
135 for (i = 0; i < P256_NDIGITS; ++i) {
136 carry += *c;
137 carry += P256_DIGIT(MOD, i) & mask;
138 *c++ = (p256_digit)carry;
139 carry >>= P256_BITSPERDIGIT;
140 }
141 return top + (p256_digit)carry;
142}
143
144// c = a * b mod MOD. c can be a and/or b.
145void p256_modmul(const p256_int* MOD,
146 const p256_int* a,
147 const p256_digit top_b,
148 const p256_int* b,
149 p256_int* c) {
150 p256_digit tmp[P256_NDIGITS * 2 + 1] = { 0 };
151 p256_digit top = 0;
152 int i;
153
154 // Multiply/add into tmp.
155 for (i = 0; i < P256_NDIGITS; ++i) {
156 if (i) tmp[i + P256_NDIGITS - 1] = top;
157 top = mulAdd(a, P256_DIGIT(b, i), 0, tmp + i);
158 }
159
160 // Multiply/add top digit
161 tmp[i + P256_NDIGITS - 1] = top;
162 top = mulAdd(a, top_b, 0, tmp + i);
163
164 // Reduce tmp, digit by digit.
165 for (; i >= 0; --i) {
166 p256_digit reducer[P256_NDIGITS] = { 0 };
167 p256_digit top_reducer;
168
169 // top can be any value at this point.
170 // Guestimate reducer as top * MOD, since msw of MOD is -1.
171 top_reducer = mulAdd(MOD, top, 0, reducer);
172
173 // Subtract reducer from top | tmp.
174 top = subTop(top_reducer, reducer, top, tmp + i);
175
176 // top is now either 0 or 1. Make it 0, fixed-timing.
177 assert(top <= 1);
178
179 top = subM(MOD, top, tmp + i, ~(top - 1));
180
181 assert(top == 0);
182
183 // We have now reduced the top digit off tmp. Fetch new top digit.
184 top = tmp[i + P256_NDIGITS - 1];
185 }
186
187 // tmp might still be larger than MOD, yet same bit length.
188 // Make sure it is less, fixed-timing.
189 addM(MOD, 0, tmp, subM(MOD, 0, tmp, -1));
190
191 memcpy(c, tmp, P256_NBYTES);
192}
193int p256_is_odd(const p256_int* a) { return P256_DIGIT(a, 0) & 1; }
194int p256_is_even(const p256_int* a) { return !(P256_DIGIT(a, 0) & 1); }
195
196p256_digit p256_shl(const p256_int* a, int n, p256_int* b) {
197 int i;
198 p256_digit top = P256_DIGIT(a, P256_NDIGITS - 1);
199
200 n %= P256_BITSPERDIGIT;
201 for (i = P256_NDIGITS - 1; i > 0; --i) {
202 p256_digit accu = (P256_DIGIT(a, i) << n);
203 accu |= (P256_DIGIT(a, i - 1) >> (P256_BITSPERDIGIT - n));
204 P256_DIGIT(b, i) = accu;
205 }
206 P256_DIGIT(b, i) = (P256_DIGIT(a, i) << n);
207
208 top = (p256_digit)((((p256_ddigit)top) << n) >> P256_BITSPERDIGIT);
209
210 return top;
211}
212
213void p256_shr(const p256_int* a, int n, p256_int* b) {
214 int i;
215
216 n %= P256_BITSPERDIGIT;
217 for (i = 0; i < P256_NDIGITS - 1; ++i) {
218 p256_digit accu = (P256_DIGIT(a, i) >> n);
219 accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - n));
220 P256_DIGIT(b, i) = accu;
221 }
222 P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> n);
223}
224
225static void p256_shr1(const p256_int* a, int highbit, p256_int* b) {
226 int i;
227
228 for (i = 0; i < P256_NDIGITS - 1; ++i) {
229 p256_digit accu = (P256_DIGIT(a, i) >> 1);
230 accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - 1));
231 P256_DIGIT(b, i) = accu;
232 }
233 P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> 1) |
234 (highbit << (P256_BITSPERDIGIT - 1));
235}
236
237// Return -1, 0, 1 for a < b, a == b or a > b respectively.
238int p256_cmp(const p256_int* a, const p256_int* b) {
239 int i;
240 p256_sddigit borrow = 0;
241 p256_digit notzero = 0;
242
243 for (i = 0; i < P256_NDIGITS; ++i) {
244 borrow += (p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
245 // Track whether any result digit is ever not zero.
246 // Relies on !!(non-zero) evaluating to 1, e.g., !!(-1) evaluating to 1.
247 notzero |= !!((p256_digit)borrow);
248 borrow >>= P256_BITSPERDIGIT;
249 }
250 return (int)borrow | notzero;
251}
252
253// c = a - b. Returns borrow: 0 or -1.
254int p256_sub(const p256_int* a, const p256_int* b, p256_int* c) {
255 int i;
256 p256_sddigit borrow = 0;
257
258 for (i = 0; i < P256_NDIGITS; ++i) {
259 borrow += (p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
260 if (c) P256_DIGIT(c, i) = (p256_digit)borrow;
261 borrow >>= P256_BITSPERDIGIT;
262 }
263 return (int)borrow;
264}
265
266// c = a + b. Returns carry: 0 or 1.
267int p256_add(const p256_int* a, const p256_int* b, p256_int* c) {
268 int i;
269 p256_ddigit carry = 0;
270
271 for (i = 0; i < P256_NDIGITS; ++i) {
272 carry += (p256_ddigit)P256_DIGIT(a, i) + P256_DIGIT(b, i);
273 if (c) P256_DIGIT(c, i) = (p256_digit)carry;
274 carry >>= P256_BITSPERDIGIT;
275 }
276 return (int)carry;
277}
278
279// b = a + d. Returns carry, 0 or 1.
280int p256_add_d(const p256_int* a, p256_digit d, p256_int* b) {
281 int i;
282 p256_ddigit carry = d;
283
284 for (i = 0; i < P256_NDIGITS; ++i) {
285 carry += (p256_ddigit)P256_DIGIT(a, i);
286 if (b) P256_DIGIT(b, i) = (p256_digit)carry;
287 carry >>= P256_BITSPERDIGIT;
288 }
289 return (int)carry;
290}
291
292// b = 1/a mod MOD, binary euclid.
293void p256_modinv_vartime(const p256_int* MOD,
294 const p256_int* a,
295 p256_int* b) {
296 p256_int R = P256_ZERO;
297 p256_int S = P256_ONE;
298 p256_int U = *MOD;
299 p256_int V = *a;
300
301 for (;;) {
302 if (p256_is_even(&U)) {
303 p256_shr1(&U, 0, &U);
304 if (p256_is_even(&R)) {
305 p256_shr1(&R, 0, &R);
306 } else {
307 // R = (R+MOD)/2
308 p256_shr1(&R, p256_add(&R, MOD, &R), &R);
309 }
310 } else if (p256_is_even(&V)) {
311 p256_shr1(&V, 0, &V);
312 if (p256_is_even(&S)) {
313 p256_shr1(&S, 0, &S);
314 } else {
315 // S = (S+MOD)/2
316 p256_shr1(&S, p256_add(&S, MOD, &S) , &S);
317 }
318 } else { // U,V both odd.
319 if (!p256_sub(&V, &U, NULL)) {
320 p256_sub(&V, &U, &V);
321 if (p256_sub(&S, &R, &S)) p256_add(&S, MOD, &S);
322 if (p256_is_zero(&V)) break; // done.
323 } else {
324 p256_sub(&U, &V, &U);
325 if (p256_sub(&R, &S, &R)) p256_add(&R, MOD, &R);
326 }
327 }
328 }
329
330 p256_mod(MOD, &R, b);
331}
332
333void p256_mod(const p256_int* MOD,
334 const p256_int* in,
335 p256_int* out) {
336 if (out != in) *out = *in;
337 addM(MOD, 0, P256_DIGITS(out), subM(MOD, 0, P256_DIGITS(out), -1));
338}
339
340// Verify y^2 == x^3 - 3x + b mod p
341// and 0 < x < p and 0 < y < p
342int p256_is_valid_point(const p256_int* x, const p256_int* y) {
343 p256_int y2, x3;
344
345 if (p256_cmp(&SECP256r1_p, x) <= 0 ||
346 p256_cmp(&SECP256r1_p, y) <= 0 ||
347 p256_is_zero(x) ||
348 p256_is_zero(y)) return 0;
349
350 p256_modmul(&SECP256r1_p, y, 0, y, &y2); // y^2
351
352 p256_modmul(&SECP256r1_p, x, 0, x, &x3); // x^2
353 p256_modmul(&SECP256r1_p, x, 0, &x3, &x3); // x^3
354 if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - x
355 if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - 2x
356 if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - 3x
357 if (p256_add(&x3, &SECP256r1_b, &x3)) // x^3 - 3x + b
358 p256_sub(&x3, &SECP256r1_p, &x3);
359
360 return p256_cmp(&y2, &x3) == 0;
361}
362
363void p256_from_bin(const uint8_t src[P256_NBYTES], p256_int* dst) {
364 int i;
365 const uint8_t* p = &src[0];
366
367 for (i = P256_NDIGITS - 1; i >= 0; --i) {
368 P256_DIGIT(dst, i) =
369 (p[0] << 24) |
370 (p[1] << 16) |
371 (p[2] << 8) |
372 p[3];
373 p += 4;
374 }
375}