md5.h 4.2 KB

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