⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.c

📁 网络中交换节点的上数据的交换和下行数据分发的硬件实现
💻 C
📖 第 1 页 / 共 2 页
字号:
 #include "MInt.h"
 #include "head.h"
 #include "reg.h"
 //#define SWAB32(X)  ((((X)&0xff000000)>>24)|(((X)&0x00ff0000)>>8)|(((X)&0x0000ff00)<<8)|(((X)&0x000000ff)<<24))
 /*本函数用于使用输入的RSA公钥加、解密数据*/
  
  
 void  ResetRSA()
   { 
   unsigned volatile int *point,temp;
   int j; 
   *(unsigned volatile int *)CECTL2 = 0x2AB22A23;
  *(unsigned volatile int *)PCR0   = *(unsigned volatile int *)PCR0 & 0xfffffffe;
    for(j=0;j<0x20;j++)
   { asm(" nop ");
     }
    *(unsigned volatile int *)PCR0   = *(unsigned volatile int *)PCR0 |0x01;

    point = (unsigned volatile int *)0x2030400;
   *point = 0xffff;
   temp = *point;
    }

     void delay()
    {
    unsigned volatile int i;
    for (i=0;i<100;i++)
        {  
          asm ("nop");
         }
     }
  
     int rsapub_inputkey_ende()
       {
       
        extern void memcopy(),poke();
        extern int peek();
        unsigned volatile int *point;
        MInt N,A,C,M;
	    unsigned  volatile int  p0,q0,temp;
	    dword   N0;
         int i;
       M.length =  32;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
         C.length =  32;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
       N.length =  32;
        for(i=0;i<32;i++)
        {
        N.value[i] = peek(0x80008808+4*i,2);
        }
        C.length = 32;
        for(i=0;i<32;i++)
        {
        C.value[i] = peek(0x80008888+4*i,2);
        }
       
        Generate_A_N0(&N,&A,&N0);          
         ResetRSA();
        
//set rsa mode = 1024
point = (unsigned volatile int *)0x2030410;
*point = 0x80000008;

//send N0
point = (unsigned volatile int *)0x2030430;
*point = N0;

//send N
point = (unsigned volatile int *)0x2030000;
for(i=0;i<32;i++)
   {
      *point = N.value[i];
      delay();
      point++;
   } 


//send E
point = (unsigned volatile int *)0x2030100;

   /*   for(i=0;i<32;i++)
   {
      *point = E.value[i];
      delay();
      point++;
   } */
   *point = 65537;

//send C

point = (unsigned volatile int *)0x2030200;
for(i=0;i<32;i++)
   {
      *point = C.value[i];
      point ++;
   }   
   

   
//send A

point = (unsigned volatile int *)0x2030300;
for(i=0;i<32;i++)
   {
      *point = A.value[i];
      point ++;
   }

//send 指数长度寄存器a
point = (unsigned volatile int *)0x2030450;
*point = 0;


//start rsa
point = (unsigned volatile int *)0x2030410;
*point = 0x80000009;

wait_rsa_ready:
   point = (unsigned volatile int *)0x2030420;
   temp = *point & 0x04;
   if( temp == 0x04)
   {goto wait_rsa_ready;}

//read M
  point =  (unsigned volatile int *)0x2030300;
  for(i=0;i<32;i++)
   {
       M.value[i] = *point ;
      point ++;
   }  
        
   for(i=0;i<32;i++)
     {
       poke(0x80008804+4*i,M.value[i],2);
     }   
          
    poke(0x80008800,0x0,2);
        
     return 0x00;

       }
        
        
   /*本函数用于使用卡内的私钥加解密数据*/
   
   
       int rsapri_ende()
       {  
       
       extern void memcopy(),poke(),DR(),NN_Assign(),NN_Mult();
        extern int peek();
        
       
        DECODEBLOCK RSA_key_d;
        unsigned volatile int *point;
        BIG c,t1,t2,mp,mq,m;
        MInt N1,A1,C1,M1,N2,A2,C2,M2,E1,E2;
	    unsigned  volatile int  p0,q0,N01,temp,N02,digits;
         int i,j;
            N1.length=16;
            A1.length=16;
            C1.length=16;
            M1.length=16;
            E1.length=16;
            N2.length=16;
            A2.length=16;
            C2.length=16;
            M2.length=16;
            E2.length=16;            
         memcopy(0x8000C40C,RSA_key_d.p,16,2);         /*读出本卡的私钥p*/
        memcopy(0x8000C48C,RSA_key_d.q,16,2);         /*读出本卡的私钥q*/
        
        memcopy(0x8000C50C,RSA_key_d.dModp_1,16,2);   /*读出本卡的私钥dModp_1*/
        memcopy(0x8000C58C,RSA_key_d.dModq_1,32,2);   /*读出本卡的私钥dModq_1*/
        memcopy(0x8000C60c,RSA_key_d.qInv,32,2);      /*读出本卡的私钥qInv*/
        memcopy(0x80008808,c,32,2);
        for(i=0;i<16;i++)
        {
        N1.value[i] = peek(0x8000C40C+4*i,2);
        N2.value[i] = peek(0x8000C48C+4*i,2);
        E1.value[i] = peek(0x8000C50C+4*i,2);
        E2.value[i] = peek(0x8000C58C+4*i,2);
        }
        digits=(1024/2+NN_DIGIT_BITS-1)/NN_DIGIT_BITS;
/*  c mod p */
        NN_Mod(t1,c,digits*2,RSA_key_d.p,digits);
        
        memcopy(t1,0x80008804,16,2);
        for(i=0;i<16;i++)
        {
       C1.value[i] = peek(0x80008804+4*i,2);
       
        }
/*  c mod q */         
         NN_Mod(t2,c,digits*2,RSA_key_d.q,digits);
        
        memcopy(t2,0x80008804,16,2);
        for(i=0;i<16;i++)
        {
       C2.value[i] = peek(0x80008804+4*i,2);
       
        }
 
   Generate_A_N0(&N1,&A1,&N01); 
      
     ResetRSA();
        
//set rsa mode = 512
point = (unsigned volatile int *)0x2030410;
*point = 0x8000000C;

//send N0
point = (unsigned volatile int *)0x2030430;
*point = N01;

//send N
point = (unsigned volatile int *)0x2030000;
for(i=0;i<16;i++)
   {
      *point = N1.value[i];
      delay();
      point++;
   } 


//send E
point = (unsigned volatile int *)0x2030100;
for(i=0;i<16;i++)
   {
      *point = E1.value[i];
      delay();
      point++;
   }
  
//send C

point = (unsigned volatile int *)0x2030200;
for(i=0;i<16;i++)
   {
      *point = C1.value[i];
      point ++;
   }   
   

   
//send A

point = (unsigned volatile int *)0x2030300;
for(i=0;i<32;i++)
   {
      *point = A1.value[i];
      point ++;
   }

//send 指数长度寄存器a
point = (unsigned volatile int *)0x2030450;
*point = 15;


//start rsa
point = (unsigned volatile int *)0x2030410;
*point = 0x8000000D;

wait_rsa_ready:
   point = (unsigned volatile int *)0x2030420;
   temp = *point & 0x04;
   if( temp == 0x04)
   {goto wait_rsa_ready;}

//read M
  point =  (unsigned volatile int *)0x2030300;
  for(i=0;i<16;i++)
   {
       M1.value[i] = *point ;
      point ++;
   }  
        
   
        
   Generate_A_N0(&N2,&A2,&N02); 
      
     ResetRSA();
        
//set rsa mode = 512
point = (unsigned volatile int *)0x2030410;
*point = 0x8000000C;

//send N0
point = (unsigned volatile int *)0x2030430;
*point = N02;

//send N
point = (unsigned volatile int *)0x2030000;
for(i=0;i<16;i++)
   {
      *point = N2.value[i];
      delay();
      point++;
   } 


//send E
point = (unsigned volatile int *)0x2030100;
for(i=0;i<16;i++)
   {
      *point = E2.value[i];
      delay();
      point++;
   }
  
//send C

point = (unsigned volatile int *)0x2030200;
for(i=0;i<16;i++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -