hmac.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* hmac.h
  2. *
  3. * Copyright (C) 2006-2013 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifndef NO_HMAC
  22. #ifndef CTAO_CRYPT_HMAC_H
  23. #define CTAO_CRYPT_HMAC_H
  24. #include <cyassl/ctaocrypt/types.h>
  25. #ifndef NO_MD5
  26. #include <cyassl/ctaocrypt/md5.h>
  27. #endif
  28. #ifndef NO_SHA
  29. #include <cyassl/ctaocrypt/sha.h>
  30. #endif
  31. #ifndef NO_SHA256
  32. #include <cyassl/ctaocrypt/sha256.h>
  33. #endif
  34. #ifdef CYASSL_SHA512
  35. #include <cyassl/ctaocrypt/sha512.h>
  36. #endif
  37. #ifdef HAVE_CAVIUM
  38. #include <cyassl/ctaocrypt/logging.h>
  39. #include "cavium_common.h"
  40. #endif
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #define CYASSL_HMAC_CAVIUM_MAGIC 0xBEEF0005
  45. enum {
  46. IPAD = 0x36,
  47. OPAD = 0x5C,
  48. /* If any hash is not enabled, add the ID here. */
  49. #ifdef NO_MD5
  50. MD5 = 0,
  51. #endif
  52. #ifdef NO_SHA
  53. SHA = 1,
  54. #endif
  55. #ifdef NO_SHA256
  56. SHA256 = 2,
  57. #endif
  58. #ifndef CYASSL_SHA512
  59. SHA512 = 4,
  60. #endif
  61. #ifndef CYASSL_SHA384
  62. SHA384 = 5,
  63. #endif
  64. /* Select the largest available hash for the buffer size. */
  65. #if defined(CYASSL_SHA512)
  66. INNER_HASH_SIZE = SHA512_DIGEST_SIZE,
  67. HMAC_BLOCK_SIZE = SHA512_BLOCK_SIZE
  68. #elif defined(CYASSL_SHA384)
  69. INNER_HASH_SIZE = SHA384_DIGEST_SIZE,
  70. HMAC_BLOCK_SIZE = SHA384_BLOCK_SIZE
  71. #elif !defined(NO_SHA256)
  72. INNER_HASH_SIZE = SHA256_DIGEST_SIZE,
  73. HMAC_BLOCK_SIZE = SHA256_BLOCK_SIZE
  74. #elif !defined(NO_SHA)
  75. INNER_HASH_SIZE = SHA_DIGEST_SIZE,
  76. HMAC_BLOCK_SIZE = SHA_BLOCK_SIZE
  77. #elif !defined(NO_MD5)
  78. INNER_HASH_SIZE = MD5_DIGEST_SIZE,
  79. HMAC_BLOCK_SIZE = 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. Md5 md5;
  88. #endif
  89. #ifndef NO_SHA
  90. Sha sha;
  91. #endif
  92. #ifndef NO_SHA256
  93. Sha256 sha256;
  94. #endif
  95. #ifdef CYASSL_SHA384
  96. Sha384 sha384;
  97. #endif
  98. #ifdef CYASSL_SHA512
  99. Sha512 sha512;
  100. #endif
  101. } Hash;
  102. /* Hmac digest */
  103. typedef struct Hmac {
  104. Hash hash;
  105. word32 ipad[HMAC_BLOCK_SIZE / sizeof(word32)]; /* same block size all*/
  106. word32 opad[HMAC_BLOCK_SIZE / sizeof(word32)];
  107. word32 innerHash[INNER_HASH_SIZE / sizeof(word32)]; /* max size */
  108. byte macType; /* md5 sha or sha256 */
  109. byte innerHashKeyed; /* keyed flag */
  110. #ifdef HAVE_CAVIUM
  111. word16 keyLen; /* hmac key length */
  112. word16 dataLen;
  113. HashType type; /* hmac key type */
  114. int devId; /* nitrox device id */
  115. word32 magic; /* using cavium magic */
  116. word64 contextHandle; /* nitrox context memory handle */
  117. byte* data; /* buffered input data for one call */
  118. #endif
  119. } Hmac;
  120. /* does init */
  121. CYASSL_API void HmacSetKey(Hmac*, int type, const byte* key, word32 keySz);
  122. CYASSL_API void HmacUpdate(Hmac*, const byte*, word32);
  123. CYASSL_API void HmacFinal(Hmac*, byte*);
  124. #ifdef HAVE_CAVIUM
  125. CYASSL_API int HmacInitCavium(Hmac*, int);
  126. CYASSL_API void HmacFreeCavium(Hmac*);
  127. #endif
  128. #ifdef __cplusplus
  129. } /* extern "C" */
  130. #endif
  131. #endif /* CTAO_CRYPT_HMAC_H */
  132. #endif /* NO_HMAC */