VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Volume/EncryptionModeCBC.cpp
blob: 2892986b585a00c57df5e55cb48c5e8b6378d408 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.

 Governed by the TrueCrypt License 3.0 the full text of which is contained in
 the file License.txt included in TrueCrypt binary and source code distribution
 packages.
*/

#include "Platform/Memory.h"
#include "Common/Crc.h"
#include "Common/Endian.h"
#include "EncryptionModeCBC.h"

namespace VeraCrypt
{
	void EncryptionModeCBC::Decrypt (byte *data, uint64 length) const
	{
		if_debug (ValidateState ());
		if_debug (ValidateParameters (data, length));

		if (IsOuterCBC (Ciphers))
		{
			DecryptBuffer (data, length, Ciphers, (uint32 *) IV.Ptr(), (uint32 *) (IV.Ptr() + WhiteningIVOffset));
		}
		else
		{
			for (CipherList::const_reverse_iterator iCipherList = Ciphers.rbegin();
				iCipherList != Ciphers.rend();
				++iCipherList)
			{
				CipherList cl;
				cl.push_back (*iCipherList);

				DecryptBuffer (data, length, cl, (uint32 *) IV.Ptr(), (uint32 *) (IV.Ptr() + WhiteningIVOffset));
			}
		}
	}

	void EncryptionModeCBC::DecryptBuffer (byte *data, uint64 length, const CipherList &ciphers, const uint32 *iv, const uint32 *whitening) const
	{
		size_t blockSize = ciphers.front()->GetBlockSize();
		if (blockSize != 8 && blockSize != 16)
			throw ParameterIncorrect (SRC_POS);

		uint32 *data32 = (uint32 *) data;
		uint32 bufIV[4];
		uint32 ct[4];
		uint64 i;

		bufIV[0] = iv[0];
		bufIV[1] = iv[1];
		if (blockSize == 16)
		{
			bufIV[2] = iv[2];
			bufIV[3] = iv[3];
		}

		for (i = 0; i < length / blockSize; i++)
		{
			// Dewhitening
			data32[0] ^= whitening[0];
			data32[1] ^= whitening[1];
			if (blockSize == 16)
			{
				data32[2] ^= whitening[0];
				data32[3] ^= whitening[1];
			}

			// CBC
			ct[0] = data32[0];
			ct[1] = data32[1];
			if (blockSize == 16)
			{
				ct[2] = data32[2];
				ct[3] = data32[3];
			}

			for (CipherList::const_reverse_iterator iCipherList = ciphers.rbegin();
				iCipherList != ciphers.rend();
				++iCipherList)
			{
				const Cipher &c = **iCipherList;

				if (c.GetBlockSize () != blockSize)
					throw ParameterIncorrect (SRC_POS);

				c.DecryptBlock ((byte *) data32);
			}

			// CBC
			data32[0] ^= bufIV[0];
			data32[1] ^= bufIV[1];
			bufIV[0] = ct[0];
			bufIV[1] = ct[1];
			if (blockSize == 16)
			{
				data32[2] ^= bufIV[2];
				data32[3] ^= bufIV[3];
				bufIV[2] = ct[2];
				bufIV[3] = ct[3];
			}

			data32 += blockSize / sizeof(*data32);
		}

		Memory::Erase (bufIV, sizeof (bufIV));
		Memory::Erase (ct, sizeof (ct));
	}

	void EncryptionModeCBC::DecryptSectorsCurrentThread (byte *data, uint64 sectorIndex, uint64 sectorCount, size_t sectorSize) const
	{
		if_debug (ValidateState ());
		if_debug (ValidateParameters (data, sectorCount, sectorSize));

		uint32 sectorIV[4];
		uint32 sectorWhitening[2];

		while (sectorCount--)
		{
			if (IsOuterCBC (Ciphers))
			{
				InitSectorIVAndWhitening (sectorIndex, Ciphers.front()->GetBlockSize(), (uint64 *) IV.Ptr(), sectorIV, sectorWhitening);
				DecryptBuffer (data, sectorSize, Ciphers, sectorIV, sectorWhitening);
			}
			else
			{
				for (CipherList::const_reverse_iterator iCipherList = Ciphers.rbegin();
					iCipherList != Ciphers.rend();
					++iCipherList)
				{
					const Cipher &c = **iCipherList;
					CipherList cl;
					cl.push_back (*iCipherList);

					InitSectorIVAndWhitening (sectorIndex, c.GetBlockSize(), (uint64 *) IV.Ptr(), sectorIV, sectorWhitening);
					DecryptBuffer (data, sectorSize, cl, sectorIV, sectorWhitening);
				}
			}

			data += sectorSize;
			sectorIndex++;
		}

		Memory::Erase (sectorIV, sizeof (sectorIV));
		Memory::Erase (sectorWhitening, sizeof (sectorWhitening));
	}

	void EncryptionModeCBC::Encrypt (byte *data, uint64 length) const
	{
		if_debug (ValidateState ());
		if_debug (ValidateParameters (data, length));

		if (IsOuterCBC (Ciphers))
		{
			EncryptBuffer (data, length, Ciphers, (uint32 *) IV.Ptr(), (uint32 *) (IV.Ptr() + WhiteningIVOffset));
		}
		else
		{
			for (CipherList::const_iterator iCipherList = Ciphers.begin();
				iCipherList != Ciphers.end();
				++iCipherList)
			{
				CipherList cl;
				cl.push_back (*iCipherList);

				EncryptBuffer (data, length, cl, (uint32 *) IV.Ptr(), (uint32 *) (IV.Ptr() + WhiteningIVOffset));
			}
		}
	}

