Skip to content
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
3 changes: 3 additions & 0 deletions ports/nordic/boards/clue_nrf52840_express/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ MCU_CHIP = nrf52840

QSPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICES = "GD25Q16C, W25Q16JVxQ"

CIRCUITPY_HASHLIB = 1
CIRCUITPY_HASHLIB_SHA256 = 1
3 changes: 3 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ CFLAGS += -DCIRCUITPY_HASHLIB_MBEDTLS=$(CIRCUITPY_HASHLIB_MBEDTLS)
CIRCUITPY_HASHLIB_MBEDTLS_ONLY ?= $(call enable-if-all,$(CIRCUITPY_HASHLIB_MBEDTLS) $(call enable-if-not,$(CIRCUITPY_SSL)))
CFLAGS += -DCIRCUITPY_HASHLIB_MBEDTLS_ONLY=$(CIRCUITPY_HASHLIB_MBEDTLS_ONLY)

CIRCUITPY_HASHLIB_SHA256 ?= 0
CFLAGS += -DCIRCUITPY_HASHLIB_SHA256=$(CIRCUITPY_HASHLIB_SHA256)

CIRCUITPY_I2CTARGET ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_I2CTARGET=$(CIRCUITPY_I2CTARGET)

Expand Down
20 changes: 20 additions & 0 deletions shared-module/hashlib/Hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *dat
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
return;
}
#if CIRCUITPY_HASHLIB_SHA256
else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
mbedtls_sha256_update_ret(&self->sha256, data, datalen);
return;
}
#endif
}

void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
Expand All @@ -28,11 +34,25 @@ void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, siz
mbedtls_sha1_finish_ret(&self->sha1, data);
mbedtls_sha1_clone(&self->sha1, &copy);
}
#if CIRCUITPY_HASHLIB_SHA256
else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
mbedtls_sha256_context copy;
mbedtls_sha256_clone(&copy, &self->sha256);
mbedtls_sha256_finish_ret(&self->sha256, data);
mbedtls_sha256_clone(&self->sha256, &copy);
}
#endif
}

size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
return 20;
}
#if CIRCUITPY_HASHLIB_SHA256
else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
return 32;
}
#endif

return 0;
}
6 changes: 6 additions & 0 deletions shared-module/hashlib/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
#pragma once

#include "mbedtls/sha1.h"
#if CIRCUITPY_HASHLIB_SHA256
#include "mbedtls/sha256.h"
#endif

typedef struct {
mp_obj_base_t base;
union {
mbedtls_sha1_context sha1;
#if CIRCUITPY_HASHLIB_SHA256
mbedtls_sha256_context sha256;
#endif
};
// Of MBEDTLS_SSL_HASH_*
uint8_t hash_type;
Expand Down
8 changes: 8 additions & 0 deletions shared-module/hashlib/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
mbedtls_sha1_starts_ret(&self->sha1);
return true;
}
#if CIRCUITPY_HASHLIB_SHA256
else if (strcmp(algorithm, "sha256") == 0) {
self->hash_type = MBEDTLS_SSL_HASH_SHA256;
mbedtls_sha256_init(&self->sha256);
mbedtls_sha256_starts_ret(&self->sha256, 0);
return true;
}
#endif
return false;
}
7 changes: 7 additions & 0 deletions shared-module/hashlib/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
#define mbedtls_sha1_starts_ret mbedtls_sha1_starts
#define mbedtls_sha1_update_ret mbedtls_sha1_update
#define mbedtls_sha1_finish_ret mbedtls_sha1_finish

#if CIRCUITPY_HASHLIB_SHA256
#define mbedtls_sha256_starts_ret mbedtls_sha256_starts
#define mbedtls_sha256_update_ret mbedtls_sha256_update
#define mbedtls_sha256_finish_ret mbedtls_sha256_finish
#endif

#endif