|
|
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
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.15! root 8: by the TrueCrypt License 2.5 the full text of which is contained in the
1.1.1.13 root 9: file License.txt included in TrueCrypt binary and source code distribution
1.1.1.11 root 10: 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
122: TRIPLEDES, // Deprecated/legacy
123: DES56 // Deprecated/legacy (used only by Triple DES)
124: #endif
125: };
1.1 root 126:
1.1.1.5 root 127: typedef struct
128: {
129: int Id; // Cipher ID
130: char *Name; // Name
131: int BlockSize; // Block size (bytes)
132: int KeySize; // Key size (bytes)
133: int KeyScheduleSize; // Scheduled key size (bytes)
134: } Cipher;
1.1 root 135:
1.1.1.5 root 136: typedef struct
137: {
138: int Ciphers[4]; // Null terminated array of ciphers used by encryption algorithm
1.1.1.13 root 139: int Modes[LAST_MODE_OF_OPERATION + 1]; // Null terminated array of modes of operation
1.1.1.11 root 140: int FormatEnabled;
1.1.1.5 root 141: } EncryptionAlgorithm;
142:
1.1.1.9 root 143: typedef struct
144: {
145: int Id; // Hash ID
146: char *Name; // Name
1.1.1.13 root 147: BOOL Deprecated;
148: BOOL SystemEncryption; // Available for system encryption
1.1.1.9 root 149: } Hash;
150:
1.1.1.5 root 151: // Maxium length of scheduled key
1.1.1.14 root 152: #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)
1.1.1.13 root 153: # define AES_KS (sizeof(aes_encrypt_ctx) + sizeof(aes_decrypt_ctx))
154: #else
155: # define AES_KS (sizeof(aes_context))
156: #endif
1.1.1.5 root 157: #define SERPENT_KS (140 * 4)
1.1.1.14 root 158:
159: #ifdef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE
160:
161: # ifdef TC_WINDOWS_BOOT_AES
162: # define MAX_EXPANDED_KEY AES_KS
163: # elif defined (TC_WINDOWS_BOOT_SERPENT)
164: # define MAX_EXPANDED_KEY SERPENT_KS
165: # elif defined (TC_WINDOWS_BOOT_TWOFISH)
166: # define MAX_EXPANDED_KEY TWOFISH_KS
167: # endif
168:
169: #else
170:
1.1.1.5 root 171: #define MAX_EXPANDED_KEY (AES_KS + SERPENT_KS + TWOFISH_KS)
1.1 root 172:
1.1.1.14 root 173: #endif
174:
1.1.1.15! root 175: #ifdef DEBUG
! 176: # define PRAND_DISK_WIPE_PASSES 3
! 177: #else
! 178: # define PRAND_DISK_WIPE_PASSES 100
! 179: #endif
1.1.1.13 root 180:
1.1.1.14 root 181: #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)
1.1.1.13 root 182: # include "Aes.h"
183: #else
184: # include "AesSmall.h"
185: #endif
1.1.1.9 root 186:
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.8 root 222: GfCtx gf_ctx;
1.1.1.5 root 223:
1.1.1.13 root 224: 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). */
225: 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. */
226: unsigned __int8 salt[PKCS5_SALT_SIZE];
1.1 root 227: int noIterations;
228: int pkcs5;
229:
230: unsigned __int64 volume_creation_time;
231: unsigned __int64 header_creation_time;
232:
1.1.1.5 root 233: // Hidden volume status & parameters
1.1.1.8 root 234: BOOL bProtectHiddenVolume; // Indicates whether the volume contains a hidden volume to be protected against overwriting
1.1.1.7 root 235: 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 236:
! 237: unsigned __int64 volDataAreaOffset; // Absolute position, in bytes, of the first data sector of the volume.
1.1.1.13 root 238:
1.1.1.5 root 239: unsigned __int64 hiddenVolumeSize; // Size of the hidden volume excluding the header (in bytes). Set to 0 for standard volumes.
1.1.1.7 root 240: unsigned __int64 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.
1.1.1.15! root 241: unsigned __int64 hiddenVolumeProtectedSize;
1.1.1.14 root 242:
243: BOOL bPartitionInInactiveSysEncScope; // If TRUE, the volume is a partition located on an encrypted system drive and mounted without pre-boot authentication.
244:
245: 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 246:
! 247: uint16 RequiredProgramVersion;
! 248: BOOL LegacyVolume;
! 249:
1.1.1.14 root 250: #endif // TC_WINDOWS_BOOT
1.1.1.13 root 251:
252: UINT64_STRUCT VolumeSize;
253:
254: UINT64_STRUCT EncryptedAreaStart;
255: UINT64_STRUCT EncryptedAreaLength;
1.1 root 256:
1.1.1.15! root 257: uint32 HeaderFlags;
! 258:
1.1.1.5 root 259: } CRYPTO_INFO, *PCRYPTO_INFO;
1.1 root 260:
261: PCRYPTO_INFO crypto_open (void);
262: void crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen);
263: void crypto_close (PCRYPTO_INFO cryptoInfo);
1.1.1.5 root 264:
265: int CipherGetBlockSize (int cipher);
266: int CipherGetKeySize (int cipher);
267: int CipherGetKeyScheduleSize (int cipher);
268: char * CipherGetName (int cipher);
269:
1.1.1.7 root 270: int CipherInit (int cipher, unsigned char *key, unsigned char *ks);
271: int EAInit (int ea, unsigned char *key, unsigned char *ks);
1.1.1.15! root 272: BOOL EAInitMode (PCRYPTO_INFO ci);
1.1.1.5 root 273: void EncipherBlock(int cipher, void *data, void *ks);
274: void DecipherBlock(int cipher, void *data, void *ks);
275:
276: int EAGetFirst ();
277: int EAGetCount (void);
278: int EAGetNext (int previousEA);
279: char * EAGetName (char *buf, int ea);
1.1.1.8 root 280: int EAGetByName (char *name);
1.1.1.5 root 281: int EAGetKeySize (int ea);
1.1.1.8 root 282: int EAGetFirstMode (int ea);
283: int EAGetNextMode (int ea, int previousModeId);
284: char * EAGetModeName (int ea, int mode, BOOL capitalLetters);
1.1.1.5 root 285: int EAGetKeyScheduleSize (int ea);
286: int EAGetLargestKey ();
1.1.1.13 root 287: int EAGetLargestKeyForMode (int mode);
1.1.1.5 root 288:
289: int EAGetCipherCount (int ea);
290: int EAGetFirstCipher (int ea);
291: int EAGetLastCipher (int ea);
292: int EAGetNextCipher (int ea, int previousCipherId);
293: int EAGetPreviousCipher (int ea, int previousCipherId);
1.1.1.11 root 294: int EAIsFormatEnabled (int ea);
1.1.1.13 root 295: BOOL EAIsModeSupported (int ea, int testedMode);
1.1.1.5 root 296:
1.1.1.13 root 297: char *HashGetName (int hash_algo_id);
298: BOOL HashIsDeprecated (int hashId);
1.1.1.8 root 299:
1.1.1.13 root 300: int GetMaxPkcs5OutSize (void);
1.1.1.7 root 301:
1.1.1.13 root 302: void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.15! root 303: void EncryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.13 root 304: void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.15! root 305: void DecryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.13 root 306: void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
307: void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
308: #ifndef TC_NO_COMPILER_INT64
309: void EncryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
310: void DecryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
311: void EncryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
312: void DecryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
313: unsigned __int64 DataUnit2LRWIndex (unsigned __int64 dataUnit, int blockSize, PCRYPTO_INFO ci);
314: #endif // #ifndef TC_NO_COMPILER_INT64
1.1.1.9 root 315:
1.1.1.13 root 316: #ifdef __cplusplus
317: }
318: #endif
1.1.1.5 root 319:
1.1.1.7 root 320: #endif /* CRYPTO_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.