1 // Copyright (c) 2009-2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 #include <openssl/ec.h>
6 #include <openssl/ecdsa.h>
8 // Generate a private key from just the secret parameter
9 int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key)
13 EC_POINT *pub_key = NULL;
17 const EC_GROUP *group = EC_KEY_get0_group(eckey);
19 if ((ctx = BN_CTX_new()) == NULL)
22 pub_key = EC_POINT_new(group);
27 if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
30 EC_KEY_set_private_key(eckey,priv_key);
31 EC_KEY_set_public_key(eckey,pub_key);
38 EC_POINT_free(pub_key);
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)
69 const EC_GROUP *group = EC_KEY_get0_group(eckey);
70 if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; }
72 order = BN_CTX_get(ctx);
73 if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; }
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; }
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; }
89 if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; }
90 n = EC_GROUP_get_degree(group);
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; }
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; }
113 if (R != NULL) EC_POINT_free(R);
114 if (O != NULL) EC_POINT_free(O);
115 if (Q != NULL) EC_POINT_free(Q);