AnCH Framework  0.1
Another C++ Hack Framework
ctr.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_CTR_H_
21 #define _ANCH_CRYPTO_CTR_H_
22 
23 #include "crypto/cipher/bcModOp.hpp"
24 
25 namespace anch {
26  namespace crypto {
27 
37  template<typename Cipher>
38  class CTR: public BlockCipherModeOfOperation<CTR<Cipher>,Cipher> {
39 
40  // Attributes +
41  private:
43  std::array<uint8_t,Cipher::getBlockSize()> _nonce;
44  // Attributes -
45 
46 
47  // Constructors +
48  public:
56  CTR(const std::array<uint8_t,Cipher::getBlockSize()>& nonce, unsigned int nbThread = 1):
57  BlockCipherModeOfOperation<CTR<Cipher>,Cipher>(true, true, nbThread),
58  _nonce(nonce) {
59  // Nothing to do
60  }
61  // Constructors -
62 
63 
64  // Destructor +
68  virtual ~CTR() {
69  // Nothing to do
70  }
71  // Destructor -
72 
73 
74  // Methods +
75  protected:
88  virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
89  std::streamsize nbRead,
90  std::array<uint8_t,Cipher::getBlockSize()>& output,
91  uint32_t index,
92  Cipher& cipher) override {
93  std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
94  uint16_t* counter = reinterpret_cast<uint16_t*>(&ctxtVect.data()[Cipher::getBlockSize() - 2]);
95  *counter = index;
96  std::array<uint8_t,Cipher::getBlockSize()> data;
97  cipher.cipher(ctxtVect, data);
98  for(std::streamsize i = 0 ; i < nbRead ; ++i) {
99  output[i] = input[i] ^ data[i];
100  }
101  return nbRead;
102  }
103 
116  virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
117  std::array<uint8_t,Cipher::getBlockSize()>&,
118  std::streamsize nbRead,
119  bool,
120  std::array<uint8_t,Cipher::getBlockSize()>& output,
121  uint32_t index,
122  Cipher& cipher) override {
123  std::array<uint8_t,Cipher::getBlockSize()> ctxtVect(_nonce);
124  uint16_t* counter = reinterpret_cast<uint16_t*>(&ctxtVect.data()[Cipher::getBlockSize() - 2]);
125  *counter = index;
126  std::array<uint8_t,Cipher::getBlockSize()> data;
127  cipher.cipher(ctxtVect, data);
128  for(std::streamsize i = 0 ; i < nbRead ; ++i) {
129  output[i] = input[i] ^ data[i];
130  }
131  return nbRead;
132  }
133 
139  virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() {
140  return _nonce;
141  }
142  // Methods -
143 
144  };
145 
146  }
147 }
148 
149 #endif // _ANCH_CRYPTO_CTR_H_
virtual ~CTR()
Definition: ctr.hpp:68
virtual std::size_t cipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::streamsize nbRead, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t index, Cipher &cipher) override
Definition: ctr.hpp:88
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 index, Cipher &cipher) override
Definition: ctr.hpp:116
AnCH framework base namespace.
Definition: base64.hpp:28
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition: ctr.hpp:139
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition: bcModOp.hpp:133
Counter implementation.
Definition: ctr.hpp:38
Block cipher mode of operation interface.
Definition: bcModOp.hpp:49
CTR(const std::array< uint8_t, Cipher::getBlockSize()> &nonce, unsigned int nbThread=1)
Definition: ctr.hpp:56