49#if COAP_OSCORE_SUPPORT
57 size_t rem_size =
sizeof(buf);
68 memcpy(result, buf, *len);
78 size_t rem_size =
sizeof(buf);
86 memcpy(result, buf, *len);
90#if COAP_OSCORE_GROUP_SUPPORT
95extract_param(
const uint8_t **oscore_cbor_array,
size_t *array_len) {
102 for (uint16_t i = 0; i < arr_size; i++) {
116extract_type(
const uint8_t **oscore_cbor_array,
size_t *array_len) {
164 const uint8_t *oscore_option,
165 size_t oscore_option_len,
167#
if COAP_OSCORE_GROUP_SUPPORT
170 uint8_t *external_aad_ptr,
171 size_t external_aad_size) {
172 size_t external_aad_len = 0;
173 size_t rem_size = external_aad_size;
176#if ! COAP_OSCORE_GROUP_SUPPORT
178 (void)oscore_option_len;
182#if COAP_OSCORE_GROUP_SUPPORT
186 external_aad_len += len;
191 external_aad_len += len;
192#if COAP_OSCORE_GROUP_SUPPORT
199 external_aad_len += len;
201#if COAP_OSCORE_GROUP_SUPPORT
207 external_aad_len += len;
212 external_aad_len += len;
213#if COAP_OSCORE_GROUP_SUPPORT
223 external_aad_len += len;
228 external_aad_len += len;
232 external_aad_len += len;
235 if (ctx->alg_group_enc) {
238 ctx->alg_group_enc)) == 0)
240 external_aad_len += len;
244 external_aad_len += len;
247 if (ctx->alg_signature) {
250 external_aad_len += len;
254 external_aad_len += len;
257 if (ctx->alg_pairwise_key_agreement) {
260 ctx->alg_pairwise_key_agreement)) == 0)
262 external_aad_len += len;
266 external_aad_len += len;
277 external_aad_len += len;
285 external_aad_len += len;
291 external_aad_len += len;
293#if COAP_OSCORE_GROUP_SUPPORT
306 if (sender_cred->cred_type == OSCORE_AUTH_CRED_CCS) {
319 if (ctx->gm_cred->cred_type == OSCORE_AUTH_CRED_CCS) {
323 ctx->gm_cred->auth_cred->s,
324 ctx->gm_cred->auth_cred->length);
329 ctx->gm_cred->pub_der->s,
330 ctx->gm_cred->pub_der->length);
335 return external_aad_len;
338 coap_log_info(
"oscore_prepare_e_aad: Buffer size too small: %" PRIuS "\n", external_aad_size);
347 size_t option_buf_len,
350 uint8_t appendix_b_2) {
354#if ! COAP_OSCORE_GROUP_SUPPORT
360 if (option_buf_len > 255)
361 option_buf_len = 255;
362#if COAP_OSCORE_GROUP_SUPPORT
363 if (group_flag == 1) {
364 option_buffer[0] = 0x20;
365 cose->group_flag = 1;
368 option_buffer[0] = 0;
370 rem_space = option_buf_len - 1;
374 if (rem_space < cose->partial_iv.length)
377 memcpy(&(option_buffer[offset]),
387 uint8_t *ptr = &option_buffer[offset+1];
391 option_buffer[0] |= 0x10;
395 offset += option_buffer[offset] + 1;
399 if (rem_space < 1 + cose->kid_context.length)
401 option_buffer[0] |= 0x10;
405 memcpy(&(option_buffer[offset]),
414 option_buffer[0] |= 0x08;
415 if (rem_space < cose->key_id.length)
424 if (offset == 1 && option_buffer[0] == 0) {
444 uint8_t partial_iv_len;
453 partial_iv_len = (opt_value[0] & 0x07);
454 if (option_len > 255 || partial_iv_len == 6 || partial_iv_len == 7 ||
455 (opt_value[0] & 0xC0) != 0) {
459 if ((opt_value[0] & 0x20) != 0) {
460#if COAP_OSCORE_GROUP_SUPPORT
461 cose->group_flag = 1;
463 cose->group_flag = 0;
469 if (partial_iv_len != 0) {
471 if (offset + partial_iv_len > option_len) {
474 partial_iv.
s = &(opt_value[offset]);
475 partial_iv.
length = partial_iv_len;
477 offset += partial_iv_len;
480 if ((opt_value[0] & 0x10) != 0) {
483 if (offset >= option_len)
485 kid_context.
length = opt_value[offset];
487 if (offset + kid_context.
length > option_len) {
490 kid_context.
s = &(opt_value[offset]);
492 offset = offset + kid_context.
length;
495 if ((opt_value[0] & 0x08) != 0) {
498 key_id.
length = option_len - offset;
499 if ((
int)key_id.
length < 0) {
502 key_id.
s = &(opt_value[offset]);
508#if COAP_OSCORE_GROUP_SUPPORT
511oscore_populate_sign(cose_sign1_t *sign,
518 cose_sign1_set_alg(sign,
520 extract_param(&sign_param.
s, &sign_param.
length),
521 extract_type(&sign_type.
s, &sign_type.
length));
524 cose_sign1_set_private_key(sign, private_key);
526 cose_sign1_set_public_cred(sign, cred);
535oscore_prepare_sig_structure(uint8_t *sig_ptr,
537 const uint8_t *e_aad_buffer,
542 size_t rem_size = sig_size;
543 char countersig0[] =
"CounterSignature0";
551 strlen(countersig0));
572 size_t external_aad_len,
577 size_t rem_size = aad_size;
578 char encrypt0[] =
"Encrypt0";
600 external_aad_len)) == 0)
624 memset(buffer, 0, size);
632 memcpy(&(buffer[((size - 5) - tmp_len)]),
636 buffer[0] = (uint8_t)tmp_len;
647 memcpy(&(buffer[size - tmp_len]),
652 for (
int i = 0; i < size; i++) {
653 buffer[i] = buffer[i] ^ (uint8_t)ctx->
common_iv->
s[i];
664 uint64_t incoming_seq =
668 coap_log_warn(
"OSCORE Replay protection, SEQ larger than SEQ_MAX.\n");
683 }
else if (incoming_seq > ctx->
last_seq) {
685 uint64_t shift = incoming_seq - ctx->
last_seq;
694 }
else if (incoming_seq == ctx->
last_seq) {
699 uint64_t shift = ctx->
last_seq - incoming_seq;
704 "OSCORE: Replay protection, SEQ outside of replay window (%" PRIu64
710 pattern = 1ULL << shift;
766#pragma GCC diagnostic ignored "-Wunused-function"
Library specific build wrapper for coap_internal.h.
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.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
#define coap_log_oscore(...)
#define coap_log_info(...)
#define coap_log_warn(...)
size_t oscore_cbor_put_text(uint8_t **buffer, size_t *buf_size, const char *text, size_t text_len)
uint64_t oscore_cbor_get_element_size(const uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value)
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_size)
size_t oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value)
size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len)
uint8_t oscore_cbor_get_number(const uint8_t **data, size_t *buf_size, int64_t *value)
size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements)
void cose_encrypt0_set_kid_context(cose_encrypt0_t *ptr, coap_bin_const_t *kid_context)
void cose_encrypt0_set_partial_iv(cose_encrypt0_t *ptr, coap_bin_const_t *partial_iv)
void cose_encrypt0_set_key_id(cose_encrypt0_t *ptr, coap_bin_const_t *key_id)
size_t oscore_prepare_aad(const uint8_t *external_aad_buffer, size_t external_aad_len, uint8_t *aad_buffer, size_t aad_size)
uint8_t oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose)
int oscore_decode_option_value(const uint8_t *option_value, size_t option_len, cose_encrypt0_t *cose)
uint8_t oscore_increment_sender_seq(oscore_ctx_t *ctx)
void oscore_roll_back_seq(oscore_recipient_ctx_t *ctx)
uint8_t * oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len)
void oscore_generate_nonce(cose_encrypt0_t *ptr, oscore_ctx_t *ctx, uint8_t *buffer, uint8_t size)
uint8_t * oscore_cs_params(int8_t param, int8_t param_type, size_t *len)
size_t oscore_prepare_e_aad(oscore_ctx_t *ctx, cose_encrypt0_t *cose, const uint8_t *oscore_option, size_t oscore_option_len, coap_crypto_cred_t *sender_cred, uint8_t *external_aad_ptr, size_t external_aad_size)
ssize_t oscore_encode_option_value(uint8_t *option_buffer, size_t option_buf_len, cose_encrypt0_t *cose, uint8_t group_flag, uint8_t appendix_b_2)
Encode the OSCORE option.
@ OSCORE_MODE_SINGLE
Vanilla RFC 8613 support.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
Holds credential definitions.
coap_bin_const_t * auth_cred
Authentication Credentials of type type.
coap_bin_const_t * pub_der
DER Value of the public key abstracted from key_tls.
Holds private key definitions.
coap_bin_const_t partial_iv
coap_bin_const_t kid_context
coap_bin_const_t oscore_option
uint32_t replay_window_size
coap_bin_const_t * common_iv
Derived from Master Secret, Master Salt, and ID Context.
oscore_sender_ctx_t * sender_context
cose_alg_t aead_alg
Set to one of COSE_ALGORITHM_AES*.
coap_bin_const_t * id_context
contains GID in case of group
uint64_t rollback_sliding_window
uint8_t rollback_initial_state
uint64_t rollback_last_seq
uint64_t seq
Sender Sequence Number.