hash.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*!
  2. \ingroup wolfCrypt
  3. \brief This function will return the OID for the wc_HashType provided.
  4. \return OID returns value greater than 0
  5. \return HASH_TYPE_E hash type not supported.
  6. \return BAD_FUNC_ARG one of the provided arguments is incorrect.
  7. \param hash_type A hash type from the “enum wc_HashType” such
  8. as “WC_HASH_TYPE_SHA256”.
  9. _Example_
  10. \code
  11. enum wc_HashType hash_type = WC_HASH_TYPE_SHA256;
  12. int oid = wc_HashGetOID(hash_type);
  13. if (oid > 0) {
  14. // Success
  15. }
  16. \endcode
  17. \sa wc_HashGetDigestSize
  18. \sa wc_Hash
  19. */
  20. WOLFSSL_API int wc_HashGetOID(enum wc_HashType hash_type);
  21. /*!
  22. \ingroup wolfCrypt
  23. \brief This function returns the size of the digest (output) for a
  24. hash_type. The returns size is used to make sure the output buffer
  25. provided to wc_Hash is large enough.
  26. \return Success A positive return value indicates the digest size
  27. for the hash.
  28. \return Error Returns HASH_TYPE_E if hash_type is not supported.
  29. \return Failure Returns BAD_FUNC_ARG if an invalid hash_type was used.
  30. \param hash_type A hash type from the “enum wc_HashType” such as
  31. “WC_HASH_TYPE_SHA256”.
  32. _Example_
  33. \code
  34. int hash_len = wc_HashGetDigestSize(hash_type);
  35. if (hash_len <= 0) {
  36. WOLFSSL_MSG("Invalid hash type/len");
  37. return BAD_FUNC_ARG;
  38. }
  39. \endcode
  40. \sa wc_Hash
  41. */
  42. WOLFSSL_API int wc_HashGetDigestSize(enum wc_HashType hash_type);
  43. /*!
  44. \ingroup wolfCrypt
  45. \brief This function performs a hash on the provided data buffer
  46. and returns it in the hash buffer provided.
  47. \return 0 Success, else error (such as BAD_FUNC_ARG or BUFFER_E).
  48. \param hash_type A hash type from the “enum wc_HashType”
  49. such as “WC_HASH_TYPE_SHA256”.
  50. \param data Pointer to buffer containing the data to hash.
  51. \param data_len Length of the data buffer.
  52. \param hash Pointer to buffer used to output the final hash to.
  53. \param hash_len Length of the hash buffer.
  54. _Example_
  55. \code
  56. enum wc_HashType hash_type = WC_HASH_TYPE_SHA256;
  57. int hash_len = wc_HashGetDigestSize(hash_type);
  58. if (hash_len > 0) {
  59. int ret = wc_Hash(hash_type, data, data_len, hash_data, hash_len);
  60. if(ret == 0) {
  61. // Success
  62. }
  63. }
  64. \endcode
  65. \sa wc_HashGetDigestSize
  66. */
  67. WOLFSSL_API int wc_Hash(enum wc_HashType hash_type,
  68. const byte* data, word32 data_len,
  69. byte* hash, word32 hash_len);
  70. /*!
  71. \ingroup MD5
  72. \brief Convenience function, handles all the hashing and places the
  73. result into hash.
  74. \return 0 Returned upon successfully hashing the data.
  75. \return Memory_E memory error, unable to allocate memory. This is only
  76. possible with the small stack option enabled.
  77. \param data the data to hash
  78. \param len the length of data
  79. \param hash Byte array to hold hash value.
  80. _Example_
  81. \code
  82. const byte* data;
  83. word32 data_len;
  84. byte* hash;
  85. int ret;
  86. ...
  87. ret = wc_Md5Hash(data, data_len, hash);
  88. if (ret != 0) {
  89. // Md5 Hash Failure Case.
  90. }
  91. \endcode
  92. \sa wc_Md5Hash
  93. \sa wc_Md5Final
  94. \sa wc_InitMd5
  95. */
  96. WOLFSSL_API int wc_Md5Hash(const byte* data, word32 len, byte* hash);
  97. /*!
  98. \ingroup SHA
  99. \brief Convenience function, handles all the hashing and places the
  100. result into hash.
  101. \return 0 Returned upon successfully ….
  102. \return Memory_E memory error, unable to allocate memory. This is only
  103. possible with the small stack option enabled.
  104. \param data the data to hash
  105. \param len the length of data
  106. \param hash Byte array to hold hash value.
  107. _Example_
  108. \code
  109. none
  110. \endcode
  111. \sa wc_ShaHash
  112. \sa wc_ShaFinal
  113. \sa wc_InitSha
  114. */
  115. WOLFSSL_API int wc_ShaHash(const byte*, word32, byte*);
  116. /*!
  117. \ingroup SHA
  118. \brief Convenience function, handles all the hashing and places the
  119. result into hash.
  120. \return 0 Returned upon successfully …
  121. \return Memory_E memory error, unable to allocate memory. This is only
  122. possible with the small stack option enabled.
  123. \param data the data to hash
  124. \param len the length of data
  125. \param hash Byte array to hold hash value.
  126. _Example_
  127. \code
  128. none
  129. \endcode
  130. \sa wc_Sha256Hash
  131. \sa wc_Sha256Final
  132. \sa wc_InitSha256
  133. */
  134. WOLFSSL_API int wc_Sha256Hash(const byte*, word32, byte*);
  135. /*!
  136. \ingroup SHA
  137. \brief Convenience function, handles all the hashing and places the
  138. result into hash.
  139. \return 0 Success
  140. \return <0 Error
  141. \param data the data to hash
  142. \param len the length of data
  143. \param hash Byte array to hold hash value.
  144. _Example_
  145. \code
  146. none
  147. \endcode
  148. \sa wc_InitSha224
  149. \sa wc_Sha224Update
  150. \sa wc_Sha224Final
  151. */
  152. WOLFSSL_API int wc_Sha224Hash(const byte*, word32, byte*);
  153. /*!
  154. \ingroup SHA
  155. \brief Convenience function, handles all the hashing and places the
  156. result into hash.
  157. \return 0 Returned upon successfully hashing the inputted data
  158. \return Memory_E memory error, unable to allocate memory. This is only
  159. possible with the small stack option enabled.
  160. \param data the data to hash
  161. \param len the length of data
  162. \param hash Byte array to hold hash value.
  163. _Example_
  164. \code
  165. none
  166. \endcode
  167. \sa wc_Sha512Hash
  168. \sa wc_Sha512Final
  169. \sa wc_InitSha512
  170. */
  171. WOLFSSL_API int wc_Sha512Hash(const byte*, word32, byte*);
  172. /*!
  173. \ingroup SHA
  174. \brief Convenience function, handles all the hashing and places the
  175. result into hash.
  176. \return 0 Returned upon successfully hashing the data
  177. \return Memory_E memory error, unable to allocate memory. This is only
  178. possible with the small stack option enabled.
  179. \param data the data to hash
  180. \param len the length of data
  181. \param hash Byte array to hold hash value.
  182. _Example_
  183. \code
  184. none
  185. \endcode
  186. \sa wc_Sha384Hash
  187. \sa wc_Sha384Final
  188. \sa wc_InitSha384
  189. */
  190. WOLFSSL_API int wc_Sha384Hash(const byte*, word32, byte*);