]>
Commit | Line | Data |
---|---|---|
07aa4c70 DA |
1 | /*********************************************************************** |
2 | * Copyright (c) 2013-2015 Pieter Wuille * | |
3 | * Distributed under the MIT software license, see the accompanying * | |
4 | * file COPYING or https://www.opensource.org/licenses/mit-license.php.* | |
5 | ***********************************************************************/ | |
0a433ea2 | 6 | |
ae9e6485 GM |
7 | #define SECP256K1_BUILD |
8 | ||
04e34d18 | 9 | #include "include/secp256k1.h" |
238305fd | 10 | #include "include/secp256k1_preallocated.h" |
04e34d18 | 11 | |
7c068998 | 12 | #include "assumptions.h" |
1c7fa133 | 13 | #include "util.h" |
11ab5622 | 14 | #include "field_impl.h" |
a9f5c8b8 | 15 | #include "scalar_impl.h" |
11ab5622 PW |
16 | #include "group_impl.h" |
17 | #include "ecmult_impl.h" | |
44015000 | 18 | #include "ecmult_const_impl.h" |
949c1ebb | 19 | #include "ecmult_gen_impl.h" |
11ab5622 | 20 | #include "ecdsa_impl.h" |
e2f71f1e | 21 | #include "eckey_impl.h" |
b37fbc28 | 22 | #include "hash_impl.h" |
548de42e | 23 | #include "scratch_impl.h" |
8bc6aeff | 24 | #include "selftest.h" |
254327e4 | 25 | |
ae9e6485 GM |
26 | #ifdef SECP256K1_NO_BUILD |
27 | # error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c" | |
28 | #endif | |
29 | ||
7b50483a GM |
30 | #if defined(VALGRIND) |
31 | # include <valgrind/memcheck.h> | |
32 | #endif | |
33 | ||
995c5487 PW |
34 | #define ARG_CHECK(cond) do { \ |
35 | if (EXPECT(!(cond), 0)) { \ | |
dd891e0e | 36 | secp256k1_callback_call(&ctx->illegal_callback, #cond); \ |
995c5487 PW |
37 | return 0; \ |
38 | } \ | |
39 | } while(0) | |
40 | ||
6095a863 TR |
41 | #define ARG_CHECK_NO_RETURN(cond) do { \ |
42 | if (EXPECT(!(cond), 0)) { \ | |
43 | secp256k1_callback_call(&ctx->illegal_callback, #cond); \ | |
44 | } \ | |
45 | } while(0) | |
46 | ||
5db782e6 | 47 | #ifndef USE_EXTERNAL_DEFAULT_CALLBACKS |
908bdce6 TR |
48 | #include <stdlib.h> |
49 | #include <stdio.h> | |
77defd2c | 50 | static void secp256k1_default_illegal_callback_fn(const char* str, void* data) { |
995c5487 PW |
51 | (void)data; |
52 | fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str); | |
53 | abort(); | |
54 | } | |
77defd2c | 55 | static void secp256k1_default_error_callback_fn(const char* str, void* data) { |
995c5487 PW |
56 | (void)data; |
57 | fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str); | |
58 | abort(); | |
59 | } | |
5db782e6 | 60 | #else |
77defd2c TR |
61 | void secp256k1_default_illegal_callback_fn(const char* str, void* data); |
62 | void secp256k1_default_error_callback_fn(const char* str, void* data); | |
5db782e6 TR |
63 | #endif |
64 | ||
65 | static const secp256k1_callback default_illegal_callback = { | |
77defd2c | 66 | secp256k1_default_illegal_callback_fn, |
5db782e6 TR |
67 | NULL |
68 | }; | |
995c5487 | 69 | |
dd891e0e | 70 | static const secp256k1_callback default_error_callback = { |
77defd2c | 71 | secp256k1_default_error_callback_fn, |
995c5487 PW |
72 | NULL |
73 | }; | |
74 | ||
a9b6595e | 75 | struct secp256k1_context_struct { |
dd891e0e PW |
76 | secp256k1_ecmult_context ecmult_ctx; |
77 | secp256k1_ecmult_gen_context ecmult_gen_ctx; | |
78 | secp256k1_callback illegal_callback; | |
79 | secp256k1_callback error_callback; | |
7b50483a | 80 | int declassify; |
a9b6595e PW |
81 | }; |
82 | ||
ed7c0841 AP |
83 | static const secp256k1_context secp256k1_context_no_precomp_ = { |
84 | { 0 }, | |
85 | { 0 }, | |
77defd2c | 86 | { secp256k1_default_illegal_callback_fn, 0 }, |
7b50483a GM |
87 | { secp256k1_default_error_callback_fn, 0 }, |
88 | 0 | |
ed7c0841 AP |
89 | }; |
90 | const secp256k1_context *secp256k1_context_no_precomp = &secp256k1_context_no_precomp_; | |
91 | ||
ef020de1 TR |
92 | size_t secp256k1_context_preallocated_size(unsigned int flags) { |
93 | size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context)); | |
ebfa2058 TR |
94 | /* A return value of 0 is reserved as an indicator for errors when we call this function internally. */ |
95 | VERIFY_CHECK(ret != 0); | |
ef020de1 TR |
96 | |
97 | if (EXPECT((flags & SECP256K1_FLAGS_TYPE_MASK) != SECP256K1_FLAGS_TYPE_CONTEXT, 0)) { | |
98 | secp256k1_callback_call(&default_illegal_callback, | |
99 | "Invalid flags"); | |
100 | return 0; | |
101 | } | |
102 | ||
103 | if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) { | |
104 | ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE; | |
105 | } | |
106 | if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) { | |
107 | ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE; | |
108 | } | |
109 | return ret; | |
110 | } | |
111 | ||
5feadde4 TR |
112 | size_t secp256k1_context_preallocated_clone_size(const secp256k1_context* ctx) { |
113 | size_t ret = ROUND_TO_ALIGN(sizeof(secp256k1_context)); | |
ba12dd08 | 114 | VERIFY_CHECK(ctx != NULL); |
5feadde4 TR |
115 | if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) { |
116 | ret += SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE; | |
117 | } | |
118 | if (secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)) { | |
119 | ret += SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE; | |
120 | } | |
121 | return ret; | |
122 | } | |
123 | ||
c4fd5dab TR |
124 | secp256k1_context* secp256k1_context_preallocated_create(void* prealloc, unsigned int flags) { |
125 | void* const base = prealloc; | |
ba12dd08 TR |
126 | size_t prealloc_size; |
127 | secp256k1_context* ret; | |
c4fd5dab | 128 | |
8bc6aeff PW |
129 | if (!secp256k1_selftest()) { |
130 | secp256k1_callback_call(&default_error_callback, "self test failed"); | |
131 | } | |
ebfa2058 | 132 | |
ba12dd08 | 133 | prealloc_size = secp256k1_context_preallocated_size(flags); |
ebfa2058 TR |
134 | if (prealloc_size == 0) { |
135 | return NULL; | |
136 | } | |
137 | VERIFY_CHECK(prealloc != NULL); | |
ba12dd08 | 138 | ret = (secp256k1_context*)manual_alloc(&prealloc, sizeof(secp256k1_context), base, prealloc_size); |
995c5487 PW |
139 | ret->illegal_callback = default_illegal_callback; |
140 | ret->error_callback = default_error_callback; | |
a9b6595e PW |
141 | |
142 | secp256k1_ecmult_context_init(&ret->ecmult_ctx); | |
143 | secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx); | |
144 | ||
ebfa2058 TR |
145 | /* Flags have been checked by secp256k1_context_preallocated_size. */ |
146 | VERIFY_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_CONTEXT); | |
9234391e | 147 | if (flags & SECP256K1_FLAGS_BIT_CONTEXT_SIGN) { |
c4fd5dab | 148 | secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &prealloc); |
04e34d18 | 149 | } |
9234391e | 150 | if (flags & SECP256K1_FLAGS_BIT_CONTEXT_VERIFY) { |
c4fd5dab | 151 | secp256k1_ecmult_context_build(&ret->ecmult_ctx, &prealloc); |
04e34d18 | 152 | } |
7b50483a | 153 | ret->declassify = !!(flags & SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY); |
a9b6595e | 154 | |
c4fd5dab TR |
155 | return (secp256k1_context*) ret; |
156 | } | |
157 | ||
158 | secp256k1_context* secp256k1_context_create(unsigned int flags) { | |
159 | size_t const prealloc_size = secp256k1_context_preallocated_size(flags); | |
160 | secp256k1_context* ctx = (secp256k1_context*)checked_malloc(&default_error_callback, prealloc_size); | |
161 | if (EXPECT(secp256k1_context_preallocated_create(ctx, flags) == NULL, 0)) { | |
162 | free(ctx); | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | return ctx; | |
254327e4 PW |
167 | } |
168 | ||
5feadde4 | 169 | secp256k1_context* secp256k1_context_preallocated_clone(const secp256k1_context* ctx, void* prealloc) { |
ba12dd08 TR |
170 | size_t prealloc_size; |
171 | secp256k1_context* ret; | |
172 | VERIFY_CHECK(ctx != NULL); | |
173 | ARG_CHECK(prealloc != NULL); | |
174 | ||
175 | prealloc_size = secp256k1_context_preallocated_clone_size(ctx); | |
176 | ret = (secp256k1_context*)prealloc; | |
c4fd5dab TR |
177 | memcpy(ret, ctx, prealloc_size); |
178 | secp256k1_ecmult_gen_context_finalize_memcpy(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx); | |
179 | secp256k1_ecmult_context_finalize_memcpy(&ret->ecmult_ctx, &ctx->ecmult_ctx); | |
d899b5b6 AP |
180 | return ret; |
181 | } | |
182 | ||
5feadde4 | 183 | secp256k1_context* secp256k1_context_clone(const secp256k1_context* ctx) { |
ba12dd08 TR |
184 | secp256k1_context* ret; |
185 | size_t prealloc_size; | |
186 | ||
187 | VERIFY_CHECK(ctx != NULL); | |
188 | prealloc_size = secp256k1_context_preallocated_clone_size(ctx); | |
189 | ret = (secp256k1_context*)checked_malloc(&ctx->error_callback, prealloc_size); | |
5feadde4 TR |
190 | ret = secp256k1_context_preallocated_clone(ctx, ret); |
191 | return ret; | |
192 | } | |
193 | ||
c4fd5dab | 194 | void secp256k1_context_preallocated_destroy(secp256k1_context* ctx) { |
6095a863 | 195 | ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp); |
2b199de8 | 196 | if (ctx != NULL) { |
912f203f GM |
197 | secp256k1_ecmult_context_clear(&ctx->ecmult_ctx); |
198 | secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx); | |
c4fd5dab TR |
199 | } |
200 | } | |
9aac0080 | 201 | |
c4fd5dab TR |
202 | void secp256k1_context_destroy(secp256k1_context* ctx) { |
203 | if (ctx != NULL) { | |
204 | secp256k1_context_preallocated_destroy(ctx); | |
912f203f GM |
205 | free(ctx); |
206 | } | |
254327e4 PW |
207 | } |
208 | ||
dd891e0e | 209 | void secp256k1_context_set_illegal_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) { |
6095a863 | 210 | ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp); |
2b199de8 | 211 | if (fun == NULL) { |
77defd2c | 212 | fun = secp256k1_default_illegal_callback_fn; |
912f203f | 213 | } |
995c5487 PW |
214 | ctx->illegal_callback.fn = fun; |
215 | ctx->illegal_callback.data = data; | |
216 | } | |
217 | ||
dd891e0e | 218 | void secp256k1_context_set_error_callback(secp256k1_context* ctx, void (*fun)(const char* message, void* data), const void* data) { |
6095a863 | 219 | ARG_CHECK_NO_RETURN(ctx != secp256k1_context_no_precomp); |
2b199de8 | 220 | if (fun == NULL) { |
77defd2c | 221 | fun = secp256k1_default_error_callback_fn; |
912f203f | 222 | } |
995c5487 PW |
223 | ctx->error_callback.fn = fun; |
224 | ctx->error_callback.data = data; | |
225 | } | |
226 | ||
6fe50439 | 227 | secp256k1_scratch_space* secp256k1_scratch_space_create(const secp256k1_context* ctx, size_t max_size) { |
548de42e | 228 | VERIFY_CHECK(ctx != NULL); |
6fe50439 | 229 | return secp256k1_scratch_create(&ctx->error_callback, max_size); |
548de42e AP |
230 | } |
231 | ||
c2b028a2 AP |
232 | void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space* scratch) { |
233 | VERIFY_CHECK(ctx != NULL); | |
234 | secp256k1_scratch_destroy(&ctx->error_callback, scratch); | |
548de42e AP |
235 | } |
236 | ||
7b50483a GM |
237 | /* Mark memory as no-longer-secret for the purpose of analysing constant-time behaviour |
238 | * of the software. This is setup for use with valgrind but could be substituted with | |
239 | * the appropriate instrumentation for other analysis tools. | |
240 | */ | |
3e08b02e | 241 | static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context* ctx, const void *p, size_t len) { |
7b50483a GM |
242 | #if defined(VALGRIND) |
243 | if (EXPECT(ctx->declassify,0)) VALGRIND_MAKE_MEM_DEFINED(p, len); | |
244 | #else | |
245 | (void)ctx; | |
246 | (void)p; | |
247 | (void)len; | |
248 | #endif | |
249 | } | |
250 | ||
dd891e0e PW |
251 | static int secp256k1_pubkey_load(const secp256k1_context* ctx, secp256k1_ge* ge, const secp256k1_pubkey* pubkey) { |
252 | if (sizeof(secp256k1_ge_storage) == 64) { | |
253 | /* When the secp256k1_ge_storage type is exactly 64 byte, use its | |
254 | * representation inside secp256k1_pubkey, as conversion is very fast. | |
23cfa914 | 255 | * Note that secp256k1_pubkey_save must use the same representation. */ |
dd891e0e | 256 | secp256k1_ge_storage s; |
c7680e57 | 257 | memcpy(&s, &pubkey->data[0], sizeof(s)); |
23cfa914 | 258 | secp256k1_ge_from_storage(ge, &s); |
23cfa914 PW |
259 | } else { |
260 | /* Otherwise, fall back to 32-byte big endian for X and Y. */ | |
dd891e0e | 261 | secp256k1_fe x, y; |
23cfa914 | 262 | secp256k1_fe_set_b32(&x, pubkey->data); |
23cfa914 PW |
263 | secp256k1_fe_set_b32(&y, pubkey->data + 32); |
264 | secp256k1_ge_set_xy(ge, &x, &y); | |
265 | } | |
995c5487 PW |
266 | ARG_CHECK(!secp256k1_fe_is_zero(&ge->x)); |
267 | return 1; | |
23cfa914 PW |
268 | } |
269 | ||
dd891e0e PW |
270 | static void secp256k1_pubkey_save(secp256k1_pubkey* pubkey, secp256k1_ge* ge) { |
271 | if (sizeof(secp256k1_ge_storage) == 64) { | |
272 | secp256k1_ge_storage s; | |
23cfa914 | 273 | secp256k1_ge_to_storage(&s, ge); |
c7680e57 | 274 | memcpy(&pubkey->data[0], &s, sizeof(s)); |
23cfa914 PW |
275 | } else { |
276 | VERIFY_CHECK(!secp256k1_ge_is_infinity(ge)); | |
277 | secp256k1_fe_normalize_var(&ge->x); | |
278 | secp256k1_fe_normalize_var(&ge->y); | |
279 | secp256k1_fe_get_b32(pubkey->data, &ge->x); | |
280 | secp256k1_fe_get_b32(pubkey->data + 32, &ge->y); | |
281 | } | |
282 | } | |
283 | ||
dd891e0e PW |
284 | int secp256k1_ec_pubkey_parse(const secp256k1_context* ctx, secp256k1_pubkey* pubkey, const unsigned char *input, size_t inputlen) { |
285 | secp256k1_ge Q; | |
23cfa914 | 286 | |
ee2cb400 GM |
287 | VERIFY_CHECK(ctx != NULL); |
288 | ARG_CHECK(pubkey != NULL); | |
289 | memset(pubkey, 0, sizeof(*pubkey)); | |
290 | ARG_CHECK(input != NULL); | |
23cfa914 | 291 | if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) { |
23cfa914 PW |
292 | return 0; |
293 | } | |
08d7d892 PW |
294 | if (!secp256k1_ge_is_in_correct_subgroup(&Q)) { |
295 | return 0; | |
296 | } | |
23cfa914 PW |
297 | secp256k1_pubkey_save(pubkey, &Q); |
298 | secp256k1_ge_clear(&Q); | |
299 | return 1; | |
300 | } | |
301 | ||
dd891e0e PW |
302 | int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey* pubkey, unsigned int flags) { |
303 | secp256k1_ge Q; | |
5b71a3f4 GM |
304 | size_t len; |
305 | int ret = 0; | |
23cfa914 | 306 | |
ee2cb400 | 307 | VERIFY_CHECK(ctx != NULL); |
ee2cb400 | 308 | ARG_CHECK(outputlen != NULL); |
1309c03c | 309 | ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33u : 65u)); |
5b71a3f4 GM |
310 | len = *outputlen; |
311 | *outputlen = 0; | |
312 | ARG_CHECK(output != NULL); | |
313 | memset(output, 0, len); | |
ee2cb400 | 314 | ARG_CHECK(pubkey != NULL); |
9234391e | 315 | ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION); |
5b71a3f4 GM |
316 | if (secp256k1_pubkey_load(ctx, &Q, pubkey)) { |
317 | ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION); | |
318 | if (ret) { | |
319 | *outputlen = len; | |
320 | } | |
321 | } | |
322 | return ret; | |
23cfa914 PW |
323 | } |
324 | ||
dd891e0e | 325 | static void secp256k1_ecdsa_signature_load(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, const secp256k1_ecdsa_signature* sig) { |
439d34ad | 326 | (void)ctx; |
dd891e0e PW |
327 | if (sizeof(secp256k1_scalar) == 32) { |
328 | /* When the secp256k1_scalar type is exactly 32 byte, use its | |
329 | * representation inside secp256k1_ecdsa_signature, as conversion is very fast. | |
439d34ad PW |
330 | * Note that secp256k1_ecdsa_signature_save must use the same representation. */ |
331 | memcpy(r, &sig->data[0], 32); | |
332 | memcpy(s, &sig->data[32], 32); | |
333 | } else { | |
334 | secp256k1_scalar_set_b32(r, &sig->data[0], NULL); | |
335 | secp256k1_scalar_set_b32(s, &sig->data[32], NULL); | |
336 | } | |
337 | } | |
338 | ||
dd891e0e PW |
339 | static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature* sig, const secp256k1_scalar* r, const secp256k1_scalar* s) { |
340 | if (sizeof(secp256k1_scalar) == 32) { | |
439d34ad PW |
341 | memcpy(&sig->data[0], r, 32); |
342 | memcpy(&sig->data[32], s, 32); | |
343 | } else { | |
344 | secp256k1_scalar_get_b32(&sig->data[0], r); | |
345 | secp256k1_scalar_get_b32(&sig->data[32], s); | |
346 | } | |
347 | } | |
348 | ||
dd891e0e PW |
349 | int secp256k1_ecdsa_signature_parse_der(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) { |
350 | secp256k1_scalar r, s; | |
74a2acdb | 351 | |
bcc4881d | 352 | VERIFY_CHECK(ctx != NULL); |
995c5487 PW |
353 | ARG_CHECK(sig != NULL); |
354 | ARG_CHECK(input != NULL); | |
74a2acdb | 355 | |
18c329c5 | 356 | if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) { |
439d34ad | 357 | secp256k1_ecdsa_signature_save(sig, &r, &s); |
74a2acdb PW |
358 | return 1; |
359 | } else { | |
360 | memset(sig, 0, sizeof(*sig)); | |
361 | return 0; | |
362 | } | |
363 | } | |
364 | ||
3bb9c447 PW |
365 | int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input64) { |
366 | secp256k1_scalar r, s; | |
367 | int ret = 1; | |
368 | int overflow = 0; | |
369 | ||
bcc4881d | 370 | VERIFY_CHECK(ctx != NULL); |
3bb9c447 PW |
371 | ARG_CHECK(sig != NULL); |
372 | ARG_CHECK(input64 != NULL); | |
373 | ||
374 | secp256k1_scalar_set_b32(&r, &input64[0], &overflow); | |
375 | ret &= !overflow; | |
376 | secp256k1_scalar_set_b32(&s, &input64[32], &overflow); | |
377 | ret &= !overflow; | |
378 | if (ret) { | |
379 | secp256k1_ecdsa_signature_save(sig, &r, &s); | |
380 | } else { | |
381 | memset(sig, 0, sizeof(*sig)); | |
382 | } | |
383 | return ret; | |
384 | } | |
385 | ||
dd891e0e PW |
386 | int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context* ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature* sig) { |
387 | secp256k1_scalar r, s; | |
74a2acdb | 388 | |
bcc4881d | 389 | VERIFY_CHECK(ctx != NULL); |
995c5487 PW |
390 | ARG_CHECK(output != NULL); |
391 | ARG_CHECK(outputlen != NULL); | |
392 | ARG_CHECK(sig != NULL); | |
74a2acdb | 393 | |
439d34ad | 394 | secp256k1_ecdsa_signature_load(ctx, &r, &s, sig); |
18c329c5 | 395 | return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s); |
74a2acdb PW |
396 | } |
397 | ||
3bb9c447 PW |
398 | int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context* ctx, unsigned char *output64, const secp256k1_ecdsa_signature* sig) { |
399 | secp256k1_scalar r, s; | |
400 | ||
bcc4881d | 401 | VERIFY_CHECK(ctx != NULL); |
3bb9c447 PW |
402 | ARG_CHECK(output64 != NULL); |
403 | ARG_CHECK(sig != NULL); | |
404 | ||
405 | secp256k1_ecdsa_signature_load(ctx, &r, &s, sig); | |
406 | secp256k1_scalar_get_b32(&output64[0], &r); | |
407 | secp256k1_scalar_get_b32(&output64[32], &s); | |
408 | return 1; | |
409 | } | |
410 | ||
0c6ab2ff PW |
411 | int secp256k1_ecdsa_signature_normalize(const secp256k1_context* ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) { |
412 | secp256k1_scalar r, s; | |
413 | int ret = 0; | |
414 | ||
415 | VERIFY_CHECK(ctx != NULL); | |
416 | ARG_CHECK(sigin != NULL); | |
417 | ||
418 | secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin); | |
419 | ret = secp256k1_scalar_is_high(&s); | |
420 | if (sigout != NULL) { | |
421 | if (ret) { | |
422 | secp256k1_scalar_negate(&s, &s); | |
423 | } | |
424 | secp256k1_ecdsa_signature_save(sigout, &r, &s); | |
425 | } | |
426 | ||
427 | return ret; | |
428 | } | |
429 | ||
f587f04e | 430 | int secp256k1_ecdsa_verify(const secp256k1_context* ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) { |
dd891e0e PW |
431 | secp256k1_ge q; |
432 | secp256k1_scalar r, s; | |
433 | secp256k1_scalar m; | |
b183b411 | 434 | VERIFY_CHECK(ctx != NULL); |
995c5487 | 435 | ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)); |
f587f04e | 436 | ARG_CHECK(msghash32 != NULL); |
995c5487 PW |
437 | ARG_CHECK(sig != NULL); |
438 | ARG_CHECK(pubkey != NULL); | |
1c7fa133 | 439 | |
f587f04e | 440 | secp256k1_scalar_set_b32(&m, msghash32, NULL); |
439d34ad | 441 | secp256k1_ecdsa_signature_load(ctx, &r, &s, sig); |
0c6ab2ff PW |
442 | return (!secp256k1_scalar_is_high(&s) && |
443 | secp256k1_pubkey_load(ctx, &q, pubkey) && | |
995c5487 | 444 | secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m)); |
607884fc PW |
445 | } |
446 | ||
c7680e57 TS |
447 | static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *offset, const void *data, unsigned int len) { |
448 | memcpy(buf + *offset, data, len); | |
449 | *offset += len; | |
450 | } | |
451 | ||
05732c5a | 452 | static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { |
a5a66c70 | 453 | unsigned char keydata[112]; |
c7680e57 | 454 | unsigned int offset = 0; |
d1dc9dfc | 455 | secp256k1_rfc6979_hmac_sha256 rng; |
f735446c | 456 | unsigned int i; |
3e6f1e20 PW |
457 | /* We feed a byte array to the PRNG as input, consisting of: |
458 | * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d. | |
459 | * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data. | |
b30fc85c GM |
460 | * - optionally 16 extra bytes with the algorithm name. |
461 | * Because the arguments have distinct fixed lengths it is not possible for | |
462 | * different argument mixtures to emulate each other and result in the same | |
463 | * nonces. | |
3e6f1e20 | 464 | */ |
c7680e57 TS |
465 | buffer_append(keydata, &offset, key32, 32); |
466 | buffer_append(keydata, &offset, msg32, 32); | |
3e6f1e20 | 467 | if (data != NULL) { |
c7680e57 | 468 | buffer_append(keydata, &offset, data, 32); |
3e6f1e20 | 469 | } |
a5a66c70 | 470 | if (algo16 != NULL) { |
c7680e57 | 471 | buffer_append(keydata, &offset, algo16, 16); |
a5a66c70 | 472 | } |
c7680e57 | 473 | secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset); |
3e6f1e20 | 474 | memset(keydata, 0, sizeof(keydata)); |
f735446c | 475 | for (i = 0; i <= counter; i++) { |
bbd5ba7c PW |
476 | secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32); |
477 | } | |
478 | secp256k1_rfc6979_hmac_sha256_finalize(&rng); | |
479 | return 1; | |
480 | } | |
481 | ||
dd891e0e PW |
482 | const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979; |
483 | const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979; | |
bbd5ba7c | 484 | |
2876af4f | 485 | static int secp256k1_ecdsa_sign_inner(const secp256k1_context* ctx, secp256k1_scalar* r, secp256k1_scalar* s, int* recid, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) { |
dd891e0e | 486 | secp256k1_scalar sec, non, msg; |
439d34ad | 487 | int ret = 0; |
3fec9826 | 488 | int is_sec_valid; |
34a67c77 GM |
489 | unsigned char nonce32[32]; |
490 | unsigned int count = 0; | |
2876af4f ET |
491 | /* Default initialization here is important so we won't pass uninit values to the cmov in the end */ |
492 | *r = secp256k1_scalar_zero; | |
493 | *s = secp256k1_scalar_zero; | |
494 | if (recid) { | |
495 | *recid = 0; | |
496 | } | |
439d34ad PW |
497 | if (noncefp == NULL) { |
498 | noncefp = secp256k1_nonce_function_default; | |
499 | } | |
500 | ||
439d34ad | 501 | /* Fail if the secret key is invalid. */ |
3fec9826 JN |
502 | is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey); |
503 | secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid); | |
34a67c77 GM |
504 | secp256k1_scalar_set_b32(&msg, msg32, NULL); |
505 | while (1) { | |
3fec9826 JN |
506 | int is_nonce_valid; |
507 | ret = !!noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count); | |
34a67c77 GM |
508 | if (!ret) { |
509 | break; | |
510 | } | |
3fec9826 JN |
511 | is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32); |
512 | /* The nonce is still secret here, but it being invalid is is less likely than 1:2^255. */ | |
513 | secp256k1_declassify(ctx, &is_nonce_valid, sizeof(is_nonce_valid)); | |
514 | if (is_nonce_valid) { | |
2876af4f | 515 | ret = secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, r, s, &sec, &msg, &non, recid); |
7b50483a GM |
516 | /* The final signature is no longer a secret, nor is the fact that we were successful or not. */ |
517 | secp256k1_declassify(ctx, &ret, sizeof(ret)); | |
518 | if (ret) { | |
439d34ad PW |
519 | break; |
520 | } | |
439d34ad | 521 | } |
34a67c77 | 522 | count++; |
439d34ad | 523 | } |
3fec9826 JN |
524 | /* We don't want to declassify is_sec_valid and therefore the range of |
525 | * seckey. As a result is_sec_valid is included in ret only after ret was | |
526 | * used as a branching variable. */ | |
527 | ret &= is_sec_valid; | |
34a67c77 GM |
528 | memset(nonce32, 0, 32); |
529 | secp256k1_scalar_clear(&msg); | |
530 | secp256k1_scalar_clear(&non); | |
531 | secp256k1_scalar_clear(&sec); | |
2876af4f ET |
532 | secp256k1_scalar_cmov(r, &secp256k1_scalar_zero, !ret); |
533 | secp256k1_scalar_cmov(s, &secp256k1_scalar_zero, !ret); | |
534 | if (recid) { | |
535 | const int zero = 0; | |
536 | secp256k1_int_cmov(recid, &zero, !ret); | |
537 | } | |
538 | return ret; | |
539 | } | |
540 | ||
f587f04e | 541 | int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void* noncedata) { |
2876af4f ET |
542 | secp256k1_scalar r, s; |
543 | int ret; | |
544 | VERIFY_CHECK(ctx != NULL); | |
545 | ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); | |
f587f04e | 546 | ARG_CHECK(msghash32 != NULL); |
2876af4f ET |
547 | ARG_CHECK(signature != NULL); |
548 | ARG_CHECK(seckey != NULL); | |
549 | ||
f587f04e | 550 | ret = secp256k1_ecdsa_sign_inner(ctx, &r, &s, NULL, msghash32, seckey, noncefp, noncedata); |
34a67c77 | 551 | secp256k1_ecdsa_signature_save(signature, &r, &s); |
3fec9826 | 552 | return ret; |
439d34ad PW |
553 | } |
554 | ||
dd891e0e PW |
555 | int secp256k1_ec_seckey_verify(const secp256k1_context* ctx, const unsigned char *seckey) { |
556 | secp256k1_scalar sec; | |
f735446c | 557 | int ret; |
b183b411 | 558 | VERIFY_CHECK(ctx != NULL); |
995c5487 | 559 | ARG_CHECK(seckey != NULL); |
f735446c | 560 | |
3fec9826 | 561 | ret = secp256k1_scalar_set_b32_seckey(&sec, seckey); |
a9f5c8b8 | 562 | secp256k1_scalar_clear(&sec); |
42cccdaf PW |
563 | return ret; |
564 | } | |
565 | ||
f0010349 | 566 | static int secp256k1_ec_pubkey_create_helper(const secp256k1_ecmult_gen_context *ecmult_gen_ctx, secp256k1_scalar *seckey_scalar, secp256k1_ge *p, const unsigned char *seckey) { |
dd891e0e | 567 | secp256k1_gej pj; |
f0010349 JN |
568 | int ret; |
569 | ||
570 | ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey); | |
571 | secp256k1_scalar_cmov(seckey_scalar, &secp256k1_scalar_one, !ret); | |
572 | ||
573 | secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar); | |
574 | secp256k1_ge_set_gej(p, &pj); | |
575 | return ret; | |
576 | } | |
577 | ||
578 | int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) { | |
dd891e0e | 579 | secp256k1_ge p; |
f0010349 | 580 | secp256k1_scalar seckey_scalar; |
0065a8fb | 581 | int ret = 0; |
b183b411 | 582 | VERIFY_CHECK(ctx != NULL); |
995c5487 | 583 | ARG_CHECK(pubkey != NULL); |
5b71a3f4 GM |
584 | memset(pubkey, 0, sizeof(*pubkey)); |
585 | ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); | |
995c5487 | 586 | ARG_CHECK(seckey != NULL); |
1c7fa133 | 587 | |
f0010349 | 588 | ret = secp256k1_ec_pubkey_create_helper(&ctx->ecmult_gen_ctx, &seckey_scalar, &p, seckey); |
34a67c77 | 589 | secp256k1_pubkey_save(pubkey, &p); |
e89278f2 | 590 | secp256k1_memczero(pubkey, sizeof(*pubkey), !ret); |
34a67c77 | 591 | |
f0010349 | 592 | secp256k1_scalar_clear(&seckey_scalar); |
99fd963b TK |
593 | return ret; |
594 | } | |
595 | ||
41fc7856 | 596 | int secp256k1_ec_seckey_negate(const secp256k1_context* ctx, unsigned char *seckey) { |
8e48aa60 | 597 | secp256k1_scalar sec; |
5894e1f1 | 598 | int ret = 0; |
8e48aa60 AP |
599 | VERIFY_CHECK(ctx != NULL); |
600 | ARG_CHECK(seckey != NULL); | |
601 | ||
5894e1f1 JN |
602 | ret = secp256k1_scalar_set_b32_seckey(&sec, seckey); |
603 | secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret); | |
8e48aa60 AP |
604 | secp256k1_scalar_negate(&sec, &sec); |
605 | secp256k1_scalar_get_b32(seckey, &sec); | |
606 | ||
069870d9 | 607 | secp256k1_scalar_clear(&sec); |
5894e1f1 | 608 | return ret; |
8e48aa60 AP |
609 | } |
610 | ||
41fc7856 JN |
611 | int secp256k1_ec_privkey_negate(const secp256k1_context* ctx, unsigned char *seckey) { |
612 | return secp256k1_ec_seckey_negate(ctx, seckey); | |
613 | } | |
614 | ||
8e48aa60 AP |
615 | int secp256k1_ec_pubkey_negate(const secp256k1_context* ctx, secp256k1_pubkey *pubkey) { |
616 | int ret = 0; | |
617 | secp256k1_ge p; | |
618 | VERIFY_CHECK(ctx != NULL); | |
619 | ARG_CHECK(pubkey != NULL); | |
620 | ||
621 | ret = secp256k1_pubkey_load(ctx, &p, pubkey); | |
622 | memset(pubkey, 0, sizeof(*pubkey)); | |
623 | if (ret) { | |
624 | secp256k1_ge_neg(&p, &p); | |
625 | secp256k1_pubkey_save(pubkey, &p); | |
626 | } | |
627 | return ret; | |
628 | } | |
629 | ||
f0010349 | 630 | |
6e85d675 | 631 | static int secp256k1_ec_seckey_tweak_add_helper(secp256k1_scalar *sec, const unsigned char *tweak32) { |
dd891e0e | 632 | secp256k1_scalar term; |
f0010349 JN |
633 | int overflow = 0; |
634 | int ret = 0; | |
635 | ||
6e85d675 | 636 | secp256k1_scalar_set_b32(&term, tweak32, &overflow); |
f0010349 JN |
637 | ret = (!overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term); |
638 | secp256k1_scalar_clear(&term); | |
639 | return ret; | |
640 | } | |
641 | ||
6e85d675 | 642 | int secp256k1_ec_seckey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) { |
dd891e0e | 643 | secp256k1_scalar sec; |
0065a8fb | 644 | int ret = 0; |
b183b411 | 645 | VERIFY_CHECK(ctx != NULL); |
995c5487 | 646 | ARG_CHECK(seckey != NULL); |
6e85d675 | 647 | ARG_CHECK(tweak32 != NULL); |
1c7fa133 | 648 | |
5894e1f1 | 649 | ret = secp256k1_scalar_set_b32_seckey(&sec, seckey); |
6e85d675 | 650 | ret &= secp256k1_ec_seckey_tweak_add_helper(&sec, tweak32); |
34a67c77 GM |
651 | secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret); |
652 | secp256k1_scalar_get_b32(seckey, &sec); | |
eb74c36b | 653 | |
a9f5c8b8 | 654 | secp256k1_scalar_clear(&sec); |
561b0e10 PW |
655 | return ret; |
656 | } | |
657 | ||
6e85d675 JN |
658 | int secp256k1_ec_privkey_tweak_add(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) { |
659 | return secp256k1_ec_seckey_tweak_add(ctx, seckey, tweak32); | |
41fc7856 JN |
660 | } |
661 | ||
6e85d675 | 662 | static int secp256k1_ec_pubkey_tweak_add_helper(const secp256k1_ecmult_context* ecmult_ctx, secp256k1_ge *p, const unsigned char *tweak32) { |
176bfb11 JN |
663 | secp256k1_scalar term; |
664 | int overflow = 0; | |
6e85d675 | 665 | secp256k1_scalar_set_b32(&term, tweak32, &overflow); |
176bfb11 JN |
666 | return !overflow && secp256k1_eckey_pubkey_tweak_add(ecmult_ctx, p, &term); |
667 | } | |
668 | ||
6e85d675 | 669 | int secp256k1_ec_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) { |
dd891e0e | 670 | secp256k1_ge p; |
0065a8fb | 671 | int ret = 0; |
b183b411 | 672 | VERIFY_CHECK(ctx != NULL); |
995c5487 PW |
673 | ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)); |
674 | ARG_CHECK(pubkey != NULL); | |
6e85d675 | 675 | ARG_CHECK(tweak32 != NULL); |
1c7fa133 | 676 | |
176bfb11 | 677 | ret = secp256k1_pubkey_load(ctx, &p, pubkey); |
bb5aa4df | 678 | memset(pubkey, 0, sizeof(*pubkey)); |
6e85d675 | 679 | ret = ret && secp256k1_ec_pubkey_tweak_add_helper(&ctx->ecmult_ctx, &p, tweak32); |
bb5aa4df | 680 | if (ret) { |
176bfb11 | 681 | secp256k1_pubkey_save(pubkey, &p); |
86d3cce2 | 682 | } |
eb74c36b | 683 | |
86d3cce2 PW |
684 | return ret; |
685 | } | |
686 | ||
6e85d675 | 687 | int secp256k1_ec_seckey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) { |
dd891e0e PW |
688 | secp256k1_scalar factor; |
689 | secp256k1_scalar sec; | |
0065a8fb | 690 | int ret = 0; |
f735446c | 691 | int overflow = 0; |
b183b411 | 692 | VERIFY_CHECK(ctx != NULL); |
995c5487 | 693 | ARG_CHECK(seckey != NULL); |
6e85d675 | 694 | ARG_CHECK(tweak32 != NULL); |
1c7fa133 | 695 | |
6e85d675 | 696 | secp256k1_scalar_set_b32(&factor, tweak32, &overflow); |
5894e1f1 JN |
697 | ret = secp256k1_scalar_set_b32_seckey(&sec, seckey); |
698 | ret &= (!overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor); | |
34a67c77 GM |
699 | secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !ret); |
700 | secp256k1_scalar_get_b32(seckey, &sec); | |
eb74c36b | 701 | |
a9f5c8b8 PW |
702 | secp256k1_scalar_clear(&sec); |
703 | secp256k1_scalar_clear(&factor); | |
86d3cce2 PW |
704 | return ret; |
705 | } | |
706 | ||
6e85d675 JN |
707 | int secp256k1_ec_privkey_tweak_mul(const secp256k1_context* ctx, unsigned char *seckey, const unsigned char *tweak32) { |
708 | return secp256k1_ec_seckey_tweak_mul(ctx, seckey, tweak32); | |
41fc7856 JN |
709 | } |
710 | ||
6e85d675 | 711 | int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) { |
dd891e0e PW |
712 | secp256k1_ge p; |
713 | secp256k1_scalar factor; | |
0065a8fb | 714 | int ret = 0; |
f735446c | 715 | int overflow = 0; |
b183b411 | 716 | VERIFY_CHECK(ctx != NULL); |
995c5487 PW |
717 | ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx)); |
718 | ARG_CHECK(pubkey != NULL); | |
6e85d675 | 719 | ARG_CHECK(tweak32 != NULL); |
1c7fa133 | 720 | |
6e85d675 | 721 | secp256k1_scalar_set_b32(&factor, tweak32, &overflow); |
bb5aa4df GM |
722 | ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey); |
723 | memset(pubkey, 0, sizeof(*pubkey)); | |
724 | if (ret) { | |
725 | if (secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor)) { | |
23cfa914 PW |
726 | secp256k1_pubkey_save(pubkey, &p); |
727 | } else { | |
bb5aa4df | 728 | ret = 0; |
0065a8fb | 729 | } |
561b0e10 | 730 | } |
eb74c36b | 731 | |
561b0e10 PW |
732 | return ret; |
733 | } | |
734 | ||
dd891e0e | 735 | int secp256k1_context_randomize(secp256k1_context* ctx, const unsigned char *seed32) { |
b183b411 | 736 | VERIFY_CHECK(ctx != NULL); |
61983752 TR |
737 | if (secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)) { |
738 | secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32); | |
739 | } | |
d2275795 GM |
740 | return 1; |
741 | } | |
0739bbb6 | 742 | |
8e48787d GM |
743 | int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) { |
744 | size_t i; | |
dd891e0e PW |
745 | secp256k1_gej Qj; |
746 | secp256k1_ge Q; | |
a5a66c70 PW |
747 | |
748 | ARG_CHECK(pubnonce != NULL); | |
c69dea02 | 749 | memset(pubnonce, 0, sizeof(*pubnonce)); |
a5a66c70 PW |
750 | ARG_CHECK(n >= 1); |
751 | ARG_CHECK(pubnonces != NULL); | |
752 | ||
753 | secp256k1_gej_set_infinity(&Qj); | |
754 | ||
755 | for (i = 0; i < n; i++) { | |
756 | secp256k1_pubkey_load(ctx, &Q, pubnonces[i]); | |
757 | secp256k1_gej_add_ge(&Qj, &Qj, &Q); | |
758 | } | |
759 | if (secp256k1_gej_is_infinity(&Qj)) { | |
a5a66c70 PW |
760 | return 0; |
761 | } | |
762 | secp256k1_ge_set_gej(&Q, &Qj); | |
763 | secp256k1_pubkey_save(pubnonce, &Q); | |
764 | return 1; | |
765 | } | |
766 | ||
0739bbb6 AP |
767 | #ifdef ENABLE_MODULE_ECDH |
768 | # include "modules/ecdh/main_impl.h" | |
769 | #endif | |
a5a66c70 | 770 | |
9f443be0 PW |
771 | #ifdef ENABLE_MODULE_RECOVERY |
772 | # include "modules/recovery/main_impl.h" | |
773 | #endif | |
47e6618e JN |
774 | |
775 | #ifdef ENABLE_MODULE_EXTRAKEYS | |
776 | # include "modules/extrakeys/main_impl.h" | |
777 | #endif | |
7a703fd9 JN |
778 | |
779 | #ifdef ENABLE_MODULE_SCHNORRSIG | |
780 | # include "modules/schnorrsig/main_impl.h" | |
781 | #endif |