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 ) {
0 commit comments