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