hmac.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* hmac.h
  2. *
  3. * Copyright (C) 2006-2017 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. #ifndef NO_HMAC
  22. #ifndef WOLF_CRYPT_HMAC_H
  23. #define WOLF_CRYPT_HMAC_H
  24. #include <wolfssl/wolfcrypt/hash.h>
  25. #ifdef HAVE_FIPS
  26. /* for fips */
  27. #include <cyassl/ctaocrypt/hmac.h>
  28. #endif
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #ifndef HAVE_FIPS
  33. #ifdef WOLFSSL_ASYNC_CRYPT
  34. #include <wolfssl/wolfcrypt/async.h>
  35. #endif
  36. #ifndef NO_OLD_WC_NAMES
  37. #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
  38. #endif
  39. enum {
  40. HMAC_FIPS_MIN_KEY = 14, /* 112 bit key length minimum */
  41. IPAD = 0x36,
  42. OPAD = 0x5C,
  43. /* If any hash is not enabled, add the ID here. */
  44. #ifdef NO_MD5
  45. WC_MD5 = 0,
  46. #endif
  47. #ifdef NO_SHA
  48. WC_SHA = 1,
  49. #endif
  50. #ifdef NO_SHA256
  51. WC_SHA256 = 2,
  52. #endif
  53. #ifndef WOLFSSL_SHA512
  54. WC_SHA512 = 4,
  55. #endif
  56. #ifndef WOLFSSL_SHA384
  57. WC_SHA384 = 5,
  58. #endif
  59. #ifndef HAVE_BLAKE2
  60. BLAKE2B_ID = 7,
  61. #endif
  62. #ifndef WOLFSSL_SHA224
  63. WC_SHA224 = 8,
  64. #endif
  65. /* Select the largest available hash for the buffer size. */
  66. #if defined(WOLFSSL_SHA512)
  67. WC_HMAC_BLOCK_SIZE = WC_SHA512_BLOCK_SIZE,
  68. #elif defined(HAVE_BLAKE2)
  69. WC_HMAC_BLOCK_SIZE = BLAKE2B_BLOCKBYTES,
  70. #elif defined(WOLFSSL_SHA384)
  71. WC_HMAC_BLOCK_SIZE = WC_SHA384_BLOCK_SIZE
  72. #elif !defined(NO_SHA256)
  73. WC_HMAC_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE
  74. #elif defined(WOLFSSL_SHA224)
  75. WC_HMAC_BLOCK_SIZE = WC_SHA224_BLOCK_SIZE
  76. #elif !defined(NO_SHA)
  77. WC_HMAC_BLOCK_SIZE = WC_SHA_BLOCK_SIZE,
  78. #elif !defined(NO_MD5)
  79. WC_HMAC_BLOCK_SIZE = WC_MD5_BLOCK_SIZE,
  80. #else
  81. #error "You have to have some kind of hash if you want to use HMAC."
  82. #endif
  83. };
  84. /* hash union */
  85. typedef union {
  86. #ifndef NO_MD5
  87. wc_Md5 md5;
  88. #endif
  89. #ifndef NO_SHA
  90. wc_Sha sha;
  91. #endif
  92. #ifdef WOLFSSL_SHA224
  93. wc_Sha224 sha224;
  94. #endif
  95. #ifndef NO_SHA256
  96. wc_Sha256 sha256;
  97. #endif
  98. #ifdef WOLFSSL_SHA512
  99. #ifdef WOLFSSL_SHA384
  100. wc_Sha384 sha384;
  101. #endif
  102. wc_Sha512 sha512;
  103. #endif
  104. #ifdef HAVE_BLAKE2
  105. Blake2b blake2b;
  106. #endif
  107. } Hash;
  108. /* Hmac digest */
  109. typedef struct Hmac {
  110. Hash hash;
  111. word32 ipad[WC_HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
  112. word32 opad[WC_HMAC_BLOCK_SIZE / sizeof(word32)];
  113. word32 innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
  114. void* heap; /* heap hint */
  115. byte macType; /* md5 sha or sha256 */
  116. byte innerHashKeyed; /* keyed flag */
  117. #ifdef WOLFSSL_ASYNC_CRYPT
  118. WC_ASYNC_DEV asyncDev;
  119. word16 keyLen; /* hmac key length (key in ipad) */
  120. #ifdef HAVE_CAVIUM
  121. byte* data; /* buffered input data for one call */
  122. word16 dataLen;
  123. #endif /* HAVE_CAVIUM */
  124. #endif /* WOLFSSL_ASYNC_CRYPT */
  125. } Hmac;
  126. #endif /* HAVE_FIPS */
  127. /* does init */
  128. WOLFSSL_API int wc_HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
  129. WOLFSSL_API int wc_HmacUpdate(Hmac*, const byte*, word32);
  130. WOLFSSL_API int wc_HmacFinal(Hmac*, byte*);
  131. WOLFSSL_API int wc_HmacSizeByType(int type);
  132. WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
  133. WOLFSSL_API void wc_HmacFree(Hmac*);
  134. WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
  135. WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
  136. #ifdef HAVE_HKDF
  137. WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
  138. const byte* inKey, word32 inKeySz, byte* out);
  139. WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
  140. const byte* info, word32 infoSz,
  141. byte* out, word32 outSz);
  142. WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
  143. const byte* salt, word32 saltSz,
  144. const byte* info, word32 infoSz,
  145. byte* out, word32 outSz);
  146. #endif /* HAVE_HKDF */
  147. #ifdef __cplusplus
  148. } /* extern "C" */
  149. #endif
  150. #endif /* WOLF_CRYPT_HMAC_H */
  151. #endif /* NO_HMAC */