Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ci/cpu_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ def runs_in(runner, test):
},
"sha512": {
"lib": [
# Every hash's tables of functions: their dispatchers' choices
# of the SHA-512 family's tables, which only these lines make.
"hashes::tables",
"hashes::sha512",
"hashes::sha512_224",
"hashes::sha512_256",
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/brainpoolp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<H: HmacHash> sealed::Functions<BrainpoolP256r1> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 32, 64>(sign, f, d, digest) }
unsafe { sign_with::<H, 32, 64>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/brainpoolp384r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<H: HmacHash> sealed::Functions<BrainpoolP384r1> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 48, 96>(sign, f, d, digest) }
unsafe { sign_with::<H, 48, 96>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/brainpoolp512r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<H: HmacHash> sealed::Functions<BrainpoolP512r1> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 64, 128>(sign, f, d, digest) }
unsafe { sign_with::<H, 64, 128>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/p192.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn verify_with(q: &[u8; 49], digest: &[u8], signature: &[u8; 48]) -> Result<(),
impl<H: HmacHash> sealed::Functions<P192> for H {
fn sign(d: &[u8; 24], digest: &H::Output) -> Result<[u8; 48], InvalidKey> {
// SAFETY: `vg_ecdsa_p192_sign` needs no CPU feature.
unsafe { sign_with::<H, 24, 48>(vg_ecdsa_p192_sign, crate::cpu::detected(), d, digest) }
unsafe { sign_with::<H, 24, 48>(vg_ecdsa_p192_sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/p224.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<H: HmacHash> sealed::Functions<P224> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 28, 56>(sign, f, d, digest) }
unsafe { sign_with::<H, 28, 56>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<H: HmacHash> sealed::Functions<P256> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 32, 64>(sign, f, d, digest) }
unsafe { sign_with::<H, 32, 64>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<H: HmacHash> sealed::Functions<P384> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 48, 96>(sign, f, d, digest) }
unsafe { sign_with::<H, 48, 96>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/p521.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<H: HmacHash> sealed::Functions<P521> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 66, 132>(sign, f, d, digest) }
unsafe { sign_with::<H, 66, 132>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<H: HmacHash> sealed::Functions<Secp256k1> for H {
};
// SAFETY: `sign` needs no CPU feature that the multiplications
// selected for this CPU do not.
unsafe { sign_with::<H, 32, 64>(sign, f, d, digest) }
unsafe { sign_with::<H, 32, 64>(sign, d, digest) }
}

/// The verified function takes the hash itself, of any length, and
Expand Down
55 changes: 26 additions & 29 deletions src/ec/ecdsa/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,60 @@ use crate::hashes::sha512_224::Sha512_224;
use crate::hashes::sha512_256::Sha512_256;
use crate::hashes::tables::TableHash;
use crate::zeroize::zeroize;
use verified_garbage_cpu::Features;

/// A hash function that the verified signatures take HMAC over as a table:
/// one with HMAC (`VG.Spec.HmacTable.hmacOf`).
pub trait HmacHash: HashFunction {
/// Its table of `Hmac`, as the verified functions take it, for the
/// implementation that a CPU with the features `f` runs best
/// (`TableHash::hmac_table`).
fn hmac_table(f: Features) -> *const [usize; 6];
/// The best of its tables of `Hmac` that this CPU runs, as the verified
/// functions take it (`TableHash::hmac_table`).
fn hmac_table() -> *const [usize; 6];
}

impl HmacHash for Md5 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Md5.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Md5.hmac_table()
}
}

impl HmacHash for Sha1 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha1.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha1.hmac_table()
}
}

impl HmacHash for Sha224 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha224.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha224.hmac_table()
}
}

impl HmacHash for Sha256 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha256.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha256.hmac_table()
}
}

impl HmacHash for Sha384 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha384.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha384.hmac_table()
}
}

impl HmacHash for Sha512 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha512.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha512.hmac_table()
}
}

impl HmacHash for Sha512_224 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha512_224.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha512_224.hmac_table()
}
}

