Skip to content

feat(hash): Add hashing library and new algorithms #11676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ set(CORE_SRCS
cores/esp32/freertos_stats.cpp
cores/esp32/FunctionalInterrupt.cpp
cores/esp32/HardwareSerial.cpp
cores/esp32/HEXBuilder.cpp
cores/esp32/HashBuilder.cpp
cores/esp32/HexBuilder.cpp
cores/esp32/IPAddress.cpp
cores/esp32/libb64/cdecode.c
cores/esp32/libb64/cencode.c
cores/esp32/MacAddress.cpp
cores/esp32/main.cpp
cores/esp32/MD5Builder.cpp
cores/esp32/Print.cpp
cores/esp32/SHA1Builder.cpp
cores/esp32/stdlib_noniso.c
cores/esp32/Stream.cpp
cores/esp32/StreamString.cpp
Expand Down Expand Up @@ -93,6 +93,7 @@ set(ARDUINO_ALL_LIBRARIES
Ethernet
FFat
FS
Hash
HTTPClient
HTTPUpdate
Insights
Expand Down Expand Up @@ -154,6 +155,13 @@ set(ARDUINO_LIBRARY_FS_SRCS
libraries/FS/src/FS.cpp
libraries/FS/src/vfs_api.cpp)

set(ARDUINO_LIBRARY_Hash_SRCS
libraries/Hash/src/SHA1Builder.cpp
libraries/Hash/src/SHA2Builder.cpp
libraries/Hash/src/SHA3Builder.cpp
libraries/Hash/src/PBKDF2_HMACBuilder.cpp
)

set(ARDUINO_LIBRARY_HTTPClient_SRCS libraries/HTTPClient/src/HTTPClient.cpp)

set(ARDUINO_LIBRARY_HTTPUpdate_SRCS libraries/HTTPUpdate/src/HTTPUpdate.cpp)
Expand Down
38 changes: 38 additions & 0 deletions cores/esp32/HashBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 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.

#include "HashBuilder.h"

void HashBuilder::add(const char *data) {
add((const uint8_t *)data, strlen(data));
}

void HashBuilder::add(String data) {
add(data.c_str());
}

void HashBuilder::addHexString(const char *data) {
size_t len = strlen(data);
uint8_t *tmp = (uint8_t *)malloc(len / 2);
if (tmp == NULL) {
return;
}
hex2bytes(tmp, len / 2, data);
add(tmp, len / 2);
free(tmp);
}

void HashBuilder::addHexString(String data) {
addHexString(data.c_str());
}
23 changes: 10 additions & 13 deletions cores/esp32/HashBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,28 @@
#include <WString.h>
#include <Stream.h>

#include "HEXBuilder.h"
#include "HexBuilder.h"

class HashBuilder : public HEXBuilder {
// Base class for hash builders

class HashBuilder : public HexBuilder {
public:
virtual ~HashBuilder() {}
virtual void begin() = 0;

virtual void add(const uint8_t *data, size_t len) = 0;
virtual void add(const char *data) {
add((const uint8_t *)data, strlen(data));
}
virtual void add(String data) {
add(data.c_str());
}

virtual void addHexString(const char *data) = 0;
virtual void addHexString(String data) {
addHexString(data.c_str());
}
void add(const char *data);
void add(String data);

void addHexString(const char *data);
void addHexString(String data);

virtual bool addStream(Stream &stream, const size_t maxLen) = 0;
virtual void calculate() = 0;
virtual void getBytes(uint8_t *output) = 0;
virtual void getChars(char *output) = 0;
virtual String toString() = 0;
virtual size_t getHashSize() const = 0;
};

#endif
11 changes: 5 additions & 6 deletions cores/esp32/HEXBuilder.cpp → cores/esp32/HexBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <HEXBuilder.h>
#include "HexBuilder.h"

static uint8_t hex_char_to_byte(uint8_t c) {
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa))
Expand All @@ -27,11 +26,11 @@ static uint8_t hex_char_to_byte(uint8_t c) {
: 0x10; // unknown char is 16
}

size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, String &in) {
size_t HexBuilder::hex2bytes(unsigned char *out, size_t maxlen, String &in) {
return hex2bytes(out, maxlen, in.c_str());
}

size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, const char *in) {
size_t HexBuilder::hex2bytes(unsigned char *out, size_t maxlen, const char *in) {
size_t len = 0;
for (; *in; in++) {
uint8_t c = hex_char_to_byte(*in);
Expand All @@ -54,7 +53,7 @@ size_t HEXBuilder::hex2bytes(unsigned char *out, size_t maxlen, const char *in)
return (len + 1) / 2;
}

size_t HEXBuilder::bytes2hex(char *out, size_t maxlen, const unsigned char *in, size_t len) {
size_t HexBuilder::bytes2hex(char *out, size_t maxlen, const unsigned char *in, size_t len) {
for (size_t i = 0; i < len; i++) {
if (i * 2 + 1 < maxlen) {
sprintf(out + (i * 2), "%02x", in[i]);
Expand All @@ -63,7 +62,7 @@ size_t HEXBuilder::bytes2hex(char *out, size_t maxlen, const unsigned char *in,
return len * 2 + 1;
}

String HEXBuilder::bytes2hex(const unsigned char *in, size_t len) {
String HexBuilder::bytes2hex(const unsigned char *in, size_t len) {
size_t maxlen = len * 2 + 1;
char *out = (char *)malloc(maxlen);
if (!out) {
Expand Down
8 changes: 5 additions & 3 deletions cores/esp32/HEXBuilder.h → cores/esp32/HexBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef HEXBuilder_h
#define HEXBuilder_h
#ifndef HexBuilder_h
#define HexBuilder_h

#include <WString.h>
#include <Stream.h>

class HEXBuilder {
// Basic hex/byte conversion class to be used by hash builders

class HexBuilder {
public:
static size_t hex2bytes(unsigned char *out, size_t maxlen, String &in);
static size_t hex2bytes(unsigned char *out, size_t maxlen, const char *in);
Expand Down
16 changes: 2 additions & 14 deletions cores/esp32/MD5Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <HEXBuilder.h>
#include <MD5Builder.h>
#include "HexBuilder.h"
#include "MD5Builder.h"

void MD5Builder::begin(void) {
memset(_buf, 0x00, ESP_ROM_MD5_DIGEST_LEN);
Expand All @@ -30,17 +29,6 @@ void MD5Builder::add(const uint8_t *data, size_t len) {
esp_rom_md5_update(&_ctx, data, len);
}

void MD5Builder::addHexString(const char *data) {
size_t len = strlen(data);
uint8_t *tmp = (uint8_t *)malloc(len / 2);
if (tmp == NULL) {
return;
}
hex2bytes(tmp, len / 2, data);
add(tmp, len / 2);
free(tmp);
}

bool MD5Builder::addStream(Stream &stream, const size_t maxLen) {
const int buf_size = 512;
int maxLengthLeft = maxLen;
Expand Down
9 changes: 3 additions & 6 deletions cores/esp32/MD5Builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,16 @@ class MD5Builder : public HashBuilder {
uint8_t _buf[ESP_ROM_MD5_DIGEST_LEN];

public:
void begin(void) override;

using HashBuilder::add;
void add(const uint8_t *data, size_t len) override;

using HashBuilder::addHexString;
void addHexString(const char *data) override;

void begin(void) override;
void add(const uint8_t *data, size_t len) override;
bool addStream(Stream &stream, const size_t maxLen) override;
void calculate(void) override;
void getBytes(uint8_t *output) override;
void getChars(char *output) override;
String toString(void) override;
size_t getHashSize() const override { return ESP_ROM_MD5_DIGEST_LEN; }
};

#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#include <HEXBuilder.h>
/*
Usage example for the HexBuilder class.

This example shows how to convert a HEX string to a binary buffer and vice versa.
*/

#include <HexBuilder.h>

void setup() {
Serial.begin(115200);
Expand All @@ -11,7 +17,7 @@ void setup() {
const char *hexin = "48656c6c6f20576f726c6400"; // As the string above is \0 terminated too

unsigned char buff[256];
size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), hexin);
size_t len = HexBuilder::hex2bytes(buff, sizeof(buff), hexin);

if (len != 1 + strlen(out)) {
Serial.println("Odd - length 1 is wrong");
Expand All @@ -30,7 +36,7 @@ void setup() {
const char hello[] = "Hello World";

unsigned char buff[256];
size_t len = HEXBuilder::hex2bytes(buff, sizeof(buff), helloHEX);
size_t len = HexBuilder::hex2bytes(buff, sizeof(buff), helloHEX);

if (len != strlen(hello)) {
Serial.println("Odd - length 2 is wrong");
Expand All @@ -45,7 +51,7 @@ void setup() {
const unsigned char helloBytes[] = {0x48, 0x56, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};
String helloHEX = "48566c6c6f20576f726c64";

String out = HEXBuilder::bytes2hex(helloBytes, sizeof(helloBytes));
String out = HexBuilder::bytes2hex(helloBytes, sizeof(helloBytes));
if (out.length() != 2 * sizeof(helloBytes)) {
Serial.println("Odd - length 3 is wrong");
}
Expand All @@ -61,7 +67,7 @@ void setup() {
const char helloHex[] = "6c6c6f20576f726c64";

char buff[256];
size_t len = HEXBuilder::bytes2hex(buff, sizeof(buff), helloBytes, sizeof(helloBytes));
size_t len = HexBuilder::bytes2hex(buff, sizeof(buff), helloBytes, sizeof(helloBytes));
if (len != 1 + 2 * sizeof(helloBytes)) {
Serial.println("Odd - length 4 is wrong");
}
Expand Down
Loading
Loading