AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
pcbc.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#include <array>
27
28
29namespace anch::crypto {
30
40 template<typename Cipher, typename Padding>
41 class ANCH_CRYPTO_EXPORT PCBC: public BlockCipherModeOfOperation<PCBC<Cipher,Padding>,Cipher> {
42
43 // Attributes +
44 private:
46 std::array<uint8_t,Cipher::getBlockSize()> _initVect;
47
49 std::array<uint8_t,Cipher::getBlockSize()> _ctxtVect;
50 // Attributes -
51
52
53 // Constructors +
54 public:
62 PCBC(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread = 1);
63 // Constructors -
64
65
66 // Destructor +
70 virtual ~PCBC();
71 // Destructor -
72
73
74 // Methods +
75 protected:
87 virtual std::size_t cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
88 std::streamsize nbRead,
89 std::array<uint8_t,Cipher::getBlockSize()>& output,
90 uint64_t, Cipher& cipher) override;
91
104 virtual std::size_t decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
105 std::array<uint8_t,Cipher::getBlockSize()>&,
106 std::streamsize nbRead,
107 bool lastBlock,
108 std::array<uint8_t,Cipher::getBlockSize()>& output,
109 uint64_t, Cipher& cipher) override;
110
116 virtual const std::array<uint8_t,Cipher::getBlockSize()>& reset() override;
117 // Methods -
118
119 };
120
121 // Constructors +
122 template<typename Cipher, typename Padding>
123 PCBC<Cipher,Padding>::PCBC(const std::array<uint8_t,Cipher::getBlockSize()>& initVect, unsigned int nbThread):
124 BlockCipherModeOfOperation<PCBC<Cipher,Padding>,Cipher>(false, false, nbThread),
125 _initVect(initVect),
126 _ctxtVect() {
127 // Nothing to do
128 }
129 // Constructors -
130
131
132 // Destructor +
133 template<typename Cipher, typename Padding>
135 // Nothing to do
136 }
137 // Destructor -
138
139
140 // Methods +
141 template<typename Cipher, typename Padding>
142 std::size_t
143 PCBC<Cipher,Padding>::cipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
144 std::streamsize nbRead,
145 std::array<uint8_t,Cipher::getBlockSize()>& output,
146 uint64_t, Cipher& cipher) {
147 if(static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
148 Padding::pad(input.data(), static_cast<std::size_t>(nbRead), Cipher::getBlockSize());
149 }
150 std::array<uint8_t,Cipher::getBlockSize()> data;
151 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
152 data[i] = input[i] ^ _ctxtVect[i];
153 }
154 cipher.cipher(data, output);
155 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
156 _ctxtVect[i] = input[i] ^ output[i];
157 }
158 return Cipher::getBlockSize(); // This mode pad data => the number of bytes to write will always be a complete block
159 }
160
161 template<typename Cipher, typename Padding>
162 std::size_t
163 PCBC<Cipher,Padding>::decipherBlock(std::array<uint8_t,Cipher::getBlockSize()>& input,
164 std::array<uint8_t,Cipher::getBlockSize()>&,
165 std::streamsize nbRead,
166 bool lastBlock,
167 std::array<uint8_t,Cipher::getBlockSize()>& output,
168 uint64_t, Cipher& cipher) {
169 if(lastBlock && static_cast<std::size_t>(nbRead) != Cipher::getBlockSize()) {
170 throw InvalidBlockException("Invalid block size");
171 }
172 std::array<uint8_t,Cipher::getBlockSize()> data;
173 cipher.decipher(input, data);
174 for(std::size_t i = 0 ; i < Cipher::getBlockSize() ; ++i) {
175 output[i] = data[i] ^ _ctxtVect[i];
176 _ctxtVect[i] = input[i] ^ output[i];
177 }
178 if(lastBlock) {
179 return Padding::length(output.data(), Cipher::getBlockSize());
180 } else {
181 return Cipher::getBlockSize();
182 }
183 }
184
185 template<typename Cipher, typename Padding>
186 const std::array<uint8_t,Cipher::getBlockSize()>&
188 _ctxtVect = _initVect;
189 return _initVect;
190 }
191 // Methods -
192
193} // 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
InvalidBlockException(const std::string &message)
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 pcbc.hpp:143
virtual const std::array< uint8_t, Cipher::getBlockSize()> & reset() override
Definition pcbc.hpp:187
virtual ~PCBC()
Definition pcbc.hpp:134
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 pcbc.hpp:163
PCBC(const std::array< uint8_t, Cipher::getBlockSize()> &initVect, unsigned int nbThread=1)
Definition pcbc.hpp:123
Cryptography namespace.
Definition base64.hpp:30