20 #ifndef _ANCH_CRYPTO_PCBC_H_ 21 #define _ANCH_CRYPTO_PCBC_H_ 23 #include "crypto/cipher/bcModOp.hpp" 40 template<
typename Cipher,
typename Padding>
46 std::array<uint8_t,Cipher::getBlockSize()> _initVect;
49 std::array<uint8_t,Cipher::getBlockSize()> _ctxtVect;
62 PCBC(
const std::array<uint8_t,Cipher::getBlockSize()>& initVect,
unsigned int nbThread = 1):
94 virtual std::size_t
cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
95 std::streamsize nbRead,
96 std::array<uint8_t,Cipher::getBlockSize()>& output,
97 uint32_t, Cipher&
cipher)
override {
98 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
99 Padding::pad(input.data(), nbRead, Cipher::getBlockSize());
101 std::array<uint8_t,Cipher::getBlockSize()> data;
102 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
103 data[i] = input[i] ^ _ctxtVect[i];
105 cipher.cipher(data, output);
106 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
107 _ctxtVect[i] = input[i] ^ output[i];
109 return Cipher::getBlockSize();
124 virtual std::size_t
decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
125 std::array<uint8_t,Cipher::getBlockSize()>&,
126 std::streamsize nbRead,
128 std::array<uint8_t,Cipher::getBlockSize()>& output,
129 uint32_t, Cipher&
cipher)
override {
130 if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
133 std::array<uint8_t,Cipher::getBlockSize()> data;
134 cipher.decipher(input, data);
135 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
136 output[i] = data[i] ^ _ctxtVect[i];
137 _ctxtVect[i] = input[i] ^ output[i];
140 return Padding::length(output.data(), Cipher::getBlockSize());
142 return Cipher::getBlockSize();
151 virtual const std::array<uint8_t,Cipher::getBlockSize()>&
reset() {
152 _ctxtVect = _initVect;
162 #endif // _ANCH_CRYPTO_PCBC_H_ virtual ~PCBC()
Definition: pcbc.hpp:75
PCBC(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition: pcbc.hpp:62
Propagating cipher-block chaining implementation.
Definition: pcbc.hpp:41
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: pcbc.hpp:151
Exception on receiving an invalid block.
Definition: invalidBlockException.hpp:39
AnCH framework base namespace.
Definition: base64.hpp:28
virtual std::size_t decipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::array< uint8_t, Cipher::getBlockSize()> &, std::streamsize nbRead, bool lastBlock, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t, Cipher &cipher) override
Definition: pcbc.hpp:124
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 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: pcbc.hpp:94