Initial tests for implementing basic AES functionality
This commit is contained in:
parent
e9369f5250
commit
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));
|
||||||
|
}
|
15
src/main.rs
15
src/main.rs
@ -1,8 +1,11 @@
|
|||||||
mod sha;
|
mod sha;
|
||||||
|
mod aes;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Running SHA tests...");
|
println!("Running SHA tests...");
|
||||||
test_sha();
|
test_sha();
|
||||||
|
|
||||||
|
aes::test();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_sha() {
|
fn test_sha() {
|
||||||
@ -18,7 +21,11 @@ fn test_sha() {
|
|||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
|
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592",
|
||||||
sha::sha256("The quick brown fox jumps over the lazy dog".to_string().as_bytes()),
|
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\""
|
"Testing hash for \"The quick brown fox jumps over the lazy dog\""
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -33,7 +40,11 @@ fn test_sha() {
|
|||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
|
"730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
|
||||||
sha::sha224("The quick brown fox jumps over the lazy dog".to_string().as_bytes()),
|
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\""
|
"Testing hash for \"The quick brown fox jumps over the lazy dog\""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user