Skip to content

Commit

Permalink
update to new belthash + move reverse into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
makavity committed Jul 25, 2024
1 parent f71606f commit 422db56
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 84 deletions.
111 changes: 33 additions & 78 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bake-kdf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["crypto", "bake", "stb", "kdf"]
categories = ["cryptography", "no-std"]

[dependencies]
belt-hash = { version = "0.1.1", default-features = false }
belt-hash = { version = "0.2.0-pre.3", default-features = false }

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down
13 changes: 12 additions & 1 deletion bake-kdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ pub fn belt_keyrep<const M: usize>(
_ => unreachable!(),
};

let mut x_swapped = [0u32; 8];
for (i, &val) in x.iter().enumerate() {
x_swapped[i] = u32::swap_bytes(val);
}
let x = &x_swapped[..(n / 32)];

let s = belt_keyexpand(x).map_err(|_| InvalidKeyLength)?;
let mut d = [d[0], d[1], d[2]];
let mut i = [i[0], i[1], i[2], i[3]];
Expand Down Expand Up @@ -154,13 +160,18 @@ pub fn bake_kdf(x: &[u8], s: &[u8], c: u128) -> Result<[u32; 8], Error> {
hasher.update(x);
hasher.update(s);
let y = hasher.finalize_fixed();

let mut y = to_u32::<8>(&y);
for y in y.iter_mut() {
*y = u32::swap_bytes(*y);
}

let d: [u32; 3] = [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF];

let mut c = to_u32::<4>(&c.to_be_bytes());
c.reverse();

let mut out = [0u32; 8];
belt_keyrep::<256>(&to_u32::<8>(&y), &d, &c, &mut out)?;
belt_keyrep::<256>(&y, &d, &c, &mut out)?;
Ok(out)
}
6 changes: 2 additions & 4 deletions bake-kdf/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ fn test_keyexpand() {

#[test]
fn test_keyrep() {
let mut x: [u32; 8] = [
let x: [u32; 8] = [
0xE9DEE72C, 0x8F0C0FA6, 0x2DDB49F4, 0x6F739647, 0x06075316, 0xED247A37, 0x39CBA383,
0x03A98BF6,
];
let d: [u32; 3] = [0x01000000, 0x00000000, 0x00000000];
let i: [u32; 4] = [0x5BE3D612, 0x17B96181, 0xFE6786AD, 0x716B890B];

x.iter_mut().for_each(|x| *x = u32::swap_bytes(*x));


let out: &mut [u32] = &mut [0; 4];
belt_keyrep::<128>(&x, &d, &i, out).unwrap();
assert_eq!(out, [0x6BBBC233, 0x6670D31A, 0xB83DAA90, 0xD52C0541]);
Expand Down

0 comments on commit 422db56

Please sign in to comment.