Other Parts Discussed in Thread: CC2650
Tool/software: TI-RTOS
Hi guys, I have problem with encryption using CryptoCC26XX.h Library. I did everything as in this example dev.ti.com/.../_crypto_c_c26_x_x_8h.html, but it doesn't work.
variable status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *)&trans); gets always value 1 insted of 0, which means that encryption has failed.
Here is the code:
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <ti/drivers/Power.h>
#include <xdc/runtime/System.h>
#include <stdio.h>
#include <string.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/crypto/CryptoCC26XX.h>
/* Board Header file */
#include "Board.h"
void encrypt();
void decrypt();
typedef struct
{
uint8_t key[16]; // Stores the Aes Key
CryptoCC26XX_KeyLocation keyLocation; // Location in Key RAM
uint8_t clearText[AES_ECB_LENGTH]; // Input message - cleartext
uint8_t msgOut[AES_ECB_LENGTH]; // Output message
} AESECBExample;
AESECBExample ecbExample =
{
{ 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C },
CRYPTOCC26XX_KEY_0,
{ 't','h','i','s','i','s','a','p','l','a','i','n','t','e','x','t' },
{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
};
CryptoCC26XX_Handle handle;
int32_t keyIndex;
int32_t status;
CryptoCC26XX_AESECB_Transaction trans;
void encrypt() //memcpy(ecbExample.clearText, data, 16); //memcpy(data, ecbExample.msgOut, 16); if you don't want to use structure directly
{
CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *)&trans, CRYPTOCC26XX_OP_AES_ECB_ENCRYPT); // Initialize transaction
// Setup transaction
trans.keyIndex = keyIndex;
trans.msgIn = (uint32_t *)ecbExample.clearText;
trans.msgOut = (uint32_t *)ecbExample.msgOut;
// Encrypt the plaintext with AES ECB
status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *)&trans);
if (status != CRYPTOCC26XX_STATUS_SUCCESS) {
System_abort("Encryption failed.\n");
}
}
void decrypt()
{
CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *)&trans, CRYPTOCC26XX_OP_AES_ECB_DECRYPT); // Initialize transaction
// Setup transaction
trans.keyIndex = keyIndex;
trans.msgIn = (uint32_t *)ecbExample.msgOut;
trans.msgOut = (uint32_t *)ecbExample.clearText;
// Zero original clear text before decrypting the cypher text into the ecbExample.clearText array
memset(ecbExample.clearText, 0x0, AES_ECB_LENGTH);
// Decrypt the plaintext with AES ECB
status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *)&trans);
if (status != CRYPTOCC26XX_STATUS_SUCCESS) {
System_abort("Encryption failed.\n");
}
}
void *mainThread(void *arg0)
{
CryptoCC26XX_init(); // Initialize Crypto driver
handle = CryptoCC26XX_open(Board_CRYPTO0, false, NULL); // Attempt to open CryptoCC26XX.
if (!handle) System_abort("Crypto module could not be opened.\n");
keyIndex = CryptoCC26XX_allocateKey(handle, ecbExample.keyLocation,(const uint32_t *)ecbExample.key);
if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) System_abort("Key Location was not allocated.");
uint32_t time = 1;/* 1 second delay */
/* Call driver init functions */
GPIO_init();
// I2C_init();
// SDSPI_init();
// SPI_init();
// UART_init();
// Watchdog_init();
/* Configure the LED pin */
GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Turn on user LED */
//GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
while (1) {
sleep(time);
GPIO_toggle(Board_GPIO_LED0);
encrypt();
printf("encrypted\n");
decrypt();
printf("decrypted\n");
}
}
Does anyone know why it doesn't work?