Contiki-NG
Loading...
Searching...
No Matches
edhoc-trace.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025, RISE Research Institutes of Sweden AB.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31/**
32 * \file
33 * EDHOC tracing and logging module.
34 * \author
35 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
36 */
37
38#include "edhoc-trace.h"
39#include "edhoc-config.h"
40#include "ecc-common.h"
41
42#include "sys/log.h"
43#define LOG_MODULE "EDHOC-Trace"
44#define LOG_LEVEL LOG_LEVEL_EDHOC
45
46/*---------------------------------------------------------------------------*/
47void
48edhoc_trace_message(uint8_t msg_num,
49 const uint8_t *msg_data,
50 size_t msg_len,
51 bool is_tx)
52{
53 if(LOG_LEVEL >= LOG_LEVEL_DBG) {
54 const char *direction = is_tx ? "TX" : "RX";
55 LOG_DBG_("%s message_%d CBOR (%zu bytes): ", direction, msg_num, msg_len);
56 if(msg_len > 0) {
57 LOG_DBG_BYTES(msg_data, msg_len);
58 }
59 LOG_DBG_("\n");
60 }
61}
62/*---------------------------------------------------------------------------*/
63void
64edhoc_trace_ephemeral_key(const char *role_label,
65 const uint8_t *pub_x,
66 const uint8_t *pub_y,
67 const uint8_t *priv)
68{
69 if(LOG_LEVEL >= LOG_LEVEL_DBG) {
70 LOG_DBG("=== %s Ephemeral Key Generation ===\n", role_label);
71
72 if(pub_x) {
73 EDHOC_TRACE_VALUE("Ephemeral public key (x coordinate)",
74 pub_x, ECC_KEY_LEN);
75 }
76
77 if(pub_y) {
78 EDHOC_TRACE_VALUE("Ephemeral public key (y coordinate)",
79 pub_y, ECC_KEY_LEN);
80 }
81
82 /* Only show the private key in RFC 9529 test-vector builds (EDHOC_TEST_VECTOR_TRACE_DH); hide it otherwise */
83#if EDHOC_TEST == EDHOC_TEST_VECTOR_TRACE_DH
84 if(priv) {
85 EDHOC_TRACE_VALUE("Ephemeral private key (test vector)",
86 priv, ECC_KEY_LEN);
87 }
88#else
89 if(priv) {
90 LOG_DBG("Ephemeral private key: [hidden for security]\n");
91 }
92#endif
93 }
94}
95/*---------------------------------------------------------------------------*/
96void
97edhoc_trace_transcript_hash(const char *th_label,
98 const uint8_t *th_data,
99 const uint8_t *input_data,
100 size_t input_len)
101{
102 if(LOG_LEVEL >= LOG_LEVEL_DBG) {
103 LOG_DBG("=== Computing %s ===\n", th_label);
104
105 if(input_data && input_len > 0) {
106 EDHOC_TRACE_VALUE("Input to transcript hash (CBOR)",
107 input_data, input_len);
108 }
109
110 if(th_data) {
111 EDHOC_TRACE_VALUE(th_label, th_data, HASH_LEN);
112 }
113 }
114}
115/*---------------------------------------------------------------------------*/
116void
117edhoc_trace_prk_derivation(const char *prk_label,
118 const uint8_t *prk_data,
119 const uint8_t *salt_data,
120 const uint8_t *ikm_data)
121{
122 if(LOG_LEVEL >= LOG_LEVEL_DBG) {
123 LOG_DBG("=== Deriving %s ===\n", prk_label);
124
125 if(salt_data) {
126 EDHOC_TRACE_VALUE("Salt", salt_data, HASH_LEN);
127 }
128
129 if(ikm_data) {
130 EDHOC_TRACE_VALUE("Input Keying Material (IKM)", ikm_data, ECC_KEY_LEN);
131 }
132
133 if(prk_data) {
134 EDHOC_TRACE_VALUE(prk_label, prk_data, HASH_LEN);
135 }
136 }
137}
138/*---------------------------------------------------------------------------*/
139void
140edhoc_trace_mac_computation(const char *mac_label,
141 const uint8_t *mac_data,
142 const uint8_t *context_data,
143 size_t context_len)
144{
145 if(LOG_LEVEL >= LOG_LEVEL_DBG) {
146 LOG_DBG("=== Computing %s ===\n", mac_label);
147
148 if(context_data && context_len > 0) {
149 EDHOC_TRACE_VALUE("MAC computation context", context_data, context_len);
150 }
151
152 if(mac_data) {
153 EDHOC_TRACE_VALUE(mac_label, mac_data, HASH_LEN);
154 }
155 }
156}
157/*---------------------------------------------------------------------------*/
158void
159edhoc_trace_credential(const char *cred_label,
160 const uint8_t *cred_data,
161 size_t cred_len,
162 const uint8_t *id_cred_data,
163 size_t id_cred_len)
164{
165 if(LOG_LEVEL >= LOG_LEVEL_DBG) {
166 LOG_DBG("=== %s Authentication ===\n", cred_label);
167
168 if(id_cred_data && id_cred_len > 0) {
169 char id_label[32];
170 snprintf(id_label, sizeof(id_label), "ID_%s", cred_label);
171 EDHOC_TRACE_VALUE(id_label, id_cred_data, id_cred_len);
172 }
173
174 if(cred_data && cred_len > 0) {
175 EDHOC_TRACE_VALUE(cred_label, cred_data, cred_len);
176 }
177 }
178}
179/*---------------------------------------------------------------------------*/
180void
181edhoc_trace_session_summary(const edhoc_context_t *ctx)
182{
183 if(!ctx || LOG_LEVEL < LOG_LEVEL_DBG) {
184 return;
185 }
186
187 LOG_DBG("=== EDHOC Session Summary ===\n");
188 LOG_DBG("Protocol Role: %s\n",
189 ctx->config.role == EDHOC_INITIATOR ? "Initiator" : "Responder");
190 LOG_DBG("Method: %d\n", ctx->config.method);
191 LOG_DBG("Cipher Suite: %d\n", ctx->state.suite_selected);
192
193 if(ctx->state.cid_len > 0) {
194 EDHOC_TRACE_VALUE("Local Connection ID",
195 ctx->state.cid, ctx->state.cid_len);
196 }
197
198 if(ctx->state.cid_rx_len > 0) {
199 EDHOC_TRACE_VALUE("Peer Connection ID",
200 ctx->state.cid_rx, ctx->state.cid_rx_len);
201 }
202
203 LOG_DBG("Test Vector Mode: %s\n",
204 EDHOC_TEST == EDHOC_TEST_VECTOR_TRACE_DH ? "RFC 9529" : "Production");
205
206 LOG_DBG("Session Status: Ready for key export\n");
207}
Common ECC types used by the EDHOC implementation.
EDHOC configuration file.
void edhoc_trace_transcript_hash(const char *th_label, const uint8_t *th_data, const uint8_t *input_data, size_t input_len)
Print transcript hash computation.
Definition edhoc-trace.c:97
void edhoc_trace_credential(const char *cred_label, const uint8_t *cred_data, size_t cred_len, const uint8_t *id_cred_data, size_t id_cred_len)
Print credential information.
void edhoc_trace_mac_computation(const char *mac_label, const uint8_t *mac_data, const uint8_t *context_data, size_t context_len)
Print MAC computation step.
void edhoc_trace_ephemeral_key(const char *role_label, const uint8_t *pub_x, const uint8_t *pub_y, const uint8_t *priv)
Print ephemeral key generation step.
Definition edhoc-trace.c:64
void edhoc_trace_message(uint8_t msg_num, const uint8_t *msg_data, size_t msg_len, bool is_tx)
Print EDHOC message in RFC 9529 trace format.
Definition edhoc-trace.c:48
void edhoc_trace_session_summary(const edhoc_context_t *ctx)
Print session summary at protocol completion.
void edhoc_trace_prk_derivation(const char *prk_label, const uint8_t *prk_data, const uint8_t *salt_data, const uint8_t *ikm_data)
Print PRK derivation step.
EDHOC tracing and logging header.
#define EDHOC_TRACE_VALUE(label, data, len)
Print cryptographic value in RFC 9529 trace format.
Definition edhoc-trace.h:59
Header file for the logging system.