Contiki-NG
Loading...
Searching...
No Matches
edhoc-exporter.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024, RISE Research Institutes of Sweden AB
3 * Copyright (c) 2020, Industrial Systems Institute (ISI), Patras, Greece
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32/**
33 * \file
34 * edhoc-export an implementation to export keys from the EDHOC shared secret
35 *
36 * \author
37 * Lidia Pocero <pocero@isi.gr>
38 * Rikard Höglund
39 * Marco Tiloca
40 */
41
42#include "edhoc-exporter.h"
43#include "contiki-lib.h"
44
45#include "sys/log.h"
46#define LOG_MODULE "EDHOC"
47#define LOG_LEVEL LOG_LEVEL_EDHOC
48
49/*---------------------------------------------------------------------------*/
50void
52{
53 LOG_PRINT("Initiator client CID: 0x%02x\n", osc->client_ID);
54 LOG_PRINT("Responder server CID: 0x%02x\n", osc->server_ID);
55 LOG_PRINT("OSCORE Master Secret (%d bytes): ", OSCORE_KEY_SZ);
56 LOG_PRINT_BYTES(osc->master_secret, OSCORE_KEY_SZ);
57 LOG_PRINT_("\n");
58 LOG_PRINT("OSCORE Master Salt (%d bytes): ", OSCORE_SALT_SZ);
59 LOG_PRINT_BYTES(osc->master_salt, OSCORE_SALT_SZ);
60 LOG_PRINT_("\n");
61}
62/*---------------------------------------------------------------------------*/
63int8_t
64edhoc_exporter(const uint8_t *in_key, uint8_t exporter_label,
65 const uint8_t *context, uint8_t context_sz,
66 uint16_t length, uint8_t *result)
67{
68 return edhoc_kdf(in_key, exporter_label, context, context_sz,
69 length, result);
70}
71/*---------------------------------------------------------------------------*/
72/* TODO: May be better to actually store PRK_out & PRK_exporter and
73 then use them in edhoc_exporter above */
74int8_t
75edhoc_exporter_oscore(oscore_ctx_t *osc, edhoc_context_t *ctx)
76{
77 /* Derive prk_out */
78 int prk_out_sz = HASH_LEN;
79 uint8_t prk_out[prk_out_sz];
80 int8_t err = edhoc_kdf(ctx->state.prk_4e3m, PRK_OUT_LABEL, ctx->state.th,
81 HASH_LEN, prk_out_sz, prk_out);
82 if(err < 0) {
83 return err;
84 }
85 LOG_DBG("PRK_out (%d bytes): ", prk_out_sz);
86 LOG_DBG_BYTES(prk_out, prk_out_sz);
87 LOG_DBG_("\n");
88
89 /* Derive prk_exporter */
90 int prk_exporter_sz = HASH_LEN;
91 uint8_t prk_exporter[prk_exporter_sz];
92 err = edhoc_kdf(prk_out, PRK_EXPORTER_LABEL, NULL, 0,
93 prk_exporter_sz, prk_exporter);
94 if(err < 0) {
95 return err;
96 }
97 LOG_DBG("PRK_exporter (%d bytes): ", prk_exporter_sz);
98 LOG_DBG_BYTES(prk_exporter, prk_exporter_sz);
99 LOG_DBG_("\n");
100
101 /* The OSCORE client is the initiator */
102 if(EDHOC_ROLE == EDHOC_INITIATOR) {
103 /* For OSCORE, convert CID to integer (only for single-byte CIDs) */
104 if(ctx->state.cid_len == 1) {
105 osc->client_ID = ctx->state.cid[0];
106 } else {
107 LOG_WARN("Multi-byte CID not supported for OSCORE\n");
108 osc->client_ID = 0; /* Fallback */
109 }
110 if(ctx->state.cid_rx_len == 1) {
111 osc->server_ID = ctx->state.cid_rx[0];
112 } else {
113 LOG_WARN("Multi-byte CID not supported for OSCORE\n");
114 osc->server_ID = 0; /* Fallback */
115 }
116 }
117 if(EDHOC_ROLE == EDHOC_RESPONDER) {
118 if(ctx->state.cid_rx_len == 1) {
119 osc->client_ID = ctx->state.cid_rx[0];
120 } else {
121 LOG_WARN("Multi-byte CID not supported for OSCORE\n");
122 osc->client_ID = 0; /* Fallback */
123 }
124 if(ctx->state.cid_len == 1) {
125 osc->server_ID = ctx->state.cid[0];
126 } else {
127 LOG_WARN("Multi-byte CID not supported for OSCORE\n");
128 osc->server_ID = 0; /* Fallback */
129 }
130 }
131
132 /* Derive OSCORE Master Secret */
133 err = edhoc_exporter(prk_exporter, OSCORE_MASTER_SECRET_LABEL, NULL,
134 0, OSCORE_KEY_SZ, osc->master_secret);
135 if(err < 0) {
136 return err;
137 }
138
139 err = edhoc_exporter(prk_exporter, OSCORE_MASTER_SALT_LABEL, NULL,
140 0, OSCORE_SALT_SZ, osc->master_salt);
141 return err;
142}
143/*---------------------------------------------------------------------------*/
EDHOC Exporter API - Interface to derive application security contexts from the EDHOC shared secret.
int16_t edhoc_kdf(const uint8_t *prk, uint8_t info_label, const uint8_t *context, uint8_t context_sz, uint16_t length, uint8_t *result)
EDHOC Key Derivation Function (KDF) based on HMAC-based Expand (RFC 5869).
Definition edhoc.c:373
struct oscore_ctx oscore_ctx_t
OSCORE context struct.
void print_oscore_ctx(oscore_ctx_t *osc)
Print OSCORE Security Context contents for debugging.
int8_t edhoc_exporter(const uint8_t *in_key, uint8_t exporter_label, const uint8_t *context, uint8_t context_sz, uint16_t length, uint8_t *result)
Derive an application-specific key from EDHOC.
#define EDHOC_ROLE
Set the EDHOC Protocol role.
int8_t edhoc_exporter_oscore(oscore_ctx_t *osc, edhoc_context_t *ctx)
Derive an OSCORE Context from EDHOC.
Header file for the logging system.