Speed

Key Anatomy

A private RSA key file holds eight numbers, not one; the program lists them for a 512-bit test key, and one command prints the modulus of another.

The question

A private RSA key is stored in a file of a few lines of text. Is it one secret number? No. The file holds a bundle: the modulus n, the public exponent e, the private exponent d, the two primes p and q that multiply to n, and three more numbers that only make the private operation faster. Which of these are public, and which must stay secret?

The keys on this page are test material: keys made for these lessons, mostly of 512 bits and committed with the page. 512 bits is the smallest key OpenSSL makes, and it is far too small to use; the lessons use it because the numbers still fit on a screen.

Work it by hand

RFC 8017 section 3.2 says what a private key consists of. Appendix A.1.2 gives the file syntax, which names nine fields: version, modulus, publicExponent, privateExponent, prime1, prime2, exponent1, exponent2 and coefficient.

  • The public key is the pair (n, e). Both are public: the modulus and the small exponent.
  • The private key adds d, with e times d equal to 1 modulo the least common multiple of p minus 1 and q minus 1.
  • p and q are the factors of n. For these test keys each has 256 bits, so their product has 512 bits.
  • exponent1 is d modulo p minus 1, exponent2 is d modulo q minus 1, and coefficient is the inverse of q modulo p. They are shortcuts (the Chinese remainder theorem) and add no new secret.

Anyone who learns p or q can compute d, so the primes are as secret as d. The size of a key is the number of bits of n. The oracle of this lesson recomputes the relations with Python integers: p times q equals n, and the three shortcut numbers follow from the others.

Run it

The program takes the private key as text. Line 1 reads it, bits() on line 19 is the size of n, and text() is the dump of the numbers. The loop on lines 5 to 15 goes through the dump and keeps the names of the lines that start in the first column, the fields. Line 12 keeps the line of the public exponent.

For the test key carol the answer is 512, the type is RSA, there are eight fields from modulus to coefficient, and the public exponent line reads publicExponent: 65537 (0x10001). The numbers themselves are not returned: the lesson is about the structure.

The key is 512 bits and of type RSA. The public exponent line reads publicExponent: 65537 (0x10001). The eight fields are modulus, publicExponent, privateExponent, prime1, prime2, exponent1, exponent2 and coefficient.

Public test material, generated for this lesson. Never use it for anything real.

let k = ssl.parse_key(key);
let lines = str_split(k.text(), "\n");
let names = "";
let exponent = "";
for (i in range(1, lines.len())) {
  let line = lines.at(i);
  if (str_len(line) > 0) {
    if (str_substr(line, 0, 1) != " ") {
      let name = str_substr(line, 0, str_find(line, ":"));
      if (names != "") { names = names + "\n"; }
      names = names + name;
      if (name == "publicExponent") { exponent = line; }
    }
  }
}
lmcode_return_put("type", k.type());
lmcode_return_put("fields", names);
lmcode_return_put("public_exponent", exponent);
let answer = k.bits();
{
  "answer": 512,
  "type": "RSA",
  "fields": "modulus\npublicExponent\nprivateExponent\nprime1\nprime2\nexponent1\nexponent2\ncoefficient",
  "public_exponent": "publicExponent: 65537 (0x10001)"
}

You can change the key. Another private key works: an elliptic-curve key (test material from another lesson) gives the type EC, 256 bits and the fields priv, pub, ASN1 OID and NIST CURVE. A public key, an empty text or any other text stops line 1 with "Ssl.parse_key: cannot read private key (encrypted keys need a passphrase): error:1E08010C:DECODER routines::unsupported".

Your turn

The command below prints every number of the private key in in/key.txt. Edit it so that it prints only the modulus: one line, Modulus= followed by the hexadecimal digits of n in capital letters. Check runs your command on the key shown and on three others, each in the same file.

The first two lines of the key file, a label line and the line that opens the private key block, and the first two lines that the starting command prints: Private-Key: (512 bit, 2 primes) and modulus:.

Public test material, generated for this lesson. Never use it for anything real.

# Reads the private key in in/key.txt.
# Check runs it with other keys in that file.
rsa -in in/key.txt -noout -text
{
  "answer": "Private-Key: (512 bit, 2 primes)\nmodulus:\n    c1:47:41:0d:9e:27:0a:2e:34:a1:e9:95:96:6d:60:65:\n    3c:0c:eb:00:67:6d:ae:50:a9:28:aa:c2:1d:fd:8f:e8:\n    e0:66:ad:c3:cb:b1:13:35:7f:d1:49:b3:bf:0e:d3:9f:\n    1b:00:e6:3a:54:e7:3d:b6:22:6b:81:49:8f:1a:77:79\npublicExponent: 65537 (0x10001)\nprivateExponent:\n    67:1f:74:77:26:95:df:d9:ad:5d:62:3a:dc:ea:f8:ce:\n    aa:ce:9d:a8:36:87:0f:c5:9b:ab:c6:08:12:66:52:9c:\n    58:c4:de:46:4f:dd:c6:ae:b7:ec:22:e4:65:0e:fa:70:\n    0f:cb:84:2a:5f:68:2b:af:63:e2:7a:7f:fa:bc:18:31\nprime1:\n    f1:cd:65:2a:8b:1c:29:6a:d3:cf:ea:c3:88:ea:0c:ab:\n    0a:ec:50:0d:76:d8:e4:52:ba:5b:d8:48:b6:10:79:fd\nprime2:\n    cc:a0:7a:e1:16:34:bc:f3:fe:de:1d:05:93:38:f7:22:\n    41:ff:d6:b2:9d:2f:b4:2e:7c:19:a8:79:0b:9c:fe:2d\nexponent1:\n    91:31:ed:b2:9b:2f:c0:eb:05:df:4e:b0:35:71:9d:43:\n    c3:de:5c:bf:89:02:fa:2f:76:fa:19:a8:a7:b2:c3:1d\nexponent2:\n    35:97:62:17:49:45:8e:a7:28:7c:eb:84:e6:45:e9:13:\n    a3:cd:69:61:66:21:74:43:28:65:5f:e6:33:d0:6e:11\ncoefficient:\n    ab:12:ce:89:26:c9:fc:04:11:a0:32:24:b5:7f:a9:98:\n    b8:ac:ff:98:14:f4:49:a5:6f:96:ed:4d:d3:2c:81:ef"
}

Write one command that prints only the modulus of the RSA private key in in/key.txt: a single line, Modulus= followed by the hexadecimal digits of n in capital letters. Check runs your command with other keys in that file.

Read the result carefully

The modulus is public: it is also in the public key, and printing it reveals nothing the public key does not. The command reads a private key file, and the same command printed with -text shows d, p and q on a screen and in a log. Real private keys are kept out of such places.

The page shows what a 512-bit test key contains, not how keys are made, how they are stored or encrypted in a file, or how a library keeps them in memory. The key files here have no passphrase; a protected key needs one before any command can read it.

Key sizes in use are far above the 512 bits of this lesson, and these test keys must never protect anything real. This book never says that a key size is secure; it says what a run shows and cites what a standard says.

Recap

  • A private RSA key file holds n, e, d, p, q and three shortcut numbers; RFC 8017 section 3.2 names them.
  • The public key is only n and e; p, q and d are secret.
  • The size of a key is the number of bits of n: 512 for the test keys of this book.
  • The modulus is public, and a key tool can print it alone, as Modulus= followed by capital hexadecimal digits.