diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1f090ec0..aa9f6c2d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ repos: rev: v0.9.0 hooks: - id: astyle_py - files: '^(man).*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$' + files: '^(man|include/oscore|src/oscore|src/coap_oscore).*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$' exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$' args: ['--style=google', '--align-pointer=name', @@ -14,9 +14,10 @@ repos: '--break-one-line-headers', '--break-return-type', '--convert-tabs', + '--indent-continuation=2', '--indent-preproc-define', '--indent=spaces=2', - '--max-code-length=120', + '--max-code-length=100', '--max-continuation-indent=60', '--min-conditional-indent=0', '--pad-header', diff --git a/include/oscore/oscore.h b/include/oscore/oscore.h index 601acb8e..8f0ebea5 100644 --- a/include/oscore/oscore.h +++ b/include/oscore/oscore.h @@ -72,8 +72,7 @@ uint8_t *oscore_cs_params(int8_t param, int8_t param_type, size_t *len); /* oscore_cs_key_params * returns cbor array [paramtype, param] */ -uint8_t * -oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len); +uint8_t *oscore_cs_key_params(cose_curve_t param, int8_t param_type, size_t *len); /* * oscore_encode_option_value diff --git a/include/oscore/oscore_cbor.h b/include/oscore/oscore_cbor.h index dc7e3f77..84532de8 100644 --- a/include/oscore/oscore_cbor.h +++ b/include/oscore/oscore_cbor.h @@ -82,30 +82,24 @@ size_t oscore_cbor_put_text(uint8_t **buffer, const char *text, size_t text_len); -size_t -oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements); +size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements); size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len); -size_t -oscore_cbor_put_map(uint8_t **buffer, size_t *buf_size, size_t elements); +size_t oscore_cbor_put_map(uint8_t **buffer, size_t *buf_size, size_t elements); -size_t -oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value); +size_t oscore_cbor_put_number(uint8_t **buffer, size_t *buf_size, int64_t value); -size_t -oscore_cbor_put_simple_value(uint8_t **buffer, size_t *buf_size, uint8_t value); +size_t oscore_cbor_put_simple_value(uint8_t **buffer, size_t *buf_size, uint8_t value); -size_t -oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value); +size_t oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value); size_t oscore_cbor_put_tag(uint8_t **buffer, size_t *buf_size, uint64_t value); -size_t -oscore_cbor_put_negative(uint8_t **buffer, size_t *buf_size, int64_t value); +size_t oscore_cbor_put_negative(uint8_t **buffer, size_t *buf_size, int64_t value); uint8_t oscore_cbor_get_next_element(const uint8_t **buffer, size_t *buf_size); @@ -127,7 +121,7 @@ uint64_t oscore_cbor_get_unsigned_integer(const uint8_t **buffer, size_t *buf_size); void oscore_cbor_get_string(const uint8_t **buffer, size_t *buf_size, - char *str, size_t size); + char *str, size_t size); void oscore_cbor_get_array(const uint8_t **buffer, size_t *buf_size, uint8_t *arr, size_t size); diff --git a/include/oscore/oscore_cose.h b/include/oscore/oscore_cose.h index dd1e2ac3..df78a462 100644 --- a/include/oscore/oscore_cose.h +++ b/include/oscore/oscore_cose.h @@ -167,13 +167,13 @@ typedef enum { COSE_HKDF_ALG_HKDF_SHA_256 = -10, } cose_hkdf_alg_t; -const char* cose_get_curve_name(cose_curve_t id, char* buffer, size_t buflen); +const char *cose_get_curve_name(cose_curve_t id, char *buffer, size_t buflen); cose_curve_t cose_get_curve_id(const char *name); -const char* cose_get_alg_name(cose_alg_t id, char* buffer, size_t buflen); +const char *cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen); cose_alg_t cose_get_alg_id(const char *name); -const char* cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char* buffer, +const char *cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char *buffer, size_t buflen); int cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, @@ -221,8 +221,7 @@ void cose_encrypt0_init(cose_encrypt0_t *ptr); void cose_encrypt0_set_alg(cose_encrypt0_t *ptr, uint8_t alg); -void -cose_encrypt0_set_plaintext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size); +void cose_encrypt0_set_plaintext(cose_encrypt0_t *ptr, uint8_t *buffer, size_t size); void cose_encrypt0_set_ciphertext(cose_encrypt0_t *ptr, uint8_t *buffer, diff --git a/src/coap_oscore.c b/src/coap_oscore.c index 1ecd374e..1eab5e4d 100644 --- a/src/coap_oscore.c +++ b/src/coap_oscore.c @@ -36,8 +36,7 @@ coap_oscore_initiate(coap_session_t *session, coap_oscore_conf_t *oscore_conf) { oscore_ctx_t *osc_ctx; if (oscore_conf->recipient_id_count == 0) { - coap_log_warn( - "OSCORE: Recipient ID must be defined for a client\n"); + coap_log_warn("OSCORE: Recipient ID must be defined for a client\n"); return 0; } if (oscore_conf->rfc8613_b_2) { @@ -146,7 +145,7 @@ coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu) { uint8_t *keep_proxy_uri = NULL; if ((option = - coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter)) == NULL) + coap_check_option(pdu, COAP_OPTION_PROXY_URI, &opt_iter)) == NULL) return 1; /* Need to break down into the component parts, but keep data safe */ @@ -159,10 +158,9 @@ coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu) { if (coap_split_proxy_uri(keep_proxy_uri, coap_opt_length(option), &uri) < 0 || uri.scheme >= COAP_URI_SCHEME_LAST) { - coap_log_warn( - "Proxy URI '%.*s' not decodable\n", - coap_opt_length(option), - (const char *)coap_opt_value(option)); + coap_log_warn("Proxy URI '%.*s' not decodable\n", + coap_opt_length(option), + (const char *)coap_opt_value(option)); goto error; } if (!coap_remove_option(pdu, COAP_OPTION_PROXY_URI)) @@ -173,8 +171,7 @@ coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu) { uri.host.length, uri.host.s)) goto error; - if (uri.port != (coap_uri_scheme_is_secure(&uri) ? COAPS_DEFAULT_PORT - : COAP_DEFAULT_PORT) && + if (uri.port != (coap_uri_scheme_is_secure(&uri) ? COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT) && !coap_insert_option(pdu, COAP_OPTION_URI_PORT, coap_encode_var_safe(option_value_buffer, @@ -315,8 +312,8 @@ coap_oscore_new_pdu_encrypted(coap_session_t *session, coap_log_debug("PDU to encrypt\n"); coap_show_pdu(COAP_LOG_DEBUG, pdu); osc_pdu = coap_pdu_init(pdu->type == COAP_MESSAGE_NON && - session->b_2_step != COAP_OSCORE_B_2_NONE ? - COAP_MESSAGE_CON : pdu->type, + session->b_2_step != COAP_OSCORE_B_2_NONE ? + COAP_MESSAGE_CON : pdu->type, code, pdu->mid, pdu->used_size + coap_oscore_overhead(session, pdu)); @@ -553,7 +550,7 @@ coap_oscore_new_pdu_encrypted(coap_session_t *session, * coap_rebuild_pdu_for_proxy() before calling * coap_oscore_new_pdu_encrypted() */ - assert (0); + assert(0); break; default: /* Make as Inner option */ @@ -597,9 +594,8 @@ coap_oscore_new_pdu_encrypted(coap_session_t *session, ciphertext_buffer, plain_pdu->used_size + AES_CCM_TAG); if ((int)ciphertext_len <= 0) { - coap_log_warn( - "OSCORE: Encryption Failure, result code: %d \n", - (int)ciphertext_len); + coap_log_warn("OSCORE: Encryption Failure, result code: %d \n", + (int)ciphertext_len); goto error; } assert(ciphertext_len < OSCORE_CRYPTO_BUFFER_SIZE); @@ -689,7 +685,7 @@ coap_oscore_new_pdu_encrypted(coap_session_t *session, association->sent_pdu = NULL; } } else if (!oscore_new_association(session, - session->b_2_step != COAP_OSCORE_B_2_NONE ? pdu : NULL, + session->b_2_step != COAP_OSCORE_B_2_NONE ? pdu : NULL, &pdu_token, rcp_ctx, &cose->aad, @@ -724,12 +720,12 @@ build_and_send_error_pdu(coap_session_t *session, unsigned char buf[4]; token = coap_pdu_get_token(rcvd); - err_pdu = coap_pdu_init(rcvd->type == COAP_MESSAGE_NON ? COAP_MESSAGE_NON - : COAP_MESSAGE_ACK, + err_pdu = coap_pdu_init(rcvd->type == COAP_MESSAGE_NON ? COAP_MESSAGE_NON : + COAP_MESSAGE_ACK, code, rcvd->mid, token.length + 2 + 8 + - (diagnostic ? strlen(diagnostic) : 0)); + (diagnostic ? strlen(diagnostic) : 0)); if (!err_pdu) return; coap_add_token(err_pdu, token.length, token.s); @@ -916,12 +912,11 @@ coap_oscore_decrypt_pdu(coap_session_t *session, const uint8_t *ptr; size_t length; /* Appendix B.2 protocol check - Is the recipient key_id known */ - osc_ctx = oscore_find_context( - session->context, - cose->key_id, - NULL, - session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL, - &rcp_ctx); + osc_ctx = oscore_find_context(session->context, + cose->key_id, + NULL, + session->oscore_r2 != 0 ? (uint8_t *)&session->oscore_r2 : NULL, + &rcp_ctx); ptr = cose->kid_context.s; length = cose->kid_context.length; if (ptr && osc_ctx && osc_ctx->rfc8613_b_2 && @@ -1009,7 +1004,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, incoming_seq = coap_decode_var_bytes8(cose->partial_iv.s, cose->partial_iv.length); rcp_ctx->last_seq = incoming_seq; - } else /* !coap_request */ { + } else { /* !coap_request */ /* * 8.4 Step 2 * Decompress COSE object @@ -1171,7 +1166,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, } /* So association is not released when handling decrypt */ association = NULL; - } else /* ! coap_request */ { + } else { /* ! coap_request */ /* Need to do nonce before AAD because of different partial_iv */ /* * 8.4 Step 4. @@ -1186,14 +1181,13 @@ coap_oscore_decrypt_pdu(coap_session_t *session, if (rcp_ctx->initial_state == 0 && !oscore_validate_sender_seq(rcp_ctx, cose)) { - coap_log_warn("OSCORE: Replayed or old message\n"); + coap_log_warn("OSCORE: Replayed or old message\n"); goto error; } last_seq = - coap_decode_var_bytes8(cose->partial_iv.s, cose->partial_iv.length); + coap_decode_var_bytes8(cose->partial_iv.s, cose->partial_iv.length); if (rcp_ctx->last_seq>= OSCORE_SEQ_MAX) { - coap_log_warn( - "OSCORE Replay protection, SEQ larger than SEQ_MAX.\n"); + coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n"); goto error; } if (last_seq > rcp_ctx->last_seq) @@ -1320,9 +1314,8 @@ coap_oscore_decrypt_pdu(coap_session_t *session, pltxt_size = cose_encrypt0_decrypt(cose, plain_pdu->token, encrypt_len - tag_len); if (pltxt_size <= 0) { - coap_log_warn( - "OSCORE: Decryption Failure, result code: %d \n", - (int)pltxt_size); + coap_log_warn("OSCORE: Decryption Failure, result code: %d \n", + (int)pltxt_size); if (coap_request) { build_and_send_error_pdu(session, pdu, @@ -1425,7 +1418,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, if (sent_pdu) { coap_log_oscore("Appendix B.2 retransmit pdu\n"); if (coap_retransmit_oscore_pdu(session, sent_pdu, NULL) == - COAP_INVALID_MID) + COAP_INVALID_MID) goto error_no_ack; } goto error_no_ack; @@ -1515,7 +1508,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, association->is_observe = 1; association = NULL; } - /* Fall Through */ + /* Fall Through */ default: if (!coap_insert_option(decrypt_pdu, opt_iter.number, @@ -1536,7 +1529,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, plain_pdu->data = &opt_iter.next_option[1]; if (!coap_add_data(decrypt_pdu, plain_pdu->used_size - - (plain_pdu->data - plain_pdu->token), + (plain_pdu->data - plain_pdu->token), plain_pdu->data)) { if (!coap_request) coap_handle_event(session->context, @@ -1576,7 +1569,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session, coap_show_pdu(COAP_LOG_DEBUG, decrypt_pdu); coap_log_oscore("RFC9175 retransmit pdu\n"); if (coap_retransmit_oscore_pdu(session, sent_pdu, opt) == - COAP_INVALID_MID) + COAP_INVALID_MID) goto error_no_ack; goto error_no_ack; } @@ -1613,12 +1606,13 @@ static struct coap_oscore_encoding_t { coap_str_const_t name; coap_oscore_coding_t encoding; } oscore_encoding[] = { - TEXT_MAPPING(ascii, COAP_ENC_ASCII), - TEXT_MAPPING(hex, COAP_ENC_HEX), - TEXT_MAPPING(integer, COAP_ENC_INTEGER), - TEXT_MAPPING(text, COAP_ENC_TEXT), - TEXT_MAPPING(bool, COAP_ENC_BOOL), - {{0, NULL}, COAP_ENC_LAST}}; + TEXT_MAPPING(ascii, COAP_ENC_ASCII), + TEXT_MAPPING(hex, COAP_ENC_HEX), + TEXT_MAPPING(integer, COAP_ENC_INTEGER), + TEXT_MAPPING(text, COAP_ENC_TEXT), + TEXT_MAPPING(bool, COAP_ENC_BOOL), + {{0, NULL}, COAP_ENC_LAST} +}; typedef struct { coap_oscore_coding_t encoding; @@ -1653,7 +1647,7 @@ parse_hex_bin(const char *begin, const char *end) { if (binary == NULL) goto bad_entry; for (i = 0; (i < (size_t)(end - begin)) && isxdigit(begin[i]) && - isxdigit(begin[i + 1]); + isxdigit(begin[i + 1]); i += 2) { binary->s[i / 2] = (hex2char(begin[i]) << 4) + hex2char(begin[i + 1]); } @@ -1716,7 +1710,7 @@ retry: goto bad_entry; for (i = 0; oscore_encoding[i].name.s; i++) { - coap_str_const_t temp = { split - begin, (const uint8_t*)begin }; + coap_str_const_t temp = { split - begin, (const uint8_t *)begin }; if (coap_string_equal(&temp, &oscore_encoding[i].name)) { value->encoding = oscore_encoding[i].encoding; @@ -1771,10 +1765,9 @@ retry: return 1; bad_entry: - coap_log_warn( - "oscore_conf: Unrecognized configuration entry '%.*s'\n", - (int)(end - begin), - begin); + coap_log_warn("oscore_conf: Unrecognized configuration entry '%.*s'\n", + (int)(end - begin), + begin); return 0; } @@ -1790,13 +1783,15 @@ typedef struct oscore_text_mapping_t { /* Naming as per https://www.iana.org/assignments/cose/cose.xhtml#algorithms */ static oscore_text_mapping_t text_aead_alg[] = { - TEXT_MAPPING(AES-CCM-16-64-128, COSE_ALGORITHM_AES_CCM_16_64_128), - TEXT_MAPPING(AES-CCM-16-64-256, COSE_ALGORITHM_AES_CCM_16_64_256), - {{0, NULL}, 0}}; + TEXT_MAPPING(AES-CCM-16-64-128, COSE_ALGORITHM_AES_CCM_16_64_128), + TEXT_MAPPING(AES-CCM-16-64-256, COSE_ALGORITHM_AES_CCM_16_64_256), + {{0, NULL}, 0} +}; static oscore_text_mapping_t text_hkdf_alg[] = { - TEXT_MAPPING(direct+HKDF-SHA-256, COSE_HKDF_ALG_HKDF_SHA_256), - {{0, NULL}, 0}}; + TEXT_MAPPING(direct+HKDF-SHA-256, COSE_HKDF_ALG_HKDF_SHA_256), + {{0, NULL}, 0} +}; static struct oscore_config_t { coap_str_const_t str_keyword; @@ -1804,19 +1799,19 @@ static struct oscore_config_t { size_t offset; oscore_text_mapping_t *text_mapping; } oscore_config[] = { - CONFIG_ENTRY(master_secret, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), - CONFIG_ENTRY(master_salt, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), - CONFIG_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), - CONFIG_ENTRY(id_context, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), - CONFIG_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), - CONFIG_ENTRY(replay_window, COAP_ENC_INTEGER, NULL), - CONFIG_ENTRY(ssn_freq, COAP_ENC_INTEGER, NULL), - CONFIG_ENTRY(aead_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg), - CONFIG_ENTRY(hkdf_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_hkdf_alg), - CONFIG_ENTRY(rfc8613_b_1_2, COAP_ENC_BOOL, NULL), - CONFIG_ENTRY(rfc8613_b_2, COAP_ENC_BOOL, NULL), - CONFIG_ENTRY(break_sender_key, COAP_ENC_BOOL, NULL), - CONFIG_ENTRY(break_recipient_key, COAP_ENC_BOOL, NULL), + CONFIG_ENTRY(master_secret, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), + CONFIG_ENTRY(master_salt, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), + CONFIG_ENTRY(sender_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), + CONFIG_ENTRY(id_context, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), + CONFIG_ENTRY(recipient_id, COAP_ENC_HEX | COAP_ENC_ASCII, NULL), + CONFIG_ENTRY(replay_window, COAP_ENC_INTEGER, NULL), + CONFIG_ENTRY(ssn_freq, COAP_ENC_INTEGER, NULL), + CONFIG_ENTRY(aead_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_aead_alg), + CONFIG_ENTRY(hkdf_alg, COAP_ENC_INTEGER | COAP_ENC_TEXT, text_hkdf_alg), + CONFIG_ENTRY(rfc8613_b_1_2, COAP_ENC_BOOL, NULL), + CONFIG_ENTRY(rfc8613_b_2, COAP_ENC_BOOL, NULL), + CONFIG_ENTRY(break_sender_key, COAP_ENC_BOOL, NULL), + CONFIG_ENTRY(break_recipient_key, COAP_ENC_BOOL, NULL), }; int @@ -1872,8 +1867,7 @@ coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) { value.encoding & oscore_config[i].encoding) { if (coap_string_equal(coap_make_str_const("recipient_id"), &keyword)) { if (value.u.value_bin->length > 7) { - coap_log_warn( - "oscore_conf: Maximum size of recipient_id is 7 bytes\n"); + coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n"); goto error_free_value_bin; } /* Special case as there are potentially multiple entries */ @@ -1881,7 +1875,7 @@ coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) { coap_realloc_type(COAP_STRING, oscore_conf->recipient_id, sizeof(oscore_conf->recipient_id[0]) * - (oscore_conf->recipient_id_count + 1)); + (oscore_conf->recipient_id_count + 1)); if (oscore_conf->recipient_id == NULL) { goto error_free_value_bin; } @@ -1897,10 +1891,9 @@ coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) { &(((char *)oscore_conf)[oscore_config[i].offset]), sizeof(unused_check)); if (unused_check != NULL) { - coap_log_warn( - "oscore_conf: Keyword '%.*s' duplicated\n", - (int)keyword.length, - (const char *)keyword.s); + coap_log_warn("oscore_conf: Keyword '%.*s' duplicated\n", + (int)keyword.length, + (const char *)keyword.s); goto error; } memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]), @@ -1916,7 +1909,7 @@ coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) { case COAP_ENC_TEXT: for (j = 0; oscore_config[i].text_mapping[j].text.s != NULL; j++) { if (coap_string_equal(&value.u.value_str, - &oscore_config[i].text_mapping[j].text)) { + &oscore_config[i].text_mapping[j].text)) { memcpy(&(((char *)oscore_conf)[oscore_config[i].offset]), &oscore_config[i].text_mapping[j].value, sizeof(oscore_config[i].text_mapping[j].value)); @@ -1924,12 +1917,11 @@ coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) { } } if (oscore_config[i].text_mapping[j].text.s == NULL) { - coap_log_warn( - "oscore_conf: Keyword '%.*s': value '%.*s' unknown\n", - (int)keyword.length, - (const char *)keyword.s, - (int)value.u.value_str.length, - (const char *)value.u.value_str.s); + coap_log_warn("oscore_conf: Keyword '%.*s': value '%.*s' unknown\n", + (int)keyword.length, + (const char *)keyword.s, + (int)value.u.value_str.length, + (const char *)value.u.value_str.s); goto error; } break; @@ -1943,34 +1935,29 @@ coap_parse_oscore_conf_mem(coap_str_const_t conf_mem) { } } if (i == sizeof(oscore_config) / sizeof(oscore_config[0])) { - coap_log_warn( - "oscore_conf: Keyword '%.*s', type '%s' unknown\n", - (int)keyword.length, - (const char *)keyword.s, - value.encoding_name); + coap_log_warn("oscore_conf: Keyword '%.*s', type '%s' unknown\n", + (int)keyword.length, + (const char *)keyword.s, + value.encoding_name); if (value.encoding == COAP_ENC_HEX || value.encoding == COAP_ENC_ASCII) coap_delete_bin_const(value.u.value_bin); goto error; } } if (!oscore_conf->master_secret) { - coap_log_warn( - "oscore_conf: master_secret not defined\n"); + coap_log_warn("oscore_conf: master_secret not defined\n"); goto error; } if (!oscore_conf->sender_id) { - coap_log_warn( - "oscore_conf: sender_id not defined\n"); + coap_log_warn("oscore_conf: sender_id not defined\n"); goto error; } if (oscore_conf->sender_id->length > 7) { - coap_log_warn( - "oscore_conf: Maximum size of sender_id is 7 bytes\n"); + coap_log_warn("oscore_conf: Maximum size of sender_id is 7 bytes\n"); goto error; } if (oscore_conf->recipient_id && oscore_conf->recipient_id[0]->length > 7) { - coap_log_warn( - "oscore_conf: Maximum size of recipient_id is 7 bytes\n"); + coap_log_warn("oscore_conf: Maximum size of recipient_id is 7 bytes\n"); goto error; } return oscore_conf; @@ -1987,15 +1974,13 @@ coap_oscore_init(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) { oscore_ctx_t *osc_ctx = NULL; if (!coap_crypto_check_cipher_alg(oscore_conf->aead_alg)) { - coap_log_warn( - "COSE: Cipher Algorithm %d not supported\n", - oscore_conf->aead_alg); + coap_log_warn("COSE: Cipher Algorithm %d not supported\n", + oscore_conf->aead_alg); goto error; } if (!coap_crypto_check_hkdf_alg(oscore_conf->hkdf_alg)) { - coap_log_warn( - "COSE: HKDF Algorithm %d not supported\n", - oscore_conf->hkdf_alg); + coap_log_warn("COSE: HKDF Algorithm %d not supported\n", + oscore_conf->hkdf_alg); goto error; } diff --git a/src/oscore/oscore.c b/src/oscore/oscore.c index 08cf1f64..fe0195c7 100644 --- a/src/oscore/oscore.c +++ b/src/oscore/oscore.c @@ -199,8 +199,8 @@ oscore_encode_option_value(uint8_t *option_buffer, option_buffer[0] |= 0x10; option_buffer[offset] = (uint8_t)oscore_cbor_put_bytes(&ptr, &rem_space, - cose->kid_context.s, - cose->kid_context.length); + cose->kid_context.s, + cose->kid_context.length); offset += option_buffer[offset] + 1; } else { option_buffer[0] |= 0x10; @@ -368,8 +368,7 @@ oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose) { coap_decode_var_bytes8(cose->partial_iv.s, cose->partial_iv.length); if (incoming_seq >= OSCORE_SEQ_MAX) { - coap_log_warn( - "OSCORE Replay protection, SEQ larger than SEQ_MAX.\n"); + coap_log_warn("OSCORE Replay protection, SEQ larger than SEQ_MAX.\n"); return 0; } @@ -390,35 +389,32 @@ oscore_validate_sender_seq(oscore_recipient_ctx_t *ctx, cose_encrypt0_t *cose) { ctx->sliding_window |= 1; ctx->last_seq = incoming_seq; } else if (incoming_seq == ctx->last_seq) { - coap_log_warn( - "OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n", - incoming_seq); + coap_log_warn("OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n", + incoming_seq); return 0; } else { /* incoming_seq < last_seq */ uint64_t shift = ctx->last_seq - incoming_seq - 1; uint64_t pattern; if (shift > ctx->osc_ctx->replay_window_size || shift > 63) { - coap_log_warn( - "OSCORE: Replay protection, SEQ outside of replay window (%" - PRIu64 " %" PRIu64 ")\n", - ctx->last_seq, - incoming_seq); + coap_log_warn("OSCORE: Replay protection, SEQ outside of replay window (%" + PRIu64 " %" PRIu64 ")\n", + ctx->last_seq, + incoming_seq); return 0; } /* seq + replay_window_size > last_seq */ pattern = 1ULL << shift; if (ctx->sliding_window & pattern) { - coap_log_warn( - "OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n", - incoming_seq); + coap_log_warn("OSCORE: Replay protection, replayed SEQ (%" PRIu64 ")\n", + incoming_seq); return 0; } /* bitfield. B0 biggest seq seen. B1 seq-1 seen, B2 seq-2 seen etc. */ ctx->sliding_window |= pattern; } coap_log_oscore("OSCORE: window 0x%" PRIx64 " seq-B0 %" PRIu64 " SEQ %" - PRIu64 "\n", + PRIu64 "\n", ctx->sliding_window, ctx->last_seq, incoming_seq); diff --git a/src/oscore/oscore_cbor.c b/src/oscore/oscore_cbor.c index 9b08e196..cf997703 100644 --- a/src/oscore/oscore_cbor.c +++ b/src/oscore/oscore_cbor.c @@ -196,7 +196,7 @@ oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value) { *buffer = (*buffer) + 5; put_b_f(buffer, value, 3); return 5; - } else /*if(value > 0xffffffff)*/ { + } else { /*if(value > 0xffffffff)*/ /* 8 bytes uint64_t */ assert(*buf_size >= 9); (*buf_size) -= 9; @@ -217,7 +217,7 @@ static inline uint8_t get_byte_inc(const uint8_t **buffer, size_t *buf_len) { assert((*buf_len) > 0); (*buf_len)--; - return((*buffer)++)[0]; + return ((*buffer)++)[0]; } uint8_t diff --git a/src/oscore/oscore_context.c b/src/oscore/oscore_context.c index ddd41a2a..1ea9ddf5 100644 --- a/src/oscore/oscore_context.c +++ b/src/oscore/oscore_context.c @@ -249,9 +249,8 @@ oscore_log_hex_value(coap_log_t level, char number[3 * OSCORE_LOG_SIZE + 4]; oscore_convert_to_hex(&value->s[i], - value->length - i > OSCORE_LOG_SIZE - ? OSCORE_LOG_SIZE - : value->length - i, + value->length - i > OSCORE_LOG_SIZE ? + OSCORE_LOG_SIZE : value->length - i, number, sizeof(number)); coap_log(level, " %-16s %s\n", i == 0 ? name : "", number); @@ -312,9 +311,9 @@ oscore_build_key(oscore_ctx_t *osc_ctx, size_t out_len) { uint8_t info_buffer[60]; size_t info_len; - uint8_t hkdf_tmp[CONTEXT_KEY_LEN > CONTEXT_INIT_VECT_LEN - ? CONTEXT_KEY_LEN - : CONTEXT_INIT_VECT_LEN]; + uint8_t hkdf_tmp[CONTEXT_KEY_LEN > CONTEXT_INIT_VECT_LEN ? + CONTEXT_KEY_LEN : + CONTEXT_INIT_VECT_LEN]; info_len = compose_info(info_buffer, sizeof(info_buffer), @@ -354,7 +353,7 @@ oscore_log_context(oscore_ctx_t *osc_ctx, const char *heading) { sizeof(buffer))); oscore_log_char_value(COAP_LOG_OSCORE, "HKDF alg", cose_get_hkdf_alg_name(osc_ctx->hkdf_alg, buffer, - sizeof(buffer))); + sizeof(buffer))); oscore_log_hex_value(COAP_LOG_OSCORE, "ID Context", osc_ctx->id_context); oscore_log_hex_value(COAP_LOG_OSCORE, "Master Secret", @@ -532,9 +531,9 @@ oscore_derive_ctx(coap_context_t *c_context, coap_oscore_conf_t *oscore_conf) { osc_ctx->hkdf_alg = oscore_conf->hkdf_alg; osc_ctx->id_context = oscore_conf->id_context; osc_ctx->ssn_freq = oscore_conf->ssn_freq ? oscore_conf->ssn_freq : 1; - osc_ctx->replay_window_size = oscore_conf->replay_window - ? oscore_conf->replay_window - : COAP_OSCORE_DEFAULT_REPLAY_WINDOW; + osc_ctx->replay_window_size = oscore_conf->replay_window ? + oscore_conf->replay_window : + COAP_OSCORE_DEFAULT_REPLAY_WINDOW; osc_ctx->rfc8613_b_1_2 = oscore_conf->rfc8613_b_1_2; osc_ctx->rfc8613_b_2 = oscore_conf->rfc8613_b_2; osc_ctx->save_seq_num_func = oscore_conf->save_seq_num_func; @@ -603,8 +602,7 @@ oscore_add_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid, oscore_recipient_ctx_t *recipient_ctx = NULL; if (rid->length > 7) { - coap_log_warn( - "oscore_add_recipient: Maximum size of recipient_id is 7 bytes\n"); + coap_log_warn("oscore_add_recipient: Maximum size of recipient_id is 7 bytes\n"); return NULL; } /* Check this is not a duplicate recipient id */ @@ -617,8 +615,8 @@ oscore_add_recipient(oscore_ctx_t *osc_ctx, coap_bin_const_t *rid, rcp_ctx = rcp_ctx->next_recipient; } recipient_ctx = (oscore_recipient_ctx_t *)coap_malloc_type( - COAP_OSCORE_REC, - sizeof(oscore_recipient_ctx_t)); + COAP_OSCORE_REC, + sizeof(oscore_recipient_ctx_t)); if (recipient_ctx == NULL) return NULL; memset(recipient_ctx, 0, sizeof(oscore_recipient_ctx_t)); diff --git a/src/oscore/oscore_cose.c b/src/oscore/oscore_cose.c index 2dc36a82..a4b869c0 100644 --- a/src/oscore/oscore_cose.c +++ b/src/oscore/oscore_cose.c @@ -60,8 +60,8 @@ static struct cose_curve_desc curve_mapping[] = { { "secp256k1", COSE_CURVE_SECP256K1 }, }; -const char* -cose_get_curve_name(cose_curve_t id, char* buffer, size_t buflen) { +const char * +cose_get_curve_name(cose_curve_t id, char *buffer, size_t buflen) { for (size_t i = 0; i < sizeof(curve_mapping)/sizeof(curve_mapping[0]); i++) { if (id == curve_mapping[i].id) { snprintf(buffer, buflen, "%s (%d)", curve_mapping[i].name, id); @@ -116,8 +116,8 @@ static struct cose_alg_desc alg_mapping[] = { { "AES-CCM-64-128-256", COSE_ALGORITHM_AES_CCM_64_128_256 }, }; -const char* -cose_get_alg_name(cose_alg_t id, char* buffer, size_t buflen) { +const char * +cose_get_alg_name(cose_alg_t id, char *buffer, size_t buflen) { for (size_t i = 0; i < sizeof(alg_mapping)/sizeof(alg_mapping[0]); i++) { if (id == alg_mapping[i].id) { snprintf(buffer, buflen, "%s (%d)", alg_mapping[i].name, id); @@ -147,8 +147,8 @@ static struct cose_hkdf_alg_desc hkdf_alg_mapping[] = { { "direct+HKDF-SHA-256", COSE_HKDF_ALG_HKDF_SHA_256 }, }; -const char* -cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char* buffer, size_t buflen) { +const char * +cose_get_hkdf_alg_name(cose_hkdf_alg_t id, char *buffer, size_t buflen) { for (size_t i = 0; i < sizeof(hkdf_alg_mapping)/sizeof(hkdf_alg_mapping[0]); i++) { if (id == hkdf_alg_mapping[i].id) { snprintf(buffer, buflen, "%s (%d)", hkdf_alg_mapping[i].name, id); @@ -167,8 +167,8 @@ static struct hkdf_hmac_algs { cose_hkdf_alg_t hkdf_alg; cose_hmac_alg_t hmac_alg; } hkdf_hmacs[] = { - {COSE_HKDF_ALG_HKDF_SHA_256, COSE_HMAC_ALG_HMAC256_256}, - {COSE_HKDF_ALG_HKDF_SHA_512, COSE_HMAC_ALG_HMAC512_512}, + {COSE_HKDF_ALG_HKDF_SHA_256, COSE_HMAC_ALG_HMAC256_256}, + {COSE_HKDF_ALG_HKDF_SHA_512, COSE_HMAC_ALG_HMAC512_512}, }; /* @@ -187,7 +187,7 @@ cose_get_hmac_alg_for_hkdf(cose_hkdf_alg_t hkdf_alg, cose_hmac_alg_t *hmac_alg) } } coap_log_debug("cose_get_hmac_alg_for_hkdf: COSE HKDF %d not supported\n", - hkdf_alg); + hkdf_alg); return 0; }