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