Botan: src/stream/ctr/ctr.cpp Source File

Botan 1.10.16

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
17 CTR_BE::CTR_BE(BlockCipher* ciph) :
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 */
28 CTR_BE::~CTR_BE()
29 {
30 delete permutation;
31 }
32
33 /*
34 * Zeroize
35 */
36 void CTR_BE::clear()
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 }
Botan::CTR_BE::name
std::string name() const
Definition: ctr.cpp:58
Botan::Algorithm::clear
virtual void clear()=0
Botan::CTR_BE::cipher
void cipher(const byte in[], byte out[], size_t length)
Definition: ctr.cpp:66
Botan::Invalid_IV_Length
Definition: exceptn.h:78
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:120
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::CTR_BE::clear
void clear()
Definition: ctr.cpp:36
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::CTR_BE::~CTR_BE
~CTR_BE()
Definition: ctr.cpp:28
Botan
Definition: algo_base.h:14
Botan::Algorithm::name
virtual std::string name() const =0
Botan::CTR_BE::CTR_BE
CTR_BE(BlockCipher *cipher)
Definition: ctr.cpp:17
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::BlockCipher::encrypt_n
virtual void encrypt_n(const byte in[], byte out[], size_t blocks) const =0
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::CTR_BE::valid_iv_length
bool valid_iv_length(size_t iv_len) const
Definition: ctr.h:26
Botan::CTR_BE::set_iv
void set_iv(const byte iv[], size_t iv_len)
Definition: ctr.cpp:83
Botan::BlockCipher
Definition: block_cipher.h:18