utils_sha256.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _IOTX_COMMON_SHA256_H_
  31. #define _IOTX_COMMON_SHA256_H_
  32. #include <stdint.h>
  33. #define SHA256_DIGEST_LENGTH (32)
  34. #define SHA256_BLOCK_LENGTH (64)
  35. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  36. #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
  37. /**
  38. * \brief SHA-256 context structure
  39. */
  40. typedef struct {
  41. uint32_t total[2]; /*!< number of bytes processed */
  42. uint32_t state[8]; /*!< intermediate digest state */
  43. unsigned char buffer[64]; /*!< data block being processed */
  44. int is224; /*!< 0 => SHA-256, else SHA-224 */
  45. } iot_sha256_context;
  46. typedef union {
  47. char sptr[8];
  48. uint64_t lint;
  49. } u_retLen;
  50. /**
  51. * \brief Initialize SHA-256 context
  52. *
  53. * \param ctx SHA-256 context to be initialized
  54. */
  55. void utils_sha256_init(iot_sha256_context *ctx);
  56. /**
  57. * \brief Clear SHA-256 context
  58. *
  59. * \param ctx SHA-256 context to be cleared
  60. */
  61. void utils_sha256_free(iot_sha256_context *ctx);
  62. /**
  63. * \brief Clone (the state of) a SHA-256 context
  64. *
  65. * \param dst The destination context
  66. * \param src The context to be cloned
  67. */
  68. void utils_sha256_clone(iot_sha256_context *dst,
  69. const iot_sha256_context *src);
  70. /**
  71. * \brief SHA-256 context setup
  72. *
  73. * \param ctx context to be initialized
  74. */
  75. void utils_sha256_starts(iot_sha256_context *ctx);
  76. /**
  77. * \brief SHA-256 process buffer
  78. *
  79. * \param ctx SHA-256 context
  80. * \param input buffer holding the data
  81. * \param ilen length of the input data
  82. */
  83. void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen);
  84. /**
  85. * \brief SHA-256 final digest
  86. *
  87. * \param ctx SHA-256 context
  88. * \param output SHA-256 checksum result
  89. */
  90. void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32]);
  91. /* Internal use */
  92. void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64]);
  93. /**
  94. * \brief Output = SHA-256( input buffer )
  95. *
  96. * \param input buffer holding the data
  97. * \param ilen length of the input data
  98. * \param output SHA-256 checksum result
  99. */
  100. void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32]);
  101. #endif