Skip to content

Commit 377e363

Browse files
Teppo JärvelinAntti Kauppila
authored andcommitted
Added missing optimizations based on mbedtls/baremetal.h config
1 parent c801caf commit 377e363

File tree

10 files changed

+162
-46
lines changed

10 files changed

+162
-46
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: 23 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,21 @@ 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 mbedtls_ctr_drbg_seed
106+
#define DRBG_SEED_ERROR "mbedtls_ctr_drbg_seed failed: %d\n"
107+
#define DRBG_RANDOM mbedtls_ctr_drbg_random
108+
#elif defined(MBEDTLS_HMAC_DRBG_C)
109+
mbedtls_hmac_drbg_context _drbg;
110+
#define DRBG_INIT mbedtls_hmac_drbg_init
111+
#define DRBG_SEED mbedtls_hmac_drbg_seed
112+
#define DRBG_SEED_ERROR "mbedtls_hmac_drbg_seed failed: %d\n"
113+
#define DRBG_RANDOM mbedtls_hmac_drbg_random
114+
#else
115+
#error "CTR or HMAC must be defined for coap_security_handler!"
116+
#endif
102117
mbedtls_x509_crt *cert;
103118
mbedtls_pk_context *pkey;
104119
mbedtls_x509_crt *ca;
@@ -721,12 +736,15 @@ altcp_tls_create_config(int is_server, int have_cert, int have_pkey, int have_ca
721736

722737
mbedtls_ssl_config_init(&conf->conf);
723738
mbedtls_entropy_init(&conf->entropy);
724-
mbedtls_ctr_drbg_init(&conf->ctr_drbg);
739+
740+
DRBG_INIT(&conf->_drbg);
725741

726742
/* Seed the RNG */
727-
ret = mbedtls_ctr_drbg_seed(&conf->ctr_drbg, ALTCP_MBEDTLS_RNG_FN, &conf->entropy, ALTCP_MBEDTLS_ENTROPY_PTR, ALTCP_MBEDTLS_ENTROPY_LEN);
743+
ret = DRBG_SEED(&conf->_drbg, ALTCP_MBEDTLS_RNG_FN, &conf->entropy, ALTCP_MBEDTLS_ENTROPY_PTR, ALTCP_MBEDTLS_ENTROPY_LEN);
744+
728745
if (ret != 0) {
729-
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_ctr_drbg_seed failed: %d\n", ret));
746+
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, (DRBG_SEED_ERROR, ret));
747+
730748
altcp_mbedtls_free_config(conf);
731749
return NULL;
732750
}
@@ -742,7 +760,7 @@ altcp_tls_create_config(int is_server, int have_cert, int have_pkey, int have_ca
742760
mbedtls_ssl_conf_authmode(&conf->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
743761

744762
#if !defined(MBEDTLS_SSL_CONF_RNG)
745-
mbedtls_ssl_conf_rng(&conf->conf, mbedtls_ctr_drbg_random, &conf->ctr_drbg);
763+
mbedtls_ssl_conf_rng(&conf->conf, DRBG_RANDOM, &conf->ctr_drbg);
746764
#endif
747765

748766
#if ALTCP_MBEDTLS_DEBUG != LWIP_DBG_OFF

features/mbedtls/platform/src/shared_rng.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ int init_global_rng()
3333
mbedtls_entropy_init(&global_entropy);
3434
mbedtls_hmac_drbg_init(&global_hmac_drbg);
3535

36-
int ret = mbedtls_hmac_drbg_seed(&global_hmac_drbg, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
37-
mbedtls_entropy_func, &global_entropy, NULL, 0);
36+
int ret = mbedtls_hmac_drbg_seed(&global_hmac_drbg,
37+
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
38+
mbedtls_entropy_func, &global_entropy, NULL, 0);
3839

3940
if (ret != 0) {
4041
tr_error(" init_global_rng failed! mbedtls_hmac_drbg_seed returned -0x%x", -ret);
@@ -44,7 +45,6 @@ int init_global_rng()
4445
}
4546

4647
return ret;
47-
This conversation was marked as resolved by jarvte
4848
}
4949

5050
void free_global_rng()

features/nanostack/coap-service/source/coap_security_handler.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "mbedtls/entropy.h"
3131
#include "mbedtls/entropy_poll.h"
3232
#include "mbedtls/ctr_drbg.h"
33+
#include "mbedtls/hmac_drbg.h"
3334
#include "mbedtls/ssl_ciphersuites.h"
3435

3536
#include "ns_trace.h"
@@ -41,7 +42,14 @@ struct coap_security_s {
4142
mbedtls_ssl_config _conf;
4243
mbedtls_ssl_context _ssl;
4344

44-
mbedtls_ctr_drbg_context _ctr_drbg;
45+
#if defined(MBEDTLS_CTR_DRBG_C)
46+
mbedtls_ctr_drbg_context _drbg;
47+
#elif defined(MBEDTLS_HMAC_DRBG_C)
48+
mbedtls_hmac_drbg_context _drbg;
49+
#else
50+
#error "CTR or HMAC must be defined for coap_security_handler!"
51+
#endif
52+
4553
mbedtls_entropy_context _entropy;
4654
bool _is_started;
4755
simple_cookie_t _cookie;
@@ -114,7 +122,11 @@ static int coap_security_handler_init(coap_security_t *sec)
114122

115123
mbedtls_ssl_init(&sec->_ssl);
116124
mbedtls_ssl_config_init(&sec->_conf);
117-
mbedtls_ctr_drbg_init(&sec->_ctr_drbg);
125+
#if defined(MBEDTLS_CTR_DRBG_C)
126+
mbedtls_ctr_drbg_init(&sec->_drbg);
127+
#elif defined(MBEDTLS_HMAC_DRBG_C)
128+
mbedtls_hmac_drbg_init(&sec->_drbg);
129+
#endif
118130
mbedtls_entropy_init(&sec->_entropy);
119131

120132
#if defined(MBEDTLS_X509_CRT_PARSE_C)
@@ -132,12 +144,20 @@ static int coap_security_handler_init(coap_security_t *sec)
132144
128, entropy_source_type) < 0) {
133145
return -1;
134146
}
135-
136-
if ((mbedtls_ctr_drbg_seed(&sec->_ctr_drbg, mbedtls_entropy_func, &sec->_entropy,
147+
#if defined(MBEDTLS_CTR_DRBG_C)
148+
if ((mbedtls_ctr_drbg_seed(&sec->_drbg, mbedtls_entropy_func, &sec->_entropy,
149+
(const unsigned char *) pers,
150+
strlen(pers))) != 0) {
151+
return -1;
152+
}
153+
#elif defined(MBEDTLS_HMAC_DRBG_C)
154+
if ((mbedtls_hmac_drbg_seed(&sec->_drbg, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
155+
mbedtls_entropy_func, &sec->_entropy,
137156
(const unsigned char *) pers,
138157
strlen(pers))) != 0) {
139158
return -1;
140159
}
160+
#endif
141161
return 0;
142162
}
143163

@@ -160,7 +180,11 @@ static void coap_security_handler_reset(coap_security_t *sec)
160180
#endif
161181

162182
mbedtls_entropy_free(&sec->_entropy);
163-
mbedtls_ctr_drbg_free(&sec->_ctr_drbg);
183+
#if defined(MBEDTLS_CTR_DRBG_C)
184+
mbedtls_ctr_drbg_free(&sec->_drbg);
185+
#elif defined(MBEDTLS_HMAC_DRBG_C)
186+
mbedtls_hmac_drbg_free(&sec->_drbg);
187+
#endif
164188
mbedtls_ssl_config_free(&sec->_conf);
165189
mbedtls_ssl_free(&sec->_ssl);
166190
#if defined(MBEDTLS_PLATFORM_C)
@@ -397,7 +421,11 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
397421
}
398422

399423
#if !defined(MBEDTLS_SSL_CONF_RNG)
400-
mbedtls_ssl_conf_rng(&sec->_conf, mbedtls_ctr_drbg_random, &sec->_ctr_drbg);
424+
#if defined(MBEDTLS_CTR_DRBG_C)
425+
mbedtls_ssl_conf_rng(&sec->_conf, mbedtls_ctr_drbg_random, &sec->_drbg);
426+
#elif defined(MBEDTLS_HMAC_DRBG_C)
427+
mbedtls_ssl_conf_rng(&sec->_conf, mbedtls_hmac_drbg_random, &sec->_drbg);
428+
#endif
401429
#endif
402430

403431
if ((mbedtls_ssl_setup(&sec->_ssl, &sec->_conf)) != 0) {

features/nanostack/coap-service/test/coap-service/unittest/stub/mbedtls_stub.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,38 @@ int mbedtls_ctr_drbg_random(void *p_rng,
248248
return mbedtls_stub.crt_expected_int;
249249
}
250250

251+
// from hmac_drbg.h
252+
void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx )
253+
{
254+
255+
}
256+
257+
void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx )
258+
{
259+
260+
}
261+
262+
int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
263+
mbedtls_md_handle_t md_info,
264+
int (*f_entropy)(void *, unsigned char *, size_t),
265+
void *p_entropy,
266+
const unsigned char *custom,
267+
size_t len )
268+
{
269+
return mbedtls_stub.crt_expected_int;
270+
}
271+
272+
int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
273+
{
274+
return mbedtls_stub.crt_expected_int;
275+
}
276+
277+
// from md.h
278+
mbedtls_md_handle_t mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
279+
{
280+
return 0;
281+
}
282+
251283
//From x509_crt.h
252284
void mbedtls_x509_crt_init(mbedtls_x509_crt *a)
253285
{

features/nanostack/coap-service/test/coap-service/unittest/stub/mbedtls_stub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
#include "mbedtls/platform.h"
2525
#include "mbedtls/ssl.h"
2626
#include "mbedtls/ctr_drbg.h"
27+
#include "mbedtls/hmac_drbg.h"
2728
#include "mbedtls/x509_crt.h"
2829
#include "mbedtls/sha256.h"
2930
#include "mbedtls/entropy.h"
3031
#include "mbedtls/pk.h"
3132
#include "mbedtls/platform.h"
33+
#include "mbedtls/md.h"
3234

3335

3436
#define HANDSHAKE_FINISHED_VALUE 8888

features/nanostack/sal-stack-nanostack/nanostack/ns_sha256.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,45 @@ static inline void ns_sha256_clone(ns_sha256_context *dst,
6161

6262
static inline void ns_sha256_starts(ns_sha256_context *ctx)
6363
{
64-
mbedtls_sha256_starts(ctx, 0);
64+
(void)mbedtls_sha256_starts_ret(ctx, 0);
6565
}
6666

6767
static inline void ns_sha256_update(ns_sha256_context *ctx, const void *input,
6868
size_t ilen)
6969
{
70-
mbedtls_sha256_update(ctx, input, ilen);
70+
(void)mbedtls_sha256_update_ret(ctx, input, ilen);
7171
}
7272

7373
static inline void ns_sha256_finish(ns_sha256_context *ctx, void *output)
7474
{
75-
mbedtls_sha256_finish(ctx, output);
75+
(void)mbedtls_sha256_finish_ret(ctx, output);
7676
}
7777

7878
static inline void ns_sha256(const void *input, size_t ilen, void *output)
7979
{
80-
mbedtls_sha256(input, ilen, output, 0);
80+
(void)mbedtls_sha256_ret(input, ilen, output, 0);
8181
}
8282

8383
/* Extensions to standard mbed TLS - output the first bits of a hash only */
8484
/* Number of bits must be a multiple of 32, and <=256 */
8585
static inline void ns_sha256_finish_nbits(ns_sha256_context *ctx, void *output, unsigned obits)
8686
{
8787
if (obits == 256) {
88-
mbedtls_sha256_finish(ctx, output);
88+
(void)mbedtls_sha256_finish_ret(ctx, output);
8989
} else {
9090
uint8_t sha256[32];
91-
mbedtls_sha256_finish(ctx, sha256);
91+
(void)mbedtls_sha256_finish_ret(ctx, sha256);
9292
memcpy(output, sha256, obits / 8);
9393
}
9494
}
9595

9696
static inline void ns_sha256_nbits(const void *input, size_t ilen, void *output, unsigned obits)
9797
{
9898
if (obits == 256) {
99-
mbedtls_sha256(input, ilen, output, 0);
99+
(void)mbedtls_sha256_ret(input, ilen, output, 0);
100100
} else {
101101
uint8_t sha256[32];
102-
mbedtls_sha256(input, ilen, sha256, 0);
102+
(void)mbedtls_sha256_ret(input, ilen, sha256, 0);
103103
memcpy(output, sha256, obits / 8);
104104
}
105105
}

0 commit comments

Comments
 (0)