81#define uECC_WORD_SIZE 4
84#ifndef uECC_RNG_MAX_TRIES
85#define uECC_RNG_MAX_TRIES 64
89typedef int8_t wordcount_t;
90typedef int16_t bitcount_t;
92typedef int8_t cmpresult_t;
94typedef unsigned int uECC_word_t;
96typedef uint64_t uECC_dword_t;
99#define HIGH_BIT_SET 0x80000000
100#define uECC_WORD_BITS 32
101#define uECC_WORD_BITS_SHIFT 5
102#define uECC_WORD_BITS_MASK 0x01F
105#define NUM_ECC_WORDS 8
107#define NUM_ECC_BYTES (uECC_WORD_SIZE*NUM_ECC_WORDS)
111typedef const struct uECC_Curve_t * uECC_Curve;
113 wordcount_t num_words;
114 wordcount_t num_bytes;
115 bitcount_t num_n_bits;
116 uECC_word_t p[NUM_ECC_WORDS];
117 uECC_word_t n[NUM_ECC_WORDS];
118 uECC_word_t G[NUM_ECC_WORDS * 2];
119 uECC_word_t b[NUM_ECC_WORDS];
120 void (*double_jacobian)(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * Z1,
122 void (*x_side)(uECC_word_t *result,
const uECC_word_t *x, uECC_Curve curve);
123 void (*mmod_fast)(uECC_word_t *result, uECC_word_t *product);
133void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
134 uECC_word_t * Z1, uECC_Curve curve);
142void x_side_default(uECC_word_t *result,
const uECC_word_t *x,
151void vli_mmod_fast_secp256r1(
unsigned int *result,
unsigned int *product);
154#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e
155#define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a
156#define BITS_TO_WORDS(num_bits) \
157 ((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8))
158#define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
161static const struct uECC_Curve_t curve_secp256r1 = {
165 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
166 BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00),
167 BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
168 BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
170 BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
171 BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
172 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
173 BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
175 BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
176 BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
177 BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),
178 BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B),
180 BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB),
181 BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B),
182 BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E),
183 BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F)
185 BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B),
186 BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65),
187 BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3),
188 BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A)
190 &double_jacobian_default,
192 &vli_mmod_fast_secp256r1
195uECC_Curve uECC_secp256r1(
void);
205int uECC_generate_random_int(uECC_word_t *random,
const uECC_word_t *top,
206 wordcount_t num_words);
225typedef int(*uECC_RNG_Function)(uint8_t *dest,
unsigned int size);
237void uECC_set_rng(uECC_RNG_Function rng_function);
243uECC_RNG_Function uECC_get_rng(
void);
250int uECC_curve_private_key_size(uECC_Curve curve);
257int uECC_curve_public_key_size(uECC_Curve curve);
266int uECC_compute_public_key(
const uint8_t *private_key,
267 uint8_t *public_key, uECC_Curve curve);
276uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
277 uECC_word_t *private_key, uECC_Curve curve);
288uECC_word_t regularize_k(
const uECC_word_t *
const k, uECC_word_t *k0,
289 uECC_word_t *k1, uECC_Curve curve);
302void EccPoint_mult(uECC_word_t * result,
const uECC_word_t * point,
303 const uECC_word_t * scalar,
const uECC_word_t * initial_Z,
304 bitcount_t num_bits, uECC_Curve curve);
312uECC_word_t uECC_vli_isZero(
const uECC_word_t *vli, wordcount_t num_words);
320uECC_word_t EccPoint_isZero(
const uECC_word_t *point, uECC_Curve curve);
329cmpresult_t uECC_vli_cmp(
const uECC_word_t *left,
const uECC_word_t *right,
330 wordcount_t num_words);
340cmpresult_t uECC_vli_cmp_unsafe(
const uECC_word_t *left,
const uECC_word_t *right,
341 wordcount_t num_words);
353void uECC_vli_modSub(uECC_word_t *result,
const uECC_word_t *left,
354 const uECC_word_t *right,
const uECC_word_t *mod,
355 wordcount_t num_words);
367void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * X2,
368 uECC_word_t * Y2, uECC_Curve curve);
377void apply_z(uECC_word_t * X1, uECC_word_t * Y1,
const uECC_word_t *
const Z,
389uECC_word_t uECC_vli_testBit(
const uECC_word_t *vli, bitcount_t bit);
398void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
399 const uECC_word_t *mod, wordcount_t num_words);
408void uECC_vli_modMult_fast(uECC_word_t *result,
const uECC_word_t *left,
409 const uECC_word_t *right, uECC_Curve curve);
420uECC_word_t uECC_vli_sub(uECC_word_t *result,
const uECC_word_t *left,
421 const uECC_word_t *right, wordcount_t num_words);
430uECC_word_t uECC_vli_equal(
const uECC_word_t *left,
const uECC_word_t *right,
431 wordcount_t num_words);
441void uECC_vli_modMult(uECC_word_t *result,
const uECC_word_t *left,
442 const uECC_word_t *right,
const uECC_word_t *mod,
443 wordcount_t num_words);
454void uECC_vli_modInv(uECC_word_t *result,
const uECC_word_t *input,
455 const uECC_word_t *mod, wordcount_t num_words);
463void uECC_vli_set(uECC_word_t *dest,
const uECC_word_t *src,
464 wordcount_t num_words);
476void uECC_vli_modAdd(uECC_word_t *result,
const uECC_word_t *left,
477 const uECC_word_t *right,
const uECC_word_t *mod,
478 wordcount_t num_words);
486bitcount_t uECC_vli_numBits(
const uECC_word_t *vli,
487 const wordcount_t max_words);
494void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words);
505int uECC_valid_point(
const uECC_word_t *point, uECC_Curve curve);
521int uECC_valid_public_key(
const uint8_t *public_key, uECC_Curve curve);
529void uECC_vli_nativeToBytes(uint8_t *bytes,
int num_bytes,
530 const unsigned int *native);
538void uECC_vli_bytesToNative(
unsigned int *native,
const uint8_t *bytes,