| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
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
|
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
void uECC_set_rng(uECC_RNG_Function rng_function) { |
|
191
|
0
|
|
|
|
|
|
g_rng_function = rng_function; |
|
192
|
0
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
uECC_RNG_Function uECC_get_rng(void) { |
|
195
|
0
|
|
|
|
|
|
return g_rng_function; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
int uECC_curve_private_key_size(uECC_Curve curve) { |
|
199
|
0
|
|
|
|
|
|
return BITS_TO_BYTES(curve->num_n_bits); |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
int uECC_curve_public_key_size(uECC_Curve curve) { |
|
203
|
0
|
|
|
|
|
|
return 2 * curve->num_bytes; |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
#if !asm_clear |
|
207
|
0
|
|
|
|
|
|
uECC_VLI_API void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words) { |
|
208
|
|
|
|
|
|
|
wordcount_t i; |
|
209
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_words; ++i) { |
|
210
|
0
|
|
|
|
|
|
vli[i] = 0; |
|
211
|
|
|
|
|
|
|
} |
|
212
|
0
|
|
|
|
|
|
} |
|
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. */ |
|
217
|
0
|
|
|
|
|
|
uECC_VLI_API uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words) { |
|
218
|
0
|
|
|
|
|
|
uECC_word_t bits = 0; |
|
219
|
|
|
|
|
|
|
wordcount_t i; |
|
220
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_words; ++i) { |
|
221
|
0
|
|
|
|
|
|
bits |= vli[i]; |
|
222
|
|
|
|
|
|
|
} |
|
223
|
0
|
|
|
|
|
|
return (bits == 0); |
|
224
|
|
|
|
|
|
|
} |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
/* Returns nonzero if bit 'bit' of vli is set. */ |
|
227
|
0
|
|
|
|
|
|
uECC_VLI_API uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit) { |
|
228
|
0
|
|
|
|
|
|
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. */ |
|
232
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) { |
|
|
|
0
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
} |
|
238
|
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
return (i + 1); |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
/* Counts the number of bits required to represent vli. */ |
|
243
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_digits = vli_numDigits(vli, max_words); |
|
248
|
0
|
0
|
|
|
|
|
if (num_digits == 0) { |
|
249
|
0
|
|
|
|
|
|
return 0; |
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
digit = vli[num_digits - 1]; |
|
253
|
0
|
0
|
|
|
|
|
for (i = 0; digit; ++i) { |
|
254
|
0
|
|
|
|
|
|
digit >>= 1; |
|
255
|
|
|
|
|
|
|
} |
|
256
|
|
|
|
|
|
|
|
|
257
|
0
|
|
|
|
|
|
return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); |
|
258
|
|
|
|
|
|
|
} |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
/* Sets dest = src. */ |
|
261
|
|
|
|
|
|
|
#if !asm_set |
|
262
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_words; ++i) { |
|
265
|
0
|
|
|
|
|
|
dest[i] = src[i]; |
|
266
|
|
|
|
|
|
|
} |
|
267
|
0
|
|
|
|
|
|
} |
|
268
|
|
|
|
|
|
|
#endif /* !asm_set */ |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
/* Returns sign of left - right. */ |
|
271
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
for (i = num_words - 1; i >= 0; --i) { |
|
276
|
0
|
0
|
|
|
|
|
if (left[i] > right[i]) { |
|
277
|
0
|
|
|
|
|
|
return 1; |
|
278
|
0
|
0
|
|
|
|
|
} else if (left[i] < right[i]) { |
|
279
|
0
|
|
|
|
|
|
return -1; |
|
280
|
|
|
|
|
|
|
} |
|
281
|
|
|
|
|
|
|
} |
|
282
|
0
|
|
|
|
|
|
return 0; |
|
283
|
|
|
|
|
|
|
} |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
/* Constant-time comparison function - secure way to compare long integers */ |
|
286
|
|
|
|
|
|
|
/* Returns one if left == right, zero otherwise. */ |
|
287
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t diff = 0; |
|
291
|
|
|
|
|
|
|
wordcount_t i; |
|
292
|
0
|
0
|
|
|
|
|
for (i = num_words - 1; i >= 0; --i) { |
|
293
|
0
|
|
|
|
|
|
diff |= (left[i] ^ right[i]); |
|
294
|
|
|
|
|
|
|
} |
|
295
|
0
|
|
|
|
|
|
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. */ |
|
304
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words); |
|
309
|
0
|
|
|
|
|
|
uECC_word_t equal = uECC_vli_isZero(tmp, num_words); |
|
310
|
0
|
|
|
|
|
|
return (!equal - 2 * neg); |
|
311
|
|
|
|
|
|
|
} |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
/* Computes vli = vli >> 1. */ |
|
314
|
|
|
|
|
|
|
#if !asm_rshift1 |
|
315
|
0
|
|
|
|
|
|
uECC_VLI_API void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) { |
|
316
|
0
|
|
|
|
|
|
uECC_word_t *end = vli; |
|
317
|
0
|
|
|
|
|
|
uECC_word_t carry = 0; |
|
318
|
|
|
|
|
|
|
|
|
319
|
0
|
|
|
|
|
|
vli += num_words; |
|
320
|
0
|
0
|
|
|
|
|
while (vli-- > end) { |
|
321
|
0
|
|
|
|
|
|
uECC_word_t temp = *vli; |
|
322
|
0
|
|
|
|
|
|
*vli = (temp >> 1) | carry; |
|
323
|
0
|
|
|
|
|
|
carry = temp << (uECC_WORD_BITS - 1); |
|
324
|
|
|
|
|
|
|
} |
|
325
|
0
|
|
|
|
|
|
} |
|
326
|
|
|
|
|
|
|
#endif /* !asm_rshift1 */ |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
/* Computes result = left + right, returning carry. Can modify in place. */ |
|
329
|
|
|
|
|
|
|
#if !asm_add |
|
330
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t carry = 0; |
|
335
|
|
|
|
|
|
|
wordcount_t i; |
|
336
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_words; ++i) { |
|
337
|
0
|
|
|
|
|
|
uECC_word_t sum = left[i] + right[i] + carry; |
|
338
|
0
|
0
|
|
|
|
|
if (sum != left[i]) { |
|
339
|
0
|
|
|
|
|
|
carry = (sum < left[i]); |
|
340
|
|
|
|
|
|
|
} |
|
341
|
0
|
|
|
|
|
|
result[i] = sum; |
|
342
|
|
|
|
|
|
|
} |
|
343
|
0
|
|
|
|
|
|
return carry; |
|
344
|
|
|
|
|
|
|
} |
|
345
|
|
|
|
|
|
|
#endif /* !asm_add */ |
|
346
|
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
/* Computes result = left - right, returning borrow. Can modify in place. */ |
|
348
|
|
|
|
|
|
|
#if !asm_sub |
|
349
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t borrow = 0; |
|
354
|
|
|
|
|
|
|
wordcount_t i; |
|
355
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_words; ++i) { |
|
356
|
0
|
|
|
|
|
|
uECC_word_t diff = left[i] - right[i] - borrow; |
|
357
|
0
|
0
|
|
|
|
|
if (diff != left[i]) { |
|
358
|
0
|
|
|
|
|
|
borrow = (diff > left[i]); |
|
359
|
|
|
|
|
|
|
} |
|
360
|
0
|
|
|
|
|
|
result[i] = diff; |
|
361
|
|
|
|
|
|
|
} |
|
362
|
0
|
|
|
|
|
|
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))) |
|
369
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_dword_t p = (uECC_dword_t)a * b; |
|
401
|
0
|
|
|
|
|
|
uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; |
|
402
|
0
|
|
|
|
|
|
r01 += p; |
|
403
|
0
|
|
|
|
|
|
*r2 += (r01 < p); |
|
404
|
0
|
|
|
|
|
|
*r1 = r01 >> uECC_WORD_BITS; |
|
405
|
0
|
|
|
|
|
|
*r0 = (uECC_word_t)r01; |
|
406
|
|
|
|
|
|
|
#endif |
|
407
|
0
|
|
|
|
|
|
} |
|
408
|
|
|
|
|
|
|
#endif /* muladd needed */ |
|
409
|
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
#if !asm_mult |
|
411
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t r0 = 0; |
|
416
|
0
|
|
|
|
|
|
uECC_word_t r1 = 0; |
|
417
|
0
|
|
|
|
|
|
uECC_word_t r2 = 0; |
|
418
|
|
|
|
|
|
|
wordcount_t i, k; |
|
419
|
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
/* Compute each digit of result in sequence, maintaining the carries. */ |
|
421
|
0
|
0
|
|
|
|
|
for (k = 0; k < num_words; ++k) { |
|
422
|
0
|
0
|
|
|
|
|
for (i = 0; i <= k; ++i) { |
|
423
|
0
|
|
|
|
|
|
muladd(left[i], right[k - i], &r0, &r1, &r2); |
|
424
|
|
|
|
|
|
|
} |
|
425
|
0
|
|
|
|
|
|
result[k] = r0; |
|
426
|
0
|
|
|
|
|
|
r0 = r1; |
|
427
|
0
|
|
|
|
|
|
r1 = r2; |
|
428
|
0
|
|
|
|
|
|
r2 = 0; |
|
429
|
|
|
|
|
|
|
} |
|
430
|
0
|
0
|
|
|
|
|
for (k = num_words; k < num_words * 2 - 1; ++k) { |
|
431
|
0
|
0
|
|
|
|
|
for (i = (k + 1) - num_words; i < num_words; ++i) { |
|
432
|
0
|
|
|
|
|
|
muladd(left[i], right[k - i], &r0, &r1, &r2); |
|
433
|
|
|
|
|
|
|
} |
|
434
|
0
|
|
|
|
|
|
result[k] = r0; |
|
435
|
0
|
|
|
|
|
|
r0 = r1; |
|
436
|
0
|
|
|
|
|
|
r1 = r2; |
|
437
|
0
|
|
|
|
|
|
r2 = 0; |
|
438
|
|
|
|
|
|
|
} |
|
439
|
0
|
|
|
|
|
|
result[num_words * 2 - 1] = r0; |
|
440
|
0
|
|
|
|
|
|
} |
|
441
|
|
|
|
|
|
|
#endif /* !asm_mult */ |
|
442
|
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
#if uECC_SQUARE_FUNC |
|
444
|
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
#if !asm_square |
|
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
|
|
|
|
|
|
|
|
|
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 |
|
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. */ |
|
535
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t carry = uECC_vli_add(result, left, right, num_words); |
|
541
|
0
|
0
|
|
|
|
|
if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) { |
|
|
|
0
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
/* result > mod (result = mod + remainder), so subtract mod to get remainder. */ |
|
543
|
0
|
|
|
|
|
|
uECC_vli_sub(result, result, mod, num_words); |
|
544
|
|
|
|
|
|
|
} |
|
545
|
0
|
|
|
|
|
|
} |
|
546
|
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
/* Computes result = (left - right) % mod. |
|
548
|
|
|
|
|
|
|
Assumes that left < mod and right < mod, and that result does not overlap mod. */ |
|
549
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words); |
|
555
|
0
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_add(result, result, mod, num_words); |
|
559
|
|
|
|
|
|
|
} |
|
560
|
0
|
|
|
|
|
|
} |
|
561
|
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
/* Computes result = product % mod, where product is 2N words long. */ |
|
563
|
|
|
|
|
|
|
/* Currently only designed to work for curve_p or curve_n. */ |
|
564
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) - uECC_vli_numBits(mod, num_words); |
|
575
|
0
|
|
|
|
|
|
wordcount_t word_shift = shift / uECC_WORD_BITS; |
|
576
|
0
|
|
|
|
|
|
wordcount_t bit_shift = shift % uECC_WORD_BITS; |
|
577
|
0
|
|
|
|
|
|
uECC_word_t carry = 0; |
|
578
|
0
|
|
|
|
|
|
uECC_vli_clear(mod_multiple, word_shift); |
|
579
|
0
|
0
|
|
|
|
|
if (bit_shift > 0) { |
|
580
|
0
|
0
|
|
|
|
|
for(index = 0; index < (uECC_word_t)num_words; ++index) { |
|
581
|
0
|
|
|
|
|
|
mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry; |
|
582
|
0
|
|
|
|
|
|
carry = mod[index] >> (uECC_WORD_BITS - bit_shift); |
|
583
|
|
|
|
|
|
|
} |
|
584
|
|
|
|
|
|
|
} else { |
|
585
|
0
|
|
|
|
|
|
uECC_vli_set(mod_multiple + word_shift, mod, num_words); |
|
586
|
|
|
|
|
|
|
} |
|
587
|
|
|
|
|
|
|
|
|
588
|
0
|
0
|
|
|
|
|
for (index = 1; shift >= 0; --shift) { |
|
589
|
0
|
|
|
|
|
|
uECC_word_t borrow = 0; |
|
590
|
|
|
|
|
|
|
wordcount_t i; |
|
591
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_words * 2; ++i) { |
|
592
|
0
|
|
|
|
|
|
uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow; |
|
593
|
0
|
0
|
|
|
|
|
if (diff != v[index][i]) { |
|
594
|
0
|
|
|
|
|
|
borrow = (diff > v[index][i]); |
|
595
|
|
|
|
|
|
|
} |
|
596
|
0
|
|
|
|
|
|
v[1 - index][i] = diff; |
|
597
|
|
|
|
|
|
|
} |
|
598
|
0
|
|
|
|
|
|
index = !(index ^ borrow); /* Swap the index if there was no borrow */ |
|
599
|
0
|
|
|
|
|
|
uECC_vli_rshift1(mod_multiple, num_words); |
|
600
|
0
|
|
|
|
|
|
mod_multiple[num_words - 1] |= mod_multiple[num_words] << (uECC_WORD_BITS - 1); |
|
601
|
0
|
|
|
|
|
|
uECC_vli_rshift1(mod_multiple + num_words, num_words); |
|
602
|
|
|
|
|
|
|
} |
|
603
|
0
|
|
|
|
|
|
uECC_vli_set(result, v[index], num_words); |
|
604
|
0
|
|
|
|
|
|
} |
|
605
|
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
/* Computes result = (left * right) % mod. */ |
|
607
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_mult(product, left, right, num_words); |
|
614
|
0
|
|
|
|
|
|
uECC_vli_mmod(result, product, mod, num_words); |
|
615
|
0
|
|
|
|
|
|
} |
|
616
|
|
|
|
|
|
|
|
|
617
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_mult(product, left, right, curve->num_words); |
|
623
|
|
|
|
|
|
|
#if (uECC_OPTIMIZATION_LEVEL > 0) |
|
624
|
0
|
|
|
|
|
|
curve->mmod_fast(result, product); |
|
625
|
|
|
|
|
|
|
#else |
|
626
|
|
|
|
|
|
|
uECC_vli_mmod(result, product, curve->p, curve->num_words); |
|
627
|
|
|
|
|
|
|
#endif |
|
628
|
0
|
|
|
|
|
|
} |
|
629
|
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
#if uECC_SQUARE_FUNC |
|
631
|
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
#if uECC_ENABLE_VLI_API |
|
633
|
|
|
|
|
|
|
/* Computes result = left^2 % mod. */ |
|
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
|
|
|
|
|
|
|
|
|
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 |
|
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
|
|
|
|
|
|
|
|
|
667
|
0
|
|
|
|
|
|
uECC_VLI_API void uECC_vli_modSquare_fast(uECC_word_t *result, |
|
668
|
|
|
|
|
|
|
const uECC_word_t *left, |
|
669
|
|
|
|
|
|
|
uECC_Curve curve) { |
|
670
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(result, left, left, curve); |
|
671
|
0
|
|
|
|
|
|
} |
|
672
|
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
#endif /* uECC_SQUARE_FUNC */ |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
#define EVEN(vli) (!(vli[0] & 1)) |
|
676
|
0
|
|
|
|
|
|
static void vli_modInv_update(uECC_word_t *uv, |
|
677
|
|
|
|
|
|
|
const uECC_word_t *mod, |
|
678
|
|
|
|
|
|
|
wordcount_t num_words) { |
|
679
|
0
|
|
|
|
|
|
uECC_word_t carry = 0; |
|
680
|
0
|
0
|
|
|
|
|
if (!EVEN(uv)) { |
|
681
|
0
|
|
|
|
|
|
carry = uECC_vli_add(uv, uv, mod, num_words); |
|
682
|
|
|
|
|
|
|
} |
|
683
|
0
|
|
|
|
|
|
uECC_vli_rshift1(uv, num_words); |
|
684
|
0
|
0
|
|
|
|
|
if (carry) { |
|
685
|
0
|
|
|
|
|
|
uv[num_words - 1] |= HIGH_BIT_SET; |
|
686
|
|
|
|
|
|
|
} |
|
687
|
0
|
|
|
|
|
|
} |
|
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" */ |
|
691
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (uECC_vli_isZero(input, num_words)) { |
|
699
|
0
|
|
|
|
|
|
uECC_vli_clear(result, num_words); |
|
700
|
0
|
|
|
|
|
|
return; |
|
701
|
|
|
|
|
|
|
} |
|
702
|
|
|
|
|
|
|
|
|
703
|
0
|
|
|
|
|
|
uECC_vli_set(a, input, num_words); |
|
704
|
0
|
|
|
|
|
|
uECC_vli_set(b, mod, num_words); |
|
705
|
0
|
|
|
|
|
|
uECC_vli_clear(u, num_words); |
|
706
|
0
|
|
|
|
|
|
u[0] = 1; |
|
707
|
0
|
|
|
|
|
|
uECC_vli_clear(v, num_words); |
|
708
|
0
|
0
|
|
|
|
|
while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) { |
|
709
|
0
|
0
|
|
|
|
|
if (EVEN(a)) { |
|
710
|
0
|
|
|
|
|
|
uECC_vli_rshift1(a, num_words); |
|
711
|
0
|
|
|
|
|
|
vli_modInv_update(u, mod, num_words); |
|
712
|
0
|
0
|
|
|
|
|
} else if (EVEN(b)) { |
|
713
|
0
|
|
|
|
|
|
uECC_vli_rshift1(b, num_words); |
|
714
|
0
|
|
|
|
|
|
vli_modInv_update(v, mod, num_words); |
|
715
|
0
|
0
|
|
|
|
|
} else if (cmpResult > 0) { |
|
716
|
0
|
|
|
|
|
|
uECC_vli_sub(a, a, b, num_words); |
|
717
|
0
|
|
|
|
|
|
uECC_vli_rshift1(a, num_words); |
|
718
|
0
|
0
|
|
|
|
|
if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) { |
|
719
|
0
|
|
|
|
|
|
uECC_vli_add(u, u, mod, num_words); |
|
720
|
|
|
|
|
|
|
} |
|
721
|
0
|
|
|
|
|
|
uECC_vli_sub(u, u, v, num_words); |
|
722
|
0
|
|
|
|
|
|
vli_modInv_update(u, mod, num_words); |
|
723
|
|
|
|
|
|
|
} else { |
|
724
|
0
|
|
|
|
|
|
uECC_vli_sub(b, b, a, num_words); |
|
725
|
0
|
|
|
|
|
|
uECC_vli_rshift1(b, num_words); |
|
726
|
0
|
0
|
|
|
|
|
if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) { |
|
727
|
0
|
|
|
|
|
|
uECC_vli_add(v, v, mod, num_words); |
|
728
|
|
|
|
|
|
|
} |
|
729
|
0
|
|
|
|
|
|
uECC_vli_sub(v, v, u, num_words); |
|
730
|
0
|
|
|
|
|
|
vli_modInv_update(v, mod, num_words); |
|
731
|
|
|
|
|
|
|
} |
|
732
|
|
|
|
|
|
|
} |
|
733
|
0
|
|
|
|
|
|
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) */ |
|
748
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(t1, Z, curve); /* z^2 */ |
|
755
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */ |
|
756
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(t1, t1, Z, curve); /* z^3 */ |
|
757
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */ |
|
758
|
0
|
|
|
|
|
|
} |
|
759
|
|
|
|
|
|
|
|
|
760
|
|
|
|
|
|
|
/* P = (x1, y1) => 2P, (x2, y2) => P' */ |
|
761
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
769
|
0
|
0
|
|
|
|
|
if (initial_Z) { |
|
770
|
0
|
|
|
|
|
|
uECC_vli_set(z, initial_Z, num_words); |
|
771
|
|
|
|
|
|
|
} else { |
|
772
|
0
|
|
|
|
|
|
uECC_vli_clear(z, num_words); |
|
773
|
0
|
|
|
|
|
|
z[0] = 1; |
|
774
|
|
|
|
|
|
|
} |
|
775
|
|
|
|
|
|
|
|
|
776
|
0
|
|
|
|
|
|
uECC_vli_set(X2, X1, num_words); |
|
777
|
0
|
|
|
|
|
|
uECC_vli_set(Y2, Y1, num_words); |
|
778
|
|
|
|
|
|
|
|
|
779
|
0
|
|
|
|
|
|
apply_z(X1, Y1, z, curve); |
|
780
|
0
|
|
|
|
|
|
curve->double_jacobian(X1, Y1, z, curve); |
|
781
|
0
|
|
|
|
|
|
apply_z(X2, Y2, z, curve); |
|
782
|
0
|
|
|
|
|
|
} |
|
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
|
|
|
|
|
|
|
*/ |
|
788
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
796
|
|
|
|
|
|
|
|
|
797
|
0
|
|
|
|
|
|
uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */ |
|
798
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */ |
|
799
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */ |
|
800
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */ |
|
801
|
0
|
|
|
|
|
|
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */ |
|
802
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(t5, Y2, curve); /* t5 = (y2 - y1)^2 = D */ |
|
803
|
|
|
|
|
|
|
|
|
804
|
0
|
|
|
|
|
|
uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */ |
|
805
|
0
|
|
|
|
|
|
uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */ |
|
806
|
0
|
|
|
|
|
|
uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */ |
|
807
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(Y1, Y1, X2, curve); /* t2 = y1*(C - B) */ |
|
808
|
0
|
|
|
|
|
|
uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */ |
|
809
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(Y2, Y2, X2, curve); /* t4 = (y2 - y1)*(B - x3) */ |
|
810
|
0
|
|
|
|
|
|
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */ |
|
811
|
|
|
|
|
|
|
|
|
812
|
0
|
|
|
|
|
|
uECC_vli_set(X2, t5, num_words); |
|
813
|
0
|
|
|
|
|
|
} |
|
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
|
|
|
|
|
|
|
*/ |
|
819
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
829
|
|
|
|
|
|
|
|
|
830
|
0
|
|
|
|
|
|
uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */ |
|
831
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */ |
|
832
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */ |
|
833
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */ |
|
834
|
0
|
|
|
|
|
|
uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */ |
|
835
|
0
|
|
|
|
|
|
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */ |
|
836
|
|
|
|
|
|
|
|
|
837
|
0
|
|
|
|
|
|
uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */ |
|
838
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(Y1, Y1, t6, curve); /* t2 = y1 * (C - B) = E */ |
|
839
|
0
|
|
|
|
|
|
uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */ |
|
840
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(X2, Y2, curve); /* t3 = (y2 - y1)^2 = D */ |
|
841
|
0
|
|
|
|
|
|
uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */ |
|
842
|
|
|
|
|
|
|
|
|
843
|
0
|
|
|
|
|
|
uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */ |
|
844
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(Y2, Y2, t7, curve); /* t4 = (y2 - y1)*(B - x3) */ |
|
845
|
0
|
|
|
|
|
|
uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = (y2 - y1)*(B - x3) - E = y3 */ |
|
846
|
|
|
|
|
|
|
|
|
847
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(t7, t5, curve); /* t7 = (y2 + y1)^2 = F */ |
|
848
|
0
|
|
|
|
|
|
uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */ |
|
849
|
0
|
|
|
|
|
|
uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */ |
|
850
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(t6, t6, t5, curve); /* t6 = (y2+y1)*(x3' - B) */ |
|
851
|
0
|
|
|
|
|
|
uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words); /* t2 = (y2+y1)*(x3' - B) - E = y3' */ |
|
852
|
|
|
|
|
|
|
|
|
853
|
0
|
|
|
|
|
|
uECC_vli_set(X1, t7, num_words); |
|
854
|
0
|
|
|
|
|
|
} |
|
855
|
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
/* result may overlap point. */ |
|
857
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
870
|
|
|
|
|
|
|
|
|
871
|
0
|
|
|
|
|
|
uECC_vli_set(Rx[1], point, num_words); |
|
872
|
0
|
|
|
|
|
|
uECC_vli_set(Ry[1], point + num_words, num_words); |
|
873
|
|
|
|
|
|
|
|
|
874
|
0
|
|
|
|
|
|
XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve); |
|
875
|
|
|
|
|
|
|
|
|
876
|
0
|
0
|
|
|
|
|
for (i = num_bits - 2; i > 0; --i) { |
|
877
|
0
|
|
|
|
|
|
nb = !uECC_vli_testBit(scalar, i); |
|
878
|
0
|
|
|
|
|
|
XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve); |
|
879
|
0
|
|
|
|
|
|
XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve); |
|
880
|
|
|
|
|
|
|
} |
|
881
|
|
|
|
|
|
|
|
|
882
|
0
|
|
|
|
|
|
nb = !uECC_vli_testBit(scalar, 0); |
|
883
|
0
|
|
|
|
|
|
XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve); |
|
884
|
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
/* Find final 1/Z value. */ |
|
886
|
0
|
|
|
|
|
|
uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */ |
|
887
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve); /* Yb * (X1 - X0) */ |
|
888
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(z, z, point, curve); /* xP * Yb * (X1 - X0) */ |
|
889
|
0
|
|
|
|
|
|
uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0)) */ |
|
890
|
|
|
|
|
|
|
/* yP / (xP * Yb * (X1 - X0)) */ |
|
891
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(z, z, point + num_words, curve); |
|
892
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve); /* Xb * yP / (xP * Yb * (X1 - X0)) */ |
|
893
|
|
|
|
|
|
|
/* End 1/Z calculation */ |
|
894
|
|
|
|
|
|
|
|
|
895
|
0
|
|
|
|
|
|
XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve); |
|
896
|
0
|
|
|
|
|
|
apply_z(Rx[0], Ry[0], z, curve); |
|
897
|
|
|
|
|
|
|
|
|
898
|
0
|
|
|
|
|
|
uECC_vli_set(result, Rx[0], num_words); |
|
899
|
0
|
|
|
|
|
|
uECC_vli_set(result + num_words, Ry[0], num_words); |
|
900
|
0
|
|
|
|
|
|
} |
|
901
|
|
|
|
|
|
|
|
|
902
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); |
|
907
|
0
|
|
|
|
|
|
bitcount_t num_n_bits = curve->num_n_bits; |
|
908
|
0
|
0
|
|
|
|
|
uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) || |
|
|
|
0
|
|
|
|
|
|
|
909
|
0
|
0
|
|
|
|
|
(num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) && |
|
910
|
0
|
|
|
|
|
|
uECC_vli_testBit(k0, num_n_bits)); |
|
911
|
0
|
|
|
|
|
|
uECC_vli_add(k1, k0, curve->n, num_n_words); |
|
912
|
0
|
|
|
|
|
|
return carry; |
|
913
|
|
|
|
|
|
|
} |
|
914
|
|
|
|
|
|
|
|
|
915
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
carry = regularize_k(private_key, tmp1, tmp2, curve); |
|
926
|
|
|
|
|
|
|
|
|
927
|
0
|
|
|
|
|
|
EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve); |
|
928
|
|
|
|
|
|
|
|
|
929
|
0
|
0
|
|
|
|
|
if (EccPoint_isZero(result, curve)) { |
|
930
|
0
|
|
|
|
|
|
return 0; |
|
931
|
|
|
|
|
|
|
} |
|
932
|
0
|
|
|
|
|
|
return 1; |
|
933
|
|
|
|
|
|
|
} |
|
934
|
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
#if uECC_WORD_SIZE == 1 |
|
936
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
954
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_bytes; ++i) { |
|
959
|
0
|
|
|
|
|
|
unsigned b = num_bytes - 1 - i; |
|
960
|
0
|
|
|
|
|
|
bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE)); |
|
961
|
|
|
|
|
|
|
} |
|
962
|
0
|
|
|
|
|
|
} |
|
963
|
|
|
|
|
|
|
|
|
964
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE); |
|
969
|
0
|
0
|
|
|
|
|
for (i = 0; i < num_bytes; ++i) { |
|
970
|
0
|
|
|
|
|
|
unsigned b = num_bytes - 1 - i; |
|
971
|
0
|
|
|
|
|
|
native[b / uECC_WORD_SIZE] |= |
|
972
|
0
|
|
|
|
|
|
(uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE)); |
|
973
|
|
|
|
|
|
|
} |
|
974
|
0
|
|
|
|
|
|
} |
|
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. */ |
|
980
|
0
|
|
|
|
|
|
uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random, |
|
981
|
|
|
|
|
|
|
const uECC_word_t *top, |
|
982
|
|
|
|
|
|
|
wordcount_t num_words) { |
|
983
|
0
|
|
|
|
|
|
uECC_word_t mask = (uECC_word_t)-1; |
|
984
|
|
|
|
|
|
|
uECC_word_t tries; |
|
985
|
0
|
|
|
|
|
|
bitcount_t num_bits = uECC_vli_numBits(top, num_words); |
|
986
|
|
|
|
|
|
|
|
|
987
|
0
|
0
|
|
|
|
|
if (!g_rng_function) { |
|
988
|
0
|
|
|
|
|
|
return 0; |
|
989
|
|
|
|
|
|
|
} |
|
990
|
|
|
|
|
|
|
|
|
991
|
0
|
0
|
|
|
|
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
|
992
|
0
|
0
|
|
|
|
|
if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) { |
|
993
|
0
|
|
|
|
|
|
return 0; |
|
994
|
|
|
|
|
|
|
} |
|
995
|
0
|
|
|
|
|
|
random[num_words - 1] &= mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits)); |
|
996
|
0
|
|
|
|
|
|
if (!uECC_vli_isZero(random, num_words) && |
|
997
|
0
|
|
|
|
|
|
uECC_vli_cmp(top, random, num_words) == 1) { |
|
998
|
0
|
|
|
|
|
|
return 1; |
|
999
|
|
|
|
|
|
|
} |
|
1000
|
|
|
|
|
|
|
} |
|
1001
|
0
|
|
|
|
|
|
return 0; |
|
1002
|
|
|
|
|
|
|
} |
|
1003
|
|
|
|
|
|
|
|
|
1004
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
|
1017
|
0
|
0
|
|
|
|
|
if (!uECC_generate_random_int(_private, curve->n, BITS_TO_WORDS(curve->num_n_bits))) { |
|
1018
|
0
|
|
|
|
|
|
return 0; |
|
1019
|
|
|
|
|
|
|
} |
|
1020
|
|
|
|
|
|
|
|
|
1021
|
0
|
0
|
|
|
|
|
if (EccPoint_compute_public_key(_public, _private, curve)) { |
|
1022
|
|
|
|
|
|
|
#if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0 |
|
1023
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(private_key, BITS_TO_BYTES(curve->num_n_bits), _private); |
|
1024
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public); |
|
1025
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes( |
|
1026
|
0
|
|
|
|
|
|
public_key + curve->num_bytes, curve->num_bytes, _public + curve->num_words); |
|
1027
|
|
|
|
|
|
|
#endif |
|
1028
|
0
|
|
|
|
|
|
return 1; |
|
1029
|
|
|
|
|
|
|
} |
|
1030
|
|
|
|
|
|
|
} |
|
1031
|
0
|
|
|
|
|
|
return 0; |
|
1032
|
|
|
|
|
|
|
} |
|
1033
|
|
|
|
|
|
|
|
|
1034
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_word_t *p2[2] = {_private, tmp}; |
|
1043
|
0
|
|
|
|
|
|
uECC_word_t *initial_Z = 0; |
|
1044
|
|
|
|
|
|
|
uECC_word_t carry; |
|
1045
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
1046
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(_private, private_key, BITS_TO_BYTES(curve->num_n_bits)); |
|
1053
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(_public, public_key, num_bytes); |
|
1054
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (g_rng_function) { |
|
1064
|
0
|
0
|
|
|
|
|
if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) { |
|
1065
|
0
|
|
|
|
|
|
return 0; |
|
1066
|
|
|
|
|
|
|
} |
|
1067
|
0
|
|
|
|
|
|
initial_Z = p2[carry]; |
|
1068
|
|
|
|
|
|
|
} |
|
1069
|
|
|
|
|
|
|
|
|
1070
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(secret, num_bytes, _public); |
|
1075
|
|
|
|
|
|
|
#endif |
|
1076
|
0
|
|
|
|
|
|
return !EccPoint_isZero(_public, curve); |
|
1077
|
|
|
|
|
|
|
} |
|
1078
|
|
|
|
|
|
|
|
|
1079
|
|
|
|
|
|
|
#if uECC_SUPPORT_COMPRESSED_POINT |
|
1080
|
0
|
|
|
|
|
|
void uECC_compress(const uint8_t *public_key, uint8_t *compressed, uECC_Curve curve) { |
|
1081
|
|
|
|
|
|
|
wordcount_t i; |
|
1082
|
0
|
0
|
|
|
|
|
for (i = 0; i < curve->num_bytes; ++i) { |
|
1083
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
compressed[0] = 2 + (public_key[curve->num_bytes * 2 - 1] & 0x01); |
|
1089
|
|
|
|
|
|
|
#endif |
|
1090
|
0
|
|
|
|
|
|
} |
|
1091
|
|
|
|
|
|
|
|
|
1092
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(point, compressed + 1, curve->num_bytes); |
|
1103
|
|
|
|
|
|
|
#endif |
|
1104
|
0
|
|
|
|
|
|
curve->x_side(y, point, curve); |
|
1105
|
0
|
|
|
|
|
|
curve->mod_sqrt(y, curve); |
|
1106
|
|
|
|
|
|
|
|
|
1107
|
0
|
0
|
|
|
|
|
if ((y[0] & 0x01) != (compressed[0] & 0x01)) { |
|
1108
|
0
|
|
|
|
|
|
uECC_vli_sub(y, curve->p, y, curve->num_words); |
|
1109
|
|
|
|
|
|
|
} |
|
1110
|
|
|
|
|
|
|
|
|
1111
|
|
|
|
|
|
|
#if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0 |
|
1112
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(public_key, curve->num_bytes, point); |
|
1113
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(public_key + curve->num_bytes, curve->num_bytes, y); |
|
1114
|
|
|
|
|
|
|
#endif |
|
1115
|
0
|
|
|
|
|
|
} |
|
1116
|
|
|
|
|
|
|
#endif /* uECC_SUPPORT_COMPRESSED_POINT */ |
|
1117
|
|
|
|
|
|
|
|
|
1118
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
1122
|
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
/* The point at infinity is invalid. */ |
|
1124
|
0
|
0
|
|
|
|
|
if (EccPoint_isZero(point, curve)) { |
|
1125
|
0
|
|
|
|
|
|
return 0; |
|
1126
|
|
|
|
|
|
|
} |
|
1127
|
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
/* x and y must be smaller than p. */ |
|
1129
|
0
|
|
|
|
|
|
if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 || |
|
1130
|
0
|
|
|
|
|
|
uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) { |
|
1131
|
0
|
|
|
|
|
|
return 0; |
|
1132
|
|
|
|
|
|
|
} |
|
1133
|
|
|
|
|
|
|
|
|
1134
|
0
|
|
|
|
|
|
uECC_vli_modSquare_fast(tmp1, point + num_words, curve); |
|
1135
|
0
|
|
|
|
|
|
curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */ |
|
1136
|
|
|
|
|
|
|
|
|
1137
|
|
|
|
|
|
|
/* Make sure that y^2 == x^3 + ax + b */ |
|
1138
|
0
|
|
|
|
|
|
return (int)(uECC_vli_equal(tmp1, tmp2, num_words)); |
|
1139
|
|
|
|
|
|
|
} |
|
1140
|
|
|
|
|
|
|
|
|
1141
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(_public, public_key, curve->num_bytes); |
|
1150
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative( |
|
1151
|
0
|
|
|
|
|
|
_public + curve->num_words, public_key + curve->num_bytes, curve->num_bytes); |
|
1152
|
|
|
|
|
|
|
#endif |
|
1153
|
0
|
|
|
|
|
|
return uECC_valid_point(_public, curve); |
|
1154
|
|
|
|
|
|
|
} |
|
1155
|
|
|
|
|
|
|
|
|
1156
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) { |
|
1171
|
0
|
|
|
|
|
|
return 0; |
|
1172
|
|
|
|
|
|
|
} |
|
1173
|
|
|
|
|
|
|
|
|
1174
|
0
|
0
|
|
|
|
|
if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) { |
|
1175
|
0
|
|
|
|
|
|
return 0; |
|
1176
|
|
|
|
|
|
|
} |
|
1177
|
|
|
|
|
|
|
|
|
1178
|
|
|
|
|
|
|
/* Compute public key. */ |
|
1179
|
0
|
0
|
|
|
|
|
if (!EccPoint_compute_public_key(_public, _private, curve)) { |
|
1180
|
0
|
|
|
|
|
|
return 0; |
|
1181
|
|
|
|
|
|
|
} |
|
1182
|
|
|
|
|
|
|
|
|
1183
|
|
|
|
|
|
|
#if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0 |
|
1184
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public); |
|
1185
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes( |
|
1186
|
0
|
|
|
|
|
|
public_key + curve->num_bytes, curve->num_bytes, _public + curve->num_words); |
|
1187
|
|
|
|
|
|
|
#endif |
|
1188
|
0
|
|
|
|
|
|
return 1; |
|
1189
|
|
|
|
|
|
|
} |
|
1190
|
|
|
|
|
|
|
|
|
1191
|
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
/* -------- ECDSA code -------- */ |
|
1193
|
|
|
|
|
|
|
|
|
1194
|
0
|
|
|
|
|
|
static void bits2int(uECC_word_t *native, |
|
1195
|
|
|
|
|
|
|
const uint8_t *bits, |
|
1196
|
|
|
|
|
|
|
unsigned bits_size, |
|
1197
|
|
|
|
|
|
|
uECC_Curve curve) { |
|
1198
|
0
|
|
|
|
|
|
unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits); |
|
1199
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (bits_size > num_n_bytes) { |
|
1205
|
0
|
|
|
|
|
|
bits_size = num_n_bytes; |
|
1206
|
|
|
|
|
|
|
} |
|
1207
|
|
|
|
|
|
|
|
|
1208
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(native, bits, bits_size); |
|
1213
|
|
|
|
|
|
|
#endif |
|
1214
|
0
|
0
|
|
|
|
|
if (bits_size * 8 <= (unsigned)curve->num_n_bits) { |
|
1215
|
0
|
|
|
|
|
|
return; |
|
1216
|
|
|
|
|
|
|
} |
|
1217
|
0
|
|
|
|
|
|
shift = bits_size * 8 - curve->num_n_bits; |
|
1218
|
0
|
|
|
|
|
|
carry = 0; |
|
1219
|
0
|
|
|
|
|
|
ptr = native + num_n_words; |
|
1220
|
0
|
0
|
|
|
|
|
while (ptr-- > native) { |
|
1221
|
0
|
|
|
|
|
|
uECC_word_t temp = *ptr; |
|
1222
|
0
|
|
|
|
|
|
*ptr = (temp >> shift) | carry; |
|
1223
|
0
|
|
|
|
|
|
carry = temp << (uECC_WORD_BITS - shift); |
|
1224
|
|
|
|
|
|
|
} |
|
1225
|
|
|
|
|
|
|
|
|
1226
|
|
|
|
|
|
|
/* Reduce mod curve_n */ |
|
1227
|
0
|
0
|
|
|
|
|
if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) { |
|
1228
|
0
|
|
|
|
|
|
uECC_vli_sub(native, native, curve->n, num_n_words); |
|
1229
|
|
|
|
|
|
|
} |
|
1230
|
|
|
|
|
|
|
} |
|
1231
|
|
|
|
|
|
|
|
|
1232
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
1249
|
0
|
|
|
|
|
|
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); |
|
1250
|
0
|
|
|
|
|
|
bitcount_t num_n_bits = curve->num_n_bits; |
|
1251
|
|
|
|
|
|
|
|
|
1252
|
|
|
|
|
|
|
/* Make sure 0 < k < curve_n */ |
|
1253
|
0
|
0
|
|
|
|
|
if (uECC_vli_isZero(k, num_words) || uECC_vli_cmp(curve->n, k, num_n_words) != 1) { |
|
|
|
0
|
|
|
|
|
|
|
1254
|
0
|
|
|
|
|
|
return 0; |
|
1255
|
|
|
|
|
|
|
} |
|
1256
|
|
|
|
|
|
|
|
|
1257
|
0
|
|
|
|
|
|
carry = regularize_k(k, tmp, s, curve); |
|
1258
|
0
|
|
|
|
|
|
EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve); |
|
1259
|
0
|
0
|
|
|
|
|
if (uECC_vli_isZero(p, num_words)) { |
|
1260
|
0
|
|
|
|
|
|
return 0; |
|
1261
|
|
|
|
|
|
|
} |
|
1262
|
|
|
|
|
|
|
|
|
1263
|
|
|
|
|
|
|
/* If an RNG function was specified, get a random number |
|
1264
|
|
|
|
|
|
|
to prevent side channel analysis of k. */ |
|
1265
|
0
|
0
|
|
|
|
|
if (!g_rng_function) { |
|
1266
|
0
|
|
|
|
|
|
uECC_vli_clear(tmp, num_n_words); |
|
1267
|
0
|
|
|
|
|
|
tmp[0] = 1; |
|
1268
|
0
|
0
|
|
|
|
|
} else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) { |
|
1269
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */ |
|
1275
|
0
|
|
|
|
|
|
uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */ |
|
1276
|
0
|
|
|
|
|
|
uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */ |
|
1277
|
|
|
|
|
|
|
|
|
1278
|
|
|
|
|
|
|
#if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0 |
|
1279
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits)); /* tmp = d */ |
|
1286
|
|
|
|
|
|
|
#endif |
|
1287
|
|
|
|
|
|
|
|
|
1288
|
0
|
|
|
|
|
|
s[num_n_words - 1] = 0; |
|
1289
|
0
|
|
|
|
|
|
uECC_vli_set(s, p, num_words); |
|
1290
|
0
|
|
|
|
|
|
uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */ |
|
1291
|
|
|
|
|
|
|
|
|
1292
|
0
|
|
|
|
|
|
bits2int(tmp, message_hash, hash_size, curve); |
|
1293
|
0
|
|
|
|
|
|
uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */ |
|
1294
|
0
|
|
|
|
|
|
uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */ |
|
1295
|
0
|
0
|
|
|
|
|
if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) { |
|
1296
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s); |
|
1302
|
|
|
|
|
|
|
#endif |
|
1303
|
0
|
|
|
|
|
|
return 1; |
|
1304
|
|
|
|
|
|
|
} |
|
1305
|
|
|
|
|
|
|
|
|
1306
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
|
1315
|
0
|
0
|
|
|
|
|
if (!uECC_generate_random_int(k, curve->n, BITS_TO_WORDS(curve->num_n_bits))) { |
|
1316
|
0
|
|
|
|
|
|
return 0; |
|
1317
|
|
|
|
|
|
|
} |
|
1318
|
|
|
|
|
|
|
|
|
1319
|
0
|
0
|
|
|
|
|
if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature, curve)) { |
|
1320
|
0
|
|
|
|
|
|
return 1; |
|
1321
|
|
|
|
|
|
|
} |
|
1322
|
|
|
|
|
|
|
} |
|
1323
|
0
|
|
|
|
|
|
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. */ |
|
1328
|
0
|
|
|
|
|
|
static void HMAC_init(const uECC_HashContext *hash_context, const uint8_t *K) { |
|
1329
|
0
|
|
|
|
|
|
uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size; |
|
1330
|
|
|
|
|
|
|
unsigned i; |
|
1331
|
0
|
0
|
|
|
|
|
for (i = 0; i < hash_context->result_size; ++i) |
|
1332
|
0
|
|
|
|
|
|
pad[i] = K[i] ^ 0x36; |
|
1333
|
0
|
0
|
|
|
|
|
for (; i < hash_context->block_size; ++i) |
|
1334
|
0
|
|
|
|
|
|
pad[i] = 0x36; |
|
1335
|
|
|
|
|
|
|
|
|
1336
|
0
|
|
|
|
|
|
hash_context->init_hash(hash_context); |
|
1337
|
0
|
|
|
|
|
|
hash_context->update_hash(hash_context, pad, hash_context->block_size); |
|
1338
|
0
|
|
|
|
|
|
} |
|
1339
|
|
|
|
|
|
|
|
|
1340
|
0
|
|
|
|
|
|
static void HMAC_update(const uECC_HashContext *hash_context, |
|
1341
|
|
|
|
|
|
|
const uint8_t *message, |
|
1342
|
|
|
|
|
|
|
unsigned message_size) { |
|
1343
|
0
|
|
|
|
|
|
hash_context->update_hash(hash_context, message, message_size); |
|
1344
|
0
|
|
|
|
|
|
} |
|
1345
|
|
|
|
|
|
|
|
|
1346
|
0
|
|
|
|
|
|
static void HMAC_finish(const uECC_HashContext *hash_context, |
|
1347
|
|
|
|
|
|
|
const uint8_t *K, |
|
1348
|
|
|
|
|
|
|
uint8_t *result) { |
|
1349
|
0
|
|
|
|
|
|
uint8_t *pad = hash_context->tmp + 2 * hash_context->result_size; |
|
1350
|
|
|
|
|
|
|
unsigned i; |
|
1351
|
0
|
0
|
|
|
|
|
for (i = 0; i < hash_context->result_size; ++i) |
|
1352
|
0
|
|
|
|
|
|
pad[i] = K[i] ^ 0x5c; |
|
1353
|
0
|
0
|
|
|
|
|
for (; i < hash_context->block_size; ++i) |
|
1354
|
0
|
|
|
|
|
|
pad[i] = 0x5c; |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
0
|
|
|
|
|
|
hash_context->finish_hash(hash_context, result); |
|
1357
|
|
|
|
|
|
|
|
|
1358
|
0
|
|
|
|
|
|
hash_context->init_hash(hash_context); |
|
1359
|
0
|
|
|
|
|
|
hash_context->update_hash(hash_context, pad, hash_context->block_size); |
|
1360
|
0
|
|
|
|
|
|
hash_context->update_hash(hash_context, result, hash_context->result_size); |
|
1361
|
0
|
|
|
|
|
|
hash_context->finish_hash(hash_context, result); |
|
1362
|
0
|
|
|
|
|
|
} |
|
1363
|
|
|
|
|
|
|
|
|
1364
|
|
|
|
|
|
|
/* V = HMAC_K(V) */ |
|
1365
|
0
|
|
|
|
|
|
static void update_V(const uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) { |
|
1366
|
0
|
|
|
|
|
|
HMAC_init(hash_context, K); |
|
1367
|
0
|
|
|
|
|
|
HMAC_update(hash_context, V, hash_context->result_size); |
|
1368
|
0
|
|
|
|
|
|
HMAC_finish(hash_context, K, V); |
|
1369
|
0
|
|
|
|
|
|
} |
|
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: | | (1 byte overlapped 0x00 or 0x01) / */ |
|
1377
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uint8_t *K = hash_context->tmp; |
|
1384
|
0
|
|
|
|
|
|
uint8_t *V = K + hash_context->result_size; |
|
1385
|
0
|
|
|
|
|
|
wordcount_t num_bytes = curve->num_bytes; |
|
1386
|
0
|
|
|
|
|
|
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); |
|
1387
|
0
|
|
|
|
|
|
bitcount_t num_n_bits = curve->num_n_bits; |
|
1388
|
|
|
|
|
|
|
uECC_word_t tries; |
|
1389
|
|
|
|
|
|
|
unsigned i; |
|
1390
|
0
|
0
|
|
|
|
|
for (i = 0; i < hash_context->result_size; ++i) { |
|
1391
|
0
|
|
|
|
|
|
V[i] = 0x01; |
|
1392
|
0
|
|
|
|
|
|
K[i] = 0; |
|
1393
|
|
|
|
|
|
|
} |
|
1394
|
|
|
|
|
|
|
|
|
1395
|
|
|
|
|
|
|
/* K = HMAC_K(V || 0x00 || int2octets(x) || h(m)) */ |
|
1396
|
0
|
|
|
|
|
|
HMAC_init(hash_context, K); |
|
1397
|
0
|
|
|
|
|
|
V[hash_context->result_size] = 0x00; |
|
1398
|
0
|
|
|
|
|
|
HMAC_update(hash_context, V, hash_context->result_size + 1); |
|
1399
|
0
|
|
|
|
|
|
HMAC_update(hash_context, private_key, num_bytes); |
|
1400
|
0
|
|
|
|
|
|
HMAC_update(hash_context, message_hash, hash_size); |
|
1401
|
0
|
|
|
|
|
|
HMAC_finish(hash_context, K, K); |
|
1402
|
|
|
|
|
|
|
|
|
1403
|
0
|
|
|
|
|
|
update_V(hash_context, K, V); |
|
1404
|
|
|
|
|
|
|
|
|
1405
|
|
|
|
|
|
|
/* K = HMAC_K(V || 0x01 || int2octets(x) || h(m)) */ |
|
1406
|
0
|
|
|
|
|
|
HMAC_init(hash_context, K); |
|
1407
|
0
|
|
|
|
|
|
V[hash_context->result_size] = 0x01; |
|
1408
|
0
|
|
|
|
|
|
HMAC_update(hash_context, V, hash_context->result_size + 1); |
|
1409
|
0
|
|
|
|
|
|
HMAC_update(hash_context, private_key, num_bytes); |
|
1410
|
0
|
|
|
|
|
|
HMAC_update(hash_context, message_hash, hash_size); |
|
1411
|
0
|
|
|
|
|
|
HMAC_finish(hash_context, K, K); |
|
1412
|
|
|
|
|
|
|
|
|
1413
|
0
|
|
|
|
|
|
update_V(hash_context, K, V); |
|
1414
|
|
|
|
|
|
|
|
|
1415
|
0
|
0
|
|
|
|
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
|
1416
|
|
|
|
|
|
|
uECC_word_t T[uECC_MAX_WORDS]; |
|
1417
|
0
|
|
|
|
|
|
uint8_t *T_ptr = (uint8_t *)T; |
|
1418
|
0
|
|
|
|
|
|
wordcount_t T_bytes = 0; |
|
1419
|
|
|
|
|
|
|
for (;;) { |
|
1420
|
0
|
|
|
|
|
|
update_V(hash_context, K, V); |
|
1421
|
0
|
0
|
|
|
|
|
for (i = 0; i < hash_context->result_size; ++i) { |
|
1422
|
0
|
|
|
|
|
|
T_ptr[T_bytes++] = V[i]; |
|
1423
|
0
|
0
|
|
|
|
|
if (T_bytes >= num_n_words * uECC_WORD_SIZE) { |
|
1424
|
0
|
|
|
|
|
|
goto filled; |
|
1425
|
|
|
|
|
|
|
} |
|
1426
|
|
|
|
|
|
|
} |
|
1427
|
0
|
|
|
|
|
|
} |
|
1428
|
|
|
|
|
|
|
filled: |
|
1429
|
0
|
0
|
|
|
|
|
if ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8 > num_n_bits) { |
|
1430
|
0
|
|
|
|
|
|
uECC_word_t mask = (uECC_word_t)-1; |
|
1431
|
0
|
|
|
|
|
|
T[num_n_words - 1] &= |
|
1432
|
0
|
|
|
|
|
|
mask >> ((bitcount_t)(num_n_words * uECC_WORD_SIZE * 8 - num_n_bits)); |
|
1433
|
|
|
|
|
|
|
} |
|
1434
|
|
|
|
|
|
|
|
|
1435
|
0
|
0
|
|
|
|
|
if (uECC_sign_with_k(private_key, message_hash, hash_size, T, signature, curve)) { |
|
1436
|
0
|
|
|
|
|
|
return 1; |
|
1437
|
|
|
|
|
|
|
} |
|
1438
|
|
|
|
|
|
|
|
|
1439
|
|
|
|
|
|
|
/* K = HMAC_K(V || 0x00) */ |
|
1440
|
0
|
|
|
|
|
|
HMAC_init(hash_context, K); |
|
1441
|
0
|
|
|
|
|
|
V[hash_context->result_size] = 0x00; |
|
1442
|
0
|
|
|
|
|
|
HMAC_update(hash_context, V, hash_context->result_size + 1); |
|
1443
|
0
|
|
|
|
|
|
HMAC_finish(hash_context, K, K); |
|
1444
|
|
|
|
|
|
|
|
|
1445
|
0
|
|
|
|
|
|
update_V(hash_context, K, V); |
|
1446
|
|
|
|
|
|
|
} |
|
1447
|
0
|
|
|
|
|
|
return 0; |
|
1448
|
|
|
|
|
|
|
} |
|
1449
|
|
|
|
|
|
|
|
|
1450
|
0
|
|
|
|
|
|
static bitcount_t smax(bitcount_t a, bitcount_t b) { |
|
1451
|
0
|
|
|
|
|
|
return (a > b ? a : b); |
|
1452
|
|
|
|
|
|
|
} |
|
1453
|
|
|
|
|
|
|
|
|
1454
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
wordcount_t num_words = curve->num_words; |
|
1478
|
0
|
|
|
|
|
|
wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits); |
|
1479
|
|
|
|
|
|
|
|
|
1480
|
0
|
|
|
|
|
|
rx[num_n_words - 1] = 0; |
|
1481
|
0
|
|
|
|
|
|
r[num_n_words - 1] = 0; |
|
1482
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(_public, public_key, curve->num_bytes); |
|
1489
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative( |
|
1490
|
0
|
|
|
|
|
|
_public + num_words, public_key + curve->num_bytes, curve->num_bytes); |
|
1491
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(r, signature, curve->num_bytes); |
|
1492
|
0
|
|
|
|
|
|
uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes); |
|
1493
|
|
|
|
|
|
|
#endif |
|
1494
|
|
|
|
|
|
|
|
|
1495
|
|
|
|
|
|
|
/* r, s must not be 0. */ |
|
1496
|
0
|
0
|
|
|
|
|
if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) { |
|
|
|
0
|
|
|
|
|
|
|
1497
|
0
|
|
|
|
|
|
return 0; |
|
1498
|
|
|
|
|
|
|
} |
|
1499
|
|
|
|
|
|
|
|
|
1500
|
|
|
|
|
|
|
/* r, s must be < n. */ |
|
1501
|
0
|
|
|
|
|
|
if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 || |
|
1502
|
0
|
|
|
|
|
|
uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) { |
|
1503
|
0
|
|
|
|
|
|
return 0; |
|
1504
|
|
|
|
|
|
|
} |
|
1505
|
|
|
|
|
|
|
|
|
1506
|
|
|
|
|
|
|
/* Calculate u1 and u2. */ |
|
1507
|
0
|
|
|
|
|
|
uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */ |
|
1508
|
0
|
|
|
|
|
|
u1[num_n_words - 1] = 0; |
|
1509
|
0
|
|
|
|
|
|
bits2int(u1, message_hash, hash_size, curve); |
|
1510
|
0
|
|
|
|
|
|
uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */ |
|
1511
|
0
|
|
|
|
|
|
uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */ |
|
1512
|
|
|
|
|
|
|
|
|
1513
|
|
|
|
|
|
|
/* Calculate sum = G + Q. */ |
|
1514
|
0
|
|
|
|
|
|
uECC_vli_set(sum, _public, num_words); |
|
1515
|
0
|
|
|
|
|
|
uECC_vli_set(sum + num_words, _public + num_words, num_words); |
|
1516
|
0
|
|
|
|
|
|
uECC_vli_set(tx, curve->G, num_words); |
|
1517
|
0
|
|
|
|
|
|
uECC_vli_set(ty, curve->G + num_words, num_words); |
|
1518
|
0
|
|
|
|
|
|
uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */ |
|
1519
|
0
|
|
|
|
|
|
XYcZ_add(tx, ty, sum, sum + num_words, curve); |
|
1520
|
0
|
|
|
|
|
|
uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */ |
|
1521
|
0
|
|
|
|
|
|
apply_z(sum, sum + num_words, z, curve); |
|
1522
|
|
|
|
|
|
|
|
|
1523
|
|
|
|
|
|
|
/* Use Shamir's trick to calculate u1*G + u2*Q */ |
|
1524
|
0
|
|
|
|
|
|
points[0] = 0; |
|
1525
|
0
|
|
|
|
|
|
points[1] = curve->G; |
|
1526
|
0
|
|
|
|
|
|
points[2] = _public; |
|
1527
|
0
|
|
|
|
|
|
points[3] = sum; |
|
1528
|
0
|
|
|
|
|
|
num_bits = smax(uECC_vli_numBits(u1, num_n_words), |
|
1529
|
0
|
|
|
|
|
|
uECC_vli_numBits(u2, num_n_words)); |
|
1530
|
|
|
|
|
|
|
|
|
1531
|
0
|
|
|
|
|
|
point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) | |
|
1532
|
0
|
0
|
|
|
|
|
((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)]; |
|
1533
|
0
|
|
|
|
|
|
uECC_vli_set(rx, point, num_words); |
|
1534
|
0
|
|
|
|
|
|
uECC_vli_set(ry, point + num_words, num_words); |
|
1535
|
0
|
|
|
|
|
|
uECC_vli_clear(z, num_words); |
|
1536
|
0
|
|
|
|
|
|
z[0] = 1; |
|
1537
|
|
|
|
|
|
|
|
|
1538
|
0
|
0
|
|
|
|
|
for (i = num_bits - 2; i >= 0; --i) { |
|
1539
|
|
|
|
|
|
|
uECC_word_t index; |
|
1540
|
0
|
|
|
|
|
|
curve->double_jacobian(rx, ry, z, curve); |
|
1541
|
|
|
|
|
|
|
|
|
1542
|
0
|
0
|
|
|
|
|
index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1); |
|
1543
|
0
|
|
|
|
|
|
point = points[index]; |
|
1544
|
0
|
0
|
|
|
|
|
if (point) { |
|
1545
|
0
|
|
|
|
|
|
uECC_vli_set(tx, point, num_words); |
|
1546
|
0
|
|
|
|
|
|
uECC_vli_set(ty, point + num_words, num_words); |
|
1547
|
0
|
|
|
|
|
|
apply_z(tx, ty, z, curve); |
|
1548
|
0
|
|
|
|
|
|
uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */ |
|
1549
|
0
|
|
|
|
|
|
XYcZ_add(tx, ty, rx, ry, curve); |
|
1550
|
0
|
|
|
|
|
|
uECC_vli_modMult_fast(z, z, tz, curve); |
|
1551
|
|
|
|
|
|
|
} |
|
1552
|
|
|
|
|
|
|
} |
|
1553
|
|
|
|
|
|
|
|
|
1554
|
0
|
|
|
|
|
|
uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */ |
|
1555
|
0
|
|
|
|
|
|
apply_z(rx, ry, z, curve); |
|
1556
|
|
|
|
|
|
|
|
|
1557
|
|
|
|
|
|
|
/* v = x1 (mod n) */ |
|
1558
|
0
|
0
|
|
|
|
|
if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) { |
|
1559
|
0
|
|
|
|
|
|
uECC_vli_sub(rx, rx, curve->n, num_n_words); |
|
1560
|
|
|
|
|
|
|
} |
|
1561
|
|
|
|
|
|
|
|
|
1562
|
|
|
|
|
|
|
/* Accept only if v == r. */ |
|
1563
|
0
|
|
|
|
|
|
return (int)(uECC_vli_equal(rx, r, num_words)); |
|
1564
|
|
|
|
|
|
|
} |
|
1565
|
|
|
|
|
|
|
|
|
1566
|
|
|
|
|
|
|
#if uECC_ENABLE_VLI_API |
|
1567
|
|
|
|
|
|
|
|
|
1568
|
|
|
|
|
|
|
unsigned uECC_curve_num_words(uECC_Curve curve) { |
|
1569
|
|
|
|
|
|
|
return curve->num_words; |
|
1570
|
|
|
|
|
|
|
} |
|
1571
|
|
|
|
|
|
|
|
|
1572
|
|
|
|
|
|
|
unsigned uECC_curve_num_bytes(uECC_Curve curve) { |
|
1573
|
|
|
|
|
|
|
return curve->num_bytes; |
|
1574
|
|
|
|
|
|
|
} |
|
1575
|
|
|
|
|
|
|
|
|
1576
|
|
|
|
|
|
|
unsigned uECC_curve_num_bits(uECC_Curve curve) { |
|
1577
|
|
|
|
|
|
|
return curve->num_bytes * 8; |
|
1578
|
|
|
|
|
|
|
} |
|
1579
|
|
|
|
|
|
|
|
|
1580
|
|
|
|
|
|
|
unsigned uECC_curve_num_n_words(uECC_Curve curve) { |
|
1581
|
|
|
|
|
|
|
return BITS_TO_WORDS(curve->num_n_bits); |
|
1582
|
|
|
|
|
|
|
} |
|
1583
|
|
|
|
|
|
|
|
|
1584
|
|
|
|
|
|
|
unsigned uECC_curve_num_n_bytes(uECC_Curve curve) { |
|
1585
|
|
|
|
|
|
|
return BITS_TO_BYTES(curve->num_n_bits); |
|
1586
|
|
|
|
|
|
|
} |
|
1587
|
|
|
|
|
|
|
|
|
1588
|
|
|
|
|
|
|
unsigned uECC_curve_num_n_bits(uECC_Curve curve) { |
|
1589
|
|
|
|
|
|
|
return curve->num_n_bits; |
|
1590
|
|
|
|
|
|
|
} |
|
1591
|
|
|
|
|
|
|
|
|
1592
|
|
|
|
|
|
|
const uECC_word_t *uECC_curve_p(uECC_Curve curve) { |
|
1593
|
|
|
|
|
|
|
return curve->p; |
|
1594
|
|
|
|
|
|
|
} |
|
1595
|
|
|
|
|
|
|
|
|
1596
|
|
|
|
|
|
|
const uECC_word_t *uECC_curve_n(uECC_Curve curve) { |
|
1597
|
|
|
|
|
|
|
return curve->n; |
|
1598
|
|
|
|
|
|
|
} |
|
1599
|
|
|
|
|
|
|
|
|
1600
|
|
|
|
|
|
|
const uECC_word_t *uECC_curve_G(uECC_Curve curve) { |
|
1601
|
|
|
|
|
|
|
return curve->G; |
|
1602
|
|
|
|
|
|
|
} |
|
1603
|
|
|
|
|
|
|
|
|
1604
|
|
|
|
|
|
|
const uECC_word_t *uECC_curve_b(uECC_Curve curve) { |
|
1605
|
|
|
|
|
|
|
return curve->b; |
|
1606
|
|
|
|
|
|
|
} |
|
1607
|
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
#if uECC_SUPPORT_COMPRESSED_POINT |
|
1609
|
|
|
|
|
|
|
void uECC_vli_mod_sqrt(uECC_word_t *a, uECC_Curve curve) { |
|
1610
|
|
|
|
|
|
|
curve->mod_sqrt(a, curve); |
|
1611
|
|
|
|
|
|
|
} |
|
1612
|
|
|
|
|
|
|
#endif |
|
1613
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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 */ |