1 /**
2  *
3  * \file
4  *
5  * \brief WINC Crypto Application Interface.
6  *
7  * Copyright (c) 2016-2017 Atmel Corporation. All rights reserved.
8  *
9  * \asf_license_start
10  *
11  * \page License
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  *
19  * 2. Redistributions in binary form must reproduce the above copyright notice,
20  *    this list of conditions and the following disclaimer in the documentation
21  *    and/or other materials provided with the distribution.
22  *
23  * 3. The name of Atmel may not be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
29  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
30  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * \asf_license_stop
39  *
40  */
41 
42 #ifndef __M2M_CRYPTO_H__
43 #define __M2M_CRYPTO_H__
44 
45 
46 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
47 INCLUDES
48 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
49 
50 
51 #include "common/include/nm_common.h"
52 #include "driver/include/m2m_types.h"
53 #include "driver/source/m2m_hif.h"
54 
55 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
56 MACROS
57 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
58 #define M2M_MAX_RSA_LEN					(256)
59 #define M2M_SHA256_DIGEST_LEN			32
60 #define M2M_SHA256_MAX_DATA				(M2M_BUFFER_MAX_SIZE - M2M_SHA256_CONTEXT_BUFF_LEN - M2M_HIF_HDR_OFFSET)
61 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
62 DATA TYPES
63 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
64 
65 /*!
66 @struct	\
67 	tstrM2mSha256Ctxt
68 
69 @brief
70 	SHA256 context data
71 */
72 typedef struct sha256ctxt{
73 	uint32	au32Sha256CtxtBuff[M2M_SHA256_CONTEXT_BUFF_LEN/sizeof(uint32)];
74 } tstrM2mSha256Ctxt;
75 
76 
77 
78 /*!
79 @enum	\
80 	tenuRsaSignStatus
81 
82 @brief
83 	RSA Signature status: pass or fail.
84 
85 @see
86 	m2m_crypto_rsa_sign_gen
87 */
88 typedef enum{
89 	M2M_RSA_SIGN_OK,
90 	M2M_RSA_SIGN_FAIL
91 } tenuRsaSignStatus;
92 
93 /*!
94 @typedef \
95 	tpfAppCryproCb
96 
97 @brief			Crypto Calback function receiving the crypto related messages
98 @param [in]	u8MsgType
99 				Crypto command about which the notification is received.
100 @param [in]	pvResp
101 				A pointer to the result associated with the notification.
102 @param [in]	pvMsg
103 				A pointer to a buffer containing the notification parameters (if any). It should be
104 				Casted to the correct data type corresponding to the notification type.
105 @see
106 	m2m_crypto_init
107 	tenuM2mCryptoCmd
108 */
109 typedef void (*tpfAppCryproCb) (uint8 u8MsgType,void * pvResp, void * pvMsg);
110 
111 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
112 FUNCTION PROTOTYPES
113 *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
114 
115 
116 #ifdef __cplusplus
117      extern "C" {
118 #endif
119 /*!
120 @fn	\
121 	sint8 m2m_crypto_init();
122 
123 @brief	crypto initialization.
124 
125 @param[in]	pfAppCryproCb
126 				Pointer to the Crypto Calback function receiving the crypto related messages.
127 @see
128 	tpfAppCryproCb
129 
130 @return
131 	The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
132 */
133 sint8 m2m_crypto_init(tpfAppCryproCb pfAppCryproCb);
134 /*!
135 @fn	\
136 	sint8 m2m_sha256_hash_init(tstrM2mSha256Ctxt *psha256Ctxt);
137 
138 @brief	SHA256 hash initialization
139 
140 @param[in]	psha256Ctxt
141 				Pointer to a sha256 context allocated by the caller.
142 @return
143 	The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
144 */
145 sint8 m2m_crypto_sha256_hash_init(tstrM2mSha256Ctxt *psha256Ctxt);
146 
147 
148 /*!
149 @fn	\
150 	sint8 m2m_sha256_hash_update(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Data, uint16 u16DataLength);
151 
152 @brief	SHA256 hash update
153 
154 @param [in]	psha256Ctxt
155 				Pointer to the sha256 context.
156 
157 @param [in]	pu8Data
158 				Buffer holding the data submitted to the hash.
159 
160 @param [in]	u16DataLength
161 				Size of the data bufefr in bytes.
162 @pre SHA256 module should be initialized first through m2m_crypto_sha256_hash_init function.
163 
164 @see m2m_crypto_sha256_hash_init
165 
166 @return
167 	The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
168 
169 */
170 sint8 m2m_crypto_sha256_hash_update(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Data, uint16 u16DataLength);
171 
172 
173 /*!
174 @fn	\
175 	sint8 m2m_sha256_hash_finish(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Sha256Digest);
176 
177 @brief	SHA256 hash finalization
178 
179 @param[in]	psha256Ctxt
180 				Pointer to a sha256 context allocated by the caller.
181 
182 @param [in] pu8Sha256Digest
183 				Buffer allocated by the caller which will hold the resultant SHA256 Digest. It must be allocated no less than M2M_SHA256_DIGEST_LEN.
184 
185 @return
186 	The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
187 */
188 sint8 m2m_crypto_sha256_hash_finish(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Sha256Digest);
189 
190 
191 /*!
192 @fn	\
193 	sint8 m2m_rsa_sign_verify(uint8 *pu8N, uint16 u16NSize, uint8 *pu8E, uint16 u16ESize, uint8 *pu8SignedMsgHash, \
194 		uint16 u16HashLength, uint8 *pu8RsaSignature);
195 
196 @brief	RSA Signature Verification
197 
198 	The function shall request the RSA Signature verification from the WINC Firmware for the given message. The signed message shall be
199 	compressed to the corresponding hash algorithm before calling this function.
200 	The hash type is identified by the given hash length. For example, if the hash length is 32 bytes, then it is SHA256.
201 
202 @param[in]	pu8N
203 				RSA Key modulus n.
204 
205 @param[in]	u16NSize
206 				Size of the RSA modulus n in bytes.
207 
208 @param[in]	pu8E
209 				RSA public exponent.
210 
211 @param[in]	u16ESize
212 				Size of the RSA public exponent in bytes.
213 
214 @param[in]	pu8SignedMsgHash
215 				The hash digest of the signed message.
216 
217 @param[in]	u16HashLength
218 				The length of the hash digest.
219 
220 @param[out] pu8RsaSignature
221 				Signature value to be verified.
222 
223 @return
224 	The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
225 */
226 sint8 m2m_crypto_rsa_sign_verify(uint8 *pu8N, uint16 u16NSize, uint8 *pu8E, uint16 u16ESize, uint8 *pu8SignedMsgHash,
227 						  uint16 u16HashLength, uint8 *pu8RsaSignature);
228 
229 
230 /*!
231 @fn	\
232 	sint8 m2m_rsa_sign_gen(uint8 *pu8N, uint16 u16NSize, uint8 *pu8d, uint16 u16dSize, uint8 *pu8SignedMsgHash, \
233 		uint16 u16HashLength, uint8 *pu8RsaSignature);
234 
235 @brief	RSA Signature Generation
236 
237 	The function shall request the RSA Signature generation from the WINC Firmware for the given message. The signed message shall be
238 	compressed to the corresponding hash algorithm before calling this function.
239 	The hash type is identified by the given hash length. For example, if the hash length is 32 bytes, then it is SHA256.
240 
241 @param[in]	pu8N
242 				RSA Key modulus n.
243 
244 @param[in]	u16NSize
245 				Size of the RSA modulus n in bytes.
246 
247 @param[in]	pu8d
248 				RSA private exponent.
249 
250 @param[in]	u16dSize
251 				Size of the RSA private exponent in bytes.
252 
253 @param[in]	pu8SignedMsgHash
254 				The hash digest of the signed message.
255 
256 @param[in]	u16HashLength
257 				The length of the hash digest.
258 
259 @param[out] pu8RsaSignature
260 				Pointer to a user buffer allocated by teh caller shall hold the generated signature.
261 
262 @return
263 	The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
264 */
265 sint8 m2m_crypto_rsa_sign_gen(uint8 *pu8N, uint16 u16NSize, uint8 *pu8d, uint16 u16dSize, uint8 *pu8SignedMsgHash,
266 					   uint16 u16HashLength, uint8 *pu8RsaSignature);
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 
272 #endif /* __M2M_CRYPTO_H__ */
273