summaryrefslogtreecommitdiff
path: root/fitz/filt_aesd.c
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2009-08-19 02:39:58 +0200
committerTor Andersson <tor@ghostscript.com>2009-08-19 02:39:58 +0200
commitac4f72884effe8c177f09d27bf4977c84eb3ec58 (patch)
tree5f0eafafab91d1dc07434f0a23b19399c6682818 /fitz/filt_aesd.c
parent1d2eb9d542b11e488dbd206c6ef9936b4f60fc13 (diff)
downloadmupdf-ac4f72884effe8c177f09d27bf4977c84eb3ec58.tar.xz
Implement support for AES encryption.
Diffstat (limited to 'fitz/filt_aesd.c')
-rw-r--r--fitz/filt_aesd.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/fitz/filt_aesd.c b/fitz/filt_aesd.c
new file mode 100644
index 00000000..63145bf1
--- /dev/null
+++ b/fitz/filt_aesd.c
@@ -0,0 +1,73 @@
+#include "fitz_base.h"
+#include "fitz_stream.h"
+
+typedef struct fz_aesd_s fz_aesd;
+
+struct fz_aesd_s
+{
+ fz_filter super;
+ fz_aes aes;
+ unsigned char iv[16];
+ int ivcount;
+};
+
+fz_error
+fz_newaesdfilter(fz_filter **fp, unsigned char *key, unsigned keylen)
+{
+ FZ_NEWFILTER(fz_aesd, f, aesdfilter);
+ aes_setkey_dec(&f->aes, key, keylen * 8);
+ f->ivcount = 0;
+ return fz_okay;
+}
+
+void
+fz_dropaesdfilter(fz_filter *f)
+{
+}
+
+fz_error
+fz_processaesdfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out)
+{
+ fz_aesd *f = (fz_aesd*)filter;
+ int n;
+
+ while (1)
+ {
+ if (in->rp + 16 > in->wp)
+ {
+ if (in->eof)
+ return fz_iodone;
+ return fz_ioneedin;
+ }
+
+ if (f->ivcount < 16)
+ {
+ f->iv[f->ivcount++] = *in->rp++;
+ }
+ else
+ {
+ if (out->wp + 16 > out->ep)
+ return fz_ioneedout;
+
+ n = MIN(in->wp - in->rp, out->ep - out->wp);
+ if (n % 16 != 0)
+ return fz_throw("size of data to decrypt not a multiple of 16");
+ if (n < 16)
+ return fz_throw("size of data to decrypt too small");
+
+ aes_crypt_cbc(&f->aes, AES_DECRYPT, n, f->iv, in->rp, out->wp);
+ in->rp += n;
+ out->wp += n;
+
+ /* Remove padding bytes */
+ if (in->eof && in->rp == in->wp)
+ {
+ int pad = out->wp[-1];
+ if (pad < 1 || pad > 16)
+ return fz_throw("aes padding out of range: %d", pad);
+ out->wp -= pad;
+ }
+ }
+ }
+}
+