22#include "anch-crypto-export.hpp"
24#include "crypto/cipher/bcModOp.hpp"
37 template<
typename Cipher,
typename Padding>
43 std::array<uint8_t,Cipher::getBlockSize()> _initVect;
46 std::array<uint8_t,Cipher::getBlockSize()> _ctxtVect;
59 CBC(
const std::array<uint8_t,Cipher::getBlockSize()>& initVect,
unsigned int nbThread = 1);
84 virtual std::size_t
cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
85 std::streamsize nbRead,
86 std::array<uint8_t,Cipher::getBlockSize()>& output,
87 uint64_t, Cipher&
cipher)
override;
102 virtual std::size_t
decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
103 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
104 std::streamsize nbRead,
106 std::array<uint8_t,Cipher::getBlockSize()>& output,
107 uint64_t, Cipher&
cipher)
override;
114 virtual const std::array<uint8_t,Cipher::getBlockSize()>&
reset()
override;
120 template<
typename Cipher,
typename Padding>
131 template<
typename Cipher,
typename Padding>
139 template<
typename Cipher,
typename Padding>
142 std::streamsize nbRead,
143 std::array<uint8_t,Cipher::getBlockSize()>& output,
144 uint64_t, Cipher&
cipher) {
145 if(
static_cast<std::size_t
>(nbRead) != Cipher::getBlockSize()) {
146 Padding::pad(input.data(),
static_cast<std::size_t
>(nbRead), Cipher::getBlockSize());
148 std::array<uint8_t,Cipher::getBlockSize()> data;
149 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
150 data[i] = input[i] ^ _ctxtVect[i];
152 cipher.cipher(data, output);
153 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
154 _ctxtVect[i] = output[i];
156 return Cipher::getBlockSize();
159 template<
typename Cipher,
typename Padding>
162 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
163 std::streamsize nbRead,
165 std::array<uint8_t,Cipher::getBlockSize()>& output,
166 uint64_t, Cipher&
cipher) {
167 if(lastBlock &&
static_cast<std::size_t
>(nbRead) != Cipher::getBlockSize()) {
170 std::array<uint8_t,Cipher::getBlockSize()> data;
171 cipher.decipher(input, data);
172 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
173 output[i] = data[i] ^ prevInput[i];
176 return Padding::length(output.data(), Cipher::getBlockSize());
178 return Cipher::getBlockSize();
182 template<
typename Cipher,
typename Padding>
183 const std::array<uint8_t,Cipher::getBlockSize()>&
185 _ctxtVect = _initVect;
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
CBC(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition cbc.hpp:121
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset() override
Definition cbc.hpp:184
virtual ~CBC()
Definition cbc.hpp:132
virtual std::size_t cipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::streamsize nbRead, std::array< uint8_t, Cipher::getBlockSize()> &output, uint64_t, Cipher &cipher) override
Definition cbc.hpp:141
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, Cipher &cipher) override
Definition cbc.hpp:161
InvalidBlockException(const std::string &message)
Cryptography namespace.
Definition base64.hpp:30