crys_hmac.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 HMAC APIs, as well as the APIs themselves.
  38. @defgroup crys_hmac CryptoCell HMAC APIs
  39. @{
  40. @ingroup cryptocell_api
  41. HMAC is a wrapping algorithm that uses a HASH function (one of the supported HASH algorithms, as specified in the HASH chapter) and a key,
  42. to generate a unique authentication code over the input data.
  43. HMAC calculation can be performed in either of the following two modes of operation:
  44. <ul><li> Integrated operation - Processes all data in a single function call. This flow is applicable when all data is available prior to
  45. the cryptographic operation.</li>
  46. <li> Block operation - Processes a subset of the data buffers, and is called multiple times in a sequence. This flow is applicable when
  47. the next data buffer becomes available only during/after processing of the current data buffer.</li></ul>
  48. The following is a typical HMAC Block operation flow:
  49. <ol><li> ::CRYS_HMAC_Init: This function initializes the HMAC machine on the CRYS level by setting the context pointer that is
  50. used on the entire HMAC operation.</li>
  51. <li> ::CRYS_HMAC_Update: This function runs an HMAC operation on a block of data allocated by the user. This function may be called as
  52. many times as required.</li>
  53. <li> ::CRYS_HMAC_Finish: This function ends the HMAC operation. It returns the digest result and clears the context.</li></ol>
  54. */
  55. #ifndef CRYS_HMAC_H
  56. #define CRYS_HMAC_H
  57. #include "ssi_pal_types.h"
  58. #include "crys_error.h"
  59. #include "crys_hash.h"
  60. #include "crys_hmac_defs.h"
  61. #ifdef __cplusplus
  62. extern "C"
  63. {
  64. #endif
  65. /************************ Defines ******************************/
  66. /*! HMAC key size after padding for MD5, SHA1, SHA256. */
  67. #define CRYS_HMAC_KEY_SIZE_IN_BYTES 64
  68. /*! HMAC key size after padding for SHA384, SHA512 */
  69. #define CRYS_HMAC_SHA2_1024BIT_KEY_SIZE_IN_BYTES 128
  70. /************************ Enums ********************************/
  71. /************************ Typedefs ****************************/
  72. /*********************** Structures ****************************/
  73. /*! User's context prototype - the argument type that is passed by the user
  74. to the HMAC APIs. The context saves the state of the operation and must be saved by the user
  75. till the end of the APIs flow */
  76. typedef struct CRYS_HMACUserContext_t {
  77. /*! Context buffer for internal use */
  78. uint32_t buff[CRYS_HMAC_USER_CTX_SIZE_IN_WORDS];
  79. }CRYS_HMACUserContext_t;
  80. /************************ Structs ******************************/
  81. /************************ Public Variables **********************/
  82. /************************ Public Functions **********************/
  83. /*!
  84. @brief This function initializes the HMAC machine.
  85. It allocates and initializes the HMAC Context. It initiates a HASH session and processes a HASH update on the Key XOR ipad,
  86. then stores it in the context
  87. @return CRYS_OK on success.
  88. @return A non-zero value from crys_hmac_error.h or crys_hash_error.h on failure.
  89. */
  90. CIMPORT_C CRYSError_t CRYS_HMAC_Init(
  91. CRYS_HMACUserContext_t *ContextID_ptr, /*!< [in] Pointer to the HMAC context buffer allocated by the user, which is used
  92. for the HMAC machine operation. */
  93. CRYS_HASH_OperationMode_t OperationMode, /*!< [in] One of the supported HASH modes, as defined in CRYS_HASH_OperationMode_t. */
  94. uint8_t *key_ptr, /*!< [in] The pointer to the user's key buffer. */
  95. uint16_t keySize /*!< [in] The key size in bytes. If the key size is bigger than the HASH block, the key will be hashed.
  96. The limitations on the key size are the same as the limitations on MAX hash size. */
  97. );
  98. /*!
  99. @brief This function processes a block of data to be HASHed.
  100. It receives a handle to the HMAC Context, and updates the HASH value with the new data.
  101. @return CRYS_OK on success.
  102. @return A non-zero value from crys_hmac_error.h or crys_hash_error.h on failure.
  103. */
  104. CIMPORT_C CRYSError_t CRYS_HMAC_Update(
  105. CRYS_HMACUserContext_t *ContextID_ptr, /*!< [in] Pointer to the HMAC context buffer allocated by the user
  106. that is used for the HMAC machine operation. */
  107. uint8_t *DataIn_ptr, /*!< [in] Pointer to the input data to be HASHed.
  108. The size of the scatter/gather list representing the data buffer is limited to
  109. 128 entries, and the size of each entry is limited to 64KB
  110. (fragments larger than 64KB are broken into fragments <= 64KB). */
  111. size_t DataInSize /*!< [in] Byte size of the input data. Must be > 0.
  112. If not a multiple of the HASH block size (64 for SHA-1 and SHA-224/256,
  113. 128 for SHA-384/512), no further calls to ::CRYS_HMAC_Update are allowed in
  114. this context, and only ::CRYS_HMAC_Finish can be called to complete the
  115. computation. */
  116. );
  117. /*!
  118. @brief This function finalizes the HMAC processing of a data block.
  119. It receives a handle to the HMAC context that was previously initialized by ::CRYS_HMAC_Init, or by ::CRYS_HMAC_Update.
  120. It completes the HASH calculation on the ipad and text, and then executes a new HASH operation with the key XOR opad and the previous
  121. HASH operation result.
  122. @return CRYS_OK on success.
  123. @return A non-zero value from crys_hmac_error.h or crys_hash_error.h on failure.
  124. */
  125. CIMPORT_C CRYSError_t CRYS_HMAC_Finish(
  126. CRYS_HMACUserContext_t *ContextID_ptr, /*!< [in] Pointer to the HMAC context buffer allocated by the user, which is used
  127. for the HMAC machine operation. */
  128. CRYS_HASH_Result_t HmacResultBuff /*!< [out] Pointer to the word-aligned 64 byte buffer. The actual size of the
  129. HASH result depends on CRYS_HASH_OperationMode_t. */
  130. );
  131. /*!
  132. @brief This function is a service function that frees the context if the operation has failed.
  133. The function executes the following major steps:
  134. <ol><li> Checks the validity of all of the inputs of the function. </li>
  135. <li> Clears the user's context.</li>
  136. <li> Exits the handler with the OK code.</li></ol>
  137. @return CRYS_OK on success.
  138. @return a non-zero value from crys_hmac_error.h on failure.
  139. */
  140. CIMPORT_C CRYSError_t CRYS_HMAC_Free(
  141. CRYS_HMACUserContext_t *ContextID_ptr /*!< [in] Pointer to the HMAC context buffer allocated by the user, which is used for
  142. the HMAC machine operation. */
  143. );
  144. /*!
  145. @brief This function processes a single buffer of data, and returns the data buffer's message digest.
  146. @return CRYS_OK on success.
  147. @return A non-zero value from crys_hmac_error.h or crys_hash_error.h on failure.
  148. */
  149. CIMPORT_C CRYSError_t CRYS_HMAC (
  150. CRYS_HASH_OperationMode_t OperationMode, /*!< [in] One of the supported HASH modes, as defined in CRYS_HASH_OperationMode_t. */
  151. uint8_t *key_ptr, /*!< [in] The pointer to the user's key buffer. */
  152. uint16_t keySize, /*!< [in] The key size in bytes. If the key size is bigger than the HASH block, the key will be hashed.
  153. The limitations on the key size are the same as the limitations on MAX hash size.*/
  154. uint8_t *DataIn_ptr, /*!< [in] Pointer to the input data to be HASHed.
  155. The size of the scatter/gather list representing the data buffer is limited to 128
  156. entries, and the size of each entry is limited to 64KB (fragments larger than
  157. 64KB are broken into fragments <= 64KB). */
  158. size_t DataSize, /*!< [in] The size of the data to be hashed (in bytes). */
  159. CRYS_HASH_Result_t HmacResultBuff /*!< [out] Pointer to the word-aligned 64 byte buffer. The actual size of the
  160. HMAC result depends on CRYS_HASH_OperationMode_t. */
  161. );
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. /**
  166. @}
  167. */
  168. #endif