Skip to content

Commit

Permalink
Merge pull request #2 from AnarchistHoneybun/clippy_rslv
Browse files Browse the repository at this point in the history
Resolving clippy warnings/errors
  • Loading branch information
jkoudys authored Jun 29, 2024
2 parents 097aa55 + 0d75cf9 commit f0ed076
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 60 deletions.
19 changes: 4 additions & 15 deletions kupyna/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod sub_units;
#[cfg(test)]
mod tests;
mod sub_units;

const STATE_SIZE: usize = 1024;
const HASH_SIZE: usize = 512;

const MAX_MESSAGE_LENGTH: usize = 18446744073709551615;

fn pad_message(message: &[u8], msg_len_bits: usize, state_size: usize) -> Vec<u8> {
let round_msg_len = message.len() * 8;
let d =
Expand Down Expand Up @@ -58,44 +56,35 @@ pub fn hash(message: Vec<u8>, length: Option<usize>) -> Result<Vec<u8>, &'static
let mut message = message;
let message_length: usize;
if let Some(len) = length {
if len > MAX_MESSAGE_LENGTH {
return Err("Message is too long");
}
if len > message.len() * 8 {
return Err("Message length is less than the provided length");
}

let mut trimmed_message = message[..(len/8)].to_vec();
let mut trimmed_message = message[..(len / 8)].to_vec();

if len % 8 != 0 {
let extra_byte = message[len/8];
let extra_byte = message[len / 8];
let extra_bits = len % 8;
let mask = 0xFF << (8 - extra_bits);
trimmed_message.push(extra_byte & mask);
}

message = trimmed_message;
message_length = len;

} else {
if message.len() * 8 > MAX_MESSAGE_LENGTH {
return Err("Message is too long");
}
message_length = message.len() * 8;
}

let padded_message = pad_message(&message, message_length, STATE_SIZE);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE);

let mut init_vector: Vec<u8> = vec![0; STATE_SIZE/8];
let mut init_vector: Vec<u8> = vec![0; STATE_SIZE / 8];
init_vector[0] = 0x80; // set the first bit of this init vector to high


let fin_vector = sub_units::plant(blocks, &init_vector);

let hash = truncate(&fin_vector, HASH_SIZE);

Ok(hash)
}

2 changes: 0 additions & 2 deletions kupyna/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use kupyna;

