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