|
|
1.1.1.3 root 1: /* The source code contained in this file has been derived from the source code
2: of Encryption for the Masses 2.02a by Paul Le Roux. Modifications and
3: additions to that source code contained in this file are Copyright (c) 2004
4: TrueCrypt Team and Copyright (c) 2004 TrueCrypt Foundation. Unmodified
5: parts are Copyright (c) 1998-99 Paul Le Roux. This is a TrueCrypt Foundation
6: release. Please see the file license.txt for full license details. */
1.1 root 7:
8:
9: // User text input limits
10: #ifndef _DEBUG
11: #define MIN_PASSWORD 12
12: #else
13: #define MIN_PASSWORD 0
14: #endif
15: #define MAX_PASSWORD 64
16:
17: // User key
18: #define USERKEY_ITERATIONS 2000
19: #define USERKEY_SALT_SIZE 64
20:
21: // Disk key + IV
22: #define DISKKEY_SIZE 256
23: #define DISK_IV_SIZE 32
24:
25: // Volume header byte offsets
26: #define HEADER_USERKEY_SALT 0
27: #define HEADER_ENCRYPTEDDATA USERKEY_SALT_SIZE
28: #define HEADER_DISKKEY 256
29:
30: // Volume header sizes
31: #define HEADER_SIZE 512
32: #define HEADER_ENCRYPTEDDATASIZE (HEADER_SIZE - HEADER_ENCRYPTEDDATA)
33:
34: // PKCS5 PRF hash algorithm ID
35: #define SHA1 1
36: #define RIPEMD160 2
1.1.1.3 root 37: #define LAST_PRF_ID 2 // Last PRF used for volume encryption
1.1 root 38:
39: /* The encryption algorithm ID */
40: #define NONE 0
41: #define BLOWFISH 1
1.1.1.2 root 42: #define AES 2
43: #define CAST 3
1.1.1.4 ! root 44: #define TRIPLEDES 4
1.1 root 45: #define DES56 100 // Used only for DES test vectors
46:
1.1.1.4 ! root 47: #define LAST_CIPHER_ID 4 // Last cipher used for volume encryption
1.1 root 48:
49: // Length in bytes of the longest key used by encryption algorithms
50: #define MAX_CIPHER_KEY 56 // Blowfish 448 bits
51:
52: /* Length of scheduled keys */
53: #define DES_KS 128
54: #define TRIPLEDES_KS (DES_KS*3)
55: #define BLOWFISH_KS 4168
1.1.1.2 root 56: #define AES_KS 240
1.1 root 57: #define CAST_KS 128
58:
59: #define MAX_EXPANDED_KEY 4168
60:
61: #include "des.h"
62: #include "blowfish.h"
1.1.1.2 root 63: #include "aes.h"
1.1 root 64: #include "cast.h"
1.1.1.3 root 65: #include "sha1.h"
66: #include "rmd160.h"
1.1 root 67:
68: /* _cdecl is needed here because the device driver defaults to stdcall */
69: typedef void (_cdecl * sector_func) (unsigned long *, unsigned __int64, unsigned long,
70: unsigned char *, unsigned char *, int);
71:
72: typedef struct keyInfo_t
73: {
74: int noIterations; /* No.of times to iterate setup */
75: int keyLength; /* Length of the key */
76: char userKey[MAX_PASSWORD]; /* Max pass, WITHOUT +1 for the NULL */
77: char key_salt[USERKEY_SALT_SIZE]; /* Key setup salt */
78: char key[DISKKEY_SIZE]; /* The keying material itself */
79: } KEY_INFO, *PKEY_INFO;
80:
81: typedef struct CRYPTO_INFO_t
82: {
83: /* cipher information */
84: int cipher;
85: sector_func encrypt_sector;
86: sector_func decrypt_sector;
87: unsigned char iv[DISK_IV_SIZE];
88: unsigned char ks[MAX_EXPANDED_KEY];
89:
90: /* volume information */
91: unsigned char master_decrypted_key[DISKKEY_SIZE];
92: unsigned char key_salt[USERKEY_SALT_SIZE];
93: int noIterations;
94: int pkcs5;
95:
96: unsigned __int64 volume_creation_time;
97: unsigned __int64 header_creation_time;
98: } CRYPTO_INFO, *PCRYPTO_INFO;
99:
100: #define decipher_block(cipher, data, ks) \
101: {\
102: if (cipher == BLOWFISH) BF_decrypt ((void *) data, (void *) ks); \
1.1.1.2 root 103: else if (cipher == AES) aes_decrypt ((void *) data, (void *) data, (void *)((char *)(ks)+sizeof(aes_encrypt_ctx))); \
1.1 root 104: else if (cipher == DES56) des_encrypt ((void *) data, (void *) ks, 0); \
105: else if (cipher == CAST) CAST_ecb_encrypt((void *) data,(void *) data,(void*)ks,0); \
106: else if (cipher == TRIPLEDES) des_ecb3_encrypt ((void *) data,(void *) data, (void *) ks, \
107: (void*)((char*)ks+DES_KS),(void*)((char*)ks+DES_KS*2),0); \
108: }
109:
110: #define encipher_block(cipher, data, ks) \
111: {\
112: if (cipher == BLOWFISH) BF_encrypt ((void *) data, (void *) ks); \
1.1.1.2 root 113: else if (cipher == AES) aes_encrypt ((void *) data, (void *) data, (void *) ks); \
1.1 root 114: else if (cipher == DES56) des_encrypt ((void *) data, (void *) ks, 1); \
115: else if (cipher == CAST) CAST_ecb_encrypt((void *) data,(void *) data,(void*)ks,1); \
116: else if (cipher == TRIPLEDES) des_ecb3_encrypt ((void *) data,(void *) data, (void *) ks, \
117: (void*)((char*)ks+DES_KS),(void*)((char*)ks+DES_KS*2),1); \
118: }
119:
120: #define init_cipher(cipher, key, ks) \
121: {\
122: if (cipher == BLOWFISH) BF_set_key ((void*)ks, 56, (void*) (key)); \
1.1.1.2 root 123: else if (cipher == AES) {aes_encrypt_key((void*)(key), 32, (void*)ks); aes_decrypt_key((void*)(key), 32, (void*)((char *)(ks)+sizeof(aes_encrypt_ctx)));} \
1.1 root 124: else if (cipher == DES56) des_key_sched ((void*) (key), (void*)ks); \
125: else if (cipher == CAST) CAST_set_key((void*)ks, 16, (void*)(key)); \
126: else if (cipher == TRIPLEDES) { \
127: des_key_sched ((void*) (key), (void*)ks); \
128: des_key_sched ((void*) ((char*)(key)+8), (void*)((char*)ks+DES_KS)); \
129: des_key_sched ((void*) ((char*)(key)+16), (void*)((char*)ks+DES_KS*2)); \
130: } \
131: }
132:
133:
134: PCRYPTO_INFO crypto_open (void);
135: void crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen);
136: void crypto_close (PCRYPTO_INFO cryptoInfo);
137: int get_block_size (int cipher);
138: int get_key_size (int cipher);
139: char * get_cipher_name (int cipher);
140: char * get_hash_name (int pkcs5);
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.