| 1 |
//! Tests for low-level "hazmat" AES functions. |
| 2 |
|
| 3 |
// TODO(tarcieri): support for using the hazmat functions with the `soft` backend |
| 4 |
#![cfg(feature = "hazmat")] |
| 5 |
|
| 6 |
use aes::{Block, Block8}; |
| 7 |
use hex_literal::hex; |
| 8 |
|
| 9 |
/// Round function tests vectors. |
| 10 |
struct RoundTestVector { |
| 11 |
/// State at start of `round[r]`. |
| 12 |
start: [u8; 16], |
| 13 |
|
| 14 |
/// Key schedule value for `round[r]`. |
| 15 |
k_sch: [u8; 16], |
| 16 |
|
| 17 |
/// Cipher output. |
| 18 |
output: [u8; 16], |
| 19 |
} |
| 20 |
|
| 21 |
/// Cipher round function test vectors from FIPS 197 Appendix C.1. |
| 22 |
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[ |
| 23 |
// round 1 |
| 24 |
RoundTestVector { |
| 25 |
start: hex!("00102030405060708090a0b0c0d0e0f0"), |
| 26 |
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"), |
| 27 |
output: hex!("89d810e8855ace682d1843d8cb128fe4"), |
| 28 |
}, |
| 29 |
// round 2 |
| 30 |
RoundTestVector { |
| 31 |
start: hex!("89d810e8855ace682d1843d8cb128fe4"), |
| 32 |
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"), |
| 33 |
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"), |
| 34 |
}, |
| 35 |
// round 3 |
| 36 |
RoundTestVector { |
| 37 |
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"), |
| 38 |
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"), |
| 39 |
output: hex!("fa636a2825b339c940668a3157244d17"), |
| 40 |
}, |
| 41 |
// round 4 |
| 42 |
RoundTestVector { |
| 43 |
start: hex!("fa636a2825b339c940668a3157244d17"), |
| 44 |
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"), |
| 45 |
output: hex!("247240236966b3fa6ed2753288425b6c"), |
| 46 |
}, |
| 47 |
]; |
| 48 |
|
| 49 |
/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1. |
| 50 |
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[ |
| 51 |
// round 1 |
| 52 |
RoundTestVector { |
| 53 |
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"), |
| 54 |
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"), |
| 55 |
output: hex!("54d990a16ba09ab596bbf40ea111702f"), |
| 56 |
}, |
| 57 |
// round 2 |
| 58 |
RoundTestVector { |
| 59 |
start: hex!("54d990a16ba09ab596bbf40ea111702f"), |
| 60 |
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"), |
| 61 |
output: hex!("3e1c22c0b6fcbf768da85067f6170495"), |
| 62 |
}, |
| 63 |
// round 3 |
| 64 |
RoundTestVector { |
| 65 |
start: hex!("3e1c22c0b6fcbf768da85067f6170495"), |
| 66 |
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"), |
| 67 |
output: hex!("b458124c68b68a014b99f82e5f15554c"), |
| 68 |
}, |
| 69 |
// round 4 |
| 70 |
RoundTestVector { |
| 71 |
start: hex!("b458124c68b68a014b99f82e5f15554c"), |
| 72 |
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"), |
| 73 |
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"), |
| 74 |
}, |
| 75 |
]; |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn cipher_round_fips197_vectors() { |
| 79 |
for vector in CIPHER_ROUND_TEST_VECTORS { |
| 80 |
let mut block = Block::from(vector.start); |
| 81 |
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into()); |
| 82 |
assert_eq!(block.as_slice(), &vector.output); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn cipher_round_par_fips197_vectors() { |
| 88 |
let mut blocks = Block8::default(); |
| 89 |
let mut round_keys = Block8::default(); |
| 90 |
|
| 91 |
for i in 0..4 { |
| 92 |
let vector = &CIPHER_ROUND_TEST_VECTORS[i]; |
| 93 |
|
| 94 |
blocks[i] = Block::from(vector.start); |
| 95 |
blocks[i + 4] = Block::from(vector.start); |
| 96 |
|
| 97 |
round_keys[i] = Block::from(vector.k_sch); |
| 98 |
round_keys[i + 4] = Block::from(vector.k_sch); |
| 99 |
} |
| 100 |
|
| 101 |
aes::hazmat::cipher_round_par(&mut blocks, &round_keys); |
| 102 |
|
| 103 |
for i in 0..4 { |
| 104 |
let vector = &CIPHER_ROUND_TEST_VECTORS[i]; |
| 105 |
assert_eq!(blocks[i].as_slice(), &vector.output); |
| 106 |
assert_eq!(blocks[i + 4].as_slice(), &vector.output); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn equiv_inv_cipher_round_fips197_vectors() { |
| 112 |
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS { |
| 113 |
let mut block = Block::from(vector.start); |
| 114 |
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into()); |
| 115 |
assert_eq!(block.as_slice(), &vector.output); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn equiv_inv_cipher_round_par_fips197_vectors() { |
| 121 |
let mut blocks = Block8::default(); |
| 122 |
let mut round_keys = Block8::default(); |
| 123 |
|
| 124 |
for i in 0..4 { |
| 125 |
let vector = &EQUIV_INV_CIPHER_ROUND_TEST_VECTORS[i]; |
| 126 |
|
| 127 |
blocks[i] = Block::from(vector.start); |
| 128 |
blocks[i + 4] = Block::from(vector.start); |
| 129 |
|
| 130 |
round_keys[i] = Block::from(vector.k_sch); |
| 131 |
round_keys[i + 4] = Block::from(vector.k_sch); |
| 132 |
} |
| 133 |
|
| 134 |
aes::hazmat::equiv_inv_cipher_round_par(&mut blocks, &round_keys); |
| 135 |
|
| 136 |
for i in 0..4 { |
| 137 |
let vector = &EQUIV_INV_CIPHER_ROUND_TEST_VECTORS[i]; |
| 138 |
assert_eq!(blocks[i].as_slice(), &vector.output); |
| 139 |
assert_eq!(blocks[i + 4].as_slice(), &vector.output); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn mix_columns_fips197_vector() { |
| 145 |
let mut block = Block::from(hex!("6353e08c0960e104cd70b751bacad0e7")); |
| 146 |
aes::hazmat::mix_columns(&mut block); |
| 147 |
assert_eq!(block.as_slice(), &hex!("5f72641557f5bc92f7be3b291db9f91a")) |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn inv_mix_columns_fips197_vector() { |
| 152 |
let mut block = Block::from(hex!("bd6e7c3df2b5779e0b61216e8b10b689")); |
| 153 |
aes::hazmat::inv_mix_columns(&mut block); |
| 154 |
assert_eq!(block.as_slice(), &hex!("4773b91ff72f354361cb018ea1e6cf2c")) |
| 155 |
} |