Skip to content

Commit 66a97c7

Browse files
committed
mbed TLS: threading support - DRAFT
This is an implementation of mbed TLS threading abstraction layer using RTOS mutexes. Design decisions: - The is_valid flag is not checked in the init function, because it is a chance that an uninitialised variable has the value of one (it is a small chance, but it would result in incorrect behaviour) - The checks on the is_valid flag are not abbreviated because an uninitialised variable is nonzero in most of the cases - The type of a mutex is a pointer to void and not an incomplete struct, because we use definitions with mbedtls_threading_mutex_t in the library - There is a wrapper for mbedtls_threading_set_alt to make it more user friendly and to enable the use of static functions
1 parent ecbfaa7 commit 66a97c7

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

features/mbedtls/importer/adjust-config.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ add_code
7171
" \"functionality is not available\"\n" \
7272
"#endif\n"
7373

74+
# mbed OS has an alternate threading implementation
75+
conf set MBEDTLS_THREADING_C
76+
conf set MBEDTLS_THREADING_ALT
77+
7478
# not supported on mbed OS, nor used by mbed Client
7579
conf unset MBEDTLS_NET_C
7680
conf unset MBEDTLS_TIMING_C

features/mbedtls/inc/mbedtls/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@
12891289
*
12901290
* Uncomment this to allow your own alternate threading implementation.
12911291
*/
1292-
//#define MBEDTLS_THREADING_ALT
1292+
#define MBEDTLS_THREADING_ALT
12931293

12941294
/**
12951295
* \def MBEDTLS_THREADING_PTHREAD
@@ -2337,7 +2337,7 @@
23372337
*
23382338
* Enable this layer to allow use of mutexes within mbed TLS
23392339
*/
2340-
//#define MBEDTLS_THREADING_C
2340+
#define MBEDTLS_THREADING_C
23412341

23422342
/**
23432343
* \def MBEDTLS_TIMING_C
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBEDTLS_THREADING_ALT_H
18+
#define MBEDTLS_THREADING_ALT_H
19+
20+
#if defined(MBEDTLS_THREADING_ALT)
21+
22+
#ifdef __cplusplus
23+
#include "Mutex.h"
24+
25+
typedef struct
26+
{
27+
rtos::Mutex* mutex;
28+
char is_valid;
29+
} mbedtls_threading_mutex_t;
30+
#else
31+
typedef struct
32+
{
33+
void* mutex;
34+
char is_valid;
35+
} mbedtls_threading_mutex_t;
36+
#endif
37+
38+
#ifdef __cplusplus
39+
extern "C" {
40+
#endif
41+
42+
void mbedtls_threading_set_mbed( void );
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* MBEDTLS_THREADING_ALT */
49+
#endif /* threading_alt.h */
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if !defined(MBEDTLS_CONFIG_FILE)
18+
#include "mbedtls/config.h"
19+
#else
20+
#include MBEDTLS_CONFIG_FILE
21+
#endif
22+
23+
#if defined(MBEDTLS_THREADING_C)
24+
25+
#include "mbedtls/threading.h"
26+
27+
#if defined(MBEDTLS_THREADING_ALT)
28+
static void threading_mutex_init_mbed( mbedtls_threading_mutex_t *mutex )
29+
{
30+
if( mutex == NULL )
31+
return;
32+
33+
mutex->mutex = new rtos::Mutex();
34+
35+
mutex->is_valid = true;
36+
}
37+
38+
static void threading_mutex_free_mbed( mbedtls_threading_mutex_t *mutex )
39+
{
40+
if( mutex == NULL || mutex->is_valid != true )
41+
return;
42+
43+
delete mutex->mutex;
44+
45+
mutex->is_valid = false;
46+
}
47+
48+
static int threading_mutex_lock_mbed( mbedtls_threading_mutex_t *mutex )
49+
{
50+
if( mutex == NULL || mutex->is_valid != true )
51+
return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
52+
53+
if( mutex->mutex->lock() != osOK )
54+
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
55+
56+
return( 0 );
57+
}
58+
59+
static int threading_mutex_unlock_mbed( mbedtls_threading_mutex_t *mutex )
60+
{
61+
if( mutex == NULL || mutex->is_valid != true )
62+
return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA );
63+
64+
if( mutex->mutex->unlock() != osOK )
65+
return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
66+
67+
return( 0 );
68+
}
69+
70+
void mbedtls_threading_set_mbed( void )
71+
{
72+
mbedtls_threading_set_alt( threading_mutex_init_mbed,
73+
threading_mutex_free_mbed, threading_mutex_lock_mbed,
74+
threading_mutex_unlock_mbed );
75+
}
76+
77+
#endif /* MBEDTLS_THREADING_ALT */
78+
79+
#endif /* MBEDTLS_THREADING_C */

0 commit comments

Comments
 (0)