|
| 1 | +/** |
| 2 | + * Copyright Soramitsu Co., Ltd. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include "crypto/secp256k1/impl/secp256k1_provider_impl.hpp" |
| 7 | + |
| 8 | +#include <openssl/bn.h> |
| 9 | +#include <openssl/ec.h> |
| 10 | +#include <openssl/obj_mac.h> |
| 11 | +#include "crypto/secp256k1/secp256k1_error.hpp" |
| 12 | +#include "secp256k1_recovery.h" |
| 13 | + |
| 14 | +namespace fc::crypto::secp256k1 { |
| 15 | + |
| 16 | + Secp256k1ProviderImpl::Secp256k1ProviderImpl() |
| 17 | + : context_(secp256k1_context_create(SECP256K1_CONTEXT_SIGN |
| 18 | + | SECP256K1_CONTEXT_VERIFY), |
| 19 | + secp256k1_context_destroy) {} |
| 20 | + |
| 21 | + outcome::result<KeyPair> Secp256k1ProviderImpl::generate() const { |
| 22 | + PublicKeyUncompressed public_key{}; |
| 23 | + PrivateKey private_key{}; |
| 24 | + std::shared_ptr<EC_KEY> key{EC_KEY_new_by_curve_name(NID_secp256k1), |
| 25 | + EC_KEY_free}; |
| 26 | + if (EC_KEY_generate_key(key.get()) != 1) { |
| 27 | + return Secp256k1Error::KEY_GENERATION_FAILED; |
| 28 | + } |
| 29 | + const BIGNUM *privateNum = EC_KEY_get0_private_key(key.get()); |
| 30 | + if (BN_bn2binpad(privateNum, private_key.data(), private_key.size()) < 0) { |
| 31 | + return Secp256k1Error::KEY_GENERATION_FAILED; |
| 32 | + } |
| 33 | + EC_KEY_set_conv_form(key.get(), POINT_CONVERSION_UNCOMPRESSED); |
| 34 | + auto public_key_length = i2o_ECPublicKey(key.get(), nullptr); |
| 35 | + if (public_key_length != public_key.size()) { |
| 36 | + return Secp256k1Error::KEY_GENERATION_FAILED; |
| 37 | + } |
| 38 | + uint8_t *public_key_ptr = public_key.data(); |
| 39 | + if (i2o_ECPublicKey(key.get(), &public_key_ptr) != public_key_length) { |
| 40 | + return Secp256k1Error::KEY_GENERATION_FAILED; |
| 41 | + } |
| 42 | + return KeyPair{private_key, public_key}; |
| 43 | + } |
| 44 | + |
| 45 | + outcome::result<PublicKeyUncompressed> Secp256k1ProviderImpl::derive( |
| 46 | + const PrivateKey &key) const { |
| 47 | + secp256k1_pubkey pubkey; |
| 48 | + |
| 49 | + if (!secp256k1_ec_pubkey_create(context_.get(), &pubkey, key.data())) { |
| 50 | + return Secp256k1Error::KEY_GENERATION_FAILED; |
| 51 | + } |
| 52 | + |
| 53 | + PublicKeyUncompressed public_key{}; |
| 54 | + size_t outputlen = kPublicKeyUncompressedLength; |
| 55 | + if (!secp256k1_ec_pubkey_serialize(context_.get(), |
| 56 | + public_key.data(), |
| 57 | + &outputlen, |
| 58 | + &pubkey, |
| 59 | + SECP256K1_EC_UNCOMPRESSED)) { |
| 60 | + return Secp256k1Error::PUBKEY_SERIALIZATION_ERROR; |
| 61 | + } |
| 62 | + |
| 63 | + return public_key; |
| 64 | + } |
| 65 | + |
| 66 | + outcome::result<PublicKeyUncompressed> Secp256k1ProviderImpl::sign( |
| 67 | + gsl::span<const uint8_t> message, const PrivateKey &key) const { |
| 68 | + secp256k1_ecdsa_recoverable_signature sig_struct; |
| 69 | + if (!secp256k1_ecdsa_sign_recoverable(context_.get(), |
| 70 | + &sig_struct, |
| 71 | + message.data(), |
| 72 | + key.cbegin(), |
| 73 | + secp256k1_nonce_function_rfc6979, |
| 74 | + nullptr)) { |
| 75 | + return Secp256k1Error::CANNOT_SIGN_ERROR; |
| 76 | + } |
| 77 | + SignatureCompact signature{}; |
| 78 | + int recid = 0; |
| 79 | + if (!secp256k1_ecdsa_recoverable_signature_serialize_compact( |
| 80 | + context_.get(), signature.data(), &recid, &sig_struct)) { |
| 81 | + return Secp256k1Error::SIGNATURE_SERIALIZATION_ERROR; |
| 82 | + } |
| 83 | + signature[64] = (uint8_t)recid; |
| 84 | + return signature; |
| 85 | + } |
| 86 | + |
| 87 | + outcome::result<bool> Secp256k1ProviderImpl::verify( |
| 88 | + gsl::span<const uint8_t> message, |
| 89 | + const SignatureCompact &signature, |
| 90 | + const PublicKeyUncompressed &key) const { |
| 91 | + OUTCOME_TRY(checkSignature(signature)); |
| 92 | + |
| 93 | + secp256k1_ecdsa_signature sig; |
| 94 | + secp256k1_pubkey pubkey; |
| 95 | + |
| 96 | + if (!secp256k1_ecdsa_signature_parse_compact( |
| 97 | + context_.get(), &sig, signature.data())) { |
| 98 | + return Secp256k1Error::SIGNATURE_PARSE_ERROR; |
| 99 | + } |
| 100 | + if (!secp256k1_ec_pubkey_parse( |
| 101 | + context_.get(), &pubkey, key.data(), key.size())) { |
| 102 | + return Secp256k1Error::PUBKEY_PARSE_ERROR; |
| 103 | + } |
| 104 | + return (secp256k1_ecdsa_verify( |
| 105 | + context_.get(), &sig, message.data(), &pubkey) == 1); |
| 106 | + } |
| 107 | + |
| 108 | + outcome::result<PublicKeyUncompressed> |
| 109 | + Secp256k1ProviderImpl::recoverPublicKey( |
| 110 | + gsl::span<const uint8_t> message, |
| 111 | + const SignatureCompact &signature) const { |
| 112 | + OUTCOME_TRY(checkSignature(signature)); |
| 113 | + |
| 114 | + secp256k1_ecdsa_recoverable_signature sig_rec; |
| 115 | + secp256k1_pubkey pubkey; |
| 116 | + |
| 117 | + if (!secp256k1_ecdsa_recoverable_signature_parse_compact( |
| 118 | + context_.get(), &sig_rec, signature.data(), (int)signature[64])) { |
| 119 | + return Secp256k1Error::SIGNATURE_PARSE_ERROR; |
| 120 | + } |
| 121 | + if (!secp256k1_ecdsa_recover( |
| 122 | + context_.get(), &pubkey, &sig_rec, message.data())) { |
| 123 | + return Secp256k1Error::RECOVER_ERROR; |
| 124 | + } |
| 125 | + PublicKeyUncompressed pubkey_out; |
| 126 | + size_t outputlen = kPublicKeyUncompressedLength; |
| 127 | + if (!secp256k1_ec_pubkey_serialize(context_.get(), |
| 128 | + pubkey_out.data(), |
| 129 | + &outputlen, |
| 130 | + &pubkey, |
| 131 | + SECP256K1_EC_UNCOMPRESSED)) { |
| 132 | + return Secp256k1Error::PUBKEY_SERIALIZATION_ERROR; |
| 133 | + } |
| 134 | + |
| 135 | + return pubkey_out; |
| 136 | + } |
| 137 | + |
| 138 | + outcome::result<void> Secp256k1ProviderImpl::checkSignature( |
| 139 | + const SignatureCompact &signature) const { |
| 140 | + if (signature[64] > 3) { |
| 141 | + return Secp256k1Error::SIGNATURE_PARSE_ERROR; |
| 142 | + } |
| 143 | + return outcome::success(); |
| 144 | + } |
| 145 | + |
| 146 | +} // namespace fc::crypto::secp256k1 |
0 commit comments