hmac.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* hmac.h
  2. *
  3. * Copyright (C) 2006-2022 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. /* for fips @wc_fips */
  31. #include <cyassl/ctaocrypt/hmac.h>
  32. #define WC_HMAC_BLOCK_SIZE HMAC_BLOCK_SIZE
  33. #endif
  34. #if defined(HAVE_FIPS) && \
  35. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  36. #include <wolfssl/wolfcrypt/fips.h>
  37. #endif
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* avoid redefinition of structs */
  42. #if !defined(HAVE_FIPS) || \
  43. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  44. #ifdef WOLFSSL_ASYNC_CRYPT
  45. #include <wolfssl/wolfcrypt/async.h>
  46. #endif
  47. #if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_HMAC)
  48. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  49. #endif
  50. #ifndef NO_OLD_WC_NAMES
  51. #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
  52. #endif
  53. #define WC_HMAC_INNER_HASH_KEYED_SW 1
  54. #define WC_HMAC_INNER_HASH_KEYED_DEV 2
  55. enum {
  56. HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
  57. IPAD = 0x36,
  58. OPAD = 0x5C,
  59. /* If any hash is not enabled, add the ID here. */
  60. #ifdef NO_MD5
  61. WC_MD5 = WC_HASH_TYPE_MD5,
  62. #endif
  63. #ifdef NO_SHA
  64. WC_SHA = WC_HASH_TYPE_SHA,
  65. #endif
  66. #ifdef NO_SHA256
  67. WC_SHA256 = WC_HASH_TYPE_SHA256,
  68. #endif
  69. #ifndef WOLFSSL_SHA512
  70. WC_SHA512 = WC_HASH_TYPE_SHA512,
  71. #ifndef WOLFSSL_NOSHA512_224
  72. WC_SHA512_224 = WC_HASH_TYPE_SHA512_224,
  73. #endif
  74. #ifndef WOLFSSL_NOSHA512_256
  75. WC_SHA512_256 = WC_HASH_TYPE_SHA512_256,
  76. #endif
  77. #endif
  78. #ifndef WOLFSSL_SHA384
  79. WC_SHA384 = WC_HASH_TYPE_SHA384,
  80. #endif
  81. #ifndef WOLFSSL_SHA224
  82. WC_SHA224 = WC_HASH_TYPE_SHA224,
  83. #endif
  84. #ifndef WOLFSSL_SHA3
  85. WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
  86. WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
  87. WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
  88. WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
  89. #endif
  90. #ifdef WOLF_PRIVATE_KEY_ID
  91. HMAC_MAX_ID_LEN = 32,
  92. HMAC_MAX_LABEL_LEN = 32,
  93. #endif
  94. };
  95. /* Select the largest available hash for the buffer size. */
  96. #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
  97. #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && \
  98. !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && \
  99. defined(WOLFSSL_SHA224) && defined(NO_SHA) && defined(NO_MD5)
  100. #error "You have to have some kind of hash if you want to use HMAC."
  101. #endif
  102. /* hmac hash union */
  103. typedef union {
  104. #ifndef NO_MD5
  105. wc_Md5 md5;
  106. #endif
  107. #ifndef NO_SHA
  108. wc_Sha sha;
  109. #endif
  110. #ifdef WOLFSSL_SHA224
  111. wc_Sha224 sha224;
  112. #endif
  113. #ifndef NO_SHA256
  114. wc_Sha256 sha256;
  115. #endif
  116. #ifdef WOLFSSL_SHA384
  117. wc_Sha384 sha384;
  118. #endif
  119. #ifdef WOLFSSL_SHA512
  120. wc_Sha512 sha512;
  121. #endif
  122. #ifdef WOLFSSL_SHA3
  123. wc_Sha3 sha3;
  124. #endif
  125. } wc_HmacHash;
  126. /* Hmac digest */
  127. struct Hmac {
  128. wc_HmacHash hash;
  129. word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
  130. word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)];
  131. word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
  132. void* heap; /* heap hint */
  133. byte macType; /* md5 sha or sha256 */
  134. byte innerHashKeyed; /* keyed flag */
  135. #ifdef WOLFSSL_KCAPI_HMAC
  136. struct kcapi_handle* handle;
  137. #endif
  138. #ifdef WOLFSSL_ASYNC_CRYPT
  139. WC_ASYNC_DEV asyncDev;
  140. #endif /* WOLFSSL_ASYNC_CRYPT */
  141. #if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HMAC)
  142. WC_CRYPTODEV ctx;
  143. #endif
  144. #ifdef WOLF_CRYPTO_CB
  145. int devId;
  146. void* devCtx;
  147. const byte* keyRaw;
  148. #endif
  149. #ifdef WOLF_PRIVATE_KEY_ID
  150. byte id[HMAC_MAX_ID_LEN];
  151. int idLen;
  152. char label[HMAC_MAX_LABEL_LEN];
  153. int labelLen;
  154. #endif
  155. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  156. word16 keyLen; /* hmac key length (key in ipad) */
  157. #endif
  158. };
  159. #ifndef WC_HMAC_TYPE_DEFINED
  160. typedef struct Hmac Hmac;
  161. #define WC_HMAC_TYPE_DEFINED
  162. #endif
  163. #endif /* HAVE_FIPS */
  164. /* does init */
  165. WOLFSSL_API int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 keySz);
  166. WOLFSSL_API int wc_HmacUpdate(Hmac* hmac, const byte* in, word32 sz);
  167. WOLFSSL_API int wc_HmacFinal(Hmac* hmac, byte* out);
  168. #ifdef WOLFSSL_KCAPI_HMAC
  169. WOLFSSL_API int wc_HmacSetKey_Software(Hmac* hmac, int type, const byte* key,
  170. word32 keySz);
  171. WOLFSSL_API int wc_HmacUpdate_Software(Hmac* hmac, const byte* in, word32 sz);
  172. WOLFSSL_API int wc_HmacFinal_Software(Hmac* hmac, byte* out);
  173. #endif
  174. WOLFSSL_API int wc_HmacSizeByType(int type);
  175. WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
  176. #ifdef WOLF_PRIVATE_KEY_ID
  177. WOLFSSL_API int wc_HmacInit_Id(Hmac* hmac, byte* id, int len, void* heap,
  178. int devId);
  179. WOLFSSL_API int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap,
  180. int devId);
  181. #endif
  182. WOLFSSL_API void wc_HmacFree(Hmac* hmac);
  183. WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
  184. WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
  185. #ifdef HAVE_HKDF
  186. WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
  187. const byte* inKey, word32 inKeySz, byte* out);
  188. WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
  189. const byte* info, word32 infoSz,
  190. byte* out, word32 outSz);
  191. WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  192. const byte* salt, word32 saltSz,
  193. const byte* info, word32 infoSz,
  194. byte* out, word32 outSz);
  195. #endif /* HAVE_HKDF */
  196. #ifdef __cplusplus
  197. } /* extern "C" */
  198. #endif
  199. #endif /* NO_HMAC */
  200. #endif /* WOLF_CRYPT_HMAC_H */