AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
ofb.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 OFB: public BlockCipherModeOfOperation<OFB<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 OFB(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread = 1);
59 // Constructors -
60
61
62 // Destructor +
66 virtual ~OFB();
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
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, Cipher& cipher) override;
105
111 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset();
112 // Methods -
113
114 };
115
116 // Constructors +
117 template<typename Cipher>
118 OFB<Cipher>::OFB(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread):
119 BlockCipherModeOfOperation<OFB<Cipher>,Cipher>(false, false, nbThread),
120 _initVect(initVect),
121 _ctxtVect() {
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 OFB<Cipher>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
139 std::streamsize nbRead,
140 std::array<uint8_t,Cipher::getBlockSize()>& output,
141 uint32_t, Cipher& cipher) {
142 std::array<uint8_t,Cipher::getBlockSize()> data;
143 cipher.cipher(_ctxtVect, data);
144 for(std::size_t i = 0 ; i < static_cast<std::size_t>(nbRead) ; ++i) {
145 output[i] = input[i] ^ data[i];
146 _ctxtVect[i] = data[i];
147 }
148 return static_cast<std::size_t>(nbRead);
149 }
150
151 template<typename Cipher>
152 std::size_t
153 OFB<Cipher>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
154 std::array<uint8_t,Cipher::getBlockSize()>&,
155 std::streamsize nbRead,
156 bool,
157 std::array<uint8_t,Cipher::getBlockSize()>& output,
158 uint32_t, Cipher& cipher) {
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 _ctxtVect[i] = data[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
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, Cipher &cipher) override
Definition ofb.hpp:153
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 ofb.hpp:138
OFB(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition ofb.hpp:118
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition ofb.hpp:170
virtual ~OFB()
Definition ofb.hpp:129
Cryptography namespace.
Definition base64.hpp:28
AnCH framework base namespace.
Definition app.hpp:28