Skip to content

Commit 080cc54

Browse files
committed
Share the implementation of hashlib across ports
.. many platforms can use mbedtls to implement hashlib. Compile-tested with pico-w. Tested on feather rp2040 dvi.
1 parent f5a03a8 commit 080cc54

File tree

16 files changed

+109
-10
lines changed

16 files changed

+109
-10
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
File renamed without changes.
File renamed without changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
27+
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
28+
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
32+
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
39+
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
47+
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 */
File renamed without changes.

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

py/circuitpy_defns.mk

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,6 @@ SRC_COMMON_HAL_ALL = \
474474
gnss/GNSS.c \
475475
gnss/PositionFix.c \
476476
gnss/SatelliteSystem.c \
477-
hashlib/__init__.c \
478-
hashlib/Hash.c \
479477
i2ctarget/I2CTarget.c \
480478
i2ctarget/__init__.c \
481479
memorymap/__init__.c \
@@ -761,6 +759,29 @@ SRC_MOD += lib/tjpgd/src/tjpgd.c
761759
$(BUILD)/lib/tjpgd/src/tjpgd.o: CFLAGS += -Wno-shadow -Wno-cast-align
762760
endif
763761

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

0 commit comments

Comments
 (0)