ecdsa 签名验证

ecdsa signature verification

下一个问题: 消息已在 python 应用程序中签名,并将在 erlang 中使用 public 密钥

进行验证

python 可以通过两种方式对消息进行签名:

  1. 具有可变长度的签名 - erlang 验证正常

  2. 具有不可变的签名长度 - erlang 无法验证它

来自 python 库的一些信息: 还有多种表示签名的方法。默认的 sk.sign() 和 vk.verify() 方法将其呈现为一个短字符串,以实现简单性和最小开销。要使用不同的方案,请使用 sk.sign(sigencode=) 和 vk.verify(sigdecode=) 参数。 “ecdsa.util”模块中的辅助函数在这里很有用。

示例:

DataToSign = 
<<96,0,0,0,0,0,253,0,254,128,131,0,33,1,255,255,48,142,92,131,100,75,214,244,255,60,0,0,0,0,0,0,0,0,0,0,0,0,0,25,0,0,16,92,17,66,0,1,0,0,0,0,0,0,3,9,89,185,3,24,89,185,5,112,0,0,123,3,0,3,253,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>.

PublicKey = {{'ECPoint',<<4,71,71,171,183,56,205,109,58,24,36,197,220,179,74,166,21,145,
              79,209,245,157,150,237,93,96,31,1,248,198,232,97,28,197,90,243,
              89,210,74,169,213,236,46,124,133,31,44,215,222,184,221,251,95,
              40,182,101,193,50,35,62,174,134,178,142,120>>},
 {namedCurve,{1,2,840,10045,3,1,7}}}.



%% Signature from python (with constant length):

ImmutableSign = 
<<179,160,225,5,1,154,246,29,57,97,160,132,95,35,135,238,69,21,136,152,51,104,224,126,124,226,168,143,253,213,37,233,196,67,254,155,60,218,45,8,60,140,76,44,20,189,213,143,42,231,4,177,62,124,141,95,236,20,59,20,28,10,58,232>>.

%% signed with sk.sign(data, hashfunc=hashlib.sha256)

MutableSign =
<<48,68,2,32,6,82,67,217,251,248,93,74,223,170,47,192,93,228,218,228,132,4,69,177,57,49,20,161,241,48,52,106,39,130,197,2,2,32,88,35,228,122,252,140,214,
191,187,117,10,187,54,106,16,36,198,155,49,78,198,58,103,57,124,212,244,31,9,242,101,169>>

%% signed with sk.sign(data, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der)


public_key:verify(DataToSign, sha256, ImmutableSign, PublicKey) -> false

public_key:verify(DataToSign, sha256, MutableSign, PublicKey) -> true

如果通过 openssl 验证也很有趣

可变长度符号:

openssl dgst -ecdsa-with-SHA1 -verify ec-pub.pem -signature sig2.bin data
Verification Failure

失败(因为用 sha256 叹息)

不可变长度符号:

openssl dgst -ecdsa-with-SHA1 -verify ec-pub.pem -signature sig.bin data
Error Verifying Data
33773:error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.6/src/crypto/asn1/asn1_lib.c:153:
33773:error:0D068066:asn1 encoding routines:ASN1_CHECK_TLEN:bad object header:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.6/src/crypto/asn1/tasn_dec.c:1331:
33773:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.6/src/crypto/asn1/tasn_dec.c:387:Type=ECDSA_SIG

所以我们需要将短符号格式化为 DER 以验证加密

fix_signed(<<First:8, _/bytes>> = Value) when First >= 16#80 -> <<0:8, Value/bytes>>;
fix_signed(Value) -> Value.

get_signed_big_endian(Value) ->
  Res = binary:encode_unsigned(Value),
  fix_signed(Res).

sign_to_der_format(<<R:256, S:256>>) ->

  RBin = get_signed_big_endian(R),
  SBin = get_signed_big_endian(S),

  RSize = byte_size(RBin),
  SSize = byte_size(SBin),

  Len = 4 + RSize + SSize,

  <<48:8, Len:8, 2:8, RSize:8, RBin/bytes, 2:8, SSize:8, SBin/bytes>>.