others/fw-verif/8051secureboot/fw/rsa.c
// set up message and compute SHA
set up message and compute SHA
set up message and compute SHA
unsigned char sha1(unsigned char *m, unsigned int len)
{
    unsigned int i;
    unsigned int mlen;
    if(sha_regs.rd_addr != sha_m || sha_regs.wr_addr != hash)
        return 0;
    mlen = ((len+4) & 0xFFC0) + 64; 
    unlock(pshai, sha_regs.rd_addr, sha_regs.rd_addr+mlen);
    if(m != sha_regs.rd_addr) 
        load(m, len, sha_regs.rd_addr, 0); 
    writec(pshai, sha_regs.rd_addr+len, 0x80, 1);
    for(i=len+1; i<mlen; i++)
        writec(pshai, sha_regs.rd_addr+i, 0, 1);
    writec(pshai, sha_regs.rd_addr+mlen-1, (len << 3) & 0xFF, 1);
    writec(pshai, sha_regs.rd_addr+mlen-2, (len >> 5) & 0xFF, 1);
    writec(pshai, sha_regs.rd_addr+mlen-3, (len >> 13) & 0xFF, 1);
    lock(pshai, sha_regs.rd_addr, sha_regs.rd_addr+mlen);
    unlock(pshao, sha_regs.wr_addr, sha_regs.wr_addr+H);
    unlock(SHA, &sha_regs.start, (unsigned char*)(&sha_regs.len));
    writei(SHA, &sha_regs.len, mlen);
    writec(SHA, &sha_regs.start, 1, 1);  
    writec(SHA, &sha_regs.state, 1, 1);  
    while (readc(&sha_regs.state) != 0);
    lock(pshao, sha_regs.wr_addr, sha_regs.wr_addr+H);
    lock(SHA, &sha_regs.start, (unsigned char*)(&sha_regs.len));
    return 1;
}
unsigned char sha1 ( unsigned char * m , unsigned int len ) { unsigned int i ; unsigned int mlen ; if ( sha_regs . rd_addr != sha_m || sha_regs . wr_addr != hash ) return 0 ; mlen = ( ( len + 4 ) & 0xFFC0 ) + 64 ; unlock ( pshai , sha_regs . rd_addr , sha_regs . rd_addr + mlen ) ; if ( m != sha_regs . rd_addr ) load ( m , len , sha_regs . rd_addr , 0 ) ; writec ( pshai , sha_regs . rd_addr + len , 0x80 , 1 ) ; for ( i = len + 1 ; i < mlen ; i ++ ) writec ( pshai , sha_regs . rd_addr + i , 0 , 1 ) ; writec ( pshai , sha_regs . rd_addr + mlen - 1 , ( len << 3 ) & 0xFF , 1 ) ; writec ( pshai , sha_regs . rd_addr + mlen - 2 , ( len >> 5 ) & 0xFF , 1 ) ; writec ( pshai , sha_regs . rd_addr + mlen - 3 , ( len >> 13 ) & 0xFF , 1 ) ; lock ( pshai , sha_regs . rd_addr , sha_regs . rd_addr + mlen ) ; unlock ( pshao , sha_regs . wr_addr , sha_regs . wr_addr + H ) ; unlock ( SHA , & sha_regs . start , ( unsigned char * ) ( & sha_regs . len ) ) ; writei ( SHA , & sha_regs . len , mlen ) ; writec ( SHA , & sha_regs . start , 1 , 1 ) ; writec ( SHA , & sha_regs . state , 1 , 1 ) ; while ( readc ( & sha_regs . state ) != 0 ) ; lock ( pshao , sha_regs . wr_addr , sha_regs . wr_addr + H ) ; lock ( SHA , & sha_regs . start , ( unsigned char * ) ( & sha_regs . len ) ) ; return 1 ; }
set up message and compute SHA
set up message and compute SHA
//assume (sha_ptr.state == 0); //sassert (sha_ptr.state == 0); //assume (rsa_ptr.state == 0); //sassert (sha_regs.rd_addr == sha_m); // addresses have changed //sassert (sha_regs.rd_addr == sha_m); //sassert (sha_regs.wr_addr == hash); // setup data // round len+5 up to multiple of 64 // FIXME //pt.valid[pshai]=1; // reset validity of sha input // don't copy if already in right address // copy m // add 100.. padding //assume (rsa_ptr.state == 0); //sassert (rsa_ptr.state == 0); // insert length in bits // encrypt with sha1 //pt.valid[pshao]=1; // reset validity of sha output //pt.valid[SHA]=1; //sassert (sha_ptr.state == 0); // start HW //sassert (sha_ptr.state == 0); //sassert (readc(&sha_regs.state) == 0); //sassert (sha_ptr.state == 1); //sassert (readc(&sha_regs.state) == 1); //sassert (sha_ptr.state != 1); // XXX encoded bug //sassert (sha_ptr.state == 0); //sassert (readc(&sha_regs.state) == 0); //c_sha(len);         // do SW //while(sha_regs.state != 0); /*
    int cnt = 0;
    while (readc(&sha_regs.state) != 0) {
        cnt += 1;
    }
    assume (cnt <= 2);
    */ //sassert (sha_ptr.state == 0); //sassert (readc(&sha_regs.state) == 0); //if (nd()) sassert (0); //sassert (0);