AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
ecb.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-crypto-export.hpp"
23
24#include "crypto/cipher/bcModOp.hpp"
25
26
27namespace anch::crypto {
28
38 template<typename Cipher, typename Padding>
39 class ANCH_CRYPTO_EXPORT 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 // Constructors -
58
59
60 // Destructor +
64 virtual ~ECB();
65 // Destructor -
66
67
68 // Methods +
69 protected:
81 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
82 std::streamsize nbRead,
83 std::array<uint8_t,Cipher::getBlockSize()>& output,
84 uint64_t, Cipher& cipher) override;
85
98 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
99 std::array<uint8_t,Cipher::getBlockSize()>&,
100 std::streamsize nbRead,
101 bool lastBlock,
102 std::array<uint8_t,Cipher::getBlockSize()>& output,
103 uint64_t, Cipher& cipher) override;
104
110 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() override;
111 // Methods -
112
113 };
114
115 // Constructors +
116 template<typename Cipher, typename Padding>
117 ECB<Cipher,Padding>::ECB(unsigned int nbThread):
118 BlockCipherModeOfOperation<ECB<Cipher,Padding>,Cipher>(true, true, nbThread), _initVect() {
119 // Nothing to do
120 }
121 // Constructors -
122
123
124 // Destructor +
125 template<typename Cipher, typename Padding>
127 // Nothing to do
128 }
129 // Destructor -
130
131
132 // Methods +
133 template<typename Cipher, typename Padding>
134 std::size_t
135 ECB<Cipher,Padding>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
136 std::streamsize nbRead,
137 std::array<uint8_t,Cipher::getBlockSize()>& output,
138 uint64_t, Cipher& cipher) {
139 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
140 Padding::pad(input.data(), static_cast<std::size_t>(nbRead), Cipher::getBlockSize());
141 }
142 cipher.cipher(input, output);
143 return Cipher::getBlockSize(); // This mode pad data => the number of bytes to write will always be a complete block
144 }
145
146 template<typename Cipher, typename Padding>
147 std::size_t
148 ECB<Cipher,Padding>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
149 std::array<uint8_t,Cipher::getBlockSize()>&,
150 std::streamsize nbRead,
151 bool lastBlock,
152 std::array<uint8_t,Cipher::getBlockSize()>& output,
153 uint64_t, Cipher& cipher) {
154 if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
155 throw InvalidBlockException("Invalid block size");
156 }
157 cipher.decipher(input, output);
158 if(lastBlock) {
159 return Padding::length(output.data(), Cipher::getBlockSize());
160 } else {
161 return Cipher::getBlockSize();
162 }
163 }
164
165 template<typename Cipher, typename Padding>
166 const std::array<uint8_t,Cipher::getBlockSize()>&
168 return _initVect;
169 }
170 // Methods -
171
172} // anch::crypto
BlockCipherModeOfOperation(bool cipherParallelizable, bool decipherParallelizable, unsigned int nbThread=1)
Definition bcModOp.hpp:273
void cipher(std::istream &input, std::ostream &output, const std::string &key)
Definition bcModOp.hpp:304
ECB(unsigned int nbThread=1)
Definition ecb.hpp:117
virtual ~ECB()
Definition ecb.hpp:126
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset() override
Definition ecb.hpp:167
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, uint64_t, Cipher &cipher) override
Definition ecb.hpp:148
virtual std::size_t cipherBlock(std::array< uint8_t, Cipher::getBlockSize()> &input, std::streamsize nbRead, std::array< uint8_t, Cipher::getBlockSize()> &output, uint64_t, Cipher &cipher) override
Definition ecb.hpp:135
InvalidBlockException(const std::string &message)
Cryptography namespace.
Definition base64.hpp:30