This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/CC1350STK: CryptoCC26XX.h AES128 ECB not working

Part Number: CC1350STK
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?

  • Note that ECB is not recommended.

    I wrote some code at one point here: e2e.ti.com/.../581133

    Here is an example using CCM: e2e.ti.com/.../641353
  • Hi TER, I know it isn't recommended, but I need it only for some measuring of power consumption so it doesn't really matter.
    It would be really helpful to know what I did wrong, as I followed the example from library documentation.

  • If it doesn't matter which crypto mode you are using I suggest you run the CCM code I linked to.
  • I actually need to use ECB for my project, sorry if I wasn't clear enough
  • as I am using the code from dev.ti.com/.../_crypto_c_c26_x_x_8h.html I am not sure why it doesn't work.

    The CCM example from the same link also doesn't work in my project

  • When I am debugging I at one point I get this error;
    Can't find a source file at "C:\Jenkins\jobs\FWGroup-DriverLib\workspace\modules\output\cc13xx_cha_2_0_ext\driverlib\bin\ccs/./../../../driverlib/crypto.c"

    Could you help me solve that problem? What drivers do I have to install?

    Also note that it is looking for file at Jenkins user, which isn't mine and doesn't exist on my PC, as I am the only user

  • Could you try to do this with the newest SDK? Jenkins is a build server on our side and you should not have any reference to this when you import the code.
  • I used the latest SDKs on resource explorer homepage and still the same issue.
    Following versions are installed:
    SimpleLink CC13x0 SDK 2.20.00.38
    TI-RTOS for CC2650 2.21.00.06

    do I have to uninstall one of those two? I used empty project template from CC13x0 sdk.

  • You have to check which one the project uses :

    Project properties: general:

    (example from a random project I had open)

  • Here is the screenshot

  • is there then any way to solve this issue?
  • *  ======== empty.c ========
     */
    
    /* For usleep() */
    #include <unistd.h>
    #include <stdint.h>
    #include <stddef.h>
    #include <stdio.h>
    #include <string.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    // #include <ti/drivers/I2C.h>
    // #include <ti/drivers/SDSPI.h>
    // #include <ti/drivers/SPI.h>
    // #include <ti/drivers/UART.h>
    // #include <ti/drivers/Watchdog.h>
    
    /* Board Header file */
    #include "Board.h"
    #include <xdc/runtime/System.h>
    
    // AES-ECB example struct
    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;
    
      // AES ECB example data
      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 }
      };
    
    
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
        // Declaration (typically done in a task)
        CryptoCC26XX_Handle             handle;
        int32_t                         keyIndex;
        int32_t                         status;
        CryptoCC26XX_AESECB_Transaction trans;
    
         // Initialize Crypto driver
          CryptoCC26XX_init();
    
         // Attempt to open CryptoCC26XX.
         handle = CryptoCC26XX_open(Board_CRYPTO0, false, NULL);
         if (!handle) {
            System_abort("Crypto module could not be opened.");
         }
    
         keyIndex = CryptoCC26XX_allocateKey(handle, ecbExample.keyLocation,
                                            (const uint32_t *) ecbExample.key);
         if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) {
               System_abort("Key Location was not allocated.");
         }
    
         // Initialize transaction
         CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_ECB_ENCRYPT);
    
         // 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.");
         }
    
         // Initialize transaction
         CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_ECB_DECRYPT);
    
         // 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.");
         }
    
         CryptoCC26XX_releaseKey(handle, &keyIndex);
    
    
        /* 1 second delay */
        uint32_t time = 1;
    
        /* 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);
        }
    }
    

    I just copied the code from crypto.h into the empty example from CC13x0 SDK 2.20 and it looks like it works: