Skip to content

Commit cace1e9

Browse files
Teppo JärvelinArto Kinnunen
authored andcommitted
Added missing optimizations based on mbedtls/baremetal.h config
1 parent 2cc7442 commit cace1e9

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

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) {

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
{

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

0 commit comments

Comments
 (0)