diff --git a/.drone.yml b/.drone.yml index 4af58a8..db20cc3 100644 --- a/.drone.yml +++ b/.drone.yml @@ -4,7 +4,7 @@ name: default steps: - name: build-install-locally - image: maven:3-jdk-11 + image: maven:3-openjdk-17 commands: - mvn -B -DskipTests clean install - mvn test -B diff --git a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/BucketTreeHeader.java b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/BucketTreeHeader.java index 70d4504..6c32176 100644 --- a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/BucketTreeHeader.java +++ b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/BucketTreeHeader.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. @@ -18,26 +18,41 @@ */ package libKonogonka.Tools.NCA.NCASectionTableBlock; +import libKonogonka.Converter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.nio.charset.StandardCharsets; import java.util.Arrays; import static libKonogonka.Converter.getLEint; public class BucketTreeHeader { + private final static Logger log = LogManager.getLogger(BucketTreeHeader.class); + private final String magic; private final int version; private final int entryCount; private final byte[] unknown; BucketTreeHeader(byte[] rawBytes){ - magic = new String(Arrays.copyOfRange(rawBytes, 0x0, 0x4), StandardCharsets.US_ASCII); - version = getLEint(rawBytes, 0x4); - entryCount = getLEint(rawBytes, 0x8); - unknown = Arrays.copyOfRange(rawBytes, 0xc, 0x10); + this.magic = new String(Arrays.copyOfRange(rawBytes, 0x0, 0x4), StandardCharsets.US_ASCII); + this.version = getLEint(rawBytes, 0x4); + this.entryCount = getLEint(rawBytes, 0x8); + this.unknown = Arrays.copyOfRange(rawBytes, 0xc, 0x10); } public String getMagic() {return magic;} public int getVersion() {return version;} public int getEntryCount() {return entryCount;} public byte[] getUnknown() {return unknown;} + + public void printDebug(){ + log.debug("BucketTreeHeader\n" + + "Magic : " + magic + "\n" + + "Version : " + version + "\n" + + "EntryCount :" + entryCount + "\n" + + "Unknown :" + Converter.byteArrToHexStringAsLE(unknown) + "\n" + ); + } } diff --git a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/CompressionInfo.java b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/CompressionInfo.java index fcef6d1..9e1d69d 100644 --- a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/CompressionInfo.java +++ b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/CompressionInfo.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. @@ -18,11 +18,17 @@ */ package libKonogonka.Tools.NCA.NCASectionTableBlock; +import libKonogonka.Converter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.util.Arrays; import static libKonogonka.Converter.getLElong; public class CompressionInfo { + private final static Logger log = LogManager.getLogger(CompressionInfo.class); + private final long offset; private final long size; private final BucketTreeHeader bktr; @@ -42,4 +48,12 @@ public class CompressionInfo { public int getBktrEntryCount() { return bktr.getEntryCount(); } public byte[] getBktrUnknown() { return bktr.getUnknown(); } public byte[] getUnknown() {return unknown;} + + public void printDebug(){ + log.debug("CompressionInfo:\n" + + "Offset : " + offset + "\n" + + "Size : " + size + "\n"); + bktr.printDebug(); + log.debug("\nUnknown : " + Converter.byteArrToHexStringAsLE(unknown)); + } } diff --git a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/MetaDataHashDataInfo.java b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/MetaDataHashDataInfo.java index c97fc24..3e9156f 100644 --- a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/MetaDataHashDataInfo.java +++ b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/MetaDataHashDataInfo.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. @@ -18,21 +18,36 @@ */ package libKonogonka.Tools.NCA.NCASectionTableBlock; +import libKonogonka.Converter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.util.Arrays; import static libKonogonka.Converter.getLElong; public class MetaDataHashDataInfo { + private final static Logger log = LogManager.getLogger(MetaDataHashDataInfo.class); + private final long offset; private final long size; private final byte[] tableHash; + MetaDataHashDataInfo(byte[] rawTable){ - offset = getLElong(rawTable, 0); - size = getLElong(rawTable, 0x8); - tableHash = Arrays.copyOfRange(rawTable, 0x10, 0x20); + this.offset = getLElong(rawTable, 0); + this.size = getLElong(rawTable, 0x8); + this.tableHash = Arrays.copyOfRange(rawTable, 0x10, 0x20); } public long getOffset() {return offset;} public long getSize() {return size;} public byte[] getTableHash() {return tableHash;} + + public void printDebug(){ + log.debug("MetaDataHashDataInfo:\n" + + "Offset : " + offset + "\n" + + "Size : " + size + "\n" + + "Table Hash : " + Converter.byteArrToHexStringAsLE(tableHash) + "\n" + ); + } } diff --git a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/NcaFsHeader.java b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/NcaFsHeader.java index d0a8c2a..3419d66 100644 --- a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/NcaFsHeader.java +++ b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/NcaFsHeader.java @@ -290,6 +290,10 @@ public class NcaFsHeader { "Unknown End Padding : " + byteArrToHexStringAsLE(unknownEndPadding) + "\n" + "################################################################################################\n" ); + + sparseInfo.printDebug(); + compressionInfo.printDebug(); + metaDataHashDataInfo.printDebug(); } } diff --git a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/SparseInfo.java b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/SparseInfo.java index 39ce9c7..9b21470 100644 --- a/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/SparseInfo.java +++ b/src/main/java/libKonogonka/Tools/NCA/NCASectionTableBlock/SparseInfo.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. @@ -18,11 +18,17 @@ */ package libKonogonka.Tools.NCA.NCASectionTableBlock; +import libKonogonka.Converter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import java.util.Arrays; import static libKonogonka.Converter.getLElong; public class SparseInfo { + private final static Logger log = LogManager.getLogger(SparseInfo.class); + private final long offset; private final long size; private final BucketTreeHeader bktr; @@ -48,4 +54,15 @@ public class SparseInfo { public long getPhysicalOffset() {return physicalOffset;} public byte[] getGeneration() {return generation;} public byte[] getUnknown() {return unknown;} + + public void printDebug(){ + log.debug("SparseInfo:\n" + + "Offset : " + offset + "\n" + + "Size : " + size + "\n"); + bktr.printDebug(); + log.debug( + "\nPhysicalOffset : " + physicalOffset + "\n" + + "Generation : " + Converter.byteArrToHexStringAsLE(generation) + "\n" + + "Unknown : " + Converter.byteArrToHexStringAsLE(unknown) + "\n"); + } } \ No newline at end of file diff --git a/src/main/java/libKonogonka/Tools/NSO/NSO0Header.java b/src/main/java/libKonogonka/Tools/NSO/NSO0Header.java index 7c3d9f0..b0eef23 100644 --- a/src/main/java/libKonogonka/Tools/NSO/NSO0Header.java +++ b/src/main/java/libKonogonka/Tools/NSO/NSO0Header.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/NSO/NSO0Provider.java b/src/main/java/libKonogonka/Tools/NSO/NSO0Provider.java index 9c91779..cbb9567 100644 --- a/src/main/java/libKonogonka/Tools/NSO/NSO0Provider.java +++ b/src/main/java/libKonogonka/Tools/NSO/NSO0Provider.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/NSO/NSO0Raw.java b/src/main/java/libKonogonka/Tools/NSO/NSO0Raw.java index 97304e8..db7e42f 100644 --- a/src/main/java/libKonogonka/Tools/NSO/NSO0Raw.java +++ b/src/main/java/libKonogonka/Tools/NSO/NSO0Raw.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/NSO/NSO0Unpacker.java b/src/main/java/libKonogonka/Tools/NSO/NSO0Unpacker.java index 8d81cc7..7f0a872 100644 --- a/src/main/java/libKonogonka/Tools/NSO/NSO0Unpacker.java +++ b/src/main/java/libKonogonka/Tools/NSO/NSO0Unpacker.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/NSO/SegmentHeader.java b/src/main/java/libKonogonka/Tools/NSO/SegmentHeader.java index bb97e13..445cd68 100644 --- a/src/main/java/libKonogonka/Tools/NSO/SegmentHeader.java +++ b/src/main/java/libKonogonka/Tools/NSO/SegmentHeader.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/NSO/SegmentHeaderRelative.java b/src/main/java/libKonogonka/Tools/NSO/SegmentHeaderRelative.java index e4ce826..7eb28de 100644 --- a/src/main/java/libKonogonka/Tools/NSO/SegmentHeaderRelative.java +++ b/src/main/java/libKonogonka/Tools/NSO/SegmentHeaderRelative.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/PFS0/PFS0Header.java b/src/main/java/libKonogonka/Tools/PFS0/PFS0Header.java index b66e963..afe6846 100644 --- a/src/main/java/libKonogonka/Tools/PFS0/PFS0Header.java +++ b/src/main/java/libKonogonka/Tools/PFS0/PFS0Header.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/RomFs/RomFsConstruct.java b/src/main/java/libKonogonka/Tools/RomFs/RomFsConstruct.java index e9047bf..2b9da2f 100644 --- a/src/main/java/libKonogonka/Tools/RomFs/RomFsConstruct.java +++ b/src/main/java/libKonogonka/Tools/RomFs/RomFsConstruct.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/RomFs/view/FileSystemTreeViewMaker.java b/src/main/java/libKonogonka/Tools/RomFs/view/FileSystemTreeViewMaker.java index d70a34c..ce2c5e8 100644 --- a/src/main/java/libKonogonka/Tools/RomFs/view/FileSystemTreeViewMaker.java +++ b/src/main/java/libKonogonka/Tools/RomFs/view/FileSystemTreeViewMaker.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/main/java/libKonogonka/Tools/other/System2/System2Header.java b/src/main/java/libKonogonka/Tools/other/System2/System2Header.java new file mode 100644 index 0000000..76c8e64 --- /dev/null +++ b/src/main/java/libKonogonka/Tools/other/System2/System2Header.java @@ -0,0 +1,142 @@ +/* + Copyright 2019-2022 Dmitry Isaenko + + This file is part of libKonogonka. + + libKonogonka is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libKonogonka is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libKonogonka. If not, see . + */ +package libKonogonka.Tools.other.System2; + +import libKonogonka.Converter; +import libKonogonka.RainbowDump; +import libKonogonka.ctraes.AesCtrDecrypt; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class System2Header { + private final static Logger log = LogManager.getLogger(System2Header.class); + + private final byte[] headerCtr; + private byte[] section0Ctr; + private byte[] section1Ctr; + private byte[] section2Ctr; + private byte[] section3Ctr; + private String magic; + private int baseOffset; + private byte[] zeroOrReserved; // Or base offset always been int8? + private byte package2Version; + private byte bootloaderVersion; + private byte[] padding; + private int section0size; + private int section1size; + private int section2size; + private int section3size; + private int section0offset; + private int section1offset; + private int section2offset; + private int section3offset; + private byte[] sha256overEncryptedSection0; + private byte[] sha256overEncryptedSection1; + private byte[] sha256overEncryptedSection2; + private byte[] sha256overEncryptedSection3; + + private HashMap package2Keys; + private byte[] decodedHeaderBytes; + + public System2Header(byte[] headerBytes, HashMap keys) throws Exception{ + this.headerCtr = Arrays.copyOfRange(headerBytes, 0, 0x10); + collectKeys(keys); + decodeEncrypted(headerBytes); + buildHeader(); + } + private void collectKeys(HashMap keys){ + package2Keys = new HashMap<>(); + + for (String key: keys.keySet()){ + if (key.matches("package2_key_[0-f][0-f]")) + package2Keys.put(key, keys.get(key)); + } + } + private void decodeEncrypted(byte[] headerBytes) throws Exception{ + for (Map.Entry entry: package2Keys.entrySet()){ + AesCtrDecrypt decrypt = new AesCtrDecrypt(entry.getValue(), headerCtr, 0x100); + + decodedHeaderBytes = decrypt.decryptNext(headerBytes); + byte[] magicBytes = Arrays.copyOfRange(decodedHeaderBytes, 0x50, 0x54); + magic = new String(magicBytes, StandardCharsets.US_ASCII); + if (magic.equals("PK21")) { + log.debug("Header key used "+entry.getKey() + " = " + entry.getValue()); + return; + } + } + throw new Exception("Header hasn't been decoded. No appropriate package2_key_XX?"); + } + private void buildHeader(){ + section0Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x10, 0x20); + section1Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x20, 0x30); + section2Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x30, 0x40); + section3Ctr = Arrays.copyOfRange(decodedHeaderBytes, 0x40, 0x50); + + baseOffset = Converter.getLEint(decodedHeaderBytes, 0x54); + zeroOrReserved = Arrays.copyOfRange(decodedHeaderBytes, 0x58, 0x5c); + package2Version = decodedHeaderBytes[0x5c]; + bootloaderVersion = decodedHeaderBytes[0x5d]; + padding = Arrays.copyOfRange(decodedHeaderBytes, 0x5e, 0x60); + section0size = Converter.getLEint(decodedHeaderBytes, 0x60); + section1size = Converter.getLEint(decodedHeaderBytes, 0x64); + section2size = Converter.getLEint(decodedHeaderBytes, 0x68); + section3size = Converter.getLEint(decodedHeaderBytes, 0x6c); + section0offset = Converter.getLEint(decodedHeaderBytes, 0x70); + section1offset = Converter.getLEint(decodedHeaderBytes, 0x74); + section2offset = Converter.getLEint(decodedHeaderBytes, 0x78); + section3offset = Converter.getLEint(decodedHeaderBytes, 0x7c); + sha256overEncryptedSection0 = Arrays.copyOfRange(decodedHeaderBytes, 0x80, 0xa0); + sha256overEncryptedSection1 = Arrays.copyOfRange(decodedHeaderBytes, 0xa0, 0xc0); + sha256overEncryptedSection2 = Arrays.copyOfRange(decodedHeaderBytes, 0xc0, 0xe0); + sha256overEncryptedSection3 = Arrays.copyOfRange(decodedHeaderBytes, 0xe0, 0x100); + } + + public void printDebug(){ + log.debug("== System2 Header ==\n" + + "Header CTR : " + Converter.byteArrToHexStringAsLE(headerCtr) + "\n" + + "Section 0 CTR : " + Converter.byteArrToHexStringAsLE(section0Ctr) + "\n" + + "Section 1 CTR : " + Converter.byteArrToHexStringAsLE(section1Ctr) + "\n" + + "Section 2 CTR : " + Converter.byteArrToHexStringAsLE(section2Ctr) + "\n" + + "Section 3 CTR : " + Converter.byteArrToHexStringAsLE(section3Ctr) + "\n" + + "Magic PK21 : " + magic + "\n" + + "Offset : " + RainbowDump.formatDecHexString(baseOffset) + "\n" + + "Zero/reserved : " + Converter.byteArrToHexStringAsLE(zeroOrReserved) + "\n" + + "Package2 version : " + RainbowDump.formatDecHexString(package2Version) + "\n" + + "Bootloader version : " + RainbowDump.formatDecHexString(bootloaderVersion) + "\n" + + "Padding : " + Converter.byteArrToHexStringAsLE(padding) + "\n" + + "Section 0 size : " + RainbowDump.formatDecHexString(section0size) + "\n" + + "Section 1 size : " + RainbowDump.formatDecHexString(section1size) + "\n" + + "Section 2 size : " + RainbowDump.formatDecHexString(section2size) + "\n" + + "Section 3 size : " + RainbowDump.formatDecHexString(section3size) + "\n" + + "Section 0 offset : " + RainbowDump.formatDecHexString(section0offset) + "\n" + + "Section 1 offset : " + RainbowDump.formatDecHexString(section1offset) + "\n" + + "Section 2 offset : " + RainbowDump.formatDecHexString(section2offset) + "\n" + + "Section 3 offset : " + RainbowDump.formatDecHexString(section3offset) + "\n" + + "SHA256 ov.enc.sec 0 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection0) + "\n" + + "SHA256 ov.enc.sec 1 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection1) + "\n" + + "SHA256 ov.enc.sec 2 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection2) + "\n" + + "SHA256 ov.enc.sec 3 : " + Converter.byteArrToHexStringAsLE(sha256overEncryptedSection3) + "\n" + ); + } +} diff --git a/src/main/java/libKonogonka/Tools/other/System2/System2Provider.java b/src/main/java/libKonogonka/Tools/other/System2/System2Provider.java new file mode 100644 index 0000000..571f3c2 --- /dev/null +++ b/src/main/java/libKonogonka/Tools/other/System2/System2Provider.java @@ -0,0 +1,56 @@ +/* + Copyright 2019-2022 Dmitry Isaenko + + This file is part of libKonogonka. + + libKonogonka is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libKonogonka is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libKonogonka. If not, see . + */ +package libKonogonka.Tools.other.System2; + +import libKonogonka.KeyChainHolder; + +import java.io.BufferedInputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class System2Provider { + private byte[] Rsa2048signature; + private System2Header header; + // ... + + private final String pathToFile; + private final KeyChainHolder keyChainHolder; + + public System2Provider(String pathToFile, KeyChainHolder keyChainHolder) throws Exception{ + this.pathToFile = pathToFile; + this.keyChainHolder = keyChainHolder; + + readHeaderCtr(); + } + + private void readHeaderCtr() throws Exception{ + try (BufferedInputStream stream = new BufferedInputStream(Files.newInputStream(Paths.get(pathToFile)))){ + if (0x100 != stream.skip(0x100)) + throw new Exception("Can't skip RSA-2048 signature offset (0x100)"); + byte[] headerBytes = new byte[0x100]; + if (0x100 != stream.read(headerBytes)) + throw new Exception("System2 header is too small"); + this.header = new System2Header(headerBytes, keyChainHolder.getRawKeySet()); + } + } + + public System2Header getHeader() { + return header; + } +} diff --git a/src/main/java/libKonogonka/ctraes/AesCtr.java b/src/main/java/libKonogonka/ctraes/AesCtr.java index 67657c3..b020ec9 100644 --- a/src/main/java/libKonogonka/ctraes/AesCtr.java +++ b/src/main/java/libKonogonka/ctraes/AesCtr.java @@ -34,8 +34,8 @@ public class AesCtr { BCinitialized = true; } - private Cipher cipher; - private SecretKeySpec key; + private final Cipher cipher; + private final SecretKeySpec key; public AesCtr(byte[] keyArray) throws Exception{ if ( ! BCinitialized) diff --git a/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java b/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java index aabe078..2837dd5 100644 --- a/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java +++ b/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. @@ -18,7 +18,6 @@ */ package libKonogonka.ctraes; -import libKonogonka.Converter; import libKonogonka.RainbowDump; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/src/main/java/libKonogonka/ctraes/AesCtrDecrypt.java b/src/main/java/libKonogonka/ctraes/AesCtrDecrypt.java new file mode 100644 index 0000000..771cde1 --- /dev/null +++ b/src/main/java/libKonogonka/ctraes/AesCtrDecrypt.java @@ -0,0 +1,69 @@ +/* + Copyright 2019-2022 Dmitry Isaenko + + This file is part of libKonogonka. + + libKonogonka is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libKonogonka is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libKonogonka. If not, see . +*/ +package libKonogonka.ctraes; + +/** + * Simplify decryption of the CTR + */ +public class AesCtrDecrypt { + + private long realMediaOffset; + private byte[] IVarray; + private AesCtr aesCtr; + + private final byte[] initialKey; + private final byte[] initialSectionCTR; + private final long initialRealMediaOffset; + + public AesCtrDecrypt(String key, byte[] sectionCTR, long realMediaOffset) throws Exception{ + this.initialKey = hexStrToByteArray(key); + this.initialSectionCTR = sectionCTR; + this.initialRealMediaOffset = realMediaOffset; + reset(); + } + + public void skipNext(){ + realMediaOffset += 0x200; + } + + public void skipNext(long blocksNum){ + realMediaOffset += blocksNum * 0x200; + } + + public byte[] decryptNext(byte[] encryptedBlock) throws Exception{ + byte[] decryptedBlock = aesCtr.decrypt(encryptedBlock, IVarray); + realMediaOffset += 0x200; + return decryptedBlock; + } + + public void reset() throws Exception{ + realMediaOffset = initialRealMediaOffset; + aesCtr = new AesCtr(initialKey); + IVarray = initialSectionCTR;//Converter.flip(); + } + private byte[] hexStrToByteArray(String s) { + int len = s.length(); + byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + + Character.digit(s.charAt(i+1), 16)); + } + return data; + } +} diff --git a/src/main/java/libKonogonka/ctraes/AesCtrDecryptSimple.java b/src/main/java/libKonogonka/ctraes/AesCtrDecryptSimple.java index dbd7bee..1547b41 100644 --- a/src/main/java/libKonogonka/ctraes/AesCtrDecryptSimple.java +++ b/src/main/java/libKonogonka/ctraes/AesCtrDecryptSimple.java @@ -19,10 +19,9 @@ package libKonogonka.ctraes; import libKonogonka.Converter; -import libKonogonka.RainbowDump; /** - * Simplify decryption of the CTR + * Simplify decryption of the CTR for NCA's AesCtr sections */ public class AesCtrDecryptSimple { diff --git a/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java b/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java index 4ab892b..c91241b 100644 --- a/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java +++ b/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/ExportNso0FromNcaTest.java b/src/test/java/libKonogonka/RomFsDecrypted/ExportNso0FromNcaTest.java index e8dff26..080f7ee 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/ExportNso0FromNcaTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/ExportNso0FromNcaTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/KeyChainHolderTest.java b/src/test/java/libKonogonka/RomFsDecrypted/KeyChainHolderTest.java index 7805299..97cb3dc 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/KeyChainHolderTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/KeyChainHolderTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/NCAProviderSimpleTest.java b/src/test/java/libKonogonka/RomFsDecrypted/NCAProviderSimpleTest.java index 8d92ec7..e765a71 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/NCAProviderSimpleTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/NCAProviderSimpleTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/NSODecompressTest.java b/src/test/java/libKonogonka/RomFsDecrypted/NSODecompressTest.java index 6704653..16b3931 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/NSODecompressTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/NSODecompressTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/NSOTest.java b/src/test/java/libKonogonka/RomFsDecrypted/NSOTest.java index b6e2a6f..05395d1 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/NSOTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/NSOTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/NSPpfs0EncryptedTest.java b/src/test/java/libKonogonka/RomFsDecrypted/NSPpfs0EncryptedTest.java index 1f436b1..8d34dc5 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/NSPpfs0EncryptedTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/NSPpfs0EncryptedTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/PFS0Test.java b/src/test/java/libKonogonka/RomFsDecrypted/PFS0Test.java index 13b554c..1f4d3a6 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/PFS0Test.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/PFS0Test.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/Pfs0EncryptedTest.java b/src/test/java/libKonogonka/RomFsDecrypted/Pfs0EncryptedTest.java index 510bb5f..4931616 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/Pfs0EncryptedTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/Pfs0EncryptedTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/RomFsDecryptedTest.java b/src/test/java/libKonogonka/RomFsDecrypted/RomFsDecryptedTest.java index 0bb9307..e0d4e7f 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/RomFsDecryptedTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/RomFsDecryptedTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/RomFsEncryptedTest.java b/src/test/java/libKonogonka/RomFsDecrypted/RomFsEncryptedTest.java index fe2b2ca..ac7dd06 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/RomFsEncryptedTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/RomFsEncryptedTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka. diff --git a/src/test/java/libKonogonka/RomFsDecrypted/XciTest.java b/src/test/java/libKonogonka/RomFsDecrypted/XciTest.java index 1a82d56..90fad08 100644 --- a/src/test/java/libKonogonka/RomFsDecrypted/XciTest.java +++ b/src/test/java/libKonogonka/RomFsDecrypted/XciTest.java @@ -1,5 +1,5 @@ /* - Copyright 2018-2022 Dmitry Isaenko + Copyright 2019-2022 Dmitry Isaenko This file is part of libKonogonka.