more wycheproof tests
Karel Miko
5 years ago
12 | 12 | use CryptX; |
13 | 13 | use Crypt::Misc 'read_rawfile'; |
14 | 14 | use Crypt::Digest 'digest_data'; |
15 | ||
16 | if (1) { | |
17 | use Crypt::Mac::OMAC; | |
18 | ||
19 | my $tests = CryptX::_decode_json read_rawfile 't/wycheproof/aes_cmac_test.json'; | |
20 | for my $g (@{$tests->{testGroups}}) { | |
21 | my $type = $g->{type}; | |
22 | my $tsize = $g->{tagSize} / 8; | |
23 | my $ksize = $g->{keySize} / 8; | |
24 | for my $t (@{$g->{tests}}) { | |
25 | my $tcId = $t->{tcId}; # 1 | |
26 | my $comment = $t->{comment}; # "" | |
27 | my $result = $t->{result}; # "valid" | |
28 | my $key = pack "H*", $t->{key}; # "ee8e1ed9ff2540ae8f2ba9f50bc2f27c" | |
29 | my $msg = pack "H*", $t->{msg}; # "48656c6c6f20776f726c64" | |
30 | my $tag = pack "H*", $t->{tag}; # "4fbcdeb7e4793f4a1d7e4faa70100af1" | |
31 | # do the test | |
32 | #my $tag2 = substr($m->add($msg)->mac, 0, $tsize); | |
33 | my $tag2 = eval { Crypt::Mac::OMAC->new("AES", $key)->add($msg)->mac }; | |
34 | my $testname = "type=$type tcId=$tcId comment='$comment' expected-result=$result"; | |
35 | if ($result eq 'valid' || $result eq 'acceptable') { | |
36 | is(unpack("H*", substr($tag2, 0, $tsize)), $t->{tag}, "$testname TAG-v"); | |
37 | } | |
38 | elsif ($result eq 'invalid') { | |
39 | if (defined $tag2) { | |
40 | isnt(unpack("H*", substr($tag2, 0, $tsize)), $t->{tag}, "$testname TAG-i"); | |
41 | } | |
42 | else { | |
43 | is($tag2, undef, "$testname PT-i"); | |
44 | } | |
45 | } | |
46 | else { | |
47 | ok(0, "UNEXPECTED result=$result"); | |
48 | } | |
49 | } | |
50 | } | |
51 | } | |
15 | 52 | |
16 | 53 | if (0) { |
17 | 54 | use Crypt::Mode::CBC; |