-
Notifications
You must be signed in to change notification settings - Fork 770
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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( | ||
&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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the early return style: |
||
self.sign_(rng, digest) | ||
} else { | ||
Err(error::Unspecified) | ||
} | ||
} | ||
|
||
/// Returns the signature of message digest `h` using a "random" nonce | ||
/// generated by `rng`. | ||
fn sign_( | ||
|
@@ -435,6 +454,44 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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( | ||
|
@@ -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(()) | ||
}, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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."