Skip to content

Commit 648aae0

Browse files
authored
Merge pull request #10740 from samblenny/hashlib-sha1-sha256
Implement hashlib.new('sha256') and enable it for CLUE
2 parents 5f25989 + 39332eb commit 648aae0

File tree

6 files changed

+25
-1
lines changed

6 files changed

+25
-1
lines changed

ports/nordic/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ CIRCUITPY_MEMORYMAP ?= 1
6363
CIRCUITPY_RGBMATRIX ?= 1
6464
CIRCUITPY_FRAMEBUFFERIO ?= 1
6565

66+
CIRCUITPY_HASHLIB ?= 1
67+
6668
CIRCUITPY_COUNTIO ?= 1
6769
CIRCUITPY_WATCHDOG ?= 1
6870

shared-bindings/hashlib/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//|
2121
//| def new(name: str, data: bytes = b"") -> hashlib.Hash:
2222
//| """Returns a Hash object setup for the named algorithm. Raises ValueError when the named
23-
//| algorithm is unsupported.
23+
//| algorithm is unsupported. Supported algorithms for ``name`` are ``'sha1`` and ``'sha256'``.
2424
//|
2525
//| :return: a hash object for the given algorithm
2626
//| :rtype: hashlib.Hash"""

shared-module/hashlib/Hash.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *dat
1313
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
1414
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
1515
return;
16+
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
17+
mbedtls_sha256_update_ret(&self->sha256, data, datalen);
18+
return;
1619
}
1720
}
1821

@@ -27,12 +30,19 @@ void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, siz
2730
mbedtls_sha1_clone(&copy, &self->sha1);
2831
mbedtls_sha1_finish_ret(&self->sha1, data);
2932
mbedtls_sha1_clone(&self->sha1, &copy);
33+
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
34+
mbedtls_sha256_context copy;
35+
mbedtls_sha256_clone(&copy, &self->sha256);
36+
mbedtls_sha256_finish_ret(&self->sha256, data);
37+
mbedtls_sha256_clone(&self->sha256, &copy);
3038
}
3139
}
3240

3341
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
3442
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
3543
return 20;
44+
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
45+
return 32;
3646
}
3747
return 0;
3848
}

shared-module/hashlib/Hash.h

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

99
#include "mbedtls/sha1.h"
10+
#include "mbedtls/sha256.h"
1011

1112
typedef struct {
1213
mp_obj_base_t base;
1314
union {
1415
mbedtls_sha1_context sha1;
16+
mbedtls_sha256_context sha256;
1517
};
1618
// Of MBEDTLS_SSL_HASH_*
1719
uint8_t hash_type;

shared-module/hashlib/__init__.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
1616
mbedtls_sha1_init(&self->sha1);
1717
mbedtls_sha1_starts_ret(&self->sha1);
1818
return true;
19+
} else if (strcmp(algorithm, "sha256") == 0) {
20+
self->hash_type = MBEDTLS_SSL_HASH_SHA256;
21+
mbedtls_sha256_init(&self->sha256);
22+
mbedtls_sha256_starts_ret(&self->sha256, 0);
23+
return true;
1924
}
2025
return false;
2126
}

shared-module/hashlib/__init__.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
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+
#define mbedtls_sha256_starts_ret mbedtls_sha256_starts
18+
#define mbedtls_sha256_update_ret mbedtls_sha256_update
19+
#define mbedtls_sha256_finish_ret mbedtls_sha256_finish
20+
1621
#endif

0 commit comments

Comments
 (0)