#include <ioslibc.h>
#include <types.h>
#include <csl.h>
#include <iosctypes.h>
#include <nitro/os.h>
#include "conversions.h"
#include "test_utils.h"
#include "poly_math.h"


static unsigned long seed;

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

int
memcmp(const void *s1, const void *s2, size_t n)
{
    const u8 *a = s1;
    const u8 *b = s2;

    if (n == 0) {
        return 0;
    }
    while (--n && (*a == *b)) {
        ++a;
        ++b;
    }
    return (*a - *b);
}


void
ecctest()
{
    
    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];
    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;    
    OSTick ticks;
    u64 start_tick, end_tick;
    u64 mSec;
    u32 mSecLo;
//    unsigned int seed;
    seed = 0x12345678;

    OS_InitTick();
    OS_EnableIrq();
    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);
    ticks = OS_GetTick();
    CSL_GenerateEccPublicKey(private_key1_ref_str, public_key1_str);
    ticks = OS_GetTick() - ticks;
    mSec = OS_TicksToMilliSeconds(ticks);
    mSecLo = (u32) (mSec & 0xffffffffULL);
    printf("Elapsed msec for Generate Public Key = %d\n", mSecLo);

    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]);
    }
    ticks = OS_GetTick();
    CSL_GenerateEccSharedKey(private_key1_ref_str, public_key2_str, shared_key2_str, sizeof(CSLOSAesKey));
    ticks = OS_GetTick() - ticks;
    mSec = OS_TicksToMilliSeconds(ticks);
    mSecLo = (u32) (mSec & 0xffffffffULL);
    printf("Elapsed msec for Generate Shared Key = %d\n", mSecLo);


    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] = 0x18B5228A;
    shared_key1_ref[1] = 0xDCA9CE19;
    shared_key1_ref[2] = 0xD2238B7B;
    shared_key1_ref[3] = 0x1BED114E;
    /* test later  shared_key1_ref[4] = htonl(0x7168E15D);*/

    
    if(memcmp(shared_key1_str, 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);

    ticks = OS_GetTick();
    CSL_ComputeEccSig(ref_hash_string, private_key1_ref_str, eccsign_str, rand_input_str);
    ticks = OS_GetTick() - ticks;
    mSec = OS_TicksToMilliSeconds(ticks);
    mSecLo = (u32) (mSec & 0xffffffffULL);
    printf("Elapsed msec for Generate ECC Sign = %d\n", mSecLo);
    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);

    ticks = OS_GetTick();
    result = CSL_VerifyEccSig(ref_hash_string, public_key1_str, eccsign_str);
    ticks = OS_GetTick() - ticks;
    mSec = OS_TicksToMilliSeconds(ticks);
    mSecLo = (u32) (mSec & 0xffffffffULL);
    printf("Elapsed msec for Verify ECC Sign = %d\n", mSecLo);
    printCSLOSEccPublicKey(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);

      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 < 2; 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);
          }
      }

    }
}


