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 "bk_arm_arch.h"
20 #include "bk_icu.h"
21 #include "bk_drv_model.h"
22
23 #include "bk_arm_arch.h"
24 #include "security_reg.h"
25 #include "security.h"
26 #include "hal_aes.h"
27 #include <driver/int.h>
28
29 static struct sec_done_des aes_done_callback = {NULL, NULL};
30
security_aes_start(unsigned int mode)31 int security_aes_start(unsigned int mode)
32 {
33 UINT32 reg;
34
35 reg = REG_READ(SECURITY_AES_CONFIG);
36 if (mode == ENCODE)
37 reg |= SECURITY_AES_ENCODE_BIT;
38 else
39 reg &= ~(SECURITY_AES_ENCODE_BIT);
40 REG_WRITE(SECURITY_AES_CONFIG, reg);
41
42 reg = REG_READ(SECURITY_AES_CTRL);
43 reg |= SECURITY_AES_AUTEO_BIT;
44 REG_WRITE(SECURITY_AES_CTRL, reg);
45
46 // wait
47 while ((REG_READ(SECURITY_AES_STATUS) & SECURITY_AES_VALID) == 0);
48
49 return AES_OK;
50 }
51
security_aes_init(sec_done_callback callback,void * param)52 int security_aes_init(sec_done_callback callback, void *param)
53 {
54 UINT32 reg;
55 GLOBAL_INT_DECLARATION();
56
57 reg = REG_READ(SECURITY_AES_CTRL);
58 reg &= ~(SECURITY_AES_INT_EN_BIT | SECURITY_AES_AUTEO_BIT |
59 SECURITY_AES_NEXT_BIT | SECURITY_AES_INIT_BIT);
60
61 REG_WRITE(SECURITY_AES_CTRL, reg);
62
63 REG_WRITE(SECURITY_AES_CTRL, 0);
64
65 GLOBAL_INT_DISABLE();
66 aes_done_callback.callback = callback;
67 aes_done_callback.param = param;
68 GLOBAL_INT_RESTORE();
69 return 0;
70 }
71
security_aes_set_key(const unsigned char * key,unsigned int keybits)72 int security_aes_set_key(const unsigned char *key, unsigned int keybits)
73 {
74 UINT32 mode, reg;
75 int end_reg_pos = 0;
76
77 if (keybits == 128) {
78 mode = AES128;
79 end_reg_pos = 3;
80 } else if (keybits == 192) {
81 mode = AES192;
82 end_reg_pos = 5;
83 } else if (keybits == 256) {
84 mode = AES256;
85 end_reg_pos = 7;
86 } else {
87 os_printf("key size:%d, only support 128/192/265 bits\r\n");
88 return AES_KEYLEN_ERR;
89 }
90
91 for (int i = 0, j = 0; i < 8; i++) {
92 // reg SECURITY_AES_KEY0 is key's MSB, no mater how long the key is
93 // so key should set start from 0, end of end_reg_pos, others set to 0
94 if (i <= end_reg_pos) {
95 // in SECURITY_AES_KEYx, MSB first.
96 // key's addr may no 4byte align, so copy like this
97 UINT8 *data = (UINT8 *)®
98
99 data[0] = key[4 * j + 3];
100 data[1] = key[4 * j + 2];
101 data[2] = key[4 * j + 1];
102 data[3] = key[4 * j + 0];
103
104 REG_WRITE(SECURITY_AES_KEY_X(i), reg);
105 j++;
106 } else {
107 reg = 0;
108 REG_WRITE(SECURITY_AES_KEY_X(i), reg);
109 }
110 }
111
112 reg = REG_READ(SECURITY_AES_CONFIG);
113 reg &= ~(SECURITY_AES_MODE_MASK << SECURITY_AES_MODE_POSI);
114 reg |= ((mode & SECURITY_AES_MODE_MASK) << SECURITY_AES_MODE_POSI);
115
116 REG_WRITE(SECURITY_AES_CONFIG, reg);
117
118 return AES_OK;
119 }
120
security_aes_set_block_data(const unsigned char * block_data)121 int security_aes_set_block_data(const unsigned char *block_data)
122 {
123 UINT32 tmp_data;
124
125 for (int i = 0; i < 4; i++) {
126 UINT8 *data = (UINT8 *)&tmp_data;
127
128 data[0] = block_data[4 * i + 3];
129 data[1] = block_data[4 * i + 2];
130 data[2] = block_data[4 * i + 1];
131 data[3] = block_data[4 * i + 0];
132
133 REG_WRITE(SECURITY_AES_BLOCK_X(i), tmp_data);
134 }
135
136 return AES_OK;
137 }
138
security_aes_get_result_data(unsigned char * pul_data)139 int security_aes_get_result_data(unsigned char *pul_data)
140 {
141 UINT32 tmp_data;
142
143 for (int i = 0; i < 4; i++) {
144 UINT8 *data = (UINT8 *)&tmp_data;
145
146 tmp_data = REG_READ(SECURITY_AES_RESULT_X(i));
147
148 pul_data[4 * i + 0] = data[3];
149 pul_data[4 * i + 1] = data[2];
150 pul_data[4 * i + 2] = data[1];
151 pul_data[4 * i + 3] = data[0];
152 }
153
154 return AES_OK;
155 }
156
bk_secrity_isr(void)157 void bk_secrity_isr(void)
158 {
159 unsigned long secrity_state;
160
161 secrity_state = REG_READ(SECURITY_AES_STATUS);
162 if ((secrity_state & SECURITY_AES_INT_FLAG) == SECURITY_AES_INT_FLAG) {
163 REG_WRITE(SECURITY_AES_STATUS, secrity_state);
164 if (aes_done_callback.callback)
165 aes_done_callback.callback(aes_done_callback.param);
166 }
167
168 secrity_state = REG_READ(SECURITY_SHA_STATUS);
169 if ((secrity_state & 0x05) == 0x05)
170 REG_WRITE(SECURITY_SHA_STATUS, secrity_state);
171
172 secrity_state = REG_READ(SECURITY_RSA_STATE);
173 if ((secrity_state & 0x03) == 0x03)
174 REG_WRITE(SECURITY_RSA_STATE, secrity_state);
175 }
176
bk_secrity_init(void)177 void bk_secrity_init(void)
178 {
179 bk_int_isr_register(INT_SRC_SECURITY, bk_secrity_isr, NULL);
180 }
181
bk_secrity_exit(void)182 void bk_secrity_exit(void)
183 {
184 }
185
186 #endif
187
188