impl HmacHash for Sha512_256 {
fn hmac_table(f: Features) -> *const [usize; 6] {
TableHash::Sha512_256.hmac_table(f)
fn hmac_table() -> *const [usize; 6] {
TableHash::Sha512_256.hmac_table()
}
}

Expand Down Expand Up @@ -97,19 +95,18 @@ pub(super) type SignFn<const Q: usize, const S: usize> = unsafe extern "C" fn(
) -> u32;

/// The signature `r ‖ s` of `digest`, the hash of a message with `H`, with
/// the key `d`, by `sign` with `H`'s table of `Hmac` for this CPU (`f`, its
/// features).
/// the key `d`, by `sign` with the best of `H`'s tables of `Hmac` that this
/// CPU runs.
///
/// # Safety
///
/// `sign` needs no CPU feature that this CPU lacks.
pub(super) unsafe fn sign_with<H: HmacHash, const Q: usize, const S: usize>(
sign: SignFn<Q, S>,
f: Features,
d: &[u8; Q],
digest: &H::Output,
) -> Result<[u8; S], InvalidKey> {
let table = H::hmac_table(f);
let table = H::hmac_table();
let digest = digest.as_ref();
let mut out = [0; S];
let mut scratch = [0u64; 1024];
Expand All @@ -120,8 +117,9 @@ pub(super) unsafe fn sign_with<H: HmacHash, const Q: usize, const S: usize>(
// others, so none overlaps another or the call's stack frame, and, as
// Rust objects, none wraps around the address space. `table` is one of
// the tables of the interface `Hmac`, for `H`, whose functions need no
// CPU feature that `f`, this CPU's, lacks (`TableHash::hmac_table`); the
// caller guarantees the features `sign` needs.
// CPU feature that this CPU lacks (its dispatcher chose it,
// `TableHash::hmac_table`); the caller guarantees the features `sign`
// needs.
let ok = unsafe {
sign(
&mut out,
Expand All @@ -139,14 +137,13 @@ pub(super) unsafe fn sign_with<H: HmacHash, const Q: usize, const S: usize>(
#[cfg(test)]
mod tests {
use super::{HmacHash, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sha512_224, Sha512_256};
use verified_garbage_cpu::Features;

/// `H`'s table of `Hmac` names `H`'s digest length and HMAC's block
/// size (its words 4 and 5).
fn check<H: HmacHash>() {
// SAFETY: `hmac_table` points to the table's six words, of an
// immutable `'static` `repr(C)` value of six pointer-sized words.
let t = unsafe { *H::hmac_table(Features(0)) };
let t = unsafe { *H::hmac_table() };
assert_eq!((t[4], t[5]), (H::OUTPUT_SIZE, H::BLOCK_SIZE));
}

Expand Down
7 changes: 0 additions & 7 deletions src/hashes/md5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ super::streaming_hash!(
backends: Md5Backend {
Scalar => (vg_md5_update, vg_md5_finalize),
},
tables: {
Scalar => {
hash: crate::arch::fn_tables::VG_HASH_MD5 [],
md_hash: crate::arch::fn_tables::VG_MD_HASH_MD5 [],
hmac: crate::arch::fn_tables::VG_HMAC_MD5 [],
},
},
}
);

Expand Down
52 changes: 2 additions & 50 deletions src/hashes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ pub trait HashFunction: Clone {
/// `digest` calls: the match is exhaustive, so an implementation without one
/// does not compile. Without it, `digest` uses the streaming functions.
///
/// `tables`, if given, is every implementation's tables of functions
/// (`crate::arch::fn_tables`), each with the generated constants of the CPU
/// features it needs (none for the baseline ISA's: it fails to compile if
/// they are not all among those `select` requires to choose that
/// implementation), which `tables` returns for the verified functions that take a hash function as a table
/// (`tables::TableHash`): its table of `Hash`, its table of `MdHash` and,
/// on the architectures with tables of `Hmac` (`tables::Tables`), its table
/// of `Hmac`. The match is exhaustive, so an implementation without its
/// tables does not compile.
///
/// Every region is a distinct Rust object, so none overlaps another or the
/// return address, and none wraps around the end of the address space; the
/// length is kept exactly (`update` panics rather than let it reach 2⁶⁴
Expand Down Expand Up @@ -127,13 +117,7 @@ macro_rules! streaming_hash {
$(, digests: {
$($(#[$dattr:meta])* $dvariant:ident => $digest:path),* $(,)?
})?
$(, tables: {
$($(#[$tattr:meta])* $tvariant:ident => {
hash: $thash:path [$($threq:path),*],
md_hash: $tmd:path [$($tmreq:path),*]
$(, hmac: $thmac:path [$($thmreq:path),*])? $(,)?
}),* $(,)?
})? $(,)?
$(,)?
}
) => {
/// The implementations of the streaming primitives.
Expand All @@ -154,8 +138,7 @@ macro_rules! streaming_hash {
/// every CPU it is chosen for has them, so a function chosen
/// with it may need no other, which its caller checks when it
/// compiles (`assert!` in a `const`). (A hash function with no
/// implementation but the baseline, and no tables, does not use
/// it.)
/// implementation but the baseline does not use it.)
#[allow(dead_code)]
pub(crate) const REQUIRES: &[verified_garbage_cpu::Features] = &[
verified_garbage_cpu::Features(0),
Expand Down Expand Up @@ -240,37 +223,6 @@ macro_rules! streaming_hash {
)?
}

$(
// Each implementation's tables need no CPU feature that `select`
// does not require to choose it.
$(
$(#[$tattr])*
const _: () = assert!(
$backend::REQUIRES[$backend::$tvariant as usize].contains(
verified_garbage_cpu::Features::all(&[$($threq,)* $($tmreq,)* $($($thmreq),*)?])
),
"a table needs a CPU feature its implementation is not chosen for"
);
)*

impl $backend {
/// The tables of functions of this implementation.
pub(crate) fn tables(self) -> $crate::hashes::tables::Tables {
match self {
$($(#[$tattr])* Self::$tvariant => $crate::hashes::tables::Tables {
#[cfg(feature = "alloc")]
hash: &$thash,
#[cfg(feature = "alloc")]
md_hash: &$tmd,
$(
hmac: &$thmac,
)?
},)*
}
}
}
)?

$(#[$doc])*
///
/// Messages are limited to 2⁶⁴ − 1 bytes.
Expand Down
25 changes: 0 additions & 25 deletions src/hashes/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,6 @@ super::streaming_hash!(
#[cfg(target_arch = "x86_64")]
AvxBmi => vg_sha1_digest_avx_bmi,
},
tables: {
Scalar => {
hash: crate::arch::fn_tables::VG_HASH_SHA1 [],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1 [],
hmac: crate::arch::fn_tables::VG_HMAC_SHA1 [],
},
#[cfg(target_arch = "aarch64")]
Sha2 => {
hash: crate::arch::fn_tables::VG_HASH_SHA1_SHA2 [crate::arch::fn_tables::VG_HASH_SHA1_SHA2_FEATURES],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1_SHA2 [crate::arch::fn_tables::VG_MD_HASH_SHA1_SHA2_FEATURES],
hmac: crate::arch::fn_tables::VG_HMAC_SHA1_SHA2 [crate::arch::fn_tables::VG_HMAC_SHA1_SHA2_FEATURES],
},
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
ShaNi => {
hash: crate::arch::fn_tables::VG_HASH_SHA1_SHANI [crate::arch::fn_tables::VG_HASH_SHA1_SHANI_FEATURES],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA1_SHANI_FEATURES],
hmac: crate::arch::fn_tables::VG_HMAC_SHA1_SHANI [crate::arch::fn_tables::VG_HMAC_SHA1_SHANI_FEATURES],
},
#[cfg(target_arch = "x86_64")]
AvxBmi => {
hash: crate::arch::fn_tables::VG_HASH_SHA1_AVX_BMI [crate::arch::fn_tables::VG_HASH_SHA1_AVX_BMI_FEATURES],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1_AVX_BMI [crate::arch::fn_tables::VG_MD_HASH_SHA1_AVX_BMI_FEATURES],
hmac: crate::arch::fn_tables::VG_HMAC_SHA1_AVX_BMI [crate::arch::fn_tables::VG_HMAC_SHA1_AVX_BMI_FEATURES],
},
},
}
);

Expand Down
25 changes: 0 additions & 25 deletions src/hashes/sha224.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,6 @@ super::streaming_hash!(
#[cfg(target_arch = "x86_64")]
Avx2 => vg_sha224_digest_avx2,
},
tables: {
Scalar => {
hash: crate::arch::fn_tables::VG_HASH_SHA224 [],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224 [],
hmac: crate::arch::fn_tables::VG_HMAC_SHA224 [],
},
#[cfg(target_arch = "aarch64")]
Sha2 => {
hash: crate::arch::fn_tables::VG_HASH_SHA224_SHA2 [crate::arch::fn_tables::VG_HASH_SHA224_SHA2_FEATURES],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224_SHA2 [crate::arch::fn_tables::VG_MD_HASH_SHA224_SHA2_FEATURES],
hmac: crate::arch::fn_tables::VG_HMAC_SHA224_SHA2 [crate::arch::fn_tables::VG_HMAC_SHA224_SHA2_FEATURES],
},
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
ShaNi => {
hash: crate::arch::fn_tables::VG_HASH_SHA224_SHANI [crate::arch::fn_tables::VG_HASH_SHA224_SHANI_FEATURES],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA224_SHANI_FEATURES],
hmac: crate::arch::fn_tables::VG_HMAC_SHA224_SHANI [crate::arch::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES],
},
#[cfg(target_arch = "x86_64")]
Avx2 => {
hash: crate::arch::fn_tables::VG_HASH_SHA224_AVX2 [crate::arch::fn_tables::VG_HASH_SHA224_AVX2_FEATURES],
md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA224_AVX2_FEATURES],
hmac: crate::arch::fn_tables::VG_HMAC_SHA224_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA224_AVX2_FEATURES],
},
},
}
);

Expand Down
Loading
Loading