-
Notifications
You must be signed in to change notification settings - Fork 2
/
mactest.pike
77 lines (61 loc) · 2.26 KB
/
mactest.pike
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
/*
* Copyright (C) 2022 Opera Norway AS. All rights reserved.
* This file is an original work developed by Joshua Rogers.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
http:www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* mactest.pike: Functions concerning the MacTest tests.
*/
/*
* This function compares the hashes generated by the respective
* functions, versus the standards provided in Wycheproof.
* The function returns whether the test passed (true) or not.
*/
bool mactest_test(mapping test, string algorithm) {
mixed state = lookup_init(algorithm);
begin_ever(test["tcId"], test["comment"]);
string (8bit) hash = state(test["key"])(test["msg"]);
hash = hash[..test["tagSize"]/8-1];
if(hash != test["tag"]) {
if(test["result"] == "invalid") {
DBG("GENERAL PASS");
return true;
}
log_err(DBG_ERROR, false, "Generated hash is not the same as the provided value in tcId %d: %s/%s.", test["tcId"], String.string2hex(hash), String.string2hex(test["tag"]));
return false;
}
if(test["result"] == "invalid") {
log_err(DBG_ERROR, false, "Generated hash is the same when it shouldn't be in tcId %d.", test["tcId"]);
return false;
}
DBG("GENERAL PASS");
return true;
}
/*
* This function loops through each of the tests, and runs the cases through
* each of the function(s) corresponding to the type of test.
* This function deals with MacTest tests, and returns the number of failed tests.
*/
int mactest_tests(mapping testGroup, string algorithm) {
int numTests = sizeof(testGroup["tests"]);
int fail_count = 0;
for(int j=0; j<numTests; j++) {
mapping test = testGroup["tests"][j];
convert_test_to_string(test);
test["tagSize"] = testGroup["tagSize"];
handle_special_actions(test, algorithm);
if(!mactest_test(test, algorithm)) {
fail_count++;
continue;
}
}
return fail_count;
}