crypto.c 8.21 KB
#include <PR/bcp.h>
#include <PR/R4300.h>
#include <PR/bbsim.h>
#include <PR/bbskapi.h>
#include <PR/bbvirage.h>
#include <PR/os.h>
#include <PR/os_bbfs.h>
#include <PR/os_bbatb.h>
#include <PR/os_bbexec.h>
#include <sha1.h>

#include "libfb.h"
#include "crypto.h"
#include <PR/bbtypes.h>
#include <algorithms.h>
#include <poly_math.h>
#include <bb_nn.h>



/*
 * buffer for FS must be dcache aligned. use macro below.
 */
#define ALIGN_DCACHE __attribute__((aligned(DCACHE_LINESIZE)))

/*
 * debug print support
 */

#undef PRINTF

#ifdef DEBUG
#define	PRINTF	osSyncPrintf
#else
#define	PRINTF(format, args...)
#endif



/*
 * Thread and stack structures
 */

char   bootStack[STACKSIZE] __attribute__ ((aligned (8)));

static OSThread idleThread;
static char     idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));

static OSThread mainThread;
static char     mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));

/* 
 * FS specific definitions
 */

static void	idleproc(char *);
static void	mainproc(char *);



#define DMA_QUEUE_SIZE	200

static OSMesg           PiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue      PiMessageQ;

static OSMesg           SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue      SiMessageQ;



/*
 * simple graphics
 */
static OSMesgQueue      retraceMessageQ;
static OSMesg           dummyMessage, retraceMessageBuf;

#if SCREEN_LOW
#define WD	320
#define HT	240
#else
#define WD	640
#define HT	480
#endif
static u16 cfb[WD*HT] __attribute__((aligned(64)));


void  __osBbVideoPllInit(u32);
void boot(u32 miSecModeRegVal)
{
    /* Init the video PLL */
    __osBbVideoPllInit(OS_TV_NTSC);

    osInitialize();

    osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
                   idleThreadStack+STACKSIZE, 8);
    osStartThread(&idleThread);
}


static void
idleproc(char *argv)		/* priority 8 */
{
    osCreateViManager(OS_PRIORITY_VIMGR);
#if	SCREEN_LOW
    osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
#else
    osViSetMode(&osViModeTable[OS_VI_NTSC_HPF1]);
#endif

    /*
     * Start PI Mgr for access to cartridge - start before the debugger
     */
    osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
            DMA_QUEUE_SIZE);

    osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
    osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);
    /*
     * The main thread's priority must be the same or lower than the original
     * idle's thread priority. This allows the idle thread to change its
     * priority to 0 before the main thread starts execution.
     */
    osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
           mainThreadStack+STACKSIZE/8, (OSPri)7);
    osStartThread(&mainThread);

    osSetThreadPri(0, OS_PRIORITY_IDLE);

    for(;;) {
#if 0
	osBbSetErrorLed(1);
	delay();
	osBbSetErrorLed(0);
	delay();
#endif
    }
}

/* debug print support */
#undef PRINTF
#define DEBUG
#ifdef DEBUG
#define	PRINTF	osSyncPrintf
#else
#define	PRINTF(format, args...)
#endif

/* some psuedo random generator */

unsigned int myrand(unsigned int seed){
    seed = 0x343fd * seed + 0x269ec3;
    return ((seed )& 0xffffffff);
            }

/* to get a random field for testing */

void random_key(BbEccPrivateKey key, unsigned int seed)
{
    short int i;
    for(i = 0; i< MAX_LONG; i++){
        key[i] = myrand(seed);
        seed = key[i];
    }
    key[0] &= UPR_MASK;
}


