|
18 | 18 |
|
19 | 19 | #define TX_IDLE_BUFFER_SIZE (MBEDTLS_SSL_HEADER_LEN + CACHE_BUFFER_SIZE) |
20 | 20 |
|
| 21 | +#define ESP_MBEDTLS_RETURN_IF_RX_BUF_STATIC(ssl) \ |
| 22 | + do { \ |
| 23 | + if (ssl->MBEDTLS_PRIVATE(in_buf)) { \ |
| 24 | + esp_mbedtls_ssl_buf_states state = esp_mbedtls_get_buf_state(ssl->MBEDTLS_PRIVATE(in_buf)); \ |
| 25 | + if (state == ESP_MBEDTLS_SSL_BUF_STATIC) { \ |
| 26 | + return 0; \ |
| 27 | + } \ |
| 28 | + } \ |
| 29 | + } while(0) |
| 30 | + |
| 31 | + |
21 | 32 | static const char *TAG = "Dynamic Impl"; |
22 | 33 |
|
23 | 34 | static void esp_mbedtls_set_buf_state(unsigned char *buf, esp_mbedtls_ssl_buf_states state) |
@@ -140,6 +151,29 @@ static void init_rx_buffer(mbedtls_ssl_context *ssl, unsigned char *buf) |
140 | 151 | ssl->MBEDTLS_PRIVATE(in_left) = 0; |
141 | 152 | } |
142 | 153 |
|
| 154 | +esp_err_t esp_mbedtls_dynamic_set_rx_buf_static(mbedtls_ssl_context *ssl) |
| 155 | +{ |
| 156 | + unsigned char cache_buf[16]; |
| 157 | + memcpy(cache_buf, ssl->MBEDTLS_PRIVATE(in_buf), 16); |
| 158 | + esp_mbedtls_reset_free_rx_buffer(ssl); |
| 159 | + |
| 160 | + struct esp_mbedtls_ssl_buf *esp_buf; |
| 161 | + int buffer_len = tx_buffer_len(ssl, MBEDTLS_SSL_IN_BUFFER_LEN); |
| 162 | + esp_buf = mbedtls_calloc(1, SSL_BUF_HEAD_OFFSET_SIZE + buffer_len); |
| 163 | + if (!esp_buf) { |
| 164 | + ESP_LOGE(TAG, "rx buf alloc(%d bytes) failed", SSL_BUF_HEAD_OFFSET_SIZE + buffer_len); |
| 165 | + return ESP_ERR_NO_MEM; |
| 166 | + } |
| 167 | + esp_mbedtls_init_ssl_buf(esp_buf, buffer_len); |
| 168 | + init_rx_buffer(ssl, esp_buf->buf); |
| 169 | + |
| 170 | + memcpy(ssl->MBEDTLS_PRIVATE(in_ctr), cache_buf, 8); |
| 171 | + memcpy(ssl->MBEDTLS_PRIVATE(in_iv), cache_buf + 8, 8); |
| 172 | + esp_mbedtls_set_buf_state(ssl->MBEDTLS_PRIVATE(in_buf), ESP_MBEDTLS_SSL_BUF_STATIC); |
| 173 | + return ESP_OK; |
| 174 | + |
| 175 | +} |
| 176 | + |
143 | 177 | static int esp_mbedtls_alloc_tx_buf(mbedtls_ssl_context *ssl, int len) |
144 | 178 | { |
145 | 179 | struct esp_mbedtls_ssl_buf *esp_buf; |
@@ -324,6 +358,12 @@ int esp_mbedtls_free_tx_buffer(mbedtls_ssl_context *ssl) |
324 | 358 |
|
325 | 359 | int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl) |
326 | 360 | { |
| 361 | + /* |
| 362 | + * If RX buffer is set to static mode, this macro will return early |
| 363 | + * and skip dynamic buffer allocation logic below |
| 364 | + */ |
| 365 | + ESP_MBEDTLS_RETURN_IF_RX_BUF_STATIC(ssl); |
| 366 | + |
327 | 367 | int cached = 0; |
328 | 368 | int ret = 0; |
329 | 369 | int buffer_len; |
@@ -405,6 +445,12 @@ int esp_mbedtls_add_rx_buffer(mbedtls_ssl_context *ssl) |
405 | 445 |
|
406 | 446 | int esp_mbedtls_free_rx_buffer(mbedtls_ssl_context *ssl) |
407 | 447 | { |
| 448 | + /* |
| 449 | + * If RX buffer is set to static mode, this macro will return early |
| 450 | + * and skip dynamic buffer free logic below |
| 451 | + */ |
| 452 | + ESP_MBEDTLS_RETURN_IF_RX_BUF_STATIC(ssl); |
| 453 | + |
408 | 454 | int ret = 0; |
409 | 455 | unsigned char buf[16]; |
410 | 456 | struct esp_mbedtls_ssl_buf *esp_buf; |
|
0 commit comments