Skip to content

Add ECDSA P-256 signing support #207

Closed

Description

@briansmith

This is harder than EdDSA because there's no code to do the actual ECDSA signing anymore; the ECDSA_sign function in crypto/ecdsa/ecdsa.c was removed to help with the migration to the new Rust ECC code. So, a new implementation of ECDSA signing in Rust is necessary.

For all platforms it is OK (preferable, even) to use crypto::ec::PrivateKey::generate for the key generation (i.e. base point multiplication). So, that part is already done.

I have some code sitting around to do the constant-time modular inversions (mod q) and (mod n). I will try to integrate it...soonish.

For 64-bit platforms, we can use crypto/ec/p256-64.c and crypto/ec/p256-x86_64.c for the variable point multiplication (the multiplication done during the signing operation). However, we don't have any usable code for 32-bit platforms yet (at least, not checked in).

For 32-bit ARM, x86, and AAarch64, I will eventually import in the ecp_nistz256 field operations from OpenSSL. Actually, I'm planning to do this real soon. However, I don't think the ecp_nistz256 group operations are good to use for any platform except x86-64 (even then...). We might try, instead, adapting the p256-64.c variable point multiplication to work using the ecp_nistz256 field operations (which would require adapting it to work on Montgomery-encoded field elements).

Similarly, we need to do make new nonce generation, as the old C code that Adam Langley wrote was removed. It may be worth copying how golang is doing it. Regardless, it would be nice to do as much of this as possible in Rust.

NIST has some test vectors for ECDSA signing, which are the absolute minimum level of testing necessary before the API can be exposed.

There is also no code for serializing private keys or deserializing them, which is pretty essential to most users of ECDSA signing. This should probably be split off into its own task as it is a fair amount of work on its own.

See also any general notes about signing APIs in #205.

