/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 | | encrypt(encrypt), |
19 | | cbc_mode(cbc_mode), |
20 | | cbc_block(cbc_block), |
21 | | nrounds(0) |
22 | 1.95M | { |
23 | 1.95M | size_t keybits = 8 * key_bytes; |
24 | 1.95M | this->key = std::make_unique<unsigned char[]>(key_bytes); |
25 | 1.95M | this->rk = std::make_unique<uint32_t[]>(RKLENGTH(keybits)); |
26 | 1.95M | size_t rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t); |
27 | 1.95M | std::memcpy(this->key.get(), key, key_bytes); |
28 | 1.95M | std::memset(this->rk.get(), 0, rk_bytes); |
29 | 1.95M | if (encrypt) { |
30 | 1.90M | this->nrounds = rijndaelSetupEncrypt(this->rk.get(), this->key.get(), keybits); |
31 | 1.90M | } else { |
32 | 47.9k | this->nrounds = rijndaelSetupDecrypt(this->rk.get(), this->key.get(), keybits); |
33 | 47.9k | } |
34 | 1.95M | } |
35 | | |
36 | | void |
37 | | AES_PDF_native::update(unsigned char* in_data, unsigned char* out_data) |
38 | 506M | { |
39 | 506M | if (this->encrypt) { |
40 | 499M | if (this->cbc_mode) { |
41 | 8.49G | for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) { |
42 | 7.99G | in_data[i] ^= this->cbc_block[i]; |
43 | 7.99G | } |
44 | 499M | } |
45 | 499M | rijndaelEncrypt(this->rk.get(), this->nrounds, in_data, out_data); |
46 | 499M | if (this->cbc_mode) { |
47 | 499M | memcpy(this->cbc_block, out_data, QPDFCryptoImpl::rijndael_buf_size); |
48 | 499M | } |
49 | 499M | } else { |
50 | 6.18M | rijndaelDecrypt(this->rk.get(), this->nrounds, in_data, out_data); |
51 | 6.18M | if (this->cbc_mode) { |
52 | 105M | for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) { |
53 | 98.9M | out_data[i] ^= this->cbc_block[i]; |
54 | 98.9M | } |
55 | 6.18M | memcpy(this->cbc_block, in_data, QPDFCryptoImpl::rijndael_buf_size); |
56 | 6.18M | } |
57 | 6.18M | } |
58 | 506M | } |