AnCH Framework  0.1
Another C++ Hack Framework
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 #ifndef _ANCH_CRYPTO_OFB_H_
21 #define _ANCH_CRYPTO_OFB_H_
22 
23 #include "crypto/cipher/bcModOp.hpp"
24 
25 namespace anch {
26  namespace crypto {
27 
37  template<typename Cipher>
38  class 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  BlockCipherModeOfOperation<OFB<Cipher>,Cipher>(false, false, nbThread),
61  _initVect(initVect),
62  _ctxtVect() {
63  // Nothing to do
64  }
65  // Constructors -
66 
67 
68  // Destructor +
72  virtual ~OFB() {
73  // Nothing to do
74  }
75  // Destructor -
76 
77 
78  // Methods +
79  protected:
91  virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
92  std::streamsize nbRead,
93  std::array<uint8_t,Cipher::getBlockSize()>& output,
94  uint32_t, Cipher& cipher) override {
95  std::array<uint8_t,Cipher::getBlockSize()> data;
96  cipher.cipher(_ctxtVect, data);
97  for(std::streamsize i = 0 ; i < nbRead ; ++i) {
98  output[i] = input[i] ^ data[i];
99  _ctxtVect[i] = data[i];
100  }
101  return nbRead;
102  }
103 
115  virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
116  std::array<uint8_t,Cipher::getBlockSize()>&,
117  std::streamsize nbRead,
118  bool,
119  std::array<uint8_t,Cipher::getBlockSize()>& output,
120  uint32_t, Cipher& cipher) override {
121  std::array<uint8_t,Cipher::getBlockSize()> data;
122  cipher.cipher(_ctxtVect, data);
123  for(std::streamsize i = 0 ; i < nbRead ; ++i) {
124  output[i] = input[i] ^ data[i];
125  _ctxtVect[i] = data[i];
126  }
127  return nbRead;
128  }
129 
135  virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() {
136  _ctxtVect = _initVect;
137  return _initVect;
138  }
139  // Methods -
140 
141  };
142 
143  }
144 }
145 
146 #endif // _ANCH_CRYPTO_OFB_H_
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:115
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: ofb.hpp:135
AnCH framework base namespace.
Definition: base64.hpp:28
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition: bcModOp.hpp:133
virtual ~OFB()
Definition: ofb.hpp:72
Block cipher mode of operation interface.
Definition: bcModOp.hpp:49
OFB(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition: ofb.hpp:59
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:91
Output feedback implementation.
Definition: ofb.hpp:38