Skip to content
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

p256: add RFC6979 test vector #147

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 43 additions & 28 deletions p256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,37 +143,52 @@ impl VerifyPrimitive<NistP256> for AffinePoint {

#[cfg(all(test, feature = "ecdsa"))]
mod tests {
use crate::{test_vectors::ecdsa::ECDSA_TEST_VECTORS, NistP256};

#[cfg(feature = "rand")]
use {
crate::{BlindedScalar, Scalar},
core::convert::TryInto,
ecdsa_core::hazmat::SignPrimitive,
elliptic_curve::{rand_core::OsRng, FromBytes},
};

mod signing {
use super::{NistP256, ECDSA_TEST_VECTORS};
mod sign {
use crate::{
ecdsa::{signature::Signer as _, Signer},
test_vectors::ecdsa::ECDSA_TEST_VECTORS,
NistP256,
};
use hex_literal::hex;

#[cfg(feature = "rand")]
use crate::{elliptic_curve::rand_core::OsRng, BlindedScalar, Scalar};

ecdsa_core::new_signing_test!(NistP256, ECDSA_TEST_VECTORS);
}

mod verification {
use super::{NistP256, ECDSA_TEST_VECTORS};
ecdsa_core::new_verification_test!(NistP256, ECDSA_TEST_VECTORS);
// Test vector from RFC 6979 Appendix 2.5 (NIST P-256 + SHA-256)
// <https://tools.ietf.org/html/rfc6979#appendix-A.2.5>
#[test]
fn rfc6979() {
let x = &hex!("c9afa9d845ba75166b5c215767b1d6934e50c3db36e89b127b8a622b120f6721");
let signer = Signer::from_bytes(x).unwrap();
let signature = signer.sign(b"sample");
assert_eq!(
signature.as_ref(),
&hex!(
"efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716
f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8"
)[..]
);
}

#[cfg(feature = "rand")]
#[test]
fn scalar_blinding() {
let vector = &ECDSA_TEST_VECTORS[0];
let d = Scalar::from_bytes(vector.d.try_into().unwrap()).unwrap();
let k = Scalar::from_bytes(vector.k.try_into().unwrap()).unwrap();
let k_blinded = BlindedScalar::new(k, &mut OsRng);
let z = Scalar::from_bytes(vector.m.try_into().unwrap()).unwrap();
let sig = d.try_sign_prehashed(&k_blinded, &z).unwrap();

assert_eq!(vector.r, sig.r().as_slice());
assert_eq!(vector.s, sig.s().as_slice());
}
}

#[cfg(feature = "rand")]
#[test]
fn scalar_blinding() {
let vector = &ECDSA_TEST_VECTORS[0];
let d = Scalar::from_bytes(vector.d.try_into().unwrap()).unwrap();
let k = Scalar::from_bytes(vector.k.try_into().unwrap()).unwrap();
let k_blinded = BlindedScalar::new(k, &mut OsRng);
let z = Scalar::from_bytes(vector.m.try_into().unwrap()).unwrap();
let sig = d.try_sign_prehashed(&k_blinded, &z).unwrap();

assert_eq!(vector.r, sig.r().as_slice());
assert_eq!(vector.s, sig.s().as_slice());
mod verify {
use crate::{test_vectors::ecdsa::ECDSA_TEST_VECTORS, NistP256};
ecdsa_core::new_verification_test!(NistP256, ECDSA_TEST_VECTORS);
}
}