tiny_sha2.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * \file sha2.h
  3. *
  4. * Based on TropicSSL: Copyright (C) 2017 Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
  7. *
  8. * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * * Neither the names of PolarSSL or XySSL nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  31. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #ifndef TINY_CRYPT_SHA2_H__
  38. #define TINY_CRYPT_SHA2_H__
  39. /**
  40. * \brief SHA-256 context structure
  41. */
  42. typedef struct {
  43. unsigned long total[2]; /*!< number of bytes processed */
  44. unsigned long state[8]; /*!< intermediate digest state */
  45. unsigned char buffer[64]; /*!< data block being processed */
  46. unsigned char ipad[64]; /*!< HMAC: inner padding */
  47. unsigned char opad[64]; /*!< HMAC: outer padding */
  48. int is224; /*!< 0 => SHA-256, else SHA-224 */
  49. } tiny_sha2_context;
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**
  54. * \brief SHA-256 context setup
  55. *
  56. * \param ctx context to be initialized
  57. * \param is224 0 = use SHA256, 1 = use SHA224
  58. */
  59. void tiny_sha2_starts(tiny_sha2_context * ctx, int is224);
  60. /**
  61. * \brief SHA-256 process buffer
  62. *
  63. * \param ctx SHA-256 context
  64. * \param input buffer holding the data
  65. * \param ilen length of the input data
  66. */
  67. void tiny_sha2_update(tiny_sha2_context * ctx, unsigned char *input, int ilen);
  68. /**
  69. * \brief SHA-256 final digest
  70. *
  71. * \param ctx SHA-256 context
  72. * \param output SHA-224/256 checksum result
  73. */
  74. void tiny_sha2_finish(tiny_sha2_context * ctx, unsigned char output[32]);
  75. /**
  76. * \brief Output = SHA-256( input buffer )
  77. *
  78. * \param input buffer holding the data
  79. * \param ilen length of the input data
  80. * \param output SHA-224/256 checksum result
  81. * \param is224 0 = use SHA256, 1 = use SHA224
  82. */
  83. void tiny_sha2(unsigned char *input, int ilen,
  84. unsigned char output[32], int is224);
  85. /**
  86. * \brief SHA-256 HMAC context setup
  87. *
  88. * \param ctx HMAC context to be initialized
  89. * \param key HMAC secret key
  90. * \param keylen length of the HMAC key
  91. * \param is224 0 = use SHA256, 1 = use SHA224
  92. */
  93. void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, unsigned char *key,
  94. int keylen, int is224);
  95. /**
  96. * \brief SHA-256 HMAC process buffer
  97. *
  98. * \param ctx HMAC context
  99. * \param input buffer holding the data
  100. * \param ilen length of the input data
  101. */
  102. void tiny_sha2_hmac_update(tiny_sha2_context * ctx, unsigned char *input,
  103. int ilen);
  104. /**
  105. * \brief SHA-256 HMAC final digest
  106. *
  107. * \param ctx HMAC context
  108. * \param output SHA-224/256 HMAC checksum result
  109. */
  110. void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, unsigned char output[32]);
  111. /**
  112. * \brief Output = HMAC-SHA-256( hmac key, input buffer )
  113. *
  114. * \param key HMAC secret key
  115. * \param keylen length of the HMAC key
  116. * \param input buffer holding the data
  117. * \param ilen length of the input data
  118. * \param output HMAC-SHA-224/256 result
  119. * \param is224 0 = use SHA256, 1 = use SHA224
  120. */
  121. void tiny_sha2_hmac(unsigned char *key, int keylen,
  122. unsigned char *input, int ilen,
  123. unsigned char output[32], int is224);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif /* sha2.h */