Skip to content

Add sign_digest to EcdsaKeyPair to allow signing of pre-digested messages.#915

Open
IanLuites wants to merge 1 commit intobriansmith:masterfrom
IanLuites:b/allow-ecdsa-sign-digest
Open

Add sign_digest to EcdsaKeyPair to allow signing of pre-digested messages.#915
IanLuites wants to merge 1 commit intobriansmith:masterfrom
IanLuites:b/allow-ecdsa-sign-digest

Conversation

@IanLuites
Copy link
Copy Markdown

I need to sign a relatively large message and was using digest::Context to piece wise stream and update the digest.

I did however find out that sign does not accept a digest, but only an undigested message.

This PR would add a sign_digest sibling, which verifies the digest is generated with the correct (matching) algorithm, and then signs it.

@IanLuites IanLuites changed the title Add sign_digest to EcdsaKeyPair to allow signing of pre-digests messages. Add sign_digest to EcdsaKeyPair to allow signing of pre-digested messages. Nov 13, 2019
Copy link
Copy Markdown
Owner

@briansmith briansmith left a comment

Choose a reason for hiding this comment

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

Thanks for doing this. I'll get this merged right away if you make the changes I requested.

digest: digest::Digest,
) -> Result<signature::Signature, error::Unspecified> {
// Step 4 (out of order, already performed by caller).
if digest.algorithm() == self.alg.digest_alg {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please use the early return style: if ... != ... { return Err(...); }

);
}

#[test]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Tests of the public API should be done in tests/ecdsa_tests.rs.

I suggest that you add a constructor pub(crate) try_from_test_vector to ring::digest::Digest that accepts a digest algorithm and a precomputed value. Then you can rewrite the existing tests to use your new function instead of calling sign_ directly as they currently do.

/// generated by `rng`.
///
/// The `digest` algorithm must match that of the signing algorithm.
pub fn sign_digest(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please name this sign_digest_less_safe.

Please add a note like the following to the documentation: "In general, it is not safe to sign an arbitrary digest. Ensure that you only sign digests that you have computed yourself, or that you otherwise know are safe to sign. It could be a bad mistake to sign an attacker-controlled digest."

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants