]> Git Repo - secp256k1.git/blame_incremental - src/secp256k1.c
Add constant-time multiply `secp256k1_ecmult_const` for ECDH
[secp256k1.git] / src / secp256k1.c
... / ...
CommitLineData
1/**********************************************************************
2 * Copyright (c) 2013-2015 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
6
7#define SECP256K1_BUILD (1)
8
9#include "include/secp256k1.h"
10
11#include "util.h"
12#include "num_impl.h"
13#include "field_impl.h"
14#include "scalar_impl.h"
15#include "group_impl.h"
16#include "ecmult_impl.h"
17#include "ecmult_const_impl.h"
18#include "ecmult_gen_impl.h"
19#include "ecdsa_impl.h"
20#include "eckey_impl.h"
21#include "hash_impl.h"
22
23#define ARG_CHECK(cond) do { \
24 if (EXPECT(!(cond), 0)) { \
25 ctx->illegal_callback.fn(#cond, ctx->illegal_callback.data); \
26 return 0; \
27 } \
28} while(0)
29
30static void default_illegal_callback_fn(const char* str, void* data) {
31 (void)data;
32 fprintf(stderr, "[libsecp256k1] illegal argument: %s\n", str);
33 abort();
34}
35
36static const callback_t default_illegal_callback = {
37 default_illegal_callback_fn,
38 NULL
39};
40
41static void default_error_callback_fn(const char* str, void* data) {
42 (void)data;
43 fprintf(stderr, "[libsecp256k1] internal consistency check failed: %s\n", str);
44 abort();
45}
46
47static const callback_t default_error_callback = {
48 default_error_callback_fn,
49 NULL
50};
51
52
53struct secp256k1_context_struct {
54 secp256k1_ecmult_context_t ecmult_ctx;
55 secp256k1_ecmult_gen_context_t ecmult_gen_ctx;
56 callback_t illegal_callback;
57 callback_t error_callback;
58};
59
60secp256k1_context_t* secp256k1_context_create(int flags) {
61 secp256k1_context_t* ret = (secp256k1_context_t*)checked_malloc(&default_error_callback, sizeof(secp256k1_context_t));
62 ret->illegal_callback = default_illegal_callback;
63 ret->error_callback = default_error_callback;
64
65 secp256k1_ecmult_context_init(&ret->ecmult_ctx);
66 secp256k1_ecmult_gen_context_init(&ret->ecmult_gen_ctx);
67
68 if (flags & SECP256K1_CONTEXT_SIGN) {
69 secp256k1_ecmult_gen_context_build(&ret->ecmult_gen_ctx, &ret->error_callback);
70 }
71 if (flags & SECP256K1_CONTEXT_VERIFY) {
72 secp256k1_ecmult_context_build(&ret->ecmult_ctx, &ret->error_callback);
73 }
74
75 return ret;
76}
77
78secp256k1_context_t* secp256k1_context_clone(const secp256k1_context_t* ctx) {
79 secp256k1_context_t* ret = (secp256k1_context_t*)checked_malloc(&ctx->error_callback, sizeof(secp256k1_context_t));
80 ret->illegal_callback = ctx->illegal_callback;
81 ret->error_callback = ctx->error_callback;
82 secp256k1_ecmult_context_clone(&ret->ecmult_ctx, &ctx->ecmult_ctx, &ctx->error_callback);
83 secp256k1_ecmult_gen_context_clone(&ret->ecmult_gen_ctx, &ctx->ecmult_gen_ctx, &ctx->error_callback);
84 return ret;
85}
86
87void secp256k1_context_destroy(secp256k1_context_t* ctx) {
88 secp256k1_ecmult_context_clear(&ctx->ecmult_ctx);
89 secp256k1_ecmult_gen_context_clear(&ctx->ecmult_gen_ctx);
90
91 free(ctx);
92}
93
94void secp256k1_context_set_illegal_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) {
95 ctx->illegal_callback.fn = fun;
96 ctx->illegal_callback.data = data;
97}
98
99void secp256k1_context_set_error_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) {
100 ctx->error_callback.fn = fun;
101 ctx->error_callback.data = data;
102}
103
104static int secp256k1_pubkey_load(const secp256k1_context_t* ctx, secp256k1_ge_t* ge, const secp256k1_pubkey_t* pubkey) {
105 if (sizeof(secp256k1_ge_storage_t) == 64) {
106 /* When the secp256k1_ge_storage_t type is exactly 64 byte, use its
107 * representation inside secp256k1_pubkey_t, as conversion is very fast.
108 * Note that secp256k1_pubkey_save must use the same representation. */
109 secp256k1_ge_storage_t s;
110 memcpy(&s, &pubkey->data[0], 64);
111 secp256k1_ge_from_storage(ge, &s);
112 } else {
113 /* Otherwise, fall back to 32-byte big endian for X and Y. */
114 secp256k1_fe_t x, y;
115 secp256k1_fe_set_b32(&x, pubkey->data);
116 secp256k1_fe_set_b32(&y, pubkey->data + 32);
117 secp256k1_ge_set_xy(ge, &x, &y);
118 }
119 ARG_CHECK(!secp256k1_fe_is_zero(&ge->x));
120 return 1;
121}
122
123static void secp256k1_pubkey_save(secp256k1_pubkey_t* pubkey, secp256k1_ge_t* ge) {
124 if (sizeof(secp256k1_ge_storage_t) == 64) {
125 secp256k1_ge_storage_t s;
126 secp256k1_ge_to_storage(&s, ge);
127 memcpy(&pubkey->data[0], &s, 64);
128 } else {
129 VERIFY_CHECK(!secp256k1_ge_is_infinity(ge));
130 secp256k1_fe_normalize_var(&ge->x);
131 secp256k1_fe_normalize_var(&ge->y);
132 secp256k1_fe_get_b32(pubkey->data, &ge->x);
133 secp256k1_fe_get_b32(pubkey->data + 32, &ge->y);
134 }
135}
136
137int secp256k1_ec_pubkey_parse(const secp256k1_context_t* ctx, secp256k1_pubkey_t* pubkey, const unsigned char *input, int inputlen) {
138 secp256k1_ge_t Q;
139
140 (void)ctx;
141 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
142 memset(pubkey, 0, sizeof(*pubkey));
143 return 0;
144 }
145 secp256k1_pubkey_save(pubkey, &Q);
146 secp256k1_ge_clear(&Q);
147 return 1;
148}
149
150int secp256k1_ec_pubkey_serialize(const secp256k1_context_t* ctx, unsigned char *output, int *outputlen, const secp256k1_pubkey_t* pubkey, int compressed) {
151 secp256k1_ge_t Q;
152
153 (void)ctx;
154 return (secp256k1_pubkey_load(ctx, &Q, pubkey) &&
155 secp256k1_eckey_pubkey_serialize(&Q, output, outputlen, compressed));
156}
157
158static void secp256k1_ecdsa_signature_load(const secp256k1_context_t* ctx, secp256k1_scalar_t* r, secp256k1_scalar_t* s, int* recid, const secp256k1_ecdsa_signature_t* sig) {
159 (void)ctx;
160 if (sizeof(secp256k1_scalar_t) == 32) {
161 /* When the secp256k1_scalar_t type is exactly 32 byte, use its
162 * representation inside secp256k1_ecdsa_signature_t, as conversion is very fast.
163 * Note that secp256k1_ecdsa_signature_save must use the same representation. */
164 memcpy(r, &sig->data[0], 32);
165 memcpy(s, &sig->data[32], 32);
166 } else {
167 secp256k1_scalar_set_b32(r, &sig->data[0], NULL);
168 secp256k1_scalar_set_b32(s, &sig->data[32], NULL);
169 }
170 if (recid) {
171 *recid = sig->data[64];
172 }
173}
174
175static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature_t* sig, const secp256k1_scalar_t* r, const secp256k1_scalar_t* s, int recid) {
176 if (sizeof(secp256k1_scalar_t) == 32) {
177 memcpy(&sig->data[0], r, 32);
178 memcpy(&sig->data[32], s, 32);
179 } else {
180 secp256k1_scalar_get_b32(&sig->data[0], r);
181 secp256k1_scalar_get_b32(&sig->data[32], s);
182 }
183 sig->data[64] = recid;
184}
185
186int secp256k1_ecdsa_signature_parse_der(const secp256k1_context_t* ctx, secp256k1_ecdsa_signature_t* sig, const unsigned char *input, int inputlen) {
187 secp256k1_scalar_t r, s;
188
189 (void)ctx;
190 ARG_CHECK(sig != NULL);
191 ARG_CHECK(input != NULL);
192
193 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
194 secp256k1_ecdsa_signature_save(sig, &r, &s, -1);
195 return 1;
196 } else {
197 memset(sig, 0, sizeof(*sig));
198 return 0;
199 }
200}
201
202int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context_t* ctx, secp256k1_ecdsa_signature_t* sig, const unsigned char *input64, int recid) {
203 secp256k1_scalar_t r, s;
204 int ret = 1;
205 int overflow = 0;
206
207 (void)ctx;
208 ARG_CHECK(sig != NULL);
209 ARG_CHECK(input64 != NULL);
210
211 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
212 ret &= !overflow;
213 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
214 ret &= !overflow;
215 ret &= (recid == -1 || (recid >= 0 && recid < 4));
216 if (ret) {
217 secp256k1_ecdsa_signature_save(sig, &r, &s, recid);
218 } else {
219 memset(sig, 0, sizeof(*sig));
220 }
221 return ret;
222}
223
224int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context_t* ctx, unsigned char *output, int *outputlen, const secp256k1_ecdsa_signature_t* sig) {
225 secp256k1_scalar_t r, s;
226
227 (void)ctx;
228 ARG_CHECK(output != NULL);
229 ARG_CHECK(outputlen != NULL);
230 ARG_CHECK(sig != NULL);
231
232 secp256k1_ecdsa_signature_load(ctx, &r, &s, NULL, sig);
233 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
234}
235
236int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context_t* ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_signature_t* sig) {
237 secp256k1_scalar_t r, s;
238 int rec;
239
240 (void)ctx;
241 ARG_CHECK(output64 != NULL);
242 ARG_CHECK(sig != NULL);
243
244 secp256k1_ecdsa_signature_load(ctx, &r, &s, &rec, sig);
245 secp256k1_scalar_get_b32(&output64[0], &r);
246 secp256k1_scalar_get_b32(&output64[32], &s);
247 if (recid) {
248 ARG_CHECK(rec >= 0 && rec < 4);
249 *recid = rec;
250 }
251 return 1;
252}
253
254int secp256k1_ecdsa_verify(const secp256k1_context_t* ctx, const unsigned char *msg32, const secp256k1_ecdsa_signature_t *sig, const secp256k1_pubkey_t *pubkey) {
255 secp256k1_ge_t q;
256 secp256k1_scalar_t r, s;
257 secp256k1_scalar_t m;
258 ARG_CHECK(ctx != NULL);
259 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
260 ARG_CHECK(msg32 != NULL);
261 ARG_CHECK(sig != NULL);
262 ARG_CHECK(pubkey != NULL);
263
264 secp256k1_scalar_set_b32(&m, msg32, NULL);
265 secp256k1_ecdsa_signature_load(ctx, &r, &s, NULL, sig);
266 return (secp256k1_pubkey_load(ctx, &q, pubkey) &&
267 secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
268}
269
270static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, unsigned int counter, const void *data) {
271 unsigned char keydata[96];
272 secp256k1_rfc6979_hmac_sha256_t rng;
273 unsigned int i;
274 /* We feed a byte array to the PRNG as input, consisting of:
275 * - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
276 * - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
277 */
278 memcpy(keydata, key32, 32);
279 memcpy(keydata + 32, msg32, 32);
280 if (data != NULL) {
281 memcpy(keydata + 64, data, 32);
282 }
283 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, data != NULL ? 96 : 64);
284 memset(keydata, 0, sizeof(keydata));
285 for (i = 0; i <= counter; i++) {
286 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
287 }
288 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
289 return 1;
290}
291
292const secp256k1_nonce_function_t secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979;
293const secp256k1_nonce_function_t secp256k1_nonce_function_default = nonce_function_rfc6979;
294
295int secp256k1_ecdsa_sign(const secp256k1_context_t* ctx, const unsigned char *msg32, secp256k1_ecdsa_signature_t *signature, const unsigned char *seckey, secp256k1_nonce_function_t noncefp, const void* noncedata) {
296 secp256k1_scalar_t r, s;
297 secp256k1_scalar_t sec, non, msg;
298 int recid;
299 int ret = 0;
300 int overflow = 0;
301 unsigned int count = 0;
302 ARG_CHECK(ctx != NULL);
303 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
304 ARG_CHECK(msg32 != NULL);
305 ARG_CHECK(signature != NULL);
306 ARG_CHECK(seckey != NULL);
307 if (noncefp == NULL) {
308 noncefp = secp256k1_nonce_function_default;
309 }
310
311 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
312 /* Fail if the secret key is invalid. */
313 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
314 secp256k1_scalar_set_b32(&msg, msg32, NULL);
315 while (1) {
316 unsigned char nonce32[32];
317 ret = noncefp(nonce32, msg32, seckey, count, noncedata);
318 if (!ret) {
319 break;
320 }
321 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
322 memset(nonce32, 0, 32);
323 if (!secp256k1_scalar_is_zero(&non) && !overflow) {
324 if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, &recid)) {
325 break;
326 }
327 }
328 count++;
329 }
330 secp256k1_scalar_clear(&msg);
331 secp256k1_scalar_clear(&non);
332 secp256k1_scalar_clear(&sec);
333 }
334 if (ret) {
335 secp256k1_ecdsa_signature_save(signature, &r, &s, recid);
336 } else {
337 memset(signature, 0, sizeof(*signature));
338 }
339 return ret;
340}
341
342int secp256k1_ecdsa_recover(const secp256k1_context_t* ctx, const unsigned char *msg32, const secp256k1_ecdsa_signature_t *signature, secp256k1_pubkey_t *pubkey) {
343 secp256k1_ge_t q;
344 secp256k1_scalar_t r, s;
345 secp256k1_scalar_t m;
346 int recid;
347 ARG_CHECK(ctx != NULL);
348 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
349 ARG_CHECK(msg32 != NULL);
350 ARG_CHECK(signature != NULL);
351 ARG_CHECK(pubkey != NULL);
352
353 secp256k1_ecdsa_signature_load(ctx, &r, &s, &recid, signature);
354 ARG_CHECK(recid >= 0 && recid < 4);
355 secp256k1_scalar_set_b32(&m, msg32, NULL);
356 if (secp256k1_ecdsa_sig_recover(&ctx->ecmult_ctx, &r, &s, &q, &m, recid)) {
357 secp256k1_pubkey_save(pubkey, &q);
358 return 1;
359 } else {
360 memset(pubkey, 0, sizeof(*pubkey));
361 return 0;
362 }
363}
364
365int secp256k1_ec_seckey_verify(const secp256k1_context_t* ctx, const unsigned char *seckey) {
366 secp256k1_scalar_t sec;
367 int ret;
368 int overflow;
369 ARG_CHECK(ctx != NULL);
370 ARG_CHECK(seckey != NULL);
371 (void)ctx;
372
373 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
374 ret = !secp256k1_scalar_is_zero(&sec) && !overflow;
375 secp256k1_scalar_clear(&sec);
376 return ret;
377}
378
379int secp256k1_ec_pubkey_create(const secp256k1_context_t* ctx, secp256k1_pubkey_t *pubkey, const unsigned char *seckey) {
380 secp256k1_gej_t pj;
381 secp256k1_ge_t p;
382 secp256k1_scalar_t sec;
383 int overflow;
384 int ret = 0;
385 ARG_CHECK(ctx != NULL);
386 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
387 ARG_CHECK(pubkey != NULL);
388 ARG_CHECK(seckey != NULL);
389
390 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
391 ret = !overflow & !secp256k1_scalar_is_zero(&sec);
392 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
393 secp256k1_ge_set_gej(&p, &pj);
394 secp256k1_pubkey_save(pubkey, &p);
395 secp256k1_scalar_clear(&sec);
396 if (!ret) {
397 memset(pubkey, 0, sizeof(*pubkey));
398 }
399 return ret;
400}
401
402int secp256k1_ec_privkey_tweak_add(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *tweak) {
403 secp256k1_scalar_t term;
404 secp256k1_scalar_t sec;
405 int ret = 0;
406 int overflow = 0;
407 ARG_CHECK(ctx != NULL);
408 ARG_CHECK(seckey != NULL);
409 ARG_CHECK(tweak != NULL);
410 (void)ctx;
411
412 secp256k1_scalar_set_b32(&term, tweak, &overflow);
413 secp256k1_scalar_set_b32(&sec, seckey, NULL);
414
415 ret = secp256k1_eckey_privkey_tweak_add(&sec, &term) && !overflow;
416 if (ret) {
417 secp256k1_scalar_get_b32(seckey, &sec);
418 }
419
420 secp256k1_scalar_clear(&sec);
421 secp256k1_scalar_clear(&term);
422 return ret;
423}
424
425int secp256k1_ec_pubkey_tweak_add(const secp256k1_context_t* ctx, secp256k1_pubkey_t *pubkey, const unsigned char *tweak) {
426 secp256k1_ge_t p;
427 secp256k1_scalar_t term;
428 int ret = 0;
429 int overflow = 0;
430 ARG_CHECK(ctx != NULL);
431 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
432 ARG_CHECK(pubkey != NULL);
433 ARG_CHECK(tweak != NULL);
434
435 secp256k1_scalar_set_b32(&term, tweak, &overflow);
436 if (!overflow && secp256k1_pubkey_load(ctx, &p, pubkey)) {
437 ret = secp256k1_eckey_pubkey_tweak_add(&ctx->ecmult_ctx, &p, &term);
438 if (ret) {
439 secp256k1_pubkey_save(pubkey, &p);
440 } else {
441 memset(pubkey, 0, sizeof(*pubkey));
442 }
443 }
444
445 return ret;
446}
447
448int secp256k1_ec_privkey_tweak_mul(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *tweak) {
449 secp256k1_scalar_t factor;
450 secp256k1_scalar_t sec;
451 int ret = 0;
452 int overflow = 0;
453 ARG_CHECK(ctx != NULL);
454 ARG_CHECK(seckey != NULL);
455 ARG_CHECK(tweak != NULL);
456 (void)ctx;
457
458 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
459 secp256k1_scalar_set_b32(&sec, seckey, NULL);
460 ret = secp256k1_eckey_privkey_tweak_mul(&sec, &factor) && !overflow;
461 if (ret) {
462 secp256k1_scalar_get_b32(seckey, &sec);
463 }
464
465 secp256k1_scalar_clear(&sec);
466 secp256k1_scalar_clear(&factor);
467 return ret;
468}
469
470int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context_t* ctx, secp256k1_pubkey_t *pubkey, const unsigned char *tweak) {
471 secp256k1_ge_t p;
472 secp256k1_scalar_t factor;
473 int ret = 0;
474 int overflow = 0;
475 ARG_CHECK(ctx != NULL);
476 ARG_CHECK(secp256k1_ecmult_context_is_built(&ctx->ecmult_ctx));
477 ARG_CHECK(pubkey != NULL);
478 ARG_CHECK(tweak != NULL);
479
480 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
481 if (!overflow && secp256k1_pubkey_load(ctx, &p, pubkey)) {
482 ret = secp256k1_eckey_pubkey_tweak_mul(&ctx->ecmult_ctx, &p, &factor);
483 if (ret) {
484 secp256k1_pubkey_save(pubkey, &p);
485 } else {
486 memset(pubkey, 0, sizeof(*pubkey));
487 }
488 }
489
490 return ret;
491}
492
493int secp256k1_ec_privkey_export(const secp256k1_context_t* ctx, const unsigned char *seckey, unsigned char *privkey, int *privkeylen, int compressed) {
494 secp256k1_scalar_t key;
495 int ret = 0;
496 ARG_CHECK(seckey != NULL);
497 ARG_CHECK(privkey != NULL);
498 ARG_CHECK(privkeylen != NULL);
499 ARG_CHECK(ctx != NULL);
500 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
501
502 secp256k1_scalar_set_b32(&key, seckey, NULL);
503 ret = secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, privkeylen, &key, compressed);
504 secp256k1_scalar_clear(&key);
505 return ret;
506}
507
508int secp256k1_ec_privkey_import(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *privkey, int privkeylen) {
509 secp256k1_scalar_t key;
510 int ret = 0;
511 ARG_CHECK(seckey != NULL);
512 ARG_CHECK(privkey != NULL);
513 (void)ctx;
514
515 ret = secp256k1_eckey_privkey_parse(&key, privkey, privkeylen);
516 if (ret) {
517 secp256k1_scalar_get_b32(seckey, &key);
518 }
519 secp256k1_scalar_clear(&key);
520 return ret;
521}
522
523int secp256k1_context_randomize(secp256k1_context_t* ctx, const unsigned char *seed32) {
524 ARG_CHECK(ctx != NULL);
525 ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
526 secp256k1_ecmult_gen_blind(&ctx->ecmult_gen_ctx, seed32);
527 return 1;
528}
This page took 0.027557 seconds and 4 git commands to generate.