sha4.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * \file sha4.h
  3. *
  4. * \brief SHA-384 and SHA-512 cryptographic hash function
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_SHA4_H
  28. #define POLARSSL_SHA4_H
  29. #include <string.h>
  30. #if defined(_MSC_VER) || defined(__WATCOMC__)
  31. #define UL64(x) x##ui64
  32. #define int64 __int64
  33. #else
  34. #define UL64(x) x##ULL
  35. #define int64 long long
  36. #endif
  37. /**
  38. * \brief SHA-512 context structure
  39. */
  40. typedef struct
  41. {
  42. unsigned int64 total[2]; /*!< number of bytes processed */
  43. unsigned int64 state[8]; /*!< intermediate digest state */
  44. unsigned char buffer[128]; /*!< data block being processed */
  45. unsigned char ipad[128]; /*!< HMAC: inner padding */
  46. unsigned char opad[128]; /*!< HMAC: outer padding */
  47. int is384; /*!< 0 => SHA-512, else SHA-384 */
  48. }
  49. sha4_context;
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**
  54. * \brief SHA-512 context setup
  55. *
  56. * \param ctx context to be initialized
  57. * \param is384 0 = use SHA512, 1 = use SHA384
  58. */
  59. void sha4_starts( sha4_context *ctx, int is384 );
  60. /**
  61. * \brief SHA-512 process buffer
  62. *
  63. * \param ctx SHA-512 context
  64. * \param input buffer holding the data
  65. * \param ilen length of the input data
  66. */
  67. void sha4_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
  68. /**
  69. * \brief SHA-512 final digest
  70. *
  71. * \param ctx SHA-512 context
  72. * \param output SHA-384/512 checksum result
  73. */
  74. void sha4_finish( sha4_context *ctx, unsigned char output[64] );
  75. /**
  76. * \brief Output = SHA-512( input buffer )
  77. *
  78. * \param input buffer holding the data
  79. * \param ilen length of the input data
  80. * \param output SHA-384/512 checksum result
  81. * \param is384 0 = use SHA512, 1 = use SHA384
  82. */
  83. void sha4( const unsigned char *input, size_t ilen,
  84. unsigned char output[64], int is384 );
  85. /**
  86. * \brief Output = SHA-512( file contents )
  87. *
  88. * \param path input file name
  89. * \param output SHA-384/512 checksum result
  90. * \param is384 0 = use SHA512, 1 = use SHA384
  91. *
  92. * \return 0 if successful, 1 if fopen failed,
  93. * or 2 if fread failed
  94. */
  95. int sha4_file( const char *path, unsigned char output[64], int is384 );
  96. /**
  97. * \brief SHA-512 HMAC context setup
  98. *
  99. * \param ctx HMAC context to be initialized
  100. * \param is384 0 = use SHA512, 1 = use SHA384
  101. * \param key HMAC secret key
  102. * \param keylen length of the HMAC key
  103. */
  104. void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, size_t keylen,
  105. int is384 );
  106. /**
  107. * \brief SHA-512 HMAC process buffer
  108. *
  109. * \param ctx HMAC context
  110. * \param input buffer holding the data
  111. * \param ilen length of the input data
  112. */
  113. void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
  114. /**
  115. * \brief SHA-512 HMAC final digest
  116. *
  117. * \param ctx HMAC context
  118. * \param output SHA-384/512 HMAC checksum result
  119. */
  120. void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] );
  121. /**
  122. * \brief SHA-512 HMAC context reset
  123. *
  124. * \param ctx HMAC context to be reset
  125. */
  126. void sha4_hmac_reset( sha4_context *ctx );
  127. /**
  128. * \brief Output = HMAC-SHA-512( hmac key, input buffer )
  129. *
  130. * \param key HMAC secret key
  131. * \param keylen length of the HMAC key
  132. * \param input buffer holding the data
  133. * \param ilen length of the input data
  134. * \param output HMAC-SHA-384/512 result
  135. * \param is384 0 = use SHA512, 1 = use SHA384
  136. */
  137. void sha4_hmac( const unsigned char *key, size_t keylen,
  138. const unsigned char *input, size_t ilen,
  139. unsigned char output[64], int is384 );
  140. /**
  141. * \brief Checkup routine
  142. *
  143. * \return 0 if successful, or 1 if the test failed
  144. */
  145. int sha4_self_test( int verbose );
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* sha4.h */