mirror of https://github.com/ARMmbed/mbed-os.git
device key: fix coding style
parent
71d35749f9
commit
bfb8b723c3
|
@ -52,7 +52,7 @@ int inject_dummy_rot_key()
|
|||
|
||||
memcpy(key, "1234567812345678", DEVICE_KEY_16BYTE);
|
||||
int size = DEVICE_KEY_16BYTE;
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
return devkey.device_inject_root_of_trust(key, size);
|
||||
#else
|
||||
return DEVICEKEY_SUCCESS;
|
||||
|
@ -86,8 +86,8 @@ void generate_derived_key_consistency_16_byte_key_long_consistency_test(char *ke
|
|||
unsigned char salt[] = "Once upon a time, I worked for the circus and I lived in Omaha.";
|
||||
int key_type = DEVICE_KEY_16BYTE;
|
||||
uint16_t actual_size = 0;
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
size_t salt_size = sizeof(salt);
|
||||
|
||||
if (strcmp(key, MSG_KEY_DEVICE_TEST_STEP1) == 0) {
|
||||
|
@ -145,8 +145,8 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
|
|||
unsigned char salt[] = "The quick brown fox jumps over the lazy dog";
|
||||
int key_type = DEVICE_KEY_32BYTE;
|
||||
uint16_t actual_size = 0;
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
size_t salt_size = sizeof(salt);
|
||||
|
||||
if (strcmp(key, MSG_KEY_DEVICE_TEST_STEP3) == 0) {
|
||||
|
@ -198,7 +198,7 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
|
|||
*/
|
||||
void device_inject_root_of_trust_wrong_size_test()
|
||||
{
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
uint32_t key[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
|
||||
|
||||
memcpy(key, "12345678123456788765432187654321", DEVICE_KEY_32BYTE);
|
||||
|
@ -217,11 +217,11 @@ void device_inject_root_of_trust_wrong_size_test()
|
|||
*/
|
||||
void device_inject_root_of_trust_16_byte_size_test()
|
||||
{
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
uint32_t rkey[DEVICE_KEY_16BYTE / sizeof(uint32_t)];
|
||||
uint16_t actual_size;
|
||||
uint32_t key[DEVICE_KEY_16BYTE / sizeof(uint32_t)];
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -243,11 +243,11 @@ void device_inject_root_of_trust_16_byte_size_test()
|
|||
*/
|
||||
void device_inject_root_of_trust_32_byte_size_test()
|
||||
{
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
uint32_t rkey[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
|
||||
uint16_t actual_size;
|
||||
uint32_t key[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -269,9 +269,9 @@ void device_inject_root_of_trust_32_byte_size_test()
|
|||
*/
|
||||
void device_inject_root_of_trust_several_times_test()
|
||||
{
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
uint32_t key[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -299,8 +299,8 @@ void generate_derived_key_consistency_16_byte_key_test()
|
|||
unsigned char empty_buffer[DEVICE_KEY_16BYTE];
|
||||
unsigned char salt[] = "Once upon a time, I worked for the circus and I lived in Omaha.";
|
||||
int key_type = DEVICE_KEY_16BYTE;
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -334,8 +334,8 @@ void generate_derived_key_consistency_32_byte_key_test()
|
|||
unsigned char empty_buffer[DEVICE_KEY_32BYTE];
|
||||
unsigned char salt[] = "The quick brown fox jumps over the lazy dog";
|
||||
int key_type = DEVICE_KEY_32BYTE;
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -369,8 +369,8 @@ void generate_derived_key_key_type_16_test()
|
|||
unsigned char expectedString[] = "Some String";
|
||||
int key_type = DEVICE_KEY_16BYTE;
|
||||
size_t salt_size = sizeof(salt);
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -400,8 +400,8 @@ void generate_derived_key_key_type_32_test()
|
|||
int key_type = DEVICE_KEY_32BYTE;
|
||||
size_t salt_size = sizeof(salt);
|
||||
unsigned char expectedString[] = "Some String";
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
int ret = nvstore.reset();
|
||||
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
|
||||
|
@ -429,8 +429,8 @@ void generate_derived_key_wrong_key_type_test()
|
|||
unsigned char output[DEVICE_KEY_16BYTE];
|
||||
unsigned char salt[] = "The quick brown fox jumps over the lazy dog";
|
||||
size_t salt_size = sizeof(salt);
|
||||
DeviceKey& devkey = DeviceKey::get_instance();
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
DeviceKey &devkey = DeviceKey::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
|
||||
nvstore.init();
|
||||
int ret = nvstore.reset();
|
||||
|
|
|
@ -117,7 +117,7 @@ int DeviceKey::write_key_to_nvstore(uint32_t *input, size_t isize)
|
|||
return ret;
|
||||
}
|
||||
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
ret = nvstore.set(NVSTORE_DEVICEKEY_KEY, (uint16_t)isize, input);
|
||||
if (NVSTORE_WRITE_ERROR == ret || NVSTORE_BUFF_TOO_SMALL == ret) {
|
||||
return DEVICEKEY_SAVE_FAILED;
|
||||
|
@ -130,15 +130,15 @@ int DeviceKey::write_key_to_nvstore(uint32_t *input, size_t isize)
|
|||
return DEVICEKEY_SUCCESS;
|
||||
}
|
||||
|
||||
int DeviceKey::read_key_from_nvstore(uint32_t *output, size_t& size)
|
||||
int DeviceKey::read_key_from_nvstore(uint32_t *output, size_t &size)
|
||||
{
|
||||
if (size > (uint16_t)-1) {
|
||||
if (size > (uint16_t) -1) {
|
||||
return DEVICEKEY_INVALID_PARAM;
|
||||
}
|
||||
|
||||
uint16_t in_size = size;
|
||||
uint16_t out_size = 0;
|
||||
NVStore& nvstore = NVStore::get_instance();
|
||||
NVStore &nvstore = NVStore::get_instance();
|
||||
int nvStatus = nvstore.get(NVSTORE_DEVICEKEY_KEY, in_size, output, out_size);
|
||||
if (NVSTORE_NOT_FOUND == nvStatus) {
|
||||
return DEVICEKEY_NOT_FOUND;
|
||||
|
@ -179,18 +179,18 @@ int DeviceKey::get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsi
|
|||
|
||||
do {
|
||||
|
||||
mbedtls_cipher_init(&ctx);
|
||||
ret = mbedtls_cipher_setup(&ctx, cipher_info);
|
||||
if (ret != 0) {
|
||||
goto finish;
|
||||
}
|
||||
mbedtls_cipher_init(&ctx);
|
||||
ret = mbedtls_cipher_setup(&ctx, cipher_info);
|
||||
if (ret != 0) {
|
||||
goto finish;
|
||||
}
|
||||
|
||||
ret = mbedtls_cipher_cmac_starts(&ctx, (unsigned char *)ikey_buff, ikey_size * 8);
|
||||
if (ret != 0) {
|
||||
goto finish;
|
||||
}
|
||||
|
||||
DEVKEY_WRITE_UINT8_LE(counter_enc, (counter+1));
|
||||
DEVKEY_WRITE_UINT8_LE(counter_enc, (counter + 1));
|
||||
|
||||
ret = mbedtls_cipher_cmac_update(&ctx, (unsigned char *)counter_enc, sizeof(counter_enc));
|
||||
if (ret != 0) {
|
||||
|
@ -217,7 +217,7 @@ int DeviceKey::get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsi
|
|||
goto finish;
|
||||
}
|
||||
|
||||
mbedtls_cipher_free( &ctx );
|
||||
mbedtls_cipher_free(&ctx);
|
||||
|
||||
counter++;
|
||||
|
||||
|
@ -225,7 +225,7 @@ int DeviceKey::get_derived_key(uint32_t *ikey_buff, size_t ikey_size, const unsi
|
|||
|
||||
finish:
|
||||
if (DEVICEKEY_SUCCESS != ret) {
|
||||
mbedtls_cipher_free( &ctx );
|
||||
mbedtls_cipher_free(&ctx);
|
||||
return DEVICEKEY_ERR_CMAC_GENERIC_FAILURE;
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ public:
|
|||
*
|
||||
* @returns Singleton instance reference.
|
||||
*/
|
||||
static DeviceKey& get_instance()
|
||||
static DeviceKey &get_instance()
|
||||
{
|
||||
// Use this implementation of singleton (Meyer's) rather than the one that allocates
|
||||
// the instance on the heap, as it ensures destruction at program end (preventing warnings
|
||||
|
@ -100,7 +100,7 @@ private:
|
|||
* Output: The actual size of the written data
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
int read_key_from_nvstore(uint32_t *output, size_t& size);
|
||||
int read_key_from_nvstore(uint32_t *output, size_t &size);
|
||||
|
||||
/** Set a device key into the NVStore
|
||||
* @param input Input buffer contain the key.
|
||||
|
@ -109,7 +109,7 @@ private:
|
|||
*/
|
||||
int write_key_to_nvstore(uint32_t *input, size_t isize);
|
||||
|
||||
/** Get a derived key base on a salt string. The methods implements Section 5.1
|
||||
/** Get a derived key base on a salt string. The methods implements Section 5.1
|
||||
* in NIST SP 800-108, Recommendation for Key Derivation Using Pseudorandom Functions
|
||||
* @param ikey_buff Input buffer holding the ROT key
|
||||
* @param ikey_size Size of the input key. Must be 16 bytes or 32 bytes.
|
||||
|
|
Loading…
Reference in New Issue