fn main() {
let message = b"Hello, World!".to_vec();
let _message_length = 0;
Expand Down
19 changes: 5 additions & 14 deletions kupyna/src/sub_units/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod t_xor_plus;

use t_xor_plus::{t_xor_l, t_plus_l};
use t_xor_plus::{t_plus_l, t_xor_l};

const ROUNDS: usize = 14;

Expand All @@ -9,35 +9,26 @@ fn xor_bytes(a: &[u8], b: &[u8]) -> Vec<u8> {
}

fn silo(message_block: &[u8], prev_vector: &[u8]) -> Vec<u8> {

let m_xor_p = xor_bytes(message_block, prev_vector);

let t_xor_mp = t_xor_l(&m_xor_p, ROUNDS);

let t_plus_m = t_plus_l(&message_block, ROUNDS);

let return_vector = xor_bytes(&(xor_bytes(&t_xor_mp, &t_plus_m)), prev_vector);

return_vector
let t_plus_m = t_plus_l(message_block, ROUNDS);

xor_bytes(&(xor_bytes(&t_xor_mp, &t_plus_m)), prev_vector)
}

pub(crate) fn plant(message_blocks: Vec<&[u8]>, init_vector: &[u8]) -> Vec<u8> {

let mut last_vector = init_vector.to_vec();

for block in message_blocks {
last_vector = silo(block, &last_vector);
}

let last_vector = finalize(&last_vector);

last_vector
finalize(&last_vector)
}

fn finalize(ult_processed_block: &[u8]) -> Vec<u8> {
let t_xor_ult_processed_block = t_xor_l(ult_processed_block, ROUNDS);
let final_state = xor_bytes(ult_processed_block, &t_xor_ult_processed_block);
final_state
xor_bytes(ult_processed_block, &t_xor_ult_processed_block)
}

13 changes: 6 additions & 7 deletions kupyna/src/sub_units/t_xor_plus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type Matrix = [[u8; COLS]; ROWS];

use tables::{MDS_MATRIX, SBOXES};


pub(crate) fn block_to_matrix(block: &[u8]) -> Matrix {
let mut matrix = [[0u8; COLS]; ROWS];
for i in 0..ROWS {
Expand All @@ -35,9 +34,9 @@ fn matrix_to_block(matrix: Matrix) -> Vec<u8> {
}

pub(crate) fn add_constant_xor(mut state: Matrix, round: usize) -> Matrix {
for j in 0..ROWS {
for (j, row) in state.iter_mut().enumerate().take(ROWS) {
let constant = ((j * 0x10) ^ round) as u8;
state[j][0] ^= constant;
row[0] ^= constant;
}
state
}
Expand All @@ -59,8 +58,8 @@ pub(crate) fn add_constant_plus(mut state: Matrix, round: usize) -> Matrix {

pub(crate) fn s_box_layer(mut state: Matrix) -> Matrix {
for i in 0..COLS {
for j in 0..ROWS {
state[j][i] = SBOXES[i % 4][state[j][i] as usize];
for row in state.iter_mut() {
row[i] = SBOXES[i % 4][row[i] as usize];
}
}
state
Expand All @@ -70,7 +69,7 @@ pub(crate) fn rotate_rows(mut state: Matrix) -> Matrix {
let mut temp = [0u8; ROWS];
let mut shift: i32 = -1;
for i in 0..COLS {
if (i == COLS - 1) && true {
if i == COLS - 1 {
shift = 11;
} else {
shift += 1;
Expand Down Expand Up @@ -159,4 +158,4 @@ pub fn t_plus_l(block: &[u8], rounds: usize) -> Vec<u8> {
state = mix_columns(state);
}
matrix_to_block(state)
}
}
2 changes: 1 addition & 1 deletion kupyna/src/sub_units/t_xor_plus/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod test_individual_layers;
mod test_xor_plus;
mod test_individual_layers;
22 changes: 10 additions & 12 deletions kupyna/src/tests/blocking_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{pad_message, divide_into_blocks};

use crate::{divide_into_blocks, pad_message};

const STATE_SIZE_512: usize = 1024;

Expand All @@ -9,7 +8,7 @@ fn block_test_0() {

let expected_block_count = 1;

let padded_message = pad_message(&message,0, STATE_SIZE_512);
let padded_message = pad_message(&message, 0, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand All @@ -28,7 +27,7 @@ fn block_test_8() {

let expected_block_count = 1;

let padded_message = pad_message(&message,8, STATE_SIZE_512);
let padded_message = pad_message(&message, 8, STATE_SIZE_512);
let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

assert_eq!(blocks.len(), expected_block_count);
Expand All @@ -52,7 +51,7 @@ fn block_test_512() {

let expected_block_count = 1;

let padded_message = pad_message(&message,512, STATE_SIZE_512);
let padded_message = pad_message(&message, 512, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand All @@ -77,7 +76,7 @@ fn block_test_510() {

let expected_block_count = 1;

let padded_message = pad_message(&message,510, STATE_SIZE_512);
let padded_message = pad_message(&message, 510, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand All @@ -88,7 +87,6 @@ fn block_test_510() {
for block in blocks {
assert_eq!(block.len(), block_len);
}

}

#[test]
Expand All @@ -107,7 +105,7 @@ fn block_test_1024() {

let expected_block_count = 2;

let padded_message = pad_message(&message,1024, STATE_SIZE_512);
let padded_message = pad_message(&message, 1024, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand Down Expand Up @@ -145,7 +143,7 @@ fn block_test_2048() {

let expected_block_count = 3;

let padded_message = pad_message(&message,2048, STATE_SIZE_512);
let padded_message = pad_message(&message, 2048, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand Down Expand Up @@ -178,7 +176,7 @@ fn block_test_1536() {

let expected_block_count = 2;

let padded_message = pad_message(&message,1536, STATE_SIZE_512);
let padded_message = pad_message(&message, 1536, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand All @@ -204,7 +202,7 @@ fn block_test_655() {

let expected_block_count = 1;

let padded_message = pad_message(&message,655, STATE_SIZE_512);
let padded_message = pad_message(&message, 655, STATE_SIZE_512);

let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512);

Expand All @@ -215,4 +213,4 @@ fn block_test_655() {
for block in blocks {
assert_eq!(block.len(), block_len);
}
}
}
2 changes: 1 addition & 1 deletion kupyna/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod padding_tests;
mod blocking_tests;
mod hashing_tests;
mod padding_tests;
16 changes: 8 additions & 8 deletions kupyna/src/tests/padding_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn pad_test_0() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let padded_message = pad_message(&message,0, STATE_SIZE_512);
let padded_message = pad_message(&message, 0, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand All @@ -39,7 +39,7 @@ fn pad_test_8() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let padded_message = pad_message(&message,8, STATE_SIZE_512);
let padded_message = pad_message(&message, 8, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand All @@ -66,7 +66,7 @@ fn pad_test_510() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let padded_message = pad_message(&message,510, STATE_SIZE_512);
let padded_message = pad_message(&message, 510, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand All @@ -93,7 +93,7 @@ fn pad_test_512() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let padded_message = pad_message(&message,512, STATE_SIZE_512);
let padded_message = pad_message(&message, 512, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand Down Expand Up @@ -133,7 +133,7 @@ fn pad_test_1024() {
0x00,
];

let padded_message = pad_message(&message,1024, STATE_SIZE_512);
let padded_message = pad_message(&message, 1024, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ fn pad_test_2048() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let padded_message = pad_message(&message,2048, STATE_SIZE_512);
let padded_message = pad_message(&message, 2048, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ fn pad_test_1536() {
0x00,
];

let padded_message = pad_message(&message,1536, STATE_SIZE_512);
let padded_message = pad_message(&message, 1536, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}
Expand Down Expand Up @@ -262,7 +262,7 @@ fn pad_test_655() {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

let padded_message = pad_message(&message,655, STATE_SIZE_512);
let padded_message = pad_message(&message, 655, STATE_SIZE_512);

assert_eq!(padded_message, expected_output);
}

0 comments on commit f0ed076

Please sign in to comment.