Skip to content

Allow Devicekey::generate_root_of_trust() to define key size. #12823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions features/device_key/source/DeviceKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ int DeviceKey::get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsi
return DEVICEKEY_SUCCESS;
}

int DeviceKey::generate_root_of_trust()
int DeviceKey::generate_root_of_trust(size_t key_size)
{
int ret = DEVICEKEY_GENERATE_RANDOM_ERROR;
uint32_t key_buff[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
Expand All @@ -255,12 +255,16 @@ int DeviceKey::generate_root_of_trust()
return DEVICEKEY_ALREADY_EXIST;
}

if (key_size != DEVICE_KEY_32BYTE && key_size != DEVICE_KEY_16BYTE) {
return DEVICEKEY_INVALID_KEY_SIZE;
}

#if defined(DEVICE_TRNG) || defined(MBEDTLS_ENTROPY_NV_SEED) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
mbedtls_entropy_context *entropy = new mbedtls_entropy_context;
mbedtls_entropy_init(entropy);
memset(key_buff, 0, actual_size);
memset(key_buff, 0, key_size);

ret = mbedtls_entropy_func(entropy, (unsigned char *)key_buff, actual_size);
ret = mbedtls_entropy_func(entropy, (unsigned char *)key_buff, key_size);
if (ret != MBED_SUCCESS) {
ret = DEVICEKEY_GENERATE_RANDOM_ERROR;
} else {
Expand All @@ -271,7 +275,7 @@ int DeviceKey::generate_root_of_trust()
delete entropy;

if (ret == DEVICEKEY_SUCCESS) {
ret = device_inject_root_of_trust(key_buff, actual_size);
ret = device_inject_root_of_trust(key_buff, key_size);
}
#endif

Expand Down
13 changes: 8 additions & 5 deletions features/device_key/source/DeviceKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ class DeviceKey : private mbed::NonCopyable<DeviceKey> {
* Uses TRNG or various other entropy sources to generate random device key and
* inject it into device's KVStore. Device Key can only be generated once.
*
* \return DEVICEKEY_SUCCESS, when device key successfully generated and injected.
* \return DEVICEKEY_ALREADY_EXIST, if the key has already been written.
* \return DEVICEKEY_GENERATE_RANDOM_ERROR if this device does not contain entropy sources and cannot generate a key.
* \return error codes on other failures.
* @param key_size Size of key in bytes to generate. Must be 16 bytes or 32 bytes. Default is 16 bytes.
*
* @return DEVICEKEY_SUCCESS, when device key successfully generated and injected.
* @return DEVICEKEY_ALREADY_EXIST, if the key has already been written.
* @return DEVICEKEY_GENERATE_RANDOM_ERROR if this device does not contain entropy sources and cannot generate a key.
* @return DEVICEKEY_INVALID_KEY_SIZE if key_size is not 32 or 16 bytes.
* @return error codes on other failures.
*/
int generate_root_of_trust();
int generate_root_of_trust(size_t key_size = DEVICE_KEY_16BYTE);

private:
// Private constructor, as class is a singleton
Expand Down