crys_hash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**************************************************************************************
  2. * Copyright (c) 2016-2017, ARM Limited or its affiliates. All rights reserved *
  3. * *
  4. * This file and the related binary are licensed under the following license: *
  5. * *
  6. * ARM Object Code and Header Files License, v1.0 Redistribution. *
  7. * *
  8. * Redistribution and use of object code, header files, and documentation, without *
  9. * modification, are permitted provided that the following conditions are met: *
  10. * *
  11. * 1) Redistributions must reproduce the above copyright notice and the *
  12. * following disclaimer in the documentation and/or other materials *
  13. * provided with the distribution. *
  14. * *
  15. * 2) Unless to the extent explicitly permitted by law, no reverse *
  16. * engineering, decompilation, or disassembly of is permitted. *
  17. * *
  18. * 3) Redistribution and use is permitted solely for the purpose of *
  19. * developing or executing applications that are targeted for use *
  20. * on an ARM-based product. *
  21. * *
  22. * DISCLAIMER. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  23. * CONTRIBUTORS "AS IS." ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT *
  24. * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, *
  25. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
  26. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED *
  28. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  33. **************************************************************************************/
  34. /*!
  35. @file
  36. @brief This file contains all of the enums and definitions
  37. that are used for the CRYS HASH APIs, as well as the APIs themselves.
  38. @defgroup crys_hash CryptoCell HASH APIs
  39. @{
  40. @ingroup cryptocell_api
  41. This product supports the following HASH algorithms (or modes, according to product):
  42. <ul><li> CRYS_HASH_MD5 (producing 16 byte output).</li>
  43. <li> CRYS_HASH_SHA1 (producing 20 byte output).</li>
  44. <li> CRYS_HASH_SHA224 (producing 28 byte output).</li>
  45. <li> CRYS_HASH_SHA256 (producing 32 byte output).</li>
  46. <li> CRYS_HASH_SHA384 (producing 48 byte output).</li>
  47. <li> CRYS_HASH_SHA512 (producing 64 byte output).</li></ul>
  48. HASH calculation can be performed in either of the following two modes of operation:
  49. <ul><li> Integrated operation - Processes all data in a single function call. This flow is applicable when all data is available prior to the
  50. cryptographic operation.</li>
  51. <li> Block operation - Processes a subset of the data buffers, and is called multiple times in a sequence. This flow is applicable when the
  52. next data buffer becomes available only during/after processing of the current data buffer.</li></ul>
  53. The following is a typical HASH Block operation flow:
  54. <ol><li> ::CRYS_HASH_Init - this function initializes the HASH machine on the CRYS level by setting the context pointer that is used on the entire
  55. HASH operation.</li>
  56. <li> ::CRYS_HASH_Update - this function runs a HASH operation on a block of data allocated by the user. This function may be called as many times
  57. as required.</li>
  58. <li> ::CRYS_HASH_Finish - this function ends the HASH operation. It returns the digest result and clears the context.</li></ol>
  59. */
  60. #ifndef CRYS_HASH_H
  61. #define CRYS_HASH_H
  62. #include "ssi_pal_types.h"
  63. #include "crys_error.h"
  64. #include "crys_hash_defs.h"
  65. #ifdef __cplusplus
  66. extern "C"
  67. {
  68. #endif
  69. /************************ Defines ******************************/
  70. /* The hash result in words
  71. #define CRYS_HASH_RESULT_SIZE_IN_WORDS 5*/
  72. /*! The maximal hash result is 512 bits for SHA512. */
  73. #define CRYS_HASH_RESULT_SIZE_IN_WORDS 16
  74. /*! MD5 digest result size in bytes. */
  75. #define CRYS_HASH_MD5_DIGEST_SIZE_IN_BYTES 16
  76. /*! MD5 digest result size in words. */
  77. #define CRYS_HASH_MD5_DIGEST_SIZE_IN_WORDS 4
  78. /*! SHA-1 digest result size in bytes. */
  79. #define CRYS_HASH_SHA1_DIGEST_SIZE_IN_BYTES 20
  80. /*! SHA-1 digest result size in words. */
  81. #define CRYS_HASH_SHA1_DIGEST_SIZE_IN_WORDS 5
  82. /*! SHA-256 digest result size in words. */
  83. #define CRYS_HASH_SHA224_DIGEST_SIZE_IN_WORDS 7
  84. /*! SHA-256 digest result size in words. */
  85. #define CRYS_HASH_SHA256_DIGEST_SIZE_IN_WORDS 8
  86. /*! SHA-384 digest result size in words. */
  87. #define CRYS_HASH_SHA384_DIGEST_SIZE_IN_WORDS 12
  88. /*! SHA-512 digest result size in words. */
  89. #define CRYS_HASH_SHA512_DIGEST_SIZE_IN_WORDS 16
  90. /*! SHA-256 digest result size in bytes */
  91. #define CRYS_HASH_SHA224_DIGEST_SIZE_IN_BYTES 28
  92. /*! SHA-256 digest result size in bytes */
  93. #define CRYS_HASH_SHA256_DIGEST_SIZE_IN_BYTES 32
  94. /*! SHA-384 digest result size in bytes */
  95. #define CRYS_HASH_SHA384_DIGEST_SIZE_IN_BYTES 48
  96. /*! SHA-512 digest result size in bytes */
  97. #define CRYS_HASH_SHA512_DIGEST_SIZE_IN_BYTES 64
  98. /*! SHA1 hash block size in words */
  99. #define CRYS_HASH_BLOCK_SIZE_IN_WORDS 16
  100. /*! SHA1 hash block size in bytes */
  101. #define CRYS_HASH_BLOCK_SIZE_IN_BYTES 64
  102. /*! SHA2 hash block size in words */
  103. #define CRYS_HASH_SHA512_BLOCK_SIZE_IN_WORDS 32
  104. /*! SHA2 hash block size in bytes */
  105. #define CRYS_HASH_SHA512_BLOCK_SIZE_IN_BYTES 128
  106. /*! Maximal data size for update operation. */
  107. #define CRYS_HASH_UPDATE_DATA_MAX_SIZE_IN_BYTES (1 << 29)
  108. /************************ Enums ********************************/
  109. /*!
  110. HASH operation mode
  111. */
  112. typedef enum {
  113. CRYS_HASH_SHA1_mode = 0, /*!< SHA1. */
  114. CRYS_HASH_SHA224_mode = 1, /*!< SHA224. */
  115. CRYS_HASH_SHA256_mode = 2, /*!< SHA256. */
  116. CRYS_HASH_SHA384_mode = 3, /*!< SHA384. */
  117. CRYS_HASH_SHA512_mode = 4, /*!< SHA512. */
  118. CRYS_HASH_MD5_mode = 5, /*!< MD5. */
  119. /*! Number of hash modes. */
  120. CRYS_HASH_NumOfModes,
  121. /*! Reserved. */
  122. CRYS_HASH_OperationModeLast= 0x7FFFFFFF,
  123. }CRYS_HASH_OperationMode_t;
  124. /************************ Typedefs *****************************/
  125. /*! HASH result buffer. */
  126. typedef uint32_t CRYS_HASH_Result_t[CRYS_HASH_RESULT_SIZE_IN_WORDS];
  127. /************************ Structs ******************************/
  128. /*! The user's context prototype - the argument type that is passed by the user
  129. to the HASH APIs. The context saves the state of the operation and must be saved by the user
  130. till the end of the APIs flow. */
  131. typedef struct CRYS_HASHUserContext_t {
  132. /*! Internal buffer */
  133. uint32_t buff[CRYS_HASH_USER_CTX_SIZE_IN_WORDS];
  134. }CRYS_HASHUserContext_t;
  135. /************************ Public Variables **********************/
  136. /************************ Public Functions **********************/
  137. /************************************************************************************************/
  138. /*!
  139. @brief This function initializes the HASH machine and the HASH Context.
  140. It receives as input a pointer to store the context handle to the HASH Context,
  141. and initializes the HASH Context with the cryptographic attributes that are needed for the HASH block operation (initializes H's value for the HASH algorithm).
  142. @return CRYS_OK on success.
  143. @return A non-zero value from crys_hash_error.h on failure.
  144. */
  145. CIMPORT_C CRYSError_t CRYS_HASH_Init(
  146. CRYS_HASHUserContext_t *ContextID_ptr, /*!< [in] Pointer to the HASH context buffer allocated by the user that is used
  147. for the HASH machine operation. */
  148. CRYS_HASH_OperationMode_t OperationMode /*!< [in] One of the supported HASH modes, as defined in CRYS_HASH_OperationMode_t. */
  149. );
  150. /************************************************************************************************/
  151. /*!
  152. @brief This function processes a block of data to be HASHed.
  153. It updates a HASH Context that was previously initialized by CRYS_HASH_Init or updated by a previous call to CRYS_HASH_Update.
  154. @return CRYS_OK on success.
  155. @return A non-zero value from crys_hash_error.h on failure.
  156. */
  157. CIMPORT_C CRYSError_t CRYS_HASH_Update(
  158. CRYS_HASHUserContext_t *ContextID_ptr, /*!< [in] Pointer to the HASH context buffer allocated by the user, which is used for the
  159. HASH machine operation. */
  160. uint8_t *DataIn_ptr, /*!< [in] Pointer to the input data to be HASHed.
  161. it is a one contiguous memory block. */
  162. size_t DataInSize /*!< [in] Byte size of the input data. Must be > 0.
  163. If not a multiple of the HASH block size (64 for MD5, SHA-1 and SHA-224/256,
  164. 128 for SHA-384/512), no further calls
  165. to CRYS_HASH_Update are allowed in this context, and only CRYS_HASH_Finish
  166. can be called to complete the computation. */
  167. );
  168. /************************************************************************************************/
  169. /*!
  170. @brief This function finalizes the hashing process of data block.
  171. It receives a handle to the HASH Context, which was previously initialized by CRYS_HASH_Init or by CRYS_HASH_Update.
  172. It "adds" a header to the data block according to the relevant HASH standard, and computes the final message digest.
  173. @return CRYS_OK on success.
  174. @return A non-zero value from crys_hash_error.h on failure.
  175. */
  176. CIMPORT_C CRYSError_t CRYS_HASH_Finish(
  177. CRYS_HASHUserContext_t *ContextID_ptr, /*!< [in] Pointer to the HASH context buffer allocated by the user that is used for
  178. the HASH machine operation. */
  179. CRYS_HASH_Result_t HashResultBuff /*!< [in] Pointer to the word-aligned 64 byte buffer. The actual size of the HASH
  180. result depends on CRYS_HASH_OperationMode_t. */
  181. );
  182. /************************************************************************************************/
  183. /*!
  184. @brief This function is a utility function that frees the context if the operation has failed.
  185. The function executes the following major steps:
  186. <ol><li> Checks the validity of all of the inputs of the function. </li>
  187. <li> Clears the user's context.</li>
  188. <li> Exits the handler with the OK code.</li></ol>
  189. @return CRYS_OK on success.
  190. @return A non-zero value from crys_hash_error.h on failure.
  191. */
  192. CIMPORT_C CRYSError_t CRYS_HASH_Free(
  193. CRYS_HASHUserContext_t *ContextID_ptr /*!< [in] Pointer to the HASH context buffer allocated by the user that is used for
  194. the HASH machine operation. */
  195. );
  196. /************************************************************************************************/
  197. /*!
  198. @brief This function processes a single buffer of data.
  199. The function allocates an internal HASH Context, and initializes it with the cryptographic attributes
  200. that are needed for the HASH block operation (initialize H's value for the HASH algorithm).
  201. Then it processes the data block, calculating the HASH. Finally, it returns the data buffer's message digest.
  202. @return CRYS_OK on success.
  203. @return A non-zero value from crys_hash_error.h on failure.
  204. */
  205. CIMPORT_C CRYSError_t CRYS_HASH (
  206. CRYS_HASH_OperationMode_t OperationMode, /*!< [in] One of the supported HASH modes, as defined in CRYS_HASH_OperationMode_t. */
  207. uint8_t *DataIn_ptr, /*!< [in] Pointer to the input data to be HASHed.
  208. The size of the scatter/gather list representing the data buffer is limited
  209. to 128 entries, and the size of each entry is limited to 64KB
  210. (fragments larger than 64KB are broken into fragments <= 64KB). */
  211. size_t DataSize, /*!< [in] The size of the data to be hashed in bytes. */
  212. CRYS_HASH_Result_t HashResultBuff /*!< [out] Pointer to a word-aligned 64 byte buffer. The actual size of the HASH
  213. result depends on CRYS_HASH_OperationMode_t. */
  214. );
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. /**
  219. @}
  220. */
  221. #endif