#include "layer2.h"

//RIOT core

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

  //readUDS function

  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);
  //deriveDeviceIDKeyPair(CDIKEY, USE_ECC);


printf("pass 100\n");

  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


  //return pubkey and privkey both to L2?


  KeyDrv_context DID_ctx;
  DID_ctx.ENT_MODE = DETERM;	//deterministec
  DID_ctx.PKC_MODE = isECC;
  DID_ctx.seed = CDIKEY;
  DID_ctx.phrase = IDENTITY;


  if(DID_ctx.PKC_MODE == isECC)
  { 

  	//mbedtls_mpi secret;
    mbedtls_mpi_init(&DID_ctx.secret);

    //mbedtls_ecp_point Public;
    mbedtls_ecp_point_init(&DID_ctx.Public);

    //return pubkey and privkey both to L2?
    deriveECCKeyPair(&DID_ctx);		

    //secret and Public contain raw key inormation of generated keys
    //Public also needs group and context to handle ECP
    //Converting raw info to PEM is not easible like this

    //free(DID_ctx);

    return;
  }

  else
  {

    deriveRSAKeyPair();
    return;
  }


}