pad_message wrote 0x00 into the last byte of the current block and deferred the 0x80 pad byte to the next block when mbi == 63, producing incorrect digests for every message whose length is 63 mod 64 (and correspondingly wrong HMAC tags). Always write the pad byte in the current block first, then flush an extra block only when the 8-byte length field no longer fits. Adds regression tests for SHA-256 boundary lengths 55/56/63/64/65 and HMAC with a 63-byte message. Bumps to 1.0.4 for crates.io publication. Ref: docs/LAGUNA-AUDIT.md
105 lines
5.9 KiB
Rust
105 lines
5.9 KiB
Rust
use emsha::hexstr::to_hex;
|
|
use emsha::sha256;
|
|
use emsha::{Hash, Result};
|
|
|
|
pub(crate) struct HashTest<'a> {
|
|
pub(crate) output: &'a [u8],
|
|
pub(crate) input: &'a [u8],
|
|
}
|
|
|
|
impl<'a> HashTest<'a> {
|
|
pub fn new(output: &'a [u8], input: &'a [u8]) -> Self {
|
|
Self { output, input }
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_self_test() -> Result<()> {
|
|
sha256::self_test()?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_padding_boundaries() -> Result<()> {
|
|
let tests: &[(usize, &[u8])] = &[
|
|
(55, b"9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318"),
|
|
(56, b"b35439a4ac6f0948b6d6f9e3c6af0f5f590ce20f1bde7090ef7970686ec6738a"),
|
|
(63, b"7d3e74a05d7db15bce4ad9ec0658ea98e3f06eeecf16b4c6fff2da457ddc2f34"),
|
|
(64, b"ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb"),
|
|
(65, b"635361c48bb9eab14198e76ea8ab7f1a41685d6ad62aa9146d301d4f17eb0ae0"),
|
|
];
|
|
|
|
let mut h = sha256::SHA256::default();
|
|
let mut d: [u8; 32] = [0; 32];
|
|
let mut s: [u8; 64] = [0; 64];
|
|
let input_buf = [0x61u8; 65];
|
|
|
|
let mut i: usize = 0;
|
|
while i < tests.len() {
|
|
let (len, expected) = tests[i];
|
|
h.update(&input_buf[..len])?;
|
|
h.finalize(&mut d)?;
|
|
to_hex(&d, &mut s);
|
|
assert_eq!(&s, expected, "length {}", len);
|
|
h.reset()?;
|
|
i += 1;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn test_golden_tests() -> Result<()> {
|
|
let golden_tests: &[HashTest] = &[
|
|
HashTest::new(b"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", b""),
|
|
HashTest::new(b"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", b"a"),
|
|
HashTest::new(b"fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603", b"ab"),
|
|
HashTest::new(b"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", b"abc"),
|
|
HashTest::new(b"88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", b"abcd"),
|
|
HashTest::new(b"36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c", b"abcde"),
|
|
HashTest::new(b"bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721", b"abcdef"),
|
|
HashTest::new(b"7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a", b"abcdefg"),
|
|
HashTest::new(b"9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab", b"abcdefgh"),
|
|
HashTest::new(b"19cc02f26df43cc571bc9ed7b0c4d29224a3ec229529221725ef76d021c8326f", b"abcdefghi"),
|
|
HashTest::new(b"72399361da6a7754fec986dca5b7cbaf1c810a28ded4abaf56b2106d06cb78b0", b"abcdefghij"),
|
|
HashTest::new(b"a144061c271f152da4d151034508fed1c138b8c976339de229c3bb6d4bbb4fce", b"Discard medicine more than two years old."),
|
|
HashTest::new(b"6dae5caa713a10ad04b46028bf6dad68837c581616a1589a265a11288d4bb5c4", b"He who has a shady past knows that nice guys finish last."),
|
|
HashTest::new(b"ae7a702a9509039ddbf29f0765e70d0001177914b86459284dab8b348c2dce3f", b"I wouldn't marry him with a ten foot pole."),
|
|
HashTest::new(b"6748450b01c568586715291dfa3ee018da07d36bb7ea6f180c1af6270215c64f", b"Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"),
|
|
HashTest::new(b"14b82014ad2b11f661b5ae6a99b75105c2ffac278cd071cd6c05832793635774", b"The days of the digital watch are numbered. -Tom Stoppard"),
|
|
HashTest::new(b"7102cfd76e2e324889eece5d6c41921b1e142a4ac5a2692be78803097f6a48d8", b"Nepal premier won't resign."),
|
|
HashTest::new(b"23b1018cd81db1d67983c5f7417c44da9deb582459e378d7a068552ea649dc9f", b"For every action there is an equal and opposite government program."),
|
|
HashTest::new(b"8001f190dfb527261c4cfcab70c98e8097a7a1922129bc4096950e57c7999a5a", b"His money is twice tainted: 'taint yours and 'taint mine."),
|
|
HashTest::new(b"8c87deb65505c3993eb24b7a150c4155e82eee6960cf0c3a8114ff736d69cad5", b"There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"),
|
|
HashTest::new(b"bfb0a67a19cdec3646498b2e0f751bddc41bba4b7f30081b0b932aad214d16d7", b"It's a tiny change to the code and not completely disgusting. - Bob Manchek"),
|
|
HashTest::new(b"7f9a0b9bf56332e19f5a0ec1ad9c1425a153da1c624868fda44561d6b74daf36", b"size: a.out: bad magic"),
|
|
HashTest::new(b"b13f81b8aad9e3666879af19886140904f7f429ef083286195982a7588858cfc", b"The major problem is with sendmail. -Mark Horton"),
|
|
HashTest::new(b"b26c38d61519e894480c70c8374ea35aa0ad05b2ae3d6674eec5f52a69305ed4", b"Give me a rock, paper and scissors and I will move the world. CCFestoon"),
|
|
HashTest::new(b"049d5e26d4f10222cd841a119e38bd8d2e0d1129728688449575d4ff42b842c1", b"If the enemy is within range, then so are you."),
|
|
HashTest::new(b"0e116838e3cc1c1a14cd045397e29b4d087aa11b0853fc69ec82e90330d60949", b"It's well we cannot hear the screams/That we create in others' dreams."),
|
|
HashTest::new(b"4f7d8eb5bcf11de2a56b971021a444aa4eafd6ecd0f307b5109e4e776cd0fe46", b"You remind me of a TV show, but that's all right: I watch it anyway."),
|
|
HashTest::new(b"61c0cc4c4bd8406d5120b3fb4ebc31ce87667c162f29468b3c779675a85aebce", b"C is as portable as Stonehedge!!"),
|
|
HashTest::new(b"1fb2eb3688093c4a3f80cd87a5547e2ce940a4f923243a79a2a1e242220693ac", b"Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"),
|
|
HashTest::new(b"395585ce30617b62c80b93e8208ce866d4edc811a177fdb4b82d3911d8696423", b"The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"),
|
|
HashTest::new(b"4f9b189a13d030838269dce846b16a1ce9ce81fe63e65de2f636863336a98fe6", b"How can you write a big system without C++? -Paul Glick"),
|
|
];
|
|
|
|
let mut i: usize = 0;
|
|
let mut h = sha256::SHA256::default();
|
|
let mut d: [u8; 32] = [0; 32];
|
|
let mut s: [u8; 64] = [0; 64];
|
|
|
|
while i < golden_tests.len() {
|
|
h.update(golden_tests[i].input)?;
|
|
h.finalize(&mut d)?;
|
|
to_hex(&d, &mut s);
|
|
assert_eq!(s, golden_tests[i].output);
|
|
h.reset()?;
|
|
|
|
i += 1;
|
|
}
|
|
|
|
Ok(())
|
|
}
|