AuditLog: Align the implementation to the PSA header
This change modifies the AuditLog service implementation
to reflect the changes introduced by the PSA compliant API
interface. The symbol name patterns for functions, variables,
folders and source files are also changed to be audit_* /
tfm_audit*.
Change-Id: I674060979961f568912870f49a72ed571499f059
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
diff --git a/secure_fw/ns_callable/CMakeLists.inc b/secure_fw/ns_callable/CMakeLists.inc
index 214bc37..70a071f 100644
--- a/secure_fw/ns_callable/CMakeLists.inc
+++ b/secure_fw/ns_callable/CMakeLists.inc
@@ -24,7 +24,7 @@
endif()
set (SS_NS_CALLABLE_C_SRC "${CMAKE_CURRENT_LIST_DIR}/tfm_sst_veneers.c"
- "${CMAKE_CURRENT_LIST_DIR}/tfm_log_veneers.c")
+ "${CMAKE_CURRENT_LIST_DIR}/tfm_audit_veneers.c")
#Append all our source files to global lists.
list(APPEND ALL_SRC_C ${SS_NS_CALLABLE_C_SRC})
diff --git a/secure_fw/ns_callable/tfm_audit_veneers.c b/secure_fw/ns_callable/tfm_audit_veneers.c
new file mode 100644
index 0000000..f09dd9c
--- /dev/null
+++ b/secure_fw/ns_callable/tfm_audit_veneers.c
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "tfm_audit_veneers.h"
+#include "secure_fw/services/audit_logging/audit_core.h"
+#include "tfm_secure_api.h"
+#include "tfm_api.h"
+#include "spm_partition_defs.h"
+#include "audit_wrappers.h"
+
+__tfm_secure_gateway_attributes__
+enum psa_audit_err tfm_audit_veneer_retrieve_record(
+ const struct audit_core_retrieve_input *input_s,
+ struct audit_core_retrieve_output *output_s)
+{
+ TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID,
+ audit_core_retrieve_record_wrapper,
+ input_s, output_s, 0, 0);
+}
+
+__tfm_secure_gateway_attributes__
+enum psa_audit_err tfm_audit_veneer_add_record(
+ const struct psa_audit_record *record)
+{
+ TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_add_record,
+ record, 0, 0, 0);
+}
+
+__tfm_secure_gateway_attributes__
+enum psa_audit_err tfm_audit_veneer_get_info(uint32_t *num_records,
+ uint32_t *size)
+{
+ TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_get_info,
+ num_records, size, 0, 0);
+}
+
+__tfm_secure_gateway_attributes__
+enum psa_audit_err tfm_audit_veneer_get_record_info(const uint32_t record_index,
+ uint32_t *size)
+{
+ TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_get_record_info,
+ record_index, size, 0, 0);
+}
+
+__tfm_secure_gateway_attributes__
+enum psa_audit_err tfm_audit_veneer_delete_record(const uint32_t record_index,
+ const uint8_t *token,
+ const uint32_t token_size)
+{
+ TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_delete_record,
+ record_index, token, token_size, 0);
+}
diff --git a/secure_fw/ns_callable/tfm_log_veneers.c b/secure_fw/ns_callable/tfm_log_veneers.c
deleted file mode 100644
index 358c175..0000000
--- a/secure_fw/ns_callable/tfm_log_veneers.c
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#include "tfm_log_veneers.h"
-#include "secure_fw/services/audit_logging/log_core.h"
-#include "tfm_secure_api.h"
-#include "tfm_api.h"
-#include "spm_partition_defs.h"
-
-__tfm_secure_gateway_attributes__
-enum tfm_log_err tfm_log_veneer_retrieve(uint32_t size,
- int32_t start,
- uint8_t *buffer,
- struct tfm_log_info *info)
-{
- TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, log_core_retrieve,
- size, start, buffer, info);
-}
-
-__tfm_secure_gateway_attributes__
-enum tfm_log_err tfm_log_veneer_add_line(struct tfm_log_line *line)
-{
- TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, log_core_add_line,
- line, 0, 0, 0);
-}
-
-__tfm_secure_gateway_attributes__
-enum tfm_log_err tfm_log_veneer_get_info(struct tfm_log_info *info)
-{
- TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, log_core_get_info,
- info, 0, 0, 0);
-}
-
-__tfm_secure_gateway_attributes__
-enum tfm_log_err tfm_log_veneer_delete_items(uint32_t num_items,
- uint32_t *rem_items)
-{
- TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, log_core_delete_items,
- num_items, rem_items, 0, 0);
-}
diff --git a/secure_fw/services/audit_logging/CMakeLists.inc b/secure_fw/services/audit_logging/CMakeLists.inc
index ed91cd9..691d5a3 100644
--- a/secure_fw/services/audit_logging/CMakeLists.inc
+++ b/secure_fw/services/audit_logging/CMakeLists.inc
@@ -35,7 +35,8 @@
message(FATAL_ERROR "Please set TFM_ROOT_DIR before including this file.")
endif()
- set (AUDIT_LOGGING_C_SRC "${AUDIT_LOGGING_DIR}/log_core.c")
+ set (AUDIT_LOGGING_C_SRC "${AUDIT_LOGGING_DIR}/audit_core.c"
+ "${AUDIT_LOGGING_DIR}/audit_wrappers.c")
#Append all our source files to global lists.
list(APPEND ALL_SRC_C ${AUDIT_LOGGING_C_SRC})
diff --git a/secure_fw/services/audit_logging/log_core.c b/secure_fw/services/audit_logging/audit_core.c
similarity index 61%
rename from secure_fw/services/audit_logging/log_core.c
rename to secure_fw/services/audit_logging/audit_core.c
index 56ec87a..30ee564 100644
--- a/secure_fw/services/audit_logging/log_core.c
+++ b/secure_fw/services/audit_logging/audit_core.c
@@ -8,21 +8,21 @@
#include <stdint.h>
#include <string.h>
#include <stddef.h>
-#include "log_core.h"
-#include "tfm_log_defs.h"
+#include "audit_core.h"
+#include "psa_audit_defs.h"
#include "tfm_secure_api.h"
/*!
- * \def LOG_UART_REDIRECTION
+ * \def AUDIT_UART_REDIRECTION
*
* \brief If set to 1 by the build system, UART redirection is enabled. Keep it
* disabled by default.
*/
-#ifndef LOG_UART_REDIRECTION
-#define LOG_UART_REDIRECTION (0U)
+#ifndef AUDIT_UART_REDIRECTION
+#define AUDIT_UART_REDIRECTION (0U)
#endif
-#if (LOG_UART_REDIRECTION == 1U)
+#if (AUDIT_UART_REDIRECTION == 1U)
/* CMSIS Driver for UART */
#include "Driver_USART.h"
@@ -123,7 +123,7 @@
in chronological order */
uint32_t last_el_idx; /*!< Index in the log of the last element
in chronological order */
- uint32_t num_items; /*!< Indicates the number of items
+ uint32_t num_records; /*!< Indicates the number of records
currently stored in the log. It has to be
zero after a reset, i.e. log is empty */
uint32_t stored_size; /*!< Indicates the total size of the items
@@ -216,27 +216,27 @@
/*!
* \brief Static function to update the state variables of the log after the
- * addition of a new log line of a given size
+ * addition of a new log record of a given size
*
* \param[in] first_el_idx First element index
* \param[in] last_el_idx Last element index
* \param[in] stored_size New value of the stored size
- * \param[in] num_items Number of elements stored
+ * \param[in] num_records Number of elements stored
*
*/
-static void log_update_state(const uint32_t first_el_idx,
- const uint32_t last_el_idx,
- const uint32_t stored_size,
- const uint32_t num_items)
+static void audit_update_state(const uint32_t first_el_idx,
+ const uint32_t last_el_idx,
+ const uint32_t stored_size,
+ const uint32_t num_records)
{
/* Update the indexes */
log_state.first_el_idx = first_el_idx;
log_state.last_el_idx = last_el_idx;
- /* Update the items*/
- log_state.num_items = num_items;
+ /* Update the number of records stored */
+ log_state.num_records = num_records;
- /* Update the size of the stored items */
+ /* Update the size of the stored records */
log_state.stored_size = stored_size;
}
@@ -245,14 +245,14 @@
* into the log. It will replace items based on "older entries first"
* policy in case not enough space is available in the log
*
- * \param[in] size Size of the line we need to fit
+ * \param[in] size Size of the record we need to fit
* \param[out] begin Pointer to the index to begin
* \param[out] end Pointer to the index to end
*
*/
-static void log_replace_item(const uint32_t size,
- uint32_t *begin,
- uint32_t *end)
+static void audit_replace_record(const uint32_t size,
+ uint32_t *begin,
+ uint32_t *end)
{
uint32_t first_el_idx = 0, last_el_idx = 0;
uint32_t num_items = 0, stored_size = 0;
@@ -261,7 +261,7 @@
/* Retrieve the current state variables of the log */
first_el_idx = log_state.first_el_idx;
last_el_idx = log_state.last_el_idx;
- num_items = log_state.num_items;
+ num_items = log_state.num_records;
stored_size = log_state.stored_size;
/* If there is not enough size, remove older entries */
@@ -296,27 +296,27 @@
*end = stop_pos;
/* Update the state with the new values of variables */
- log_update_state(first_el_idx, last_el_idx, stored_size, num_items);
+ audit_update_state(first_el_idx, last_el_idx, stored_size, num_items);
}
/*!
* \brief Static function to perform memory copying into the log buffer. It
* takes into account circular wrapping on the log buffer size.
*
- * \param[in] dest Pointer to the destination buffer
- * \param[in] src Pointer to the source buffer
- * \param[in] size Size in bytes to be copied
+ * \param[in] src Pointer to the source buffer
+ * \param[in] size Size in bytes to be copied
+ * \param[out] dest Pointer to the destination buffer
*
*/
-static enum tfm_log_err log_buffer_copy(uint8_t *dest,
- const uint8_t *src,
- const uint32_t size)
+static enum psa_audit_err audit_buffer_copy(const uint8_t *src,
+ const uint32_t size,
+ uint8_t *dest)
{
uint32_t idx = 0;
uint32_t dest_idx = (uint32_t)dest - (uint32_t)&log_buffer[0];
if ((dest_idx >= LOG_SIZE) || (size > LOG_SIZE)) {
- return TFM_LOG_ERR_FAILURE;
+ return PSA_AUDIT_ERR_FAILURE;
}
/* TODO: This can be an optimized copy using uint32_t
@@ -328,20 +328,20 @@
log_buffer[(dest_idx + idx) % LOG_SIZE] = src[idx];
}
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
/*!
* \brief Static function to emulate memcpy
*
- * \param[out] dest Pointer to the destination buffer
* \param[in] src Pointer to the source buffer
* \param[in] size Size in bytes to be copied
+ * \param[out] dest Pointer to the destination buffer
*
*/
-static enum tfm_log_err log_memcpy(uint8_t *dest,
- const uint8_t *src,
- const uint32_t size)
+static enum psa_audit_err audit_memcpy(const uint8_t *src,
+ const uint32_t size,
+ uint8_t *dest)
{
uint32_t idx = 0;
@@ -349,25 +349,26 @@
dest[idx] = src[idx];
}
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
/*!
* \brief Static function to format a log entry before the addition to the log
*
+ * \param[in] record Pointer to the record to be added
* \param[out] buffer Pointer to the buffer to format
- * \param[in] line Pointer to the line to be added
*
*/
-static enum tfm_log_err log_format_buffer(uint64_t *buffer,
- const struct tfm_log_line *line)
+static enum psa_audit_err audit_format_buffer(
+ const struct psa_audit_record *record,
+ uint64_t *buffer)
{
struct log_hdr *hdr = NULL;
struct log_tlr *tlr = NULL;
uint32_t size, idx;
- /* Get the size from the log line */
- size = line->size;
+ /* Get the size from the record */
+ size = record->size;
/* Format the scratch buffer with the complete log item */
hdr = (struct log_hdr *) buffer;
@@ -379,10 +380,10 @@
hdr->iv_counter = 0;
hdr->partition_id = DUMMY_PARTITION_ID;
- /* Copy the log line into the scratch buffer */
- log_memcpy( (uint8_t *) &(hdr->size),
- (const uint8_t *) line,
- size+4 );
+ /* Copy the record into the scratch buffer */
+ audit_memcpy( (const uint8_t *) record,
+ size+4,
+ (uint8_t *) &(hdr->size) );
/* FIXME: The MAC here is just a dummy value for prototyping. It will be
* filled by a call to the crypto interface directly when available.
@@ -392,7 +393,7 @@
tlr->mac[idx] = idx;
}
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
/*!
@@ -405,9 +406,9 @@
* to UART
*
*/
-static void log_uart_redirection(const uint32_t start_idx)
+static void audit_uart_redirection(const uint32_t start_idx)
{
-#if (LOG_UART_REDIRECTION == 1U)
+#if (AUDIT_UART_REDIRECTION == 1U)
uint32_t size = *GET_SIZE_FIELD_POINTER(start_idx);
uint8_t end_of_line[] = {'\r', '\n'};
uint32_t idx = 0;
@@ -431,20 +432,20 @@
*/
/*!@{*/
-enum tfm_log_err log_core_init(void)
+enum psa_audit_err audit_core_init(void)
{
-#if (LOG_UART_REDIRECTION == 1U)
+#if (AUDIT_UART_REDIRECTION == 1U)
int32_t ret = ARM_DRIVER_OK;
ret = LOG_UART_NAME.Initialize(NULL);
if (ret != ARM_DRIVER_OK) {
- return TFM_LOG_ERR_FAILURE;
+ return PSA_AUDIT_ERR_FAILURE;
}
ret = LOG_UART_NAME.Control(ARM_USART_MODE_ASYNCHRONOUS,
LOG_UART_BAUD_RATE);
if (ret != ARM_DRIVER_OK) {
- return TFM_LOG_ERR_FAILURE;
+ return PSA_AUDIT_ERR_FAILURE;
}
/* If we get to this point, UART init is successful */
@@ -452,93 +453,130 @@
#endif
/* Clear the log state variables */
- log_update_state(0,0,0,0);
+ audit_update_state(0,0,0,0);
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
-enum tfm_log_err log_core_delete_items(const uint32_t num_items,
- uint32_t *rem_items)
+enum psa_audit_err audit_core_delete_record(const uint32_t record_index,
+ const uint8_t *token,
+ const uint32_t token_size)
{
- uint32_t first_el_idx = 0, idx = 0;
+ uint32_t first_el_idx, size_removed;
- if (rem_items == NULL) {
- return TFM_LOG_ERR_FAILURE;
+ /* FixMe: Currently only the removal of the oldest entry, i.e.
+ * record_index 0, is supported. This has to be extended
+ * to support removal of random records
+ */
+ if (record_index > 0) {
+ return PSA_AUDIT_ERR_NOT_SUPPORTED;
}
- /* This means to delete all items in the log */
- if (num_items >= log_state.num_items) {
+ /* FixMe: Currently token and token_size parameters are not evaluated
+ * to check if the removal of the desired record_index is
+ * authorised
+ */
+ if ((token != NULL) || (token_size != 0)) {
+ return PSA_AUDIT_ERR_NOT_SUPPORTED;
+ }
- /* Update the number of removed items (all of them) */
- *rem_items = log_state.num_items;
+ /* Check that the record index to be removed is contained in the log */
+ if (record_index >= log_state.num_records) {
+ return PSA_AUDIT_ERR_FAILURE;
+ }
+
+ /* If the log contains just one element, reset the state and return */
+ if (log_state.num_records == 1) {
/* Clear the log state variables */
- log_update_state(0,0,0,0);
+ audit_update_state(0,0,0,0);
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
- /* Get the index of the first element */
+ /* Get the index to the element to be removed */
first_el_idx = log_state.first_el_idx;
- /* Removing items means discarding items at the head */
- for (idx = 0; idx < num_items; idx++) {
- first_el_idx = GET_NEXT_LOG_INDEX(first_el_idx);
+ /* Get the size of the element that is being removed */
+ size_removed = COMPUTE_LOG_ENTRY_SIZE(
+ *GET_SIZE_FIELD_POINTER(first_el_idx));
+
+ /* Remove the oldest entry, it means moving the first element to the
+ * next log index */
+ first_el_idx = GET_NEXT_LOG_INDEX(first_el_idx);
+
+ /* Update the state with the new head and decrease the number of records
+ * currently stored and the new size of the stored records */
+ log_state.first_el_idx = first_el_idx;
+ log_state.num_records--;
+ log_state.stored_size -= size_removed;
+
+ return PSA_AUDIT_ERR_SUCCESS;
+}
+
+enum psa_audit_err audit_core_get_info(uint32_t *num_records,
+ uint32_t *size)
+{
+ /* Return the number of records that are currently stored */
+ *num_records = log_state.num_records;
+
+ /* Return the size of the records currently stored */
+ *size = log_state.stored_size;
+
+ return PSA_AUDIT_ERR_SUCCESS;
+}
+
+enum psa_audit_err audit_core_get_record_info(const uint32_t record_index,
+ uint32_t *size)
+{
+ uint32_t start_idx, idx;
+
+ if (record_index >= log_state.num_records) {
+ return PSA_AUDIT_ERR_FAILURE;
}
- /* Update the state with the new head and number of items */
- log_state.first_el_idx = first_el_idx;
- log_state.num_items -= num_items;
+ /* First element to read from the log */
+ start_idx = log_state.first_el_idx;
- /* Update the number of removed items */
- *rem_items = num_items;
+ /* Move the start_idx index to the desired element */
+ for (idx=0; idx<record_index; idx++) {
+ start_idx = GET_NEXT_LOG_INDEX(start_idx);
+ }
- return TFM_LOG_ERR_SUCCESS;
+ /* Get the size of the requested record */
+ *size = COMPUTE_LOG_ENTRY_SIZE(*GET_SIZE_FIELD_POINTER(start_idx));
+
+ return PSA_AUDIT_ERR_SUCCESS;
}
-enum tfm_log_err log_core_get_info(struct tfm_log_info *info)
+enum psa_audit_err audit_core_add_record(const struct psa_audit_record *record)
{
- /* Return the size of the items currently stored */
- info->size = log_state.stored_size;
-
- /* Return the number of items that are currently stored */
- info->num_items = log_state.num_items;
-
- return TFM_LOG_ERR_SUCCESS;
-}
-
-enum tfm_log_err log_core_add_line(const struct tfm_log_line *line)
-{
- struct tfm_log_info info;
-
uint32_t start_pos = 0, stop_pos = 0;
uint32_t first_el_idx = 0, last_el_idx = 0, size = 0;
uint32_t num_items = 0, stored_size = 0;
/* Check that the request comes from the secure world */
if (tfm_core_validate_secure_caller() != TFM_SUCCESS) {
- return TFM_LOG_ERR_FAILURE;
+ return PSA_AUDIT_ERR_FAILURE;
}
- /* Read the size from the input line */
- size = line->size;
+ /* Read the size from the input record */
+ size = record->size;
/* Check that size is a 4-byte multiple as expected */
if (size % 4) {
- return TFM_LOG_ERR_FAILURE;
+ return PSA_AUDIT_ERR_FAILURE;
}
/* Check that the entry to be added is not greater than the
* maximum space available
*/
if (size > (LOG_SIZE - (LOG_FIXED_FIELD_SIZE+LOG_MAC_SIZE))) {
- return TFM_LOG_ERR_FAILURE;
+ return PSA_AUDIT_ERR_FAILURE;
}
/* Get the size in bytes and num of elements present in the log */
- log_core_get_info(&info);
- num_items = info.num_items;
- stored_size = info.size;
+ audit_core_get_info(&num_items, &stored_size);
if (num_items == 0) {
@@ -550,22 +588,24 @@
/* The log is not empty, need to decide the candidate position
* and invalidate older entries in case there is not enough space
*/
- log_replace_item(COMPUTE_LOG_ENTRY_SIZE(size), &start_pos, &stop_pos);
+ audit_replace_record(COMPUTE_LOG_ENTRY_SIZE(size),
+ &start_pos,
+ &stop_pos);
}
/* Format the scratch buffer with the complete log item */
- log_format_buffer(&scratch_buffer[0], line);
+ audit_format_buffer(record, &scratch_buffer[0]);
/* TODO: At this point, encryption should be called if supported */
- /* Do the copy of the log line to be added in the log */
- log_buffer_copy( (uint8_t *) &log_buffer[start_pos],
- (const uint8_t *) &scratch_buffer[0],
- COMPUTE_LOG_ENTRY_SIZE(size) );
+ /* Do the copy of the log item to be added in the log */
+ audit_buffer_copy( (const uint8_t *) &scratch_buffer[0],
+ COMPUTE_LOG_ENTRY_SIZE(size),
+ (uint8_t *) &log_buffer[start_pos] );
/* Retrieve current log state */
first_el_idx = log_state.first_el_idx;
- num_items = log_state.num_items;
+ num_items = log_state.num_records;
stored_size = log_state.stored_size;
/* The last element is the one we just added */
@@ -576,144 +616,68 @@
stored_size += COMPUTE_LOG_ENTRY_SIZE(size);
/* Update the log state */
- log_update_state(first_el_idx, last_el_idx, stored_size, num_items);
+ audit_update_state(first_el_idx, last_el_idx, stored_size, num_items);
/* TODO: At this point, we would need to update the stored copy in
* persistent storage. Need to define a strategy for this
*/
/* Stream to a secure UART if available for the platform and built */
- log_uart_redirection(last_el_idx);
+ audit_uart_redirection(last_el_idx);
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
-enum tfm_log_err log_core_retrieve(const uint32_t size,
- const int32_t start,
- uint8_t *buffer,
- struct tfm_log_info *info)
+enum psa_audit_err audit_core_retrieve_record(const uint32_t record_index,
+ const uint32_t buffer_size,
+ const uint8_t *token,
+ const uint32_t token_size,
+ uint8_t *buffer,
+ uint32_t *record_size)
+
{
- uint32_t stored_size = 0, retrieved_log_size = 0;
- uint32_t index_first_empty_el = 0;
- uint32_t idx, start_idx = 0, num_items = 0;
+ uint32_t idx, start_idx, record_size_tmp;
- struct tfm_log_info info_stored;
+ enum psa_audit_err err;
- /* size must be a non-zero value */
- if (size == 0) {
- info->size = 0;
- info->num_items = 0;
- return TFM_LOG_ERR_FAILURE;
+ /* FixMe: Currently token and token_size parameters are not evaluated
+ * to be used as a challenge for encryption as encryption support
+ * is still not yet available
+ */
+ if ((token != NULL) || (token_size != 0)) {
+ return PSA_AUDIT_ERR_NOT_SUPPORTED;
}
- /* Get the size in bytes and num of elements present in the log */
- log_core_get_info(&info_stored);
- num_items = info_stored.num_items;
- stored_size = info_stored.size;
+ /* Get the size of the record we want to retrieve */
+ err = audit_core_get_record_info(record_index, &record_size_tmp);
- /* Log is empty, but still a valid scenario */
- if (num_items == 0) {
- info->size = 0;
- info->num_items = 0;
- return TFM_LOG_ERR_SUCCESS;
+ /* Propagate the error to the caller in case of failure */
+ if (err != PSA_AUDIT_ERR_SUCCESS) {
+ return err;
}
- /* Compute the size in bytes to be retrieved */
- retrieved_log_size = MIN(size, stored_size);
+ /* buffer_size must be enough to hold the requested record */
+ if (buffer_size < record_size_tmp) {
+ *record_size = 0;
+ return PSA_AUDIT_ERR_FAILURE;
+ }
/* First element to read from the log */
start_idx = log_state.first_el_idx;
- if (start == TFM_LOG_READ_RECENT) {
-
- /* Get the index of the first empty location */
- index_first_empty_el = GET_NEXT_LOG_INDEX(log_state.last_el_idx);
-
- /* If the stored log size is bigger than what we are able to retrieve,
- * just return the latest entries that fit into the available space
- */
- while (retrieved_log_size < stored_size) {
-
- start_idx = GET_NEXT_LOG_INDEX(start_idx);
-
- /* Decrement the number of items that we will return */
- num_items--;
-
- stored_size = (index_first_empty_el >= start_idx) ?
- (index_first_empty_el - start_idx) :
- (LOG_SIZE - start_idx) + index_first_empty_el;
-
- if (stored_size < retrieved_log_size) {
- /* The retrieved log size now will be the new stored log size */
- retrieved_log_size = stored_size;
- }
- }
-
- /* size available is not enough even to retrieve a single log entry */
- if (stored_size == 0) {
- info->size = 0;
- info->num_items = 0;
- return TFM_LOG_ERR_SUCCESS;
- }
-
- } else if (start < num_items) {
-
- /* Move the start_idx index to the desired element */
- for (idx=0; idx<start; idx++) {
- start_idx = GET_NEXT_LOG_INDEX(start_idx);
- }
-
- /* Initialize to the size of the first element to retrieve */
- stored_size = COMPUTE_LOG_ENTRY_SIZE(
- *GET_SIZE_FIELD_POINTER(start_idx) );
-
- /* size available is not enough even to retrieve a single log entry */
- if (stored_size > retrieved_log_size) {
- info->size = 0;
- info->num_items = 0;
- return TFM_LOG_ERR_SUCCESS;
- }
-
- /* Initialize the value of num_items */
- num_items = 1;
-
- /* Compute the total size to retrieve */
- idx = GET_NEXT_LOG_INDEX(start_idx);
- while ((stored_size + COMPUTE_LOG_ENTRY_SIZE(
- *GET_SIZE_FIELD_POINTER(idx) )) <= retrieved_log_size) {
-
- /* Update stored_size */
- stored_size += COMPUTE_LOG_ENTRY_SIZE(
- *GET_SIZE_FIELD_POINTER(idx) );
-
- /* Increment the number of items that we will return */
- num_items++;
-
- /* Move to the next item to check */
- idx = GET_NEXT_LOG_INDEX(idx);
- }
-
- /* The retrieved log size now will be the new stored log size */
- retrieved_log_size = stored_size;
-
- } else {
-
- /* The index value is wrong */
- info->size = 0;
- info->num_items = 0;
- return TFM_LOG_ERR_FAILURE;
+ /* Move the start_idx index to the desired element */
+ for (idx=0; idx<record_index; idx++) {
+ start_idx = GET_NEXT_LOG_INDEX(start_idx);
}
/* Do the copy */
- for (idx=0; idx<retrieved_log_size; idx++) {
+ for (idx=0; idx<record_size_tmp; idx++) {
buffer[idx] = log_buffer[(start_idx + idx) % LOG_SIZE];
}
/* Update the retrieved size */
- info->size = retrieved_log_size;
- /* Update the number of items returned */
- info->num_items = num_items;
+ *record_size = record_size_tmp;
- return TFM_LOG_ERR_SUCCESS;
+ return PSA_AUDIT_ERR_SUCCESS;
}
/*!@}*/
diff --git a/secure_fw/services/audit_logging/audit_core.h b/secure_fw/services/audit_logging/audit_core.h
new file mode 100644
index 0000000..cf5fb38
--- /dev/null
+++ b/secure_fw/services/audit_logging/audit_core.h
@@ -0,0 +1,250 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __AUDIT_CORE_H__
+#define __AUDIT_CORE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <stddef.h>
+#include <limits.h>
+
+#include "psa_audit_defs.h"
+
+/*!
+ * \struct log_entry
+ *
+ * \brief Structure of a single log entry
+ * in the log
+ * \details This can't be represented as a
+ * structure because the payload
+ * is of variable size, i.e.
+ *
+ * +-------------+0
+ * | TIMESTAMP |
+ * | |
+ * +-------------+8
+ * | IV_COUNTER |
+ * | |
+ * +-------------+12
+ * | PARTITION ID|
+ * | |
+ * +-------------+16
+ * | SIZE |
+ * | |
+ * +-------------+20
+ * | RECORD_ID |
+ * | |
+ * +-------------+24
+ * | PAYLOAD |
+ * | |
+ * | |
+ * | |
+ * +-------------+20 + SIZE
+ * | MAC |
+ * | |
+ * | |
+ * +-------------+20 + SIZE + MAC_SIZE
+ *
+ * SIZE: at least LOG_MIN_SIZE bytes, known only at runtime. It's the size of
+ * the (RECORD_ID, PAYLOAD) fields
+ *
+ * MAC_SIZE: known at build time (currently, 4 bytes)
+ *
+ * At runtime, when adding a record, the value of SIZE has to be checked and
+ * must be less than LOG_SIZE - MAC_SIZE - 12 and equal or greater than
+ * LOG_MIN_SIZE
+ *
+ */
+
+/*!
+ * \def LOG_MIN_SIZE
+ *
+ * \brief Minimum size of the encrypted part
+ */
+#define LOG_MIN_SIZE (4)
+
+/*!
+ * \enum audit_tlv_type
+ *
+ * \brief Possible types for a TLV entry
+ * in payload
+ */
+enum audit_tlv_type {
+ TLV_TYPE_ID = 0,
+ TLV_TYPE_AUTH = 1,
+
+ /* This is used to force the maximum size */
+ TLV_TYPE_MAX = INT_MAX
+};
+
+/*!
+ * \struct audit_tlv_entry
+ *
+ * \brief TLV entry structure with a flexible array member
+ */
+struct audit_tlv_entry {
+ enum audit_tlv_type type;
+ uint32_t length;
+ uint8_t value[];
+};
+
+/*!
+ * \def LOG_MAC_SIZE
+ *
+ * \brief Size in bytes of the MAC for each entry
+ */
+#define LOG_MAC_SIZE (4)
+
+/*!
+ * \struct log_hdr
+ *
+ * \brief Fixed size header for a log record
+ */
+struct log_hdr {
+ uint64_t timestamp;
+ uint32_t iv_counter;
+ uint32_t partition_id;
+ uint32_t size;
+ uint32_t id;
+};
+
+/*!
+ * \struct log_tlr
+ *
+ * \brief Fixed size logging entry trailer
+ */
+struct log_tlr {
+ uint8_t mac[LOG_MAC_SIZE];
+};
+
+/*!
+ * \def LOG_HDR_SIZE
+ *
+ * \brief Size in bytes of the (fixed) header for each entry
+ */
+#define LOG_HDR_SIZE (sizeof(struct log_hdr))
+
+/*!
+ * \def LOG_TLR_SIZE
+ *
+ * \brief Size in bytes of the (fixed) trailer for each entry
+ */
+#define LOG_TLR_SIZE (sizeof(struct log_tlr))
+
+/*!
+ * \brief Initializes the Audit logging service
+ * during the TFM boot up process
+ *
+ * \return Returns PSA_AUDIT_ERR_SUCCESS if init has been completed,
+ * otherwise error as specified in \ref psa_audit_err
+ */
+enum psa_audit_err audit_core_init(void);
+
+/*!
+ * \brief Retrieves a record at the specified index
+ *
+ * \details The function retrieves an item specified by index and returns
+ * it on the buffer provided. The token is passed as a challenge
+ * value for the encryption scheme
+ *
+ * \note Currently the cryptography support is not yet enabled, so the
+ * token value is not used and must be passed as NULL, with 0 size
+ *
+ * \param[in] record_index Index of the record to retrieve
+ * \param[in] buffer_size Size in bytes of the provided buffer
+ * \param[in] token Must be set to NULL. Token used as a challenge
+ * for encryption, to protect against rollback
+ * attacks
+ * \param[in] token_size Must be set to 0. Size in bytes of the token
+ * used as challenge
+ * \param[out] buffer Buffer used to store the retrieved record
+ * \param[out] record_size Size in bytes of the retrieved record
+ *
+ * \return Returns values as specified by the \ref psa_audit_err
+ *
+ */
+enum psa_audit_err audit_core_retrieve_record(const uint32_t record_index,
+ const uint32_t buffer_size,
+ const uint8_t *token,
+ const uint32_t token_size,
+ uint8_t *buffer,
+ uint32_t *record_size);
+/*!
+ * \brief Adds a record
+ *
+ * \details This function adds a record in the Audit log
+ *
+ * \param[in] record Pointer to the memory buffer containing the record
+ * to be added
+ *
+ * \return Returns values as specified by the \ref psa_audit_err
+ *
+ */
+enum psa_audit_err audit_core_add_record(const struct psa_audit_record *record);
+
+/*!
+ * \brief Returns the total number and size of the records stored
+ *
+ * \details The function returns the total size in bytes and the
+ * total number of records stored
+ *
+ * \param[out] num_records Total number of records stored
+ * \param[out] size Total size of the records stored, in bytes
+ *
+ * \return Returns values as specified by the \ref psa_audit_err
+ *
+ */
+enum psa_audit_err audit_core_get_info(uint32_t *num_records,
+ uint32_t *size);
+
+/*!
+ * \brief Returns the size of the record at the specified index
+ *
+ * \details The function returns the size of the record at the given index
+ * provided as input
+ *
+ * \param[in] record_index Index of the record to return the size
+ * \param[out] size Size of the specified record, in bytes
+ *
+ * \return Returns values as specified by the \ref psa_audit_err
+ *
+ */
+enum psa_audit_err audit_core_get_record_info(const uint32_t record_index,
+ uint32_t *size);
+/*!
+ * \brief Deletes a record at the specified index
+ *
+ * \details The function removes a record at the specified index. It passes
+ * an authorisation token for removal which is a MAC of the plain text
+ *
+ * \note Currently the cryptography support is not yet enabled, so the
+ * token value is not used and must be passed as NULL, with 0 size
+ *
+ * \param[in] record_index Index of the record to be removed. Currently, only
+ * the removal of the oldest entry, i.e. record_index 0
+ * is supported
+ * \param[in] token Must be set to NULL. Token used as authorisation for
+ * removal of the specified record_index
+ * \param[in] token_size Must be set to 0. Size in bytes of the token used as
+ * authorisation for removal
+ *
+ * \return Returns values as specified by the \ref psa_audit_err
+ *
+ */
+enum psa_audit_err audit_core_delete_record(const uint32_t record_index,
+ const uint8_t *token,
+ const uint32_t token_size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __AUDIT_CORE_H__ */
diff --git a/secure_fw/services/audit_logging/audit_wrappers.c b/secure_fw/services/audit_logging/audit_wrappers.c
new file mode 100644
index 0000000..5640e5e
--- /dev/null
+++ b/secure_fw/services/audit_logging/audit_wrappers.c
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "audit_core.h"
+#include "audit_wrappers.h"
+#include "psa_audit_defs.h"
+
+/*!
+ * \defgroup public Public functions, TF-M compatible wrappers
+ *
+ */
+
+/*!@{*/
+enum psa_audit_err audit_core_retrieve_record_wrapper(
+ const struct audit_core_retrieve_input *input_s,
+ struct audit_core_retrieve_output *output_s)
+{
+ return audit_core_retrieve_record(input_s->record_index,
+ input_s->buffer_size,
+ input_s->token,
+ input_s->token_size,
+ output_s->buffer,
+ output_s->record_size);
+}
+/*!@}*/
diff --git a/secure_fw/services/audit_logging/log_core.h b/secure_fw/services/audit_logging/log_core.h
deleted file mode 100644
index 60a51ca..0000000
--- a/secure_fw/services/audit_logging/log_core.h
+++ /dev/null
@@ -1,232 +0,0 @@
-/*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __LOG_CORE_H__
-#define __LOG_CORE_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stdint.h>
-#include <stddef.h>
-#include <limits.h>
-
-#include "tfm_log_defs.h"
-
-/*!
- * \struct log_entry
- *
- * \brief Structure of a single log entry
- * in the log
- * \details This can't be represented as a
- * structure because the payload
- * is of variable size, i.e.
- *
- * +-------------+0
- * | TIMESTAMP |
- * | |
- * +-------------+8
- * | IV_COUNTER |
- * | |
- * +-------------+12
- * | PARTITION ID|
- * | |
- * +-------------+16
- * | SIZE |
- * | |
- * +-------------+20
- * | FUNCTION ID |
- * | |
- * +-------------+24
- * | ARG0-3 |
- * | |
- * | |
- * +-------------+40
- * | PAYLOAD |
- * | |
- * | |
- * | |
- * +-------------+20 + SIZE
- * | MAC |
- * | |
- * | |
- * +-------------+20 + SIZE + MAC_SIZE
- *
- * SIZE: at least 20 bytes
- * known only at runtime. It's the
- * size of the three fields that
- * follow it.
- *
- * MAC_SIZE: known at build time (currently,
- * 4 bytes)
- *
- * At runtime SIZE has to be checked and
- * must be less than LOG_SIZE - MAC_SIZE - 12
- * and equal or greater than 20
- */
-
-/*!
- * \def LOG_MIN_SIZE
- *
- * \brief Minimum size of the encrypted
- * part
- */
-#define LOG_MIN_SIZE (20)
-
-/*!
- * \enum log_tlv_type
- *
- * \brief Possible types for a TLV entry
- * in payload
- */
-enum log_tlv_type {
- TLV_TYPE_ID = 0,
- TLV_TYPE_AUTH = 1,
-
- /* This is used to force the maximum size */
- TLV_TYPE_MAX = INT_MAX
-};
-
-/*!
- * \struct log_tlv_entry
- *
- * \brief TLV entry structure with a flexible
- * array member
- */
-struct log_tlv_entry {
- enum log_tlv_type type;
- uint32_t length;
- uint8_t value[];
-};
-
-/*!
- * \def LOG_MAC_SIZE
- *
- * \brief Size in bytes of the MAC for each entry
- */
-#define LOG_MAC_SIZE (4)
-
-/*!
- * \struct log_hdr
- *
- * \brief Fixed size logging entry header
- */
-struct log_hdr {
- uint64_t timestamp;
- uint32_t iv_counter;
- uint32_t partition_id;
- uint32_t size;
- uint32_t function_id;
- uint32_t arg[4];
-};
-
-/*!
- * \struct log_tlr
- *
- * \brief Fixed size logging entry trailer
- */
-struct log_tlr {
- uint8_t mac[LOG_MAC_SIZE];
-};
-
-/*!
- * \def LOG_HDR_SIZE
- *
- * \brief Size in bytes of the (fixed) header for
- * each entry
- */
-#define LOG_HDR_SIZE (sizeof(struct log_hdr))
-
-/*!
- * \def LOG_TLR_SIZE
- *
- * \brief Size in bytes of the (fixed) trailer for
- * each entry
- */
-#define LOG_TLR_SIZE (sizeof(struct log_tlr))
-
-/*!
- * \brief Retrieves at most size bytes from the log
- *
- * \details The function reads the audit log into the buffer provided.
- * If provided buffer size is too small to fit the full log,
- * the function will read the maximum number of items in the
- * log that fit the available space in the buffer
- *
- * \param[in] size Maximum number of bytes to retrieve from the log
- * \param[in] start Index of element from where to start retrieval
- * \param[out] buffer Pointer to the buffer that will hold the log
- * \param[out] info Pointer to the \ref tfm_log_info structure
- * contained information related to the retrieved
- * portion of the log (size and number of items)
- *
- * \return Returns TFM_LOG_ERR_SUCCESS if retrieval has been completed,
- * otherwise error as specified in \ref tfm_log_err
- *
- * \note If start is equal to TFM_ALG_READ_RECENT, the function will
- * retrieve the most recent elements that fit the provided size
- */
-enum tfm_log_err log_core_retrieve(const uint32_t size,
- const int32_t start,
- uint8_t *buffer,
- struct tfm_log_info *info);
-/*!
- * \brief Initializes the Audit logging service
- * during the TFM boot up process
- *
- * \return Returns TFM_LOG_ERR_SUCCESS if init has been completed,
- * otherwise error as specified in \ref tfm_log_err
- */
-enum tfm_log_err log_core_init(void);
-
-/*!
- * \brief Adds a log line to the log. It's meant to be called
- * only by other services that want to log information
- *
- * \param[in] line Pointer to the line to be added. This memory must
- * be accessible by the Audit logging service
- *
- * \return Returns TFM_LOG_ERR_SUCCESS if addition has been completed
- * otherwise error as specified in \ref tfm_log_err
- *
- */
-enum tfm_log_err log_core_add_line(const struct tfm_log_line *line);
-
-/*!
- * \brief Gets the log information
- *
- * \param[out] info Pointer to the \ref tfm_log_info structure that
- * holds the current log size (both in bytes and items)
- *
- * \return Returns TFM_LOG_ERR_SUCCESS if reading has been completed,
- * otherwise error as specified in \ref tfm_log_err
- */
-enum tfm_log_err log_core_get_info(struct tfm_log_info *info);
-
-/*!
- * \brief Deletes one or more elements from the head of the log
- *
- * \param[in] num_items Number of elements to be deleted
- * \param[out] rem_items Pointer to the number of elements removed. This
- * value indicates the number of elements actually
- * removed from the log. In case the number of items
- * stored is less than the number of items requested
- * to remove, this value will reflect the number of
- * items effectively removed.
- *
- * \return Returns TFM_LOG_ERR_SUCCESS if removal has been completed,
- * otherwise error as specified in \ref tfm_log_err
- */
-enum tfm_log_err log_core_delete_items(const uint32_t num_items,
- uint32_t *rem_items);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* LOG_CORE_H_ */
diff --git a/secure_fw/services/audit_logging/manifest.yaml b/secure_fw/services/audit_logging/manifest.yaml
index b124345..0dbdeb9 100644
--- a/secure_fw/services/audit_logging/manifest.yaml
+++ b/secure_fw/services/audit_logging/manifest.yaml
@@ -19,46 +19,55 @@
{
"name": "TFM_PERIPHERAL_UART1",
"permission": "READ-WRITE",
- "conditional": "LOG_UART_REDIRECTION"
+ "conditional": "AUDIT_UART_REDIRECTION"
}
],
- "tfm_init_symbol": "log_core_init",
+ "tfm_init_symbol": "audit_core_init",
"secure_functions": [
{
- "sfid": "TFM_LOG_RETRIEVE_SFID",
- "signal": "TFM_LOG_RETRIEVE",
- "tfm_symbol": "log_core_retrieve",
+ "sfid": "TFM_AUDIT_RETRIEVE_RECORD_SFID",
+ "signal": "TFM_AUDIT_RETRIEVE_RECORD",
+ "tfm_symbol": "audit_core_retrieve_record",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "strict"
},
{
- "sfid": "TFM_LOG_ADD_LINE_SFID",
- "signal": "TFM_LOG_ADD_LINE",
- "tfm_symbol": "log_core_add_line",
+ "sfid": "TFM_AUDIT_ADD_RECORD_SFID",
+ "signal": "TFM_AUDIT_ADD_RECORD",
+ "tfm_symbol": "audit_core_add_record",
"non_secure_clients": false,
"minor_version": 1,
"minor_policy": "strict"
},
{
- "sfid": "TFM_LOG_GET_INFO_SFID",
- "signal": "TFM_LOG_GET_INFO",
- "tfm_symbol": "log_core_get_info",
+ "sfid": "TFM_AUDIT_GET_INFO_SFID",
+ "signal": "TFM_AUDIT_GET_INFO",
+ "tfm_symbol": "audit_core_get_info",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "strict"
},
{
- "sfid": "TFM_LOG_DELETE_ITEMS_SFID",
- "signal": "TFM_LOG_DELETE_ITEMS",
- "tfm_symbol": "log_core_delete_items",
+ "sfid": "TFM_AUDIT_GET_RECORD_INFO_SFID",
+ "signal": "TFM_AUDIT_GET_RECORD_INFO",
+ "tfm_symbol": "audit_core_get_record_info",
+ "non_secure_clients": true,
+ "minor_version": 1,
+ "minor_policy": "strict"
+ },
+ {
+ "sfid": "TFM_AUDIT_DELETE_RECORD_SFID",
+ "signal": "TFM_AUDIT_DELETE_RECORD",
+ "tfm_symbol": "audit_core_delete_record",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "strict"
}
],
"source_files": [
- "log_core.c"
+ "audit_core.c",
+ "audit_wrappers.c"
],
"tfm_linker_pattern": {
"library_list": [
diff --git a/secure_fw/services/tfm_partition_list.inc b/secure_fw/services/tfm_partition_list.inc
index c5ad1b8..6b6547f 100644
--- a/secure_fw/services/tfm_partition_list.inc
+++ b/secure_fw/services/tfm_partition_list.inc
@@ -16,10 +16,10 @@
/******** TFM_SP_AUDIT_LOG ********/
PARTITION_DECLARE(TFM_SP_AUDIT_LOG, SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED);
-PARTITION_ADD_INIT_FUNC(TFM_SP_AUDIT_LOG, log_core_init);
-#ifdef LOG_UART_REDIRECTION
+PARTITION_ADD_INIT_FUNC(TFM_SP_AUDIT_LOG, audit_core_init);
+#ifdef AUDIT_UART_REDIRECTION
PARTITION_ADD_PERIPHERAL(TFM_SP_AUDIT_LOG, TFM_PERIPHERAL_UART1);
-#endif /* LOG_UART_REDIRECTION */
+#endif /* AUDIT_UART_REDIRECTION */
#ifdef TFM_PARTITION_TEST_CORE
/******** TFM_SP_CORE_TEST ********/