You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. /**
  2. * \file sha1.h
  3. *
  4. * \brief SHA-1 cryptographic hash function
  5. *
  6. * Copyright (C) 2006-2013, 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 HMAC_SHA1_H_
  28. #define HMAC_SHA1_H_
  29. #include <stdint.h>
  30. #include <string.h>
  31. /*
  32. * 32-bit integer manipulation macros (big endian)
  33. */
  34. #define GET_UINT32_BE(n,b,i) \
  35. { \
  36. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  37. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  38. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  39. | ( (uint32_t) (b)[(i) + 3] ); \
  40. }
  41. #define PUT_UINT32_BE(n,b,i) \
  42. { \
  43. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  44. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  45. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  46. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  47. }
  48. /**
  49. * \brief SHA-1 context structure
  50. */
  51. typedef struct
  52. {
  53. uint32_t total[2]; /*!< number of bytes processed */
  54. uint32_t state[5]; /*!< intermediate digest state */
  55. unsigned char buffer[64]; /*!< data block being processed */
  56. unsigned char ipad[64]; /*!< HMAC: inner padding */
  57. unsigned char opad[64]; /*!< HMAC: outer padding */
  58. }
  59. ss_sha1_context;
  60. /**
  61. * \brief SHA-1 context setup
  62. *
  63. * \param ctx context to be initialized
  64. */
  65. void ss_sha1_starts( ss_sha1_context *ctx );
  66. /**
  67. * \brief SHA-1 process buffer
  68. *
  69. * \param ctx SHA-1 context
  70. * \param input buffer holding the data
  71. * \param ilen length of the input data
  72. */
  73. void ss_sha1_update( ss_sha1_context *ctx, const unsigned char *input, size_t ilen );
  74. /**
  75. * \brief SHA-1 final digest
  76. *
  77. * \param ctx SHA-1 context
  78. * \param output SHA-1 checksum result
  79. */
  80. void ss_sha1_finish( ss_sha1_context *ctx, unsigned char output[20] );
  81. /* Internal use */
  82. void ss_sha1_process( ss_sha1_context *ctx, const unsigned char data[64] );
  83. /**
  84. * \brief Output = SHA-1( input buffer )
  85. *
  86. * \param input buffer holding the data
  87. * \param ilen length of the input data
  88. * \param output SHA-1 checksum result
  89. */
  90. void ss_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
  91. /**
  92. * \brief SHA-1 HMAC context setup
  93. *
  94. * \param ctx HMAC context to be initialized
  95. * \param key HMAC secret key
  96. * \param keylen length of the HMAC key
  97. */
  98. void ss_sha1_hmac_starts( ss_sha1_context *ctx, const unsigned char *key, size_t keylen );
  99. /**
  100. * \brief SHA-1 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 ss_sha1_hmac_update( ss_sha1_context *ctx, const unsigned char *input, size_t ilen );
  107. /**
  108. * \brief SHA-1 HMAC final digest
  109. *
  110. * \param ctx HMAC context
  111. * \param output SHA-1 HMAC checksum result
  112. */
  113. void ss_sha1_hmac_finish( ss_sha1_context *ctx, unsigned char output[20] );
  114. /**
  115. * \brief SHA-1 HMAC context reset
  116. *
  117. * \param ctx HMAC context to be reset
  118. */
  119. void ss_sha1_hmac_reset( ss_sha1_context *ctx );
  120. /**
  121. * \brief Output = HMAC-SHA-1( 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-1 result
  128. */
  129. void ss_sha1_hmac( const unsigned char *key, size_t keylen,
  130. const unsigned char *input, size_t ilen,
  131. unsigned char output[20] );
  132. #endif /* HMAC_SHA1_H_ */