DeviceKey API usage on nrf52840 blocks deep sleep?

BacBen
Tue, 15 Jun 2021 09:31:40 +0000
Mbed OS

Hello all,

not sure if this is a bug or known behavior but when I am using the DeviceKey API on mBedOS 6 (I have tested 6.5, 6.7 and 6.11) on my nrf52850 I experience a block of the mbedOS deep sleep functionality of about 67% of the time even though the key was already created.

Does anyone has any idea why this happens or even has a solution? Here is my (simple) example for the test (don´t forget to add “platform.all-stats-enabled”: true to your target_overrides in mbed config):

/*Device Key generation test on mBedOS 6 */

#include "mbed.h"
#include "mbed_stats.h"
#include "DeviceKey.h"

DigitalOut led_green(LED1, 1);
DigitalOut led_red(LED2, 1);

// If key derivation fails hang
void hang()
{
    printf("HARDFAULT, hanging\n");
    led_red = 0;
    while(true) {};
}

// Print an unsigned char buffer in hex format
void print_buffer(unsigned char *buf, size_t size)
{
    for (size_t i = 0; i < size; i++) {
        printf("%02X", buf[i]);
    }
}

// Create/print the device key and salt
int Device_Key_Gen() {

	int status = DeviceKey::get_instance().generate_root_of_trust();

	if(status == DEVICEKEY_SUCCESS) {
		//success
		printf("SUCCESS: Root of trust generated!\n");
	} else {
	   //error
	   printf("INFO: Root of trust not generated! Maybe already exists!\n");
	}

	DeviceKey &devkey = DeviceKey::get_instance();

	unsigned char derive_key1 [DEVICE_KEY_32BYTE];
	unsigned char salt1[] = "ch00se_s0m3_salt";
	int ret = DEVICEKEY_SUCCESS;

    // 32byte key derivation
    ret = devkey.generate_derived_key(salt1, sizeof(salt1), derive_key1, DEVICE_KEY_32BYTE);
    if (DEVICEKEY_SUCCESS != ret) {
        printf("Error, derive key failed with error code %d \n", ret);
        return -1;
    }

	// print device key and salt
	printf("Device Key = ");
	print_buffer(derive_key1, DEVICE_KEY_32BYTE);
	printf("\n");
	printf("Salt for key derivation = %s\n", salt1);

	return 0;
}

int main()
{
	// start
	led_green = 1;
	led_red = 1;

    printf("Device Key...");
	if (!Device_Key_Gen() == 0) {
		hang();
	}
	printf("OK\n");


	while (true) {

	//let the status led blink
        led_green = !led_green;
		ThisThread::sleep_for(100ms);
		led_green = 1;
		ThisThread::sleep_for(10s);

	//some mbed stats to check sleep status
 		mbed_stats_cpu_t stats;
		mbed_stats_cpu_get(&stats);
		printf("Uptime: %llu ", stats.uptime / 1000);
		printf("Sleep time: %llu ", stats.sleep_time / 1000);
		printf("Deep Sleep: %llu\n", stats.deep_sleep_time / 1000);

    }
}

4 posts - 2 participants