Botan load private key from a file with Base64 format to Sign and Verify

115 views Asked by At

I'm trying to create a Sign and Verify functions using Botan Library.

At this point i have a file with private key in Base64 format, using Botan ECDSA and secp256k1 to generate keys:

Botan::AutoSeeded_RNG rng;
Botan::ECDSA_PrivateKey private_key(rng, Botan::EC_Group("secp256k1"));
std::vector<uint8_t> publicKeyPointBytes = private_key.public_point().encode(Botan::PointGFp::UNCOMPRESSED);
std::vector<uint8_t> privateKeyBytes = Botan::BigInt::encode(private_key.private_value());
std::string privateKeyBase64 = Botan::base64_encode(privateKeyBytes.data(), privateKeyBytes.size());
std::string publicKeyPointBase64 = Botan::base64_encode(publicKeyPointBytes.data(), publicKeyPointBytes.size());

This code Saves private and public key in variables privateKeyBase64 and publicKeyPointBase64, then i save this strings to a file.

Example Private Key in Base64 format: 4wFEGUhdA6D64OLKsO1rIY6Az+/V/tJ+P0yqJ7MVbwk=

At this point, i can read the private and public keys from a this file, and what i want to do is to create a Sign and Verify functions, declared as:

std::string MainFrame::SignMessage(const std::string& message, const std::string& privateKeyBase64)

and

bool MainFrame::VerifySignature(const std::string& message, const std::string& signatureBase64, const std::string& publicKeyBase64)

But i have probems on signing function, i read Botan documentation, but i still have problems, starting on SignMessage i have programmed this one. It compiles ok, but i have an exeption and it does not return signature.

Error: Unexpected tag while decoding ECC domain params

std::string MainFrame::SignMessage(const std::string& message, const std::string& privateKeyBase64)

{
    try {
        Botan::AutoSeeded_RNG rng;
     
        Botan::secure_vector<uint8_t> decodedData = Botan::base64_decode(privateKeyBase64);
        Botan::DataSource_Memory key_source(decodedData.data(), decodedData.size());
        const Botan::OID secp256k1_oid("1.3.132.0.10");


        Botan::ECDSA_PrivateKey key(Botan::AlgorithmIdentifier(), decodedData);

        Botan::PK_Signer signer(key, rng, "SHA-256");
        signer.update(message);
        std::vector<uint8_t> signature = signer.signature(rng);
        TextBox->AppendText("\nSignatude completed");
        //TextBox->AppendText(std::to_string(signature));
        return Botan::hex_encode(signature);
    }


    catch (const std::exception& ex)
    {
            std::cerr << "Error: " << ex.what() << std::endl;
            return ex.what();
    }
}

Can someone help on how can i define this functions in Botan?

0

There are 0 answers