Botan  1.10.9
ctr.cpp
Go to the documentation of this file.
1 /*
2 * Counter mode
3 * (C) 1999-2011 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/ctr.h>
9 #include <botan/internal/xor_buf.h>
10 
11 namespace Botan {
12 
13 /*
14 * CTR-BE Constructor
15 */
16 
18  permutation(ciph),
19  counter(256 * permutation->block_size()),
20  buffer(counter.size()),
21  position(0)
22  {
23  }
24 
25 /*
26 * CTR_BE Destructor
27 */
29  {
30  delete permutation;
31  }
32 
33 /*
34 * Zeroize
35 */
37  {
38  permutation->clear();
39  zeroise(buffer);
40  zeroise(counter);
41  position = 0;
42  }
43 
44 /*
45 * Set the key
46 */
47 void CTR_BE::key_schedule(const byte key[], size_t key_len)
48  {
49  permutation->set_key(key, key_len);
50 
51  // Set a default all-zeros IV
52  set_iv(0, 0);
53  }
54 
55 /*
56 * Return the name of this type
57 */
58 std::string CTR_BE::name() const
59  {
60  return ("CTR-BE(" + permutation->name() + ")");
61  }
62 
63 /*
64 * CTR-BE Encryption/Decryption
65 */
66 void CTR_BE::cipher(const byte in[], byte out[], size_t length)
67  {
68  while(length >= buffer.size() - position)
69  {
70  xor_buf(out, in, &buffer[position], buffer.size() - position);
71  length -= (buffer.size() - position);
72  in += (buffer.size() - position);
73  out += (buffer.size() - position);
74  increment_counter();
75  }
76  xor_buf(out, in, &buffer[position], length);
77  position += length;
78  }
79 
80 /*
81 * Set CTR-BE IV
82 */
83 void CTR_BE::set_iv(const byte iv[], size_t iv_len)
84  {
85  if(!valid_iv_length(iv_len))
86  throw Invalid_IV_Length(name(), iv_len);
87 
88  const size_t bs = permutation->block_size();
89 
90  zeroise(counter);
91 
92  counter.copy(0, iv, iv_len);
93 
94  /*
95  * Set counter blocks to IV, IV + 1, ... IV + 255
96  */
97  for(size_t i = 1; i != 256; ++i)
98  {
99  counter.copy(i*bs, &counter[(i-1)*bs], bs);
100 
101  for(size_t j = 0; j != bs; ++j)
102  if(++counter[i*bs + (bs - 1 - j)])
103  break;
104  }
105 
106  permutation->encrypt_n(&counter[0], &buffer[0], 256);
107  position = 0;
108  }
109 
110 /*
111 * Increment the counter and update the buffer
112 */
113 void CTR_BE::increment_counter()
114  {
115  const size_t bs = permutation->block_size();
116 
117  /*
118  * Each counter value always needs to be incremented by 256,
119  * so we don't touch the lowest byte and instead treat it as
120  * an increment of one starting with the next byte.
121  */
122  for(size_t i = 0; i != 256; ++i)
123  {
124  for(size_t j = 1; j != bs; ++j)
125  if(++counter[i*bs + (bs - 1 - j)])
126  break;
127  }
128 
129  permutation->encrypt_n(&counter[0], &buffer[0], 256);
130 
131  position = 0;
132  }
133 
134 }
virtual void clear()=0
void cipher(const byte in[], byte out[], size_t length)
Definition: ctr.cpp:66
size_t block_size
Definition: ossl_md.cpp:41
std::string name() const
Definition: ctr.cpp:58
void copy(const T in[], size_t n)
Definition: secmem.h:120
bool valid_iv_length(size_t iv_len) const
Definition: ctr.h:26
unsigned char byte
Definition: types.h:22
void clear()
Definition: ctr.cpp:36
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
size_t size() const
Definition: secmem.h:29
virtual std::string name() const =0
CTR_BE(BlockCipher *cipher)
Definition: ctr.cpp:17
virtual void encrypt_n(const byte in[], byte out[], size_t blocks) const =0
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
void set_iv(const byte iv[], size_t iv_len)
Definition: ctr.cpp:83