• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2 
3 #include "uECC.h"
4 #include "uECC_vli.h"
5 
6 #ifndef uECC_RNG_MAX_TRIES
7     #define uECC_RNG_MAX_TRIES 64
8 #endif
9 
10 #if uECC_ENABLE_VLI_API
11     #define uECC_VLI_API
12 #else
13     #define uECC_VLI_API static
14 #endif
15 
16 #define CONCATX(a, ...) a ## __VA_ARGS__
17 #define CONCAT(a, ...) CONCATX(a, __VA_ARGS__)
18 
19 #define STRX(a) #a
20 #define STR(a) STRX(a)
21 
22 #define EVAL(...)  EVAL1(EVAL1(EVAL1(EVAL1(__VA_ARGS__))))
23 #define EVAL1(...) EVAL2(EVAL2(EVAL2(EVAL2(__VA_ARGS__))))
24 #define EVAL2(...) EVAL3(EVAL3(EVAL3(EVAL3(__VA_ARGS__))))
25 #define EVAL3(...) EVAL4(EVAL4(EVAL4(EVAL4(__VA_ARGS__))))
26 #define EVAL4(...) __VA_ARGS__
27 
28 #define DEC_1  0
29 #define DEC_2  1
30 #define DEC_3  2
31 #define DEC_4  3
32 #define DEC_5  4
33 #define DEC_6  5
34 #define DEC_7  6
35 #define DEC_8  7
36 #define DEC_9  8
37 #define DEC_10 9
38 #define DEC_11 10
39 #define DEC_12 11
40 #define DEC_13 12
41 #define DEC_14 13
42 #define DEC_15 14
43 #define DEC_16 15
44 #define DEC_17 16
45 #define DEC_18 17
46 #define DEC_19 18
47 #define DEC_20 19
48 #define DEC_21 20
49 #define DEC_22 21
50 #define DEC_23 22
51 #define DEC_24 23
52 #define DEC_25 24
53 #define DEC_26 25
54 #define DEC_27 26
55 #define DEC_28 27
56 #define DEC_29 28
57 #define DEC_30 29
58 #define DEC_31 30
59 #define DEC_32 31
60 
61 #define DEC(N) CONCAT(DEC_, N)
62 
63 #define SECOND_ARG(_, val, ...) val
64 #define SOME_CHECK_0 ~, 0
65 #define GET_SECOND_ARG(...) SECOND_ARG(__VA_ARGS__, SOME,)
66 #define SOME_OR_0(N) GET_SECOND_ARG(CONCAT(SOME_CHECK_, N))
67 
68 #define EMPTY(...)
69 #define DEFER(...) __VA_ARGS__ EMPTY()
70 
71 #define REPEAT_NAME_0() REPEAT_0
72 #define REPEAT_NAME_SOME() REPEAT_SOME
73 #define REPEAT_0(...)
74 #define REPEAT_SOME(N, stuff) DEFER(CONCAT(REPEAT_NAME_, SOME_OR_0(DEC(N))))()(DEC(N), stuff) stuff
75 #define REPEAT(N, stuff) EVAL(REPEAT_SOME(N, stuff))
76 
77 #define REPEATM_NAME_0() REPEATM_0
78 #define REPEATM_NAME_SOME() REPEATM_SOME
79 #define REPEATM_0(...)
80 #define REPEATM_SOME(N, macro) macro(N) \
81     DEFER(CONCAT(REPEATM_NAME_, SOME_OR_0(DEC(N))))()(DEC(N), macro)
82 #define REPEATM(N, macro) EVAL(REPEATM_SOME(N, macro))
83 
84 #include "platform-specific.inc"
85 
86 #if (uECC_WORD_SIZE == 1)
87     #if uECC_SUPPORTS_secp160r1
88         #define uECC_MAX_WORDS 21 /* Due to the size of curve_n. */
89     #endif
90     #if uECC_SUPPORTS_secp192r1
91         #undef uECC_MAX_WORDS
92         #define uECC_MAX_WORDS 24
93     #endif
94     #if uECC_SUPPORTS_secp224r1
95         #undef uECC_MAX_WORDS
96         #define uECC_MAX_WORDS 28
97     #endif
98     #if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1)
99         #undef uECC_MAX_WORDS
100         #define uECC_MAX_WORDS 32
101     #endif
102 #elif (uECC_WORD_SIZE == 4)
103     #if uECC_SUPPORTS_secp160r1
104         #define uECC_MAX_WORDS 6 /* Due to the size of curve_n. */
105     #endif
106     #if uECC_SUPPORTS_secp192r1
107         #undef uECC_MAX_WORDS
108         #define uECC_MAX_WORDS 6
109     #endif
110     #if uECC_SUPPORTS_secp224r1
111         #undef uECC_MAX_WORDS
112         #define uECC_MAX_WORDS 7
113     #endif
114     #if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1)
115         #undef uECC_MAX_WORDS
116         #define uECC_MAX_WORDS 8
117     #endif
118 #elif (uECC_WORD_SIZE == 8)
119     #if uECC_SUPPORTS_secp160r1
120         #define uECC_MAX_WORDS 3
121     #endif
122     #if uECC_SUPPORTS_secp192r1
123         #undef uECC_MAX_WORDS
124         #define uECC_MAX_WORDS 3
125     #endif
126     #if uECC_SUPPORTS_secp224r1
127         #undef uECC_MAX_WORDS
128         #define uECC_MAX_WORDS 4
129     #endif
130     #if (uECC_SUPPORTS_secp256r1 || uECC_SUPPORTS_secp256k1)
131         #undef uECC_MAX_WORDS
132         #define uECC_MAX_WORDS 4
133     #endif
134 #endif /* uECC_WORD_SIZE */
135 
136 #define BITS_TO_WORDS(num_bits) ((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8))
137 #define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
138 
139 struct uECC_Curve_t {
140     wordcount_t num_words;
141     wordcount_t num_bytes;
142     bitcount_t num_n_bits;
143     uECC_word_t p[uECC_MAX_WORDS];
144     uECC_word_t n[uECC_MAX_WORDS];
145     uECC_word_t G[uECC_MAX_WORDS * 2];
146     uECC_word_t b[uECC_MAX_WORDS];
147     void (*double_jacobian)(uECC_word_t * X1,
148                             uECC_word_t * Y1,
149                             uECC_word_t * Z1,
150                             uECC_Curve curve);
151 #if uECC_SUPPORT_COMPRESSED_POINT
152     void (*mod_sqrt)(uECC_word_t *a, uECC_Curve curve);
153 #endif
154     void (*x_side)(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve);
155 #if (uECC_OPTIMIZATION_LEVEL > 0)
156     void (*mmod_fast)(uECC_word_t *result, uECC_word_t *product);
157 #endif
158 };
159 
160 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
bcopy(uint8_t * dst,const uint8_t * src,unsigned num_bytes)161 static void bcopy(uint8_t *dst,
162                   const uint8_t *src,
163                   unsigned num_bytes) {
164     while (0 != num_bytes) {
165         num_bytes--;
166         dst[num_bytes] = src[num_bytes];
167     }
168 }
169 #endif
170 
171 static cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
172                                        const uECC_word_t *right,
173                                        wordcount_t num_words);
174 
175 #if (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \
176         uECC_PLATFORM == uECC_arm_thumb2)
177     #include "asm_arm.inc"
178 #endif
179 
180 #if (uECC_PLATFORM == uECC_avr)
181     #include "asm_avr.inc"
182 #endif
183 
184 #if default_RNG_defined
185 static uECC_RNG_Function g_rng_function = &default_RNG;
186 #else
187 static uECC_RNG_Function g_rng_function = 0;
188 #endif
189 
uECC_set_rng(uECC_RNG_Function rng_function)190 void uECC_set_rng(uECC_RNG_Function rng_function) {
191     g_rng_function = rng_function;
192 }
193 
uECC_get_rng(void)194 uECC_RNG_Function uECC_get_rng(void) {
195     return g_rng_function;
196 }
197 
uECC_curve_private_key_size(uECC_Curve curve)198 int uECC_curve_private_key_size(uECC_Curve curve) {
199     return BITS_TO_BYTES(curve->num_n_bits);
200 }
201 
uECC_curve_public_key_size(uECC_Curve curve)202 int uECC_curve_public_key_size(uECC_Curve curve) {
203     return 2 * curve->num_bytes;
204 }
205 
206 #if !asm_clear
uECC_vli_clear(uECC_word_t * vli,wordcount_t num_words)207 uECC_VLI_API void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words) {
208     wordcount_t i;
209     for (i = 0; i < num_words; ++i) {
210         vli[i] = 0;
211     }
212 }
213 #endif /* !asm_clear */
214 
215 /* Constant-time comparison to zero - secure way to compare long integers */
216 /* Returns 1 if vli == 0, 0 otherwise. */
uECC_vli_isZero(const uECC_word_t * vli,wordcount_t num_words)217 uECC_VLI_API uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words) {
218     uECC_word_t bits = 0;
219     wordcount_t i;
220     for (i = 0; i < num_words; ++i) {
221         bits |= vli[i];
222     }
223     return (bits == 0);
224 }
225 
226 /* Returns nonzero if bit 'bit' of vli is set. */
uECC_vli_testBit(const uECC_word_t * vli,bitcount_t bit)227 uECC_VLI_API uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit) {
228     return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
229 }
230 
231 /* Counts the number of words in vli. */
vli_numDigits(const uECC_word_t * vli,const wordcount_t max_words)232 static wordcount_t vli_numDigits(const uECC_word_t *vli, const wordcount_t max_words) {
233     wordcount_t i;
234     /* Search from the end until we find a non-zero digit.
235        We do it in reverse because we expect that most digits will be nonzero. */
236     for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
237     }
238 
239     return (i + 1);
240 }
241 
242 /* Counts the number of bits required to represent vli. */
uECC_vli_numBits(const uECC_word_t * vli,const wordcount_t max_words)243 uECC_VLI_API bitcount_t uECC_vli_numBits(const uECC_word_t *vli, const wordcount_t max_words) {
244     uECC_word_t i;
245     uECC_word_t digit;
246 
247     wordcount_t num_digits = vli_numDigits(vli, max_words);
248     if (num_digits == 0) {
249         return 0;
250     }
251 
252     digit = vli[num_digits - 1];
253     for (i = 0; digit; ++i) {
254         digit >>= 1;
255     }
256 
257     return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
258 }
259 
260 /* Sets dest = src. */
261 #if !asm_set
uECC_vli_set(uECC_word_t * dest,const uECC_word_t * src,wordcount_t num_words)262 uECC_VLI_API void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, wordcount_t num_words) {
263     wordcount_t i;
264     for (i = 0; i < num_words; ++i) {
265         dest[i] = src[i];
266     }
267 }
268 #endif /* !asm_set */
269 
270 /* Returns sign of left - right. */
uECC_vli_cmp_unsafe(const uECC_word_t * left,const uECC_word_t * right,wordcount_t num_words)271 static cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
272                                        const uECC_word_t *right,
273                                        wordcount_t num_words) {
274     wordcount_t i;
275     for (i = num_words - 1; i >= 0; --i) {
276         if (left[i] > right[i]) {
277             return 1;
278         } else if (left[i] < right[i]) {
279             return -1;
280         }
281     }
282     return 0;
283 }
284 
285 /* Constant-time comparison function - secure way to compare long integers */
286 /* Returns one if left == right, zero otherwise. */
uECC_vli_equal(const uECC_word_t * left,const uECC_word_t * right,wordcount_t num_words)287 uECC_VLI_API uECC_word_t uECC_vli_equal(const uECC_word_t *left,
288                                         const uECC_word_t *right,
289                                         wordcount_t num_words) {
290     uECC_word_t diff = 0;
291     wordcount_t i;
292     for (i = num_words - 1; i >= 0; --i) {
293         diff |= (left[i] ^ right[i]);
294     }
295     return (diff == 0);
296 }
297 
298 uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
299                                       const uECC_word_t *left,
300                                       const uECC_word_t *right,
301                                       wordcount_t num_words);
302 
303 /* Returns sign of left - right, in constant time. */
uECC_vli_cmp(const uECC_word_t * left,const uECC_word_t * right,wordcount_t num_words)304 uECC_VLI_API cmpresult_t uECC_vli_cmp(const uECC_word_t *left,
305                                       const uECC_word_t *right,
306                                       wordcount_t num_words) {
307     uECC_word_t tmp[uECC_MAX_WORDS];
308     uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
309     uECC_word_t equal = uECC_vli_isZero(tmp, num_words);
310     return (!equal - 2 * neg);
311 }
312 
313 /* Computes vli = vli >> 1. */
314 #if !asm_rshift1
uECC_vli_rshift1(uECC_word_t * vli,wordcount_t num_words)315 uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) {
316     uECC_word_t *end = vli;
317     uECC_word_t carry = 0;
318 
319     vli += num_words;
320     while (vli-- > end) {
321         uECC_word_t temp = *vli;
322         *vli = (temp >> 1) | carry;
323         carry = temp << (uECC_WORD_BITS - 1);
324     }
325 }
326 #endif /* !asm_rshift1 */
327 
328 /* Computes result = left + right, returning carry. Can modify in place. */
329 #if !asm_add
uECC_vli_add(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,wordcount_t num_words)330 uECC_VLI_API uECC_word_t uECC_vli_add(uECC_word_t *result,
331                                       const uECC_word_t *left,
332                                       const uECC_word_t *right,
333                                       wordcount_t num_words) {
334     uECC_word_t carry = 0;
335     wordcount_t i;
336     for (i = 0; i < num_words; ++i) {
337         uECC_word_t sum = left[i] + right[i] + carry;
338         if (sum != left[i]) {
339             carry = (sum < left[i]);
340         }
341         result[i] = sum;
342     }
343     return carry;
344 }
345 #endif /* !asm_add */
346 
347 /* Computes result = left - right, returning borrow. Can modify in place. */
348 #if !asm_sub
uECC_vli_sub(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,wordcount_t num_words)349 uECC_VLI_API uECC_word_t uECC_vli_sub(uECC_word_t *result,
350                                       const uECC_word_t *left,
351                                       const uECC_word_t *right,
352                                       wordcount_t num_words) {
353     uECC_word_t borrow = 0;
354     wordcount_t i;
355     for (i = 0; i < num_words; ++i) {
356         uECC_word_t diff = left[i] - right[i] - borrow;
357         if (diff != left[i]) {
358             borrow = (diff > left[i]);
359         }
360         result[i] = diff;
361     }
362     return borrow;
363 }
364 #endif /* !asm_sub */
365 
366 #if !asm_mult || (uECC_SQUARE_FUNC && !asm_square) || \
367     (uECC_SUPPORTS_secp256k1 && (uECC_OPTIMIZATION_LEVEL > 0) && \
368         ((uECC_WORD_SIZE == 1) || (uECC_WORD_SIZE == 8)))
muladd(uECC_word_t a,uECC_word_t b,uECC_word_t * r0,uECC_word_t * r1,uECC_word_t * r2)369 static void muladd(uECC_word_t a,
370                    uECC_word_t b,
371                    uECC_word_t *r0,
372                    uECC_word_t *r1,
373                    uECC_word_t *r2) {
374 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
375     uint64_t a0 = a & 0xffffffffull;
376     uint64_t a1 = a >> 32;
377     uint64_t b0 = b & 0xffffffffull;
378     uint64_t b1 = b >> 32;
379 
380     uint64_t i0 = a0 * b0;
381     uint64_t i1 = a0 * b1;
382     uint64_t i2 = a1 * b0;
383     uint64_t i3 = a1 * b1;
384 
385     uint64_t p0, p1;
386 
387     i2 += (i0 >> 32);
388     i2 += i1;
389     if (i2 < i1) { /* overflow */
390         i3 += 0x100000000ull;
391     }
392 
393     p0 = (i0 & 0xffffffffull) | (i2 << 32);
394     p1 = i3 + (i2 >> 32);
395 
396     *r0 += p0;
397     *r1 += (p1 + (*r0 < p0));
398     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
399 #else
400     uECC_dword_t p = (uECC_dword_t)a * b;
401     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
402     r01 += p;
403     *r2 += (r01 < p);
404     *r1 = r01 >> uECC_WORD_BITS;
405     *r0 = (uECC_word_t)r01;
406 #endif
407 }
408 #endif /* muladd needed */
409 
410 #if !asm_mult
uECC_vli_mult(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,wordcount_t num_words)411 uECC_VLI_API void uECC_vli_mult(uECC_word_t *result,
412                                 const uECC_word_t *left,
413                                 const uECC_word_t *right,
414                                 wordcount_t num_words) {
415     uECC_word_t r0 = 0;
416     uECC_word_t r1 = 0;
417     uECC_word_t r2 = 0;
418     wordcount_t i, k;
419 
420     /* Compute each digit of result in sequence, maintaining the carries. */
421     for (k = 0; k < num_words; ++k) {
422         for (i = 0; i <= k; ++i) {
423             muladd(left[i], right[k - i], &r0, &r1, &r2);
424         }
425         result[k] = r0;
426         r0 = r1;
427         r1 = r2;
428         r2 = 0;
429     }
430     for (k = num_words; k < num_words * 2 - 1; ++k) {
431         for (i = (k + 1) - num_words; i < num_words; ++i) {
432             muladd(left[i], right[k - i], &r0, &r1, &r2);
433         }
434         result[k] = r0;
435         r0 = r1;
436         r1 = r2;
437         r2 = 0;
438     }
439     result[num_words * 2 - 1] = r0;
440 }
441 #endif /* !asm_mult */
442 
443 #if uECC_SQUARE_FUNC
444 
445 #if !asm_square
mul2add(uECC_word_t a,uECC_word_t b,uECC_word_t * r0,uECC_word_t * r1,uECC_word_t * r2)446 static void mul2add(uECC_word_t a,
447                     uECC_word_t b,
448                     uECC_word_t *r0,
449                     uECC_word_t *r1,
450                     uECC_word_t *r2) {
451 #if uECC_WORD_SIZE == 8 && !SUPPORTS_INT128
452     uint64_t a0 = a & 0xffffffffull;
453     uint64_t a1 = a >> 32;
454     uint64_t b0 = b & 0xffffffffull;
455     uint64_t b1 = b >> 32;
456 
457     uint64_t i0 = a0 * b0;
458     uint64_t i1 = a0 * b1;
459     uint64_t i2 = a1 * b0;
460     uint64_t i3 = a1 * b1;
461 
462     uint64_t p0, p1;
463 
464     i2 += (i0 >> 32);
465     i2 += i1;
466     if (i2 < i1)
467     { /* overflow */
468         i3 += 0x100000000ull;
469     }
470 
471     p0 = (i0 & 0xffffffffull) | (i2 << 32);
472     p1 = i3 + (i2 >> 32);
473 
474     *r2 += (p1 >> 63);
475     p1 = (p1 << 1) | (p0 >> 63);
476     p0 <<= 1;
477 
478     *r0 += p0;
479     *r1 += (p1 + (*r0 < p0));
480     *r2 += ((*r1 < p1) || (*r1 == p1 && *r0 < p0));
481 #else
482     uECC_dword_t p = (uECC_dword_t)a * b;
483     uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
484     *r2 += (p >> (uECC_WORD_BITS * 2 - 1));
485     p *= 2;
486     r01 += p;
487     *r2 += (r01 < p);
488     *r1 = r01 >> uECC_WORD_BITS;
489     *r0 = (uECC_word_t)r01;
490 #endif
491 }
492 
uECC_vli_square(uECC_word_t * result,const uECC_word_t * left,wordcount_t num_words)493 uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
494                                   const uECC_word_t *left,
495                                   wordcount_t num_words) {
496     uECC_word_t r0 = 0;
497     uECC_word_t r1 = 0;
498     uECC_word_t r2 = 0;
499 
500     wordcount_t i, k;
501 
502     for (k = 0; k < num_words * 2 - 1; ++k) {
503         uECC_word_t min = (k < num_words ? 0 : (k + 1) - num_words);
504         for (i = min; i <= k && i <= k - i; ++i) {
505             if (i < k-i) {
506                 mul2add(left[i], left[k - i], &r0, &r1, &r2);
507             } else {
508                 muladd(left[i], left[k - i], &r0, &r1, &r2);
509             }
510         }
511         result[k] = r0;
512         r0 = r1;
513         r1 = r2;
514         r2 = 0;
515     }
516 
517     result[num_words * 2 - 1] = r0;
518 }
519 #endif /* !asm_square */
520 
521 #else /* uECC_SQUARE_FUNC */
522 
523 #if uECC_ENABLE_VLI_API
uECC_vli_square(uECC_word_t * result,const uECC_word_t * left,wordcount_t num_words)524 uECC_VLI_API void uECC_vli_square(uECC_word_t *result,
525                                   const uECC_word_t *left,
526                                   wordcount_t num_words) {
527     uECC_vli_mult(result, left, left, num_words);
528 }
529 #endif /* uECC_ENABLE_VLI_API */
530 
531 #endif /* uECC_SQUARE_FUNC */
532 
533 /* Computes result = (left + right) % mod.
534    Assumes that left < mod and right < mod, and that result does not overlap mod. */
uECC_vli_modAdd(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,const uECC_word_t * mod,wordcount_t num_words)535 uECC_VLI_API void uECC_vli_modAdd(uECC_word_t *result,
536                                   const uECC_word_t *left,
537                                   const uECC_word_t *right,
538                                   const uECC_word_t *mod,
539                                   wordcount_t num_words) {
540     uECC_word_t carry = uECC_vli_add(result, left, right, num_words);
541     if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
542         /* result > mod (result = mod + remainder), so subtract mod to get remainder. */
543         uECC_vli_sub(result, result, mod, num_words);
544     }
545 }
546 
547 /* Computes result = (left - right) % mod.
548    Assumes that left < mod and right < mod, and that result does not overlap mod. */
uECC_vli_modSub(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,const uECC_word_t * mod,wordcount_t num_words)549 uECC_VLI_API void uECC_vli_modSub(uECC_word_t *result,
550                                   const uECC_word_t *left,
551                                   const uECC_word_t *right,
552                                   const uECC_word_t *mod,
553                                   wordcount_t num_words) {
554     uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
555     if (l_borrow) {
556         /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
557            we can get the correct result from result + mod (with overflow). */
558         uECC_vli_add(result, result, mod, num_words);
559     }
560 }
561 
562 /* Computes result = product % mod, where product is 2N words long. */
563 /* Currently only designed to work for curve_p or curve_n. */
uECC_vli_mmod(uECC_word_t * result,uECC_word_t * product,const uECC_word_t * mod,wordcount_t num_words)564 uECC_VLI_API void uECC_vli_mmod(uECC_word_t *result,
565                                 uECC_word_t *product,
566                                 const uECC_word_t *mod,
567                                 wordcount_t num_words) {
568     uECC_word_t mod_multiple[2 * uECC_MAX_WORDS];
569     uECC_word_t tmp[2 * uECC_MAX_WORDS];
570     uECC_word_t *v[2] = {tmp, product};
571     uECC_word_t index;
572 
573     /* Shift mod so its highest set bit is at the maximum position. */
574     bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) - uECC_vli_numBits(mod, num_words);
575     wordcount_t word_shift = shift / uECC_WORD_BITS;
576     wordcount_t bit_shift = shift % uECC_WORD_BITS;
577     uECC_word_t carry = 0;
578     uECC_vli_clear(mod_multiple, word_shift);
579     if (bit_shift > 0) {
580         for(index = 0; index < (uECC_word_t)num_words; ++index) {
581             mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
582             carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
583         }
584     } else {
585         uECC_vli_set(mod_multiple + word_shift, mod, num_words);
586     }
587 
588     for (index = 1; shift >= 0; --shift) {
589         uECC_word_t borrow = 0;
590         wordcount_t i;
591         for (i = 0; i < num_words * 2; ++i) {
592             uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
593             if (diff != v[index][i]) {
594                 borrow = (diff > v[index][i]);
595             }
596             v[1 - index][i] = diff;
597         }
598         index = !(index ^ borrow); /* Swap the index if there was no borrow */
599         uECC_vli_rshift1(mod_multiple, num_words);
600         mod_multiple[num_words - 1] |= mod_multiple[num_words] << (uECC_WORD_BITS - 1);
601         uECC_vli_rshift1(mod_multiple + num_words, num_words);
602     }
603     uECC_vli_set(result, v[index], num_words);
604 }
605 
606 /* Computes result = (left * right) % mod. */
uECC_vli_modMult(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,const uECC_word_t * mod,wordcount_t num_words)607 uECC_VLI_API void uECC_vli_modMult(uECC_word_t *result,
608                                    const uECC_word_t *left,
609                                    const uECC_word_t *right,
610                                    const uECC_word_t *mod,
611                                    wordcount_t num_words) {
612     uECC_word_t product[2 * uECC_MAX_WORDS];
613     uECC_vli_mult(product, left, right, num_words);
614     uECC_vli_mmod(result, product, mod, num_words);
615 }
616 
uECC_vli_modMult_fast(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * right,uECC_Curve curve)617 uECC_VLI_API void uECC_vli_modMult_fast(uECC_word_t *result,
618                                         const uECC_word_t *left,
619                                         const uECC_word_t *right,
620                                         uECC_Curve curve) {
621     uECC_word_t product[2 * uECC_MAX_WORDS];
622     uECC_vli_mult(product, left, right, curve->num_words);
623 #if (uECC_OPTIMIZATION_LEVEL > 0)
624     curve->mmod_fast(result, product);
625 #else
626     uECC_vli_mmod(result, product, curve->p, curve->num_words);
627 #endif
628 }
629 
630 #if uECC_SQUARE_FUNC
631 
632 #if uECC_ENABLE_VLI_API
633 /* Computes result = left^2 % mod. */
uECC_vli_modSquare(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * mod,wordcount_t num_words)634 uECC_VLI_API void uECC_vli_modSquare(uECC_word_t *result,
635                                      const uECC_word_t *left,
636                                      const uECC_word_t *mod,
637                                      wordcount_t num_words) {
638     uECC_word_t product[2 * uECC_MAX_WORDS];
639     uECC_vli_square(product, left, num_words);
640     uECC_vli_mmod(result, product, mod, num_words);
641 }
642 #endif /* uECC_ENABLE_VLI_API */
643 
uECC_vli_modSquare_fast(uECC_word_t * result,const uECC_word_t * left,uECC_Curve curve)644 uECC_VLI_API void uECC_vli_modSquare_fast(uECC_word_t *result,
645                                           const uECC_word_t *left,
646                                           uECC_Curve curve) {
647     uECC_word_t product[2 * uECC_MAX_WORDS];
648     uECC_vli_square(product, left, curve->num_words);
649 #if (uECC_OPTIMIZATION_LEVEL > 0)
650     curve->mmod_fast(result, product);
651 #else
652     uECC_vli_mmod(result, product, curve->p, curve->num_words);
653 #endif
654 }
655 
656 #else /* uECC_SQUARE_FUNC */
657 
658 #if uECC_ENABLE_VLI_API
uECC_vli_modSquare(uECC_word_t * result,const uECC_word_t * left,const uECC_word_t * mod,wordcount_t num_words)659 uECC_VLI_API void uECC_vli_modSquare(uECC_word_t *result,
660                                      const uECC_word_t *left,
661                                      const uECC_word_t *mod,
662                                      wordcount_t num_words) {
663     uECC_vli_modMult(result, left, left, mod, num_words);
664 }
665 #endif /* uECC_ENABLE_VLI_API */
666 
uECC_vli_modSquare_fast(uECC_word_t * result,const uECC_word_t * left,uECC_Curve curve)667 uECC_VLI_API void uECC_vli_modSquare_fast(uECC_word_t *result,
668                                           const uECC_word_t *left,
669                                           uECC_Curve curve) {
670     uECC_vli_modMult_fast(result, left, left, curve);
671 }
672 
673 #endif /* uECC_SQUARE_FUNC */
674 
675 #define EVEN(vli) (!(vli[0] & 1))
vli_modInv_update(uECC_word_t * uv,const uECC_word_t * mod,wordcount_t num_words)676 static void vli_modInv_update(uECC_word_t *uv,
677                               const uECC_word_t *mod,
678                               wordcount_t num_words) {
679     uECC_word_t carry = 0;
680     if (!EVEN(uv)) {
681         carry = uECC_vli_add(uv, uv, mod, num_words);
682     }
683     uECC_vli_rshift1(uv, num_words);
684     if (carry) {
685         uv[num_words - 1] |= HIGH_BIT_SET;
686     }
687 }
688 
689 /* Computes result = (1 / input) % mod. All VLIs are the same size.
690    See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" */
uECC_vli_modInv(uECC_word_t * result,const uECC_word_t * input,const uECC_word_t * mod,wordcount_t num_words)691 uECC_VLI_API void uECC_vli_modInv(uECC_word_t *result,
692                                   const uECC_word_t *input,
693                                   const uECC_word_t *mod,
694                                   wordcount_t num_words) {
695     uECC_word_t a[uECC_MAX_WORDS], b[uECC_MAX_WORDS], u[uECC_MAX_WORDS], v[uECC_MAX_WORDS];
696     cmpresult_t cmpResult;
697 
698     if (uECC_vli_isZero(input, num_words)) {
699         uECC_vli_clear(result, num_words);
700         return;
701     }
702 
703     uECC_vli_set(a, input, num_words);
704     uECC_vli_set(b, mod, num_words);
705     uECC_vli_clear(u, num_words);
706     u[0] = 1;
707     uECC_vli_clear(v, num_words);
708     while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
709         if (EVEN(a)) {
710             uECC_vli_rshift1(a, num_words);
711             vli_modInv_update(u, mod, num_words);
712         } else if (EVEN(b)) {
713             uECC_vli_rshift1(b, num_words);
714             vli_modInv_update(v, mod, num_words);
715         } else if (cmpResult > 0) {
716             uECC_vli_sub(a, a, b, num_words);
717             uECC_vli_rshift1(a, num_words);
718             if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
719                 uECC_vli_add(u, u, mod, num_words);
720             }
721             uECC_vli_sub(u, u, v, num_words);
722             vli_modInv_update(u, mod, num_words);
723         } else {
724             uECC_vli_sub(b, b, a, num_words);
725             uECC_vli_rshift1(b, num_words);
726             if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
727                 uECC_vli_add(v, v, mod, num_words);
728             }
729             uECC_vli_sub(v, v, u, num_words);
730             vli_modInv_update(v, mod, num_words);
731         }
732     }
733     uECC_vli_set(result, u, num_words);
734 }
735 
736 /* ------ Point operations ------ */
737 
738 #include "curve-specific.inc"
739 
740 /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */
741 #define EccPoint_isZero(point, curve) uECC_vli_isZero((point), (curve)->num_words * 2)
742 
743 /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates.
744 From http://eprint.iacr.org/2011/338.pdf
745 */
746 
747 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
apply_z(uECC_word_t * X1,uECC_word_t * Y1,const uECC_word_t * const Z,uECC_Curve curve)748 static void apply_z(uECC_word_t * X1,
749                     uECC_word_t * Y1,
750                     const uECC_word_t * const Z,
751                     uECC_Curve curve) {
752     uECC_word_t t1[uECC_MAX_WORDS];
753 
754     uECC_vli_modSquare_fast(t1, Z, curve);    /* z^2 */
755     uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */
756     uECC_vli_modMult_fast(t1, t1, Z, curve);  /* z^3 */
757     uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */
758 }
759 
760 /* P = (x1, y1) => 2P, (x2, y2) => P' */
XYcZ_initial_double(uECC_word_t * X1,uECC_word_t * Y1,uECC_word_t * X2,uECC_word_t * Y2,const uECC_word_t * const initial_Z,uECC_Curve curve)761 static void XYcZ_initial_double(uECC_word_t * X1,
762                                 uECC_word_t * Y1,
763                                 uECC_word_t * X2,
764                                 uECC_word_t * Y2,
765                                 const uECC_word_t * const initial_Z,
766                                 uECC_Curve curve) {
767     uECC_word_t z[uECC_MAX_WORDS];
768     wordcount_t num_words = curve->num_words;
769     if (initial_Z) {
770         uECC_vli_set(z, initial_Z, num_words);
771     } else {
772         uECC_vli_clear(z, num_words);
773         z[0] = 1;
774     }
775 
776     uECC_vli_set(X2, X1, num_words);
777     uECC_vli_set(Y2, Y1, num_words);
778 
779     apply_z(X1, Y1, z, curve);
780     curve->double_jacobian(X1, Y1, z, curve);
781     apply_z(X2, Y2, z, curve);
782 }
783 
784 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
785    Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
786    or P => P', Q => P + Q
787 */
XYcZ_add(uECC_word_t * X1,uECC_word_t * Y1,uECC_word_t * X2,uECC_word_t * Y2,uECC_Curve curve)788 static void XYcZ_add(uECC_word_t * X1,
789                      uECC_word_t * Y1,
790                      uECC_word_t * X2,
791                      uECC_word_t * Y2,
792                      uECC_Curve curve) {
793     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
794     uECC_word_t t5[uECC_MAX_WORDS];
795     wordcount_t num_words = curve->num_words;
796 
797     uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
798     uECC_vli_modSquare_fast(t5, t5, curve);                  /* t5 = (x2 - x1)^2 = A */
799     uECC_vli_modMult_fast(X1, X1, t5, curve);                /* t1 = x1*A = B */
800     uECC_vli_modMult_fast(X2, X2, t5, curve);                /* t3 = x2*A = C */
801     uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
802     uECC_vli_modSquare_fast(t5, Y2, curve);                  /* t5 = (y2 - y1)^2 = D */
803 
804     uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
805     uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
806     uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
807     uECC_vli_modMult_fast(Y1, Y1, X2, curve);                /* t2 = y1*(C - B) */
808     uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
809     uECC_vli_modMult_fast(Y2, Y2, X2, curve);                /* t4 = (y2 - y1)*(B - x3) */
810     uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
811 
812     uECC_vli_set(X2, t5, num_words);
813 }
814 
815 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
816    Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
817    or P => P - Q, Q => P + Q
818 */
XYcZ_addC(uECC_word_t * X1,uECC_word_t * Y1,uECC_word_t * X2,uECC_word_t * Y2,uECC_Curve curve)819 static void XYcZ_addC(uECC_word_t * X1,
820                       uECC_word_t * Y1,
821                       uECC_word_t * X2,
822                       uECC_word_t * Y2,
823                       uECC_Curve curve) {
824     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
825     uECC_word_t t5[uECC_MAX_WORDS];
826     uECC_word_t t6[uECC_MAX_WORDS];
827     uECC_word_t t7[uECC_MAX_WORDS];
828     wordcount_t num_words = curve->num_words;
829 
830     uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
831     uECC_vli_modSquare_fast(t5, t5, curve);                  /* t5 = (x2 - x1)^2 = A */
832     uECC_vli_modMult_fast(X1, X1, t5, curve);                /* t1 = x1*A = B */
833     uECC_vli_modMult_fast(X2, X2, t5, curve);                /* t3 = x2*A = C */
834     uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
835     uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
836 
837     uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
838     uECC_vli_modMult_fast(Y1, Y1, t6, curve);                /* t2 = y1 * (C - B) = E */
839     uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
840     uECC_vli_modSquare_fast(X2, Y2, curve);                  /* t3 = (y2 - y1)^2 = D */
841     uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
842 
843     uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
844     uECC_vli_modMult_fast(Y2, Y2, t7, curve);                /* t4 = (y2 - y1)*(B - x3) */
845     uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = (y2 - y1)*(B - x3) - E = y3 */
846 
847     uECC_vli_modSquare_fast(t7, t5, curve);                  /* t7 = (y2 + y1)^2 = F */
848     uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
849     uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
850     uECC_vli_modMult_fast(t6, t6, t5, curve);                /* t6 = (y2+y1)*(x3' - B) */
851     uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words); /* t2 = (y2+y1)*(x3' - B) - E = y3' */
852 
853     uECC_vli_set(X1, t7, num_words);
854 }
855 
856 /* result may overlap point. */
EccPoint_mult(uECC_word_t * result,const uECC_word_t * point,const uECC_word_t * scalar,const uECC_word_t * initial_Z,bitcount_t num_bits,uECC_Curve curve)857 static void EccPoint_mult(uECC_word_t * result,
858                           const uECC_word_t * point,
859                           const uECC_word_t * scalar,
860                           const uECC_word_t * initial_Z,
861                           bitcount_t num_bits,
862                           uECC_Curve curve) {
863     /* R0 and R1 */
864     uECC_word_t Rx[2][uECC_MAX_WORDS];
865     uECC_word_t Ry[2][uECC_MAX_WORDS];
866     uECC_word_t z[uECC_MAX_WORDS];
867     bitcount_t i;
868     uECC_word_t nb;
869     wordcount_t num_words = curve->num_words;
870 
871     uECC_vli_set(Rx[1], point, num_words);
872     uECC_vli_set(Ry[1], point + num_words, num_words);
873 
874     XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
875 
876     for (i = num_bits - 2; i > 0; --i) {
877         nb = !uECC_vli_testBit(scalar, i);
878         XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
879         XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
880     }
881 
882     nb = !uECC_vli_testBit(scalar, 0);
883     XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
884 
885     /* Find final 1/Z value. */
886     uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
887     uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve);               /* Yb * (X1 - X0) */
888     uECC_vli_modMult_fast(z, z, point, curve);                    /* xP * Yb * (X1 - X0) */
889     uECC_vli_modInv(z, z, curve->p, num_words);            /* 1 / (xP * Yb * (X1 - X0)) */
890     /* yP / (xP * Yb * (X1 - X0)) */
891     uECC_vli_modMult_fast(z, z, point + num_words, curve);
892     uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve); /* Xb * yP / (xP * Yb * (X1 - X0)) */
893     /* End 1/Z calculation */
894 
895     XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
896     apply_z(Rx[0], Ry[0], z, curve);
897 
898     uECC_vli_set(result, Rx[0], num_words);
899     uECC_vli_set(result + num_words, Ry[0], num_words);
900 }
901 
regularize_k(const uECC_word_t * const k,uECC_word_t * k0,uECC_word_t * k1,uECC_Curve curve)902 static uECC_word_t regularize_k(const uECC_word_t * const k,
903                                 uECC_word_t *k0,
904                                 uECC_word_t *k1,
905                                 uECC_Curve curve) {
906     wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
907     bitcount_t num_n_bits = curve->num_n_bits;
908     uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) ||
909         (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
910          uECC_vli_testBit(k0, num_n_bits));
911     uECC_vli_add(k1, k0, curve->n, num_n_words);
912     return carry;
913 }
914 
EccPoint_compute_public_key(uECC_word_t * result,uECC_word_t * private_key,uECC_Curve curve)915 static uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
916                                                uECC_word_t *private_key,
917                                                uECC_Curve curve) {
918     uECC_word_t tmp1[uECC_MAX_WORDS];
919     uECC_word_t tmp2[uECC_MAX_WORDS];
920     uECC_word_t *p2[2] = {tmp1, tmp2};
921     uECC_word_t carry;
922 
923     /* Regularize the bitcount for the private key so that attackers cannot use a side channel
924        attack to learn the number of leading zeros. */
925     carry = regularize_k(private_key, tmp1, tmp2, curve);
926 
927     EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve);
928 
929     if (EccPoint_isZero(result, curve)) {
930         return 0;
931     }
932     return 1;
933 }
934 
935 #if uECC_WORD_SIZE == 1
936 
uECC_vli_nativeToBytes(uint8_t * bytes,int num_bytes,const uint8_t * native)937 uECC_VLI_API void uECC_vli_nativeToBytes(uint8_t *bytes,
938                                          int num_bytes,
939                                          const uint8_t *native) {
940     wordcount_t i;
941     for (i = 0; i < num_bytes; ++i) {
942         bytes[i] = native[(num_bytes - 1) - i];
943     }
944 }
945 
uECC_vli_bytesToNative(uint8_t * native,const uint8_t * bytes,int num_bytes)946 uECC_VLI_API void uECC_vli_bytesToNative(uint8_t *native,
947                                          const uint8_t *bytes,
948                                          int num_bytes) {
949     uECC_vli_nativeToBytes(native, num_bytes, bytes);
950 }
951 
952 #else
953 
uECC_vli_nativeToBytes(uint8_t * bytes,int num_bytes,const uECC_word_t * native)954 uECC_VLI_API void uECC_vli_nativeToBytes(uint8_t *bytes,
955                                          int num_bytes,
956                                          const uECC_word_t *native) {
957     wordcount_t i;
958     for (i = 0; i < num_bytes; ++i) {
959         unsigned b = num_bytes - 1 - i;
960         bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
961     }
962 }
963 
uECC_vli_bytesToNative(uECC_word_t * native,const uint8_t * bytes,int num_bytes)964 uECC_VLI_API void uECC_vli_bytesToNative(uECC_word_t *native,
965                                          const uint8_t *bytes,
966                                          int num_bytes) {
967     wordcount_t i;
968     uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE);
969     for (i = 0; i < num_bytes; ++i) {
970         unsigned b = num_bytes - 1 - i;
971         native[b / uECC_WORD_SIZE] |=
972             (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
973     }
974 }
975 
976 #endif /* uECC_WORD_SIZE */
977 
978 /* Generates a random integer in the range 0 < random < top.
979    Both random and top have num_words words. */
uECC_generate_random_int(uECC_word_t * random,const uECC_word_t * top,wordcount_t num_words)980 uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random,
981                                           const uECC_word_t *top,
982                                           wordcount_t num_words) {
983     uECC_word_t mask = (uECC_word_t)-1;
984     uECC_word_t tries;
985     bitcount_t num_bits = uECC_vli_numBits(top, num_words);
986 
987     if (!g_rng_function) {
988         return 0;
989     }
990 
991     for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
992         if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
993             return 0;
994 	    }
995         random[num_words - 1] &= mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
996         if (!uECC_vli_isZero(random, num_words) &&
997 		        uECC_vli_cmp(top, random, num_words) == 1) {
998             return 1;
999         }
1000     }
1001     return 0;
1002 }
1003 
uECC_make_key(uint8_t * public_key,uint8_t * private_key,uECC_Curve curve)1004 int uECC_make_key(uint8_t *public_key,
1005                   uint8_t *private_key,
1006                   uECC_Curve curve) {
1007 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1008     uECC_word_t *_private = (uECC_word_t *)private_key;
1009     uECC_word_t *_public = (uECC_word_t *)public_key;
1010 #else
1011     uECC_word_t _private[uECC_MAX_WORDS];
1012     uECC_word_t _public[uECC_MAX_WORDS * 2];
1013 #endif
1014     uECC_word_t tries;
1015 
1016     for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1017         if (!uECC_generate_random_int(_private, curve->n, BITS_TO_WORDS(curve->num_n_bits))) {
1018             return 0;
1019         }
1020 
1021         if (EccPoint_compute_public_key(_public, _private, curve)) {
1022 #if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
1023             uECC_vli_nativeToBytes(private_key, BITS_TO_BYTES(curve->num_n_bits), _private);
1024             uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
1025             uECC_vli_nativeToBytes(
1026                 public_key + curve->num_bytes, curve->num_bytes, _public + curve->num_words);
1027 #endif
1028             return 1;
1029         }
1030     }
1031     return 0;
1032 }
1033 
uECC_shared_secret(const uint8_t * public_key,const uint8_t * private_key,uint8_t * secret,uECC_Curve curve)1034 int uECC_shared_secret(const uint8_t *public_key,
1035                        const uint8_t *private_key,
1036                        uint8_t *secret,
1037                        uECC_Curve curve) {
1038     uECC_word_t _public[uECC_MAX_WORDS * 2];
1039     uECC_word_t _private[uECC_MAX_WORDS];
1040 
1041     uECC_word_t tmp[uECC_MAX_WORDS];
1042     uECC_word_t *p2[2] = {_private, tmp};
1043     uECC_word_t *initial_Z = 0;
1044     uECC_word_t carry;
1045     wordcount_t num_words = curve->num_words;
1046     wordcount_t num_bytes = curve->num_bytes;
1047 
1048 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1049     bcopy((uint8_t *) _private, private_key, num_bytes);
1050     bcopy((uint8_t *) _public, public_key, num_bytes*2);
1051 #else
1052     uECC_vli_bytesToNative(_private, private_key, BITS_TO_BYTES(curve->num_n_bits));
1053     uECC_vli_bytesToNative(_public, public_key, num_bytes);
1054     uECC_vli_bytesToNative(_public + num_words, public_key + num_bytes, num_bytes);
1055 #endif
1056 
1057     /* Regularize the bitcount for the private key so that attackers cannot use a side channel
1058        attack to learn the number of leading zeros. */
1059     carry = regularize_k(_private, _private, tmp, curve);
1060 
1061     /* If an RNG function was specified, try to get a random initial Z value to improve
1062        protection against side-channel attacks. */
1063     if (g_rng_function) {
1064         if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
1065             return 0;
1066         }
1067         initial_Z = p2[carry];
1068     }
1069 
1070     EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1, curve);
1071 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1072     bcopy((uint8_t *) secret, (uint8_t *) _public, num_bytes);
1073 #else
1074     uECC_vli_nativeToBytes(secret, num_bytes, _public);
1075 #endif
1076     return !EccPoint_isZero(_public, curve);
1077 }
1078 
1079 #if uECC_SUPPORT_COMPRESSED_POINT
uECC_compress(const uint8_t * public_key,uint8_t * compressed,uECC_Curve curve)1080 void uECC_compress(const uint8_t *public_key, uint8_t *compressed, uECC_Curve curve) {
1081     wordcount_t i;
1082     for (i = 0; i < curve->num_bytes; ++i) {
1083         compressed[i+1] = public_key[i];
1084     }
1085 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1086     compressed[0] = 2 + (public_key[curve->num_bytes] & 0x01);
1087 #else
1088     compressed[0] = 2 + (public_key[curve->num_bytes * 2 - 1] & 0x01);
1089 #endif
1090 }
1091 
uECC_decompress(const uint8_t * compressed,uint8_t * public_key,uECC_Curve curve)1092 void uECC_decompress(const uint8_t *compressed, uint8_t *public_key, uECC_Curve curve) {
1093 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1094     uECC_word_t *point = (uECC_word_t *)public_key;
1095 #else
1096     uECC_word_t point[uECC_MAX_WORDS * 2];
1097 #endif
1098     uECC_word_t *y = point + curve->num_words;
1099 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1100     bcopy(public_key, compressed+1, curve->num_bytes);
1101 #else
1102     uECC_vli_bytesToNative(point, compressed + 1, curve->num_bytes);
1103 #endif
1104     curve->x_side(y, point, curve);
1105     curve->mod_sqrt(y, curve);
1106 
1107     if ((y[0] & 0x01) != (compressed[0] & 0x01)) {
1108         uECC_vli_sub(y, curve->p, y, curve->num_words);
1109     }
1110 
1111 #if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
1112     uECC_vli_nativeToBytes(public_key, curve->num_bytes, point);
1113     uECC_vli_nativeToBytes(public_key + curve->num_bytes, curve->num_bytes, y);
1114 #endif
1115 }
1116 #endif /* uECC_SUPPORT_COMPRESSED_POINT */
1117 
uECC_valid_point(const uECC_word_t * point,uECC_Curve curve)1118 int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve) {
1119     uECC_word_t tmp1[uECC_MAX_WORDS];
1120     uECC_word_t tmp2[uECC_MAX_WORDS];
1121     wordcount_t num_words = curve->num_words;
1122 
1123     /* The point at infinity is invalid. */
1124     if (EccPoint_isZero(point, curve)) {
1125         return 0;
1126     }
1127 
1128     /* x and y must be smaller than p. */
1129     if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
1130             uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
1131         return 0;
1132     }
1133 
1134     uECC_vli_modSquare_fast(tmp1, point + num_words, curve);
1135     curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
1136 
1137     /* Make sure that y^2 == x^3 + ax + b */
1138     return (int)(uECC_vli_equal(tmp1, tmp2, num_words));
1139 }
1140 
uECC_valid_public_key(const uint8_t * public_key,uECC_Curve curve)1141 int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve) {
1142 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1143     uECC_word_t *_public = (uECC_word_t *)public_key;
1144 #else
1145     uECC_word_t _public[uECC_MAX_WORDS * 2];
1146 #endif
1147 
1148 #if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
1149     uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
1150     uECC_vli_bytesToNative(
1151         _public + curve->num_words, public_key + curve->num_bytes, curve->num_bytes);
1152 #endif
1153     return uECC_valid_point(_public, curve);
1154 }
1155 
uECC_compute_public_key(const uint8_t * private_key,uint8_t * public_key,uECC_Curve curve)1156 int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uECC_Curve curve) {
1157 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1158     uECC_word_t *_private = (uECC_word_t *)private_key;
1159     uECC_word_t *_public = (uECC_word_t *)public_key;
1160 #else
1161     uECC_word_t _private[uECC_MAX_WORDS];
1162     uECC_word_t _public[uECC_MAX_WORDS * 2];
1163 #endif
1164 
1165 #if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
1166     uECC_vli_bytesToNative(_private, private_key, BITS_TO_BYTES(curve->num_n_bits));
1167 #endif
1168 
1169     /* Make sure the private key is in the range [1, n-1]. */
1170     if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) {
1171         return 0;
1172     }
1173 
1174     if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
1175         return 0;
1176     }
1177 
1178     /* Compute public key. */
1179     if (!EccPoint_compute_public_key(_public, _private, curve)) {
1180         return 0;
1181     }
1182 
1183 #if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
1184     uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
1185     uECC_vli_nativeToBytes(
1186         public_key + curve->num_bytes, curve->num_bytes, _public + curve->num_words);
1187 #endif
1188     return 1;
1189 }
1190 
1191 
1192 /* -------- ECDSA code -------- */
1193 
bits2int(uECC_word_t * native,const uint8_t * bits,unsigned bits_size,uECC_Curve curve)1194 static void bits2int(uECC_word_t *native,
1195                      const uint8_t *bits,
1196                      unsigned bits_size,
1197                      uECC_Curve curve) {
1198     unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
1199     unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
1200     int shift;
1201     uECC_word_t carry;
1202     uECC_word_t *ptr;
1203 
1204     if (bits_size > num_n_bytes) {
1205         bits_size = num_n_bytes;
1206     }
1207 
1208     uECC_vli_clear(native, num_n_words);
1209 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1210     bcopy((uint8_t *) native, bits, bits_size);
1211 #else
1212     uECC_vli_bytesToNative(native, bits, bits_size);
1213 #endif
1214     if (bits_size * 8 <= (unsigned)curve->num_n_bits) {
1215         return;
1216     }
1217     shift = bits_size * 8 - curve->num_n_bits;
1218     carry = 0;
1219     ptr = native + num_n_words;
1220     while (ptr-- > native) {
1221         uECC_word_t temp = *ptr;
1222         *ptr = (temp >> shift) | carry;
1223         carry = temp << (uECC_WORD_BITS - shift);
1224     }
1225 
1226     /* Reduce mod curve_n */
1227     if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
1228         uECC_vli_sub(native, native, curve->n, num_n_words);
1229     }
1230 }
1231 
uECC_sign_with_k(const uint8_t * private_key,const uint8_t * message_hash,unsigned hash_size,uECC_word_t * k,uint8_t * signature,uECC_Curve curve)1232 static int uECC_sign_with_k(const uint8_t *private_key,
1233                             const uint8_t *message_hash,
1234                             unsigned hash_size,
1235                             uECC_word_t *k,
1236                             uint8_t *signature,
1237                             uECC_Curve curve) {
1238 
1239     uECC_word_t tmp[uECC_MAX_WORDS];
1240     uECC_word_t s[uECC_MAX_WORDS];
1241     uECC_word_t *k2[2] = {tmp, s};
1242 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1243     uECC_word_t *p = (uECC_word_t *)signature;
1244 #else
1245     uECC_word_t p[uECC_MAX_WORDS * 2];
1246 #endif
1247     uECC_word_t carry;
1248     wordcount_t num_words = curve->num_words;
1249     wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
1250     bitcount_t num_n_bits = curve->num_n_bits;
1251 
1252     /* Make sure 0 < k < curve_n */
1253     if (uECC_vli_isZero(k, num_words) || uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
1254         return 0;
1255     }
1256 
1257     carry = regularize_k(k, tmp, s, curve);
1258     EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
1259     if (uECC_vli_isZero(p, num_words)) {
1260         return 0;
1261     }
1262 
1263     /* If an RNG function was specified, get a random number
1264        to prevent side channel analysis of k. */
1265     if (!g_rng_function) {
1266         uECC_vli_clear(tmp, num_n_words);
1267         tmp[0] = 1;
1268     } else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
1269         return 0;
1270     }
1271 
1272     /* Prevent side channel analysis of uECC_vli_modInv() to determine
1273        bits of k / the private key by premultiplying by a random number */
1274     uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
1275     uECC_vli_modInv(k, k, curve->n, num_n_words);       /* k = 1 / k' */
1276     uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
1277 
1278 #if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
1279     uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
1280 #endif
1281 
1282 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1283     bcopy((uint8_t *) tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
1284 #else
1285     uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits)); /* tmp = d */
1286 #endif
1287 
1288     s[num_n_words - 1] = 0;
1289     uECC_vli_set(s, p, num_words);
1290     uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
1291 
1292     bits2int(tmp, message_hash, hash_size, curve);
1293     uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
1294     uECC_vli_modMult(s, s, k, curve->n, num_n_words);  /* s = (e + r*d) / k */
1295     if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
1296         return 0;
1297     }
1298 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1299     bcopy((uint8_t *) signature + curve->num_bytes, (uint8_t *) s, curve->num_bytes);
1300 #else
1301     uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
1302 #endif
1303     return 1;
1304 }
1305 
uECC_sign(const uint8_t * private_key,const uint8_t * message_hash,unsigned hash_size,uint8_t * signature,uECC_Curve curve)1306 int uECC_sign(const uint8_t *private_key,
1307               const uint8_t *message_hash,
1308               unsigned hash_size,
1309               uint8_t *signature,
1310               uECC_Curve curve) {
1311     uECC_word_t k[uECC_MAX_WORDS];
1312     uECC_word_t tries;
1313 
1314     for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1315         if (!uECC_generate_random_int(k, curve->n, BITS_TO_WORDS(curve->num_n_bits))) {
1316             return 0;
1317         }
1318 
1319         if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature, curve)) {
1320             return 1;
1321         }
1322     }
1323     return 0;
1324 }
1325 
1326 /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
1327    the same size as the hash result size. */
HMAC_init(const uECC_HashContext * hash_context,const uint8_t * K)1328 static void HMAC_init(const uECC_HashContext *hash_context, const uint8_t *K) {
1329     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
1330     unsigned i;
1331     for (i = 0; i < hash_context->result_size; ++i)
1332         pad[i] = K[i] ^ 0x36;
1333     for (; i < hash_context->block_size; ++i)
1334         pad[i] = 0x36;
1335 
1336     hash_context->init_hash(hash_context);
1337     hash_context->update_hash(hash_context, pad, hash_context->block_size);
1338 }
1339 
HMAC_update(const uECC_HashContext * hash_context,const uint8_t * message,unsigned message_size)1340 static void HMAC_update(const uECC_HashContext *hash_context,
1341                         const uint8_t *message,
1342                         unsigned message_size) {
1343     hash_context->update_hash(hash_context, message, message_size);
1344 }
1345 
HMAC_finish(const uECC_HashContext * hash_context,const uint8_t * K,uint8_t * result)1346 static void HMAC_finish(const uECC_HashContext *hash_context,
1347                         const uint8_t *K,
1348                         uint8_t *result) {
1349     uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size;
1350     unsigned i;
1351     for (i = 0; i < hash_context->result_size; ++i)
1352         pad[i] = K[i] ^ 0x5c;
1353     for (; i < hash_context->block_size; ++i)
1354         pad[i] = 0x5c;
1355 
1356     hash_context->finish_hash(hash_context, result);
1357 
1358     hash_context->init_hash(hash_context);
1359     hash_context->update_hash(hash_context, pad, hash_context->block_size);
1360     hash_context->update_hash(hash_context, result, hash_context->result_size);
1361     hash_context->finish_hash(hash_context, result);
1362 }
1363 
1364 /* V = HMAC_K(V) */
update_V(const uECC_HashContext * hash_context,uint8_t * K,uint8_t * V)1365 static void update_V(const uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) {
1366     HMAC_init(hash_context, K);
1367     HMAC_update(hash_context, V, hash_context->result_size);
1368     HMAC_finish(hash_context, K, V);
1369 }
1370 
1371 /* Deterministic signing, similar to RFC 6979. Differences are:
1372     * We just use H(m) directly rather than bits2octets(H(m))
1373       (it is not reduced modulo curve_n).
1374     * We generate a value for k (aka T) directly rather than converting endianness.
1375 
1376    Layout of hash_context->tmp: <K> | <V> | (1 byte overlapped 0x00 or 0x01) / <HMAC pad> */
uECC_sign_deterministic(const uint8_t * private_key,const uint8_t * message_hash,unsigned hash_size,const uECC_HashContext * hash_context,uint8_t * signature,uECC_Curve curve)1377 int uECC_sign_deterministic(const uint8_t *private_key,
1378                             const uint8_t *message_hash,
1379                             unsigned hash_size,
1380                             const uECC_HashContext *hash_context,
1381                             uint8_t *signature,
1382                             uECC_Curve curve) {
1383     uint8_t *K = hash_context->tmp;
1384     uint8_t *V = K + hash_context->result_size;
1385     wordcount_t num_bytes = curve->num_bytes;
1386     wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
1387     bitcount_t num_n_bits = curve->num_n_bits;
1388     uECC_word_t tries;
1389     unsigned i;
1390     for (i = 0; i < hash_context->result_size; ++i) {
1391         V[i] = 0x01;
1392         K[i] = 0;
1393     }
1394 
1395     /* K = HMAC_K(V || 0x00 || int2octets(x) || h(m)) */
1396     HMAC_init(hash_context, K);
1397     V[hash_context->result_size] = 0x00;
1398     HMAC_update(hash_context, V, hash_context->result_size + 1);
1399     HMAC_update(hash_context, private_key, num_bytes);
1400     HMAC_update(hash_context, message_hash, hash_size);
1401     HMAC_finish(hash_context, K, K);
1402 
1403     update_V(hash_context, K, V);
1404 
1405     /* K = HMAC_K(V || 0x01 || int2octets(x) || h(m)) */
1406     HMAC_init(hash_context, K);
1407     V[hash_context->result_size] = 0x01;
1408     HMAC_update(hash_context, V, hash_context->result_size + 1);
1409     HMAC_update(hash_context, private_key, num_bytes);
1410     HMAC_update(hash_context, message_hash, hash_size);
1411     HMAC_finish(hash_context, K, K);
1412 
1413     update_V(hash_context, K, V);
1414 
1415     for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1416         uECC_word_t T[uECC_MAX_WORDS];
1417         uint8_t *T_ptr = (uint8_t *)T;
1418         wordcount_t T_bytes = 0;
1419         for (;;) {
1420             update_V(hash_context, K, V);
1421             for (i = 0; i < hash_context->result_size; ++i) {
1422                 T_ptr[T_bytes++] = V[i];
1423                 if (T_bytes >= num_n_words * uECC_WORD_SIZE) {
1424                     goto filled;
1425                 }
1426             }
1427         }
1428     filled:
1429         if ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8 > num_n_bits) {
1430             uECC_word_t mask = (uECC_word_t)-1;
1431             T[num_n_words - 1] &=
1432                 mask >> ((bitcount_t)(num_n_words * uECC_WORD_SIZE * 8 - num_n_bits));
1433         }
1434 
1435         if (uECC_sign_with_k(private_key, message_hash, hash_size, T, signature, curve)) {
1436             return 1;
1437         }
1438 
1439         /* K = HMAC_K(V || 0x00) */
1440         HMAC_init(hash_context, K);
1441         V[hash_context->result_size] = 0x00;
1442         HMAC_update(hash_context, V, hash_context->result_size + 1);
1443         HMAC_finish(hash_context, K, K);
1444 
1445         update_V(hash_context, K, V);
1446     }
1447     return 0;
1448 }
1449 
smax(bitcount_t a,bitcount_t b)1450 static bitcount_t smax(bitcount_t a, bitcount_t b) {
1451     return (a > b ? a : b);
1452 }
1453 
uECC_verify(const uint8_t * public_key,const uint8_t * message_hash,unsigned hash_size,const uint8_t * signature,uECC_Curve curve)1454 int uECC_verify(const uint8_t *public_key,
1455                 const uint8_t *message_hash,
1456                 unsigned hash_size,
1457                 const uint8_t *signature,
1458                 uECC_Curve curve) {
1459     uECC_word_t u1[uECC_MAX_WORDS], u2[uECC_MAX_WORDS];
1460     uECC_word_t z[uECC_MAX_WORDS];
1461     uECC_word_t sum[uECC_MAX_WORDS * 2];
1462     uECC_word_t rx[uECC_MAX_WORDS];
1463     uECC_word_t ry[uECC_MAX_WORDS];
1464     uECC_word_t tx[uECC_MAX_WORDS];
1465     uECC_word_t ty[uECC_MAX_WORDS];
1466     uECC_word_t tz[uECC_MAX_WORDS];
1467     const uECC_word_t *points[4];
1468     const uECC_word_t *point;
1469     bitcount_t num_bits;
1470     bitcount_t i;
1471 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1472     uECC_word_t *_public = (uECC_word_t *)public_key;
1473 #else
1474     uECC_word_t _public[uECC_MAX_WORDS * 2];
1475 #endif
1476     uECC_word_t r[uECC_MAX_WORDS], s[uECC_MAX_WORDS];
1477     wordcount_t num_words = curve->num_words;
1478     wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
1479 
1480     rx[num_n_words - 1] = 0;
1481     r[num_n_words - 1] = 0;
1482     s[num_n_words - 1] = 0;
1483 
1484 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
1485     bcopy((uint8_t *) r, signature, curve->num_bytes);
1486     bcopy((uint8_t *) s, signature + curve->num_bytes, curve->num_bytes);
1487 #else
1488     uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
1489     uECC_vli_bytesToNative(
1490         _public + num_words, public_key + curve->num_bytes, curve->num_bytes);
1491     uECC_vli_bytesToNative(r, signature, curve->num_bytes);
1492     uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
1493 #endif
1494 
1495     /* r, s must not be 0. */
1496     if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
1497         return 0;
1498     }
1499 
1500     /* r, s must be < n. */
1501     if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
1502             uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
1503         return 0;
1504     }
1505 
1506     /* Calculate u1 and u2. */
1507     uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
1508     u1[num_n_words - 1] = 0;
1509     bits2int(u1, message_hash, hash_size, curve);
1510     uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
1511     uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
1512 
1513     /* Calculate sum = G + Q. */
1514     uECC_vli_set(sum, _public, num_words);
1515     uECC_vli_set(sum + num_words, _public + num_words, num_words);
1516     uECC_vli_set(tx, curve->G, num_words);
1517     uECC_vli_set(ty, curve->G + num_words, num_words);
1518     uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
1519     XYcZ_add(tx, ty, sum, sum + num_words, curve);
1520     uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
1521     apply_z(sum, sum + num_words, z, curve);
1522 
1523     /* Use Shamir's trick to calculate u1*G + u2*Q */
1524     points[0] = 0;
1525     points[1] = curve->G;
1526     points[2] = _public;
1527     points[3] = sum;
1528     num_bits = smax(uECC_vli_numBits(u1, num_n_words),
1529                     uECC_vli_numBits(u2, num_n_words));
1530 
1531     point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
1532                    ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
1533     uECC_vli_set(rx, point, num_words);
1534     uECC_vli_set(ry, point + num_words, num_words);
1535     uECC_vli_clear(z, num_words);
1536     z[0] = 1;
1537 
1538     for (i = num_bits - 2; i >= 0; --i) {
1539         uECC_word_t index;
1540         curve->double_jacobian(rx, ry, z, curve);
1541 
1542         index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
1543         point = points[index];
1544         if (point) {
1545             uECC_vli_set(tx, point, num_words);
1546             uECC_vli_set(ty, point + num_words, num_words);
1547             apply_z(tx, ty, z, curve);
1548             uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
1549             XYcZ_add(tx, ty, rx, ry, curve);
1550             uECC_vli_modMult_fast(z, z, tz, curve);
1551         }
1552     }
1553 
1554     uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
1555     apply_z(rx, ry, z, curve);
1556 
1557     /* v = x1 (mod n) */
1558     if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
1559         uECC_vli_sub(rx, rx, curve->n, num_n_words);
1560     }
1561 
1562     /* Accept only if v == r. */
1563     return (int)(uECC_vli_equal(rx, r, num_words));
1564 }
1565 
1566 #if uECC_ENABLE_VLI_API
1567 
uECC_curve_num_words(uECC_Curve curve)1568 unsigned uECC_curve_num_words(uECC_Curve curve) {
1569     return curve->num_words;
1570 }
1571 
uECC_curve_num_bytes(uECC_Curve curve)1572 unsigned uECC_curve_num_bytes(uECC_Curve curve) {
1573     return curve->num_bytes;
1574 }
1575 
uECC_curve_num_bits(uECC_Curve curve)1576 unsigned uECC_curve_num_bits(uECC_Curve curve) {
1577     return curve->num_bytes * 8;
1578 }
1579 
uECC_curve_num_n_words(uECC_Curve curve)1580 unsigned uECC_curve_num_n_words(uECC_Curve curve) {
1581     return BITS_TO_WORDS(curve->num_n_bits);
1582 }
1583 
uECC_curve_num_n_bytes(uECC_Curve curve)1584 unsigned uECC_curve_num_n_bytes(uECC_Curve curve) {
1585     return BITS_TO_BYTES(curve->num_n_bits);
1586 }
1587 
uECC_curve_num_n_bits(uECC_Curve curve)1588 unsigned uECC_curve_num_n_bits(uECC_Curve curve) {
1589     return curve->num_n_bits;
1590 }
1591 
uECC_curve_p(uECC_Curve curve)1592 const uECC_word_t *uECC_curve_p(uECC_Curve curve) {
1593     return curve->p;
1594 }
1595 
uECC_curve_n(uECC_Curve curve)1596 const uECC_word_t *uECC_curve_n(uECC_Curve curve) {
1597     return curve->n;
1598 }
1599 
uECC_curve_G(uECC_Curve curve)1600 const uECC_word_t *uECC_curve_G(uECC_Curve curve) {
1601     return curve->G;
1602 }
1603 
uECC_curve_b(uECC_Curve curve)1604 const uECC_word_t *uECC_curve_b(uECC_Curve curve) {
1605     return curve->b;
1606 }
1607 
1608 #if uECC_SUPPORT_COMPRESSED_POINT
uECC_vli_mod_sqrt(uECC_word_t * a,uECC_Curve curve)1609 void uECC_vli_mod_sqrt(uECC_word_t *a, uECC_Curve curve) {
1610     curve->mod_sqrt(a, curve);
1611 }
1612 #endif
1613 
uECC_vli_mmod_fast(uECC_word_t * result,uECC_word_t * product,uECC_Curve curve)1614 void uECC_vli_mmod_fast(uECC_word_t *result, uECC_word_t *product, uECC_Curve curve) {
1615 #if (uECC_OPTIMIZATION_LEVEL > 0)
1616     curve->mmod_fast(result, product);
1617 #else
1618     uECC_vli_mmod(result, product, curve->p, curve->num_words);
1619 #endif
1620 }
1621 
uECC_point_mult(uECC_word_t * result,const uECC_word_t * point,const uECC_word_t * scalar,uECC_Curve curve)1622 void uECC_point_mult(uECC_word_t *result,
1623                      const uECC_word_t *point,
1624                      const uECC_word_t *scalar,
1625                      uECC_Curve curve) {
1626     uECC_word_t tmp1[uECC_MAX_WORDS];
1627     uECC_word_t tmp2[uECC_MAX_WORDS];
1628     uECC_word_t *p2[2] = {tmp1, tmp2};
1629     uECC_word_t carry = regularize_k(scalar, tmp1, tmp2, curve);
1630 
1631     EccPoint_mult(result, point, p2[!carry], 0, curve->num_n_bits + 1, curve);
1632 }
1633 
1634 #endif /* uECC_ENABLE_VLI_API */
1635