Skip to content

Ran black, updated to pylint 2.x #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
94 changes: 48 additions & 46 deletions adafruit_atecc/adafruit_atecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
_ATECC_608_VER = const(0x60)

# Clock constants
_WAKE_CLK_FREQ = 100000 # slower clock speed
_TWLO_TIME = 6e-5 # TWlo, in microseconds
_WAKE_CLK_FREQ = 100000 # slower clock speed
_TWLO_TIME = 6e-5 # TWlo, in microseconds

# Command Opcodes (9-1-3)
OP_COUNTER = const(0x24)
Expand All @@ -92,30 +92,34 @@
OP_WRITE = const(0x12)

# Maximum execution times, in milliseconds (9-4)
EXEC_TIME = {OP_COUNTER: const(20),
OP_INFO: const(1),
OP_NONCE: const(7),
OP_RANDOM: const(23),
OP_SHA: const(47),
OP_LOCK: const(32),
OP_GEN_KEY: const(115),
OP_SIGN : const(70),
OP_WRITE : const(26)}


CFG_TLS = b'\x01#\x00\x00\x00\x00P\x00\x00\x00\x00\x00\x00\xc0q\x00 \
EXEC_TIME = {
OP_COUNTER: const(20),
OP_INFO: const(1),
OP_NONCE: const(7),
OP_RANDOM: const(23),
OP_SHA: const(47),
OP_LOCK: const(32),
OP_GEN_KEY: const(115),
OP_SIGN: const(70),
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 \
\xaf\x8f\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\x00 \
\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff \
\xff\xff\xff\xff\x00\x00UU\xff\xff\x00\x00\x00\x00\x00\x003 \
\x003\x003\x003\x003\x00\x1c\x00\x1c\x00\x1c\x00<\x00<\x00<\x00< \
\x00<\x00<\x00<\x00\x1c\x00'
\x00<\x00<\x00<\x00\x1c\x00"


class ATECC:
"""
CircuitPython interface for ATECCx08A Crypto Co-Processor Devices.
"""

def __init__(self, i2c_bus, address=_REG_ATECC_DEVICE_ADDR, debug=False):
"""Initializes an ATECC device.
:param busio i2c_bus: I2C Bus object.
Expand All @@ -132,7 +136,9 @@ def __init__(self, i2c_bus, address=_REG_ATECC_DEVICE_ADDR, debug=False):
self._i2c_device = I2CDevice(self._i2c_bus, address)
self.idle()
if (self.version() >> 8) not in (_ATECC_508_VER, _ATECC_608_VER):
raise RuntimeError("Failed to find 608 or 508 chip. Please check your wiring.")
raise RuntimeError(
"Failed to find 608 or 508 chip. Please check your wiring."
)

def wakeup(self):
"""Wakes up THE ATECC608A from sleep or idle modes.
Expand All @@ -148,13 +154,13 @@ def wakeup(self):
try:
self._i2c_bus.writeto(0x0, zero_bits)
except OSError:
pass # this may fail, that's ok - its just to wake up the chip!
pass # this may fail, that's ok - its just to wake up the chip!
time.sleep(_TWLO_TIME)
data = self._i2c_bus.scan() # check for an i2c device
data = self._i2c_bus.scan() # check for an i2c device

try:
if data[0] != 96:
raise TypeError('ATECCx08 not found - please check your wiring!')
raise TypeError("ATECCx08 not found - please check your wiring!")
except IndexError:
raise IndexError("ATECCx08 not found - please check your wiring!")
self._i2c_bus.unlock()
Expand Down Expand Up @@ -228,20 +234,18 @@ def lock_all_zones(self):
self.lock(0)
self.lock(1)


def lock(self, zone):
"""Locks specific ATECC zones.
:param int zone: ATECC zone to lock.
"""
self.wakeup()
self._send_command(0x17, 0x80 | zone, 0x0000)
time.sleep(EXEC_TIME[OP_LOCK]/1000)
time.sleep(EXEC_TIME[OP_LOCK] / 1000)
res = bytearray(1)
self._get_response(res)
assert res[0] == 0x00, "Failed locking ATECC!"
self.idle()


def info(self, mode, param=None):
"""Returns device state information
:param int mode: Mode encoding, see Table 9-26.
Expand All @@ -252,7 +256,7 @@ def info(self, mode, param=None):
self._send_command(OP_INFO, mode)
else:
self._send_command(OP_INFO, mode, param)
time.sleep(EXEC_TIME[OP_INFO]/1000)
time.sleep(EXEC_TIME[OP_INFO] / 1000)
info_out = bytearray(4)
self._get_response(info_out)
self.idle()
Expand Down Expand Up @@ -281,15 +285,16 @@ def nonce(self, data, mode=0, zero=0x0000):
calculated_nonce = bytearray(1)
else:
raise RuntimeError("Invalid mode specified!")
time.sleep(EXEC_TIME[OP_NONCE]/1000)
time.sleep(EXEC_TIME[OP_NONCE] / 1000)
self._get_response(calculated_nonce)
time.sleep(1/1000)
time.sleep(1 / 1000)
if mode == 0x03:
assert calculated_nonce[0] == 0x00, "Incorrectly calculated nonce in pass-thru mode"
assert (
calculated_nonce[0] == 0x00
), "Incorrectly calculated nonce in pass-thru mode"
self.idle()
return calculated_nonce


def counter(self, counter=0, increment_counter=True):
"""Reads the binary count value from one of the two monotonic
counters located on the device within the configuration zone.
Expand All @@ -306,7 +311,7 @@ def counter(self, counter=0, increment_counter=True):
self._send_command(OP_COUNTER, 0x01, counter)
else:
self._send_command(OP_COUNTER, 0x00, counter)
time.sleep(EXEC_TIME[OP_COUNTER]/1000)
time.sleep(EXEC_TIME[OP_COUNTER] / 1000)
count = bytearray(4)
self._get_response(count)
self.idle()
Expand Down Expand Up @@ -342,7 +347,7 @@ def _random(self, data):
data_len = len(data)
while data_len:
self._send_command(OP_RANDOM, 0x00, 0x0000)
time.sleep(EXEC_TIME[OP_RANDOM]/1000)
time.sleep(EXEC_TIME[OP_RANDOM] / 1000)
resp = bytearray(32)
self._get_response(resp)
copy_len = min(32, data_len)
Expand All @@ -359,7 +364,7 @@ def sha_start(self):
"""
self.wakeup()
self._send_command(OP_SHA, 0x00)
time.sleep(EXEC_TIME[OP_SHA]/1000)
time.sleep(EXEC_TIME[OP_SHA] / 1000)
status = bytearray(1)
self._get_response(status)
assert status[0] == 0x00, "Error during sha_start."
Expand All @@ -374,14 +379,13 @@ def sha_update(self, message):
"""
self.wakeup()
self._send_command(OP_SHA, 0x01, 64, message)
time.sleep(EXEC_TIME[OP_SHA]/1000)
time.sleep(EXEC_TIME[OP_SHA] / 1000)
status = bytearray(1)
self._get_response(status)
assert status[0] == 0x00, "Error during SHA Update"
self.idle()
return status


