Skip to content

Commit 1bc616c

Browse files
authored
Merge pull request #8749 from jepler/mbedtls-hashlib
Share the implementation of hashlib across ports
2 parents ae1b3e1 + d86d699 commit 1bc616c

File tree

20 files changed

+94
-92
lines changed

20 files changed

+94
-92
lines changed

ports/raspberrypi/mbedtls/crt_bundle.c renamed to lib/mbedtls_config/crt_bundle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
#include "py/runtime.h"
2121
#include "py/mperrno.h"
22-
#include "mbedtls/x509_crt.h"
23-
#include "mbedtls/crt_bundle.h"
22+
#include "lib/mbedtls/include/mbedtls/x509_crt.h"
23+
#include "lib/mbedtls_config/crt_bundle.h"
2424

2525
#define BUNDLE_HEADER_OFFSET 2
2626
#define CRT_HEADER_OFFSET 4

ports/espressif/common-hal/hashlib/Hash.h renamed to lib/mbedtls_config/mbedtls_config_hashlib.h

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2018-2019 Damien P. George
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -23,19 +23,37 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26+
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
27+
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
2628

27-
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
28-
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
29+
// If you want to debug MBEDTLS uncomment the following and
30+
// Pass 3 to mbedtls_debug_set_threshold in socket_new
31+
// #define MBEDTLS_DEBUG_C
2932

30-
#include "components/mbedtls/mbedtls/include/mbedtls/sha1.h"
33+
// Set mbedtls configuration
34+
#define MBEDTLS_PLATFORM_C
35+
#define MBEDTLS_PLATFORM_MEMORY
36+
#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
37+
#define MBEDTLS_DEPRECATED_REMOVED
38+
#define MBEDTLS_ENTROPY_HARDWARE_ALT
3139

32-
typedef struct {
33-
mp_obj_base_t base;
34-
union {
35-
mbedtls_sha1_context sha1;
36-
};
37-
// Of MBEDTLS_SSL_HASH_*
38-
uint8_t hash_type;
39-
} hashlib_hash_obj_t;
40+
// Enable mbedtls modules
41+
#define MBEDTLS_MD_C
42+
#define MBEDTLS_MD5_C
43+
#define MBEDTLS_SHA1_C
44+
#define MBEDTLS_SHA256_C
45+
#define MBEDTLS_SHA512_C
46+
#undef MBEDTLS_HAVE_TIME_DATE
4047

41-
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
48+
// Memory allocation hooks
49+
#include <stdlib.h>
50+
#include <stdio.h>
51+
void *m_tracked_calloc(size_t nmemb, size_t size);
52+
void m_tracked_free(void *ptr);
53+
#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc
54+
#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free
55+
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf
56+
57+
#include "mbedtls/check_config.h"
58+
59+
#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */

ports/espressif/common-hal/hashlib/Hash.c

Lines changed: 0 additions & 57 deletions
This file was deleted.

ports/espressif/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ CIRCUITPY_PARALLELDISPLAY = 0
9999
CIRCUITPY_RGBMATRIX = 0
100100
CIRCUITPY_TOUCHIO ?= 1
101101
CIRCUITPY_TOUCHIO_USE_NATIVE = 0
102+
CIRCUITPY_HASHLIB_MBEDTLS_ONLY = 0
102103
# Features
103104
CIRCUITPY_USB = 0
104105
CIRCUITPY_ESP_USB_SERIAL_JTAG ?= 1

ports/raspberrypi/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ SRC_MBEDTLS := $(addprefix lib/mbedtls/library/, \
354354
x509write_csr.c \
355355
xtea.c \
356356
)
357-
SRC_C += $(SRC_MBEDTLS) mbedtls/mbedtls_port.c mbedtls/crt_bundle.c
357+
SRC_C += $(SRC_MBEDTLS) $(TOP)/lib/mbedtls_config/mbedtls_port.c $(TOP)/lib/mbedtls_config/crt_bundle.c
358358
CFLAGS += \
359359
-isystem $(TOP)/lib/mbedtls/include \
360-
-DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' \
360+
-DMBEDTLS_CONFIG_FILE='"$(TOP)/lib/mbedtls_config/mbedtls_config.h"' \
361361

362362
$(BUILD)/x509_crt_bundle.S: $(TOP)/lib/certificates/data/roots.pem $(TOP)/tools/gen_crt_bundle.py
363363
$(Q)$(PYTHON) $(TOP)/tools/gen_crt_bundle.py -i $< -o $@ --asm

ports/raspberrypi/common-hal/hashlib/__init__.h

Whitespace-only changes.

ports/raspberrypi/common-hal/ssl/SSLContext.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "py/runtime.h"
3232
#include "py/stream.h"
3333

34-
#include "mbedtls/crt_bundle.h"
34+
#include "lib/mbedtls_config/crt_bundle.h"
3535

3636
void common_hal_ssl_sslcontext_construct(ssl_sslcontext_obj_t *self) {
3737
common_hal_ssl_sslcontext_set_default_verify_paths(self);

0 commit comments

Comments
 (0)