void mainproc(char *argv)
{
  BbEccPrivateKey private_key1;
  BbEccPublicKey public_key1;
  BbEccPrivateKey private_key2;
  BbEccPublicKey public_key2;
  BbAesKey shared_key1;
  BbAesKey shared_key2;
  u8 input_string[1024];
  u32 rand_input[8];
  BbEccSig eccsign;
  boolean res;
  unsigned int seed;
  int i, same;
  int iter =0;
  u32 rand_bit, rand_word, mask;
  char outstring[256];
  u32 starttime;
  u32 endtime;
    

  PRINTF("ENTERED TEST APP\n");
#if SCREEN_LOW
    fbInit(FB_LOW_RES);
#else
    fbInit(FB_HIGH_RES);
#endif
    /* setup text output to screen */
    osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
    osViSetEvent(&retraceMessageQ, dummyMessage, 1);
    osViBlack(1);
    osViSwapBuffer(cfb);
    fbSetBg(fbBlack);
    fbClear();
    osViBlack(0);
    osWritebackDCacheAll();
    osViSwapBuffer(cfb);
    
    
    fbPrintStr(fbWhite, 3, 3, "Crypto Test App");
    osWritebackDCacheAll();
    seed = 0x12345678;
    for(iter = 0; iter < 5; iter++){
    /* random private key */
      random_key( private_key1, seed);
      /* change seed */
      seed = private_key1[MAX_LONG-1];
      PRINTF("BEFORE PUBLIC KEY GEN\n");
      starttime= osGetCount();
      eccGenPublicKey(public_key1, private_key1);
      endtime = osGetCount();
      bzero(outstring, sizeof(outstring));
      sprintf(outstring, "Time taken = %llu",OS_CYCLES_TO_USEC(endtime - starttime));
      fbPrintStr(fbWhite, 3, 3, outstring);
      osWritebackDCacheAll();
      PRINTF("GENERATED ONE KEY ");
      PRINTF(outstring);

      /* random private key */
      random_key( private_key2, seed);
      seed = private_key2[MAX_LONG-1];
      
      eccGenPublicKey(public_key2, private_key2);
      PRINTF("GENERATED ANOTHER KEY \n");
      PRINTF(outstring);

      /* generate the two shared keys */
      starttime= osGetCount();
      eccGenAesKey(public_key1, private_key2, shared_key1);
      endtime = osGetCount();
      PRINTF("GENERATED SHARED KEY 1\n");
      sprintf(outstring, "Time taken = %llu",OS_CYCLES_TO_USEC(endtime - starttime));
      PRINTF(outstring);
      eccGenAesKey(public_key2, private_key1, shared_key2);
      PRINTF("GENERATED SHARED KEY 2\n");
      same = 0;
      for(i=0; i < sizeof(BbAesKey)/sizeof(u32); i++){
	if(shared_key1[i] != shared_key2[i]){
	  same = 1;
	}
      }
      if(same == 0){	
	sprintf(outstring, "KEY GEN OK %d \n", iter);
	fbPrintStr(fbWhite, 3, 7, outstring);
	osWritebackDCacheAll();
        PRINTF(outstring);
      }
      else{
	sprintf(outstring, "KEY GEN NOT OK %d \n", iter);
	fbPrintStr(fbWhite, 3, 20, outstring);
	osWritebackDCacheAll();
	PRINTF(outstring);
      }
    }

    /* use last private key and public key and test sign/verify */
    for(iter = 0; iter < 5; iter++){
      for(i = 0; i < 1024; i++){
	seed = myrand(seed);
	input_string[i] = seed & 0xff;
      }
	
      for(i = 0; i < 8; i++){
	seed = myrand(seed);
	rand_input[i] = seed;
      }  
      rand_input[0] &= UPR_MASK;
      starttime= osGetCount();


      PRINTF("STARTING SIGN\n");
      sprintf(outstring, "Iteration = %d\n", iter);
      fbPrintStr(fbWhite, 3, 7, outstring);
      osWritebackDCacheAll();
      bsl_compute_ecc_sig(input_string, 1024, private_key1, rand_input, eccsign, 0x30);
      endtime = osGetCount();
      bzero(outstring, sizeof(outstring));
      sprintf(outstring, "Time taken sign = %llu\n",OS_CYCLES_TO_USEC(endtime - starttime));
      fbPrintStr(fbWhite, 3, 9, outstring);
      osWritebackDCacheAll();
      PRINTF(outstring);

      starttime= osGetCount();
      PRINTF("STARTING VERIFY\n");
      bsl_verify_ecc_sig(input_string, 1024, public_key1, eccsign, &res, 0x30);
      endtime = osGetCount();
      bzero(outstring, sizeof(outstring));
      sprintf(outstring, "Time taken verify = %llu\n",OS_CYCLES_TO_USEC(endtime - starttime));
      fbPrintStr(fbWhite, 3, 11, outstring);
      osWritebackDCacheAll();
      PRINTF(outstring);
      
      if(res == BSL_TRUE){
        sprintf(outstring, "VERIFY OK %d \n", iter);
	fbPrintStr(fbWhite, 3, 13, outstring);
	osWritebackDCacheAll();
        PRINTF(outstring);
      }
      else{
	sprintf(outstring, "VERIFY NOT OK %d \n", iter);
	fbPrintStr(fbWhite, 3, 21, outstring);
	osWritebackDCacheAll();
        PRINTF(outstring);
      }
      /* change a bit */
      rand_word = myrand(seed) & 0x7;
      rand_bit = myrand(seed) & 0x7f;
      seed = rand_word;
      mask = 0x1 << (rand_bit % 32);
      eccsign[rand_word] = eccsign[rand_word] ^ mask;

      bsl_verify_ecc_sig(input_string, 1024, public_key1, eccsign, &res, 0x30);
          
      if(res == BSL_FALSE){
        sprintf(outstring, "VERIFY NEG TEST OK %d \n", iter);
	fbPrintStr(fbWhite, 3, 22, outstring);
	osWritebackDCacheAll();
        PRINTF(outstring);
      }
      else{
	sprintf(outstring, "VERIFY NEG TEST NOT OK %d \n", iter);
	fbPrintStr(fbWhite, 3, 15, outstring);
	osWritebackDCacheAll();
        PRINTF(outstring);
      }
      //skExit();
    }
        
    
}