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

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.14! root      169: #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)
1.1.1.13  root      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)
1.1.1.14! root      175: 
        !           176: #ifdef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE
        !           177: 
        !           178: #      ifdef TC_WINDOWS_BOOT_AES
        !           179: #              define MAX_EXPANDED_KEY AES_KS
        !           180: #      elif defined (TC_WINDOWS_BOOT_SERPENT)
        !           181: #              define MAX_EXPANDED_KEY SERPENT_KS
        !           182: #      elif defined (TC_WINDOWS_BOOT_TWOFISH)
        !           183: #              define MAX_EXPANDED_KEY TWOFISH_KS
        !           184: #      endif
        !           185: 
        !           186: #else
        !           187: 
1.1.1.5   root      188: #define MAX_EXPANDED_KEY       (AES_KS + SERPENT_KS + TWOFISH_KS)
1.1       root      189: 
1.1.1.14! root      190: #endif
        !           191: 
1.1.1.13  root      192: #define PRAND_DISK_WIPE_PASSES 200
                    193: 
1.1.1.14! root      194: #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES)
1.1.1.13  root      195: #      include "Aes.h"
                    196: #else
                    197: #      include "AesSmall.h"
                    198: #endif
1.1.1.9   root      199: 
1.1.1.7   root      200: #include "Blowfish.h"
                    201: #include "Cast.h"
                    202: #include "Des.h"
                    203: #include "Serpent.h"
                    204: #include "Twofish.h"
                    205: 
1.1.1.14! root      206: #include "Rmd160.h"
        !           207: #ifndef TC_WINDOWS_BOOT
        !           208: #      include "Sha1.h"
        !           209: #      include "Sha2.h"
        !           210: #      include "Whirlpool.h"
1.1.1.7   root      211: #endif
1.1       root      212: 
1.1.1.8   root      213: #include "GfMul.h"
1.1.1.11  root      214: #include "Password.h"
1.1.1.8   root      215: 
1.1       root      216: typedef struct keyInfo_t
                    217: {
1.1.1.13  root      218:        int noIterations;                                       /* Number of times to iterate (PKCS-5) */
1.1       root      219:        int keyLength;                                          /* Length of the key */
1.1.1.13  root      220:        __int8 userKey[MAX_PASSWORD];           /* Password (to which keyfiles may have been applied). WITHOUT +1 for the null terminator. */
                    221:        __int8 salt[PKCS5_SALT_SIZE];           /* PKCS-5 salt */
                    222:        __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      223: } KEY_INFO, *PKEY_INFO;
                    224: 
                    225: typedef struct CRYPTO_INFO_t
                    226: {
1.1.1.8   root      227:        int ea;                                                                 /* Encryption algorithm ID */
1.1.1.13  root      228:        int mode;                                                               /* Mode of operation (e.g., XTS) */
                    229:        unsigned __int8 ks[MAX_EXPANDED_KEY];   /* Primary key schedule (if it is a cascade, it conatins multiple concatenated keys) */
                    230:        unsigned __int8 ks2[MAX_EXPANDED_KEY];  /* Secondary key schedule (if cascade, multiple concatenated) for XTS mode. */
                    231: 
                    232: #ifndef TC_WINDOWS_BOOT
1.1.1.8   root      233:        GfCtx gf_ctx; 
1.1.1.5   root      234: 
1.1.1.13  root      235:        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). */
                    236:        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. */
                    237:        unsigned __int8 salt[PKCS5_SALT_SIZE];
1.1       root      238:        int noIterations;
                    239:        int pkcs5;
                    240: 
                    241:        unsigned __int64 volume_creation_time;
                    242:        unsigned __int64 header_creation_time;
                    243: 
1.1.1.5   root      244:        // Hidden volume status & parameters
1.1.1.7   root      245:        BOOL hiddenVolume;                                      // Indicates whether the volume is mounted/mountable as hidden volume
1.1.1.8   root      246:        BOOL bProtectHiddenVolume;                      // Indicates whether the volume contains a hidden volume to be protected against overwriting
1.1.1.7   root      247:        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      248: 
1.1.1.5   root      249:        unsigned __int64 hiddenVolumeSize;              // Size of the hidden volume excluding the header (in bytes). Set to 0 for standard volumes.
1.1.1.7   root      250:        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.14! root      251:        unsigned __int64 volDataAreaOffset;             // Absolute position, in bytes, of the first data sector of the volume.
        !           252: 
        !           253:        BOOL bPartitionInInactiveSysEncScope;   // If TRUE, the volume is a partition located on an encrypted system drive and mounted without pre-boot authentication.
        !           254: 
        !           255:        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).
        !           256: #endif // TC_WINDOWS_BOOT
1.1.1.13  root      257: 
                    258:        UINT64_STRUCT VolumeSize;
                    259: 
                    260:        UINT64_STRUCT EncryptedAreaStart;
                    261:        UINT64_STRUCT EncryptedAreaLength;
1.1       root      262: 
1.1.1.5   root      263: } CRYPTO_INFO, *PCRYPTO_INFO;
1.1       root      264: 
                    265: PCRYPTO_INFO crypto_open (void);
                    266: void crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen);
                    267: void crypto_close (PCRYPTO_INFO cryptoInfo);
1.1.1.5   root      268: 
                    269: int CipherGetBlockSize (int cipher);
                    270: int CipherGetKeySize (int cipher);
                    271: int CipherGetKeyScheduleSize (int cipher);
                    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.8   root      276: int 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);
                    279: 
                    280: int EAGetFirst ();
                    281: int EAGetCount (void);
                    282: int EAGetNext (int previousEA);
                    283: char * EAGetName (char *buf, int ea);
1.1.1.8   root      284: int EAGetByName (char *name);
1.1.1.5   root      285: int EAGetKeySize (int ea);
1.1.1.8   root      286: int EAGetFirstMode (int ea);
                    287: int EAGetNextMode (int ea, int previousModeId);
                    288: char * EAGetModeName (int ea, int mode, BOOL capitalLetters);
1.1.1.5   root      289: int EAGetKeyScheduleSize (int ea);
                    290: int EAGetLargestKey ();
1.1.1.13  root      291: int EAGetLargestKeyForMode (int mode);
1.1.1.5   root      292: 
                    293: int EAGetCipherCount (int ea);
                    294: int EAGetFirstCipher (int ea);
                    295: int EAGetLastCipher (int ea);
                    296: int EAGetNextCipher (int ea, int previousCipherId);
                    297: int EAGetPreviousCipher (int ea, int previousCipherId);
1.1.1.11  root      298: int EAIsFormatEnabled (int ea);
1.1.1.13  root      299: BOOL EAIsModeSupported (int ea, int testedMode);
1.1.1.5   root      300: 
1.1.1.13  root      301: char *HashGetName (int hash_algo_id);
                    302: BOOL HashIsDeprecated (int hashId);
1.1.1.8   root      303: 
1.1.1.13  root      304: int GetMaxPkcs5OutSize (void);
1.1.1.7   root      305: 
1.1.1.13  root      306: void EncryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
                    307: void DecryptDataUnits (unsigned __int8 *buf, const UINT64_STRUCT *structUnitNo, TC_LARGEST_COMPILER_UINT nbrUnits, PCRYPTO_INFO ci);
                    308: void EncryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
                    309: void DecryptBuffer (unsigned __int8 *buf, TC_LARGEST_COMPILER_UINT len, PCRYPTO_INFO cryptoInfo);
                    310: #ifndef TC_NO_COMPILER_INT64
                    311: void EncryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    312: void DecryptBufferLRW128 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    313: void EncryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    314: void DecryptBufferLRW64 (unsigned __int8 *buffer, unsigned __int64 length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo);
                    315: unsigned __int64 DataUnit2LRWIndex (unsigned __int64 dataUnit, int blockSize, PCRYPTO_INFO ci);
                    316: #endif // #ifndef TC_NO_COMPILER_INT64
1.1.1.9   root      317: 
1.1.1.13  root      318: #ifdef __cplusplus
                    319: }
                    320: #endif
1.1.1.5   root      321: 
1.1.1.7   root      322: #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.