esp_rom: extract common MD5 hash apis into esp_rom_md5.h

pull/5688/head
morris 2020-07-21 17:01:28 +08:00
rodzic 8739282a1d
commit ab0537c079
7 zmienionych plików z 94 dodań i 13 usunięć

Wyświetl plik

@ -15,7 +15,7 @@
#include "esp_flash_partitions.h" #include "esp_flash_partitions.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp32/rom/spi_flash.h" #include "esp32/rom/spi_flash.h"
#include "esp32/rom/md5_hash.h" #include "esp_rom_md5.h"
static const char *TAG = "flash_parts"; 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; struct MD5Context context;
unsigned char digest[16]; unsigned char digest[16];
MD5Init(&context); esp_rom_md5_init(&context);
MD5Update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t)); esp_rom_md5_update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t));
MD5Final(digest, &context); esp_rom_md5_final(digest, &context);
unsigned char *md5sum = ((unsigned char *) part) + 16; // skip the 2B magic number and the 14B fillup bytes unsigned char *md5sum = ((unsigned char *) part) + 16; // skip the 2B magic number and the 14B fillup bytes

Wyświetl plik

@ -13,7 +13,7 @@
#include "lwip/netdb.h" #include "lwip/netdb.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "ping/ping_sock.h" #include "ping/ping_sock.h"
#include "esp32/rom/md5_hash.h" #include "esp_rom_md5.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#if SOC_EMAC_SUPPORTED #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) #define ETH_PING_END_TIMEOUT_MS (ETH_PING_DURATION_MS * 2)
// compute md5 of download file // compute md5 of download file
static struct MD5Context md5_context; static md5_context_t md5_context;
static uint8_t digest[16]; static uint8_t digest[16];
/** Event handler for Ethernet events */ /** 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"); ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
break; break;
case HTTP_EVENT_ON_DATA: 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; break;
case HTTP_EVENT_ON_FINISH: case HTTP_EVENT_ON_FINISH:
ESP_LOGI(TAG, "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) static void eth_download_task(void *param)
{ {
EventGroupHandle_t eth_event_group = (EventGroupHandle_t)param; EventGroupHandle_t eth_event_group = (EventGroupHandle_t)param;
MD5Init(&md5_context); esp_rom_md5_init(&md5_context);
esp_http_client_config_t config = { esp_http_client_config_t config = {
.url = "https://dl.espressif.com/dl/misc/2MB.bin", .url = "https://dl.espressif.com/dl/misc/2MB.bin",
.event_handler = http_event_handle, .event_handler = http_event_handle,
@ -436,7 +436,7 @@ static void eth_download_task(void *param)
TEST_ASSERT_NOT_NULL(client); TEST_ASSERT_NOT_NULL(client);
TEST_ESP_OK(esp_http_client_perform(client)); TEST_ESP_OK(esp_http_client_perform(client));
TEST_ESP_OK(esp_http_client_cleanup(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); xEventGroupSetBits(eth_event_group, ETH_DOWNLOAD_END_BIT);
vTaskDelete(NULL); vTaskDelete(NULL);
} }

Wyświetl plik

@ -19,7 +19,7 @@
#include "esp_netif.h" #include "esp_netif.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "esp32/rom/md5_hash.h" #include "esp_rom_md5.h"
#include "mbedtls/base64.h" #include "mbedtls/base64.h"
#include "esp_system.h" #include "esp_system.h"
@ -54,9 +54,9 @@ static int md5_printf(char *md, const char *fmt, ...)
return ESP_FAIL; return ESP_FAIL;
} }
MD5Init(&md5_ctx); esp_rom_md5_init(&md5_ctx);
MD5Update(&md5_ctx, buf, len); esp_rom_md5_update(&md5_ctx, buf, len);
MD5Final(digest, &md5_ctx); esp_rom_md5_final(digest, &md5_ctx);
for (i = 0; i < 16; ++i) { for (i = 0; i < 16; ++i) {
sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]); sprintf(&md[i * 2], "%02x", (unsigned int)digest[i]);

Wyświetl plik

@ -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_one_char = uart_rx_one_char );
PROVIDE ( esp_rom_uart_rx_string = UartRxString ); PROVIDE ( esp_rom_uart_rx_string = UartRxString );
PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch ); 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 );

Wyświetl plik

@ -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_rx_string = UartRxString );
PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch ); PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch );
PROVIDE ( esp_rom_uart_usb_acm_init = Uart_Init_USB ); 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 );

Wyświetl plik

@ -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_rx_string = UartRxString );
PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch ); PROVIDE ( esp_rom_uart_set_as_console = uart_tx_switch );
PROVIDE ( esp_rom_uart_usb_acm_init = Uart_Init_USB ); 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 );

Wyświetl plik

@ -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 <stdint.h>
/**
* 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