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