mirror of
https://github.com/espressif/openthread.git
synced 2026-08-29 21:39:54 +00:00
[style] update python style to conform to PEP 8 (#3951)
With the exception of line length set to 119 vs. 79. Add tests/ and tools/ to py-pretty-check.
This commit is contained in:
@@ -63,7 +63,8 @@ class CryptoEngine:
|
||||
tuple: Encrypted message (bytearray), MIC (bytearray)
|
||||
|
||||
"""
|
||||
key, nonce, auth_data = self._crypto_material_creator.create_key_and_nonce_and_authenticated_data(message_info)
|
||||
key, nonce, auth_data = self._crypto_material_creator.create_key_and_nonce_and_authenticated_data(
|
||||
message_info)
|
||||
|
||||
cipher = AES.new(key, AES.MODE_CCM, nonce, mac_len=self.mic_length)
|
||||
cipher.update(auth_data)
|
||||
@@ -82,7 +83,8 @@ class CryptoEngine:
|
||||
bytearray: Decrypted message.
|
||||
|
||||
"""
|
||||
key, nonce, auth_data = self._crypto_material_creator.create_key_and_nonce_and_authenticated_data(message_info)
|
||||
key, nonce, auth_data = self._crypto_material_creator.create_key_and_nonce_and_authenticated_data(
|
||||
message_info)
|
||||
|
||||
cipher = AES.new(key, AES.MODE_CCM, nonce, mac_len=self.mic_length)
|
||||
cipher.update(auth_data)
|
||||
@@ -132,7 +134,6 @@ class CryptoMaterialCreator(object):
|
||||
|
||||
|
||||
class MacCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
|
||||
def __init__(self, master_key):
|
||||
"""
|
||||
Args:
|
||||
@@ -157,7 +158,9 @@ class MacCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
"""
|
||||
return bytes(eui64 + struct.pack(">LB", frame_counter, security_level))
|
||||
|
||||
def _create_authenticated_data(self, mhr, auxiliary_security_header, nonpayload_fields):
|
||||
def _create_authenticated_data(
|
||||
self, mhr, auxiliary_security_header, nonpayload_fields
|
||||
):
|
||||
""" Create Authenticated Data
|
||||
|
||||
Read more: 7.6.3.3 CCM prerequisites - Std 802.15.4-2006
|
||||
@@ -174,15 +177,21 @@ class MacCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
return bytes(mhr + auxiliary_security_header + nonpayload_fields)
|
||||
|
||||
def create_key_and_nonce_and_authenticated_data(self, message_info):
|
||||
_, mac_key = self._generate_keys(message_info.aux_sec_hdr.sequence_counter)
|
||||
_, mac_key = self._generate_keys(
|
||||
message_info.aux_sec_hdr.sequence_counter
|
||||
)
|
||||
|
||||
nonce = self._create_nonce(message_info.source_mac_address,
|
||||
message_info.aux_sec_hdr.frame_counter,
|
||||
message_info.aux_sec_hdr.security_level)
|
||||
nonce = self._create_nonce(
|
||||
message_info.source_mac_address,
|
||||
message_info.aux_sec_hdr.frame_counter,
|
||||
message_info.aux_sec_hdr.security_level,
|
||||
)
|
||||
|
||||
auth_data = self._create_authenticated_data(message_info.mhr_bytes,
|
||||
message_info.aux_sec_hdr_bytes,
|
||||
message_info.nonpayload_fields)
|
||||
auth_data = self._create_authenticated_data(
|
||||
message_info.mhr_bytes,
|
||||
message_info.aux_sec_hdr_bytes,
|
||||
message_info.nonpayload_fields,
|
||||
)
|
||||
|
||||
return mac_key, nonce, auth_data
|
||||
|
||||
@@ -192,7 +201,6 @@ class MacCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
|
||||
|
||||
class MleCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
|
||||
def __init__(self, master_key):
|
||||
"""
|
||||
Args:
|
||||
@@ -215,9 +223,14 @@ class MleCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
bytes: created Nonce
|
||||
|
||||
"""
|
||||
return bytes(source_eui64[:8] + struct.pack(">LB", frame_counter, security_level))
|
||||
return bytes(
|
||||
source_eui64[:8]
|
||||
+ struct.pack(">LB", frame_counter, security_level)
|
||||
)
|
||||
|
||||
def _create_authenticated_data(self, source_address, destination_address, auxiliary_security_header):
|
||||
def _create_authenticated_data(
|
||||
self, source_address, destination_address, auxiliary_security_header
|
||||
):
|
||||
""" Create Authenticated Data
|
||||
|
||||
Read more: 4.8 - Thread v1.0 Specification
|
||||
@@ -231,18 +244,28 @@ class MleCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
bytes: Authenticated Data
|
||||
|
||||
"""
|
||||
return bytes(source_address.packed + destination_address.packed + auxiliary_security_header)
|
||||
return bytes(
|
||||
source_address.packed
|
||||
+ destination_address.packed
|
||||
+ auxiliary_security_header
|
||||
)
|
||||
|
||||
def create_key_and_nonce_and_authenticated_data(self, message_info):
|
||||
mle_key, _ = self._generate_keys(message_info.aux_sec_hdr.sequence_counter)
|
||||
mle_key, _ = self._generate_keys(
|
||||
message_info.aux_sec_hdr.sequence_counter
|
||||
)
|
||||
|
||||
nonce = self._create_nonce(message_info.source_mac_address.mac_address,
|
||||
message_info.aux_sec_hdr.frame_counter,
|
||||
message_info.aux_sec_hdr.security_level)
|
||||
nonce = self._create_nonce(
|
||||
message_info.source_mac_address.mac_address,
|
||||
message_info.aux_sec_hdr.frame_counter,
|
||||
message_info.aux_sec_hdr.security_level,
|
||||
)
|
||||
|
||||
auth_data = self._create_authenticated_data(message_info.source_ipv6,
|
||||
message_info.destination_ipv6,
|
||||
message_info.aux_sec_hdr_bytes)
|
||||
auth_data = self._create_authenticated_data(
|
||||
message_info.source_ipv6,
|
||||
message_info.destination_ipv6,
|
||||
message_info.aux_sec_hdr_bytes,
|
||||
)
|
||||
|
||||
return mle_key, nonce, auth_data
|
||||
|
||||
@@ -252,8 +275,14 @@ class MleCryptoMaterialCreator(CryptoMaterialCreator):
|
||||
|
||||
|
||||
class AuxiliarySecurityHeader:
|
||||
|
||||
def __init__(self, key_id_mode, security_level, frame_counter, key_id, big_endian=True):
|
||||
def __init__(
|
||||
self,
|
||||
key_id_mode,
|
||||
security_level,
|
||||
frame_counter,
|
||||
key_id,
|
||||
big_endian=True,
|
||||
):
|
||||
"""
|
||||
Args:
|
||||
key_id_mode (int)
|
||||
@@ -275,14 +304,18 @@ class AuxiliarySecurityHeader:
|
||||
key_source = self.key_id[:8]
|
||||
format = ">Q" if self._big_endian else "<Q"
|
||||
if self.key_id_mode == 1:
|
||||
# Try to guess valid Key Sequence Counter based on Key Index. This one should work for now.
|
||||
# Try to guess valid Key Sequence Counter based on Key Index. This
|
||||
# one should work for now.
|
||||
return self.key_index - 1
|
||||
elif self.key_id_mode == 2:
|
||||
# In this mode sequence counter is stored on the first four bytes of Key ID.
|
||||
# In this mode sequence counter is stored on the first four bytes
|
||||
# of Key ID.
|
||||
key_source = self.key_id[:4]
|
||||
format = ">I" if self._big_endian else "<I"
|
||||
else:
|
||||
raise ValueError("Unsupported Key Index Mode: {}".format(self.key_id_mode))
|
||||
raise ValueError(
|
||||
"Unsupported Key Index Mode: {}".format(self.key_id_mode)
|
||||
)
|
||||
|
||||
return struct.unpack(format, key_source)[0]
|
||||
|
||||
@@ -308,7 +341,7 @@ class AuxiliarySecurityHeader:
|
||||
|
||||
def __repr__(self):
|
||||
return "AuxiliarySecurityHeader(key_id_mode={}, security_level={}, frame_counter={}, key_id={})".format(
|
||||
self.key_id_mode, self.security_level, self.frame_counter, hexlify(self.key_id))
|
||||
self.key_id_mode, self.security_level, self.frame_counter, hexlify(self.key_id), )
|
||||
|
||||
|
||||
class AuxiliarySecurityHeaderFactory:
|
||||
@@ -325,11 +358,11 @@ class AuxiliarySecurityHeaderFactory:
|
||||
0: _KEY_ID_LENGTH_KEY_ID_0,
|
||||
1: _KEY_ID_LENGTH_KEY_ID_1,
|
||||
2: _KEY_ID_LENGTH_KEY_ID_2,
|
||||
3: _KEY_ID_LENGTH_KEY_ID_3
|
||||
3: _KEY_ID_LENGTH_KEY_ID_3,
|
||||
}
|
||||
|
||||
def _parse_security_control(self, security_control_byte):
|
||||
security_level = (security_control_byte & 0x07)
|
||||
security_level = security_control_byte & 0x07
|
||||
key_id_mode = (security_control_byte >> 3) & 0x03
|
||||
|
||||
return security_level, key_id_mode
|
||||
@@ -341,18 +374,26 @@ class AuxiliarySecurityHeaderFactory:
|
||||
return self._key_id_lengths[key_id_mode]
|
||||
|
||||
def parse(self, data, message_info):
|
||||
security_control_bytes = bytearray(data.read(self._SECURITY_CONTROL_LENGTH))
|
||||
security_control_bytes = bytearray(
|
||||
data.read(self._SECURITY_CONTROL_LENGTH)
|
||||
)
|
||||
frame_counter_bytes = bytearray(data.read(self._FRAME_COUNTER_LENGTH))
|
||||
|
||||
security_level, key_id_mode = self._parse_security_control(security_control_bytes[0])
|
||||
security_level, key_id_mode = self._parse_security_control(
|
||||
security_control_bytes[0]
|
||||
)
|
||||
frame_counter = self._parse_frame_counter(frame_counter_bytes)
|
||||
|
||||
key_id_length = self._key_id_length(key_id_mode)
|
||||
key_id_bytes = bytearray(data.read(key_id_length))
|
||||
|
||||
aux_sec_hdr = AuxiliarySecurityHeader(key_id_mode, security_level, frame_counter, key_id_bytes)
|
||||
aux_sec_hdr = AuxiliarySecurityHeader(
|
||||
key_id_mode, security_level, frame_counter, key_id_bytes
|
||||
)
|
||||
|
||||
message_info.aux_sec_hdr_bytes = security_control_bytes + frame_counter_bytes + key_id_bytes
|
||||
message_info.aux_sec_hdr_bytes = (
|
||||
security_control_bytes + frame_counter_bytes + key_id_bytes
|
||||
)
|
||||
message_info.aux_sec_hdr = aux_sec_hdr
|
||||
|
||||
return aux_sec_hdr
|
||||
|
||||
Reference in New Issue
Block a user