1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 #include "multithread.h"
20 #include "common.h"
21
22 #include <los_mux.h>
23
24 #include <ohos_init.h>
25
26 /****************************************************************
27 * If RTOS is used and cryptography stuffs are used
28 * from more than one thread implement exclusive access
29 * for HW crypto units depending on used operation system.
30 * See documentation for your RTOS.
31 ****************************************************************/
32
33 static struct {
34 UINT32 entropy;
35 UINT32 ecp;
36 UINT32 aes;
37 } g_mutexes;
38
mbedtls_entropy_lock(void)39 void mbedtls_entropy_lock(void)
40 {
41 LOS_MuxPend(g_mutexes.entropy, LOS_WAIT_FOREVER);
42 }
43
mbedtls_entropy_unlock(void)44 void mbedtls_entropy_unlock(void)
45 {
46 LOS_MuxPost(g_mutexes.entropy);
47 }
48
mbedtls_ecp_lock(void)49 void mbedtls_ecp_lock(void)
50 {
51 LOS_MuxPend(g_mutexes.ecp, LOS_WAIT_FOREVER);
52 }
53
mbedtls_ecp_unlock(void)54 void mbedtls_ecp_unlock(void)
55 {
56 LOS_MuxPost(g_mutexes.ecp);
57 }
58
mbedtls_aes_lock(void)59 void mbedtls_aes_lock(void)
60 {
61 LOS_MuxPend(g_mutexes.aes, LOS_WAIT_FOREVER);
62 }
63
mbedtls_aes_unlock(void)64 void mbedtls_aes_unlock(void)
65 {
66 LOS_MuxPost(g_mutexes.aes);
67 }
68
mbedtls_multithread_init(void)69 static void mbedtls_multithread_init(void)
70 {
71 UINT32 res;
72 res = LOS_MuxCreate(&g_mutexes.entropy);
73 if (res != LOS_OK) {
74 printf("LOS_MuxCreate(&g_mutexes.entropy) returned %x\r\n", res);
75 }
76
77 res = LOS_MuxCreate(&g_mutexes.ecp);
78 if (res != LOS_OK) {
79 printf("LOS_MuxCreate(&g_mutexes.ecp) returned %x\r\n", res);
80 }
81
82 res = LOS_MuxCreate(&g_mutexes.aes);
83 if (res != LOS_OK) {
84 printf("LOS_MuxCreate(&g_mutexes.aes) returned %x\r\n", res);
85 }
86 }
87
88 SYS_RUN_PRI(mbedtls_multithread_init, 0);
89