Skip to content

Commit 7589e53

Browse files
committed
WIP websocket accept and hashlib
1 parent 2ae791a commit 7589e53

File tree

14 files changed

+515
-1
lines changed

14 files changed

+515
-1
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,10 @@ msgstr ""
21992199
msgid "Unsupported format"
22002200
msgstr ""
22012201

2202+
#: shared-bindings/hashlib/__init__.c
2203+
msgid "Unsupported hash algorithm"
2204+
msgstr ""
2205+
22022206
#: ports/espressif/common-hal/dualbank/__init__.c
22032207
msgid "Update Failed"
22042208
msgstr ""
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 "components/mbedtls/mbedtls/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+
mbedtls_sha1_finish_ret(&self->sha1, data);
44+
}
45+
}
46+
47+
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
48+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
49+
return 20;
50+
}
51+
return 0;
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
28+
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
29+
30+
#include "components/mbedtls/mbedtls/include/mbedtls/sha1.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
union {
35+
mbedtls_sha1_context sha1;
36+
};
37+
// Of MBEDTLS_SSL_HASH_*
38+
uint8_t hash_type;
39+
} hashlib_hash_obj_t;
40+
41+
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_HASHLIB_HASH_H
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 "components/mbedtls/mbedtls/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/espressif/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CIRCUITPY_COUNTIO ?= 1
1919
CIRCUITPY_DUALBANK ?= 1
2020
CIRCUITPY_FRAMEBUFFERIO ?= 1
2121
CIRCUITPY_FREQUENCYIO ?= 1
22+
CIRCUITPY_HASHLIB ?= 1
2223
CIRCUITPY_IMAGECAPTURE ?= 1
2324
CIRCUITPY_I2CPERIPHERAL ?= 1
2425
CIRCUITPY_RGBMATRIX ?= 1

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ endif
207207
ifeq ($(CIRCUITPY_GNSS),1)
208208
SRC_PATTERNS += gnss/%
209209
endif
210+
ifeq ($(CIRCUITPY_HASHLIB),1)
211+
SRC_PATTERNS += hashlib/%
212+
endif
210213
ifeq ($(CIRCUITPY_I2CPERIPHERAL),1)
211214
SRC_PATTERNS += i2cperipheral/%
212215
endif
@@ -419,6 +422,8 @@ SRC_COMMON_HAL_ALL = \
419422
gnss/GNSS.c \
420423
gnss/PositionFix.c \
421424
gnss/SatelliteSystem.c \
425+
hashlib/__init__.c \
426+
hashlib/Hash.c \
422427
i2cperipheral/I2CPeripheral.c \
423428
i2cperipheral/__init__.c \
424429
microcontroller/Pin.c \

shared-bindings/hashlib/Hash.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 "shared-bindings/util.h"
30+
31+
// #include "shared/runtime/buffer_helper.h"
32+
// #include "shared/runtime/interrupt_char.h"
33+
34+
// #include "py/mperrno.h"
35+
// #include "py/mphal.h"
36+
#include "py/obj.h"
37+
#include "py/objproperty.h"
38+
#include "py/objstr.h"
39+
#include "py/runtime.h"
40+
41+
//| class Hash:
42+
//| """In progress hash algorithm. This object is always created by a `hashlib.new()`. It has no
43+
//| user-visible constructor."""
44+
//|
45+
46+
//| digest_size: int
47+
//| """Digest size in bytes"""
48+
//|
49+
STATIC mp_obj_t hashlib_hash_get_digest_size(mp_obj_t self_in) {
50+
mp_check_self(mp_obj_is_type(self_in, &hashlib_hash_type));
51+
hashlib_hash_obj_t *self = MP_OBJ_TO_PTR(self_in);
52+
return MP_OBJ_NEW_SMALL_INT(common_hal_hashlib_hash_get_digest_size(self));
53+
}
54+
MP_PROPERTY_GETTER(hashlib_hash_digest_size_obj, hashlib_hash_get_digest_size);
55+
56+
//| def update(self, data: ReadableBuffer) -> None:
57+
//| """Update the hash with the given bytes.
58+
//|
59+
//| :param ~circuitpython_typing.ReadableBuffer data: Update the hash from data in this buffer"""
60+
//| ...
61+
//|
62+
mp_obj_t hashlib_hash_update(mp_obj_t self_in, mp_obj_t buf_in) {
63+
mp_check_self(mp_obj_is_type(self_in, &hashlib_hash_type));
64+
hashlib_hash_obj_t *self = MP_OBJ_TO_PTR(self_in);
65+
66+
mp_buffer_info_t bufinfo;
67+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
68+
69+
common_hal_hashlib_hash_update(self, bufinfo.buf, bufinfo.len);
70+
return mp_const_none;
71+
}
72+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(hashlib_hash_update_obj, hashlib_hash_update);
73+
74+
//| def digest(self) -> bytes:
75+
//| """Returns the current digest as bytes() with a length of `hashlib.Hash.digest_size`."""
76+
//| ...
77+
//|
78+
STATIC mp_obj_t hashlib_hash_digest(mp_obj_t self_in) {
79+
mp_check_self(mp_obj_is_type(self_in, &hashlib_hash_type));
80+
hashlib_hash_obj_t *self = MP_OBJ_TO_PTR(self_in);
81+
82+
size_t size = common_hal_hashlib_hash_get_digest_size(self);
83+
mp_obj_t obj = mp_obj_new_bytes_of_zeros(size);
84+
mp_obj_str_t *o = MP_OBJ_TO_PTR(obj);
85+
86+
common_hal_hashlib_hash_digest(self, (uint8_t *)o->data, size);
87+
return obj;
88+
}
89+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(hashlib_hash_digest_obj, hashlib_hash_digest);
90+
91+
STATIC const mp_rom_map_elem_t hashlib_hash_locals_dict_table[] = {
92+
{ MP_ROM_QSTR(MP_QSTR_digest_size), MP_ROM_PTR(&hashlib_hash_digest_size_obj) },
93+
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&hashlib_hash_update_obj) },
94+
{ MP_ROM_QSTR(MP_QSTR_digest), MP_ROM_PTR(&hashlib_hash_digest_obj) },
95+
};
96+
97+
STATIC MP_DEFINE_CONST_DICT(hashlib_hash_locals_dict, hashlib_hash_locals_dict_table);
98+
99+
const mp_obj_type_t hashlib_hash_type = {
100+
{ &mp_type_type },
101+
.name = MP_QSTR_Hash,
102+
.locals_dict = (mp_obj_dict_t *)&hashlib_hash_locals_dict,
103+
};

shared-bindings/hashlib/Hash.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_HASHLIB_HASH_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_HASHLIB_HASH_H
29+
30+
#include "py/obj.h"
31+
32+
#include "common-hal/hashlib/Hash.h"
33+
34+
extern const mp_obj_type_t hashlib_hash_type;
35+
36+
// So that new can call it when given data.
37+
mp_obj_t hashlib_hash_update(mp_obj_t self_in, mp_obj_t buf_in);
38+
39+
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen);
40+
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen);
41+
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self);
42+
43+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_HASHLIB_HASH_H

0 commit comments

Comments
 (0)