Botan 1.10.17

ofb.cpp
Go to the documentation of this file.
1 /*
2 * OFB Mode
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/ofb.h>
9 #include <botan/internal/xor_buf.h>
10 #include <algorithm>
11
12 namespace Botan {
13
14 /*
15 * OFB Constructor
16 */
17 OFB::OFB(BlockCipher* ciph) : permutation(ciph)
18 {
19 position = 0;
20 buffer.resize(permutation->block_size());
21 }
22
23 /*
24 * OFB Destructor
25 */
26 OFB::~OFB()
27 {
28 delete permutation;
29 }
30
31 /*
32 * Zeroize
33 */
34 void OFB::clear()
35 {
36 permutation->clear();
37 zeroise(buffer);
38 position = 0;
39 }
40
41 /*
42 * Set the key
43 */
44 void OFB::key_schedule(const byte key[], size_t key_len)
45 {
46 permutation->set_key(key, key_len);
47
48 // Set a default all-zeros IV
49 set_iv(0, 0);
50 }
51
52 /*
53 * Return the name of this type
54 */
55 std::string OFB::name() const
56 {
57 return ("OFB(" + permutation->name() + ")");
58 }
59
60 /*
61 * CTR-BE Encryption/Decryption
62 */
63 void OFB::cipher(const byte in[], byte out[], size_t length)
64 {
65 while(length >= buffer.size() - position)
66 {
67 xor_buf(out, in, &buffer[position], buffer.size() - position);
68 length -= (buffer.size() - position);
69 in += (buffer.size() - position);
70 out += (buffer.size() - position);
71 permutation->encrypt(buffer);
72 position = 0;
73 }
74 xor_buf(out, in, &buffer[position], length);
75 position += length;
76 }
77
78 /*
79 * Set CTR-BE IV
80 */
81 void OFB::set_iv(const byte iv[], size_t iv_len)
82 {
83 if(!valid_iv_length(iv_len))
84 throw Invalid_IV_Length(name(), iv_len);
85
86 zeroise(buffer);
87 buffer.copy(0, iv, iv_len);
88
89 permutation->encrypt(buffer);
90 position = 0;
91 }
92
93 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::Algorithm::clear
virtual void clear()=0
Botan::OFB::set_iv
void set_iv(const byte iv[], size_t iv_len)
Definition: ofb.cpp:81
Botan::OFB::name
std::string name() const
Definition: ofb.cpp:55
Botan::OFB::clear
void clear()
Definition: ofb.cpp:34
Botan::Invalid_IV_Length
Definition: exceptn.h:78
Botan::OFB::valid_iv_length
bool valid_iv_length(size_t iv_len) const
Definition: ofb.h:26
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:126
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::BlockCipher::encrypt
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::OFB::cipher
void cipher(const byte in[], byte out[], size_t length)
Definition: ofb.cpp:63
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::OFB::~OFB
~OFB()
Definition: ofb.cpp:26
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::OFB::OFB
OFB(BlockCipher *cipher)
Definition: ofb.cpp:17
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::BlockCipher
Definition: block_cipher.h:18