// Define size of AES block
#define SIZE_OF_AES_BLOCK 16
// Allocate source buffers for AES key and IV/NONCE
static uint8 key[SIZE_OF_AES_BLOCK] = {"simpliciti's key"};
static uint8 iv_nonce[SIZE_OF_AES_BLOCK] = {"longjing's smart"};
void aesInit(void)
{
uint8 i;
// Generate Key:
ENCCS = 0x04 | 0x01;
for (i = 0; i < SIZE_OF_AES_BLOCK; i++)
{
ENCDI = key[i];
}
// Monitor AES (ENCCS.RDY) to wait until key downloaded
while(!(ENCCS & 0x08));
// Generate IV:
ENCCS = 0x06 | 0x01;
for (i = 0; i < SIZE_OF_AES_BLOCK; i++)
{
ENCDI = iv_nonce[i];
}
// Monitor AES (ENCCS.RDY) to wait until IV/NONCE downloaded
while(!(ENCCS & 0x08));
}
uint8 encryptDecrypt(uint8* source,uint8 length,uint8* targ,uint8 type)
{
uint16 i, j;
uint8 nBlock = 0;
nBlock = length / 16;
if((length % 2) != 0 )//less than 128 bits
nBlock++;
if(type)
ENCCS = 0x00; // Only valid for encryption mode !
else
ENCCS = 0x02; // Only valid for decryption mode !
ENCCS |= 0X01;
for (j = 0; j < nBlock; j++)
{
// In encryption mode "aes_buffer_1" represents the data to be encrypted.
// In decryption mode "aes_buffer_1" represents the data to be decrypted.
for (i = 0; i < SIZE_OF_AES_BLOCK; i++)
{
ENCDI = ((i+j*SIZE_OF_AES_BLOCK) < length)?source[i+(j*SIZE_OF_AES_BLOCK)]:0x00;
// ENCDI = source[i+(j*SIZE_OF_AES_BLOCK)];
}
// while(!(ENCCS & 0x08));
// asm("NOP");asm("NOP");asm("NOP");asm("NOP");asm("NOP");
// Upload data block (16 bytes) to allocated AES buffer:
// In encryption mode "aes_buffer_2" represents the encrypted data.
// In decryption mode "aes_buffer_2" represents the decrypted data.
for (i = 0; i < SIZE_OF_AES_BLOCK; i++)
{
targ[i+(j*SIZE_OF_AES_BLOCK)] = ENCDO;
}
// Monitor AES (ENCCS.RDY) to wait until data block downloaded
while(!(ENCCS & 0x08)); }
}
卡在红色地方,高手指点下