Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
c0dfa7f1d6 | |||
c7d508e551 |
59
src/aes/mod.rs
Normal file
59
src/aes/mod.rs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
struct State {
|
||||||
|
state: [[u8; 4]; 4],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn new(data: [u8; 16]) -> Self {
|
||||||
|
let mut state = [[0u8; 4]; 4];
|
||||||
|
for (i, &b) in data.iter().enumerate() {
|
||||||
|
state[i % 4][i / 4] = b;
|
||||||
|
}
|
||||||
|
return State { state };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(&self, c: usize, r: usize) -> u8 {
|
||||||
|
return self.state[c][r];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set(&mut self, c: usize, r: usize, v: u8) {
|
||||||
|
self.state[c][r] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn v0(&self) -> [u8; 4] {
|
||||||
|
return self.state[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn v1(&self) -> [u8; 4] {
|
||||||
|
return self.state[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn v2(&self) -> [u8; 4] {
|
||||||
|
return self.state[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn v3(&self) -> [u8; 4] {
|
||||||
|
return self.state[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn u(&self, i: usize) -> [u8; 4] {
|
||||||
|
return self.state[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//pub fn encrypt(data: &[u8], key: &[u8]) -> &[u8] {
|
||||||
|
pub fn test() {
|
||||||
|
let state_bytes: [u8; 16] = [
|
||||||
|
0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2,
|
||||||
|
0x34,
|
||||||
|
];
|
||||||
|
|
||||||
|
let mut state = State::new(state_bytes);
|
||||||
|
|
||||||
|
println!("v0 {:02x?}", state.v0());
|
||||||
|
println!("v1 {:02x?}", state.v1());
|
||||||
|
println!("v2 {:02x?}", state.v2());
|
||||||
|
println!("v3 {:02x?}", state.v3());
|
||||||
|
println!("u {:02x?}", state.u(2));
|
||||||
|
}
|
37
src/main.rs
37
src/main.rs
@ -1,39 +1,10 @@
|
|||||||
|
mod aes;
|
||||||
mod sha;
|
mod sha;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Running SHA tests...");
|
println!("Running SHA tests...");
|
||||||
test_sha();
|
sha::test();
|
||||||
}
|
|
||||||
|
|
||||||
fn test_sha() {
|
println!("Running AES tests...");
|
||||||
assert_eq!(
|
aes::test();
|
||||||
"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\""
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
@ -1,58 +1,5 @@
|
|||||||
//////////// HELPER FUNCTIONS /////////////
|
mod utils;
|
||||||
fn ch(x: u32, y: u32, z: u32) -> u32 {
|
use utils::*;
|
||||||
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<u32> {
|
|
||||||
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::<u64>());
|
|
||||||
|
|
||||||
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 /////////////
|
|
||||||
|
|
||||||
pub fn sha224(message: &[u8]) -> String {
|
pub fn sha224(message: &[u8]) -> String {
|
||||||
// Set initial hash values
|
// 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
|
// After processing the entire message concatenate the result into the final variable
|
||||||
return hash;
|
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\""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
53
src/sha/utils.rs
Normal file
53
src/sha/utils.rs
Normal file
@ -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<u32> {
|
||||||
|
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::<u64>());
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user