Botan  2.6.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 
10 namespace Botan {
11 
13  m_cipher(cipher),
14  m_buffer(m_cipher->block_size()),
15  m_buf_pos(0)
16  {
17  }
18 
19 void OFB::clear()
20  {
21  m_cipher->clear();
22  zeroise(m_buffer);
23  m_buf_pos = 0;
24  }
25 
26 void OFB::key_schedule(const uint8_t key[], size_t key_len)
27  {
28  m_cipher->set_key(key, key_len);
29 
30  // Set a default all-zeros IV
31  set_iv(nullptr, 0);
32  }
33 
34 std::string OFB::name() const
35  {
36  return "OFB(" + m_cipher->name() + ")";
37  }
38 
39 void OFB::cipher(const uint8_t in[], uint8_t out[], size_t length)
40  {
41  while(length >= m_buffer.size() - m_buf_pos)
42  {
43  xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
44  length -= (m_buffer.size() - m_buf_pos);
45  in += (m_buffer.size() - m_buf_pos);
46  out += (m_buffer.size() - m_buf_pos);
47  m_cipher->encrypt(m_buffer);
48  m_buf_pos = 0;
49  }
50  xor_buf(out, in, &m_buffer[m_buf_pos], length);
51  m_buf_pos += length;
52  }
53 
54 void OFB::set_iv(const uint8_t iv[], size_t iv_len)
55  {
56  if(!valid_iv_length(iv_len))
57  throw Invalid_IV_Length(name(), iv_len);
58 
59  zeroise(m_buffer);
60  buffer_insert(m_buffer, 0, iv, iv_len);
61 
62  m_cipher->encrypt(m_buffer);
63  m_buf_pos = 0;
64  }
65 
66 
67 void OFB::seek(uint64_t)
68  {
69  throw Not_Implemented("OFB does not support seeking");
70  }
71 }
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: ofb.cpp:54
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: ofb.cpp:39
void clear() override
Definition: ofb.cpp:19
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:34
void seek(uint64_t offset) override
Definition: ofb.cpp:67
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:12
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183