Git Product home page Git Product logo

Comments (2)

tarcieri avatar tarcieri commented on July 3, 2024 1

This is a somewhat dangerous API in that failure to use a hash function on the input message can lead to arbitrary signature forgeries. For that reason we do not provide a high-level interface to it.

If you really, really need such an API, you'll need to use the low-level SignPrimitive API:

https://docs.rs/ecdsa/latest/ecdsa/hazmat/trait.SignPrimitive.html

You can compute a scalar from a known existing digest using <Scalar as Reduce<U256>>::from_be_bytes. See the implementation of DigestSigner as an example:

https://docs.rs/ecdsa/latest/src/ecdsa/sign.rs.html#179-181

from elliptic-curves.

davxy avatar davxy commented on July 3, 2024 1

I managed to have something working.

This is a reference for who tries to do something similar:

use ecdsa::{
    elliptic_curve::{
        generic_array::{typenum::U32, GenericArray},
        ops::Reduce,
    },
    hazmat::{rfc6979_generate_k, SignPrimitive},
    signature::PrehashSignature,
    RecoveryId,
};
use k256::{
    ecdsa::{Signature, SigningKey},
    NonZeroScalar, Scalar, U256,
};
use rand::rngs::OsRng;

fn sign_prehashed(hash: [u8; 32]) -> (Signature, Option<RecoveryId>) {
    let signing_key = SigningKey::random(&mut OsRng);

    let hash_array: GenericArray<u8, U32> = GenericArray::from_slice(&hash).clone();
    let hash_scalar = <Scalar as Reduce<U256>>::from_be_bytes_reduced(hash_array);

    let priv_bytes = signing_key.to_bytes();
    let priv_scalar = <NonZeroScalar as Reduce<U256>>::from_be_bytes_reduced(priv_bytes);

    let k = rfc6979_generate_k::<_, <Signature as PrehashSignature>::Digest>(
        &priv_scalar,
        &hash_scalar,
        &[],
    );

    priv_scalar.try_sign_prehashed(**k, hash_scalar).unwrap()
}

from elliptic-curves.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.