1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*************************************************
* ECB Mode Source File *
* (C) 1999-2008 The Botan Project *
*************************************************/
#include <botan/ecb.h>
#include <botan/lookup.h>
namespace Botan {
/*************************************************
* ECB Constructor *
*************************************************/
ECB::ECB(const std::string& cipher_name, const std::string& padding_name) :
BlockCipherMode(cipher_name, "ECB", 0), padder(get_bc_pad(padding_name))
{
}
/*************************************************
* Verify the IV is not set *
*************************************************/
bool ECB::valid_iv_size(u32bit iv_size) const
{
if(iv_size == 0)
return true;
return false;
}
/*************************************************
* Return an ECB mode name *
*************************************************/
std::string ECB::name() const
{
return (cipher->name() + "/" + mode_name + "/" + padder->name());
}
/*************************************************
* ECB Encryption Constructor *
*************************************************/
ECB_Encryption::ECB_Encryption(const std::string& cipher_name,
const std::string& padding_name) :
ECB(cipher_name, padding_name)
{
}
/*************************************************
* ECB Encryption Constructor *
*************************************************/
ECB_Encryption::ECB_Encryption(const std::string& cipher_name,
const std::string& padding_name,
const SymmetricKey& key) :
ECB(cipher_name, padding_name)
{
set_key(key);
}
/*************************************************
* Encrypt in ECB mode *
*************************************************/
void ECB_Encryption::write(const byte input[], u32bit length)
{
buffer.copy(position, input, length);
if(position + length >= BLOCK_SIZE)
{
cipher->encrypt(buffer);
send(buffer, BLOCK_SIZE);
input += (BLOCK_SIZE - position);
length -= (BLOCK_SIZE - position);
while(length >= BLOCK_SIZE)
{
cipher->encrypt(input, buffer);
send(buffer, BLOCK_SIZE);
input += BLOCK_SIZE;
length -= BLOCK_SIZE;
}
buffer.copy(input, length);
position = 0;
}
position += length;
}
/*************************************************
* Finish encrypting in ECB mode *
*************************************************/
void ECB_Encryption::end_msg()
{
SecureVector<byte> padding(BLOCK_SIZE);
padder->pad(padding, padding.size(), position);
write(padding, padder->pad_bytes(BLOCK_SIZE, position));
if(position != 0)
throw Encoding_Error(name() + ": Did not pad to full blocksize");
}
/*************************************************
* ECB Decryption Constructor *
*************************************************/
ECB_Decryption::ECB_Decryption(const std::string& cipher_name,
const std::string& padding_name) :
ECB(cipher_name, padding_name)
{
}
/*************************************************
* ECB Decryption Constructor *
*************************************************/
ECB_Decryption::ECB_Decryption(const std::string& cipher_name,
const std::string& padding_name,
const SymmetricKey& key) :
ECB(cipher_name, padding_name)
{
set_key(key);
}
/*************************************************
* Decrypt in ECB mode *
*************************************************/
void ECB_Decryption::write(const byte input[], u32bit length)
{
buffer.copy(position, input, length);
if(position + length > BLOCK_SIZE)
{
cipher->decrypt(buffer);
send(buffer, BLOCK_SIZE);
input += (BLOCK_SIZE - position);
length -= (BLOCK_SIZE - position);
while(length > BLOCK_SIZE)
{
cipher->decrypt(input, buffer);
send(buffer, BLOCK_SIZE);
input += BLOCK_SIZE;
length -= BLOCK_SIZE;
}
buffer.copy(input, length);
position = 0;
}
position += length;
}
/*************************************************
* Finish decrypting in ECB mode *
*************************************************/
void ECB_Decryption::end_msg()
{
if(position != BLOCK_SIZE)
throw Decoding_Error(name());
cipher->decrypt(buffer);
send(buffer, padder->unpad(buffer, BLOCK_SIZE));
state = buffer;
position = 0;
}
}
|