注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
dan158185的个人空间 https://home.eeworld.com.cn/space-uid-349284.html [收藏] [复制] [分享] [RSS]
日志

CC2538之TinyOS例程实验:6-CCM(AES)实验

已有 1053 次阅读2016-1-5 16:08 |个人分类:CC2538之TinyOS例程

本例程参考TI cc2538_foundation_firmware_1_0_1_0\driverlib\cc2538\examples\ccm例程修改而来;

创建c文件,和第五部做法一样,TinyOS直接使用C文件;

cc2538_ccm.h文件

  1. #ifndef CC2538AES_CCM_H  
  2. #define CC2538AES_CCM_H  
  3. #ifdef CC2538_HW_SECURITY   
  4.   
  5.   
  6. #define AUTHENTICATION_LENTH     0X4      
  7. typedef struct  
  8. {  
  9.     uint8_t  ui8CCMKey[16];             // Key  
  10.     uint8_t  ui8CCMMval;                // length of authentication  
  11.     uint8_t  ui8CCMN[13];               // Nonce  
  12.     uint8_t* ui8CCMA;                   // Additional data  
  13.     uint8_t* ui8CCMInPut;               // input message  
  14.     uint16_t ui16CCMInPutLen;           // length of message  
  15.     uint16_t ui16CCMLenA;               // length of additional data  
  16.     uint8_t  ui8CCMKeyLocation;         // location in Key RAM  
  17.     uint8_t  ui8CCMCstate[AUTHENTICATION_LENTH];           // authentication Tag  
  18.     uint8_t  ui8CCMLVal;             //  Lval for ccm  
  19.     uint8_t  ui8CCMIntEnable;           // set to true to enable interrupts  
  20. } sCCM_Config;  
  21.   
  22. int CC2538cb_CCMEncrypt(uint8_t* input, uint8_t len);  
  23. int CC2538cb_CCMDecrypt(uint8_t* input, uint8_t len);  
  24.   
  25. #endif  
  26. #endif  

cc2538_ccm.c文件:

  1. #include "aes.h"  
  2. #include "ccm.h"  
  3. #include <string.h>  
  4. #include "sys_ctrl.h"  
  5. #include "cc2538_ccm.h"  
  6. #ifdef CC2538_HW_SECURITY   
  7.   
  8. sCCM_Config sCCC_config =  
  9. {  
  10.      // example case len_a and Mval = 0  
  11.         // key  
  12.         { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,  
  13.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },  
  14.         // Mval  
  15.         AUTHENTICATION_LENTH,  
  16.         // N Nonce  
  17.         { 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,  
  18.         0x00, 0x00, 0x00, 0x00, 0x05 },  
  19.         // A  
  20.         NULL,  
  21.         // M message  
  22.         NULL,  
  23.         // len_m  
  24.         0,  
  25.         // len_a  
  26.         0x0,  
  27.         // key location  
  28.         0,  
  29.         // mic  
  30.         { 0x11, 0x22, 0x33, 0x44},  
  31.         // Lval  
  32.         2,  
  33.         false  
  34.       
  35. };  
  36.   
  37.   
  38.                            
  39. int CC2538cb_CCMEncrypt(uint8_t* input, uint8_t len){  
  40.       ...  
  41. }  
  42.   
  43. int CC2538cb_CCMDecrypt(uint8_t* input, uint8_t len){  
  44.        
  45.    ...  
  46. }  
  47.   
  48. #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路由等信息了;

  1. COMPONENT=TestCCMAppC  
  2. CFLAGS += -DNOT_USE_PRINTFC_BUT_USE_PRINT  
  3. CFLAGS += -DCC2538_HW_SECURITY  
  4. include $(MAKERULES)  


TestCCMAppC.nc文件:

  1. configuration TestCCMAppC{  
  2. }  
  3. implementation{  
  4.   components MainC, TestCCMC;   
  5.   TestCCMC -> MainC.Boot;  
  6.   
  7. }  


TestCCMC.nc文件:

  1. /*******************************************************************  
  2.  *附加实验1:CCM加密解密实验  
  3.  *节点需求数1  
  4.  *编译命令make cc2538cb  
  5.  *学会使用make中的编译选项 CFLAGS += -DCC2538_HW_SECURITY  
  6.  *开启CCM  配置在tos/chip/cc2538/aes的cc2538_ccm.c文件中,设置key  
  7.  *在tos/chip/cc2538/aes的cc2538_ccm.h文件中,设置AUTHENTICATION_LENTH长度  
  8.  *长度取值去参考tos/chip/fwlib/ccm.c(ccm.h)  
  9.  *注意,在后面的网络测试,一旦一个节点开启CCM,其他节点都要开启,否则无法通信  
  10.  *通过wireshar抓包可以看到 已加密 不知道密钥 无法解密  
  11.  *更多其他加密方式,可以参考TI加密合集(网盘TI源码)  
  12.  ********************************************************************/  
  13.   
  14. #include "printf.h"  
  15. #include "cc2538_ccm.h"  
  16.   
  17. module TestCCMC{  
  18.    uses interface Boot;  
  19. }  
  20. implementation{  
  21.   
  22.   uint8_t Test_IN[15 + AUTHENTICATION_LENTH];  
  23.   /***************************************************  
  24.   *启动事件  
  25.   ****************************************************/  
  26.   event void Boot.booted(){  
  27.     uint8_t i;  
  28.     //初始化值  
  29.     for(i = 0; i < sizeof(Test_IN); i++)  
  30.       Test_IN[i] = i;  
  31.     printf("原始:");  
  32.     for(i = 0; i < sizeof(Test_IN) - AUTHENTICATION_LENTH; i++)  
  33.       printf(" %d", Test_IN[i]);  
  34.     printf("\n");  
  35.       
  36.     // 加密  CCM Authentication + Encryption  
  37.     CC2538cb_CCMEncrypt(Test_IN, sizeof(Test_IN) - AUTHENTICATION_LENTH);  
  38.     printf("加密:");  
  39.     for(i = 0; i < sizeof(Test_IN); i++)  
  40.       printf(" %d", Test_IN[i]);  
  41.     printf("\n");  
  42.     // 解密   CCM Inverse Authentication + Decryption  
  43.     CC2538cb_CCMDecrypt(Test_IN, sizeof(Test_IN));  
  44.     printf("解密:");  
  45.     for(i = 0; i < sizeof(Test_IN) - AUTHENTICATION_LENTH; i++)  
  46.       printf(" %d", Test_IN[i]);  
  47.     printf("\n");  
  48.   
  49.   }  
  50.   
  51. }  

结合之前的知识,大家发现;TinyOS使用C文件和C语言使用c文件没有区别;例程比较简单,加密一个数组长度为15个字节;大家可以测试加密其他长度,初值可以自己设定!

下面是运行截图:



评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章