AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
cfb.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 CFB: public BlockCipherModeOfOperation<CFB<Cipher>,Cipher> {
38
39 // Attributes +
40 private:
42 std::array<uint8_t,Cipher::getBlockSize()> _initVect;
43
45 std::array<uint8_t,Cipher::getBlockSize()> _ctxtVect;
46 // Attributes -
47
48
49 // Constructors +
50 public:
58 CFB(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread = 1);
59 // Constructors -
60
61
62 // Destructor +
66 virtual ~CFB();
67 // Destructor -
68
69
70 // Methods +
71 protected:
83 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
84 std::streamsize nbRead,
85 std::array<uint8_t,Cipher::getBlockSize()>& output,
86 uint32_t, Cipher& cipher) override;
87
100 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
101 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
102 std::streamsize nbRead,
103 bool,
104 std::array<uint8_t,Cipher::getBlockSize()>& output,
105 uint32_t, 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 CFB<Cipher>::CFB(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread):
120 BlockCipherModeOfOperation<CFB<Cipher>,Cipher>(false, true, nbThread),
121 _initVect(initVect),
122 _ctxtVect() {
123 // Nothing to do
124 }
125 // Constructors -
126
127
128 // Destructor +
129 template<typename Cipher>
131 // Nothing to do
132 }
133 // Destructor -
134
135
136 // Methods +
137 template<typename Cipher>
138 std::size_t
139 CFB<Cipher>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
140 std::streamsize nbRead,
141 std::array<uint8_t,Cipher::getBlockSize()>& output,
142 uint32_t, Cipher& cipher) {
143 std::array<uint8_t,Cipher::getBlockSize()> data;
144 cipher.cipher(_ctxtVect, data);
145 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
146 output[i] = input[i] ^ data[i];
147 _ctxtVect[i] = output[i];
148 }
149 return static_cast<std::size_t>(nbRead);
150 }
151
152 template<typename Cipher>
153 std::size_t
154 CFB<Cipher>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
155 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
156 std::streamsize nbRead,
157 bool,
158 std::array<uint8_t,Cipher::getBlockSize()>& output,
159 uint32_t, Cipher& cipher) {
160 std::array<uint8_t,Cipher::getBlockSize()> data;
161 cipher.cipher(prevInput, data);
162 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
163 output[i] = data[i] ^ input[i];
164 }
165 return static_cast<std::size_t>(nbRead);
166 }
167
168 template<typename Cipher>
169 const std::array<uint8_t,Cipher::getBlockSize()>&
171 _ctxtVect = _initVect;
172 return _initVect;
173 }
174 // Methods -
175
176 }
177}
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
CFB(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition cfb.hpp:119
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition cfb.hpp:170
virtual ~CFB()
Definition cfb.hpp:130
virtual std::size_t decipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::array< uint8_t, Cipher::getBlockSize()> &prevInput, std::streamsize nbRead, bool, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t, Cipher &cipher) override
Definition cfb.hpp:154
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 cfb.hpp:139
Cryptography namespace.
Definition base64.hpp:28
AnCH framework base namespace.
Definition app.hpp:28