Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?

Nintendo Switch RSA-PKCS#1 Public Key Recovery

This is a short writeup of a fun (but ultimately pretty useless) attack I implemented on the Nintendo Switch a few months ago resulting in the recovery of some otherwise unobtainable RSA public keys. Since public keys aren't private keys, this is pretty useless, apart from letting us validate some signatures on PC. Even so, the attack is a pretty cool one, so I thought I'd write it up.

Every Switch gamecart has a unique certificate (called its "CERT"), storing an RSA signature followed by some kind of unknown but unique encrypted data. I was trying to reverse how these certificates work, and the obvious first step was to try to see how they were validated. However, when I tried looking through the FileSystem (FS) module, which should be responsible for validating these certificates, I found no references to the format at all. The "CERT" magic number was nowhere to be seen, and I couldn't find an RSA modulus that validated the signatures I had. This was interesting, but I still thought that the validation had to be done by the OS, so I started looking for other validation code for the gamecard format...and found none at all.

However, I did make an interesting discovery -- FS was loading a blob into a static buffer that Nintendo kindly left in symbols for: "nn::gc::detail::EmbeddedDataHolder::g_FwWriterBuffer".

Some background context: the Nintendo Switch has a special piece of hardware for interfacing with game cartridges (the "gamecard controller"); this hardware is an application-specific integrated circuit (ASIC), running a custom firmware written by Nintendo. The ASIC is responsible for all interactions between the OS and gamecarts -- FS makes requests to the ASIC, which reads data from the inserted gamecard and delivers it back to the system. Apparently, FS was responsible for loading some kind of firmware onto the ASIC.

Cursory inspection of the blob proved somewhat uninteresting -- at first glance, it looked like an 0x100 RSA signature, an 0x80 plaintext header (with a "LAFW" magic number, and "IDIDIDIDIDIDIDID" present at +0x30), followed by a large amount of encrypted data. Interestingly, there was no code anywhere in FS to validate the signature on this blob, and no code to decrypt it.

This implied, though, that Nintendo's ASIC was more complex than I thought -- it isn't just a simple state machine! It runs a firmware, and it knows how to verify and decrypt that firmware! In addition, I could see from reversing FS that communications between FS and the ASIC used RSA-OAEP with a console unique keypair for encryption in addition to AES-CBC; I could thus be pretty sure the ASIC was capable of complex logic. The keys used to talk to the ASIC are stored encrypted in a blob using keydata available only to TrustZone; these keys have changed only once, in system update 4.0.0 -- this was also the only system update to modify the LAFW firmware blob. To this day, I haven't actually made any additional progress figuring out how the ASIC works -- I have no idea what changed in the encrypted firmware blob between 3.0.2 and 4.0.0, and no idea why they changed the encryption keys FS uses to talk to the ASIC.

All that said, though, I still wanted to be able to validate gamecard-related formats; this seemed tough, though, because I had no way of validating RSA signatures without the accompanying public key. Luckily, though, I was aware of an attack that I thought I'd never get the chance to try in practice that would let me recover the public keys!

Most of the RSA signatures present on the console use a signature padding scheme called "Probabilistic Signature Scheme" (PSS). However, back on the 3DS (and for older consoles), Nintendo used a different scheme called PKCS#1! It stood to reason that Nintendo probably designed the ASIC early on when making the Switch, and so it might use this older padding scheme. FS's code seemed to corroborate this -- there was some "dead" code related to verifying signatures with this scheme that nothing seemed to use...maybe it was a left over?

RSA signature validation has the following parameters: S (the signature), E (the public exponent), N (the public modulus), as well as data to verify. Normally, we view a verifier as being in possession of S, E, N, and the data. In our case, though, we have S and the data, and want to find some way to calculate E and N. E, at least, is easy -- Nintendo consistently uses 65537 (0x10001) as its public exponent. This means our task simplifies to just trying to find some way to find N.

One of the interesting qualities about the PKCS#1 padding scheme is that it is deterministic: that is, for a given message, there is exactly one correct signature S that exponentiates to message exactly one correct message M (with S ^ E % N == M) that validates the data provided. But this means that given the data to verify, we can calculate M! It turns out this is all we need to find N!

Staring at the equation S ^ E % N == M, we can rewrite as follows: (S ^ E) - M % N == 0. In other words, N is a divisor of X = ((S ^ E) - M). Now, consider the case where we have two signatures, and their associated data -- S1, and S2. Given their associated data, we can calculate their associated messages, M1 and M2. From earlier, we know that N is a divisor of X1 = ((S1 ^ E) - M1), and N is also a divisor of X2 = ((S2 ^ E) - M2). This means that N is a common divisor of X1 and X2, and while factoring large numbers is a very hard problem, finding the Greatest Common Divisor is extremely easy -- there are well-known, efficient algorithms to do so. Thus, in theory, given two signatures S1 and S2, we can calculate X1 and X2, and from there find GCD(X1, X2) == N and recover the public key!

This worked like a charm, in practice. From my dumped copies of The Binding of Isaac and Breath of the Wild, I had two gamecard header signatures and two cert signatures, and thus was able to recover both validating public keys. In addition, thanks to 4.0.0 updating the gamecard firmware, I had two signatures for the LAFW blobs (even though I didn't know what data was signed). I tried a simple brute force attack checking every possible length of data for the blobs, though, and this succeeded! It turns out that the ASIC's firmware blobs' signatures are over the entire rest of the blob (the plaintext header and the encrypted body), and so that public key was recoverable too.

The public keys are reproduced here, for posterity:

Gamecart HEAD Public Modulus: 

Gamecart CERT Public Modulus:

Gamecart LAFW Public Modulus:

Practical Impact

As mentioned up above, this attack is pretty much entirely useless: it allows one to verify Nintendo's signatures for gamecart images (XCI), CERTs, and gamecart controller firmware (LAFW), and that's pretty much it.

I do think it's a great demonstration of why one should avoid using a deterministic padding scheme (it can leak information about the keys involved!), though, and makes a great case for non-deterministic padding schemes like RSA-PSS :)


gerd2002 commented Feb 6, 2018

owo wolfram mathematica

JoFelix2 commented Feb 6, 2018

I like that you put "practical impact" below what you write, because from reading this alone I would have no idea what it does.

cysin commented Feb 7, 2018


el puto amo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment