pyctr.crypto.engine module
Provides various tools to perform cryptographic operations with Nintendo 3DS data.
- exception pyctr.crypto.engine.CryptoError[source]
Bases:
PyCTRErrorGeneric exception for cryptography operations.
- exception pyctr.crypto.engine.OTPLengthError[source]
Bases:
CryptoErrorOTP is the wrong length.
- exception pyctr.crypto.engine.CorruptBootromError[source]
Bases:
CryptoErrorARM9 bootROM hash does not match.
- exception pyctr.crypto.engine.KeyslotMissingError[source]
Bases:
CryptoErrorNormal key is not set up for the keyslot.
- exception pyctr.crypto.engine.TicketLengthError(length)[source]
Bases:
CryptoErrorTicket is too small.
- exception pyctr.crypto.engine.BootromNotFoundError[source]
Bases:
CryptoErrorARM9 bootROM was not found. Main argument is a tuple of checked paths.
- exception pyctr.crypto.engine.CorruptOTPError[source]
Bases:
CryptoErrorOTP hash does not match.
- class pyctr.crypto.engine.Keyslot(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnumAES engine keyslots used by the Nintendo 3DS. Values above 0x3F (63) are used by PyCTR, and do not exist on the actual hardware. Each value explains what the keyslot is used to decrypt or encrypt.
- TWLNAND = 3
Entire TWL region, including twln, twlp, and the header.
- CTRNANDOld = 4
CTRNAND for Old Nintendo 3DS.
- CTRNANDNew = 5
CTRNAND for New Nintendo 3DS.
- FIRM = 6
FIRM partitions.
- AGB = 7
AGBSAVE partition if a GBA VC title was played.
- CMACNANDDB = 11
CMAC for NAND dbs.
- NCCH93 = 24
NCCH extra keyslot for titles exclusive to New Nintendo 3DS released after System Menu 9.3.0-21.
- CMACCardSaveNew = 25
- CardSaveNew = 26
- NCCH96 = 27
NCCH extra keyslot for titles exclusive to New Nintendo 3DS released after System Menu 9.6.0-24.
- CMACAGB = 36
CMAC for the AGBSAVE partition contents.
- NCCH70 = 37
NCCH extra keyslot for titles released after System Menu 7.0.0-13.
- NCCH = 44
NCCH original keyslot.
- UDSLocalWLAN = 45
- StreetPass = 46
- Save60 = 47
Save key for retail games released after System Menu 6.0.0-11.
- CMACSDNAND = 48
- CMACCardSave = 51
- SD = 52
SD card contents under “Nintendo 3DS”.
- CardSave = 55
- BOSS = 56
Used to encrypt SpotPass data.
- DownloadPlay = 57
- DSiWareExport = 58
Used when exporting DSiWare to the SD card.
- CommonKey = 61
Titlekeys in tickets.
- Boot9Internal = 63
Used for internal operations in the ARM9 BootROM, including decrypting OTP, FIRM sections from non-NAND sources, and generating console-unique keys.
- DecryptedTitlekey = 64
CIA and CDN contents.
- ZeroKey = 65
All zero key for NCCH titles using fixed crypto.
- FixedSystemKey = 66
Special key for NCCH system titles using fixed crypto.
- New3DSKeySector = 67
Used to decrypt the secret key sector (sector 0x96) for the New Nintendo 3DS.
- NCCHExtraKey = 68
Stores a version of another keyslot used for NCCH titles. For titles without a seed, KeyY is taken from the NCCH header. For titles with a seed, KeyY is seeded. KeyX is always the same as the source keyslot.
- class pyctr.crypto.engine.CryptoEngine(boot9=None, dev=False, setup_b9_keys=True)[source]
Bases:
objectEmulates the AES engine of the Nintendo 3DS, including keyslots and the key scrambler.
- Parameters:
- b9_path: str | None
Path that the ARM9 BootROM was loaded from. Set when
setup_keys_from_boot9_file()is called, which is automatically called on object creation if setup_b9_keys was True.
- property id0: bytes
ID0 generated from a
movable.sed. One must be loaded first withsetup_sd_key()orsetup_sd_key_from_file().
- create_ctr_cipher(keyslot, ctr)[source]
Create an AES-CTR cipher with the given keyslot.
Normal and DSi crypto will be automatically chosen depending on keyslot.
- create_ctr_io(keyslot, fh, ctr, closefd=False)[source]
Create an AES-CTR read-write file object with the given keyslot.
- create_cbc_io(keyslot, fh, iv, closefd=False)[source]
Create an AES-CBC read-only file object with the given keyslot.
- static sd_path_to_iv(path)[source]
Generate an IV from an SD file path relevant to the root of an ID1 directory (e.g. /title/00040000/0f70c600/content/00000000.app). Both Unix- and Windows-style paths are accepted.
- load_encrypted_titlekey(titlekey, common_key_index, title_id)[source]
Decrypt an encrypted titlekey and store in keyslot 0x40 (
Keyslot.DecryptedTitlekey).
- load_from_ticket(ticket)[source]
Load a titlekey from a ticket and set keyslot 0x40 to the decrypted titlekey.
- Parameters:
ticket (bytes) –
- static keygen_twl_manual(key_x, key_y)[source]
Generate a normal key using the DSi AES key scrambler.
- setup_keys_from_boot9(b9)[source]
Set up certain keys from an ARM9 bootROM dump.
- Parameters:
b9 (bytes) –
- setup_keys_from_boot9_file(path=None)[source]
Set up certain keys from an ARM9 bootROM file.
- Parameters:
path (FilePath) –
- setup_keys_from_otp(otp)[source]
Set up console-unique keys from an OTP dump. Encrypted and decrypted are supported.
- Parameters:
otp (bytes) – OTP data, encrypted or decrypted.
- setup_keys_from_otp_file(path)[source]
Set up console-unique keys from an OTP file. Encrypted and decrypted are supported.
- Parameters:
path (FilePath) –
- class pyctr.crypto.engine.CTRFileIO(file, crypto, keyslot, counter, closefd=False)[source]
Bases:
_CryptoFileBaseProvides transparent read-write AES-CTR encryption as a file-like object.
- Parameters:
file (BinaryIO) –
crypto (CryptoEngine) –
keyslot (Keyslot) –
counter (int) –
closefd (bool) –
- seek(seek, whence=0)[source]
Change stream position.
Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence. Values for whence are:
0 – start of stream (the default); offset should be zero or positive
1 – current stream position; offset may be negative
2 – end of stream; offset is usually negative
Return the new absolute position.
- class pyctr.crypto.engine.TWLCTRFileIO(file, crypto, keyslot, counter, closefd=False)[source]
Bases:
CTRFileIOProvides transparent read-write TWL AES-CTR encryption as a file-like object.
- Parameters:
file (BinaryIO) –
crypto (CryptoEngine) –
keyslot (Keyslot) –
counter (int) –
closefd (bool) –
- class pyctr.crypto.engine.CBCFileIO(file, crypto, keyslot, iv, closefd=False)[source]
Bases:
_CryptoFileBaseProvides transparent read-only AES-CBC encryption as a file-like object.
- Parameters:
file (BinaryIO) –
crypto (CryptoEngine) –
keyslot (Keyslot) –
iv (bytes) –
closefd (bool) –
- seek(seek, whence=0)[source]
Change stream position.
Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence. Values for whence are:
0 – start of stream (the default); offset should be zero or positive
1 – current stream position; offset may be negative
2 – end of stream; offset is usually negative
Return the new absolute position.