<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.3dbrew.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=NinjaBoy</id>
	<title>3dbrew - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.3dbrew.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=NinjaBoy"/>
	<link rel="alternate" type="text/html" href="https://www.3dbrew.org/wiki/Special:Contributions/NinjaBoy"/>
	<updated>2026-04-16T04:45:28Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://www.3dbrew.org/w/index.php?title=CIA&amp;diff=17934</id>
		<title>CIA</title>
		<link rel="alternate" type="text/html" href="https://www.3dbrew.org/w/index.php?title=CIA&amp;diff=17934"/>
		<updated>2016-08-11T03:58:08Z</updated>

		<summary type="html">&lt;p&gt;NinjaBoy: made the opening sentence make sense&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:File formats]]&lt;br /&gt;
== Overview ==&lt;br /&gt;
CIA stands for &#039;&#039;&#039;C&#039;&#039;&#039;TR &#039;&#039;&#039;I&#039;&#039;&#039;mportable &#039;&#039;&#039;A&#039;&#039;&#039;rchive. This format allows the installation of titles to the 3DS. CIA files and titles on [[Title list|Nintendo&#039;s CDN]] contain identical data. As a consequence, valid CIA files can be generated from CDN content. This also means CIA files can contain anything that titles on Nintendo&#039;s CDN can contain. &lt;br /&gt;
&lt;br /&gt;
Under normal circumstances CIA files are used where downloading a title is impractical or not possible. Such as distributing a [[Download Play]] child, or installing forced Gamecard updates. Those CIA(s) are stored by the titles in question, in an auxiliary [[NCCH#CFA|CFA]] file.&lt;br /&gt;
&lt;br /&gt;
Development Units, are capable of manually installing CIA files via the [[3DS Development Unit Software#Dev Menu|Dev Menu]].&lt;br /&gt;
&lt;br /&gt;
== Format ==&lt;br /&gt;
&lt;br /&gt;
This is the current version of the CIA format, it was finalised in late 2010. (Older versions of the CIA format can be viewed on the [[Talk:CIA|Talk]] page)&lt;br /&gt;
&lt;br /&gt;
The CIA format has a similar structure to the [http://wiibrew.org/wiki/Wad WAD format].&lt;br /&gt;
&lt;br /&gt;
The file is represented in little-endian.&lt;br /&gt;
&lt;br /&gt;
The data is aligned in 64 byte blocks (if a content ends at the middle of the block, the next content will begin from a new block).&lt;br /&gt;
&lt;br /&gt;
=== CIA Header ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!  START&lt;br /&gt;
!  SIZE&lt;br /&gt;
!  DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
|  0x00&lt;br /&gt;
|  0x04 &lt;br /&gt;
|  Archive Header Size (Usually = 0x2020 bytes)&lt;br /&gt;
|-&lt;br /&gt;
|  0x04&lt;br /&gt;
|  0x02&lt;br /&gt;
|  Type&lt;br /&gt;
|-&lt;br /&gt;
|  0x06&lt;br /&gt;
|  0x02&lt;br /&gt;
|  Version&lt;br /&gt;
|- &lt;br /&gt;
|  0x08     &lt;br /&gt;
|  0x04&lt;br /&gt;
|  Certificate chain size  &lt;br /&gt;
|-&lt;br /&gt;
|  0x0C      &lt;br /&gt;
|  0x04&lt;br /&gt;
|  [[Ticket]] size&lt;br /&gt;
|-&lt;br /&gt;
|  0x10     &lt;br /&gt;
|  0x04&lt;br /&gt;
|  [[TMD]] file size&lt;br /&gt;
|-&lt;br /&gt;
|  0x14     &lt;br /&gt;
|  0x04&lt;br /&gt;
|  Meta size (0 if no Meta data is present)&lt;br /&gt;
|-&lt;br /&gt;
|  0x18     &lt;br /&gt;
|  0x08&lt;br /&gt;
|  Content size&lt;br /&gt;
|-&lt;br /&gt;
|  0x20&lt;br /&gt;
|  0x2000&lt;br /&gt;
|  Content Index&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The order of the sections in the CIA file:&lt;br /&gt;
* certificate chain&lt;br /&gt;
* Ticket&lt;br /&gt;
* TMD file data&lt;br /&gt;
* Content file data&lt;br /&gt;
* Meta file data (Not a necessary component) &lt;br /&gt;
&lt;br /&gt;
The contents (NCCH/SRL) are encrypted using 128-bit AES-CBC. The encryption uses the decrypted titlekey from the [[Ticket#Structure|ticket]], and the content index from the TMD padded with zeros as the IV.&lt;br /&gt;
&lt;br /&gt;
=== Certificate Chain ===&lt;br /&gt;
&lt;br /&gt;
There are three [[Certificates|certificates]] in this chain:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!  CERTIFICATE&lt;br /&gt;
!  SIGNATURE TYPE&lt;br /&gt;
!  RETAIL CERT NAME&lt;br /&gt;
!  DEBUG CERT NAME&lt;br /&gt;
!  DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
|  CA&lt;br /&gt;
|  RSA-4096&lt;br /&gt;
|  CA00000003&lt;br /&gt;
|  CA00000004&lt;br /&gt;
|  Used to verify the Ticket/TMD Certificates&lt;br /&gt;
|-&lt;br /&gt;
|  Ticket&lt;br /&gt;
|  RSA-2048&lt;br /&gt;
|  XS0000000c&lt;br /&gt;
|  XS00000009&lt;br /&gt;
|  Used to verify the Ticket signature&lt;br /&gt;
|-&lt;br /&gt;
|  TMD&lt;br /&gt;
|  RSA-2048&lt;br /&gt;
|  CP0000000b&lt;br /&gt;
|  CP0000000a&lt;br /&gt;
|  Used to verify the TMD signature&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The CA certificate is issued by &#039;Root&#039;, the public key for which is stored in NATIVE_FIRM.&lt;br /&gt;
&lt;br /&gt;
=== Meta ===&lt;br /&gt;
&lt;br /&gt;
The structure of this data is as follows:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!  START&lt;br /&gt;
!  SIZE&lt;br /&gt;
!  DESCRIPTION&lt;br /&gt;
|-&lt;br /&gt;
|  0x00&lt;br /&gt;
|  0x180&lt;br /&gt;
|  Title ID dependency list - Taken from the application&#039;s [[NCCH#Extended Header|ExHeader]]&lt;br /&gt;
|-&lt;br /&gt;
|  0x180&lt;br /&gt;
|  0x180&lt;br /&gt;
|  Reserved&lt;br /&gt;
|-&lt;br /&gt;
|  0x300&lt;br /&gt;
|  0x4&lt;br /&gt;
|  Core Version&lt;br /&gt;
|-&lt;br /&gt;
|  0x304&lt;br /&gt;
|  0xFC&lt;br /&gt;
|  Reserved&lt;br /&gt;
|-&lt;br /&gt;
|  0x400&lt;br /&gt;
|  0x36C0&lt;br /&gt;
|  [[SMDH|Icon Data]](.ICN) - Taken from the application&#039;s [[ExeFS]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Obviously this section is not present in TWL CIA files, or any other CIA file which does not contain a [[NCCH#CXI|CXI]].&lt;br /&gt;
&lt;br /&gt;
== Tools ==&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/3dshax/ctr/tree/master/ctrtool ctrtool] - Reading/Extraction of CIA files. This can only decrypt the title-key for development CIAs, since retail CIAs use the [[AES]] hardware key-scrambler for the common-key keyslot.&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/ctrdev/ctrsdk/tree/master/tools/make_cia make_cia] - Generating CIA files. Requires CommonKey and ticket/TMD RSA-2048 private exponents.&lt;br /&gt;
&lt;br /&gt;
* [https://github.com/ctrdev/ctrsdk/tree/master/tools/make_cdn_cia make_cdn_cia] - (CMD)(Windows/Linux) Generates CIA files from CDN Content&lt;br /&gt;
&lt;br /&gt;
* [[makerom]] - Tool which can be used to create NCCH, CCI, and CIA files.&lt;br /&gt;
&lt;br /&gt;
== Title Key Encryption ==&lt;br /&gt;
&lt;br /&gt;
The unencrypted Title Key is used to encrypt the data in a CIA. The encrypted Title Key of a CIA can be found at offset 0x1BF in a CIA&#039;s Ticket.&lt;br /&gt;
Each Title Key is encrypted with AES-CBC to get the encrypted Title Key.&lt;br /&gt;
&lt;br /&gt;
To encrypt an unencrypted title key, you need:&lt;br /&gt;
&lt;br /&gt;
* Common key (as byte array)&lt;br /&gt;
* Title ID (as ulong)&lt;br /&gt;
* (and of course the unencrypted title key you want to encrypt) (as byte array)&lt;br /&gt;
&lt;br /&gt;
The title key encryption process starts by converting the ulong (Title ID) into a byte array using by retrieving the bytes of the Title ID using BitConverter.GetBytes().&lt;br /&gt;
If the converted bytes (title ID) are in Little Endian, reverse those bytes. (in C# it would be Array.Reverse(byte_array_from_bitconverter))&lt;br /&gt;
This process makes the Title Key encryption IV.&lt;br /&gt;
&lt;br /&gt;
Next, after you&#039;ve gotten your Title Key&#039;s IV, you can start your cryptography transformation. Using AESManaged, where:&lt;br /&gt;
&lt;br /&gt;
Key  = Common Key&lt;br /&gt;
&lt;br /&gt;
IV   = the byte array found in the conversion process above&lt;br /&gt;
&lt;br /&gt;
Mode = CipherMode.CBC&lt;br /&gt;
&lt;br /&gt;
Create the encryptor (AesManaged.CreateEncryptor(key, iv)) where the key and IV are both the same as above.&lt;br /&gt;
&lt;br /&gt;
Then, create a CryptoStream and a MemoryStream. The Crypto stream should start with the arguments (memorystream, aes_transform_from_above, CryptoStreamMode.Write).&lt;br /&gt;
&lt;br /&gt;
Write to the CryptoStream where buffer=unencrypted_titlekey, offset=0, and count=the length of the unencrypted title key.&lt;br /&gt;
&lt;br /&gt;
Use FlushFinalBlock() on the CryptoStream.&lt;br /&gt;
&lt;br /&gt;
Finally, then, the encrypted title key will be available from your memory &lt;br /&gt;
stream. (to output the calculated encrypted title key as a byte array, you can use memorystream.ToArray(), for example)&lt;br /&gt;
&lt;br /&gt;
Example function: (C#)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
        public static byte[] EncryptMyTitleKey(byte[] commonKey, byte[] titleKey, ulong titleId)&lt;br /&gt;
        {&lt;br /&gt;
            // Make encryption IV&lt;br /&gt;
            byte[] titleidasbytes = new byte[0x10];&lt;br /&gt;
            for (int i = 0; i &amp;lt; 0x10; i++)&lt;br /&gt;
            {&lt;br /&gt;
                titleidasbytes[i] = 0;&lt;br /&gt;
            }&lt;br /&gt;
            byte[] bitBytes = BitConverter.GetBytes(titleId);&lt;br /&gt;
            if (BitConverter.IsLittleEndian)&lt;br /&gt;
            {&lt;br /&gt;
                Array.Reverse(bitBytes);&lt;br /&gt;
            }&lt;br /&gt;
            bitBytes.CopyTo(titleidasbytes, 0);&lt;br /&gt;
            // Encrypt&lt;br /&gt;
            ICryptoTransform transform = new AesManaged { Key = commonKey, IV = titleidasbytes, Mode = CipherMode.CBC }.CreateEncryptor(commonKey, titleidasbytes);&lt;br /&gt;
            MemoryStream memstream = new MemoryStream();&lt;br /&gt;
            CryptoStream cryptostream = new CryptoStream(memstream, transform, CryptoStreamMode.Write);&lt;br /&gt;
            cryptostream.Write(titleKey, 0, titleKey.Length);&lt;br /&gt;
            cryptostream.FlushFinalBlock();&lt;br /&gt;
            return memstream.ToArray();&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>NinjaBoy</name></author>
	</entry>
</feed>