20 #ifndef _ANCH_CRYPTO_CTR_H_ 21 #define _ANCH_CRYPTO_CTR_H_ 23 #include "crypto/cipher/bcModOp.hpp" 37 template<
typename Cipher>
43 std::array<uint8_t,Cipher::getBlockSize()> _nonce;
56 CTR(
const std::array<uint8_t,Cipher::getBlockSize()>& nonce,
unsigned int nbThread = 1):
88 virtual std::size_t
cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
89 std::streamsize nbRead,
90 std::array<uint8_t,Cipher::getBlockSize()>& output,
93 std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
94 uint16_t* counter =
reinterpret_cast<uint16_t*
>(&ctxtVect.data()[Cipher::getBlockSize() - 2]);
96 std::array<uint8_t,Cipher::getBlockSize()> data;
97 cipher.cipher(ctxtVect, data);
98 for(std::streamsize i = 0 ; i < nbRead ; ++i) {
99 output[i] = input[i] ^ data[i];
116 virtual std::size_t
decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
117 std::array<uint8_t,Cipher::getBlockSize()>&,
118 std::streamsize nbRead,
120 std::array<uint8_t,Cipher::getBlockSize()>& output,
122 Cipher&
cipher)
override {
123 std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
124 uint16_t* counter =
reinterpret_cast<uint16_t*
>(&ctxtVect.data()[Cipher::getBlockSize() - 2]);
126 std::array<uint8_t,Cipher::getBlockSize()> data;
127 cipher.cipher(ctxtVect, data);
128 for(std::streamsize i = 0 ; i < nbRead ; ++i) {
129 output[i] = input[i] ^ data[i];
139 virtual const std::array<uint8_t,Cipher::getBlockSize()>&
reset() {
149 #endif // _ANCH_CRYPTO_CTR_H_ virtual ~CTR()
Definition: ctr.hpp:68
virtual std::size_t cipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::streamsize nbRead, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t index, Cipher &cipher) override
Definition: ctr.hpp:88
virtual std::size_t decipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::array< uint8_t, Cipher::getBlockSize()> &, std::streamsize nbRead, bool, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t index, Cipher &cipher) override
Definition: ctr.hpp:116
AnCH framework base namespace.
Definition: base64.hpp:28
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: ctr.hpp:139
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition: bcModOp.hpp:133
Counter implementation.
Definition: ctr.hpp:38
Block cipher mode of operation interface.
Definition: bcModOp.hpp:49
CTR(const std::array< uint8_t, Cipher::getBlockSize()> &nonce, unsigned int nbThread=1)
Definition: ctr.hpp:56