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

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.16! root        8:  by the TrueCrypt License 2.6 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
                    177: #      define PRAND_DISK_WIPE_PASSES   100
                    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.8   root      221:        GfCtx gf_ctx; 
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.16! root      229:        unsigned __int64 volume_creation_time;  // Legacy
        !           230:        unsigned __int64 header_creation_time;  // Legacy
1.1       root      231: 
1.1.1.8   root      232:        BOOL bProtectHiddenVolume;                      // Indicates whether the volume contains a hidden volume to be protected against overwriting
1.1.1.7   root      233:        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      234:        
                    235:        unsigned __int64 volDataAreaOffset;             // Absolute position, in bytes, of the first data sector of the volume.
1.1.1.13  root      236: 
1.1.1.5   root      237:        unsigned __int64 hiddenVolumeSize;              // Size of the hidden volume excluding the header (in bytes). Set to 0 for standard volumes.
1.1.1.7   root      238:        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      239:        unsigned __int64 hiddenVolumeProtectedSize;
1.1.1.14  root      240: 
                    241:        BOOL bPartitionInInactiveSysEncScope;   // If TRUE, the volume is a partition located on an encrypted system drive and mounted without pre-boot authentication.
                    242: 
                    243:        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      244: 
                    245:        uint16 RequiredProgramVersion;
                    246:        BOOL LegacyVolume;
                    247: 
1.1.1.14  root      248: #endif // TC_WINDOWS_BOOT
1.1.1.13  root      249: 
                    250:        UINT64_STRUCT VolumeSize;
                    251: 
                    252:        UINT64_STRUCT EncryptedAreaStart;
                    253:        UINT64_STRUCT EncryptedAreaLength;
1.1       root      254: 
1.1.1.15  root      255:        uint32 HeaderFlags;
                    256: 
1.1.1.5   root      257: } CRYPTO_INFO, *PCRYPTO_INFO;
1.1       root      258: 
                    259: PCRYPTO_INFO crypto_open (void);
                    260: void crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen);
                    261: void crypto_close (PCRYPTO_INFO cryptoInfo);
1.1.1.5   root      262: 
                    263: int CipherGetBlockSize (int cipher);
                    264: int CipherGetKeySize (int cipher);
                    265: int CipherGetKeyScheduleSize (int cipher);
                    266: char * CipherGetName (int cipher);
                    267: 
1.1.1.7   root      268: int CipherInit (int cipher, unsigned char *key, unsigned char *ks);
                    269: int EAInit (int ea, unsigned char *key, unsigned char *ks);
1.1.1.15  root      270: BOOL EAInitMode (PCRYPTO_INFO ci);
1.1.1.5   root      271: void EncipherBlock(int cipher, void *data, void *ks);
                    272: void DecipherBlock(int cipher, void *data, void *ks);
                    273: 
                    274: int EAGetFirst ();
                    275: int EAGetCount (void);
                    276: int EAGetNext (int previousEA);
                    277: char * EAGetName (char *buf, int ea);
1.1.1.8   root      278: int EAGetByName (char *name);
1.1.1.5   root      279: int EAGetKeySize (int ea);
1.1.1.8   root      280: int EAGetFirstMode (int ea);
                    281: int EAGetNextMode (int ea, int previousModeId);
                    282: char * EAGetModeName (int ea, int mode, BOOL capitalLetters);
1.1.1.5   root      283: int EAGetKeyScheduleSize (int ea);
                    284: int EAGetLargestKey ();
1.1.1.13  root      285: int EAGetLargestKeyForMode (int mode);
1.1.1.5   root      286: 
                    287: int EAGetCipherCount (int ea);
                    288: int EAGetFirstCipher (int ea);
                    289: int EAGetLastCipher (int ea);
                    290: int EAGetNextCipher (int ea, int previousCipherId);
                    291: int EAGetPreviousCipher (int ea, int previousCipherId);
1.1.1.11  root      292: int EAIsFormatEnabled (int ea);
1.1.1.13  root      293: BOOL EAIsModeSupported (int ea, int testedMode);
1.1.1.5   root      294: 
1.1.1.13  root      295: char *HashGetName (int hash_algo_id);
                    296: BOOL HashIsDeprecated (int hashId);
1.1.1.8   root      297: 
1.1.1.13  root      298: int GetMaxPkcs5OutSize (void);
1.1.1.7   root      299: 
1.1.1.13  root      300: void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.15  root      301: void EncryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.13  root      302: void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.15  root      303: void DecryptDataUnitsCurrentThread (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
1.1.1.13  root      304: void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
                    305: void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
                    306: #ifndef TC_NO_COMPILER_INT64
                    307: void EncryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    308: void DecryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    309: void EncryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    310: void DecryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    311: unsigned __int64 DataUnit2LRWIndex (unsigned __int64 dataUnit, int blockSize, PCRYPTO_INFO ci);
                    312: #endif // #ifndef TC_NO_COMPILER_INT64
1.1.1.9   root      313: 
1.1.1.13  root      314: #ifdef __cplusplus
                    315: }
                    316: #endif
1.1.1.5   root      317: 
1.1.1.7   root      318: #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.