1 /*
2 // Copyright (C) 2022 Beken Corporation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <common/sys_config.h>
16 #if (CONFIG_SOC_BK7251)
17 #include <os/os.h>
18 #include "bk_uart.h"
19 #include <os/str.h>
20 #include <os/mem.h>
21
22 #include <common/bk_kernel_err.h>
23 #include "hal_aes.h"
24 #include "security.h"
25
26 beken_mutex_t hal_aes_mutex = NULL;
27
hal_aes_done_callback(void * param)28 static void hal_aes_done_callback(void *param)
29 {
30
31 }
32
hal_aes_init(void * ctx)33 void hal_aes_init(void *ctx)
34 {
35 bk_err_t result;
36
37 if (hal_aes_mutex == NULL) {
38 result = rtos_init_mutex(&hal_aes_mutex);
39 if (result != kNoErr) {
40 os_printf("aes mutex init failed\n");
41 return;
42 }
43
44 security_aes_init(hal_aes_done_callback, NULL);
45 os_printf("hal_aes_init done\r\n");
46 }
47 }
48
hal_aes_setkey_dec(void * ctx,const unsigned char * key,unsigned int keybits)49 int hal_aes_setkey_dec(void *ctx, const unsigned char *key, unsigned int keybits)
50 {
51 AES_RETURN ret;
52
53 rtos_lock_mutex(&hal_aes_mutex);
54 ret = security_aes_set_key(key, keybits);
55 rtos_unlock_mutex(&hal_aes_mutex);
56
57 return (ret == AES_OK) ? 0 : -1;
58 }
59
hal_aes_setkey_enc(void * ctx,const unsigned char * key,unsigned int keybits)60 int hal_aes_setkey_enc(void *ctx, const unsigned char *key, unsigned int keybits)
61 {
62 AES_RETURN ret;
63
64 rtos_lock_mutex(&hal_aes_mutex);
65 ret = security_aes_set_key(key, keybits);
66 rtos_unlock_mutex(&hal_aes_mutex);
67
68 return (ret == AES_OK) ? 0 : -1;
69 }
70
hal_aes_crypt_ecb(void * ctx,int mode,const unsigned char input[16],unsigned char output[16])71 int hal_aes_crypt_ecb(void *ctx,
72 int mode,
73 const unsigned char input[16],
74 unsigned char output[16])
75 {
76 AES_RETURN ret;
77
78 rtos_lock_mutex(&hal_aes_mutex);
79 ret = security_aes_set_block_data(input);
80 if (ret != AES_OK) {
81 rtos_unlock_mutex(&hal_aes_mutex);
82 return -1;
83 }
84
85 if (mode == DECODE)
86 mode = DECODE;
87 else
88 mode = ENCODE;
89
90 security_aes_start(mode);
91
92 ret = security_aes_get_result_data(output);
93 rtos_unlock_mutex(&hal_aes_mutex);
94
95 return (ret == AES_OK) ? 0 : -1;
96 }
97
hal_aes_free(void * ctx)98 void hal_aes_free(void *ctx)
99 {
100 if (hal_aes_mutex)
101 rtos_deinit_mutex(&hal_aes_mutex);
102
103 // the same operate as hal_aes_init
104 security_aes_init(NULL, NULL);
105
106 hal_aes_mutex = NULL;
107 }
108
109 #endif
110
111
112