Activity

  1. changed the title [-]Add ECDSA signing support[/-] [+]Add ECDSA P-256 signing support[/+] on Jun 3, 2016
  2. briansmith commented on Jun 3, 2016

    @briansmith
    OwnerAuthor
    • digest -> scalar conversion
    • dedicated, optimized, constant-time P-256 field operations.
    • constant-time inversion mod n (assuming scalar multiplication and squaring are constant-time)
    • constant-time Jacobian -> affine conversion (including constant-time inversion mod q)
    • constant-time point doubling and point addition*
    • constant-time base point multiplication for x86-64.
    • constant-time base point multiplication.for other platforms.
    • Serialization of signatures (ASN.1 DER)
    • Serialization of signatures (Fixed-length PKCS311 style)
    • The actual ECDSA signing function that combines all the above steps.
    • Tests
    • Benchmarks in crypto-bench
    • key generation (partially done in ring::ec::PrivateKey)
    • Serialization of private keys
    • Deserialization of private keys
    • Audit and document side channel resistance, including base assumptions.
    • The point addition is constant-time assuming the exceptional cases aren't hit during ECDH and ECDSA signing, which is the same assumption that OpenSSL makes. We should verify that that is the case.
    • Accidental nonce reuse/poor-PRNG hardening.
  3. self-assigned this
    on Jun 6, 2016
  4. briansmith commented on Jul 3, 2016

    @briansmith
    OwnerAuthor

    Status update: All the checked-off stuff above is now in the main ring repo.

  5. removed their assignment
    on Jul 24, 2016
  6. briansmith commented on Jul 24, 2016

    @briansmith
    OwnerAuthor

    Status update: More checked-off stuff is now in the ring repo. Unassigning myself in case somebody else wants to take this on before I get to it.

  7. Keats commented on Jan 26, 2017

    @Keats

    Is it still planned? It was asked for jsonwebtoken

    I'm entirely unqualified to pick this up though :(

  8. briansmith commented on Jan 30, 2017

    @briansmith
    OwnerAuthor

    I'm entirely unqualified to pick this up though :(

    I doubt that is true!

    Is it still planned?

    Yes, I plan to do land it shortly, after I take care of the PR backlog.

  9. added this to the 0.7.n milestone on Jan 30, 2017
  10. self-assigned this
    on Jan 30, 2017
  11. modified the milestones: 0.7.n, 0.8.n on Apr 10, 2017
  12. 16 remaining items

  13. briansmith commented on Oct 27, 2018

    @briansmith
    OwnerAuthor

    But then you have to have several functions depending on which algorithm to use, as HMAC won't have a key pair for example.

    HMAC and digital signatures shouldn't have the same API anyway. The fact that the syntax is the same for both in JOSE is widely seen as one of its biggest design flaws.

  14. manifest commented on Oct 27, 2018

    @manifest

    I still don't understand how the problem that I described might be solved. @briansmith could you please elaborate on how you see using ring to verify a signature in the untrusted environment?

  15. manifest commented on Nov 7, 2018

    @manifest

    @briansmith Sorry to bother you, I'm just trying to find a solution for our problem.

    • Can we have Private Key and Public Key structs for ECDSA that we can use with sign / verify functions?
    • Can we expose API for importing / exporting these keys?

    It's what we need to solve the problem described above and start using ring library, or maybe you could suggest a different approach.

  16. briansmith commented on Nov 7, 2018

    @briansmith
    OwnerAuthor

    I'm getting the impression we might be talking past each other since we seem to have some misunderstandings.

    Very often in the case of asymmetric signing, you only have the public key so you wouldn't be able to build a KeyPair as @manifest mentioned.

    The ECDSA verification API already takes &[u8] for the public key. (If you only have the public key then you can only verify signatures, not generate signatures.)

    Only the ECDSA signing API requires a KeyPair object as input.

    So my suggestion is to change the JWT crate to require a KeyPair object for signing (only). That would be a great improvement to the JWT crate. (Taking the private key as a &[u8] means that the user of the crate would have to keep the private key bytes easily-accessible which is bad.)

    In the future we'll have a type signature::PublicKey(&[u8]) which will make this a little more symmetric. However, no changes are needed to ring to provide the functionality you need today.

  17. manifest commented on Nov 8, 2018

    @manifest

    How can KeyPair and &[u8] (that will be replaced by PublicKey in the future) be generated, in order to be used in signing and verification APIs? It's impossible without external tools at the moment, am I right?

  18. briansmith commented on Nov 8, 2018

    @briansmith
    OwnerAuthor

    ring::signature;;ECDSAKeyPair::generate_pkcs8 will generate a new ECDSA private key as a serialized PKCS#8 document. ring::signature::key_pair_from_pkcs8 will create a KeyPair from the serialized document. (I understand that these APIs should be more symmetric; it's on the TODO ist

    Incidentally, it turns out I already implemented the thing that was originally being asked for: ring::signature::ECDSAKeyPair::from_private_key_and_public_key will construct a ECDSAKeyPair from the public key bytes and the private key bytes directly. IIRC, I actually added that API to support deserialization of JWKs.

  19. manifest commented on Nov 20, 2018

    @manifest

    @briansmith I'm following the steps you described above. I've generated KeyPair, then I used it to sign a message. How can I generate a public key from KeyPair in order to pass it to another application that will use it to verify the signed message?

    There is public_key_bytes() for Ed25519KeyPair but nothing for ECDSAKeyPair.

    Example

        let rng = rand::SystemRandom::new();
        let alg = &signature::ECDSA_P256_SHA256_ASN1_SIGNING;
        let pkcs8 = signature::ECDSAKeyPair::generate_pkcs8(alg, &rng).unwrap();
        let key_pair = signature::key_pair_from_pkcs8(alg, untrusted::Input::from(pkcs8.as_ref())).unwrap();
    
        const MESSAGE: &'static [u8] = b"hello, world";
        let sig = signature::sign(&key_pair, &rng, untrusted::Input::from(MESSAGE)).unwrap();
    
    //    signature::verify(
    //        alg, 
    //        untrusted::Input::from(keys.public_key_bytes()),
    //        untrusted::Input::from(MESSAGE),
    //        untrusted::Input::from(sig.as_ref())
    //    ).unwrap();
  20. briansmith commented on Dec 22, 2018

    @briansmith
    OwnerAuthor

    @Keats @manifest Please try ring 0.14.0-alpha2. In particular, look at ring::signature::KeyPair::public_key(). I've temporarily removed the "generic" API that was was working on and I've renamed some types (RSA* -> Rsa*, ECDSA* -> Ecdsa*) and made some other minor changes which will (I hope) eventually make everything easier to use.

  21. briansmith commented on Dec 22, 2018

    @briansmith
    OwnerAuthor

    I'm closing this because for the most part it was done. There are some details that need to be taken care of but it doesn't make sense to leave this issue open any more. If you run into troubles, please file a new issue.

  22. manifest commented on Dec 22, 2018

    @manifest

    Thanks. Everything looks good. The only problem that it's still impossible to retrieve a public key because public_key function is private. I've opened an issue.

  23. briansmith commented on Dec 22, 2018

    @briansmith
    OwnerAuthor

    Thanks. Everything looks good. The only problem that it's still impossible to retrieve a public key because public_key function is private.

    Thanks for the feedback. I replied in the issue you filed. Let's continue the discussion there.

  24. moeziniaf commented on Jun 7, 2020

    @moeziniaf

    @briansmith I'm trying to figure it out how to generate key pair, sign and verify P-256 ECDSA signature in order to implement ES256 for jsonwebtoken. How can I get a public_key from P-256 ECDSA key_pair to use it with signature::verify function? It looks like there is no key_pair.public_key_bytes() or something similar.

        let rng = rand::SystemRandom::new();
        let alg = &signature::ECDSA_P256_SHA256_ASN1_SIGNING;
        let pkcs8 = signature::ECDSAKeyPair::generate_pkcs8(alg, &rng).unwrap();
        let key_pair = signature::key_pair_from_pkcs8(alg, untrusted::Input::from(pkcs8.as_ref())).unwrap();
    
        const MESSAGE: &'static [u8] = b"hello, world";
        let sig = signature::sign(&key_pair, &rng, untrusted::Input::from(MESSAGE)).unwrap();
    
    //    signature::verify(
    //        alg, 
    //        untrusted::Input::from(keys.public_key_bytes()),
    //        untrusted::Input::from(MESSAGE),
    //        untrusted::Input::from(sig.as_ref())
    //    ).unwrap();

    How do you use verify? I can't figure out how to in version 0.16.14?
    I tried rust::signature::verify and rust::signature::EcdsaKeyPair::verify..

  25. thomaseizinger commented on Oct 31, 2022

    @thomaseizinger

    As far as your specific questions go, basically the answer is "I've written the code for it, but it isn't public yet."

    That is, I have written all this code, but the implementation isn't in a PR yet. The problem is that it is using a 100% random nonce, and I want to change it to use a construction like blogs.cisco.com/security/fips-and-deterministic-ecdsa-achieving-robust-security-and-conformance, which is what BoringSSL also now does. However, I haven't had time to write the DRBG implementation yet, and that's what this is blocked on.

    Is implementation of RFC6979 still something that is planned for ring? I might have a go at implementing it then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions