aboutsummaryrefslogtreecommitdiff
path: root/mkm.c
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2019-02-01 00:20:52 -0500
committerPaul Selkirk <paul@psgd.org>2019-02-01 00:20:52 -0500
commitd8c550b79efc0db96b52c90f652462371172d52c (patch)
tree876ba1798c1657da3177f72a2cb7d593f641fa3e /mkm.c
parent8fc29991dcf60e1bd804df21e806cf5579d2d950 (diff)
Clean up mkm.c
Diffstat (limited to 'mkm.c')
-rw-r--r--mkm.c59
1 files changed, 26 insertions, 33 deletions
diff --git a/mkm.c b/mkm.c
index 527ccbe..46abe69 100644
--- a/mkm.c
+++ b/mkm.c
@@ -3,7 +3,7 @@
* -----
* Master Key Memory functions.
*
- * Copyright (c) 2016, NORDUnet A/S All rights reserved.
+ * Copyright (c) 2016-2019, NORDUnet A/S All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -49,21 +49,12 @@
* Master Key.
*/
-#define HAL_OK CMIS_HAL_OK
-#include "stm-init.h"
-#include "stm-keystore.h"
-#undef HAL_OK
-
-#define HAL_OK LIBHAL_OK
#include "hal.h"
#include "hal_internal.h"
-#undef HAL_OK
#include <string.h>
-static int volatile_init = 0;
-
#define MKM_VOLATILE_STATUS_ADDRESS 0
#define MKM_VOLATILE_SCLK_DIV 0x20
#define MKM_FLASH_STATUS_ADDRESS (KEYSTORE_SECTOR_SIZE * (KEYSTORE_NUM_SECTORS - 1))
@@ -80,19 +71,21 @@ static int volatile_init = 0;
static hal_error_t hal_mkm_volatile_init(void)
{
+ static int volatile_init = 0;
+
if (volatile_init)
- return LIBHAL_OK;
+ return HAL_OK;
hal_error_t err;
uint32_t status;
hal_core_t *core = NULL;
- if ((err = hal_core_alloc(MKMIF_NAME, &core, NULL)) != LIBHAL_OK)
+ if ((err = hal_core_alloc(MKMIF_NAME, &core, NULL)) != HAL_OK)
return err;
- if ((err = hal_mkmif_set_clockspeed(core, MKM_VOLATILE_SCLK_DIV)) != LIBHAL_OK ||
- (err = hal_mkmif_init(core)) != LIBHAL_OK ||
- (err = hal_mkmif_read_word(core, MKM_VOLATILE_STATUS_ADDRESS, &status)) != LIBHAL_OK)
+ if ((err = hal_mkmif_set_clockspeed(core, MKM_VOLATILE_SCLK_DIV)) != HAL_OK ||
+ (err = hal_mkmif_init(core)) != HAL_OK ||
+ (err = hal_mkmif_read_word(core, MKM_VOLATILE_STATUS_ADDRESS, &status)) != HAL_OK)
goto out;
if (status != MKM_STATUS_SET && status != MKM_STATUS_NOT_SET) {
@@ -101,8 +94,8 @@ static hal_error_t hal_mkm_volatile_init(void)
* while if we write the full buf too it is consistently right afterwards.
*/
uint8_t buf[KEK_LENGTH] = {0};
- if ((err = hal_mkmif_write(core, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, sizeof(buf))) != LIBHAL_OK ||
- (err = hal_mkmif_write_word(core, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_NOT_SET)) != LIBHAL_OK)
+ if ((err = hal_mkmif_write(core, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, sizeof(buf))) != HAL_OK ||
+ (err = hal_mkmif_write_word(core, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_NOT_SET)) != HAL_OK)
goto out;
}
@@ -121,8 +114,8 @@ hal_error_t hal_mkm_volatile_read(uint8_t *buf, const size_t len)
if (len && len != KEK_LENGTH)
return HAL_ERROR_MASTERKEY_BAD_LENGTH;
- if ((err = hal_mkm_volatile_init()) != LIBHAL_OK ||
- (err = hal_mkmif_read_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, &status)) != LIBHAL_OK)
+ if ((err = hal_mkm_volatile_init()) != HAL_OK ||
+ (err = hal_mkmif_read_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, &status)) != HAL_OK)
return err;
if (buf != NULL && len) {
@@ -132,12 +125,12 @@ 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)) != LIBHAL_OK)
+ else if ((err = hal_mkmif_read(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, len)) != HAL_OK)
return err;
}
if (status == MKM_STATUS_SET)
- return LIBHAL_OK;
+ return HAL_OK;
if (status == MKM_STATUS_NOT_SET)
return HAL_ERROR_MASTERKEY_NOT_SET;
@@ -155,12 +148,12 @@ hal_error_t hal_mkm_volatile_write(const uint8_t * const buf, const size_t len)
if (buf == NULL)
return HAL_ERROR_MASTERKEY_FAIL;
- if ((err = hal_mkm_volatile_init()) != LIBHAL_OK ||
- (err = hal_mkmif_write(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, len)) != LIBHAL_OK ||
- (err = hal_mkmif_write_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_SET)) != LIBHAL_OK)
+ 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)
return err;
- return LIBHAL_OK;
+ return HAL_OK;
}
hal_error_t hal_mkm_volatile_erase(const size_t len)
@@ -171,12 +164,12 @@ hal_error_t hal_mkm_volatile_erase(const size_t len)
if (len != KEK_LENGTH)
return HAL_ERROR_MASTERKEY_BAD_LENGTH;
- if ((err = hal_mkm_volatile_init()) != LIBHAL_OK ||
- (err = hal_mkmif_write(NULL, MKM_VOLATILE_STATUS_ADDRESS + 4, buf, sizeof(buf))) != LIBHAL_OK ||
- (err = hal_mkmif_write_word(NULL, MKM_VOLATILE_STATUS_ADDRESS, MKM_STATUS_NOT_SET)) != LIBHAL_OK)
+ 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)
return err;
- return LIBHAL_OK;
+ return HAL_OK;
}
/*
@@ -197,9 +190,9 @@ hal_error_t hal_mkm_get_kek(uint8_t *kek,
hal_error_t err = hal_mkm_volatile_read(kek, len);
- if (err == LIBHAL_OK) {
+ if (err == HAL_OK) {
*kek_len = len;
- return LIBHAL_OK;
+ return HAL_OK;
}
#if HAL_MKM_FLASH_BACKUP_KLUDGE
@@ -212,9 +205,9 @@ hal_error_t hal_mkm_get_kek(uint8_t *kek,
* hal_mkm_volatile_read() returns HAL_ERROR_CORE_BUSY. Whee!
*/
- if (hal_mkm_flash_read_no_lock(kek, len) == LIBHAL_OK) {
+ if (hal_mkm_flash_read_no_lock(kek, len) == HAL_OK) {
*kek_len = len;
- return LIBHAL_OK;
+ return HAL_OK;
}
#endif