blob: d31c4fcef74f87e3f5d61da0abb455cbc49390af [file] [log] [blame]
nagendra modadugu4fae5422016-05-10 16:11:54 -07001// Copyright 2016 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14#include <assert.h>
15#include <stdint.h>
16#include <string.h>
17
18#include "cryptoc/p256.h"
19
20const p256_int SECP256r1_n = // curve order
21 {{0xfc632551, 0xf3b9cac2, 0xa7179e84, 0xbce6faad, -1, -1, 0, -1}};
22
23const p256_int SECP256r1_nMin2 = // curve order - 2
24 {{0xfc632551 - 2, 0xf3b9cac2, 0xa7179e84, 0xbce6faad, -1, -1, 0, -1}};
25
26const p256_int SECP256r1_p = // curve field size
27 {{-1, -1, -1, 0, 0, 0, 1, -1 }};
28
29const p256_int SECP256r1_b = // curve b
30 {{0x27d2604b, 0x3bce3c3e, 0xcc53b0f6, 0x651d06b0,
31 0x769886bc, 0xb3ebbd55, 0xaa3a93e7, 0x5ac635d8}};
32
33static const p256_int p256_one = P256_ONE;
34
35
36void p256_init(p256_int* a) {
37 memset(a, 0, sizeof(*a));
38}
39
40void p256_clear(p256_int* a) { p256_init(a); }
41
42int p256_get_bit(const p256_int* scalar, int bit) {
43 return (P256_DIGIT(scalar, bit / P256_BITSPERDIGIT)
44 >> (bit & (P256_BITSPERDIGIT - 1))) & 1;
45}
46
47int p256_is_zero(const p256_int* a) {
48 int i, result = 0;
49 for (i = 0; i < P256_NDIGITS; ++i) result |= P256_DIGIT(a, i);
50 return !result;
51}
52
53// top, c[] += a[] * b
54// Returns new top
55static p256_digit mulAdd(const p256_int* a,
56 p256_digit b,
57 p256_digit top,
58 p256_digit* c) {
59 int i;
60 p256_ddigit carry = 0;
61
62 for (i = 0; i < P256_NDIGITS; ++i) {
63 carry += *c;
64 carry += (p256_ddigit)P256_DIGIT(a, i) * b;
65 *c++ = (p256_digit)carry;
66 carry >>= P256_BITSPERDIGIT;
67 }
68 return top + (p256_digit)carry;
69}
70
71// top, c[] -= top_a, a[]
72static p256_digit subTop(p256_digit top_a,
73 const p256_digit* a,
74 p256_digit top_c,
75 p256_digit* c) {
76 int i;
77 p256_sddigit borrow = 0;
78
79 for (i = 0; i < P256_NDIGITS; ++i) {
80 borrow += *c;
81 borrow -= *a++;
82 *c++ = (p256_digit)borrow;
83 borrow >>= P256_BITSPERDIGIT;
84 }
85 borrow += top_c;
86 borrow -= top_a;
87 top_c = (p256_digit)borrow;
88 assert((borrow >> P256_BITSPERDIGIT) == 0);
89 return top_c;
90}
91
92// top, c[] -= MOD[] & mask (0 or -1)
93// returns new top.
94static p256_digit subM(const p256_int* MOD,
95 p256_digit top,
96 p256_digit* c,
97 p256_digit mask) {
98 int i;
99 p256_sddigit borrow = 0;
100 for (i = 0; i < P256_NDIGITS; ++i) {
101 borrow += *c;
102 borrow -= P256_DIGIT(MOD, i) & mask;
103 *c++ = (p256_digit)borrow;
104 borrow >>= P256_BITSPERDIGIT;
105 }
106 return top + (p256_digit)borrow;
107}
108
109// top, c[] += MOD[] & mask (0 or -1)
110// returns new top.
111static p256_digit addM(const p256_int* MOD,
112 p256_digit top,
113 p256_digit* c,
114 p256_digit mask) {
115 int i;
116 p256_ddigit carry = 0;
117 for (i = 0; i < P256_NDIGITS; ++i) {
118 carry += *c;
119 carry += P256_DIGIT(MOD, i) & mask;
120 *c++ = (p256_digit)carry;
121 carry >>= P256_BITSPERDIGIT;
122 }
123 return top + (p256_digit)carry;
124}
125
126// c = a * b mod MOD. c can be a and/or b.
127void p256_modmul(const p256_int* MOD,
128 const p256_int* a,
129 const p256_digit top_b,
130 const p256_int* b,
131 p256_int* c) {
132 p256_digit tmp[P256_NDIGITS * 2 + 1] = { 0 };
133 p256_digit top = 0;
134 int i;
135
136 // Multiply/add into tmp.
137 for (i = 0; i < P256_NDIGITS; ++i) {
138 if (i) tmp[i + P256_NDIGITS - 1] = top;
139 top = mulAdd(a, P256_DIGIT(b, i), 0, tmp + i);
140 }
141
142 // Multiply/add top digit
143 tmp[i + P256_NDIGITS - 1] = top;
144 top = mulAdd(a, top_b, 0, tmp + i);
145
146 // Reduce tmp, digit by digit.
147 for (; i >= 0; --i) {
148 p256_digit reducer[P256_NDIGITS] = { 0 };
149 p256_digit top_reducer;
150
151 // top can be any value at this point.
152 // Guestimate reducer as top * MOD, since msw of MOD is -1.
153 top_reducer = mulAdd(MOD, top, 0, reducer);
154
155 // Subtract reducer from top | tmp.
156 top = subTop(top_reducer, reducer, top, tmp + i);
157
158 // top is now either 0 or 1. Make it 0, fixed-timing.
159 assert(top <= 1);
160
161 top = subM(MOD, top, tmp + i, ~(top - 1));
162
163 assert(top == 0);
164
165 // We have now reduced the top digit off tmp. Fetch new top digit.
166 top = tmp[i + P256_NDIGITS - 1];
167 }
168
169 // tmp might still be larger than MOD, yet same bit length.
170 // Make sure it is less, fixed-timing.
171 addM(MOD, 0, tmp, subM(MOD, 0, tmp, -1));
172
173 memcpy(c, tmp, P256_NBYTES);
174}
175
176int p256_is_odd(const p256_int* a) { return P256_DIGIT(a, 0) & 1; }
177int p256_is_even(const p256_int* a) { return !(P256_DIGIT(a, 0) & 1); }
178
179p256_digit p256_shl(const p256_int* a, int n, p256_int* b) {
180 int i;
181 p256_digit top = P256_DIGIT(a, P256_NDIGITS - 1);
182
183 n %= P256_BITSPERDIGIT;
184 for (i = P256_NDIGITS - 1; i > 0; --i) {
185 p256_digit accu = (P256_DIGIT(a, i) << n);
186 accu |= (P256_DIGIT(a, i - 1) >> (P256_BITSPERDIGIT - n));
187 P256_DIGIT(b, i) = accu;
188 }
189 P256_DIGIT(b, i) = (P256_DIGIT(a, i) << n);
190
191 top >>= (P256_BITSPERDIGIT - n);
192
193 return top;
194}
195
196void p256_shr(const p256_int* a, int n, p256_int* b) {
197 int i;
198
199 n %= P256_BITSPERDIGIT;
200 for (i = 0; i < P256_NDIGITS - 1; ++i) {
201 p256_digit accu = (P256_DIGIT(a, i) >> n);
202 accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - n));
203 P256_DIGIT(b, i) = accu;
204 }
205 P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> n);
206}
207
208static void p256_shr1(const p256_int* a, int highbit, p256_int* b) {
209 int i;
210
211 for (i = 0; i < P256_NDIGITS - 1; ++i) {
212 p256_digit accu = (P256_DIGIT(a, i) >> 1);
213 accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - 1));
214 P256_DIGIT(b, i) = accu;
215 }
216 P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> 1) |
217 (highbit << (P256_BITSPERDIGIT - 1));
218}
219
220// Return -1, 0, 1 for a < b, a == b or a > b respectively.
221int p256_cmp(const p256_int* a, const p256_int* b) {
222 int i;
223 p256_sddigit borrow = 0;
224 p256_digit notzero = 0;
225
226 for (i = 0; i < P256_NDIGITS; ++i) {
227 borrow += (p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
228 // Track whether any result digit is ever not zero.
229 // Relies on !!(non-zero) evaluating to 1, e.g., !!(-1) evaluating to 1.
230 notzero |= !!((p256_digit)borrow);
231 borrow >>= P256_BITSPERDIGIT;
232 }
233 return (int)borrow | notzero;
234}
235
236// c = a - b. Returns borrow: 0 or -1.
237int p256_sub(const p256_int* a, const p256_int* b, p256_int* c) {
238 int i;
239 p256_sddigit borrow = 0;
240
241 for (i = 0; i < P256_NDIGITS; ++i) {
242 borrow += (p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
243 if (c) P256_DIGIT(c, i) = (p256_digit)borrow;
244 borrow >>= P256_BITSPERDIGIT;
245 }
246 return (int)borrow;
247}
248
249// c = a + b. Returns carry: 0 or 1.
250int p256_add(const p256_int* a, const p256_int* b, p256_int* c) {
251 int i;
252 p256_ddigit carry = 0;
253
254 for (i = 0; i < P256_NDIGITS; ++i) {
255 carry += (p256_ddigit)P256_DIGIT(a, i) + P256_DIGIT(b, i);
256 if (c) P256_DIGIT(c, i) = (p256_digit)carry;
257 carry >>= P256_BITSPERDIGIT;
258 }
259 return (int)carry;
260}
261
262// b = a + d. Returns carry, 0 or 1.
263int p256_add_d(const p256_int* a, p256_digit d, p256_int* b) {
264 int i;
265 p256_ddigit carry = d;
266
267 for (i = 0; i < P256_NDIGITS; ++i) {
268 carry += (p256_ddigit)P256_DIGIT(a, i);
269 if (b) P256_DIGIT(b, i) = (p256_digit)carry;
270 carry >>= P256_BITSPERDIGIT;
271 }
272 return (int)carry;
273}
274
275// if (mask) dst = src, fixed-timing style.
276static void copyConditional(const p256_int* src,
277 p256_int* dst,
278 int mask) {
279 int i;
280 for (i = 0; i < P256_NDIGITS; ++i) {
281 p256_digit b = P256_DIGIT(src, i) & mask; // 0 or src[i]
282 b |= P256_DIGIT(dst, i) & ~mask; // dst[i] or 0
283 P256_DIGIT(dst, i) = b;
284 }
285}
286
287// -1 iff (x&15) == 0, 0 otherwise.
288// Relies on arithmetic shift right behavior.
289#define ZEROtoONES(x) (((int32_t)(((x)&15)-1))>>31)
290
291// tbl[0] = tbl[idx], fixed-timing style.
292static void set0ToIdx(p256_int tbl[16], int idx) {
293 int32_t i;
294 tbl[0] = p256_one;
295 for (i = 1; i < 16; ++i) {
296 copyConditional(&tbl[i], &tbl[0], ZEROtoONES(i-idx));
297 }
298}
299
300// b = 1/a mod MOD, fixed timing, Fermat's little theorem.
301void p256_modinv(const p256_int* MOD,
302 const p256_int* a,
303 p256_int* b) {
304 p256_int tbl[16];
305 int i;
306
307 // tbl[i] = a**i, tbl[0] unused.
308 tbl[1] = *a;
309 for (i = 2; i < 16; ++i) {
310 p256_modmul(MOD, &tbl[i-1], 0, a, &tbl[i]);
311 }
312
313 *b = p256_one;
314 for (i = 256; i > 0; i -= 4) {
315 int32_t idx = 0;
316 p256_modmul(MOD, b, 0, b, b);
317 p256_modmul(MOD, b, 0, b, b);
318 p256_modmul(MOD, b, 0, b, b);
319 p256_modmul(MOD, b, 0, b, b);
320 idx |= p256_get_bit(&SECP256r1_nMin2, i - 1) << 3;
321 idx |= p256_get_bit(&SECP256r1_nMin2, i - 2) << 2;
322 idx |= p256_get_bit(&SECP256r1_nMin2, i - 3) << 1;
323 idx |= p256_get_bit(&SECP256r1_nMin2, i - 4) << 0;
324 set0ToIdx(tbl, idx); // tbl[0] = tbl[idx]
325 p256_modmul(MOD, b, 0, &tbl[0], &tbl[0]);
326 copyConditional(&tbl[0], b, ~ZEROtoONES(idx));
327 }
328}
329
330// b = 1/a mod MOD, binary euclid.
331void p256_modinv_vartime(const p256_int* MOD,
332 const p256_int* a,
333 p256_int* b) {
334 p256_int R = P256_ZERO;
335 p256_int S = P256_ONE;
336 p256_int U = *MOD;
337 p256_int V = *a;
338
339 for (;;) {
340 if (p256_is_even(&U)) {
341 p256_shr1(&U, 0, &U);
342 if (p256_is_even(&R)) {
343 p256_shr1(&R, 0, &R);
344 } else {
345 // R = (R+MOD)/2
346 p256_shr1(&R, p256_add(&R, MOD, &R), &R);
347 }
348 } else if (p256_is_even(&V)) {
349 p256_shr1(&V, 0, &V);
350 if (p256_is_even(&S)) {
351 p256_shr1(&S, 0, &S);
352 } else {
353 // S = (S+MOD)/2
354 p256_shr1(&S, p256_add(&S, MOD, &S) , &S);
355 }
356 } else { // U,V both odd.
357 if (!p256_sub(&V, &U, NULL)) {
358 p256_sub(&V, &U, &V);
359 if (p256_sub(&S, &R, &S)) p256_add(&S, MOD, &S);
360 if (p256_is_zero(&V)) break; // done.
361 } else {
362 p256_sub(&U, &V, &U);
363 if (p256_sub(&R, &S, &R)) p256_add(&R, MOD, &R);
364 }
365 }
366 }
367
368 p256_mod(MOD, &R, b);
369}
370
371void p256_mod(const p256_int* MOD,
372 const p256_int* in,
373 p256_int* out) {
374 if (out != in) *out = *in;
375 addM(MOD, 0, P256_DIGITS(out), subM(MOD, 0, P256_DIGITS(out), -1));
376}
377
378// Verify y^2 == x^3 - 3x + b mod p
379// and 0 < x < p and 0 < y < p
380int p256_is_valid_point(const p256_int* x, const p256_int* y) {
381 p256_int y2, x3;
382
383 if (p256_cmp(&SECP256r1_p, x) <= 0 ||
384 p256_cmp(&SECP256r1_p, y) <= 0 ||
385 p256_is_zero(x) ||
386 p256_is_zero(y)) return 0;
387
388 p256_modmul(&SECP256r1_p, y, 0, y, &y2); // y^2
389
390 p256_modmul(&SECP256r1_p, x, 0, x, &x3); // x^2
391 p256_modmul(&SECP256r1_p, x, 0, &x3, &x3); // x^3
392 if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - x
393 if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - 2x
394 if (p256_sub(&x3, x, &x3)) p256_add(&x3, &SECP256r1_p, &x3); // x^3 - 3x
395 if (p256_add(&x3, &SECP256r1_b, &x3)) // x^3 - 3x + b
396 p256_sub(&x3, &SECP256r1_p, &x3);
397 if (p256_sub(&x3, &SECP256r1_p, &x3)) // make sure 0 <= x3 < p
398 p256_add(&x3, &SECP256r1_p, &x3);
399
400 return p256_cmp(&y2, &x3) == 0;
401}
402
403void p256_from_bin(const uint8_t src[P256_NBYTES], p256_int* dst) {
404 int i;
405 const uint8_t* p = &src[0];
406
407 for (i = P256_NDIGITS - 1; i >= 0; --i) {
408 P256_DIGIT(dst, i) =
409 (p[0] << 24) |
410 (p[1] << 16) |
411 (p[2] << 8) |
412 p[3];
413 p += 4;
414 }
415}
416
417void p256_to_bin(const p256_int* src, uint8_t dst[P256_NBYTES]) {
418 int i;
419 uint8_t* p = &dst[0];
420
421 for (i = P256_NDIGITS - 1; i >= 0; --i) {
422 p256_digit digit = P256_DIGIT(src, i);
423 p[0] = (uint8_t)(digit >> 24);
424 p[1] = (uint8_t)(digit >> 16);
425 p[2] = (uint8_t)(digit >> 8);
426 p[3] = (uint8_t)(digit);
427 p += 4;
428 }
429}