public bool Sign(byte[] XboxHDKey, bool skipContentHashing, out string errorMessage) { if (Equals(null, XboxHDKey) || XboxHDKey.Length != 0x10) { errorMessage = "Bad key"; return(false); } byte[] buffer, hash; //calculate content hashes if (!skipContentHashing) { object result; if (!GetSection(Types.SectionType.TableOfContents, out result)) { errorMessage = "Failed to parse section data"; return(false); } Types.Section section = Sections[1]; Types.TableOfContents fileTable = (Types.TableOfContents)result; //parsed toc data foreach (Types.TableEntry file in fileTable.entries) { if (!file.Exists(Path.GetDirectoryName(FilePath))) { errorMessage = string.Format("Content file not found: {0}", file.fileName); return(false); } uint hashStartOffset = file.fileHashOffset; uint hashLength = file.fileHashLength; uint size = (uint)new FileInfo(Path.Combine(Path.GetDirectoryName(FilePath), file.fileName)).Length; if (size != file.fileSize) { //errorMessage = string.Format("The size of the file \"{0}\" is invalid\n\nExpected size: {1} ({2} bytes)\nActual size: {3} ({4} bytes)", file.fileName, file.fileSize.RoundBytes(), file.fileSize, size.RoundBytes(), size); //return false; //instead of returning an error here I decided to correct the file size, in case of modders :) buffer = BitConverter.GetBytes(size); IO.Position = (((section.sectionOffset + fileTable.entryTableOffset) + file.entryOffset) + 0x8); IO.Write(buffer, 0, 4); //file size //set hash to cover entire file IO.Write(new byte[4], 0, 4); //hash start offset IO.Write(buffer, 0, 4); //hash length hashStartOffset = 0; hashLength = size; } buffer = File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(FilePath), file.fileName)); hash = XCalcSig.CalculateDigest(buffer, (int)hashStartOffset, (int)hashLength); IO.Position = ((section.sectionOffset + 0xC) + (file.fileHashIndex * 0x14)); IO.Write(hash, 0, 0x14); } IO.Flush(); } //calculate section hashes foreach (Types.Section section in Sections) { if (!section.IsAllocated) { continue; } buffer = new byte[section.sectionLength]; IO.Position = section.sectionOffset; IO.Read(buffer, 0, (int)section.sectionLength); hash = XCalcSig.CalculateDigest(buffer); IO.Position = section.hashOffset; IO.Write(hash, 0, 0x14); } IO.Flush(); //calculate header "signature" buffer = new byte[(Header.headerSize - 0x14)]; IO.Position = 0x14; IO.Read(buffer, 0, (int)(Header.headerSize - 0x14)); hash = XCalcSig.CalculateNonRoamable(BitConverter.GetBytes(Header.titleId), XboxHDKey, buffer); IO.Position = 0; IO.Write(hash, 0, 0x14); IO.Flush(); buffer = null; hash = null; errorMessage = null; return(true); }
bool Read() { Types.Header header = new Types.Header(); byte[] buffer = new byte[4]; IO.Position = 0; header.headerHash = new byte[0x14]; IO.Read(header.headerHash, 0, 0x14); IO.Read(buffer, 0, 4); header.magic = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); header.headerSize = buffer.ToUInt32(false); header.headerType = (Types.HeaderType)header.headerSize; IO.Read(buffer, 0, 4); header.contentType = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); header.contentFlags = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); header.titleId = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); header.offerId = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); header.offerTitleId = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); header.publisherFlags = buffer.ToUInt32(false); Header = header; uint sectionCount = ((header.headerSize - 0x34) / 0x1C); if (sectionCount < 2) { buffer = null; IO.Dispose(); IO = null; return(false); } Sections = new Types.Section[sectionCount]; for (int i = 0; i < sectionCount; i++) { Types.Section section = new Types.Section(); IO.Read(buffer, 0, 4); section.sectionOffset = buffer.ToUInt32(false); IO.Read(buffer, 0, 4); section.sectionLength = buffer.ToUInt32(false); section.hashOffset = (uint)IO.Position; //for convenience when signing section.sectionHash = new byte[0x14]; IO.Read(section.sectionHash, 0, 0x14); Sections[i] = section; } for (int i = 0; i < sectionCount; i++) { IO.Position = Sections[i].sectionOffset; Sections[i].sectionData = new byte[Sections[i].sectionLength]; IO.Read(Sections[i].sectionData, 0, (int)Sections[i].sectionLength); } buffer = null; //section types always in the same order depending on header size if (header.headerType == Types.HeaderType.Content) //0x88 = content { Sections[0].sectionType = Types.SectionType.Language; Sections[1].sectionType = Types.SectionType.TableOfContents; Sections[2].sectionType = Types.SectionType.Optional; } if (header.headerType == Types.HeaderType.Update) //0x6C = update { Sections[0].sectionType = Types.SectionType.Unknown; Sections[1].sectionType = Types.SectionType.TableOfContents; } return(true); }