From 2f47a9d4f25ba6b28e82744daaa81e6967bde1fb Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 8 Jan 2024 00:13:35 +0800 Subject: [PATCH] more rework of pre-hash argument While ideally we'd be as light-fingered as possible on the cryptographic APIs, unfortunately the reality is we have exactly one hardware hash engine and we can't just leave the context laying around. Given that the verification routine would consume the hash, that would make it unavailable for use later on to pass to the system. So, this patch set causes the pre-hash algorithm to take a simple slice-u8 instead of a whole hash context. The downside of this is it erases the type information about needing a 64-bit hash to go into the context. This is partially mitigated with an assert that gives us a run-time check of this property. --- loader/ed25519-dalek-loader/src/keypair.rs | 8 ++++---- loader/ed25519-dalek-loader/src/public.rs | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/loader/ed25519-dalek-loader/src/keypair.rs b/loader/ed25519-dalek-loader/src/keypair.rs index f8f757872..adb28f7b6 100644 --- a/loader/ed25519-dalek-loader/src/keypair.rs +++ b/loader/ed25519-dalek-loader/src/keypair.rs @@ -320,14 +320,14 @@ impl Keypair { /// ``` /// /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1 - pub fn verify_prehashed( + pub fn verify_prehashed( &self, - prehashed_message: D, + // note: this used to be D: Digest, but because we have only + // one hardware hasher, this has to be finalized before handing down to the function. + prehashed_message: &[u8], context: Option<&[u8]>, signature: &ed25519::Signature, ) -> Result<(), SignatureError> - where - D: Digest, { self.public.verify_prehashed(prehashed_message, context, signature) } diff --git a/loader/ed25519-dalek-loader/src/public.rs b/loader/ed25519-dalek-loader/src/public.rs index 6c8cd2821..d63b0c260 100644 --- a/loader/ed25519-dalek-loader/src/public.rs +++ b/loader/ed25519-dalek-loader/src/public.rs @@ -13,7 +13,6 @@ use core::convert::TryFrom; use core::fmt::Debug; use curve25519_dalek_loader::constants; -use curve25519_dalek_loader::digest::generic_array::typenum::U64; use curve25519_dalek_loader::digest::Digest; use curve25519_dalek_loader::edwards::CompressedEdwardsY; use curve25519_dalek_loader::edwards::EdwardsPoint; @@ -179,19 +178,23 @@ impl PublicKey { /// /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1 #[allow(non_snake_case)] - pub fn verify_prehashed( + pub fn verify_prehashed( &self, - prehashed_message: D, + // note: this used to be D: Digest, but because we have only + // one hardware hasher, this has to be finalized before handing down to the function. + prehashed_message: &[u8], context: Option<&[u8]>, signature: &ed25519::Signature, ) -> Result<(), SignatureError> - where - D: Digest, { + // This assert does a run-time check on what would otherwise be a static check in Rust + // due to the original type bounds on this function. However, because we have to finalize + // the hash (due to there being only a single hardware hash engine) prior to passing into + // this function, we also erase the type information. This assert somewhat makes up for that. + assert!(prehashed_message.len() == 64, "Incorrect pre-hash length"); + let signature = InternalSignature::try_from(signature)?; - // The prehash needs to be finalized before we create a new hasher instance. We - // only have one hardware hasher available. - let prehash = prehashed_message.finalize(); + let mut h: Sha512 = Sha512::default(); let R: EdwardsPoint; let k: Scalar; @@ -207,7 +210,7 @@ impl PublicKey { h.update(ctx); h.update(signature.R.as_bytes()); h.update(self.as_bytes()); - h.update(prehash.as_slice()); + h.update(prehashed_message); k = Scalar::from_hash(h); R = EdwardsPoint::vartime_double_scalar_mul_basepoint(&k, &(minus_A), &signature.s);