AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
ctr.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 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 <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "crypto/cipher/bcModOp.hpp"
23
24namespace anch {
25 namespace crypto {
26
36 template<typename Cipher>
37 class CTR: public BlockCipherModeOfOperation<CTR<Cipher>,Cipher> {
38
39 // Attributes +
40 private:
42 std::array<uint8_t,Cipher::getBlockSize()> _nonce;
43 // Attributes -
44
45
46 // Constructors +
47 public:
55 CTR(const std::array<uint8_t,Cipher::getBlockSize()>& nonce, unsigned int nbThread = 1);
56 // Constructors -
57
58
59 // Destructor +
63 virtual ~CTR();
64 // Destructor -
65
66
67 // Methods +
68 protected:
81 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
82 std::streamsize nbRead,
83 std::array<uint8_t,Cipher::getBlockSize()>& output,
84 uint32_t index,
85 Cipher& cipher) override;
86
99 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
100 std::array<uint8_t,Cipher::getBlockSize()>&,
101 std::streamsize nbRead,
102 bool,
103 std::array<uint8_t,Cipher::getBlockSize()>& output,
104 uint32_t index,
105 Cipher& cipher) override;
106
112 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset();
113 // Methods -
114
115 };
116
117 // Constructors +
118 template<typename Cipher>
119 CTR<Cipher>::CTR(const std::array<uint8_t,Cipher::getBlockSize()>& nonce, unsigned int nbThread):
120 BlockCipherModeOfOperation<CTR<Cipher>,Cipher>(true, true, nbThread),
121 _nonce(nonce) {
122 // Nothing to do
123 }
124 // Constructors -
125
126
127 // Destructor +
128 template<typename Cipher>
130 // Nothing to do
131 }
132 // Destructor -
133
134
135 // Methods +
136 template<typename Cipher>
137 std::size_t
138 CTR<Cipher>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
139 std::streamsize nbRead,
140 std::array<uint8_t,Cipher::getBlockSize()>& output,
141 uint32_t index,
142 Cipher& cipher) {
143 std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
144 uint16_t* counter = reinterpret_cast<uint16_t*>(&ctxtVect.data()[Cipher::getBlockSize() - 2]);
145 *counter = static_cast<uint16_t>(index);
146 std::array<uint8_t,Cipher::getBlockSize()> data;
147 cipher.cipher(ctxtVect, data);
148 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
149 output[i] = input[i] ^ data[i];
150 }
151 return static_cast<std::size_t>(nbRead);
152 }
153
154 template<typename Cipher>
155 std::size_t
156 CTR<Cipher>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
157 std::array<uint8_t,Cipher::getBlockSize()>&,
158 std::streamsize nbRead,
159 bool,
160 std::array<uint8_t,Cipher::getBlockSize()>& output,
161 uint32_t index,
162 Cipher& cipher) {
163 std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
164 uint16_t* counter = reinterpret_cast<uint16_t*>(&ctxtVect.data()[Cipher::getBlockSize() - 2]);
165 *counter = static_cast<uint16_t>(index);
166 std::array<uint8_t,Cipher::getBlockSize()> data;
167 cipher.cipher(ctxtVect, data);
168 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
169 output[i] = input[i] ^ data[i];
170 }
171 return static_cast<std::size_t>(nbRead);
172 }
173
174 template<typename Cipher>
175 const std::array<uint8_t,Cipher::getBlockSize()>&
177 return _nonce;
178 }
179 // Methods -
180
181 }
182}
BlockCipherModeOfOperation(bool cipherParallelizable, bool decipherParallelizable, unsigned int nbThread=1)
Definition bcModOp.hpp:269
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition bcModOp.hpp:300
CTR(const std::array< uint8_t, Cipher::getBlockSize()> &nonce, unsigned int nbThread=1)
Definition ctr.hpp:119
virtual ~CTR()
Definition ctr.hpp:129
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:156
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:138
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition ctr.hpp:176
Cryptography namespace.
Definition base64.hpp:28
AnCH framework base namespace.
Definition app.hpp:28