|
|
1.1.1.11 root 1: /*
1.1.1.13 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
1.1.1.20 root 6: the original source code (contained in this file) and all other portions
1.1.1.21! root 7: of this file are Copyright (c) 2003-2010 TrueCrypt Developers Association
! 8: and are governed by the TrueCrypt License 3.0 the full text of which is
1.1.1.20 root 9: contained in the file License.txt included in TrueCrypt binary and source
10: code distribution packages. */
1.1 root 11:
1.1.1.5 root 12: /* Update the following when adding a new cipher or EA:
13:
14: Crypto.h:
15: ID #define
16: MAX_EXPANDED_KEY #define
17:
18: Crypto.c:
19: Ciphers[]
20: EncryptionAlgorithms[]
21: CipherInit()
22: EncipherBlock()
23: DecipherBlock()
1.1.1.7 root 24:
1.1.1.5 root 25: */
1.1 root 26:
1.1.1.7 root 27: #ifndef CRYPTO_H
28: #define CRYPTO_H
29:
1.1.1.13 root 30: #include "Tcdefs.h"
1.1 root 31:
1.1.1.13 root 32: #ifdef __cplusplus
33: extern "C" {
34: #endif
35:
36: // Encryption data unit size, which may differ from the sector size and must always be 512
37: #define ENCRYPTION_DATA_UNIT_SIZE 512
38:
39: // Size of the salt (in bytes)
40: #define PKCS5_SALT_SIZE 64
41:
42: // Size of the volume header area containing concatenated master key(s) and secondary key(s) (XTS mode)
43: #define MASTER_KEYDATA_SIZE 256
44:
45: // Size of the deprecated volume header item containing either an IV seed (CBC mode) or tweak key (LRW mode)
46: #define LEGACY_VOL_IV_SIZE 32
1.1 root 47:
1.1.1.13 root 48: // The first PRF to try when mounting
49: #define FIRST_PRF_ID 1
50:
51: // Hash algorithms (pseudorandom functions).
52: enum
53: {
54: RIPEMD160 = FIRST_PRF_ID,
55: #ifndef TC_WINDOWS_BOOT
56: SHA512,
57: WHIRLPOOL,
58: SHA1, // Deprecated/legacy
59: #endif
60: HASH_ENUM_END_ID
61: };
62:
63: // The last PRF to try when mounting and also the number of implemented PRFs
64: #define LAST_PRF_ID (HASH_ENUM_END_ID - 1)
1.1.1.7 root 65:
66: #define RIPEMD160_BLOCKSIZE 64
67: #define RIPEMD160_DIGESTSIZE 20
1.1.1.13 root 68:
69: #define SHA1_BLOCKSIZE 64
70: #define SHA1_DIGESTSIZE 20
71:
72: #define SHA512_BLOCKSIZE 128
73: #define SHA512_DIGESTSIZE 64
74:
1.1.1.7 root 75: #define WHIRLPOOL_BLOCKSIZE 64
76: #define WHIRLPOOL_DIGESTSIZE 64
1.1.1.13 root 77:
1.1.1.7 root 78: #define MAX_DIGESTSIZE WHIRLPOOL_DIGESTSIZE
79:
1.1.1.13 root 80: #define DEFAULT_HASH_ALGORITHM FIRST_PRF_ID
81: #define DEFAULT_HASH_ALGORITHM_BOOT RIPEMD160
82:
83: // The mode of operation used for newly created volumes and first to try when mounting
84: #define FIRST_MODE_OF_OPERATION_ID 1
1.1.1.5 root 85:
86: // Modes of operation
87: enum
88: {
1.1.1.13 root 89: /* If you add/remove a mode, update the following: GetMaxPkcs5OutSize(), EAInitMode() */
90:
91: XTS = FIRST_MODE_OF_OPERATION_ID,
92: #ifndef TC_WINDOWS_BOOT
93: LRW, // Deprecated/legacy
1.1.1.8 root 94: CBC, // Deprecated/legacy
95: OUTER_CBC, // Deprecated/legacy
96: INNER_CBC, // Deprecated/legacy
1.1.1.13 root 97: #endif
98: MODE_ENUM_END_ID
1.1.1.5 root 99: };
1.1 root 100:
1.1.1.13 root 101:
102: // The last mode of operation to try when mounting and also the number of implemented modes
103: #define LAST_MODE_OF_OPERATION (MODE_ENUM_END_ID - 1)
104:
105: // Ciphertext/plaintext block size for XTS mode (in bytes)
106: #define BYTES_PER_XTS_BLOCK 16
107:
108: // Number of ciphertext/plaintext blocks per XTS data unit
109: #define BLOCKS_PER_XTS_DATA_UNIT (ENCRYPTION_DATA_UNIT_SIZE / BYTES_PER_XTS_BLOCK)
110:
111:
1.1.1.5 root 112: // Cipher IDs
1.1.1.13 root 113: enum
114: {
115: NONE = 0,
116: AES,
117: SERPENT,
118: TWOFISH,
119: #ifndef TC_WINDOWS_BOOT
120: BLOWFISH, // Deprecated/legacy
121: CAST, // Deprecated/legacy
1.1.1.16 root 122: TRIPLEDES // Deprecated/legacy
1.1.1.13 root 123: #endif
124: };
1.1 root 125:
1.1.1.5 root 126: typedef struct
127: {
128: int Id; // Cipher ID
129: char *Name; // Name
130: int BlockSize; // Block size (bytes)
131: int KeySize; // Key size (bytes)
132: int KeyScheduleSize; // Scheduled key size (bytes)
133: } Cipher;
1.1 root 134:
1.1.1.5 root 135: typedef struct
136: {
137: int Ciphers[4]; // Null terminated array of ciphers used by encryption algorithm
1.1.1.13 root 138: int Modes[LAST_MODE_OF_OPERATION + 1]; // Null terminated array of modes of operation
1.1.1.11 root 139: int FormatEnabled;
1.1.1.5 root 140: } EncryptionAlgorithm;
141:
1.1.1.9 root 142: typedef struct
143: {
144: int Id; // Hash ID
145: char *Name; // Name
1.1.1.13 root 146: BOOL Deprecated;
147: BOOL SystemEncryption; // Available for system encryption
1.1.1.9 root 148: } Hash;
149:
1.1.1.5 root 150: // Maxium length of scheduled key
1.1.1.14 root 151: #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)
1.1.1.13 root 152: # define AES_KS (sizeof(aes_encrypt_ctx) + sizeof(aes_decrypt_ctx))
153: #else
154: # define AES_KS (sizeof(aes_context))
155: #endif
1.1.1.5 root 156: #define SERPENT_KS (140 * 4)
1.1.1.14 root 157:
158: #ifdef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE
159:
160: # ifdef TC_WINDOWS_BOOT_AES
161: # define MAX_EXPANDED_KEY AES_KS
162: # elif defined (TC_WINDOWS_BOOT_SERPENT)
163: # define MAX_EXPANDED_KEY SERPENT_KS
164: # elif defined (TC_WINDOWS_BOOT_TWOFISH)
165: # define MAX_EXPANDED_KEY TWOFISH_KS
166: # endif
167:
168: #else
169:
1.1.1.5 root 170: #define MAX_EXPANDED_KEY (AES_KS + SERPENT_KS + TWOFISH_KS)
1.1 root 171:
1.1.1.14 root 172: #endif
173:
1.1.1.15 root 174: #ifdef DEBUG
175: # define PRAND_DISK_WIPE_PASSES 3
176: #else
1.1.1.18 root 177: # define PRAND_DISK_WIPE_PASSES 256
1.1.1.15 root 178: #endif
1.1.1.13 root 179:
1.1.1.14 root 180: #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)
1.1.1.13 root 181: # include "Aes.h"
182: #else
183: # include "AesSmall.h"
184: #endif
1.1.1.9 root 185:
1.1.1.21! root 186: #include "Aes_hw_cpu.h"
1.1.1.7 root 187: #include "Blowfish.h"
188: #include "Cast.h"
189: #include "Des.h"
190: #include "Serpent.h"
191: #include "Twofish.h"
192:
1.1.1.14 root 193: #include "Rmd160.h"
194: #ifndef TC_WINDOWS_BOOT
195: # include "Sha1.h"
196: # include "Sha2.h"
197: # include "Whirlpool.h"
1.1.1.7 root 198: #endif
1.1 root 199:
1.1.1.8 root 200: #include "GfMul.h"
1.1.1.11 root 201: #include "Password.h"
1.1.1.8 root 202:
1.1 root 203: typedef struct keyInfo_t
204: {
1.1.1.13 root 205: int noIterations; /* Number of times to iterate (PKCS-5) */
1.1 root 206: int keyLength; /* Length of the key */
1.1.1.13 root 207: __int8 userKey[MAX_PASSWORD]; /* Password (to which keyfiles may have been applied). WITHOUT +1 for the null terminator. */
208: __int8 salt[PKCS5_SALT_SIZE]; /* PKCS-5 salt */
209: __int8 master_keydata[MASTER_KEYDATA_SIZE]; /* Concatenated master primary and secondary key(s) (XTS mode). For LRW (deprecated/legacy), it contains the tweak key before the master key(s). For CBC (deprecated/legacy), it contains the IV seed before the master key(s). */
1.1 root 210: } KEY_INFO, *PKEY_INFO;
211:
212: typedef struct CRYPTO_INFO_t
213: {
1.1.1.8 root 214: int ea; /* Encryption algorithm ID */
1.1.1.13 root 215: int mode; /* Mode of operation (e.g., XTS) */
216: unsigned __int8 ks[MAX_EXPANDED_KEY]; /* Primary key schedule (if it is a cascade, it conatins multiple concatenated keys) */
217: unsigned __int8 ks2[MAX_EXPANDED_KEY]; /* Secondary key schedule (if cascade, multiple concatenated) for XTS mode. */
218:
1.1.1.15 root 219: BOOL hiddenVolume; // Indicates whether the volume is mounted/mountable as hidden volume
220:
1.1.1.13 root 221: #ifndef TC_WINDOWS_BOOT
1.1.1.17 root 222: uint16 HeaderVersion;
223:
1.1.1.8 root 224: GfCtx gf_ctx;
1.1.1.5 root 225:
1.1.1.13 root 226: unsigned __int8 master_keydata[MASTER_KEYDATA_SIZE]; /* This holds the volume header area containing concatenated master key(s) and secondary key(s) (XTS mode). For LRW (deprecated/legacy), it contains the tweak key before the master key(s). For CBC (deprecated/legacy), it contains the IV seed before the master key(s). */
227: unsigned __int8 k2[MASTER_KEYDATA_SIZE]; /* For XTS, this contains the secondary key (if cascade, multiple concatenated). For LRW (deprecated/legacy), it contains the tweak key. For CBC (deprecated/legacy), it contains the IV seed. */
228: unsigned __int8 salt[PKCS5_SALT_SIZE];
1.1 root 229: int noIterations;
230: int pkcs5;
231:
1.1.1.17 root 232: uint64 volume_creation_time; // Legacy
233: uint64 header_creation_time; // Legacy
1.1 root 234:
1.1.1.8 root 235: BOOL bProtectHiddenVolume; // Indicates whether the volume contains a hidden volume to be protected against overwriting
1.1.1.7 root 236: BOOL bHiddenVolProtectionAction; // TRUE if a write operation has been denied by the driver in order to prevent the hidden volume from being overwritten (set to FALSE upon volume mount).
1.1.1.15 root 237:
1.1.1.17 root 238: uint64 volDataAreaOffset; // Absolute position, in bytes, of the first data sector of the volume.
1.1.1.13 root 239:
1.1.1.17 root 240: uint64 hiddenVolumeSize; // Size of the hidden volume excluding the header (in bytes). Set to 0 for standard volumes.
241: uint64 hiddenVolumeOffset; // Absolute position, in bytes, of the first hidden volume data sector within the host volume (provided that there is a hidden volume within). This must be set for all hidden volumes; in case of a normal volume, this variable is only used when protecting a hidden volume within it.
242: uint64 hiddenVolumeProtectedSize;
1.1.1.14 root 243:
244: BOOL bPartitionInInactiveSysEncScope; // If TRUE, the volume is a partition located on an encrypted system drive and mounted without pre-boot authentication.
245:
246: UINT64_STRUCT FirstDataUnitNo; // First data unit number of the volume. This is 0 for file-hosted and non-system partition-hosted volumes. For partitions within key scope of system encryption this reflects real physical offset within the device (this is used e.g. when such a partition is mounted as a regular volume without pre-boot authentication).
1.1.1.15 root 247:
248: uint16 RequiredProgramVersion;
249: BOOL LegacyVolume;
250:
1.1.1.21! root 251: uint32 SectorSize;
! 252:
! 253: #endif // !TC_WINDOWS_BOOT
1.1.1.13 root 254:
255: UINT64_STRUCT VolumeSize;
256:
257: UINT64_STRUCT EncryptedAreaStart;
258: UINT64_STRUCT EncryptedAreaLength;
1.1 root 259:
1.1.1.15 root 260: uint32 HeaderFlags;
261:
1.1.1.5 root 262: } CRYPTO_INFO, *PCRYPTO_INFO;
1.1 root 263:
264: PCRYPTO_INFO crypto_open (void);
265: void crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen);
266: void crypto_close (PCRYPTO_INFO cryptoInfo);
1.1.1.5 root 267:
268: int CipherGetBlockSize (int cipher);
269: int CipherGetKeySize (int cipher);
270: int CipherGetKeyScheduleSize (int cipher);
1.1.1.21! root 271: BOOL CipherSupportsIntraDataUnitParallelization (int cipher);
1.1.1.5 root 272: char * CipherGetName (int cipher);
273:
1.1.1.7 root 274: int CipherInit (int cipher, unsigned char *key, unsigned char *ks);
275: int EAInit (int ea, unsigned char *key, unsigned char *ks);
1.1.1.15 root 276: BOOL EAInitMode (PCRYPTO_INFO ci);
1.1.1.5 root 277: void EncipherBlock(int cipher, void *data, void *ks);
278: void DecipherBlock(int cipher, void *data, void *ks);
1.1.1.21! root 279: #ifndef TC_WINDOWS_BOOT
! 280: void EncipherBlocks (int cipher, void *dataPtr, void *ks, size_t blockCount);
! 281: void DecipherBlocks (int cipher, void *dataPtr, void *ks, size_t blockCount);
! 282: #endif
1.1.1.5 root 283:
284: int EAGetFirst ();
285: int EAGetCount (void);
286: int EAGetNext (int previousEA);
287: char * EAGetName (char *buf, int ea);
1.1.1.8 root 288: int EAGetByName (char *name);
1.1.1.5 root 289: int EAGetKeySize (int ea);
1.1.1.8 root 290: int EAGetFirstMode (int ea);
291: int EAGetNextMode (int ea, int previousModeId);
292: char * EAGetModeName (int ea, int mode, BOOL capitalLetters);
1.1.1.5 root 293: int EAGetKeyScheduleSize (int ea);
294: int EAGetLargestKey ();
1.1.1.13 root 295: int EAGetLargestKeyForMode (int mode);
1.1.1.5 root 296:
297: int EAGetCipherCount (int ea);
298: int EAGetFirstCipher (int ea);
299: int EAGetLastCipher (int ea);
300: int EAGetNextCipher (int ea, int previousCipherId);
301: int EAGetPreviousCipher (int ea, int previousCipherId);
1.1.1.11 root 302: int EAIsFormatEnabled (int ea);
1.1.1.13 root 303: BOOL EAIsModeSupported (int ea, int testedMode);
1.1.1.5 root 304:
1.1.1.13 root 305: char *HashGetName (int hash_algo_id);
306: BOOL HashIsDeprecated (int hashId);
1.1.1.8 root 307:
1.1.1.13 root 308: int GetMaxPkcs5OutSize (void);
1.1.1.7 root 309:
1.1.1.19 root 310: void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, uint32 nbrUnits, PCRYPTO_INFO ci);
1.1.1.15 root 311: void EncryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.19 root 312: void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, uint32 nbrUnits, PCRYPTO_INFO ci);
1.1.1.15 root 313: void DecryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.13 root 314: void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
315: void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
316: #ifndef TC_NO_COMPILER_INT64
1.1.1.17 root 317: void EncryptBufferLRW128 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
318: void DecryptBufferLRW128 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
319: void EncryptBufferLRW64 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
320: void DecryptBufferLRW64 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
321: uint64 DataUnit2LRWIndex (uint64 dataUnit, int blockSize, PCRYPTO_INFO ci);
1.1.1.13 root 322: #endif // #ifndef TC_NO_COMPILER_INT64
1.1.1.9 root 323:
1.1.1.21! root 324: BOOL IsAesHwCpuSupported ();
! 325: void EnableHwEncryption (BOOL enable);
! 326: BOOL IsHwEncryptionEnabled ();
! 327:
1.1.1.13 root 328: #ifdef __cplusplus
329: }
330: #endif
1.1.1.5 root 331:
1.1.1.7 root 332: #endif /* CRYPTO_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.