|
|
1.1.1.10 root 1: /*
1.1.1.12 root 2: Legal Notice: Some portions of the source code contained in this file were
3: derived from the source code of Encryption for the Masses 2.02a, which is
4: Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License
5: Agreement for Encryption for the Masses'. Modifications and additions to
6: the original source code (contained in this file) and all other portions of
7: this file are Copyright (c) 2003-2008 TrueCrypt Foundation and are governed
8: by the TrueCrypt License 2.4 the full text of which is contained in the
9: file License.txt included in TrueCrypt binary and source code distribution
1.1.1.10 root 10: packages. */
1.1 root 11:
1.1.1.6 root 12: #include "Tcdefs.h"
1.1 root 13:
1.1.1.12 root 14: #ifndef TC_WINDOWS_BOOT
1.1 root 15: #include <fcntl.h>
1.1.1.10 root 16: #include <string.h>
1.1 root 17: #include <sys/types.h>
18: #include <sys/stat.h>
19: #include <time.h>
1.1.1.12 root 20: #endif
1.1 root 21:
1.1.1.6 root 22: #ifdef _WIN32
23: #include <io.h>
24: #include "Random.h"
25: #endif
26:
27: #include "Crypto.h"
1.1.1.10 root 28: #include "Common/Endian.h"
1.1.1.6 root 29: #include "Volumes.h"
30:
31: #include "Pkcs5.h"
32: #include "Crc.h"
1.1 root 33:
34:
1.1.1.12 root 35:
36: /* Volume header v3 structure: */
37: //
38: // Offset Length Description
39: // ------------------------------------------
40: // Unencrypted:
41: // 0 64 Salt
42: // Encrypted:
43: // 64 4 ASCII string 'TRUE'
44: // 68 2 Header version
45: // 70 2 Required program version
46: // 72 4 CRC-32 checksum of the (decrypted) bytes 256-511
47: // 76 8 Volume creation time
48: // 84 8 Header creation time
49: // 92 8 Size of hidden volume in bytes (0 = normal volume)
50: // 100 8 Size of the volume in bytes (identical with field 92 for hidden volumes)
51: // 108 8 Start byte offset of the encrypted area of the volume
52: // 116 8 Size of the encrypted area of the volume in bytes
53: // 124 132 Reserved (set to zero)
54: // 256 256 Concatenated primary master key(s) and secondary master key(s) (XTS mode)
1.1 root 55:
56:
1.1.1.12 root 57: /* Deprecated/legacy volume header v2 structure (used before TrueCrypt 5.0): */
1.1 root 58: //
59: // Offset Length Description
60: // ------------------------------------------
61: // Unencrypted:
1.1.1.6 root 62: // 0 64 Salt
1.1 root 63: // Encrypted:
1.1.1.4 root 64: // 64 4 ASCII string 'TRUE'
1.1 root 65: // 68 2 Header version
66: // 70 2 Required program version
1.1.1.7 root 67: // 72 4 CRC-32 checksum of the (decrypted) bytes 256-511
1.1 root 68: // 76 8 Volume creation time
69: // 84 8 Header creation time
1.1.1.4 root 70: // 92 8 Size of hidden volume in bytes (0 = normal volume)
1.1.1.7 root 71: // 100 156 Reserved (set to zero)
1.1.1.12 root 72: // 256 32 For LRW (deprecated/legacy), secondary key
73: // For CBC (deprecated/legacy), data used to generate IV and whitening values
1.1.1.7 root 74: // 288 224 Master key(s)
1.1 root 75:
1.1.1.12 root 76:
77:
78: uint16 GetHeaderField16 (byte *header, size_t offset)
79: {
80: return BE16 (*(uint16 *) (header + offset));
81: }
82:
83:
84: uint32 GetHeaderField32 (byte *header, size_t offset)
85: {
86: return BE32 (*(uint32 *) (header + offset));
87: }
88:
89:
90: UINT64_STRUCT GetHeaderField64 (byte *header, size_t offset)
91: {
92: UINT64_STRUCT uint64Struct;
93:
94: #ifndef TC_NO_COMPILER_INT64
95: uint64Struct.Value = BE64 (*(uint64 *) (header + offset));
96: #else
97: uint64Struct.HighPart = BE32 (*(uint32 *) (header + offset));
98: uint64Struct.LowPart = BE32 (*(uint32 *) (header + offset + 4));
99: #endif
100: return uint64Struct;
101: }
102:
103:
1.1.1.13! root 104: #ifndef TC_WINDOWS_BOOT
! 105:
1.1.1.12 root 106: int VolumeReadHeader (BOOL bBoot, char *encryptedHeader, Password *password, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo)
1.1 root 107: {
1.1.1.6 root 108: char header[HEADER_SIZE];
1.1 root 109: KEY_INFO keyInfo;
110: PCRYPTO_INFO cryptoInfo;
1.1.1.12 root 111: char dk[MASTER_KEYDATA_SIZE];
112: int pkcs5_prf;
1.1 root 113: int headerVersion, requiredVersion;
1.1.1.6 root 114: int status;
1.1.1.12 root 115: int primaryKeyOffset;
1.1.1.6 root 116:
1.1.1.12 root 117: #ifdef _WIN32
118: #ifndef DEVICE_DRIVER
119: VirtualLock (&keyInfo, sizeof (keyInfo));
120: VirtualLock (&dk, sizeof (dk));
121: #endif
122: #endif
1.1.1.7 root 123:
1.1.1.12 root 124: if (retHeaderCryptoInfo != NULL)
125: {
126: cryptoInfo = retHeaderCryptoInfo;
127: }
128: else
129: {
130: cryptoInfo = *retInfo = crypto_open ();
131: if (cryptoInfo == NULL)
132: return ERR_OUTOFMEMORY;
133: }
1.1 root 134:
1.1.1.12 root 135: crypto_loadkey (&keyInfo, password->Text, (int) password->Length);
1.1 root 136:
1.1.1.12 root 137: // PKCS5 is used to derive the primary header key(s) and secondary header key(s) (XTS mode) from the password
138: memcpy (keyInfo.salt, encryptedHeader + HEADER_SALT_OFFSET, PKCS5_SALT_SIZE);
1.1 root 139:
1.1.1.3 root 140: // Test all available PKCS5 PRFs
1.1.1.12 root 141: for (pkcs5_prf = FIRST_PRF_ID; pkcs5_prf <= LAST_PRF_ID; pkcs5_prf++)
1.1 root 142: {
1.1.1.12 root 143: BOOL lrw64InitDone = FALSE; // Deprecated/legacy
144: BOOL lrw128InitDone = FALSE; // Deprecated/legacy
1.1.1.7 root 145:
1.1.1.12 root 146: keyInfo.noIterations = get_pkcs5_iteration_count (pkcs5_prf, bBoot);
1.1.1.6 root 147:
1.1.1.12 root 148: switch (pkcs5_prf)
1.1.1.3 root 149: {
1.1.1.12 root 150: case RIPEMD160:
151: derive_key_ripemd160 (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
152: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
1.1.1.6 root 153: break;
154:
1.1.1.12 root 155: case SHA512:
156: derive_key_sha512 (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
157: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
158: break;
159:
160: case SHA1:
161: // Deprecated/legacy
162: derive_key_sha1 (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
163: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
1.1.1.6 root 164: break;
165:
166: case WHIRLPOOL:
1.1.1.12 root 167: derive_key_whirlpool (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
168: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
1.1.1.6 root 169: break;
1.1.1.12 root 170:
171: default:
172: // Unknown/wrong ID
173: TC_THROW_FATAL_EXCEPTION;
1.1.1.4 root 174: }
1.1 root 175:
1.1.1.12 root 176: // Test all available modes of operation
177: for (cryptoInfo->mode = FIRST_MODE_OF_OPERATION_ID;
178: cryptoInfo->mode <= LAST_MODE_OF_OPERATION;
179: cryptoInfo->mode++)
1.1.1.3 root 180: {
1.1.1.12 root 181: switch (cryptoInfo->mode)
182: {
183: case LRW:
184: case CBC:
185: case INNER_CBC:
186: case OUTER_CBC:
187:
188: // For LRW (deprecated/legacy), copy the tweak key
189: // For CBC (deprecated/legacy), copy the IV/whitening seed
190: memcpy (cryptoInfo->k2, dk, LEGACY_VOL_IV_SIZE);
191: primaryKeyOffset = LEGACY_VOL_IV_SIZE;
192: break;
1.1.1.13! root 193:
1.1.1.12 root 194: default:
195: primaryKeyOffset = 0;
196: }
1.1.1.6 root 197:
1.1.1.12 root 198: // Test all available encryption algorithms
199: for (cryptoInfo->ea = EAGetFirst ();
200: cryptoInfo->ea != 0;
201: cryptoInfo->ea = EAGetNext (cryptoInfo->ea))
1.1.1.6 root 202: {
1.1.1.12 root 203: int blockSize;
204:
205: if (!EAIsModeSupported (cryptoInfo->ea, cryptoInfo->mode))
206: continue; // This encryption algorithm has never been available with this mode of operation
1.1.1.7 root 207:
1.1.1.12 root 208: blockSize = CipherGetBlockSize (EAGetFirstCipher (cryptoInfo->ea));
209:
210: status = EAInit (cryptoInfo->ea, dk + primaryKeyOffset, cryptoInfo->ks);
211: if (status == ERR_CIPHER_INIT_FAILURE)
212: goto err;
213:
214: // Init objects related to the mode of operation
215:
216: if (cryptoInfo->mode == XTS)
217: {
218: // Copy the secondary key (if cascade, multiple concatenated)
219: memcpy (cryptoInfo->k2, dk + EAGetKeySize (cryptoInfo->ea), EAGetKeySize (cryptoInfo->ea));
220:
221: // Secondary key schedule
222: if (!EAInitMode (cryptoInfo))
223: {
224: status = ERR_MODE_INIT_FAILED;
225: goto err;
226: }
227: }
228: else if (cryptoInfo->mode == LRW
1.1.1.7 root 229: && (blockSize == 8 && !lrw64InitDone || blockSize == 16 && !lrw128InitDone))
230: {
1.1.1.12 root 231: // Deprecated/legacy
232:
1.1.1.7 root 233: if (!EAInitMode (cryptoInfo))
234: {
235: status = ERR_MODE_INIT_FAILED;
236: goto err;
237: }
238:
1.1.1.12 root 239: if (blockSize == 8)
1.1.1.7 root 240: lrw64InitDone = TRUE;
241: else if (blockSize == 16)
242: lrw128InitDone = TRUE;
243: }
244:
1.1.1.12 root 245: // Copy the header for decryption
246: memcpy (header, encryptedHeader, HEADER_SIZE);
1.1.1.7 root 247:
248: // Try to decrypt header
249:
1.1.1.12 root 250: DecryptBuffer (header + HEADER_ENCRYPTED_DATA_OFFSET, HEADER_ENCRYPTED_DATA_SIZE, cryptoInfo);
1.1.1.7 root 251:
252: // Magic 'TRUE'
1.1.1.12 root 253: if (GetHeaderField32 (header, TC_HEADER_OFFSET_MAGIC) != 0x54525545)
1.1.1.7 root 254: continue;
255:
256: // Header version
1.1.1.12 root 257: headerVersion = GetHeaderField16 (header, TC_HEADER_OFFSET_VERSION);
1.1.1.7 root 258:
259: // Required program version
1.1.1.12 root 260: requiredVersion = GetHeaderField16 (header, TC_HEADER_OFFSET_REQUIRED_VERSION);
1.1.1.7 root 261:
262: // Check CRC of the key set
1.1.1.12 root 263: if (GetHeaderField32 (header, TC_HEADER_OFFSET_KEY_AREA_CRC) != GetCrc32 (header + HEADER_MASTER_KEYDATA_OFFSET, MASTER_KEYDATA_SIZE))
1.1.1.7 root 264: continue;
265:
266: // Now we have the correct password, cipher, hash algorithm, and volume type
267:
268: // Check the version required to handle this volume
269: if (requiredVersion > VERSION_NUM)
270: {
271: status = ERR_NEW_VERSION_REQUIRED;
272: goto err;
273: }
274:
275: // Volume creation time
1.1.1.12 root 276: cryptoInfo->volume_creation_time = GetHeaderField64 (header, TC_HEADER_OFFSET_VOLUME_CREATION_TIME).Value;
1.1.1.7 root 277:
278: // Header creation time
1.1.1.12 root 279: cryptoInfo->header_creation_time = GetHeaderField64 (header, TC_HEADER_OFFSET_MODIFICATION_TIME).Value;
1.1.1.7 root 280:
281: // Hidden volume size (if any)
1.1.1.12 root 282: cryptoInfo->hiddenVolumeSize = GetHeaderField64 (header, TC_HEADER_OFFSET_HIDDEN_VOLUME_SIZE).Value;
1.1.1.13! root 283:
1.1.1.12 root 284: // Volume size
285: cryptoInfo->VolumeSize = GetHeaderField64 (header, TC_HEADER_OFFSET_VOLUME_SIZE);
286:
287: // Encrypted area size and length
288: cryptoInfo->EncryptedAreaStart = GetHeaderField64 (header, TC_HEADER_OFFSET_ENCRYPTED_AREA_START);
289: cryptoInfo->EncryptedAreaLength = GetHeaderField64 (header, TC_HEADER_OFFSET_ENCRYPTED_AREA_LENGTH);
290:
291: // Preserve scheduled header keys if requested
292: if (retHeaderCryptoInfo)
293: {
294: if (retInfo == NULL)
295: {
296: cryptoInfo->pkcs5 = pkcs5_prf;
297: cryptoInfo->noIterations = keyInfo.noIterations;
298: goto ret;
299: }
300:
301: cryptoInfo = *retInfo = crypto_open ();
302: if (cryptoInfo == NULL)
303: {
304: status = ERR_OUTOFMEMORY;
305: goto err;
306: }
1.1.1.7 root 307:
1.1.1.12 root 308: memcpy (cryptoInfo, retHeaderCryptoInfo, sizeof (*cryptoInfo));
309: }
310:
311: // Master key data
312: memcpy (keyInfo.master_keydata, header + HEADER_MASTER_KEYDATA_OFFSET, MASTER_KEYDATA_SIZE);
313: memcpy (cryptoInfo->master_keydata, keyInfo.master_keydata, MASTER_KEYDATA_SIZE);
314:
315: // PKCS #5
316: memcpy (cryptoInfo->salt, keyInfo.salt, PKCS5_SALT_SIZE);
317: cryptoInfo->pkcs5 = pkcs5_prf;
1.1.1.7 root 318: cryptoInfo->noIterations = keyInfo.noIterations;
319:
320: // Init the encryption algorithm with the decrypted master key
1.1.1.12 root 321: status = EAInit (cryptoInfo->ea, keyInfo.master_keydata + primaryKeyOffset, cryptoInfo->ks);
1.1.1.7 root 322: if (status == ERR_CIPHER_INIT_FAILURE)
323: goto err;
324:
1.1.1.12 root 325: switch (cryptoInfo->mode)
326: {
327: case LRW:
328: case CBC:
329: case INNER_CBC:
330: case OUTER_CBC:
331:
332: // For LRW (deprecated/legacy), the tweak key
333: // For CBC (deprecated/legacy), the IV/whitening seed
334: memcpy (cryptoInfo->k2, keyInfo.master_keydata, LEGACY_VOL_IV_SIZE);
335: break;
1.1.1.13! root 336:
1.1.1.12 root 337: default:
338: // The secondary master key (if cascade, multiple concatenated)
339: memcpy (cryptoInfo->k2, keyInfo.master_keydata + EAGetKeySize (cryptoInfo->ea), EAGetKeySize (cryptoInfo->ea));
340:
341: }
1.1.1.7 root 342:
343: if (!EAInitMode (cryptoInfo))
344: {
345: status = ERR_MODE_INIT_FAILED;
346: goto err;
347: }
1.1.1.6 root 348:
1.1.1.12 root 349: // Clear out the temporary key buffers
350: ret:
1.1.1.7 root 351: burn (dk, sizeof(dk));
1.1.1.12 root 352: burn (&keyInfo, sizeof (keyInfo));
1.1.1.6 root 353:
1.1.1.7 root 354: return 0;
355: }
1.1.1.2 root 356: }
1.1 root 357: }
1.1.1.6 root 358: status = ERR_PASSWORD_WRONG;
1.1 root 359:
1.1.1.6 root 360: err:
1.1.1.12 root 361: if (cryptoInfo != retHeaderCryptoInfo)
362: {
363: crypto_close(cryptoInfo);
364: *retInfo = NULL;
365: }
366:
1.1 root 367: burn (&keyInfo, sizeof (keyInfo));
1.1.1.12 root 368: burn (dk, sizeof(dk));
1.1.1.6 root 369: return status;
1.1 root 370: }
371:
1.1.1.13! root 372: #else // TC_WINDOWS_BOOT
! 373:
! 374: int VolumeReadHeader (BOOL bBoot, char *header, Password *password, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo)
! 375: {
! 376: #ifdef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE
! 377: char dk[32 * 2]; // 2 * 256-bit key
! 378: char masterKey[32 * 2];
! 379: #else
! 380: char dk[32 * 2 * 3]; // 6 * 256-bit key
! 381: char masterKey[32 * 2 * 3];
! 382: #endif
! 383:
! 384: PCRYPTO_INFO cryptoInfo;
! 385: int status;
! 386:
! 387: if (retHeaderCryptoInfo != NULL)
! 388: cryptoInfo = retHeaderCryptoInfo;
! 389: else
! 390: cryptoInfo = *retInfo = crypto_open ();
! 391:
! 392: // PKCS5 PRF
! 393: derive_key_ripemd160 (password->Text, (int) password->Length, header + HEADER_SALT_OFFSET,
! 394: PKCS5_SALT_SIZE, 1000, dk, sizeof (dk));
! 395:
! 396: // Mode of operation
! 397: cryptoInfo->mode = FIRST_MODE_OF_OPERATION_ID;
! 398:
! 399: // Test all available encryption algorithms
! 400: for (cryptoInfo->ea = EAGetFirst (); cryptoInfo->ea != 0; cryptoInfo->ea = EAGetNext (cryptoInfo->ea))
! 401: {
! 402: status = EAInit (cryptoInfo->ea, dk, cryptoInfo->ks);
! 403: if (status == ERR_CIPHER_INIT_FAILURE)
! 404: goto err;
! 405:
! 406: // Secondary key schedule
! 407: EAInit (cryptoInfo->ea, dk + EAGetKeySize (cryptoInfo->ea), cryptoInfo->ks2);
! 408:
! 409: // Try to decrypt header
! 410: DecryptBuffer (header + HEADER_ENCRYPTED_DATA_OFFSET, HEADER_ENCRYPTED_DATA_SIZE, cryptoInfo);
! 411:
! 412: // Check magic 'TRUE' and key CRC
! 413: if (GetHeaderField32 (header, TC_HEADER_OFFSET_MAGIC) != 0x54525545
! 414: || GetHeaderField32 (header, TC_HEADER_OFFSET_KEY_AREA_CRC) != GetCrc32 (header + HEADER_MASTER_KEYDATA_OFFSET, MASTER_KEYDATA_SIZE))
! 415: {
! 416: EncryptBuffer (header + HEADER_ENCRYPTED_DATA_OFFSET, HEADER_ENCRYPTED_DATA_SIZE, cryptoInfo);
! 417: continue;
! 418: }
! 419:
! 420: // Header decrypted
! 421: status = 0;
! 422:
! 423: // Volume size
! 424: cryptoInfo->VolumeSize = GetHeaderField64 (header, TC_HEADER_OFFSET_VOLUME_SIZE);
! 425:
! 426: // Encrypted area size and length
! 427: cryptoInfo->EncryptedAreaStart = GetHeaderField64 (header, TC_HEADER_OFFSET_ENCRYPTED_AREA_START);
! 428: cryptoInfo->EncryptedAreaLength = GetHeaderField64 (header, TC_HEADER_OFFSET_ENCRYPTED_AREA_LENGTH);
! 429:
! 430: memcpy (masterKey, header + HEADER_MASTER_KEYDATA_OFFSET, sizeof (masterKey));
! 431: EncryptBuffer (header + HEADER_ENCRYPTED_DATA_OFFSET, HEADER_ENCRYPTED_DATA_SIZE, cryptoInfo);
! 432:
! 433: if (retHeaderCryptoInfo)
! 434: goto ret;
! 435:
! 436: // Init the encryption algorithm with the decrypted master key
! 437: status = EAInit (cryptoInfo->ea, masterKey, cryptoInfo->ks);
! 438: if (status == ERR_CIPHER_INIT_FAILURE)
! 439: goto err;
! 440:
! 441: // The secondary master key (if cascade, multiple concatenated)
! 442: EAInit (cryptoInfo->ea, masterKey + EAGetKeySize (cryptoInfo->ea), cryptoInfo->ks2);
! 443: goto ret;
! 444: }
! 445:
! 446: status = ERR_PASSWORD_WRONG;
! 447:
! 448: err:
! 449: if (cryptoInfo != retHeaderCryptoInfo)
! 450: {
! 451: crypto_close(cryptoInfo);
! 452: *retInfo = NULL;
! 453: }
! 454:
! 455: ret:
! 456: burn (dk, sizeof(dk));
! 457: burn (masterKey, sizeof(masterKey));
! 458: return status;
! 459: }
! 460:
! 461: #endif // TC_WINDOWS_BOOT
! 462:
! 463:
1.1.1.12 root 464: #if !defined (DEVICE_DRIVER) && !defined (TC_WINDOWS_BOOT)
1.1 root 465:
466: #ifdef VOLFORMAT
1.1.1.10 root 467: #include "../Format/TcFormat.h"
1.1 root 468: #endif
469:
1.1.1.12 root 470: // Creates a volume header in memory
471: int VolumeWriteHeader (BOOL bBoot, char *header, int ea, int mode, Password *password,
472: int pkcs5_prf, char *masterKeydata, unsigned __int64 volumeCreationTime, PCRYPTO_INFO *retInfo,
473: unsigned __int64 volumeSize, unsigned __int64 hiddenVolumeSize,
474: unsigned __int64 encryptedAreaStart, unsigned __int64 encryptedAreaLength, BOOL bWipeMode)
1.1 root 475: {
476: unsigned char *p = (unsigned char *) header;
1.1.1.6 root 477: static KEY_INFO keyInfo;
1.1 root 478:
1.1.1.6 root 479: int nUserKeyLen = password->Length;
1.1 root 480: PCRYPTO_INFO cryptoInfo = crypto_open ();
1.1.1.12 root 481: static char dk[MASTER_KEYDATA_SIZE];
1.1 root 482: int x;
1.1.1.6 root 483: int retVal = 0;
1.1.1.12 root 484: int primaryKeyOffset;
1.1 root 485:
486: if (cryptoInfo == NULL)
487: return ERR_OUTOFMEMORY;
488:
1.1.1.12 root 489: memset (header, 0, HEADER_SIZE);
1.1.1.8 root 490:
491: #ifdef _WIN32
1.1.1.2 root 492: VirtualLock (&keyInfo, sizeof (keyInfo));
1.1.1.6 root 493: VirtualLock (&dk, sizeof (dk));
1.1.1.8 root 494: #endif
1.1 root 495:
1.1.1.6 root 496: /* Encryption setup */
1.1 root 497:
1.1.1.12 root 498: if (masterKeydata == NULL)
1.1.1.7 root 499: {
1.1.1.12 root 500: // We have no master key data (creating a new volume) so we'll use the TrueCrypt RNG to generate them
1.1.1.7 root 501:
1.1.1.12 root 502: int bytesNeeded;
503:
504: switch (mode)
505: {
506: case LRW:
507: case CBC:
508: case INNER_CBC:
509: case OUTER_CBC:
510:
511: // Deprecated/legacy modes of operation
512: bytesNeeded = LEGACY_VOL_IV_SIZE + EAGetKeySize (ea);
513:
514: /* In fact, this should never be the case since new volumes are not supposed to use
515: any deprecated mode of operation. */
516: return ERR_VOL_FORMAT_BAD;
517:
518: default:
519: bytesNeeded = EAGetKeySize (ea) * 2; // Size of primary + secondary key(s)
520: }
521:
522: if (!RandgetBytes (keyInfo.master_keydata, bytesNeeded, TRUE))
1.1.1.8 root 523: return ERR_CIPHER_INIT_WEAK_KEY;
1.1.1.7 root 524: }
1.1.1.2 root 525: else
1.1.1.12 root 526: {
527: // We already have existing master key data (the header is being re-encrypted)
528: memcpy (keyInfo.master_keydata, masterKeydata, MASTER_KEYDATA_SIZE);
529: }
1.1.1.2 root 530:
1.1.1.6 root 531: // User key
532: memcpy (keyInfo.userKey, password->Text, nUserKeyLen);
1.1 root 533: keyInfo.keyLength = nUserKeyLen;
1.1.1.12 root 534: keyInfo.noIterations = get_pkcs5_iteration_count (pkcs5_prf, bBoot);
1.1 root 535:
536: // User selected encryption algorithm
1.1.1.4 root 537: cryptoInfo->ea = ea;
1.1 root 538:
1.1.1.7 root 539: // Mode of operation
540: cryptoInfo->mode = mode;
541:
1.1.1.8 root 542: // Salt for header key derivation
1.1.1.12 root 543: if (!RandgetBytes (keyInfo.salt, PKCS5_SALT_SIZE, !bWipeMode))
1.1.1.8 root 544: return ERR_CIPHER_INIT_WEAK_KEY;
1.1 root 545:
1.1.1.12 root 546: // PBKDF2 (PKCS5) is used to derive primary header key(s) and secondary header key(s) (XTS) from the password/keyfiles
547: switch (pkcs5_prf)
1.1 root 548: {
1.1.1.12 root 549: case SHA512:
550: derive_key_sha512 (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
551: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
552: break;
553:
1.1.1.6 root 554: case SHA1:
1.1.1.12 root 555: // Deprecated/legacy
556: derive_key_sha1 (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
557: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
1.1.1.6 root 558: break;
559:
560: case RIPEMD160:
1.1.1.12 root 561: derive_key_ripemd160 (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
562: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
1.1.1.6 root 563: break;
564:
565: case WHIRLPOOL:
1.1.1.12 root 566: derive_key_whirlpool (keyInfo.userKey, keyInfo.keyLength, keyInfo.salt,
567: PKCS5_SALT_SIZE, keyInfo.noIterations, dk, GetMaxPkcs5OutSize());
1.1.1.6 root 568: break;
1.1.1.7 root 569:
1.1.1.12 root 570: default:
571: // Unknown/wrong ID
572: TC_THROW_FATAL_EXCEPTION;
573: }
1.1 root 574:
1.1.1.6 root 575: /* Header setup */
1.1 root 576:
577: // Salt
1.1.1.12 root 578: mputBytes (p, keyInfo.salt, PKCS5_SALT_SIZE);
1.1 root 579:
580: // Magic
1.1.1.8 root 581: mputLong (p, 0x54525545);
1.1 root 582:
583: // Header version
1.1.1.12 root 584: switch (mode)
585: {
586: case LRW:
587: case CBC:
588: case OUTER_CBC:
589: case INNER_CBC:
590: // Deprecated/legacy modes (used before TrueCrypt 5.0)
591: mputWord (p, 0x0002);
592: break;
593: default:
594: mputWord (p, VOLUME_HEADER_VERSION);
595: }
1.1 root 596:
597: // Required program version to handle this volume
1.1.1.12 root 598: switch (mode)
599: {
600: case LRW:
601: // Deprecated/legacy
602: mputWord (p, 0x0410);
603: break;
604: case OUTER_CBC:
605: case INNER_CBC:
606: // Deprecated/legacy
607: mputWord (p, 0x0300);
608: break;
609: case CBC:
610: // Deprecated/legacy
611: mputWord (p, hiddenVolumeSize > 0 ? 0x0300 : 0x0100);
612: break;
613: default:
614: mputWord (p, VOL_REQ_PROG_VERSION);
615: }
1.1 root 616:
1.1.1.12 root 617: // CRC of the master key data
618: x = GetCrc32(keyInfo.master_keydata, MASTER_KEYDATA_SIZE);
1.1 root 619: mputLong (p, x);
620:
621: // Time
622: {
1.1.1.8 root 623: #ifdef _WIN32
1.1 root 624: SYSTEMTIME st;
625: FILETIME ft;
626:
627: // Volume creation time
628: if (volumeCreationTime == 0)
629: {
630: GetLocalTime (&st);
631: SystemTimeToFileTime (&st, &ft);
632: }
633: else
634: {
635: ft.dwHighDateTime = (DWORD)(volumeCreationTime >> 32);
636: ft.dwLowDateTime = (DWORD)volumeCreationTime;
637: }
638: mputLong (p, ft.dwHighDateTime);
639: mputLong (p, ft.dwLowDateTime);
640:
1.1.1.6 root 641: // Header modification time/date
1.1 root 642: GetLocalTime (&st);
643: SystemTimeToFileTime (&st, &ft);
644: mputLong (p, ft.dwHighDateTime);
645: mputLong (p, ft.dwLowDateTime);
1.1.1.8 root 646:
647: #else
648: struct timeval tv;
649: unsigned __int64 ct, wt;
650: gettimeofday (&tv, NULL);
651:
652: // Unix time => Windows file time
653: wt = ((unsigned __int64)tv.tv_sec + 134774LL * 24 * 3600) * 1000LL * 1000 * 10;
654:
655: if (volumeCreationTime == 0)
656: ct = wt;
657: else
658: ct = volumeCreationTime;
659:
660: mputInt64 (p, ct);
661: mputInt64 (p, wt);
662: #endif
663:
1.1 root 664: }
665:
1.1.1.12 root 666: // Size of hidden volume (if any)
1.1.1.4 root 667: cryptoInfo->hiddenVolumeSize = hiddenVolumeSize;
668: mputInt64 (p, cryptoInfo->hiddenVolumeSize);
1.1.1.12 root 669:
1.1.1.7 root 670: cryptoInfo->hiddenVolume = cryptoInfo->hiddenVolumeSize != 0;
1.1.1.4 root 671:
1.1.1.12 root 672: // Volume size
673: cryptoInfo->VolumeSize.Value = volumeSize;
674: mputInt64 (p, volumeSize);
675:
676: // Encrypted area start
677: cryptoInfo->EncryptedAreaStart.Value = encryptedAreaStart;
678: mputInt64 (p, encryptedAreaStart);
679:
680: // Encrypted area size
681: cryptoInfo->EncryptedAreaLength.Value = encryptedAreaLength;
682: mputInt64 (p, encryptedAreaLength);
683:
684: // The master key data
685: memcpy (header + HEADER_MASTER_KEYDATA_OFFSET, keyInfo.master_keydata, MASTER_KEYDATA_SIZE);
1.1 root 686:
687:
1.1.1.6 root 688: /* Header encryption */
1.1 root 689:
1.1.1.12 root 690: switch (mode)
691: {
692: case LRW:
693: case CBC:
694: case INNER_CBC:
695: case OUTER_CBC:
696:
697: // For LRW (deprecated/legacy), the tweak key
698: // For CBC (deprecated/legacy), the IV/whitening seed
699: memcpy (cryptoInfo->k2, dk, LEGACY_VOL_IV_SIZE);
700: primaryKeyOffset = LEGACY_VOL_IV_SIZE;
701: break;
702:
703: default:
704: // The secondary key (if cascade, multiple concatenated)
705: memcpy (cryptoInfo->k2, dk + EAGetKeySize (cryptoInfo->ea), EAGetKeySize (cryptoInfo->ea));
706: primaryKeyOffset = 0;
707: }
708:
709: retVal = EAInit (cryptoInfo->ea, dk + primaryKeyOffset, cryptoInfo->ks);
710: if (retVal != ERR_SUCCESS)
1.1.1.6 root 711: return retVal;
1.1 root 712:
1.1.1.7 root 713: // Mode of operation
714: if (!EAInitMode (cryptoInfo))
715: return ERR_OUTOFMEMORY;
716:
1.1.1.12 root 717:
718: // Encrypt the entire header (except the salt)
719: EncryptBuffer (header + HEADER_ENCRYPTED_DATA_OFFSET,
720: HEADER_ENCRYPTED_DATA_SIZE,
1.1.1.7 root 721: cryptoInfo);
1.1 root 722:
723:
1.1.1.6 root 724: /* cryptoInfo setup for further use (disk format) */
1.1 root 725:
1.1.1.12 root 726: // Init with the master key(s)
727: retVal = EAInit (cryptoInfo->ea, keyInfo.master_keydata + primaryKeyOffset, cryptoInfo->ks);
728: if (retVal != ERR_SUCCESS)
1.1.1.6 root 729: return retVal;
1.1 root 730:
1.1.1.12 root 731: memcpy (cryptoInfo->master_keydata, keyInfo.master_keydata, MASTER_KEYDATA_SIZE);
732:
733: switch (cryptoInfo->mode)
734: {
735: case LRW:
736: case CBC:
737: case INNER_CBC:
738: case OUTER_CBC:
739:
740: // For LRW (deprecated/legacy), the tweak key
741: // For CBC (deprecated/legacy), the IV/whitening seed
742: memcpy (cryptoInfo->k2, keyInfo.master_keydata, LEGACY_VOL_IV_SIZE);
743: break;
744:
745: default:
746: // The secondary master key (if cascade, multiple concatenated)
747: memcpy (cryptoInfo->k2, keyInfo.master_keydata + EAGetKeySize (cryptoInfo->ea), EAGetKeySize (cryptoInfo->ea));
748: }
1.1 root 749:
1.1.1.7 root 750: // Mode of operation
751: if (!EAInitMode (cryptoInfo))
752: return ERR_OUTOFMEMORY;
753:
1.1.1.2 root 754:
1.1 root 755: #ifdef VOLFORMAT
1.1.1.3 root 756: if (showKeys)
1.1 root 757: {
758: BOOL dots3 = FALSE;
759: int i, j;
760:
1.1.1.4 root 761: j = EAGetKeySize (ea);
1.1 root 762:
1.1.1.6 root 763: if (j > NBR_KEY_BYTES_TO_DISPLAY)
1.1 root 764: {
765: dots3 = TRUE;
1.1.1.6 root 766: j = NBR_KEY_BYTES_TO_DISPLAY;
1.1 root 767: }
768:
1.1.1.12 root 769: MasterKeyGUIView[0] = 0;
1.1 root 770: for (i = 0; i < j; i++)
771: {
1.1.1.12 root 772: char tmp2[8] = {0};
773: sprintf (tmp2, "%02X", (int) (unsigned char) keyInfo.master_keydata[i + primaryKeyOffset]);
774: strcat (MasterKeyGUIView, tmp2);
1.1 root 775: }
776:
1.1.1.6 root 777: if (dots3)
1.1 root 778: {
1.1.1.12 root 779: strcat (MasterKeyGUIView, "...");
1.1 root 780: }
781:
1.1.1.12 root 782: SendMessage (hMasterKey, WM_SETTEXT, 0, (LPARAM) MasterKeyGUIView);
1.1 root 783:
1.1.1.12 root 784: HeaderKeyGUIView[0] = 0;
1.1.1.6 root 785: for (i = 0; i < NBR_KEY_BYTES_TO_DISPLAY; i++)
1.1 root 786: {
787: char tmp2[8];
1.1.1.12 root 788: sprintf (tmp2, "%02X", (int) (unsigned char) dk[primaryKeyOffset + i]);
789: strcat (HeaderKeyGUIView, tmp2);
1.1 root 790: }
791:
1.1.1.6 root 792: if (dots3)
1.1.1.3 root 793: {
1.1.1.12 root 794: strcat (HeaderKeyGUIView, "...");
1.1.1.3 root 795: }
796:
1.1.1.12 root 797: SendMessage (hHeaderKey, WM_SETTEXT, 0, (LPARAM) HeaderKeyGUIView);
1.1 root 798: }
1.1.1.12 root 799: #endif // #ifdef VOLFORMAT
1.1 root 800:
1.1.1.3 root 801: burn (dk, sizeof(dk));
1.1 root 802: burn (&keyInfo, sizeof (keyInfo));
803:
804: *retInfo = cryptoInfo;
805: return 0;
806: }
807:
1.1.1.12 root 808: #endif // !defined (DEVICE_DRIVER) && !defined (TC_WINDOWS_BOOT)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.