monotone

monotone Mtn Source Tree

Root/botan/ecb.cpp

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

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status