Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

260 строки
8.6 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief WINC Crypto Application Interface.
  6. *
  7. * Copyright (c) 2015 - 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. #ifndef __M2M_CRYPTO_H__
  42. #define __M2M_CRYPTO_H__
  43. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  44. INCLUDES
  45. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  46. #include "common/include/nm_common.h"
  47. #include "driver/include/m2m_types.h"
  48. #include "driver/source/m2m_hif.h"
  49. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  50. MACROS
  51. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  52. #define M2M_MAX_RSA_LEN (256)
  53. #define M2M_SHA256_DIGEST_LEN 32
  54. #define M2M_SHA256_MAX_DATA (M2M_BUFFER_MAX_SIZE - M2M_SHA256_CONTEXT_BUFF_LEN - M2M_HIF_HDR_OFFSET)
  55. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  56. DATA TYPES
  57. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  58. /*!
  59. @struct \
  60. tstrM2mSha256Ctxt
  61. @brief
  62. SHA256 context data
  63. */
  64. typedef struct sha256ctxt {
  65. uint32 au32Sha256CtxtBuff[M2M_SHA256_CONTEXT_BUFF_LEN / sizeof(uint32)];
  66. } tstrM2mSha256Ctxt;
  67. /*!
  68. @enum \
  69. tenuRsaSignStatus
  70. @brief
  71. RSA Signature status: pass or fail.
  72. @see
  73. m2m_crypto_rsa_sign_gen
  74. */
  75. typedef enum { M2M_RSA_SIGN_OK, M2M_RSA_SIGN_FAIL } tenuRsaSignStatus;
  76. /*!
  77. @typedef \
  78. tpfAppCryproCb
  79. @brief Crypto Calback function receiving the crypto related messages
  80. @param [in] u8MsgType
  81. Crypto command about which the notification is received.
  82. @param [in] pvResp
  83. A pointer to the result associated with the notification.
  84. @param [in] pvMsg
  85. A pointer to a buffer containing the notification parameters (if any). It should be
  86. Casted to the correct data type corresponding to the notification type.
  87. @see
  88. m2m_crypto_init
  89. tenuM2mCryptoCmd
  90. */
  91. typedef void (*tpfAppCryproCb)(uint8 u8MsgType, void *pvResp, void *pvMsg);
  92. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  93. FUNCTION PROTOTYPES
  94. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98. /*!
  99. @fn \
  100. sint8 m2m_crypto_init();
  101. @brief crypto initialization.
  102. @param[in] pfAppCryproCb
  103. Pointer to the Crypto Calback function receiving the crypto related messages.
  104. @see
  105. tpfAppCryproCb
  106. @return
  107. The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
  108. */
  109. sint8 m2m_crypto_init(tpfAppCryproCb pfAppCryproCb);
  110. /*!
  111. @fn \
  112. sint8 m2m_sha256_hash_init(tstrM2mSha256Ctxt *psha256Ctxt);
  113. @brief SHA256 hash initialization
  114. @param[in] psha256Ctxt
  115. Pointer to a sha256 context allocated by the caller.
  116. @return
  117. The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
  118. */
  119. sint8 m2m_crypto_sha256_hash_init(tstrM2mSha256Ctxt *psha256Ctxt);
  120. /*!
  121. @fn \
  122. sint8 m2m_sha256_hash_update(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Data, uint16 u16DataLength);
  123. @brief SHA256 hash update
  124. @param [in] psha256Ctxt
  125. Pointer to the sha256 context.
  126. @param [in] pu8Data
  127. Buffer holding the data submitted to the hash.
  128. @param [in] u16DataLength
  129. Size of the data bufefr in bytes.
  130. @pre SHA256 module should be initialized first through m2m_crypto_sha256_hash_init function.
  131. @see m2m_crypto_sha256_hash_init
  132. @return
  133. The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
  134. */
  135. sint8 m2m_crypto_sha256_hash_update(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Data, uint16 u16DataLength);
  136. /*!
  137. @fn \
  138. sint8 m2m_sha256_hash_finish(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Sha256Digest);
  139. @brief SHA256 hash finalization
  140. @param[in] psha256Ctxt
  141. Pointer to a sha256 context allocated by the caller.
  142. @param [in] pu8Sha256Digest
  143. Buffer allocated by the caller which will hold the resultant SHA256 Digest. It must be allocated no less
  144. than M2M_SHA256_DIGEST_LEN.
  145. @return
  146. The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
  147. */
  148. sint8 m2m_crypto_sha256_hash_finish(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Sha256Digest);
  149. /*!
  150. @fn \
  151. sint8 m2m_rsa_sign_verify(uint8 *pu8N, uint16 u16NSize, uint8 *pu8E, uint16 u16ESize, uint8 *pu8SignedMsgHash, \
  152. uint16 u16HashLength, uint8 *pu8RsaSignature);
  153. @brief RSA Signature Verification
  154. The function shall request the RSA Signature verification from the WINC Firmware for the given message. The signed
  155. message shall be compressed to the corresponding hash algorithm before calling this function. The hash type is
  156. identified by the given hash length. For example, if the hash length is 32 bytes, then it is SHA256.
  157. @param[in] pu8N
  158. RSA Key modulus n.
  159. @param[in] u16NSize
  160. Size of the RSA modulus n in bytes.
  161. @param[in] pu8E
  162. RSA public exponent.
  163. @param[in] u16ESize
  164. Size of the RSA public exponent in bytes.
  165. @param[in] pu8SignedMsgHash
  166. The hash digest of the signed message.
  167. @param[in] u16HashLength
  168. The length of the hash digest.
  169. @param[out] pu8RsaSignature
  170. Signature value to be verified.
  171. @return
  172. The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
  173. */
  174. sint8 m2m_crypto_rsa_sign_verify(uint8 *pu8N, uint16 u16NSize, uint8 *pu8E, uint16 u16ESize, uint8 *pu8SignedMsgHash,
  175. uint16 u16HashLength, uint8 *pu8RsaSignature);
  176. /*!
  177. @fn \
  178. sint8 m2m_rsa_sign_gen(uint8 *pu8N, uint16 u16NSize, uint8 *pu8d, uint16 u16dSize, uint8 *pu8SignedMsgHash, \
  179. uint16 u16HashLength, uint8 *pu8RsaSignature);
  180. @brief RSA Signature Generation
  181. The function shall request the RSA Signature generation from the WINC Firmware for the given message. The signed
  182. message shall be compressed to the corresponding hash algorithm before calling this function. The hash type is
  183. identified by the given hash length. For example, if the hash length is 32 bytes, then it is SHA256.
  184. @param[in] pu8N
  185. RSA Key modulus n.
  186. @param[in] u16NSize
  187. Size of the RSA modulus n in bytes.
  188. @param[in] pu8d
  189. RSA private exponent.
  190. @param[in] u16dSize
  191. Size of the RSA private exponent in bytes.
  192. @param[in] pu8SignedMsgHash
  193. The hash digest of the signed message.
  194. @param[in] u16HashLength
  195. The length of the hash digest.
  196. @param[out] pu8RsaSignature
  197. Pointer to a user buffer allocated by teh caller shall hold the generated signature.
  198. @return
  199. The function returns @ref M2M_SUCCESS for successful operation and a negative value otherwise.
  200. */
  201. sint8 m2m_crypto_rsa_sign_gen(uint8 *pu8N, uint16 u16NSize, uint8 *pu8d, uint16 u16dSize, uint8 *pu8SignedMsgHash,
  202. uint16 u16HashLength, uint8 *pu8RsaSignature);
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #endif /* __M2M_CRYPTO_H__ */