plain_aes/internal/
decryption.rs

1use super::key_expansion::ExpandedKey;
2
3const MULTIPLE_9: [u8; 256] = [
4    0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
5    0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf, 0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7,
6    0x3b, 0x32, 0x29, 0x20, 0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
7    0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8, 0xc7, 0xce, 0xd5, 0xdc,
8    0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49, 0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01,
9    0xe6, 0xef, 0xf4, 0xfd, 0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91,
10    0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e, 0x21, 0x28, 0x33, 0x3a,
11    0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2, 0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa,
12    0xec, 0xe5, 0xfe, 0xf7, 0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b,
13    0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f, 0x10, 0x19, 0x02, 0x0b,
14    0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8, 0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0,
15    0x47, 0x4e, 0x55, 0x5c, 0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30,
16    0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9, 0xf6, 0xff, 0xe4, 0xed,
17    0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35, 0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d,
18    0xa1, 0xa8, 0xb3, 0xba, 0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6,
19    0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62, 0x5d, 0x54, 0x4f, 0x46,
20];
21const MULTIPLE_11: [u8; 256] = [
22    0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0x58, 0x53, 0x4e, 0x45, 0x74, 0x7f, 0x62, 0x69,
23    0xb0, 0xbb, 0xa6, 0xad, 0x9c, 0x97, 0x8a, 0x81, 0xe8, 0xe3, 0xfe, 0xf5, 0xc4, 0xcf, 0xd2, 0xd9,
24    0x7b, 0x70, 0x6d, 0x66, 0x57, 0x5c, 0x41, 0x4a, 0x23, 0x28, 0x35, 0x3e, 0x0f, 0x04, 0x19, 0x12,
25    0xcb, 0xc0, 0xdd, 0xd6, 0xe7, 0xec, 0xf1, 0xfa, 0x93, 0x98, 0x85, 0x8e, 0xbf, 0xb4, 0xa9, 0xa2,
26    0xf6, 0xfd, 0xe0, 0xeb, 0xda, 0xd1, 0xcc, 0xc7, 0xae, 0xa5, 0xb8, 0xb3, 0x82, 0x89, 0x94, 0x9f,
27    0x46, 0x4d, 0x50, 0x5b, 0x6a, 0x61, 0x7c, 0x77, 0x1e, 0x15, 0x08, 0x03, 0x32, 0x39, 0x24, 0x2f,
28    0x8d, 0x86, 0x9b, 0x90, 0xa1, 0xaa, 0xb7, 0xbc, 0xd5, 0xde, 0xc3, 0xc8, 0xf9, 0xf2, 0xef, 0xe4,
29    0x3d, 0x36, 0x2b, 0x20, 0x11, 0x1a, 0x07, 0x0c, 0x65, 0x6e, 0x73, 0x78, 0x49, 0x42, 0x5f, 0x54,
30    0xf7, 0xfc, 0xe1, 0xea, 0xdb, 0xd0, 0xcd, 0xc6, 0xaf, 0xa4, 0xb9, 0xb2, 0x83, 0x88, 0x95, 0x9e,
31    0x47, 0x4c, 0x51, 0x5a, 0x6b, 0x60, 0x7d, 0x76, 0x1f, 0x14, 0x09, 0x02, 0x33, 0x38, 0x25, 0x2e,
32    0x8c, 0x87, 0x9a, 0x91, 0xa0, 0xab, 0xb6, 0xbd, 0xd4, 0xdf, 0xc2, 0xc9, 0xf8, 0xf3, 0xee, 0xe5,
33    0x3c, 0x37, 0x2a, 0x21, 0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55,
34    0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44, 0x75, 0x7e, 0x63, 0x68,
35    0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80, 0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8,
36    0x7a, 0x71, 0x6c, 0x67, 0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13,
37    0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f, 0xbe, 0xb5, 0xa8, 0xa3,
38];
39const MULTIPLE_13: [u8; 256] = [
40    0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f, 0x5c, 0x51, 0x46, 0x4b,
41    0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3, 0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b,
42    0xbb, 0xb6, 0xa1, 0xac, 0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0,
43    0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14, 0x37, 0x3a, 0x2d, 0x20,
44    0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e, 0x05, 0x08, 0x1f, 0x12, 0x31, 0x3c, 0x2b, 0x26,
45    0xbd, 0xb0, 0xa7, 0xaa, 0x89, 0x84, 0x93, 0x9e, 0xd5, 0xd8, 0xcf, 0xc2, 0xe1, 0xec, 0xfb, 0xf6,
46    0xd6, 0xdb, 0xcc, 0xc1, 0xe2, 0xef, 0xf8, 0xf5, 0xbe, 0xb3, 0xa4, 0xa9, 0x8a, 0x87, 0x90, 0x9d,
47    0x06, 0x0b, 0x1c, 0x11, 0x32, 0x3f, 0x28, 0x25, 0x6e, 0x63, 0x74, 0x79, 0x5a, 0x57, 0x40, 0x4d,
48    0xda, 0xd7, 0xc0, 0xcd, 0xee, 0xe3, 0xf4, 0xf9, 0xb2, 0xbf, 0xa8, 0xa5, 0x86, 0x8b, 0x9c, 0x91,
49    0x0a, 0x07, 0x10, 0x1d, 0x3e, 0x33, 0x24, 0x29, 0x62, 0x6f, 0x78, 0x75, 0x56, 0x5b, 0x4c, 0x41,
50    0x61, 0x6c, 0x7b, 0x76, 0x55, 0x58, 0x4f, 0x42, 0x09, 0x04, 0x13, 0x1e, 0x3d, 0x30, 0x27, 0x2a,
51    0xb1, 0xbc, 0xab, 0xa6, 0x85, 0x88, 0x9f, 0x92, 0xd9, 0xd4, 0xc3, 0xce, 0xed, 0xe0, 0xf7, 0xfa,
52    0xb7, 0xba, 0xad, 0xa0, 0x83, 0x8e, 0x99, 0x94, 0xdf, 0xd2, 0xc5, 0xc8, 0xeb, 0xe6, 0xf1, 0xfc,
53    0x67, 0x6a, 0x7d, 0x70, 0x53, 0x5e, 0x49, 0x44, 0x0f, 0x02, 0x15, 0x18, 0x3b, 0x36, 0x21, 0x2c,
54    0x0c, 0x01, 0x16, 0x1b, 0x38, 0x35, 0x22, 0x2f, 0x64, 0x69, 0x7e, 0x73, 0x50, 0x5d, 0x4a, 0x47,
55    0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3, 0x80, 0x8d, 0x9a, 0x97,
56];
57const MULTIPLE_14: [u8; 256] = [
58    0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62, 0x48, 0x46, 0x54, 0x5a,
59    0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca, 0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba,
60    0xdb, 0xd5, 0xc7, 0xc9, 0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81,
61    0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59, 0x73, 0x7d, 0x6f, 0x61,
62    0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87, 0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7,
63    0x4d, 0x43, 0x51, 0x5f, 0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17,
64    0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14, 0x3e, 0x30, 0x22, 0x2c,
65    0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc, 0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc,
66    0x41, 0x4f, 0x5d, 0x53, 0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b,
67    0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3, 0xe9, 0xe7, 0xf5, 0xfb,
68    0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0, 0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0,
69    0x7a, 0x74, 0x66, 0x68, 0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20,
70    0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e, 0xa4, 0xaa, 0xb8, 0xb6,
71    0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26, 0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56,
72    0x37, 0x39, 0x2b, 0x25, 0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d,
73    0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5, 0x9f, 0x91, 0x83, 0x8d,
74];
75/// Rijndael's reverse S-box.
76const INVERSE_RIJNDAEL_S_BOX: [u8; 256] = [
77    0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
78    0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
79    0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
80    0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
81    0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
82    0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
83    0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
84    0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
85    0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
86    0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
87    0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
88    0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
89    0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
90    0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
91    0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
92    0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d,
93];
94/// Inverse of the [shift_rows] transformation.
95fn inv_shift_rows(state: &mut [u8; 16]) {
96    let orig_state = state.clone();
97    // First column
98    state[1] = orig_state[13];
99    state[2] = orig_state[10];
100    state[3] = orig_state[7];
101    // Second column
102    state[5] = orig_state[1];
103    state[6] = orig_state[14];
104    state[7] = orig_state[11];
105    // Third column
106    state[9] = orig_state[5];
107    state[10] = orig_state[2];
108    state[11] = orig_state[15];
109    // Forth column
110    state[13] = orig_state[9];
111    state[14] = orig_state[6];
112    state[15] = orig_state[3];
113}
114
115/// Substitute each byte in the state with another byte according to [INVERSE_RIJNDAEL_S_BOX].
116fn inv_sub_bytes(state: &mut [u8; 16]) {
117    for i in 0..16 {
118        let byte: usize = state[i].into();
119        state[i] = INVERSE_RIJNDAEL_S_BOX[byte];
120    }
121}
122
123/// Performs a Galois field GF(2^8) matrix multiplication of each byte from the state and a circulant MDS matrix ( 14 11 13 9; 9 14 11 13; 13 9 14 11; 11 13 9 14 ).
124fn inv_mix_columns(state: &mut [u8; 16]) {
125    let orig_state = state.clone();
126    // First column
127    state[0] = MULTIPLE_14[orig_state[0] as usize]
128        ^ MULTIPLE_11[orig_state[1] as usize]
129        ^ MULTIPLE_13[orig_state[2] as usize]
130        ^ MULTIPLE_9[orig_state[3] as usize];
131    state[1] = MULTIPLE_9[orig_state[0] as usize]
132        ^ MULTIPLE_14[orig_state[1] as usize]
133        ^ MULTIPLE_11[orig_state[2] as usize]
134        ^ MULTIPLE_13[orig_state[3] as usize];
135    state[2] = MULTIPLE_13[orig_state[0] as usize]
136        ^ MULTIPLE_9[orig_state[1] as usize]
137        ^ MULTIPLE_14[orig_state[2] as usize]
138        ^ MULTIPLE_11[orig_state[3] as usize];
139    state[3] = MULTIPLE_11[orig_state[0] as usize]
140        ^ MULTIPLE_13[orig_state[1] as usize]
141        ^ MULTIPLE_9[orig_state[2] as usize]
142        ^ MULTIPLE_14[orig_state[3] as usize];
143    // Second column
144    state[4] = MULTIPLE_14[orig_state[4] as usize]
145        ^ MULTIPLE_11[orig_state[5] as usize]
146        ^ MULTIPLE_13[orig_state[6] as usize]
147        ^ MULTIPLE_9[orig_state[7] as usize];
148    state[5] = MULTIPLE_9[orig_state[4] as usize]
149        ^ MULTIPLE_14[orig_state[5] as usize]
150        ^ MULTIPLE_11[orig_state[6] as usize]
151        ^ MULTIPLE_13[orig_state[7] as usize];
152    state[6] = MULTIPLE_13[orig_state[4] as usize]
153        ^ MULTIPLE_9[orig_state[5] as usize]
154        ^ MULTIPLE_14[orig_state[6] as usize]
155        ^ MULTIPLE_11[orig_state[7] as usize];
156    state[7] = MULTIPLE_11[orig_state[4] as usize]
157        ^ MULTIPLE_13[orig_state[5] as usize]
158        ^ MULTIPLE_9[orig_state[6] as usize]
159        ^ MULTIPLE_14[orig_state[7] as usize];
160    // Third column
161
162    state[8] = MULTIPLE_14[orig_state[8] as usize]
163        ^ MULTIPLE_11[orig_state[9] as usize]
164        ^ MULTIPLE_13[orig_state[10] as usize]
165        ^ MULTIPLE_9[orig_state[11] as usize];
166    state[9] = MULTIPLE_9[orig_state[8] as usize]
167        ^ MULTIPLE_14[orig_state[9] as usize]
168        ^ MULTIPLE_11[orig_state[10] as usize]
169        ^ MULTIPLE_13[orig_state[11] as usize];
170    state[10] = MULTIPLE_13[orig_state[8] as usize]
171        ^ MULTIPLE_9[orig_state[9] as usize]
172        ^ MULTIPLE_14[orig_state[10] as usize]
173        ^ MULTIPLE_11[orig_state[11] as usize];
174    state[11] = MULTIPLE_11[orig_state[8] as usize]
175        ^ MULTIPLE_13[orig_state[9] as usize]
176        ^ MULTIPLE_9[orig_state[10] as usize]
177        ^ MULTIPLE_14[orig_state[11] as usize];
178    // Forth column
179
180    state[12] = MULTIPLE_14[orig_state[12] as usize]
181        ^ MULTIPLE_11[orig_state[13] as usize]
182        ^ MULTIPLE_13[orig_state[14] as usize]
183        ^ MULTIPLE_9[orig_state[15] as usize];
184    state[13] = MULTIPLE_9[orig_state[12] as usize]
185        ^ MULTIPLE_14[orig_state[13] as usize]
186        ^ MULTIPLE_11[orig_state[14] as usize]
187        ^ MULTIPLE_13[orig_state[15] as usize];
188    state[14] = MULTIPLE_13[orig_state[12] as usize]
189        ^ MULTIPLE_9[orig_state[13] as usize]
190        ^ MULTIPLE_14[orig_state[14] as usize]
191        ^ MULTIPLE_11[orig_state[15] as usize];
192    state[15] = MULTIPLE_11[orig_state[12] as usize]
193        ^ MULTIPLE_13[orig_state[13] as usize]
194        ^ MULTIPLE_9[orig_state[14] as usize]
195        ^ MULTIPLE_14[orig_state[15] as usize];
196}
197
198/// Decrypt a single block of data using an expanded key.
199///
200/// This function takes the expanded key, and not the original key,
201///  because it is used to implement [crate::decrypt] for each [crate::ModeOfOperation].
202/// In order not to re-expand the key for every block, the expanded key is used instead.
203/// # Warning
204/// This function does not enforce the block size, and as such, passing a block of size other than 16 bytes produces undefined behavior.
205///
206/// Ideally, you should only use this to implement your own mode of operation, for most use cases, you should consider using the higher-level [crate::decrypt] instead.
207
208pub fn decrypt_block(cypher_block: &[u8], expanded_key: &ExpandedKey) -> [u8; 16] {
209    use crate::add_round_key;
210    let mut block = [0; 16];
211    for i in 0..16 {
212        block[i] = cypher_block[i];
213    }
214    let expanded_key_content = expanded_key.content();
215    let rounds_num = expanded_key.round_num();
216    // Final inverse round
217    add_round_key(
218        &mut block,
219        &expanded_key_content[expanded_key_content.len() - 16..],
220    );
221    for i in (1..=rounds_num - 1).rev() {
222        inv_sub_bytes(&mut block);
223        inv_shift_rows(&mut block);
224        add_round_key(
225            &mut block,
226            &expanded_key_content[i * 16..(i + 1) * 16], // Skip the last 16 bytes, they're the same as the last key.
227        );
228        inv_mix_columns(&mut block);
229    }
230    // First inverse round
231    inv_shift_rows(&mut block);
232    inv_sub_bytes(&mut block);
233    add_round_key(&mut block, expanded_key.original_key()); // Add the final key.
234    block
235}
236#[cfg(test)]
237mod tests {
238    use super::*;
239    use crate::{CipherVersion, ModeOfOperation};
240
241    #[test]
242    fn inv_sub_bytes_test() {
243        let inv_substitution_expected: [u8; 16] = [
244            0x54, 0x68, 0x69, 0x73, 0x20, 0x6C, 0x69, 0x62, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6F,
245            0x6F, 0x6C,
246        ]; // This lib is cool
247        let mut mock_state: [u8; 16] = [
248            0x20, 0x45, 0xf9, 0x8f, 0xb7, 0x50, 0xf9, 0xaa, 0xb7, 0xf9, 0x8f, 0xb7, 0xfb, 0xa8,
249            0xa8, 0x50,
250        ];
251        inv_sub_bytes(&mut mock_state);
252        assert!(inv_substitution_expected.iter().eq(&mock_state));
253    }
254    #[test]
255    fn inv_shift_rows_test() {
256        //! Refer to [shift_rows_test] for reversed mock state.
257        let inv_shift_expected: [u8; 16] = [
258            0x20, 0x45, 0xf9, 0x8f, 0xb7, 0x50, 0xf9, 0xaa, 0xb7, 0xf9, 0x8f, 0xb7, 0xfb, 0xa8,
259            0xa8, 0x50,
260        ];
261        let mut mock_state: [u8; 16] = [
262            0x20, 0x50, 0x8f, 0x50, 0xb7, 0xf9, 0xa8, 0x8f, 0xb7, 0xa8, 0xf9, 0xaa, 0xfb, 0x45,
263            0xf9, 0xb7,
264        ];
265        inv_shift_rows(&mut mock_state);
266        assert!(inv_shift_expected.iter().eq(&mock_state));
267    }
268    #[test]
269    fn inv_mix_columns_test() {
270        let inv_mix_expected: [u8; 16] = [
271            0x20, 0x50, 0x8f, 0x50, 0xb7, 0xf9, 0xa8, 0x8f, 0xb7, 0xa8, 0xf9, 0xaa, 0xfb, 0x45,
272            0xf9, 0xb7,
273        ];
274        let mut mock_state: [u8; 16] = [
275            0x6f, 0x5a, 0x85, 0x1f, 0x42, 0x32, 0x8f, 0x96, 0xc5, 0x46, 0x13, 0xdc, 0x6c, 0xd6,
276            0x95, 0xdf,
277        ];
278        inv_mix_columns(&mut mock_state);
279        assert!(inv_mix_expected.iter().eq(&mock_state));
280    }
281    #[test]
282    fn decrypt_block_aes128_test() {
283        let key = [
284            0x54, 0x68, 0x69, 0x73, 0x20, 0x6C, 0x69, 0x62, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6F,
285            0x6F, 0x6C,
286        ]; // This lib is cool
287        let cipher_version = CipherVersion::Aes128(&key[..], ModeOfOperation::ECB);
288        let expanded_key = ExpandedKey::new(&cipher_version);
289        let decrypt_expected = [
290            0x54, 0x48, 0x69, 0x73, 0x20, 0x61, 0x6E, 0x20, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C,
291            0x65, 0x31,
292        ]; // THis an example1
293        let mock_cypher_block = [
294            0x75, 0x92, 0xce, 0xdb, 0x29, 0xf9, 0x3a, 0x1f, 0x9b, 0x98, 0xcb, 0x08, 0x86, 0xd5,
295            0x82, 0x01,
296        ];
297        let decrypt_actual = decrypt_block(&mock_cypher_block, &expanded_key.unwrap());
298        assert!(decrypt_expected.iter().eq(&decrypt_actual));
299    }
300    #[test]
301    fn decrypt_block_aes192_test() {
302        let key = [
303            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
304            0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
305        ];
306        let cipher_version = CipherVersion::Aes192(&key[..], ModeOfOperation::ECB);
307
308        let expanded_key = ExpandedKey::new(&cipher_version);
309        let decrypt_expected = [
310            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
311            0xee, 0xff,
312        ];
313        let mock_block = [
314            0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d,
315            0x71, 0x91,
316        ];
317        let decrypt_actual = decrypt_block(&mock_block, &expanded_key.unwrap());
318        assert!(decrypt_expected.iter().eq(&decrypt_actual));
319    }
320}