From 717ff59ebb368ea41a479e19eb7a9fa2d2f7bcba Mon Sep 17 00:00:00 2001 From: N2891 Date: Mon, 8 Aug 2011 03:00:09 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9A=97=E5=8F=B7=E5=8C=96=E3=83=BB=E5=BE=A9?= =?UTF-8?q?=E5=8F=B7=E5=8C=96=E3=83=A2=E3=82=B8=E3=83=A5=E3=83=BC=E3=83=AB?= =?UTF-8?q?=EF=BC=88TWLBackupBlock=EF=BC=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_test_tools@4 6b0af911-cb57-b745-895f-eec5701120e1 --- .../TWLBackupBlock/TWLBackupBlock.sln | 20 + .../TWLBackupBlock.sln.VisualState.xml | 13 + .../TWLBackupBlock/TWLBackupBlock.suo | Bin 0 -> 59904 bytes .../TWLBackupBlock/AbstractBody.cs | 70 +++ .../TWLBackupBlock/TWLBackupBlock/AesCmac.cs | 170 ++++++ .../TWLBackupBlock/TWLBackupBlock/Block.cs | 53 ++ .../TWLBackupBlock/TWLBackupBlock/Blocks.cs | 88 ++++ .../TWLBackupBlock/TWLBackupBlock/Body.cs | 81 +++ .../TWLBackupBlock/ExtBinaryReader.cs | 138 +++++ .../TWLBackupBlock/HeaderBody.cs | 260 +++++++++ .../TWLBackupBlock/Properties/AssemblyInfo.cs | 36 ++ .../Properties/Resources.Designer.cs | 63 +++ .../TWLBackupBlock/Properties/Resources.resx | 101 ++++ .../Properties/Settings.Designer.cs | 26 + .../Properties/Settings.settings | 6 + .../TWLBackupBlock/SignatureBody.cs | 192 +++++++ .../TWLBackupBlock/TWLBackupBlock.csproj.user | 1 + .../TWLBackupBlock/TwlBackupBlock.csproj | 93 ++++ .../TWLBackupBlock/TWLBackupBlock/Utility.cs | 496 ++++++++++++++++++ 19 files changed, 1907 insertions(+) create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln.VisualState.xml create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.suo create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AbstractBody.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AesCmac.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Block.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Blocks.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Body.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/ExtBinaryReader.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/HeaderBody.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/AssemblyInfo.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.Designer.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.resx create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.Designer.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.settings create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/SignatureBody.cs create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TWLBackupBlock.csproj.user create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TwlBackupBlock.csproj create mode 100644 TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Utility.cs diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln new file mode 100644 index 0000000..864a6c6 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual C# Express 2008 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwlBackupBlock", "TWLBackupBlock\TwlBackupBlock.csproj", "{E62F6169-868A-48E4-9B1D-C2EB3BE11F39}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E62F6169-868A-48E4-9B1D-C2EB3BE11F39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E62F6169-868A-48E4-9B1D-C2EB3BE11F39}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E62F6169-868A-48E4-9B1D-C2EB3BE11F39}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E62F6169-868A-48E4-9B1D-C2EB3BE11F39}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln.VisualState.xml b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln.VisualState.xml new file mode 100644 index 0000000..5758c9a --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.sln.VisualState.xml @@ -0,0 +1,13 @@ + + + [0-1000]C:\Users\N2891\Documents\Visual Studio 2008\Projects\TWLBackupBlock\TWLBackupBlock.sln + [0-1004]TwlBackupBlockTest.AesCmacTest.GetMacTest + false + + + + + + + + \ No newline at end of file diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.suo b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock.suo new file mode 100644 index 0000000000000000000000000000000000000000..a37f9f0affb007911004d67045e53fc1fd3a38ec GIT binary patch literal 59904 zcmeHQdvILUdB2i*n2?lb2;r$12W*hndaSLL4F*e=u!&zF*@m(dR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TO`5y2gB|N4TD` zZR;=UquJrmU$k{p^~|HKFzA;};eQvE|-qs1!H|EXI3Di2aZJj{9W2;QxmfohJ)-ir; zDRp{!|3iWNKXpVPSSar)E*Zn~lD>xCGN0(n59WV~Umom3WF5?GwTI8)>HTI3efdlC i{f{rA>{kPnAH^i!FVO!KztTcasX^P%UVYrm`2PVgA(~PE literal 0 HcmV?d00001 diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AbstractBody.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AbstractBody.cs new file mode 100644 index 0000000..3979d5d --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AbstractBody.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace TwlBackupBlock +{ + /// + /// データ本体を表す抽象クラスです。 + /// + public abstract class AbstractBody : IEnumerable, ICloneable + { + /// + /// 指定したインデックスにアクセスします。 + /// + /// アクセスするインデックス。 + /// 指定したインデックスのバイトデータ。 + public abstract byte this[int index] + { + get; + set; + } + + /// + /// データ本体のバイトサイズを取得します。 + /// + public abstract int Length + { + get; + } + + /// + /// イテレータブロック。 + /// + /// 列挙子。 + public IEnumerator GetEnumerator() + { + for (int i = 0; i < Length; i++) + { + yield return this[i]; + } + } + + /// + /// イテレータブロック。 + /// + /// 列挙子。 + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// データ本体を表すバイト配列を取得します。 + /// + /// データ本体を表すバイト配列 + public abstract byte[] GetBytes(); + + /// + /// バイト配列からデータ本体を構成します。 + /// + /// データ本体を表すバイト配列 + public abstract void SetBytes(byte[] bytes); + + /// + /// AbstractBodyのディープコピーを作成します。 + /// + /// AbstractBodyのコピー + public abstract object Clone(); + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AesCmac.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AesCmac.cs new file mode 100644 index 0000000..42ce0a5 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/AesCmac.cs @@ -0,0 +1,170 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Security.Cryptography; + +namespace TwlBackupBlock +{ + /// + /// AES-CMACによるMACを生成します。 + /// 鍵は16バイト、24バイト、32バイトに対応しています。 + /// + public class AesCmac + { + private const int BLOCK_SIZE = 16; + private static readonly int[] LEGAL_KEY_SIZES = { 16, 24, 32 }; + + /// + /// 指定された鍵を利用してデータのMACを取得します。 + /// + /// MACを発行したいデータ。 + /// AES-CMACの鍵。 + /// に対するMAC。 + public static byte[] GetMac(byte[] data, byte[] key) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + if (key == null) + { + throw new ArgumentNullException("key"); + } + if (!CheckKeySize(key.Length)) + { + string message = string.Format("key.Length is illegal (key.Length:{0})", key.Length); + throw new ArgumentException("key", message); + } + + var context = new AesCmacContext(block => Encrypt(block, key)); + + context.Update(data); + + return context.GetMac(); + } + + /// + /// 指定された鍵を利用して16バイトブロックをAESで暗号化します。 + /// + /// 暗号化するブロック。メソッド呼出し後は、暗号化されたブロック。 + /// AESの鍵。 + private static void Encrypt(byte[] block, byte[] key) + { + Debug.Assert(block.Length == BLOCK_SIZE); + Debug.Assert(CheckKeySize(key.Length)); + + using (var ms = new MemoryStream(block)) + { + var aes = new RijndaelManaged(); + aes.BlockSize = BLOCK_SIZE * 8; + aes.Mode = CipherMode.ECB; + aes.Key = key; + using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Read)) + { + cs.Read(block, 0, block.Length); + } + } + } + + /// + /// 鍵のサイズが16バイト、24バイト、32バイトのいずれであるか判定します。 + /// + /// 鍵のバイトサイズ + /// + private static bool CheckKeySize(int keySize) + { + return LEGAL_KEY_SIZES.Any(e => (e == keySize)); + } + + /// + /// AES-CMAC生成器です。データの入力履歴に応じたMAC生成器の内部状態を保持します。 + /// + private class AesCmacContext + { + public delegate void Encrypt(byte[] block); + + private Encrypt encryptOperation; + private byte[] block; + private int filledIndex; + + /// + /// 指定された暗号化処理をもとに、AesCmacContextクラスの新しいインスタンスを初期化します。 + /// + /// 16バイトブロックを暗号化するメソッド + public AesCmacContext(Encrypt op) + { + encryptOperation = op; + block = Enumerable.Repeat(0, BLOCK_SIZE).ToArray(); + filledIndex = 0; + } + + /// + /// 指定された入力データによりMAC生成器を更新します。 + /// + /// MACを発行したいデータ + public void Update(byte[] data) + { + for (int i = 0; i < data.Length; ) + { + if (filledIndex >= BLOCK_SIZE) + { + encryptOperation(block); + filledIndex = 0; + } + while (filledIndex < BLOCK_SIZE && i < data.Length) + { + block[filledIndex] ^= data[i]; + ++filledIndex; + ++i; + } + } + } + + /// + /// 現在までの入力履歴からMACを生成します。 + /// + /// これまでの入力データに対するMAC + public byte[] GetMac() + { + byte[] padding = Enumerable.Repeat(0, BLOCK_SIZE).ToArray(); + encryptOperation(padding); + Multiply128(padding); + + if (filledIndex < BLOCK_SIZE) + { + block[filledIndex] ^= 0x80; + Multiply128(padding); + } + + for (int i = 0; i < BLOCK_SIZE; ++i) + { + padding[i] ^= block[i]; + } + + encryptOperation(padding); + + return padding; + } + + /// + /// 16バイトの整数に128を乗じます。オーバーフローは無視します。 + /// + /// 16バイト整数 + private void Multiply128(byte[] block) + { + bool isCarried = ((block[0] & 0x80) != 0); + + for (int i = 0; i < block.Length - 1; i++) + { + block[i] = (byte)(((uint)block[i] << 1) | ((uint)block[i + 1] >> 7)); + } + block[block.Length - 1] <<= 1; + if (isCarried) + { + block[block.Length - 1] = (byte)(block[block.Length - 1] ^ 0x87); + } + } + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Block.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Block.cs new file mode 100644 index 0000000..37a0ea4 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Block.cs @@ -0,0 +1,53 @@ +using System; +using System.Diagnostics; + +namespace TwlBackupBlock +{ + /// + /// データブロックです。 + /// + public class Block : ICloneable + { + private const int MAC_SIZE = 16; + private const int IV_SIZE = 16; + + public AbstractBody body; + public byte[] mac; + public byte[] iv; + + /// + /// Blockクラスの新しいインスタンスを初期化します。 + /// + public Block() + { + body = null; + mac = null; + iv = null; + } + + /// + /// 指定したデータ本体、MAC、IVを使用して、Blockクラスの新しいインスタンスを初期化します。 + /// + /// データ本体。 + /// MAC。 + /// IV。 + public Block(AbstractBody body, byte[] mac, byte[] iv) + { + Debug.Assert(mac.Length == MAC_SIZE); + Debug.Assert(iv.Length == IV_SIZE); + + this.body = body; + this.mac = mac; + this.iv = iv; + } + + /// + /// Blockのディープコピーを作成します。 + /// + /// + public object Clone() + { + return new Block((AbstractBody)body.Clone(), (byte[])mac.Clone(), (byte[])iv.Clone()); + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Blocks.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Blocks.cs new file mode 100644 index 0000000..d217dd3 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Blocks.cs @@ -0,0 +1,88 @@ +using System; +using System.Diagnostics; + +namespace TwlBackupBlock +{ + /// + /// ブロックの集合です。 + /// + public class Blocks : ICloneable + { + private const int NUM_BLOCKS = 7; + + public Block banner; + public Block header; + public Block signature; + public Block tmd; + public Block content; + public Block saveData; + public Block subBanner; + + /// + /// Blocksクラスの新しいインスタンスを初期化します。 + /// + public Blocks() + { + banner = new Block(); + header = new Block(); + signature = new Block(); + tmd = new Block(); + content = new Block(); + saveData = new Block(); + subBanner = new Block(); + } + + /// + /// 指定したインデックスのブロックにアクセスします。 + /// + /// インデックス。 + /// 指定したインデックスのブロック。 + public Block this[int index] + { + get + { + switch (index) + { + case 0: return banner; + case 1: return header; + case 2: return signature; + case 3: return tmd; + case 4: return content; + case 5: return saveData; + case 6: return subBanner; + default: + Debug.Assert(false); + return null; // never reach + } + } + } + + /// + /// ブロック数(=7)を取得します。 + /// + public int Length + { + get + { + return NUM_BLOCKS; + } + } + + /// + /// Blocksのディープコピーを作成します。 + /// + /// Blocksのコピー + public object Clone() + { + Blocks newBlocks = new Blocks(); + newBlocks.banner = (Block)banner.Clone(); + newBlocks.header = (Block)header.Clone(); + newBlocks.signature = (Block)signature.Clone(); + newBlocks.tmd = (Block)tmd.Clone(); + newBlocks.content = (Block)content.Clone(); + newBlocks.saveData = (Block)saveData.Clone(); + newBlocks.subBanner = (Block)subBanner.Clone(); + return newBlocks; + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Body.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Body.cs new file mode 100644 index 0000000..8ffa98c --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Body.cs @@ -0,0 +1,81 @@ +using System; +using System.Diagnostics; + +namespace TwlBackupBlock +{ + /// + /// バイト単位でアクセスする機能だけを持ったデータ本体です。 + /// + public class Body : AbstractBody + { + private byte[] data; + + /// + /// 指定したデータ本体をもとに、Bodyクラスの新しいインスタンスを初期化します。 + /// + /// + public Body(byte[] data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + this.data = (byte[])data.Clone(); + } + + public override byte this[int index] + { + get + { + if (index < 0 || index >= data.Length) + { + throw new ArgumentOutOfRangeException("index"); + } + return data[index]; + } + set + { + if (index < 0 || index >= data.Length) + { + throw new ArgumentOutOfRangeException("index"); + } + data[index] = value; + } + } + + public override int Length + { + get + { + Debug.Assert(data != null); + return data.Length; + } + } + + public override byte[] GetBytes() + { + Debug.Assert(data != null); + return (byte[])data.Clone(); + } + + public override void SetBytes(byte[] bytes) + { + Debug.Assert(data != null); + if (bytes == null) + { + throw new ArgumentNullException("bytes"); + } + if (bytes.Length != data.Length) + { + string message = string.Format("bytes.Length != {0} (bytes.Length:{1})", data.Length, bytes.Length); + throw new ArgumentException("bytes", message); + } + bytes.CopyTo(data, 0); + } + + public override object Clone() + { + return new Body(GetBytes()); + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/ExtBinaryReader.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/ExtBinaryReader.cs new file mode 100644 index 0000000..5c66a91 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/ExtBinaryReader.cs @@ -0,0 +1,138 @@ +using System; +using System.IO; + +namespace TwlBackupBlock +{ + /// + /// BinaryReaderのReadをラップするクラスです。 + /// + public class ExtBinaryReader : IDisposable + { + private bool disposed; + private BinaryReader br; + + /// + /// 指定したMemoryStreamをもとに、ExtBinaryReaderクラスの新しいインスタンスを初期化します。 + /// + /// + public ExtBinaryReader(MemoryStream ms) + { + disposed = false; + br = new BinaryReader(ms); + } + + ~ExtBinaryReader() + { + Dispose(false); + } + + /// + /// ExtBinaryReaderを破棄します。 + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// ExtBinaryReaderを破棄します。 + /// + /// BinaryReaderも破棄する場合はtrue。それ以外の場合はfalse。 + protected virtual void Dispose(bool disposing) + { + if (!disposed) + { + if (disposing) + { + ((IDisposable)br).Dispose(); + } + } + disposed = true; + } + + /// + /// 現在のストリームから次のバイトを読み取り、ストリームの現在位置を1バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref Byte value) + { + value = br.ReadByte(); + } + + /// + /// 現在のストリームから2バイト符号なし整数を読み取り、ストリームの現在位置を2バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref UInt16 value) + { + value = br.ReadUInt16(); + } + + /// + /// 現在のストリームから4バイト符号なし整数を読み取り、ストリームの現在位置を4バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref UInt32 value) + { + value = br.ReadUInt32(); + } + + /// + /// 現在のストリームから8バイト符号なし整数を読み取り、ストリームの現在位置を8バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref UInt64 value) + { + value = br.ReadUInt64(); + } + + /// + /// 現在のストリームから2バイト符号付き整数を読み取り、ストリームの現在位置を2バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref Int16 value) + { + value = br.ReadInt16(); + } + + /// + /// 現在のストリームから4バイト符号付き整数を読み取り、ストリームの現在位置を4バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref Int32 value) + { + value = br.ReadInt32(); + } + + /// + /// 現在のストリームから8バイト符号付き整数を読み取り、ストリームの現在位置を8バイトだけ進めます。 + /// + /// データを読み取るバッファ + public void Read(ref Int64 value) + { + value = br.ReadInt64(); + } + + /// + /// 現在のストリームから指定した配列にバイトを読み取り、ストリームの現在位置を指定した配列のバイト数だけ進めます。 + /// + /// データを読み取るバッファ + public void Read(Byte[] buffer) + { + br.Read(buffer, 0, buffer.Length); + } + + /// + /// 現在のストリームから指定した配列に4バイト符号なし整数を読み取り、ストリームの現在位置を指定した配列のバイト数だけ進めます。 + /// + /// データを読み取るバッファ + public void Read(UInt32[] buffer) + { + for (int i = 0; i < buffer.Length; i++) + { + Read(ref buffer[i]); + } + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/HeaderBody.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/HeaderBody.cs new file mode 100644 index 0000000..a84680e --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/HeaderBody.cs @@ -0,0 +1,260 @@ +using System; +using System.IO; +using System.Diagnostics; + +namespace TwlBackupBlock +{ + /// + /// ヘッダブロックのデータ本体。 + /// + public class HeaderBody : AbstractBody + { + public const int HEADER_SIZE = 160; + + public const int UNIQUE_ID_SIZE = 32; + public const int HARDWARE_ID_SIZE = 16; + public const int NUM_OF_SECTION = 4; + public const int TMD_RESERVED_SIZE = 62; + public const int RESERVED_SIZE = 4; + + public UInt32 signature; + public UInt16 companyCode; + public UInt16 version; + public readonly Byte[] uniqueId; + public readonly Byte[] hardwareId; + public UInt64 titleId; + public Int64 requiredSize; + public readonly UInt32[] fileSizes; + public UInt32 contentId; + public readonly TmdReserved tmdReserved; + public UInt16 contentIndex; + public readonly Byte[] reserved; + + public HeaderBody(byte[] data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + if (data.Length != HEADER_SIZE) + { + string message = string.Format("data.Length % {0} != 0 (data.Length:{1})", HEADER_SIZE, data.Length); + throw new ArgumentException("data", message); + } + + uniqueId = new Byte[UNIQUE_ID_SIZE]; + hardwareId = new Byte[HARDWARE_ID_SIZE]; + fileSizes = new UInt32[NUM_OF_SECTION]; + tmdReserved = new TmdReserved(); + reserved = new Byte[RESERVED_SIZE]; + + SetBytes(data); + } + + public override byte this[int index] + { + get + { + if (index < 0 || index >= HEADER_SIZE) + { + throw new ArgumentException("index"); + } + return ReadByte(index); + } + set + { + if (index < 0 || index >= HEADER_SIZE) + { + throw new ArgumentException("index"); + } + WriteByte(index, value); + } + } + + public override int Length + { + get + { + return HEADER_SIZE; + } + } + + /// + /// 指定したバイトを取得します。 + /// + /// 取得したいバイトのインデックス。 + /// で指定したバイト + public byte ReadByte(int index) + { + if (index < 0 || index >= HEADER_SIZE) + { + throw new ArgumentException("index"); + } + // TODO 処理が遅ければ効率化 + byte[] bytes = GetBytes(); + return bytes[index]; + } + + /// + /// 指定したバイトを書き換えます。 + /// + /// 書き換えたいバイトのインデックス。 + /// 上書きに使うバイト。 + public void WriteByte(int index, byte b) + { + if (index < 0 || index >= HEADER_SIZE) + { + throw new ArgumentException("index"); + } + // TODO 処理が遅ければ効率化 + byte[] bytes = GetBytes(); + bytes[index] = b; + SetBytes(bytes); + } + + public override byte[] GetBytes() + { + byte[] bytes = new byte[HEADER_SIZE]; + + using (var ms = new MemoryStream(bytes)) + using (var bw = new BinaryWriter(ms)) + { + bw.Write(signature); + bw.Write(companyCode); + bw.Write(version); + bw.Write(uniqueId); + bw.Write(hardwareId); + bw.Write(titleId); + bw.Write(requiredSize); + foreach (UInt32 e in fileSizes) + { + bw.Write(e); + } + bw.Write(contentId); + bw.Write(tmdReserved.GetBytes()); + bw.Write(contentIndex); + bw.Write(reserved); + + Debug.Assert(ms.Position == HEADER_SIZE); + } + + return bytes; + } + + public override void SetBytes(byte[] bytes) + { + if (bytes == null) + { + throw new ArgumentNullException("bytes"); + } + if (bytes.Length != HEADER_SIZE) + { + string message = string.Format("bytes.Length != {0} (bytes.Length:{1})", HEADER_SIZE, bytes.Length); + throw new ArgumentException("bytes", message); + } + + using (var ms = new MemoryStream(bytes)) + using (var br = new ExtBinaryReader(ms)) + { + br.Read(ref signature); + br.Read(ref companyCode); + br.Read(ref version); + br.Read(uniqueId); + br.Read(hardwareId); + br.Read(ref titleId); + br.Read(ref requiredSize); + br.Read(fileSizes); + br.Read(ref contentId); + { + byte[] tmdReservedBytes = new byte[TMD_RESERVED_SIZE]; + br.Read(tmdReservedBytes); + tmdReserved.SetBytes(tmdReservedBytes); + } + br.Read(ref contentIndex); + br.Read(reserved); + + Debug.Assert(ms.Position == HEADER_SIZE); + } + } + + public override object Clone() + { + return new HeaderBody(GetBytes()); + } + } + + /// + /// ヘッダブロック中のTMDReserved。 + /// + public class TmdReserved + { + private const int TMD_RESERVED_SIZE = 62; + + public const int RESERVED8_SIZE = 3; + public const int PARENTAL_CONTROL_SIZE = 16; + public const int RESERVED_SIZE = 30; + + public UInt32 publicSaveSize; + public UInt32 privateSaveSize; + public UInt32 reserved32; + public Byte flags; + public readonly Byte[] reserved8; + public readonly Byte[] parentalControl; + public readonly Byte[] reserved; + + public TmdReserved() + { + reserved8 = new Byte[RESERVED8_SIZE]; + parentalControl = new Byte[PARENTAL_CONTROL_SIZE]; + reserved = new Byte[RESERVED_SIZE]; + } + + public byte[] GetBytes() + { + byte[] bytes = new byte[TMD_RESERVED_SIZE]; + + using (var ms = new MemoryStream(bytes)) + using (var bw = new BinaryWriter(ms)) + { + bw.Write(publicSaveSize); + bw.Write(privateSaveSize); + bw.Write(reserved32); + bw.Write(flags); + bw.Write(reserved8); + bw.Write(parentalControl); + bw.Write(reserved); + + Debug.Assert(ms.Position == TMD_RESERVED_SIZE); + } + + return bytes; + } + + public void SetBytes(byte[] bytes) + { + if (bytes == null) + { + throw new ArgumentNullException("bytes"); + } + if (bytes.Length != TMD_RESERVED_SIZE) + { + string message = string.Format("bytes.Length != {0} (bytes.Length:{1})", TMD_RESERVED_SIZE, bytes.Length); + throw new ArgumentException("bytes", message); + } + + using (var ms = new MemoryStream(bytes)) + using (var br = new ExtBinaryReader(ms)) + { + br.Read(ref publicSaveSize); + br.Read(ref privateSaveSize); + br.Read(ref reserved32); + br.Read(ref flags); + br.Read(reserved8); + br.Read(parentalControl); + br.Read(reserved); + + Debug.Assert(ms.Position == TMD_RESERVED_SIZE); + } + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/AssemblyInfo.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..61551d7 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// アセンブリに関する一般情報は以下の属性セットをとおして制御されます。 +// アセンブリに関連付けられている情報を変更するには、 +// これらの属性値を変更してください。 +[assembly: AssemblyTitle("TWLBackupBlock")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("TWLBackupBlock")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible を false に設定すると、その型はこのアセンブリ内で COM コンポーネントから +// 参照不可能になります。COM からこのアセンブリ内の型にアクセスする場合は、 +// その型の ComVisible 属性を true に設定してください。 +[assembly: ComVisible(false)] + +// 次の GUID は、このプロジェクトが COM に公開される場合の、typelib の ID です +[assembly: Guid("46f10db4-5827-4ead-9f8c-ef885eb49f40")] + +// アセンブリのバージョン情報は、以下の 4 つの値で構成されています: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を +// 既定値にすることができます: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.Designer.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.Designer.cs new file mode 100644 index 0000000..ac2ffd0 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// このコードはツールによって生成されました。 +// ランタイム バージョン:2.0.50727.4959 +// +// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 +// コードが再生成されるときに損失したりします。 +// +//------------------------------------------------------------------------------ + +namespace TwlBackupBlock.Properties { + using System; + + + /// + /// ローカライズされた文字列などを検索するための、厳密に型指定されたリソース クラスです。 + /// + // このクラスは StronglyTypedResourceBuilder クラスが ResGen + // または Visual Studio のようなツールを使用して自動生成されました。 + // メンバを追加または削除するには、.ResX ファイルを編集して、/str オプションと共に + // ResGen を実行し直すか、または VS プロジェクトをビルドし直します。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// このクラスで使用されているキャッシュされた ResourceManager インスタンスを返します。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TwlBackupBlock.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 厳密に型指定されたこのリソース クラスを使用して、すべての検索リソースに対し、 + /// 現在のスレッドの CurrentUICulture プロパティをオーバーライドします。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.resx b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.resx new file mode 100644 index 0000000..85c9090 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Resources.resx @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.Designer.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.Designer.cs new file mode 100644 index 0000000..2e8ed9f --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// このコードはツールによって生成されました。 +// ランタイム バージョン:2.0.50727.4959 +// +// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 +// コードが再生成されるときに損失したりします。 +// +//------------------------------------------------------------------------------ + +namespace TwlBackupBlock.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.settings b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.settings new file mode 100644 index 0000000..15034e7 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Properties/Settings.settings @@ -0,0 +1,6 @@ + + + + + + diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/SignatureBody.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/SignatureBody.cs new file mode 100644 index 0000000..3873d11 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/SignatureBody.cs @@ -0,0 +1,192 @@ +using System; +using System.IO; +using System.Diagnostics; + +namespace TwlBackupBlock +{ + /// + /// 署名ブロック中のハッシュセット。 + /// + public class TwlBackupHashSet + { + public const int HASH_SIZE = 32; + public const int NUM_OF_SECTION = 4; + + public readonly byte[] banner; + public readonly byte[] header; + public readonly byte[][] section; + + public TwlBackupHashSet() + { + banner = new byte[HASH_SIZE]; + header = new byte[HASH_SIZE]; + section = new byte[NUM_OF_SECTION][]; + for (int i = 0; i < section.Length; i++) + { + section[i] = new byte[HASH_SIZE]; + } + } + } + /// + /// 署名ブロックのデータ本体。 + /// + public class SignatureBody : AbstractBody + { + public const int SIGNATURE_SIZE = 1024; + + public const int ECC_SIGN_SIZE = 60; + public const int ECC_CERT_SIZE = 384; + + public readonly TwlBackupHashSet digest; + public readonly byte[] sign; + public readonly byte[][] cert; + public UInt32 reserved; + + public SignatureBody(byte[] data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + if (data.Length != SIGNATURE_SIZE) + { + string message = string.Format("data.Length % {0} != 0 (data.Length:{1})", SIGNATURE_SIZE, data.Length); + throw new ArgumentException("data", message); + } + + digest = new TwlBackupHashSet(); + sign = new byte[ECC_SIGN_SIZE]; + cert = new byte[2][]; + for (int i = 0; i < cert.Length; i++) + { + cert[i] = new byte[ECC_CERT_SIZE]; + } + + SetBytes(data); + } + + public override byte this[int index] + { + get + { + if (index < 0 || index >= SIGNATURE_SIZE) + { + throw new ArgumentException("index"); + } + return ReadByte(index); + } + set + { + if (index < 0 || index >= SIGNATURE_SIZE) + { + throw new ArgumentException("index"); + } + WriteByte(index, value); + } + } + + public override int Length + { + get + { + return SIGNATURE_SIZE; + } + } + + /// + /// 指定したバイトを取得します。 + /// + /// 取得したいバイトのインデックス。 + /// で指定したバイト + public byte ReadByte(int index) + { + if (index < 0 || index >= SIGNATURE_SIZE) + { + throw new ArgumentException("index"); + } + // TODO 処理が遅ければ効率化 + byte[] bytes = GetBytes(); + return bytes[index]; + } + + /// + /// 指定したバイトを書き換えます。 + /// + /// 書き換えたいバイトのインデックス。 + /// 上書きに使うバイト。 + public void WriteByte(int index, byte b) + { + if (index < 0 || index >= SIGNATURE_SIZE) + { + throw new ArgumentException("index"); + } + // TODO 処理が遅ければ効率化 + byte[] bytes = GetBytes(); + bytes[index] = b; + SetBytes(bytes); + } + + public override byte[] GetBytes() + { + byte[] bytes = new byte[SIGNATURE_SIZE]; + + using (var ms = new MemoryStream(bytes)) + using (var bw = new BinaryWriter(ms)) + { + bw.Write(digest.banner); + bw.Write(digest.header); + foreach (var e in digest.section) + { + bw.Write(e); + } + bw.Write(sign); + foreach (var e in cert) + { + bw.Write(e); + } + bw.Write(reserved); + + Debug.Assert(ms.Position == SIGNATURE_SIZE); + } + + return bytes; + } + + public override void SetBytes(byte[] bytes) + { + if (bytes == null) + { + throw new ArgumentNullException("bytes"); + } + if (bytes.Length != SIGNATURE_SIZE) + { + string message = string.Format("bytes.Length != {0} (bytes.Length:{1})", SIGNATURE_SIZE, bytes.Length); + throw new ArgumentException("bytes", message); + } + + using (var ms = new MemoryStream(bytes)) + using (var br = new ExtBinaryReader(ms)) + { + br.Read(digest.banner); + br.Read(digest.header); + foreach (var e in digest.section) + { + br.Read(e); + } + br.Read(sign); + foreach (var e in cert) + { + br.Read(e); + } + br.Read(ref reserved); + + Debug.Assert(ms.Position == SIGNATURE_SIZE); + } + } + + public override object Clone() + { + return new SignatureBody(GetBytes()); + } + } +} diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TWLBackupBlock.csproj.user b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TWLBackupBlock.csproj.user new file mode 100644 index 0000000..7ff3943 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TWLBackupBlock.csproj.user @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TwlBackupBlock.csproj b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TwlBackupBlock.csproj new file mode 100644 index 0000000..6ece8d8 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/TwlBackupBlock.csproj @@ -0,0 +1,93 @@ + + + + Debug + AnyCPU + 9.0.30729 + 2.0 + {E62F6169-868A-48E4-9B1D-C2EB3BE11F39} + Library + Properties + TwlBackupBlock + TwlBackupBlock + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + true + document.xml + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + true + document.xml + + + + + 3.5 + + + 3.5 + + + 3.5 + + + + + + + + + + True + True + Resources.resx + + + True + True + Settings.settings + + + + + + + + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + \ No newline at end of file diff --git a/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Utility.cs b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Utility.cs new file mode 100644 index 0000000..d12c4b3 --- /dev/null +++ b/TwlBackupChecker/TWLBackupBlock/TWLBackupBlock/Utility.cs @@ -0,0 +1,496 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Security.Cryptography; + +namespace TwlBackupBlock +{ + /// + /// TWLバックアップデータの暗号化・復号化処理を提供します。 + /// + public class Utility + { + private const int KEY_SIZE = 16; + private const int ROUND_SCALE = 16; + + private const int AES_MAC_SIZE = 16; + private const int AES_IV_SIZE = 16; + private const int AES_SIGN_HEADER_SIZE = 32; + + private const int BANNER_SIZE = 16 * 1024; + private const int HEADER_SIZE = 160; + private const int SIGNATURE_SIZE = 1024; + + private const int ENCRYPT_BANNER_SIZE = BANNER_SIZE + AES_SIGN_HEADER_SIZE; + private const int ENCRYPT_HEADER_SIZE = HEADER_SIZE + AES_SIGN_HEADER_SIZE; + private const int ENCRYPT_SIGNATURE_SIZE = SIGNATURE_SIZE + AES_SIGN_HEADER_SIZE; + + /// + /// バックアップデータをブロックに分割して復号化をおこないます。 + /// + /// バックアップデータ。 + /// データ本体の復号鍵。 + /// 分割、復号化されたブロック。 + public static Blocks DecryptBackupData(byte[] data, byte[] key) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + if (key == null) + { + throw new ArgumentNullException("key"); + } + { + int minDataSize = ENCRYPT_BANNER_SIZE + ENCRYPT_HEADER_SIZE + ENCRYPT_SIGNATURE_SIZE; + if (data.Length < minDataSize) + { + string message = string.Format("data.Length < {0} (data.Length:{1})", data, minDataSize); + throw new ArgumentException("data", message); + } + } + if (key.Length != KEY_SIZE) + { + string message = string.Format("key.Length != {0} (key.Length:{1})", KEY_SIZE, key.Length); + throw new ArgumentException("key", message); + } + + Blocks blocks = new Blocks(); + + using (var ms = new MemoryStream(data)) + using (var br = new BinaryReader(ms)) + { + blocks.banner.body = new Body(DecryptBlock(br, BANNER_SIZE, key, out blocks.banner.mac, out blocks.banner.iv)); + HeaderBody headerBody = new HeaderBody(DecryptBlock(br, HEADER_SIZE, key, out blocks.header.mac, out blocks.header.iv)); + blocks.header.body = headerBody; + + if (!CheckSignature(headerBody)) + { + throw new Exception("ヘッダブロックのシグネチャが正しくありません"); + } + + blocks.signature.body = new SignatureBody(DecryptBlock(br, SIGNATURE_SIZE, key, out blocks.signature.mac, out blocks.signature.iv)); + + int TMD_SIZE = (int)headerBody.fileSizes[0]; + int CONTENT_SIZE = (int)headerBody.fileSizes[1]; + int SAVE_DATA_SIZE = (int)headerBody.fileSizes[2]; + int SUB_BANNER_SIZE = (int)headerBody.fileSizes[3]; + + blocks.tmd.body = new Body(DecryptBlock(br, TMD_SIZE, key, out blocks.tmd.mac, out blocks.tmd.iv)); + blocks.content.body = new Body(DecryptBlock(br, CONTENT_SIZE, key, out blocks.content.mac, out blocks.content.iv)); + blocks.saveData.body = new Body(DecryptBlock(br, SAVE_DATA_SIZE, key, out blocks.saveData.mac, out blocks.saveData.iv)); + blocks.subBanner.body = new Body(DecryptBlock(br, SUB_BANNER_SIZE, key, out blocks.subBanner.mac, out blocks.subBanner.iv)); + + Debug.Assert(ms.Position == data.Length); + } + + return blocks; + } + + /// + /// ヘッダのデータ本体のシグネチャが正しいか判定します。 + /// + /// ヘッダのデータ本体。 + /// シグネチャが正しい場合はtrue。それ以外の場合はfalse。 + private static bool CheckSignature(HeaderBody header) + { + byte[] signature = { (byte)'3', (byte)'F', (byte)'D', (byte)'T' }; + byte[] bytes = BitConverter.GetBytes(header.signature); + Debug.Assert(signature.Length == bytes.Length); + + for (int i = 0; i < bytes.Length; i++) + { + if (bytes[i] != signature[i]) + { + return false; + } + } + + return true; + } + + /// + /// binaryReaderから読み込んだブロックを復号化します。 + /// + /// ブロックを読み出すBinaryReader。 + /// 暗号化前のブロックのデータ本体のバイトサイズ。 + /// データ本体の復号鍵。 + /// ブロックのMAC。 + /// ブロックのIV。 + /// ブロックのデータ本体。 + private static byte[] DecryptBlock(BinaryReader binaryReader, int bodySize, byte[] key, out byte[] mac, out byte[] iv) + { + Debug.Assert(key.Length == KEY_SIZE); + + int encryptBodySize = RoundUp(bodySize, ROUND_SCALE); + + byte[] body = binaryReader.ReadBytes(encryptBodySize); + mac = binaryReader.ReadBytes(AES_MAC_SIZE); + iv = binaryReader.ReadBytes(AES_IV_SIZE); + + return DecryptBody(body, key, iv, bodySize); + } + + /// + /// ブロックのデータ本体を復号化します。 + /// + /// 暗号化されたデータ本体。 + /// データ本体の復号鍵。 + /// 暗号化に利用したIV。 + /// 暗号化前のブロックのデータ本体のバイトサイズ。 + /// 復号化されたデータ本体。 + public static byte[] DecryptBody(byte[] body, byte[] key, byte[] iv, int size) + { + if (body == null) + { + throw new ArgumentNullException("body"); + } + if (key == null) + { + throw new ArgumentNullException("key"); + } + if (iv == null) + { + throw new ArgumentNullException("iv"); + } + if (body.Length % ROUND_SCALE != 0) + { + string message = string.Format("body.Length % {0} != 0 (body.Length:{1})", ROUND_SCALE, body.Length); + throw new ArgumentException("body", message); + } + if (key.Length != KEY_SIZE) + { + string message = string.Format("key.Length != {0} (key.Length:{1})", KEY_SIZE, key.Length); + throw new ArgumentException("key", message); + } + if (iv.Length != AES_IV_SIZE) + { + string message = string.Format("iv.Length != {0} (iv.Length:{1})", AES_IV_SIZE, iv.Length); + throw new ArgumentException("iv", message); + } + if (size < 0) + { + string message = string.Format("size < 0 (size:{0})", size); + throw new ArgumentException("size", message); + } + if (size > body.Length) + { + string message = string.Format("size > body.Length (size:{0}, body.Length:{1})", size, body.Length); + throw new ArgumentException("size", message); + } + + byte[] result = new byte[size]; + + using (var ms = new MemoryStream(body)) + { + RijndaelManaged aes = new RijndaelManaged(); + aes.Mode = CipherMode.CBC; + aes.Padding = PaddingMode.Zeros; + aes.Key = key; + aes.IV = iv; + using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) + { + cs.Read(result, 0, result.Length); + } + } + + return result; + } + + /// + /// ブロックのデータ本体を暗号化します。 + /// + /// 復号化されたデータ本体。 + /// データ本体の暗号鍵。 + /// 暗号化に利用するIV。 + /// 暗号化されたデータ本体。 + public static byte[] EncryptBody(byte[] body, byte[] key, byte[] iv) + { + if (body == null) + { + throw new ArgumentNullException("body"); + } + if (key == null) + { + throw new ArgumentNullException("key"); + } + if (iv == null) + { + throw new ArgumentNullException("iv"); + } + if (key.Length != KEY_SIZE) + { + string message = string.Format("key.Length != {0} (key.Length:{1})", KEY_SIZE, key.Length); + throw new ArgumentException("key", message); + } + if (iv.Length != AES_IV_SIZE) + { + string message = string.Format("iv.Length != {0} (iv.Length:{1})", AES_IV_SIZE, iv.Length); + throw new ArgumentException("iv", message); + } + + byte[] result = new byte[RoundUp(body.Length, ROUND_SCALE)]; + + using (var ms = new MemoryStream(body)) + { + RijndaelManaged aes = new RijndaelManaged(); + aes.Mode = CipherMode.CBC; + aes.Padding = PaddingMode.Zeros; + aes.Key = key; + aes.IV = iv; + using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Read)) + { + cs.Read(result, 0, result.Length); + } + } + + return result; + } + + /// + /// ブロックのデータ本体を暗号化します。内部的にはEncryptBody(body.GetBytes(), key, iv)を呼び出します。 + /// + /// 暗号化されたデータ本体。 + /// データ本体の暗号鍵。 + /// 暗号化に利用するIV。 + /// 暗号化されたデータ本体。 + public static byte[] EncryptBody(AbstractBody body, byte[] key, byte[] iv) + { + if (body == null) + { + throw new ArgumentNullException("body"); + } + + return EncryptBody(body.GetBytes(), key, iv); + } + + /// + /// ブロックのデータ本体からハッシュを取得します。 + /// + /// 暗号化前のデータ本体。 + /// 32バイトのハッシュ。 + public static byte[] GetSha256(byte[] data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + + var sha = new SHA256Managed(); + + byte[] buf = Enumerable.Repeat(0, RoundUp(data.Length, ROUND_SCALE)).ToArray(); + Array.Copy(data, 0, buf, 0, data.Length); + + return sha.ComputeHash(buf); + } + + /// + /// ハッシュからMACを取得します。 + /// + /// データ本体のハッシュ。 + /// AES-CMACの鍵。 + /// 16バイトのMAC。 + public static byte[] GetAesCmac(byte[] data, byte[] key) + { + return AesCmac.GetMac(data, key); + } + + /// + /// 基準値の倍数のうち、指定された整数以上で最小のものを返します。 + /// + /// 整数。 + /// 基準値。 + /// yの倍数のうち、x以上で最小の整数。 + private static int RoundUp(int x, int y) + { + return (x + y - 1) / y * y; + } + + /// + /// インポートシーケンスと同様の手順でバックアップデータを検証します。 + /// ただし、このメソッドでは署名データの検証はおこないません。 + /// 署名以外の整合性がとれており、署名の整合性がとれていない場合、このメソッドはtrueを返します。 + /// + /// 検証にかけるバックアップデータ。 + /// データ本体の暗号鍵。 + /// MACの鍵。 + /// 検証に成功した場合はtrue。それ以外の場合はfalse。 + public static bool Verify(byte[] data, byte[] aesCbcKey, byte[] aesCmacKey) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + if (aesCbcKey == null) + { + throw new ArgumentNullException("key"); + } + if (aesCbcKey.Length != KEY_SIZE) + { + string message = string.Format("key.Length != {0} (key.Length:{1})", KEY_SIZE, aesCbcKey.Length); + throw new ArgumentException("key", message); + } + + { + int minDataSize = ENCRYPT_BANNER_SIZE + ENCRYPT_HEADER_SIZE + ENCRYPT_SIGNATURE_SIZE; + if (data.Length < minDataSize) + { + Console.WriteLine("バックアップファイルサイズがバナー+ヘッダ+署名ブロック未満です"); + return false; + } + } + + Blocks blocks = new Blocks(); + + using (var ms = new MemoryStream(data)) + using (var br = new BinaryReader(ms)) + { + blocks.banner.body = new Body(DecryptBlock(br, BANNER_SIZE, aesCbcKey, out blocks.banner.mac, out blocks.banner.iv)); + HeaderBody headerBody = new HeaderBody(DecryptBlock(br, HEADER_SIZE, aesCbcKey, out blocks.header.mac, out blocks.header.iv)); + blocks.header.body = headerBody; + SignatureBody signatureBody = new SignatureBody(DecryptBlock(br, SIGNATURE_SIZE, aesCbcKey, out blocks.signature.mac, out blocks.signature.iv)); + blocks.signature.body = signatureBody; + + if (!CheckMac(blocks.header, aesCmacKey, blocks.header.mac)) + { + Console.WriteLine("ヘッダブロックのMACが一致しません"); + return false; + } + if (!CheckSignature(headerBody)) + { + Console.WriteLine("ヘッダブロックのシグネチャが正しくありません"); + return false; + } + if (!CheckMac(blocks.signature, aesCmacKey, blocks.signature.mac)) + { + Console.WriteLine("署名ブロックのMACが一致しません"); + return false; + } + /* 署名データの検証 */ + if (!CheckHash(blocks.header, signatureBody.digest.header)) + { + Console.WriteLine("ヘッダブロックのハッシュが一致しません"); + return false; + } + if (!CheckMac(blocks.banner, aesCmacKey, blocks.banner.mac)) + { + Console.WriteLine("バナーブロックのMACが一致しません"); + return false; + } + if (!CheckHash(blocks.banner, signatureBody.digest.banner)) + { + Console.WriteLine("バナーブロックのハッシュが一致しません"); + return false; + } + + int TMD_SIZE = (int)headerBody.fileSizes[0]; + int CONTENT_SIZE = (int)headerBody.fileSizes[1]; + int SAVE_DATA_SIZE = (int)headerBody.fileSizes[2]; + int SUB_BANNER_SIZE = (int)headerBody.fileSizes[3]; + + int restSize = RoundUp(TMD_SIZE, ROUND_SCALE) + RoundUp(CONTENT_SIZE, ROUND_SCALE) + + RoundUp(SAVE_DATA_SIZE, ROUND_SCALE) + RoundUp(SUB_BANNER_SIZE, ROUND_SCALE) + + AES_SIGN_HEADER_SIZE * 4; + if (data.Length - ms.Position != restSize) + { + Console.WriteLine("TMD、コンテンツ、publicセーブデータ、サブバナーブロックサイズが実際のサイズと一致しません"); + return false; + } + blocks.tmd.body = new Body(DecryptBlock(br, TMD_SIZE, aesCbcKey, out blocks.tmd.mac, out blocks.tmd.iv)); + if (!CheckMac(blocks.tmd, aesCmacKey, blocks.tmd.mac)) + { + Console.WriteLine("TMDブロックのMACが一致しません"); + return false; + } + if (!CheckHash(blocks.tmd, signatureBody.digest.section[0])) + { + Console.WriteLine("TMDブロックのハッシュが一致しません"); + return false; + } + blocks.content.body = new Body(DecryptBlock(br, CONTENT_SIZE, aesCbcKey, out blocks.content.mac, out blocks.content.iv)); + if (!CheckMac(blocks.content, aesCmacKey, blocks.content.mac)) + { + Console.WriteLine("コンテンツブロックのMACが一致しません"); + return false; + } + if (!CheckHash(blocks.content, signatureBody.digest.section[1])) + { + Console.WriteLine("コンテンツブロックのハッシュが一致しません"); + return false; + } + blocks.saveData.body = new Body(DecryptBlock(br, SAVE_DATA_SIZE, aesCbcKey, out blocks.saveData.mac, out blocks.saveData.iv)); + if (!CheckMac(blocks.saveData, aesCmacKey, blocks.saveData.mac)) + { + Console.WriteLine("publicセーブデータブロックのMACが一致しません"); + return false; + } + if (!CheckHash(blocks.saveData, signatureBody.digest.section[2])) + { + Console.WriteLine("publicセーブデータブロックのハッシュが一致しません"); + return false; + } + blocks.subBanner.body = new Body(DecryptBlock(br, SUB_BANNER_SIZE, aesCbcKey, out blocks.subBanner.mac, out blocks.subBanner.iv)); + if (!CheckMac(blocks.subBanner, aesCmacKey, blocks.subBanner.mac)) + { + Console.WriteLine("サブバナーブロックの内容とMACが一致しません"); + return false; + } + if (!CheckHash(blocks.subBanner, signatureBody.digest.section[3])) + { + Console.WriteLine("サブバナーブロックのハッシュが一致しません"); + return false; + } + + Debug.Assert(ms.Position == data.Length); + } + + return true; + } + + /// + /// ハッシュによりブロックが正しいか判定します。 + /// + /// ブロック。 + /// ハッシュ。 + /// ハッシュがブロックから生成したものと一致した場合はtrue。それ以外の場合はfalse。 + private static bool CheckHash(Block block, byte[] hash) + { + byte[] tryHash = GetSha256(block.body.GetBytes()); + Debug.Assert(tryHash.Length == hash.Length); + + for (int i = 0; i < tryHash.Length; i++) + { + if (tryHash[i] != hash[i]) + { + return false; + } + } + return true; + } + + /// + /// MACによりブロックが正しいか判定します。 + /// + /// ブロック。 + /// MACの鍵。 + /// MAC。 + /// MACがブロックから生成したものと一致した場合はtrue。それ以外の場合はfalse。 + private static bool CheckMac(Block block, byte[] key, byte[] mac) + { + byte[] tryMac = GetAesCmac(GetSha256(block.body.GetBytes()), key); + Debug.Assert(tryMac.Length == mac.Length); + + for (int i = 0; i < tryMac.Length; ++i) + { + if (tryMac[i] != mac[i]) + { + return false; + } + } + return true; + } + } +}