关于openssl:C11-ECDSAwithSHA256验签

46次阅读

共计 3685 个字符,预计需要花费 10 分钟才能阅读完成。

这里不做签名,只验签

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

#include <iostream>
#include <memory>
#include <string>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <openssl/ecdsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/bn.h>

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<void(BIGNUM*)>> rr(NULL, [](BIGNUM* b) {BN_free(b); });
    // BIGNUM* r_ptr = rr.get();
    // std::unique_ptr< BIGNUM, std::function<void(BIGNUM*)>> 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<void(BIGNUM*)>> gx(NULL, [](BIGNUM* b) {BN_free(b); });
    std::unique_ptr< BIGNUM, std::function<void(BIGNUM*)>> 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<void(ECDSA_SIG*)>> 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<void(EC_KEY*)>> 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;
}

正文完
 0