Skip to content

Commit e0df0e6

Browse files
Replace do_final try_into unwrap with direct array output in hash mldsa, adjust mlkem imports
1 parent 80db9c1 commit e0df0e6

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

crypto/mldsa/src/hash_mldsa.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
//! ```rust
3030
//! use bouncycastle_core::errors::SignatureError;
3131
//! use bouncycastle_mldsa::{HashMLDSA65_with_SHA512, MLDSATrait, HashMLDSA44_with_SHA512};
32-
//! use bouncycastle_core::traits::{Signature, PHSignature, Hash};
32+
//! use bouncycastle_core::traits::{Signature, PHSignature, HashFixedOutput};
3333
//! use bouncycastle_sha2::SHA512;
3434
//!
3535
//! let msg = b"The quick brown fox jumped over the lazy dog";
3636
//!
3737
//! // Here, and in contrast to External Mu mode of ML-DSA, we can pre-hash the message before
3838
//! // even generating the signing key.
39-
//! let ph: [u8; 64] = SHA512::default().hash(msg).as_slice().try_into().unwrap();
39+
//! let ph: [u8; 64] = SHA512::default().hash(msg);
4040
//!
4141
//!
4242
//! let (pk, sk) = HashMLDSA65_with_SHA512::keygen().unwrap();
@@ -898,7 +898,9 @@ impl<
898898
}
899899

900900
fn sign_final_out(self, output: &mut [u8; SIG_LEN]) -> Result<usize, SignatureError> {
901-
let ph: [u8; PH_LEN] = self.hash.do_final().try_into().unwrap();
901+
let mut ph = [0u8; PH_LEN];
902+
let written = self.hash.do_final_out(&mut ph);
903+
debug_assert_eq!(written, PH_LEN);
902904

903905
if self.sk.is_none() && self.seed.is_none() {
904906
return Err(SignatureError::GenericError(
@@ -975,7 +977,9 @@ impl<
975977
self.pk.is_some(),
976978
"Somehow you managed to construct a streaming verifier without a public key, impressive!"
977979
);
978-
let ph: [u8; PH_LEN] = self.hash.do_final().try_into().unwrap();
980+
let mut ph = [0u8; PH_LEN];
981+
let written = self.hash.do_final_out(&mut ph);
982+
debug_assert_eq!(written, PH_LEN);
979983
Self::verify_ph(&self.pk.unwrap(), &ph, Some(&self.ctx[..self.ctx_len]), sig)
980984
}
981985
}

crypto/mldsa_lowmemory/src/hash_mldsa.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
//!
2929
//! ```rust
3030
//! use bouncycastle_core::errors::SignatureError;
31-
//! use bouncycastle_core::traits::{Signature, PHSignature, Hash};
31+
//! use bouncycastle_core::traits::{Signature, PHSignature, HashFixedOutput};
3232
//! use bouncycastle_sha2::SHA512;
3333
//! use bouncycastle_mldsa_lowmemory::{MLDSATrait, HashMLDSA65_with_SHA512, HashMLDSA44_with_SHA512};
3434
//!
3535
//! let msg = b"The quick brown fox jumped over the lazy dog";
3636
//!
3737
//! // Here, and in contrast to External Mu mode of ML-DSA, we can pre-hash the message before
3838
//! // even generating the signing key.
39-
//! let ph: [u8; 64] = SHA512::default().hash(msg).as_slice().try_into().unwrap();
39+
//! let ph: [u8; 64] = SHA512::default().hash(msg);
4040
//!
4141
//!
4242
//! let (pk, sk) = HashMLDSA65_with_SHA512::keygen().unwrap();
@@ -843,7 +843,9 @@ impl<
843843
}
844844

845845
fn sign_final_out(self, output: &mut [u8; SIG_LEN]) -> Result<usize, SignatureError> {
846-
let ph: [u8; PH_LEN] = self.hash.do_final().try_into().unwrap();
846+
let mut ph = [0u8; PH_LEN];
847+
let written = self.hash.do_final_out(&mut ph);
848+
debug_assert_eq!(written, PH_LEN);
847849

848850
if self.sk.is_none() && self.seed.is_none() {
849851
return Err(SignatureError::GenericError(
@@ -923,7 +925,9 @@ impl<
923925
self.pk.is_some(),
924926
"Somehow you managed to construct a streaming verifier without a public key, impressive!"
925927
);
926-
let ph: [u8; PH_LEN] = self.hash.do_final().try_into().unwrap();
928+
let mut ph = [0u8; PH_LEN];
929+
let written = self.hash.do_final_out(&mut ph);
930+
debug_assert_eq!(written, PH_LEN);
927931
Self::verify_ph(&self.pk.unwrap(), &ph, Some(&self.ctx[..self.ctx_len]), &sig[..SIG_LEN])
928932
}
929933
}

crypto/mlkem/src/mlkem_keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::mlkem::{MLKEM1024_PK_LEN, MLKEM1024_SK_LEN, MLKEM1024_k};
99
use crate::{ML_KEM_512_NAME, ML_KEM_768_NAME, ML_KEM_1024_NAME};
1010
use bouncycastle_core::errors::KEMError;
1111
use bouncycastle_core::key_material::{KeyMaterial, KeyMaterialTrait, KeyType};
12-
use bouncycastle_core::traits::{Hash, KEMPrivateKey, KEMPublicKey, Secret, SecurityStrength};
12+
use bouncycastle_core::traits::{Hash, HashFixedOutput, KEMPrivateKey, KEMPublicKey, Secret, SecurityStrength};
1313
use bouncycastle_sha3::SHA3_256;
1414

1515

crypto/mlkem_lowmemory/src/mlkem_keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::polynomial::Polynomial;
1919
use crate::{ML_KEM_512_NAME, ML_KEM_768_NAME, ML_KEM_1024_NAME};
2020
use bouncycastle_core::errors::KEMError;
2121
use bouncycastle_core::key_material::{KeyMaterial, KeyMaterialTrait, KeyType};
22-
use bouncycastle_core::traits::{Hash, KEMPrivateKey, KEMPublicKey, Secret, SecurityStrength};
22+
use bouncycastle_core::traits::{Hash, HashFixedOutput, KEMPrivateKey, KEMPublicKey, Secret, SecurityStrength};
2323
use bouncycastle_sha3::SHA3_256;
2424
use core::fmt;
2525
use core::fmt::{Debug, Display, Formatter};

0 commit comments

Comments
 (0)