Do not implement Default for HmacEngine

The `HmacEngine` should be created using a key. Currently we are
providing a `Default` impl that uses `&[]` as the key. This is, I
believe, a hangover from when we had a `Default` trait bound somewhere
else. It is incorrect and an API footgun - remove it.
This commit is contained in:
Tobin C. Harding 2025-01-30 10:47:18 +11:00
parent 98db7bca74
commit c352d376ed
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 3 additions and 26 deletions

View File

@ -31,13 +31,6 @@ pub struct HmacEngine<T: GeneralHash> {
oengine: T::Engine,
}
impl<T: GeneralHash> Default for HmacEngine<T>
where
<T as GeneralHash>::Engine: Default,
{
fn default() -> Self { HmacEngine::new(&[]) }
}
impl<T: GeneralHash> HmacEngine<T> {
/// Constructs a new keyed HMAC from `key`.
///
@ -328,7 +321,7 @@ mod benches {
#[bench]
pub fn hmac_sha256_10(bh: &mut Bencher) {
let mut engine = Hmac::<sha256::Hash>::engine();
let mut engine = HmacEngine::<sha256::Hashe>::new(&[]);
let bytes = [1u8; 10];
bh.iter(|| {
engine.input(&bytes);
@ -338,7 +331,7 @@ mod benches {
#[bench]
pub fn hmac_sha256_1k(bh: &mut Bencher) {
let mut engine = Hmac::<sha256::Hash>::engine();
let mut engine = HmacEngine::<sha256::Hashe>::new(&[]);
let bytes = [1u8; 1024];
bh.iter(|| {
engine.input(&bytes);
@ -348,7 +341,7 @@ mod benches {
#[bench]
pub fn hmac_sha256_64k(bh: &mut Bencher) {
let mut engine = Hmac::<sha256::Hash>::engine();
let mut engine = HmacEngine::<sha256::Hashe>::new(&[]);
let bytes = [1u8; 65536];
bh.iter(|| {
engine.input(&bytes);

View File

@ -56,22 +56,6 @@ fn regression_sha256t() {
assert_eq!(got, want);
}
#[test]
fn regression_hmac_sha256_with_default_key() {
let hash = Hmac::<sha256::Hash>::hash(DATA.as_bytes());
let got = format!("{}", hash);
let want = "58cc7ed8567bd86eba61f7ed2d5a4edab1774dc10488e57de2eb007a2d9ae82d";
assert_eq!(got, want);
}
#[test]
fn regression_hmac_sha512_with_default_key() {
let hash = Hmac::<sha512::Hash>::hash(DATA.as_bytes());
let got = format!("{}", hash);
let want = "5f5db2f3e1178bf19af5db38a0ed04dc5bc52d641648542886eea9b6bbec0db658ed7a5799ca18f5bc1949f39d24151a32990ee85974e40bb8a35e2288f494ce";
assert_eq!(got, want);
}
#[test]
fn regression_hmac_sha256_with_key() {
let mut engine = HmacEngine::<sha256::Hash>::new(HMAC_KEY);