Skip to content

Commit c121fe6

Browse files
author
simonmkwii
committed
Add files
1 parent 5c61135 commit c121fe6

27 files changed

Lines changed: 2932 additions & 0 deletions

NCABuilder.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27428.2027
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NCABuilder", "NCABuilder\NCABuilder.csproj", "{56B6D80D-05D1-4A7D-9BBB-16EF69894837}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{56B6D80D-05D1-4A7D-9BBB-16EF69894837}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{56B6D80D-05D1-4A7D-9BBB-16EF69894837}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{56B6D80D-05D1-4A7D-9BBB-16EF69894837}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{56B6D80D-05D1-4A7D-9BBB-16EF69894837}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {9AA3B6F5-CF56-4D17-A182-4807DD727BC9}
24+
EndGlobalSection
25+
EndGlobal

NCABuilder/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

NCABuilder/CNMTConstructor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.IO;
2+
3+
namespace NCABuilder
4+
{
5+
internal class CNMTConstructor
6+
{
7+
public static byte[] CNMTHead(ulong TitleID, uint Version, byte Type, short NumOfContentEntries, uint MinFirmVer)
8+
{
9+
var Final = new BinaryWriter(new MemoryStream());
10+
Final.Write(TitleID);
11+
Final.Write(Version);
12+
Final.Write(0);
13+
Final.Write(Type);
14+
Final.Write((byte)0);
15+
Final.Write((short)0x10);
16+
Final.Write(NumOfContentEntries);
17+
Final.Write(Utils.Pad(0xE));
18+
Final.Dispose();
19+
return new MemoryStream().ToArray();
20+
}
21+
}
22+
}

NCABuilder/Crypto/AES-CTR.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System.Collections.Generic;
2+
using System.Security.Cryptography;
3+
4+
public class Aes128CounterMode : SymmetricAlgorithm
5+
{
6+
private readonly byte[] Counter;
7+
8+
private readonly AesManaged AESCtr; public Aes128CounterMode(byte[] CTR)
9+
{
10+
AESCtr = new AesManaged
11+
{
12+
Mode = CipherMode.ECB,
13+
Padding = PaddingMode.None
14+
};
15+
Counter = CTR;
16+
}
17+
18+
public override ICryptoTransform CreateEncryptor(byte[] Key, byte[] Input)
19+
{
20+
return new CryptoTransform(AESCtr, Key, Counter);
21+
}
22+
23+
public override ICryptoTransform CreateDecryptor(byte[] Key, byte[] Input)
24+
{
25+
return new CryptoTransform(AESCtr, Key, Counter);
26+
}
27+
28+
public override void GenerateKey()
29+
{
30+
AESCtr.GenerateKey();
31+
}
32+
33+
public override void GenerateIV()
34+
{
35+
}
36+
}
37+
38+
public class CryptoTransform : ICryptoTransform
39+
{
40+
private readonly byte[] Counter;
41+
private readonly ICryptoTransform Transform;
42+
private readonly Queue<byte> QueuedBytes = new Queue<byte>();
43+
private readonly SymmetricAlgorithm Algorithm;
44+
45+
public CryptoTransform(SymmetricAlgorithm Alg, byte[] Key, byte[] Counter)
46+
{
47+
Algorithm = Alg;
48+
this.Counter = Counter;
49+
var Block = new byte[Algorithm.BlockSize / 8];
50+
Transform = Alg.CreateEncryptor(Key, Block);
51+
}
52+
53+
public byte[] TransformFinalBlock(byte[] InputData, int StartOffset, int Length)
54+
{
55+
var EncryptedData = new byte[Length];
56+
TransformBlock(InputData, StartOffset, Length, EncryptedData, 0);
57+
return EncryptedData;
58+
}
59+
60+
public int TransformBlock(byte[] InputData, int StartOffset, int Length, byte[] Output, int OutOffset)
61+
{
62+
for (var i = 0; i < Length; i++)
63+
{
64+
if (Null()) CounterTransform(); var TransformBytes = QueuedBytes.Dequeue();
65+
Output[OutOffset + i] = (byte)(InputData[StartOffset + i] ^ TransformBytes);
66+
}
67+
return Length;
68+
}
69+
70+
private bool Null()
71+
{
72+
return QueuedBytes.Count == 0;
73+
}
74+
75+
private void CounterTransform()
76+
{
77+
var Output = new byte[Algorithm.BlockSize / 8];
78+
Transform.TransformBlock(Counter, 0, Counter.Length, Output, 0);
79+
Countup();
80+
foreach (var Byte in Output)
81+
{
82+
QueuedBytes.Enqueue(Byte);
83+
}
84+
}
85+
86+
private void Countup()
87+
{
88+
for (var i = Counter.Length - 1; i >= 0; i--)
89+
{
90+
if (++Counter[i] != 0) break;
91+
}
92+
}
93+
94+
public int InputBlockSize
95+
{
96+
get
97+
{
98+
return Algorithm.BlockSize / 8;
99+
}
100+
}
101+
102+
public int OutputBlockSize
103+
{
104+
get
105+
{
106+
return Algorithm.BlockSize / 8;
107+
}
108+
}
109+
110+
public bool CanTransformMultipleBlocks
111+
{
112+
get
113+
{
114+
return true;
115+
}
116+
}
117+
118+
public bool CanReuseTransform
119+
{
120+
get
121+
{
122+
return false;
123+
}
124+
}
125+
126+
public void Dispose()
127+
{
128+
}
129+
}

0 commit comments

Comments
 (0)