Coverage Report

Created: 2020-05-23 13:54

/src/botan/src/lib/stream/ofb/ofb.cpp
Line
Count
Source (jump to first uncovered line)
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
13
OFB::OFB(BlockCipher* cipher) :
14
   m_cipher(cipher),
15
   m_buffer(m_cipher->block_size()),
16
   m_buf_pos(0)
17
0
   {
18
0
   }
19
20
void OFB::clear()
21
0
   {
22
0
   m_cipher->clear();
23
0
   zeroise(m_buffer);
24
0
   m_buf_pos = 0;
25
0
   }
26
27
void OFB::key_schedule(const uint8_t key[], size_t key_len)
28
0
   {
29
0
   m_cipher->set_key(key, key_len);
30
0
31
0
   // Set a default all-zeros IV
32
0
   set_iv(nullptr, 0);
33
0
   }
34
35
std::string OFB::name() const
36
0
   {
37
0
   return "OFB(" + m_cipher->name() + ")";
38
0
   }
39
40
size_t OFB::default_iv_length() const
41
0
   {
42
0
   return m_cipher->block_size();
43
0
   }
44
45
bool OFB::valid_iv_length(size_t iv_len) const
46
0
   {
47
0
   return (iv_len <= m_cipher->block_size());
48
0
   }
49
50
Key_Length_Specification OFB::key_spec() const
51
0
   {
52
0
   return m_cipher->key_spec();
53
0
   }
54
55
OFB* OFB::clone() const
56
0
   {
57
0
   return new OFB(m_cipher->clone());
58
0
   }
59
60
void OFB::cipher(const uint8_t in[], uint8_t out[], size_t length)
61
0
   {
62
0
   while(length >= m_buffer.size() - m_buf_pos)
63
0
      {
64
0
      xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
65
0
      length -= (m_buffer.size() - m_buf_pos);
66
0
      in += (m_buffer.size() - m_buf_pos);
67
0
      out += (m_buffer.size() - m_buf_pos);
68
0
      m_cipher->encrypt(m_buffer);
69
0
      m_buf_pos = 0;
70
0
      }
71
0
   xor_buf(out, in, &m_buffer[m_buf_pos], length);
72
0
   m_buf_pos += length;
73
0
   }
74
75
void OFB::set_iv(const uint8_t iv[], size_t iv_len)
76
0
   {
77
0
   if(!valid_iv_length(iv_len))
78
0
      throw Invalid_IV_Length(name(), iv_len);
79
0
80
0
   zeroise(m_buffer);
81
0
   buffer_insert(m_buffer, 0, iv, iv_len);
82
0
83
0
   m_cipher->encrypt(m_buffer);
84
0
   m_buf_pos = 0;
85
0
   }
86
87
88
void OFB::seek(uint64_t)
89
0
   {
90
0
   throw Not_Implemented("OFB does not support seeking");
91
0
   }
92
}