AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
cbc.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_CBC_H_
21 #define _ANCH_CRYPTO_CBC_H_
22 
23 #include "crypto/cipher/bcModOp.hpp"
24 
25 namespace anch {
26  namespace crypto {
27 
37  template<typename Cipher, typename Padding>
38  class CBC: public BlockCipherModeOfOperation<CBC<Cipher,Padding>,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  CBC(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread = 1):
60  BlockCipherModeOfOperation<CBC<Cipher,Padding>,Cipher>(false, true, nbThread),
61  _initVect(initVect),
62  _ctxtVect() {
63  // Nothing to do
64  }
65  // Constructors -
66 
67 
68  // Destructor +
72  virtual ~CBC() {
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  if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
96  Padding::pad(input.data(), nbRead, Cipher::getBlockSize());
97  }
98  std::array<uint8_t,Cipher::getBlockSize()> data;
99  for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
100  data[i] = input[i] ^ _ctxtVect[i];
101  }
102  cipher.cipher(data, output);
103  for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
104  _ctxtVect[i] = output[i];
105  }
106  return Cipher::getBlockSize(); // This mode pad data => the number of bytes to write will always be a complete block
107  }
108 
122  virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
123  std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
124  std::streamsize nbRead,
125  bool lastBlock,
126  std::array<uint8_t,Cipher::getBlockSize()>& output,
127  uint32_t, Cipher& cipher) override {
128  if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
129  throw InvalidBlockException("Invalid block size");
130  }
131  std::array<uint8_t,Cipher::getBlockSize()> data;
132  cipher.decipher(input, data);
133  for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
134  output[i] = data[i] ^ prevInput[i];
135  }
136  if(lastBlock) {
137  return Padding::length(output.data(), Cipher::getBlockSize());
138  } else {
139  return Cipher::getBlockSize();
140  }
141  }
142 
148  virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() {
149  _ctxtVect = _initVect;
150  return _initVect;
151  }
152  // Methods -
153 
154  };
155 
156  }
157 }
158 
159 #endif // _ANCH_CRYPTO_CBC_H_
Cipher-block chaining implementation.
Definition: cbc.hpp:38
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: cbc.hpp:91
virtual std::size_t decipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::array< uint8_t, Cipher::getBlockSize()> &prevInput, std::streamsize nbRead, bool lastBlock, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t, Cipher &cipher) override
Definition: cbc.hpp:122
Exception on receiving an invalid block.
Definition: invalidBlockException.hpp:39
AnCH framework base namespace.
Definition: base64.hpp:28
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition: bcModOp.hpp:133
Block cipher mode of operation interface.
Definition: bcModOp.hpp:49
virtual ~CBC()
Definition: cbc.hpp:72
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: cbc.hpp:148
CBC(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition: cbc.hpp:59