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 crc.c
44 **
45 ** Common API of crc.
46 ** @link crcGroup Some description @endlink
47 **
48 ** - 2017-05-16
49 **
50 ******************************************************************************/
51
52 /*******************************************************************************
53 * Include files
54 ******************************************************************************/
55 #include "aes.h"
56 /**
57 *******************************************************************************
58 ** \addtogroup CrcGroup
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 * \brief
88 * ADC初始化
89 *
90 * \param [in] pu32Data 待加密数据
91 * \param [in] pu32Key 加密KEY
92 * \param [out] pu32Cipher 加密后数据
93 *
94 * \retval en_result_t Ok: 配置成功
95 * \retval en_result_t ErrorInvalidParameter: 无效参数
96 */
AES_Encrypt(uint32_t * pu32Data,uint32_t * pu32Key,uint32_t * pu32Cipher)97 en_result_t AES_Encrypt(uint32_t* pu32Data, uint32_t *pu32Key, uint32_t *pu32Cipher)
98 {
99 if ((NULL == pu32Data)||(NULL == pu32Key)||(NULL == pu32Cipher))
100 {
101 return ErrorInvalidParameter;
102 }
103
104 //Key config
105 M0P_AES->KEY0 = pu32Key[0];
106 M0P_AES->KEY1 = pu32Key[1];
107 M0P_AES->KEY2 = pu32Key[2];
108 M0P_AES->KEY3 = pu32Key[3];
109
110 //Data config
111 M0P_AES->DATA0 = pu32Data[0];
112 M0P_AES->DATA1 = pu32Data[1];
113 M0P_AES->DATA2 = pu32Data[2];
114 M0P_AES->DATA3 = pu32Data[3];
115
116 M0P_AES->CR_f.MODE = 0;//Encry
117 M0P_AES->CR_f.START = 1;
118 while(M0P_AES->CR_f.START == 1)
119 {
120 ;
121 }
122 pu32Cipher[0] = M0P_AES->DATA0;
123 pu32Cipher[1] = M0P_AES->DATA1;
124 pu32Cipher[2] = M0P_AES->DATA2;
125 pu32Cipher[3] = M0P_AES->DATA3;
126 return Ok;
127 }
128
129
130 /**
131 * \brief
132 * ADC初始化
133 *
134 * \param [in] pu32Cipher 待解密数据
135 * \param [in] pu32Key 加密KEY
136 * \param [out] pu32Data 解密后数据
137 *
138 * \retval en_result_t Ok: 配置成功
139 * \retval en_result_t ErrorInvalidParameter: 无效参数
140 */
AES_Decrypt(uint32_t * pu32Cipher,uint32_t * pu32Key,uint32_t * pu32Plaintext)141 en_result_t AES_Decrypt(uint32_t *pu32Cipher,uint32_t *pu32Key, uint32_t* pu32Plaintext)
142 {
143 if ((NULL == pu32Plaintext)||(NULL == pu32Key)||(NULL == pu32Cipher))
144 {
145 return ErrorInvalidParameter;
146 }
147
148 //Key config
149 M0P_AES->KEY0 = pu32Key[0];
150 M0P_AES->KEY1 = pu32Key[1];
151 M0P_AES->KEY2 = pu32Key[2];
152 M0P_AES->KEY3 = pu32Key[3];
153
154 //Data config
155 M0P_AES->DATA0 = pu32Cipher[0];
156 M0P_AES->DATA1 = pu32Cipher[1];
157 M0P_AES->DATA2 = pu32Cipher[2];
158 M0P_AES->DATA3 = pu32Cipher[3];
159
160 M0P_AES->CR_f.MODE = 1;//UnEncry
161 M0P_AES->CR_f.START = 1;
162 while(M0P_AES->CR_f.START == 1)
163 {
164 ;
165 }
166 pu32Plaintext[0] = M0P_AES->DATA0;
167 pu32Plaintext[1] = M0P_AES->DATA1;
168 pu32Plaintext[2] = M0P_AES->DATA2;
169 pu32Plaintext[3] = M0P_AES->DATA3;
170 return Ok;
171 }
172 //@} // CrcGroup
173
174 /*******************************************************************************
175 * EOF (not truncated)
176 ******************************************************************************/
177