# The encryption ## History and sources - Andreas Naive broke the scheme in June 2007; MAME 0.116u2 (20 June 2007) was the first release to boot the games. His own write-up was **not found** in this phase (his blog's 2007 archive returns 404; the search results were news items). The algorithm below is taken from the function that carries his credit in MAME, `cps3_state::cps3_mask` `[mame]` (BSD-3-Clause; the description here is written from it, the code is not copied). - The same function, with the same constants, is in FinalBurn Neo and in both FPGA cores. - No Capcom or Hitachi document describes it. The tier is 5 for the algorithm as text, raised by a check that does not depend on any emulator: the output is valid SH-2 code and readable text (below). ## The mask The cipher is a 32-bit XOR mask that depends only on the address of the longword and on two 32-bit keys held in the cartridge CPU. Nothing depends on the data. Let `rol(x, n)` rotate a 16-bit value left. Define on 16-bit values: ``` rotxor(v, x): r = (v + rol(v, 2)) mod 2^16 return rol(r, 4) XOR (r AND (v XOR x)) mask(address, key1, key2): a = address XOR key1 v = (a AND FFFF) XOR FFFF v = rotxor(v, key2 AND FFFF) v = v XOR (a >> 16) XOR FFFF v = rotxor(v, key2 >> 16) v = v XOR (a AND FFFF) XOR (key2 AND FFFF) return v repeated in both halves of a longword ``` `address` is the longword's address (low two bits clear). Plain = stored XOR mask. Encryption and decryption are the same operation. ## Keys `[mame]` | Game | key1 | key2 | Mode | | --- | --- | --- | --- | | Red Earth / War-Zard | `9e300ab1` | `a175b82c` | live key | | Street Fighter III: New Generation | `b5fe053e` | `fc03925a` | live key | | Street Fighter III 2nd Impact | `00000000` | `00000000` | dead-cart mode | | JoJo's Venture | `02203ee3` | `01301972` | live key | | Street Fighter III 3rd Strike | `a55432b4` | `0c129981` | live key | | JoJo's Bizarre Adventure | `23323ee3` | `03021972` | live key | | `cps3boot` bootleg BIOS for a stock SH-2 | none | none | no encryption | The keys are per game, not per region or revision. ## What is encrypted | Region | Live key | Dead-cart mode (2nd Impact) | Stock SH-2 | | --- | --- | --- | --- | | BIOS `00000000-0007FFFF` | data reads and fetches masked, address as is | the same, with key 0 | plain | | Program SIMMs `06000000-06FFFFFF` | data reads and fetches masked, address `06000000 + offset` | **fetches masked with key 0, data reads plain** | plain | | Code in the cache data array `C0000000-C00003FF` | fetches masked, address `C0000000 + offset` `[mame]` | the same, key 0 `[mame, jt]` | plain | | On-chip DMAC reads from the two ranges | not decrypted `[mame]` | | | | Graphics and sound SIMMs, work RAM | never encrypted | | | Three modes, then: `live`, `dead`, `none`. JTCPS3 names the same three `[jt]`. The dead-cart row is the important one. A cartridge whose battery has died behaves as a 2nd Impact cartridge: key zero, fetch-only decryption of the program SIMMs. MAME's code does exactly this (its opcode map is `raw XOR mask(address, 0, 0)` while its data handlers return raw bytes when `m_altEncryption` is set), although its header comment says 2nd Impact uses "plain not encrypted SIMMs 1&2 code&data" (C8). The modding community depends on the fetch-only behaviour: to run a modified program on a dead cartridge, every word must be classified as instruction or data, and only instructions encrypted (herzmx's instruction table, pinned). That is behaviour reported from real cartridges, second-hand. Whether Capcom's CPU decrypts through the cache or before it is U2. Whether the decryption costs fetch time is unmeasured `[board]` (the measured board had a stock CPU). ## The check that ran `[ran]` The mask was written out in a throwaway script (not committed: no emulator code in this phase) and applied to BIOS images and SIMM 1 from the local non-merged MAME sets: | Set | BIOS raw first two longwords | Decrypted | SIMM 1 first two longwords, decrypted | | --- | --- | --- | --- | | `redearthnr1` | `D8A5DCA5 174D1545` | `00000400 02080000` | `06001660 02018AAC` | | `sfiiin` | `7F247B24 DF6DDD65` | `00000400 02080000` | `06000EA0 0200846C` | | `sfiii2n` | `05370137 60F362FB` | `00000400 02080000` | raw is already `06000EA0 020097E4` | | `jojon` | `D9BEDDBE 0554075C` | `00000400 02080000` | `06001664 02080000` | | `sfiii3n` | `FF10FB10 48484A40` | `00000400 02080000` | `06000EA0 02008F94` | | `jojoba` | `31683568 1D121F1A` | `00000400 02080000` | `06001668 02080000` | All six BIOS images give the same power-on vectors, PC `00000400` and SP `02080000` (the top of work RAM), and MAME's trace of `redearthnr1` starts at `00000400` with R15 = `02080000` ([mame-oracle.md](mame-oracle.md)). The decrypted BIOS images contain readable text (build dates "Jan 29 1997", "Nov 28 1998", "Apr 09 1999", "Sep 02 1999"; the utility menu of [boot.md](boot.md)). 2nd Impact's SIMM 1 holds a plain vector table, as the dead-cart mode predicts. This is milestone 1's exit test, already known to pass. ## The cartridge - The BIOS is a 512 KiB 29F400TA flash in the cartridge, on an 8-bit bus `[board]`. It differs by game, by region and by CD or NO CD: 29 distinct images across the 60 sets ([census.md](census.md)). - Region and the NO CD flag are "two different bytes in the flashROM" `[mame]`. Which bytes was not established here (U3). - The keys live in static RAM on the CPU die, kept by the battery. The CPU erases them on tampering `[mame]`. No public source documents how the key RAM is written at the factory, or reads it back; Darksoft's kit does not rewrite keys. It replaces the BIOS with one that carries the games' key tables for a dead cartridge, or replaces the CPU with a stock HD6417604 or HD6417095 and uses plain images (`cps3boot` in MAME; a hobbyist write-up and forum posts, unpinned: the pages refuse automated fetches). - Four cartridge types (A to D); A/B and C/D differ in a CPLD and an optional second flash footprint `[mame]`. ## What the board must model A `SecurityCart` with the BIOS bytes, `(key1, key2)` and a mode. The board decrypts data-side reads in its bus callback and gives the core a fetch filter ([sh2-core.md](sh2-core.md)). The `cps3boot` family (mode `none`) is the cheapest first target: no cipher at all.