aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Pl_AES_PDF.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libqpdf/Pl_AES_PDF.cc')
-rw-r--r--libqpdf/Pl_AES_PDF.cc120
1 files changed, 47 insertions, 73 deletions
diff --git a/libqpdf/Pl_AES_PDF.cc b/libqpdf/Pl_AES_PDF.cc
index 581ca9ab..7389cfca 100644
--- a/libqpdf/Pl_AES_PDF.cc
+++ b/libqpdf/Pl_AES_PDF.cc
@@ -1,19 +1,22 @@
#include <qpdf/Pl_AES_PDF.hh>
+#include <qpdf/QIntC.hh>
+#include <qpdf/QPDFCryptoProvider.hh>
#include <qpdf/QUtil.hh>
-#include <cstring>
#include <assert.h>
+#include <cstring>
#include <stdexcept>
-#include <qpdf/QIntC.hh>
-#include <qpdf/QPDFCryptoProvider.hh>
-#include <string>
#include <stdlib.h>
+#include <string>
bool Pl_AES_PDF::use_static_iv = false;
-Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next,
- bool encrypt, unsigned char const* key,
- size_t key_bytes) :
+Pl_AES_PDF::Pl_AES_PDF(
+ char const* identifier,
+ Pipeline* next,
+ bool encrypt,
+ unsigned char const* key,
+ size_t key_bytes) :
Pipeline(identifier, next),
crypto(QPDFCryptoProvider::getImpl()),
encrypt(encrypt),
@@ -51,11 +54,11 @@ Pl_AES_PDF::disablePadding()
void
Pl_AES_PDF::setIV(unsigned char const* iv, size_t bytes)
{
- if (bytes != this->buf_size)
- {
+ if (bytes != this->buf_size) {
throw std::logic_error(
"Pl_AES_PDF: specified initialization vector"
- " size in bytes must be " + QUtil::uint_to_string(bytes));
+ " size in bytes must be " +
+ QUtil::uint_to_string(bytes));
}
this->use_specified_iv = true;
memcpy(this->specified_iv, iv, bytes);
@@ -79,10 +82,8 @@ Pl_AES_PDF::write(unsigned char* data, size_t len)
size_t bytes_left = len;
unsigned char* p = data;
- while (bytes_left > 0)
- {
- if (this->offset == this->buf_size)
- {
+ while (bytes_left > 0) {
+ if (this->offset == this->buf_size) {
flush(false);
}
@@ -98,39 +99,32 @@ Pl_AES_PDF::write(unsigned char* data, size_t len)
void
Pl_AES_PDF::finish()
{
- if (this->encrypt)
- {
- if (this->offset == this->buf_size)
- {
+ if (this->encrypt) {
+ if (this->offset == this->buf_size) {
flush(false);
}
- if (! this->disable_padding)
- {
+ if (!this->disable_padding) {
// 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 =
- QIntC::to_uchar(this->buf_size - this->offset);
+ unsigned char pad = QIntC::to_uchar(this->buf_size - this->offset);
memset(this->inbuf + this->offset, pad, pad);
this->offset = this->buf_size;
flush(false);
}
- }
- else
- {
- if (this->offset != this->buf_size)
- {
+ } else {
+ if (this->offset != this->buf_size) {
// This is never supposed to happen as the output is
// always supposed to be padded. However, we have
// encountered files for which the output is not a
// multiple of the block size. In this case, pad with
// zeroes and hope for the best.
assert(this->buf_size > this->offset);
- std::memset(this->inbuf + this->offset, 0,
- this->buf_size - this->offset);
+ std::memset(
+ this->inbuf + this->offset, 0, this->buf_size - this->offset);
this->offset = this->buf_size;
}
- flush(! this->disable_padding);
+ flush(!this->disable_padding);
}
this->crypto->rijndael_finalize();
getNext()->finish();
@@ -139,26 +133,17 @@ Pl_AES_PDF::finish()
void
Pl_AES_PDF::initializeVector()
{
- if (use_zero_iv)
- {
- for (unsigned int i = 0; i < this->buf_size; ++i)
- {
+ if (use_zero_iv) {
+ for (unsigned int i = 0; i < this->buf_size; ++i) {
this->cbc_block[i] = 0;
}
- }
- else if (use_specified_iv)
- {
+ } else if (use_specified_iv) {
std::memcpy(this->cbc_block, this->specified_iv, this->buf_size);
- }
- else if (use_static_iv)
- {
- for (unsigned int i = 0; i < this->buf_size; ++i)
- {
+ } else if (use_static_iv) {
+ for (unsigned int i = 0; i < this->buf_size; ++i) {
this->cbc_block[i] = static_cast<unsigned char>(14U * (1U + i));
}
- }
- else
- {
+ } else {
QUtil::initializeWithRandomBytes(this->cbc_block, this->buf_size);
}
}
@@ -168,30 +153,22 @@ Pl_AES_PDF::flush(bool strip_padding)
{
assert(this->offset == this->buf_size);
- if (first)
- {
+ if (first) {
first = false;
bool return_after_init = false;
- if (this->cbc_mode)
- {
- if (encrypt)
- {
+ if (this->cbc_mode) {
+ if (encrypt) {
// Set cbc_block to the initialization vector, and if
// not zero, write it to the output stream.
initializeVector();
- if (! (this->use_zero_iv || this->use_specified_iv))
- {
+ if (!(this->use_zero_iv || this->use_specified_iv)) {
getNext()->write(this->cbc_block, this->buf_size);
}
- }
- else if (this->use_zero_iv || this->use_specified_iv)
- {
+ } else if (this->use_zero_iv || this->use_specified_iv) {
// Initialize vector with zeroes; zero vector was not
// written to the beginning of the input file.
initializeVector();
- }
- else
- {
+ } 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);
@@ -200,32 +177,29 @@ Pl_AES_PDF::flush(bool strip_padding)
}
}
this->crypto->rijndael_init(
- encrypt, this->key.get(), key_bytes,
- this->cbc_mode, this->cbc_block);
- if (return_after_init)
- {
+ encrypt,
+ this->key.get(),
+ key_bytes,
+ this->cbc_mode,
+ this->cbc_block);
+ if (return_after_init) {
return;
}
}
this->crypto->rijndael_process(this->inbuf, this->outbuf);
unsigned int bytes = this->buf_size;
- if (strip_padding)
- {
+ if (strip_padding) {
unsigned char last = this->outbuf[this->buf_size - 1];
- if (last <= this->buf_size)
- {
+ if (last <= this->buf_size) {
bool strip = true;
- for (unsigned int i = 1; i <= last; ++i)
- {
- if (this->outbuf[this->buf_size - i] != last)
- {
+ for (unsigned int i = 1; i <= last; ++i) {
+ if (this->outbuf[this->buf_size - i] != last) {
strip = false;
break;
}
}
- if (strip)
- {
+ if (strip) {
bytes -= last;
}
}