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