使用 Openssl 验证签名 ECDSA 签名

Verify Signature ECDSA signature with Openssl

我想创建一个签名并用 openssl 验证它。 我想要我的签名的十六进制输出。

这是我的代码

#create private key
openssl ecparam -genkey -name secp256k1 -rand /dev/urandom -noout -out private.pem

#public key derivation
openssl ec -in private.pem -pubout -out public.pem

#create signature
openssl dgst -sha256 -hex -sign private.pem msg.txt  > signature.hex

#check signature
openssl dgst -sha256 -verify public.pem -signature signature.hex msg.txt

我收到这个错误:

Error Verifying Data
4573216364:error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag:tasn_dec.c:1220:
4573216364:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:tasn_dec.c:386:Type=ECDSA_SIG

如果我在创建签名期间删除 -hex,它会起作用。

$ openssl version
OpenSSL 1.0.2s  28 May 2019

openssl dgst 命令“-hex”参数意味着输出不是二进制的,而是二进制输出的十六进制转储。

引用:

-hex

digest is to be output as a hex dump. This is the default case for a "normal" digest as opposed to a digital signature. See NOTES below for digital signatures using -hex.

以及注释部分:

Hex signatures cannot be verified using openssl. Instead, use "xxd -r" or similar program to transform the hex signature into a binary signature prior to verification.

因此,如果您对十六进制转储使用 -hex 选项,则需要在将其传递到 openssl 进行验证之前以某种方式自行将其转换回二进制。