#include<reg51.h>
#include<stdio.h>
#include<debug.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sam_syni=P2^0;
sbit sam_reset=P2^1;
sbit sam_ctrlio=P2^2;
sbit sam_control0=P2^3;
sbit sam_control1=P2^4;
sbit sam_ready=P2^5;
sbit sam_busy=P2^6;
sbit sam_cs=P3^5;
//uchar code table1[32]={0x70,0xD0,0x39,0x77,0xCD,0x26,0x78,0x75,
//0x24,0x18,0x09,0x01,0x6A,0xBF,0x13,0x31,0x38,0x72,0xA4,0x1D,
//0x7C,0x2E,0x18,0xA6,0xFB,0xA4,0x94,0xD9,0x16,0x80,0xB9,0x11};
uchar xdata table[];
uchar code table1[32]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01};//明文M
uchar code table2[32]={0x56,0x9E,0xE5,0x58,0xB3,0x4B,0x71,0x6A,
0x55,0x68,0xE7,0x64,0xD6,0x1D,0x11,0x7F,0x50,0x0F,0xC2,0x13,
0xA0,0x45,0xB0,0x98,0x6A,0xFF,0xE5,0xDE,0x0A,0xCB,0xD1,0x09 };//密钥E
uchar code table3[32]={0xB8,0x72,0x04,0x8E,0x00,0xFF,0xD2,0xAA,
0xC4,0x8E,0x4E,0x00,0x25,0x47,0x60,0xF1,0x88,0x4C,0x04,0xB1,
0xBF,0xAC,0x3D,0xD0,0x7B,0x36,0x22,0xD3,0x98,0x17,0x18,0x69 };//密钥N
uchar code table4[32]={0x20,0x47,0xb4,0x95,0x16,0x1b,0x24,0x64,
0xe4,0x5d,0xa8,0xf0,0x3f,0xeb,0x7f,0x0b,0xf8,0xb3,0x89,0x46,
0x2c,0x7f,0x9d,0x3c,0x33,0xf5,0x5e,0x51,0xcb,0x1a,0x00,0xe0};//密钥C
uchar buffer[32];
void delayus(uint i)
{
while(i--);
}
void sam_init()
{
sam_reset=0;
sam_control0=0;
sam_control1=0;
sam_syni=0;
sam_ctrlio=0;
sam_ready=1;
_nop_();
_nop_();
}
void sam_command()
{
sam_ctrlio=1;
sam_control1=0;
sam_control0=1;
P0=table[0];
_nop_();
_nop_();
sam_syni=1;
delayus(100);
sam_syni=0;
delayus(100);
}
void input_plaintext()//输入RSA明文(长度:256bits)
{
uchar i;
sam_ctrlio=1;
sam_control1=1;
sam_control0=0;
//printf("table1 is:");
for(i=0;i<32;i++)
{
P0=table1[i];//输入相应的数据给sam作为明文
_nop_();
_nop_();
sam_syni=1; //同步信号,常低
delayus(100);
sam_syni=0;
delayus(100);
//printf("%2x\t",table1[i]);
}
//printf("table1 length is %x\n",i);
}
void input_secretkey_E()//输入RSA密钥E(长度:256bits)
{
uchar i;
sam_ctrlio=1;
sam_control1=1;
sam_control0=1;
//printf("table2 is:");
for(i=0;i<32;i++)
{
P0=table2[i];//输入相应的数据给sam作为密钥
_nop_();
_nop_();
sam_syni=1; //同步信号,常低
delayus(100);
sam_syni=0;
delayus(100);
//printf("%2x\t",table2[i]);
}
//printf("table2 length is %x\n",i);
}
void input_secretkey_N()//输入RSA密钥N(长度:256bits)
{
uchar i;
sam_ctrlio=1;
sam_control1=1;
sam_control0=0;
for(i=0;i<32;i++)
{
P0=table3[i];//输入相应的数据给sam作为密钥
_nop_();
_nop_();
sam_syni=1; //同步信号,常低
delayus(100);
sam_syni=0;
delayus(100);
//printf(" %2x\t",table3[i]);
}
}
void input_secretkey_C()//输入RSA密钥C(长度:256bits)
{
uchar i;
sam_ctrlio=1;
sam_control1=1;
sam_control0=1;
//printf("table4 is:");
for(i=0;i<32;i++)
{
P0=table4[i];//输入相应的数据给sam作为密钥
_nop_();
_nop_();
sam_syni=1; //同步信号,常低
delayus(100);
sam_syni=0;
delayus(100);
// printf(" %2x\t",table4[i]);
}
}
void sam_output_result()//输出运算结果
{
uchar i,sam_data;
sam_ctrlio=1;
sam_control1=0;
sam_control0=1;
P0=table[0]; //输入输出指令
_nop_();
sam_syni=1;
delayus(100);
sam_syni=0;
delayus(100);
P0=0xff; //输出前赋初值
sam_ctrlio=0;
sam_data=P0;
printf("buffer is:\n");
printf("%x\t",sam_data);
//sam_ctrlio=0;
//printf("buffer is:\n");
for(i=0;i<31;i++)
{
//P0=0xff;//P0口要赋值
_nop_();
_nop_();
_nop_();
_nop_();
sam_syni=1; //同步信号,常低
delayus(100);
buffer[i]=P0;
sam_syni=0;
delayus(100);
printf("%2x\t",buffer[i]);
}
}
void main()
{
sam_cs=1;
sam_init();
T_Com("sam init success...");
delayus(100);
sam_reset=1;
sam_command(0x11);
T_Com("sam command 0x11...");
input_plaintext();
T_Com("input first plaintext success...");
input_secretkey_E();
T_Com("input secretkey E success...");
input_secretkey_N();
T_Com("input secretkey N success...");
input_secretkey_C();
T_Com("input secretkey C success...");
sam_command(0xff);
T_Com("sam command 0xff...");
T_Com("sam operate start... \n");
while(sam_ready==1);//等待sam计算完毕。
T_Com("sam operate first success... \n");
// sam_command(0x19);
// T_Com("sam command 0x19...");
sam_output_result();
T_Com("fisrst result success...");
sam_command(0x14);
T_Com("sam command 0x14...");
input_plaintext();
T_Com("input second plaintext success...");
sam_command(0xff);
T_Com("sam command 0xff...");
while(sam_ready==1);//等待sam计算完毕。
T_Com("sam operate second success... \n");
sam_command(0x19);
T_Com("sam command 0x19...");
sam_output_result();
T_Com("second result success...");
while(1);
}
void input()interrupt 4
{
uchar len,i,serialStart,j=0;
if(RI) //收到数据
{
serialStart=SBUF;
RI=0;
if(serialStart==0xFA)
{
while(RI==0);
len=SBUF; //第一个字节是数据包的长度
RI=0; //清零RI以便接收下一个
for(i=0;i<len+1;i++)
{
while(!RI)
{
j++;
if(j>1000)
{
break; //break跳出循环语句
}
}
if(j<1000)
{
table[i]=SBUF;
RI=0;
j=0;
}
else
{
break;
}
}
if((i==len+1)&&(table[i-1]==0xFB)) //最后一位是0xFB。
{
REN=0; // 清零接收位
// serialCmd=TRUE; //接收完毕,收到命令有效,置位1 又说明pc发送的数据单片机接收成功
}
else
return;
}
else
return;
}
}