Botan  2.9.0
Crypto and TLS for C++11
ofb.cpp
Go to the documentation of this file.
1 /*
2 * OFB Mode
3 * (C) 1999-2007,2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/ofb.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
14  m_cipher(cipher),
15  m_buffer(m_cipher->block_size()),
16  m_buf_pos(0)
17  {
18  }
19 
20 void OFB::clear()
21  {
22  m_cipher->clear();
23  zeroise(m_buffer);
24  m_buf_pos = 0;
25  }
26 
27 void OFB::key_schedule(const uint8_t key[], size_t key_len)
28  {
29  m_cipher->set_key(key, key_len);
30 
31  // Set a default all-zeros IV
32  set_iv(nullptr, 0);
33  }
34 
35 std::string OFB::name() const
36  {
37  return "OFB(" + m_cipher->name() + ")";
38  }
39 
40 size_t OFB::default_iv_length() const
41  {
42  return m_cipher->block_size();
43  }
44 
45 bool OFB::valid_iv_length(size_t iv_len) const
46  {
47  return (iv_len <= m_cipher->block_size());
48  }
49 
51  {
52  return m_cipher->key_spec();
53  }
54 
55 OFB* OFB::clone() const
56  {
57  return new OFB(m_cipher->clone());
58  }
59 
60 void OFB::cipher(const uint8_t in[], uint8_t out[], size_t length)
61  {
62  while(length >= m_buffer.size() - m_buf_pos)
63  {
64  xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
65  length -= (m_buffer.size() - m_buf_pos);
66  in += (m_buffer.size() - m_buf_pos);
67  out += (m_buffer.size() - m_buf_pos);
68  m_cipher->encrypt(m_buffer);
69  m_buf_pos = 0;
70  }
71  xor_buf(out, in, &m_buffer[m_buf_pos], length);
72  m_buf_pos += length;
73  }
74 
75 void OFB::set_iv(const uint8_t iv[], size_t iv_len)
76  {
77  if(!valid_iv_length(iv_len))
78  throw Invalid_IV_Length(name(), iv_len);
79 
80  zeroise(m_buffer);
81  buffer_insert(m_buffer, 0, iv, iv_len);
82 
83  m_cipher->encrypt(m_buffer);
84  m_buf_pos = 0;
85  }
86 
87 
88 void OFB::seek(uint64_t)
89  {
90  throw Not_Implemented("OFB does not support seeking");
91  }
92 }
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: ofb.cpp:75
size_t default_iv_length() const override
Definition: ofb.cpp:40
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: ofb.cpp:60
void clear() override
Definition: ofb.cpp:20
Definition: ofb.h:19
Key_Length_Specification key_spec() const override
Definition: ofb.cpp:50
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:203
Definition: alg_id.cpp:13
bool valid_iv_length(size_t iv_len) const override
Definition: ofb.cpp:45
std::string name() const override
Definition: ofb.cpp:35
void seek(uint64_t offset) override
Definition: ofb.cpp:88
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:80
OFB * clone() const override
Definition: ofb.cpp:55
OFB(BlockCipher *cipher)
Definition: ofb.cpp:13
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:160