From 55e4450a65eb47c74620ef11698618035faca985 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 17 Jan 2024 04:30:51 +0000 Subject: [PATCH] rfc6979: add P-521 + SHA-512 test vectors (#775) These now work after #781 --- rfc6979/src/lib.rs | 33 +++++++++++++++++++++++++++++++-- rfc6979/tests/lib.rs | 14 -------------- 2 files changed, 31 insertions(+), 16 deletions(-) delete mode 100644 rfc6979/tests/lib.rs diff --git a/rfc6979/src/lib.rs b/rfc6979/src/lib.rs index 4e718128..e6f62d04 100644 --- a/rfc6979/src/lib.rs +++ b/rfc6979/src/lib.rs @@ -161,9 +161,12 @@ where #[cfg(test)] mod tests { - use crate::{consts::U21, generate_k}; + use crate::{ + consts::{U21, U66}, + generate_k, Array, + }; use hex_literal::hex; - use sha2::Sha256; + use sha2::{Digest, Sha256, Sha512}; /// "Detailed Example" from RFC6979 Appendix A.1. /// @@ -181,4 +184,30 @@ mod tests { let k = generate_k::(&x.into(), &q.into(), &h2.into(), aad); assert_eq!(k, hex!("023AF4074C90A02B3FE61D286D5C87F425E6BDD81B")); } + + /// Example from RFC6979 Appendix A.2.7. + #[test] + fn p521_sha512() { + let q = hex!( + "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409" + ); + + let x = hex!( + "00FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538" + ); + + // Hash message and emulate `bits2octets` to produce the input digest + let message = "sample"; + let mut h = Array::::default(); + h[2..].copy_from_slice(&Sha512::digest(message)); + + let aad = b""; + let k = generate_k::(&x.into(), &q.into(), &h.into(), aad); + + let expected_k = hex!( + "01DAE2EA071F8110DC26882D4D5EAE0621A3256FC8847FB9022E2B7D28E6F10198B1574FDD03A9053C08A1854A168AA5A57470EC97DD5CE090124EF52A2F7ECBFFD3" + ); + + assert_eq!(k, expected_k); + } } diff --git a/rfc6979/tests/lib.rs b/rfc6979/tests/lib.rs deleted file mode 100644 index 1b622f8b..00000000 --- a/rfc6979/tests/lib.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Smoke tests which use `MockCurve` - -#![cfg(feature = "dev")] - -use elliptic_curve::dev::MockCurve; - -type Signature = ecdsa::Signature; -type SignatureBytes = ecdsa::SignatureBytes; - -#[test] -fn rejects_all_zero_signature() { - let all_zero_bytes = SignatureBytes::default(); - assert!(Signature::try_from(all_zero_bytes.as_ref()).is_err()); -}