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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "common-hal/ssl/__init__.h"
2929
#include "shared-bindings/ssl/__init__.h"
3030
#include "shared-bindings/ssl/SSLContext.h"
31-
#include "mbedtls/crt_bundle.h"
31+
#include "lib/mbedtls_config/crt_bundle.h"
3232

3333
void common_hal_ssl_create_default_context(ssl_sslcontext_obj_t *self) {
3434
common_hal_ssl_sslcontext_construct(self);

ports/raspberrypi/mpconfigport.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE ?= 1
66
CIRCUITPY_WIFI_RADIO_SETTABLE_MAC_ADDRESS = 0
77

88
CIRCUITPY_ALARM ?= 1
9-
109
CIRCUITPY_RP2PIO ?= 1
1110
CIRCUITPY_NEOPIXEL_WRITE ?= $(CIRCUITPY_RP2PIO)
1211
CIRCUITPY_FLOPPYIO ?= 1
1312
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_DISPLAYIO)
1413
CIRCUITPY_FULL_BUILD ?= 1
1514
CIRCUITPY_AUDIOMP3 ?= 1
1615
CIRCUITPY_BITOPS ?= 1
16+
CIRCUITPY_HASHLIB ?= 1
17+
CIRCUITPY_HASHLIB_MBEDTLS ?= 1
1718
CIRCUITPY_IMAGECAPTURE ?= 1
1819
CIRCUITPY_MEMORYMAP ?= 1
1920
CIRCUITPY_PWMIO ?= 1
@@ -24,8 +25,9 @@ CIRCUITPY_SYNTHIO_MAX_CHANNELS = 12
2425
CIRCUITPY_USB_HOST ?= 1
2526

2627
# Things that need to be implemented.
27-
# Use PWM internally
2828
CIRCUITPY_FREQUENCYIO = 0
29+
30+
# Use PWM internally
2931
CIRCUITPY_I2CTARGET = 1
3032
CIRCUITPY_NVM = 1
3133
# Use PIO internally

ports/silabs/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ ifeq ($(MCU_SERIES),MG24)
3434
CIRCUITPY_USB = 0
3535
endif
3636

37+
CIRCUITPY_HASHLIB_MBEDTLS_ONLY = 0
38+
3739
CIRCUITPY_BUILD_EXTENSIONS ?= bin

py/circuitpy_defns.mk

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,6 @@ SRC_COMMON_HAL_ALL = \
477477
gnss/GNSS.c \
478478
gnss/PositionFix.c \
479479
gnss/SatelliteSystem.c \
480-
hashlib/__init__.c \
481-
hashlib/Hash.c \
482480
i2ctarget/I2CTarget.c \
483481
i2ctarget/__init__.c \
484482
memorymap/__init__.c \
@@ -765,6 +763,29 @@ SRC_MOD += lib/tjpgd/src/tjpgd.c
765763
$(BUILD)/lib/tjpgd/src/tjpgd.o: CFLAGS += -Wno-shadow -Wno-cast-align
766764
endif
767765

