blob: e4d47c5a96e1396f2b80db3f1eef484cd5cc0f7f [file] [log] [blame]
#include <common.h>
#include <tee.h>
#include <malloc.h>
#include <asm/arch/cpu.h>
#include "asr_hw_aes.h"
#ifdef CONFIG_TEE_OS
#include "cipher_optee.h"
#endif
#ifdef CONFIG_ASR_TE200
#include "te200_cipher.h"
#endif
#ifdef CONFIG_ASR_BCM
#include "bcm/asr_cipher.h"
#endif
#include "../geu/asr_geu.h"
int aes_ecb_encrypt(const uint8_t *key, uint32_t keylen, bool use_rkek,
const void *plaintext, void *ciphertext, size_t size)
{
#ifdef CONFIG_ASR_TE200
if (cpu_is_asr1903_b0()) {
#ifdef CONFIG_TEE_OS
return aes_ecb_encrypt_optee((uint8_t *)key, keylen, use_rkek,
(void *)plaintext, ciphertext, size);
#else
return aes_ecb_encrypt_te200((uint8_t *)key, keylen, use_rkek,
(void *)plaintext, ciphertext, size);
#endif
}
#endif
#ifdef CONFIG_ASR_BCM
#ifdef CONFIG_TEE_OS
return aes_ecb_encrypt_optee((uint8_t *)key, keylen, use_rkek,
(void *)plaintext, ciphertext, size);
#else
return aes_ecb_encrypt_bcm((uint8_t *)key, keylen, use_rkek,
(void *)plaintext, ciphertext, size);
#endif
#endif
return aes_ecb_encrypt_geu(key, keylen, use_rkek, plaintext, ciphertext, size);
}
int aes_ecb_decrypt(const uint8_t *key, uint32_t keylen, bool use_rkek,
const void *ciphertext, void *plaintext, size_t size)
{
#ifdef CONFIG_ASR_TE200
if (cpu_is_asr1903_b0()) {
#ifdef CONFIG_TEE_OS
return aes_ecb_decrypt_optee((uint8_t *)key, keylen, use_rkek,
(void *)ciphertext, plaintext, size);
#else
return aes_ecb_decrypt_te200((uint8_t *)key, keylen, use_rkek,
(void *)ciphertext, plaintext, size);
#endif
}
#endif
#ifdef CONFIG_ASR_BCM
#ifdef CONFIG_TEE_OS
return aes_ecb_decrypt_optee((uint8_t *)key, keylen, use_rkek,
(void *)ciphertext, plaintext, size);
#else
return aes_ecb_decrypt_bcm((uint8_t *)key, keylen, use_rkek,
(void *)ciphertext, plaintext, size);
#endif
#endif
return aes_ecb_decrypt_geu(key, keylen, use_rkek, ciphertext, plaintext, size);
}
int aes_cbc_encrypt(const uint8_t *iv, const uint8_t *key, uint32_t keylen,
bool use_rkek, const void *plaintext, void *ciphertext, size_t size)
{
uint8_t in_iv[16] = {0};
memcpy(in_iv, iv, 16);
#ifdef CONFIG_ASR_TE200
if (cpu_is_asr1903_b0()) {
#ifdef CONFIG_TEE_OS
return aes_cbc_encrypt_optee(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)plaintext, ciphertext, size);
#else
return aes_cbc_encrypt_te200(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)plaintext, ciphertext, size);
#endif
}
#endif
#ifdef CONFIG_ASR_BCM
#ifdef CONFIG_TEE_OS
return aes_cbc_encrypt_optee(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)plaintext, ciphertext, size);
#else
return aes_cbc_encrypt_bcm(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)plaintext, ciphertext, size);
#endif
#endif
return aes_cbc_encrypt_geu(in_iv, key, keylen, use_rkek, plaintext, ciphertext, size);
}
int aes_cbc_decrypt(const uint8_t *iv, const uint8_t *key, uint32_t keylen,
bool use_rkek, const void *ciphertext, void *plaintext, size_t size)
{
uint8_t in_iv[16] = {0};
memcpy(in_iv, iv, 16);
#ifdef CONFIG_ASR_TE200
if (cpu_is_asr1903_b0()) {
#ifdef CONFIG_TEE_OS
return aes_cbc_decrypt_optee(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)ciphertext, plaintext, size);
#else
return aes_cbc_decrypt_te200(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)ciphertext, plaintext, size);
#endif
}
#endif
#ifdef CONFIG_ASR_BCM
#ifdef CONFIG_TEE_OS
return aes_cbc_decrypt_optee(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)ciphertext, plaintext, size);
#else
return aes_cbc_decrypt_bcm(in_iv, (uint8_t *)key, keylen,
use_rkek, (void *)ciphertext, plaintext, size);
#endif
#endif
return aes_cbc_decrypt_geu(in_iv, key, keylen, use_rkek, ciphertext, plaintext, size);
}
// #define ASR_HW_AES_TEST
#ifdef ASR_HW_AES_TEST
__attribute__ ((aligned (16)))
static unsigned int key128_ecb[] = {
0x67452301, 0xefcdab89, 0x67452301, 0xefcdab89
};
__attribute__ ((aligned (16)))
static unsigned int pt_ecb[] = {
0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c,
0x23222120, 0x27262524, 0x2b2a2928, 0x2f2e2d2c,
0x33323130, 0x37363534, 0x3b3a3938, 0x3f3e3d3c,
};
__attribute__ ((aligned (16)))
static unsigned int ct_ecb[] = {
0x246295ad, 0x6426631f, 0x62d522a4, 0xa3aa53f7,
0x41c3c69f, 0xda7c6ad1, 0xe647f9a0, 0x32abc4c9,
0xb51b9cdf, 0x5139d75b, 0xa643b208, 0x94e6f543,
0x38da0179, 0xd7bc88bb, 0x52ac77a6, 0x05ed84b4,
};
static int hw_aes_ecb_test(void *pverfiy_buf)
{
int ret = 0;
/* step1: aes ecb encrypt test*/
ret = aes_ecb_encrypt(key128_ecb, sizeof(key128_ecb), false,
pt_ecb, pverfiy_buf, sizeof(pt_ecb));
if (ret) {
printf("aes ecb encrypt err\r\n");
return ret;
}
if (memcmp(pverfiy_buf, ct_ecb, sizeof(ct_ecb))) {
printf("aes ecb encrypt err\r\n");
return ret;
}
printf("aes ecb encrypt test: pass\r\n");
/* step2: aes ecb decrypt test*/
ret = aes_ecb_decrypt(key128_ecb, sizeof(key128_ecb), false,
ct_ecb, pverfiy_buf, sizeof(ct_ecb));
if (ret) {
printf("aes ecb decrypt err\r\n");
return ret;
}
if (memcmp(pverfiy_buf, pt_ecb, sizeof(pt_ecb))) {
printf("aes ecb decrypt err\r\n");
return ret;
}
printf("aes ecb decrypt test: pass\r\n");
return ret;
}
static int hw_aes_ecb_rkek_test(void)
{
int ret = 0;
uint32_t data_size = 512;
uint8_t *plaintext = NULL;
uint8_t *ciphertext = NULL;
plaintext = (uint8_t *)malloc(data_size);
if (plaintext == NULL) {
ret = -1;
goto exit;
}
ciphertext = (uint8_t *)malloc(data_size);
if (ciphertext == NULL) {
ret = -1;
goto exit;
}
/* test aes ecb without hwkey encrypt/decrypt */
memset(plaintext, 0xAB, data_size);
memset(ciphertext, 0, data_size);
if (0 > aes_ecb_encrypt(NULL, 32, true, plaintext, ciphertext, data_size)){
ret = -1;
goto exit;
}
if (0 > aes_ecb_decrypt(NULL, 32, true, ciphertext, ciphertext, data_size)){
ret = -1;
goto exit;
}
if (memcmp(plaintext, ciphertext, data_size)) {
printf("aes ecb rkek test: failed\r\n");
ret = -1;
goto exit;
}
printf("aes ecb rkek test: pass\r\n");
exit:
if (plaintext != NULL) {
free(plaintext);
}
if (ciphertext != NULL) {
free(ciphertext);
}
return ret;
}
void hw_aes_test(void)
{
void *pverfiy_buf = (void *)malloc(256);
if (!pverfiy_buf) {
printf("te200 aes test: malloc failed\r\n");
}
memset(pverfiy_buf, 0 , 256);
hw_aes_ecb_test(pverfiy_buf);
hw_aes_ecb_rkek_test();
free(pverfiy_buf);
return;
}
#endif