/src/qpdf/libqpdf/AES_PDF_native.cc
Line | Count | Source |
1 | | #include <qpdf/AES_PDF_native.hh> |
2 | | |
3 | | #include <qpdf/QIntC.hh> |
4 | | #include <qpdf/QPDFCryptoImpl.hh> |
5 | | #include <qpdf/QUtil.hh> |
6 | | #include <qpdf/rijndael.h> |
7 | | #include <cstring> |
8 | | #include <stdexcept> |
9 | | #include <stdlib.h> |
10 | | #include <string> |
11 | | |
12 | | AES_PDF_native::AES_PDF_native( |
13 | | bool encrypt, |
14 | | unsigned char const* key, |
15 | | size_t key_bytes, |
16 | | bool cbc_mode, |
17 | | unsigned char* cbc_block) : |
18 | 2.81M | encrypt(encrypt), |
19 | 2.81M | cbc_mode(cbc_mode), |
20 | 2.81M | cbc_block(cbc_block) |
21 | 2.81M | { |
22 | 2.81M | size_t keybits = 8 * key_bytes; |
23 | 2.81M | this->key = std::make_unique<unsigned char[]>(key_bytes); |
24 | 2.81M | this->rk = std::make_unique<uint32_t[]>(RKLENGTH(keybits)); |
25 | 2.81M | size_t rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t); |
26 | 2.81M | std::memcpy(this->key.get(), key, key_bytes); |
27 | 2.81M | std::memset(this->rk.get(), 0, rk_bytes); |
28 | 2.81M | if (encrypt) { |
29 | 2.77M | this->nrounds = rijndaelSetupEncrypt(this->rk.get(), this->key.get(), keybits); |
30 | 2.77M | } else { |
31 | 40.9k | this->nrounds = rijndaelSetupDecrypt(this->rk.get(), this->key.get(), keybits); |
32 | 40.9k | } |
33 | 2.81M | } |
34 | | |
35 | | void |
36 | | AES_PDF_native::update(unsigned char* in_data, unsigned char* out_data) |
37 | 799M | { |
38 | 799M | if (this->encrypt) { |
39 | 798M | if (this->cbc_mode) { |
40 | 13.5G | for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) { |
41 | 12.7G | in_data[i] ^= this->cbc_block[i]; |
42 | 12.7G | } |
43 | 798M | } |
44 | 798M | rijndaelEncrypt(this->rk.get(), this->nrounds, in_data, out_data); |
45 | 798M | if (this->cbc_mode) { |
46 | 798M | memcpy(this->cbc_block, out_data, QPDFCryptoImpl::rijndael_buf_size); |
47 | 798M | } |
48 | 798M | } else { |
49 | 1.38M | rijndaelDecrypt(this->rk.get(), this->nrounds, in_data, out_data); |
50 | 1.38M | if (this->cbc_mode) { |
51 | 23.5M | for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) { |
52 | 22.1M | out_data[i] ^= this->cbc_block[i]; |
53 | 22.1M | } |
54 | 1.38M | memcpy(this->cbc_block, in_data, QPDFCryptoImpl::rijndael_buf_size); |
55 | 1.38M | } |
56 | 1.38M | } |
57 | 799M | } |