Annotation of truecrypt/common/crypto.h, revision 1.1.1.18

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
1.1.1.17  root        7:  this file are Copyright (c) 2003-2009 TrueCrypt Foundation and are governed
1.1.1.18! root        8:  by the TrueCrypt License 2.7 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
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.7   root      186: #include "Blowfish.h"
                    187: #include "Cast.h"
                    188: #include "Des.h"
                    189: #include "Serpent.h"
                    190: #include "Twofish.h"
                    191: 
1.1.1.14  root      192: #include "Rmd160.h"
                    193: #ifndef TC_WINDOWS_BOOT
                    194: #      include "Sha1.h"
                    195: #      include "Sha2.h"
                    196: #      include "Whirlpool.h"
1.1.1.7   root      197: #endif
1.1       root      198: 
1.1.1.8   root      199: #include "GfMul.h"
1.1.1.11  root      200: #include "Password.h"
1.1.1.8   root      201: 
1.1       root      202: typedef struct keyInfo_t
                    203: {
1.1.1.13  root      204:        int noIterations;                                       /* Number of times to iterate (PKCS-5) */
1.1       root      205:        int keyLength;                                          /* Length of the key */
1.1.1.13  root      206:        __int8 userKey[MAX_PASSWORD];           /* Password (to which keyfiles may have been applied). WITHOUT +1 for the null terminator. */
                    207:        __int8 salt[PKCS5_SALT_SIZE];           /* PKCS-5 salt */
                    208:        __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      209: } KEY_INFO, *PKEY_INFO;
                    210: 
                    211: typedef struct CRYPTO_INFO_t
                    212: {
1.1.1.8   root      213:        int ea;                                                                 /* Encryption algorithm ID */
1.1.1.13  root      214:        int mode;                                                               /* Mode of operation (e.g., XTS) */
                    215:        unsigned __int8 ks[MAX_EXPANDED_KEY];   /* Primary key schedule (if it is a cascade, it conatins multiple concatenated keys) */
                    216:        unsigned __int8 ks2[MAX_EXPANDED_KEY];  /* Secondary key schedule (if cascade, multiple concatenated) for XTS mode. */
                    217: 
1.1.1.15  root      218:        BOOL hiddenVolume;                                              // Indicates whether the volume is mounted/mountable as hidden volume
                    219: 
1.1.1.13  root      220: #ifndef TC_WINDOWS_BOOT
1.1.1.17  root      221:        uint16 HeaderVersion;
                    222: 
1.1.1.8   root      223:        GfCtx gf_ctx; 
1.1.1.5   root      224: 
1.1.1.13  root      225:        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). */
                    226:        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. */
                    227:        unsigned __int8 salt[PKCS5_SALT_SIZE];
1.1       root      228:        int noIterations;
                    229:        int pkcs5;
                    230: 
1.1.1.17  root      231:        uint64 volume_creation_time;    // Legacy
                    232:        uint64 header_creation_time;    // Legacy
1.1       root      233: 
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:        
1.1.1.17  root      237:        uint64 volDataAreaOffset;               // Absolute position, in bytes, of the first data sector of the volume.
1.1.1.13  root      238: 
1.1.1.17  root      239:        uint64 hiddenVolumeSize;                // Size of the hidden volume excluding the header (in bytes). Set to 0 for standard volumes.
                    240:        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.
                    241:        uint64 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
1.1.1.17  root      309: void EncryptBufferLRW128 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    310: void DecryptBufferLRW128 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    311: void EncryptBufferLRW64 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    312: void DecryptBufferLRW64 (byte *buffer, uint64 length, uint64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    313: uint64 DataUnit2LRWIndex (uint64 dataUnit, int blockSize, PCRYPTO_INFO ci);
1.1.1.13  root      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 */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.