esp_rom_md5.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stdint.h>
  11. #include "sdkconfig.h"
  12. /**
  13. * The MD5 functions calculate a 128-bit cryptographic digest for any number of input bytes.
  14. */
  15. #define ESP_ROM_MD5_DIGEST_LEN 16
  16. #if CONFIG_IDF_TARGET_ESP8684
  17. /**
  18. * \brief MD5 context structure
  19. *
  20. * \warning MD5 is considered a weak message digest and its use
  21. * constitutes a security risk. We recommend considering
  22. * stronger message digests instead.
  23. *
  24. */
  25. typedef struct mbedtls_md5_context {
  26. uint32_t total[2]; /*!< number of bytes processed */
  27. uint32_t state[4]; /*!< intermediate digest state */
  28. unsigned char buffer[64]; /*!< data block being processed */
  29. } md5_context_t;
  30. /* Functions extracted from ROM, do not use it as an public API */
  31. void esp_rom_mbedtls_md5_starts_ret(md5_context_t *context);
  32. void esp_rom_mbedtls_md5_update_ret(md5_context_t *context, const void *buf, uint32_t len);
  33. void esp_rom_mbedtls_md5_finish_ret(md5_context_t *context, uint8_t *digest);
  34. /**
  35. * @brief Initialize the MD5 context
  36. *
  37. * @param context Context object allocated by user
  38. */
  39. static inline void esp_rom_md5_init(md5_context_t *context)
  40. {
  41. esp_rom_mbedtls_md5_starts_ret(context);
  42. }
  43. /**
  44. * @brief Running MD5 algorithm over input data
  45. *
  46. * @param context MD5 context which has been initialized by `MD5Init`
  47. * @param buf Input buffer
  48. * @param len Buffer length in bytes
  49. */
  50. static inline void esp_rom_md5_update(md5_context_t *context, const void *buf, uint32_t len)
  51. {
  52. esp_rom_mbedtls_md5_update_ret(context, buf, len);
  53. }
  54. /**
  55. * @brief Extract the MD5 result, and erase the context
  56. *
  57. * @param digest Where to store the 128-bit digest value
  58. * @param context MD5 context
  59. */
  60. static inline void esp_rom_md5_final(uint8_t *digest, md5_context_t *context)
  61. {
  62. esp_rom_mbedtls_md5_finish_ret(context, digest);
  63. }
  64. #else //#if !CONFIG_IDF_TARGET_ESP8684
  65. /**
  66. * @brief Type defined for MD5 context
  67. *
  68. */
  69. typedef struct MD5Context {
  70. uint32_t buf[4];
  71. uint32_t bits[2];
  72. uint8_t in[64];
  73. } md5_context_t;
  74. /**
  75. * @brief Initialize the MD5 context
  76. *
  77. * @param context Context object allocated by user
  78. */
  79. void esp_rom_md5_init(md5_context_t *context);
  80. /**
  81. * @brief Running MD5 algorithm over input data
  82. *
  83. * @param context MD5 context which has been initialized by `MD5Init`
  84. * @param buf Input buffer
  85. * @param len Buffer length in bytes
  86. */
  87. void esp_rom_md5_update(md5_context_t *context, const void *buf, uint32_t len);
  88. /**
  89. * @brief Extract the MD5 result, and erase the context
  90. *
  91. * @param digest Where to store the 128-bit digest value
  92. * @param context MD5 context
  93. */
  94. void esp_rom_md5_final(uint8_t *digest, md5_context_t *context);
  95. #endif
  96. #ifdef __cplusplus
  97. }
  98. #endif