AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
cbc.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
26namespace anch::crypto {
27
37 template<typename Cipher, typename Padding>
38 class ANCH_CRYPTO_EXPORT 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 // Constructors -
61
62
63 // Destructor +
67 virtual ~CBC();
68 // Destructor -
69
70
71 // Methods +
72 protected:
84 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
85 std::streamsize nbRead,
86 std::array<uint8_t,Cipher::getBlockSize()>& output,
87 uint64_t, Cipher& cipher) override;
88
102 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
103 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
104 std::streamsize nbRead,
105 bool lastBlock,
106 std::array<uint8_t,Cipher::getBlockSize()>& output,
107 uint64_t, Cipher& cipher) override;
108
114 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() override;
115 // Methods -
116
117 };
118
119 // Constructors +
120 template<typename Cipher, typename Padding>
121 CBC<Cipher,Padding>::CBC(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread):
122 BlockCipherModeOfOperation<CBC<Cipher,Padding>,Cipher>(false, true, nbThread),
123 _initVect(initVect),
124 _ctxtVect() {
125 // Nothing to do
126 }
127 // Constructors -
128
129
130 // Destructor +
131 template<typename Cipher, typename Padding>
133 // Nothing to do
134 }
135 // Destructor -
136
137
138 // Methods +
139 template<typename Cipher, typename Padding>
140 std::size_t
141 CBC<Cipher,Padding>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
142 std::streamsize nbRead,
143 std::array<uint8_t,Cipher::getBlockSize()>& output,
144 uint64_t, Cipher& cipher) {
145 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
146 Padding::pad(input.data(), static_cast<std::size_t>(nbRead), Cipher::getBlockSize());
147 }
148 std::array<uint8_t,Cipher::getBlockSize()> data;
149 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
150 data[i] = input[i] ^ _ctxtVect[i];
151 }
152 cipher.cipher(data, output);
153 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
154 _ctxtVect[i] = output[i];
155 }
156 return Cipher::getBlockSize(); // This mode pad data => the number of bytes to write will always be a complete block
157 }
158
159 template<typename Cipher, typename Padding>
160 std::size_t
161 CBC<Cipher,Padding>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
162 std::array<uint8_t,Cipher::getBlockSize()>& prevInput,
163 std::streamsize nbRead,
164 bool lastBlock,
165 std::array<uint8_t,Cipher::getBlockSize()>& output,
166 uint64_t, Cipher& cipher) {
167 if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
168 throw InvalidBlockException("Invalid block size");
169 }
170 std::array<uint8_t,Cipher::getBlockSize()> data;
171 cipher.decipher(input, data);
172 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
173 output[i] = data[i] ^ prevInput[i];
174 }
175 if(lastBlock) {
176 return Padding::length(output.data(), Cipher::getBlockSize());
177 } else {
178 return Cipher::getBlockSize();
179 }
180 }
181
182 template<typename Cipher, typename Padding>
183 const std::array<uint8_t,Cipher::getBlockSize()>&
185 _ctxtVect = _initVect;
186 return _initVect;
187 }
188 // Methods -
189
190} // 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
CBC(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition cbc.hpp:121
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset() override
Definition cbc.hpp:184
virtual ~CBC()
Definition cbc.hpp:132
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 cbc.hpp:141
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, uint64_t, Cipher &cipher) override
Definition cbc.hpp:161
InvalidBlockException(const std::string &message)
Cryptography namespace.
Definition base64.hpp:30