/* * 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 #include #include #include #include /* Drivers */ #include #include #include // /* 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_RLED | 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 46 #define PACKET_INTERVAL (uint32_t)(4000000*0.5f) /* Set packet interval to 500ms */ #define macLength (4) #define clearTextLength (16) #define cipherTextLength (macLength + clearTextLength) #define nonceLength (12) #define aadLength (14) /***** Prototypes *****/ static void txTaskFunction(UArg arg0, UArg arg1); //atic void encryptPacketECB(uint8_t *key); static void encryptPacketCCM(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; typedef struct { uint8_t key[16]; // A 128 Bit AES key CryptoCC26XX_KeyLocation keyLocation; // One of 8 key locations in the hardware uint8_t clearAndCipherText[cipherTextLength]; // Holds the cleartext before, and the ciphertext // after the encryption operation. // Ciphertext = encrypted text + message authentication code (MAC). uint8_t nonce[nonceLength]; // A value that is used only once (cryptographic term 'nonce') uint8_t header[aadLength]; // A header that is not encrypted but is authenticated in the operation (AAD). uint8_t verificationMAC[macLength]; // Location that the recalculated and encrypted MAC is stored during decryption. } AesCcmExample; AesCcmExample ccmSetup = { .key = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }, .keyLocation = CRYPTOCC26XX_KEY_0, .clearAndCipherText = { 't','h','i','s','i','s','a','p','l','a','i','n','t','e','x','t','0','0','0','0' }, .nonce = { 't','h','i','s','i','s','a','n','o','n','c','e' }, .header = { 't','h','i','s','i','s','a','h','e','a','d','e','r','1' } }; static PIN_Handle pinHandle; void encryptPacketCCM(uint8_t * key) { uint8_t clearTextBuffer[AES_ECB_LENGTH]; CryptoCC26XX_Handle handle; int32_t keyIndex; int32_t status; CryptoCC26XX_AESCCM_Transaction trans; // // Initialize Crypto driver CryptoCC26XX_init(); // Open the crypto hardware with non-exclusive access and default parameters // Attempt to open CryptoCC26XX. handle = CryptoCC26XX_open(Board_CRYPTO, false, NULL); if (!handle) { System_abort("Crypto module could not be opened."); } // Allocate a key storage location in the hardware keyIndex = CryptoCC26XX_allocateKey(handle, ccmSetup.keyLocation, (const uint32_t *) ccmSetup.key); //keyIndex = CryptoCC26XX_allocateKey(handle, ecbExample.keyLocation, //(const uint32_t *) ecbExample.key); // keyIndex = CryptoCC26XX_allocateKey(handle, keyLocation, (const uint32_t *) key); if (keyIndex == CRYPTOCC26XX_STATUS_ERROR) { System_abort("Key Location was not allocated."); } //*************************************************************************************************** // // Encrypt and authenticate the message // CryptoCC26XX_Transac_init((CryptoCC26XX_Transaction *) &trans, CRYPTOCC26XX_OP_AES_CCM_ENCRYPT); /// only change is CRYPTOCC26XX_OP_AES_CCM // Setup transaction trans.keyIndex = keyIndex; trans.authLength = macLength; trans.nonce = (char *) ccmSetup.nonce; trans.header = (char *) ccmSetup.header; trans.fieldLength = 3; trans.msgInLength = clearTextLength; trans.headerLength = aadLength; trans.msgIn = (char *) &(ccmSetup.clearAndCipherText[0]); // Message is encrypted in place trans.msgOut = (char *) &(ccmSetup.clearAndCipherText[clearTextLength]); // MAC will be written to this position memcpy(packet,trans.header,aadLength); memcpy(&packet[aadLength],trans.nonce,nonceLength); memcpy(&packet[aadLength+nonceLength],ccmSetup.clearAndCipherText[clearTextLength],cipherTextLength); status = CryptoCC26XX_transact(handle, (CryptoCC26XX_Transaction *) &trans); if(status != CRYPTOCC26XX_STATUS_SUCCESS){ System_abort("Encryption and signing failed."); } // Release the key location status = CryptoCC26XX_releaseKey(handle, &keyIndex); if (status != CRYPTOCC26XX_STATUS_SUCCESS) { System_abort("Key release was not successful."); } 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; /// to do- check length with //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) { encryptPacketCCM(ccmSetup.key); 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_RLED,!PIN_getOutputValue(Board_RLED)); 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); }