[Crypto] RSA: how Chinese Remainder Theorem is used here

You probably saw this:

% openssl genrsa -out keypair.pem 512

% openssl rsa -noout -text -in keypair.pem

RSA Private-Key: (512 bit, 2 primes)
publicExponent: 65537 (0x10001)

What is with all these parameters? When all crypto textbooks says that p/q/exponent are the core of RSA? Let's see.

There is another connection between CRT (Chinese Remainder Theorem) and RSA. (One more: low-exponent attack).

RSA decryption phase is: \( m = c^d \pmod {pq} \), where m=message, c=ciphertext, d=decryption exponent, pq=modulus. (For short RSA intro, you may read in my book.) So, for decryption you need to know d/p/q.

But exponentiation is slow, when d has 512 bits, or, nowadays, when 4096 is not uncommon. This is the reason why RSA is slow. Some clever math trickery can speed this process up.

In short, CRT allows to calculate this step in two parts.

Precompute for your private RSA key:

Now, at the step of decryption, when you received 'c', calculate this:

In fact, the following equations hold:

Why is this? A digression here. For example, we need to calculate \( c^d \pmod p \), but 'd' is very big, much larger than 'p', Since \( c^d \) is 'cut' by 'p', the result must be lower than 'p' and much lower than 'd'. Euler's theorem allow us save some computation power. It says that:

\[ c^d \pmod p = c^{d \pmod {\varphi(p)}} \pmod p = c^{d \pmod{p-1}} \pmod p \]

Hence, to calculate \( c^d \pmod p \), just calculate \( c^{d \pmod{p-1}} \pmod p \), exponentiation to \( d \pmod{p-1} \) would be must faster than to 'd'. Now let's rewrite our system of equations, by expanding dp and dq and see that equations holds, thanks to Euler's theorem:

And since we already calculated \( c^{dp} \) and \( c^{dq} \), and we know p/q (we are at decryption side), and p and q are coprime to each other, so we can use CRT to recover the 'm' (which is modulo pq, of course).

Now the real example. PEM file, PEM file dumped, version 2. A SageMath notebook, where I use crt() built-in function to decrypt message, alongside with a more 'usual' method from textbooks.

So why using that esoteric math? For 512-bit RSA key, pq is 512-bit, p and q are 256-bit each other. Two exponentaion operations by 256 bits are much faster than one exponentiation by 512 bit number. Speed up may be as large as fourfold. Many texts about CRT says that you can process a number by processing its parts separately, thanks to CRT.

And this is why you see all these parameters in PEM files. They are precomputed for your private key. dp=exponent1, dq=exponent2. This is why when I factored RSA-512, I used an additional script that will precompute these paremeters for newly created PEM file.

So far so good, but how can be get rid of crt() function? This is a pure Python notebook, where I used Gauss algorithm for CRT, copypasted from somewhere. There is also a short algorithm I copied from SSH 1.x, which is fast and used everywhere. But cryptic, due to optimization. It uses modulo inverse of 'q', and this is why this precomputed value is also present in PEM files, named 'coefficient' or 'q_inv'.

What RFC 2437 says about it all, by the way?

   dP            p's exponent, a positive integer such that:
                  e(dP)\equiv 1 (mod(p-1))

   dQ            q's exponent, a positive integer such that:
                  e(dQ)\equiv 1 (mod(q-1))


   qInv          CRT coefficient, a positive integer less
                 than p such: q(qInv)\equiv 1 (mod p)


11.1.2 Private-key syntax

   An RSA private key should be represented with ASN.1 type

   RSAPrivateKey ::= SEQUENCE {
     version Version,
     modulus INTEGER, -- n
     publicExponent INTEGER, -- e
     privateExponent INTEGER, -- d
     prime1 INTEGER, -- p
     prime2 INTEGER, -- q
     exponent1 INTEGER, -- d mod (p-1)
     exponent2 INTEGER, -- d mod (q-1)
     coefficient INTEGER -- (inverse of q) mod p }

   Version ::= INTEGER

   The fields of type RSAPrivateKey have the following meanings:

   -version is the version number, for compatibility with future
   revisions of this document. It shall be 0 for this version of the
   -modulus is the modulus n.
   -publicExponent is the public exponent e.
   -privateExponent is the private exponent d.
   -prime1 is the prime factor p of n.
   -prime2 is the prime factor q of n.
   -exponent1 is d mod (p-1).
   -exponent2 is d mod (q-1).
   -coefficient is the Chinese Remainder Theorem coefficient q-1 mod p.

Summary: a highly-popular CRT method used in RSA decryption step is cryptic, but fast and optimized. Python and SageMath Jupyter notebooks used.

(the post first published at 20220518.)

List of my other blog posts.

Subscribe to my news feed

Yes, I know about these lousy Disqus ads. Please use adblocker. I would consider to subscribe to 'pro' version of Disqus if the signal/noise ratio in comments would be good enough.