aboutsummaryrefslogtreecommitdiffstats
path: root/libtests
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2009-10-17 05:14:47 +0200
committerJay Berkenbilt <ejb@ql.org>2009-10-17 05:14:47 +0200
commit846c9f6bcc9aa86067850088808ff8d724a0d18f (patch)
tree587f6ff5589ebaefad50ca9f1b1b7fa81736a119 /libtests
parentad19b03fd346e6779a029c43b6228e377919852f (diff)
downloadqpdf-846c9f6bcc9aa86067850088808ff8d724a0d18f.tar.zst
checkpoint -- started doing some R4 encryption support
git-svn-id: svn+q:///qpdf/trunk@807 71b93d88-0707-0410-a8cf-f5a4172ac649
Diffstat (limited to 'libtests')
-rw-r--r--libtests/aes.cc97
-rw-r--r--libtests/build.mk1
2 files changed, 98 insertions, 0 deletions
diff --git a/libtests/aes.cc b/libtests/aes.cc
new file mode 100644
index 00000000..0c4b8e19
--- /dev/null
+++ b/libtests/aes.cc
@@ -0,0 +1,97 @@
+#include <qpdf/Pl_AES_PDF.hh>
+#include <qpdf/Pl_StdioFile.hh>
+
+#include <stdio.h>
+#include <string.h>
+#include <iostream>
+#include <stdlib.h>
+
+static void usage()
+{
+ std::cerr << "Usage: aes { -encrypt | -decrypt }"
+ << " hex-key infile outfile" << std::endl;
+ exit(2);
+}
+
+int main(int argc, char* argv[])
+{
+ if (argc != 5)
+ {
+ usage();
+ }
+
+ char* action = argv[1];
+ char* hexkey = argv[2];
+ char* infilename = argv[3];
+ char* outfilename = argv[4];
+
+ bool encrypt = true;
+ if (strcmp(action, "-decrypt") == 0)
+ {
+ encrypt = false;
+ }
+ else if (strcmp(action, "-encrypt") != 0)
+ {
+ usage();
+ }
+
+ unsigned int hexkeylen = 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)
+ {
+ std::cerr << "can't open " << infilename << std::endl;
+ exit(2);
+ }
+
+ FILE* outfile = fopen(outfilename, "wb");
+ if (outfile == 0)
+ {
+ std::cerr << "can't open " << outfilename << std::endl;
+ exit(2);
+ }
+
+ unsigned char key[Pl_AES_PDF::key_size];
+ for (unsigned int i = 0; i < strlen(hexkey); i += 2)
+ {
+ char t[3];
+ t[0] = hexkey[i];
+ t[1] = hexkey[i + 1];
+ t[2] = '\0';
+
+ long val = strtol(t, 0, 16);
+ key[i/2] = (unsigned char) val;
+ }
+
+ Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
+ Pl_AES_PDF* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key);
+
+ // 16 < buffer size, buffer_size is not a multiple of 8 for testing
+ unsigned char buf[83];
+ bool done = false;
+ while (! done)
+ {
+ int len = fread(buf, 1, sizeof(buf), infile);
+ if (len <= 0)
+ {
+ done = true;
+ }
+ else
+ {
+ aes->write(buf, len);
+ }
+ }
+ aes->finish();
+ delete aes;
+ delete out;
+ fclose(infile);
+ fclose(outfile);
+ return 0;
+}
diff --git a/libtests/build.mk b/libtests/build.mk
index 8b996fb8..1df2c16f 100644
--- a/libtests/build.mk
+++ b/libtests/build.mk
@@ -1,4 +1,5 @@
BINS_libtests = \
+ aes \
ascii85 \
bits \
buffer \