summaryrefslogtreecommitdiff
path: root/libtests
diff options
context:
space:
mode:
Diffstat (limited to 'libtests')
-rw-r--r--libtests/aes.cc116
-rw-r--r--libtests/build.mk3
-rw-r--r--libtests/qtest/sha2.test18
-rw-r--r--libtests/qtest/sha2/sha2.out9
-rw-r--r--libtests/sha2.cc69
5 files changed, 180 insertions, 35 deletions
diff --git a/libtests/aes.cc b/libtests/aes.cc
index ad2f0dd..381148c 100644
--- a/libtests/aes.cc
+++ b/libtests/aes.cc
@@ -8,52 +8,86 @@
static void usage()
{
- std::cerr << "Usage: aes [+-]cbc { -encrypt | -decrypt }"
- << " hex-key infile outfile" << std::endl;
+ std::cerr << "Usage: aes options hex-key infile outfile" << std::endl
+ << " -cbc -- disable CBC mode" << std::endl
+ << " +cbc -- enable CBC mode" << std::endl
+ << " -encrypt -- encrypt" << std::endl
+ << " -decrypt -- decrypt CBC mode" << std::endl
+ << " -zero-iv -- use zero initialization vector" << std::endl
+ << " -static-iv -- use static initialization vector" << std::endl
+ << " -no-padding -- disable padding" << std::endl
+ << "Options must precede key and file names." << std::endl;
exit(2);
}
int main(int argc, char* argv[])
{
- if (argc != 6)
- {
- usage();
- }
-
- char* cbc = argv[1];
- char* action = argv[2];
- char* hexkey = argv[3];
- char* infilename = argv[4];
- char* outfilename = argv[5];
-
+ bool encrypt = true;
bool cbc_mode = true;
- if (strcmp(cbc, "-cbc") == 0)
- {
- cbc_mode = false;
- }
- else if (strcmp(cbc, "+cbc") != 0)
- {
- usage();
- }
+ char* hexkey = 0;
+ char* infilename = 0;
+ char* outfilename = 0;
+ bool zero_iv = false;
+ bool static_iv = false;
+ bool disable_padding = false;
- bool encrypt = true;
- if (strcmp(action, "-decrypt") == 0)
+ for (int i = 1; i < argc; ++i)
{
- encrypt = false;
+ char* arg = argv[i];
+ if ((arg[0] == '-') || (arg[0] == '+'))
+ {
+ if (strcmp(arg, "-cbc") == 0)
+ {
+ cbc_mode = false;
+ }
+ else if (strcmp(arg, "+cbc") == 0)
+ {
+ cbc_mode = true;
+ }
+ else if (strcmp(arg, "-decrypt") == 0)
+ {
+ encrypt = false;
+ }
+ else if (strcmp(arg, "-encrypt") == 0)
+ {
+ encrypt = true;
+ }
+ else if (strcmp(arg, "-zero-iv") == 0)
+ {
+ zero_iv = true;
+ }
+ else if (strcmp(arg, "-static-iv") == 0)
+ {
+ static_iv = true;
+ }
+ else if (strcmp(arg, "-no-padding") == 0)
+ {
+ disable_padding = true;
+ }
+ else
+ {
+ usage();
+ }
+ }
+ else if (argc == i + 3)
+ {
+ hexkey = argv[i];
+ infilename = argv[i+1];
+ outfilename = argv[i+2];
+ break;
+ }
+ else
+ {
+ usage();
+ }
}
- else if (strcmp(action, "-encrypt") != 0)
+ if (outfilename == 0)
{
- usage();
+ usage();
}
unsigned int hexkeylen = (unsigned int)strlen(hexkey);
unsigned int keylen = hexkeylen / 2;
- if (keylen != Pl_AES_PDF::key_size)
- {
- std::cerr << "key length must be " << Pl_AES_PDF::key_size
- << " bytes" << std::endl;
- exit(2);
- }
FILE* infile = fopen(infilename, "rb");
if (infile == 0)
@@ -69,7 +103,7 @@ int main(int argc, char* argv[])
exit(2);
}
- unsigned char key[Pl_AES_PDF::key_size];
+ unsigned char* key = new unsigned char[keylen];
for (unsigned int i = 0; i < strlen(hexkey); i += 2)
{
char t[3];
@@ -82,11 +116,25 @@ 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);
+ Pl_AES_PDF* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key, keylen);
+ delete [] key;
+ key = 0;
if (! cbc_mode)
{
aes->disableCBC();
}
+ if (zero_iv)
+ {
+ aes->useZeroIV();
+ }
+ else if (static_iv)
+ {
+ aes->useStaticIV();
+ }
+ if (disable_padding)
+ {
+ aes->disablePadding();
+ }
// 16 < buffer size, buffer_size is not a multiple of 8 for testing
unsigned char buf[83];
diff --git a/libtests/build.mk b/libtests/build.mk
index 6464502..7a53595 100644
--- a/libtests/build.mk
+++ b/libtests/build.mk
@@ -12,7 +12,8 @@ BINS_libtests = \
png_filter \
pointer_holder \
qutil \
- rc4
+ rc4 \
+ sha2
TARGETS_libtests = $(foreach B,$(BINS_libtests),libtests/$(OUTPUT_DIR)/$(call binname,$(B)))
diff --git a/libtests/qtest/sha2.test b/libtests/qtest/sha2.test
new file mode 100644
index 0000000..34d668f
--- /dev/null
+++ b/libtests/qtest/sha2.test
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+require 5.008;
+BEGIN { $^W = 1; }
+use strict;
+
+chdir("sha2") or die "chdir testdir failed: $!\n";
+
+require TestDriver;
+
+my $td = new TestDriver('sha2');
+
+$td->runtest("sha2",
+ {$td->COMMAND => "sha2"},
+ {$td->FILE => "sha2.out",
+ $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
+
+$td->report(1);
diff --git a/libtests/qtest/sha2/sha2.out b/libtests/qtest/sha2/sha2.out
new file mode 100644
index 0000000..702264f
--- /dev/null
+++ b/libtests/qtest/sha2/sha2.out
@@ -0,0 +1,9 @@
+256 short: passed
+256 long: passed
+256 million: passed
+384 short: passed
+384 long: passed
+384 million: passed
+512 short: passed
+512 long: passed
+512 million: passed
diff --git a/libtests/sha2.cc b/libtests/sha2.cc
new file mode 100644
index 0000000..2b9aac3
--- /dev/null
+++ b/libtests/sha2.cc
@@ -0,0 +1,69 @@
+#include <qpdf/Pl_SHA2.hh>
+#include <iostream>
+#include <stdlib.h>
+#include <string.h>
+#include <qpdf/QUtil.hh>
+
+static void test(Pl_SHA2& sha2, char const* description, int bits,
+ char const* input, std::string const& output)
+{
+ sha2.resetBits(bits);
+ sha2.write((unsigned char*) input, strlen(input));
+ sha2.finish();
+ std::cout << description << ": ";
+ if (output == sha2.getHexDigest())
+ {
+ std::cout << "passed\n";
+ }
+ else
+ {
+ std::cout << "failed\n"
+ << " expected: " << output << "\n"
+ << " actual: " << sha2.getHexDigest() << "\n";
+ }
+}
+
+int main( int argc, char *argv[] )
+{
+ Pl_SHA2 sha2;
+ char million_a[1000001];
+ memset(million_a, 'a', 1000000);
+ million_a[1000000] = '\0';
+ test(sha2, "256 short", 256,
+ "abc",
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+ test(sha2, "256 long", 256,
+ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
+ test(sha2, "256 million", 256,
+ million_a,
+ "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
+ test(sha2, "384 short", 384,
+ "abc",
+ "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"
+ "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7");
+ test(sha2, "384 long", 384,
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
+ "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2"
+ "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039");
+ test(sha2, "384 million", 384,
+ million_a,
+ "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"
+ "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985");
+ test(sha2, "512 short", 512,
+ "abc",
+ "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
+ "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
+ test(sha2, "512 long", 512,
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
+ "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
+ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
+ "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");
+ test(sha2, "512 million", 512,
+ million_a,
+ "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"
+ "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");
+
+ return 0;
+}