AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
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#pragma once
21
22#include "crypto/cipher/bcModOp.hpp"
23
24
25namespace anch {
26 namespace crypto {
27
37 template<typename Cipher, typename Padding>
38 class ECB: public BlockCipherModeOfOperation<ECB<Cipher,Padding>,Cipher> {
39
40 // Attributes +
41 private:
43 std::array<uint8_t,Cipher::getBlockSize()> _initVect;
44 // Attributes -
45
46
47 // Constructors +
48 public:
55 ECB(unsigned int nbThread = 1);
56 // Constructors -
57
58
59 // Destructor +
63 virtual ~ECB();
64 // Destructor -
65
66
67 // Methods +
68 protected:
80 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
81 std::streamsize nbRead,
82 std::array<uint8_t,Cipher::getBlockSize()>& output,
83 uint32_t, Cipher& cipher) override;
84
97 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
98 std::array<uint8_t,Cipher::getBlockSize()>&,
99 std::streamsize nbRead,
100 bool lastBlock,
101 std::array<uint8_t,Cipher::getBlockSize()>& output,
102 uint32_t, Cipher& cipher) override;
103
109 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset();
110 // Methods -
111
112 };
113
114 // Constructors +
115 template<typename Cipher, typename Padding>
116 ECB<Cipher,Padding>::ECB(unsigned int nbThread):
117 BlockCipherModeOfOperation<ECB<Cipher,Padding>,Cipher>(true, true, nbThread), _initVect() {
118 // Nothing to do
119 }
120 // Constructors -
121
122
123 // Destructor +
124 template<typename Cipher, typename Padding>
126 // Nothing to do
127 }
128 // Destructor -
129
130
131 // Methods +
132 template<typename Cipher, typename Padding>
133 std::size_t
134 ECB<Cipher,Padding>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
135 std::streamsize nbRead,
136 std::array<uint8_t,Cipher::getBlockSize()>& output,
137 uint32_t, Cipher& cipher) {
138 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
139 Padding::pad(input.data(), static_cast<std::size_t>(nbRead), Cipher::getBlockSize());
140 }
141 cipher.cipher(input, output);
142 return Cipher::getBlockSize(); // This mode pad data => the number of bytes to write will always be a complete block
143 }
144
145 template<typename Cipher, typename Padding>
146 std::size_t
147 ECB<Cipher,Padding>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
148 std::array<uint8_t,Cipher::getBlockSize()>&,
149 std::streamsize nbRead,
150 bool lastBlock,
151 std::array<uint8_t,Cipher::getBlockSize()>& output,
152 uint32_t, Cipher& cipher) {
153 if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
154 throw InvalidBlockException("Invalid block size");
155 }
156 cipher.decipher(input, output);
157 if(lastBlock) {
158 return Padding::length(output.data(), Cipher::getBlockSize());
159 } else {
160 return Cipher::getBlockSize();
161 }
162 }
163
164 template<typename Cipher, typename Padding>
165 const std::array<uint8_t,Cipher::getBlockSize()>&
167 return _initVect;
168 }
169 // Methods -
170
171 }
172}
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 lastBlock, std::array< uint8_t, Cipher::getBlockSize()> &output, uint32_t, Cipher &cipher) override
Definition ecb.hpp:147
ECB(unsigned int nbThread=1)
Definition ecb.hpp:116
virtual ~ECB()
Definition ecb.hpp:125
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset()
Definition ecb.hpp:166
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:134
Exception on receiving an invalid block.
Definition invalidBlockException.hpp:38
Cryptography namespace.
Definition base64.hpp:28
AnCH framework base namespace.
Definition app.hpp:28