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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::str::FromStr;
use json::JsonValue;
use crate::{
blkcipher::BlkCipherMCTOutput,
drbg::DrbgMode,
msgauth::MsgAuthOutput,
util::{AcvpAlgorithm, Direction, IVMode, TestType},
AcvpError, AcvpResult,
};
pub trait TestGroup {
fn new(algorithm: &str, tgjson: &str) -> AcvpResult<Self>
where
Self: Sized;
fn dump(&self) -> String;
fn pretty(&self) -> String;
}
pub trait TestCase {
fn new(test: &str, tgdata: &TestGroupData) -> AcvpResult<Self>
where
Self: Sized;
fn get_result(&self) -> AcvpResult<JsonValue>;
fn dump_result(&self) -> AcvpResult<String>;
fn pretty_result(&self) -> AcvpResult<String>;
}
pub trait TestResult<T> {
fn set_result(&mut self, res: T) -> AcvpResult<()>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TestGroupData {
pub algorithm: String,
pub test_type: TestType,
pub taglen: usize,
pub ivmode: IVMode,
pub ivlen: usize,
pub direction: Direction,
pub drbgmode: DrbgMode,
pub prediction_resistance: bool,
pub reseed: bool,
pub der_func: bool,
pub returned_bits_len: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcvpTest<T> {
pub tcid: u32,
pub tgdata: TestGroupData,
pub test: T,
test_json: JsonValue,
}
impl<T: TestCase> TestCase for AcvpTest<T> {
fn new(test: &str, tgdata: &TestGroupData) -> AcvpResult<Self> {
let tc = match json::parse(test) {
Ok(tc) => tc,
Err(_e) => {
return Err(AcvpError {
code: -libc::EINVAL,
message: "Failed to parse testcase JSON".to_string(),
});
}
};
if !tc.has_key("tcId") {
return Err(AcvpError {
code: -libc::EINVAL,
message: "Required field tcID missing from testcase JSON".to_string(),
});
}
let tcid = crate::util::get_acvp_u32("tcId", &tc)?;
let test = T::new(test, tgdata)?;
Ok(AcvpTest {
tcid,
tgdata: tgdata.clone(),
test,
test_json: tc,
})
}
fn get_result(&self) -> AcvpResult<JsonValue> {
self.test.get_result()
}
fn dump_result(&self) -> AcvpResult<String> {
self.test.dump_result()
}
fn pretty_result(&self) -> AcvpResult<String> {
self.test.pretty_result()
}
}
impl<T: TestResult<bool>> TestResult<bool> for AcvpTest<T> {
fn set_result(&mut self, res: bool) -> AcvpResult<()> {
self.test.set_result(res)
}
}
impl<T: TestResult<Vec<u8>>> TestResult<Vec<u8>> for AcvpTest<T> {
fn set_result(&mut self, res: Vec<u8>) -> AcvpResult<()> {
self.test.set_result(res)
}
}
impl<T: TestResult<Vec<Vec<u8>>>> TestResult<Vec<Vec<u8>>> for AcvpTest<T> {
fn set_result(&mut self, res: Vec<Vec<u8>>) -> AcvpResult<()> {
self.test.set_result(res)
}
}
impl<T: TestResult<Vec<BlkCipherMCTOutput>>> TestResult<Vec<BlkCipherMCTOutput>> for AcvpTest<T> {
fn set_result(&mut self, res: Vec<BlkCipherMCTOutput>) -> AcvpResult<()> {
self.test.set_result(res)
}
}
impl<T: TestResult<MsgAuthOutput>> TestResult<MsgAuthOutput> for AcvpTest<T> {
fn set_result(&mut self, res: MsgAuthOutput) -> AcvpResult<()> {
self.test.set_result(res)
}
}
impl<T: Clone + TestCase> AcvpTest<T> {
pub fn get_test_data(&self) -> T {
self.test.clone()
}
pub fn dump(&self) -> String {
self.test_json.dump()
}
pub fn pretty(&self) -> String {
self.test_json.pretty(3)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcvpTestGroup<T> {
test_type: TestType,
tgid: u32,
pub tests: Vec<AcvpTest<T>>,
testgroup_json: JsonValue,
}
impl<T: TestCase> TestGroup for AcvpTestGroup<T> {
fn new(algorithm: &str, tgjson: &str) -> AcvpResult<Self> {
let tg = match json::parse(tgjson) {
Ok(tg) => tg,
Err(_e) => {
return Err(AcvpError {
code: -libc::EINVAL,
message: "Failed to parse testgroup JSON".to_string(),
});
}
};
if !tg.has_key("tgId") || !tg.has_key("testType") {
return Err(AcvpError {
code: -libc::EINVAL,
message: "Provided testgroup JSON does not have required fields".to_string(),
});
}
let tgid = crate::util::get_acvp_u32("tgId", &tg)?;
let test_type = TestType::from_string(&crate::util::get_acvp_str("testType", &tg)?)?;
let mut direction = Direction::Nil;
if tg.has_key("direction") {
direction = Direction::from_string(&crate::util::get_acvp_str("direction", &tg)?)?;
}
let mut taglen = 0;
if tg.has_key("tagLen") {
taglen = crate::util::get_acvp_u32("tagLen", &tg)? as usize;
} else if tg.has_key("macLen") {
taglen = crate::util::get_acvp_u32("macLen", &tg)? as usize;
}
let mut ivmode = IVMode::Nil;
if tg.has_key("ivGen") {
let ivmode_str = crate::util::get_acvp_str("ivGen", &tg)?;
ivmode = IVMode::from_string(&ivmode_str)?;
}
let mut ivlen = 0;
if tg.has_key("ivLen") {
ivlen = crate::util::get_acvp_u32("ivLen", &tg)? as usize;
}
let mut drbgmode = DrbgMode::Nil;
if tg.has_key("mode") {
let mode = crate::util::get_acvp_str("mode", &tg)?;
drbgmode = DrbgMode::from_str(&mode)?;
}
let mut prediction_resistance = false;
if tg.has_key("predResistance") {
prediction_resistance = crate::util::get_acvp_bool("predResistance", &tg)?;
}
let mut der_func = false;
if tg.has_key("derFunc") {
der_func = crate::util::get_acvp_bool("derFunc", &tg)?;
}
let mut reseed = false;
if tg.has_key("reSeed") {
reseed = crate::util::get_acvp_bool("reSeed", &tg)?;
}
let mut returned_bits_len: usize = 0;
if tg.has_key("returnedBitsLen") {
returned_bits_len = crate::util::get_acvp_u32("returnedBitsLen", &tg)? as usize;
}
let tgdata = TestGroupData {
algorithm: algorithm.to_string(),
test_type,
taglen,
ivmode,
ivlen,
direction,
drbgmode,
prediction_resistance,
der_func,
reseed,
returned_bits_len,
};
let tcs = &tg["tests"];
let mut tests = Vec::new();
for tc in tcs.members() {
let test = AcvpTest::<T>::new(&tc.dump(), &tgdata)?;
tests.push(test)
}
Ok(AcvpTestGroup {
test_type,
tgid,
tests,
testgroup_json: tg,
})
}
fn dump(&self) -> String {
self.testgroup_json.dump()
}
fn pretty(&self) -> String {
self.testgroup_json.pretty(3)
}
}
impl<T: TestCase> AcvpTestGroup<T> {
pub fn get_result(&self) -> AcvpResult<JsonValue> {
let mut results = JsonValue::new_array();
for test in &self.tests {
let res = test.get_result()?;
match results.push(res) {
Ok(()) => {}
Err(_e) => {
return Err(AcvpError {
code: -1,
message: "Unexpected error pushing to JsonValue array".to_string(),
});
}
}
}
Ok(json::object! {
tgId: self.tgid,
tests: results,
})
}
pub fn dump_result(&self) -> AcvpResult<String> {
let res = self.get_result()?;
Ok(res.dump())
}
pub fn pretty_result(&self) -> AcvpResult<String> {
let res = self.get_result()?;
Ok(res.pretty(3))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcvpRequest<T> {
pub version: String,
pub revision: String,
pub vsid: u32,
pub algorithm: String,
pub alg_type: AcvpAlgorithm,
pub is_sample: bool,
pub testgroups: Vec<AcvpTestGroup<T>>,
request_json: JsonValue,
}
impl<T: TestCase> AcvpRequest<T> {
pub fn new(vector: &str) -> AcvpResult<Self> {
let request = match json::parse(vector) {
Ok(req) => req,
Err(_e) => {
return Err(AcvpError {
code: -libc::EINVAL,
message: "Invalid ACVP Request JSON".to_string(),
});
}
};
if !request.is_array() {
return Err(AcvpError {
code: -libc::EINVAL,
message: "ACVP Request vector must be a JSON Array".to_string(),
});
}
let mut testgroups = Vec::new();
let mut algorithm = "".to_string();
let mut alg_type = AcvpAlgorithm::Nil;
let mut revision = "".to_string();
let mut vsid = 0;
let mut is_sample = false;
let mut version = "".to_string();
for req in request.members() {
if req.has_key("acvVersion") {
version = crate::util::get_acvp_str("acvVersion", req)?;
continue;
}
algorithm = crate::util::get_acvp_str("algorithm", req)?;
alg_type = AcvpAlgorithm::alg_type(&algorithm)?;
revision = crate::util::get_acvp_str("revision", req)?;
vsid = crate::util::get_acvp_u32("vsId", req)?;
is_sample = crate::util::get_acvp_bool("isSample", req)?;
let tgs = &req["testGroups"];
for tg in tgs.members() {
let testgroup = AcvpTestGroup::<T>::new(&algorithm, &tg.dump())?;
testgroups.push(testgroup);
}
}
Ok(AcvpRequest {
version,
revision,
vsid,
algorithm,
alg_type,
is_sample,
testgroups,
request_json: request,
})
}
pub fn get_result(&self) -> AcvpResult<JsonValue> {
let mut results = JsonValue::new_array();
for tg in &self.testgroups {
let res = tg.get_result()?;
match results.push(res) {
Ok(()) => {}
Err(_e) => {
return Err(AcvpError {
code: -1,
message: "Unexpected error pushing to JsonValue array".to_string(),
});
}
}
}
let vers = json::object! {
acvVersion: self.version.clone()
};
let resp = json::object! {
vsId: self.vsid,
algorithm: self.algorithm.clone(),
revision: self.revision.clone(),
isSample: self.is_sample,
testGroups: results,
};
Ok(json::array![vers, resp])
}
pub fn dump_result(&self) -> AcvpResult<String> {
let res = self.get_result()?;
Ok(res.dump())
}
pub fn pretty_result(&self) -> AcvpResult<String> {
let res = self.get_result()?;
Ok(res.pretty(3))
}
pub fn dump(&self) -> String {
self.request_json.dump()
}
pub fn pretty(&self) -> String {
self.request_json.pretty(3)
}
}