Skip to content

Commit 163e5c9

Browse files
Teppo JärvelinAntti Kauppila
authored andcommitted
Added new global rng, needed for MbedTLS optimisations
1 parent a325320 commit 163e5c9

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,15 @@ altcp_mbedtls_setup(void *conf, struct altcp_pcb *conn, struct altcp_pcb *inner_
599599
altcp_mbedtls_free(conf, state);
600600
return ERR_MEM;
601601
}
602+
// Defines MBEDTLS_SSL_CONF_RECV/SEND/RECV_TIMEOUT define global functions which should be the same for all
603+
// callers of mbedtls_ssl_set_bio_ctx and there should be only one ssl context. If these rules don't apply,
604+
// these defines can't be used.
605+
#if !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT)
602606
/* tell mbedtls about our I/O functions */
603607
mbedtls_ssl_set_bio(&state->ssl_context, conn, altcp_mbedtls_bio_send, altcp_mbedtls_bio_recv, NULL);
608+
#else
609+
mbedtls_ssl_set_bio_ctx(&state->ssl_context, conn);
610+
#endif /* !defined(MBEDTLS_SSL_CONF_RECV) && !defined(MBEDTLS_SSL_CONF_SEND) && !defined(MBEDTLS_SSL_CONF_RECV_TIMEOUT) */
604611

605612
altcp_mbedtls_setup_callbacks(conn, inner_conn);
606613
conn->inner_conn = inner_conn;
@@ -734,7 +741,10 @@ altcp_tls_create_config(int is_server, int have_cert, int have_pkey, int have_ca
734741
}
735742
mbedtls_ssl_conf_authmode(&conf->conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
736743

744+
#if !defined(MBEDTLS_SSL_CONF_RNG)
737745
mbedtls_ssl_conf_rng(&conf->conf, mbedtls_ctr_drbg_random, &conf->ctr_drbg);
746+
#endif
747+
738748
#if ALTCP_MBEDTLS_DEBUG != LWIP_DBG_OFF
739749
mbedtls_ssl_conf_dbg(&conf->conf, altcp_mbedtls_debug, stdout);
740750
#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, 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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* shared_rng.cpp
3+
*
4+
* Copyright (C) 2019, 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+
mbedtls_hmac_drbg_context global_hmac_drbg;
28+
mbedtls_entropy_context global_entropy;
29+
static bool is_initialized = false;
30+
31+
int init_global_rng()
32+
{
33+
mbedtls_entropy_init(&global_entropy);
34+
mbedtls_hmac_drbg_init(&global_hmac_drbg);
35+
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);
38+
39+
if (ret != 0) {
40+
tr_error(" init_global_rng failed! mbedtls_hmac_drbg_seed returned -0x%x", -ret);
41+
free_global_rng();
42+
} else {
43+
is_initialized = true;
44+
}
45+
46+
return ret;
47+
This conversation was marked as resolved by jarvte
48+
}
49+
50+
void free_global_rng()
51+
{
52+
mbedtls_entropy_free(&global_entropy);
53+
mbedtls_hmac_drbg_free(&global_hmac_drbg);
54+
is_initialized = false;
55+
}
56+
57+
int global_rng( void *ctx, unsigned char *dst, size_t len )
58+
{
59+
if (!is_initialized) {
60+
return MBED_SHARED_RNG_NOT_INITIALIZED;
61+
}
62+
return mbedtls_hmac_drbg_random(&global_hmac_drbg, dst, len);
63+
}
64+
65+
mbedtls_hmac_drbg_context *get_global_hmac_drbg()
66+
{
67+
return &global_hmac_drbg;
68+
}
69+
70+
mbedtls_entropy_context *get_global_entropy()
71+
{
72+
return &global_entropy;
73+
}
74+
75+
#endif // MBEDTLS_SSL_CONF_RNG

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ int coap_security_handler_connect_non_blocking(coap_security_t *sec, bool is_ser
396396
mbedtls_ssl_conf_handshake_timeout(&sec->_conf, timeout_min, timeout_max);
397397
}
398398

399+
#if !defined(MBEDTLS_SSL_CONF_RNG)
399400
mbedtls_ssl_conf_rng(&sec->_conf, mbedtls_ctr_drbg_random, &sec->_ctr_drbg);
401+
#endif
400402

401403
if ((mbedtls_ssl_setup(&sec->_ssl, &sec->_conf)) != 0) {
402404
return -1;

features/nanostack/sal-stack-nanostack/source/Security/protocols/tls_sec_prot/tls_sec_prot_lib.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,10 @@ int8_t tls_sec_prot_lib_connect(tls_security_t *sec, bool is_server, const sec_p
327327
return -1;
328328
}
329329

330+
#if !defined(MBEDTLS_SSL_CONF_RNG)
330331
// Configure random number generator
331332
mbedtls_ssl_conf_rng(&sec->conf, mbedtls_ctr_drbg_random, &sec->ctr_drbg);
333+
#endif
332334

333335
#ifdef MBEDTLS_ECP_RESTARTABLE
334336
// Set ECC calculation maximum operations (affects only client)

features/netsocket/TLSSocketWrapper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ nsapi_error_t TLSSocketWrapper::start_handshake(bool first_call)
190190
return NSAPI_ERROR_AUTH_FAILURE;
191191
}
192192

193+
#if !defined(MBEDTLS_SSL_CONF_RNG)
193194
mbedtls_ssl_conf_rng(get_ssl_config(), mbedtls_ctr_drbg_random, &_ctr_drbg);
195+
#endif
194196

195197

196198
#if MBED_CONF_TLS_SOCKET_DEBUG_LEVEL > 0

0 commit comments

Comments
 (0)