1use crate::aes_hmac_sha1;
2use crate::cryptography::AesSizes;
3use kerberos_constants::etypes::{
4    AES128_CTS_HMAC_SHA1_96, AES256_CTS_HMAC_SHA1_96,
5};
6
7use crate::KerberosCipher;
8use crate::Result;
9
10pub struct AesCipher {
12    aes_sizes: AesSizes,
13
14    #[cfg(test)]
15    preamble: Option<Vec<u8>>,
16}
17
18#[cfg(test)]
19impl AesCipher {
20    pub fn new(aes_sizes: AesSizes) -> Self {
21        return Self {
22            aes_sizes,
23            preamble: None,
24        };
25    }
26
27    fn set_preamble(&mut self, preamble: &[u8; 16]) {
28        self.preamble = Some(preamble.to_vec());
29    }
30
31    fn preamble(&self) -> Vec<u8> {
32        if let Some(self_preamble) = &self.preamble {
33            return self_preamble.clone();
34        } else {
35            return aes_hmac_sha1::generate_preamble(&self.aes_sizes);
36        }
37    }
38}
39
40#[cfg(not(test))]
41impl AesCipher {
42    pub fn new(aes_sizes: AesSizes) -> Self {
43        return Self { aes_sizes };
44    }
45
46    fn preamble(&self) -> Vec<u8> {
47        return aes_hmac_sha1::generate_preamble(&self.aes_sizes);
48    }
49}
50
51impl KerberosCipher for AesCipher {
52    fn etype(&self) -> i32 {
53        match self.aes_sizes {
54            AesSizes::Aes128 => AES128_CTS_HMAC_SHA1_96,
55            AesSizes::Aes256 => AES256_CTS_HMAC_SHA1_96,
56        }
57    }
58
59    fn generate_salt(&self, realm: &str, client_name: &str) -> Vec<u8> {
60        return aes_hmac_sha1::generate_salt(realm, client_name);
61    }
62
63    fn generate_key(&self, key: &[u8], salt: &[u8]) -> Vec<u8> {
64        return aes_hmac_sha1::generate_key(key, salt, &self.aes_sizes);
65    }
66
67    fn generate_key_from_string(&self, password: &str, salt: &[u8]) -> Vec<u8> {
68        return aes_hmac_sha1::generate_key_from_string(
69            password,
70            salt,
71            &self.aes_sizes,
72        );
73    }
74
75    fn decrypt(
76        &self,
77        key: &[u8],
78        key_usage: i32,
79        ciphertext: &[u8],
80    ) -> Result<Vec<u8>> {
81        return aes_hmac_sha1::decrypt(
82            key,
83            key_usage,
84            ciphertext,
85            &self.aes_sizes,
86        );
87    }
88
89    fn encrypt(&self, key: &[u8], key_usage: i32, plaintext: &[u8]) -> Vec<u8> {
90        let preamble = self.preamble();
91        return aes_hmac_sha1::encrypt(
92            key,
93            key_usage,
94            plaintext,
95            &preamble,
96            &self.aes_sizes,
97        );
98    }
99}
100
101#[cfg(test)]
102mod test {
103    use super::*;
104
105    #[test]
106    fn test_aes_256_hmac_sh1_encrypt() {
107        let mut aes256_cipher = AesCipher::new(AesSizes::Aes256);
108        aes256_cipher.set_preamble(&[0; 16]);
109
110        assert_eq!(
111            vec![
112                0x29, 0x73, 0x7f, 0x3d, 0xb6, 0xbc, 0xdf, 0xe9, 0x99, 0x0f,
113                0xb2, 0x13, 0x6d, 0x3e, 0xfe, 0x6f, 0x21, 0x00, 0xe6, 0xc4,
114                0xac, 0x75, 0x82, 0x42, 0x99, 0xd8, 0xd3, 0x70, 0x2f, 0x5a,
115                0x2e, 0x31, 0xc7, 0xa3, 0x36, 0x74, 0x7d, 0xfd, 0x73, 0x4a,
116                0x1e, 0xa0, 0x16, 0x5e, 0xbb, 0x27, 0xc0, 0xd7, 0xce, 0x9b,
117                0x5a, 0xec, 0x7a
118            ],
119            aes256_cipher.generate_key_from_string_and_encrypt(
120                "admin",
121                "admin1234".as_bytes(),
122                1,
123                &[
124                    0x33, 0x61, 0x68, 0x77, 0x7a, 0x74, 0x39, 0x4d, 0x47, 0x39,
125                    0x57, 0x56, 0x45, 0x75, 0x42, 0x56, 0x43, 0x35, 0x6a, 0x30,
126                    0x6f, 0x69, 0x36, 0x73, 0x49
127                ]
128            )
129        );
130
131        assert_eq!(
132            vec![
133                0x3d, 0x29, 0x1c, 0x68, 0x54, 0x89, 0xe7, 0xb7, 0x5d, 0xab,
134                0xdc, 0x6e, 0x01, 0x0a, 0xd0, 0x01, 0x9d, 0xb1, 0x64, 0x81,
135                0xb1, 0x2c, 0xb8, 0xbf, 0xa5, 0x13, 0x61, 0x92, 0x42, 0x76,
136                0x1f, 0x99, 0x0d, 0xe2, 0xc0, 0x27, 0x66, 0x1c, 0x98, 0x33,
137                0xbc, 0xce, 0xd3
138            ],
139            aes256_cipher.generate_key_from_string_and_encrypt(
140                "test",
141                "test1234".as_bytes(),
142                2,
143                &[
144                    0x6c, 0x4a, 0x33, 0x66, 0x74, 0x66, 0x77, 0x78, 0x6a, 0x73,
145                    0x52, 0x35, 0x32, 0x32, 0x4f
146                ]
147            )
148        );
149
150        assert_eq!(
151            vec![
152                0xb4, 0xc9, 0x95, 0x36, 0x2e, 0x8f, 0xb1, 0x7c, 0x5f, 0x8f,
153                0xcf, 0xc9, 0xe2, 0xe8, 0x26, 0xb9, 0xb2, 0x6f, 0xb4, 0x8c,
154                0xab, 0x44, 0x29, 0xdf, 0xfd, 0x93, 0x96, 0x59, 0x70, 0xfd,
155                0xb5, 0x59, 0xb3, 0xdf, 0x3f, 0xa1, 0xe4, 0x33, 0x5f, 0x82,
156                0xbd, 0xd3, 0x33, 0x1b, 0x60
157            ],
158            aes256_cipher.generate_key_from_string_and_encrypt(
159                "1337",
160                "13371234".as_bytes(),
161                3,
162                &[
163                    0x51, 0x42, 0x64, 0x33, 0x69, 0x71, 0x6b, 0x4b, 0x79, 0x5a,
164                    0x72, 0x35, 0x59, 0x4a, 0x62, 0x6c, 0x4e
165                ]
166            )
167        );
168
169        assert_eq!(
170            vec![
171                0xff, 0xb5, 0xa2, 0xaa, 0xe1, 0xaa, 0x26, 0x0b, 0xad, 0xcf,
172                0x5d, 0xcb, 0xe4, 0x3c, 0xdc, 0x30, 0x00, 0x2e, 0x3d, 0x97,
173                0x05, 0x22, 0xf1, 0x83, 0x95, 0x18, 0xbf, 0x62, 0x46, 0xbb,
174                0xec, 0x0d, 0x4c, 0x89, 0xb0, 0xc5, 0xb5, 0x81, 0xae
175            ],
176            aes256_cipher.generate_key_from_string_and_encrypt(
177                "",
178                "1234".as_bytes(),
179                4,
180                &[
181                    0x64, 0x4d, 0x61, 0x72, 0x7a, 0x4b, 0x43, 0x4f, 0x45, 0x54,
182                    0x37
183                ]
184            )
185        );
186
187        assert_eq!(
188            vec![
189                0x53, 0x2b, 0x02, 0xa2, 0xe0, 0xc9, 0x74, 0xb6, 0x79, 0x41,
190                0xca, 0xc1, 0x21, 0x72, 0x29, 0x50, 0x4f, 0x1f, 0xb2, 0x27,
191                0xf5, 0xe0, 0x40, 0xb3, 0xd2, 0x5c, 0xf5, 0xdd, 0x0b, 0x1a,
192                0x3f, 0x3d, 0x93, 0x26, 0x7c, 0xbd, 0x69, 0xa6, 0x24, 0x48,
193                0x09, 0x3d
194            ],
195            aes256_cipher.generate_key_from_string_and_encrypt(
196                "12345678",
197                "123456781234".as_bytes(),
198                5,
199                &[
200                    0x71, 0x75, 0x65, 0x4a, 0x6d, 0x72, 0x78, 0x76, 0x50, 0x47,
201                    0x5a, 0x68, 0x6d, 0x78
202                ]
203            )
204        );
205
206        assert_eq!(
207            vec![
208                0x4d, 0x48, 0x34, 0xe8, 0x61, 0x61, 0x8d, 0xa5, 0x8f, 0x27,
209                0x88, 0xff, 0xa7, 0xeb, 0xbb, 0x23, 0x6a, 0x74, 0x0f, 0x4c,
210                0xb9, 0x44, 0x79, 0xf7, 0xdc, 0xc3, 0xc3, 0xa3, 0xdc, 0xf2,
211                0xd6, 0x96, 0x36, 0x8c, 0xb7, 0xf3, 0xcc, 0xc5, 0x8a, 0x29,
212                0x6e, 0xf8, 0x5d, 0x09, 0xc9, 0xb8, 0x34, 0x0b, 0x93, 0xa0,
213                0xd8
214            ],
215            aes256_cipher.generate_key_from_string_and_encrypt(
216                "123456789",
217                "1234567891234".as_bytes(),
218                6,
219                &[
220                    0x6d, 0x4a, 0x79, 0x31, 0x42, 0x6d, 0x74, 0x54, 0x39, 0x33,
221                    0x31, 0x56, 0x72, 0x50, 0x63, 0x6b, 0x38, 0x6c, 0x61, 0x4e,
222                    0x77, 0x32, 0x56
223                ]
224            )
225        );
226    }
227
228    #[test]
229    fn test_aes_256_hmac_sh1_decrypt() {
230        let aes256_cipher = AesCipher::new(AesSizes::Aes256);
231
232        assert_eq!(
233            vec![
234                0x33, 0x61, 0x68, 0x77, 0x7a, 0x74, 0x39, 0x4d, 0x47, 0x39,
235                0x57, 0x56, 0x45, 0x75, 0x42, 0x56, 0x43, 0x35, 0x6a, 0x30,
236                0x6f, 0x69, 0x36, 0x73, 0x49
237            ],
238            aes256_cipher
239                .generate_key_from_string_and_decrypt(
240                    "admin",
241                    "admin1234".as_bytes(),
242                    1,
243                    &[
244                        0x29, 0x73, 0x7f, 0x3d, 0xb6, 0xbc, 0xdf, 0xe9, 0x99,
245                        0x0f, 0xb2, 0x13, 0x6d, 0x3e, 0xfe, 0x6f, 0x21, 0x00,
246                        0xe6, 0xc4, 0xac, 0x75, 0x82, 0x42, 0x99, 0xd8, 0xd3,
247                        0x70, 0x2f, 0x5a, 0x2e, 0x31, 0xc7, 0xa3, 0x36, 0x74,
248                        0x7d, 0xfd, 0x73, 0x4a, 0x1e, 0xa0, 0x16, 0x5e, 0xbb,
249                        0x27, 0xc0, 0xd7, 0xce, 0x9b, 0x5a, 0xec, 0x7a
250                    ]
251                )
252                .unwrap()
253        );
254
255        assert_eq!(
256            vec![
257                0x6c, 0x4a, 0x33, 0x66, 0x74, 0x66, 0x77, 0x78, 0x6a, 0x73,
258                0x52, 0x35, 0x32, 0x32, 0x4f
259            ],
260            aes256_cipher
261                .generate_key_from_string_and_decrypt(
262                    "test",
263                    "test1234".as_bytes(),
264                    2,
265                    &[
266                        0x3d, 0x29, 0x1c, 0x68, 0x54, 0x89, 0xe7, 0xb7, 0x5d,
267                        0xab, 0xdc, 0x6e, 0x01, 0x0a, 0xd0, 0x01, 0x9d, 0xb1,
268                        0x64, 0x81, 0xb1, 0x2c, 0xb8, 0xbf, 0xa5, 0x13, 0x61,
269                        0x92, 0x42, 0x76, 0x1f, 0x99, 0x0d, 0xe2, 0xc0, 0x27,
270                        0x66, 0x1c, 0x98, 0x33, 0xbc, 0xce, 0xd3
271                    ]
272                )
273                .unwrap()
274        );
275
276        assert_eq!(
277            vec![
278                0x51, 0x42, 0x64, 0x33, 0x69, 0x71, 0x6b, 0x4b, 0x79, 0x5a,
279                0x72, 0x35, 0x59, 0x4a, 0x62, 0x6c, 0x4e
280            ],
281            aes256_cipher
282                .generate_key_from_string_and_decrypt(
283                    "1337",
284                    "13371234".as_bytes(),
285                    3,
286                    &[
287                        0xb4, 0xc9, 0x95, 0x36, 0x2e, 0x8f, 0xb1, 0x7c, 0x5f,
288                        0x8f, 0xcf, 0xc9, 0xe2, 0xe8, 0x26, 0xb9, 0xb2, 0x6f,
289                        0xb4, 0x8c, 0xab, 0x44, 0x29, 0xdf, 0xfd, 0x93, 0x96,
290                        0x59, 0x70, 0xfd, 0xb5, 0x59, 0xb3, 0xdf, 0x3f, 0xa1,
291                        0xe4, 0x33, 0x5f, 0x82, 0xbd, 0xd3, 0x33, 0x1b, 0x60
292                    ]
293                )
294                .unwrap()
295        );
296
297        assert_eq!(
298            vec![
299                0x64, 0x4d, 0x61, 0x72, 0x7a, 0x4b, 0x43, 0x4f, 0x45, 0x54,
300                0x37
301            ],
302            aes256_cipher
303                .generate_key_from_string_and_decrypt(
304                    "",
305                    "1234".as_bytes(),
306                    4,
307                    &[
308                        0xff, 0xb5, 0xa2, 0xaa, 0xe1, 0xaa, 0x26, 0x0b, 0xad,
309                        0xcf, 0x5d, 0xcb, 0xe4, 0x3c, 0xdc, 0x30, 0x00, 0x2e,
310                        0x3d, 0x97, 0x05, 0x22, 0xf1, 0x83, 0x95, 0x18, 0xbf,
311                        0x62, 0x46, 0xbb, 0xec, 0x0d, 0x4c, 0x89, 0xb0, 0xc5,
312                        0xb5, 0x81, 0xae
313                    ]
314                )
315                .unwrap()
316        );
317
318        assert_eq!(
319            vec![
320                0x71, 0x75, 0x65, 0x4a, 0x6d, 0x72, 0x78, 0x76, 0x50, 0x47,
321                0x5a, 0x68, 0x6d, 0x78
322            ],
323            aes256_cipher
324                .generate_key_from_string_and_decrypt(
325                    "12345678",
326                    "123456781234".as_bytes(),
327                    5,
328                    &[
329                        0x53, 0x2b, 0x02, 0xa2, 0xe0, 0xc9, 0x74, 0xb6, 0x79,
330                        0x41, 0xca, 0xc1, 0x21, 0x72, 0x29, 0x50, 0x4f, 0x1f,
331                        0xb2, 0x27, 0xf5, 0xe0, 0x40, 0xb3, 0xd2, 0x5c, 0xf5,
332                        0xdd, 0x0b, 0x1a, 0x3f, 0x3d, 0x93, 0x26, 0x7c, 0xbd,
333                        0x69, 0xa6, 0x24, 0x48, 0x09, 0x3d
334                    ]
335                )
336                .unwrap()
337        );
338
339        assert_eq!(
340            vec![
341                0x6d, 0x4a, 0x79, 0x31, 0x42, 0x6d, 0x74, 0x54, 0x39, 0x33,
342                0x31, 0x56, 0x72, 0x50, 0x63, 0x6b, 0x38, 0x6c, 0x61, 0x4e,
343                0x77, 0x32, 0x56
344            ],
345            aes256_cipher
346                .generate_key_from_string_and_decrypt(
347                    "123456789",
348                    "1234567891234".as_bytes(),
349                    6,
350                    &[
351                        0x4d, 0x48, 0x34, 0xe8, 0x61, 0x61, 0x8d, 0xa5, 0x8f,
352                        0x27, 0x88, 0xff, 0xa7, 0xeb, 0xbb, 0x23, 0x6a, 0x74,
353                        0x0f, 0x4c, 0xb9, 0x44, 0x79, 0xf7, 0xdc, 0xc3, 0xc3,
354                        0xa3, 0xdc, 0xf2, 0xd6, 0x96, 0x36, 0x8c, 0xb7, 0xf3,
355                        0xcc, 0xc5, 0x8a, 0x29, 0x6e, 0xf8, 0x5d, 0x09, 0xc9,
356                        0xb8, 0x34, 0x0b, 0x93, 0xa0, 0xd8
357                    ]
358                )
359                .unwrap()
360        );
361    }
362
363    #[test]
364    fn test_aes_128_hmac_sh1_encrypt() {
365        let mut aes128_cipher = AesCipher::new(AesSizes::Aes128);
366        aes128_cipher.set_preamble(&[0; 16]);
367
368        assert_eq!(
369            vec![
370                0xde, 0xd4, 0xd3, 0xbf, 0xd7, 0x88, 0xa4, 0xb5, 0xec, 0x6b,
371                0x0d, 0x9c, 0x8f, 0x24, 0x00, 0xea, 0x04, 0x47, 0x94, 0xa5,
372                0xea, 0x27, 0xec, 0xd0, 0x3f, 0x8c, 0xf4, 0x2b, 0x58, 0x00,
373                0x8c, 0xcd, 0x27, 0xf4, 0x27, 0x78, 0x19, 0xa2, 0x6b, 0x27,
374                0xd9
375            ],
376            aes128_cipher.generate_key_from_string_and_encrypt(
377                "admin",
378                "admin1234".as_bytes(),
379                1,
380                &[
381                    0x6c, 0x38, 0x38, 0x70, 0x53, 0x78, 0x6b, 0x4d, 0x79, 0x78,
382                    0x77, 0x68, 0x67
383                ]
384            )
385        );
386
387        assert_eq!(
388            vec![
389                0xec, 0x67, 0xab, 0x44, 0xbc, 0x06, 0x00, 0x1a, 0x99, 0x34,
390                0x82, 0xd6, 0xc9, 0xa7, 0x1a, 0xd8, 0xb7, 0x8c, 0x9b, 0xd3,
391                0x23, 0xf8, 0x46, 0x97, 0x99, 0x01, 0x72, 0x41, 0xab, 0xed,
392                0x5e, 0x66, 0x24, 0xc5, 0xa5, 0x99, 0x84, 0x6a, 0x9f, 0xed,
393                0x46, 0xfe, 0xf5, 0xd5
394            ],
395            aes128_cipher.generate_key_from_string_and_encrypt(
396                "test",
397                "test1234".as_bytes(),
398                2,
399                &[
400                    0x37, 0x78, 0x58, 0x72, 0x46, 0x36, 0x49, 0x4b, 0x6b, 0x63,
401                    0x54, 0x47, 0x75, 0x6f, 0x6f, 0x4e
402                ]
403            )
404        );
405
406        assert_eq!(
407            vec![
408                0x3c, 0x8d, 0xb2, 0xe5, 0xe4, 0x7c, 0x7e, 0x5c, 0x7b, 0x74,
409                0x69, 0xa2, 0xdd, 0xb2, 0x5d, 0xbf, 0xc9, 0x40, 0x79, 0x88,
410                0x9d, 0x5b, 0x03, 0xe1, 0x8a, 0x9f, 0x29, 0xd8, 0x64, 0xb6,
411                0x6c, 0xf9, 0x16, 0xc3, 0x62, 0x61, 0xd4, 0xa3
412            ],
413            aes128_cipher.generate_key_from_string_and_encrypt(
414                "1337",
415                "13371234".as_bytes(),
416                3,
417                &[0x39, 0x4e, 0x72, 0x46, 0x64, 0x74, 0x74, 0x68, 0x4d, 0x38]
418            )
419        );
420
421        assert_eq!(
422            vec![
423                0x6b, 0x17, 0x24, 0x0a, 0x56, 0xd6, 0xd1, 0xf5, 0x1b, 0x73,
424                0x76, 0xfa, 0x54, 0xd8, 0xb0, 0x43, 0x14, 0xe3, 0x1b, 0x1f,
425                0x05, 0xda, 0x5b, 0x74, 0xea, 0xc8, 0x4b, 0x42, 0x9c, 0x8d,
426                0x41, 0xfa, 0x46, 0x9e, 0x65, 0x76, 0xa1, 0x62, 0xe2, 0xde,
427                0xfb, 0x57, 0xb1, 0x01, 0xa1, 0x2f, 0xde, 0xc9, 0x56, 0x76,
428                0x7a, 0xe2, 0x3c, 0x56, 0x71, 0xd7, 0xf0, 0x91, 0x80
429            ],
430            aes128_cipher.generate_key_from_string_and_encrypt(
431                "",
432                "1234".as_bytes(),
433                4,
434                &[
435                    0x62, 0x4a, 0x71, 0x70, 0x5a, 0x49, 0x69, 0x43, 0x45, 0x44,
436                    0x68, 0x6f, 0x78, 0x51, 0x76, 0x47, 0x58, 0x74, 0x30, 0x43,
437                    0x6c, 0x62, 0x50, 0x30, 0x36, 0x66, 0x51, 0x4f, 0x56, 0x36,
438                    0x6b
439                ]
440            )
441        );
442
443        assert_eq!(
444            vec![
445                0x8a, 0xe4, 0x4b, 0xca, 0xba, 0x4d, 0x5f, 0x9a, 0xb4, 0xbc,
446                0x0f, 0x86, 0xc7, 0xa3, 0x05, 0x05, 0x28, 0x9b, 0xae, 0x9b,
447                0x9e, 0x66, 0xb5, 0xb9, 0x4e, 0xa4, 0xaa, 0x59, 0x6f, 0x55,
448                0x60, 0x41, 0x8d, 0x8a, 0x46, 0xad, 0x39, 0x40, 0x58, 0x9e,
449                0xca, 0x62, 0xef, 0x26, 0x24, 0x54, 0x95, 0xca, 0x0c, 0x01,
450                0xfd, 0x07, 0xf1
451            ],
452            aes128_cipher.generate_key_from_string_and_encrypt(
453                "12345678",
454                "123456781234".as_bytes(),
455                5,
456                &[
457                    0x42, 0x39, 0x70, 0x37, 0x77, 0x6d, 0x6f, 0x59, 0x55, 0x57,
458                    0x5a, 0x76, 0x6a, 0x6e, 0x39, 0x44, 0x55, 0x61, 0x44, 0x4c,
459                    0x51, 0x4c, 0x70, 0x5a, 0x78
460                ]
461            )
462        );
463
464        assert_eq!(
465            vec![
466                0x05, 0x0d, 0x9c, 0x4c, 0x30, 0x3d, 0x26, 0x39, 0xb6, 0xff,
467                0x82, 0xe0, 0x37, 0x83, 0xee, 0x60, 0x2a, 0x10, 0x3c, 0xb6,
468                0x77, 0x6b, 0x66, 0x80, 0x3c, 0x7f, 0xe5, 0xe0, 0xe0, 0x65,
469                0x6e, 0x68, 0xec, 0x15, 0x49, 0x71, 0xf5, 0xba, 0x45, 0x99,
470                0xf2, 0x52, 0x34, 0x18, 0x58, 0x32, 0xff, 0x29, 0x2f, 0x0d,
471                0x26, 0x32, 0x6b, 0x2b, 0x01, 0xd2, 0xe3
472            ],
473            aes128_cipher.generate_key_from_string_and_encrypt(
474                "123456789",
475                "1234567891234".as_bytes(),
476                6,
477                &[
478                    0x70, 0x4d, 0x46, 0x5a, 0x56, 0x6e, 0x79, 0x36, 0x47, 0x42,
479                    0x64, 0x35, 0x48, 0x35, 0x38, 0x73, 0x76, 0x37, 0x43, 0x37,
480                    0x77, 0x51, 0x37, 0x42, 0x69, 0x30, 0x6a, 0x48, 0x70
481                ]
482            )
483        );
484    }
485
486    #[test]
487    fn test_aes_128_hmac_sh1_decrypt() {
488        let aes128_cipher = AesCipher::new(AesSizes::Aes128);
489
490        assert_eq!(
491            vec![
492                0x6c, 0x38, 0x38, 0x70, 0x53, 0x78, 0x6b, 0x4d, 0x79, 0x78,
493                0x77, 0x68, 0x67
494            ],
495            aes128_cipher
496                .generate_key_from_string_and_decrypt(
497                    "admin",
498                    "admin1234".as_bytes(),
499                    1,
500                    &[
501                        0xde, 0xd4, 0xd3, 0xbf, 0xd7, 0x88, 0xa4, 0xb5, 0xec,
502                        0x6b, 0x0d, 0x9c, 0x8f, 0x24, 0x00, 0xea, 0x04, 0x47,
503                        0x94, 0xa5, 0xea, 0x27, 0xec, 0xd0, 0x3f, 0x8c, 0xf4,
504                        0x2b, 0x58, 0x00, 0x8c, 0xcd, 0x27, 0xf4, 0x27, 0x78,
505                        0x19, 0xa2, 0x6b, 0x27, 0xd9
506                    ]
507                )
508                .unwrap()
509        );
510
511        assert_eq!(
512            vec![
513                0x37, 0x78, 0x58, 0x72, 0x46, 0x36, 0x49, 0x4b, 0x6b, 0x63,
514                0x54, 0x47, 0x75, 0x6f, 0x6f, 0x4e
515            ],
516            aes128_cipher
517                .generate_key_from_string_and_decrypt(
518                    "test",
519                    "test1234".as_bytes(),
520                    2,
521                    &[
522                        0xec, 0x67, 0xab, 0x44, 0xbc, 0x06, 0x00, 0x1a, 0x99,
523                        0x34, 0x82, 0xd6, 0xc9, 0xa7, 0x1a, 0xd8, 0xb7, 0x8c,
524                        0x9b, 0xd3, 0x23, 0xf8, 0x46, 0x97, 0x99, 0x01, 0x72,
525                        0x41, 0xab, 0xed, 0x5e, 0x66, 0x24, 0xc5, 0xa5, 0x99,
526                        0x84, 0x6a, 0x9f, 0xed, 0x46, 0xfe, 0xf5, 0xd5
527                    ]
528                )
529                .unwrap()
530        );
531
532        assert_eq!(
533            vec![0x39, 0x4e, 0x72, 0x46, 0x64, 0x74, 0x74, 0x68, 0x4d, 0x38],
534            aes128_cipher
535                .generate_key_from_string_and_decrypt(
536                    "1337",
537                    "13371234".as_bytes(),
538                    3,
539                    &[
540                        0x3c, 0x8d, 0xb2, 0xe5, 0xe4, 0x7c, 0x7e, 0x5c, 0x7b,
541                        0x74, 0x69, 0xa2, 0xdd, 0xb2, 0x5d, 0xbf, 0xc9, 0x40,
542                        0x79, 0x88, 0x9d, 0x5b, 0x03, 0xe1, 0x8a, 0x9f, 0x29,
543                        0xd8, 0x64, 0xb6, 0x6c, 0xf9, 0x16, 0xc3, 0x62, 0x61,
544                        0xd4, 0xa3
545                    ]
546                )
547                .unwrap()
548        );
549
550        assert_eq!(
551            vec![
552                0x62, 0x4a, 0x71, 0x70, 0x5a, 0x49, 0x69, 0x43, 0x45, 0x44,
553                0x68, 0x6f, 0x78, 0x51, 0x76, 0x47, 0x58, 0x74, 0x30, 0x43,
554                0x6c, 0x62, 0x50, 0x30, 0x36, 0x66, 0x51, 0x4f, 0x56, 0x36,
555                0x6b
556            ],
557            aes128_cipher
558                .generate_key_from_string_and_decrypt(
559                    "",
560                    "1234".as_bytes(),
561                    4,
562                    &[
563                        0x6b, 0x17, 0x24, 0x0a, 0x56, 0xd6, 0xd1, 0xf5, 0x1b,
564                        0x73, 0x76, 0xfa, 0x54, 0xd8, 0xb0, 0x43, 0x14, 0xe3,
565                        0x1b, 0x1f, 0x05, 0xda, 0x5b, 0x74, 0xea, 0xc8, 0x4b,
566                        0x42, 0x9c, 0x8d, 0x41, 0xfa, 0x46, 0x9e, 0x65, 0x76,
567                        0xa1, 0x62, 0xe2, 0xde, 0xfb, 0x57, 0xb1, 0x01, 0xa1,
568                        0x2f, 0xde, 0xc9, 0x56, 0x76, 0x7a, 0xe2, 0x3c, 0x56,
569                        0x71, 0xd7, 0xf0, 0x91, 0x80
570                    ]
571                )
572                .unwrap()
573        );
574
575        assert_eq!(
576            vec![
577                0x42, 0x39, 0x70, 0x37, 0x77, 0x6d, 0x6f, 0x59, 0x55, 0x57,
578                0x5a, 0x76, 0x6a, 0x6e, 0x39, 0x44, 0x55, 0x61, 0x44, 0x4c,
579                0x51, 0x4c, 0x70, 0x5a, 0x78
580            ],
581            aes128_cipher
582                .generate_key_from_string_and_decrypt(
583                    "12345678",
584                    "123456781234".as_bytes(),
585                    5,
586                    &[
587                        0x8a, 0xe4, 0x4b, 0xca, 0xba, 0x4d, 0x5f, 0x9a, 0xb4,
588                        0xbc, 0x0f, 0x86, 0xc7, 0xa3, 0x05, 0x05, 0x28, 0x9b,
589                        0xae, 0x9b, 0x9e, 0x66, 0xb5, 0xb9, 0x4e, 0xa4, 0xaa,
590                        0x59, 0x6f, 0x55, 0x60, 0x41, 0x8d, 0x8a, 0x46, 0xad,
591                        0x39, 0x40, 0x58, 0x9e, 0xca, 0x62, 0xef, 0x26, 0x24,
592                        0x54, 0x95, 0xca, 0x0c, 0x01, 0xfd, 0x07, 0xf1
593                    ]
594                )
595                .unwrap()
596        );
597
598        assert_eq!(
599            vec![
600                0x70, 0x4d, 0x46, 0x5a, 0x56, 0x6e, 0x79, 0x36, 0x47, 0x42,
601                0x64, 0x35, 0x48, 0x35, 0x38, 0x73, 0x76, 0x37, 0x43, 0x37,
602                0x77, 0x51, 0x37, 0x42, 0x69, 0x30, 0x6a, 0x48, 0x70
603            ],
604            aes128_cipher
605                .generate_key_from_string_and_decrypt(
606                    "123456789",
607                    "1234567891234".as_bytes(),
608                    6,
609                    &[
610                        0x05, 0x0d, 0x9c, 0x4c, 0x30, 0x3d, 0x26, 0x39, 0xb6,
611                        0xff, 0x82, 0xe0, 0x37, 0x83, 0xee, 0x60, 0x2a, 0x10,
612                        0x3c, 0xb6, 0x77, 0x6b, 0x66, 0x80, 0x3c, 0x7f, 0xe5,
613                        0xe0, 0xe0, 0x65, 0x6e, 0x68, 0xec, 0x15, 0x49, 0x71,
614                        0xf5, 0xba, 0x45, 0x99, 0xf2, 0x52, 0x34, 0x18, 0x58,
615                        0x32, 0xff, 0x29, 0x2f, 0x0d, 0x26, 0x32, 0x6b, 0x2b,
616                        0x01, 0xd2, 0xe3
617                    ]
618                )
619                .unwrap()
620        );
621    }
622}