Botan  1.11.34
Crypto and TLS for C++11
ecb.cpp
Go to the documentation of this file.
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/ecb.h>
10 #include <botan/internal/rounding.h>
11 
12 namespace Botan {
13 
15  m_cipher(cipher),
16  m_padding(padding)
17  {
18  if(!m_padding->valid_blocksize(cipher->block_size()))
19  throw Invalid_Argument("Padding " + m_padding->name() +
20  " cannot be used with " +
21  cipher->name() + "/ECB");
22  }
23 
25  {
26  m_cipher->clear();
27  }
28 
30  {
31  // no msg state here
32  return;
33  }
34 
35 std::string ECB_Mode::name() const
36  {
37  return cipher().name() + "/ECB/" + padding().name();
38  }
39 
41  {
42  return cipher().parallel_bytes();
43  }
44 
46  {
47  return cipher().key_spec();
48  }
49 
51  {
52  return 0;
53  }
54 
55 bool ECB_Mode::valid_nonce_length(size_t n) const
56  {
57  return (n == 0);
58  }
59 
60 void ECB_Mode::key_schedule(const byte key[], size_t length)
61  {
62  m_cipher->set_key(key, length);
63  }
64 
65 void ECB_Mode::start_msg(const byte[], size_t nonce_len)
66  {
67  if(nonce_len != 0)
68  throw Invalid_IV_Length(name(), nonce_len);
69  }
70 
72  {
73  return 0;
74  }
75 
76 size_t ECB_Encryption::output_length(size_t input_length) const
77  {
78  if(input_length == 0)
79  return cipher().block_size();
80  else
81  return round_up(input_length, cipher().block_size());
82  }
83 
84 size_t ECB_Encryption::process(uint8_t buf[], size_t sz)
85  {
86  const size_t BS = cipher().block_size();
87  BOTAN_ASSERT(sz % BS == 0, "ECB input is full blocks");
88  const size_t blocks = sz / BS;
89  cipher().encrypt_n(buf, buf, blocks);
90  return sz;
91  }
92 
93 void ECB_Encryption::finish(secure_vector<byte>& buffer, size_t offset)
94  {
95  BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
96  const size_t sz = buffer.size() - offset;
97 
98  const size_t BS = cipher().block_size();
99 
100  const size_t bytes_in_final_block = sz % BS;
101 
102  padding().add_padding(buffer, bytes_in_final_block, BS);
103 
104  if(buffer.size() % BS)
105  throw Exception("Did not pad to full block size in " + name());
106 
107  update(buffer, offset);
108  }
109 
110 size_t ECB_Decryption::output_length(size_t input_length) const
111  {
112  return input_length;
113  }
114 
116  {
117  return cipher().block_size();
118  }
119 
120 size_t ECB_Decryption::process(uint8_t buf[], size_t sz)
121  {
122  const size_t BS = cipher().block_size();
123  BOTAN_ASSERT(sz % BS == 0, "Input is full blocks");
124  size_t blocks = sz / BS;
125  cipher().decrypt_n(buf, buf, blocks);
126  return sz;
127  }
128 
129 void ECB_Decryption::finish(secure_vector<byte>& buffer, size_t offset)
130  {
131  BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane");
132  const size_t sz = buffer.size() - offset;
133 
134  const size_t BS = cipher().block_size();
135 
136  if(sz == 0 || sz % BS)
137  throw Decoding_Error(name() + ": Ciphertext not a multiple of block size");
138 
139  update(buffer, offset);
140 
141  const size_t pad_bytes = BS - padding().unpad(&buffer[buffer.size()-BS], BS);
142  buffer.resize(buffer.size() - pad_bytes); // remove padding
143  }
144 
145 }
void update(secure_vector< byte > &buffer, size_t offset=0)
Definition: cipher_mode.h:81
size_t process(uint8_t buf[], size_t size) override
Definition: ecb.cpp:84
size_t parallel_bytes() const
Definition: block_cipher.h:62
void finish(secure_vector< byte > &final_block, size_t offset=0) override
Definition: ecb.cpp:129
size_t process(uint8_t buf[], size_t size) override
Definition: ecb.cpp:120
ECB_Mode(BlockCipher *cipher, BlockCipherModePaddingMethod *padding)
Definition: ecb.cpp:14
Key_Length_Specification key_spec() const override
Definition: ecb.cpp:45
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
void clear() override
Definition: ecb.cpp:24
size_t minimum_final_size() const override
Definition: ecb.cpp:115
size_t output_length(size_t input_length) const override
Definition: ecb.cpp:110
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
virtual std::string name() const =0
bool valid_nonce_length(size_t n) const override
Definition: ecb.cpp:55
const BlockCipherModePaddingMethod & padding() const
Definition: ecb.h:43
Definition: alg_id.cpp:13
size_t update_granularity() const override
Definition: ecb.cpp:40
size_t minimum_final_size() const override
Definition: ecb.cpp:71
size_t output_length(size_t input_length) const override
Definition: ecb.cpp:76
virtual Key_Length_Specification key_spec() const =0
size_t default_nonce_length() const override
Definition: ecb.cpp:50
virtual void encrypt_n(const byte in[], byte out[], size_t blocks) const =0
void reset() override
Definition: ecb.cpp:29
std::string name() const override
Definition: ecb.cpp:35
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22
virtual void add_padding(secure_vector< byte > &buffer, size_t final_block_bytes, size_t block_size) const =0
virtual void decrypt_n(const byte in[], byte out[], size_t blocks) const =0
virtual std::string name() const =0
virtual size_t block_size() const =0
void finish(secure_vector< byte > &final_block, size_t offset=0) override
Definition: ecb.cpp:93
virtual size_t unpad(const byte block[], size_t size) const =0
const BlockCipher & cipher() const
Definition: ecb.h:41
std::uint8_t byte
Definition: types.h:31