Speed

OAEP Adds Randomness

RSA-OAEP pads a message with a hash of a label, zero bytes and a random seed before the exponentiation, so every encryption is different and a message has a size limit; the page works out that limit and decrypts OAEP ciphertexts with one command.

The question

Raw RSA turns the same message into the same ciphertext every time. Padding fixes that, and the scheme OpenSSL calls OAEP, defined in RFC 8017 section 7.1, does it with a random seed. What goes into the padded block, how long may a message be on a 512-bit key, and what does a program get if it decrypts an OAEP ciphertext without saying that the padding is OAEP?

The key alice of this lesson is test material, a 512-bit key made for these lessons. The OAEP ciphertexts are test material too: they were made once and are fixed, because every new encryption would be different.

Work it by hand

RFC 8017 section 7.1.1 builds the block in these steps. Write k for the key length in bytes, 64 here, and hLen for the length of the hash, 20 bytes for SHA-1, the default hash of OpenSSL's OAEP.

  • lHash is the hash of the label, which is empty by default: 20 bytes.
  • PS is zero bytes: k minus the message length minus 2 times hLen minus 2 of them.
  • DB is lHash, PS, a single byte 01 and the message. It has k minus hLen minus 1 bytes: 43.
  • seed is hLen random bytes. A mask made from the seed hides DB, and a mask made from the masked DB hides the seed.
  • EM is 00, the masked seed and the masked DB: k bytes, which RSA then raises to the power e.

Step 2d of the RFC generates the seed at random, so encrypting the same message twice gives two different ciphertexts; this is stated here from the RFC, and the page does not run it. The padding must fit, so the message length is at most k minus 2 hLen minus 2. For k equal to 64 and hLen equal to 20 that is 22 bytes. With a 32-byte hash it would be 64 minus 64 minus 2, which is negative: no message fits.

Run it

The program works out the limit from the two sizes. Line 1 is the limit with the first hash, line 2 the limit with the second, and line 3 the size of DB. It returns the first limit, 22; the extra values are -2 for the second hash and 43 for DB.

With a key of 64 bytes the limit is 22 bytes for the first hash and minus 2 for the second, and the data block has 43 bytes.
let limit_a = key_bytes - 2 * hash_a - 2;
let limit_b = key_bytes - 2 * hash_b - 2;
let block_bytes = key_bytes - hash_a - 1;
lmcode_return_put("limit_second_hash", limit_b);
lmcode_return_put("data_block_bytes", block_bytes);
let answer = limit_a;
{
  "answer": 22,
  "limit_second_hash": -2,
  "data_block_bytes": 43
}

You can change the three sizes: the program is plain arithmetic and stops on no value. A negative result means that no message fits. For the test key, OpenSSL agrees with the arithmetic (measured with lmssl 0.2.2, stated here, not run on this page): a message of 22 bytes encrypts, one of 23 stops with "data too large for key size", and so does hi! with -pkeyopt rsa_oaep_md:sha256.

Your turn

The command below should decrypt the OAEP ciphertext in in/ciphertext.bin with the private key in in/key.txt and write the message to out.bin. Decide whether it does, and edit it so that it does. Check runs your command on the data shown and on four others, each a different ciphertext made with the default OAEP settings.

The first two lines of the private key file, a label line and the line that opens the private key block, the first 16 of the 64 ciphertext bytes, and the first 16 bytes that the starting command wrote.

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

# Reads the private key in in/key.txt and the ciphertext in in/ciphertext.bin.
# Writes the decrypted bytes to out.bin. Check runs it with other ciphertexts.
pkeyutl -decrypt -inkey in/key.txt -in in/ciphertext.bin -out out.bin
{
  "answer": "4108cc561e02a8b798ca49fe24d5a4362a73d8ec431610ecbfbbc4e0dee04da29ce2cf30d3d902d2fbb6f4db",
  "file": "out.bin",
  "bytes": 44
}

Write one command that decrypts the 64-byte RSA-OAEP ciphertext in in/ciphertext.bin with the private key in in/key.txt, using the default OAEP settings of OpenSSL, and writes the message to out.bin. Check runs your command with other ciphertexts in that file.

Read the result carefully

The block that RSA decrypts carries its own check: the hash of the label has to come back unchanged, and the zero bytes and the 01 byte have to be where they belong. A decryption is therefore more than a power modulo n. OpenSSL does not stop a decryption whose padding mode does not match the ciphertext: it can write bytes and report no error, so compare the output with the message you expect.

OAEP is not shown with its label, other hashes (a 512-bit key cannot hold a message with a 32-byte hash), attacks on padding or timing. RFC 8017 section 7.2 describes a chosen-ciphertext attack on the older PKCS#1 v1.5 encryption, named here and not run. The key and the ciphertexts are test material, and 512 bits is far too small to use.

Recap

  • OAEP builds a block from the hash of a label, zero bytes, a 01 byte, the message and a random seed.
  • The seed is random, so encryptions of one message differ; the padding limits the message to k minus 2 hLen minus 2 bytes.
  • 22 bytes for a 64-byte key with SHA-1; a 32-byte hash leaves no room.
  • RFC 8017 section 7.2 describes a chosen-ciphertext attack on the older PKCS#1 v1.5 encryption padding; the page names it and does not run it.