AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
bcModOp.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-crypto-export.hpp"
23
24#include "crypto/cipher/invalidBlockException.hpp"
25
26#include <istream>
27#include <ostream>
28#include <array>
29#include <thread>
30#include <condition_variable>
31#include <atomic>
32#include <cstddef>
33#include <cstring>
34#include <deque>
35#include <functional>
36#include <map>
37#include <mutex>
38#include <string>
39#include <vector>
40
41
42namespace anch::crypto {
43
53 template<typename Derived, typename Cipher>
54 class ANCH_CRYPTO_EXPORT BlockCipherModeOfOperation {
55
56 // Attributes +
57 private:
59 bool _cipherParallelizable;
60
62 bool _decipherParallelizable;
63
65 unsigned int _nbThread;
66
68 std::mutex _streamMutex;
69
71 std::condition_variable _writeBlock;
72
74 std::atomic<uint32_t> _endIdx;
75
77 std::atomic<uint32_t> _writeIdx;
78
80 std::atomic<bool> _error;
81
83 std::ostream* _stream;
84 // Attributes -
85
86
87 // Constructors +
88 protected:
97 BlockCipherModeOfOperation(bool cipherParallelizable,
98 bool decipherParallelizable,
99 unsigned int nbThread = 1);
100 // Constructors -
101
102
103 // Destructor +
108 // Destructor -
109
110
111 // Methods +
112 public:
120 void cipher(std::istream& input, std::ostream& output, const std::string& key);
121
129 void decipher(std::istream& input, std::ostream& output, const std::string& key);
130
131 protected:
144 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
145 std::streamsize nbRead,
146 std::array<uint8_t,Cipher::getBlockSize()>& output,
147 uint64_t index,
148 Cipher& cipher) = 0;
149
164 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
165 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
166 std::streamsize nbRead,
167 bool lastBlock,
168 std::array<uint8_t,Cipher::getBlockSize()>& output,
169 uint64_t index,
170 Cipher& cipher) = 0;
171
177 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() = 0;
178
179 private:
187 void cipherSequentially(std::istream& input, std::ostream& output, const std::string& key);
188
196 void cipherInParallel(std::istream& input, std::ostream& output, const std::string& key);
197
206 void decipherSequentially(std::istream& input,
207 std::ostream& output,
208 const std::string& key,
209 std::array<uint8_t,Cipher::getBlockSize()>& prevData);
210
219 void decipherInParallel(std::istream& input,
220 std::ostream& output,
221 const std::string& key,
222 std::array<uint8_t,Cipher::getBlockSize()>& prevData);
223
232 void deferredCipherBlock(uint64_t index,
233 std::array<uint8_t,Cipher::getBlockSize()>& input,
234 std::streamsize nbRead,
235 std::array<uint8_t,Cipher::getBlockSize()>& output,
236 Cipher& cipher,
237 std::size_t& nbWrite);
238
248 virtual void deferredDecipherBlock(uint64_t index,
249 std::array<uint8_t, Cipher::getBlockSize()>& input,
250 std::array<uint8_t, Cipher::getBlockSize()> prevInput,
251 std::streamsize nbRead,
252 bool lastBlock,
253 std::array<uint8_t, Cipher::getBlockSize()>& output,
254 Cipher& cipher,
255 std::size_t& nbWrite);
256 // Methods -
257
258
259 // Accessors +
260 public:
266 void setNbThread(unsigned int nbThread);
267 // Accessors -
268
269 };
270
271 // Constructors +
272 template<typename Derived, typename Cipher>
274 bool decipherParallelizable,
275 unsigned int nbThread):
276 _cipherParallelizable(cipherParallelizable),
277 _decipherParallelizable(decipherParallelizable),
278 _nbThread(nbThread),
279 _writeBlock(),
280 _endIdx(UINT32_MAX),
281 _writeIdx(0),
282 _error(false) {
283 if(nbThread == 0 && (cipherParallelizable || decipherParallelizable)) {
284 _nbThread = std::thread::hardware_concurrency();
285 if(_nbThread == 0) { // Keep at least 1 thread running
286 _nbThread = 1;
287 }
288 }
289 }
290 // Constructors -
291
292
293 // Destructor +
294 template<typename Derived, typename Cipher>
298 // Destructor -
299
300
301 // Methods +
302 template<typename Derived, typename Cipher>
303 void
305 std::ostream& output,
306 const std::string& key) {
307 if(input && output) {
308 reset();
309 if(!_cipherParallelizable || _nbThread == 1) {
310 cipherSequentially(input, output, key);
311
312 } else {
313 cipherInParallel(input, output, key);
314 }
315 } else {
316 throw std::ios_base::failure("Input and/or output streams are closed");
317 }
318 }
319
320 template<typename Derived, typename Cipher>
321 void
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);
329
330 } else {
331 decipherInParallel(input, output, key, prevData);
332 }
333 } else {
334 throw std::ios_base::failure("Input and/or output streams are closed");
335 }
336 }
337
338 template<typename Derived, typename Cipher>
339 inline void
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;
346 uint64_t index = 0;
347 while(!input.eof()) {
348 input.read(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(Cipher::getBlockSize()));
349 std::streamsize nbRead = input.gcount();
350 if(nbRead == 0) {
351 break;
352 }
353 std::size_t nbBlocks = cipherBlock(data, nbRead, out, index, cipher);
354 for(std::size_t i = 0 ; i < nbBlocks ; ++i) {
355 output << out[i];
356 }
357 index++;
358 }
359 output.flush();
360 }
361
362 template<typename Derived, typename Cipher>
363 inline void
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];
370 uint64_t index = 0;
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]));
376 }
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();
381 if(nbRead == 0) {
382 break;
383 }
384 threads.push_back(std::thread(&Derived::deferredCipherBlock,
385 this,
386 index++,
387 std::ref(data[i]),
388 nbRead,
389 std::ref(result[i]),
390 std::ref(ciph[i]),
391 std::ref(nbWrite[i])));
392 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
393 break;
394 }
395 }
396 for(uint32_t i = 0 ; !threads.empty() ; ++i) {
397 std::thread& th = threads.front();
398 th.join();
399 for(uint32_t j = 0 ; j < nbWrite[i] ; ++j) {
400 output << result[i][j];
401 }
402 threads.pop_front();
403 }
404 }
405
406 output.flush();
407 delete[] data;
408 delete[] result;
409 delete[] nbWrite;
410 }
411
412 template<typename Derived, typename Cipher>
413 void
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();
424 uint64_t index = 0;
425 do {
426 cipherData = data;
427 std::streamsize read = nbRead;
428 input.read(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(Cipher::getBlockSize()));
429
430 std::size_t end = decipherBlock(cipherData, prevData, read, input.eof(), out, index, cipher);
431 for(std::size_t i = 0 ; i < end ; ++i) {
432 output << out[i];
433 }
434
435 prevData = cipherData;
436 nbRead = input.gcount();
437 index++;
438
439 } while(nbRead != 0);
440 output.flush();
441 }
442
443 template<typename Derived, typename Cipher>
444 inline void
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];
452 uint64_t index = 0;
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]));
458 }
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());
462 do {
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();
466 if(nextCount == 0) {
467 lastBlock = true;
468 }
469 threads.push_back(std::thread(&Derived::deferredDecipherBlock,
470 this,
471 index++,
472 std::ref(data[i]),
473 prevData,
474 nbRead,
475 lastBlock,
476 std::ref(result[i]),
477 std::ref(ciph[i]),
478 std::ref(nbWrite[i])));
479 if(lastBlock) {
480 break;
481 }
482 nbRead = static_cast<std::size_t>(nextCount);
483 prevData = data[i];
484 }
485 for(uint32_t i = 0 ; !threads.empty() ; ++i) {
486 std::thread& th = threads.front();
487 th.join();
488 for(uint32_t j = 0 ; j < nbWrite[i] ; ++j) {
489 output << result[i][j];
490 }
491 threads.pop_front();
492 }
493 if(_error) {
494 throw InvalidBlockException("Error while decipher stream");
495 }
496 data[0] = data[_nbThread];
497 } while(!lastBlock);
498
499 output.flush();
500 delete[] data;
501 delete[] result;
502 delete[] nbWrite;
503 }
504
505 template<typename Derived, typename Cipher>
506 void
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,
511 Cipher& cipher,
512 std::size_t& nbWrite) {
513 nbWrite = cipherBlock(input, nbRead, output, index, cipher);
514 }
515
516 template<typename Derived, typename Cipher>
517 void
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,
522 bool lastBlock,
523 std::array<uint8_t, Cipher::getBlockSize()>& output,
524 Cipher& cipher,
525 std::size_t& nbWrite) {
526 try {
527 nbWrite = decipherBlock(input, prevInput, nbRead, lastBlock, output, index, cipher);
528 } catch(const InvalidBlockException& e) {
529 _error = true;
530 }
531 }
532 // Methods -
533
534
535 // Accessors +
536 template<typename Derived, typename Cipher>
537 inline void
539 _nbThread = nbThread;
540 }
541 // Accessors -
542
543} // anch::crypto
virtual std::size_t decipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::array< uint8_t, Cipher::getBlockSize()> &prevInput, std::streamsize nbRead, bool lastBlock, std::array< uint8_t, Cipher::getBlockSize()> &output, uint64_t index, Cipher &cipher)=0
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()=0
void decipher(std::istream &input, std::ostream &output, const std::string &key)
Definition bcModOp.hpp:322
BlockCipherModeOfOperation(bool cipherParallelizable, bool decipherParallelizable, unsigned int nbThread=1)
Definition bcModOp.hpp:273
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition bcModOp.hpp:304
virtual ~BlockCipherModeOfOperation()
Definition bcModOp.hpp:295
void setNbThread(unsigned int nbThread)
Definition bcModOp.hpp:538
virtual std::size_t cipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::streamsize nbRead, std::array< uint8_t, Cipher::getBlockSize()> &output, uint64_t index, Cipher &cipher)=0
Exception on receiving an invalid block.
Definition invalidBlockException.hpp:39
InvalidBlockException(const std::string &message)
Cryptography namespace.
Definition base64.hpp:30