1 /******************************************************************************
2 *Copyright(C)2017, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 
43 /** \file aes.c
44  **
45  ** Common API of AES.
46  ** @link AesGroup Some description @endlink
47  **
48  **   - 2019-04-16
49  **
50  ******************************************************************************/
51 
52 /*******************************************************************************
53  * Include files
54  ******************************************************************************/
55 #include "hc32l196_aes.h"
56 /**
57  *******************************************************************************
58  ** \addtogroup AesGroup
59  ******************************************************************************/
60 //@{
61 
62 /*******************************************************************************
63  * Local pre-processor symbols/macros ('#define')
64  ******************************************************************************/
65 
66 /*******************************************************************************
67  * Global variable definitions (declared in header file with 'extern')
68  ******************************************************************************/
69 
70 /*******************************************************************************
71  * Local type definitions ('typedef')
72  ******************************************************************************/
73 
74 /*******************************************************************************
75  * Local variable definitions ('static')
76  ******************************************************************************/
77 
78 /*******************************************************************************
79  * Local function prototypes ('static')
80  ******************************************************************************/
81 
82 
83 /*******************************************************************************
84  * Function implementation - global ('extern') and local ('static')
85  ******************************************************************************/
86 
87 /**
88  * \brief
89  *          AES 加密
90  *
91  * \param   [in]   pstcAesCfg    AES 配置结构体 @ref stc_aes_cfg_t
92  *
93  * \retval  en_result_t  Ok:  配置成功
94  * \retval  en_result_t  ErrorInvalidParameter: 无效参数
95  */
AES_Encrypt(stc_aes_cfg_t * pstcAesCfg)96 en_result_t AES_Encrypt(stc_aes_cfg_t* pstcAesCfg)
97 {
98     if (NULL == pstcAesCfg)
99     {
100         return ErrorInvalidParameter;
101     }
102 
103     M0P_AES->CR_f.KEYSIZE = pstcAesCfg->enKeyLen;
104 
105     //Key cfg
106     M0P_AES->KEY0 = pstcAesCfg->pu32Key[0];
107     M0P_AES->KEY1 = pstcAesCfg->pu32Key[1];
108     M0P_AES->KEY2 = pstcAesCfg->pu32Key[2];
109     M0P_AES->KEY3 = pstcAesCfg->pu32Key[3];
110 
111     if(AesKey192 == pstcAesCfg->enKeyLen)
112     {
113             M0P_AES->KEY4 = pstcAesCfg->pu32Key[4];
114             M0P_AES->KEY5 = pstcAesCfg->pu32Key[5];
115     }
116 
117     if(AesKey256 == pstcAesCfg->enKeyLen)
118     {
119             M0P_AES->KEY4 = pstcAesCfg->pu32Key[4];
120             M0P_AES->KEY5 = pstcAesCfg->pu32Key[5];
121             M0P_AES->KEY6 = pstcAesCfg->pu32Key[6];
122             M0P_AES->KEY7 = pstcAesCfg->pu32Key[7];
123     }
124 
125     //Data cfg
126     M0P_AES->DATA0 = pstcAesCfg->pu32Plaintext[0];
127     M0P_AES->DATA1 = pstcAesCfg->pu32Plaintext[1];
128     M0P_AES->DATA2 = pstcAesCfg->pu32Plaintext[2];
129     M0P_AES->DATA3 = pstcAesCfg->pu32Plaintext[3];
130 
131     M0P_AES->CR_f.MODE = 0;//Encry
132     M0P_AES->CR_f.START = 1;
133     while(M0P_AES->CR_f.START == 1)
134     {
135         ;
136     }
137     pstcAesCfg->pu32Cipher[0] = M0P_AES->DATA0;
138     pstcAesCfg->pu32Cipher[1] = M0P_AES->DATA1;
139     pstcAesCfg->pu32Cipher[2] = M0P_AES->DATA2;
140     pstcAesCfg->pu32Cipher[3] = M0P_AES->DATA3;
141     return Ok;
142 }
143 
144 
145 /**
146  * \brief
147  *          AES 解密
148  *
149  * \param   [in]   pstcAesCfg    AES 配置结构体 @ref stc_aes_cfg_t
150  *
151  * \retval  en_result_t  Ok:  配置成功
152  * \retval  en_result_t  ErrorInvalidParameter: 无效参数
153  */
AES_Decrypt(stc_aes_cfg_t * pstcAesCfg)154 en_result_t AES_Decrypt(stc_aes_cfg_t* pstcAesCfg)
155 {
156     if (NULL == pstcAesCfg)
157     {
158         return ErrorInvalidParameter;
159     }
160 
161     M0P_AES->CR_f.KEYSIZE = pstcAesCfg->enKeyLen;
162 
163     //Key cfg
164     M0P_AES->KEY0 = pstcAesCfg->pu32Key[0];
165     M0P_AES->KEY1 = pstcAesCfg->pu32Key[1];
166     M0P_AES->KEY2 = pstcAesCfg->pu32Key[2];
167     M0P_AES->KEY3 = pstcAesCfg->pu32Key[3];
168 
169     if(AesKey192 == pstcAesCfg->enKeyLen)
170     {
171             M0P_AES->KEY4 = pstcAesCfg->pu32Key[4];
172             M0P_AES->KEY5 = pstcAesCfg->pu32Key[5];
173     }
174 
175     if(AesKey256 == pstcAesCfg->enKeyLen)
176     {
177             M0P_AES->KEY4 = pstcAesCfg->pu32Key[4];
178             M0P_AES->KEY5 = pstcAesCfg->pu32Key[5];
179             M0P_AES->KEY6 = pstcAesCfg->pu32Key[6];
180             M0P_AES->KEY7 = pstcAesCfg->pu32Key[7];
181     }
182 
183     //Data cfg
184     M0P_AES->DATA0 = pstcAesCfg->pu32Cipher[0];
185     M0P_AES->DATA1 = pstcAesCfg->pu32Cipher[1];
186     M0P_AES->DATA2 = pstcAesCfg->pu32Cipher[2];
187     M0P_AES->DATA3 = pstcAesCfg->pu32Cipher[3];
188 
189     M0P_AES->CR_f.MODE = 1;//UnEncry
190     M0P_AES->CR_f.START = 1;
191     while(M0P_AES->CR_f.START == 1)
192     {
193         ;
194     }
195     pstcAesCfg->pu32Plaintext[0] = M0P_AES->DATA0;
196     pstcAesCfg->pu32Plaintext[1] = M0P_AES->DATA1;
197     pstcAesCfg->pu32Plaintext[2] = M0P_AES->DATA2;
198     pstcAesCfg->pu32Plaintext[3] = M0P_AES->DATA3;
199     return Ok;
200 }
201 
202 //@} // AesGroup
203 
204 /*******************************************************************************
205  * EOF (not truncated)
206  ******************************************************************************/
207