Skip to content
Open
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
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: check-format check-format-diff format lint

check-format:
poetry run black --check --color .
poetry run isort --check --color .

check-format-diff:
poetry run black --check --diff --color .
poetry run isort --check --diff --color .

format:
poetry run black --color .
poetry run isort --color .

lint:
poetry run pylint bluetooth_mesh tests --exit-zero --output-format=colorized
4 changes: 2 additions & 2 deletions bluetooth_mesh/network/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def k2(N, P):

k = (T1 + T2 + T3)[-33:]

n, e, p = bitstring.BitString(k).unpack("pad:1, uint:7, bits:128, bits:128")
n, e, p = bitstring.BitStream(k).unpack("pad:1, uint:7, bits:128, bits:128")

return n, e.bytes, p.bytes

Expand All @@ -89,7 +89,7 @@ def k4(N):

k = aes_cmac(T, b"id6\x01")[-1:]

(aid,) = bitstring.BitString(k).unpack("pad:2, uint:6")
(aid,) = bitstring.BitStream(k).unpack("pad:2, uint:6")

return aid

Expand Down
26 changes: 13 additions & 13 deletions bluetooth_mesh/network/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import bitstring
from construct import ValidationError
from crc.crc import Configuration, CrcCalculator
from crc import Configuration, Calculator

from bluetooth_mesh.network.crypto import (
ApplicationKey,
Expand Down Expand Up @@ -71,7 +71,7 @@ def __str__(self):

@classmethod
def unpack(cls, beacon):
uuid, oob, uri_hash = bitstring.BitString(beacon).unpack(cls.BEACON_FORMAT)
uuid, oob, uri_hash = bitstring.BitStream(beacon).unpack(cls.BEACON_FORMAT)

if uri_hash and len(uri_hash) != 4:
raise ValueError("Wrong size of URI hash, expected 4 bytes")
Expand Down Expand Up @@ -111,7 +111,7 @@ def unpack(cls, message):
message[-cls.BEACON_AUTH_SIZE :],
)

iv_update, key_refresh, network_id, iv_index = bitstring.BitString(
iv_update, key_refresh, network_id, iv_index = bitstring.BitStream(
beacon
).unpack(cls.BEACON_FORMAT)

Expand Down Expand Up @@ -165,7 +165,7 @@ def unpack(cls, message, private_beacon_key):

private_beacon_data = bytes(map(operator.xor, s[:5], obfuscated_private_beacon_data))

iv_update, key_refresh, iv_index = bitstring.BitString(
iv_update, key_refresh, iv_index = bitstring.BitStream(
private_beacon_data
).unpack(cls.BEACON_FORMAT)

Expand Down Expand Up @@ -350,7 +350,7 @@ def segments(self, application_key, seq, iv_index, szmic=False, seg=False):

@classmethod
def decrypt(cls, app_key, iv_index, ctl, ttl, seq, src, dst, transport_pdu):
seg, akf, aid = bitstring.BitString(transport_pdu).unpack(
seg, akf, aid = bitstring.BitStream(transport_pdu).unpack(
"uint:1, uint:1, uint:6"
)

Expand Down Expand Up @@ -389,7 +389,7 @@ def segments(self, application_key, seq, iv_index, szmic=False, seg=False):

@classmethod
def decrypt(cls, ttl, src, dst, transport_pdu):
seg, opcode = bitstring.BitString(transport_pdu).unpack("uint:1, uint:7")
seg, opcode = bitstring.BitStream(transport_pdu).unpack("uint:1, uint:7")

# works only for unsegmented messages!
if seg:
Expand Down Expand Up @@ -424,7 +424,7 @@ def __init__(self, src, dst=0):
self.opcode = bytes()

def get_opcode(self, application_key):
return bitstring.BitString()
return bitstring.BitStream()

def segments(self, application_key, seq, iv_index, szmic=False, seg=False):
yield bytes()
Expand Down Expand Up @@ -531,7 +531,7 @@ def unpack(
):
# pylint: disable=R0914
_nid, encryption_key, privacy_key = net_key.encryption_keys
last_iv, nid, obfuscated_header, encoded_data_mic = bitstring.BitString(
last_iv, nid, obfuscated_header, encoded_data_mic = bitstring.BitStream(
network_pdu
).unpack("uint:1, uint:7, bytes:6, bytes")
if nid != _nid:
Expand All @@ -545,7 +545,7 @@ def unpack(

pecb = aes_ecb(privacy_key, privacy_random)[:6]
deobfuscated = bytes(map(operator.xor, obfuscated_header, pecb))
ctl, ttl, seq, src = bitstring.BitString(deobfuscated).unpack(
ctl, ttl, seq, src = bitstring.BitStream(deobfuscated).unpack(
"uint:1, uint:7, uintbe:24, uintbe:16"
)
net_mic_len = 8 if ctl else 4
Expand All @@ -557,7 +557,7 @@ def unpack(
encryption_key, nonce, encoded_data_mic, tag_length=net_mic_len
)

dst, transport_pdu = bitstring.BitString(decrypted_net).unpack(
dst, transport_pdu = bitstring.BitStream(decrypted_net).unpack(
"uintbe:16, bytes"
)

Expand All @@ -582,7 +582,7 @@ def unpack(
reverse_output=True,
)

CRC_CALCULATOR = CrcCalculator(configuration=MESH_CRC)
CRC_CALCULATOR = Calculator(configuration=MESH_CRC)


class ProvisioningTransaction:
Expand All @@ -598,7 +598,7 @@ def pack(pdu):
segments += [pdu[0 + i : 23 + i] for i in range(20, len(pdu), 23)]

total_len = len(pdu)
fcs = CRC_CALCULATOR.calculate_checksum(pdu)
fcs = CRC_CALCULATOR.checksum(pdu)

yield GenericProvisioning.build(
dict(
Expand Down Expand Up @@ -650,7 +650,7 @@ def key(segment):

pdu = start.data + b"".join(continuation.data for continuation in continuations)

fcs = CRC_CALCULATOR.calculate_checksum(pdu)
fcs = CRC_CALCULATOR.checksum(pdu)
if start.total_length != len(pdu) or fcs != start.frame_check:
raise ValidationError(
f"Transaction checksum is invalid, expected {start.frame_check:02x}, got {fcs:02x}",
Expand Down
Loading