Skip to content

Commit 3453683

Browse files
committed
implement hashlib.new('sha256') support
1 parent 108860a commit 3453683

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ CFLAGS += -DCIRCUITPY_HASHLIB_MBEDTLS=$(CIRCUITPY_HASHLIB_MBEDTLS)
343343
CIRCUITPY_HASHLIB_MBEDTLS_ONLY ?= $(call enable-if-all,$(CIRCUITPY_HASHLIB_MBEDTLS) $(call enable-if-not,$(CIRCUITPY_SSL)))
344344
CFLAGS += -DCIRCUITPY_HASHLIB_MBEDTLS_ONLY=$(CIRCUITPY_HASHLIB_MBEDTLS_ONLY)
345345

346+
CIRCUITPY_HASHLIB_SHA256 ?= 0
347+
CFLAGS += -DCIRCUITPY_HASHLIB_SHA256=$(CIRCUITPY_HASHLIB_SHA256)
348+
346349
CIRCUITPY_I2CTARGET ?= $(CIRCUITPY_FULL_BUILD)
347350
CFLAGS += -DCIRCUITPY_I2CTARGET=$(CIRCUITPY_I2CTARGET)
348351

shared-module/hashlib/Hash.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *dat
1414
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
1515
return;
1616
}
17+
#if CIRCUITPY_HASHLIB_SHA256
18+
else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
19+
mbedtls_sha256_update_ret(&self->sha256, data, datalen);
20+
return;
21+
}
22+
#endif
1723
}
1824

1925
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
@@ -28,11 +34,25 @@ void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, siz
2834
mbedtls_sha1_finish_ret(&self->sha1, data);
2935
mbedtls_sha1_clone(&self->sha1, &copy);
3036
}
37+
#if CIRCUITPY_HASHLIB_SHA256
38+
else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
39+
mbedtls_sha256_context copy;
40+
mbedtls_sha256_clone(&copy, &self->sha256);
41+
mbedtls_sha256_finish_ret(&self->sha256, data);
42+
mbedtls_sha256_clone(&self->sha256, &copy);
43+
}
44+
#endif
3145
}
3246

3347
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
3448
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
3549
return 20;
3650
}
51+
#if CIRCUITPY_HASHLIB_SHA256
52+
else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
53+
return 32;
54+
}
55+
#endif
56+
3757
return 0;
3858
}

shared-module/hashlib/Hash.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
#pragma once
88

99
#include "mbedtls/sha1.h"
10+
#if CIRCUITPY_HASHLIB_SHA256
11+
#include "mbedtls/sha256.h"
12+
#endif
1013

1114
typedef struct {
1215
mp_obj_base_t base;
1316
union {
1417
mbedtls_sha1_context sha1;
18+
#if CIRCUITPY_HASHLIB_SHA256
19+
mbedtls_sha256_context sha256;
20+
#endif
1521
};
1622
// Of MBEDTLS_SSL_HASH_*
1723
uint8_t hash_type;

shared-module/hashlib/__init__.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,13 @@ bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
1717
mbedtls_sha1_starts_ret(&self->sha1);
1818
return true;
1919
}
20+
#if CIRCUITPY_HASHLIB_SHA256
21+
else if (strcmp(algorithm, "sha256") == 0) {
22+
self->hash_type = MBEDTLS_SSL_HASH_SHA256;
23+
mbedtls_sha256_init(&self->sha256);
24+
mbedtls_sha256_starts_ret(&self->sha256, 0);
25+
return true;
26+
}
27+
#endif
2028
return false;
2129
}

shared-module/hashlib/__init__.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313
#define mbedtls_sha1_starts_ret mbedtls_sha1_starts
1414
#define mbedtls_sha1_update_ret mbedtls_sha1_update
1515
#define mbedtls_sha1_finish_ret mbedtls_sha1_finish
16+
17+
#if CIRCUITPY_HASHLIB_SHA256
18+
#define mbedtls_sha256_starts_ret mbedtls_sha256_starts
19+
#define mbedtls_sha256_update_ret mbedtls_sha256_update
20+
#define mbedtls_sha256_finish_ret mbedtls_sha256_finish
21+
#endif
22+
1623
#endif

0 commit comments

Comments
 (0)