22#include "anch-crypto-export.hpp"
24#include "crypto/cipher/bcModOp.hpp"
39 template<
typename Cipher>
45 std::array<uint8_t,Cipher::getBlockSize()> _nonce;
58 CTR(
const std::array<uint8_t,Cipher::getBlockSize()>& nonce,
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,
102 virtual std::size_t
decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
103 std::array<uint8_t,Cipher::getBlockSize()>&,
104 std::streamsize nbRead,
106 std::array<uint8_t,Cipher::getBlockSize()>& output,
115 virtual const std::array<uint8_t,Cipher::getBlockSize()>&
reset()
override;
126 static void setCounter(std::array<uint8_t,Cipher::getBlockSize()>& ctxtVect, uint64_t index);
132 template<
typename Cipher>
133 CTR<Cipher>::CTR(
const std::array<uint8_t,Cipher::getBlockSize()>& nonce,
unsigned int nbThread):
142 template<
typename Cipher>
150 template<
typename Cipher>
153 std::streamsize nbRead,
154 std::array<uint8_t,Cipher::getBlockSize()>& output,
157 std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
158 setCounter(ctxtVect, index);
159 std::array<uint8_t,Cipher::getBlockSize()> data;
160 cipher.cipher(ctxtVect, data);
161 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
162 output[i] = input[i] ^ data[i];
164 return static_cast<std::size_t
>(nbRead);
167 template<
typename Cipher>
170 std::array<uint8_t,Cipher::getBlockSize()>&,
171 std::streamsize nbRead,
173 std::array<uint8_t,Cipher::getBlockSize()>& output,
176 std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
177 setCounter(ctxtVect, index);
178 std::array<uint8_t,Cipher::getBlockSize()> data;
179 cipher.cipher(ctxtVect, data);
180 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
181 output[i] = input[i] ^ data[i];
183 return static_cast<std::size_t
>(nbRead);
186 template<
typename Cipher>
187 const std::array<uint8_t,Cipher::getBlockSize()>&
192 template<
typename Cipher>
194 CTR<Cipher>::setCounter(std::array<uint8_t,Cipher::getBlockSize()>& ctxtVect, uint64_t index) {
196 uint64_t carry = index;
197 for(std::size_t i = Cipher::getBlockSize() ; i > 0 && carry != 0 ; --i) {
198 uint64_t sum = ctxtVect[i - 1] + (carry & 0xff);
199 ctxtVect[i - 1] =
static_cast<uint8_t
>(sum & 0xff);
200 carry = (carry >> 8) + (sum >> 8);
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 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) override
Definition ctr.hpp:152
CTR(const std::array< uint8_t, Cipher::getBlockSize()> &nonce, unsigned int nbThread=1)
Definition ctr.hpp:133
virtual ~CTR()
Definition ctr.hpp:143
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, uint64_t index, Cipher &cipher) override
Definition ctr.hpp:169
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset() override
Definition ctr.hpp:188
Cryptography namespace.
Definition base64.hpp:30