Optimal asymmetric encryption padding
In cryptography, Optimal Asymmetric Encryption Padding is a padding scheme often used together with RSA encryption. OAEP was introduced by Bellare and Rogaway, and subsequently standardized in PKCS#1 v2 and RFC 2437.
The OAEP algorithm is a form of Feistel network which uses a pair of random oracles G and H to process the plaintext prior to asymmetric encryption. When combined with any secure trapdoor [one-way function|trapdoor one-way permutation], this processing is proved in the random oracle model to result in a combined scheme which is semantically secure under chosen plaintext attack (IND-CPA). When implemented with certain trapdoor permutations, OAEP is also proven to be secure against chosen ciphertext attack. OAEP can be used to build an all-or-nothing transform.
OAEP satisfies the following two goals:
- Add an element of randomness which can be used to convert a deterministic encryption scheme into a probabilistic scheme.
- Prevent partial decryption of ciphertexts by ensuring that an adversary cannot recover any portion of the plaintext without being able to invert the trapdoor one-way permutation.
Algorithm
In the diagram,MGF is the mask generating function, usually MGF1,Hash is the chosen hash function,hLen is the length of the output of the hash function in bytes,k is the length of the RSA modulus n in bytes,M is the message to be padded, with length mLen,L is an optional label to be associated with the message,PS is a byte string of null-bytes.- ⊕ is an XOR-Operation.
Encoding
- Hash the label L using the chosen hash function:
- Generate a padding string PS consisting of bytes.
- Concatenate lHash, PS, the single byte 0x01, and the message M to form a data block DB:. This data block has length bytes.
- Generate a random seed of length hLen.
- Use the mask generating function to generate a mask of the appropriate length for the data block:
- Mask the data block with the generated mask:
- Use the mask generating function to generate a mask of length hLen for the seed:
- Mask the seed with the generated mask:
- The encoded message is the byte 0x00 concatenated with the maskedSeed and maskedDB:
Decoding
Decoding works by reversing the steps taken in the encoding algorithm:- Hash the label L using the chosen hash function:
- To reverse step 9, split the encoded message EM into the byte 0x00, the maskedSeed and the maskedDB:
- Generate the seedMask which was used to mask the seed:
- To reverse step 8, recover the seed with the seedMask:
- Generate the dbMask which was used to mask the data block:
- To reverse step 6, recover the data block DB:
- To reverse step 3, split the data block into its parts:.
- # Verify that:
- #* lHash' is equal to the computed lHash
- #* PS only consists of bytes 0x00
- #* PS and M are separated by the 0x01 byte and
- #* the first byte of EM is the byte 0x00.
- # If any of these conditions aren't met, then the padding is invalid.