Skip to content

Commit feae56e

Browse files
author
Cruz Monrreal
authored
Merge pull request #9493 from RonEld/add_platform_initialization_in_trng_test
Initialize platform in trng test
2 parents f527a8f + 77f9faf commit feae56e

File tree

15 files changed

+161
-18
lines changed

15 files changed

+161
-18
lines changed

TESTS/mbed_hal/trng/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "base64b.h"
4444
#include "pithy.h"
4545
#include <stdio.h>
46+
#include "mbedtls/config.h"
47+
#include "mbedtls/platform.h"
4648

4749
#if !DEVICE_TRNG
4850
#error [NOT_SUPPORTED] TRNG API not supported for this target
@@ -268,11 +270,17 @@ Specification specification(greentea_test_setup, cases, greentea_test_teardown_h
268270

269271
int main()
270272
{
273+
int ret = 0;
274+
#if defined(MBEDTLS_PLATFORM_C)
275+
ret = mbedtls_platform_setup(NULL);
276+
#endif /* MBEDTLS_PLATFORM_C */
271277
#if (defined(TARGET_PSA) && defined(COMPONENT_PSA_SRV_IPC) && defined(MBEDTLS_PSA_CRYPTO_C))
272278
inject_entropy_for_psa();
273279
#endif
274-
bool ret = !Harness::run(specification);
275-
280+
ret = !Harness::run(specification);
281+
#if defined(MBEDTLS_PLATFORM_C)
282+
mbedtls_platform_teardown(NULL);
283+
#endif /* MBEDTLS_PLATFORM_C */
276284
return ret;
277285
}
278286

UNITTESTS/features/lorawan/loramaccrypto/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(unittest-test-sources
3535
stubs/cipher_stub.c
3636
stubs/aes_stub.c
3737
stubs/cmac_stub.c
38+
../features/nanostack/coap-service/test/coap-service/unittest/stub/mbedtls_stub.c
3839

3940
)
4041

UNITTESTS/stubs/LoRaMacCrypto_stub.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ LoRaMacCrypto::LoRaMacCrypto()
3030
{
3131
}
3232

33+
LoRaMacCrypto::~LoRaMacCrypto()
34+
{
35+
}
36+
3337
int LoRaMacCrypto::compute_mic(const uint8_t *, uint16_t, const uint8_t *, uint32_t, uint32_t,
3438
uint8_t dir, uint32_t, uint32_t *)
3539
{

features/device_key/source/DeviceKey.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#if DEVICEKEY_ENABLED
2020
#include "mbedtls/config.h"
2121
#include "mbedtls/cmac.h"
22+
#include "mbedtls/platform.h"
2223
#include "KVStore.h"
2324
#include "TDBStore.h"
2425
#include "KVMap.h"
@@ -59,15 +60,25 @@ namespace mbed {
5960

6061
DeviceKey::DeviceKey()
6162
{
63+
6264
int ret = kv_init_storage_config();
6365
if (ret != MBED_SUCCESS) {
6466
tr_error("DeviceKey: Fail to initialize KvStore configuration.");
6567
}
68+
#if defined(MBEDTLS_PLATFORM_C)
69+
ret = mbedtls_platform_setup(NULL);
70+
if (ret != MBED_SUCCESS) {
71+
tr_error("DeviceKey: Fail in mbedtls_platform_setup.");
72+
}
73+
#endif /* MBEDTLS_PLATFORM_C */
6674
return;
6775
}
6876

6977
DeviceKey::~DeviceKey()
7078
{
79+
#if defined(MBEDTLS_PLATFORM_C)
80+
mbedtls_platform_teardown(NULL);
81+
#endif /* MBEDTLS_PLATFORM_C */
7182
return;
7283
}
7384

features/lorawan/lorastack/mac/LoRaMacCrypto.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,26 @@
2828

2929
#include "LoRaMacCrypto.h"
3030
#include "system/lorawan_data_structures.h"
31+
#include "mbedtls/platform.h"
3132

3233

3334
#if defined(MBEDTLS_CMAC_C) && defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_C)
3435

3536
LoRaMacCrypto::LoRaMacCrypto()
3637
{
38+
#if defined(MBEDTLS_PLATFORM_C)
39+
int ret = mbedtls_platform_setup(NULL);
40+
if (ret != 0) {
41+
MBED_ASSERT(0 && "LoRaMacCrypto: Fail in mbedtls_platform_setup.");
42+
}
43+
#endif /* MBEDTLS_PLATFORM_C */
44+
}
45+
46+
LoRaMacCrypto::~LoRaMacCrypto()
47+
{
48+
#if defined(MBEDTLS_PLATFORM_C)
49+
mbedtls_platform_teardown(NULL);
50+
#endif /* MBEDTLS_PLATFORM_C */
3751
}
3852

3953
int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size,
@@ -291,6 +305,10 @@ LoRaMacCrypto::LoRaMacCrypto()
291305
MBED_ASSERT(0 && "[LoRaCrypto] Must enable AES, CMAC & CIPHER from mbedTLS");
292306
}
293307

308+
LoRaMacCrypto::~LoRaMacCrypto()
309+
{
310+
}
311+
294312
// If mbedTLS is not configured properly, these dummies will ensure that
295313
// user knows what is wrong and in addition to that these ensure that
296314
// Mbed-OS compiles properly under normal conditions where LoRaWAN in conjunction

features/lorawan/lorastack/mac/LoRaMacCrypto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SPDX-License-Identifier: BSD-3-Clause
3030
#ifndef MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
3131
#define MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
3232

33+
#include "mbedtls/config.h"
3334
#include "mbedtls/aes.h"
3435
#include "mbedtls/cmac.h"
3536

@@ -41,6 +42,11 @@ class LoRaMacCrypto {
4142
*/
4243
LoRaMacCrypto();
4344

45+
/**
46+
* Destructor
47+
*/
48+
~LoRaMacCrypto();
49+
4450
/**
4551
* Computes the LoRaMAC frame MIC field
4652
*

features/lwipstack/lwip/src/apps/snmp/lwip_snmpv3_mbedtls.c

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
#include "mbedtls/md5.h"
4949
#include "mbedtls/sha1.h"
50+
#include "mbedtls/platform.h"
5051

5152
err_t
5253
snmpv3_auth(struct snmp_pbuf_stream* stream, u16_t length,
@@ -59,19 +60,24 @@ snmpv3_auth(struct snmp_pbuf_stream* stream, u16_t length,
5960
struct snmp_pbuf_stream read_stream;
6061
snmp_pbuf_stream_init(&read_stream, stream->pbuf, stream->offset, stream->length);
6162

63+
#if defined(MBEDTLS_PLATFORM_C)
64+
if (mbedtls_platform_setup(NULL) != 0) {
65+
return ERR_ARG;
66+
}
67+
#endif /* MBEDTLS_PLATFORM_C */
6268
if (algo == SNMP_V3_AUTH_ALGO_MD5) {
6369
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
6470
key_len = SNMP_V3_MD5_LEN;
6571
} else if (algo == SNMP_V3_AUTH_ALGO_SHA) {
6672
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
6773
key_len = SNMP_V3_SHA_LEN;
6874
} else {
69-
return ERR_ARG;
75+
goto platform_teardown;
7076
}
7177

7278
mbedtls_md_init(&ctx);
7379
if(mbedtls_md_setup(&ctx, md_info, 1) != 0) {
74-
return ERR_ARG;
80+
goto platform_teardown;
7581
}
7682

7783
if (mbedtls_md_hmac_starts(&ctx, key, key_len) != 0) {
@@ -95,10 +101,17 @@ snmpv3_auth(struct snmp_pbuf_stream* stream, u16_t length,
95101
}
96102

97103
mbedtls_md_free(&ctx);
104+
#if defined(MBEDTLS_PLATFORM_C)
105+
mbedtls_platform_teardown(NULL);
106+
#endif /* MBEDTLS_PLATFORM_C */
98107
return ERR_OK;
99108

100109
free_md:
101110
mbedtls_md_free(&ctx);
111+
platform_teardown:
112+
#if defined(MBEDTLS_PLATFORM_C)
113+
mbedtls_platform_teardown(NULL);
114+
#endif /* MBEDTLS_PLATFORM_C */
102115
return ERR_ARG;
103116
}
104117

@@ -117,6 +130,11 @@ snmpv3_crypt(struct snmp_pbuf_stream* stream, u16_t length,
117130
struct snmp_pbuf_stream write_stream;
118131
snmp_pbuf_stream_init(&read_stream, stream->pbuf, stream->offset, stream->length);
119132
snmp_pbuf_stream_init(&write_stream, stream->pbuf, stream->offset, stream->length);
133+
#if defined(MBEDTLS_PLATFORM_C)
134+
if (mbedtls_platform_setup(NULL) != 0) {
135+
return ERR_ARG;
136+
}
137+
#endif /* MBEDTLS_PLATFORM_C */
120138
mbedtls_cipher_init(&ctx);
121139

122140
if (algo == SNMP_V3_PRIV_ALGO_DES) {
@@ -126,15 +144,15 @@ snmpv3_crypt(struct snmp_pbuf_stream* stream, u16_t length,
126144

127145
/* RFC 3414 mandates padding for DES */
128146
if ((length & 0x07) != 0) {
129-
return ERR_ARG;
147+
goto platform_teardown;
130148
}
131149

132150
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_CBC);
133151
if(mbedtls_cipher_setup(&ctx, cipher_info) != 0) {
134-
return ERR_ARG;
152+
goto platform_teardown
135153
}
136154
if(mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE) != 0) {
137-
return ERR_ARG;
155+
goto platform_teardown;
138156
}
139157
if(mbedtls_cipher_setkey(&ctx, key, 8*8, (mode == SNMP_V3_PRIV_MODE_ENCRYPT)? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT) != 0) {
140158
goto error;
@@ -174,7 +192,7 @@ snmpv3_crypt(struct snmp_pbuf_stream* stream, u16_t length,
174192

175193
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CFB128);
176194
if(mbedtls_cipher_setup(&ctx, cipher_info) != 0) {
177-
return ERR_ARG;
195+
goto platform_teardown;
178196
}
179197
if(mbedtls_cipher_setkey(&ctx, key, 16*8, (mode == SNMP_V3_PRIV_MODE_ENCRYPT)? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT) != 0) {
180198
goto error;
@@ -209,15 +227,19 @@ snmpv3_crypt(struct snmp_pbuf_stream* stream, u16_t length,
209227
snmp_pbuf_stream_write(&write_stream, out_byte);
210228
}
211229
} else {
212-
return ERR_ARG;
230+
goto platform_teardown;
213231
}
214232

215233
mbedtls_cipher_free(&ctx);
216234
return ERR_OK;
217235

218236
error:
219237
mbedtls_cipher_free(&ctx);
220-
return ERR_OK;
238+
platform_teardown:
239+
#if defined(MBEDTLS_PLATFORM_C)
240+
mbedtls_platform_teardown(NULL);
241+
#endif /* MBEDTLS_PLATFORM_C */
242+
return ERR_ARG;
221243
}
222244

223245
#endif /* LWIP_SNMP_V3_CRYPTO */
@@ -237,6 +259,11 @@ snmpv3_password_to_key_md5(
237259
u8_t i;
238260
u32_t count = 0;
239261

262+
#if defined(MBEDTLS_PLATFORM_C)
263+
if (mbedtls_platform_setup(NULL) != 0) {
264+
goto end;
265+
}
266+
#endif /* MBEDTLS_PLATFORM_C */
240267
mbedtls_md5_init(&MD); /* initialize MD5 */
241268
mbedtls_md5_starts(&MD);
242269

@@ -272,6 +299,11 @@ snmpv3_password_to_key_md5(
272299
mbedtls_md5_finish(&MD, key);
273300

274301
mbedtls_md5_free(&MD);
302+
303+
end:
304+
#if defined(MBEDTLS_PLATFORM_C)
305+
mbedtls_platform_teardown(NULL);
306+
#endif /* MBEDTLS_PLATFORM_C */
275307
return;
276308
}
277309

@@ -290,6 +322,11 @@ snmpv3_password_to_key_sha(
290322
u8_t i;
291323
u32_t count = 0;
292324

325+
#if defined(MBEDTLS_PLATFORM_C)
326+
if (mbedtls_platform_setup(NULL) != 0) {
327+
goto end;
328+
}
329+
#endif /* MBEDTLS_PLATFORM_C */
293330
mbedtls_sha1_init(&SH); /* initialize SHA */
294331
mbedtls_sha1_starts(&SH);
295332

@@ -325,6 +362,11 @@ snmpv3_password_to_key_sha(
325362
mbedtls_sha1_finish(&SH, key);
326363

327364
mbedtls_sha1_free(&SH);
365+
366+
end:
367+
#if defined(MBEDTLS_PLATFORM_C)
368+
mbedtls_platform_teardown(NULL);
369+
#endif /* MBEDTLS_PLATFORM_C */
328370
return;
329371
}
330372

features/mbedtls/platform/src/mbed_trng.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@
1717
#if DEVICE_TRNG
1818

1919
#include "hal/trng_api.h"
20+
#include "platform/SingletonPtr.h"
2021
#include "platform/PlatformMutex.h"
2122

23+
SingletonPtr<PlatformMutex> mbedtls_mutex;
24+
2225
extern "C"
2326
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen ) {
24-
static PlatformMutex trng_mutex;
2527
trng_t trng_obj;
26-
trng_mutex.lock();
28+
mbedtls_mutex->lock();
2729
trng_init(&trng_obj);
2830
int ret = trng_get_bytes(&trng_obj, output, len, olen);
2931
trng_free(&trng_obj);
30-
trng_mutex.unlock();
32+
mbedtls_mutex->unlock();
3133
return ret;
3234
}
3335

features/mbedtls/platform/src/platform_alt.c renamed to features/mbedtls/platform/src/platform_alt.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,38 @@
2020

2121
#include "mbedtls/platform.h"
2222
#if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT)
23-
#include "mbed_critical.h"
23+
#include "platform/SingletonPtr.h"
24+
#include "platform/PlatformMutex.h"
2425

2526
mbedtls_platform_context plat_ctx = { { 0 } };
27+
extern SingletonPtr<PlatformMutex> mbedtls_mutex;
2628

2729
int mbedtls_platform_setup( mbedtls_platform_context *unused_ctx )
2830
{
2931
int ret = 0;
30-
31-
core_util_atomic_incr_u32( ( volatile uint32_t * )&plat_ctx.reference_count, 1 );
32+
mbedtls_mutex->lock();
33+
++plat_ctx.reference_count;
3234

3335
if( plat_ctx.reference_count == 1 )
3436
{
3537
/* call platform specific code to setup crypto driver */
3638
ret = crypto_platform_setup( &plat_ctx.platform_impl_ctx );
3739
}
40+
mbedtls_mutex->unlock();
3841
return ( ret );
3942
}
4043

4144
void mbedtls_platform_teardown( mbedtls_platform_context *unused_ctx )
4245
{
43-
core_util_atomic_decr_u32( ( volatile uint32_t * )&plat_ctx.reference_count, 1 );
46+
mbedtls_mutex->lock();
47+
--plat_ctx.reference_count;
4448
if( plat_ctx.reference_count < 1 )
4549
{
4650
/* call platform specific code to terminate crypto driver */
4751
crypto_platform_terminate( &plat_ctx.platform_impl_ctx );
4852
plat_ctx.reference_count = 0;
4953
}
54+
mbedtls_mutex->unlock();
5055
}
5156

5257
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ static int coap_security_handler_init(coap_security_t *sec)
102102
const int entropy_source_type = MBEDTLS_ENTROPY_SOURCE_WEAK;
103103
#endif
104104

105+
#if defined(MBEDTLS_PLATFORM_C)
106+
if (mbedtls_platform_setup(NULL) != 0)
107+
return -1;
108+
#endif /* MBEDTLS_PLATFORM_C */
109+
105110
mbedtls_ssl_init(&sec->_ssl);
106111
mbedtls_ssl_config_init(&sec->_conf);
107112
mbedtls_ctr_drbg_init(&sec->_ctr_drbg);
@@ -153,6 +158,9 @@ static void coap_security_handler_reset(coap_security_t *sec)
153158
mbedtls_ctr_drbg_free(&sec->_ctr_drbg);
154159
mbedtls_ssl_config_free(&sec->_conf);
155160
mbedtls_ssl_free(&sec->_ssl);
161+
#if defined(MBEDTLS_PLATFORM_C)
162+
mbedtls_platform_teardown(NULL);
163+
#endif /* MBEDTLS_PLATFORM_C */
156164
}
157165

158166

0 commit comments

Comments
 (0)