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/LAUNCHXL-CC1350: Encrypt and decrypt on two different sides

Part Number: LAUNCHXL-CC1350

Tool/software: TI-RTOS

Hello All,

I am able to encrypt and decrypt data in 1 program but if i want to encrypt me data on one side and decrypt this data back in original form, what changes i need to make to my program. When i am trying to decrypt the data, I am getting encrypted data only. I am not getting the original data. Please help me with this.

  • I will back to you tomorrow with a simplified example.
  • Okay Sir. I would be waiting for your Reply. Thanks a lot for the Support Sir.
    Thanks
    Shubham
  • At the moment this code only encrypt/ decrypt one 16 byte block. My plan is to make it more generic. 

    0410.rfPacketRx.c
    /*
     * Copyright (c) 2015-2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /***** Includes *****/
    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #include <ti/sysbios/knl/Semaphore.h>
    
    /* Drivers */
    #include <ti/drivers/rf/RF.h>
    #include <ti/drivers/PIN.h>
    
    #ifdef DEVICE_FAMILY
        #undef DEVICE_FAMILY_PATH
        #define DEVICE_FAMILY_PATH(x) <ti/devices/DEVICE_FAMILY/x>
        #include DEVICE_FAMILY_PATH(driverlib/rf_prop_mailbox.h)
    #else
        #error "You must define DEVICE_FAMILY at the project level as one of cc26x0, cc26x0r2, cc13x0, etc."
    #endif
    
    /* Board Header files */
    #include "Board.h"
    
    #include "RFQueue.h"
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    #include "smartrf_settings/smartrf_settings.h"
    
    #include <stdlib.h>
    
    /* Pin driver handle */
    static PIN_Handle ledPinHandle;
    static PIN_State ledPinState;
    
    /*
     * Application LED pin configuration table:
     *   - All LEDs board LEDs are off.
     */
    PIN_Config pinTable[] =
    {
        Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        Board_PIN_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        PIN_TERMINATE
    };
    
    
    /***** Defines *****/
    #define RX_TASK_STACK_SIZE 1024
    #define RX_TASK_PRIORITY   2
    
    /* Packet RX Configuration */
    #define DATA_ENTRY_HEADER_SIZE 8  /* Constant header size of a Generic Data Entry */
    #define MAX_LENGTH             30 /* Max length byte the radio will accept */
    #define NUM_DATA_ENTRIES       2  /* NOTE: Only two data entries supported at the moment */
    #define NUM_APPENDED_BYTES     2  /* The Data Entries data field will contain:
                                       * 1 Header byte (RF_cmdPropRx.rxConf.bIncludeHdr = 0x1)
                                       * Max 30 payload bytes
                                       * 1 status byte (RF_cmdPropRx.rxConf.bAppendStatus = 0x1) */
    
    
    
    /***** Prototypes *****/
    static void rxTaskFunction(UArg arg0, UArg arg1);
    static void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e);
    
    /***** Variable declarations *****/
    static Task_Params rxTaskParams;
    Task_Struct rxTask;    /* not static so you can see in ROV */
    static uint8_t rxTaskStack[RX_TASK_STACK_SIZE];
    
    static RF_Object rfObject;
    static RF_Handle rfHandle;
    
    /* Buffer which contains all Data Entries for receiving data.
     * Pragmas are needed to make sure this buffer is 4 byte aligned (requirement from the RF Core) */
    #if defined(__TI_COMPILER_VERSION__)
        #pragma DATA_ALIGN (rxDataEntryBuffer, 4);
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__IAR_SYSTEMS_ICC__)
        #pragma data_alignment = 4
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__GNUC__)
            static uint8_t rxDataEntryBuffer [RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                MAX_LENGTH, NUM_APPENDED_BYTES)] __attribute__ ((aligned (4)));
    #else
        #error This compiler is not supported.
    #endif
    
    /* Receive dataQueue for RF Core to fill in data */
    static dataQueue_t dataQueue;
    static rfc_dataEntryGeneral_t* currentDataEntry;
    static uint8_t packetLength;
    static uint8_t* packetDataPointer;
    
    static PIN_Handle pinHandle;
    
    static uint8_t packet[MAX_LENGTH + NUM_APPENDED_BYTES - 1]; /* The length byte is stored in a separate variable */
    
    //semaphore object def
    Semaphore_Struct semStruct;
    Semaphore_Handle semHandle;
    
    
    /***** Function definitions *****/
    void RxTask_init(PIN_Handle ledPinHandle) {
        pinHandle = ledPinHandle;
    
        Task_Params_init(&rxTaskParams);
        rxTaskParams.stackSize = RX_TASK_STACK_SIZE;
        rxTaskParams.priority = RX_TASK_PRIORITY;
        rxTaskParams.stack = &rxTaskStack;
        rxTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&rxTask, rxTaskFunction, &rxTaskParams, NULL);
    }
    
    
    
    static void rxTaskFunction(UArg arg0, UArg arg1)
    {
        // Crypto defs
        // 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 }
        };
        // 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.");
        }
    
        // Radio defs
        RF_Params rfParams;
        RF_Params_init(&rfParams);
    
        if( RFQueue_defineQueue(&dataQueue,
                                rxDataEntryBuffer,
                                sizeof(rxDataEntryBuffer),
                                NUM_DATA_ENTRIES,
                                MAX_LENGTH + NUM_APPENDED_BYTES))
        {
            /* Failed to allocate space for all data entries */
            while(1);
        }
    
        /* Modify CMD_PROP_RX command for application needs */
        RF_cmdPropRx.pQueue = &dataQueue;           /* Set the Data Entity queue for received data */
        RF_cmdPropRx.rxConf.bAutoFlushIgnored = 1;  /* Discard ignored packets from Rx queue */
        RF_cmdPropRx.rxConf.bAutoFlushCrcErr = 1;   /* Discard packets with CRC error from Rx queue */
        RF_cmdPropRx.maxPktLen = MAX_LENGTH;        /* Implement packet length filtering to avoid PROP_ERROR_RXBUF */
        RF_cmdPropRx.pktConf.bRepeatOk = 1;
        RF_cmdPropRx.pktConf.bRepeatNok = 1;
    
        /* Request access to the radio */
        rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
    
        /* Set the frequency */
        RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
    
        /* Enter RX mode and stay forever in RX */
        RF_postCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &callback, IRQ_RX_ENTRY_DONE);
    
        while(1) {
            Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
            PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
            // Initialize transaction
            memcpy(ecbExample.msgOut,packet,16);
            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.");
            }
    
        };
    }
    
    void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
    {
        if (e & RF_EventRxEntryDone)
        {
            /* Toggle pin to indicate RX */
            PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
    
            /* Get current unhandled data entry */
            currentDataEntry = RFQueue_getDataEntry();
    
            /* Handle the packet data, located at &currentDataEntry->data:
             * - Length is the first byte with the current configuration
             * - Data starts from the second byte */
            packetLength      = *(uint8_t*)(&currentDataEntry->data);
            packetDataPointer = (uint8_t*)(&currentDataEntry->data + 1);
    
            /* Copy the payload + the status byte to the packet variable */
            memcpy(packet, packetDataPointer, (packetLength + 1));
    
            RFQueue_nextEntry();
    
            Semaphore_post(semHandle);
        }
    }
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        Semaphore_Params semParams;
        /* Construct a Semaphore object to be use as a resource lock, inital count 1 */
        Semaphore_Params_init(&semParams);
        Semaphore_construct(&semStruct, 1, &semParams);
    
        /* Obtain instance handle */
        semHandle = Semaphore_handle(&semStruct);
        /* Call driver init functions. */
        Board_initGeneral();
    
        /* Open LED pins */
        ledPinHandle = PIN_open(&ledPinState, pinTable);
        if(!ledPinHandle)
        {
            System_abort("Error initializing board LED pins\n");
        }
    
        /* Initialize task */
        RxTask_init(ledPinHandle);
    
        /* Start BIOS */
        BIOS_start();
    
        return (0);
    }
    
    3124.rfPacketTx.c
    /*
     * Copyright (c) 2015-2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /***** Includes *****/
    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/runtime/System.h>
    
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    
    /* Drivers */
    #include <ti/drivers/rf/RF.h>
    #include <ti/drivers/PIN.h>
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    
    
    /* Board Header files */
    #include "Board.h"
    #include "smartrf_settings/smartrf_settings.h"
    
    /* Pin driver handle */
    static PIN_Handle ledPinHandle;
    static PIN_State ledPinState;
    
    /*
     * Application LED pin configuration table:
     *   - All LEDs board LEDs are off.
     */
    PIN_Config pinTable[] =
    {
        Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        PIN_TERMINATE
    };
    
    
    /***** Defines *****/
    #define TX_TASK_STACK_SIZE 1024
    #define TX_TASK_PRIORITY   2
    
    /* Packet TX Configuration */
    #define PAYLOAD_LENGTH      16
    #define PACKET_INTERVAL     (uint32_t)(4000000*0.5f) /* Set packet interval to 500ms */
    
    
    
    /***** Prototypes *****/
    static void txTaskFunction(UArg arg0, UArg arg1);
    
    
    
    /***** Variable declarations *****/
    static Task_Params txTaskParams;
    Task_Struct txTask;    /* not static so you can see in ROV */
    static uint8_t txTaskStack[TX_TASK_STACK_SIZE];
    
    static RF_Object rfObject;
    static RF_Handle rfHandle;
    
    uint32_t curtime;
    static uint8_t rawPacket[PAYLOAD_LENGTH];
    static uint8_t packet[PAYLOAD_LENGTH];
    static uint16_t seqNumber;
    static PIN_Handle pinHandle;
    
    
    /***** Function definitions *****/
    void TxTask_init(PIN_Handle inPinHandle)
    {
        pinHandle = inPinHandle;
    
        Task_Params_init(&txTaskParams);
        txTaskParams.stackSize = TX_TASK_STACK_SIZE;
        txTaskParams.priority = TX_TASK_PRIORITY;
        txTaskParams.stack = &txTaskStack;
        txTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&txTask, txTaskFunction, &txTaskParams, NULL);
    }
    
    static void txTaskFunction(UArg arg0, UArg arg1)
    {
        uint32_t curtime;
        uint8_t counter = 0;
        RF_Params rfParams;
        RF_Params_init(&rfParams);
    
        RF_cmdPropTx.pktLen = PAYLOAD_LENGTH;
        RF_cmdPropTx.pPkt = packet;
        RF_cmdPropTx.startTrigger.triggerType = TRIG_NOW;
        RF_cmdPropTx.startTrigger.pastTrig = 1;
        RF_cmdPropTx.startTime = 0;
    
        /* Request access to the radio */
        rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
    
        /* Set the frequency */
        RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
    
        /* Get current time */
        curtime = RF_getCurrentTime();
    
        // 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 }
        };
        // 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.");
        }
    
    
        while(1)
        {
            counter++;
            /* Create packet with incrementing sequence number and random payload */
            //rawPacket[0] = (uint8_t)(seqNumber >> 8);
            //rawPacket[1] = (uint8_t)(seqNumber++);
    
            rawPacket[0] = 0xCC;
            rawPacket[1] = 0xAA;
    
            uint8_t i;
            for (i = 2; i < PAYLOAD_LENGTH; i++)
            {
                //rawPacket[i] = rand();
                rawPacket[i] = 2*i+1;
            }
    
            memcpy(ecbExample.clearText, rawPacket,16);
    
            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.");
            }
    
            memcpy(packet, ecbExample.msgOut,16);
    
            /* Set absolute TX time to utilize automatic power management */
            curtime += PACKET_INTERVAL;
            RF_cmdPropTx.startTime = curtime;
    
            /* Send packet */
            RF_EventMask result = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, 0);
            if (!(result & RF_EventLastCmdDone))
            {
                /* Error */
                while(1);
            }
    
    //        RF_yield(rfHandle);
    
            PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
    
            Task_sleep(500000/Clock_tickPeriod);
        }
    }
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        /* Call driver init functions. */
        Board_initGeneral();
    
        /* Open LED pins */
        ledPinHandle = PIN_open(&ledPinState, pinTable);
        if(!ledPinHandle)
        {
            System_abort("Error initializing board LED pins\n");
        }
    
        /* Initialize task */
        TxTask_init(ledPinHandle);
    
        /* Start BIOS */
        BIOS_start();
    
        return (0);
    }
    

  • Thanks for giving this wonderful solution.
    Will this solution work when I am sending data through UART. One more thing Can you please explain why it works only on one 16 byte block. What changes we need to make in order to make it more generic.

    But Thanks a lot for this Solution Sir.

    Thanks
    Shubham Jindal
  • Hello Sir,

    What about the generic code. The problem with this code is that i need to reset my launch pad after every transaction. Is it possible to send continuous data without resetting the device. Please help me with this Sir.

    Thank You
  • Hi sir,
    I want to know why this solution is sending data only one time. I have to reset transmitter again and again to send different blocks of data in spite of defining separate tasks in transmitter side for getting data through UART and for AES encrypted Sub1 GHz transmission.
    But on receiver side, I need not to reset it again and again to receive and decrypt different data blocks.
    What is the root cause behind it? What need to be done on transmission side to send different blocks of encrypted data? Help required.
  • 7115.rfPacketRx.c
    /*
     * Copyright (c) 2015-2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /***** Includes *****/
    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/cfg/global.h>
    #include <xdc/runtime/System.h>
    
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    #include <ti/sysbios/knl/Semaphore.h>
    
    /* Drivers */
    #include <ti/drivers/rf/RF.h>
    #include <ti/drivers/PIN.h>
    
    #ifdef DEVICE_FAMILY
        #undef DEVICE_FAMILY_PATH
        #define DEVICE_FAMILY_PATH(x) <ti/devices/DEVICE_FAMILY/x>
        #include DEVICE_FAMILY_PATH(driverlib/rf_prop_mailbox.h)
    #else
        #error "You must define DEVICE_FAMILY at the project level as one of cc26x0, cc26x0r2, cc13x0, etc."
    #endif
    
    /* Board Header files */
    #include "Board.h"
    
    #include "RFQueue.h"
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    #include "smartrf_settings/smartrf_settings.h"
    
    #include <stdlib.h>
    
    /* Pin driver handle */
    static PIN_Handle ledPinHandle;
    static PIN_State ledPinState;
    
    /*
     * Application LED pin configuration table:
     *   - All LEDs board LEDs are off.
     */
    PIN_Config pinTable[] =
    {
        Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        Board_PIN_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        PIN_TERMINATE
    };
    
    
    /***** Defines *****/
    #define RX_TASK_STACK_SIZE 1024
    #define RX_TASK_PRIORITY   2
    
    /* Packet RX Configuration */
    #define DATA_ENTRY_HEADER_SIZE 8  /* Constant header size of a Generic Data Entry */
    #define MAX_LENGTH             100 /* Max length byte the radio will accept */
    #define NUM_DATA_ENTRIES       2  /* NOTE: Only two data entries supported at the moment */
    #define NUM_APPENDED_BYTES     2  /* The Data Entries data field will contain:
                                       * 1 Header byte (RF_cmdPropRx.rxConf.bIncludeHdr = 0x1)
                                       * Max 30 payload bytes
                                       * 1 status byte (RF_cmdPropRx.rxConf.bAppendStatus = 0x1) */
    
    
    
    /***** Prototypes *****/
    static void rxTaskFunction(UArg arg0, UArg arg1);
    static void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e);
    static void decryptPacketECB(uint8_t *key);
    
    /***** Variable declarations *****/
    static Task_Params rxTaskParams;
    Task_Struct rxTask;    /* not static so you can see in ROV */
    static uint8_t rxTaskStack[RX_TASK_STACK_SIZE];
    
    static RF_Object rfObject;
    static RF_Handle rfHandle;
    
    /* Buffer which contains all Data Entries for receiving data.
     * Pragmas are needed to make sure this buffer is 4 byte aligned (requirement from the RF Core) */
    #if defined(__TI_COMPILER_VERSION__)
        #pragma DATA_ALIGN (rxDataEntryBuffer, 4);
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__IAR_SYSTEMS_ICC__)
        #pragma data_alignment = 4
            static uint8_t rxDataEntryBuffer[RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                                                                     MAX_LENGTH,
                                                                     NUM_APPENDED_BYTES)];
    #elif defined(__GNUC__)
            static uint8_t rxDataEntryBuffer [RF_QUEUE_DATA_ENTRY_BUFFER_SIZE(NUM_DATA_ENTRIES,
                MAX_LENGTH, NUM_APPENDED_BYTES)] __attribute__ ((aligned (4)));
    #else
        #error This compiler is not supported.
    #endif
    
    /* Receive dataQueue for RF Core to fill in data */
    static dataQueue_t dataQueue;
    static rfc_dataEntryGeneral_t* currentDataEntry;
    static uint8_t packetLength;
    static uint8_t* packetDataPointer;
    
    static PIN_Handle pinHandle;
    
    static uint8_t packet[MAX_LENGTH + NUM_APPENDED_BYTES - 1]; /* The length byte is stored in a separate variable */
    static uint8_t decryptPacket[MAX_LENGTH + NUM_APPENDED_BYTES - 1];
    // Key variables
    static CryptoCC26XX_KeyLocation keyLocation = CRYPTOCC26XX_KEY_0;
    static uint8_t cryptoKey[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
                        0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
    
    //semaphore object definitions
    Semaphore_Struct semStruct;
    Semaphore_Handle semHandle;
    
    void decryptPacketECB(uint8_t * key)
    {
        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, keyLocation, (const uint32_t *) key);
        if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) {
                System_abort("Key Location was not allocated.");
        }
    
        CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_ECB_DECRYPT);
        // Setup transaction
        trans.keyIndex         = keyIndex;
        uint8_t i;
        for (i = 0; i < (packetLength/AES_ECB_LENGTH); i++)
        {
            trans.msgIn            = (uint32_t *) &packet[i*AES_ECB_LENGTH];
            trans.msgOut           = (uint32_t *) &decryptPacket[i*AES_ECB_LENGTH];
            // Zero original clear text before decrypting the cypher text into the ecbExample.clearText array
            memset(&decryptPacket[i*AES_ECB_LENGTH], 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);
        CryptoCC26XX_close(handle);
    }
    
    /***** Function definitions *****/
    void RxTask_init(PIN_Handle ledPinHandle) {
        pinHandle = ledPinHandle;
    
        Task_Params_init(&rxTaskParams);
        rxTaskParams.stackSize = RX_TASK_STACK_SIZE;
        rxTaskParams.priority = RX_TASK_PRIORITY;
        rxTaskParams.stack = &rxTaskStack;
        rxTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&rxTask, rxTaskFunction, &rxTaskParams, NULL);
    }
    
    
    
    static void rxTaskFunction(UArg arg0, UArg arg1)
    {
        // Radio defs
        RF_Params rfParams;
        RF_Params_init(&rfParams);
    
        if( RFQueue_defineQueue(&dataQueue,
                                rxDataEntryBuffer,
                                sizeof(rxDataEntryBuffer),
                                NUM_DATA_ENTRIES,
                                MAX_LENGTH + NUM_APPENDED_BYTES))
        {
            /* Failed to allocate space for all data entries */
            while(1);
        }
    
        /* Modify CMD_PROP_RX command for application needs */
        RF_cmdPropRx.pQueue = &dataQueue;           /* Set the Data Entity queue for received data */
        RF_cmdPropRx.rxConf.bAutoFlushIgnored = 1;  /* Discard ignored packets from Rx queue */
        RF_cmdPropRx.rxConf.bAutoFlushCrcErr = 1;   /* Discard packets with CRC error from Rx queue */
        RF_cmdPropRx.maxPktLen = MAX_LENGTH;        /* Implement packet length filtering to avoid PROP_ERROR_RXBUF */
        RF_cmdPropRx.pktConf.bRepeatOk = 1;
        RF_cmdPropRx.pktConf.bRepeatNok = 1;
    
        /* Request access to the radio */
        rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
    
        /* Set the frequency */
        RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
    
        /* Enter RX mode and stay forever in RX */
        RF_postCmd(rfHandle, (RF_Op*)&RF_cmdPropRx, RF_PriorityNormal, &callback, IRQ_RX_ENTRY_DONE);
    
        while(1) {
            Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
            decryptPacketECB(cryptoKey);
            PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
       }
    }
    
    
    void callback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
    {
        if (e & RF_EventRxEntryDone)
        {
            /* Toggle pin to indicate RX */
            PIN_setOutputValue(pinHandle, Board_PIN_LED2,!PIN_getOutputValue(Board_PIN_LED2));
    
            /* Get current unhandled data entry */
            currentDataEntry = RFQueue_getDataEntry();
    
            /* Handle the packet data, located at &currentDataEntry->data:
             * - Length is the first byte with the current configuration
             * - Data starts from the second byte */
            packetLength      = *(uint8_t*)(&currentDataEntry->data);
            packetDataPointer = (uint8_t*)(&currentDataEntry->data + 1);
    
            /* Copy the payload + the status byte to the packet variable */
            memcpy(packet, packetDataPointer, (packetLength + 1));
    
            RFQueue_nextEntry();
    
            Semaphore_post(semHandle);
        }
    }
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        Semaphore_Params semParams;
        /* Construct a Semaphore object to be use as a resource lock, inital count 1 */
        Semaphore_Params_init(&semParams);
        Semaphore_construct(&semStruct, 0, &semParams);
    
        /* Obtain instance handle */
        semHandle = Semaphore_handle(&semStruct);
        /* Call driver init functions. */
        Board_initGeneral();
    
        /* Open LED pins */
        ledPinHandle = PIN_open(&ledPinState, pinTable);
        if(!ledPinHandle)
        {
            System_abort("Error initializing board LED pins\n");
        }
    
        /* Initialize task */
        RxTask_init(ledPinHandle);
    
        /* Start BIOS */
        BIOS_start();
    
        return (0);
    }
    
    6567.rfPacketTx.c
    /*
     * Copyright (c) 2015-2016, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /***** Includes *****/
    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/runtime/System.h>
    
    #include <ti/sysbios/BIOS.h>
    #include <ti/sysbios/knl/Task.h>
    
    /* Drivers */
    #include <ti/drivers/rf/RF.h>
    #include <ti/drivers/PIN.h>
    #include <ti/drivers/crypto/CryptoCC26XX.h>
    
    
    /* Board Header files */
    #include "Board.h"
    #include "smartrf_settings/smartrf_settings.h"
    
    /* Pin driver handle */
    static PIN_Handle ledPinHandle;
    static PIN_State ledPinState;
    
    /*
     * Application LED pin configuration table:
     *   - All LEDs board LEDs are off.
     */
    PIN_Config pinTable[] =
    {
        Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
        PIN_TERMINATE
    };
    
    
    /***** Defines *****/
    #define TX_TASK_STACK_SIZE 1024
    #define TX_TASK_PRIORITY   2
    
    /* Packet TX Configuration */
    #define PAYLOAD_LENGTH      24
    #define PACKET_INTERVAL     (uint32_t)(4000000*0.5f) /* Set packet interval to 500ms */
    
    
    
    /***** Prototypes *****/
    static void txTaskFunction(UArg arg0, UArg arg1);
    static void encryptPacketECB(uint8_t *key);
    
    
    /***** Variable declarations *****/
    static Task_Params txTaskParams;
    Task_Struct txTask;    /* not static so you can see in ROV */
    static uint8_t txTaskStack[TX_TASK_STACK_SIZE];
    
    static RF_Object rfObject;
    static RF_Handle rfHandle;
    
    uint32_t curtime;
    static uint8_t rawPacket[PAYLOAD_LENGTH];
    // Allocate enough space for encrypted packet
    static uint8_t packet[PAYLOAD_LENGTH + AES_ECB_LENGTH];
    // Key variables
    static CryptoCC26XX_KeyLocation keyLocation = CRYPTOCC26XX_KEY_0;
    static uint8_t cryptoKey[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
                        0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
    //static uint16_t seqNumber;
    static PIN_Handle pinHandle;
    
    void encryptPacketECB(uint8_t * key)
    {
            uint8_t clearTextBuffer[AES_ECB_LENGTH];
            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, keyLocation, (const uint32_t *) key);
            if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) {
                    System_abort("Key Location was not allocated.");
            }
    
            CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_ECB_ENCRYPT);
            // Setup transaction
            trans.keyIndex         = keyIndex;
            uint8_t i;
            for (i = 0; i < (sizeof(rawPacket)/AES_ECB_LENGTH); i++)
            {
               trans.msgIn            = (uint32_t *) &rawPacket[i * AES_ECB_LENGTH];
               trans.msgOut           = (uint32_t *) &packet[i*AES_ECB_LENGTH];
               // Encrypt the plaintext with AES ECB
               status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *) &trans);
               if(status != CRYPTOCC26XX_STATUS_SUCCESS){
                   System_abort("Encryption failed.");
               }
            }
            if (sizeof(packet)%AES_ECB_LENGTH != 0)
            {
                memset(clearTextBuffer, 0x0, AES_ECB_LENGTH);
                memcpy(clearTextBuffer, &rawPacket[i*AES_ECB_LENGTH],(i+1)*AES_ECB_LENGTH-sizeof(rawPacket));
                trans.msgIn            = (uint32_t *) clearTextBuffer;
                trans.msgOut           = (uint32_t *) &packet[i*AES_ECB_LENGTH];
    
                // Encrypt 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);
            CryptoCC26XX_close(handle);
    }
    
    
    /***** Function definitions *****/
    void TxTask_init(PIN_Handle inPinHandle)
    {
        pinHandle = inPinHandle;
    
        Task_Params_init(&txTaskParams);
        txTaskParams.stackSize = TX_TASK_STACK_SIZE;
        txTaskParams.priority = TX_TASK_PRIORITY;
        txTaskParams.stack = &txTaskStack;
        txTaskParams.arg0 = (UInt)1000000;
    
        Task_construct(&txTask, txTaskFunction, &txTaskParams, NULL);
    }
    
    static void txTaskFunction(UArg arg0, UArg arg1)
    {
        uint32_t curtime;
        RF_Params rfParams;
        RF_Params_init(&rfParams);
        RF_cmdPropTx.pktLen = (PAYLOAD_LENGTH / AES_ECB_LENGTH + 1)*AES_ECB_LENGTH;
        //RF_cmdPropTx.pktLen = PAYLOAD_LENGTH;
        RF_cmdPropTx.pPkt = packet;
        RF_cmdPropTx.startTrigger.triggerType = TRIG_NOW;
        RF_cmdPropTx.startTrigger.pastTrig = 1;
        RF_cmdPropTx.startTime = 0;
    
        /* Request access to the radio */
        rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
    
        /* Set the frequency */
        RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
    
        /* Get current time */
        curtime = RF_getCurrentTime();
    
        while(1)
        {
            /* Create packet with incrementing sequence number and random payload */
            //rawPacket[0] = (uint8_t)(seqNumber >> 8);
            //rawPacket[1] = (uint8_t)(seqNumber++);
    
            rawPacket[0] = 0x00;
            rawPacket[1] = 0x01;
    
            uint8_t i;
            for (i = 2; i < PAYLOAD_LENGTH; i++)
            {
                //rawPacket[i] = rand();
                rawPacket[i] = i;
            }
    
            encryptPacketECB(cryptoKey);
            //decryptPacketECB(cryptoKey);
    
            curtime += PACKET_INTERVAL;
            RF_cmdPropTx.startTime = curtime;
    
            /* Send packet */
            RF_EventMask result = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, 0);
            if (!(result & RF_EventLastCmdDone))
            {
                /* Error */
                while(1);
            }
    
    //        RF_yield(rfHandle);
    
            PIN_setOutputValue(pinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
    
            Task_sleep(5000000/Clock_tickPeriod);
        }
    }
    
    /*
     *  ======== main ========
     */
    int main(void)
    {
        /* Call driver init functions. */
        Board_initGeneral();
    
        /* Open LED pins */
        ledPinHandle = PIN_open(&ledPinState, pinTable);
        if(!ledPinHandle)
        {
            System_abort("Error initializing board LED pins\n");
        }
    
        /* Initialize task */
        TxTask_init(ledPinHandle);
    
        /* Start BIOS */
        BIOS_start();
    
        return (0);
    }
    
    The encryption and decryption has to be done 16 bytes at the time since the crypto is block based. If your packet is not a multiple of 16 byte you have to pad with '0' until you get 16 byte.

    Note that this example uses ECB which is not recommended to used in the real world()