aboutsummaryrefslogtreecommitdiffstats
path: root/src/modes/ctr/ctr.cpp
blob: a3476c4742d7ffb3a0a5a6a686b82ab783ae36f5 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* CTR Mode
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

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

namespace Botan {

namespace {

const u32bit PARALLEL_BLOCKS = BOTAN_PARALLEL_BLOCKS_CTR;

}

/*
* CTR-BE Constructor
*/
CTR_BE::CTR_BE(BlockCipher* ciph) : cipher(ciph)
   {
   position = 0;

   counter.create(ciph->BLOCK_SIZE * PARALLEL_BLOCKS);
   enc_buffer.create(ciph->BLOCK_SIZE * PARALLEL_BLOCKS);
   }

/*
* CTR-BE Constructor
*/
CTR_BE::CTR_BE(BlockCipher* ciph, const SymmetricKey& key,
               const InitializationVector& iv) :
   cipher(ciph)
   {
   position = 0;

   counter.create(ciph->BLOCK_SIZE * PARALLEL_BLOCKS);
   enc_buffer.create(ciph->BLOCK_SIZE * PARALLEL_BLOCKS);

   cipher->set_key(key);
   set_iv(iv);
   }

/*
* CTR_BE Destructor
*/
CTR_BE::~CTR_BE()
   {
   delete cipher;
   }

/*
* Return the name of this type
*/
std::string CTR_BE::name() const
   {
   return ("CTR-BE/" + cipher->name());
   }

/*
* Set CTR-BE IV
*/
void CTR_BE::set_iv(const InitializationVector& iv)
   {
   if(iv.length() != cipher->BLOCK_SIZE)
      throw Invalid_IV_Length(name(), iv.length());

   enc_buffer.clear();
   position = 0;

   for(u32bit i = 0; i != PARALLEL_BLOCKS; ++i)
      {
      counter.copy(i*cipher->BLOCK_SIZE, iv.begin(), iv.length());

      // FIXME: this is stupid
      for(u32bit j = 0; j != i; ++j)
         for(s32bit k = cipher->BLOCK_SIZE - 1; k >= 0; --k)
            if(++counter[i*cipher->BLOCK_SIZE+k])
               break;
      }

   cipher->encrypt_n(counter, enc_buffer, PARALLEL_BLOCKS);
   }

/*
* CTR-BE Encryption/Decryption
*/
void CTR_BE::write(const byte input[], u32bit length)
   {
   u32bit copied = std::min(enc_buffer.size() - position, length);
   xor_buf(enc_buffer + position, input, copied);
   send(enc_buffer + position, copied);
   input += copied;
   length -= copied;
   position += copied;

   if(position == enc_buffer.size())
      increment_counter();

   while(length >= enc_buffer.size())
      {
      xor_buf(enc_buffer, input, enc_buffer.size());
      send(enc_buffer, enc_buffer.size());

      input += enc_buffer.size();
      length -= enc_buffer.size();
      increment_counter();
      }

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

/*
* Increment the counter and update the buffer
*/
void CTR_BE::increment_counter()
   {
   for(u32bit i = 0; i != PARALLEL_BLOCKS; ++i)
      {
      // FIXME: Can do it in a single loop
      /*
      for(u32bit j = 1; j != cipher->BLOCK_SIZE; ++j)
         {
         byte carry = 0;
         byte z = counter[(i+1)*cipher->BLOCK_SIZE-1] + PARALLEL_BLOCKS;

      if(
      */
      for(u32bit j = 0; j != PARALLEL_BLOCKS; ++j)
         for(s32bit k = cipher->BLOCK_SIZE - 1; k >= 0; --k)
            if(++counter[i*cipher->BLOCK_SIZE+k])
               break;
      }

   cipher->encrypt_n(counter, enc_buffer, PARALLEL_BLOCKS);

   position = 0;
   }

}