blob: afbd951a7aa78ca6a9388dc0d2ad8a141ed19baf [file] [log] [blame]
Jamie Foxe7f8b4e2023-05-30 18:03:20 +01001/*
Tamas Bana5e2f582024-01-25 16:59:26 +01002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Jamie Foxe7f8b4e2023-05-30 18:03:20 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "dpe_log.h"
Maulik Patelad2f3db2023-05-17 15:41:36 +01009#include "dpe_context_mngr.h"
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010010
11#if (TFM_PARTITION_LOG_LEVEL >= TFM_PARTITION_LOG_LEVEL_DEBUG)
Maulik Patel780943f2024-03-14 11:40:14 +000012#define LOG_BOOL_VAL(arg) ((arg) ? "true" : "false")
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010013
14static void print_byte_array(const uint8_t *array, size_t len)
15{
16 size_t i;
17
18 if (array != NULL) {
19 for (i = 0; i < len; ++i) {
20 if ((i & 0xF) == 0) {
21 LOG_DBGFMT("\r\n ");
22 }
23 if (array[i] < 0x10) {
24 LOG_DBGFMT(" 0%x", array[i]);
25 } else {
26 LOG_DBGFMT(" %x", array[i]);
27 }
28 }
29 }
30
31 LOG_DBGFMT("\r\n");
32}
33
34static void log_dice_inputs(const DiceInputValues *input)
35{
36 LOG_DBGFMT(" - DICE code_hash =");
37 print_byte_array(input->code_hash, sizeof(input->code_hash));
38 LOG_DBGFMT(" - DICE code_descriptor =");
39 print_byte_array(input->code_descriptor, input->code_descriptor_size);
40 LOG_DBGFMT(" - DICE config_type = %d\r\n", input->config_type);
41 LOG_DBGFMT(" - DICE config_value =");
42 print_byte_array(input->config_value, sizeof(input->config_value));
43 LOG_DBGFMT(" - DICE config_descriptor =");
44 print_byte_array(input->config_descriptor, input->config_descriptor_size);
45 LOG_DBGFMT(" - DICE authority_hash =");
46 print_byte_array(input->authority_hash, sizeof(input->authority_hash));
47 LOG_DBGFMT(" - DICE authority_descriptor =");
48 print_byte_array(input->authority_descriptor,
49 input->authority_descriptor_size);
50 LOG_DBGFMT(" - DICE mode = %d\r\n", input->mode);
51 LOG_DBGFMT(" - DICE hidden =");
52 print_byte_array(input->hidden, sizeof(input->hidden));
53}
54
Maulik Patelad2f3db2023-05-17 15:41:36 +010055void log_derive_rot_context(const DiceInputValues *dice_inputs)
56{
57 LOG_DBGFMT("DPE DeriveRoTContext:\r\n");
58 log_dice_inputs(dice_inputs);
59}
60
Maulik Patel9a2a5672024-03-14 13:43:58 +000061static void log_handle(int context_handle)
62{
63 LOG_DBGFMT(" index - %d,", GET_IDX(context_handle));
64 LOG_DBGFMT(" nonce - 0x%x\r\n", GET_NONCE(context_handle));
65}
66
Maulik Patela81605b2023-10-24 12:17:03 +010067void log_derive_context(int context_handle,
Maulik Patelcb14cde2024-01-23 12:39:53 +000068 uint32_t cert_id,
Maulik Patela81605b2023-10-24 12:17:03 +010069 bool retain_parent_context,
70 bool allow_new_context_to_derive,
71 bool create_certificate,
72 const DiceInputValues *dice_inputs,
73 int32_t client_id)
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010074{
Maulik Patela81605b2023-10-24 12:17:03 +010075 LOG_DBGFMT("DPE DeriveContext:\r\n");
Maulik Patel9a2a5672024-03-14 13:43:58 +000076 LOG_DBGFMT(" - input context handle:");
77 log_handle(context_handle);
Maulik Patelcb14cde2024-01-23 12:39:53 +000078 LOG_DBGFMT(" - cert_id = 0x%x\r\n", cert_id);
Maulik Patel780943f2024-03-14 11:40:14 +000079 LOG_DBGFMT(" - retain_parent_context = %s\r\n", LOG_BOOL_VAL(retain_parent_context));
80 LOG_DBGFMT(" - allow_new_context_to_derive = %s\r\n", LOG_BOOL_VAL(allow_new_context_to_derive));
81 LOG_DBGFMT(" - create_certificate = %s\r\n", LOG_BOOL_VAL(create_certificate));
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010082 log_dice_inputs(dice_inputs);
Maulik Patelad2f3db2023-05-17 15:41:36 +010083 LOG_DBGFMT(" - client_id = %d\r\n", client_id);
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010084}
85
Maulik Patel54d65f72023-06-28 13:04:36 +010086void log_destroy_context(int context_handle, bool destroy_recursively)
87{
88 LOG_DBGFMT("DPE DestroyContext:\r\n");
Maulik Patel9a2a5672024-03-14 13:43:58 +000089 LOG_DBGFMT(" - input context handle:");
90 log_handle(context_handle);
Maulik Patel780943f2024-03-14 11:40:14 +000091 LOG_DBGFMT(" - destroy_recursively = %s\r\n", LOG_BOOL_VAL(destroy_recursively));
Maulik Patel54d65f72023-06-28 13:04:36 +010092}
93
Jamie Foxe7f8b4e2023-05-30 18:03:20 +010094void log_certify_key(int context_handle,
95 bool retain_context,
96 const uint8_t *public_key,
97 size_t public_key_size,
98 const uint8_t *label,
99 size_t label_size)
100{
101 LOG_DBGFMT("DPE CertifyKey:\r\n");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000102 LOG_DBGFMT(" - input context handle:");
103 log_handle(context_handle);
Maulik Patel780943f2024-03-14 11:40:14 +0000104 LOG_DBGFMT(" - retain_context = %s\r\n", LOG_BOOL_VAL(retain_context));
Jamie Foxe7f8b4e2023-05-30 18:03:20 +0100105 LOG_DBGFMT(" - public_key =");
106 print_byte_array(public_key, public_key_size);
107 LOG_DBGFMT(" - label =");
108 print_byte_array(label, label_size);
109}
110
Maulik Patel83a6b592023-12-05 15:20:30 +0000111void log_get_certificate_chain(int context_handle,
112 bool retain_context,
Tamas Bana5e2f582024-01-25 16:59:26 +0100113 bool clear_from_context,
114 size_t cert_chain_buf_size)
Maulik Patel83a6b592023-12-05 15:20:30 +0000115{
116 LOG_DBGFMT("DPE GetCertificateChain:\r\n");
Maulik Patel9a2a5672024-03-14 13:43:58 +0000117 LOG_DBGFMT(" - input context handle:");
118 log_handle(context_handle);
Maulik Patel780943f2024-03-14 11:40:14 +0000119 LOG_DBGFMT(" - retain_context = %s\r\n", LOG_BOOL_VAL(retain_context));
120 LOG_DBGFMT(" - clear_from_context = %s\r\n", LOG_BOOL_VAL(clear_from_context));
Tamas Bana5e2f582024-01-25 16:59:26 +0100121 LOG_DBGFMT(" - cert_chain_buf_size = %d\r\n", cert_chain_buf_size);
Maulik Patel83a6b592023-12-05 15:20:30 +0000122}
123
Maulik Patel00d06b62024-07-03 14:51:50 +0100124void log_intermediate_certificate(const uint8_t *cert_buf,
Tamas Ban7daae9e2024-04-03 13:54:34 +0200125 size_t cert_size)
Maulik Patel2358bbb2023-07-21 10:56:56 +0100126{
127 LOG_DBGFMT("DPE Intermediate Certificate:\r\n");
Tamas Ban7daae9e2024-04-03 13:54:34 +0200128 LOG_DBGFMT(" - size = %d\r\n", cert_size);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100129 LOG_DBGFMT(" - certificate =");
Tamas Ban7daae9e2024-04-03 13:54:34 +0200130 print_byte_array(cert_buf, cert_size);
Maulik Patel2358bbb2023-07-21 10:56:56 +0100131}
132
Maulik Patele6adc112023-08-18 14:21:51 +0100133void log_certificate_chain(const uint8_t *certificate_chain_buf,
134 size_t certificate_chain_size)
135{
136 LOG_DBGFMT("DPE Certificate Chain:\r\n");
137 LOG_DBGFMT(" - size = %d\r\n", certificate_chain_size);
138 print_byte_array(certificate_chain_buf, certificate_chain_size);
139}
140
Maulik Patel9a2a5672024-03-14 13:43:58 +0000141void log_derive_context_output_handles(int parent_context_handle,
142 int new_context_handle)
143{
144 LOG_DBGFMT("DPE DeriveContext output handles:\r\n");
145 LOG_DBGFMT(" - parent context handle:");
146 log_handle(parent_context_handle);
147 LOG_DBGFMT(" - new context handle:");
148 log_handle(new_context_handle);
149}
150
151void log_certify_key_output_handle(int new_context_handle)
152{
153 LOG_DBGFMT("DPE CertifyKey output handle:\r\n");
154 LOG_DBGFMT(" - new context handle:");
155 log_handle(new_context_handle);
156}
157
158void log_get_certificate_chain_output_handle(int new_context_handle)
159{
160 LOG_DBGFMT("DPE GetCertificateChain output handle:\r\n");
161 LOG_DBGFMT(" - new context handle:");
162 log_handle(new_context_handle);
163}
164
Maulik Patel5ac87802024-03-14 14:22:19 +0000165void log_dpe_component_ctx_metadata(const struct component_context_t *ctx_ptr,
166 int component_index)
167{
168 LOG_DBGFMT(" DPE component_ctx_array[%d]: \r\n", component_index);
169 LOG_DBGFMT(" - in_use = %s\r\n", LOG_BOOL_VAL(ctx_ptr->in_use));
170 LOG_DBGFMT(" - is_allowed_to_derive = %s\r\n",
171 LOG_BOOL_VAL(ctx_ptr->is_allowed_to_derive));
172 LOG_DBGFMT(" - is_export_cdi_allowed = %s\r\n",
173 LOG_BOOL_VAL(ctx_ptr->is_export_cdi_allowed));
174 LOG_DBGFMT(" - nonce = 0x%x\r\n", ctx_ptr->nonce);
Maulik Patel5ac87802024-03-14 14:22:19 +0000175 LOG_DBGFMT(" - target_locality = %d\r\n", ctx_ptr->target_locality);
176 LOG_DBGFMT(" - expected_mhu_id = %u\r\n", ctx_ptr->expected_mhu_id);
Maulik Patel00d06b62024-07-03 14:51:50 +0100177 LOG_DBGFMT(" - parent_comp_ctx->nonce = %d\r\n", ctx_ptr->parent_comp_ctx->nonce);
178 if (ctx_ptr->linked_cert_ctx != NULL) {
179 LOG_DBGFMT(" - linked_cert_ctx->cert_id = %d\r\n",
180 ctx_ptr->linked_cert_ctx->cert_id);
181 }
Maulik Patel5ac87802024-03-14 14:22:19 +0000182}
183
Maulik Patel00d06b62024-07-03 14:51:50 +0100184void log_dpe_cert_ctx_metadata(const struct cert_context_t *ctx_ptr)
Maulik Patel5ac87802024-03-14 14:22:19 +0000185{
Maulik Patel00d06b62024-07-03 14:51:50 +0100186 LOG_DBGFMT(" DPE cert_ctx_array[]: \r\n");
Maulik Patel5ac87802024-03-14 14:22:19 +0000187 LOG_DBGFMT(" - cert_id = 0x%x\r\n", ctx_ptr->cert_id);
Maulik Patel5ac87802024-03-14 14:22:19 +0000188 LOG_DBGFMT(" - state = %d\r\n", ctx_ptr->state);
189 LOG_DBGFMT(" - is_external_pub_key_provided = %s\r\n",
190 LOG_BOOL_VAL(ctx_ptr->is_external_pub_key_provided));
191 LOG_DBGFMT(" - is_cdi_to_be_exported = %s\r\n",
192 LOG_BOOL_VAL(ctx_ptr->is_cdi_to_be_exported));
193}
194
Maulik Patelb3c82a02024-07-24 13:05:42 +0100195void log_derive_context_output(int *new_context_handle,
196 int *new_parent_context_handle,
197 struct component_context_t *derived_ctx,
198 int free_component_idx,
199 struct cert_context_t *cert_ctx,
200 uint8_t *new_certificate_buf,
Jamie Foxc4c3d792024-08-27 15:24:04 +0100201 size_t *new_certificate_actual_size)
Maulik Patelb3c82a02024-07-24 13:05:42 +0100202{
203 log_derive_context_output_handles(*new_parent_context_handle,
204 *new_context_handle);
205
206 /* Log component context, certificate context & certificate if no error */
207 log_dpe_component_ctx_metadata(derived_ctx, free_component_idx);
208 if (cert_ctx != NULL) {
209 log_dpe_cert_ctx_metadata(cert_ctx);
210 }
Jamie Foxc4c3d792024-08-27 15:24:04 +0100211 if (new_certificate_actual_size != NULL && *new_certificate_actual_size > 0) {
Maulik Patelb3c82a02024-07-24 13:05:42 +0100212 log_intermediate_certificate(new_certificate_buf,
Jamie Foxc4c3d792024-08-27 15:24:04 +0100213 *new_certificate_actual_size);
Maulik Patelb3c82a02024-07-24 13:05:42 +0100214 }
215}
216
Jamie Foxe7f8b4e2023-05-30 18:03:20 +0100217#endif /* TFM_PARTITION_LOG_LEVEL */