hmac.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* hmac.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/hmac.h
  23. */
  24. #ifndef WOLF_CRYPT_HMAC_H
  25. #define WOLF_CRYPT_HMAC_H
  26. #include <wolfssl/wolfcrypt/hash.h>
  27. #ifndef NO_HMAC
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  30. #include <wolfssl/wolfcrypt/fips.h>
  31. #endif
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* avoid redefinition of structs */
  36. #if !defined(HAVE_FIPS) || \
  37. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  38. #ifdef WOLFSSL_ASYNC_CRYPT
  39. #include <wolfssl/wolfcrypt/async.h>
  40. #endif
  41. #if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_HMAC)
  42. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  43. #endif
  44. #ifndef NO_OLD_WC_NAMES
  45. #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
  46. #endif
  47. #define WC_HMAC_INNER_HASH_KEYED_SW 1
  48. #define WC_HMAC_INNER_HASH_KEYED_DEV 2
  49. enum {
  50. HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
  51. IPAD = 0x36,
  52. OPAD = 0x5C,
  53. /* If any hash is not enabled, add the ID here. */
  54. #ifdef NO_MD5
  55. WC_MD5 = WC_HASH_TYPE_MD5,
  56. #endif
  57. #ifdef NO_SHA
  58. WC_SHA = WC_HASH_TYPE_SHA,
  59. #endif
  60. #ifdef NO_SHA256
  61. WC_SHA256 = WC_HASH_TYPE_SHA256,
  62. #endif
  63. #ifndef WOLFSSL_SHA512
  64. WC_SHA512 = WC_HASH_TYPE_SHA512,
  65. #ifndef WOLFSSL_NOSHA512_224
  66. WC_SHA512_224 = WC_HASH_TYPE_SHA512_224,
  67. #endif
  68. #ifndef WOLFSSL_NOSHA512_256
  69. WC_SHA512_256 = WC_HASH_TYPE_SHA512_256,
  70. #endif
  71. #endif
  72. #ifndef WOLFSSL_SHA384
  73. WC_SHA384 = WC_HASH_TYPE_SHA384,
  74. #endif
  75. #ifndef WOLFSSL_SHA224
  76. WC_SHA224 = WC_HASH_TYPE_SHA224,
  77. #endif
  78. #ifndef WOLFSSL_SHA3
  79. WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
  80. WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
  81. WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
  82. WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
  83. #endif
  84. #ifdef WOLF_PRIVATE_KEY_ID
  85. HMAC_MAX_ID_LEN = 32,
  86. HMAC_MAX_LABEL_LEN = 32,
  87. #endif
  88. WOLF_ENUM_DUMMY_LAST_ELEMENT(HMAC)
  89. };
  90. /* Select the largest available hash for the buffer size. */
  91. #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
  92. #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && \
  93. !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && \
  94. defined(WOLFSSL_SHA224) && defined(NO_SHA) && defined(NO_MD5)
  95. #error "You have to have some kind of hash if you want to use HMAC."
  96. #endif
  97. /* hmac hash union */
  98. typedef union {
  99. #ifndef NO_MD5
  100. wc_Md5 md5;
  101. #endif
  102. #ifndef NO_SHA
  103. wc_Sha sha;
  104. #endif
  105. #ifdef WOLFSSL_SHA224
  106. wc_Sha224 sha224;
  107. #endif
  108. #ifndef NO_SHA256
  109. wc_Sha256 sha256;
  110. #endif
  111. #ifdef WOLFSSL_SHA384
  112. wc_Sha384 sha384;
  113. #endif
  114. #ifdef WOLFSSL_SHA512
  115. wc_Sha512 sha512;
  116. #endif
  117. #ifdef WOLFSSL_SHA3
  118. wc_Sha3 sha3;
  119. #endif
  120. #ifdef WOLFSSL_SM3
  121. wc_Sm3 sm3;
  122. #endif
  123. } wc_HmacHash;
  124. /* Hmac digest */
  125. struct Hmac {
  126. wc_HmacHash hash;
  127. word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
  128. word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)];
  129. word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
  130. void* heap; /* heap hint */
  131. byte macType; /* md5 sha or sha256 */
  132. byte innerHashKeyed; /* keyed flag */
  133. #ifdef WOLFSSL_KCAPI_HMAC
  134. struct kcapi_handle* handle;
  135. #endif
  136. #ifdef WOLFSSL_ASYNC_CRYPT
  137. WC_ASYNC_DEV asyncDev;
  138. #endif /* WOLFSSL_ASYNC_CRYPT */
  139. #if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HMAC)
  140. WC_CRYPTODEV ctx;
  141. #endif
  142. #ifdef WOLF_CRYPTO_CB
  143. int devId;
  144. void* devCtx;
  145. const byte* keyRaw;
  146. #endif
  147. #ifdef WOLF_PRIVATE_KEY_ID
  148. byte id[HMAC_MAX_ID_LEN];
  149. int idLen;
  150. char label[HMAC_MAX_LABEL_LEN];
  151. int labelLen;
  152. #endif
  153. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  154. word16 keyLen; /* hmac key length (key in ipad) */
  155. #endif
  156. };
  157. #ifndef WC_HMAC_TYPE_DEFINED
  158. typedef struct Hmac Hmac;
  159. #define WC_HMAC_TYPE_DEFINED
  160. #endif
  161. #endif /* HAVE_FIPS */
  162. /* does init */
  163. WOLFSSL_API int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 keySz);
  164. WOLFSSL_API int wc_HmacUpdate(Hmac* hmac, const byte* in, word32 sz);
  165. WOLFSSL_API int wc_HmacFinal(Hmac* hmac, byte* out);
  166. #ifdef WOLFSSL_KCAPI_HMAC
  167. WOLFSSL_API int wc_HmacSetKey_Software(Hmac* hmac, int type, const byte* key,
  168. word32 keySz);
  169. WOLFSSL_API int wc_HmacUpdate_Software(Hmac* hmac, const byte* in, word32 sz);
  170. WOLFSSL_API int wc_HmacFinal_Software(Hmac* hmac, byte* out);
  171. #endif
  172. WOLFSSL_API int wc_HmacSizeByType(int type);
  173. WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
  174. #ifdef WOLF_PRIVATE_KEY_ID
  175. WOLFSSL_API int wc_HmacInit_Id(Hmac* hmac, byte* id, int len, void* heap,
  176. int devId);
  177. WOLFSSL_API int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap,
  178. int devId);
  179. #endif
  180. WOLFSSL_API void wc_HmacFree(Hmac* hmac);
  181. WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
  182. WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
  183. #ifdef HAVE_HKDF
  184. WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
  185. const byte* inKey, word32 inKeySz, byte* out);
  186. WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
  187. const byte* info, word32 infoSz,
  188. byte* out, word32 outSz);
  189. WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  190. const byte* salt, word32 saltSz,
  191. const byte* info, word32 infoSz,
  192. byte* out, word32 outSz);
  193. #endif /* HAVE_HKDF */
  194. #ifdef __cplusplus
  195. } /* extern "C" */
  196. #endif
  197. #endif /* NO_HMAC */
  198. #endif /* WOLF_CRYPT_HMAC_H */