monotone

monotone Mtn Source Tree

Root/botan/cts.cpp

1/*************************************************
2* CTS Mode Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/cts.h>
7#include <botan/lookup.h>
8#include <botan/bit_ops.h>
9#include <algorithm>
10
11namespace Botan {
12
13/*************************************************
14* CTS Encryption Constructor *
15*************************************************/
16CTS_Encryption::CTS_Encryption(const std::string& cipher_name) :
17 BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2)
18 {
19 }
20
21/*************************************************
22* CTS Encryption Constructor *
23*************************************************/
24CTS_Encryption::CTS_Encryption(const std::string& cipher_name,
25 const SymmetricKey& key,
26 const InitializationVector& iv) :
27 BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2)
28 {
29 set_key(key);
30 set_iv(iv);
31 }
32
33/*************************************************
34* Encrypt a block *
35*************************************************/
36void CTS_Encryption::encrypt(const byte block[])
37 {
38 xor_buf(state, block, BLOCK_SIZE);
39 cipher->encrypt(state);
40 send(state, BLOCK_SIZE);
41 }
42
43/*************************************************
44* Encrypt in CTS mode *
45*************************************************/
46void CTS_Encryption::write(const byte input[], u32bit length)
47 {
48 u32bit copied = std::min(BUFFER_SIZE - position, length);
49 buffer.copy(position, input, copied);
50 length -= copied;
51 input += copied;
52 position += copied;
53
54 if(length == 0) return;
55
56 encrypt(buffer);
57 if(length > BLOCK_SIZE)
58 {
59 encrypt(buffer + BLOCK_SIZE);
60 while(length > 2*BLOCK_SIZE)
61 {
62 encrypt(input);
63 length -= BLOCK_SIZE;
64 input += BLOCK_SIZE;
65 }
66 position = 0;
67 }
68 else
69 {
70 copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE);
71 position = BLOCK_SIZE;
72 }
73 buffer.copy(position, input, length);
74 position += length;
75 }
76
77/*************************************************
78* Finish encrypting in CTS mode *
79*************************************************/
80void CTS_Encryption::end_msg()
81 {
82 if(position < BLOCK_SIZE + 1)
83 throw Exception("CTS_Encryption: insufficient data to encrypt");
84 xor_buf(state, buffer, BLOCK_SIZE);
85 cipher->encrypt(state);
86 SecureVector<byte> cn = state;
87 clear_mem(buffer + position, BUFFER_SIZE - position);
88 encrypt(buffer + BLOCK_SIZE);
89 send(cn, position - BLOCK_SIZE);
90 }
91
92/*************************************************
93* CTS Decryption Constructor *
94*************************************************/
95CTS_Decryption::CTS_Decryption(const std::string& cipher_name) :
96 BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2)
97 {
98 temp.create(BLOCK_SIZE);
99 }
100
101/*************************************************
102* CTS Decryption Constructor *
103*************************************************/
104CTS_Decryption::CTS_Decryption(const std::string& cipher_name,
105 const SymmetricKey& key,
106 const InitializationVector& iv) :
107 BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2)
108 {
109 temp.create(BLOCK_SIZE);
110 set_key(key);
111 set_iv(iv);
112 }
113
114/*************************************************
115* Decrypt a block *
116*************************************************/
117void CTS_Decryption::decrypt(const byte block[])
118 {
119 cipher->decrypt(block, temp);
120 xor_buf(temp, state, BLOCK_SIZE);
121 send(temp, BLOCK_SIZE);
122 state.copy(block, BLOCK_SIZE);
123 }
124
125/*************************************************
126* Decrypt in CTS mode *
127*************************************************/
128void CTS_Decryption::write(const byte input[], u32bit length)
129 {
130 u32bit copied = std::min(BUFFER_SIZE - position, length);
131 buffer.copy(position, input, copied);
132 length -= copied;
133 input += copied;
134 position += copied;
135
136 if(length == 0) return;
137
138 decrypt(buffer);
139 if(length > BLOCK_SIZE)
140 {
141 decrypt(buffer + BLOCK_SIZE);
142 while(length > 2*BLOCK_SIZE)
143 {
144 decrypt(input);
145 length -= BLOCK_SIZE;
146 input += BLOCK_SIZE;
147 }
148 position = 0;
149 }
150 else
151 {
152 copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE);
153 position = BLOCK_SIZE;
154 }
155 buffer.copy(position, input, length);
156 position += length;
157 }
158
159/*************************************************
160* Finish decrypting in CTS mode *
161*************************************************/
162void CTS_Decryption::end_msg()
163 {
164 cipher->decrypt(buffer, temp);
165 xor_buf(temp, buffer + BLOCK_SIZE, position - BLOCK_SIZE);
166 SecureVector<byte> xn = temp;
167 copy_mem(buffer + position, xn + (position - BLOCK_SIZE),
168 BUFFER_SIZE - position);
169 cipher->decrypt(buffer + BLOCK_SIZE, temp);
170 xor_buf(temp, state, BLOCK_SIZE);
171 send(temp, BLOCK_SIZE);
172 send(xn, position - BLOCK_SIZE);
173 }
174
175}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status