【问题标题】:ECDSA_do_verify fails to verify for some hash onlyECDSA_do_verify 无法仅验证某些哈希
【发布时间】:2019-12-14 13:26:49
【问题描述】:

我有一个设备,我知道它的公钥。该设备能够使用其私钥对随机数进行签名并返回签名。在我的工作流程中,我使用一个随机数并给设备签名这个数字。设备计算此随机数的 SHA256 散列并使用其私钥对其进行签名。我必须用随机数和设备的公钥来验证这个签名。我正在使用 openssl 来实现这一点。我还计算了随机数的哈希值并使用 ECDSA_do_verify 函数来验证签名。问题是,我大部分时间都可以验证这个签名。但有时 ECDSA_do_verify 无法验证签名。我尝试生成 100 个随机数并验证它们的签名。我可以验证签名 86 次,其他 14 次验证失败。知道可能缺少什么。

bool verify_signature(const unsigned char* hash, const ECDSA_SIG* signature, EC_KEY* eckey)
{
    int verify_status = ECDSA_do_verify(hash, strlen((const char*)hash), 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);

    BN_hex2bn(&r_ptr, sSignatureR.c_str());
    BN_hex2bn(&s_ptr, sSignatureS.c_str());

    ECDSA_SIG_set0(pSign, r_ptr, s_ptr);

    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 = "228B756444CFF74453ABA22BF1FD052965682FDFDC915647F8B07068636BE6827938ED61B6C388551A6D4CCF3397858E14F5EA648FE13454C13292364BB40C1C";
    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;
}

这些是正确验证的一些集合。
随机数:65560886818773090201885807838738706912015073749623293202319529
签名:D506D976EC17DD3717C40329E28FD8DB4F32D6A3773454A6427FD12E69728157508086B661D91E07ADF5B57E787EA1EEA526A84500436E430E89B1C1F8532A41

随机数:99740602803090660927832030976281034564516575306436870967722262
签名:C46E17807113A4000E703FA96266CF3A00765BB8AC207C9A4D424AAF1AB8ABF9BA2B4972D72DA1457953B1C44E16DED1C363AEFBC28DCE1D4A498972C41AB922

随机数:72678600666159100061395904673222749914485786786827057090965704
签名:539D94A976B417E1577581B73E2C0926BAE1D4DFE120CC2BD04405D6AE16E9CBD2E50343A69881A9AC13C23E5E68A9ECCA8F2CF41FC3AEC219B03F948482121B

================================================ ==================================================== ============================================ 这些是一些未通过验证的集合。
随机数:71953998552024151452665049464440435681055869491222530611033199
签名:CDD5274A1CB429FD3F78095B0262973ACDEAD85338F46E772F3095C816ADF4D25F8EDC7C916E91EFF753ABE6DE0287878E4CBD6E1B59F7D5D122C8B85D20CF1C

随机数:58079150817698947252033239072161198505487375300964223454033169
签名:D3C90D47D15DF8E066CDB09F7B218A24D69066E8B896F3013DFB4B6A9CBBA555AB2A0F07BDE9915DD48A42BCB2164CB969045C31D2035FC0DB8B4FA9877C2FFD

随机数:53433823585580925787026650456476141879880067601547801597683336
签名:6EFD052EB961D089B4CA16CAF0818711F02500CD0540B2BE1A7426B5126E1C45D200E3C61970D49508173C7024A729A53ADF87CD2A6AF26A2D0CD3BF9EB2C0C1

【问题讨论】:

  • 而不是std::unique_ptr&lt; BIGNUM, std::function&lt;void(BIGNUM*)&gt;&gt; rr(NULL, [](BIGNUM* b) { BN_free(b); }); 为什么不是std::unique_ptr&lt;BIGNUM, void(*)(BIGNUM*)&gt; rr(NULL, BN_free);
  • 据我所知,为什么声明像 std::unique_ptr)(BIGNUM)> 比声明 std::unique_ptr> rr(NULL, [](BIGNUM* b) { BN_free(b); }) ?一个明显的原因是打字更少,看起来更干净......还有其他原因吗?是否必须使用自定义删除器大小?
  • “看起来更干净”是关键点。它使以后的读者(可能是您自己)更容易理解发生了什么。

标签: c++ openssl ecdsa


【解决方案1】:

验证失败的所有签名的 SHA256 哈希包含一个或多个 0x00 值。

例子:

Random number: 71953998552024151452665049464440435681055869491222530611033199
SHA256 Hash:   63 dd ae d4 11 be a8 b6 9c 00 75 5c 8d 90 bc e0 11 b0 50 16 7f 94 fd fc ac e5 ed 00 68 34 0b b6

0x00 值出现在索引位置 9 和 27。

当在verify_signature 方法中调用ECDSA_do_verify 时,strlen((const char*)hash) 作为第二个参数传递。此表达式计算直到第一个 0x00 值(不包括)的长度。因此,只有部分哈希用于验证,验证失败。

通过将strlen((const char*)hash) 替换为SHA256_DIGEST_LENGTH 可以轻松解决该问题,因为SHA256 哈希具有固定长度(32 字节)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2014-02-06
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多