Skip to content

Commit a1df3bb

Browse files
pandiannanolbuque
authored andcommitted
libs/unit/id.py: Integrated with ATECC608B hardware cryptographic chip.
Signed-off-by: Pandian Nano <[email protected]>
1 parent da6bb81 commit a1df3bb

File tree

13 files changed

+1349
-0
lines changed

13 files changed

+1349
-0
lines changed

docs/en/refs/unit.id.ref

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. |IDUnit| image:: https://static-cdn.m5stack.com/resource/docs/products/unit/id/id_01.webp
2+
:target: https://docs.m5stack.com/en/unit/id
3+
:height: 200px
4+
:width: 200 px
5+
6+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/example.png
7+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/init.png
8+
.. |get_revision_number.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/get_revision_number.png
9+
.. |get_serial_number.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/get_serial_number.png
10+
.. |randint.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/randint.png
11+
.. |random.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/random.png
12+
.. |randrange_max.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/randrange_max.png
13+
.. |randrange.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/randrange.png
14+
.. |uniform.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/uniform.png
15+
.. |get_generate_key.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/get_generate_key.png
16+
.. |get_sha256_hash.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/get_sha256_hash.png
17+
.. |get_ecdsa_sign.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/get_ecdsa_sign.png
18+
.. |get_verify_ecdsa_sign.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/get_verify_ecdsa_sign.png
19+
.. |set_certificate_signing_request.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/id/set_certificate_signing_request.png
20+
21+
22+
.. |cores3_id_example.m5f2| raw:: html
23+
24+
<a
25+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/id/cores3_id_example.m5f2"
26+
target="_blank"
27+
>
28+
cores3_id_example.m5f2
29+
</a>
30+

