libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
oscore_crypto.c
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
3/*
4 * Copyright (c) 2018, SICS, RISE AB
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Institute nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
46
48
49#if COAP_OSCORE_SUPPORT
50
51#include <string.h>
52
53#include <stdio.h>
54
55/*
56 * return 0 fail
57 * 1 OK
58 */
59int
62 coap_bin_const_t *data,
63 coap_bin_const_t **hmac) {
64 if (!coap_crypto_hmac(hmac_alg, key, data, hmac)) {
65 coap_log_warn("oscore_hmac_hash: Failed hmac\n");
66 return 0;
67 }
68 return 1;
69}
70
71/*
72 * return 0 fail
73 * 1 OK
74 */
75int
77 coap_bin_const_t *salt,
79 coap_bin_const_t **hkdf_extract) {
80 cose_hmac_alg_t hmac_alg;
81
82 assert(ikm);
83 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
84 return 0;
85 if (salt == NULL || salt->s == NULL) {
86 uint8_t zeroes_data[32];
87 coap_bin_const_t zeroes;
88
89 memset(zeroes_data, 0, sizeof(zeroes_data));
90 zeroes.s = zeroes_data;
91 zeroes.length = sizeof(zeroes_data);
92
93 return oscore_hmac_hash(hmac_alg, &zeroes, ikm, hkdf_extract);
94 } else {
95 return oscore_hmac_hash(hmac_alg, salt, ikm, hkdf_extract);
96 }
97}
98
99/*
100 * return 0 fail
101 * 1 OK
102 */
103int
105 coap_bin_const_t *prk,
106 uint8_t *info,
107 size_t info_len,
108 uint8_t *okm,
109 size_t okm_len) {
110 size_t N = (okm_len + 32 - 1) / 32; /* ceil(okm_len/32) */
111 uint8_t *aggregate_buffer = coap_malloc_type(COAP_STRING, 32 + info_len + 1);
112 uint8_t *out_buffer =
113 coap_malloc_type(COAP_STRING, (N + 1) * 32); /* 32 extra bytes to fit the last block */
114 size_t i;
115 coap_bin_const_t data;
116 coap_bin_const_t *hkdf = NULL;
117 cose_hmac_alg_t hmac_alg;
118
119 if (!cose_get_hmac_alg_for_hkdf(hkdf_alg, &hmac_alg))
120 goto fail;
121
122 /* Compose T(1) */
123 memcpy(aggregate_buffer, info, info_len);
124 aggregate_buffer[info_len] = 0x01;
125
126 data.s = aggregate_buffer;
127 data.length = info_len + 1;
128 if (!oscore_hmac_hash(hmac_alg, prk, &data, &hkdf))
129 goto fail;
130 memcpy(&out_buffer[0], hkdf->s, hkdf->length);
132
133 /* Compose T(2) -> T(N) */
134 memcpy(aggregate_buffer, &(out_buffer[0]), 32);
135 for (i = 1; i < N; i++) {
136 memcpy(&(aggregate_buffer[32]), info, info_len);
137 aggregate_buffer[32 + info_len] = (uint8_t)(i + 1);
138 data.s = aggregate_buffer;
139 data.length = 32 + info_len + 1;
140 if (!oscore_hmac_hash(hmac_alg, prk, &data, &hkdf))
141 goto fail;
142 memcpy(&out_buffer[i * 32], hkdf->s, hkdf->length);
144 memcpy(aggregate_buffer, &(out_buffer[i * 32]), 32);
145 }
146 memcpy(okm, out_buffer, okm_len);
147 coap_free_type(COAP_STRING, out_buffer);
148 coap_free_type(COAP_STRING, aggregate_buffer);
149 return 1;
150
151fail:
152 coap_free_type(COAP_STRING, out_buffer);
153 coap_free_type(COAP_STRING, aggregate_buffer);
154 return 0;
155}
156
157/*
158 * return 0 fail
159 * 1 OK
160 */
161int
163 coap_bin_const_t *salt,
164 coap_bin_const_t *ikm,
165 uint8_t *info,
166 size_t info_len,
167 uint8_t *okm,
168 size_t okm_len) {
169 int ret;
170 coap_bin_const_t *hkdf_extract = NULL;
171 if (!oscore_hkdf_extract(hkdf_alg, salt, ikm, &hkdf_extract))
172 return 0;
173 ret =
174 oscore_hkdf_expand(hkdf_alg, hkdf_extract, info, info_len, okm, okm_len);
175 coap_delete_bin_const(hkdf_extract);
176 return ret;
177}
178
179#if COAP_OSCORE_GROUP_SUPPORT
180/* Return 0 if key pair generation failure. Key lengths are derived from ed25519
181 * values. No check is done to ensure that buffers are of the correct length. */
182
183#if 0
184int
185oscore_edDSA_keypair(cose_alg_t alg, cose_curve_t alg_param,
186 uint8_t *private_key,
187 uint8_t *public_key, uint8_t *ed25519_seed) {
188 if (alg != COSE_Algorithm_EdDSA || alg_param != COSE_Elliptic_Curve_Ed25519) {
189 return 0;
190 }
191 ed25519_create_keypair(public_key, private_key, ed25519_seed);
192
194 coap_log(COAP_LOG_OSCORE, "Key Pair\n");
195 oscore_log_hex_value(COAP_LOG_OSCORE, "Public Key", public_key);
196 oscore_log_hex_value(COAP_LOG_OSCORE, "Private Key", private_key);
197 oscore_log_hex_value(COAP_LOG_OSCORE, "Seed", ed25519_seed);
198 }
199
200 return 1;
201}
202#endif
203
204#endif /* COAP_OSCORE_GROUP_SUPPORT */
205
206#else /* ! COAP_OSCORE_SUPPORT */
207
208#ifdef __clang__
209/* Make compilers happy that do not like empty modules. As this function is
210 * never used, we ignore -Wunused-function at the end of compiling this file
211 */
212#pragma GCC diagnostic ignored "-Wunused-function"
213#endif
214static inline void
215dummy(void) {
216}
217
218#endif /* ! COAP_OSCORE_SUPPORT */
Library specific build wrapper for coap_internal.h.
@ COAP_STRING
Definition coap_mem.h:33
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define NULL
Definition coap_option.h:30
int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Create a HMAC hash of the provided data.
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log(level,...)
Logging function.
Definition coap_debug.h:290
@ COAP_LOG_OSCORE
Definition coap_debug.h:65
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
cose_hkdf_alg_t
cose_hmac_alg_t
cose_curve_t
Definition oscore_cose.h:62
cose_alg_t
int oscore_hkdf_expand(cose_hkdf_alg_t hkdf_alg, coap_bin_const_t *prk, uint8_t *info, size_t info_len, uint8_t *okm, size_t okm_len)
Derive the key using HKDF-Expand() function.
int oscore_hkdf(cose_hkdf_alg_t hkdf_alg, coap_bin_const_t *salt, coap_bin_const_t *ikm, uint8_t *info, size_t info_len, uint8_t *okm, size_t okm_len)
Derive the key using HKDF() function.
void oscore_log_hex_value(coap_log_t level, const char *name, coap_bin_const_t *value)
int oscore_hmac_hash(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, coap_bin_const_t *data, coap_bin_const_t **hmac)
Derive the hmac hash using HMAC-HASH() function.
int oscore_hkdf_extract(cose_hkdf_alg_t hkdf_alg, coap_bin_const_t *salt, coap_bin_const_t *ikm, coap_bin_const_t **hkdf_extract)
Derive the pseudorandom key using HKDF-Extract() function.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
static void dummy(void)
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67