Skip to content

Commit 94d202a

Browse files
committed
Update for primary commit obgm#6 (to be squashed later)
Fix for later versions of MBed TLS Fix a couple of ifdef which should be if
1 parent f7cc58c commit 94d202a

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/coap_mbedtls.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2755,7 +2755,9 @@ coap_crypto_aead_encrypt(const coap_crypto_param_t *params,
27552755
size_t *max_result_len) {
27562756
mbedtls_cipher_context_t ctx;
27572757
const coap_crypto_aes_ccm_t *ccm;
2758+
#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
27582759
unsigned char tag[16];
2760+
#endif /* MBEDTLS_VERSION_NUMBER < 0x02150000 */
27592761
int ret = 0;
27602762
size_t result_len = *max_result_len;
27612763
coap_bin_const_t laad;
@@ -2785,6 +2787,7 @@ coap_crypto_aead_encrypt(const coap_crypto_param_t *params,
27852787
laad.length = 0;
27862788
}
27872789

2790+
#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
27882791
C(mbedtls_cipher_auth_encrypt(&ctx,
27892792
ccm->nonce,
27902793
15 - ccm->l, /* iv */
@@ -2797,7 +2800,6 @@ coap_crypto_aead_encrypt(const coap_crypto_param_t *params,
27972800
tag,
27982801
ccm->tag_len /* tag */
27992802
));
2800-
28012803
/* check if buffer is sufficient to hold tag */
28022804
if ((result_len + ccm->tag_len) > *max_result_len) {
28032805
coap_log(LOG_ERR, "coap_encrypt: buffer too small\n");
@@ -2807,6 +2809,23 @@ coap_crypto_aead_encrypt(const coap_crypto_param_t *params,
28072809
memcpy(result + result_len, tag, ccm->tag_len);
28082810
*max_result_len = result_len + ccm->tag_len;
28092811
ret = 1;
2812+
#else /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
2813+
C(mbedtls_cipher_auth_encrypt_ext(&ctx,
2814+
ccm->nonce,
2815+
15 - ccm->l, /* iv */
2816+
laad.s,
2817+
laad.length, /* ad */
2818+
data->s,
2819+
data->length, /* input */
2820+
result,
2821+
result_len,
2822+
&result_len, /* output */
2823+
ccm->tag_len /* tag */
2824+
));
2825+
*max_result_len = result_len;
2826+
ret = 1;
2827+
#endif /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
2828+
28102829
error:
28112830
mbedtls_cipher_free(&ctx);
28122831
return ret;
@@ -2820,7 +2839,9 @@ coap_crypto_aead_decrypt(const coap_crypto_param_t *params,
28202839
size_t *max_result_len) {
28212840
mbedtls_cipher_context_t ctx;
28222841
const coap_crypto_aes_ccm_t *ccm;
2842+
#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
28232843
const unsigned char *tag;
2844+
#endif /* MBEDTLS_VERSION_NUMBER < 0x02150000 */
28242845
int ret = 0;
28252846
size_t result_len = *max_result_len;
28262847
coap_bin_const_t laad;
@@ -2856,6 +2877,7 @@ coap_crypto_aead_decrypt(const coap_crypto_param_t *params,
28562877
laad.length = 0;
28572878
}
28582879

2880+
#if (MBEDTLS_VERSION_NUMBER < 0x02150000)
28592881
tag = data->s + data->length - ccm->tag_len;
28602882
C(mbedtls_cipher_auth_decrypt(&ctx,
28612883
ccm->nonce,
@@ -2869,6 +2891,21 @@ coap_crypto_aead_decrypt(const coap_crypto_param_t *params,
28692891
tag,
28702892
ccm->tag_len /* tag */
28712893
));
2894+
#else /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
2895+
C(mbedtls_cipher_auth_decrypt_ext(&ctx,
2896+
ccm->nonce,
2897+
15 - ccm->l, /* iv */
2898+
laad.s,
2899+
laad.length, /* ad */
2900+
data->s,
2901+
// data->length - ccm->tag_len, /* input */
2902+
data->length, /* input */
2903+
result,
2904+
result_len,
2905+
&result_len, /* output */
2906+
ccm->tag_len /* tag */
2907+
));
2908+
#endif /* MBEDTLS_VERSION_NUMBER >= 0x02150000 */
28722909

28732910
*max_result_len = result_len;
28742911
ret = 1;

src/coap_oscore.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
static oscore_ctx_t *coap_oscore_init(coap_context_t *c_context,
2929
coap_oscore_conf_t *oscore_conf);
3030

31-
#ifdef COAP_CLIENT_SUPPORT
31+
#if COAP_CLIENT_SUPPORT
3232

3333
int
3434
coap_oscore_initiate(coap_session_t *session, coap_oscore_conf_t *oscore_conf) {
@@ -122,7 +122,7 @@ coap_new_client_session_oscore_pki(coap_context_t *ctx,
122122
return session;
123123
}
124124
#endif /* COAP_CLIENT_SUPPORT */
125-
#ifdef COAP_SERVER_SUPPORT
125+
#if COAP_SERVER_SUPPORT
126126

127127
int
128128
coap_context_oscore_server(coap_context_t *context,
@@ -787,7 +787,9 @@ coap_oscore_decrypt_pdu(coap_session_t *session,
787787
uint8_t external_aad_buffer[100];
788788
coap_bin_const_t external_aad;
789789
oscore_sender_ctx_t *snd_ctx = NULL;
790+
#if COAP_CLIENT_SUPPORT
790791
coap_pdu_t *sent_pdu = NULL;
792+
#endif /* COAP_CLIENT_SUPPORT */
791793

792794
opt = coap_check_option(pdu, COAP_OPTION_OSCORE, &opt_iter);
793795
assert(opt);
@@ -1013,8 +1015,8 @@ coap_oscore_decrypt_pdu(coap_session_t *session,
10131015
rcp_ctx = association->recipient_ctx;
10141016
osc_ctx = rcp_ctx->osc_ctx;
10151017
snd_ctx = osc_ctx->sender_context;
1018+
#if COAP_CLIENT_SUPPORT
10161019
sent_pdu = association->sent_pdu;
1017-
#ifdef COAP_CLIENT_SUPPORT
10181020
if (session->b_2_step != COAP_OSCORE_B_2_NONE) {
10191021
const uint8_t *ptr = cose->kid_context.s;
10201022

@@ -1366,7 +1368,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session,
13661368
1);
13671369
goto error_no_ack;
13681370
}
1369-
#ifdef COAP_CLIENT_SUPPORT
1371+
#if COAP_CLIENT_SUPPORT
13701372
if (session->b_2_step == COAP_OSCORE_B_2_STEP_3) {
13711373
/*
13721374
* Need to update Security Context with new (R2 || R3) ID Context
@@ -1404,7 +1406,7 @@ coap_oscore_decrypt_pdu(coap_session_t *session,
14041406
}
14051407
#endif /* COAP_CLIENT_SUPPORT */
14061408

1407-
#ifdef COAP_SERVER_SUPPORT
1409+
#if COAP_SERVER_SUPPORT
14081410
/* Appendix B.1.2 request Trap */
14091411
if (coap_request && osc_ctx->rfc8613_b_1_2) {
14101412
if (rcp_ctx->initial_state == 1) {

src/coap_session.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ const char *coap_endpoint_str(const coap_endpoint_t *endpoint) {
17511751
return szEndpoint;
17521752
}
17531753
#endif /* COAP_SERVER_SUPPORT */
1754-
#ifdef COAP_CLIENT_SUPPORT
1754+
#if COAP_CLIENT_SUPPORT
17551755
void
17561756
coap_session_set_no_observe_cancel(coap_session_t *session) {
17571757
session->no_observe_cancel = 1;

0 commit comments

Comments
 (0)