Debian Codegraph @

src/lib/crypto/Botan_ecb.cpp

raw
1 /*
2 * ECB Mode
3 * (C) 1999-2009,2013 Jack Lloyd
4 * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9 #include <botan/version.h>
10
11 // ECB cipher mode was dropped in Botan 2.0.0
12 // so including this code in SoftHSM for continued support
13 // for e.g. CKA_VALUE_CHECK
14
15 #include "Botan_ecb.h"
16
17 namespace Botan {
18
19 ECB_Mode::ECB_Mode(BlockCipher* cipher, bool with_pkcs7_padding) :
20 m_cipher(cipher),
21 m_with_pkcs7_padding(with_pkcs7_padding)
22 {
23 }
24
25 void ECB_Mode::clear()
26 {
27 m_cipher->clear();
28 }
29
30 void ECB_Mode::reset()
31 {
32 // no msg state here
33 return;
34 }
35
36 std::string ECB_Mode::name() const
37 {
38 std::string name = cipher().name();
39 name += "/ECB/";
40 if(m_with_pkcs7_padding)
41 name += "PKCS7";
42 else
43 name += "NoPadding";
44 return name;
45 }
46
47 size_t ECB_Mode::update_granularity() const
48 {
49 return cipher().parallel_bytes();
50 }
51
52 Key_Length_Specification ECB_Mode::key_spec() const
53 {
54 return cipher().key_spec();
55 }
56
57 size_t ECB_Mode::default_nonce_length() const
58 {
59 return 0;
60 }
61
62 bool ECB_Mode::valid_nonce_length(size_t n) const
63 {
64 return (n == 0);
65 }
66
67 void ECB_Mode::key_schedule(const byte key[], size_t length)
68 {
69 m_cipher->set_key(key, length);
70 }
71
72 void ECB_Mode::start_msg(const byte[], size_t nonce_len)
73 {
74 if(nonce_len != 0)
75 throw Invalid_IV_Length(name(), nonce_len);
76 }
77
78 size_t ECB_Encryption::minimum_final_size() const
79 {
80 return 0;
81 }
82
83 namespace {
84
85 inline size_t round_up(size_t n, size_t align_to)
86 {
87 BOTAN_ASSERT(align_to != 0, "align_to must not be 0");
88
89 if(n % align_to)
90 n += align_to - (n % align_to);
91 return n;
92 }
93
94 }
95
96 size_t ECB_Encryption::output_length(size_t input_length) const
97 {
98 if(input_length == 0)
99 return cipher().block_size();
100 else
101 return round_up(input_length, cipher().block_size());
102 }
103
104 size_t ECB_Encryption::process(uint8_t buf[], size_t sz)
105 {
106 const size_t BS = cipher().block_size();
107 BOTAN_ASSERT(sz % BS == 0, "ECB input is full blocks");
108 const size_t blocks = sz / BS;
109 cipher().encrypt_n(buf, buf, blocks);
110 return sz;
111 }
112
113 void ECB_Encryption::finish(secure_vector<byte>& buffer, size_t offset)
114 {
115 BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
116 const size_t sz = buffer.size() - offset;
117
118 const size_t BS = cipher().block_size();
119
120 const size_t bytes_in_final_block = sz % BS;
121
122 if(with_pkcs7_padding())
123 {
124 const uint8_t pad_value = static_cast<uint8_t>(BS - bytes_in_final_block);
125
126 for(size_t i = 0; i != pad_value; ++i)
127 buffer.push_back(pad_value);
128 }
129
130 if(buffer.size() % BS)
131 throw Encoding_Error("Did not pad to full block size in " + name());
132
133 update(buffer, offset);
134 }
135
136 size_t ECB_Decryption::output_length(size_t input_length) const
137 {
138 return input_length;
139 }
140
141 size_t ECB_Decryption::minimum_final_size() const
142 {
143 return cipher().block_size();
144 }
145
146 size_t ECB_Decryption::process(uint8_t buf[], size_t sz)
147 {
148 const size_t BS = cipher().block_size();
149 BOTAN_ASSERT(sz % BS == 0, "Input is full blocks");
150 size_t blocks = sz / BS;
151 cipher().decrypt_n(buf, buf, blocks);
152 return sz;
153 }
154
155 namespace {
156
157 size_t pkcs7_unpad(const byte block[], size_t size)
158 {
159 size_t position = block[size-1];
160
161 if(position > size)
162 throw Decoding_Error("Bad PKCS7 padding");
163
164 for(size_t j = size-position; j != size-1; ++j)
165 if(block[j] != position)
166 throw Decoding_Error("Bad PKCS7 padding");
167
168 return (size-position);
169 }
170
171 }
172
173 void ECB_Decryption::finish(secure_vector<byte>& buffer, size_t offset)
174 {
175 BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
176 const size_t sz = buffer.size() - offset;
177
178 const size_t BS = cipher().block_size();
179
180 if(sz == 0 || sz % BS)
181 throw Decoding_Error(name() + ": Ciphertext not a multiple of block size");
182
183 update(buffer, offset);
184
185 if(with_pkcs7_padding())
186 {
187 const size_t pad_bytes = BS - pkcs7_unpad(&buffer[buffer.size()-BS], BS);
188 buffer.resize(buffer.size() - pad_bytes); // remove padding
189 }
190 }
191
192 }