Skip to content

Commit 930ef84

Browse files
authored
Merge pull request #12823 from SeppoTakalo/generate_devicekey
Allow Devicekey::generate_root_of_trust() to define key size.
2 parents 08a0920 + fc9e75b commit 930ef84

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

features/device_key/source/DeviceKey.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ int DeviceKey::get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsi
245245
return DEVICEKEY_SUCCESS;
246246
}
247247

248-
int DeviceKey::generate_root_of_trust()
248+
int DeviceKey::generate_root_of_trust(size_t key_size)
249249
{
250250
int ret = DEVICEKEY_GENERATE_RANDOM_ERROR;
251251
uint32_t key_buff[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
@@ -255,12 +255,16 @@ int DeviceKey::generate_root_of_trust()
255255
return DEVICEKEY_ALREADY_EXIST;
256256
}
257257

258+
if (key_size != DEVICE_KEY_32BYTE && key_size != DEVICE_KEY_16BYTE) {
259+
return DEVICEKEY_INVALID_KEY_SIZE;
260+
}
261+
258262
#if defined(DEVICE_TRNG) || defined(MBEDTLS_ENTROPY_NV_SEED) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
259263
mbedtls_entropy_context *entropy = new mbedtls_entropy_context;
260264
mbedtls_entropy_init(entropy);
261-
memset(key_buff, 0, actual_size);
265+
memset(key_buff, 0, key_size);
262266

263-
ret = mbedtls_entropy_func(entropy, (unsigned char *)key_buff, actual_size);
267+
ret = mbedtls_entropy_func(entropy, (unsigned char *)key_buff, key_size);
264268
if (ret != MBED_SUCCESS) {
265269
ret = DEVICEKEY_GENERATE_RANDOM_ERROR;
266270
} else {
@@ -271,7 +275,7 @@ int DeviceKey::generate_root_of_trust()
271275
delete entropy;
272276

273277
if (ret == DEVICEKEY_SUCCESS) {
274-
ret = device_inject_root_of_trust(key_buff, actual_size);
278+
ret = device_inject_root_of_trust(key_buff, key_size);
275279
}
276280
#endif
277281

features/device_key/source/DeviceKey.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@ class DeviceKey : private mbed::NonCopyable<DeviceKey> {
110110
* Uses TRNG or various other entropy sources to generate random device key and
111111
* inject it into device's KVStore. Device Key can only be generated once.
112112
*
113-
* \return DEVICEKEY_SUCCESS, when device key successfully generated and injected.
114-
* \return DEVICEKEY_ALREADY_EXIST, if the key has already been written.
115-
* \return DEVICEKEY_GENERATE_RANDOM_ERROR if this device does not contain entropy sources and cannot generate a key.
116-
* \return error codes on other failures.
113+
* @param key_size Size of key in bytes to generate. Must be 16 bytes or 32 bytes. Default is 16 bytes.
114+
*
115+
* @return DEVICEKEY_SUCCESS, when device key successfully generated and injected.
116+
* @return DEVICEKEY_ALREADY_EXIST, if the key has already been written.
117+
* @return DEVICEKEY_GENERATE_RANDOM_ERROR if this device does not contain entropy sources and cannot generate a key.
118+
* @return DEVICEKEY_INVALID_KEY_SIZE if key_size is not 32 or 16 bytes.
119+
* @return error codes on other failures.
117120
*/
118-
int generate_root_of_trust();
121+
int generate_root_of_trust(size_t key_size = DEVICE_KEY_16BYTE);
119122

120123
private:
121124
// Private constructor, as class is a singleton

0 commit comments

Comments
 (0)