分享一下CC1310的AES使用方法
时间:12-23
整理:3721RD
点击:
1,使能AES模块,禁止进入待机
// 使能AEC模块, 禁止待机 , 加密的时候才需要这个代码,加密结束之后,释放,允许进入待机就可以了
Power_setDependency(PowerCC26XX_PERIPH_CRYPTO);
Power_setConstraint(PowerCC26XX_SB_DISALLOW);
2,装载密钥到crypto
//密码硬件最多可存储8个密钥。 //对于128位密钥,所有8个从0到8的关键区域都有效 //但是对于192位和256位键,只有键区0,2,4,6有效。
uint8_t keyIndex = CRYPTO_KEY_AREA_0;
uint8_t aesKey[16] = {0x5a, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6c, 0x6c, 0x69, 0x61, 0x6e, 0x63, 0x65, 0x30, 0x39};// 设置变量,也就是加密代码
// Load the key to the crypto unit
CRYPTOAesLoadKey((uint32_t*)aesKey, keyIndex);
3,加密或者解密,并等待成功
CRYPTOAesEcb((uint32_t*)inputdata, (uint32_t*)ouputdata, keyIndex, Encrypt, InterruptsDisabled);// 以阻塞模式进行加密
while (CRYPTOAesEcbStatus() != AES_SUCCESS);
CRYPTOAesEcb((uint32_t*)inputdata, (uint32_t*)ouputdata, keyIndex, Decrypt, InterruptsDisabled);// 以阻塞模式进行解密
while (CRYPTOAesEcbStatus() != AES_SUCCESS);
4,结束AES,这个非常重要,少了这步就会出错了。
CRYPTOAesEcbFinish();
5,失能AES模块,允许进入休眠模式。
// Allow standby and release the crypto module.
Power_releaseConstraint(PowerCC26XX_SB_DISALLOW);
Power_releaseDependency(PowerCC26XX_PERIPH_CRYPTO);
下面是完整的函数测试代码,自己可以修改到自己喜欢的去使用。
#include <aes/aes.h>
#include "uartdisplay/uartdisplay.h"
#include <ti/devices/cc13x0/driverlib/crypto.h>
#include <ti/drivers/power/PowerCC26XX.h>
// 设置变量,也就是加密代码
uint8_t aesKey[16] = {0x5a, 0x69, 0x67, 0x42, 0x65, 0x65, 0x41, 0x6c, 0x6c, 0x69, 0x61, 0x6e, 0x63, 0x65, 0x30, 0x39};
uint8_t AEC_input[16];
uint8_t AEC_output[16];
uint8_t AEC_output2[16];
void testAes()
{
// Enable the AES module
Power_setDependency(PowerCC26XX_PERIPH_CRYPTO);
// 禁止待机 , 加密的时候才需要这个代码,加密结束之后,释放,允许进入待机就可以了
Power_setConstraint(PowerCC26XX_SB_DISALLOW);
uint8_t i;
uint8_t encryptedData[16] = { 0 };
enum
{
Encrypt = 0x01,
Decrypt = 0x00,
InterruptsEnabled = 0x01,
InterruptsDisabled = 0x00,
};
//密码硬件最多可存储8个密钥。 //对于128位密钥,所有8个从0到8的关键区域都有效 //但是对于192位和256位键,只有键区0,2,4,6有效。
uint8_t keyIndex = CRYPTO_KEY_AREA_3;
// Load the key to the crypto unit
CRYPTOAesLoadKey((uint32_t*)aesKey, keyIndex);
// 以阻塞模式进行加密
CRYPTOAesEcb((uint32_t*)AEC_input, (uint32_t*)encryptedData, keyIndex, Encrypt, InterruptsDisabled);
while (CRYPTOAesEcbStatus() != AES_SUCCESS);
// 再次以阻塞模式解密数据
CRYPTOAesEcb((uint32_t*)encryptedData, (uint32_t*)AEC_output, keyIndex, Decrypt, InterruptsDisabled);
while (CRYPTOAesEcbStatus() != AES_SUCCESS);
CRYPTOAesEcbFinish();
// Allow standby and release the crypto module.
Power_releaseConstraint(PowerCC26XX_SB_DISALLOW);
Power_releaseDependency(PowerCC26XX_PERIPH_CRYPTO);
}
赞!
