summaryrefslogtreecommitdiffstats
path: root/botan/src/modes/ofb/ofb.cpp
blob: cb40fdeaa23fa37f20e9da4a90a0e4e4c15059c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* OFB Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/ofb.h>
#include <botan/xor_buf.h>
#include <algorithm>

namespace Botan {

/*
* OFB Constructor
*/
OFB::OFB(BlockCipher* ciph) :
   BlockCipherMode(ciph, "OFB", ciph->BLOCK_SIZE, 2)
   {
   }

/*
* OFB Constructor
*/
OFB::OFB(BlockCipher* ciph, const SymmetricKey& key,
         const InitializationVector& iv) :
   BlockCipherMode(ciph, "OFB", ciph->BLOCK_SIZE, 2)
   {
   set_key(key);
   set_iv(iv);
   }

/*
* OFB Encryption/Decryption
*/
void OFB::write(const byte input[], u32bit length)
   {
   u32bit copied = std::min(BLOCK_SIZE - position, length);
   xor_buf(buffer, input, state + position, copied);
   send(buffer, copied);
   input += copied;
   length -= copied;
   position += copied;

   if(position == BLOCK_SIZE)
      {
      cipher->encrypt(state);
      position = 0;
      }

   while(length >= BLOCK_SIZE)
      {
      xor_buf(buffer, input, state, BLOCK_SIZE);
      send(buffer, BLOCK_SIZE);

      input += BLOCK_SIZE;
      length -= BLOCK_SIZE;
      cipher->encrypt(state);
      }

   xor_buf(buffer, input, state + position, length);
   send(buffer, length);
   position += length;
   }

}