PKCS#1 v1.5 encryption (RSA)

Warning

Use PKCS#1 OAEP (RSA) instead. This module is provided only for legacy purposes.

See RFC8017 or the original RSA Labs specification .

This scheme is more properly called RSAES-PKCS1-v1_5.

As an example, a sender may encrypt a secret AES key in this way:

>>> from Crypto.Cipher import PKCS1_v1_5
>>> from Crypto.PublicKey import RSA
>>> from Crypto.Random import get_random_bytes
>>>
>>> aes_key = get_random_bytes(16)
>>>
>>> rsa_key = RSA.importKey(open('pubkey.der').read())
>>> cipher = PKCS1_v1_5.new(rsa_key)
>>> ciphertext = cipher.encrypt(aes_key)

At the receiver side, decryption can be done using the private part of the RSA key:

>>> rsa_key = RSA.importKey(open('privkey.der').read())
>>>
>>> cipher = PKCS1_v1_5.new(rsa_key)
>>> aes_key = cipher.decrypt(ciphertext, expected_pt_len=16)

Decryption never reports an incorrect padding: in that case, it returns a pseudo-random message instead, which depends only on the private key and on the ciphertext (implicit rejection, as defined in draft-irtf-cfrg-rsa-guidance). In the example above, aes_key is then a pseudo-random 16-byte key, and the protocol fails later (for instance, when the AES key is used to decrypt and authenticate some data), in the same way as with a ciphertext that has a correct padding but that encrypts the wrong key.

Note

Before version 4.3, the method decrypt() required a sentinel, the value to return in case of an incorrect padding. It is still possible to pass a sentinel (and then there is no implicit rejection), but it is hard to use safely: a caller (or the party that submitted the ciphertext) can often find out when it is returned, and that is the oracle exploited by Bleichenbacher’s attack.

class Crypto.Cipher.PKCS1_v1_5.PKCS115_Cipher(key, randfunc)

This cipher can perform PKCS#1 v1.5 RSA encryption or decryption. Do not instantiate directly. Use Crypto.Cipher.PKCS1_v1_5.new() instead.

can_decrypt()

Return True if this cipher object can be used for decryption.

Return type:

bool

can_encrypt()

Return True if this cipher object can be used for encryption.

Return type:

bool

decrypt(ciphertext, sentinel=<no sentinel>, expected_pt_len=0)

Decrypt a PKCS#1 v1.5 ciphertext.

This is the function RSAES-PKCS1-V1_5-DECRYPT specified in section 7.2.2 of RFC8017, with the implicit rejection of section 7.2 of draft-irtf-cfrg-rsa-guidance: when the padding is not correct, the function does not fail, and it returns instead a pseudo-random message. That message only depends on the private key and on the ciphertext (the same ciphertext always results in the same message), and it cannot be told apart from the message of a ciphertext with correct padding by those who do not know the private key.

Parameters:
  • ciphertext (bytes/bytearray/memoryview) – The ciphertext that contains the message to recover.

  • expected_pt_len (integer) – The length the plaintext is known to have, or 0 if unknown. When it is not 0, a plaintext of a different length is also rejected (and the pseudo-random message has this length).

  • sentinel (any type) – The object to return when the padding is not correct. If present, the function does not perform implicit rejection. Do not pass it, unless the party that submitted the ciphertext can never find out (directly or indirectly) that sentinel was returned.

Return type:

Union[bytes, TypeVar(T)]

Returns (byte string):

The original message, or the pseudo-random message (or sentinel, if passed) in case the padding is not correct.

Raises:
  • ValueError – if the ciphertext does not have the length of the modulus, or if it is not smaller than the modulus.

  • TypeError – if the RSA key is not private.

Warning

PKCS#1 v1.5 decryption is intrinsically vulnerable to Bleichenbacher’s attack. Use PKCS#1 OAEP instead.

Implicit rejection hides from the caller if the padding was correct. The protocol that uses this function must then also treat in the same way messages that are random or that have an unexpected length (for instance, they must fail later and in the same way as a message that has the expected format but the wrong value).

With a sentinel, the result of the padding check is something that the caller can detect, and any difference in the behaviour of the application that follows from it is the oracle exploited by the attack. This includes subtle differences: for instance, a random sentinel changes at each call, whereas the plaintext of a ciphertext with correct padding does not.

encrypt(message)

Produce the PKCS#1 v1.5 encryption of a message.

This function is named RSAES-PKCS1-V1_5-ENCRYPT, and it is specified in section 7.2.1 of RFC8017.

Parameters:

message (bytes/bytearray/memoryview) – The message to encrypt, also known as plaintext. It can be of variable length, but not longer than the RSA modulus (in bytes) minus 11.

Returns:

A byte string, the ciphertext in which the message is encrypted. It is as long as the RSA modulus (in bytes).

Raises ValueError:

If the RSA key length is not sufficiently long to deal with the given message.

Return type:

bytes

Crypto.Cipher.PKCS1_v1_5.new(key, randfunc=None)

Create a cipher for performing PKCS#1 v1.5 encryption or decryption.

Parameters:
  • key (RSA key object) – The key to use to encrypt or decrypt the message. This is a Crypto.PublicKey.RSA object. Decryption is only possible if key is a private RSA key.

  • randfunc (callable) – Function that return random bytes. The default is Crypto.Random.get_random_bytes().

Return type:

PKCS115_Cipher

Returns:

A cipher object PKCS115_Cipher.