docs/en/units/id.rst

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
ID Unit
2+
=======
3+
4+
.. include:: ../refs/unit.id.ref
5+
6+
The ``ID Unit`` is a crypto coprocessor with hardware-based secure key storage, integrated with ATECC608B hardware cryptographic chip, using I2C communication interface. The chip has a built-in 10Kb EEPROM for storing keys, certificates, data, consumption records and security configurations.
7+
8+
Support the following products:
9+
10+
|IDUnit|
11+
12+
Micropython Example::
13+
14+
import os, sys, io
15+
import M5
16+
from M5 import *
17+
from hardware import *
18+
from unit import IDUnit
19+
20+
21+
i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
22+
id_0 = IDUnit(i2c0)
23+
print(id_0.get_sha256_hash('Hello M5', 1))
24+
print(id_0.get_generate_key(0, False))
25+
print(id_0.randrange(500, 1000, 5))
26+
27+
UIFLOW2 Example:
28+
29+
|example.png|
30+
31+
.. only:: builder_html
32+
33+
|cores3_id_example.m5f2|
34+
35+
36+
class IDUnit
37+
------------
38+
39+
Constructors
40+
------------
41+
42+
.. class:: IDUnit(i2c)
43+
44+
Create a IDUnit object
45+
46+
:param object i2c: the I2C object.
47+
48+
UIFLOW2:
49+
50+
|init.png|
51+
52+
53+
Methods
54+
-------
55+
56+
.. method:: IDUnit.get_revision_number() -> int
57+
58+
Returns the ATECC608B revision number. A revision number refers to a version identifier that indicates a specific
59+
iteration or update of the hardware design. The revision number helps distinguish between different versions of the same chip model.
60+
61+
- Return: ``int``: hexdecimal
62+
63+
UIFLOW2:
64+
65+
|get_revision_number.png|
66+
67+
68+
.. method:: IDUnit.get_serial_number() -> str
69+
70+
Returns the ATECC serial number.
71+
72+
9-byte serial number is structured as follows:
73+
74+
First 4 Bytes: These bytes are the first part of the serial number, which includes a fixed pattern and a portion that is unique to the device.
75+
Next 2 Bytes: These bytes are reserved and typically set to 0x00 or other reserved values.
76+
Last 3 Bytes: These bytes are the final part of the serial number and are unique to the device.
77+
78+
- Return: ``string``
79+
80+
UIFLOW2:
81+
82+
|get_serial_number.png|
83+
84+
85+
.. method:: IDUnit.randint(min, max) -> int
86+
87+
Returns the random number(4 byte). generate true random numbers using its hardware-based random number generator(RNG).
88+
This RNG is often used in secure applications where high-quality randomness is needed, such as in key generation.
89+
90+
:param int min: 0 ~ 4294967295.
91+
:param int max: 0 ~ 4294967295.
92+
93+
- Return: ``int``: 0 ~ 4294967295
94+
95+
UIFLOW2:
96+
97+
|randint.png|
98+
99+
100+
.. method:: IDUnit.random() -> float
101+
102+
Returns a random floating point number in the range [0.0 ~ 1.0].
103+
104+
- Return: ``float``: 0.0 ~ 1.0
105+
106+
UIFLOW2:
107+
108+
|random.png|
109+
110+
111+
.. method:: IDUnit.randrange(min, max, step) -> int
112+
113+
The first form returns a random integer from the range(0, max).
114+
The second form returns a random integer from the range (min, max, step) in steps of step.
115+
For instance, calling randrange(1, 10, 2) will return odd numbers between 1 and 9 inclusive.
116+
117+
:param int min: 0 ~ 4294967295.
118+
:param int max: 0 ~ 4294967295.
119+
:param int step: 0 ~ 4294967295.
120+
121+
- Return: ``int``: 0 ~ 4294967295
122+
123+
UIFLOW2:
124+
125+
|randrange_max.png|
126+
|randrange.png|
127+
128+
129+
.. method:: IDUnit.uniform(min, max) -> float
130+
131+
Return a random floating point number N such that min <= N <= max for min <= max, and max <= N <= min for max < min.
132+
133+
:param float min:
134+
:param float max:
135+
136+
- Return: ``float``:
137+
138+
UIFLOW2:
139+
140+
|uniform.png|
141+
142+
143+
.. method:: IDUnit.get_generate_key(slot_num, private_key) -> bytearray
144+
145+
Returns the generates a private or public key. A private key is a confidential piece of data that is used in
146+
cryptography to perform various functions. A public key is a cryptographic key that is paired with a private
147+
key in public-key cryptography.
148+
149+
:param int slot_num: 0 ~ 4
150+
:param bool private_key: True or False
151+
152+
- Return: ``bytearray``:
153+
154+
UIFLOW2:
155+
156+
|get_generate_key.png|
157+
158+
159+
.. method:: IDUnit.get_ecdsa_sign(slot, message) -> bytearray
160+
161+
Returns the ECDSA signatures. ECDSA is widely used in digital signatures
162+
for ensuring the authenticity and integrity of messages and documents.
163+
164+
:param int slot: 0 ~ 4
165+
:param message: ``string`` or ``list`` or ``bytearray``
166+
167+
- Return: ``bytearray``:
168+
169+
UIFLOW2:
170+
171+
|get_ecdsa_sign.png|
172+
173+
174+
.. method:: IDUnit.get_verify_ecdsa_sign(message, sign, key) -> bool
175+
176+
Returns the verify ecsda signature status. A signature verification in the Elliptic Curve Digital Signature Algorithm (ECDSA)
177+
is the process of checking whether a given digital signature is valid and was indeed generated by the holder of the corresponding
178+
private key or public key. This process ensures that the message or data.
179+
180+
:param message: ``string`` or ``list`` or ``bytearray``
181+
:param sign: ``bytearray``
182+
:param key: ``bytearray``
183+
184+
- Return: ``bool``: True or False
185+
186+
UIFLOW2:
187+
188+
|get_verify_ecdsa_sign.png|
189+
190+
191+
.. method:: IDUnit.get_sha256_hash(message, format) -> str
192+
193+
Get the generate the SHA-256 hash value. SHA-256 (Secure Hash Algorithm 256-bit) is a cryptographic hash
194+
function that produces a fixed-size, 256-bit (32-byte) hash value, regardless of the size of the input data.
195+
196+
:param message: ``string``
197+
:param format: 0: hexdecimal, 1: base64
198+
199+
- Return: ``string``:
200+
201+
UIFLOW2:
202+
203+
|get_sha256_hash.png|
204+
205+
206+
.. method:: IDUnit.set_certificate_signing_request(slot_num, private_key, country, state_prov, city, org, org_unit, file_path) -> None
207+
208+
A Certificate Signing Request (CSR) is a block of encoded text that is sent to a Certificate Authority (CA) when you apply for
209+
an SSL/TLS certificate. It contains information that the CA uses to create your certificate, including your public key and some
210+
information about your organization.
211+
212+
:param int slot_num: 0 ~ 4
213+
:param bool private_key: True or False
214+
:param str country: country name example: China
215+
:param str state_prov: states or province name
216+
:param str city: city name
217+
:param str org: organization or company name
218+
:param str org_unit: organization or company unit name
219+
:param str file_path: Store the file to flash or SD
220+
221+
UIFLOW2:
222+
223+
|set_certificate_signing_request.png|
224+
225+

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Unit
3232
grove2grove.rst
3333
hall_effect.rst
3434
heart.rst
35+
id.rst
3536
imu.rst
3637
imupro.rst
3738
ir.rst
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","versionNumber":"V2.1.3","type":"cores3","components":[{"name":"screen","type":"screen","layer":0,"screenId":"builtin","screenName":"","id":"__cores3_screen","createTime":1726022847392,"x":0,"y":0,"width":320,"height":240,"backgroundColor":"#222222","size":0,"isSelected":true}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","speaker","touch","als","mic","i2c"]},{"unit":["unit_id"]}],"units":[{"type":"unit_id","name":"id_0","portList":["A","PAHUB","Custom"],"portType":"A","userPort":[22,21],"id":"d%aNa_FqEropoVNf","createTime":1726022856991,"bus":"i2c0","pahubPortList":[0,1,2,3,4,5],"pahubPort":0,"initBlockId":"by%_u]FOyQ@!:-o-54m?"}],"hats":[],"bases":[],"i2cs":[{"id":"i2c0","portType":"A","userPort":[22,21],"freq":"100000","blockId":"/M[[4GQ2N+_:Nge#6aQ~"}],"blockly":"<block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"-70\" y=\"-170\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"system_m5_begin\"><next><block type=\"i2c_init\" id=\"/M[[4GQ2N+_:Nge#6aQ~\"><field name=\"NAME\">0</field><field name=\"FREQ\">100000</field><value name=\"SCL\"><shadow type=\"math_number\" id=\"TeCkH@08PX/agUUdAc+Q\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow></value><value name=\"SDA\"><shadow type=\"math_number\" id=\"N^+wLJDJeg_f0c=%X)gY\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">2</field></shadow></value><next><block type=\"unit_id_init\" id=\"by%_u]FOyQ@!:-o-54m?\"><field name=\"NAME\">id_0</field><next><block type=\"text_print\" id=\"Zr~z`HIgx?RGR~n2CpN8\"><value name=\"TEXT\"><shadow type=\"text\" id=\"{bXhAp69_?;N$C:{yNTY\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"unit_id_get_sha256_hash\" id=\"b0.^bB*W}5fh`).)nSh@\"><field name=\"NAME\">id_0</field><field name=\"FORMAT\">1</field><value name=\"VALUE\"><shadow type=\"text\" id=\"[?)C@[_=M-6htTWGh%}5\"><field name=\"TEXT\">Hello M5</field></shadow></value></block></value><next><block type=\"text_print\" id=\"*1Jc|j?(+LpTmVMvr$.p\"><value name=\"TEXT\"><shadow type=\"text\" id=\"{bXhAp69_?;N$C:{yNTY\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"unit_id_get_generate_key\" id=\"1Aq*M(znz=LZBcuNTvOR\"><field name=\"NAME\">id_0</field><value name=\"SLOT\"><shadow type=\"math_slider\" id=\"^Vd$2(W`7*pS!EE_HKGS\"><mutation max=\"4\" min=\"0\" step=\"1\" precision=\"1\"></mutation><field name=\"NUM\">0</field></shadow></value><value name=\"TYPE\"><shadow type=\"unit_id_key_type_option\" id=\"3tYMcwgKFT|yHI8qsU62\"><field name=\"VALUE\">False</field></shadow></value></block></value><next><block type=\"text_print\" id=\"r#vLV5^{txprgt)S;0z]\"><value name=\"TEXT\"><shadow type=\"text\" id=\"{bXhAp69_?;N$C:{yNTY\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"unit_id_get_random_number_range\" id=\"08.K`-XvtD2uDdXPfF{r\"><field name=\"NAME\">id_0</field><value name=\"MIN\"><shadow type=\"math_number\" id=\"3u`y):Gm?,%t](nTVExd\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">500</field></shadow></value><value name=\"MAX\"><shadow type=\"math_number\" id=\"Tp6T}!jT=f7y6[QJjoC{\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">1000</field></shadow></value><value name=\"STEP\"><shadow type=\"math_number\" id=\"C`9+_DAsjhs7E^[xO6n_\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">5</field></shadow></value></block></value></block></next></block></next></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"-70\" y=\"130\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"system_m5_update\"></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":320,"height":240,"scale":0.78,"screenName":"","blockId":"","screenColorType":0,"id":"builtin","createTime":1726022847390}],"logicWhenNum":0,"customList":[]}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os, sys, io
6+
import M5
7+
from M5 import *
8+
from hardware import *
9+
from unit import IDUnit
10+
11+
i2c0 = None
12+
id_0 = None
13+
14+
15+
def setup():
16+
global i2c0, id_0
17+
18+
M5.begin()
19+
Widgets.fillScreen(0x222222)
20+
21+
i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
22+
id_0 = IDUnit(i2c0)
23+
print(id_0.get_sha256_hash("Hello M5", 1))
24+
print(id_0.get_generate_key(0, False))
25+
print(id_0.randrange(500, 1000, 5))
26+
27+
28+
def loop():
29+
global i2c0, id_0
30+
M5.update()
31+
32+
33+
if __name__ == "__main__":
34+
try:
35+
setup()
36+
while True:
37+
loop()
38+
except (Exception, KeyboardInterrupt) as e:
39+
try:
40+
from utility import print_error_msg
41+
42+
print_error_msg(e)
43+
except ImportError:
44+
print("please update to latest firmware")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2018 Arduino SA
2+
# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
3+
# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
# Copyright (c) 2018 Arduino SA. All rights reserved.
8+
#
9+
# This library is free software; you can redistribute it and/or
10+
# modify it under the terms of the GNU Lesser General Public
11+
# License as published by the Free Software Foundation; either
12+
# version 2.1 of the License, or (at your option) any later version.
13+
#
14+
# This library is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17+
# Lesser General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU Lesser General Public
20+
# License along with this library; if not, write to the Free Software
21+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

0 commit comments

Comments
 (0)