2009-10-17 03:14:47 +00:00
|
|
|
#include <qpdf/Pl_AES_PDF.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <qpdf/MD5.hh>
|
2009-10-17 03:14:47 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdexcept>
|
2009-10-17 15:01:20 +00:00
|
|
|
#include <qpdf/rijndael.h>
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <string>
|
|
|
|
#include <stdlib.h>
|
2009-10-17 03:14:47 +00:00
|
|
|
|
|
|
|
Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next,
|
2009-10-17 23:37:55 +00:00
|
|
|
bool encrypt, unsigned char const key[key_size]) :
|
2009-10-17 03:14:47 +00:00
|
|
|
Pipeline(identifier, next),
|
|
|
|
encrypt(encrypt),
|
2009-10-17 18:54:51 +00:00
|
|
|
cbc_mode(true),
|
|
|
|
first(true),
|
2009-10-17 15:01:20 +00:00
|
|
|
offset(0),
|
|
|
|
nrounds(0)
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
2009-10-17 15:01:20 +00:00
|
|
|
static int const keybits = 128;
|
|
|
|
assert(key_size == KEYLENGTH(keybits));
|
|
|
|
assert(sizeof(this->rk) / sizeof(uint32_t) == RKLENGTH(keybits));
|
|
|
|
std::memcpy(this->key, key, key_size);
|
|
|
|
std::memset(this->rk, 0, sizeof(this->rk));
|
|
|
|
std::memset(this->inbuf, 0, this->buf_size);
|
|
|
|
std::memset(this->outbuf, 0, this->buf_size);
|
2009-10-17 18:54:51 +00:00
|
|
|
std::memset(this->cbc_block, 0, this->buf_size);
|
2009-10-17 15:01:20 +00:00
|
|
|
if (encrypt)
|
|
|
|
{
|
|
|
|
this->nrounds = rijndaelSetupEncrypt(this->rk, this->key, keybits);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->nrounds = rijndaelSetupDecrypt(this->rk, this->key, keybits);
|
|
|
|
}
|
|
|
|
assert(this->nrounds == NROUNDS(keybits));
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pl_AES_PDF::~Pl_AES_PDF()
|
|
|
|
{
|
2009-10-17 15:01:20 +00:00
|
|
|
// nothing needed
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::disableCBC()
|
|
|
|
{
|
|
|
|
this->cbc_mode = false;
|
|
|
|
}
|
|
|
|
|
2009-10-17 03:14:47 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::write(unsigned char* data, int len)
|
|
|
|
{
|
|
|
|
unsigned int bytes_left = len;
|
|
|
|
unsigned char* p = data;
|
|
|
|
|
|
|
|
while (bytes_left > 0)
|
|
|
|
{
|
|
|
|
if (this->offset == this->buf_size)
|
|
|
|
{
|
|
|
|
flush(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int available = this->buf_size - this->offset;
|
|
|
|
int bytes = (bytes_left < available ? bytes_left : available);
|
|
|
|
bytes_left -= bytes;
|
2009-10-17 15:01:20 +00:00
|
|
|
std::memcpy(this->inbuf + this->offset, p, bytes);
|
2009-10-17 03:14:47 +00:00
|
|
|
this->offset += bytes;
|
|
|
|
p += bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_AES_PDF::finish()
|
|
|
|
{
|
|
|
|
if (this->encrypt)
|
|
|
|
{
|
|
|
|
if (this->offset == this->buf_size)
|
|
|
|
{
|
|
|
|
flush(false);
|
|
|
|
}
|
|
|
|
// Pad as described in section 3.5.1 of version 1.7 of the PDF
|
|
|
|
// specification, including providing an entire block of padding
|
|
|
|
// if the input was a multiple of 16 bytes.
|
|
|
|
unsigned char pad = this->buf_size - this->offset;
|
2009-10-17 15:01:20 +00:00
|
|
|
memset(this->inbuf + this->offset, pad, pad);
|
2009-10-17 03:14:47 +00:00
|
|
|
this->offset = this->buf_size;
|
|
|
|
flush(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this->offset != this->buf_size)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(
|
|
|
|
"aes encrypted stream length was not a multiple of " +
|
|
|
|
QUtil::int_to_string(this->buf_size) + " bytes (offset = " +
|
|
|
|
QUtil::int_to_string(this->offset) + ")");
|
|
|
|
}
|
|
|
|
flush(true);
|
|
|
|
}
|
|
|
|
getNext()->finish();
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::initializeVector()
|
|
|
|
{
|
|
|
|
std::string seed_str;
|
|
|
|
seed_str += QUtil::int_to_string((int)QUtil::get_current_time());
|
|
|
|
seed_str += " QPDF aes random";
|
|
|
|
MD5 m;
|
|
|
|
m.encodeString(seed_str.c_str());
|
|
|
|
MD5::Digest digest;
|
|
|
|
m.digest(digest);
|
|
|
|
assert(sizeof(digest) >= sizeof(unsigned int));
|
|
|
|
unsigned int seed;
|
|
|
|
memcpy((void*)(&seed), digest, sizeof(unsigned int));
|
|
|
|
srandom(seed);
|
|
|
|
for (unsigned int i = 0; i < this->buf_size; ++i)
|
|
|
|
{
|
|
|
|
this->cbc_block[i] = (unsigned char)(random() & 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 03:14:47 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::flush(bool strip_padding)
|
|
|
|
{
|
|
|
|
assert(this->offset == this->buf_size);
|
2009-10-17 18:54:51 +00:00
|
|
|
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
if (this->cbc_mode)
|
|
|
|
{
|
|
|
|
if (encrypt)
|
|
|
|
{
|
|
|
|
// Set cbc_block to a random initialization vector and
|
|
|
|
// write it to the output stream
|
|
|
|
initializeVector();
|
|
|
|
getNext()->write(this->cbc_block, this->buf_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Take the first block of input as the initialization
|
|
|
|
// vector. There's nothing to write at this time.
|
|
|
|
memcpy(this->cbc_block, this->inbuf, this->buf_size);
|
|
|
|
this->offset = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 03:14:47 +00:00
|
|
|
if (this->encrypt)
|
|
|
|
{
|
2009-10-17 18:54:51 +00:00
|
|
|
if (this->cbc_mode)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->buf_size; ++i)
|
|
|
|
{
|
|
|
|
this->inbuf[i] ^= this->cbc_block[i];
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 15:01:20 +00:00
|
|
|
rijndaelEncrypt(this->rk, this->nrounds, this->inbuf, this->outbuf);
|
2009-10-17 18:54:51 +00:00
|
|
|
if (this->cbc_mode)
|
|
|
|
{
|
|
|
|
memcpy(this->cbc_block, this->outbuf, this->buf_size);
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-17 15:01:20 +00:00
|
|
|
rijndaelDecrypt(this->rk, this->nrounds, this->inbuf, this->outbuf);
|
2009-10-17 18:54:51 +00:00
|
|
|
if (this->cbc_mode)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->buf_size; ++i)
|
|
|
|
{
|
|
|
|
this->outbuf[i] ^= this->cbc_block[i];
|
|
|
|
}
|
|
|
|
memcpy(this->cbc_block, this->inbuf, this->buf_size);
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
unsigned int bytes = this->buf_size;
|
|
|
|
if (strip_padding)
|
|
|
|
{
|
2009-10-17 15:01:20 +00:00
|
|
|
unsigned char last = this->outbuf[this->buf_size - 1];
|
2009-10-17 03:14:47 +00:00
|
|
|
if (last <= this->buf_size)
|
|
|
|
{
|
|
|
|
bool strip = true;
|
|
|
|
for (unsigned int i = 1; i <= last; ++i)
|
|
|
|
{
|
2009-10-17 15:01:20 +00:00
|
|
|
if (this->outbuf[this->buf_size - i] != last)
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
|
|
|
strip = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strip)
|
|
|
|
{
|
|
|
|
bytes -= last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 15:01:20 +00:00
|
|
|
getNext()->write(this->outbuf, bytes);
|
2009-10-17 03:14:47 +00:00
|
|
|
this->offset = 0;
|
|
|
|
}
|