	void EncryptionModeCBC::EncryptBuffer (byte *data, uint64 length, const CipherList &ciphers, const uint32 *iv, const uint32 *whitening) const
	{
		size_t blockSize = ciphers.front()->GetBlockSize();
		if (blockSize != 8 && blockSize != 16)
			throw ParameterIncorrect (SRC_POS);

		uint32 *data32 = (uint32 *) data;
		uint32 bufIV[4];
		uint64 i;

		bufIV[0] = iv[0];
		bufIV[1] = iv[1];
		if (blockSize == 16)
		{
			bufIV[2] = iv[2];
			bufIV[3] = iv[3];
		}

		for (i = 0; i < length / blockSize; i++)
		{
			data32[0] ^= bufIV[0];
			data32[1] ^= bufIV[1];
			if (blockSize == 16)
			{
				data32[2] ^= bufIV[2];
				data32[3] ^= bufIV[3];
			}

			for (CipherList::const_iterator iCipherList = ciphers.begin();
				iCipherList != ciphers.end();
				++iCipherList)
			{
				const Cipher &c = **iCipherList;

				if (c.GetBlockSize () != blockSize)
					throw ParameterIncorrect (SRC_POS);

				c.EncryptBlock ((byte *) data32);
			}

			bufIV[0] = data32[0];
			bufIV[1] = data32[1];
			if (blockSize == 16)
			{
				bufIV[2] = data32[2];
				bufIV[3] = data32[3];
			}

			data32[0] ^= whitening[0];
			data32[1] ^= whitening[1];
			if (blockSize == 16)
			{
				data32[2] ^= whitening[0];
				data32[3] ^= whitening[1];
			}

			data32 += blockSize / sizeof(*data32);
		}

		Memory::Erase (bufIV, sizeof (bufIV));
	}

	void EncryptionModeCBC::EncryptSectorsCurrentThread (byte *data, uint64 sectorIndex, uint64 sectorCount, size_t sectorSize) const
	{
		if_debug (ValidateState ());
		if_debug (ValidateParameters (data, sectorCount, sectorSize));

		uint32 sectorIV[4];
		uint32 sectorWhitening[2];

		while (sectorCount--)
		{
			if (IsOuterCBC (Ciphers))
			{
				InitSectorIVAndWhitening (sectorIndex, Ciphers.front()->GetBlockSize(), (uint64 *) IV.Ptr(), sectorIV, sectorWhitening);
				EncryptBuffer (data, sectorSize, Ciphers, sectorIV, sectorWhitening);
			}
			else
			{
				for (CipherList::const_iterator iCipherList = Ciphers.begin();
					iCipherList != Ciphers.end();
					++iCipherList)
				{
					const Cipher &c = **iCipherList;
					CipherList cl;
					cl.push_back (*iCipherList);

					InitSectorIVAndWhitening (sectorIndex, c.GetBlockSize(), (uint64 *) IV.Ptr(), sectorIV, sectorWhitening);
					EncryptBuffer (data, sectorSize, cl, sectorIV, sectorWhitening);
				}
			}

			data += sectorSize;
			sectorIndex++;
		}

		Memory::Erase (sectorIV, sizeof (sectorIV));
		Memory::Erase (sectorWhitening, sizeof (sectorWhitening));
	}

	void EncryptionModeCBC::InitSectorIVAndWhitening (uint64 sectorIndex, size_t blockSize, const uint64 *ivSeed, uint32 *iv, uint32 *whitening) const
	{
		if (blockSize != 8 && blockSize != 16)
			throw ParameterIncorrect (SRC_POS);

		uint64 iv64[4];
		uint32 *iv32 = (uint32 *) iv64;

		iv64[0] = ivSeed[0] ^ Endian::Little (sectorIndex);
		iv64[1] = ivSeed[1] ^ Endian::Little (sectorIndex);
		iv64[2] = ivSeed[2] ^ Endian::Little (sectorIndex);
		if (blockSize == 16)
		{
			iv64[3] = ivSeed[3] ^ Endian::Little (sectorIndex);
		}

		iv[0] = iv32[0];
		iv[1] = iv32[1];

		if (blockSize == 8)
		{
			whitening[0] = Endian::Little ( crc32int ( &iv32[2] ) ^ crc32int ( &iv32[5] ) );
			whitening[1] = Endian::Little ( crc32int ( &iv32[3] ) ^ crc32int ( &iv32[4] ) );
		}
		else
		{
			iv[2] = iv32[2];
			iv[3] = iv32[3];

			whitening[0] = Endian::Little ( crc32int ( &iv32[4] ) ^ crc32int ( &iv32[7] ) );
			whitening[1] = Endian::Little ( crc32int ( &iv32[5] ) ^ crc32int ( &iv32[6] ) );
		}
	}

	bool EncryptionModeCBC::IsOuterCBC (const CipherList &ciphers) const
	{
		if (ciphers.size() < 2)
			return false;

		size_t blockSize = ciphers.front()->GetBlockSize();

		for (CipherList::const_iterator iCipherList = ciphers.begin();
			iCipherList != ciphers.end();
			++iCipherList)
		{
			const Cipher &c = **iCipherList;
			if (c.GetBlockSize() != blockSize)
				return false;
		}

		return true;
	}

	void EncryptionModeCBC::SetKey (const ConstBufferPtr &key)
	{
		if (key.Size() != GetKeySize ())
			throw ParameterIncorrect (SRC_POS);

		if (!KeySet)
			IV.Allocate (GetKeySize ());
		
		IV.CopyFrom (key);
		KeySet = true;
	}
}