|
| 1 | +#![cfg(all(cmac, feature = "mac"))] |
| 2 | + |
| 3 | +use digest::{KeyInit, Mac}; |
| 4 | +use wolfssl_wolfcrypt::cmac_mac::CmacAes128; |
| 5 | + |
| 6 | +#[test] |
| 7 | +fn test_cmac_aes128_mac_trait() { |
| 8 | + let key = [ |
| 9 | + 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 10 | + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 11 | + ]; |
| 12 | + let message = [ |
| 13 | + 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 14 | + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 15 | + ]; |
| 16 | + let expected = [ |
| 17 | + 0x07u8, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, |
| 18 | + 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c |
| 19 | + ]; |
| 20 | + |
| 21 | + let mut mac = CmacAes128::new_from_slice(&key) |
| 22 | + .expect("CMAC init failed"); |
| 23 | + mac.update(&message); |
| 24 | + mac.verify_slice(&expected).expect("CMAC verification failed"); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn test_cmac_aes128_mac_finalize() { |
| 29 | + let key = [ |
| 30 | + 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 31 | + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 32 | + ]; |
| 33 | + let message = [ |
| 34 | + 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 35 | + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 36 | + ]; |
| 37 | + let expected: &[u8] = &[ |
| 38 | + 0x07u8, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, |
| 39 | + 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c |
| 40 | + ]; |
| 41 | + |
| 42 | + let mac = CmacAes128::new_from_slice(&key) |
| 43 | + .expect("CMAC init failed") |
| 44 | + .chain_update(&message); |
| 45 | + let result = mac.finalize(); |
| 46 | + assert_eq!(result.as_bytes().as_slice(), expected); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn test_cmac_aes128_mac_verify_fail() { |
| 51 | + let key = [ |
| 52 | + 0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 53 | + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 54 | + ]; |
| 55 | + let message = [ |
| 56 | + 0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, |
| 57 | + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, |
| 58 | + ]; |
| 59 | + let wrong_tag = [0u8; 16]; |
| 60 | + |
| 61 | + let mut mac = CmacAes128::new_from_slice(&key) |
| 62 | + .expect("CMAC init failed"); |
| 63 | + mac.update(&message); |
| 64 | + assert!(mac.verify_slice(&wrong_tag).is_err()); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn test_cmac_aes128_wrong_key_size() { |
| 69 | + let bad_key = [0u8; 15]; // wrong size for AES-128 |
| 70 | + assert!(CmacAes128::new_from_slice(&bad_key).is_err()); |
| 71 | +} |
0 commit comments