This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Trying to implement ECDH key exchange using tinycrypt but the random num gen keeps crashing

Hello, 

I have been trying to implement the Elliptic-curve Diffie–Hellman key exchange using tiny crypt.

As per the API, a random number generator function needs to be passed to create the public and private keys. The random gen function is passed using the setter function uECC_set_rng, I was able to pass the random gen function as a parameter to that function, and further when I call the  uECC_make_key and when that function, in turn calls the random gen function for its purpose, the program crashes and reboots. The code has been sown below: 

static int random_gen(uint8_t *dest, unsigned size);

void main(void)
{
    int rc;
    uint8_t pk[64];
    uint8_t private_key[32];
	uint8_t buffer[10];
	
    uECC_set_rng(&random_gen);
    
	rc = uECC_make_key(pk, private_key, &curve_secp256r1);
	if(!rc){
		printk("Failed\n");
	 }
	else{
		printk("Success\n");	
	}
	
	return ;
}


static int random_gen(uint8_t *dest, unsigned size) {

	uint8_t buffer[64];
	int r = 0;
	
	r = sys_csrand_get(dest,size); //crashes here and reboots
	if (!r){
		printk("success!\n");
	}

	return 1;
}
The program crashes at the sys_csrand_get function, I tried replacing this function and implementing something similar to the entropy example to generate a random number but with the same result.
Any help would be appreciated.
cheers.
Parents Reply
  • Hey, 

    No problem, Please find attached the project file. I have attached two projects, one is the project that implements ECDH using Tinycrypt and the other using mbedtls library, both have similar problems, they crash out at some point due to the random number generator. If you could help with getting either one of them to work, it would be great. The mbedtls project is based on an ECDH sample that was provided in their Github. I'm using the latest SDK v1.4.0 with the latest nRF command-line tools with the nRF52840-DK board.

    Thanks for your time.

    .Tinycrypt-ECDH-sample.zip

    crypto.zip

Children
Related