1. 程式人生 > >DSP TMS320操作加密晶片原始碼

DSP TMS320操作加密晶片原始碼

DSP TMS320操作SMEC98SP加密晶片的事例程式碼,如果需要完整程式碼(包括加密晶片程式碼),請到中巨偉業 http://www.sinormous.com/download.html下載

//###########################################################################
//
// FILE: SMEC98SPforTMS320
//
// TITLE: SMEC98SP加密晶片在DSP中的應用
// 深圳市中巨偉業資訊科技有限公司
// www.sinormous.com;
// Author:顧萬水
// QQ:47583353

#include “DSP2833x_Device.h” // DSP2833x 標頭檔案
#include “DSP2833x_Examples.h” // DSP2833x 例子相關標頭檔案
#include

#include <stdlib.h>
#include “iic_smec98sp.h”
#include “smec98sp.h”

/*
1.獲取SMEC98SP的UID號, 獲取MCU隨機數
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;					//隨機數種子
unsigned char bRandom[8] = {0};				//存放隨機數
unsigned char bSessionKey[8] = {0};			//存放過程金鑰,過程金鑰為臨時產生的金鑰
unsigned char bDataBuf[64] = {0};
unsigned char ret, bLen;
unsigned short i, j;


RandomSeek = (unsigned short)time(0);	//RandomSeek一定要使用隨機的變數,否則主控MCU產生的隨機數將是固定植

/*獲取SMEC98SP的UID*/
ret = SMEC_GetUid(bSmec98spUid);
if(ret)
{
	//printf("SMEC_GetUid -> Error !\r\n");
	while(1);
}
//printf("SMEC_GetUid: ");
//PrintHex(bSmec98spUid, 12);
//printf("\r\n");

/*將隨機數RandomSeek,再做一次隨機處理(SMEC98SP的UID作繫結, 使得即使相同情況下,不同的SMEC98SP隨機數種子也不同)*/
for(i = 0; i < 6; i += 2)
{
	/*使RandomSeek與SMEC98SP的UID相關*/
	j = (bSmec98spUid[i] << 8) + bSmec98spUid[i + 1];
	RandomSeek ^= j;
}
srand(RandomSeek);
//printf("RandomSeek: %04x \r\n", RandomSeek);

/*PIN碼驗證*/
ret = SMEC_CheckPin(Pin, (unsigned char)sizeof(Pin));
if(ret)
{
	//printf("SMEC_CheckPin -> Error !\r\n");
	while(1);
}
//printf("SMEC_CheckPin OK !\r\n");

/*內部認證, 主控晶片對SMEC98SP加密晶片合法性判斷*/
for(i = 0; i < 8; i ++)
{
	bRandom[i] = (unsigned char) rand();
}
ret = SMEC_IntrAuth(InternalKey, bRandom);
if(ret)
{
	//printf("SMEC_IntrAuth -> Error !\r\n");
	while(1);
}
//printf("SMEC_IntrAuth OK !\r\n");

/*外部認證, SMEC98SP加密晶片對主控晶片合法性判斷*/
ret = SMEC_ExtrAuth(ExternalKey);
if(ret)
{
	//printf("SMEC_ExtrAuth -> Error !\r\n");
	while(1);
}

// printf(“SMEC_ExtrAuth OK !\r\n”);

/*SHA1摘要演算法認證, 資料長度可自己設定*/
for(i = 0; i < 16; i ++)
{
	bDataBuf[i] = (unsigned char) rand();
	//bDataBuf[i] = (unsigned char) i;
}
ret = SMEC_Sha1Auth(SHA1_Key, (unsigned char)sizeof(SHA1_Key), bDataBuf, 16);
if(ret)
{
	//printf("SMEC_Sha1Auth -> Error !\r\n");
	while(1);
}
//printf("SMEC_Sha1Auth OK !\r\n");


/*呼叫加密晶片內部計算圓周長演算法*/
bDataBuf[0] = 0x02;
ret = SMEC_CircleAlg(bDataBuf, 1, bDataBuf, &bLen);
if(ret)
{
	//printf("SMEC_CircleAlg -> Error !\r\n");
	while(1);
}
//printf("SMEC_CircleAlg OK, C = %02x !\r\n", bDataBuf[0]);

/*產生過程金鑰,用於後續的Flash資料加密讀,及構造的"埠資料運算"*/
for(i = 0; i < 8; i ++)
{
	bRandom[i] = (unsigned char) rand();
}
ret = SMEC_GenSessionKey(MKey, bRandom, bSessionKey);
if(ret)
{
	//printf("SMEC_GenSessionKey -> Error !\r\n");
	while(1);
}
//printf("SMEC_GenSessionKey OK !\r\n");

/*密文讀取Flash資料*/
ret = SMEC_CryptReadFlash(bSessionKey, 0x0000, bDataBuf, 16);
if(ret)
{
	//printf("SMEC_CryptReadFlash -> Error !\r\n");
	while(1);
}
//printf("SMEC_CryptReadFlash OK:\r\n");
//PrintHex(bDataBuf, 16);

/*讀取Flash資料*/
ret = SMEC_ReadFlash(0x0000, bDataBuf, 16);
if(ret)
{
	//printf("SMEC_ReadFlash -> Error !\r\n");
	while(1);
}
//printf("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)
{
	//printf("SMEC_WriteFlash -> Error !\r\n");
	while(1);
}
//printf("SMEC_WriteFlash OK!\r\n");

/*構造"埠資料運算", 可以用實際的PA~PC埠資料*/
bDataBuf[0] = 0x00;
bDataBuf[1] = 0x00;
ret = SMEC_GpioAlg(bSessionKey, bDataBuf,2, bDataBuf);
if(ret)
{
	//printf("SMEC_GpioAlg -> Error !\r\n");
	while(1);
}
//printf("SMEC_GpioAlg OK:\r\n");

// PrintHex(bDataBuf, 2);

/*呼叫加密晶片內部計算圓周長演算法,並密文線上路上傳輸*/
bDataBuf[0] = 0x02;
ret = SMEC_CircleAlgCrypt(bSessionKey, bDataBuf, 1, bDataBuf, &bLen);
if(ret)
{
	//printf("SMEC_CircleAlgCrypt -> Error !\r\n");
	while(1);
}
//printf("SMEC_CircleAlg OK, C = %02x !\r\n", bDataBuf[0]);

}

void main(void)
{
InitSysCtrl();

SMEC_I2cInit();
SMEC_Test();

while(1);
}

//===========================================================================
// No more.
//===========================================================================