272 template<
typename Derived,
typename Cipher>
274 bool decipherParallelizable,
275 unsigned int nbThread):
276 _cipherParallelizable(cipherParallelizable),
277 _decipherParallelizable(decipherParallelizable),
283 if(nbThread == 0 && (cipherParallelizable || decipherParallelizable)) {
284 _nbThread = std::thread::hardware_concurrency();
294 template<
typename Derived,
typename Cipher>
302 template<
typename Derived,
typename Cipher>
305 std::ostream& output,
306 const std::string& key) {
307 if(input && output) {
309 if(!_cipherParallelizable || _nbThread == 1) {
310 cipherSequentially(input, output, key);
313 cipherInParallel(input, output, key);
316 throw std::ios_base::failure(
"Input and/or output streams are closed");
320 template<
typename Derived,
typename Cipher>
323 std::ostream& output,
324 const std::string& key) {
325 if(input && output) {
326 std::array<uint8_t,Cipher::getBlockSize()> prevData =
reset();
327 if(!_decipherParallelizable || _nbThread == 1) {
328 decipherSequentially(input, output, key, prevData);
331 decipherInParallel(input, output, key, prevData);
334 throw std::ios_base::failure(
"Input and/or output streams are closed");
338 template<
typename Derived,
typename Cipher>
340 BlockCipherModeOfOperation<Derived,Cipher>::cipherSequentially(std::istream& input,
341 std::ostream& output,
342 const std::string& key) {
343 Cipher cipher(
reinterpret_cast<const uint8_t*
>(key.data()));
344 std::array<uint8_t, Cipher::getBlockSize()> data;
345 std::array<uint8_t, Cipher::getBlockSize()> out;
347 while(!input.eof()) {
348 input.read(
reinterpret_cast<char*
>(data.data()),
static_cast<std::streamsize
>(Cipher::getBlockSize()));
349 std::streamsize nbRead = input.gcount();
353 std::size_t nbBlocks = cipherBlock(data, nbRead, out, index, cipher);
354 for(std::size_t i = 0 ; i < nbBlocks ; ++i) {
362 template<
typename Derived,
typename Cipher>
364 BlockCipherModeOfOperation<Derived,Cipher>::cipherInParallel(std::istream& input,
365 std::ostream& output,
366 const std::string& key) {
367 std::array<uint8_t,Cipher::getBlockSize()>* data =
new std::array<uint8_t,Cipher::getBlockSize()>[_nbThread];
368 std::array<uint8_t,Cipher::getBlockSize()>* result =
new std::array<uint8_t,Cipher::getBlockSize()>[_nbThread];
369 std::size_t* nbWrite =
new std::size_t[_nbThread];
371 std::deque<std::thread> threads;
372 std::vector<Cipher> ciph;
373 ciph.push_back(Cipher(
reinterpret_cast<const uint8_t*
>(key.data())));
374 for(std::size_t i = 1 ; i < _nbThread ; ++i) {
375 ciph.push_back(Cipher(ciph[0]));
377 while(!input.eof()) {
378 for(std::size_t i = 0 ; i < _nbThread ; ++i) {
379 input.read(
reinterpret_cast<char*
>(data[i].data()),
static_cast<std::streamsize
>(Cipher::getBlockSize()));
380 std::streamsize nbRead = input.gcount();
384 threads.push_back(std::thread(&Derived::deferredCipherBlock,
391 std::ref(nbWrite[i])));
392 if(
static_cast<std::size_t
>(nbRead) != Cipher::getBlockSize()) {
396 for(uint32_t i = 0 ; !threads.empty() ; ++i) {
397 std::thread& th = threads.front();
399 for(uint32_t j = 0 ; j < nbWrite[i] ; ++j) {
400 output << result[i][j];
412 template<
typename Derived,
typename Cipher>
414 BlockCipherModeOfOperation<Derived,Cipher>::decipherSequentially(std::istream& input,
415 std::ostream& output,
416 const std::string& key,
417 std::array<uint8_t,Cipher::getBlockSize()>& prevData) {
418 Cipher cipher(
reinterpret_cast<const uint8_t*
>(key.data()));
419 std::array<uint8_t,Cipher::getBlockSize()> out;
420 std::array<uint8_t,Cipher::getBlockSize()> data;
421 std::array<uint8_t,Cipher::getBlockSize()> cipherData;
422 input.read(
reinterpret_cast<char*
>(data.data()),
static_cast<std::streamsize
>(Cipher::getBlockSize()));
423 std::streamsize nbRead = input.gcount();
427 std::streamsize read = nbRead;
428 input.read(
reinterpret_cast<char*
>(data.data()),
static_cast<std::streamsize
>(Cipher::getBlockSize()));
430 std::size_t end = decipherBlock(cipherData, prevData, read, input.eof(), out, index, cipher);
431 for(std::size_t i = 0 ; i < end ; ++i) {
435 prevData = cipherData;
436 nbRead = input.gcount();
439 }
while(nbRead != 0);
443 template<
typename Derived,
typename Cipher>
445 BlockCipherModeOfOperation<Derived,Cipher>::decipherInParallel(std::istream& input,
446 std::ostream& output,
447 const std::string& key,
448 std::array<uint8_t,Cipher::getBlockSize()>& prevData) {
449 std::array<uint8_t,Cipher::getBlockSize()>* data =
new std::array<uint8_t,Cipher::getBlockSize()>[_nbThread + 1];
450 std::array<uint8_t,Cipher::getBlockSize()>* result =
new std::array<uint8_t,Cipher::getBlockSize()>[_nbThread];
451 std::size_t* nbWrite =
new std::size_t[_nbThread];
453 std::deque<std::thread> threads;
454 std::vector<Cipher> ciph;
455 ciph.push_back(Cipher(
reinterpret_cast<const uint8_t*
>(key.data())));
456 for(std::size_t i = 1 ; i < _nbThread ; ++i) {
457 ciph.push_back(Cipher(ciph[0]));
459 bool lastBlock =
false;
460 input.read(
reinterpret_cast<char*
>(data[0].data()),
static_cast<std::streamsize
>(Cipher::getBlockSize()));
461 std::size_t nbRead =
static_cast<std::size_t
>(input.gcount());
463 for(std::size_t i = 0, idx = 1 ; i < _nbThread ; ++i, ++idx) {
464 input.read(
reinterpret_cast<char*
>(data[idx].data()),
static_cast<std::streamsize
>(Cipher::getBlockSize()));
465 std::streamsize nextCount = input.gcount();
469 threads.push_back(std::thread(&Derived::deferredDecipherBlock,
478 std::ref(nbWrite[i])));
482 nbRead =
static_cast<std::size_t
>(nextCount);
485 for(uint32_t i = 0 ; !threads.empty() ; ++i) {
486 std::thread& th = threads.front();
488 for(uint32_t j = 0 ; j < nbWrite[i] ; ++j) {
489 output << result[i][j];
496 data[0] = data[_nbThread];
505 template<
typename Derived,
typename Cipher>
507 BlockCipherModeOfOperation<Derived,Cipher>::deferredCipherBlock(uint64_t index,
508 std::array<uint8_t,Cipher::getBlockSize()>& input,
509 std::streamsize nbRead,
510 std::array<uint8_t,Cipher::getBlockSize()>& output,
512 std::size_t& nbWrite) {
513 nbWrite = cipherBlock(input, nbRead, output, index, cipher);
516 template<
typename Derived,
typename Cipher>
518 BlockCipherModeOfOperation<Derived,Cipher>::deferredDecipherBlock(uint64_t index,
519 std::array<uint8_t, Cipher::getBlockSize()>& input,
520 std::array<uint8_t, Cipher::getBlockSize()> prevInput,
521 std::streamsize nbRead,
523 std::array<uint8_t, Cipher::getBlockSize()>& output,
525 std::size_t& nbWrite) {
527 nbWrite = decipherBlock(input, prevInput, nbRead, lastBlock, output, index, cipher);
536 template<
typename Derived,
typename Cipher>
539 _nbThread = nbThread;