ecdsa signature verification

876 Views Asked by At

next problem: message is signed in python application and will be verified in erlang with public key

python can sign message in two ways:

  1. with muttable length of signature - erlang verifies ok

  2. with imuttable length of signatur - erlang cant verify it

some information from python lib: There are also multiple ways to represent a signature. The default sk.sign() and vk.verify() methods present it as a short string, for simplicity and minimal overhead. To use a different scheme, use the sk.sign(sigencode=) and vk.verify(sigdecode=) arguments. There are helper funcions in the "ecdsa.util" module that can be useful here.

Examples:

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

also interesting thing if verify via openssl

Mutable length sign:

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

fails (because sigh with sha256)

Immutable length sign:

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

There are 1 best solutions below

0
On BEST ANSWER

so we needed to format short sign to DER which verifies crypto

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>>.