Botan 3.6.1
Crypto and TLS for C&
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/internal/ofb.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan {
14
15OFB::OFB(std::unique_ptr<BlockCipher> cipher) :
16 m_cipher(std::move(cipher)), m_buffer(m_cipher->block_size()), m_buf_pos(0) {}
17
18void OFB::clear() {
19 m_cipher->clear();
20 zeroise(m_buffer);
21 m_buf_pos = 0;
22}
23
25 return m_cipher->has_keying_material();
26}
27
28size_t OFB::buffer_size() const {
29 return m_buffer.size(); // block size
30}
31
32void OFB::key_schedule(std::span<const uint8_t> key) {
33 m_cipher->set_key(key);
34
35 // Set a default all-zeros IV
36 set_iv(nullptr, 0);
37}
38
39std::string OFB::name() const {
40 return fmt("OFB({})", m_cipher->name());
41}
42
43size_t OFB::default_iv_length() const {
44 return m_cipher->block_size();
45}
46
47bool OFB::valid_iv_length(size_t iv_len) const {
48 return (iv_len <= m_cipher->block_size());
49}
50
52 return m_cipher->key_spec();
53}
54
55std::unique_ptr<StreamCipher> OFB::new_object() const {
56 return std::make_unique<OFB>(m_cipher->new_object());
57}
58
59void OFB::cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) {
60 while(length >= m_buffer.size() - m_buf_pos) {
61 xor_buf(out, in, &m_buffer[m_buf_pos], m_buffer.size() - m_buf_pos);
62 length -= (m_buffer.size() - m_buf_pos);
63 in += (m_buffer.size() - m_buf_pos);
64 out += (m_buffer.size() - m_buf_pos);
65 m_cipher->encrypt(m_buffer);
66 m_buf_pos = 0;
67 }
68 xor_buf(out, in, &m_buffer[m_buf_pos], length);
69 m_buf_pos += length;
70}
71
72void OFB::set_iv_bytes(const uint8_t iv[], size_t iv_len) {
73 if(!valid_iv_length(iv_len)) {
74 throw Invalid_IV_Length(name(), iv_len);
75 }
76
77 zeroise(m_buffer);
78 BOTAN_ASSERT_NOMSG(m_buffer.size() >= iv_len);
79 copy_mem(&m_buffer[0], iv, iv_len);
80
81 m_cipher->encrypt(m_buffer);
82 m_buf_pos = 0;
83}
84
85void OFB::seek(uint64_t /*offset*/) {
86 throw Not_Implemented("OFB does not support seeking");
87}
88} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
size_t default_iv_length() const override
Definition ofb.cpp:43
std::string name() const override
Definition ofb.cpp:39
void clear() override
Definition ofb.cpp:18
bool valid_iv_length(size_t iv_len) const override
Definition ofb.cpp:47
size_t buffer_size() const override
Definition ofb.cpp:28
bool has_keying_material() const override
Definition ofb.cpp:24
OFB(std::unique_ptr< BlockCipher > cipher)
Definition ofb.cpp:15
void seek(uint64_t offset) override
Definition ofb.cpp:85
Key_Length_Specification key_spec() const override
Definition ofb.cpp:51
std::unique_ptr< StreamCipher > new_object() const override
Definition ofb.cpp:55
void set_iv(const uint8_t iv[], size_t iv_len)
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:108
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:341
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146