void ltc_init(void) { int cipherID; unsigned char key[ENCRYPTION_KEY_LENGTH]; #if defined(ENCRYPTION_CTR) || defined(ENCRYPTION_CBC) unsigned char IV[ENCRYPTION_BLOCK_LENGTH]; #endif TRACE_DEBUG("LTC: Initializing ...\n\r"); // Register cipher register_cipher(&CIPHER_DESC); cipherID = find_cipher(CIPHER_NAME); // Load key ASCII2Hex(ENCRYPTION_KEY, key, ENCRYPTION_KEY_LENGTH); #if defined(ENCRYPTION_CTR) || defined(ENCRYPTION_CBC) // Load IV ASCII2Hex(ENCRYPTION_IV, IV, ENCRYPTION_BLOCK_LENGTH); #endif // Start decryption mode #if defined(ENCRYPTION_ECB) ecb_start(cipherID, key, ENCRYPTION_KEY_LENGTH, 0, &sECB); #elif defined(ENCRYPTION_CBC) cbc_start(cipherID, IV, key, ENCRYPTION_KEY_LENGTH, 0, &sCBC); #elif defined(ENCRYPTION_CTR) ctr_start(cipherID, IV, key, ENCRYPTION_KEY_LENGTH, 0, CTR_COUNTER_BIG_ENDIAN, &sCTR); #endif TRACE_DEBUG("LTC: Initialization done.\n\r"); }
C4Err ECB_Encrypt(Cipher_Algorithm algorithm, const void * key, const void * in, size_t bytesIn, void * out ) { int err = kC4Err_NoErr; int status = CRYPT_OK; symmetric_ECB ECB; int keylen = 0; int cipher = -1; switch(algorithm) { case kCipher_Algorithm_AES128: keylen = 128 >> 3; cipher = find_cipher("aes"); break; case kCipher_Algorithm_AES192: keylen = 192 >> 3; cipher = find_cipher("aes"); break; case kCipher_Algorithm_AES256: keylen = 256 >> 3; cipher = find_cipher("aes"); break; case kCipher_Algorithm_2FISH256: keylen = 256 >> 3; cipher = find_cipher("twofish"); break; default: RETERR(kC4Err_BadCipherNumber); } status = ecb_start(cipher, key, keylen, 0, &ECB ); CKSTAT; status = ecb_encrypt(in, out, bytesIn, &ECB); CKSTAT; done: ecb_done(&ECB); if(status != CRYPT_OK) err = sCrypt2C4Err(status); return err; }
void ltc_init_3DES_ECB(void) { int cipherID; unsigned char key[ENCRYPTION_KEY_LENGTH]; TRACE_DEBUG("LTC: Initializing ECB...\n\r"); // Register cipher register_cipher(&des3_desc); cipherID = find_cipher("3des"); // Load key ASCII2Hex(ENCRYPTION_KEY, key, ENCRYPTION_KEY_LENGTH); // Start decryption mode ecb_start(cipherID, key, ENCRYPTION_KEY_LENGTH, 0, &sECB); TRACE_DEBUG("LTC: Initialization done.\n\r"); }
static int EncryptECB( int cipher, int rounds, int counterMode, unsigned char *iv, unsigned char *key, unsigned long keyLength, unsigned char *data, unsigned long dataLength, unsigned char *dest ) { int status; symmetric_ECB state; status = ecb_start(cipher, key, keyLength, rounds, &state); if (status == CRYPT_OK) { status = ecb_encrypt(data, dest, dataLength, &state); ecb_done(&state); } return status; }
TEE_Result tee_cipher_init3(void *ctx, uint32_t algo, TEE_OperationMode mode, const uint8_t *key1, size_t key1_len, const uint8_t *key2, size_t key2_len, const uint8_t *iv, size_t iv_len) { TEE_Result res; int ltc_res, ltc_cipherindex; uint8_t *real_key, key_array[24]; size_t real_key_len; struct symmetric_CTS *cts; res = tee_algo_to_ltc_cipherindex(algo, <c_cipherindex); if (res != TEE_SUCCESS) return TEE_ERROR_NOT_SUPPORTED; switch (algo) { case TEE_ALG_AES_ECB_NOPAD: case TEE_ALG_DES_ECB_NOPAD: ltc_res = ecb_start( ltc_cipherindex, key1, key1_len, 0, (symmetric_ECB *)ctx); break; case TEE_ALG_DES3_ECB_NOPAD: /* either des3 or des2, depending on the size of the key */ get_des2_key(key1, key1_len, key_array, &real_key, &real_key_len); ltc_res = ecb_start( ltc_cipherindex, real_key, real_key_len, 0, (symmetric_ECB *)ctx); break; case TEE_ALG_AES_CBC_NOPAD: case TEE_ALG_DES_CBC_NOPAD: if (iv_len != (size_t)cipher_descriptor[ltc_cipherindex].block_length) return TEE_ERROR_BAD_PARAMETERS; ltc_res = cbc_start( ltc_cipherindex, iv, key1, key1_len, 0, (symmetric_CBC *)ctx); break; case TEE_ALG_DES3_CBC_NOPAD: /* either des3 or des2, depending on the size of the key */ get_des2_key(key1, key1_len, key_array, &real_key, &real_key_len); if (iv_len != (size_t)cipher_descriptor[ltc_cipherindex].block_length) return TEE_ERROR_BAD_PARAMETERS; ltc_res = cbc_start( ltc_cipherindex, iv, real_key, real_key_len, 0, (symmetric_CBC *)ctx); break; case TEE_ALG_AES_CTR: if (iv_len != (size_t)cipher_descriptor[ltc_cipherindex].block_length) return TEE_ERROR_BAD_PARAMETERS; ltc_res = ctr_start( ltc_cipherindex, iv, key1, key1_len, 0, CTR_COUNTER_BIG_ENDIAN, (symmetric_CTR *)ctx); break; case TEE_ALG_AES_CTS: cts = (struct symmetric_CTS *)ctx; res = tee_cipher_init3( (void *)(&(cts->ecb)), TEE_ALG_AES_ECB_NOPAD, mode, key1, key1_len, key2, key2_len, iv, iv_len); if (res != TEE_SUCCESS) return res; res = tee_cipher_init3( (void *)(&(cts->cbc)), TEE_ALG_AES_CBC_NOPAD, mode, key1, key1_len, key2, key2_len, iv, iv_len); if (res != TEE_SUCCESS) return res; ltc_res = CRYPT_OK; break; case TEE_ALG_AES_XTS: if (key1_len != key2_len) return TEE_ERROR_BAD_PARAMETERS; ltc_res = xts_start( ltc_cipherindex, key1, key2, key1_len, 0, (symmetric_xts *)ctx); break; default: return TEE_ERROR_NOT_SUPPORTED; } if (ltc_res == CRYPT_OK) return TEE_SUCCESS; else return TEE_ERROR_BAD_STATE; }