Botan  2.7.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 void OFB::cipher(const uint8_t in[], uint8_t out[], size_t length)
41  {
42  while(length >= m_buffer.size() - m_buf_pos)
43  {
44  xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
45  length -= (m_buffer.size() - m_buf_pos);
46  in += (m_buffer.size() - m_buf_pos);
47  out += (m_buffer.size() - m_buf_pos);
48  m_cipher->encrypt(m_buffer);
49  m_buf_pos = 0;
50  }
51  xor_buf(out, in, &m_buffer[m_buf_pos], length);
52  m_buf_pos += length;
53  }
54 
55 void OFB::set_iv(const uint8_t iv[], size_t iv_len)
56  {
57  if(!valid_iv_length(iv_len))
58  throw Invalid_IV_Length(name(), iv_len);
59 
60  zeroise(m_buffer);
61  buffer_insert(m_buffer, 0, iv, iv_len);
62 
63  m_cipher->encrypt(m_buffer);
64  m_buf_pos = 0;
65  }
66 
67 
68 void OFB::seek(uint64_t)
69  {
70  throw Not_Implemented("OFB does not support seeking");
71  }
72 }
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: ofb.cpp:55
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: ofb.cpp:40
void clear() override
Definition: ofb.cpp:20
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
Definition: alg_id.cpp:13
bool valid_iv_length(size_t iv_len) const override
Definition: ofb.h:26
std::string name() const override
Definition: ofb.cpp:35
void seek(uint64_t offset) override
Definition: ofb.cpp:68
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:103
OFB(BlockCipher *cipher)
Definition: ofb.cpp:13
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183