libcoap 4.3.5-develop-490e4e0
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
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 /* Compose T(1) */
122 memcpy(aggregate_buffer, info, info_len);
123 aggregate_buffer[info_len] = 0x01;
124
125 data.s = aggregate_buffer;
126 data.length = info_len + 1;
127 if (!oscore_hmac_hash(hmac_alg, prk, &data, &hkdf))
128 goto fail;
129 memcpy(&out_buffer[0], hkdf->s, hkdf->length);
131
132 /* Compose T(2) -> T(N) */
133 memcpy(aggregate_buffer, &(out_buffer[0]), 32);
134 for (i = 1; i < N; i++) {
135 memcpy(&(aggregate_buffer[32]), info, info_len);
136 aggregate_buffer[32 + info_len] = (uint8_t)(i + 1);
137 data.s = aggregate_buffer;
138 data.length = 32 + info_len + 1;
139 if (!oscore_hmac_hash(hmac_alg, prk, &data, &hkdf))
140 goto fail;
141 memcpy(&out_buffer[i * 32], hkdf->s, hkdf->length);
143 memcpy(aggregate_buffer, &(out_buffer[i * 32]), 32);
144 }
145 memcpy(okm, out_buffer, okm_len);
146 coap_free_type(COAP_STRING, out_buffer);
147 coap_free_type(COAP_STRING, aggregate_buffer);
148 return 1;
149
150fail:
151 coap_free_type(COAP_STRING, out_buffer);
152 coap_free_type(COAP_STRING, aggregate_buffer);
153 return 0;
154}
155
156/*
157 * return 0 fail
158 * 1 OK
159 */
160int
162 coap_bin_const_t *salt,
163 coap_bin_const_t *ikm,
164 uint8_t *info,
165 size_t info_len,
166 uint8_t *okm,
167 size_t okm_len) {
168 int ret;
169 coap_bin_const_t *hkdf_extract = NULL;
170 if (!oscore_hkdf_extract(hkdf_alg, salt, ikm, &hkdf_extract))
171 return 0;
172 ret =
173 oscore_hkdf_expand(hkdf_alg, hkdf_extract, info, info_len, okm, okm_len);
174 coap_delete_bin_const(hkdf_extract);
175 return ret;
176}
177
178#else /* ! COAP_OSCORE_SUPPORT */
179
180#ifdef __clang__
181/* Make compilers happy that do not like empty modules. As this function is
182 * never used, we ignore -Wunused-function at the end of compiling this file
183 */
184#pragma GCC diagnostic ignored "-Wunused-function"
185#endif
186static inline void
187dummy(void) {
188}
189
190#endif /* ! COAP_OSCORE_SUPPORT */
Library specific build wrapper for coap_internal.h.
@ COAP_STRING
Definition coap_mem.h:34
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().
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.
#define coap_log_warn(...)
Definition coap_debug.h:108
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
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.
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:120
static void dummy(void)
CoAP binary data definition with const data.
Definition coap_str.h:67
size_t length
length of binary data
Definition coap_str.h:68
const uint8_t * s
read-only binary data
Definition coap_str.h:69