Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

formatSignature can not take output of calculateSignature since it already adds ASN.1 lenghts #7

Open
s00500 opened this issue Aug 16, 2019 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@s00500
Copy link

s00500 commented Aug 16, 2019

Hey
I was trying to verify a signature generated by this library using openssl on my computer.

It did not work until I finally discovered this issue

Since OpenSSL likes Signatures in the ASN.1 menthod I took the convenient function formatSignature like this:

  /*
   * Generate a signature NIST-P256
   */
  printGreen("Generate Signature ... ");
  ret = trustX.calculateSignature(hash, hashLen, eFIRST_DEVICE_PRIKEY_1, formSign, signLen);
  Serial.printf("Code: %u\n", ret);
  ASSERT(ret);
  output_result("Signature", formSign, signLen);

  printGreen("Format Signature ... ");
  ret = trustX.formatSignature(formSign, signLen, format, formatLen);
  ASSERT(ret);
  output_result("Signature Formated", format, formatLen);

Theoretically that should be fine, or did I misunderstand something?

As it is this does not work correctly though because the calculate Signature function will already add the 02 20 tags and the 00 for negative values before the r and s numbers. When I then use the formatSignature function they are added again and the rest of the buffer, so 4-6 bytes of the actual signature are discarded. Then verification of course does not work

When I take the data coming out of calculateSignature and add 0x30 and the length infront of it I get the correct ASN.1 format

Either I did not understand how this is supposed to be used or this is definitly a bug, in the first case pls still add this to the docs!

Thanks

@ayushev
Copy link
Member

ayushev commented Aug 20, 2019

Hi @s00500 , the format signature was intended originally for a different purpose.
The output of the calculateSignature is two DER encoded integers like below

0x000000: 02 20 38 0f 56 c8 90 53 18 9d 8f 58 b4 46 35 a0 . 8.V..S...X.F5.
0x000010: d7 07 63 ef 9f a2 30 64 93 e4 3d bf 7b db 57 a1 ..c...0d..=.{.W.
0x000020: b6 d7 02 20 4f 5e 3a db 6b 1a eb ac 66 9a 15 69 ... O^:.k...f..i
0x000030: 0d 7d 46 5b 44 72 40 06 a5 7b 06 84 0f d7 6e 0f .}F[Dr@..{....n.
0x000040: 4b 45 7f 50                                     KE.P 

In order to let OpenSSL verify it, you need to make it ASN.1. For this as you correctly said adding length and tag should be sufficient.

Meanwhile I treat this issue as a bug in the formatSignature() function. It should be fixed in the next version.

@ayushev ayushev self-assigned this Aug 20, 2019
@ayushev ayushev added the bug Something isn't working label Aug 20, 2019
@s00500
Copy link
Author

s00500 commented Aug 20, 2019

Great, thanks very much :-)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants