C++11 ECDSA-withSHA256验签

这里不做签名,只验签

使用方法:
openssl版本:1.0.2g 其他的自行验证
编译:g++ test.cpp -o test -lssl -lcrypto -std=c++11
执行:./test
签名过程:随机数进行SHA256哈希后再使用私钥对其签名
验签过程:用随机数的SHA256和公钥来验证签名
以下代码是验证签名

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

bool verify_signature(const unsigned char* hash, const ECDSA_SIG* signature, EC_KEY* eckey)
{
    int verify_status = ECDSA_do_verify(hash, SHA256_DIGEST_LENGTH, signature, eckey);
    if (1 != verify_status)
    {
        printf("Failed to verify EC Signature\n");
        return false;
    }

    printf("Verifed EC Signature\n");

    return true;
}


void SetOpensslSignature(const std::string& sSignatureInHex, ECDSA_SIG* pSign)
{
    // std::unique_ptr< BIGNUM, std::function> rr(NULL, [](BIGNUM* b) { BN_free(b); });
    // BIGNUM* r_ptr = rr.get();
    // std::unique_ptr< BIGNUM, std::function> ss(NULL, [](BIGNUM* b) { BN_free(b); });
    // BIGNUM* s_ptr = ss.get();

    std::string sSignatureR = sSignatureInHex.substr(0, sSignatureInHex.size() / 2);
    std::string sSignatureS = sSignatureInHex.substr(sSignatureInHex.size() / 2);

    pSign->r = BN_new();
    pSign->s = BN_new();

    BN_hex2bn(&pSign->r, sSignatureR.c_str());
    BN_hex2bn(&pSign->s, sSignatureS.c_str());



    return;
}

bool SetOpensslPublicKey(const std::string& sPublicKeyInHex, EC_KEY* pKey)
{
    const char* sPubKeyString = sPublicKeyInHex.c_str();

    char cx[65];

    std::unique_ptr< BIGNUM, std::function> gx(NULL, [](BIGNUM* b) { BN_free(b); });
    std::unique_ptr< BIGNUM, std::function> gy(NULL, [](BIGNUM* b) { BN_free(b); });

    BIGNUM* gx_ptr = gx.get();
    BIGNUM* gy_ptr = gy.get();

    EC_KEY_set_asn1_flag(pKey, OPENSSL_EC_NAMED_CURVE);
    memcpy(cx, sPubKeyString, 64);
    cx[64] = 0;

    if (!BN_hex2bn(&gx_ptr, cx)) {
        std::cout << "Error getting to binary format" << std::endl;
    }

    if (!BN_hex2bn(&gy_ptr, &sPubKeyString[64])) {
        std::cout << "Error getting to binary format" << std::endl;
    }

    if (!EC_KEY_set_public_key_affine_coordinates(pKey, gx_ptr, gy_ptr)) {
        std::cout << "setting public key attributes" << std::endl;
    }

    if (EC_KEY_check_key(pKey) == 1)
    {
        printf("EC Key valid.\n");
        return true;
    }
    else {
        printf("EC Key Invalid!\n");
        return false;
    }
}

std::string sha256(const std::string str)
{
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    SHA256_Update(&sha256, str.c_str(), str.size());
    SHA256_Final(hash, &sha256);
    std::stringstream ss;
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        ss << hash[i];
    }
    return ss.str();
}


bool Verify(const std::string& sRandomNumber, const std::string& sSignature, const std::string& sDevicePubKeyInHex)
{
    std::unique_ptr< ECDSA_SIG, std::function> zSignature(ECDSA_SIG_new(), [](ECDSA_SIG* b) { ECDSA_SIG_free(b); });
    // Set up the signature... 
    SetOpensslSignature(sSignature, zSignature.get());

    std::unique_ptr< EC_KEY, std::function> zPublicKey(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1), [](EC_KEY* b) { EC_KEY_free(b); });
    if (!SetOpensslPublicKey(sDevicePubKeyInHex, zPublicKey.get()))
        std::cout << "Failed to get the public key from the hex input" << std::endl;

    std::string sHash = sha256(sRandomNumber);

    return verify_signature((const unsigned char*)sHash.c_str(), zSignature.get(), zPublicKey.get());
}


int main(int argc, char* argv[])
{
    std::string sSignatureInHex = "D506D976EC17DD3717C40329E28FD8DB4F32D6A3773454A6427FD12E69728157508086B661D91E07ADF5B57E787EA1EEA526A84500436E430E89B1C1F8532A41";
    std::string sPublicKeyInHex = "94E62E0C77A2955B1FB3EE98AEAA99AACAD742F20E45B727EACDD10487C2F7D0D8257C6102921880ABE953245D573D7E33EC88A67E2BA930980CB9C3D6722F8A";
    std::string sRandomNumber = "65560886818773090201885807838738706912015073749623293202319529";
    
    if (!Verify(sRandomNumber, sSignatureInHex, sPublicKeyInHex))
        std::cout << "Verification failed." << std::endl;
    else
        std::cout << "Verification succeeded" << std::endl;
}

你可能感兴趣的:(C++11 ECDSA-withSHA256验签)