Skip to content

Commit d7687be

Browse files
authored
Merge pull request #38 from CaptainHaddock2016/patch-1
Added ECDH support
2 parents 9eb8714 + 6d76421 commit d7687be

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

adafruit_atecc/adafruit_atecc.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60):
9494
OP_GEN_KEY = const(0x40)
9595
OP_SIGN = const(0x41)
9696
OP_WRITE = const(0x12)
97+
OP_ECDH = const(0x43)
9798

9899
# Maximum execution times, in milliseconds (9-4)
99100
EXEC_TIME = {
@@ -106,6 +107,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60):
106107
OP_GEN_KEY: const(115),
107108
OP_SIGN: const(70),
108109
OP_WRITE: const(26),
110+
OP_ECDH: const(80),
109111
}
110112

111113
"""
@@ -454,6 +456,27 @@ def sha_digest(self, message: bytearray = None) -> bytearray:
454456
self.idle()
455457
return digest
456458

459+
def ecdh(self, slot_num: int, public_key: bytearray, mode: int = 0x0C) -> bytearray:
460+
"""
461+
Performs ECDH key agreement operation.
462+
:param int slot_num: ECC slot (0-4) containing private key.
463+
:param bytearray public_key: 64-byte public key (X||Y).
464+
:param int mode: Mode parameter, defaults to 0x0C.
465+
:return: bytearray containing the shared secret
466+
"""
467+
468+
assert len(public_key) == 64, "Public key must be 64 bytes (X||Y)"
469+
470+
self.wakeup()
471+
# Send ECDH command (opcode 0x43)
472+
self._send_command(OP_ECDH, mode, slot_num, public_key)
473+
time.sleep(EXEC_TIME[OP_ECDH] / 1000)
474+
475+
response = bytearray(32) # shared secret
476+
self._get_response(response)
477+
self.idle()
478+
return response
479+
457480
def gen_key(self, key: bytearray, slot_num: int, private_key: bool = False) -> bytearray:
458481
"""
459482
Generates a private or public key.

0 commit comments

Comments
 (0)