From 4d9addf3e11de2afb74e9f31cbf3ae334c8b59e1 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Thu, 25 Feb 2021 22:39:32 +0100 Subject: [PATCH 1/7] - Added documentation for config bytes - Added documentation for i2c config byte 16 - Added I2C address variable used to derive config byte 16 - Added I2C address to config byte 16 of CFG_TLS - Modified representation of CFG_TLS --- adafruit_atecc/adafruit_atecc.py | 71 ++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index f0c8b8c..2c22e70 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -52,8 +52,20 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ATECC.git" + +def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): + int(i2c_addr, 16) + return i2c_addr << 1 + + # Device Address -_REG_ATECC_ADDR = const(0xC0) +_I2C_ADDR = 0x60 +_REG_ATECC_ADDR = _convert_i2c_addr_to_atecc_addr(i2c_addr=_I2C_ADDR) + +# TODO: Verify that _REG_ATECC_ADDR is still 0xC0 +# TODO: Remove assertion test afterwards +assert _REG_ATECC_ADDR == 0xC0 + _REG_ATECC_DEVICE_ADDR = _REG_ATECC_ADDR >> 1 # Version Registers @@ -88,7 +100,6 @@ OP_WRITE: const(26), } - CFG_TLS = b"\x01#\x00\x00\x00\x00P\x00\x00\x00\x00\x00\x00\xc0q\x00 \ \xc0\x00U\x00\x83 \x87 \x87 \x87/\x87/\x8f\x8f\x9f\x8f\xaf \ \x8f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \ @@ -98,6 +109,58 @@ \x003\x003\x003\x003\x00\x1c\x00\x1c\x00\x1c\x00<\x00<\x00<\x00< \ \x00<\x00<\x00<\x00\x1c\x00" +""" +Serial Number (Bytes 0-3 and 8-12), Revision Number (Bytes 4-7) +AES Enable (Byte 13), I2C Enable (Byte 14), Reserved (Byte 15) +I2C Address (Byte 16), Reserved (Byte 17); Count Match (Byte 18) +Chi Mode (Byte 19), Slot Config (Bytes 20-51) +Counter 0 (Bytes 52-59), Counter 1 (Bytes 60-67) +Use Lock (Byte 68), Volatile Key Permission (Byte 69) +Secure Boot (Bytes 70-71), KDF (Bytes 72-74) +Reserved (Bytes 75-83), User Extra (Bytes 84-85) +Lock Config (Bytes 86-89), Chip Options (Bytes 90-91) +X509 (Bytes 92-95), Key Config (Bytes 96-127) +""" +CFG_TLS_HEX = bytes( + bytearray.fromhex( + '01 23 00 00 00 00 50 00 00 00 00 00 00 c0 71 00' + '20 20 20 20 20 20 20 20 20 20 20 20 20 c0 00 55' + '00 83 20 87 20 87 20 87 2f 87 2f 8f 8f 9f 8f af' + '20 20 20 20 20 20 20 20 20 20 20 20 20 8f 00 00' + '00 00 00 00 00 00 00 00 00 00 00 00 20 20 20 20' + '20 20 20 20 20 20 20 20 20 af 8f ff ff ff ff 00' + '00 00 00 ff ff ff ff 00 20 20 20 20 20 20 20 20' + '20 20 20 20 20 00 00 00 ff ff ff ff ff ff ff ff' + 'ff ff ff ff 20 20 20 20 20 20 20 20 20 20 20 20' + '20 ff ff ff ff 00 00 55 55 ff ff 00 00 00 00 00' + '00 33 20 20 20 20 20 20 20 20 20 20 20 20 20 00' + '33 00 33 00 33 00 33 00 1c 00 1c 00 1c 00 3c 00' + '3c 00 3c 00 3c 20 20 20 20 20 20 20 20 20 20 20' + '20 20 00 3c 00 3c 00 3c 00 1c 00' + ) +) + +# TODO: Verify that both representations are identical +# TODO: Decide whether to use alternate representation of config bytes +# TODO: Remove assertion tests +assert CFG_TLS == CFG_TLS_HEX +assert bytearray(CFG_TLS)[16] == 0x20 # + +"""I2C Config + HEX DEC BIN Description +Byte 14: C0 192 1100 0000 + ^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip +Byte 16: C0 192 0010 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA +Byte 16: 6A 106 0010 0000 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS +Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN +""" + +# Convert I2C address to config byte 16 and update CFG_TLS +_CFG_BYTES = bytearray(CFG_TLS) +_CFG_BYTE_16 = hex(_I2C_ADDR << 1) +_CFG_BYTES[16] = ord(_CFG_BYTE_16) +CFG_TLS = bytes(_CFG_BYTES) + class ATECC: """ @@ -254,7 +317,7 @@ def nonce(self, data, mode=0, zero=0x0000): time.sleep(1 / 1000) if mode == 0x03: assert ( - calculated_nonce[0] == 0x00 + calculated_nonce[0] == 0x00 ), "Incorrectly calculated nonce in pass-thru mode" self.idle() return calculated_nonce @@ -424,7 +487,7 @@ def write_config(self, data): if i == 84: # can't write continue - self._write(0, i // 4, data[i : i + 4]) + self._write(0, i // 4, data[i: i + 4]) def _write(self, zone, address, buffer): self.wakeup() From d3e6401d947ca62b2a2f5574ed43dcf4aabcadf0 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Fri, 26 Feb 2021 06:38:50 +0100 Subject: [PATCH 2/7] - Making pylint happy - Moved I2C Config doc to CFG_TLS_HEX description - Corrected BIN representation of 0x6A --- adafruit_atecc/adafruit_atecc.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index 2c22e70..e9d81cd 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -110,6 +110,8 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): \x00<\x00<\x00<\x00\x1c\x00" """ +Configuration Zone Bytes + Serial Number (Bytes 0-3 and 8-12), Revision Number (Bytes 4-7) AES Enable (Byte 13), I2C Enable (Byte 14), Reserved (Byte 15) I2C Address (Byte 16), Reserved (Byte 17); Count Match (Byte 18) @@ -120,6 +122,15 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): Reserved (Bytes 75-83), User Extra (Bytes 84-85) Lock Config (Bytes 86-89), Chip Options (Bytes 90-91) X509 (Bytes 92-95), Key Config (Bytes 96-127) + +I2C Config + + HEX DEC BIN Description +Byte 14: C0 192 1100 0000 + ^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip +Byte 16: C0 192 0010 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA +Byte 16: 6A 106 0110 1010 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS +Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN """ CFG_TLS_HEX = bytes( bytearray.fromhex( @@ -146,15 +157,6 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): assert CFG_TLS == CFG_TLS_HEX assert bytearray(CFG_TLS)[16] == 0x20 # -"""I2C Config - HEX DEC BIN Description -Byte 14: C0 192 1100 0000 - ^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip -Byte 16: C0 192 0010 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA -Byte 16: 6A 106 0010 0000 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS -Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN -""" - # Convert I2C address to config byte 16 and update CFG_TLS _CFG_BYTES = bytearray(CFG_TLS) _CFG_BYTE_16 = hex(_I2C_ADDR << 1) From 61bd4b6b18d14160301488e553d338538bea9b84 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Fri, 26 Feb 2021 06:38:50 +0100 Subject: [PATCH 3/7] - Making pylint happy - Moved I2C Config doc to CFG_TLS_HEX description - Corrected BIN representation of 0x6A --- adafruit_atecc/adafruit_atecc.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index 2c22e70..9183fa1 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -110,6 +110,8 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): \x00<\x00<\x00<\x00\x1c\x00" """ +Configuration Zone Bytes + Serial Number (Bytes 0-3 and 8-12), Revision Number (Bytes 4-7) AES Enable (Byte 13), I2C Enable (Byte 14), Reserved (Byte 15) I2C Address (Byte 16), Reserved (Byte 17); Count Match (Byte 18) @@ -120,6 +122,15 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): Reserved (Bytes 75-83), User Extra (Bytes 84-85) Lock Config (Bytes 86-89), Chip Options (Bytes 90-91) X509 (Bytes 92-95), Key Config (Bytes 96-127) + +I2C Config + + HEX DEC BIN Description +Byte 14: C0 192 1100 0000 + ^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip +Byte 16: C0 192 0010 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA +Byte 16: 6A 106 0110 1010 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS +Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN """ CFG_TLS_HEX = bytes( bytearray.fromhex( @@ -144,16 +155,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): # TODO: Decide whether to use alternate representation of config bytes # TODO: Remove assertion tests assert CFG_TLS == CFG_TLS_HEX -assert bytearray(CFG_TLS)[16] == 0x20 # - -"""I2C Config - HEX DEC BIN Description -Byte 14: C0 192 1100 0000 - ^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip -Byte 16: C0 192 0010 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA -Byte 16: 6A 106 0010 0000 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS -Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN -""" +assert bytearray(CFG_TLS)[16] == 0x20 # Convert I2C address to config byte 16 and update CFG_TLS _CFG_BYTES = bytearray(CFG_TLS) From a6bb6ca6ea75a865f905e9b38591229b153f85d2 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Fri, 26 Feb 2021 06:53:35 +0100 Subject: [PATCH 4/7] Make pylint happy. --- adafruit_atecc/adafruit_atecc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index e9d81cd..9183fa1 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -155,7 +155,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): # TODO: Decide whether to use alternate representation of config bytes # TODO: Remove assertion tests assert CFG_TLS == CFG_TLS_HEX -assert bytearray(CFG_TLS)[16] == 0x20 # +assert bytearray(CFG_TLS)[16] == 0x20 # Convert I2C address to config byte 16 and update CFG_TLS _CFG_BYTES = bytearray(CFG_TLS) From 88ef369aa08ffdef37c43cc37fbc8067b3b855e3 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Fri, 26 Feb 2021 07:05:39 +0100 Subject: [PATCH 5/7] Make black happy. --- adafruit_atecc/adafruit_atecc.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index 9183fa1..f2e2261 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -134,20 +134,20 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): """ CFG_TLS_HEX = bytes( bytearray.fromhex( - '01 23 00 00 00 00 50 00 00 00 00 00 00 c0 71 00' - '20 20 20 20 20 20 20 20 20 20 20 20 20 c0 00 55' - '00 83 20 87 20 87 20 87 2f 87 2f 8f 8f 9f 8f af' - '20 20 20 20 20 20 20 20 20 20 20 20 20 8f 00 00' - '00 00 00 00 00 00 00 00 00 00 00 00 20 20 20 20' - '20 20 20 20 20 20 20 20 20 af 8f ff ff ff ff 00' - '00 00 00 ff ff ff ff 00 20 20 20 20 20 20 20 20' - '20 20 20 20 20 00 00 00 ff ff ff ff ff ff ff ff' - 'ff ff ff ff 20 20 20 20 20 20 20 20 20 20 20 20' - '20 ff ff ff ff 00 00 55 55 ff ff 00 00 00 00 00' - '00 33 20 20 20 20 20 20 20 20 20 20 20 20 20 00' - '33 00 33 00 33 00 33 00 1c 00 1c 00 1c 00 3c 00' - '3c 00 3c 00 3c 20 20 20 20 20 20 20 20 20 20 20' - '20 20 00 3c 00 3c 00 3c 00 1c 00' + "01 23 00 00 00 00 50 00 00 00 00 00 00 c0 71 00" + "20 20 20 20 20 20 20 20 20 20 20 20 20 c0 00 55" + "00 83 20 87 20 87 20 87 2f 87 2f 8f 8f 9f 8f af" + "20 20 20 20 20 20 20 20 20 20 20 20 20 8f 00 00" + "00 00 00 00 00 00 00 00 00 00 00 00 20 20 20 20" + "20 20 20 20 20 20 20 20 20 af 8f ff ff ff ff 00" + "00 00 00 ff ff ff ff 00 20 20 20 20 20 20 20 20" + "20 20 20 20 20 00 00 00 ff ff ff ff ff ff ff ff" + "ff ff ff ff 20 20 20 20 20 20 20 20 20 20 20 20" + "20 ff ff ff ff 00 00 55 55 ff ff 00 00 00 00 00" + "00 33 20 20 20 20 20 20 20 20 20 20 20 20 20 00" + "33 00 33 00 33 00 33 00 1c 00 1c 00 1c 00 3c 00" + "3c 00 3c 00 3c 20 20 20 20 20 20 20 20 20 20 20" + "20 20 00 3c 00 3c 00 3c 00 1c 00" ) ) @@ -319,7 +319,7 @@ def nonce(self, data, mode=0, zero=0x0000): time.sleep(1 / 1000) if mode == 0x03: assert ( - calculated_nonce[0] == 0x00 + calculated_nonce[0] == 0x00 ), "Incorrectly calculated nonce in pass-thru mode" self.idle() return calculated_nonce @@ -489,7 +489,7 @@ def write_config(self, data): if i == 84: # can't write continue - self._write(0, i // 4, data[i: i + 4]) + self._write(0, i // 4, data[i : i + 4]) def _write(self, zone, address, buffer): self.wakeup() From 3718158267f089fdcd96722d87b07653b67e1536 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Fri, 26 Feb 2021 07:25:19 +0100 Subject: [PATCH 6/7] Fixed typo. --- adafruit_atecc/adafruit_atecc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index f2e2261..7bd03b4 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -115,7 +115,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): Serial Number (Bytes 0-3 and 8-12), Revision Number (Bytes 4-7) AES Enable (Byte 13), I2C Enable (Byte 14), Reserved (Byte 15) I2C Address (Byte 16), Reserved (Byte 17); Count Match (Byte 18) -Chi Mode (Byte 19), Slot Config (Bytes 20-51) +Chip Mode (Byte 19), Slot Config (Bytes 20-51) Counter 0 (Bytes 52-59), Counter 1 (Bytes 60-67) Use Lock (Byte 68), Volatile Key Permission (Byte 69) Secure Boot (Bytes 70-71), KDF (Bytes 72-74) From 5eacdce9a2bdde06b9c87ed2e8d07af79cb933d4 Mon Sep 17 00:00:00 2001 From: Reiner Rottmann Date: Fri, 26 Feb 2021 07:27:19 +0100 Subject: [PATCH 7/7] Fixed binary representation. --- adafruit_atecc/adafruit_atecc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_atecc/adafruit_atecc.py b/adafruit_atecc/adafruit_atecc.py index 7bd03b4..129c5f8 100755 --- a/adafruit_atecc/adafruit_atecc.py +++ b/adafruit_atecc/adafruit_atecc.py @@ -128,7 +128,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60): HEX DEC BIN Description Byte 14: C0 192 1100 0000 ^xxx xxxx Bit 0 (MSB): 0:Single Wire, 1:I2C; Bit 1-7: Set by Microchip -Byte 16: C0 192 0010 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA +Byte 16: C0 192 1100 0000 Default 7 bit I2C Address: 0xC0>>1: 0x60 ATECC608A-MAHDA Byte 16: 6A 106 0110 1010 Default 7 bit I2C Address: 0x6A>>1: 0x35 ATECC608A-TNGTLS Byte 16: 20 32 0010 0000 Default 7 bit I2C Address: 0x20>>1: 0x10 ATECC608A-UNKNOWN """