From c0dfa7f1d6dbd92190c334181c1ed4a96bb3238d Mon Sep 17 00:00:00 2001 From: _N0x Date: Mon, 11 Nov 2024 22:58:21 +0100 Subject: [PATCH] Restuctured sha module --- src/main.rs | 46 ++--------------------- src/sha/mod.rs | 98 +++++++++++++++++++++--------------------------- src/sha/utils.rs | 53 ++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 98 deletions(-) create mode 100644 src/sha/utils.rs diff --git a/src/main.rs b/src/main.rs index 720df6d..f697591 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,50 +1,10 @@ -mod sha; mod aes; +mod sha; fn main() { println!("Running SHA tests..."); - test_sha(); + sha::test(); + println!("Running AES tests..."); aes::test(); } - -fn test_sha() { - assert_eq!( - "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - sha::sha256("".to_string().as_bytes()), - "Testing hash for \"\"" - ); - assert_eq!( - "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", - sha::sha256("abc".to_string().as_bytes()), - "Testing hash for \"abc\"" - ); - assert_eq!( - "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", - sha::sha256( - "The quick brown fox jumps over the lazy dog" - .to_string() - .as_bytes() - ), - "Testing hash for \"The quick brown fox jumps over the lazy dog\"" - ); - assert_eq!( - "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", - sha::sha224("".to_string().as_bytes()), - "Testing hash for \"\"" - ); - assert_eq!( - "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", - sha::sha224("abc".to_string().as_bytes()), - "Testing hash for \"abc\"" - ); - assert_eq!( - "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525", - sha::sha224( - "The quick brown fox jumps over the lazy dog" - .to_string() - .as_bytes() - ), - "Testing hash for \"The quick brown fox jumps over the lazy dog\"" - ); -} diff --git a/src/sha/mod.rs b/src/sha/mod.rs index e719538..a11522e 100644 --- a/src/sha/mod.rs +++ b/src/sha/mod.rs @@ -1,58 +1,5 @@ -//////////// HELPER FUNCTIONS ///////////// -fn ch(x: u32, y: u32, z: u32) -> u32 { - let ret: u32 = (x & y) ^ (!x & z); - return ret; -} - -fn maj(x: u32, y: u32, z: u32) -> u32 { - let ret: u32 = (x & y) ^ (x & z) ^ (y & z); - return ret; -} - -fn bsig0(x: u32) -> u32 { - let ret: u32 = x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22); - return ret; -} - -fn bsig1(x: u32) -> u32 { - let ret: u32 = x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25); - return ret; -} - -fn ssig0(x: u32) -> u32 { - let ret: u32 = x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3); - return ret; -} - -fn ssig1(x: u32) -> u32 { - let ret: u32 = x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10); - return ret; -} - -fn pad_message(msg: &[u8]) -> Vec { - let l = (msg.len() as u64) << 3; - let k = ((448u64.wrapping_sub((l + 8) % 512) % 512) >> 3) as usize; - - let mut result_u8 = Vec::with_capacity(msg.len() + 1 + k + std::mem::size_of::()); - - result_u8.extend(msg); - result_u8.push(0x80); - result_u8.resize(result_u8.len() + k, 0u8); - result_u8.extend(&l.to_be_bytes()); - - let mut result_u32 = Vec::with_capacity(result_u8.len() / 4); // Allocate the right size for u32 vector - - for chunk in result_u8.chunks(4) { - let value = (chunk[0] as u32) << 24 - | (chunk[1] as u32) << 16 - | (chunk[2] as u32) << 8 - | (chunk[3] as u32); - result_u32.push(value); - } - - return result_u32; -} -//////////// END HELPER FUNCTIONS ///////////// +mod utils; +use utils::*; pub fn sha224(message: &[u8]) -> String { // Set initial hash values @@ -177,3 +124,44 @@ fn sha_u32_calculate(init_hash_value: [u32; 8], message: &[u8]) -> [u32; 8] { // After processing the entire message concatenate the result into the final variable return hash; } + +pub fn test() { + assert_eq!( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + sha256("".to_string().as_bytes()), + "Testing hash for \"\"" + ); + assert_eq!( + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + sha256("abc".to_string().as_bytes()), + "Testing hash for \"abc\"" + ); + assert_eq!( + "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", + sha256( + "The quick brown fox jumps over the lazy dog" + .to_string() + .as_bytes() + ), + "Testing hash for \"The quick brown fox jumps over the lazy dog\"" + ); + assert_eq!( + "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", + sha224("".to_string().as_bytes()), + "Testing hash for \"\"" + ); + assert_eq!( + "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", + sha224("abc".to_string().as_bytes()), + "Testing hash for \"abc\"" + ); + assert_eq!( + "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525", + sha224( + "The quick brown fox jumps over the lazy dog" + .to_string() + .as_bytes() + ), + "Testing hash for \"The quick brown fox jumps over the lazy dog\"" + ); +} diff --git a/src/sha/utils.rs b/src/sha/utils.rs new file mode 100644 index 0000000..99087fd --- /dev/null +++ b/src/sha/utils.rs @@ -0,0 +1,53 @@ +pub fn ch(x: u32, y: u32, z: u32) -> u32 { + let ret: u32 = (x & y) ^ (!x & z); + return ret; +} + +pub fn maj(x: u32, y: u32, z: u32) -> u32 { + let ret: u32 = (x & y) ^ (x & z) ^ (y & z); + return ret; +} + +pub fn bsig0(x: u32) -> u32 { + let ret: u32 = x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22); + return ret; +} + +pub fn bsig1(x: u32) -> u32 { + let ret: u32 = x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25); + return ret; +} + +pub fn ssig0(x: u32) -> u32 { + let ret: u32 = x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3); + return ret; +} + +pub fn ssig1(x: u32) -> u32 { + let ret: u32 = x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10); + return ret; +} + +pub fn pad_message(msg: &[u8]) -> Vec { + let l = (msg.len() as u64) << 3; + let k = ((448u64.wrapping_sub((l + 8) % 512) % 512) >> 3) as usize; + + let mut result_u8 = Vec::with_capacity(msg.len() + 1 + k + std::mem::size_of::()); + + result_u8.extend(msg); + result_u8.push(0x80); + result_u8.resize(result_u8.len() + k, 0u8); + result_u8.extend(&l.to_be_bytes()); + + let mut result_u32 = Vec::with_capacity(result_u8.len() / 4); // Allocate the right size for u32 vector + + for chunk in result_u8.chunks(4) { + let value = (chunk[0] as u32) << 24 + | (chunk[1] as u32) << 16 + | (chunk[2] as u32) << 8 + | (chunk[3] as u32); + result_u32.push(value); + } + + return result_u32; +}