#include <functions.h>

//string used to test changing FWID


void ROMprotocol(void)
{
  printf("Hello function!\n");

  //readUDS function

 //  uint8_t* UDS_ID = calloc(1,sizeof(uint8_t)*UDS_DGST_SIZE);
	// readUDS(UDS_ID);

	// for (int i = 0; i < UDS_ID_SIZE; i++)
 //  	printf("%x", UDS_ID[i]);
 //  printf(" :	UDS ID\n" );

  	//readFWID function
  // uint8_t* FW_ID = calloc(1,sizeof(uint8_t)*FW_DGST_SIZE);
  // readFWID(FW_ID);

  // for(int i = 0; i < SHA256_DGST_SIZE; i++)
  //    printf("%x",FW_ID[i]);
  //  printf(" : FW_ID\n");

  // uint8_t* CDID = calloc(1,sizeof(uint8_t)*CDI_DGST_SIZE);
  // _calcCDID(CDID);

  // for(int i = 0; i < SHA256_DGST_SIZE; i++)
  //    printf("%x",CDID[i]);
  //  printf(" : CDID main\n");

  uint8_t * CDIKEY = calloc(1, sizeof(uint8_t)*CDI_KEY_SIZE);
  _calcCDIKEY(CDIKEY);

    for(int i = 0; i < CDI_KEY_SIZE; i++)
        printf("%x",CDIKEY[i]);
    printf(" : CDIKEY main\n");

  printf("USE_ECC %d\n", USE_ECC);
  deriveDeviceIDKeyPair(CDIKEY, USE_ECC);


printf("pass 100\n");

  return;
}

void seedRNGSource(uint8_t * CDIKEY)
{
  //srand(); lib fun call //https://stackoverflow.com/questions/55927662/generate-every-time-same-rsa-key-with-c
  //ctr_drbg    //programs/test/benchmark.c:705
  //hmac_drbg

  //seed rng with CDIKEY
  //init
  //drbg seed




  return;
}



void deriveDeviceIDKeyPair(uint8_t * CDIKEY, int isECC)
{

  //firt generate ECC/RA key. 
  //check for deterministic consistency
  //seed RNGs with CDI
  //let's see how it goes

  //add entropy source?
  //seed RNG
  //create ctx
  //init
  //gen keypair

  if(isECC)
  { 
    //mbedtls_ecp_group_id grpID = ECC_CURVE;
printf("pass 1\n");
    const mbedtls_ecp_curve_info *crvInfo;
    crvInfo = mbedtls_ecp_curve_info_from_grp_id(ECC_CURVE);
printf("pass 2\n");

    mbedtls_ecp_group *ecpGrp;
printf("pass 3\n");    
    //mbedtls_ecp_group_init(ecpGrp);
    //mbedtls_ecp_group_load(ecpGrp,ECC_CURVE);
printf("pass 4\n");
    //mbedtls_ecp_keypair *keyPair = {0};
printf("pass 5\n");
    //mbedtls_ecp_keypair_init(keyPair);

printf("pass 6\n");


    printf("ECC is not yet implemented\n");

    //mbedtls_ecp_keypair_free(keyPair);
    //mbedtls_ecp_group_free(ecpGrp);
    printf("pass 7\n");
    return;
  }

  else
  {

    printf("RSA is not yet implemented\n");
    return;
  }


}