Contiki-NG
Loading...
Searching...
No Matches
edhoc-key-storage.h
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 key storage - Implementation of key storage for managing
35 * DH-static authentication key pairs. Can also be used to store
36 * the DH-static authentication public keys of other EDHOC peers.
37 *
38 * \author
39 * Lidia Pocero <pocero@isi.gr>, Peter A Jonsson, Rikard Höglund, Marco Tiloca
40 * Christos Koulamas <cklm@isi.gr>
41 */
42#ifndef _EDHOC_KEY_STORAGE_H_
43#define _EDHOC_KEY_STORAGE_H_
44#include "contiki.h"
45#include "contiki-lib.h"
46#include "ecc-common.h"
47#include "edhoc-error.h"
48#include "edhoc-config.h"
49#include <stdio.h>
50
51/**
52 * \brief KEY length in bytes
53 *
54 */
55#ifndef ECC_KEY_LEN
56#define ECC_KEY_LEN 32
57#endif
58
59/* COSE KEY struct */
60typedef struct cose_key {
61 struct cose_key_t *next;
62 uint8_t kid[EDHOC_MAX_KID_LEN];
63 uint8_t kid_sz;
64 char identity[EDHOC_MAX_IDENTITY_LEN];
65 uint8_t identity_sz;
66 uint8_t kty;
67 uint8_t crv;
68 ecc_key_t ecc;
70
71/**
72 * \brief Create the keys repository
73 *
74 * Create a repository of keys in the form of a list
75 * \return EDHOC_SUCCESS on success, error code on failure
76 */
78
79/**
80 * \brief Add a DH key to the repository
81 * \param key Input key to add in cose_key_t format
82 *
83 * Adds a new key to the repository in the form of a cose_key_t struct.
84 * \return EDHOC_SUCCESS on success, error code on failure
85 */
87
88/**
89 * \brief Check in the keys repository for the key with the specific KID
90 * \param kid Input Key Identification
91 * \param kid_sz Input Key Identification length
92 * \param auth_key Output cose_key_t key that corresponds to the KID
93 * \return EDHOC_SUCCESS if found, EDHOC_ERR_KEY_NOT_FOUND if not found, other error code on failure
94 *
95 * This function checks the repository and returns a DH cose_key_t key
96 * that is associated with the requested KID if it exists.
97 */
98edhoc_error_t edhoc_check_key_list_kid(uint8_t *kid, uint8_t kid_sz, cose_key_t **auth_key);
99
100/**
101 * \brief Check in the keys repository for the key with the specific identity
102 * \param identity Input key identity
103 * \param identity_sz Input key identity length
104 * \param auth_key Output cose_key_t key that corresponds to the identity
105 * \return EDHOC_SUCCESS if found, EDHOC_ERR_KEY_NOT_FOUND if not found, other error code on failure
106 *
107 * This function checks the repository and returns a DH cose_key_t key
108 * that is associated with the requested identity if it exists.
109 */
110edhoc_error_t edhoc_check_key_list_identity(char *identity, uint8_t identity_sz, cose_key_t **auth_key);
111
112/**
113 * \brief Remove from the keys repository the key with the specific KID
114 * \param kid Input Key Identification
115 * \param kid_sz Input Key Identification length
116 * \return EDHOC_SUCCESS if removed, EDHOC_ERR_KEY_NOT_FOUND if not found, other error code on failure
117 *
118 * This function deletes from the repository the DH cose_key_t key
119 * that is associated with the KID if it exists.
120 */
121edhoc_error_t edhoc_remove_key_kid(uint8_t *kid, uint8_t kid_sz);
122
123/**
124 * \brief Remove from the keys repository the key with the specific identity
125 * \param identity Input key identity
126 * \param identity_sz Input key identity length
127 * \return EDHOC_SUCCESS if removed, EDHOC_ERR_KEY_NOT_FOUND if not found, other error code on failure
128 *
129 * This function deletes from the repository the DH cose_key_t key
130 * that is associated with the identity if it exists.
131 */
132edhoc_error_t edhoc_remove_key_identity(char *identity, uint8_t identity_sz);
133
134/**
135 * \brief Remove from the keys repository the specific DH cose_key_t key
136 * \param auth_key Input key to remove from the repository
137 *
138 * This function deletes from the repository the DH cose_key_t key pointed to by the auth_key parameter.
139 * \return EDHOC_SUCCESS on success, error code on failure
140 */
142
143/**
144 * \brief Copy a COSE key from one structure to another
145 * \param k The destination where the COSE key will be copied
146 * \param key The source COSE key to be copied
147 *
148 * This function copies the contents of the COSE key structure from the source (key)
149 * to the destination (k). The entire structure is copied using memcpy.
150 * \return EDHOC_SUCCESS on success, error code on failure
151 */
153
154/**
155 * \brief Print a key in cose_key_t struct format for debugging
156 * \param cose Input cose_key_t struct
157 */
158void cose_print_key(cose_key_t *cose);
159
160/**
161 * \brief Print detailed key information at INFO level for user visibility
162 * \param key The COSE key to print
163 * \param label A descriptive label for the key (e.g., "Client", "Server")
164 */
165void edhoc_print_key_info(const cose_key_t *key, const char *label);
166
167/**
168 * \brief Print credential (CRED_I/CRED_R) values in readable format
169 * \param label A descriptive label for the credential (e.g., "CRED_I", "CRED_R")
170 * \param cred The credential bytes to print
171 * \param cred_sz The size of the credential
172 */
173void edhoc_print_credential(const char *label, const uint8_t *cred, size_t cred_sz);
174
175/**
176 * \brief Get the total number of keys loaded in the key storage
177 * \return The number of keys currently stored
178 */
179uint8_t edhoc_get_key_count(void);
180
181/**
182 * \brief List all keys in the key storage with basic information
183 */
184void edhoc_list_all_keys(void);
185
186/**
187 * \brief Validate the current key setup and print diagnostic information
188 * \return EDHOC_SUCCESS if validation passes, error code otherwise
189 */
191
192/**
193 * \brief Helper function to set up a key pair (own + peer) with validation
194 * \param own_key The key for own identity (with private key)
195 * \param peer_key The key for peer identity (public only)
196 * \param own_label Descriptive label for own key
197 * \param peer_label Descriptive label for peer key
198 * \return EDHOC_SUCCESS if setup successful, error code otherwise
199 */
201 const char *own_label, const char *peer_label);
202
203#endif
Common ECC types used by the EDHOC implementation.
EDHOC configuration file.
Error handling module header for EDHOC.
edhoc_error_t
Unified error type for EDHOC operations.
Definition edhoc-error.h:60
void edhoc_print_credential(const char *label, const uint8_t *cred, size_t cred_sz)
Print credential (CRED_I/CRED_R) values in readable format.
edhoc_error_t edhoc_create_key_list(void)
Create the keys repository.
void edhoc_list_all_keys(void)
List all keys in the key storage with basic information.
edhoc_error_t edhoc_remove_key_identity(char *identity, uint8_t identity_sz)
Remove from the keys repository the key with the specific identity.
edhoc_error_t edhoc_check_key_list_kid(uint8_t *kid, uint8_t kid_sz, cose_key_t **auth_key)
Check in the keys repository for the key with the specific KID.
edhoc_error_t edhoc_add_key(cose_key_t *key)
Add a DH key to the repository.
edhoc_error_t edhoc_remove_key_kid(uint8_t *kid, uint8_t kid_sz)
Remove from the keys repository the key with the specific KID.
void edhoc_print_key_info(const cose_key_t *key, const char *label)
Print detailed key information at INFO level for user visibility.
void cose_print_key(cose_key_t *cose)
Print a key in cose_key_t struct format for debugging.
edhoc_error_t edhoc_validate_key_setup(void)
Validate the current key setup and print diagnostic information.
edhoc_error_t edhoc_copy_key(cose_key_t *k, cose_key_t *key)
Copy a COSE key from one structure to another.
struct cose_key cose_key_t
KEY length in bytes.
edhoc_error_t edhoc_check_key_list_identity(char *identity, uint8_t identity_sz, cose_key_t **auth_key)
Check in the keys repository for the key with the specific identity.
edhoc_error_t edhoc_setup_key_pair(cose_key_t *own_key, cose_key_t *peer_key, const char *own_label, const char *peer_label)
Helper function to set up a key pair (own + peer) with validation.
edhoc_error_t edhoc_remove_key(cose_key_t *auth_key)
Remove from the keys repository the specific DH cose_key_t key.
uint8_t edhoc_get_key_count(void)
Get the total number of keys loaded in the key storage.
#define EDHOC_MAX_IDENTITY_LEN
Maximum length of identity strings in credentials Used for human-readable credential identities (e....
#define EDHOC_MAX_KID_LEN
Maximum length of Key Identifiers (KID) in credentials COSE Key IDs can be 1-4 bytes typically,...
KEY length in bytes.