utils_sha2.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #ifndef _UTILS_SHA2_H
  16. #define _UTILS_SHA2_H
  17. #include <stdint.h>
  18. #define SHA256_DIGEST_LENGTH (32)
  19. #define SHA256_BLOCK_LENGTH (64)
  20. #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
  21. #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
  22. /**
  23. * \brief SHA-256 context structure
  24. */
  25. typedef struct {
  26. uint32_t total[2]; /*!< number of bytes processed */
  27. uint32_t state[8]; /*!< intermediate digest state */
  28. unsigned char buffer[64]; /*!< data block being processed */
  29. int is224; /*!< 0 => SHA-256, else SHA-224 */
  30. } iot_sha256_context;
  31. typedef union {
  32. char sptr[8];
  33. uint64_t lint;
  34. } u_retLen;
  35. /**
  36. * \brief Initialize SHA-256 context
  37. *
  38. * \param ctx SHA-256 context to be initialized
  39. */
  40. void utils_sha256_init(iot_sha256_context *ctx);
  41. /**
  42. * \brief Clear SHA-256 context
  43. *
  44. * \param ctx SHA-256 context to be cleared
  45. */
  46. void utils_sha256_free(iot_sha256_context *ctx);
  47. /**
  48. * \brief SHA-256 context setup
  49. *
  50. * \param ctx context to be initialized
  51. */
  52. void utils_sha256_starts(iot_sha256_context *ctx);
  53. /**
  54. * \brief SHA-256 process buffer
  55. *
  56. * \param ctx SHA-256 context
  57. * \param input buffer holding the data
  58. * \param ilen length of the input data
  59. */
  60. void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen);
  61. /**
  62. * \brief SHA-256 final digest
  63. *
  64. * \param ctx SHA-256 context
  65. * \param output SHA-256 checksum result
  66. */
  67. void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32]);
  68. /* Internal use */
  69. void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64]);
  70. /**
  71. * \brief Output = SHA-256( input buffer )
  72. *
  73. * \param input buffer holding the data
  74. * \param ilen length of the input data
  75. * \param output SHA-256 checksum result
  76. */
  77. void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32]);
  78. void utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32]);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif