Skip to content

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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/ec/suite_b/ecdsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ impl EcdsaKeyPair {

/// Returns the signature of the `message` using a random nonce
/// generated by `rng`.
///
/// The `message` is digested with the appropriate algorithm.
pub fn sign(
&self,
rng: &dyn rand::SecureRandom,
Expand All @@ -163,6 +165,23 @@ impl EcdsaKeyPair {
self.sign_(rng, h)
}

/// Returns the signature of the `digest` using a random nonce
/// generated by `rng`.
///
/// The `digest` algorithm must match that of the signing algorithm.
pub fn sign_digest(
Copy link
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."

&self,
rng: &dyn rand::SecureRandom,
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
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(...); }

self.sign_(rng, digest)
} else {
Err(error::Unspecified)
}
}

/// Returns the signature of message digest `h` using a "random" nonce
/// generated by `rng`.
fn sign_(
Expand Down Expand Up @@ -435,6 +454,44 @@ mod tests {
);
}

#[test]
Copy link
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.

fn signature_ecdsa_sign_digest_fixed_test() {
test::run(
test_file!("ecdsa_sign_fixed_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

let curve_name = test_case.consume_string("Curve");
let digest_name = test_case.consume_string("Digest");
let msg = test_case.consume_bytes("Msg");
let d = test_case.consume_bytes("d");
let q = test_case.consume_bytes("Q");
let k = test_case.consume_bytes("k");

let expected_result = test_case.consume_bytes("Sig");

let alg = match (curve_name.as_str(), digest_name.as_str()) {
("P-256", "SHA256") => &signature::ECDSA_P256_SHA256_FIXED_SIGNING,
("P-384", "SHA384") => &signature::ECDSA_P384_SHA384_FIXED_SIGNING,
_ => {
panic!("Unsupported curve+digest: {}+{}", curve_name, digest_name);
}
};

let private_key =
signature::EcdsaKeyPair::from_private_key_and_public_key(alg, &d, &q).unwrap();
let rng = test::rand::FixedSliceRandom { bytes: &k };

let digest = crate::digest::digest(alg.digest_alg, &msg);
let actual_result = private_key.sign_digest(&rng, digest).unwrap();

assert_eq!(actual_result.as_ref(), &expected_result[..]);

Ok(())
},
);
}

#[test]
fn signature_ecdsa_sign_asn1_test() {
test::run(
Expand Down Expand Up @@ -471,4 +528,42 @@ mod tests {
},
);
}

#[test]
fn signature_ecdsa_sign_digest_asn1_test() {
test::run(
test_file!("ecdsa_sign_asn1_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

let curve_name = test_case.consume_string("Curve");
let digest_name = test_case.consume_string("Digest");
let msg = test_case.consume_bytes("Msg");
let d = test_case.consume_bytes("d");
let q = test_case.consume_bytes("Q");
let k = test_case.consume_bytes("k");

let expected_result = test_case.consume_bytes("Sig");

let alg = match (curve_name.as_str(), digest_name.as_str()) {
("P-256", "SHA256") => &signature::ECDSA_P256_SHA256_ASN1_SIGNING,
("P-384", "SHA384") => &signature::ECDSA_P384_SHA384_ASN1_SIGNING,
_ => {
panic!("Unsupported curve+digest: {}+{}", curve_name, digest_name);
}
};

let private_key =
signature::EcdsaKeyPair::from_private_key_and_public_key(alg, &d, &q).unwrap();
let rng = test::rand::FixedSliceRandom { bytes: &k };

let digest = crate::digest::digest(alg.digest_alg, &msg);
let actual_result = private_key.sign_digest(&rng, digest).unwrap();

assert_eq!(actual_result.as_ref(), &expected_result[..]);

Ok(())
},
);
}
}