本例程参考TI cc2538_foundation_firmware_1_0_1_0\driverlib\cc2538\examples\ccm例程修改而来;
创建c文件,和第五部做法一样,TinyOS直接使用C文件;
cc2538_ccm.h文件:
- #ifndef CC2538AES_CCM_H
- #define CC2538AES_CCM_H
- #ifdef CC2538_HW_SECURITY
-
-
- #define AUTHENTICATION_LENTH 0X4
- typedef struct
- {
- uint8_t ui8CCMKey[16];
- uint8_t ui8CCMMval;
- uint8_t ui8CCMN[13];
- uint8_t* ui8CCMA;
- uint8_t* ui8CCMInPut;
- uint16_t ui16CCMInPutLen;
- uint16_t ui16CCMLenA;
- uint8_t ui8CCMKeyLocation;
- uint8_t ui8CCMCstate[AUTHENTICATION_LENTH];
- uint8_t ui8CCMLVal;
- uint8_t ui8CCMIntEnable;
- } sCCM_Config;
-
- int CC2538cb_CCMEncrypt(uint8_t* input, uint8_t len);
- int CC2538cb_CCMDecrypt(uint8_t* input, uint8_t len);
-
- #endif
- #endif
cc2538_ccm.c文件:
- #include "aes.h"
- #include "ccm.h"
- #include <string.h>
- #include "sys_ctrl.h"
- #include "cc2538_ccm.h"
- #ifdef CC2538_HW_SECURITY
-
- sCCM_Config sCCC_config =
- {
-
-
- { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
-
- AUTHENTICATION_LENTH,
-
- { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
- 0x00, 0x00, 0x00, 0x00, 0x05 },
-
- NULL,
-
- NULL,
-
- 0,
-
- 0x0,
-
- 0,
-
- { 0x11, 0x22, 0x33, 0x44},
-
- 2,
- false
-
- };
-
-
-
- int CC2538cb_CCMEncrypt(uint8_t* input, uint8_t len){
- ...
- }
-
- int CC2538cb_CCMDecrypt(uint8_t* input, uint8_t len){
-
- ...
- }
-
- #endif
需要修改key可以修改结构体初始化值;使用需要开启宏定义CC2538_HW_SECURITY
例程目录tinyos-main-release_tinyos_2_1_2\apps\cc2538_Test\TestCCM;大家可以综合前面的nesC的语法讲解来阅读代码:
首先还是Makefile文件;上面介绍了需要开启CC2538_HW_SECURITY;这是因为CCM的使用者是RF射频加密;调试需要抓包状态没有必要加密,加密后wireshark无法
解密出数据的RPL路由等信息了;
- COMPONENT=TestCCMAppC
- CFLAGS += -DNOT_USE_PRINTFC_BUT_USE_PRINT
- CFLAGS += -DCC2538_HW_SECURITY
- include $(MAKERULES)
TestCCMAppC.nc文件:
- configuration TestCCMAppC{
- }
- implementation{
- components MainC, TestCCMC;
- TestCCMC -> MainC.Boot;
-
- }
TestCCMC.nc文件:
- /*******************************************************************
- *附加实验1:CCM加密解密实验
- *节点需求数1
- *编译命令make cc2538cb
- *学会使用make中的编译选项 CFLAGS += -DCC2538_HW_SECURITY
- *开启CCM 配置在tos/chip/cc2538/aes的cc2538_ccm.c文件中,设置key
- *在tos/chip/cc2538/aes的cc2538_ccm.h文件中,设置AUTHENTICATION_LENTH长度
- *长度取值去参考tos/chip/fwlib/ccm.c(ccm.h)
- *注意,在后面的网络测试,一旦一个节点开启CCM,其他节点都要开启,否则无法通信
- *通过wireshar抓包可以看到 已加密 不知道密钥 无法解密
- *更多其他加密方式,可以参考TI加密合集(网盘TI源码)
- ********************************************************************/
-
- #include "printf.h"
- #include "cc2538_ccm.h"
-
- module TestCCMC{
- uses interface Boot;
- }
- implementation{
-
- uint8_t Test_IN[15 + AUTHENTICATION_LENTH];
- /***************************************************
- *启动事件
- ****************************************************/
- event void Boot.booted(){
- uint8_t i;
- //初始化值
- for(i = 0; i < sizeof(Test_IN); i++)
- Test_IN[i] = i;
- printf("原始:");
- for(i = 0; i < sizeof(Test_IN) - AUTHENTICATION_LENTH; i++)
- printf(" %d", Test_IN[i]);
- printf("\n");
-
- // 加密 CCM Authentication + Encryption
- CC2538cb_CCMEncrypt(Test_IN, sizeof(Test_IN) - AUTHENTICATION_LENTH);
- printf("加密:");
- for(i = 0; i < sizeof(Test_IN); i++)
- printf(" %d", Test_IN[i]);
- printf("\n");
- // 解密 CCM Inverse Authentication + Decryption
- CC2538cb_CCMDecrypt(Test_IN, sizeof(Test_IN));
- printf("解密:");
- for(i = 0; i < sizeof(Test_IN) - AUTHENTICATION_LENTH; i++)
- printf(" %d", Test_IN[i]);
- printf("\n");
-
- }
-
- }
结合之前的知识,大家发现;TinyOS使用C文件和C语言使用c文件没有区别;例程比较简单,加密一个数组长度为15个字节;大家可以测试加密其他长度,初值可以自己设定!
下面是运行截图:
