aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2020-03-10 19:32:23 -0400
committerPaul Selkirk <paul@psgd.org>2020-03-10 19:32:23 -0400
commitfd8ca81821b6a146e6e628860436f86020eac998 (patch)
tree7bad402336f9ea6ee27ed57db524ba6335cc8d70
parentccf20b321cf88d2f4af53c0789c9b18cfd2386ea (diff)
alloc mkmif core around both reads (status + KEK)
-rw-r--r--mkm.c37
1 files changed, 28 insertions, 9 deletions
diff --git a/mkm.c b/mkm.c
index 46abe69..4119744 100644
--- a/mkm.c
+++ b/mkm.c
@@ -4,6 +4,8 @@
* Master Key Memory functions.
*
* Copyright (c) 2016-2019, NORDUnet A/S All rights reserved.
+ * Copyright: 2020, The Commons Conservancy Cryptech Project
+ * SPDX-License-Identifier: BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -110,14 +112,18 @@ hal_error_t hal_mkm_volatile_read(uint8_t *buf, const size_t len)
{
hal_error_t err;
uint32_t status;
+ hal_core_t *core = NULL;
if (len && len != KEK_LENGTH)
return HAL_ERROR_MASTERKEY_BAD_LENGTH;
if ((err = hal_mkm_volatile_init()) != HAL_OK ||
- (err = hal_mkmif_read_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, &status)) != HAL_OK)
+ (err = hal_core_alloc(MKMIF_NAME, &core, NULL)) != HAL_OK)
return err;
+ if ((err = hal_mkmif_read_word(core, MKM_VOLATILE_STATUS_ADDRESS, &status)) != HAL_OK)
+ goto out;
+
if (buf != NULL && len) {
/*
* Don't return the random bytes in the RAM memory in case it isn't initialized.
@@ -125,10 +131,15 @@ hal_error_t hal_mkm_volatile_read(uint8_t *buf, const size_t len)
*/
if (status != MKM_STATUS_SET)
memset(buf, 0x0, len);
- else if ((err = hal_mkmif_read(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, len)) != HAL_OK)
- return err;
+ else
+ err = hal_mkmif_read(core, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, len);
}
+out:
+ hal_core_free(core);
+ if (err != HAL_OK)
+ return err;
+
if (status == MKM_STATUS_SET)
return HAL_OK;
@@ -141,6 +152,7 @@ hal_error_t hal_mkm_volatile_read(uint8_t *buf, const size_t len)
hal_error_t hal_mkm_volatile_write(const uint8_t * const buf, const size_t len)
{
hal_error_t err;
+ hal_core_t *core = NULL;
if (len != KEK_LENGTH)
return HAL_ERROR_MASTERKEY_BAD_LENGTH;
@@ -149,27 +161,34 @@ hal_error_t hal_mkm_volatile_write(const uint8_t * const buf, const size_t len)
return HAL_ERROR_MASTERKEY_FAIL;
if ((err = hal_mkm_volatile_init()) != HAL_OK ||
- (err = hal_mkmif_write(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, len)) != HAL_OK ||
- (err = hal_mkmif_write_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_SET)) != HAL_OK)
+ (err = hal_core_alloc(MKMIF_NAME, &core, NULL)) != HAL_OK)
return err;
- return HAL_OK;
+ if ((err = hal_mkmif_write(core, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, len)) == HAL_OK)
+ err = hal_mkmif_write_word(core, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_SET);
+
+ hal_core_free(core);
+ return err;
}
hal_error_t hal_mkm_volatile_erase(const size_t len)
{
uint8_t buf[KEK_LENGTH] = {0};
hal_error_t err;
+ hal_core_t *core = NULL;
if (len != KEK_LENGTH)
return HAL_ERROR_MASTERKEY_BAD_LENGTH;
if ((err = hal_mkm_volatile_init()) != HAL_OK ||
- (err = hal_mkmif_write(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, sizeof(buf))) != HAL_OK ||
- (err = hal_mkmif_write_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_NOT_SET)) != HAL_OK)
+ (err = hal_core_alloc(MKMIF_NAME, &core, NULL)) != HAL_OK)
return err;
- return HAL_OK;
+ if ((err = hal_mkmif_write(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, sizeof(buf))) == HAL_OK)
+ err = hal_mkmif_write_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_NOT_SET);
+
+ hal_core_free(core);
+ return err;
}
/*