?? ecc_encrypt.c
字號:
//***************程序說明****************************************//
//程序名稱: 射頻數(shù)據(jù)通信 AUCT-Scanner.C //
//功能: 無線掃碼器 //
//硬件環(huán)境: MSP430F135; NRF2401 //
//軟件開發(fā)環(huán)境:IAR Embedded Workbench for MSP430 V3 //
//制作單位: 艾易(武漢)電子技術有限公司 //
//作者: //
//日期: 2007.1.1 //
//***************************************************************//
#include <msp430x13x.h>
#include "ECC_Encrypt.h"
#include <string.h>
//--------------------------------------可調(diào)用函數(shù)(子程序)---------------------------//
//**************************************************************
//函數(shù)名稱: void Software_delay(unsigned int i)
//函數(shù)說明: 軟件延時程序
//參數(shù)說明: 延時時間單位:1/4微秒,16位長
//返回值 : 無
//**************************************************************
#include <stdio.h>
#include "stdio.h"
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdlib.h>
unsigned char p=29;
unsigned char a=4;
unsigned char b=20;
//--------------------------------------可調(diào)用函數(shù)(子程序)---------------------------//
//**************************************************************
//函數(shù)名稱: void Software_delay(unsigned int i)
//函數(shù)說明: 軟件延時程序
//參數(shù)說明: 延時時間單位:1/4微秒,16位長
//返回值 : 無
//**************************************************************
void Software_delay(unsigned int i)
{
while(i--)
{;}
}
//**************************************************************
//函數(shù)名稱: void Software_delay(unsigned int i)
//函數(shù)說明: 軟件延時程序
//參數(shù)說明: 延時時間單位:1/4微秒,16位長
//返回值 : 無
//**************************************************************
void delayms(unsigned int i)
{int j;
while(i--)
{
for(j=1;j<=500;j++)
{;}
}
}
//**************************************************************
//函數(shù)名稱: void send_uart_a_byte(unsigned char sendbyte)
//函數(shù)說明: 異步串口發(fā)送一字節(jié)數(shù)據(jù)
//參數(shù)說明: 要發(fā)送的數(shù)據(jù),
//返回值 : 無
//調(diào)用函數(shù):
//引腳說明: TXD_PIN---串口發(fā)送引腳
//**************************************************************
void send_uart_a_byte(unsigned char sendbyte)
{
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready? 異步串口發(fā)送緩存是否準備好。
TXBUF0 = sendbyte; //將輸入的要發(fā)送的數(shù)據(jù)存放到緩存,
}
//******************************************************************
//函數(shù)名稱: void uart_command_prog(void)
//函數(shù)說明: 串口命令處理
//返回值: error
//調(diào)用函數(shù): void send_spi_configure(unsigned char ByteCount); void send_spi_string(void);
//全局變量: g_UartCommand;g_recv_count; g_SpiDataCount; g_SpiAddrCount;g_buf[32]; g_configer[15];
//函數(shù)功能說明:串口命令字為g_UartCommand,其命令共有三個:01,02,03。
// 當為01時表示重新配置射頻頭;為02時表示發(fā)射一幀數(shù)據(jù);為03時表示待機.
//******************************************************************
void uart_command_prog(void)
{
unsigned char i,j;
unsigned char ciphertext1,ciphertext2,cleartext,cleartext1;
unsigned char temp[2];
j=0;
switch(g_UartCommand)
{
case GET_ENCRYPT_PK_COMD: //命令1:請求加密方公鑰
{
if(g_recv_count == 3) //只配置最后一個字節(jié)進行收發(fā)轉換
{
//g_configer[TR_CONFIG_BYTE] = g_buf[0];
//生成加密方私鑰,傳出g_encrypt_private_key:1個字節(jié)
SHA_a_4(g_buf[0],&g_encrypt_private_key);
//生成公鑰,輸入g_encrypt_private_key,px(CONT),py(CONT);輸出公鑰g_encrypt_pk_x,g_encrypt_pk_y。
kp(g_encrypt_private_key,px,py,&g_encrypt_pk_x,&g_encrypt_pk_y);
//生成串口一幀數(shù)據(jù)(命令字,數(shù)據(jù)長度,加密方的公鑰)
g_buf_out[0]=0x01;
g_buf_out[1]=0x04;
g_buf_out[2]=g_encrypt_pk_x;
g_buf_out[3]=g_encrypt_pk_y;
// for(i=0;i<=1000;i++)
//{Software_delay(200);}
//從串口傳出加密方公鑰數(shù)據(jù)
// for(i=0;i<=30;i++)
//{
//delayms(20);
for(i=0;i<=3;i++)
{
send_uart_a_byte(g_buf_out[i]+0x30);
}
//}//
}
else
{
g_uart_answer = 1; //如果g_recv_count不為 3或 17,則報錯。
}
break;
}
case SENT_DECRYPT_PK_COMD: //命令2:向加密方傳送解密方的公鑰
{
g_decrypt_pk_x=g_buf[0]; //保存解密方公鑰x
g_decrypt_pk_y=g_buf[1]; //保存解密方公鑰y
g_buf_out[0]=0x02;
g_buf_out[1]=0x02;
for(i=0;i<=1;i++)
{
send_uart_a_byte(g_buf_out[i]+0x30);
}
break;
}
case SENT_ENCRYPT_COMD: //命令3:向加密方發(fā)送加密命令
{
//從串口收明文mw,向串口發(fā)密文cx,cy。交叉存。
g_buf_out[j++]=0x03;
g_buf_out[j++]=0x08;
for(i=0;i<g_recv_count-2;i++)
{
//加密,mw為明文,輸出cx、cy為密文,yes為是嵌入。
temp[0]=g_buf[i]&0x0F;
temp[1]=g_buf[i]>>4;
//for()
encryption(temp[0],g_decrypt_pk_x,g_decrypt_pk_y,&ciphertext1,&ciphertext2,&yes);
g_buf_out[j++]=ciphertext1;//傳出X
g_buf_out[j++]=ciphertext2;//傳出Y
g_buf_out[j++]=yes;//傳出嵌入標志
encryption(temp[1],g_decrypt_pk_x,g_decrypt_pk_y,&ciphertext1,&ciphertext2,&yes);
g_buf_out[j++]=ciphertext1;//傳出X
g_buf_out[j++]=ciphertext2;//傳出Y
g_buf_out[j++]=yes;//傳出嵌入標志
}
//從串口傳出加密方公鑰數(shù)據(jù)
for(i=0;i<j;i++)
{
send_uart_a_byte(g_buf_out[i]);
}
break;
}
case GET_DECRYPT_PK_COMD:
{
if(g_recv_count == ONE_BYTE_CONFIG) //只配置最后一個字節(jié)進行收發(fā)轉換
{
//g_configer[TR_CONFIG_BYTE] = g_buf[0];
//生成解密方私鑰,傳出g_decrypt_private_key:1個字節(jié)
SHA_b_4(g_buf[0],&g_decrypt_private_key);
//生成公鑰,輸入g_encrypt_private_key,px(CONT),py(CONT);輸出公鑰g_decrypt_pk_x,g_decrypt_pk_y。
kp(g_decrypt_private_key,px,py,&g_decrypt_pk_x,&g_decrypt_pk_y);
//生成串口一幀數(shù)據(jù)(命令字,數(shù)據(jù)長度,解密方的公鑰)
g_buf_out[0]=0x04;
g_buf_out[1]=0x04;
g_buf_out[2]=g_decrypt_pk_x;
g_buf_out[3]=g_decrypt_pk_y;
//從串口傳出解密方公鑰數(shù)據(jù)
for(i=0;i<=3;i++)
{
send_uart_a_byte(g_buf_out[i]+0x30);
}
}
else
{
g_uart_answer = 1; //如果g_recv_count不為 3或 17,則報錯。
}
break;
}
case SENT_ENCRYPT_PK_COMD:
{
g_encrypt_pk_x=g_buf[0]; //保存加密方公鑰x
g_encrypt_pk_y=g_buf[1]; //保存加密方公鑰y
g_buf_out[0]=0x05;
g_buf_out[1]=0x02;
for(i=0;i<=1;i++)
{
send_uart_a_byte(g_buf_out[i]+0x30);
}
break;
}
case SENT_DECRYPT_COMD:
{
g_buf_out[j++]=0x06;
g_buf_out[j++]=0x03;
/* for(i=0;i<g_recv_count-2;i++)
{
//加密,mw為明文,輸出cx、cy為密文,yes為是嵌入。
//decode(g_buf[i],g_buf[i+1],g_encrypt_pk_x,g_encrypt_pk_y,yes,&cleartext);
decode(g_buf[i],g_buf[i+1],g_encrypt_pk_x,g_encrypt_pk_y,g_buf[i+2],&cleartext);
g_buf_out[j++]=cleartext;
i++;
i++;
}*/
//加密,mw為明文,輸出cx、cy為密文,yes為是嵌入。
//decode(g_buf[i],g_buf[i+1],g_encrypt_pk_x,g_encrypt_pk_y,yes,&cleartext);
decode(g_buf[0],g_buf[1],g_encrypt_pk_x,g_encrypt_pk_y,g_buf[2],&cleartext);
decode(g_buf[3],g_buf[4],g_encrypt_pk_x,g_encrypt_pk_y,g_buf[5],&cleartext1);
g_buf_out[j++]=cleartext|(cleartext1<<4);
//從串口傳出加密方公鑰數(shù)據(jù)
for(i=0;i<j;i++)
{
send_uart_a_byte(g_buf_out[i]);
}
break;
}
default:
g_uart_answer = 1; //報錯
break;
}//switch()語句結束
}
unsigned char add(unsigned char add_1,unsigned char add_2)
{
unsigned char sum;
sum=add_1+add_2;
sum=sum%p;
return sum;
}
unsigned char sub(unsigned char sub_1,unsigned char sub_2)
{
unsigned char sum;
sum=sub_1-sub_2;
sum=sum%p;
return sum;
}
unsigned char mul(unsigned char mul_1,unsigned char mul_2)
{
unsigned char sum;
sum=mul_1*mul_2;
sum=sum%p;
return sum;
}
unsigned char division(unsigned char div_1,unsigned char div_2)
{
unsigned char sum;
sum=div_1/div_2;
sum=sum%p;
return sum;
}
void addition(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char *x3,unsigned char *y3)
{
unsigned char x3_tmp=0;
unsigned char y3_tmp=0;
unsigned char fenzi=0;
unsigned char fenmu=0;
unsigned char kuohao=0;
unsigned char kuohao2=0;
unsigned char xiaokuohao=0;
unsigned char dakuohao=0;
if(x1==x2)
square(x1,y1,&x3_tmp,&y3_tmp); //如果兩個數(shù)相同,調(diào)用倍點
else if(y1==0)
{
x3_tmp=x2;
y3_tmp=y2;
}
else if(y2==0)
{
x3_tmp=x1;
y3_tmp=y1;
}
else
{
fenzi=sub(y2,y1);//fenzi=y2-y1
fenmu=sub(x2,x1);//fenmu=x2-x1
kuohao=division(fenzi,fenmu);//kuohao=(y2-y1)/(x2-x1)
kuohao2=mul(kuohao,kuohao);//kuohao2=kuohao^2
x3_tmp=sub(kuohao2,x1);//x3
x3_tmp=sub(x3_tmp,x2);//x3
xiaokuohao=sub(x1,x3_tmp);//xiaokuohao=x1-x3
dakuohao=mul(kuohao,xiaokuohao);//dakuohao=(y2-y1)/(x2-x1)*(x1-x3)
y3_tmp=sub(dakuohao,y1);//y3
}
*x3=x3_tmp;
*y3=y3_tmp;
}
void square(unsigned char x1,unsigned char y1,unsigned char *x3,unsigned char *y3)
{
unsigned char x3_tmp=0;
unsigned char y3_tmp=0;
unsigned char fenzi=0;
unsigned char fenmu=0;
unsigned char kuohao=0;
unsigned char kuohao2=0;
unsigned char xiaokuohao=0;
unsigned char dakuohao=0;
unsigned char h2=2;
unsigned char h3=3;
if(y1==0)
{
x3_tmp=x1;
y3_tmp=y1;
}
else
{
fenzi=mul(x1,x1);//fenzi=x1^2
fenzi=mul(fenzi,h3);//fenzi=3*x1^2
fenzi=add(fenzi,a);//fenzi=3*x1^2+a
fenmu=mul(y1,h2);//fenmu=2*y1
kuohao=division(fenzi,fenmu);//kuohao=(3*x1^2+a)/(2*y1)
kuohao2=mul(kuohao,kuohao);//kuohao2=kuohao^2
x3_tmp=sub(kuohao2,x1);
x3_tmp=sub(x3_tmp,x1);//x3
xiaokuohao=sub(x1,x3_tmp);//xiaokuohao=x1-x3
dakuohao=mul(kuohao,xiaokuohao);//dakuohao=(3*x1^2+a)/(2*y1)*(x1-x3)
y3_tmp=sub(dakuohao,y1);//y3
}
*x3=x3_tmp;
*y3=y3_tmp;
}
void proclaimed(unsigned char mw,unsigned char *pmx,unsigned char *pmy,unsigned char *yes)
{
unsigned char h1=1;
unsigned char h20=20;
unsigned char x3=0;
unsigned char xa=0;
unsigned char bufferyes=0;
int t=0,j;
x3=mul(mw,mw);
x3=mul(x3,mw);
xa=mul(mw,a);
x3=add(x3,xa);
x3=add(x3,b);//x3=x^3+4x+20
t=oula(x3);
if(t==1) //可以嵌入
{
*pmx=mw;
*pmy=(unsigned char)sqrt(x3);
}
else
{
mw=mul(mw,h20);
mw=sub(mw,h1);
for(j=0;j<=19;j++)
{
mw=add(mw,h1);
x3=mul(mw,mw);
x3=mul(x3,mw);
xa=mul(mw,a);
x3=add(x3,xa);
x3=add(x3,b);//x3=x^3+4x+20
t=oula(x3);
if(t==1)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -