AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
ofb.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 OFB: public BlockCipherModeOfOperation<OFB<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 OFB(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread = 1);
60 // Constructors -
61
62
63 // Destructor +
67 virtual ~OFB();
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
100 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
101 std::array<uint8_t,Cipher::getBlockSize()>&,
102 std::streamsize nbRead,
103 bool,
104 std::array<uint8_t,Cipher::getBlockSize()>& output,
105 uint64_t, Cipher& cipher) override;
106
112 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() override;
113 // Methods -
114
115 };
116
117 // Constructors +
118 template<typename Cipher>
119 OFB<Cipher>::OFB(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread):
120 BlockCipherModeOfOperation<OFB<Cipher>,Cipher>(false, false, 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 OFB<Cipher>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
140 std::streamsize nbRead,
141 std::array<uint8_t,Cipher::getBlockSize()>& output,
142 uint64_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] = data[i];
148 }
149 return static_cast<std::size_t>(nbRead);
150 }
151
152 template<typename Cipher>
153 std::size_t
154 OFB<Cipher>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
155 std::array<uint8_t,Cipher::getBlockSize()>&,
156 std::streamsize nbRead,
157 bool,
158 std::array<uint8_t,Cipher::getBlockSize()>& output,
159 uint64_t, Cipher& cipher) {
160 std::array<uint8_t,Cipher::getBlockSize()> data;
161 cipher.cipher(_ctxtVect, data);
162 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
163 output[i] = input[i] ^ data[i];
164 _ctxtVect[i] = data[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
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset() override
Definition ofb.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 ofb.hpp:139
OFB(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition ofb.hpp:119
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, Cipher &cipher) override
Definition ofb.hpp:154
virtual ~OFB()
Definition ofb.hpp:130
Cryptography namespace.
Definition base64.hpp:30