20 #ifndef _ANCH_CRYPTO_CBC_H_ 21 #define _ANCH_CRYPTO_CBC_H_ 23 #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):
91 virtual std::size_t
cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
92 std::streamsize nbRead,
93 std::array<uint8_t,Cipher::getBlockSize()>& output,
94 uint32_t, Cipher&
cipher)
override {
95 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
96 Padding::pad(input.data(), nbRead, Cipher::getBlockSize());
98 std::array<uint8_t,Cipher::getBlockSize()> data;
99 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
100 data[i] = input[i] ^ _ctxtVect[i];
102 cipher.cipher(data, output);
103 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
104 _ctxtVect[i] = output[i];
106 return Cipher::getBlockSize();
122 virtual std::size_t
decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
123 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
124 std::streamsize nbRead,
126 std::array<uint8_t,Cipher::getBlockSize()>& output,
127 uint32_t, Cipher&
cipher)
override {
128 if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
131 std::array<uint8_t,Cipher::getBlockSize()> data;
132 cipher.decipher(input, data);
133 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
134 output[i] = data[i] ^ prevInput[i];
137 return Padding::length(output.data(), Cipher::getBlockSize());
139 return Cipher::getBlockSize();
148 virtual const std::array<uint8_t,Cipher::getBlockSize()>&
reset() {
149 _ctxtVect = _initVect;
159 #endif // _ANCH_CRYPTO_CBC_H_ Cipher-block chaining implementation.
Definition: cbc.hpp:38
virtual std::size_t cipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::streamsize nbRead, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t, Cipher &cipher) override
Definition: cbc.hpp:91
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, uint32_t, Cipher &cipher) override
Definition: cbc.hpp:122
Exception on receiving an invalid block.
Definition: invalidBlockException.hpp:39
AnCH framework base namespace.
Definition: base64.hpp:28
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition: bcModOp.hpp:133
Block cipher mode of operation interface.
Definition: bcModOp.hpp:49
virtual ~CBC()
Definition: cbc.hpp:72
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: cbc.hpp:148
CBC(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition: cbc.hpp:59