766+
ifeq ($(CIRCUITPY_HASHLIB_MBEDTLS_ONLY),1)
767+
SRC_MOD += $(addprefix lib/mbedtls/library/, \
768+
sha1.c \
769+
sha256.c \
770+
sha512.c \
771+
platform_util.c \
772+
)
773+
CFLAGS += \
774+
-isystem $(TOP)/lib/mbedtls/include \
775+
-DMBEDTLS_CONFIG_FILE='"$(TOP)/lib/mbedtls_config/mbedtls_config_hashlib.h"' \
776+
777+
endif
778+
779+
ifeq ($(CIRCUITPY_HASHLIB_MBEDTLS),1)
780+
SRC_SHARED_MODULE_ALL += \
781+
hashlib/Hash.c \
782+
hashlib/__init__.c
783+
else
784+
SRC_COMMON_HAL_ALL += \
785+
hashlib/Hash.c \
786+
hashlib/__init__.c
787+
endif
788+
768789
ifeq ($(CIRCUITPY_RGBMATRIX),1)
769790
SRC_MOD += $(addprefix lib/protomatter/src/, \
770791
core.c \

py/circuitpy_mpconfig.mk

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
enable-if-any=$(lastword $(sort $(1) 0))
3838
enable-if-all=$(firstword $(sort $(1) 1))
3939

40+
# Implement the 'not' function; When first argument argument of $(if) is non-empty,
41+
# the result is the 2nd arg, else it's the third arg. So if the value of $(1)
42+
# is exactly 1, the result of $(filter) is empty, and the result is 0. Otherwise,
43+
# the result is 1. You use this by $(call)ing it.
44+
enable-if-not=$(if $(filter 1,$(1)),0,1)
45+
4046
# To use any/all, you "$(call)" it, with the values to test after a comma.
4147
# Usually the values are other $(CIRCUITPY_foo) variables. The definition
4248
# of CIRCUITPY_AUDIOCORE and CIRCUITPY_AUDIOMP3 below are typical of how
@@ -292,6 +298,13 @@ CFLAGS += -DCIRCUITPY_GNSS=$(CIRCUITPY_GNSS)
292298
CIRCUITPY_HASHLIB ?= $(CIRCUITPY_WEB_WORKFLOW)
293299
CFLAGS += -DCIRCUITPY_HASHLIB=$(CIRCUITPY_HASHLIB)
294300

301+
CIRCUITPY_HASHLIB_MBEDTLS ?= $(CIRCUITPY_HASHLIB)
302+
CFLAGS += -DCIRCUITPY_HASHLIB_MBEDTLS=$(CIRCUITPY_HASHLIB_MBEDTLS)
303+
304+
# i.e., we need to include a subset of mbedtls only for hashlib's own needs
305+
CIRCUITPY_HASHLIB_MBEDTLS_ONLY ?= $(call enable-if-all,$(CIRCUITPY_HASHLIB_MBEDTLS) $(call enable-if-not,$(CIRCUITPY_SSL)))
306+
CFLAGS += -DCIRCUITPY_HASHLIB_MBEDTLS_ONLY=$(CIRCUITPY_HASHLIB_MBEDTLS_ONLY)
307+
295308
CIRCUITPY_I2CTARGET ?= $(CIRCUITPY_FULL_BUILD)
296309
CFLAGS += -DCIRCUITPY_I2CTARGET=$(CIRCUITPY_I2CTARGET)
297310

shared-bindings/hashlib/Hash.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929

3030
#include "py/obj.h"
3131

32+
#if CIRCUITPY_HASHLIB_MBEDTLS
33+
#include "shared-module/hashlib/Hash.h"
34+
#else
3235
#include "common-hal/hashlib/Hash.h"
36+
#endif
3337

3438
extern const mp_obj_type_t hashlib_hash_type;
3539

ports/raspberrypi/common-hal/hashlib/Hash.c renamed to shared-module/hashlib/Hash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "shared-bindings/hashlib/Hash.h"
28+
#include "shared-module/hashlib/__init__.h"
2829

2930
#include "mbedtls/ssl.h"
3031

ports/raspberrypi/common-hal/hashlib/__init__.c renamed to shared-module/hashlib/__init__.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "shared-bindings/hashlib/__init__.h"
28+
#include "shared-module/hashlib/__init__.h"
2829

2930
#include "mbedtls/ssl.h"
3031

ports/espressif/common-hal/hashlib/__init__.c renamed to shared-module/hashlib/__init__.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
7+
* Copyright (c) 2014 Paul Sokolovsky
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -24,17 +25,12 @@
2425
* THE SOFTWARE.
2526
*/
2627

27-
#include "shared-bindings/hashlib/__init__.h"
28+
#pragma once
2829

29-
#include "components/mbedtls/mbedtls/include/mbedtls/ssl.h"
30+
#include "mbedtls/version.h"
3031

31-
32-
bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
33-
if (strcmp(algorithm, "sha1") == 0) {
34-
self->hash_type = MBEDTLS_SSL_HASH_SHA1;
35-
mbedtls_sha1_init(&self->sha1);
36-
mbedtls_sha1_starts(&self->sha1);
37-
return true;
38-
}
39-
return false;
40-
}
32+
#if MBEDTLS_VERSION_NUMBER < 0x02070000 || MBEDTLS_VERSION_NUMBER >= 0x03000000
33+
#define mbedtls_sha1_starts_ret mbedtls_sha1_starts
34+
#define mbedtls_sha1_update_ret mbedtls_sha1_update
35+
#define mbedtls_sha1_finish_ret mbedtls_sha1_finish
36+
#endif

0 commit comments

Comments
 (0)