1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#include <qpdf/Pl_AES_PDF.hh>
#include <qpdf/QUtil.hh>
#include <cstring>
#include <assert.h>
#include <stdexcept>
#include <qpdf/rijndael.h>
#include <string>
#include <stdlib.h>
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,
unsigned int key_bytes) :
Pipeline(identifier, next),
encrypt(encrypt),
cbc_mode(true),
first(true),
offset(0),
nrounds(0),
use_zero_iv(false),
use_specified_iv(false),
disable_padding(false)
{
unsigned int keybits = 8 * key_bytes;
assert(key_bytes == KEYLENGTH(keybits));
this->key = new unsigned char[key_bytes];
this->rk = new uint32_t[RKLENGTH(keybits)];
unsigned int rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t);
std::memcpy(this->key, key, key_bytes);
std::memset(this->rk, 0, rk_bytes);
std::memset(this->inbuf, 0, this->buf_size);
std::memset(this->outbuf, 0, this->buf_size);
std::memset(this->cbc_block, 0, this->buf_size);
if (encrypt)
{
this->nrounds = rijndaelSetupEncrypt(this->rk, this->key, keybits);
}
else
{
this->nrounds = rijndaelSetupDecrypt(this->rk, this->key, keybits);
}
assert(this->nrounds == NROUNDS(keybits));
}
Pl_AES_PDF::~Pl_AES_PDF()
{
delete [] this->key;
delete [] this->rk;
}
void
Pl_AES_PDF::useZeroIV()
{
this->use_zero_iv = true;
}
void
Pl_AES_PDF::disablePadding()
{
this->disable_padding = true;
}
void
Pl_AES_PDF::setIV(unsigned char const* iv, size_t bytes)
{
if (bytes != this->buf_size)
{
throw std::logic_error(
"Pl_AES_PDF: specified initialization vector"
" size in bytes must be " + QUtil::int_to_string(bytes));
}
this->use_specified_iv = true;
memcpy(this->specified_iv, iv, bytes);
}
void
Pl_AES_PDF::disableCBC()
{
this->cbc_mode = false;
}
void
Pl_AES_PDF::useStaticIV()
{
use_static_iv = true;
}
void
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)
{
flush(false);
}
size_t available = this->buf_size - this->offset;
size_t bytes = (bytes_left < available ? bytes_left : available);
bytes_left -= bytes;
std::memcpy(this->inbuf + this->offset, p, bytes);
this->offset += bytes;
p += bytes;
}
}
void
Pl_AES_PDF::finish()
{
if (this->encrypt)
{
if (this->offset == this->buf_size)
{
flush(false);
}
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 =
static_cast<unsigned char>(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)
{
// 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);
this->offset = this->buf_size;
}
flush(! this->disable_padding);
}
getNext()->finish();
}
void
Pl_AES_PDF::initializeVector()
{
if (use_zero_iv)
{
for (unsigned int i = 0; i < this->buf_size; ++i)
{
this->cbc_block[i] = 0;
}
}
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)
{
this->cbc_block[i] = 14 * (1 + i);
}
}
else
{
QUtil::initializeWithRandomBytes(this->cbc_block, this->buf_size);
}
}
void
Pl_AES_PDF::flush(bool strip_padding)
{
assert(this->offset == this->buf_size);
if (first)
{
first = false;
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))
{
getNext()->write(this->cbc_block, this->buf_size);
}
}
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
{
// 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;
}
}
}
if (this->encrypt)
{
if (this->cbc_mode)
{
for (unsigned int i = 0; i < this->buf_size; ++i)
{
this->inbuf[i] ^= this->cbc_block[i];
}
}
rijndaelEncrypt(this->rk, this->nrounds, this->inbuf, this->outbuf);
if (this->cbc_mode)
{
memcpy(this->cbc_block, this->outbuf, this->buf_size);
}
}
else
{
rijndaelDecrypt(this->rk, this->nrounds, this->inbuf, this->outbuf);
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);
}
}
unsigned int bytes = this->buf_size;
if (strip_padding)
{
unsigned char last = this->outbuf[this->buf_size - 1];
if (last <= this->buf_size)
{
bool strip = true;
for (unsigned int i = 1; i <= last; ++i)
{
if (this->outbuf[this->buf_size - i] != last)
{
strip = false;
break;
}
}
if (strip)
{
bytes -= last;
}
}
}
getNext()->write(this->outbuf, bytes);
this->offset = 0;
}
|