Skip to content

Commit 3b3fe44

Browse files
committed
implement hashlib for picow
1 parent bb3e04a commit 3b3fe44

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
lines changed

ports/raspberrypi/boards/raspberry_pi_pico_w/mpconfigboard.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CIRCUITPY__EVE = 1
1313
CIRCUITPY_CYW43 = 1
1414
CIRCUITPY_SSL = 1
1515
CIRCUITPY_SSL_MBEDTLS = 1
16-
CIRCUITPY_HASHLIB = 0
16+
CIRCUITPY_HASHLIB = 1
1717
CIRCUITPY_WEB_WORKFLOW = 0
1818
CIRCUITPY_MDNS = 0
1919
CIRCUITPY_SOCKETPOOL = 1
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
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+
27+
#include "shared-bindings/hashlib/Hash.h"
28+
29+
#include "mbedtls/ssl.h"
30+
31+
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
32+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
33+
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
34+
return;
35+
}
36+
}
37+
38+
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
39+
if (datalen < common_hal_hashlib_hash_get_digest_size(self)) {
40+
return;
41+
}
42+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
43+
// We copy the sha1 state so we can continue to update if needed or get
44+
// the digest a second time.
45+
mbedtls_sha1_context copy;
46+
mbedtls_sha1_clone(&copy, &self->sha1);
47+
mbedtls_sha1_finish_ret(&self->sha1, data);
48+
mbedtls_sha1_clone(&self->sha1, &copy);
49+
}
50+
}
51+
52+
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
53+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
54+
return 20;
55+
}
56+
return 0;
57+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Jeff Epler for Adafruit Industries
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+
27+
#pragma once
28+
29+
#include "mbedtls/sha1.h"
30+
31+
typedef struct {
32+
mp_obj_base_t base;
33+
union {
34+
mbedtls_sha1_context sha1;
35+
};
36+
// Of MBEDTLS_SSL_HASH_*
37+
uint8_t hash_type;
38+
} hashlib_hash_obj_t;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
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+
27+
#include "shared-bindings/hashlib/__init__.h"
28+
29+
#include "mbedtls/ssl.h"
30+
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_ret(&self->sha1);
37+
return true;
38+
}
39+
return false;
40+
}

ports/raspberrypi/common-hal/hashlib/__init__.h

Whitespace-only changes.

0 commit comments

Comments
 (0)