gpt4 book ai didi

c++ - OpenSSL 在 EVP_VerifyFinal 失败

转载 作者:太空狗 更新时间:2023-10-29 20:39:21 28 4
gpt4 key购买 nike

在搜索互联网和 openssl 文档几天后,我遇到了瓶颈。我正在尝试使用私钥签署消息,然后使用公钥验证消息的真实性。

key 生成代码:

    //Generate RSA Keys
EVP_PKEY_CTX* KeyCtx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (KeyCtx == nullptr)
return false;

if (EVP_PKEY_keygen_init(KeyCtx) <= 0)
return false;

if (EVP_PKEY_CTX_set_rsa_keygen_bits(KeyCtx, RSA_KeyLen) <= 0)
return false;

if (EVP_PKEY_keygen(KeyCtx, &m_ServerKeyPair) <= 0)
return false;

if (EVP_PKEY_keygen(KeyCtx, &m_ClientKeyPair) <= 0)
return false;

//Write Keys to EVP_PKEYS for actual internal encryption,BIO mem is wiped after read so we need a new copy
PEM_read_bio_PrivateKey(KeyToPrivBio(m_ServerKeyPair), &m_ServerPrivateKey, NULL, NULL);
PEM_read_bio_PUBKEY(KeyToPubBio(m_ServerKeyPair), &m_ServerPublicKey, NULL, NULL);
PEM_read_bio_PrivateKey(KeyToPrivBio(m_ClientKeyPair), &m_ClientPrivateKey, NULL, NULL);
PEM_read_bio_PUBKEY(KeyToPubBio(m_ClientKeyPair), &m_ClientPublicKey, NULL, NULL);

RSA 标志:

bool SecureCrypto::RSASign(const unsigned char* Msg, size_t MsgLen,unsigned char** EncMsg, size_t& MsgLenEnc)
{
EVP_PKEY_size(m_ServerPrivateKey);
*EncMsg = (unsigned char*)malloc(EVP_PKEY_size(m_ServerPrivateKey));

if (EVP_SignInit_ex(m_RSASignCtx, EVP_sha1(), 0) <= 0)
{
printf("Failed Init\n");
return false;
}

if (EVP_SignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0)
{
printf("Failed Update\n");
return false;
}

if (EVP_SignFinal(m_RSASignCtx, *EncMsg, &MsgLenEnc, m_ServerPrivateKey) <=0)
{
printf("Failed Final Sign\n");
return false;
}
EVP_MD_CTX_cleanup(m_RSASignCtx);

return true;
}

RSA 验证:

bool SecureCrypto::RSAVerifySignature(const unsigned char* MsgHash, size_t MsgHashLen,unsigned char* Msg, size_t MsgLen)
{
if (EVP_VerifyInit(m_RSAVerifyCtx, EVP_sha1()) <= 0)
{
printf("Failed Verify Init\n");
return false;
}

if (EVP_VerifyUpdate(m_RSAVerifyCtx, Msg, MsgLen) <= 0)
{
printf("Failed Verify \n");
return false;
}

if (EVP_VerifyFinal(m_RSAVerifyCtx, MsgHash, MsgHashLen, m_ServerPublicKey)<=0);
{
printf("Failed Final Verify %s\n",ERR_error_string(ERR_get_error(),NULL));
return false;
}
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true;
}

key 生成成功,我查看了它创建的 key ,它们似乎是有效的并且格式为 -----BEGIN PRIVATE KEY-----,一些大质数, -----结束私钥-----。签名似乎成功并返回一个散列。 EVP_Verify 在最后一步 EVP_VerifyFinal 上失败,ERR_error_string 报告:

error:00000000:lib(0):func(0):reason(0)

这显然没有帮助,有人可以指出我可能做错了什么。这是我第一次深入了解 openssl,因此这里可能存在一些大问题。

最佳答案

感谢 JWW 和 indiv 我能够解决我的问题,这是我使用旧 API 和不正确的返回检查的问题。解决方案:

bool SecureCrypto::RSASign(const unsigned char* Msg, size_t MsgLen,unsigned char** EncMsg, size_t* MsgLenEnc)
{
if (EVP_DigestSignInit(m_RSASignCtx,NULL, EVP_sha256(), NULL,m_ServerPrivateKey) <= 0)
{
printf("Failed Init\n");
return false;
}

if (EVP_DigestSignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0)
{
printf("Failed Update\n");
return false;
}

//Get Hash Size
if (EVP_DigestSignFinal(m_RSASignCtx, NULL,MsgLenEnc) <=0)
{
printf("Failed Final Sign\n");
return false;
}

//Allocate Space for hash
*EncMsg = (unsigned char*)malloc(*MsgLenEnc);
if (EVP_DigestSignFinal(m_RSASignCtx, *EncMsg, MsgLenEnc) <= 0)
{
printf("Failed Final Sign 1\n");
return false;
}

EVP_MD_CTX_cleanup(m_RSASignCtx);

return true;
}

bool SecureCrypto::RSAVerifySignature(const unsigned char* MsgHash, size_t MsgHashLen,unsigned char* Msg, size_t MsgLen,bool* Authentic)
{
if (EVP_DigestVerifyInit(m_RSAVerifyCtx,NULL, EVP_sha256(),NULL,m_ServerPrivateKey) <= 0)
{
printf("Failed Verify Init\n");
return false;
}

if (EVP_DigestVerifyUpdate(m_RSAVerifyCtx, Msg, MsgLen) <= 0)
{
printf("Failed Verify \n");
return false;
}

int AuthStatus = EVP_DigestVerifyFinal(m_RSAVerifyCtx, (unsigned char*)MsgHash, MsgHashLen);

if (AuthStatus==1)
{
//Message Authentic
*Authentic = true;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true;
} else if(AuthStatus==0){
//Message Not Authentic
*Authentic = false;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return true; //Still did our job correctly
} else{
printf("Error\n");
*Authentic = false;
EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
return false;
}
}

我不敢相信我错过了一个如此明显的例子,谢谢你们的帮助,抱歉我看起来很愚蠢哈哈。

关于c++ - OpenSSL 在 EVP_VerifyFinal 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954979/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com