PKCS1_v1_5.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/PKCS1-v1_5.py : PKCS#1 v1.5
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. __all__ = [ 'new', 'PKCS115_Cipher' ]
  23. from tls.Crypto.Util.number import ceil_div, bytes_to_long, long_to_bytes
  24. from tls.Crypto.Util.py3compat import bord, _copy_bytes
  25. import Crypto.Util.number
  26. from tls.Crypto import Random
  27. class PKCS115_Cipher:
  28. """This cipher can perform PKCS#1 v1.5 RSA encryption or decryption.
  29. Do not instantiate directly. Use :func:`Crypto.Cipher.PKCS1_v1_5.new` instead."""
  30. def __init__(self, key, randfunc):
  31. """Initialize this PKCS#1 v1.5 cipher object.
  32. :Parameters:
  33. key : an RSA key object
  34. If a private half is given, both encryption and decryption are possible.
  35. If a public half is given, only encryption is possible.
  36. randfunc : callable
  37. Function that returns random bytes.
  38. """
  39. self._key = key
  40. self._randfunc = randfunc
  41. def can_encrypt(self):
  42. """Return True if this cipher object can be used for encryption."""
  43. return self._key.can_encrypt()
  44. def can_decrypt(self):
  45. """Return True if this cipher object can be used for decryption."""
  46. return self._key.can_decrypt()
  47. def encrypt(self, message):
  48. """Produce the PKCS#1 v1.5 encryption of a message.
  49. This function is named ``RSAES-PKCS1-V1_5-ENCRYPT``, and it is specified in
  50. `section 7.2.1 of RFC8017
  51. <https://tools.ietf.org/html/rfc8017#page-28>`_.
  52. :param message:
  53. The message to encrypt, also known as plaintext. It can be of
  54. variable length, but not longer than the RSA modulus (in bytes) minus 11.
  55. :type message: bytes/bytearray/memoryview
  56. :Returns: A byte string, the ciphertext in which the message is encrypted.
  57. It is as long as the RSA modulus (in bytes).
  58. :Raises ValueError:
  59. If the RSA key length is not sufficiently long to deal with the given
  60. message.
  61. """
  62. # See 7.2.1 in RFC8017
  63. modBits = Crypto.Util.number.size(self._key.n)
  64. k = ceil_div(modBits,8) # Convert from bits to bytes
  65. mLen = len(message)
  66. # Step 1
  67. if mLen > k - 11:
  68. raise ValueError("Plaintext is too long.")
  69. # Step 2a
  70. ps = []
  71. while len(ps) != k - mLen - 3:
  72. new_byte = self._randfunc(1)
  73. if bord(new_byte[0]) == 0x00:
  74. continue
  75. ps.append(new_byte)
  76. ps = b"".join(ps)
  77. assert(len(ps) == k - mLen - 3)
  78. # Step 2b
  79. em = b'\x00\x02' + ps + b'\x00' + _copy_bytes(None, None, message)
  80. # Step 3a (OS2IP)
  81. em_int = bytes_to_long(em)
  82. # Step 3b (RSAEP)
  83. m_int = self._key._encrypt(em_int)
  84. # Step 3c (I2OSP)
  85. c = long_to_bytes(m_int, k)
  86. return c
  87. def decrypt(self, ciphertext, sentinel):
  88. r"""Decrypt a PKCS#1 v1.5 ciphertext.
  89. This function is named ``RSAES-PKCS1-V1_5-DECRYPT``, and is specified in
  90. `section 7.2.2 of RFC8017
  91. <https://tools.ietf.org/html/rfc8017#page-29>`_.
  92. :param ciphertext:
  93. The ciphertext that contains the message to recover.
  94. :type ciphertext: bytes/bytearray/memoryview
  95. :param sentinel:
  96. The object to return whenever an error is detected.
  97. :type sentinel: any type
  98. :Returns: A byte string. It is either the original message or the ``sentinel`` (in case of an error).
  99. :Raises ValueError:
  100. If the ciphertext length is incorrect
  101. :Raises TypeError:
  102. If the RSA key has no private half (i.e. it cannot be used for
  103. decyption).
  104. .. warning::
  105. You should **never** let the party who submitted the ciphertext know that
  106. this function returned the ``sentinel`` value.
  107. Armed with such knowledge (for a fair amount of carefully crafted but invalid ciphertexts),
  108. an attacker is able to recontruct the plaintext of any other encryption that were carried out
  109. with the same RSA public key (see `Bleichenbacher's`__ attack).
  110. In general, it should not be possible for the other party to distinguish
  111. whether processing at the server side failed because the value returned
  112. was a ``sentinel`` as opposed to a random, invalid message.
  113. In fact, the second option is not that unlikely: encryption done according to PKCS#1 v1.5
  114. embeds no good integrity check. There is roughly one chance
  115. in 2\ :sup:`16` for a random ciphertext to be returned as a valid message
  116. (although random looking).
  117. It is therefore advisabled to:
  118. 1. Select as ``sentinel`` a value that resembles a plausable random, invalid message.
  119. 2. Not report back an error as soon as you detect a ``sentinel`` value.
  120. Put differently, you should not explicitly check if the returned value is the ``sentinel`` or not.
  121. 3. Cover all possible errors with a single, generic error indicator.
  122. 4. Embed into the definition of ``message`` (at the protocol level) a digest (e.g. ``SHA-1``).
  123. It is recommended for it to be the rightmost part ``message``.
  124. 5. Where possible, monitor the number of errors due to ciphertexts originating from the same party,
  125. and slow down the rate of the requests from such party (or even blacklist it altogether).
  126. **If you are designing a new protocol, consider using the more robust PKCS#1 OAEP.**
  127. .. __: http://www.bell-labs.com/user/bleichen/papers/pkcs.ps
  128. """
  129. # See 7.2.1 in RFC3447
  130. modBits = Crypto.Util.number.size(self._key.n)
  131. k = ceil_div(modBits,8) # Convert from bits to bytes
  132. # Step 1
  133. if len(ciphertext) != k:
  134. raise ValueError("Ciphertext with incorrect length.")
  135. # Step 2a (O2SIP)
  136. ct_int = bytes_to_long(ciphertext)
  137. # Step 2b (RSADP)
  138. m_int = self._key._decrypt(ct_int)
  139. # Complete step 2c (I2OSP)
  140. em = long_to_bytes(m_int, k)
  141. # Step 3
  142. sep = em.find(b'\x00', 2)
  143. if not em.startswith(b'\x00\x02') or sep < 10:
  144. return sentinel
  145. # Step 4
  146. return em[sep + 1:]
  147. def new(key, randfunc=None):
  148. """Create a cipher for performing PKCS#1 v1.5 encryption or decryption.
  149. :param key:
  150. The key to use to encrypt or decrypt the message. This is a `Crypto.PublicKey.RSA` object.
  151. Decryption is only possible if *key* is a private RSA key.
  152. :type key: RSA key object
  153. :param randfunc:
  154. Function that return random bytes.
  155. The default is :func:`Crypto.Random.get_random_bytes`.
  156. :type randfunc: callable
  157. :returns: A cipher object `PKCS115_Cipher`.
  158. """
  159. if randfunc is None:
  160. randfunc = Random.get_random_bytes
  161. return PKCS115_Cipher(key, randfunc)