30
30
#include "mbedtls/entropy.h"
31
31
#include "mbedtls/entropy_poll.h"
32
32
#include "mbedtls/ctr_drbg.h"
33
+ #include "mbedtls/hmac_drbg.h"
33
34
#include "mbedtls/ssl_ciphersuites.h"
34
35
35
36
#include "ns_trace.h"
@@ -41,7 +42,14 @@ struct coap_security_s {
41
42
mbedtls_ssl_config _conf ;
42
43
mbedtls_ssl_context _ssl ;
43
44
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
+
45
53
mbedtls_entropy_context _entropy ;
46
54
bool _is_started ;
47
55
simple_cookie_t _cookie ;
@@ -114,7 +122,11 @@ static int coap_security_handler_init(coap_security_t *sec)
114
122
115
123
mbedtls_ssl_init (& sec -> _ssl );
116
124
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
118
130
mbedtls_entropy_init (& sec -> _entropy );
119
131
120
132
#if defined(MBEDTLS_X509_CRT_PARSE_C )
@@ -132,12 +144,20 @@ static int coap_security_handler_init(coap_security_t *sec)
132
144
128 , entropy_source_type ) < 0 ) {
133
145
return -1 ;
134
146
}
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 ,
137
156
(const unsigned char * ) pers ,
138
157
strlen (pers ))) != 0 ) {
139
158
return -1 ;
140
159
}
160
+ #endif
141
161
return 0 ;
142
162
}
143
163
@@ -160,7 +180,11 @@ static void coap_security_handler_reset(coap_security_t *sec)
160
180
#endif
161
181
162
182
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
164
188
mbedtls_ssl_config_free (& sec -> _conf );
165
189
mbedtls_ssl_free (& sec -> _ssl );
166
190
#if defined(MBEDTLS_PLATFORM_C )
@@ -397,7 +421,11 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
397
421
}
398
422
399
423
#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
401
429
#endif
402
430
403
431
if ((mbedtls_ssl_setup (& sec -> _ssl , & sec -> _conf )) != 0 ) {
0 commit comments