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
12namespace Botan {
13
14/*
15* OFB Constructor
16*/
17OFB::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*/
35 {
36 permutation->clear();
37 zeroise(buffer);
38 position = 0;
39 }
40
41/*
42* Set the key
43*/
44void 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*/
55std::string OFB::name() const
56 {
57 return ("OFB(" + permutation->name() + ")");
58 }
59
60/*
61* CTR-BE Encryption/Decryption
62*/
63void 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*/
81void 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}
OFB(BlockCipher *cipher)
Definition ofb.cpp:17
bool valid_iv_length(size_t iv_len) const
Definition ofb.h:26
void set_iv(const byte iv[], size_t iv_len)
Definition ofb.cpp:81
std::string name() const
Definition ofb.cpp:55
void cipher(const byte in[], byte out[], size_t length)
Definition ofb.cpp:63
void clear()
Definition ofb.cpp:34
void set_key(const SymmetricKey &key)
Definition sym_algo.h:60
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21
Invalid_IV_Length(const std::string &mode, size_t bad_len)
Definition exceptn.h:80