Skip to content

Commit 1938190

Browse files
DeviceKey Root of Trust generation refactored.
It's no longer automatically and silently created.
1 parent 8dc15ee commit 1938190

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

features/device_key/source/DeviceKey.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,10 @@ int DeviceKey::generate_derived_key(const unsigned char *salt, size_t isalt_size
9494

9595
//First try to read the key from KVStore
9696
int ret = read_key_from_kvstore(key_buff, actual_size);
97-
if (DEVICEKEY_SUCCESS != ret && DEVICEKEY_NOT_FOUND != ret) {
97+
if (DEVICEKEY_SUCCESS != ret) {
9898
return ret;
9999
}
100100

101-
//If the key was not found in KVStore we will create it by using random generation and then save it to KVStore
102-
if (DEVICEKEY_NOT_FOUND == ret) {
103-
ret = generate_key_by_random(key_buff, actual_size);
104-
if (DEVICEKEY_SUCCESS != ret) {
105-
return ret;
106-
}
107-
108-
ret = device_inject_root_of_trust(key_buff, actual_size);
109-
if (DEVICEKEY_SUCCESS != ret) {
110-
return ret;
111-
}
112-
}
113-
114101
ret = get_derived_key(key_buff, actual_size, salt, isalt_size, output, ikey_type);
115102
return ret;
116103
}
@@ -259,22 +246,22 @@ int DeviceKey::get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsi
259246
return DEVICEKEY_SUCCESS;
260247
}
261248

262-
int DeviceKey::generate_key_by_random(uint32_t *output, size_t size)
249+
int DeviceKey::generate_root_of_trust()
263250
{
264251
int ret = DEVICEKEY_GENERATE_RANDOM_ERROR;
252+
uint32_t key_buff[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
253+
size_t actual_size = DEVICE_KEY_32BYTE;
265254

266-
if (DEVICE_KEY_16BYTE > size) {
267-
return DEVICEKEY_BUFFER_TOO_SMALL;
268-
} else if (DEVICE_KEY_16BYTE != size && DEVICE_KEY_32BYTE != size) {
269-
return DEVICEKEY_INVALID_PARAM;
255+
if (read_key_from_kvstore(key_buff, actual_size) == DEVICEKEY_SUCCESS) {
256+
return DEVICEKEY_ALREADY_EXIST;
270257
}
271258

272259
#if defined(DEVICE_TRNG) || defined(MBEDTLS_ENTROPY_NV_SEED) || defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
273260
mbedtls_entropy_context *entropy = new mbedtls_entropy_context;
274261
mbedtls_entropy_init(entropy);
275-
memset(output, 0, size);
262+
memset(key_buff, 0, actual_size);
276263

277-
ret = mbedtls_entropy_func(entropy, (unsigned char *)output, size);
264+
ret = mbedtls_entropy_func(entropy, (unsigned char *)key_buff, actual_size);
278265
if (ret != MBED_SUCCESS) {
279266
ret = DEVICEKEY_GENERATE_RANDOM_ERROR;
280267
} else {
@@ -283,7 +270,7 @@ int DeviceKey::generate_key_by_random(uint32_t *output, size_t size)
283270

284271
mbedtls_entropy_free(entropy);
285272
delete entropy;
286-
273+
ret = device_inject_root_of_trust(key_buff, actual_size);
287274
#endif
288275

289276
return ret;

features/device_key/source/DeviceKey.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,16 @@ class DeviceKey : private mbed::NonCopyable<DeviceKey> {
139139
int get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsigned char *isalt, size_t isalt_size,
140140
unsigned char *output, uint32_t ikey_type);
141141

142-
/** Generate a random ROT key by using entropy
143-
* @param output Output buffer for the generated key.
144-
* @param size Input: The size of the buffer. If size is less
145-
* than 16 bytes, the method generates an
146-
* error. 16-31 bytes creates a 16-byte key.
147-
* 32 or higher generates a 32-byte key
148-
* Output: The actual written size to the buffer
149-
* @return 0 on success, negative error code on failure
142+
/** Generate Root of Trust from random sources.
143+
* Uses TRNG or various other entropy sources to generate random device key and
144+
* inject that into device's KVStore. Device Key can only be generated once.
145+
*
146+
* \return DEVICEKEY_SUCCESS, when device key succesfully generated and injected.
147+
* \return DEVICEKEY_ALREADY_EXIST, if the key has already been written.
148+
* \return DEVICEKEY_GENERATE_RANDOM_ERROR if this device does not contain entropy sources and cannot generate a key.
149+
* \return error codes on other failures.
150150
*/
151-
int generate_key_by_random(uint32_t *output, size_t size);
151+
int generate_root_of_trust();
152152

153153
};
154154

0 commit comments

Comments
 (0)