Merge branch 'refactor/common_rom_md5_apis' into 'master'

esp_rom: extract common MD5 hash apis into esp_rom_md5.h

See merge request espressif/esp-idf!9254
This commit is contained in:
Angus Gratton 2020-07-22 12:39:37 +08:00
commit 862e2da51f
7 changed files with 94 additions and 13 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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]);

View File

@ -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 );

View File

@ -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 );

View File

@ -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 );

View File

@ -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