Skip to content

Commit

Permalink
fixed consts docs and moved all consts defintion into consts.rs
Browse files Browse the repository at this point in the history
added source for test vectors
  • Loading branch information
truthixify committed Jan 3, 2025
1 parent 332fb95 commit d7939a7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 59 deletions.
7 changes: 0 additions & 7 deletions md6/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ keywords = ["crypto", "md6", "hash", "digest"]
categories = ["cryptography", "no-std"]
rust-version = "1.81"

[lib]
name = "md6"

[dependencies]
digest = "=0.11.0-pre.9"

Expand All @@ -27,7 +24,3 @@ default = ["oid", "std"]
std = ["digest/std"]
oid = ["digest/oid"]
zeroize = ["digest/zeroize"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
16 changes: 0 additions & 16 deletions md6/src/compress.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
use crate::consts::*;

const W: usize = MD6_W; // number of bits in a word (64)
const C: usize = MD6_C; // size of compression output in words (16)
const N: usize = MD6_N; // size of compression input block in words (89)
const Q: usize = MD6_Q; // Q words in a compression block (>= 0) (15)
const K: usize = MD6_K; // key words per compression block (>= 0) (8)
const U: usize = MD6_U; // words for unique node ID (0 or 64/w)
const V: usize = MD6_V; // words for control word (0 or 64/w)
const B: usize = MD6_B; // data words per compression block (> 0) (64)

const T0: usize = 17; // index for linear feedback
const T1: usize = 18; // index for first input to first and
const T2: usize = 21; // index for second input to first and
const T3: usize = 31; // index for first input to second and
const T4: usize = 67; // index for second input to second and
const T5: usize = 89; // last tap

macro_rules! call_loop_bodies {
($w: ident, $s: expr, $i: expr) => {
if $w == 64 {
Expand Down
74 changes: 58 additions & 16 deletions md6/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
/// MD6 constants related to standard mode of operation
pub(crate) type Md6Word = u64;
pub(crate) type Md6ControlWord = u64;
pub(crate) type Md6NodeID = u64;
pub type Md6Word = u64;
pub type Md6ControlWord = u64;
pub type Md6NodeID = u64;

pub(crate) const MD6_MAX_STACK_HEIGHT: usize = 29; // maximum stack height
pub(crate) const MD6_MAX_R: usize = 255; // maximum number of rounds
pub(crate) const MD6_DEFAULT_L: usize = 64; // large so that MD6 is fully hierarchical
/// Maximum stack height
pub const MD6_MAX_STACK_HEIGHT: usize = 29;
/// Maximum number of rounds
pub const MD6_MAX_R: usize = 255;
/// Large so that MD6 is fully hierarchical
pub const MD6_DEFAULT_L: usize = 64;

pub(crate) const MD6_W: usize = 64; // number of bits in a word
pub(crate) const MD6_C: usize = 16; // size of compression output in words
pub(crate) const MD6_N: usize = 89; // size of compression input block in words
/// Number of bits in a word
pub const MD6_W: usize = 64;
/// Size of compression output in words
pub const MD6_C: usize = 16;
/// Size of compression input block in words
pub const MD6_N: usize = 89;

/// These five values give lengths of the components of compression
/// input block; they should sum to MD6_N.
pub(crate) const MD6_Q: usize = 15; // Q words in a compression block (>= 0)
pub(crate) const MD6_K: usize = 8; // key words per compression block (>= 0)
pub(crate) const MD6_U: usize = 64 / MD6_W; // words for unique node ID (0 or 64/w)
pub(crate) const MD6_V: usize = 64 / MD6_W; // words for control word (0 or 64/w)
pub(crate) const MD6_B: usize = 64; // data words per compression block (> 0)
// These five values give lengths of the components of compression
// input block; they should sum to MD6_N.

// Q words in a compression block (>= 0)
pub const MD6_Q: usize = 15;
/// Key words per compression block (>= 0)
pub const MD6_K: usize = 8;
/// Words for unique node ID (0 or 64/w)
pub const MD6_U: usize = 64 / MD6_W;
/// Words for control word (0 or 64/w)
pub const MD6_V: usize = 64 / MD6_W;
/// Data words per compression block (> 0)
pub const MD6_B: usize = 64;

/// Number of bits in a word (64)
pub const W: usize = MD6_W;
/// Size of compression output in words (16)
pub const C: usize = MD6_C;
/// Size of compression input block in words (89)
pub const N: usize = MD6_N;
/// Q words in a compression block (>= 0) (15)
pub const Q: usize = MD6_Q;
/// Key words per compression block (>= 0) (8)
pub const K: usize = MD6_K;
/// Words for unique node ID (0 or 64/w)
pub const U: usize = MD6_U;
/// Words for control word (0 or 64/w)
pub const V: usize = MD6_V;
/// Data words per compression block (> 0) (64)
pub const B: usize = MD6_B;

/// Index for linear feedback
pub const T0: usize = 17;
/// Index for first input to first and
pub const T1: usize = 18;
/// Index for second input to first and
pub const T2: usize = 21;
/// Index for first input to second and
pub const T3: usize = 31;
/// Index for second input to second and
pub const T4: usize = 67;
/// Last tap
pub const T5: usize = 89;
6 changes: 0 additions & 6 deletions md6/src/md6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ use digest::{
HashMarker, Output,
};

const W: usize = MD6_W; // number of bits in a word (64)
const C: usize = MD6_C; // size of compression output in words (16)
const K: usize = MD6_K; // key words per compression block (8)
const B: usize = MD6_B; // data words per compression block (64)

pub struct Md6VarCore {
d: usize,
hashbitlen: usize,
Expand Down Expand Up @@ -289,7 +284,6 @@ impl SerializableState for Md6VarCore {
impl Md6VarCore {
#[inline]
fn init(d: usize) -> Self {
//
Self::full_init(d, None, 0, MD6_DEFAULT_L, default_r(d, 0))
}

Expand Down
28 changes: 14 additions & 14 deletions md6/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![no_std]

use digest::Digest;
use hex_literal::hex;

// Test vectors from https://github.com/Snack-X/md6/blob/master/test/result.csv

#[test]
fn test_md6_64() {
const INPUT: &[(&[u8], &[u8; 8])] = &[
const TEST_VECTOR: &[(&[u8], &[u8; 8])] = &[
(b"a", &hex!("32d13030a6815e95")),
(b"aa", &hex!("af7966908a5d9c13")),
(b"aaa", &hex!("3d8a4ff7a21eb0c6")),
Expand All @@ -30,7 +30,7 @@ fn test_md6_64() {
(b"md6 FTW", &hex!("47cda109418592ca")),
];

for (msg, &expected_hash) in INPUT.iter() {
for (msg, &expected_hash) in TEST_VECTOR.iter() {
let mut hasher = md6::Md6_64::new();
hasher.update(msg);
let output = hasher.finalize();
Expand All @@ -41,7 +41,7 @@ fn test_md6_64() {

#[test]
fn test_md6_128() {
const INPUT: &[(&[u8], &[u8; 16])] = &[
const TEST_VECTOR: &[(&[u8], &[u8; 16])] = &[
(b"a", &hex!("bb691c1bfa4b4345292eb35f364919ea")),
(b"aa", &hex!("19487e566f9ae2584d62628af2795f8c")),
(b"aaa", &hex!("319f1b026f76f9caf62320b4e2e79e29")),
Expand All @@ -66,7 +66,7 @@ fn test_md6_128() {
(b"md6 FTW", &hex!("e866b430fa07b5bea28981db1f9b24a6")),
];

for (msg, &expected_hash) in INPUT.iter() {
for (msg, &expected_hash) in TEST_VECTOR.iter() {
let mut hasher = md6::Md6_128::new();
hasher.update(msg);
let output = hasher.finalize();
Expand All @@ -77,7 +77,7 @@ fn test_md6_128() {

#[test]
fn test_md6_224() {
const INPUT: &[(&[u8], &[u8; 28])] = &[
const TEST_VECTOR: &[(&[u8], &[u8; 28])] = &[
(
b"a",
&hex!("05de8792a96e024c806eb815f9f30053cf9f1b50661047a4934121b7"),
Expand Down Expand Up @@ -168,7 +168,7 @@ fn test_md6_224() {
),
];

for (msg, &expected_hash) in INPUT.iter() {
for (msg, &expected_hash) in TEST_VECTOR.iter() {
let mut hasher = md6::Md6_224::new();
hasher.update(msg);
let output = hasher.finalize();
Expand All @@ -179,7 +179,7 @@ fn test_md6_224() {

#[test]
fn test_md6_256() {
const INPUT: &[(&[u8], &[u8; 32])] = &[
const TEST_VECTOR: &[(&[u8], &[u8; 32])] = &[
(
b"a",
&hex!("2b0a697a081c21269514640aab4d74ffafeb3c0212df68ce92922087c69b0a77"),
Expand Down Expand Up @@ -270,7 +270,7 @@ fn test_md6_256() {
),
];

for (msg, &expected_hash) in INPUT.iter() {
for (msg, &expected_hash) in TEST_VECTOR.iter() {
let mut hasher = md6::Md6_256::new();
hasher.update(msg);
let output = hasher.finalize();
Expand All @@ -281,7 +281,7 @@ fn test_md6_256() {

#[test]
fn test_md6_384() {
const INPUT: &[(&[u8], &[u8; 48])] = &[
const TEST_VECTOR: &[(&[u8], &[u8; 48])] = &[
(b"a", &hex!("a40c8d059495a278fadd30b96e3b2227758090c759b934197265bf632cabf8547a7429e5316d496c2a1ddae8d27e87ee")),
(b"aa", &hex!("330547441b6518e7693ea01bfc55158bcfc084853fa1960a9e8999f98b57cea7d8b0564bf192b6ab1eb7638939dc9bbf")),
(b"aaa", &hex!("f43bb4e108ec31e0cf8ded506f79373e69cddcd8c7c46298f1bd475401132e4c255c08e378c9db988f0de97131cbe36c")),
Expand All @@ -306,7 +306,7 @@ fn test_md6_384() {
(b"md6 FTW", &hex!("7a4e8ecd1035ccdf00567595c15aa5a382fef2b6a4ec4bc609e0c655887b1c05e10eee223dd6c0ba5fa4a46159c70757")),
];

for (msg, &expected_hash) in INPUT.iter() {
for (msg, &expected_hash) in TEST_VECTOR.iter() {
let mut hasher = md6::Md6_384::new();
hasher.update(msg);
let output = hasher.finalize();
Expand All @@ -317,7 +317,7 @@ fn test_md6_384() {

#[test]
fn test_md6_512() {
const INPUT: &[(&[u8], &[u8; 64])] = &[
const TEST_VECTOR: &[(&[u8], &[u8; 64])] = &[
(b"a", &hex!("c0e4e18acb69cd1a7e5a20981fe6cc6f7b5b70e814d3a13b05ac292aba74c0d8c9d34c211414e7ab755a9559c27211cd749fc3eb09ae670e138881743b8d5051")),
(b"aa", &hex!("2afa253b05702770343e5c46e9d47231812a741d7bba479539a3c5484a412ea419f0d0ca96e124ba92e4ca506ca12684579323051d9d52fe5a669d079a226683")),
(b"aaa", &hex!("56b0131875d458f6d30ed1c594991df1efa8d6cae0c8abb36a9b811df23ac476c58e36d9adbe845e840d3de9175a8ceda11235144c3222587af108b902ce0fc5")),
Expand All @@ -342,7 +342,7 @@ fn test_md6_512() {
(b"md6 FTW", &hex!("75df3b6031e8241ef59d01628b093b05906f1a2d80c43908cb2883f7db6fbdd1cadffd7d643505c20b9529b6a5d19f8b6ff1623cabbc14a606caa7bcb239611a")),
];

for (msg, &expected_hash) in INPUT.iter() {
for (msg, &expected_hash) in TEST_VECTOR.iter() {
let mut hasher = md6::Md6_512::new();
hasher.update(msg);
let output = hasher.finalize();
Expand Down

0 comments on commit d7939a7

Please sign in to comment.