sha2.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * \file sha2.h
  3. *
  4. * \brief SHA-224 and SHA-256 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_SHA2_H
  28. #define POLARSSL_SHA2_H
  29. #include <string.h>
  30. /**
  31. * \brief SHA-256 context structure
  32. */
  33. typedef struct
  34. {
  35. unsigned long total[2]; /*!< number of bytes processed */
  36. unsigned long state[8]; /*!< intermediate digest state */
  37. unsigned char buffer[64]; /*!< data block being processed */
  38. unsigned char ipad[64]; /*!< HMAC: inner padding */
  39. unsigned char opad[64]; /*!< HMAC: outer padding */
  40. int is224; /*!< 0 => SHA-256, else SHA-224 */
  41. }
  42. sha2_context;
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * \brief SHA-256 context setup
  48. *
  49. * \param ctx context to be initialized
  50. * \param is224 0 = use SHA256, 1 = use SHA224
  51. */
  52. void sha2_starts( sha2_context *ctx, int is224 );
  53. /**
  54. * \brief SHA-256 process buffer
  55. *
  56. * \param ctx SHA-256 context
  57. * \param input buffer holding the data
  58. * \param ilen length of the input data
  59. */
  60. void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
  61. /**
  62. * \brief SHA-256 final digest
  63. *
  64. * \param ctx SHA-256 context
  65. * \param output SHA-224/256 checksum result
  66. */
  67. void sha2_finish( sha2_context *ctx, unsigned char output[32] );
  68. /**
  69. * \brief Output = SHA-256( input buffer )
  70. *
  71. * \param input buffer holding the data
  72. * \param ilen length of the input data
  73. * \param output SHA-224/256 checksum result
  74. * \param is224 0 = use SHA256, 1 = use SHA224
  75. */
  76. void sha2( const unsigned char *input, size_t ilen,
  77. unsigned char output[32], int is224 );
  78. /**
  79. * \brief Output = SHA-256( file contents )
  80. *
  81. * \param path input file name
  82. * \param output SHA-224/256 checksum result
  83. * \param is224 0 = use SHA256, 1 = use SHA224
  84. *
  85. * \return 0 if successful, 1 if fopen failed,
  86. * or 2 if fread failed
  87. */
  88. int sha2_file( const char *path, unsigned char output[32], int is224 );
  89. /**
  90. * \brief SHA-256 HMAC context setup
  91. *
  92. * \param ctx HMAC context to be initialized
  93. * \param key HMAC secret key
  94. * \param keylen length of the HMAC key
  95. * \param is224 0 = use SHA256, 1 = use SHA224
  96. */
  97. void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
  98. int is224 );
  99. /**
  100. * \brief SHA-256 HMAC process buffer
  101. *
  102. * \param ctx HMAC context
  103. * \param input buffer holding the data
  104. * \param ilen length of the input data
  105. */
  106. void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
  107. /**
  108. * \brief SHA-256 HMAC final digest
  109. *
  110. * \param ctx HMAC context
  111. * \param output SHA-224/256 HMAC checksum result
  112. */
  113. void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
  114. /**
  115. * \brief SHA-256 HMAC context reset
  116. *
  117. * \param ctx HMAC context to be reset
  118. */
  119. void sha2_hmac_reset( sha2_context *ctx );
  120. /**
  121. * \brief Output = HMAC-SHA-256( hmac key, input buffer )
  122. *
  123. * \param key HMAC secret key
  124. * \param keylen length of the HMAC key
  125. * \param input buffer holding the data
  126. * \param ilen length of the input data
  127. * \param output HMAC-SHA-224/256 result
  128. * \param is224 0 = use SHA256, 1 = use SHA224
  129. */
  130. void sha2_hmac( const unsigned char *key, size_t keylen,
  131. const unsigned char *input, size_t ilen,
  132. unsigned char output[32], int is224 );
  133. /**
  134. * \brief Checkup routine
  135. *
  136. * \return 0 if successful, or 1 if the test failed
  137. */
  138. int sha2_self_test( int verbose );
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif /* sha2.h */