#include <iostypes.h>
#include "conversions.h"
#include "poly_math.h"
#include "diag.h"
#include "iop.h"
#include "rvldv.h"
#include "csl.h"

#define STACK_SIZE (32*1024)
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;


static unsigned long seed;

unsigned long myrand(){
    seed++;
    return ((seed << 16) ^ seed);
}

/* printing and other utilities */
void printCSLOSEccPrivateKey(CSLOSEccPrivateKey private){
    int i;
     for(i=0; i< sizeof(CSLOSEccPrivateKey); i++){
        printf(" %02x", private[i]);
        if(((i + 3) % 4)==0){
            printf("\n");
        }
    }
    printf("\n");
}

void printCSLOSEccPublicKey(CSLOSEccPublicKey public){
    int i;
    printf("\nx->\n");
    for(i=0; i< sizeof(CSLOSEccPublicKey)/2; i++){
        printf(" %02x", public[i]);
        if(((i + 3) % 4)==0){
            printf("\n");
        }
     }
     
    printf("\ny->\n");
    for(i=sizeof(CSLOSEccPublicKey)/2; i< sizeof(CSLOSEccPublicKey); i++){
        printf(" %02x", public[i]);
        if(((i + 1) % 4)==0){
            printf("\n");
        }
    }
    printf("\n");
}

