Botan 1.10.17
ecb.cpp
Go to the documentation of this file.
1/*
2* ECB Mode
3* (C) 1999-2009 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/ecb.h>
9
10namespace Botan {
11
12/*
13* ECB_Encryption Constructor
14*/
17 Buffered_Filter(ciph->parallel_bytes(), 0)
18 {
19 cipher = ciph;
20 padder = pad;
21
22 temp.resize(buffered_block_size());
23 }
24
25/*
26* ECB_Encryption Constructor
27*/
30 const SymmetricKey& key) :
31 Buffered_Filter(ciph->parallel_bytes(), 0)
32 {
33 cipher = ciph;
34 padder = pad;
35
36 temp.resize(buffered_block_size());
37
38 cipher->set_key(key);
39 }
40
41/*
42* ECB_Encryption Destructor
43*/
45 {
46 delete cipher;
47 delete padder;
48 }
49
50/*
51* Return an ECB mode name
52*/
53std::string ECB_Encryption::name() const
54 {
55 return (cipher->name() + "/ECB/" + padder->name());
56 }
57
58/*
59* Encrypt in ECB mode
60*/
61void ECB_Encryption::write(const byte input[], size_t length)
62 {
63 Buffered_Filter::write(input, length);
64 }
65
66/*
67* Finish encrypting in ECB mode
68*/
70 {
71 size_t last_block = current_position() % cipher->block_size();
72
73 SecureVector<byte> padding(cipher->block_size());
74 padder->pad(padding, padding.size(), last_block);
75
76 size_t pad_bytes = padder->pad_bytes(cipher->block_size(), last_block);
77
78 if(pad_bytes)
79 Buffered_Filter::write(padding, pad_bytes);
81 }
82
83void ECB_Encryption::buffered_block(const byte input[], size_t input_length)
84 {
85 const size_t blocks_in_temp = temp.size() / cipher->block_size();
86 size_t blocks = input_length / cipher->block_size();
87
88 while(blocks)
89 {
90 size_t to_proc = std::min(blocks, blocks_in_temp);
91
92 cipher->encrypt_n(input, &temp[0], to_proc);
93
94 send(temp, to_proc * cipher->block_size());
95
96 input += to_proc * cipher->block_size();
97 blocks -= to_proc;
98 }
99 }
100
101void ECB_Encryption::buffered_final(const byte input[], size_t input_length)
102 {
103 if(input_length % cipher->block_size() == 0)
104 buffered_block(input, input_length);
105 else if(input_length != 0)
106 throw Encoding_Error(name() + ": Did not pad to full blocksize");
107 }
108
109/*
110* ECB_Decryption Constructor
111*/
114 Buffered_Filter(ciph->parallel_bytes(), 1)
115 {
116 cipher = ciph;
117 padder = pad;
118
119 temp.resize(buffered_block_size());
120 }
121
122/*
123* ECB_Decryption Constructor
124*/
127 const SymmetricKey& key) :
128 Buffered_Filter(ciph->parallel_bytes(), 1)
129 {
130 cipher = ciph;
131 padder = pad;
132
133 temp.resize(buffered_block_size());
134
135 cipher->set_key(key);
136 }
137
138/*
139* ECB_Decryption Destructor
140*/
142 {
143 delete cipher;
144 delete padder;
145 }
146
147/*
148* Return an ECB mode name
149*/
150std::string ECB_Decryption::name() const
151 {
152 return (cipher->name() + "/ECB/" + padder->name());
153 }
154
155/*
156* Decrypt in ECB mode
157*/
158void ECB_Decryption::write(const byte input[], size_t length)
159 {
160 Buffered_Filter::write(input, length);
161 }
162
163/*
164* Finish decrypting in ECB mode
165*/
167 {
169 }
170
171/*
172* Decrypt in ECB mode
173*/
174void ECB_Decryption::buffered_block(const byte input[], size_t length)
175 {
176 const size_t blocks_in_temp = temp.size() / cipher->block_size();
177 size_t blocks = length / cipher->block_size();
178
179 while(blocks)
180 {
181 size_t to_proc = std::min(blocks, blocks_in_temp);
182
183 cipher->decrypt_n(input, &temp[0], to_proc);
184
185 send(temp, to_proc * cipher->block_size());
186
187 input += to_proc * cipher->block_size();
188 blocks -= to_proc;
189 }
190 }
191
192/*
193* Finish encrypting in ECB mode
194*/
195void ECB_Decryption::buffered_final(const byte input[], size_t length)
196 {
197 if(length == 0 || length % cipher->block_size() != 0)
198 throw Decoding_Error(name() + ": Ciphertext not multiple of block size");
199
200 size_t extra_blocks = (length - 1) / cipher->block_size();
201
202 buffered_block(input, extra_blocks * cipher->block_size());
203
204 input += extra_blocks * cipher->block_size();
205
206 cipher->decrypt(input, temp);
207 send(temp, padder->unpad(temp, cipher->block_size()));
208 }
209
210}
virtual size_t pad_bytes(size_t block_size, size_t position) const
Definition mode_pad.cpp:17
virtual void pad(byte block[], size_t size, size_t current_position) const =0
virtual size_t block_size() const =0
size_t current_position() const
Definition buf_filt.h:72
Buffered_Filter(size_t block_size, size_t final_minimum)
Definition buf_filt.cpp:18
void write(const byte in[], size_t length)
Definition buf_filt.cpp:34
size_t buffered_block_size() const
Definition buf_filt.h:67
ECB_Decryption(BlockCipher *ciph, BlockCipherModePaddingMethod *pad)
Definition ecb.cpp:112
std::string name() const
Definition ecb.cpp:150
std::string name() const
Definition ecb.cpp:53
ECB_Encryption(BlockCipher *ciph, BlockCipherModePaddingMethod *pad)
Definition ecb.cpp:15
void send(const byte in[], size_t length)
Definition filter.cpp:28
OctetString SymmetricKey
Definition symkey.h:147
Decoding_Error(const std::string &name)
Definition exceptn.h:140
Encoding_Error(const std::string &name)
Definition exceptn.h:131