feat: code quality (#39)

This commit is contained in:
dni ⚡ 2024-08-29 12:24:15 +02:00 committed by GitHub
commit 7213d5ca93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 2902 additions and 159 deletions

View file

@ -1,5 +1,4 @@
# https://www.nxp.com/docs/en/application-note/AN12196.pdf
from typing import Tuple
from Cryptodome.Cipher import AES
from Cryptodome.Hash import CMAC
@ -7,30 +6,30 @@ from Cryptodome.Hash import CMAC
SV2 = "3CC300010080"
def myCMAC(key: bytes, msg: bytes = b"") -> bytes:
def my_cmac(key: bytes, msg: bytes = b"") -> bytes:
cobj = CMAC.new(key, ciphermod=AES)
if msg != b"":
cobj.update(msg)
return cobj.digest()
def decryptSUN(sun: bytes, key: bytes) -> Tuple[bytes, bytes]:
IVbytes = b"\x00" * 16
def decrypt_sun(sun: bytes, key: bytes) -> tuple[bytes, bytes]:
ivbytes = b"\x00" * 16
cipher = AES.new(key, AES.MODE_CBC, IVbytes)
cipher = AES.new(key, AES.MODE_CBC, ivbytes)
sun_plain = cipher.decrypt(sun)
UID = sun_plain[1:8]
uid = sun_plain[1:8]
counter = sun_plain[8:11]
return UID, counter
return uid, counter
def getSunMAC(UID: bytes, counter: bytes, key: bytes) -> bytes:
def get_sun_mac(uid: bytes, counter: bytes, key: bytes) -> bytes:
sv2prefix = bytes.fromhex(SV2)
sv2bytes = sv2prefix + UID + counter
sv2bytes = sv2prefix + uid + counter
mac1 = myCMAC(key, sv2bytes)
mac2 = myCMAC(mac1)
mac1 = my_cmac(key, sv2bytes)
mac2 = my_cmac(mac1)
return mac2[1::2]