Commit 4eccb9d87b793ad2b6e1532ef4c89ab9d2bb3a90
1 parent
16a23368
Add random number functions to QUtil
Showing
3 changed files
with
52 additions
and
18 deletions
include/qpdf/QUtil.hh
| ... | ... | @@ -83,6 +83,18 @@ namespace QUtil |
| 83 | 83 | // encoding for the unicode value passed in. |
| 84 | 84 | QPDF_DLL |
| 85 | 85 | std::string toUTF8(unsigned long uval); |
| 86 | + | |
| 87 | + // Wrapper around random from stdlib. Calls srandom automatically | |
| 88 | + // the first time it is called. | |
| 89 | + QPDF_DLL | |
| 90 | + long random(); | |
| 91 | + | |
| 92 | + // Wrapper around srandom from stdlib. | |
| 93 | + QPDF_DLL | |
| 94 | + void srandom(unsigned int seed); | |
| 95 | + | |
| 96 | + QPDF_DLL | |
| 97 | + void initializeWithRandomBytes(unsigned char* data, size_t len); | |
| 86 | 98 | }; |
| 87 | 99 | |
| 88 | 100 | #endif // __QUTIL_HH__ | ... | ... |
libqpdf/Pl_AES_PDF.cc
| ... | ... | @@ -6,11 +6,6 @@ |
| 6 | 6 | #include <qpdf/rijndael.h> |
| 7 | 7 | #include <string> |
| 8 | 8 | #include <stdlib.h> |
| 9 | -#include <qpdf/qpdf-config.h> | |
| 10 | -#ifndef HAVE_RANDOM | |
| 11 | -# define random rand | |
| 12 | -# define srandom srand | |
| 13 | -#endif | |
| 14 | 9 | |
| 15 | 10 | bool Pl_AES_PDF::use_static_iv = false; |
| 16 | 11 | |
| ... | ... | @@ -155,15 +150,6 @@ Pl_AES_PDF::finish() |
| 155 | 150 | void |
| 156 | 151 | Pl_AES_PDF::initializeVector() |
| 157 | 152 | { |
| 158 | - static bool seeded_random = false; | |
| 159 | - if (! seeded_random) | |
| 160 | - { | |
| 161 | - // Seed the random number generator with something simple, but | |
| 162 | - // just to be interesting, don't use the unmodified current | |
| 163 | - // time.... | |
| 164 | - srandom((int)QUtil::get_current_time() ^ 0xcccc); | |
| 165 | - seeded_random = true; | |
| 166 | - } | |
| 167 | 153 | if (use_zero_iv) |
| 168 | 154 | { |
| 169 | 155 | for (unsigned int i = 0; i < this->buf_size; ++i) |
| ... | ... | @@ -184,10 +170,7 @@ Pl_AES_PDF::initializeVector() |
| 184 | 170 | } |
| 185 | 171 | else |
| 186 | 172 | { |
| 187 | - for (unsigned int i = 0; i < this->buf_size; ++i) | |
| 188 | - { | |
| 189 | - this->cbc_block[i] = (unsigned char)((random() & 0xff0) >> 4); | |
| 190 | - } | |
| 173 | + QUtil::initializeWithRandomBytes(this->cbc_block, this->buf_size); | |
| 191 | 174 | } |
| 192 | 175 | } |
| 193 | 176 | ... | ... |
libqpdf/QUtil.cc
| ... | ... | @@ -333,3 +333,42 @@ QUtil::toUTF8(unsigned long uval) |
| 333 | 333 | |
| 334 | 334 | return result; |
| 335 | 335 | } |
| 336 | + | |
| 337 | +long | |
| 338 | +QUtil::random() | |
| 339 | +{ | |
| 340 | + static bool seeded_random = false; | |
| 341 | + if (! seeded_random) | |
| 342 | + { | |
| 343 | + // Seed the random number generator with something simple, but | |
| 344 | + // just to be interesting, don't use the unmodified current | |
| 345 | + // time.... | |
| 346 | + QUtil::srandom((int)QUtil::get_current_time() ^ 0xcccc); | |
| 347 | + seeded_random = true; | |
| 348 | + } | |
| 349 | + | |
| 350 | +#ifdef HAVE_RANDOM | |
| 351 | + return ::random(); | |
| 352 | +#else | |
| 353 | + return rand(); | |
| 354 | +#endif | |
| 355 | +} | |
| 356 | + | |
| 357 | +void | |
| 358 | +QUtil::srandom(unsigned int seed) | |
| 359 | +{ | |
| 360 | +#ifdef HAVE_RANDOM | |
| 361 | + ::srandom(seed); | |
| 362 | +#else | |
| 363 | + srand(seed); | |
| 364 | +#endif | |
| 365 | +} | |
| 366 | + | |
| 367 | +void | |
| 368 | +QUtil::initializeWithRandomBytes(unsigned char* data, size_t len) | |
| 369 | +{ | |
| 370 | + for (size_t i = 0; i < len; ++i) | |
| 371 | + { | |
| 372 | + data[i] = (unsigned char)((QUtil::random() & 0xff0) >> 4); | |
| 373 | + } | |
| 374 | +} | ... | ... |