Dash Core  0.12.2.1
P2P Digital Currency
ecdsa_impl.h
Go to the documentation of this file.
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 
8 #ifndef _SECP256K1_ECDSA_IMPL_H_
9 #define _SECP256K1_ECDSA_IMPL_H_
10 
11 #include "scalar.h"
12 #include "field.h"
13 #include "group.h"
14 #include "ecmult.h"
15 #include "ecmult_gen.h"
16 #include "ecdsa.h"
17 
32  0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
33  0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL
34 );
35 
46  0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL
47 );
48 
49 static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned char *sigend) {
50  int lenleft, b1;
51  size_t ret = 0;
52  if (*sigp >= sigend) {
53  return -1;
54  }
55  b1 = *((*sigp)++);
56  if (b1 == 0xFF) {
57  /* X.690-0207 8.1.3.5.c the value 0xFF shall not be used. */
58  return -1;
59  }
60  if ((b1 & 0x80) == 0) {
61  /* X.690-0207 8.1.3.4 short form length octets */
62  return b1;
63  }
64  if (b1 == 0x80) {
65  /* Indefinite length is not allowed in DER. */
66  return -1;
67  }
68  /* X.690-207 8.1.3.5 long form length octets */
69  lenleft = b1 & 0x7F;
70  if (lenleft > sigend - *sigp) {
71  return -1;
72  }
73  if (**sigp == 0) {
74  /* Not the shortest possible length encoding. */
75  return -1;
76  }
77  if ((size_t)lenleft > sizeof(size_t)) {
78  /* The resulting length would exceed the range of a size_t, so
79  * certainly longer than the passed array size.
80  */
81  return -1;
82  }
83  while (lenleft > 0) {
84  if ((ret >> ((sizeof(size_t) - 1) * 8)) != 0) {
85  }
86  ret = (ret << 8) | **sigp;
87  if (ret + lenleft > (size_t)(sigend - *sigp)) {
88  /* Result exceeds the length of the passed array. */
89  return -1;
90  }
91  (*sigp)++;
92  lenleft--;
93  }
94  if (ret < 128) {
95  /* Not the shortest possible length encoding. */
96  return -1;
97  }
98  return ret;
99 }
100 
101 static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char **sig, const unsigned char *sigend) {
102  int overflow = 0;
103  unsigned char ra[32] = {0};
104  int rlen;
105 
106  if (*sig == sigend || **sig != 0x02) {
107  /* Not a primitive integer (X.690-0207 8.3.1). */
108  return 0;
109  }
110  (*sig)++;
111  rlen = secp256k1_der_read_len(sig, sigend);
112  if (rlen <= 0 || (*sig) + rlen > sigend) {
113  /* Exceeds bounds or not at least length 1 (X.690-0207 8.3.1). */
114  return 0;
115  }
116  if (**sig == 0x00 && rlen > 1 && (((*sig)[1]) & 0x80) == 0x00) {
117  /* Excessive 0x00 padding. */
118  return 0;
119  }
120  if (**sig == 0xFF && rlen > 1 && (((*sig)[1]) & 0x80) == 0x80) {
121  /* Excessive 0xFF padding. */
122  return 0;
123  }
124  if ((**sig & 0x80) == 0x80) {
125  /* Negative. */
126  overflow = 1;
127  }
128  while (rlen > 0 && **sig == 0) {
129  /* Skip leading zero bytes */
130  rlen--;
131  (*sig)++;
132  }
133  if (rlen > 32) {
134  overflow = 1;
135  }
136  if (!overflow) {
137  memcpy(ra + 32 - rlen, *sig, rlen);
138  secp256k1_scalar_set_b32(r, ra, &overflow);
139  }
140  if (overflow) {
142  }
143  (*sig) += rlen;
144  return 1;
145 }
146 
147 static int secp256k1_ecdsa_sig_parse(secp256k1_scalar *rr, secp256k1_scalar *rs, const unsigned char *sig, size_t size) {
148  const unsigned char *sigend = sig + size;
149  int rlen;
150  if (sig == sigend || *(sig++) != 0x30) {
151  /* The encoding doesn't start with a constructed sequence (X.690-0207 8.9.1). */
152  return 0;
153  }
154  rlen = secp256k1_der_read_len(&sig, sigend);
155  if (rlen < 0 || sig + rlen > sigend) {
156  /* Tuple exceeds bounds */
157  return 0;
158  }
159  if (sig + rlen != sigend) {
160  /* Garbage after tuple. */
161  return 0;
162  }
163 
164  if (!secp256k1_der_parse_integer(rr, &sig, sigend)) {
165  return 0;
166  }
167  if (!secp256k1_der_parse_integer(rs, &sig, sigend)) {
168  return 0;
169  }
170 
171  if (sig != sigend) {
172  /* Trailing garbage inside tuple. */
173  return 0;
174  }
175 
176  return 1;
177 }
178 
179 static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const secp256k1_scalar* ar, const secp256k1_scalar* as) {
180  unsigned char r[33] = {0}, s[33] = {0};
181  unsigned char *rp = r, *sp = s;
182  size_t lenR = 33, lenS = 33;
183  secp256k1_scalar_get_b32(&r[1], ar);
184  secp256k1_scalar_get_b32(&s[1], as);
185  while (lenR > 1 && rp[0] == 0 && rp[1] < 0x80) { lenR--; rp++; }
186  while (lenS > 1 && sp[0] == 0 && sp[1] < 0x80) { lenS--; sp++; }
187  if (*size < 6+lenS+lenR) {
188  *size = 6 + lenS + lenR;
189  return 0;
190  }
191  *size = 6 + lenS + lenR;
192  sig[0] = 0x30;
193  sig[1] = 4 + lenS + lenR;
194  sig[2] = 0x02;
195  sig[3] = lenR;
196  memcpy(sig+4, rp, lenR);
197  sig[4+lenR] = 0x02;
198  sig[5+lenR] = lenS;
199  memcpy(sig+lenR+6, sp, lenS);
200  return 1;
201 }
202 
203 static int secp256k1_ecdsa_sig_verify(const secp256k1_ecmult_context *ctx, const secp256k1_scalar *sigr, const secp256k1_scalar *sigs, const secp256k1_ge *pubkey, const secp256k1_scalar *message) {
204  unsigned char c[32];
205  secp256k1_scalar sn, u1, u2;
206  secp256k1_fe xr;
207  secp256k1_gej pubkeyj;
208  secp256k1_gej pr;
209 
211  return 0;
212  }
213 
214  secp256k1_scalar_inverse_var(&sn, sigs);
215  secp256k1_scalar_mul(&u1, &sn, message);
216  secp256k1_scalar_mul(&u2, &sn, sigr);
217  secp256k1_gej_set_ge(&pubkeyj, pubkey);
218  secp256k1_ecmult(ctx, &pr, &pubkeyj, &u2, &u1);
219  if (secp256k1_gej_is_infinity(&pr)) {
220  return 0;
221  }
222  secp256k1_scalar_get_b32(c, sigr);
223  secp256k1_fe_set_b32(&xr, c);
224 
241  if (secp256k1_gej_eq_x_var(&xr, &pr)) {
242  /* xr * pr.z^2 mod p == pr.x, so the signature is valid. */
243  return 1;
244  }
246  /* xr + n >= p, so we can skip testing the second case. */
247  return 0;
248  }
250  if (secp256k1_gej_eq_x_var(&xr, &pr)) {
251  /* (xr + n) * pr.z^2 mod p == pr.x, so the signature is valid. */
252  return 1;
253  }
254  return 0;
255 }
256 
257 static int secp256k1_ecdsa_sig_sign(const secp256k1_ecmult_gen_context *ctx, secp256k1_scalar *sigr, secp256k1_scalar *sigs, const secp256k1_scalar *seckey, const secp256k1_scalar *message, const secp256k1_scalar *nonce, int *recid) {
258  unsigned char b[32];
259  secp256k1_gej rp;
260  secp256k1_ge r;
262  int overflow = 0;
263 
264  secp256k1_ecmult_gen(ctx, &rp, nonce);
265  secp256k1_ge_set_gej(&r, &rp);
268  secp256k1_fe_get_b32(b, &r.x);
269  secp256k1_scalar_set_b32(sigr, b, &overflow);
270  if (secp256k1_scalar_is_zero(sigr)) {
271  /* P.x = order is on the curve, so technically sig->r could end up zero, which would be an invalid signature.
272  * This branch is cryptographically unreachable as hitting it requires finding the discrete log of P.x = N.
273  */
274  secp256k1_gej_clear(&rp);
275  secp256k1_ge_clear(&r);
276  return 0;
277  }
278  if (recid) {
279  /* The overflow condition is cryptographically unreachable as hitting it requires finding the discrete log
280  * of some P where P.x >= order, and only 1 in about 2^127 points meet this criteria.
281  */
282  *recid = (overflow ? 2 : 0) | (secp256k1_fe_is_odd(&r.y) ? 1 : 0);
283  }
284  secp256k1_scalar_mul(&n, sigr, seckey);
285  secp256k1_scalar_add(&n, &n, message);
286  secp256k1_scalar_inverse(sigs, nonce);
287  secp256k1_scalar_mul(sigs, sigs, &n);
289  secp256k1_gej_clear(&rp);
290  secp256k1_ge_clear(&r);
291  if (secp256k1_scalar_is_zero(sigs)) {
292  return 0;
293  }
294  if (secp256k1_scalar_is_high(sigs)) {
295  secp256k1_scalar_negate(sigs, sigs);
296  if (recid) {
297  *recid ^= 1;
298  }
299  }
300  return 1;
301 }
302 
303 #endif
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecmult_context *ctx, const secp256k1_scalar *sigr, const secp256k1_scalar *sigs, const secp256k1_ge *pubkey, const secp256k1_scalar *message)
Definition: ecdsa_impl.h:203
static int secp256k1_ecdsa_sig_sign(const secp256k1_ecmult_gen_context *ctx, secp256k1_scalar *sigr, secp256k1_scalar *sigs, const secp256k1_scalar *seckey, const secp256k1_scalar *message, const secp256k1_scalar *nonce, int *recid)
Definition: ecdsa_impl.h:257
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const secp256k1_scalar *ar, const secp256k1_scalar *as)
Definition: ecdsa_impl.h:179
static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
static const secp256k1_fe secp256k1_ecdsa_const_order_as_fe
Definition: ecdsa_impl.h:31
#define SECP256K1_FE_CONST(d7, d6, d5, d4, d3, d2, d1, d0)
Definition: field_10x26.h:38
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar *rr, secp256k1_scalar *rs, const unsigned char *sig, size_t size)
Definition: ecdsa_impl.h:147
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static int secp256k1_fe_is_odd(const secp256k1_fe *a)
static int secp256k1_der_read_len(const unsigned char **sigp, const unsigned char *sigend)
Definition: ecdsa_impl.h:49
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *a)
static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char **sig, const unsigned char *sigend)
Definition: ecdsa_impl.h:101
static secp256k1_context * ctx
Definition: tests.c:42
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
static void secp256k1_gej_clear(secp256k1_gej *r)
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
static void secp256k1_scalar_clear(secp256k1_scalar *r)
secp256k1_fe x
Definition: group.h:15
static int secp256k1_gej_eq_x_var(const secp256k1_fe *x, const secp256k1_gej *a)
static void secp256k1_ge_clear(secp256k1_ge *r)
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b)
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *a)
void * memcpy(void *a, const void *b, size_t c)
static void secp256k1_fe_normalize(secp256k1_fe *r)
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
secp256k1_fe y
Definition: group.h:16
static const secp256k1_fe secp256k1_ecdsa_const_p_minus_order
Definition: ecdsa_impl.h:45