mirror of
https://github.com/NinjaCheetah/libWiiPy.git
synced 2025-04-26 13:21:01 -04:00
Improved comments, moved TID to IV conversion into a function in shared.py
This commit is contained in:
parent
ba320a29de
commit
98666285db
@ -1,9 +1,8 @@
|
|||||||
# "crypto.py" from libWiiPy by NinjaCheetah & Contributors
|
# "crypto.py" from libWiiPy by NinjaCheetah & Contributors
|
||||||
# https://github.com/NinjaCheetah/libWiiPy
|
# https://github.com/NinjaCheetah/libWiiPy
|
||||||
import binascii
|
|
||||||
|
|
||||||
import struct
|
import struct
|
||||||
from .commonkeys import get_common_key
|
from .commonkeys import get_common_key
|
||||||
|
from .shared import convert_tid_to_iv
|
||||||
|
|
||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
|
|
||||||
@ -31,18 +30,7 @@ def decrypt_title_key(title_key_enc: bytes, common_key_index: int, title_id: byt
|
|||||||
# Load the correct common key for the title.
|
# Load the correct common key for the title.
|
||||||
common_key = get_common_key(common_key_index)
|
common_key = get_common_key(common_key_index)
|
||||||
# Convert the IV into the correct format based on the type provided.
|
# Convert the IV into the correct format based on the type provided.
|
||||||
title_key_iv = b''
|
title_key_iv = convert_tid_to_iv(title_id)
|
||||||
if type(title_id) is bytes:
|
|
||||||
if len(title_id) == 16:
|
|
||||||
title_key_iv = binascii.unhexlify(title_id)
|
|
||||||
elif len(title_id) == 8:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise ValueError("Title ID is not valid!")
|
|
||||||
elif type(title_id) is str:
|
|
||||||
title_key_iv = binascii.unhexlify(title_id)
|
|
||||||
else:
|
|
||||||
raise TypeError("Title ID is not valid! It must be either type str or bytes.")
|
|
||||||
# The IV will always be in the same format by this point, so add the last 8 bytes.
|
# The IV will always be in the same format by this point, so add the last 8 bytes.
|
||||||
title_key_iv = title_key_iv + (b'\x00' * 8)
|
title_key_iv = title_key_iv + (b'\x00' * 8)
|
||||||
# Create a new AES object with the values provided.
|
# Create a new AES object with the values provided.
|
||||||
@ -75,18 +63,7 @@ def encrypt_title_key(title_key_dec: bytes, common_key_index: int, title_id: byt
|
|||||||
# Load the correct common key for the title.
|
# Load the correct common key for the title.
|
||||||
common_key = get_common_key(common_key_index)
|
common_key = get_common_key(common_key_index)
|
||||||
# Convert the IV into the correct format based on the type provided.
|
# Convert the IV into the correct format based on the type provided.
|
||||||
title_key_iv = b''
|
title_key_iv = convert_tid_to_iv(title_id)
|
||||||
if type(title_id) is bytes:
|
|
||||||
if len(title_id) == 16:
|
|
||||||
title_key_iv = binascii.unhexlify(title_id)
|
|
||||||
elif len(title_id) == 8:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise ValueError("Title ID is not valid!")
|
|
||||||
elif type(title_id) is str:
|
|
||||||
title_key_iv = binascii.unhexlify(title_id)
|
|
||||||
else:
|
|
||||||
raise TypeError("Title ID is not valid! It must be either type str or bytes.")
|
|
||||||
# The IV will always be in the same format by this point, so add the last 8 bytes.
|
# The IV will always be in the same format by this point, so add the last 8 bytes.
|
||||||
title_key_iv = title_key_iv + (b'\x00' * 8)
|
title_key_iv = title_key_iv + (b'\x00' * 8)
|
||||||
# Create a new AES object with the values provided.
|
# Create a new AES object with the values provided.
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
# This file defines general functions that may be useful in other modules of libWiiPy. Putting them here cuts down on
|
# This file defines general functions that may be useful in other modules of libWiiPy. Putting them here cuts down on
|
||||||
# clutter in other files.
|
# clutter in other files.
|
||||||
|
|
||||||
|
import binascii
|
||||||
|
|
||||||
def align_value(value, alignment=64) -> int:
|
def align_value(value, alignment=64) -> int:
|
||||||
"""
|
"""
|
||||||
Aligns the provided value to the set alignment (defaults to 64).
|
Aligns the provided value to the set alignment (defaults to 64).
|
||||||
@ -45,3 +47,24 @@ def pad_bytes_stream(data, alignment=64) -> bytes:
|
|||||||
while (data.getbuffer().nbytes % alignment) != 0:
|
while (data.getbuffer().nbytes % alignment) != 0:
|
||||||
data.write(b'\x00')
|
data.write(b'\x00')
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def convert_tid_to_iv(title_id: str) -> bytes:
|
||||||
|
title_key_iv = b''
|
||||||
|
if type(title_id) is bytes:
|
||||||
|
# This catches the format b'0000000100000002'
|
||||||
|
if len(title_id) == 16:
|
||||||
|
title_key_iv = binascii.unhexlify(title_id)
|
||||||
|
# This catches the format b'\x00\x00\x00\x01\x00\x00\x00\x02'
|
||||||
|
elif len(title_id) == 8:
|
||||||
|
pass
|
||||||
|
# If it isn't one of those lengths, it cannot possibly be valid, so reject it.
|
||||||
|
else:
|
||||||
|
raise ValueError("Title ID is not valid!")
|
||||||
|
# Allow for a string like "0000000100000002"
|
||||||
|
elif type(title_id) is str:
|
||||||
|
title_key_iv = binascii.unhexlify(title_id)
|
||||||
|
# If the Title ID isn't bytes or a string, it isn't valid and is rejected.
|
||||||
|
else:
|
||||||
|
raise TypeError("Title ID type is not valid! It must be either type str or bytes.")
|
||||||
|
return title_key_iv
|
||||||
|
Loading…
x
Reference in New Issue
Block a user