/src/qpdf/libqpdf/AES_PDF_native.cc
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
23 | 0 | size_t keybits = 8 * key_bytes; |
24 | 0 | this->key = std::make_unique<unsigned char[]>(key_bytes); |
25 | 0 | this->rk = std::make_unique<uint32_t[]>(RKLENGTH(keybits)); |
26 | 0 | size_t rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t); |
27 | 0 | std::memcpy(this->key.get(), key, key_bytes); |
28 | 0 | std::memset(this->rk.get(), 0, rk_bytes); |
29 | 0 | if (encrypt) { |
30 | 0 | this->nrounds = rijndaelSetupEncrypt(this->rk.get(), this->key.get(), keybits); |
31 | 0 | } else { |
32 | 0 | this->nrounds = rijndaelSetupDecrypt(this->rk.get(), this->key.get(), keybits); |
33 | 0 | } |
34 | 0 | } |
35 | | |
36 | | void |
37 | | AES_PDF_native::update(unsigned char* in_data, unsigned char* out_data) |
38 | 0 | { |
39 | 0 | if (this->encrypt) { |
40 | 0 | if (this->cbc_mode) { |
41 | 0 | for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) { |
42 | 0 | in_data[i] ^= this->cbc_block[i]; |
43 | 0 | } |
44 | 0 | } |
45 | 0 | rijndaelEncrypt(this->rk.get(), this->nrounds, in_data, out_data); |
46 | 0 | if (this->cbc_mode) { |
47 | 0 | memcpy(this->cbc_block, out_data, QPDFCryptoImpl::rijndael_buf_size); |
48 | 0 | } |
49 | 0 | } else { |
50 | 0 | rijndaelDecrypt(this->rk.get(), this->nrounds, in_data, out_data); |
51 | 0 | if (this->cbc_mode) { |
52 | 0 | for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) { |
53 | 0 | out_data[i] ^= this->cbc_block[i]; |
54 | 0 | } |
55 | 0 | memcpy(this->cbc_block, in_data, QPDFCryptoImpl::rijndael_buf_size); |
56 | 0 | } |
57 | 0 | } |
58 | 0 | } |