Botan  1.10.9
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 */
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 }
void resize(size_t n)
Definition: secmem.h:211
virtual void clear()=0
void set_iv(const byte iv[], size_t iv_len)
Definition: ofb.cpp:81
std::string name() const
Definition: ofb.cpp:55
void clear()
Definition: ofb.cpp:34
void copy(const T in[], size_t n)
Definition: secmem.h:120
unsigned char byte
Definition: types.h:22
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
void cipher(const byte in[], byte out[], size_t length)
Definition: ofb.cpp:63
size_t size() const
Definition: secmem.h:29
virtual std::string name() const =0
~OFB()
Definition: ofb.cpp:26
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
OFB(BlockCipher *cipher)
Definition: ofb.cpp:17
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
virtual size_t block_size() const =0
bool valid_iv_length(size_t iv_len) const
Definition: ofb.h:26