Update License in File Headers
[novacoin.git] / src / key.cpp
1 // Copyright (c) 2009-2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <openssl/ec.h>
6 #include <openssl/ecdsa.h>
7
8 // Generate a private key from just the secret parameter
9 int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
10 {
11     int ok = 0;
12     BN_CTX *ctx = NULL;
13     EC_POINT *pub_key = NULL;
14
15     if (!eckey) return 0;
16
17     const EC_GROUP *group = EC_KEY_get0_group(eckey);
18
19     if ((ctx = BN_CTX_new()) == NULL)
20         goto err;
21
22     pub_key = EC_POINT_new(group);
23
24     if (pub_key == NULL)
25         goto err;
26
27     if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
28         goto err;
29
30     EC_KEY_set_private_key(eckey,priv_key);
31     EC_KEY_set_public_key(eckey,pub_key);
32
33     ok = 1;
34
35 err:
36
37     if (pub_key)
38         EC_POINT_free(pub_key);
39     if (ctx != NULL)
40         BN_CTX_free(ctx);
41
42     return(ok);
43 }
44
45 // Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields
46 // recid selects which key is recovered
47 // if check is nonzero, additional checks are performed
48 int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
49 {
50     if (!eckey) return 0;
51
52     int ret = 0;
53     BN_CTX *ctx = NULL;
54
55     BIGNUM *x = NULL;
56     BIGNUM *e = NULL;
57     BIGNUM *order = NULL;
58     BIGNUM *sor = NULL;
59     BIGNUM *eor = NULL;
60     BIGNUM *field = NULL;
61     EC_POINT *R = NULL;
62     EC_POINT *O = NULL;
63     EC_POINT *Q = NULL;
64     BIGNUM *rr = NULL;
65     BIGNUM *zero = NULL;
66     int n = 0;
67     int i = recid / 2;
68
69     const EC_GROUP *group = EC_KEY_get0_group(eckey);
70     if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
71     BN_CTX_start(ctx);
72     order = BN_CTX_get(ctx);
73     if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
74     x = BN_CTX_get(ctx);
75     if (!BN_copy(x, order)) { ret=-1; goto err; }
76     if (!BN_mul_word(x, i)) { ret=-1; goto err; }
77     if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; }
78     field = BN_CTX_get(ctx);
79     if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
80     if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
81     if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
82     if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; }
83     if (check)
84     {
85         if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
86         if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; }
87         if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; }
88     }
89     if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
90     n = EC_GROUP_get_degree(group);
91     e = BN_CTX_get(ctx);
92     if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; }
93     if (8*msglen > n) BN_rshift(e, e, 8-(n & 7));
94     zero = BN_CTX_get(ctx);
95     if (!BN_zero(zero)) { ret=-1; goto err; }
96     if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
97     rr = BN_CTX_get(ctx);
98     if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; }
99     sor = BN_CTX_get(ctx);
100     if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; }
101     eor = BN_CTX_get(ctx);
102     if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
103     if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
104     if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; }
105
106     ret = 1;
107
108 err:
109     if (ctx) {
110         BN_CTX_end(ctx);
111         BN_CTX_free(ctx);
112     }
113     if (R != NULL) EC_POINT_free(R);
114     if (O != NULL) EC_POINT_free(O);
115     if (Q != NULL) EC_POINT_free(Q);
116     return ret;
117 }