Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not device:
raise ValueError("device")
expected = b"\x1a:\xa5E\x04\x94S\xaf\xdf\x17\xe9\x89\xa4\x1f\xa0\x97\x94\xa5\x1b\xd5\xdb\x9167gU\x0c\x0f\n\xf3'\xd4"
message = b'\xBC' * ATCA_SHA256_BLOCK_SIZE
packet = device.atcab_sha(message)
assert expected in bytes(packet.response_data), hexlify(packet.response_data)
log.debug("atcab_sha: %s", hexlify(packet.response_data))
expected = b'p~\x97\xe6\xf8d]\xf5\xd8\x068.g\x01\xc8\xe2\xe2\x16`\x17\xf6\nV\xe6\xaa\xc0\xc2\xd2\xdb\xbb"\x81'
message = b'\x5A' * ATCA_SHA256_BLOCK_SIZE
packet = device.atcab_sha_base(SHA_MODE_SHA256_START)
packet = device.atcab_sha_base(SHA_MODE_SHA256_UPDATE, message)
packet = device.atcab_sha_base(SHA_MODE_SHA256_UPDATE, message)
packet = device.atcab_sha_base(SHA_MODE_SHA256_UPDATE, message)
packet = device.atcab_sha_base(SHA_MODE_SHA256_END)
assert expected in bytes(packet.response_data), hexlify(packet.response_data)
log.debug("atcab_sha_base: %s", hexlify(packet.response_data))
# test HW SHA with a long message > SHA block size and not an exact SHA block-size increment
expected = b'\xa9"\x18VCp\xa0W\'?\xf4\x85\xa8\x07?2\xfc\x1f\x14\x12\xec\xa2\xe3\x0b\x81\xa8\x87v\x0ba1r'
message = b'\xBC' * (ATCA_SHA256_BLOCK_SIZE + 63)
packet = device.atcab_sha(message)
assert expected in bytes(packet.response_data), hexlify(packet.response_data)
log.debug("atcab_sha %d: %s", len(message), hexlify(packet.response_data))
# test HW SHA with a short message < SHA block size and not an exact SHA block-size increment
expected = b'0?\xf8\xba@\xa2\x06\xe7\xa9P\x02\x1e\xf5\x10f\xd4\xa0\x01Tu2>\xe9\xf2J\xc8\xc9c)\x8f4\xce'
message = b'\xBC' * 10
packet = device.atcab_sha(message)
assert expected in bytes(packet.response_data), hexlify(packet.response_data)
log.debug("atcab_sha %d: %s", len(message), hexlify(packet.response_data))
def atcab_sha(self, data):
bs = ATCA_CONSTANTS.ATCA_SHA256_BLOCK_SIZE
d_mv = memoryview(data)
packet = self.atcab_sha_base(ATCA_CONSTANTS.SHA_MODE_SHA256_START)
chunks, rest = divmod(len(d_mv), bs)
for chunk in range(chunks):
m = ATCA_CONSTANTS.SHA_MODE_SHA256_UPDATE
b = d_mv[chunk:chunk + bs]
packet = self.atcab_sha_base(m, b)
m = ATCA_CONSTANTS.SHA_MODE_SHA256_END
b = d_mv[chunks * bs:chunks * bs + rest]
packet = self.atcab_sha_base(m, b)
return packet
def atcab_sha_base(self, mode=0, data=b'', key_slot=None):
if not isinstance(data, _BYTES_LIKE_OBJECT):
raise ATCA_EXCEPTIONS.BadArgumentError()
txsize = 0
cmd_mode = mode & ATCA_CONSTANTS.SHA_MODE_MASK
if cmd_mode in (
ATCA_CONSTANTS.SHA_MODE_SHA256_START,
ATCA_CONSTANTS.SHA_MODE_HMAC_START,
ATCA_CONSTANTS.SHA_MODE_SHA256_PUBLIC
):
txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN
elif cmd_mode in (
ATCA_CONSTANTS.SHA_MODE_SHA256_UPDATE,
ATCA_CONSTANTS.SHA_MODE_SHA256_END,
ATCA_CONSTANTS.SHA_MODE_HMAC_END
):
txsize = ATCA_CONSTANTS.ATCA_CMD_SIZE_MIN + len(data)
else:
raise ATCA_EXCEPTIONS.BadArgumentError()
packet = ATCAPacket(
txsize=txsize,
opcode=ATCA_CONSTANTS.ATCA_SHA,
param1=mode,
param2=len(data) if not isinstance(key_slot, int) else key_slot,
request_data=data
)
self.execute(packet)
return packet