int main(){
    
    
    u32  private_key1[64];
    point pt, pt_copy;
    unsigned char pt_string[30];
    unsigned char private_key1_str[32], private_key1_dup[32];
    int i;
    field_2n pvt_key_field, pvt_key_field_copy;
    CSLOSEccPrivateKey pvt_key;
    int iter;

    field_2n private_key1_ref_field, private_key2_ref_field;
    point public_key1_ref_pt;
    CSLOSEccPublicKey pt_ref_string;
    CSLOSEccPrivateKey private_key1_ref_str;
    CSLOSEccPublicKey public_key1_str, public_key2_str;
    CSLOSEccPrivateKey private_key2_ref_str;
    CSLOSEccSharedKey shared_key1_str, shared_key2_str;
    u32 shared_key1_ref[4];
    CSLOSEccSharedKey shared_key1_ref_str;
    field_2n private_key1_field;
    CSLOSEccExpPublicKey public_key1_exp;
    CSLOSEccExpPublicKey public_key2_exp;
    CSLOSEccPrivateKey private_key2_str, rand_key1_str, rand_key2_str;
    u32 rand_bit, rand_word, mask;
    CSLOSEccSig eccsign_str;
    point eccsign_ref_pt;
    u8 input_hash_string[20];
    u8 ref_hash_string[20] = {0xbc, 0x80, 0xff, 0x5a, 0x0c, 0x7b, 0x28,0xc2,
                              0x3e, 0x63, 0xdb, 0x94, 0xde, 0xd8, 0x04, 0x45, 
                              0xd9, 0x32, 0x54, 0xe6};
#if 0
    u8 input_hash_string[20];
#endif
    field_2n rand_input_field;
    CSLOSEccPrivateKey rand_input_str;    
    CSL_error result;    
//    unsigned int seed;
    seed = 0x12345678;

    for(i=0; i<32; i++){
        private_key1_str[i] = myrand() & 0xff;
    }
    OS2IP((bigint_digit *)private_key1, 8, private_key1_str, 32);
    
    I2OSP(private_key1_dup, 32, (bigint_digit *)private_key1, 8);


    if(memcmp(private_key1_dup, private_key1_str, 32) == 0x0){
        printf("OS2IP <-> I2OSP TEST PASS\n");
    }
    else{
        printf("OS2IP <-> I2OSP TEST FAIL\n");
    }
   

    for(i =0; i < 8; i++){
        pt.x.e[i] = myrand();
        pt.y.e[i] = myrand();
        
    }
    pt.x.e[0] &= UPR_MASK;
    pt.y.e[0] &= UPR_MASK;
    
           
    EC2OSP(&pt, pt_string, OCTET_STRING_LEN*2);

    OS2ECP(pt_string, OCTET_STRING_LEN*2, &pt_copy);
    if((memcmp(pt.x.e, pt_copy.x.e, 32) == 0x0)&&(memcmp(pt.y.e, pt_copy.y.e, 32) == 0x0)){
        printf("EC2OSP <-> OS2ECP TEST PASS\n");
    }
    else{
        printf("EC2OSP <-> OS2ECP TEST FAIL\n");
    }

    for(i =0; i < 8; i++){
        pvt_key_field.e[i] = myrand();         
    }
    pvt_key_field.e[0] &= UPR_MASK;
    pvt_key_field.e[0] &= UPR_MASK;
    
    FE2OSP(pvt_key, &pvt_key_field);
    
    OS2FEP(pvt_key, &pvt_key_field_copy);
    
    if(memcmp(pvt_key_field_copy.e, pvt_key_field.e, 32) == 0x0){
        printf("FE2OSP <-> OS2FEP TEST PASS\n");
    }
    else{
        printf("FE2OSP <-> OS2FEP TEST FAIL\n");
    }



    
    /* compatibility test for key generation */
    private_key1_ref_field.e[0] = 0x00000076;
    private_key1_ref_field.e[1] = 0xe5fa76a8;
    private_key1_ref_field.e[2] = 0xadbe9dbd;
    private_key1_ref_field.e[3] = 0x964a09f2;
    private_key1_ref_field.e[4] = 0xd5d0856a;
    private_key1_ref_field.e[5] = 0x6a458c9d;
    private_key1_ref_field.e[6] = 0x8f499f04;
    private_key1_ref_field.e[7] = 0xafee68e2;
    
    FE2OSP(private_key1_ref_str, &private_key1_ref_field);
    printf("Ref private key in OS form\n");
    printCSLOSEccPrivateKey(private_key1_ref_str);
    CSL_GenerateEccPublicKey(private_key1_ref_str, public_key1_str);
    printf("Public key generated->\n");
    printCSLOSEccPublicKey(public_key1_str);
    
    public_key1_ref_pt.x.e[0] = 0x000001cf;
    public_key1_ref_pt.x.e[1] = 0x00ce2c85;
    public_key1_ref_pt.x.e[2] = 0x08bf5a7f;
    public_key1_ref_pt.x.e[3] = 0x987a2599;
    public_key1_ref_pt.x.e[4] = 0xc3c8031c;
    public_key1_ref_pt.x.e[5] = 0xf7ce069d;
    public_key1_ref_pt.x.e[6] = 0x0b900b2a;
    public_key1_ref_pt.x.e[7] = 0xfc317524;

    public_key1_ref_pt.y.e[0] = 0x000000d0;
    public_key1_ref_pt.y.e[1] = 0xebfbecc7;
    public_key1_ref_pt.y.e[2] = 0x31e8f76c;
    public_key1_ref_pt.y.e[3] = 0x35731bb0;
    public_key1_ref_pt.y.e[4] = 0xed35f89d;
    public_key1_ref_pt.y.e[5] = 0x655a6a6a;
    public_key1_ref_pt.y.e[6] = 0x44ce2f6a;
    public_key1_ref_pt.y.e[7] = 0x47fb4539;

    EC2OSP(&public_key1_ref_pt, pt_ref_string, OCTET_STRING_LEN*2);
    printf("Reference needed->\n");
    printCSLOSEccPublicKey(pt_ref_string);

    if(memcmp(public_key1_str, pt_ref_string, OCTET_STRING_LEN*2) == 0){
        printf("Key generation reference test PASS\n");
    }
    else{
        printf("Key generation reference test FAIL\n");
    }


    /* shared key reference test */
    
    printf("DH reference test.\n");
    private_key2_ref_field.e[0] = 0x00000049;
    private_key2_ref_field.e[1] = 0xF23F599E;
    private_key2_ref_field.e[2] = 0x5DD6BE39;
    private_key2_ref_field.e[3] = 0x4C199BC9;
    private_key2_ref_field.e[4] = 0x583B30A5;
    private_key2_ref_field.e[5] = 0xA2452510;
    private_key2_ref_field.e[6] = 0xA9F9A76F;
    private_key2_ref_field.e[7] = 0xA1733DA1;

    FE2OSP(private_key2_ref_str, &private_key2_ref_field);
    CSL_GenerateEccPublicKey(private_key2_ref_str, public_key2_str);
    
 
    CSL_GenerateEccSharedKey(private_key2_ref_str, public_key1_str, shared_key1_str, sizeof(CSLOSAesKey));
    for(i=0; i< sizeof(CSLOSAesKey); i++){
        printf("shared key aes = %02x\n", shared_key1_str[i]);
    }
 
    CSL_GenerateEccSharedKey(private_key1_ref_str, public_key2_str, shared_key2_str, sizeof(CSLOSAesKey));
    
    if(memcmp(shared_key2_str, shared_key1_str, sizeof(CSLOSAesKey)) ==0x0){
        printf("Shared key compare PASS\n");
    }
    else{
        printf("Shared key compare FAIL\n");
    }

    shared_key1_ref[0] = swap(0x18B5228A);
    shared_key1_ref[1] = swap(0xDCA9CE19);
    shared_key1_ref[2] = swap(0xD2238B7B);
    shared_key1_ref[3] = swap(0x1BED114E);
    /* test later  shared_key1_ref[4] = htonl(0x7168E15D);*/
    memcpy(shared_key1_ref_str, (u8 *)shared_key1_ref, 16);

    for(i=0; i< sizeof(CSLOSAesKey); i++){
      printf("shared key aes = %02x ref = %02x ref_string = %02x\n", shared_key1_str[i], ((u8 *)shared_key1_ref)[i], shared_key1_ref_str[i]);
    }
    if(memcmp(shared_key1_str, (u8 *)shared_key1_ref, 16) == 0){
        printf("Shared Key generation reference test PASS\n");
    }
    else{
        printf("Shared Key generation reference test FAIL\n");
    }


    /* reference test for ECDSA */
    
    /* ref test for ECDSA */
    
    private_key1_ref_field.e[0] = 0x00000029;
    private_key1_ref_field.e[1] = 0x14680292;
    private_key1_ref_field.e[2] = 0xb7f75470;
    private_key1_ref_field.e[3] = 0x325ca4d0;
    private_key1_ref_field.e[4] = 0xa72e1a67;
    private_key1_ref_field.e[5] = 0x18167ca7;
    private_key1_ref_field.e[6] = 0x93047726;
    private_key1_ref_field.e[7] = 0x6d7a4156;
        
    rand_input_field.e[0] = 0x000000B1;
    rand_input_field.e[1] = 0x90060336;
    rand_input_field.e[2] = 0x5D5FA288;
    rand_input_field.e[3] = 0xE12F419C;
    rand_input_field.e[4] = 0xE1DCBD7C;
    rand_input_field.e[5] = 0xDD06CA7A;
    rand_input_field.e[6] = 0x5437A2AC;
    rand_input_field.e[7] = 0xE2B970FC;

    FE2OSP(private_key1_ref_str, &private_key1_ref_field);
    FE2OSP(rand_input_str, &rand_input_field);
      
    CSL_ComputeEccSig(ref_hash_string, private_key1_ref_str, eccsign_str, rand_input_str);
    printCSLOSEccPublicKey(eccsign_str);
      

    eccsign_ref_pt.x.e[0] = 0x00000058;
    eccsign_ref_pt.x.e[1] = 0x7801B2F4;
    eccsign_ref_pt.x.e[2] = 0xAE7BC9FB;
    eccsign_ref_pt.x.e[3] = 0x5F602E24;
    eccsign_ref_pt.x.e[4] = 0x9EF7376C;
    eccsign_ref_pt.x.e[5] = 0xDC1315BA;
    eccsign_ref_pt.x.e[6] = 0x47DE1559;
    eccsign_ref_pt.x.e[7] = 0x34588CC9;
    eccsign_ref_pt.y.e[0] = 0x00000048;
    eccsign_ref_pt.y.e[1] = 0x19235241;
    eccsign_ref_pt.y.e[2] = 0xD19340CC;
    eccsign_ref_pt.y.e[3] = 0xF49ACEA8;
    eccsign_ref_pt.y.e[4] = 0xDABD4FA2;
    eccsign_ref_pt.y.e[5] = 0xE8F2812E;
    eccsign_ref_pt.y.e[6] = 0xBE9E6855;
    eccsign_ref_pt.y.e[7] = 0xEFA314C2;
    EC2OSP(&eccsign_ref_pt, pt_string, OCTET_STRING_LEN*2);
    /* check against reference */
    if (memcmp(pt_string, eccsign_str, sizeof(pt_string)) == 0){
        printf("reference check ECDSA PASS\n");
    }
    else {
        printf("reference check ECDSA FAIL\n");
    }


    CSL_GenerateEccPublicKey(private_key1_ref_str, public_key1_str);
    result = CSL_VerifyEccSig(ref_hash_string, public_key1_str, eccsign_str);
    
      
    if(result == CSL_OK){
        printf("SIGN VERIFY PASS REFERENCE \n");
    }
    else{
        printf("SIGN VERIFY FAIL REFERENCE \n");
    }

    /* random tests for Key generation */
    printf("Random tests for Shared Key Generation\n");
     

    for(iter = 0; iter < 2; iter++){
    /* random private key */
      for(i=0; i< 32; i++){
	rand_key1_str[i] = myrand() & 0xff;
      }
      
      CSL_GenerateEccKeyPair(rand_key1_str, private_key1_str, public_key1_str);
      printCSLOSEccPrivateKey(private_key1_str);
      /* another random private key */
      for(i=0; i< 32; i++){
	rand_key2_str[i] = myrand() & 0xff;
      }
      
      CSL_GenerateEccKeyPair(rand_key2_str, private_key2_str, public_key2_str);
      printCSLOSEccPrivateKey(private_key2_str);
      /* generate the two shared keys */
        
      CSL_GenerateEccSharedKey(private_key2_str, public_key1_str, shared_key1_str,   sizeof(CSLOSAesKey));
      CSL_GenerateEccSharedKey(private_key1_str, public_key2_str, shared_key2_str,  sizeof(CSLOSAesKey));

      if(memcmp(shared_key2_str, shared_key1_str, sizeof(CSLOSAesKey)) ==0x0){
	printf("KEY GEN RANDOM TEST PASS %d \n", iter);
      }
      else {
	printf("KEY GEN RANDOM TEST FAIL %d \n", iter);
      }

     /* testing with precomputed public keys */
      CSL_PrecomputeFour(public_key1_str, public_key1_exp);
      CSL_PrecomputeFour(public_key2_str, public_key2_exp);

      CSL_GenerateEccSharedKeyPre(private_key1_str, public_key2_exp, shared_key1_str, sizeof(CSLOSAesKey));
      CSL_GenerateEccSharedKeyPre(private_key2_str, public_key1_exp, shared_key2_str, sizeof(CSLOSAesKey)); 

      if(memcmp(shared_key2_str, shared_key1_str, sizeof(CSLOSAesKey)) ==0x0){
	printf("KEY GEN PASS (PRECOMPUTED) %d \n", iter);
      }
      else{
	printf("KEY GEN FAIL (PRECOMPUTED) %d \n", iter);
      }

      
      /* negative test, purturb private_key 2 */
      rand_word = ((myrand() & 0x1f) % 29);
      rand_bit = myrand() & 0xff;
      mask = 0x1 << (rand_bit % 8);
      printf("mask = %08x\n", (unsigned int )mask);
      printf("rand_word= %08x\n", (unsigned int )rand_word);
      private_key2_str[rand_word] = private_key2_str[rand_word] ^ mask;
      CSL_GenerateEccSharedKey(private_key2_str, public_key1_str, shared_key1_str, sizeof(CSLOSAesKey));
      
      if(memcmp(shared_key2_str, shared_key1_str, sizeof(CSLOSAesKey)) ==0x0){
	printf("KEY GEN FAIL NEG TEST (PRECOMPUTED) %d \n", iter);
      }
      else{
	printf("KEY GEN PASS NEG TEST (PRECOMPUTED) %d \n", iter);
      }
      
      
    /* test the ECDSA part iteratively */
      for(iter = 0; iter < 20; iter++){
          for(i = 0; i < 20; i++){
              input_hash_string[i] = myrand() & 0xff;
          }
          
          for(i=0; i< 8; i++){
              private_key1_field.e[i] = myrand();
          }
          private_key1_field.e[0] &= UPR_MASK;
      
          for(i=0; i< 8; i++){
              rand_input_field.e[i] = myrand();
          }
          rand_input_field.e[0] &= UPR_MASK;

          FE2OSP(private_key1_str, &private_key1_field);
          FE2OSP(rand_input_str, &rand_input_field);
       
          CSL_ComputeEccSig(input_hash_string, private_key1_str, eccsign_str, rand_input_str);

          CSL_GenerateEccPublicKey(private_key1_str, public_key1_str);
        
          result = CSL_VerifyEccSig(input_hash_string, public_key1_str, eccsign_str);
          
          if(result == CSL_OK){
              printf("SIGN VERIFY PASS %d\n", iter);
          }
          else{
              printf("SIGN VERIFY FAIL %d\n", iter);
          }
      }

    }

    return 0;
}


