Skip to content

Commit 9929ddd

Browse files
authored
Merge pull request #12723 from AnttiKauppila/mbedtls_fixes5.15
Mbedtls fixes for 5.15.2
2 parents eaeb983 + 9c32811 commit 9929ddd

File tree

13 files changed

+419
-48
lines changed

13 files changed

+419
-48
lines changed

TESTS/mbedtls/multi/main.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
using namespace utest::v1;
3434

3535
#if defined(MBEDTLS_SHA256_C)
36-
/* Tests several call to mbedtls_sha256_update function that are not modulo 64 bytes */
36+
/* Tests several call to mbedtls_sha256_update_ret function that are not modulo 64 bytes */
3737
void test_case_sha256_split()
3838
{
3939
const unsigned char test_buf[] = {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"};
@@ -50,18 +50,18 @@ void test_case_sha256_split()
5050
mbedtls_sha256_context ctx;
5151
printf("test sha256\n");
5252
mbedtls_sha256_init(&ctx);
53-
mbedtls_sha256_starts(&ctx, 0);
53+
(void)mbedtls_sha256_starts_ret(&ctx, 0);
5454
#if 0
5555
printf("test not splitted\n");
56-
mbedtls_sha256_update(&ctx, test_buf, 168);
56+
(void)mbedtls_sha256_update_ret(&ctx, test_buf, 168);
5757
#else
5858
printf("test splitted into 3 pieces\n");
59-
mbedtls_sha256_update(&ctx, test_buf, 2);
60-
mbedtls_sha256_update(&ctx, test_buf + 2, 66);
61-
mbedtls_sha256_update(&ctx, test_buf + 68, 100);
59+
(void)mbedtls_sha256_update_ret(&ctx, test_buf, 2);
60+
(void)mbedtls_sha256_update_ret(&ctx, test_buf + 2, 66);
61+
(void)mbedtls_sha256_update_ret(&ctx, test_buf + 68, 100);
6262
#endif
6363

64-
mbedtls_sha256_finish(&ctx, outsum);
64+
(void)mbedtls_sha256_finish_ret(&ctx, outsum);
6565
mbedtls_sha256_free(&ctx);
6666

6767
printf("\nreceived result : ");
@@ -113,29 +113,29 @@ void test_case_sha256_multi()
113113
mbedtls_sha256_init(&ctx2);
114114
mbedtls_sha256_init(&ctx3);
115115
//Start both contexts
116-
mbedtls_sha256_starts(&ctx1, 0);
117-
mbedtls_sha256_starts(&ctx2, 0);
116+
(void)mbedtls_sha256_starts_ret(&ctx1, 0);
117+
(void)mbedtls_sha256_starts_ret(&ctx2, 0);
118118

119119
printf("upd ctx1\n");
120-
mbedtls_sha256_update(&ctx1, test_buf, 56);
120+
(void)mbedtls_sha256_update_ret(&ctx1, test_buf, 56);
121121
printf("upd ctx2\n");
122-
mbedtls_sha256_update(&ctx2, test_buf, 66);
122+
(void)mbedtls_sha256_update_ret(&ctx2, test_buf, 66);
123123
printf("finish ctx1\n");
124-
mbedtls_sha256_finish(&ctx1, outsum1);
124+
(void)mbedtls_sha256_finish_ret(&ctx1, outsum1);
125125
printf("upd ctx2\n");
126-
mbedtls_sha256_update(&ctx2, test_buf + 66, 46);
126+
(void)mbedtls_sha256_update_ret(&ctx2, test_buf + 66, 46);
127127
printf("clone ctx2 in ctx3\n");
128128
mbedtls_sha256_clone(&ctx3, (const mbedtls_sha256_context *)&ctx2);
129129
printf("free ctx1\n");
130130
mbedtls_sha256_free(&ctx1);
131131
printf("upd ctx2\n");
132-
mbedtls_sha256_update(&ctx2, test_buf + 112, 56);
132+
(void)mbedtls_sha256_update_ret(&ctx2, test_buf + 112, 56);
133133
printf("upd ctx3 with different values than ctx2\n");
134-
mbedtls_sha256_update(&ctx3, test_buf2, 56);
134+
(void)mbedtls_sha256_update_ret(&ctx3, test_buf2, 56);
135135
printf("finish ctx2\n");
136-
mbedtls_sha256_finish(&ctx2, outsum2);
136+
(void)mbedtls_sha256_finish_ret(&ctx2, outsum2);
137137
printf("finish ctx3\n");
138-
mbedtls_sha256_finish(&ctx3, outsum3);
138+
(void)mbedtls_sha256_finish_ret(&ctx3, outsum3);
139139
printf("free ctx2\n");
140140
mbedtls_sha256_free(&ctx2);
141141
printf("free ctx3\n");

features/device_key/TESTS/device_key/functionality/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ void generate_derived_key_long_consistency_test()
7272
generate_derived_key_consistency_16_byte_key_long_consistency_test(key);
7373
strcpy(key, MSG_KEY_DEVICE_TEST_STEP2);
7474
generate_derived_key_consistency_16_byte_key_long_consistency_test(key);
75+
#ifndef MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
7576
strcpy(key, MSG_KEY_DEVICE_TEST_STEP3);
7677
generate_derived_key_consistency_32_byte_key_long_consistency_test(key);
7778
strcpy(key, MSG_KEY_DEVICE_TEST_STEP4);
7879
generate_derived_key_consistency_32_byte_key_long_consistency_test(key);
80+
#endif
7981

8082
}
8183

@@ -476,12 +478,16 @@ Case cases[] = {
476478
Case("Device Key - long consistency test", generate_derived_key_long_consistency_test, greentea_failure_handler),
477479
Case("Device Key - inject value wrong size", device_inject_root_of_trust_wrong_size_test, greentea_failure_handler),
478480
Case("Device Key - inject value 16 byte size", device_inject_root_of_trust_16_byte_size_test, greentea_failure_handler),
481+
#ifndef MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
479482
Case("Device Key - inject value 32 byte size", device_inject_root_of_trust_32_byte_size_test, greentea_failure_handler),
483+
#endif
480484
Case("Device Key - inject value several times", device_inject_root_of_trust_several_times_test, greentea_failure_handler),
481485
Case("Device Key - derived key consistency 16 byte key", generate_derived_key_consistency_16_byte_key_test, greentea_failure_handler),
482486
Case("Device Key - derived key consistency 32 byte key", generate_derived_key_consistency_32_byte_key_test, greentea_failure_handler),
483487
Case("Device Key - derived key key type 16", generate_derived_key_key_type_16_test, greentea_failure_handler),
488+
#ifndef MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH
484489
Case("Device Key - derived key key type 32", generate_derived_key_key_type_32_test, greentea_failure_handler),
490+
#endif
485491
Case("Device Key - derived key wrong key type", generate_derived_key_wrong_key_type_test, greentea_failure_handler)
486492
};
487493

features/lwipstack/lwip/src/apps/altcp_tls/altcp_tls_mbedtls.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
/* @todo: which includes are really needed? */
7070
#include "mbedtls/entropy.h"
7171
#include "mbedtls/ctr_drbg.h"
72+
#include "mbedtls/hmac_drbg.h"
7273
#include "mbedtls/certs.h"
7374
#include "mbedtls/x509.h"
7475
#include "mbedtls/ssl.h"
@@ -98,7 +99,19 @@ extern const struct altcp_functions altcp_mbedtls_functions;
9899
struct altcp_tls_config {
99100
mbedtls_ssl_config conf;
100101
mbedtls_entropy_context entropy;
101-
mbedtls_ctr_drbg_context ctr_drbg;
102+
#if defined(MBEDTLS_CTR_DRBG_C)
103+
mbedtls_ctr_drbg_context _drbg;
104+
#define DRBG_INIT mbedtls_ctr_drbg_init
105+
#define DRBG_SEED_ERROR "mbedtls_ctr_drbg_seed failed: %d\n"
106+
#define DRBG_RANDOM mbedtls_ctr_drbg_random
107+
#elif defined(MBEDTLS_HMAC_DRBG_C)
108+
mbedtls_hmac_drbg_context _drbg;
109+
#define DRBG_INIT mbedtls_hmac_drbg_init
110+
#define DRBG_SEED_ERROR "mbedtls_hmac_drbg_seed failed: %d\n"
111+
#define DRBG_RANDOM mbedtls_hmac_drbg_random
112+
#else
113+
#error "CTR or HMAC must be defined for altcp_tls_mbedtls!"
114+
#endif
102115
mbedtls_x509_crt *cert;
103116
mbedtls_pk_context *pkey;
104117
mbedtls_x509_crt *ca;
@@ -599,8 +612,15 @@ altcp_mbedtls_setup(void *conf, struct altcp_pcb *conn, struct altcp_pcb *inner_
599612
altcp_mbedtls_free(conf, state);
600613
return ERR_MEM;
601614
}
615+
// Defines MBEDTLS_SSL_CONF_RECV/SEND/RECV_TIMEOUT define global functions which should be the same for all
616+
// callers of mbedtls_ssl_set_bio_ctx and there should be only one ssl context. If these rules don't apply,
617+
// these defines can't be used.
618+
#if !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
602619
/* tell mbedtls about our I/O functions */
603620
mbedtls_ssl_set_bio(&state->ssl_context, conn, altcp_mbedtls_bio_send, altcp_mbedtls_bio_recv, NULL);
621+
#else
622+
mbedtls_ssl_set_bio_ctx(&state->ssl_context, conn);
623+
#endif /* !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) */
604624

605625
altcp_mbedtls_setup_callbacks(conn, inner_conn);
606626
conn->inner_conn = inner_conn;
@@ -714,12 +734,24 @@ altcp_tls_create_config(int is_server, int have_cert, int have_pkey, int have_ca
714734

715735
mbedtls_ssl_config_init(&conf->conf);
716736
mbedtls_entropy_init(&conf->entropy);
717-
mbedtls_ctr_drbg_init(&conf->ctr_drbg);
737+
738+
DRBG_INIT(&conf->_drbg);
718739

719740
/* Seed the RNG */
720-
ret = mbedtls_ctr_drbg_seed(&conf->ctr_drbg, ALTCP_MBEDTLS_RNG_FN, &conf->entropy, ALTCP_MBEDTLS_ENTROPY_PTR, ALTCP_MBEDTLS_ENTROPY_LEN);
741+
#if defined(MBEDTLS_CTR_DRBG_C)
742+
ret = mbedtls_ctr_drbg_seed(&conf->_drbg, ALTCP_MBEDTLS_RNG_FN,
743+
&conf->entropy, ALTCP_MBEDTLS_ENTROPY_PTR, ALTCP_MBEDTLS_ENTROPY_LEN);
744+
#elif defined(MBEDTLS_HMAC_DRBG_C)
745+
ret = mbedtls_hmac_drbg_seed(&conf->_drbg, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
746+
ALTCP_MBEDTLS_RNG_FN, &conf->entropy,
747+
ALTCP_MBEDTLS_ENTROPY_PTR, ALTCP_MBEDTLS_ENTROPY_LEN);
748+
#else
749+
#error "CTR or HMAC must be defined for altcp_tls_mbedtls!"
750+
#endif
751+
721752
if (ret != 0) {
722-
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_ctr_drbg_seed failed: %d\n", ret));
753+
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, (DRBG_SEED_ERROR, ret));
754+
723755
altcp_mbedtls_free_config(conf);
724756
return NULL;
725757
}
@@ -734,7 +766,10 @@ altcp_tls_create_config(int is_server, int have_cert, int have_pkey, int have_ca
734766
}
735767
mbedtls_ssl_conf_authmode(&conf->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
736768

737-
mbedtls_ssl_conf_rng(&conf->conf, mbedtls_ctr_drbg_random, &conf->ctr_drbg);
769+
#if !defined(MBEDTLS_SSL_CONF_RNG)
770+
mbedtls_ssl_conf_rng(&conf->conf, DRBG_RANDOM, &conf->ctr_drbg);
771+
#endif
772+
738773
#if ALTCP_MBEDTLS_DEBUG != LWIP_DBG_OFF
739774
mbedtls_ssl_conf_dbg(&conf->conf, altcp_mbedtls_debug, stdout);
740775
#endif
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* shared_rng.h
3+
*
4+
* Copyright (C) 2019-2020, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#ifndef SHARED_RNG_H
22+
#define SHARED_RNG_H
23+
24+
#if !defined(MBEDTLS_CONFIG_FILE)
25+
#include "config.h"
26+
#else
27+
#include MBEDTLS_CONFIG_FILE
28+
#endif
29+
30+
#if defined(MBEDTLS_SSL_CONF_RNG)
31+
32+
#define MBED_SHARED_RNG_NOT_INITIALIZED -1 /**< init_global_rng not called before global_rng */
33+
34+
#ifdef __cplusplus
35+
extern "C" {
36+
#endif
37+
38+
#include "mbedtls/hmac_drbg.h"
39+
#include "mbedtls/entropy.h"
40+
41+
/**
42+
* \brief Initializes hmac ready for rng
43+
*
44+
* \return 0 if successful, or
45+
* MBEDTLS_ERR_MD_BAD_INPUT_DATA, or
46+
* MBEDTLS_ERR_MD_ALLOC_FAILED, or
47+
* MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.
48+
*/
49+
int init_global_rng();
50+
51+
/**
52+
* \brief Global HMAC_DRBG generate random
53+
*
54+
* \note Automatically reseeds if reseed_counter is reached or PR is enabled.
55+
* \note init_global_rng function must be called
56+
* before calling this function!
57+
*
58+
* \param ctx DRBG context
59+
* \param dst Buffer to fill
60+
* \param len Length of the buffer
61+
*
62+
* \return 0 if successful, or
63+
* MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
64+
* MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG or
65+
* MBED_SHARED_RNG_NOT_INITIALIZED
66+
*/
67+
int global_rng( void *ctx, unsigned char *dst, size_t len );
68+
69+
/**
70+
* \brief Free allocated resources
71+
*/
72+
void free_global_rng();
73+
74+
/**
75+
* \brief Getter function for global hmac context
76+
*
77+
* \return global hmac context
78+
*/
79+
mbedtls_hmac_drbg_context *get_global_hmac_drbg();
80+
81+
/**
82+
* \brief Getter function for global entropy context
83+
*
84+
* \return global entropy context
85+
*/
86+
mbedtls_entropy_context *get_global_entropy();
87+
88+
#ifdef __cplusplus
89+
}
90+
#endif
91+
92+
#endif // MBEDTLS_SSL_CONF_RNG
93+
#endif // SHARED_RNG_H
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* shared_rng.cpp
3+
*
4+
* Copyright (C) 2019-2020, Arm Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include "shared_rng.h"
22+
23+
#if defined(MBEDTLS_SSL_CONF_RNG)
24+
25+
#include "mbed_trace.h"
26+
27+
#define TRACE_GROUP "SRNG"
28+
29+
mbedtls_hmac_drbg_context global_hmac_drbg;
30+
mbedtls_entropy_context global_entropy;
31+
static bool is_initialized = false;
32+
33+
int init_global_rng()
34+
{
35+
mbedtls_entropy_init(&global_entropy);
36+
mbedtls_hmac_drbg_init(&global_hmac_drbg);
37+
38+
int ret = mbedtls_hmac_drbg_seed(&global_hmac_drbg,
39+
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
40+
mbedtls_entropy_func, &global_entropy, NULL, 0);
41+
42+
if (ret != 0) {
43+
tr_error(" init_global_rng failed! mbedtls_hmac_drbg_seed returned -0x%x", -ret);
44+
free_global_rng();
45+
} else {
46+
is_initialized = true;
47+
}
48+
49+
return ret;
50+
}
51+
52+
void free_global_rng()
53+
{
54+
mbedtls_entropy_free(&global_entropy);
55+
mbedtls_hmac_drbg_free(&global_hmac_drbg);
56+
is_initialized = false;
57+
}
58+
59+
int global_rng( void *ctx, unsigned char *dst, size_t len )
60+
{
61+
if (!is_initialized) {
62+
return MBED_SHARED_RNG_NOT_INITIALIZED;
63+
}
64+
return mbedtls_hmac_drbg_random(&global_hmac_drbg, dst, len);
65+
}
66+
67+
mbedtls_hmac_drbg_context *get_global_hmac_drbg()
68+
{
69+
return &global_hmac_drbg;
70+
}
71+
72+
mbedtls_entropy_context *get_global_entropy()
73+
{
74+
return &global_entropy;
75+
}
76+
77+
#endif // MBEDTLS_SSL_CONF_RNG

0 commit comments

Comments
 (0)