AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
ecb.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_ECB_H_
21 #define _ANCH_CRYPTO_ECB_H_
22 
23 #include "crypto/cipher/bcModOp.hpp"
24 
25 
26 namespace anch {
27  namespace crypto {
28 
38  template<typename Cipher, typename Padding>
39  class ECB: public BlockCipherModeOfOperation<ECB<Cipher,Padding>,Cipher> {
40 
41  // Attributes +
42  private:
44  std::array<uint8_t,Cipher::getBlockSize()> _initVect;
45  // Attributes -
46 
47 
48  // Constructors +
49  public:
56  ECB(unsigned int nbThread = 1):
57  BlockCipherModeOfOperation<ECB<Cipher,Padding>,Cipher>(true, true, nbThread), _initVect() {
58  // Nothing to do
59  }
60  // Constructors -
61 
62 
63  // Destructor +
67  virtual ~ECB() {
68  // Nothing to do
69  }
70  // Destructor -
71 
72 
73  // Methods +
74  protected:
86  virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
87  std::streamsize nbRead,
88  std::array<uint8_t,Cipher::getBlockSize()>& output,
89  uint32_t, Cipher& cipher) override {
90  if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
91  Padding::pad(input.data(), nbRead, Cipher::getBlockSize());
92  }
93  cipher.cipher(input, output);
94  return Cipher::getBlockSize(); // This mode pad data => the number of bytes to write will always be a complete block
95  }
96 
109  virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
110  std::array<uint8_t,Cipher::getBlockSize()>&,
111  std::streamsize nbRead,
112  bool lastBlock,
113  std::array<uint8_t,Cipher::getBlockSize()>& output,
114  uint32_t, Cipher& cipher) override {
115  if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
116  throw InvalidBlockException("Invalid block size");
117  }
118  cipher.decipher(input, output);
119  if(lastBlock) {
120  return Padding::length(output.data(), Cipher::getBlockSize());
121  } else {
122  return Cipher::getBlockSize();
123  }
124  }
125 
131  virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() {
132  return _initVect;
133  }
134  // Methods -
135 
136  };
137 
138  }
139 }
140 
141 #endif // _ANCH_CRYPTO_ECB_H_
virtual ~ECB()
Definition: ecb.hpp:67
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: ecb.hpp:86
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
ECB(unsigned int nbThread=1)
Definition: ecb.hpp:56
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: ecb.hpp:131
virtual std::size_t decipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::array< uint8_t, Cipher::getBlockSize()> &, std::streamsize nbRead, bool lastBlock, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t, Cipher &cipher) override
Definition: ecb.hpp:109
Electronic codebook implementation.
Definition: ecb.hpp:39