多种 OpenSSL RSA 签名方法产生不同的结果 [英] Multiple OpenSSL RSA signing methods produce different results

查看:21
本文介绍了多种 OpenSSL RSA 签名方法产生不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图围绕签名和使用/测试各种选项.

Trying to wrap my head around signing and use/test various options.

我可以使用这个命令签名:

I can sign using this command:

openssl dgst -sha256 -sign private_key.pem -binary -out sig_file data_file

但是文档好像说我也可以用这个方法

But the documentation seems to say that I can also use this method

openssl dgst -sha256 -binary data_file > hash_file
openssl rsautl -sign -inkey private_key.pem -keyform PEM -in hash_file > sig_file2

但是当我希望它们相同时,签名是不同的.要么我错过了选项中的某些内容,要么我的假设中存在其他错误.

But the signatures are different when I'd expect them to be identical. Either I missed something in the options or something else is wrong in my assumptions.

这个问题的真正问题是:鉴于我已经拥有哈希值并生成与上述第一个命令相同的签名,是否有办法使用命令行选项进行签名.

The real question from this issue: Is there a way to sign using command line options given that I already have the hash value and produce a signature that is identical to the first command above.

要添加更多内容,我可以在代码中轻松重现第一个命令,它与上面的第一个命令匹配,这意味着我可以使用首先计算的哈希值进行签名.

To add more to this, I can reproduce the first command easily in code and it matches the first command above meaning that I can sign with the hash value calculated first.

mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
EVP_DigestUpdate(mdctx, data, len);
EVP_DigestFinal_ex(mdctx, hash, &s);
EVP_MD_CTX_destroy(mdctx);

kfile = fopen64(key_file, "r");
key = PEM_read_RSAPrivateKey(kfile, NULL, NULL, NULL);
fclose(kfile);

*sig = malloc(RSA_size(key));
RSA_sign(NID_sha256, hash, hlen, *sig, siglen, key);

推荐答案

Dupe: 区别在 openSSL rsautl 和 dgst 之间
密切相关:
为什么我使用 OpenSSL 和 Java 生成的 RSA-SHA256 签名不同吗?
使用C例程和openssl时的不同签名dgst、rsautl 命令
使用 256 位 RSA 密钥签署 20 字节消息,使用 openssl.exe 但不在代码中
Crossdupe:https://superuser.com/questions/943972/what-is-the-difference-between-openssl-pkeyutl-sign-and-openssl-rsautl-sign

TLDR:dgst -sign for RSA 执行完整的 RSASSA-PKCS1-v1_5:对数据进行散列,在 ASN.1 中对散列进行编码,填充结果,然后模态 D.rsautl -sign 只执行最后两个,而 dgst 本身只执行第一个,因此跳过编码会产生不同的非标准签名.dgst(或您自己的哈希)然后 pkeyutl -sign 带有 RSA 密钥 -pkeyoptdigest:name_of_digest(重要!) 也可以解决您的实际问题.

TLDR: dgst -sign for RSA does the full RSASSA-PKCS1-v1_5: hash the data, encode the hash in ASN.1, pad the result, and modexp d. rsautl -sign does only the last two and dgst by itself only the first, thus skipping the encode producing a different and nonstandard signature. dgst (or your own hash) then pkeyutl -sign with an RSA key and -pkeyopt digest:name_of_digest (important!) also works and answers your real question.

这篇关于多种 OpenSSL RSA 签名方法产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