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