def sha_digest(self, message=None):
"""Returns the digest of the data passed to the
sha_update method so far.
Expand All @@ -397,14 +401,13 @@ def sha_digest(self, message=None):
self._send_command(OP_SHA, 0x02, len(message), message)
else:
self._send_command(OP_SHA, 0x02)
time.sleep(EXEC_TIME[OP_SHA]/1000)
time.sleep(EXEC_TIME[OP_SHA] / 1000)
digest = bytearray(32)
self._get_response(digest)
assert len(digest) == 32, "SHA response length does not match expected length."
self.idle()
return digest


def gen_key(self, key, slot_num, private_key=False):
"""Generates a private or public key.
:param int slot_num: ECC slot (from 0 to 4).
Expand All @@ -417,7 +420,7 @@ def gen_key(self, key, slot_num, private_key=False):
self._send_command(OP_GEN_KEY, 0x04, slot_num)
else:
self._send_command(OP_GEN_KEY, 0x00, slot_num)
time.sleep(EXEC_TIME[OP_GEN_KEY]/1000)
time.sleep(EXEC_TIME[OP_GEN_KEY] / 1000)
self._get_response(key)
time.sleep(0.001)
self.idle()
Expand All @@ -442,7 +445,7 @@ def sign(self, slot_id):
"""
self.wakeup()
self._send_command(0x41, 0x80, slot_id)
time.sleep(EXEC_TIME[OP_SIGN]/1000)
time.sleep(EXEC_TIME[OP_SIGN] / 1000)
signature = bytearray(64)
self._get_response(signature)
self.idle()
Expand All @@ -457,7 +460,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()
Expand All @@ -466,7 +469,7 @@ def _write(self, zone, address, buffer):
if len(buffer) == 32:
zone |= 0x80
self._send_command(0x12, zone, address, buffer)
time.sleep(26/1000)
time.sleep(26 / 1000)
status = bytearray(1)
self._get_response(status)
self.idle()
Expand All @@ -483,26 +486,26 @@ def _read(self, zone, address, buffer):
time.sleep(0.001)
self.idle()

def _send_command(self, opcode, param_1, param_2=0x00, data=''):
def _send_command(self, opcode, param_1, param_2=0x00, data=""):
"""Sends a security command packet over i2c.
:param byte opcode: The command Opcode
:param byte param_1: The first parameter
:param byte param_2: The second parameter, can be two bytes.
:param byte param_3 data: Optional remaining input data.
"""
# assembling command packet
command_packet = bytearray(8+len(data))
command_packet = bytearray(8 + len(data))
# word address
command_packet[0] = 0x03
# i/o group: count
command_packet[1] = len(command_packet) - 1 # count
command_packet[1] = len(command_packet) - 1 # count
# security command packets
command_packet[2] = opcode
command_packet[3] = param_1
command_packet[4] = param_2 & 0xFF
command_packet[5] = param_2 >> 8
for i, cmd in enumerate(data):
command_packet[6+i] = cmd
command_packet[6 + i] = cmd
if self._debug:
print("Command Packet Sz: ", len(command_packet))
print("\tSending:", [hex(i) for i in command_packet])
Expand All @@ -517,12 +520,11 @@ def _send_command(self, opcode, param_1, param_2=0x00, data=''):
# small sleep
time.sleep(0.001)


def _get_response(self, buf, length=None, retries=20):
self.wakeup()
if length is None:
length = len(buf)
response = bytearray(length+3) # 1 byte header, 2 bytes CRC, len bytes data
response = bytearray(length + 3) # 1 byte header, 2 bytes CRC, len bytes data
with self._i2c_device as i2c:
for _ in range(retries):
try:
Expand All @@ -539,7 +541,7 @@ def _get_response(self, buf, length=None, retries=20):
if crc != crc2:
raise RuntimeError("CRC Mismatch")
for i in range(length):
buf[i] = response[i+1]
buf[i] = response[i + 1]
return response[1]

@staticmethod
Expand All @@ -553,7 +555,7 @@ def _at_crc(data, length=None):
for b in data:
for shift in range(8):
data_bit = 0
if b & (1<<shift):
if b & (1 << shift):
data_bit = 1
crc_bit = (crc >> 15) & 0x1
crc <<= 1
Expand Down
Loading