Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Schnorr signature #93

Closed
wants to merge 3 commits into from
Closed

Add Schnorr signature #93

wants to merge 3 commits into from

Conversation

kurotych
Copy link
Contributor

No description provided.

@prusnak prusnak added crypto Stand-alone cryptography library used by both Trezor Core and the Trezor Legacy firmware feature labels Apr 21, 2019
@prusnak prusnak added this to the backlog milestone Apr 21, 2019
crypto/schnorr.c Outdated Show resolved Hide resolved
crypto/schnorr.h Outdated Show resolved Hide resolved
const char *k_hex;
const char *s_hex;
const char *r_hex;
} test_cases[] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do these test vectors come from?

If these are your own, can you please also add some that are not your own? Simple google search for "schnorr test vectors" returns some results such as:

Copy link
Contributor Author

@kurotych kurotych Apr 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got test vectors from results this function.

crypto/schnorr.c Outdated Show resolved Hide resolved
@prusnak prusnak modified the milestones: backlog, 2019-06 Apr 21, 2019
@prusnak prusnak self-assigned this Apr 21, 2019
@prusnak
Copy link
Member

prusnak commented Apr 21, 2019

Squashed and merged in 8114e0f

Thanks!

@prusnak prusnak closed this Apr 21, 2019
@prusnak
Copy link
Member

prusnak commented May 23, 2019

Hey @armatusmiles

I have reverted the commit and removed the Schnorr implementation from master. @onvej-sl found out the Schnorr implementation you provided is insecure and he will tell you more.

@kurotych
Copy link
Contributor Author

@prusnak Hello. Thank you for feedback. I’m sorry.

@hewigovens
Copy link

Hi @prusnak @onvej-sl

could you please share more details about the implementation? I believe @armatusmiles implemented Zilliqa scheme (https://github.com/Zilliqa/Zilliqa/blob/master/src/libCrypto/Schnorr.cpp), which is audited by NCC group (Zilliqa team tells us)

Thanks

@onvej-sl
Copy link
Contributor

onvej-sl commented May 27, 2019

Hi @hewigovens,

I found the implementation insecure for the following reasons:

  1. In schnorr_verify, you should check that both r and s parts of the signature are less than the curve order. That's how Schnorr signature is usually implemented, Zilliqa not being an exception.
    By the way, scalar_multiply assumes that exponent is less than the order, so in a special case verification failed with an assertion error.
  2. You represent a signature by two bignum256 numbers. Since every number has several bignum256 representation (which may be partly reduced, reduced or normalised), great care must be taken when dealing with it. The raw format (an output of bn_write_be) would be better.
    Assume bignum256 r is the r part of a signature on secp256k1 curve with r.val = {0x10364142, 0x3f497a33, 0x348a03bb, 0x2bb739ab, 0x3ffffeba, 0x3fffffff, 0x3fffffff, 0xffffffff, 0x0000fffc}. Such r pass through the condition if (bn_is_less(&curve->order, &sign->r)) return 4;, however it shouldn't (it's equal to the curve order plus one).
  3. Your nonce is user defined. Since a nonce reuse reveals the private key, it should be generated randomly or deterministically (for example as defined in RFC 6979) right in ecdsa_sign.

@hewigovens
Copy link

@onvej-sl Thanks for your elaborations, for 3 we did use generate_k_rfc6979 to generate the nonce in a wrapper method, it's just not ready to push to upstream. we'll address 1 and 2 shortly

@onvej-sl onvej-sl self-assigned this May 27, 2019
@ansnunez
Copy link

@ansnunez
Copy link

@onvej-sl for 2, I think calc_r is missing a call to bn_nnmod to avoid the problem in your example.

Here is the equivalent code in Zilliqa:
https://github.com/Zilliqa/Zilliqa/blob/4ed7a542efc98ef00ae38688f66833492786536e/src/libCrypto/Schnorr.cpp#L158

If we put this code in place, would it then be OK to keep the two bignum256 representation?

@onvej-sl
Copy link
Contributor

onvej-sl commented May 28, 2019

Hi @onvej-sl for 1 I think the checks are already there? The 4 lines starting from https://github.com/TrustWallet/wallet-core/blob/1d67cd877ae495a603715a2b649c1271cbc8ff83/trezor-crypto/src/schnorr.c#L116

They are not. What if r or s is equal to the order?

@onvej-sl for 2, I think calc_r is missing a call to bn_nnmod to avoid the problem in your example.

It won't solve the problem. The problem is that the r I provided is not normalized. Please see bignum.c comments for normalized, reduced and partly reduced numbers.

If we put this code in place, would it then be OK to keep the two bignum256 representation?

What is the reason you prefer bignum256 over raw format? I think I've proven it not easy to work with bignum256.

hewigovens added a commit to trustwallet/wallet-core that referenced this pull request May 31, 2019
@hewigovens
Copy link

@onvej-sl could you please review this pr? @ansnunez tries to fix all the concerns.

Thanks!

@onvej-sl
Copy link
Contributor

onvej-sl commented Jun 4, 2019

@onvej-sl could you please review this pr? @ansnunez tries to fix all the concerns.

Good job! It seems to be OK. Are you going to push it to trezor-firmware?

@hewigovens
Copy link

Thanks, we will push it to upstream later

hewigovens added a commit to trustwallet/wallet-core that referenced this pull request Jun 4, 2019
hewigovens added a commit to trustwallet/wallet-core that referenced this pull request Jun 4, 2019
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
naruhitokaide added a commit to naruhitokaide/core-wallet that referenced this pull request Aug 23, 2022
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
webcat359 added a commit to webcat359/wallet-core that referenced this pull request Aug 29, 2022
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
Ninja-21-dev added a commit to Ninja-21-dev/core-cross-blockchain-wallet that referenced this pull request Oct 28, 2022
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
crazycodecreator007 added a commit to crazycodecreator007/core-wallet that referenced this pull request Feb 8, 2023
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
0xExp-po added a commit to 0xExp-po/core-wallet that referenced this pull request Jan 9, 2024
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
10kick-1goal added a commit to 10kick-1goal/core-wallet that referenced this pull request Jan 10, 2024
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
Herocoin816 added a commit to Herocoin816/Wallet that referenced this pull request Sep 16, 2024
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
gncao523 added a commit to gncao523/core-wallet that referenced this pull request Nov 15, 2024
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
unicorn79dev added a commit to unicorn79dev/core-wallet that referenced this pull request Jan 13, 2025
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
christophehaight0307 pushed a commit to christophehaight0307/core-wallet that referenced this pull request Jan 21, 2025
* add zilliqa schnorr tests

* Fix trezor/trezor-firmware#93 (comment)

* Fix tabs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Stand-alone cryptography library used by both Trezor Core and the Trezor Legacy firmware
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants