hmac.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*!
  2. \ingroup HMAC
  3. \brief This function initializes an Hmac object, setting its
  4. encryption type, key and HMAC length.
  5. \return 0 Returned on successfully initializing the Hmac object
  6. \return BAD_FUNC_ARG Returned if the input type is invalid. Valid options
  7. are: MD5, SHA, SHA256, SHA384, SHA512, SHA3-224, SHA3-256, SHA3-384,
  8. SHA3-512
  9. \return MEMORY_E Returned if there is an error allocating memory for the
  10. structure to use for hashing
  11. \return HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation
  12. and the key length specified is shorter than the minimum acceptable
  13. FIPS standard
  14. \param hmac pointer to the Hmac object to initialize
  15. \param type type specifying which encryption method the Hmac object
  16. should use. Valid options are: MD5, SHA, SHA256, SHA384, SHA512, SHA3-224,
  17. SHA3-256, SHA3-384, SHA3-512
  18. \param key pointer to a buffer containing the key with which to
  19. initialize the Hmac object
  20. \param length length of the key
  21. _Example_
  22. \code
  23. Hmac hmac;
  24. byte key[] = { // initialize with key to use for encryption };
  25. if (wc_HmacSetKey(&hmac, MD5, key, sizeof(key)) != 0) {
  26. // error initializing Hmac object
  27. }
  28. \endcode
  29. \sa wc_HmacUpdate
  30. \sa wc_HmacFinal
  31. */
  32. WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
  33. /*!
  34. \ingroup HMAC
  35. \brief This function updates the message to authenticate using HMAC.
  36. It should be called after the Hmac object has been initialized with
  37. wc_HmacSetKey. This function may be called multiple times to update
  38. the message to hash. After calling wc_HmacUpdate as desired, one should
  39. call wc_HmacFinal to obtain the final authenticated message tag.
  40. \return 0 Returned on successfully updating the message to authenticate
  41. \return MEMORY_E Returned if there is an error allocating memory for
  42. use with a hashing algorithm
  43. \param hmac pointer to the Hmac object for which to update the message
  44. \param msg pointer to the buffer containing the message to append
  45. \param length length of the message to append
  46. _Example_
  47. \code
  48. Hmac hmac;
  49. byte msg[] = { // initialize with message to authenticate };
  50. byte msg2[] = { // initialize with second half of message };
  51. // initialize hmac
  52. if( wc_HmacUpdate(&hmac, msg, sizeof(msg)) != 0) {
  53. // error updating message
  54. }
  55. if( wc_HmacUpdate(&hmac, msg2, sizeof(msg)) != 0) {
  56. // error updating with second message
  57. }
  58. \endcode
  59. \sa wc_HmacSetKey
  60. \sa wc_HmacFinal
  61. */
  62. WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
  63. /*!
  64. \ingroup HMAC
  65. \brief This function computes the final hash of an Hmac object's message.
  66. \return 0 Returned on successfully computing the final hash
  67. \return MEMORY_E Returned if there is an error allocating memory for
  68. use with a hashing algorithm
  69. \param hmac pointer to the Hmac object for which to calculate the
  70. final hash
  71. \param hash pointer to the buffer in which to store the final hash.
  72. Should have room available as required by the hashing algorithm chosen
  73. _Example_
  74. \code
  75. Hmac hmac;
  76. byte hash[MD5_DIGEST_SIZE];
  77. // initialize hmac with MD5 as type
  78. // wc_HmacUpdate() with messages
  79. if (wc_HmacFinal(&hmac, hash) != 0) {
  80. // error computing hash
  81. }
  82. \endcode
  83. \sa wc_HmacSetKey
  84. \sa wc_HmacUpdate
  85. */
  86. WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
  87. /*!
  88. \ingroup HMAC
  89. \brief This function returns the largest HMAC digest size available
  90. based on the configured cipher suites.
  91. \return Success Returns the largest HMAC digest size available based
  92. on the configured cipher suites
  93. \param none No parameters.
  94. _Example_
  95. \code
  96. int maxDigestSz = wolfSSL_GetHmacMaxSize();
  97. \endcode
  98. \sa none
  99. */
  100. WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
  101. /*!
  102. \ingroup HMAC
  103. \brief This function provides access to a HMAC Key Derivation Function
  104. (HKDF). It utilizes HMAC to convert inKey, with an optional salt and
  105. optional info into a derived key, which it stores in out. The hash type
  106. defaults to MD5 if 0 or NULL is given.
  107. \return 0 Returned upon successfully generating a key with the given inputs
  108. \return BAD_FUNC_ARG Returned if an invalid hash type is given as
  109. argument. Valid types are: MD5, SHA, SHA256, SHA384, SHA512, SHA3-224,
  110. SHA3-256, SHA3-384, SHA3-512
  111. \return MEMORY_E Returned if there is an error allocating memory
  112. \return HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation
  113. and the key length specified is shorter than the minimum acceptable FIPS
  114. standard
  115. \param type hash type to use for the HKDF. Valid types are: MD5, SHA,
  116. SHA256, SHA384, SHA512, SHA3-224, SHA3-256, SHA3-384, SHA3-512
  117. \param inKey pointer to the buffer containing the key to use for KDF
  118. \param inKeySz length of the input key
  119. \param salt pointer to a buffer containing an optional salt. Use NULL
  120. instead if not using a salt
  121. \param saltSz length of the salt. Use 0 if not using a salt
  122. \param info pointer to a buffer containing optional additional info.
  123. Use NULL if not appending extra info
  124. \param infoSz length of additional info. Use 0 if not using additional info
  125. \param out pointer to the buffer in which to store the derived key
  126. \param outSz space available in the output buffer to store the
  127. generated key
  128. _Example_
  129. \code
  130. byte key[] = { // initialize with key };
  131. byte salt[] = { // initialize with salt };
  132. byte derivedKey[MAX_DIGEST_SIZE];
  133. int ret = wc_HKDF(SHA512, key, sizeof(key), salt, sizeof(salt),
  134. NULL, 0, derivedKey, sizeof(derivedKey));
  135. if ( ret != 0 ) {
  136. // error generating derived key
  137. }
  138. \endcode
  139. \sa wc_HmacSetKey
  140. */
  141. WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  142. const byte* salt, word32 saltSz,
  143. const byte* info, word32 infoSz,
  144. byte* out, word32 outSz);