|
|
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 ! 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.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: 48: // Volume header byte offsets 1.1.1.13! root 49: #define HEADER_SALT_OFFSET 0 ! 50: #define HEADER_ENCRYPTED_DATA_OFFSET PKCS5_SALT_SIZE ! 51: #define HEADER_MASTER_KEYDATA_OFFSET 256 1.1 root 52: 53: // Volume header sizes 54: #define HEADER_SIZE 512 1.1.1.13! root 55: #define HEADER_ENCRYPTED_DATA_SIZE (HEADER_SIZE - HEADER_ENCRYPTED_DATA_OFFSET) 1.1 root 56: 1.1.1.5 root 57: /* The offset, in bytes, of the hidden volume header position from the end of the file (a positive value). 58: The extra offset (SECTOR_SIZE * 2) was added because FAT file system fills the last sector with zeroes 59: (marked as free; observed when quick format was performed using the OS format tool). One extra sector was 60: added to the offset for future expandability (should the header size increase, or should header backup be 61: introduced). */ 62: #define HIDDEN_VOL_HEADER_OFFSET (HEADER_SIZE + SECTOR_SIZE * 2) 63: 1.1.1.13! root 64: ! 65: // The first PRF to try when mounting ! 66: #define FIRST_PRF_ID 1 ! 67: ! 68: // Hash algorithms (pseudorandom functions). ! 69: enum ! 70: { ! 71: RIPEMD160 = FIRST_PRF_ID, ! 72: #ifndef TC_WINDOWS_BOOT ! 73: SHA512, ! 74: WHIRLPOOL, ! 75: SHA1, // Deprecated/legacy ! 76: #endif ! 77: HASH_ENUM_END_ID ! 78: }; ! 79: ! 80: // The last PRF to try when mounting and also the number of implemented PRFs ! 81: #define LAST_PRF_ID (HASH_ENUM_END_ID - 1) 1.1.1.7 root 82: 83: #define RIPEMD160_BLOCKSIZE 64 84: #define RIPEMD160_DIGESTSIZE 20 1.1.1.13! root 85: ! 86: #define SHA1_BLOCKSIZE 64 ! 87: #define SHA1_DIGESTSIZE 20 ! 88: ! 89: #define SHA512_BLOCKSIZE 128 ! 90: #define SHA512_DIGESTSIZE 64 ! 91: 1.1.1.7 root 92: #define WHIRLPOOL_BLOCKSIZE 64 93: #define WHIRLPOOL_DIGESTSIZE 64 1.1.1.13! root 94: 1.1.1.7 root 95: #define MAX_DIGESTSIZE WHIRLPOOL_DIGESTSIZE 96: 1.1.1.13! root 97: #define DEFAULT_HASH_ALGORITHM FIRST_PRF_ID ! 98: #define DEFAULT_HASH_ALGORITHM_BOOT RIPEMD160 ! 99: ! 100: // The mode of operation used for newly created volumes and first to try when mounting ! 101: #define FIRST_MODE_OF_OPERATION_ID 1 1.1.1.5 root 102: 103: // Modes of operation 104: enum 105: { 1.1.1.13! root 106: /* If you add/remove a mode, update the following: GetMaxPkcs5OutSize(), EAInitMode() */ ! 107: ! 108: XTS = FIRST_MODE_OF_OPERATION_ID, ! 109: #ifndef TC_WINDOWS_BOOT ! 110: LRW, // Deprecated/legacy 1.1.1.8 root 111: CBC, // Deprecated/legacy 112: OUTER_CBC, // Deprecated/legacy 113: INNER_CBC, // Deprecated/legacy 1.1.1.13! root 114: #endif ! 115: MODE_ENUM_END_ID 1.1.1.5 root 116: }; 1.1 root 117: 1.1.1.13! root 118: ! 119: // The last mode of operation to try when mounting and also the number of implemented modes ! 120: #define LAST_MODE_OF_OPERATION (MODE_ENUM_END_ID - 1) ! 121: ! 122: // Ciphertext/plaintext block size for XTS mode (in bytes) ! 123: #define BYTES_PER_XTS_BLOCK 16 ! 124: ! 125: // Number of ciphertext/plaintext blocks per XTS data unit ! 126: #define BLOCKS_PER_XTS_DATA_UNIT (ENCRYPTION_DATA_UNIT_SIZE / BYTES_PER_XTS_BLOCK) ! 127: ! 128: 1.1.1.5 root 129: // Cipher IDs 1.1.1.13! root 130: enum ! 131: { ! 132: NONE = 0, ! 133: AES, ! 134: SERPENT, ! 135: TWOFISH, ! 136: #ifndef TC_WINDOWS_BOOT ! 137: BLOWFISH, // Deprecated/legacy ! 138: CAST, // Deprecated/legacy ! 139: TRIPLEDES, // Deprecated/legacy ! 140: DES56 // Deprecated/legacy (used only by Triple DES) ! 141: #endif ! 142: }; 1.1 root 143: 1.1.1.5 root 144: typedef struct 145: { 146: int Id; // Cipher ID 147: char *Name; // Name 148: int BlockSize; // Block size (bytes) 149: int KeySize; // Key size (bytes) 150: int KeyScheduleSize; // Scheduled key size (bytes) 151: } Cipher; 1.1 root 152: 1.1.1.5 root 153: typedef struct 154: { 155: int Ciphers[4]; // Null terminated array of ciphers used by encryption algorithm 1.1.1.13! root 156: int Modes[LAST_MODE_OF_OPERATION + 1]; // Null terminated array of modes of operation 1.1.1.11 root 157: int FormatEnabled; 1.1.1.5 root 158: } EncryptionAlgorithm; 159: 1.1.1.9 root 160: typedef struct 161: { 162: int Id; // Hash ID 163: char *Name; // Name 1.1.1.13! root 164: BOOL Deprecated; ! 165: BOOL SystemEncryption; // Available for system encryption 1.1.1.9 root 166: } Hash; 167: 1.1.1.5 root 168: // Maxium length of scheduled key 1.1.1.13! root 169: #ifndef TC_WINDOWS_BOOT ! 170: # define AES_KS (sizeof(aes_encrypt_ctx) + sizeof(aes_decrypt_ctx)) ! 171: #else ! 172: # define AES_KS (sizeof(aes_context)) ! 173: #endif 1.1.1.5 root 174: #define SERPENT_KS (140 * 4) 175: #define MAX_EXPANDED_KEY (AES_KS + SERPENT_KS + TWOFISH_KS) 1.1 root 176: 1.1.1.13! root 177: #define PRAND_DISK_WIPE_PASSES 200 ! 178: ! 179: #ifndef TC_WINDOWS_BOOT ! 180: # include "Aes.h" ! 181: #else ! 182: # include "AesSmall.h" ! 183: #endif 1.1.1.9 root 184: 1.1.1.7 root 185: #include "Blowfish.h" 186: #include "Cast.h" 187: #include "Des.h" 188: #include "Serpent.h" 189: #include "Twofish.h" 190: 191: #ifndef LINUX_DRIVER 1.1.1.13! root 192: # include "Rmd160.h" ! 193: # ifndef TC_WINDOWS_BOOT ! 194: # include "Sha1.h" ! 195: # include "Sha2.h" ! 196: # include "Whirlpool.h" ! 197: # endif 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: ! 219: #ifndef TC_WINDOWS_BOOT 1.1.1.8 root 220: GfCtx gf_ctx; 1.1.1.13! root 221: #endif // TC_WINDOWS_BOOT 1.1.1.5 root 222: 1.1.1.13! root 223: 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). */ ! 224: 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. */ ! 225: unsigned __int8 salt[PKCS5_SALT_SIZE]; 1.1 root 226: int noIterations; 227: int pkcs5; 228: 1.1.1.13! root 229: #ifndef TC_NO_COMPILER_INT64 1.1 root 230: unsigned __int64 volume_creation_time; 231: unsigned __int64 header_creation_time; 1.1.1.13! root 232: #endif 1.1 root 233: 1.1.1.5 root 234: // Hidden volume status & parameters 1.1.1.7 root 235: BOOL hiddenVolume; // Indicates whether the volume is mounted/mountable as hidden volume 1.1.1.8 root 236: BOOL bProtectHiddenVolume; // Indicates whether the volume contains a hidden volume to be protected against overwriting 1.1.1.7 root 237: 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.13! root 238: ! 239: #ifndef TC_NO_COMPILER_INT64 1.1.1.5 root 240: unsigned __int64 hiddenVolumeSize; // Size of the hidden volume excluding the header (in bytes). Set to 0 for standard volumes. 1.1.1.7 root 241: 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.13! root 242: #endif ! 243: ! 244: UINT64_STRUCT VolumeSize; ! 245: ! 246: UINT64_STRUCT EncryptedAreaStart; ! 247: UINT64_STRUCT EncryptedAreaLength; 1.1 root 248: 1.1.1.5 root 249: } CRYPTO_INFO, *PCRYPTO_INFO; 1.1 root 250: 251: PCRYPTO_INFO crypto_open (void); 252: void crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen); 253: void crypto_close (PCRYPTO_INFO cryptoInfo); 1.1.1.5 root 254: 255: int CipherGetBlockSize (int cipher); 256: int CipherGetKeySize (int cipher); 257: int CipherGetKeyScheduleSize (int cipher); 258: char * CipherGetName (int cipher); 259: 1.1.1.7 root 260: int CipherInit (int cipher, unsigned char *key, unsigned char *ks); 261: int EAInit (int ea, unsigned char *key, unsigned char *ks); 1.1.1.8 root 262: int EAInitMode (PCRYPTO_INFO ci); 1.1.1.5 root 263: void EncipherBlock(int cipher, void *data, void *ks); 264: void DecipherBlock(int cipher, void *data, void *ks); 265: 266: int EAGetFirst (); 267: int EAGetCount (void); 268: int EAGetNext (int previousEA); 269: char * EAGetName (char *buf, int ea); 1.1.1.8 root 270: int EAGetByName (char *name); 1.1.1.5 root 271: int EAGetKeySize (int ea); 1.1.1.8 root 272: int EAGetFirstMode (int ea); 273: int EAGetNextMode (int ea, int previousModeId); 274: char * EAGetModeName (int ea, int mode, BOOL capitalLetters); 1.1.1.5 root 275: int EAGetKeyScheduleSize (int ea); 276: int EAGetLargestKey (); 1.1.1.13! root 277: int EAGetLargestKeyForMode (int mode); 1.1.1.5 root 278: 279: int EAGetCipherCount (int ea); 280: int EAGetFirstCipher (int ea); 281: int EAGetLastCipher (int ea); 282: int EAGetNextCipher (int ea, int previousCipherId); 283: int EAGetPreviousCipher (int ea, int previousCipherId); 1.1.1.11 root 284: int EAIsFormatEnabled (int ea); 1.1.1.13! root 285: BOOL EAIsModeSupported (int ea, int testedMode); 1.1.1.5 root 286: 1.1.1.13! root 287: char *HashGetName (int hash_algo_id); ! 288: BOOL HashIsDeprecated (int hashId); 1.1.1.8 root 289: 1.1.1.13! root 290: int GetMaxPkcs5OutSize (void); 1.1.1.7 root 291: 1.1.1.13! root 292: void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci); ! 293: void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci); ! 294: void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo); ! 295: void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo); ! 296: #ifndef TC_NO_COMPILER_INT64 ! 297: void EncryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo); ! 298: void DecryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo); ! 299: void EncryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo); ! 300: void DecryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo); ! 301: unsigned __int64 DataUnit2LRWIndex (unsigned __int64 dataUnit, int blockSize, PCRYPTO_INFO ci); ! 302: #endif // #ifndef TC_NO_COMPILER_INT64 1.1.1.9 root 303: 1.1.1.13! root 304: #ifdef __cplusplus ! 305: } ! 306: #endif 1.1.1.5 root 307: 1.1.1.7 root 308: #endif /* CRYPTO_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.