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
239
240
241
use json::JsonValue;

use crate::{AcvpError, AcvpResult};

const HASHES: &[&str; 11] = &[
    "SHA-1",
    "SHA-224",
    "SHA-256",
    "SHA-384",
    "SHA-512",
    "SHA3-224",
    "SHA3-256",
    "SHA3-384",
    "SHA3-512",
    "SHAKE-128",
    "SHAKE-256",
];

const BLKCIPHERS: &[&str; 6] = &[
    "ACVP-AES-CBC",
    "ACVP-AES-CTR",
    "ACVP-AES-ECB",
    "ACVP-AES-GCM",
    "ACVP-TDES-ECB",
    "ACVP-TDES-CBC",
];

const MACS: &[&str; 13] = &[
    "HMAC-SHA-1",
    "HMAC-SHA2-224",
    "HMAC-SHA2-256",
    "HMAC-SHA2-384",
    "HMAC-SHA2-512",
    "HMAC-SHA3-224",
    "HMAC-SHA3-256",
    "HMAC-SHA3-384",
    "HMAC-SHA3-512",
    "CMAC-AES",
    "CMAC-TDES",
    "ACVP-AES-GCM",
    "ACVP-AES-GMAC",
];

const RNGS: &[&str; 3] = &["hashDRBG", "ctrDRBG", "hmacDRBG"];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AcvpAlgorithm {
    Hash,
    MsgAuth,
    BlockCipher,
    Rng,
    Nil,
}

impl AcvpAlgorithm {
    pub fn alg_type(alg: &str) -> AcvpResult<Self> {
        if str_lookup(alg, HASHES) {
            return Ok(Self::Hash);
        }
        if str_lookup(alg, MACS) {
            return Ok(Self::MsgAuth);
        }
        if str_lookup(alg, BLKCIPHERS) {
            return Ok(Self::BlockCipher);
        }
        if str_lookup(alg, RNGS) {
            return Ok(Self::Rng);
        }
        Err(AcvpError {
            code: -libc::EINVAL,
            message: format!("Uknown type for algorithm '{}'", alg),
        })
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum TestType {
    AFT,
    CTR,
    MCT,
    LDT,
    Nil,
}

impl TestType {
    pub fn from_string(ttype: &str) -> AcvpResult<Self> {
        let test_type = match ttype {
            "AFT" => TestType::AFT,
            "CTR" => TestType::CTR,
            "MCT" => TestType::MCT,
            "LDT" => TestType::LDT,
            _ => {
                return Err(AcvpError {
                    code: -libc::EINVAL,
                    message: format!("Invalid test type '{}'", ttype),
                });
            }
        };
        Ok(test_type)
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Direction {
    Encrypt,
    Decrypt,
    Generate,
    Verify,
    Nil,
}

impl Direction {
    pub fn from_string(direction: &str) -> AcvpResult<Self> {
        let dir = match direction {
            "encrypt" => Direction::Encrypt,
            "decrypt" => Direction::Decrypt,
            "gen" => Direction::Generate,
            "ver" => Direction::Verify,
            _ => {
                return Err(AcvpError {
                    code: -libc::EINVAL,
                    message: format!("Invalid direction '{}'", direction),
                });
            }
        };
        Ok(dir)
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum IVMode {
    Internal,
    External,
    Nil,
}

impl IVMode {
    pub fn from_string(mode: &str) -> AcvpResult<Self> {
        let ivmode = match mode {
            "internal" => IVMode::Internal,
            "external" => IVMode::External,
            _ => {
                return Err(AcvpError {
                    code: -libc::EINVAL,
                    message: format!("Invalid IVGenMode '{}'", mode),
                });
            }
        };
        Ok(ivmode)
    }
}

fn str_lookup(key: &str, arr: &[&str]) -> bool {
    if let Some(_str) = arr.iter().find(|&s| *s == key) {
        return true;
    }
    false
}

pub fn get_acvp_str(key: &str, json: &JsonValue) -> AcvpResult<String> {
    let value = match json[key].as_str() {
        Some(val) => val,
        None => {
            return Err(AcvpError {
                code: -libc::EINVAL,
                message: format!("Failed to obtain str value associated with key '{}'", key,),
            });
        }
    };
    Ok(value.to_string())
}

pub fn get_acvp_u32(key: &str, json: &JsonValue) -> AcvpResult<u32> {
    let value = match json[key].as_u32() {
        Some(val) => val,
        None => {
            return Err(AcvpError {
                code: -libc::EINVAL,
                message: format!("Failed to obtain u32 value associated with key '{}'", key,),
            });
        }
    };
    Ok(value)
}

pub fn get_acvp_bool(key: &str, json: &JsonValue) -> AcvpResult<bool> {
    let value = match json[key].as_bool() {
        Some(val) => val,
        None => {
            return Err(AcvpError {
                code: -libc::EINVAL,
                message: format!(
                    "Failed to obtain boolean value associated with key '{}'",
                    key,
                ),
            });
        }
    };
    Ok(value)
}

pub(crate) fn hex2bin(hex: &str) -> AcvpResult<Vec<u8>> {
    let bin = match hex::decode(hex) {
        Ok(bin) => bin,
        Err(_e) => {
            return Err(AcvpError {
                code: -libc::EINVAL,
                message: format!("Invalid hex string '{}'", hex),
            });
        }
    };
    Ok(bin)
}

pub fn get_algorithm_type(vector: &str) -> AcvpResult<AcvpAlgorithm> {
    let vec = match json::parse(vector) {
        Ok(vec) => vec,
        Err(_e) => {
            return Err(AcvpError {
                code: -libc::EINVAL,
                message: "Invalid input vector, failed to get algorithm type".to_string(),
            });
        }
    };

    let mut alg_type = AcvpAlgorithm::Nil;
    for v in vec.members() {
        if v.has_key("algorithm") {
            let alg = get_acvp_str("algorithm", v)?;
            alg_type = AcvpAlgorithm::alg_type(&alg)?;
        }
    }
    if alg_type == AcvpAlgorithm::Nil {
        return Err(AcvpError {
            code: -libc::EINVAL,
            message: "No 'algorithm' key present in input vector".to_string(),
        });
    }

    Ok(alg_type)
}