monotone

monotone Mtn Source Tree

Root/botan/cts.cpp

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

Archive Download this file

Branches

Tags

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