ecdsa签名验证

时间:2017-09-13 11:25:59

标签: python erlang ecdsa curves

下一个问题: 消息在python应用程序中签名,将在erlang中使用公钥进行验证

python可以通过两种方式签署消息:

1)具有可变的签名长度 - erlang验证确定

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

来自python lib的一些信息: 还有多种方式来表示签名。默认的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

1 个答案:

答案 0 :(得分:1)

所以我们需要将短符号格式化为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>>.