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.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 /**	@page AES
28  *
29  *	Introduction
30  *	===============
31  *	TLSRB91 supports hardware AES function.
32  *
33  *	API Reference
34  *	===============
35  *	Header File: aes.h
36  */
37 #ifndef _AES_H_
38 #define _AES_H_
39 
40 #include "compiler.h"
41 #include "./reg_include/aes_reg.h"
42 
43 /**********************************************************************************************************************
44  *                                         global constants                                                           *
45  *********************************************************************************************************************/
46 
47 /**********************************************************************************************************************
48  *                                           global macro                                                             *
49  *********************************************************************************************************************/
50 
51 /**********************************************************************************************************************
52  *                                         global data type                                                           *
53  *********************************************************************************************************************/
54 /**
55  * @brief AES mode.
56  */
57 typedef enum{
58 	AES_ENCRYPT_MODE	=  0,
59 	AES_DECRYPT_MODE	=  2,
60 }aes_mode_e;
61 /**********************************************************************************************************************
62  *                                     global variable declaration                                                    *
63  *********************************************************************************************************************/
64 
65 /**********************************************************************************************************************
66  *                                      global function prototype                                                     *
67  *********************************************************************************************************************/
68  /* @brief     This function refer to encrypt. AES module register must be used by word. , all data need big endian.
69  * @param[in] key       - the key of encrypt.
70  * @param[in] plaintext - the plaintext of encrypt.
71  * @param[in] result    - the result of encrypt.
72  * @return    none
73  */
74 int aes_encrypt(unsigned char *key, unsigned char* plaintext, unsigned char *result);
75 
76 /**
77  * @brief     This function refer to decrypt. AES module register must be used by word., all data need big endian.
78  * @param[in] key         - the key of decrypt.
79  * @param[in] decrypttext - the decrypttext of decrypt.
80  * @param[in] result      - the result of decrypt.
81  * @return    none.
82  */
83 int aes_decrypt(unsigned char *key, unsigned char* decrypttext, unsigned char *result);
84 
85 /**
86  * @brief     This function refer to set the base addr of data which use in CEVA module
87  * @param[in] addr - the base addr of CEVA data.
88  * @return    none.
89  */
90 void aes_set_em_base_addr(unsigned int addr);
91 
92 /**
93  * @brief     This function refer to encrypt/decrypt to set key and data. AES module register must be used by word.
94  * 				All data need Little endian.
95  * @param[in] key  - the key of encrypt/decrypt.
96  * @param[in] data - the data which to do encrypt/decrypt.
97  * @return    none
98  */
99 void aes_set_key_data(unsigned char *key, unsigned char* data);
100 
101 /**
102  * @brief     This function refer to encrypt/decrypt to get result. AES module register must be used by word.
103  * @param[in] result - the result of encrypt/decrypt. Little endian
104  * @return    none.
105  */
106 void aes_get_result(unsigned char *result);
107 
108 /**
109  * @brief     This function refer to set aes mode.
110  * @param[in] mode - the irq mask.
111  * @return    none.
112  */
aes_set_mode(aes_mode_e mode)113 static inline void aes_set_mode(aes_mode_e mode)
114 {
115 	reg_aes_mode = (FLD_AES_START | mode);
116 }
117 
118 /**
119  * @brief     This function refer to set aes irq mask.
120  * @param[in] mask - the irq mask.
121  * @return    none.
122  */
aes_set_irq_mask(aes_irq_e mask)123 static inline void aes_set_irq_mask(aes_irq_e mask)
124 {
125 	reg_aes_irq_mask |= mask;
126 }
127 
128 /**
129  * @brief     This function refer to clr aes irq mask.
130  * @param[in] mask - the irq mask.
131  * @return    none.
132  */
aes_clr_irq_mask(aes_irq_e mask)133 static inline void aes_clr_irq_mask(aes_irq_e mask)
134 {
135 	reg_aes_irq_mask &= (~mask);
136 }
137 
138 /**
139  * @brief     This function refer to get aes irq status.
140  * @param[in] status - the irq status to get.
141  * @return    none.
142  */
aes_get_irq_status(aes_irq_e status)143 static inline int aes_get_irq_status(aes_irq_e status)
144 {
145 	return (reg_aes_irq_status & status);
146 }
147 
148 /**
149  * @brief     This function refer to clr aes irq status.
150  * @param[in] status - the irq status to clear.
151  * @return    none.
152  */
aes_clr_irq_status(aes_irq_e status)153 static inline void aes_clr_irq_status(aes_irq_e status)
154 {
155 	reg_aes_clr_irq_status = (status);
156 }
157 
158 /**
159  * @brief     This function is a getter for aes_data_buff
160  * @return    pointer to aes_data_buff.
161  */
162 unsigned int * aes_data_buff_ptr_get(void);
163 
164 #endif /* _AES_H_ */
165