Arduino Mega 2560操作加密芯片源代码
Arduino Mega 2560操作SMEC98SP加密芯片的事例代碼,如果需要完整代碼(包括加密芯片代碼),請到中巨偉業 http://www.sinormous.com/download.html下載
#include “smec98sp.h”
#include “iic_smec98sp.h”
void PrintHex(unsigned char *str,unsigned int len)
{
unsigned int i;
for(i=0;i<len;i++)
{
Serial.print(*str++,HEX);
}
Serial.print("\r\n");
}
/*
1.獲取SMEC98SP的UID號, 獲取STM32的ID, 獲取STM32隨機數
2.驗證PIN
3.內外部認證
4.SHA1=>前置數據^隨機數
5.密文讀
6.讀數據
7.寫數據
8.構造算法(PA口數據->密文送加密芯片, 密文返回)
如果直接引用,請將print的調試信息去除
*/
void SMEC_Test(void)
{
/*各種密鑰,不會在I2C線路上傳輸,可以使用同一組.應該將密鑰分散存儲,防止主控芯片被破解后,被攻擊者在二進制碼中找到密鑰 */
unsigned char InternalKey[16] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };//內部認證密鑰,必須和SMEC98SP一致
unsigned char ExternalKey[16] = {
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F };//外部認證密鑰,必須和SMEC98SP一致
unsigned char SHA1_Key[16] = {
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F }; //哈希算法認證密鑰,必須和SMEC98SP一致
unsigned char MKey[16] = {
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F }; //主控密鑰,用于產生過程密鑰,必須和SMEC98SP一致
unsigned char Pin[8] = {
0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc }; //Pin認證密鑰,必須和SMEC98SP一致
unsigned char bSmec98spUid[12] = {
0 }; //存放SMEC98SP的UID
unsigned short RandomSeek = 0; //隨機數種子
unsigned char bRandom[8] = {
0 }; //存放隨機數
unsigned char bSessionKey[8] = {
0 }; //存放過程密鑰,過程密鑰為臨時產生的密鑰
unsigned char bDataBuf[64] = {
0 };
unsigned char ret, bLen;
unsigned short i, j;
/獲取SMEC98SP的UID/
ret = SMEC_GetUid(bSmec98spUid);
if(ret)
{
Serial.print(“SMEC_GetUid -> Error !\r\n”);
while(1);
}
Serial.print("SMEC_GetUid: ");
PrintHex(bSmec98spUid,12);
/取加密芯片的隨機數,作為MCU的RandomSeek因子,也可以用其他外部因素,如按鍵等待時間等/
if(SMEC_GetRandom(bDataBuf))//獲取8字節隨機數
{
Serial.print(“SMEC_GetRandom -> Error !\r\n”);
while(1);
}
for(i = 0; i < 4; i += 2)
{
/使RandomSeek與加密芯片隨機數有關/
j = (bDataBuf[i] << 8) + bDataBuf[i + 1];
RandomSeek ^= j;
}
for(i = 0; i < 6; i += 2)
{
/使RandomSeek與SMEC98SP的UID相關,使得即使相同狀況下,不同的加密芯片,RandomSeek值也不一樣/
j = (bSmec98spUid[i] << 8) + bSmec98spUid[i + 1];
RandomSeek ^= j;
}
srand(RandomSeek);
Serial.print(“RandomSeek:”);
PrintHex((unsigned char *)&RandomSeek, 2);
/PIN碼驗證/
ret = SMEC_CheckPin(Pin, (unsigned char)sizeof(Pin));
if(ret)
{
Serial.print(“SMEC_CheckPin -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_CheckPin OK !\r\n”);
/內部認證, 主控芯片對SMEC98SP加密芯片合法性判斷/
for(i = 0; i < 8; i ++)
{
bRandom[i] = (unsigned char) rand();
}
ret = SMEC_IntrAuth(InternalKey, bRandom);
if(ret)
{
Serial.print(“SMEC_IntrAuth -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_IntrAuth OK !\r\n”);
/外部認證, SMEC98SP加密芯片對主控芯片合法性判斷/
ret = SMEC_ExtrAuth(ExternalKey);
if(ret)
{
Serial.print(“SMEC_ExtrAuth -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_ExtrAuth OK !\r\n”);
/SHA1摘要算法認證, 數據長度可自己設定/
for(i = 0; i < 16; i ++)
{
bDataBuf[i] = (unsigned char) rand();
}
ret = SMEC_Sha1Auth(SHA1_Key, (unsigned char)sizeof(SHA1_Key), bDataBuf, 16);
if(ret)
{
Serial.print(“SMEC_Sha1Auth -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_Sha1Auth OK !\r\n”);
/調用加密芯片內部計算圓周長算法/
bDataBuf[0] = 0x02;
ret = SMEC_CircleAlg(bDataBuf, 1, bDataBuf, &bLen);
if(ret)
{
Serial.print(“SMEC_CircleAlg -> Error !\r\n”);
while(1);
}
Serial.print("SMEC_CircleAlg OK, C = ");
PrintHex(bDataBuf, 1);
/產生過程密鑰,用于后續的Flash數據加密讀,及構造的"端口數據運算"/
for(i = 0; i < 8; i ++)
{
bRandom[i] = (unsigned char) rand();
}
ret = SMEC_GenSessionKey(MKey, bRandom, bSessionKey);
if(ret)
{
Serial.print(“SMEC_GenSessionKey -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_GenSessionKey OK !\r\n”);
/密文讀取Flash數據/
ret = SMEC_CryptReadFlash(bSessionKey, 0x0000, bDataBuf, 16);
if(ret)
{
Serial.print(“SMEC_CryptReadFlash -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_CryptReadFlash OK:\r\n”);
PrintHex(bDataBuf, 16);
/讀取Flash數據/
ret = SMEC_ReadFlash(0x0000, bDataBuf, 16);
if(ret)
{
Serial.print(“SMEC_ReadFlash -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_ReadFlash OK:\r\n”);
PrintHex(bDataBuf, 16);
/寫Flash數據/
for(i = 0; i < 16; i ++)
{
bDataBuf[i] = (unsigned char) i;
}
ret = SMEC_WriteFlash(0x0000, bDataBuf, 16);
if(ret)
{
Serial.print(“SMEC_WriteFlash -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_WriteFlash OK!\r\n”);
/構造"端口數據運算", 可以用實際的PA~PC端口數據/
bDataBuf[0] = 0x00;
bDataBuf[1] = 0x00;
ret = SMEC_GpioAlg(bSessionKey, bDataBuf,2, bDataBuf);
if(ret)
{
Serial.print(“SMEC_GpioAlg -> Error !\r\n”);
while(1);
}
Serial.print(“SMEC_GpioAlg OK:\r\n”);
PrintHex(bDataBuf, 2);
/調用加密芯片內部計算圓周長算法,并密文在線路上傳輸/
bDataBuf[0] = 0x02;
ret = SMEC_CircleAlgCrypt(bSessionKey, bDataBuf, 1, bDataBuf, &bLen);
if(ret)
{
Serial.print(“SMEC_CircleAlgCrypt -> Error !\r\n”);
while(1);
}
Serial.print("SMEC_CircleAlgCrypt OK, C = ");
PrintHex(bDataBuf, 1);
}
void setup() {
Serial.begin(115200);
SMEC_I2cInit();
}
void loop() {
SMEC_Test();
delay(200);
}
總結
以上是生活随笔為你收集整理的Arduino Mega 2560操作加密芯片源代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POE供电详解
- 下一篇: GMM-HMM语音识别