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 /********************************************************************************************************
20 * @file aes.c
21 *
22 * @brief This is the source file for B91
23 *
24 * @author Driver Group
25 *
26 *******************************************************************************************************/
27 #include "aes.h"
28 #include "compiler.h"
29
30 /**********************************************************************************************************************
31 * local constants *
32 *********************************************************************************************************************/
33
34
35 /**********************************************************************************************************************
36 * local macro *
37 *********************************************************************************************************************/
38
39
40 /**********************************************************************************************************************
41 * local data type *
42 *********************************************************************************************************************/
43
44
45 /**********************************************************************************************************************
46 * global variable *
47 *********************************************************************************************************************/
48 _attribute_aes_data_sec_ unsigned int aes_data_buff[8];
49 unsigned int aes_base_addr = 0xc0000000;
50 /**********************************************************************************************************************
51 * local variable *
52 *********************************************************************************************************************/
53
54 /**********************************************************************************************************************
55 * local function prototype *
56 *********************************************************************************************************************/
57 /**
58 * @brief This function refer to wait aes encrypt/decrypt done.
59 * @return none.
60 */
61 static inline void aes_wait_done(void);
62 /**********************************************************************************************************************
63 * global function implementation *
64 *********************************************************************************************************************/
65 /**
66 * @brief This function refer to encrypt/decrypt to set key and data. AES module register must be used by word.
67 * All data need Little endian.
68 * @param[in] key - the key of encrypt/decrypt.
69 * @param[in] data - the data which to do encrypt/decrypt.
70 * @return none
71 */
aes_set_key_data(unsigned char * key,unsigned char * data)72 void aes_set_key_data(unsigned char *key, unsigned char* data)
73 {
74 unsigned int temp;
75 reg_embase_addr = aes_base_addr; //set the embase addr
76 for (unsigned char i = 0; i < 4; i++) {
77 temp = key[16-(4*i)-4]<<24 | key[16-(4*i)-3]<<16 | key[16-(4*i)-2]<<8 | key[16-(4*i)-1];
78 reg_aes_key(i) = temp;
79 temp = data[16-(4*i)-4]<<24 | data[16-(4*i)-3]<<16 | data[16-(4*i)-2]<<8 | data[16-(4*i)-1];
80 aes_data_buff[i] = temp;
81 }
82
83 reg_aes_ptr = (unsigned int)aes_data_buff;
84 }
85
86 /**
87 * @brief This function refer to encrypt/decrypt to get result. AES module register must be used by word.
88 * @param[in] result - the result of encrypt/decrypt, Little endian.
89 * @return none.
90 */
aes_get_result(unsigned char * result)91 void aes_get_result(unsigned char *result)
92 {
93 /* read out the result */
94 unsigned char *ptr = (unsigned char *)&aes_data_buff[4];
95 for (unsigned char i=0; i<16; i++) {
96 result[i] = ptr[15 - i];
97 }
98 }
99
100 /**
101 * @brief This function refer to encrypt. AES module register must be used by word, all data need big endian.
102 * @param[in] key - the key of encrypt.
103 * @param[in] plaintext - the plaintext of encrypt.
104 * @param[in] result - the result of encrypt.
105 * @return none
106 */
aes_encrypt(unsigned char * key,unsigned char * plaintext,unsigned char * result)107 int aes_encrypt(unsigned char *key, unsigned char* plaintext, unsigned char *result)
108 {
109
110 //set the key
111 aes_set_key_data(key, plaintext);
112
113 aes_set_mode(AES_ENCRYPT_MODE); //cipher mode
114
115 aes_wait_done();
116
117 aes_get_result(result);
118
119 return 1;
120 }
121
122 /**
123 * @brief This function refer to decrypt. AES module register must be used by word.all data need big endian.
124 * @param[in] key - the key of decrypt.
125 * @param[in] decrypttext - the text of decrypt.
126 * @param[in] result - the result of decrypt.
127 * @return none.
128 */
aes_decrypt(unsigned char * key,unsigned char * decrypttext,unsigned char * result)129 int aes_decrypt(unsigned char *key, unsigned char* decrypttext, unsigned char *result)
130 {
131 //set the key
132 aes_set_key_data(key, decrypttext);
133
134 aes_set_mode(AES_DECRYPT_MODE); //decipher mode
135
136 aes_wait_done();
137
138 aes_get_result(result);
139
140 return 1;
141 }
142 /**********************************************************************************************************************
143 * local function implementation *
144 *********************************************************************************************************************/
145 /**
146 * @brief This function refer to set the embase addr.
147 * @param[in] addr - the base addr of CEVA data.
148 * @return none.
149 */
aes_set_em_base_addr(unsigned int addr)150 void aes_set_em_base_addr(unsigned int addr){
151 aes_base_addr = addr; //set the embase addr
152 }
153
154 /**
155 * @brief This function refer to wait aes crypt done.
156 * @return none.
157 */
aes_wait_done(void)158 static inline void aes_wait_done(void)
159 {
160 while(FLD_AES_START == (reg_aes_mode & FLD_AES_START));
161 }
162
163 /**
164 * @brief This function is a getter for aes_data_buff
165 * @return pointer to aes_data_buff.
166 */
aes_data_buff_ptr_get(void)167 unsigned int * aes_data_buff_ptr_get(void)
168 {
169 return aes_data_buff;
170 }
171
172
173