Skip to main content

Module encrypted_write

Module encrypted_write 

Source
Expand description

Host-side cryptography for the ATECC’s encrypted write flow.

The chip can write a 32-byte slot only after the host has set up a shared “session key” in TempKey via Nonce + GenDig. The host must then encrypt the new value and produce a MAC that the chip can verify.

The full sequence is:

  1. Host generates a random 32-byte input and sends it as Nonce(passthrough). The chip’s TempKey is now equal to that input.
  2. Host issues GenDig(zone=Data, key_id=io_slot). The chip updates TempKey to SHA-256(io_key || opcode || param1 || param2 || sn[8] || sn[0..2] || zeros(25) || TempKey_prev).
  3. The host can recompute the same TempKey because it knows the io_key (passed in at provisioning) and all the other inputs.
  4. To write plaintext (32 bytes) into target_slot, the host sends Write with data = ciphertext || mac where:
    • ciphertext[i] = plaintext[i] XOR TempKey[i]
    • mac = SHA-256(io_key || opcode_write || param1 || param2 || sn[8] || sn[0..2] || zeros(25) || TempKey || plaintext)

The chip recomputes the MAC, validates it, decrypts, and stores.

This module exposes the pure host-side helpers. The orchestration against the live chip lives in crate::service::CryptoService.

Constants§

CHIP_SERIAL_LEN 🔒
Length of an ATECC chip serial number in bytes (as read from the config zone).
OP_GENDIG 🔒
ATECC opcode for GenDig.
OP_WRITE 🔒
ATECC opcode for Write.
SLOT_VALUE_LEN 🔒
Length of a slot value (32 bytes).
ZONE_DATA 🔒
Zone byte encoding Data in the GenDig and Write commands.

Functions§

build_encrypted_write_payload 🔒
Assemble the 64-byte payload (ciphertext || mac) that the driver expects in [atecc608b::AteccChannel::write_32_encrypted].
derive_session_key 🔒
Compute the TempKey value that the chip ends up with after a Nonce(passthrough, nonce_input) followed by GenDig(zone=Data, key_id=io_slot).
encrypt_payload 🔒
XOR-encrypt a 32-byte plaintext with the session key.
write_mac 🔒
Compute the MAC that the chip expects to find appended to the ciphertext in an encrypted write.