1 /*****************************************************************************
2 *         Nationz Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2020, Nationz Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Nationz's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONZ "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL NATIONZ BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ****************************************************************************/
28 /*****************************************************************************
29 * File Name: HASH.h
30 * Function: Declaring HASH algorithm library API
31 * version: V1.2.0
32 * Author: zhang.zhenshan
33 * date: 2020-4-8
34 * ****************************************************************************/
35 
36 #ifndef _N32L43X_HASH_H_
37 #define _N32L43X_HASH_H_
38 
39 #include <stdint.h>
40 
41 #define  ALG_SHA1           (uint16_t)(0x0004)
42 #define  ALG_SHA224         (uint16_t)(0x000A)
43 #define  ALG_SHA256         (uint16_t)(0x000B)
44 //#define  ALG_MD5            (u16)(0x000C)
45 #define  ALG_SM3        (uint16_t)(0x0012)
46 
47 enum
48 {
49     HASH_SEQUENCE_TRUE = 0x0105A5A5,//save IV
50     HASH_SEQUENCE_FALSE = 0x010A5A5A, //not save IV
51     HASH_Init_OK = 0,//hash init success
52     HASH_Start_OK = 0,//hash update success
53     HASH_Update_OK = 0,//hash update success
54     HASH_Complete_OK = 0,//hash complete success
55     HASH_Close_OK = 0,//hash close success
56     HASH_ByteLenPlus_OK = 0,//byte length plus success
57     HASH_PadMsg_OK = 0,//message padding success
58     HASH_ProcMsgBuf_OK = 0, //message processing success
59     SHA1_Hash_OK = 0,//sha1 operation success
60     SM3_Hash_OK = 0,//sm3 operation success
61     SHA224_Hash_OK = 0,//sha224 operation success
62     SHA256_Hash_OK = 0,//sha256 operation success
63     //MD5_Hash_OK = 0,//MD5 operation success
64 
65     HASH_Init_ERROR = 0x01044400,//hash init error
66     HASH_Start_ERROR, //hash start error
67     HASH_Update_ERROR, //hash update error
68     HASH_ByteLenPlus_ERROR,//hash byte plus error
69 };
70 
71 struct _HASH_CTX_;
72 
73 typedef struct
74 {
75     const uint16_t HashAlgID;//choice hash algorithm
76     const uint32_t * const K, KLen;//K and word length of K
77     const uint32_t * const IV, IVLen;//IV and word length of IV
78     const uint32_t HASH_SACCR, HASH_HASHCTRL;//relate registers
79     const uint32_t BlockByteLen, BlockWordLen; //byte length of block, word length of block
80     const uint32_t DigestByteLen, DigestWordLen; //byte length of digest,word length of digest
81     const uint32_t Cycle; //interation times
82     uint32_t (* const ByteLenPlus)(uint32_t *, uint32_t); //function pointer
83     uint32_t (* const PadMsg)(struct _HASH_CTX_ *); //function pointer
84 }HASH_ALG;
85 
86 typedef struct _HASH_CTX_
87 {
88     const HASH_ALG  *hashAlg;//pointer to HASH_ALG
89     uint32_t        sequence;   // TRUE if the IV should be saved
90     uint32_t    IV[16];
91     uint32_t        msgByteLen[4];
92   uint8_t       msgBuf[128+4];
93   uint32_t      msgIdx;
94 }HASH_CTX;
95 
96 extern const HASH_ALG HASH_ALG_SHA1[1];
97 extern const HASH_ALG HASH_ALG_SHA224[1];
98 extern const HASH_ALG HASH_ALG_SHA256[1];
99 //extern const HASH_ALG HASH_ALG_MD5[1];
100 extern const HASH_ALG HASH_ALG_SM3[1];
101 
102 /**
103  * @brief  Hash init
104  * @param[in] ctx pointer to HASH_CTX struct
105  * @return HASH_Init_OK,  Hash init success; othets:  Hash init fail
106  * @note   1.Please refer to the demo in user guidance before using this function
107  */
108 uint32_t HASH_Init(HASH_CTX *ctx);
109 
110 /**
111  * @brief  Hash start
112  * @param[in] ctx pointer to HASH_CTX struct
113  * @return HASH_Start_OK,  Hash start success; othets:  Hash start fail
114  * @note   1.Please refer to the demo in user guidance before using this function
115  *         2.HASH_Init() should be recalled before use this function
116  */
117 uint32_t HASH_Start(HASH_CTX *ctx);
118 
119 /**
120  * @brief  Hash update
121  * @param[in] ctx pointer to HASH_CTX struct
122  * @param[in] in pointer to message
123  * @param[out] out pointer tohash result,digest
124  * @return HASH_Update_OK,  Hash update success; othets:  Hash update fail
125  * @note   1.Please refer to the demo in user guidance before using this function
126  *         2.HASH_Init() and HASH_Start() should be recalled before use this function
127  */
128 uint32_t HASH_Update(HASH_CTX *ctx,  uint8_t *in,  uint32_t byteLen);
129 
130 /**
131  * @brief  Hash complete
132  * @param[in] ctx pointer to HASH_CTX struct
133  * @param[out] out pointer tohash result,digest
134  * @return HASH_Complete_OK,  Hash complete success; othets:  Hash complete fail
135  * @note   1.Please refer to the demo in user guidance before using this function
136  *         2.HASH_Init(), HASH_Start() and HASH_Update() should be recalled before use this function
137  */
138 uint32_t HASH_Complete(HASH_CTX *ctx,  uint8_t *out);
139 
140 /**
141  * @brief  Hash close
142  * @return HASH_Close_OK, Hash close success; othets: Hash close fail
143  * @note   1.Please refer to the demo in user guidance before using this function
144  */
145 uint32_t HASH_Close(void);
146 
147 /**
148  * @brief  SM3 Hash for 256bits digest
149  * @param[in] in pointer to message
150  * @param[in] byte length of in
151  * @param[out] out pointer tohash result,digest
152  * @return SM3_Hash_OK, SM3 hash success; othets: SM3 hash fail
153  * @note   1.Please refer to the demo in user guidance before using this function
154  */
155 uint32_t SM3_Hash(uint8_t *in,uint32_t byteLen, uint8_t* out);
156 
157 
158 /**
159  * @brief  SHA1 Hash
160  * @param[in] in pointer to message
161  * @param[in] byte length of in
162  * @param[out] out pointer tohash result,digest
163  * @return SHA1_Hash_OK, SHA1 hash success; othets: SHA1 hash fail
164  * @note   1.Please refer to the demo in user guidance before using this function
165  */
166 uint32_t SHA1_Hash(uint8_t*in, uint32_t byteLen, uint8_t*out);
167 
168 /**
169  * @brief  SHA224 Hash
170  * @param[in] in pointer to message
171  * @param[in] byte length of in
172  * @param[out] out pointer tohash result,digest
173  * @return SHA224_Hash_OK, SHA224 hash success; othets: SHA224 hash fail
174  * @note   1.Please refer to the demo in user guidance before using this function
175  */
176 uint32_t SHA224_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
177 
178 
179 /**
180  * @brief  SHA256 Hash
181  * @param[in] in pointer to message
182  * @param[in] byte length of in
183  * @param[out] out pointer tohash result,digest
184  * @return SHA256_Hash_OK, SHA256 hash success; othets: SHA256 hash fail
185  * @note   1.Please refer to the demo in user guidance before using this function
186  */
187 uint32_t SHA256_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
188 
189 /**
190  * @brief  MD5 Hash
191  * @param[in] in pointer to message
192  * @param[in] byte length of in
193  * @param[in] out pointer tohash result,digest
194  * @return MD5_Hash_OK, MD5 hash success; othets: MD5 hash fail
195  * @note   1.Please refer to the demo in user guidance before using this function
196  */
197 //u32 MD5_Hash(u8* in,u32 byteLen, u8* out);
198 
199 /**
200  * @brief Get HASH lib version
201  * @param[out] type pointer one byte type information represents the type of the lib, like Commercial version.\
202  * @Bits 0~4 stands for Commercial (C), Security (S), Normal (N), Evaluation (E), Test (T), Bits 5~7 are reserved. e.g. 0x09 stands for CE version.
203  * @param[out] customer pointer one byte customer information represents customer ID. for example, 0x00 stands for standard version, 0x01 is for Tianyu customized version...
204  * @param[out] date pointer array which include three bytes date information. If the returned bytes are 18,9,13,this denotes September 13,2018
205  * @param[out] version pointer one byte version information represents develop version of the lib. e.g. 0x12 denotes version 1.2.
206  * @return none
207  * @1.You can recall this function to get RSA lib information
208  */
209 void HASH_Version(uint8_t*type, uint8_t*customer, uint8_t date[3], uint8_t *version);
210 
211 
212 #endif
213