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
10 namespace Botan {
11
12 /*
13 * ECB_Encryption Constructor
14 */
15 ECB_Encryption::ECB_Encryption(BlockCipher* ciph,
16 BlockCipherModePaddingMethod* pad) :
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 */
28 ECB_Encryption::ECB_Encryption(BlockCipher* ciph,
29 BlockCipherModePaddingMethod* pad,
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 */
44 ECB_Encryption::~ECB_Encryption()
45 {
46 delete cipher;
47 delete padder;
48 }
49
50 /*
51 * Return an ECB mode name
52 */
53 std::string ECB_Encryption::name() const
54 {
55 return (cipher->name() + "/ECB/" + padder->name());
56 }
57
58 /*
59 * Encrypt in ECB mode
60 */
61 void 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 */
69 void ECB_Encryption::end_msg()
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);
80 Buffered_Filter::end_msg();
81 }
82
83 void 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
101 void 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 */
112 ECB_Decryption::ECB_Decryption(BlockCipher* ciph,
113 BlockCipherModePaddingMethod* pad) :
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 */
125 ECB_Decryption::ECB_Decryption(BlockCipher* ciph,
126 BlockCipherModePaddingMethod* pad,
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 */
141 ECB_Decryption::~ECB_Decryption()
142 {
143 delete cipher;
144 delete padder;
145 }
146
147 /*
148 * Return an ECB mode name
149 */
150 std::string ECB_Decryption::name() const
151 {
152 return (cipher->name() + "/ECB/" + padder->name());
153 }
154
155 /*
156 * Decrypt in ECB mode
157 */
158 void 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 */
166 void ECB_Decryption::end_msg()
167 {
168 Buffered_Filter::end_msg();
169 }
170
171 /*
172 * Decrypt in ECB mode
173 */
174 void 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 */
195 void 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 }
Botan::Buffered_Filter
Definition: buf_filt.h:19
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::OctetString
Definition: symkey.h:19
Botan::ECB_Decryption::~ECB_Decryption
~ECB_Decryption()
Definition: ecb.cpp:141
Botan::Buffered_Filter::end_msg
void end_msg()
Definition: buf_filt.cpp:82
Botan::BlockCipherModePaddingMethod::pad_bytes
virtual size_t pad_bytes(size_t block_size, size_t position) const
Definition: mode_pad.cpp:17
Botan::Buffered_Filter::write
void write(const byte in[], size_t length)
Definition: buf_filt.cpp:34
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::BlockCipher::decrypt
void decrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:57
Botan::Filter::send
void send(const byte in[], size_t length)
Definition: filter.cpp:28
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::Buffered_Filter::current_position
size_t current_position() const
Definition: buf_filt.h:72
Botan::ECB_Encryption::~ECB_Encryption
~ECB_Encryption()
Definition: ecb.cpp:44
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::BlockCipherModePaddingMethod
Definition: mode_pad.h:25
Botan::ECB_Encryption::ECB_Encryption
ECB_Encryption(BlockCipher *ciph, BlockCipherModePaddingMethod *pad)
Definition: ecb.cpp:15
Botan::ECB_Decryption::ECB_Decryption
ECB_Decryption(BlockCipher *ciph, BlockCipherModePaddingMethod *pad)
Definition: ecb.cpp:112
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::ECB_Encryption::name
std::string name() const
Definition: ecb.cpp:53
Botan::Buffered_Filter::buffered_block_size
size_t buffered_block_size() const
Definition: buf_filt.h:67
Botan::ECB_Decryption::name
std::string name() const
Definition: ecb.cpp:150
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::BlockCipher::encrypt_n
virtual void encrypt_n(const byte in[], byte out[], size_t blocks) const =0
Botan::BlockCipher::decrypt_n
virtual void decrypt_n(const byte in[], byte out[], size_t blocks) const =0
Botan::BlockCipherModePaddingMethod::name
virtual std::string name() const =0
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::BlockCipherModePaddingMethod::unpad
virtual size_t unpad(const byte block[], size_t size) const =0
Botan::BlockCipherModePaddingMethod::pad
virtual void pad(byte block[], size_t size, size_t current_position) const =0
Botan::BlockCipher
Definition: block_cipher.h:18