From aaeb71093d4ba3eafee0efece3b8bad7c18f0f00 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 17 Oct 2009 23:58:59 +0000 Subject: test aes git-svn-id: svn+q:///qpdf/trunk@813 71b93d88-0707-0410-a8cf-f5a4172ac649 --- libtests/aes.cc | 27 ++++++++++--- libtests/qtest/aes.test | 71 ++++++++++++++++++++++++++++++++++ libtests/qtest/aes/data1 | Bin 0 -> 17041 bytes libtests/qtest/aes/data2 | Bin 0 -> 16000 bytes libtests/qtest/aes/test-vector.cipher | 1 + libtests/qtest/aes/test-vector.clear | Bin 0 -> 16 bytes 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 libtests/qtest/aes.test create mode 100644 libtests/qtest/aes/data1 create mode 100644 libtests/qtest/aes/data2 create mode 100755 libtests/qtest/aes/test-vector.cipher create mode 100644 libtests/qtest/aes/test-vector.clear (limited to 'libtests') diff --git a/libtests/aes.cc b/libtests/aes.cc index 0c4b8e19..418c3297 100644 --- a/libtests/aes.cc +++ b/libtests/aes.cc @@ -8,22 +8,33 @@ static void usage() { - std::cerr << "Usage: aes { -encrypt | -decrypt }" + std::cerr << "Usage: aes [+-]cbc { -encrypt | -decrypt }" << " hex-key infile outfile" << std::endl; exit(2); } int main(int argc, char* argv[]) { - if (argc != 5) + if (argc != 6) { usage(); } - char* action = argv[1]; - char* hexkey = argv[2]; - char* infilename = argv[3]; - char* outfilename = argv[4]; + char* cbc = argv[1]; + char* action = argv[2]; + char* hexkey = argv[3]; + char* infilename = argv[4]; + char* outfilename = argv[5]; + + bool cbc_mode = true; + if (strcmp(cbc, "-cbc") == 0) + { + cbc_mode = false; + } + else if (strcmp(cbc, "+cbc") != 0) + { + usage(); + } bool encrypt = true; if (strcmp(action, "-decrypt") == 0) @@ -72,6 +83,10 @@ int main(int argc, char* argv[]) Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); Pl_AES_PDF* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key); + if (! cbc_mode) + { + aes->disableCBC(); + } // 16 < buffer size, buffer_size is not a multiple of 8 for testing unsigned char buf[83]; diff --git a/libtests/qtest/aes.test b/libtests/qtest/aes.test new file mode 100644 index 00000000..7e48840a --- /dev/null +++ b/libtests/qtest/aes.test @@ -0,0 +1,71 @@ +#!/usr/bin/env perl +require 5.008; +BEGIN { $^W = 1; } +use strict; +use File::stat; + +chdir("aes") or die "chdir testdir failed: $!\n"; + +require TestDriver; + +my $td = new TestDriver('AES'); + +cleanup(); + +my $key = '000102030405060708090a0b0c0d0e0f'; +$td->runtest("encrypt test vector", + {$td->COMMAND => "aes -cbc -encrypt $key test-vector.clear tmp1"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); +$td->runtest("check output", + {$td->FILE => "tmp1"}, + {$td->FILE => "test-vector.cipher"}); +$td->runtest("decrypt test vector", + {$td->COMMAND => "aes -cbc -decrypt $key tmp1 tmp2"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); +$td->runtest("check output", + {$td->FILE => "tmp2"}, + {$td->FILE => "test-vector.clear"}); + +$key = '243f6a8885243f6a8885243f6a888524'; +foreach my $d (['data1', 17072], ['data2', 16032]) +{ + my ($file, $size) = @$d; + $td->runtest("encrypt $file", + {$td->COMMAND => "aes +cbc -encrypt $key $file tmp1"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); + # sleep one second so random number will get a different seed + sleep(1); + $td->runtest("encrypt $file again", + {$td->COMMAND => "aes +cbc -encrypt $key $file tmp2"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); + foreach my $f (qw(tmp1 tmp2)) + { + $td->runtest("check size", + {$td->STRING => sprintf("%d\n", stat($f)->size)}, + {$td->STRING => "$size\n"}); + } + $td->runtest("verify files are different", + {$td->COMMAND => "cmp tmp1 tmp2"}, + {$td->REGEXP => '.*', $td->EXIT_STATUS => '!0'}); + $td->runtest("decrypt $file", + {$td->COMMAND => "aes +cbc -decrypt $key tmp1 tmp3"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); + $td->runtest("decrypt $file again", + {$td->COMMAND => "aes +cbc -decrypt $key tmp2 tmp4"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); + $td->runtest("check output", + {$td->FILE => "tmp3"}, + {$td->FILE => $file}); + $td->runtest("check output", + {$td->FILE => "tmp4"}, + {$td->FILE => $file}); +} + +cleanup(); + +$td->report(22); + +sub cleanup +{ + system("rm -f tmp?"); +} diff --git a/libtests/qtest/aes/data1 b/libtests/qtest/aes/data1 new file mode 100644 index 00000000..577a57d6 Binary files /dev/null and b/libtests/qtest/aes/data1 differ diff --git a/libtests/qtest/aes/data2 b/libtests/qtest/aes/data2 new file mode 100644 index 00000000..ab02cc99 Binary files /dev/null and b/libtests/qtest/aes/data2 differ diff --git a/libtests/qtest/aes/test-vector.cipher b/libtests/qtest/aes/test-vector.cipher new file mode 100755 index 00000000..77cab1f1 --- /dev/null +++ b/libtests/qtest/aes/test-vector.cipher @@ -0,0 +1 @@ +iÄàØj{0ØÍ·€p´ÅZ•Odòäènžî‚ÒhH™ \ No newline at end of file diff --git a/libtests/qtest/aes/test-vector.clear b/libtests/qtest/aes/test-vector.clear new file mode 100644 index 00000000..2fdaa8e3 Binary files /dev/null and b/libtests/qtest/aes/test-vector.clear differ -- cgit v1.2.3-54-g00ecf