test_encdec.pm
1 # Licensed under the Apache License, Version 2.0 (the "License"); 2 # you may not use this file except in compliance with the License. 3 # See the NOTICE file distributed with this work for additional 4 # information regarding copyright ownership. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 use strict; 16 use warnings; 17 use Test2::V0; 18 use IPC::Cmd qw(can_run run run_forked); 19 20 bail_out "Can't use IPC::Cmd::run_forked" 21 unless IPC::Cmd->can_use_run_forked; 22 bail_out "Can't find lesec-tool" 23 unless can_run('lesec-tool'); 24 25 my $plugin = $ENV{PLUGIN} // "vigenere"; 26 27 sub test_one_cipher { 28 my %opts = @_; 29 30 plan(2); 31 32 my $tool; 33 my $capture; 34 my $ptfile = "$opts{algo}-from.dat"; 35 $ptfile =~ s|/|-|; # Avoid slashes in file names 36 37 open my $finput, '>', $ptfile; 38 print $finput pack('H*', $opts{from}); 39 close $finput; 40 41 my $pad = defined $opts{pad} ? (' -pad ' . ($opts{pad} ? "1" : "0")) : ''; 42 my $iv = defined $opts{iv} ? (' -iv ' . "0x$opts{iv}") : ''; 43 my $nonce = defined $opts{nonce} ? (' -nonce ' . "0x$opts{nonce}") : ''; 44 $tool = "lesec-tool $opts{cmd} -plugin $plugin $opts{algo} -key:key 0x$opts{key}$pad$iv$nonce"; 45 $capture = `$tool < $ptfile`; 46 47 is($?, 0, "check execution of $tool < '$opts{from}'"); 48 like(unpack('H*', $capture), qr|^\Q$opts{to}\E$|i, "check that output is '$opts{to}'"); 49 } 50 51 sub test_encdec { 52 my @data = @_; 53 54 plan(scalar @data); 55 56 my $count = 0; 57 foreach my $d (@data) { 58 subtest "$d->{title}" => sub { 59 my $casecount = 0; 60 plan(scalar @{$d->{vectors}}); 61 for my $v (@{$d->{vectors}}) { 62 my $casetitle = $v->{title} // "case $casecount"; 63 subtest "$casetitle" => sub { 64 my $testcount = 0; 65 my @tests = sort keys %{$v->{tests}}; 66 my $testplan 67 = (defined $v->{keystream} ? 3 : 2) * scalar @tests; 68 plan($testplan); 69 my %common_test_args = ( 70 algo => $v->{algo} // $d->{algo}, 71 key => $v->{key} // $d->{key}, 72 pad => $v->{pad} // $d->{pad}, 73 iv => $v->{iv} // $d->{iv}, 74 nonce => $v->{nonce} // $d->{nonce}, 75 ); 76 foreach my $t (@tests) { 77 if (defined $v->{keystream}) { 78 subtest "test $testcount : keystream" => \&test_one_cipher, 79 %common_test_args, 80 cmd => 'enc', 81 from => '0' x length($v->{keystream}), 82 to => $v->{keystream}; 83 } 84 subtest "test $testcount : encrypt" => \&test_one_cipher, 85 %common_test_args, 86 cmd => 'enc', 87 from => $t, 88 to => $v->{tests}->{$t}; 89 subtest "test $testcount : decrypt" => \&test_one_cipher, 90 %common_test_args, 91 cmd => 'dec', 92 from => $v->{tests}->{$t}, 93 to => $t; 94 $testcount++; 95 } 96 }; 97 $casecount++; 98 } 99 }; 100 $count++; 101 } 102 } 103 104 1;