diff --git a/kupyna512/Cargo.toml b/kupyna512/Cargo.toml new file mode 100644 index 00000000..ba6fb9e6 --- /dev/null +++ b/kupyna512/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "kupyna512" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/kupyna512/src/lib.rs b/kupyna512/src/lib.rs new file mode 100644 index 00000000..da4df891 --- /dev/null +++ b/kupyna512/src/lib.rs @@ -0,0 +1,101 @@ +#[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 { + let round_msg_len = message.len() * 8; + let d = + ((-((round_msg_len + 97) as isize) % (state_size as isize)) + state_size as isize) as usize; + + // Calculate the length of padding to be added + let pad_len = d / 8; + + // We set the padded message size upfront to reduce allocations + let padded_len = (msg_len_bits + 7) / 8 + pad_len + 13; + let mut padded_message = vec![0x00; padded_len]; + + // Copy n bits from the input message + let full_bytes = msg_len_bits / 8; + let remaining_bits = msg_len_bits % 8; + + padded_message[..full_bytes].copy_from_slice(&message[..full_bytes]); + + if remaining_bits > 0 { + let last_byte = message[full_bytes]; + padded_message[full_bytes] = last_byte & ((1 << remaining_bits) - 1); + } + + // Set the n+1'th bit to high + padded_message[msg_len_bits / 8] |= 1 << (7 - (msg_len_bits % 8)); + + // Convert the length to a byte array and copy it into the padded message + let n_bytes = (msg_len_bits as u128).to_le_bytes(); // message length in little-endian + padded_message[padded_len - 12..].copy_from_slice(&n_bytes[0..12]); + + padded_message +} + +fn divide_into_blocks(padded_message: &[u8], state_size: usize) -> Vec<&[u8]> { + padded_message.chunks(state_size / 8).collect() +} + +fn truncate(block: &[u8], n: usize) -> Vec { + let bytes_to_keep = n / 8; + let start_index = if block.len() > bytes_to_keep { + block.len() - bytes_to_keep + } else { + 0 + }; + block[start_index..].to_vec() +} + +pub fn hash(message: Vec, length: Option) -> Result, &'static str> { + 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(); + + if len % 8 != 0 { + 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 = 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) +} + diff --git a/kupyna512/src/main.rs b/kupyna512/src/main.rs new file mode 100644 index 00000000..552e0bfd --- /dev/null +++ b/kupyna512/src/main.rs @@ -0,0 +1,10 @@ +use kupyna512; + +fn main() { + let message = b"Hello, World!".to_vec(); + let _message_length = 0; + + let hash_code = kupyna512::hash(message, None).unwrap(); + + println!("{:02X?}", hash_code); +} \ No newline at end of file diff --git a/kupyna512/src/sub_units/mod.rs b/kupyna512/src/sub_units/mod.rs new file mode 100644 index 00000000..beeae736 --- /dev/null +++ b/kupyna512/src/sub_units/mod.rs @@ -0,0 +1,43 @@ +mod t_xor_plus; + +use t_xor_plus::{t_xor_l, t_plus_l}; + +const ROUNDS: usize = 14; + +fn xor_bytes(a: &[u8], b: &[u8]) -> Vec { + a.iter().zip(b.iter()).map(|(x, y)| x ^ y).collect() +} + +fn silo(message_block: &[u8], prev_vector: &[u8]) -> Vec { + + 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 + +} + +pub(crate) fn plant(message_blocks: Vec<&[u8]>, init_vector: &[u8]) -> Vec { + + 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 +} + +fn finalize(ult_processed_block: &[u8]) -> Vec { + 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 +} + diff --git a/kupyna512/src/sub_units/t_xor_plus/mod.rs b/kupyna512/src/sub_units/t_xor_plus/mod.rs new file mode 100644 index 00000000..bf385edf --- /dev/null +++ b/kupyna512/src/sub_units/t_xor_plus/mod.rs @@ -0,0 +1,162 @@ +#[cfg(test)] +mod tests; + +mod tables; + +const ROWS: usize = 16; +const COLS: usize = 8; // For 512-bit state, adjust if needed + +const BITS_IN_BYTE: u8 = 8; +const REDUCTION_POLYNOMIAL: u16 = 0x011d; + +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 { + for j in 0..COLS { + matrix[i][j] = block[i * COLS + j]; + } + } + matrix +} + +fn matrix_to_block(matrix: Matrix) -> Vec { + let mut block = vec![0u8; ROWS * COLS]; + for i in 0..ROWS { + for j in 0..COLS { + block[i * COLS + j] = matrix[i][j]; + } + } + block +} + +pub(crate) fn add_constant_xor(mut state: Matrix, round: usize) -> Matrix { + for j in 0..ROWS { + let constant = ((j * 0x10) ^ round) as u8; + state[j][0] ^= constant; + } + state +} + +pub(crate) fn add_constant_plus(mut state: Matrix, round: usize) -> Matrix { + let state_ptr = state.as_mut_ptr() as *mut u64; + + for j in 0..ROWS { + // dbg!("{}",j); + unsafe { + *state_ptr.add(j) = state_ptr.add(j).read().wrapping_add( + 0x00F0F0F0F0F0F0F3u64 ^ (((((ROWS - j - 1) * 0x10) ^ round) as u64) << 56), + ); + } + } + + state +} + +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]; + } + } + state +} + +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 { + shift = 11; + } else { + shift += 1; + } + for col in 0..ROWS { + temp[(col + shift as usize) % ROWS] = state[col][i]; + } + for col in 0..ROWS { + state[col][i] = temp[col]; + } + } + state +} + +fn multiply_gf(mut x: u8, mut y: u8) -> u8 { + let mut r = 0u8; + + for _ in 0..BITS_IN_BYTE { + if y & 1 == 1 { + r ^= x; + } + let hbit = (x & 0x80) >> 7; + x <<= 1; + if hbit == 1 { + x ^= REDUCTION_POLYNOMIAL as u8; + } + y >>= 1; + } + + r +} + +pub(crate) fn mix_columns(state: Matrix) -> Matrix { + let mut result = [[0u8; COLS]; ROWS]; + + for col in 0..ROWS { + for row in (0..COLS).rev() { + let mut product = 0u8; + for b in (0..COLS).rev() { + product ^= multiply_gf(state[col][b], MDS_MATRIX[row][b]); + } + result[col][row] = product; + } + } + + result +} + +/// Placeholder for the T⊕l transformation. +/// +/// # Arguments +/// +/// * `block` - A byte slice representing the block to be transformed. +/// * `_rounds` - The number of rounds to perform. +/// +/// # Returns +/// +/// * A `Vec` containing the transformed block. +pub fn t_xor_l(block: &[u8], rounds: usize) -> Vec { + let mut state = block_to_matrix(block); + for nu in 0..rounds { + state = add_constant_xor(state, nu); + state = s_box_layer(state); + state = rotate_rows(state); + state = mix_columns(state); + } + matrix_to_block(state) +} + +/// Placeholder for the T+l transformation. +/// +/// # Arguments +/// +/// * `block` - A byte slice representing the block to be transformed. +/// * `_rounds` - The number of rounds to perform. +/// +/// # Returns +/// +/// * A `Vec` containing the transformed block. +pub fn t_plus_l(block: &[u8], rounds: usize) -> Vec { + let mut state = block_to_matrix(block); + for nu in 0..rounds { + state = add_constant_plus(state, nu); + state = s_box_layer(state); + state = rotate_rows(state); + state = mix_columns(state); + } + matrix_to_block(state) +} \ No newline at end of file diff --git a/kupyna512/src/sub_units/t_xor_plus/tables.rs b/kupyna512/src/sub_units/t_xor_plus/tables.rs new file mode 100644 index 00000000..75dfd9db --- /dev/null +++ b/kupyna512/src/sub_units/t_xor_plus/tables.rs @@ -0,0 +1,97 @@ +pub const MDS_MATRIX: [[u8; 8]; 8] = [ + [0x01, 0x01, 0x05, 0x01, 0x08, 0x06, 0x07, 0x04], + [0x04, 0x01, 0x01, 0x05, 0x01, 0x08, 0x06, 0x07], + [0x07, 0x04, 0x01, 0x01, 0x05, 0x01, 0x08, 0x06], + [0x06, 0x07, 0x04, 0x01, 0x01, 0x05, 0x01, 0x08], + [0x08, 0x06, 0x07, 0x04, 0x01, 0x01, 0x05, 0x01], + [0x01, 0x08, 0x06, 0x07, 0x04, 0x01, 0x01, 0x05], + [0x05, 0x01, 0x08, 0x06, 0x07, 0x04, 0x01, 0x01], + [0x01, 0x05, 0x01, 0x08, 0x06, 0x07, 0x04, 0x01], +]; + +pub const SBOXES: [[u8; 256]; 4] = [ + // The first subarray goes here + [ + 0xa8, 0x43, 0x5f, 0x06, 0x6b, 0x75, 0x6c, 0x59, 0x71, 0xdf, 0x87, 0x95, 0x17, 0xf0, 0xd8, + 0x09, 0x6d, 0xf3, 0x1d, 0xcb, 0xc9, 0x4d, 0x2c, 0xaf, 0x79, 0xe0, 0x97, 0xfd, 0x6f, 0x4b, + 0x45, 0x39, 0x3e, 0xdd, 0xa3, 0x4f, 0xb4, 0xb6, 0x9a, 0x0e, 0x1f, 0xbf, 0x15, 0xe1, 0x49, + 0xd2, 0x93, 0xc6, 0x92, 0x72, 0x9e, 0x61, 0xd1, 0x63, 0xfa, 0xee, 0xf4, 0x19, 0xd5, 0xad, + 0x58, 0xa4, 0xbb, 0xa1, 0xdc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, 0x85, 0x25, 0xe6, 0xca, 0x7c, 0x8b, 0x56, + 0x80, + ], + // The second subarray goes here + [ + 0xce, 0xbb, 0xeb, 0x92, 0xea, 0xcb, 0x13, 0xc1, 0xe9, 0x3a, 0xd6, 0xb2, 0xd2, 0x90, 0x17, + 0xf8, 0x42, 0x15, 0x56, 0xb4, 0x65, 0x1c, 0x88, 0x43, 0xc5, 0x5c, 0x36, 0xba, 0xf5, 0x57, + 0x67, 0x8d, 0x31, 0xf6, 0x64, 0x58, 0x9e, 0xf4, 0x22, 0xaa, 0x75, 0x0f, 0x02, 0xb1, 0xdf, + 0x6d, 0x73, 0x4d, 0x7c, 0x26, 0x2e, 0xf7, 0x08, 0x5d, 0x44, 0x3e, 0x9f, 0x14, 0xc8, 0xae, + 0x54, 0x10, 0xd8, 0xbc, 0x1a, 0x6b, 0x69, 0xf3, 0xbd, 0x33, 0xab, 0xfa, 0xd1, 0x9b, 0x68, + 0x4e, 0x16, 0x95, 0x91, 0xee, 0x4c, 0x63, 0x8e, 0x5b, 0xcc, 0x3c, 0x19, 0xa1, 0x81, 0x49, + 0x7b, 0xd9, 0x6f, 0x37, 0x60, 0xca, 0xe7, 0x2b, 0x48, 0xfd, 0x96, 0x45, 0xfc, 0x41, 0x12, + 0x0d, 0x79, 0xe5, 0x89, 0x8c, 0xe3, 0x20, 0x30, 0xdc, 0xb7, 0x6c, 0x4a, 0xb5, 0x3f, 0x97, + 0xd4, 0x62, 0x2d, 0x06, 0xa4, 0xa5, 0x83, 0x5f, 0x2a, 0xda, 0xc9, 0x00, 0x7e, 0xa2, 0x55, + 0xbf, 0x11, 0xd5, 0x9c, 0xcf, 0x0e, 0x0a, 0x3d, 0x51, 0x7d, 0x93, 0x1b, 0xfe, 0xc4, 0x47, + 0x09, 0x86, 0x0b, 0x8f, 0x9d, 0x6a, 0x07, 0xb9, 0xb0, 0x98, 0x18, 0x32, 0x71, 0x4b, 0xef, + 0x3b, 0x70, 0xa0, 0xe4, 0x40, 0xff, 0xc3, 0xa9, 0xe6, 0x78, 0xf9, 0x8b, 0x46, 0x80, 0x1e, + 0x38, 0xe1, 0xb8, 0xa8, 0xe0, 0x0c, 0x23, 0x76, 0x1d, 0x25, 0x24, 0x05, 0xf1, 0x6e, 0x94, + 0x28, 0x9a, 0x84, 0xe8, 0xa3, 0x4f, 0x77, 0xd3, 0x85, 0xe2, 0x52, 0xf2, 0x82, 0x50, 0x7a, + 0x2f, 0x74, 0x53, 0xb3, 0x61, 0xaf, 0x39, 0x35, 0xde, 0xcd, 0x1f, 0x99, 0xac, 0xad, 0x72, + 0x2c, 0xdd, 0xd0, 0x87, 0xbe, 0x5e, 0xa6, 0xec, 0x04, 0xc6, 0x03, 0x34, 0xfb, 0xdb, 0x59, + 0xb6, 0xc2, 0x01, 0xf0, 0x5a, 0xed, 0xa7, 0x66, 0x21, 0x7f, 0x8a, 0x27, 0xc7, 0xc0, 0x29, + 0xd7, + ], + // The third subarray goes here + [ + 0x93, 0xd9, 0x9a, 0xb5, 0x98, 0x22, 0x45, 0xfc, 0xba, 0x6a, 0xdf, 0x02, 0x9f, 0xdc, 0x51, + 0x59, 0x4a, 0x17, 0x2b, 0xc2, 0x94, 0xf4, 0xbb, 0xa3, 0x62, 0xe4, 0x71, 0xd4, 0xcd, 0x70, + 0x16, 0xe1, 0x49, 0x3c, 0xc0, 0xd8, 0x5c, 0x9b, 0xad, 0x85, 0x53, 0xa1, 0x7a, 0xc8, 0x2d, + 0xe0, 0xd1, 0x72, 0xa6, 0x2c, 0xc4, 0xe3, 0x76, 0x78, 0xb7, 0xb4, 0x09, 0x3b, 0x0e, 0x41, + 0x4c, 0xde, 0xb2, 0x90, 0x25, 0xa5, 0xd7, 0x03, 0x11, 0x00, 0xc3, 0x2e, 0x92, 0xef, 0x4e, + 0x12, 0x9d, 0x7d, 0xcb, 0x35, 0x10, 0xd5, 0x4f, 0x9e, 0x4d, 0xa9, 0x55, 0xc6, 0xd0, 0x7b, + 0x18, 0x97, 0xd3, 0x36, 0xe6, 0x48, 0x56, 0x81, 0x8f, 0x77, 0xcc, 0x9c, 0xb9, 0xe2, 0xac, + 0xb8, 0x2f, 0x15, 0xa4, 0x7c, 0xda, 0x38, 0x1e, 0x0b, 0x05, 0xd6, 0x14, 0x6e, 0x6c, 0x7e, + 0x66, 0xfd, 0xb1, 0xe5, 0x60, 0xaf, 0x5e, 0x33, 0x87, 0xc9, 0xf0, 0x5d, 0x6d, 0x3f, 0x88, + 0x8d, 0xc7, 0xf7, 0x1d, 0xe9, 0xec, 0xed, 0x80, 0x29, 0x27, 0xcf, 0x99, 0xa8, 0x50, 0x0f, + 0x37, 0x24, 0x28, 0x30, 0x95, 0xd2, 0x3e, 0x5b, 0x40, 0x83, 0xb3, 0x69, 0x57, 0x1f, 0x07, + 0x1c, 0x8a, 0xbc, 0x20, 0xeb, 0xce, 0x8e, 0xab, 0xee, 0x31, 0xa2, 0x73, 0xf9, 0xca, 0x3a, + 0x1a, 0xfb, 0x0d, 0xc1, 0xfe, 0xfa, 0xf2, 0x6f, 0xbd, 0x96, 0xdd, 0x43, 0x52, 0xb6, 0x08, + 0xf3, 0xae, 0xbe, 0x19, 0x89, 0x32, 0x26, 0xb0, 0xea, 0x4b, 0x64, 0x84, 0x82, 0x6b, 0xf5, + 0x79, 0xbf, 0x01, 0x5f, 0x75, 0x63, 0x1b, 0x23, 0x3d, 0x68, 0x2a, 0x65, 0xe8, 0x91, 0xf6, + 0xff, 0x13, 0x58, 0xf1, 0x47, 0x0a, 0x7f, 0xc5, 0xa7, 0xe7, 0x61, 0x5a, 0x06, 0x46, 0x44, + 0x42, 0x04, 0xa0, 0xdb, 0x39, 0x86, 0x54, 0xaa, 0x8c, 0x34, 0x21, 0x8b, 0xf8, 0x0c, 0x74, + 0x67, + ], + // The fourth subarray goes here + [ + 0x68, 0x8d, 0xca, 0x4d, 0x73, 0x4b, 0x4e, 0x2a, 0xd4, 0x52, 0x26, 0xb3, 0x54, 0x1e, 0x19, + 0x1f, 0x22, 0x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, 0x96, 0xa3, 0x1c, 0xaf, 0x6a, 0x12, 0x84, 0x39, 0xe7, 0xb0, + 0x82, 0xf7, 0xfe, 0x9d, 0x87, 0x5c, 0x81, 0x35, 0xde, 0xb4, 0xa5, 0xfc, 0x80, 0xef, 0xcb, + 0xbb, 0x6b, 0x76, 0xba, 0x5a, 0x7d, 0x78, 0x0b, 0x95, 0xe3, 0xad, 0x74, 0x98, 0x3b, 0x36, + 0x64, 0x6d, 0xdc, 0xf0, 0x59, 0xa9, 0x4c, 0x17, 0x7f, 0x91, 0xb8, 0xc9, 0x57, 0x1b, 0xe0, + 0x61, + ], +]; diff --git a/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs b/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs new file mode 100644 index 00000000..24971c08 --- /dev/null +++ b/kupyna512/src/sub_units/t_xor_plus/tests/mod.rs @@ -0,0 +1,2 @@ +mod test_xor_plus; +mod test_individual_layers; \ No newline at end of file diff --git a/kupyna512/src/sub_units/t_xor_plus/tests/test_individual_layers.rs b/kupyna512/src/sub_units/t_xor_plus/tests/test_individual_layers.rs new file mode 100644 index 00000000..44bbc4e5 --- /dev/null +++ b/kupyna512/src/sub_units/t_xor_plus/tests/test_individual_layers.rs @@ -0,0 +1,146 @@ +use crate::sub_units::t_xor_plus::*; + +#[test] +fn test_add_constant_xor() { + let input = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + let expected_output = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x30, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x60, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x78, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x50, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x48, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0xC0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0xD8, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0xF0, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0xE8, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0xA0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0xB8, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x90, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x88, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + let input_matrix = block_to_matrix(&input); + let result = add_constant_xor(input_matrix, 0); + assert_eq!(result, block_to_matrix(&expected_output)); +} + +#[test] +fn test_add_constant_plus() { + let input = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + let expected_output = [ + 0xF3, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xFB, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, + 0xEF, 0x03, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xE8, 0x0B, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0xE0, 0x13, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0xD8, 0x1B, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0xD0, 0x23, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0xC8, 0x2B, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0xC0, 0x33, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0xB8, 0x3B, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0xB0, 0x43, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0xA8, 0x4B, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0xA0, 0x53, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x98, 0x5B, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x90, 0x63, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x88, + 0x6B, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x80, + ]; + let input_matrix = block_to_matrix(&input); + let result = add_constant_plus(input_matrix, 0); + assert_eq!(result, block_to_matrix(&expected_output)); +} + +#[test] +fn test_s_box_layer() { + let input = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x18, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x30, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x60, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x78, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x50, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x48, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0xC0, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0xD8, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0xF0, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0xE8, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0xA0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0xB8, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x90, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x88, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + let expected_output = [ + 0xA8, 0xBB, 0x9A, 0x4D, 0x6B, 0xCB, 0x45, 0x2A, 0x79, 0x3A, 0xDF, 0xB3, 0x17, 0x90, 0x51, + 0x1F, 0x92, 0x15, 0x2B, 0x3D, 0xC9, 0x1C, 0xBB, 0x83, 0x1F, 0x5C, 0x71, 0xD5, 0x6F, 0x57, + 0x16, 0xBD, 0x34, 0xF6, 0xC0, 0x02, 0xB4, 0xF4, 0xAD, 0x11, 0x8E, 0x0F, 0x7A, 0x5E, 0x49, + 0x6D, 0xD1, 0x66, 0x2E, 0x26, 0xC4, 0x45, 0xD1, 0x5D, 0xB7, 0x94, 0x9C, 0x14, 0x0E, 0x1A, + 0x58, 0x10, 0xB2, 0xDF, 0x2F, 0x6B, 0xD7, 0x0E, 0x42, 0x33, 0xC3, 0x86, 0xC4, 0x9B, 0x4E, + 0x85, 0x8F, 0x95, 0xCB, 0x99, 0x81, 0x63, 0x4F, 0xEE, 0x96, 0x3C, 0x55, 0x30, 0x12, 0x49, + 0x18, 0xB1, 0xBC, 0x37, 0xE6, 0x71, 0x78, 0x2B, 0x8F, 0xFD, 0x6A, 0x45, 0xB9, 0xAA, 0x1C, + 0x0D, 0x2F, 0xAB, 0x38, 0x8C, 0xDA, 0x60, 0xEB, 0xDC, 0x05, 0x0C, 0x36, 0xB5, 0x6C, 0xEC, + 0xCD, 0x62, 0xB1, 0x7C, 0x14, 0xA5, 0x5E, 0x5B, + ]; + let input_matrix = block_to_matrix(&input); + let result = s_box_layer(input_matrix); + assert_eq!(result, block_to_matrix(&expected_output)); +} + +#[test] +fn test_rotate_rows() { + let input = [ + 0xA8, 0xBB, 0x9A, 0x4D, 0x6B, 0xCB, 0x45, 0x2A, 0x79, 0x3A, 0xDF, 0xB3, 0x17, 0x90, 0x51, + 0x1F, 0x92, 0x15, 0x2B, 0x3D, 0xC9, 0x1C, 0xBB, 0x83, 0x1F, 0x5C, 0x71, 0xD5, 0x6F, 0x57, + 0x16, 0xBD, 0x34, 0xF6, 0xC0, 0x02, 0xB4, 0xF4, 0xAD, 0x11, 0x8E, 0x0F, 0x7A, 0x5E, 0x49, + 0x6D, 0xD1, 0x66, 0x2E, 0x26, 0xC4, 0x45, 0xD1, 0x5D, 0xB7, 0x94, 0x9C, 0x14, 0x0E, 0x1A, + 0x58, 0x10, 0xB2, 0xDF, 0x2F, 0x6B, 0xD7, 0x0E, 0x42, 0x33, 0xC3, 0x86, 0xC4, 0x9B, 0x4E, + 0x85, 0x8F, 0x95, 0xCB, 0x99, 0x81, 0x63, 0x4F, 0xEE, 0x96, 0x3C, 0x55, 0x30, 0x12, 0x49, + 0x18, 0xB1, 0xBC, 0x37, 0xE6, 0x71, 0x78, 0x2B, 0x8F, 0xFD, 0x6A, 0x45, 0xB9, 0xAA, 0x1C, + 0x0D, 0x2F, 0xAB, 0x38, 0x8C, 0xDA, 0x60, 0xEB, 0xDC, 0x05, 0x0C, 0x36, 0xB5, 0x6C, 0xEC, + 0xCD, 0x62, 0xB1, 0x7C, 0x14, 0xA5, 0x5E, 0x5B, + ]; + let expected_output = [ + 0xA8, 0x62, 0x05, 0xAB, 0x6A, 0x37, 0x55, 0x66, 0x79, 0xBB, 0xB1, 0x0C, 0x38, 0x45, 0xE6, + 0x94, 0x92, 0x3A, 0x9A, 0x7C, 0x36, 0x8C, 0xB9, 0xDF, 0x1F, 0x15, 0xDF, 0x4D, 0x14, 0xB5, + 0xDA, 0x86, 0x34, 0x5C, 0x2B, 0xB3, 0x6B, 0xA5, 0x6C, 0x99, 0x8E, 0xF6, 0x71, 0x3D, 0x17, + 0xCB, 0x5E, 0x30, 0x2E, 0x0F, 0xC0, 0xD5, 0xC9, 0x90, 0x45, 0x71, 0x9C, 0x26, 0x7A, 0x02, + 0x6F, 0x1C, 0x51, 0xAA, 0x2F, 0x14, 0xC4, 0x5E, 0xB4, 0x57, 0xBB, 0x60, 0xC4, 0x6B, 0x0E, + 0x45, 0x49, 0xF4, 0x16, 0xEC, 0x81, 0x9B, 0xD7, 0x1A, 0xD1, 0x6D, 0xAD, 0x5B, 0x12, 0x63, + 0x4E, 0x0E, 0x58, 0x5D, 0xD1, 0x2A, 0x78, 0x49, 0x4F, 0x85, 0x42, 0x10, 0xB7, 0x1F, 0x1C, + 0x2B, 0x18, 0xEE, 0x8F, 0x33, 0xB2, 0x83, 0xEB, 0x0D, 0x8F, 0xB1, 0x96, 0x95, 0xC3, 0xBD, + 0xCD, 0xDC, 0x2F, 0xFD, 0xBC, 0x3C, 0xCB, 0x11, + ]; + let input_matrix = block_to_matrix(&input); + let result = rotate_rows(input_matrix); + assert_eq!(result, block_to_matrix(&expected_output)); +} + +#[test] +fn test_mix_columns() { + let input = [ + 0xA8, 0x62, 0x05, 0xAB, 0x6A, 0x37, 0x55, 0x66, 0x79, 0xBB, 0xB1, 0x0C, 0x38, 0x45, 0xE6, + 0x94, 0x92, 0x3A, 0x9A, 0x7C, 0x36, 0x8C, 0xB9, 0xDF, 0x1F, 0x15, 0xDF, 0x4D, 0x14, 0xB5, + 0xDA, 0x86, 0x34, 0x5C, 0x2B, 0xB3, 0x6B, 0xA5, 0x6C, 0x99, 0x8E, 0xF6, 0x71, 0x3D, 0x17, + 0xCB, 0x5E, 0x30, 0x2E, 0x0F, 0xC0, 0xD5, 0xC9, 0x90, 0x45, 0x71, 0x9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x60, 0xB9, 0xB2, 0x8B, 0xAE, 0x4D, 0xFE, 0xA8, 0x3F, 0xFB, 0x07, 0xF1, 0x35, 0xB5, + 0x71, 0x78, 0xE6, 0xC8, 0x9B, 0x20, 0x6A, 0xD3, + ]; + let input_matrix = block_to_matrix(&input); + let result = mix_columns(input_matrix); + assert_eq!(result, block_to_matrix(&expected_output)); +} diff --git a/kupyna512/src/sub_units/t_xor_plus/tests/test_xor_plus.rs b/kupyna512/src/sub_units/t_xor_plus/tests/test_xor_plus.rs new file mode 100644 index 00000000..01fecae9 --- /dev/null +++ b/kupyna512/src/sub_units/t_xor_plus/tests/test_xor_plus.rs @@ -0,0 +1,63 @@ +use crate::sub_units::t_xor_plus::{t_plus_l, t_xor_l}; + +#[test] +fn test_t_xor_l() { + let input = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + + let expected_output = [ + 0x60, 0x4B, 0x9D, 0xCF, 0x7E, 0xAA, 0x57, 0x85, 0x94, 0xD1, 0x83, 0xEE, 0xF2, 0xDD, 0x97, + 0xA3, 0x2C, 0x11, 0x1C, 0x81, 0x70, 0xC0, 0xA5, 0x08, 0x6A, 0x08, 0xC9, 0xE4, 0x28, 0x81, + 0x11, 0x32, 0x31, 0xBE, 0xC7, 0xB7, 0x1D, 0x0E, 0xE3, 0x1D, 0xE8, 0x36, 0x3B, 0x4A, 0xA6, + 0xAF, 0x89, 0x0B, 0xDE, 0xEE, 0x5C, 0x96, 0x66, 0x3A, 0x44, 0x38, 0x3A, 0x40, 0x09, 0x30, + 0x60, 0xE7, 0x65, 0x15, 0x2D, 0xEB, 0xEC, 0xD2, 0x5B, 0x83, 0x42, 0xC4, 0xEF, 0x4E, 0x75, + 0x0F, 0xC3, 0xF4, 0x81, 0x4F, 0xA9, 0xE1, 0xD1, 0x1F, 0xE7, 0xF6, 0xF8, 0xCF, 0x32, 0x72, + 0xE7, 0xE1, 0x61, 0x4F, 0x91, 0xAD, 0x6F, 0x01, 0xF7, 0x28, 0xD8, 0xDB, 0xBE, 0x1F, 0x2A, + 0xC1, 0x97, 0x77, 0x1E, 0x37, 0x8F, 0x8D, 0xD7, 0xD1, 0x31, 0x32, 0x7B, 0xF1, 0xA9, 0x43, + 0xA9, 0x55, 0xF1, 0xF7, 0xC8, 0x32, 0xAD, 0xF3, + ]; + + let rounds = 14; + let result = t_xor_l(&input, rounds); + assert_eq!(result, expected_output); +} + +#[test] +fn test_t_plus_l() { + let input = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + + let expected_output = [ + 0x36, 0x57, 0x5D, 0x99, 0x30, 0x36, 0xAF, 0xDE, 0xB2, 0x65, 0x4C, 0x1E, 0x13, 0x66, 0x0A, + 0x9D, 0x4F, 0x0E, 0x10, 0x5C, 0xA2, 0x33, 0x6F, 0x2B, 0xB3, 0x69, 0x00, 0x45, 0x25, 0x9A, + 0x1A, 0x9D, 0x3F, 0x24, 0x85, 0x07, 0xC3, 0x42, 0xA7, 0x0B, 0x42, 0xF7, 0x49, 0x81, 0xEC, + 0xE4, 0x6D, 0xD0, 0x5E, 0x1D, 0x30, 0x9F, 0x77, 0x4E, 0x1E, 0xD2, 0x13, 0x24, 0x7C, 0xC8, + 0x21, 0x46, 0x16, 0x73, 0xC7, 0x41, 0x9A, 0xE1, 0x2B, 0x93, 0x61, 0xF3, 0x2C, 0x75, 0x38, + 0xC1, 0x59, 0x09, 0xB1, 0x97, 0xE2, 0x0F, 0x9E, 0x09, 0xDD, 0x28, 0xCD, 0xD4, 0xD7, 0xC2, + 0x34, 0xDB, 0xDB, 0x47, 0x93, 0x18, 0xA2, 0x58, 0xA7, 0x18, 0x0B, 0x18, 0x33, 0x17, 0x8A, + 0x20, 0xFC, 0xFE, 0x05, 0xA6, 0x06, 0x4F, 0xD7, 0xB1, 0xEA, 0x96, 0x07, 0x99, 0x5E, 0x98, + 0xD9, 0x0D, 0x2D, 0x55, 0xDC, 0xF7, 0x2F, 0x5F, + ]; + + let rounds = 14; + let result = t_plus_l(&input, rounds); + assert_eq!(result, expected_output); +} diff --git a/kupyna512/src/tests/blocking_tests.rs b/kupyna512/src/tests/blocking_tests.rs new file mode 100644 index 00000000..bb257df9 --- /dev/null +++ b/kupyna512/src/tests/blocking_tests.rs @@ -0,0 +1,218 @@ +use crate::{pad_message, divide_into_blocks}; + + +const STATE_SIZE_512: usize = 1024; + +#[test] +fn block_test_0() { + let message: [u8; 0] = []; + + let expected_block_count = 1; + + let padded_message = pad_message(&message,0, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} + +#[test] +fn block_test_8() { + let message: [u8; 1] = [0xFF]; + + let expected_block_count = 1; + + 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); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} + +#[test] +fn block_test_512() { + let message: [u8; 64] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, + ]; + + let expected_block_count = 1; + + let padded_message = pad_message(&message,512, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} + +#[test] +fn block_test_510() { + let message: [u8; 64] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3C, + ]; + + let expected_block_count = 1; + + let padded_message = pad_message(&message,510, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } + +} + +#[test] +fn block_test_1024() { + let message: [u8; 128] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + + let expected_block_count = 2; + + let padded_message = pad_message(&message,1024, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} + +#[test] +fn block_test_2048() { + let message: [u8; 256] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F,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 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, + 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, + 0xFF, + ]; + + let expected_block_count = 3; + + let padded_message = pad_message(&message,2048, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} + +#[test] +fn block_test_1536() { + let message: [u8; 192] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, + 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, + 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, + ]; + + let expected_block_count = 2; + + let padded_message = pad_message(&message,1536, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} + +#[test] +fn block_test_655() { + let message: [u8; 82] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x50, + ]; + + let expected_block_count = 1; + + let padded_message = pad_message(&message,655, STATE_SIZE_512); + + let blocks = divide_into_blocks(&padded_message, STATE_SIZE_512); + + assert_eq!(blocks.len(), expected_block_count); + + // check that all the blocks have equal length + let block_len = STATE_SIZE_512 / 8; + for block in blocks { + assert_eq!(block.len(), block_len); + } +} \ No newline at end of file diff --git a/kupyna512/src/tests/hashing_tests.rs b/kupyna512/src/tests/hashing_tests.rs new file mode 100644 index 00000000..797f4fe3 --- /dev/null +++ b/kupyna512/src/tests/hashing_tests.rs @@ -0,0 +1,207 @@ +use crate::hash; + +#[test] +fn hash_test_0() { + let message: [u8; 0] = []; + let message_length = 0; + + let expected_hash = [ + 0x65, 0x6B, 0x2F, 0x4C, 0xD7, 0x14, 0x62, 0x38, 0x8B, 0x64, 0xA3, 0x70, 0x43, 0xEA, 0x55, + 0xDB, 0xE4, 0x45, 0xD4, 0x52, 0xAE, 0xCD, 0x46, 0xC3, 0x29, 0x83, 0x43, 0x31, 0x4E, 0xF0, + 0x40, 0x19, 0xBC, 0xFA, 0x3F, 0x04, 0x26, 0x5A, 0x98, 0x57, 0xF9, 0x1B, 0xE9, 0x1F, 0xCE, + 0x19, 0x70, 0x96, 0x18, 0x7C, 0xED, 0xA7, 0x8C, 0x9C, 0x1C, 0x02, 0x1C, 0x29, 0x4A, 0x06, + 0x89, 0x19, 0x85, 0x38, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_8() { + let message: [u8; 1] = [0xFF]; + let message_length = 8; + + let expected_hash = [ + 0x87, 0x1B, 0x18, 0xCF, 0x75, 0x4B, 0x72, 0x74, 0x03, 0x07, 0xA9, 0x7B, 0x44, 0x9A, 0xBE, + 0xB3, 0x2B, 0x64, 0x44, 0x4C, 0xC0, 0xD5, 0xA4, 0xD6, 0x58, 0x30, 0xAE, 0x54, 0x56, 0x83, + 0x7A, 0x72, 0xD8, 0x45, 0x8F, 0x12, 0xC8, 0xF0, 0x6C, 0x98, 0xC6, 0x16, 0xAB, 0xE1, 0x18, + 0x97, 0xF8, 0x62, 0x63, 0xB5, 0xCB, 0x77, 0xC4, 0x20, 0xFB, 0x37, 0x53, 0x74, 0xBE, 0xC5, + 0x2B, 0x6D, 0x02, 0x92, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_510() { + let message: [u8; 64] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3C, + ]; + let message_length = 510; + + let expected_hash = [ + 0x2F, 0x3B, 0xBA, 0xC9, 0x8E, 0x87, 0x71, 0xD6, 0xE3, 0xB8, 0xAA, 0x30, 0x15, 0x3A, 0xBC, + 0x4D, 0x0C, 0x29, 0x85, 0xE9, 0x1D, 0xA1, 0xB5, 0x56, 0x8F, 0xD1, 0xBD, 0xD7, 0x05, 0xCC, + 0xAB, 0x7E, 0xE8, 0xD9, 0x5D, 0x2F, 0xC9, 0x8B, 0xFA, 0x53, 0x22, 0xA2, 0x41, 0xE0, 0x9C, + 0x89, 0x6B, 0x58, 0x28, 0x4C, 0x83, 0xF2, 0x48, 0x8C, 0xF9, 0x43, 0xE4, 0xB3, 0xDE, 0x43, + 0xE0, 0x5F, 0x0D, 0xEA, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_512() { + let message: [u8; 64] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, + ]; + let message_length = 512; + + let expected_hash = [ + 0x38, 0x13, 0xE2, 0x10, 0x91, 0x18, 0xCD, 0xFB, 0x5A, 0x6D, 0x5E, 0x72, 0xF7, 0x20, 0x8D, + 0xCC, 0xC8, 0x0A, 0x2D, 0xFB, 0x3A, 0xFD, 0xFB, 0x02, 0xF4, 0x69, 0x92, 0xB5, 0xED, 0xBE, + 0x53, 0x6B, 0x35, 0x60, 0xDD, 0x1D, 0x7E, 0x29, 0xC6, 0xF5, 0x39, 0x78, 0xAF, 0x58, 0xB4, + 0x44, 0xE3, 0x7B, 0xA6, 0x85, 0xC0, 0xDD, 0x91, 0x05, 0x33, 0xBA, 0x5D, 0x78, 0xEF, 0xFF, + 0xC1, 0x3D, 0xE6, 0x2A, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_655() { + let message: [u8; 82] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x50, + ]; + let message_length = 655; + + let expected_hash = [ + 0x01, 0xB7, 0xBD, 0xA1, 0xDB, 0xA7, 0x7D, 0x73, 0x79, 0xF5, 0x3C, 0x2A, 0x49, 0x8A, 0x39, + 0x0D, 0xE5, 0xE6, 0x88, 0xA1, 0x2B, 0xC7, 0x5F, 0xEE, 0x9E, 0x01, 0x0C, 0xB6, 0xFE, 0xBE, + 0xD3, 0xB9, 0xC7, 0x02, 0x39, 0x31, 0xC7, 0x4A, 0x7B, 0x55, 0x16, 0x8A, 0x15, 0x04, 0x7D, + 0x5E, 0x2C, 0xB7, 0x8A, 0x8B, 0x5C, 0xA2, 0xF7, 0x5E, 0x05, 0xE8, 0x0C, 0xA3, 0x98, 0x03, + 0x0E, 0x02, 0xC7, 0xAA, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_1024() { + let message: [u8; 128] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + let message_length = 1024; + + let expected_hash = [ + 0x76, 0xED, 0x1A, 0xC2, 0x8B, 0x1D, 0x01, 0x43, 0x01, 0x3F, 0xFA, 0x87, 0x21, 0x3B, 0x40, + 0x90, 0xB3, 0x56, 0x44, 0x12, 0x63, 0xC1, 0x3E, 0x03, 0xFA, 0x06, 0x0A, 0x8C, 0xAD, 0xA3, + 0x2B, 0x97, 0x96, 0x35, 0x65, 0x7F, 0x25, 0x6B, 0x15, 0xD5, 0xFC, 0xA4, 0xA1, 0x74, 0xDE, + 0x02, 0x9F, 0x0B, 0x1B, 0x43, 0x87, 0xC8, 0x78, 0xFC, 0xC1, 0xC0, 0x0E, 0x87, 0x05, 0xD7, + 0x83, 0xFD, 0x7F, 0xFE, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_1536() { + let message: [u8; 192] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, + 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, + 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, + ]; + let message_length = 1536; + + let expected_hash = [ + 0xB1, 0x89, 0xBF, 0xE9, 0x87, 0xF6, 0x82, 0xF5, 0xF1, 0x67, 0xF0, 0xD7, 0xFA, 0x56, 0x53, + 0x30, 0xE1, 0x26, 0xB6, 0xE5, 0x92, 0xB1, 0xC5, 0x5D, 0x44, 0x29, 0x90, 0x64, 0xEF, 0x95, + 0xB1, 0xA5, 0x7F, 0x3C, 0x2D, 0x0E, 0xCF, 0x17, 0x86, 0x9D, 0x1D, 0x19, 0x9E, 0xBB, 0xD0, + 0x2E, 0x88, 0x57, 0xFB, 0x8A, 0xDD, 0x67, 0xA8, 0xC3, 0x1F, 0x56, 0xCD, 0x82, 0xC0, 0x16, + 0xCF, 0x74, 0x31, 0x21, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} + +#[test] +fn hash_test_2048() { + let message: [u8; 256] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,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 0xD0, 0x3D, 0x73, 0x50, 0xC4, 0x09, 0xCB, 0x3C, 0x29, 0xC2, 0x58, 0x93, 0xA0, 0x72, + 0x4F, 0x6B, 0x13, 0x3F, 0xA8, 0xB9, 0xEB, 0x90, 0xA6, 0x4D, 0x1A, 0x8F, 0xA9, 0x3B, 0x56, + 0x55, 0x66, 0x11, 0xEB, 0x18, 0x7D, 0x71, 0x5A, 0x95, 0x6B, 0x10, 0x7E, 0x3B, 0xFC, 0x76, + 0x48, 0x22, 0x98, 0x13, 0x3A, 0x9C, 0xE8, 0xCB, 0xC0, 0xBD, 0x5E, 0x14, 0x36, 0xA5, 0xB1, + 0x97, 0x28, 0x4F, 0x7E, + ]; + + let actual_hash = hash(message.to_vec(), Some(message_length)).unwrap(); + + assert_eq!(actual_hash, expected_hash); +} diff --git a/kupyna512/src/tests/mod.rs b/kupyna512/src/tests/mod.rs new file mode 100644 index 00000000..5071554b --- /dev/null +++ b/kupyna512/src/tests/mod.rs @@ -0,0 +1,3 @@ +mod padding_tests; +mod blocking_tests; +mod hashing_tests; diff --git a/kupyna512/src/tests/padding_tests.rs b/kupyna512/src/tests/padding_tests.rs new file mode 100644 index 00000000..95415088 --- /dev/null +++ b/kupyna512/src/tests/padding_tests.rs @@ -0,0 +1,268 @@ +use crate::pad_message; + +const STATE_SIZE_512: usize = 1024; + +#[test] +fn pad_test_0() { + let message: [u8; 0] = []; + + let expected_output: [u8; 128] = [ + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let padded_message = pad_message(&message,0, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_8() { + let message: [u8; 1] = [0xFF]; + + let expected_output: [u8; 128] = [ + 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let padded_message = pad_message(&message,8, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_510() { + let message: [u8; 64] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3C, + ]; + + let expected_output: [u8; 128] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let padded_message = pad_message(&message,510, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_512() { + let message: [u8; 64] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, + ]; + + let expected_output: [u8; 128] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let padded_message = pad_message(&message,512, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_1024() { + let message: [u8; 128] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + ]; + + let expected_output: [u8; 256] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, + ]; + + let padded_message = pad_message(&message,1024, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_2048() { + let message: [u8; 256] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0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, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, + 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, + 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, + 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, + 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, + 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, + 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, + 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let padded_message = pad_message(&message,2048, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_1536() { + let message: [u8; 192] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, + 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, + 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, + ]; + + let expected_output: [u8; 256] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, + 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, + 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, + 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, + ]; + + let padded_message = pad_message(&message,1536, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +} + +#[test] +fn pad_test_655() { + let message: [u8; 82] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x50, + ]; + + let expected_output: [u8; 128] = [ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, + 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, + 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, + 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8F, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let padded_message = pad_message(&message,655, STATE_SIZE_512); + + assert_eq!(padded_message, expected_output); +}