acvp_parser/
hash.rs

1use json::JsonValue;
2
3use crate::{
4    parser::{TestCase, TestGroupData, TestResult},
5    util::TestType,
6    AcvpError, AcvpResult,
7};
8
9#[derive(Debug, Clone, Eq, PartialEq)]
10pub struct SecureHash {
11    algorithm: String,
12    tcid: u32,
13    test_type: TestType,
14    res_json: JsonValue,
15    pub msg: Vec<u8>,
16}
17
18impl TestCase for SecureHash {
19    fn new(testjson: &str, tgdata: &TestGroupData) -> AcvpResult<Self> {
20        let test = match json::parse(testjson) {
21            Ok(test) => test,
22            Err(_e) => {
23                return Err(AcvpError {
24                    code: -libc::EINVAL,
25                    message: "Failed to parse testcase JSON for SHash".to_string(),
26                });
27            }
28        };
29        let tcid = crate::util::get_acvp_u32("tcId", &test)?;
30
31        let msghex = crate::util::get_acvp_str("msg", &test)?;
32        let msg = crate::util::hex2bin(&msghex)?;
33
34        Ok(SecureHash {
35            algorithm: tgdata.algorithm.to_string(),
36            tcid,
37            test_type: tgdata.test_type,
38            res_json: JsonValue::new_object(),
39            msg,
40        })
41    }
42
43    fn get_result(&self) -> AcvpResult<JsonValue> {
44        if self.res_json.is_empty() {
45            return Err(AcvpError {
46                code: -libc::EINVAL,
47                message: "The result is not yet set, call set_*_result APIs".to_string(),
48            });
49        }
50        Ok(self.res_json.clone())
51    }
52
53    fn dump_result(&self) -> AcvpResult<String> {
54        if self.res_json.is_empty() {
55            return Err(AcvpError {
56                code: -libc::EINVAL,
57                message: "The result is not yet set, call set_*_result APIs".to_string(),
58            });
59        }
60        Ok(self.res_json.dump())
61    }
62
63    fn pretty_result(&self) -> AcvpResult<String> {
64        if self.res_json.is_empty() {
65            return Err(AcvpError {
66                code: -libc::EINVAL,
67                message: "The result is not yet set, call set_*_result APIs".to_string(),
68            });
69        }
70        Ok(self.res_json.pretty(3))
71    }
72}
73
74impl TestResult<Vec<u8>> for SecureHash {
75    fn set_result(&mut self, res: Vec<u8>) -> AcvpResult<()> {
76        self.set_aft_result(res);
77        Ok(())
78    }
79}
80
81impl TestResult<Vec<Vec<u8>>> for SecureHash {
82    fn set_result(&mut self, res: Vec<Vec<u8>>) -> AcvpResult<()> {
83        self.set_mct_result(res)
84    }
85}
86
87impl SecureHash {
88    fn set_aft_result(&mut self, md: Vec<u8>) {
89        let mdhex = hex::encode(md).to_ascii_uppercase();
90        self.res_json = json::object! {
91            tcId: self.tcid,
92            md: mdhex,
93        };
94    }
95
96    fn set_mct_result(&mut self, mdvec: Vec<Vec<u8>>) -> AcvpResult<()> {
97        let mut results = JsonValue::new_array();
98        for md in mdvec {
99            let mdhex = hex::encode(md).to_ascii_uppercase();
100            let res = json::object! { md: mdhex };
101            match results.push(res) {
102                Ok(()) => {}
103                Err(_e) => {
104                    return Err(AcvpError {
105                        code: -1,
106                        message: "Unexpected failure pushing to JsonValue array".to_string(),
107                    });
108                }
109            };
110        }
111        self.res_json = json::object! {
112            tcId: self.tcid,
113            resultsArray: results,
114        };
115        Ok(())
116    }
117}