# Generate a random number with a maximum value of 1024
print("Random Value: ", atecc.random(rnd_max=1024))
# Print out the value from one of the ATECC's counters
# You should see this counter increase on every time the code.py runs.
print("ATECC Counter #1 Value: ", atecc.counter(1, increment_counter=True))
# Initialize the SHA256 calculation engine
atecc.sha_start()
# Append bytes to the SHA digest
print("Appending to the digest...")
atecc.sha_update(b"Nobody inspects")
print("Appending to the digest...")
atecc.sha_update(b" the spammish repetition")
# Return the digest of the data passed to sha_update
message = atecc.sha_digest()
print("SHA Digest: ", message)
Save the file and open the board's REPL. You should see the following output:
The ATECC will output its serial number and a random value to the REPL. You can
modify the call to atecc.random() in your code to specify a minimum or maximum
integer value to generate.
print("ATECC Serial: ", atecc.serial_number)
# Generate a random number with a maximum value of 1024
print("Random Value: ", atecc.random(rnd_max=1024))
The code also writes a value of one of the ATECC's hardware counters to the REPL.
This code uses counter #1, there's two hardware counters built into the ATECC chip.
If you run the file again (by saving it), you'll notice the counter value increase. The
counter's state is saved, even if the ATECC is powered off. If you're building an IoT
project, you can save important state information in here, such as the last frame which
uitPython's hashlib module()), you can use the ATECC's super-quick hardware to hash
data for you!
CircuitPython_ATECC implements a CPython3 hashlib-like() interface. First, initialize
the SHA256 calculate engine and memory context of the ATECC chip.
# Initialize the SHA256 calculation engine
atecc.sha_start()
Then, can append bytes to hash object by passing bytes to sha_update . You can
append as many bytes as you'd like before you create a message digest.
# Append bytes to the SHA digest
print("Appending to the digest...")
atecc.sha_update(b"Nobody inspects")
print("Appending to the digest...")
atecc.sha_update(b" the spammish repetition")
The digest of the data passed to the sha_update() method is then returned and
printed to the REPL.
# Return the digest of the data passed to sha_update
message = atecc.sha_digest()
print("SHA Digest: ", message)
Self-Signed Certificate Demo
This demo will generate a self signed certificate for a private key generated by your
crypto chip.
Copy the code below to your code.py file.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import busio
from adafruit_atecc.adafruit_atecc import ATECC, _WAKE_CLK_FREQ, CFG_TLS
import adafruit_atecc.adafruit_atecc_cert_util as cert_utils
# -- Enter your configuration below -- #
# Lock the ATECC module when the code is run?
LOCK_ATECC = False
# 2-letter country code
MY_COUNTRY = "US"
# State or Province Name
MY_STATE = "New York"
# City Name
MY_CITY = "New York"
# Organization Name
MY_ORG = "Adafruit"
# Organizational Unit Name
MY_SECTION = "Crypto"
# Which ATECC slot (0-4) to use
ATECC_SLOT = 0
# Generate new private key, or use existing key
GENERATE_PRIVATE_KEY = True
# -- END Configuration, code below -- #
# Initialize the i2c bus
i2c = busio.I2C(board.SCL, board.SDA, frequency=_WAKE_CLK_FREQ)
# Initialize a new atecc object
atecc = ATECC(i2c)
print("ATECC Serial Number: ", atecc.serial_number)
if not atecc.locked:
if not LOCK_ATECC:
raise RuntimeError(
"The ATECC is not locked, set LOCK_ATECC to True in code.py."
)
print("Writing default configuration to the device...")
atecc.write_config(CFG_TLS)
print("Wrote configuration, locking ATECC module...")
# Lock ATECC config, data, and otp zones
atecc.lock_all_zones()
print("ATECC locked!")
MY_COUNTRY = "US"
# State or Province Name
MY_STATE = "New York"
# City Name
MY_CITY = "New York"
# Organization Name
MY_ORG = "Adafruit"
# Organizational Unit Name
MY_SECTION = "Crypto"
# Which ATECC slot (0-4) to use
ATECC_SLOT = 0
# Generate new private key, or use existing key
GENERATE_PRIVATE_KEY = True
# -- END Configuration, code below -- #
Once the configuration in code.py looks correct, change the following line from:
LOCK_ATECC = False
to
LOCK_ATECC = True
This will permanently lock your chip. You will only need to set this once.
Save and run your code. You should see the following output:
ATECC Serial Number: 012347A22713EAFCEE
Writing default configuration to the device...
Wrote configuration, locking ATECC module...
ATECC locked!