#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mbedtls/config.h"

#include "mbedtls/aes.h"
#include "mbedtls/bignum.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ecp.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/hmac_drbg.h"
#include "mbedtls/hkdf.h"
#include "mbedtls/md.h"
#include "mbedtls/pk.h"
#include "mbedtls/rsa.h"
#include "mbedtls/sha1.h"
#include "mbedtls/sha256.h"


#include "defines.h"

  //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

void main()
{

  uint8_t* UDS_ID = calloc(1,sizeof(uint8_t)*UDS_DGST_SIZE);
  uint8_t* FW_ID = calloc(1,sizeof(uint8_t)*FW_DGST_SIZE);
  uint8_t* CD_ID = calloc(1,sizeof(uint8_t)*CDI_DGST_SIZE); 

  uint8_t UDSbuf[UDS_SIZE] = {0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25};
  uint8_t FWbuf[100] = {0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25,0Xe3,0xc5,0x58,0xaa,0x2f,0xd2,0x19,0x25};


  
  mbedtls_sha256_ret( UDSbuf,UDS_SIZE,UDS_ID,0 );
  mbedtls_sha256_ret(FWbuf,100,FW_ID,0 );


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

  for (int i = 0; i < 32; i++)
      printf("%hhx", FW_ID[i]);
  printf(" :  FW ID\n" );


	
  mbedtls_sha256_context sha_ctx;
  mbedtls_sha256_init(&sha_ctx);

  
  mbedtls_sha256_starts_ret(&sha_ctx,0);
  mbedtls_sha256_update_ret(&sha_ctx, UDS_ID, 32);
  mbedtls_sha256_update_ret(&sha_ctx, FW_ID, 32);
  mbedtls_sha256_finish_ret(&sha_ctx,CD_ID);

  mbedtls_sha256_free(&sha_ctx);
  free(UDS_ID);
  free(FW_ID);

  for(int i = 0; i < 32; i++)
    printf("%hhx",CD_ID[i]);
  printf(" :  CDI digest\n");
	


  printf("SUCCESSUL EXIT\n");

}
