libcoap 4.3.5-develop-5347a62
Loading...
Searching...
No Matches
oscore_cose.h
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
45
46#ifndef _OSCORE_COSE_H
47#define _OSCORE_COSE_H
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
59
60/* cose curves */
61
62typedef enum {
63 COSE_CURVE_P_256 = 1, /* NIST P-256 known as secp256r1 */
64 COSE_CURVE_X25519 = 4, /* used with ECDH only */
65 COSE_CURVE_X448 = 5, /* used with ECDH only */
66 COSE_CURVE_ED25519 = 6, /* used with EdDSA only */
67 COSE_CURVE_ED448 = 7, /* used with EdDSA only */
68 COSE_CURVE_SECP256K1 = 8, /* SECG secp256k1 curve */
70
71typedef enum {
73 COSE_KTY_OKP = 1, /* Octet Key Pair kty + crv RFC9053 */
74 COSE_KTY_EC2 = 2, /* Elliptic Curve Keys
75 w/ x- and y-coordinate pair kty + crv RFC9053 */
76 COSE_KTY_RSA = 3, /* RSA Key kty RFC8230 RFC9053 */
77 COSE_KTY_SYMMETRIC = 4, /* Symmetric Ke ys kty RFC9053 */
79
80#define COSE_ALGORITHM_ED25519_SIG_LEN 64
81#define COSE_ALGORITHM_ED25519_PRIV_KEY_LEN 32
82#define COSE_ALGORITHM_ED25519_PUB_KEY_LEN 32
83
84#define COSE_ALGORITHM_AES_CCM_64_64_128_KEY_LEN 16
85#define COSE_ALGORITHM_AES_CCM_64_64_128_NONCE_LEN 7
86#define COSE_ALGORITHM_AES_CCM_64_64_128_TAG_LEN 8
87
88#define COSE_ALGORITHM_AES_CCM_16_64_128_KEY_LEN 16
89#define COSE_ALGORITHM_AES_CCM_16_64_128_NONCE_LEN 13
90#define COSE_ALGORITHM_AES_CCM_16_64_128_TAG_LEN 8
91
92#define COSE_ALGORITHM_AES_CCM_64_128_128_KEY_LEN 16
93#define COSE_ALGORITHM_AES_CCM_64_128_128_NONCE_LEN 7
94#define COSE_ALGORITHM_AES_CCM_64_128_128_TAG_LEN 16
95
96#define COSE_ALGORITHM_AES_CCM_16_128_128_KEY_LEN 16
97#define COSE_ALGORITHM_AES_CCM_16_128_128_NONCE_LEN 13
98#define COSE_ALGORITHM_AES_CCM_16_128_128_TAG_LEN 16
99
100#define COSE_ALGORITHM_ES256_PRIV_KEY_LEN 24
101#define COSE_ALGORITHM_ES256_PUB_KEY_LEN 32
102#define COSE_ALGORITHM_ES256_SIGNATURE_LEN 64
103#define COSE_ALGORITHM_ES256_HASH_LEN 32
104
105#define COSE_ALGORITHM_ES384_PRIV_KEY_LEN 24
106#define COSE_ALGORITHM_ES384_PUB_KEY_LEN 32
107#define COSE_ALGORITHM_ES384_SIGNATURE_LEN 64
108#define COSE_ALGORITHM_ES384_HASH_LEN 48
109
110#define COSE_ALGORITHM_ES512_PRIV_KEY_LEN 24
111#define COSE_ALGORITHM_ES512_PUB_KEY_LEN 32
112#define COSE_ALGORITHM_ES512_SIGNATURE_LEN 64
113#define COSE_ALGORITHM_ES512_HASH_LEN 64
114
115#define COSE_ALGORITHM_ECDH_PRIV_KEY_LEN 32
116#define COSE_ALGORITHM_ECDH_PUB_KEY_LEN 32
117
118#define COSE_ALGORITHM_SHA_512_LEN 64
119#define COSE_ALGORITHM_SHA_512_256_LEN 32
120#define COSE_ALGORITHM_SHA_256_256_LEN 32
121#define COSE_ALGORITHM_SHA_256_64_LEN 8
122
123#define COSE_ALGORITHM_HMAC256_64_HASH_LEN 16
124#define COSE_ALGORITHM_HMAC256_256_HASH_LEN 32
125#define COSE_ALGORITHM_HMAC384_384_HASH_LEN 48
126#define COSE_ALGORITHM_HMAC512_512_HASH_LEN 64
127
128/* cose algorithms */
158
159/* cose HMAC specific algorithms */
166
167/* cose HKDF specific algorithms */
172
173const char *cose_get_curve_name(cose_curve_t id, char *buffer, size_t buflen);
177
178const char *cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen);
179cose_alg_t cose_get_alg_id(const char *name);
180
181const char *cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char *buffer,
182 size_t buflen);
183
185 cose_hmac_alg_t *hmac_alg);
186/* parameter value functions */
187
188/* return tag length belonging to cose algorithm */
189size_t cose_tag_len(cose_alg_t cose_alg);
190
191/* return hash length belonging to cose algorithm */
192size_t cose_hash_len(cose_alg_t cose_alg);
193
194/* return nonce length belonging to cose algorithm */
195size_t cose_nonce_len(cose_alg_t cose_alg);
196
197/* return key length belonging to cose algorithm */
198size_t cose_key_len(cose_alg_t cose_alg);
199
200/* COSE Encrypt0 Struct */
219
220#if COAP_OSCORE_GROUP_SUPPORT
221/* COSE Sign1 Struct */
222typedef struct cose_sign1_t {
223
224 cose_alg_t alg;
225 cose_curve_t alg_param;
226 int alg_kty;
227
228 coap_crypto_pri_key_t *private_key;
229
230 coap_crypto_cred_t *public_cred;
231
232 coap_bin_const_t ciphertext;
233
234 coap_bin_const_t sigstructure;
235
236 coap_binary_t signature;
237} cose_sign1_t;
238#endif /* COAP_OSCORE_GROUP_SUPPORT */
239
240/* Return length */
241size_t cose_encrypt0_encode(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size);
242
243/*Return status */
244int cose_encrypt0_decode(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size);
245
246/* Initiate a new COSE Encrypt0 object. */
248
250
251void cose_encrypt0_set_plaintext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size);
252
254 uint8_t *buffer,
255 size_t size);
256
257/* Return length */
259
261 coap_bin_const_t *partial_iv);
262
264
266
267/* Return length */
268size_t cose_encrypt0_get_key_id(cose_encrypt0_t *ptr, const uint8_t **buffer);
269
271 coap_bin_const_t *external_aad);
272
274
275/* Return length */
277 const uint8_t **buffer);
278
280 coap_bin_const_t *kid_context);
281
282/* Returns 1 if successful, 0 if key is of incorrect length. */
284
286
288 uint8_t *ciphertext_buffer,
289 size_t ciphertext_len);
290
292 uint8_t *plaintext_buffer,
293 size_t plaintext_len);
294
295#if COAP_OSCORE_GROUP_SUPPORT
296/* Curve signature functions */
297
298void cose_sign1_init(cose_sign1_t *ptr);
299
300void cose_sign1_set_alg(cose_sign1_t *ptr, int alg, int alg_param, int alg_kty);
301
302void cose_sign1_set_ciphertext(cose_sign1_t *ptr, uint8_t *buffer, size_t size);
303
304void cose_sign1_set_public_cred(cose_sign1_t *ptr,
305 coap_crypto_cred_t *buffer);
306
307void cose_sign1_set_private_key(cose_sign1_t *ptr,
308 coap_crypto_pri_key_t *buffer);
309
310/* Return length */
311int cose_sign1_get_signature(cose_sign1_t *ptr, const uint8_t **buffer);
312
313void cose_sign1_set_signature(cose_sign1_t *ptr, uint8_t *buffer, size_t size);
314
315int cose_sign1_sign(cose_sign1_t *ptr);
316
317void cose_sign1_set_sigstructure(cose_sign1_t *ptr, uint8_t *buffer, size_t size);
318
319int cose_sign1_verify(cose_sign1_t *ptr);
320#endif /* COAP_OSCORE_GROUP_SUPPORT */
321
323
324#ifdef __cplusplus
325}
326#endif
327
328#endif /* _OSCORE_COSE_H */
struct coap_crypto_cred_t coap_crypto_cred_t
struct coap_crypto_pri_key_t coap_crypto_pri_key_t
void cose_encrypt0_set_plaintext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
int cose_encrypt0_get_plaintext(cose_encrypt0_t *ptr, uint8_t **buffer)
const char * cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char *buffer, size_t buflen)
cose_key_type_t cose_get_curve_key_type(cose_curve_t curve)
int cose_encrypt0_set_key(cose_encrypt0_t *ptr, coap_bin_const_t *key)
size_t cose_nonce_len(cose_alg_t cose_alg)
coap_bin_const_t * cose_get_curve_bin_oid(cose_curve_t curve)
cose_alg_t cose_get_alg_id(const char *name)
void cose_encrypt0_set_kid_context(cose_encrypt0_t *ptr, coap_bin_const_t *kid_context)
cose_curve_t cose_get_curve_id(const char *name)
size_t cose_key_len(cose_alg_t cose_alg)
size_t cose_encrypt0_get_key_id(cose_encrypt0_t *ptr, const uint8_t **buffer)
const char * cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen)
int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg)
coap_bin_const_t cose_encrypt0_get_partial_iv(cose_encrypt0_t *ptr)
cose_hkdf_alg_t
size_t cose_encrypt0_get_kid_context(cose_encrypt0_t *ptr, const uint8_t **buffer)
void cose_encrypt0_set_ciphertext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
size_t cose_hash_len(cose_alg_t cose_alg)
int cose_encrypt0_decrypt(cose_encrypt0_t *ptr, uint8_t *plaintext_buffer, size_t plaintext_len)
size_t cose_tag_len(cose_alg_t cose_alg)
void cose_encrypt0_set_aad(cose_encrypt0_t *ptr, coap_bin_const_t *aad)
cose_hmac_alg_t
int cose_encrypt0_decode(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
cose_curve_t
Definition oscore_cose.h:62
int cose_encrypt0_encrypt(cose_encrypt0_t *ptr, uint8_t *ciphertext_buffer, size_t ciphertext_len)
void cose_encrypt0_set_partial_iv(cose_encrypt0_t *ptr, coap_bin_const_t *partial_iv)
cose_alg_t
const char * cose_get_curve_name(cose_curve_t id, char *buffer, size_t buflen)
cose_key_type_t
Definition oscore_cose.h:71
size_t cose_encrypt0_encode(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size)
void cose_encrypt0_set_external_aad(cose_encrypt0_t *ptr, coap_bin_const_t *external_aad)
void cose_encrypt0_init(cose_encrypt0_t *ptr)
void cose_encrypt0_set_alg(cose_encrypt0_t *ptr, uint8_t alg)
void cose_encrypt0_set_key_id(cose_encrypt0_t *ptr, coap_bin_const_t *key_id)
void cose_encrypt0_set_nonce(cose_encrypt0_t *ptr, coap_bin_const_t *nonce)
@ COSE_HKDF_ALG_HKDF_SHA_256
@ COSE_HKDF_ALG_HKDF_SHA_512
@ COSE_HMAC_ALG_HMAC384_384
@ COSE_HMAC_ALG_HMAC256_256
@ COSE_HMAC_ALG_HMAC512_512
@ COSE_HMAC_ALG_HMAC256_64
@ COSE_CURVE_X25519
Definition oscore_cose.h:64
@ COSE_CURVE_ED448
Definition oscore_cose.h:67
@ COSE_CURVE_P_256
Definition oscore_cose.h:63
@ COSE_CURVE_SECP256K1
Definition oscore_cose.h:68
@ COSE_CURVE_ED25519
Definition oscore_cose.h:66
@ COSE_CURVE_X448
Definition oscore_cose.h:65
@ COSE_ALGORITHM_HMAC256_256
@ COSE_ALGORITHM_AES_CCM_16_128_256
@ COSE_ALGORITHM_ECDH_SS_HKDF_256
@ COSE_ALGORITHM_HMAC512_512
@ COSE_ALGORITHM_SHA_256_64
@ COSE_ALGORITHM_SHA_512_256
@ COSE_ALGORITHM_ES384
@ COSE_ALGORITHM_AES_CCM_64_64_128
@ COSE_ALGORITHM_CHACHA20_P1035
@ COSE_ALGORITHM_HKDF_SHA_512
@ COSE_ALGORITHM_AES_CCM_16_128_128
@ COSE_ALGORITHM_AES_CCM_64_128_256
@ COSE_ALGORITHM_SHA_256_256
@ COSE_ALGORITHM_SHA_1
@ COSE_ALGORITHM_EDDSA
@ COSE_ALGORITHM_HMAC256_64
@ COSE_ALGORITHM_ES256
@ COSE_ALGORITHM_AES_CCM_64_64_256
@ COSE_ALGORITHM_HKDF_SHA_256
@ COSE_ALGORITHM_AES_CCM_16_64_128
@ COSE_ALGORITHM_ES512
@ COSE_ALGORITHM_HMAC384_384
@ COSE_ALGORITHM_SHA_512
@ COSE_ALGORITHM_AES_CCM_64_128_128
@ COSE_ALGORITHM_AES_CCM_16_64_256
@ COSE_ALGORITHM_ES256K
@ COSE_ALGORITHM_SHA_384
@ COSE_KTY_SYMMETRIC
Definition oscore_cose.h:77
@ COSE_KTY_OKP
Definition oscore_cose.h:73
@ COSE_KTY_UNKNOWN
Definition oscore_cose.h:72
@ COSE_KTY_EC2
Definition oscore_cose.h:74
@ COSE_KTY_RSA
Definition oscore_cose.h:76
struct coap_binary_t coap_binary_t
CoAP binary data definition.
struct coap_bin_const_t coap_bin_const_t
CoAP binary data definition with const data.
CoAP binary data definition with const data.
Definition coap_str.h:65
Holds credential definitions.
Holds private key definitions.
coap_bin_const_t aad
coap_bin_const_t key
coap_bin_const_t ciphertext
coap_bin_const_t plaintext
coap_bin_const_t partial_iv
coap_bin_const_t kid_context
coap_bin_const_t nonce
coap_bin_const_t external_aad
coap_bin_const_t key_id
coap_bin_const_t oscore_option
uint8_t partial_iv_data[8]
cose_alg_t alg