From ab0537c07908b9e2dcf457124a59beff1ebd0ca1 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 21 Jul 2020 17:01:28 +0800 Subject: [PATCH] esp_rom: extract common MD5 hash apis into esp_rom_md5.h --- .../bootloader_support/src/flash_partitions.c | 8 +-- components/esp_eth/test/test_emac.c | 10 +-- components/esp_http_client/lib/http_auth.c | 8 +-- components/esp_rom/esp32/ld/esp32.rom.api.ld | 6 ++ .../esp_rom/esp32s2/ld/esp32s2.rom.api.ld | 6 ++ .../esp_rom/esp32s3/ld/esp32s3.rom.api.ld | 6 ++ components/esp_rom/include/esp_rom_md5.h | 63 +++++++++++++++++++ 7 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 components/esp_rom/include/esp_rom_md5.h diff --git a/components/bootloader_support/src/flash_partitions.c b/components/bootloader_support/src/flash_partitions.c index 56219c8981..51c6b33c61 100644 --- a/components/bootloader_support/src/flash_partitions.c +++ b/components/bootloader_support/src/flash_partitions.c @@ -15,7 +15,7 @@ #include "esp_flash_partitions.h" #include "esp_log.h" #include "esp32/rom/spi_flash.h" -#include "esp32/rom/md5_hash.h" +#include "esp_rom_md5.h" static const char *TAG = "flash_parts"; @@ -48,9 +48,9 @@ esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table struct MD5Context context; unsigned char digest[16]; - MD5Init(&context); - MD5Update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t)); - MD5Final(digest, &context); + esp_rom_md5_init(&context); + esp_rom_md5_update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t)); + esp_rom_md5_final(digest, &context); unsigned char *md5sum = ((unsigned char *) part) + 16; // skip the 2B magic number and the 14B fillup bytes diff --git a/components/esp_eth/test/test_emac.c b/components/esp_eth/test/test_emac.c index a071140859..df8381af23 100644 --- a/components/esp_eth/test/test_emac.c +++ b/components/esp_eth/test/test_emac.c @@ -13,7 +13,7 @@ #include "lwip/netdb.h" #include "lwip/sockets.h" #include "ping/ping_sock.h" -#include "esp32/rom/md5_hash.h" +#include "esp_rom_md5.h" #include "soc/soc_caps.h" #if SOC_EMAC_SUPPORTED @@ -36,7 +36,7 @@ static const char *TAG = "esp32_eth_test"; #define ETH_PING_END_TIMEOUT_MS (ETH_PING_DURATION_MS * 2) // compute md5 of download file -static struct MD5Context md5_context; +static md5_context_t md5_context; static uint8_t digest[16]; /** Event handler for Ethernet events */ @@ -411,7 +411,7 @@ esp_err_t http_event_handle(esp_http_client_event_t *evt) ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER"); break; case HTTP_EVENT_ON_DATA: - MD5Update(&md5_context, evt->data, evt->data_len); + esp_rom_md5_update(&md5_context, evt->data, evt->data_len); break; case HTTP_EVENT_ON_FINISH: ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH"); @@ -426,7 +426,7 @@ esp_err_t http_event_handle(esp_http_client_event_t *evt) static void eth_download_task(void *param) { EventGroupHandle_t eth_event_group = (EventGroupHandle_t)param; - MD5Init(&md5_context); + esp_rom_md5_init(&md5_context); esp_http_client_config_t config = { .url = "https://dl.espressif.com/dl/misc/2MB.bin", .event_handler = http_event_handle, @@ -436,7 +436,7 @@ static void eth_download_task(void *param) TEST_ASSERT_NOT_NULL(client); TEST_ESP_OK(esp_http_client_perform(client)); TEST_ESP_OK(esp_http_client_cleanup(client)); - MD5Final(digest, &md5_context); + esp_rom_md5_final(digest, &md5_context); xEventGroupSetBits(eth_event_group, ETH_DOWNLOAD_END_BIT); vTaskDelete(NULL); } diff --git a/components/esp_http_client/lib/http_auth.c b/components/esp_http_client/lib/http_auth.c index 1fcc2f1152..d89ccfec8d 100644 --- a/components/esp_http_client/lib/http_auth.c +++ b/components/esp_http_client/lib/http_auth.c @@ -19,7 +19,7 @@ #include "esp_netif.h" #include "lwip/sockets.h" -#include "esp32/rom/md5_hash.h" +#include "esp_rom_md5.h" #include "mbedtls/base64.h" #include "esp_system.h" @@ -54,9 +54,9 @@ static int md5_printf(char *md, const char *fmt, ...) return ESP_FAIL; } - MD5Init(&md5_ctx); - MD5Update(&md5_ctx, buf, len); - MD5Final(digest, &md5_ctx); + esp_rom_md5_init(&md5_ctx); + esp_rom_md5_update(&md5_ctx, buf, len); + esp_rom_md5_final(digest, &md5_ctx); for (i = 0; i < 16; ++i) { sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]); diff --git a/components/esp_rom/esp32/ld/esp32.rom.api.ld b/components/esp_rom/esp32/ld/esp32.rom.api.ld index e560014095..e06fd7c3a0 100644 --- a/components/esp_rom/esp32/ld/esp32.rom.api.ld +++ b/components/esp_rom/esp32/ld/esp32.rom.api.ld @@ -26,3 +26,9 @@ PROVIDE ( esp_rom_uart_tx_wait_idle = uart_tx_wait_idle ); PROVIDE ( esp_rom_uart_rx_one_char = uart_rx_one_char ); PROVIDE ( esp_rom_uart_rx_string = UartRxString ); PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch ); + +/* wpa_supplicant re-implements the MD5 functions: MD5Init, MD5Update, MD5Final */ +/* so here we directly assign the symbols with the ROM API address */ +PROVIDE ( esp_rom_md5_init = 0x4005da7c ); +PROVIDE ( esp_rom_md5_update = 0x4005da9c ); +PROVIDE ( esp_rom_md5_final = 0x4005db1c ); diff --git a/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld b/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld index 00c84b2250..67d82796a0 100644 --- a/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld +++ b/components/esp_rom/esp32s2/ld/esp32s2.rom.api.ld @@ -25,3 +25,9 @@ PROVIDE ( esp_rom_uart_rx_one_char = uart_rx_one_char ); PROVIDE ( esp_rom_uart_rx_string = UartRxString ); PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch ); PROVIDE ( esp_rom_uart_usb_acm_init = Uart_Init_USB ); + +/* wpa_supplicant re-implements the MD5 functions: MD5Init, MD5Update, MD5Final */ +/* so here we directly assign the symbols with the ROM API address */ +PROVIDE ( esp_rom_md5_init = 0x4000526c ); +PROVIDE ( esp_rom_md5_update = 0x4000528c ); +PROVIDE ( esp_rom_md5_final = 0x4000530c ); diff --git a/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld b/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld index 28c742a691..002d4ab277 100644 --- a/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld +++ b/components/esp_rom/esp32s3/ld/esp32s3.rom.api.ld @@ -28,3 +28,9 @@ PROVIDE ( esp_rom_uart_rx_one_char = uart_rx_one_char ); PROVIDE ( esp_rom_uart_rx_string = UartRxString ); PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch ); PROVIDE ( esp_rom_uart_usb_acm_init = Uart_Init_USB ); + +/* wpa_supplicant re-implements the MD5 functions: MD5Init, MD5Update, MD5Final */ +/* so here we directly assign the symbols with the ROM API address */ +PROVIDE ( esp_rom_md5_init = 0x400376a0 ); +PROVIDE ( esp_rom_md5_update = 0x400376c0 ); +PROVIDE ( esp_rom_md5_final = 0x40037740 ); diff --git a/components/esp_rom/include/esp_rom_md5.h b/components/esp_rom/include/esp_rom_md5.h new file mode 100644 index 0000000000..5bb71f3eb9 --- /dev/null +++ b/components/esp_rom/include/esp_rom_md5.h @@ -0,0 +1,63 @@ +// Copyright 2010-2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * The MD5 functions calculate a 128-bit cryptographic digest for any number of input bytes. + */ + +/** + * @brief Type defined for MD5 context + * + */ +typedef struct MD5Context { + uint32_t buf[4]; + uint32_t bits[2]; + uint8_t in[64]; +} md5_context_t; + +/** + * @brief Initialize the MD5 context + * + * @param context Context object allocated by user + */ +void esp_rom_md5_init(md5_context_t *context); + +/** + * @brief Running MD5 algorithm over input data + * + * @param context MD5 context which has been initialized by `MD5Init` + * @param buf Input buffer + * @param len Buffer length + */ +void esp_rom_md5_update(md5_context_t *context, const uint8_t *buf, uint32_t len); + +/** + * @brief Extract the MD5 result, and erase the context + * + * @param digest Where to store the 128-bit digest value + * @param context MD5 context + */ +void esp_rom_md5_final(uint8_t digest[16], md5_context_t *context); + +#ifdef __cplusplus +} +#endif