sha1.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * \file sha1.h
  3. *
  4. * \brief SHA-1 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_SHA1_H
  28. #define POLARSSL_SHA1_H
  29. #include <string.h>
  30. /**
  31. * \brief SHA-1 context structure
  32. */
  33. typedef struct
  34. {
  35. unsigned long total[2]; /*!< number of bytes processed */
  36. unsigned long state[5]; /*!< 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. }
  41. sha1_context;
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /**
  46. * \brief SHA-1 context setup
  47. *
  48. * \param ctx context to be initialized
  49. */
  50. void sha1_starts( sha1_context *ctx );
  51. /**
  52. * \brief SHA-1 process buffer
  53. *
  54. * \param ctx SHA-1 context
  55. * \param input buffer holding the data
  56. * \param ilen length of the input data
  57. */
  58. void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
  59. /**
  60. * \brief SHA-1 final digest
  61. *
  62. * \param ctx SHA-1 context
  63. * \param output SHA-1 checksum result
  64. */
  65. void sha1_finish( sha1_context *ctx, unsigned char output[20] );
  66. /**
  67. * \brief Output = SHA-1( input buffer )
  68. *
  69. * \param input buffer holding the data
  70. * \param ilen length of the input data
  71. * \param output SHA-1 checksum result
  72. */
  73. void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
  74. /**
  75. * \brief Output = SHA-1( file contents )
  76. *
  77. * \param path input file name
  78. * \param output SHA-1 checksum result
  79. *
  80. * \return 0 if successful, 1 if fopen failed,
  81. * or 2 if fread failed
  82. */
  83. int sha1_file( const char *path, unsigned char output[20] );
  84. /**
  85. * \brief SHA-1 HMAC context setup
  86. *
  87. * \param ctx HMAC context to be initialized
  88. * \param key HMAC secret key
  89. * \param keylen length of the HMAC key
  90. */
  91. void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen );
  92. /**
  93. * \brief SHA-1 HMAC process buffer
  94. *
  95. * \param ctx HMAC context
  96. * \param input buffer holding the data
  97. * \param ilen length of the input data
  98. */
  99. void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
  100. /**
  101. * \brief SHA-1 HMAC final digest
  102. *
  103. * \param ctx HMAC context
  104. * \param output SHA-1 HMAC checksum result
  105. */
  106. void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] );
  107. /**
  108. * \brief SHA-1 HMAC context reset
  109. *
  110. * \param ctx HMAC context to be reset
  111. */
  112. void sha1_hmac_reset( sha1_context *ctx );
  113. /**
  114. * \brief Output = HMAC-SHA-1( hmac key, input buffer )
  115. *
  116. * \param key HMAC secret key
  117. * \param keylen length of the HMAC key
  118. * \param input buffer holding the data
  119. * \param ilen length of the input data
  120. * \param output HMAC-SHA-1 result
  121. */
  122. void sha1_hmac( const unsigned char *key, size_t keylen,
  123. const unsigned char *input, size_t ilen,
  124. unsigned char output[20] );
  125. /**
  126. * \brief Checkup routine
  127. *
  128. * \return 0 if successful, or 1 if the test failed
  129. */
  130. int sha1_self_test( int verbose );
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif /* sha1.h */