AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
ctr.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-crypto-export.hpp"
23
24#include "crypto/cipher/bcModOp.hpp"
25
26namespace anch::crypto {
27
39 template<typename Cipher>
40 class ANCH_CRYPTO_EXPORT CTR: public BlockCipherModeOfOperation<CTR<Cipher>,Cipher> {
41
42 // Attributes +
43 private:
45 std::array<uint8_t,Cipher::getBlockSize()> _nonce;
46 // Attributes -
47
48
49 // Constructors +
50 public:
58 CTR(const std::array<uint8_t,Cipher::getBlockSize()>& nonce, unsigned int nbThread = 1);
59 // Constructors -
60
61
62 // Destructor +
66 virtual ~CTR();
67 // Destructor -
68
69
70 // Methods +
71 protected:
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 index,
88 Cipher& cipher) override;
89
102 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
103 std::array<uint8_t,Cipher::getBlockSize()>&,
104 std::streamsize nbRead,
105 bool,
106 std::array<uint8_t,Cipher::getBlockSize()>& output,
107 uint64_t index,
108 Cipher& cipher) override;
109
115 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() override;
116
117 private:
126 static void setCounter(std::array<uint8_t,Cipher::getBlockSize()>& ctxtVect, uint64_t index);
127 // Methods -
128
129 };
130
131 // Constructors +
132 template<typename Cipher>
133 CTR<Cipher>::CTR(const std::array<uint8_t,Cipher::getBlockSize()>& nonce, unsigned int nbThread):
134 BlockCipherModeOfOperation<CTR<Cipher>,Cipher>(true, true, nbThread),
135 _nonce(nonce) {
136 // Nothing to do
137 }
138 // Constructors -
139
140
141 // Destructor +
142 template<typename Cipher>
144 // Nothing to do
145 }
146 // Destructor -
147
148
149 // Methods +
150 template<typename Cipher>
151 std::size_t
152 CTR<Cipher>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
153 std::streamsize nbRead,
154 std::array<uint8_t,Cipher::getBlockSize()>& output,
155 uint64_t index,
156 Cipher& cipher) {
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];
163 }
164 return static_cast<std::size_t>(nbRead);
165 }
166
167 template<typename Cipher>
168 std::size_t
169 CTR<Cipher>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
170 std::array<uint8_t,Cipher::getBlockSize()>&,
171 std::streamsize nbRead,
172 bool,
173 std::array<uint8_t,Cipher::getBlockSize()>& output,
174 uint64_t index,
175 Cipher& cipher) {
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];
182 }
183 return static_cast<std::size_t>(nbRead);
184 }
185
186 template<typename Cipher>
187 const std::array<uint8_t,Cipher::getBlockSize()>&
189 return _nonce;
190 }
191
192 template<typename Cipher>
193 inline void
194 CTR<Cipher>::setCounter(std::array<uint8_t,Cipher::getBlockSize()>& ctxtVect, uint64_t index) {
195 // Add index to the big endian counter, from the last byte, with carry
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);
201 }
202 }
203 // Methods -
204
205} // anch::crypto
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