1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! This module provides implementation of MAC algorithms

//https://en.wikipedia.org/wiki/ISO/IEC_9797-1#Complete_specification_of_the_MAC_calculation

use crate::crypto::{tdes_encrypt_cbc, des_encrypt_cbc};

/// This enum defines various supported algorithms
pub enum MacAlgo {
    //ISO9797 - algo 1
    CbcMac,
    // ISO9797 - algo 3
    RetailMac,
}

/// This enum defines all supported padding types
pub enum PaddingType {
    /// Adding 0 bits
    Type1,
    /// Adding a single 1 bit followed by 0 bits
    Type2,
}

pub struct MacError {
    pub msg: String
}


pub fn verify_mac(algo: &MacAlgo, padding_type: &PaddingType, data: &[u8], key: &Vec<u8>, expected_mac: &Vec<u8>) -> Result<(), MacError> {
    let mac = generate_mac(algo, padding_type, &data.to_vec(), key)?;
    if mac.eq(expected_mac) {
        Ok(())
    } else {
        Err(MacError { msg: format!("computed mac: {} doesn't match expected_mac: {}", hex::encode(mac), hex::encode(expected_mac)) })
    }
}

pub fn generate_mac(algo: &MacAlgo, padding_type: &PaddingType, data: &Vec<u8>, key: &Vec<u8>) -> Result<Vec<u8>, MacError> {
    let new_data = apply_padding(padding_type, data);
    let mut iv = Vec::<u8>::new();
    iv.extend_from_slice(hex::decode("0000000000000000").unwrap().as_slice());

    println!("generating mac on {}", hex::encode(data));

    match algo {
        MacAlgo::CbcMac => {
            let res = tdes_encrypt_cbc(&new_data, key, &iv);
            Ok(res[res.len() - 8..].to_vec())
        }
        MacAlgo::RetailMac => {
            let k = key.as_slice()[0..8].to_vec();

            //if there is a single block
            if data.len() == 8 {
                Ok(tdes_encrypt_cbc(&data, key, &iv))
            } else {

                //else, all but the last block DES and the last block TDES
                let d1 = &new_data[0..new_data.len() - 8].to_vec();
                let d2 = &new_data[new_data.len() - 8..].to_vec();

                let res1 = des_encrypt_cbc(&d1, &k, &iv);
                Ok(tdes_encrypt_cbc(&d2, key, &res1[(res1.len() - 8)..].to_vec()))
            }
        }
    }
}

fn apply_padding(padding_type: &PaddingType, data: &Vec<u8>) -> Vec<u8> {
    let mut new_data = data.clone();
    match padding_type {
        PaddingType::Type1 => {}
        PaddingType::Type2 => {
            new_data.push(0x80);
        }
    };

    while new_data.len() < 8 {
        new_data.push(0x00);
    }

    while new_data.len() % 8 != 0 {
        new_data.push(0x00);
    }

    new_data
}


#[cfg(test)]
mod tests {
    use crate::crypto::mac::{apply_padding, PaddingType, generate_mac, MacAlgo};
    use hex_literal::hex;

    #[test]
    fn test_padding1_shortof8() {
        let data = hex::decode("0102030405").unwrap();
        assert_eq!(hex::encode(apply_padding(&PaddingType::Type1, &data)), "0102030405000000");
    }

    #[test]
    fn test_padding1_exact() {
        let data = hex::decode("0102030405060708").unwrap();
        assert_eq!(hex::encode(apply_padding(&PaddingType::Type1, &data)), "0102030405060708");
    }

    #[test]
    fn test_padding1_typical_short() {
        let data = hex::decode("0102030405060708090a").unwrap();
        assert_eq!(hex::encode(apply_padding(&PaddingType::Type1, &data)), "0102030405060708090a000000000000");
    }


    #[test]
    fn test_padding2_shortof8() {
        let data = hex::decode("0102030405").unwrap();
        assert_eq!(hex::encode(apply_padding(&PaddingType::Type2, &data)), "0102030405800000");
    }

    #[test]
    fn test_padding2_exact() {
        let data = hex::decode("0102030405060708").unwrap();
        assert_eq!(hex::encode(apply_padding(&PaddingType::Type2, &data)), "01020304050607088000000000000000");
    }

    #[test]
    fn test_padding2_typical_short() {
        let data = hex::decode("0102030405060708090a").unwrap();
        assert_eq!(hex::encode(apply_padding(&PaddingType::Type2, &data)), "0102030405060708090a800000000000");
    }


    #[test]
    fn test_gen_mac_cbc_nopads() {
        let res = generate_mac(&MacAlgo::CbcMac, &PaddingType::Type1,
                               &Vec::from(hex!("0102030405060708")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!("7d34c3071da931b9", hex::encode(m));
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }

    #[test]
    fn test_gen_mac_cbc_2() {
        let res = generate_mac(&MacAlgo::CbcMac, &PaddingType::Type1,
                               &Vec::from(hex!("01020304050607080102030405060708")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!("0fe28f4b5537ee79", hex::encode(m));
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }


    #[test]
    fn test_gen_mac_cbc_3() {
        let res = generate_mac(&MacAlgo::CbcMac, &PaddingType::Type1,
                               &Vec::from(hex!("01020304050607080102030405")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!("8fb12963d5661a22", hex::encode(m));
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }

    #[test]
    fn test_gen_mac_cbc_2_paddingtype2() {
        let res = generate_mac(&MacAlgo::CbcMac, &PaddingType::Type2,
                               &Vec::from(hex!("01020304050607080102030405")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!("8568cd2b7698605f", hex::encode(m));
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }


    #[test]
    fn test_gen_mac_retail1_nopads() {
        let res = generate_mac(&MacAlgo::RetailMac, &PaddingType::Type1,
                               &Vec::from(hex!("0102030405060708")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!("7d34c3071da931b9", hex::encode(m));
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }

    #[test]
    fn test_gen_mac_retail2_padtype1() {
        let res = generate_mac(&MacAlgo::RetailMac, &PaddingType::Type1,
                               &Vec::from(hex!("0102030405060708010203040506070801020304050607080000")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!(hex::encode(m), "149f99288681d292");
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }

    #[test]
    fn test_gen_mac_retail_padtype2() {
        let res = generate_mac(&MacAlgo::RetailMac, &PaddingType::Type2,
                               &Vec::from(hex!("0102030405060708010203040506070801020304050607080000")), &Vec::from(hex!("e0f4543f3e2a2c5ffc7e5e5a222e3e4d")));
        match res {
            Ok(m) => {
                println!("mac = {}", hex::encode(m.as_slice()));
                assert_eq!(hex::encode(m), "4689dd5a87015394");
            }
            Err(e) => {
                assert!(false, e.msg)
            }
        }
    }
}