From d88e446a33fda3d25310f3222112286ea4782575 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 5 Jan 2024 16:07:23 +0800 Subject: [PATCH] finalize pre-hash before initializing a new hash we only have one hash engine in hardware, and we want to use hardware hashing at boot to speed things up as much as possible and reduce code size of the bootloader. This patch causes the pre-hash to finalize before a new hash is initialized which folds in all the other key parameters. --- loader/ed25519-dalek-loader/src/public.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/loader/ed25519-dalek-loader/src/public.rs b/loader/ed25519-dalek-loader/src/public.rs index c2242b4c3..6c8cd2821 100644 --- a/loader/ed25519-dalek-loader/src/public.rs +++ b/loader/ed25519-dalek-loader/src/public.rs @@ -189,7 +189,9 @@ impl PublicKey { D: Digest, { 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; @@ -205,7 +207,7 @@ impl PublicKey { h.update(ctx); h.update(signature.R.as_bytes()); h.update(self.as_bytes()); - h.update(prehashed_message.finalize().as_slice()); + h.update(prehash.as_slice()); k = Scalar::from_hash(h); R = EdwardsPoint::vartime_double_scalar_mul_basepoint(&k, &(minus_A), &signature.s);