// Returns sum of key sizes of all ciphers of the EA (in bytes) int EAGetKeySize (int ea) { int i = EAGetFirstCipher (ea); int size = CipherGetKeySize (i); while (i = EAGetNextCipher (ea, i)) { size += CipherGetKeySize (i); } return size; }
/* Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal) */ int CipherInit (int cipher, unsigned char *key, unsigned __int8 *ks) { int retVal = ERR_SUCCESS; switch (cipher) { case AES: #ifndef TC_WINDOWS_BOOT if (aes_encrypt_key256 (key, (aes_encrypt_ctx *) ks) != EXIT_SUCCESS) return ERR_CIPHER_INIT_FAILURE; if (aes_decrypt_key256 (key, (aes_decrypt_ctx *) (ks + sizeof(aes_encrypt_ctx))) != EXIT_SUCCESS) return ERR_CIPHER_INIT_FAILURE; #else if (aes_set_key (key, (length_type) CipherGetKeySize(AES), (aes_context *) ks) != 0) return ERR_CIPHER_INIT_FAILURE; #endif break; case SERPENT: serpent_set_key (key, ks); break; case TWOFISH: twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key); break; default: // Unknown/wrong cipher ID return ERR_CIPHER_INIT_FAILURE; } return retVal; }
/* Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal) */ int CipherInit (int cipher, unsigned char *key, unsigned __int8 *ks) { int retVal = ERR_SUCCESS; switch (cipher) { case AES: #ifndef TC_WINDOWS_BOOT if (aes_encrypt_key256 (key, (aes_encrypt_ctx *) ks) != EXIT_SUCCESS) return ERR_CIPHER_INIT_FAILURE; if (aes_decrypt_key256 (key, (aes_decrypt_ctx *) (ks + sizeof(aes_encrypt_ctx))) != EXIT_SUCCESS) return ERR_CIPHER_INIT_FAILURE; #else if (aes_set_key (key, (length_type) CipherGetKeySize(AES), (aes_context *) ks) != 0) return ERR_CIPHER_INIT_FAILURE; #endif break; case SERPENT: serpent_set_key (key, CipherGetKeySize(SERPENT) * 8, ks); break; case TWOFISH: twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key, CipherGetKeySize(TWOFISH) * 8); break; #ifndef TC_WINDOWS_BOOT case BLOWFISH: /* Deprecated/legacy */ BlowfishSetKey ((BF_KEY *)ks, CipherGetKeySize(BLOWFISH), key); break; case CAST: /* Deprecated/legacy */ Cast5SetKey ((CAST_KEY *) ks, CipherGetKeySize(CAST), key); break; case TRIPLEDES: /* Deprecated/legacy */ TripleDesSetKey (key, CipherGetKeySize (TRIPLEDES), (TDES_KEY *) ks); // Verify whether all three DES keys are mutually different if (((*((__int64 *) key) ^ *((__int64 *) key+1)) & 0xFEFEFEFEFEFEFEFEULL) == 0 || ((*((__int64 *) key+1) ^ *((__int64 *) key+2)) & 0xFEFEFEFEFEFEFEFEULL) == 0 || ((*((__int64 *) key) ^ *((__int64 *) key+2)) & 0xFEFEFEFEFEFEFEFEULL) == 0) retVal = ERR_CIPHER_INIT_WEAK_KEY; // Non-fatal error break; #endif // TC_WINDOWS_BOOT default: // Unknown/wrong cipher ID return ERR_CIPHER_INIT_FAILURE; } return retVal; }
// Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal) int EAInit (int ea, unsigned char *key, unsigned __int8 *ks) { int c, retVal = ERR_SUCCESS; if (ea == 0) return ERR_CIPHER_INIT_FAILURE; for (c = EAGetFirstCipher (ea); c != 0; c = EAGetNextCipher (ea, c)) { switch (CipherInit (c, key, ks)) { case ERR_CIPHER_INIT_FAILURE: return ERR_CIPHER_INIT_FAILURE; case ERR_CIPHER_INIT_WEAK_KEY: retVal = ERR_CIPHER_INIT_WEAK_KEY; // Non-fatal error break; } key += CipherGetKeySize (c); ks += CipherGetKeyScheduleSize (c); } return retVal; }