PKCS1_OAEP.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Cipher/PKCS1_OAEP.py : PKCS#1 OAEP
  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. from tls.Crypto.Signature.pss import MGF1
  23. import Crypto.Hash.SHA1
  24. from tls.Crypto.Util.py3compat import bord, _copy_bytes
  25. import Crypto.Util.number
  26. from Crypto.Util.number import ceil_div, bytes_to_long, long_to_bytes
  27. from Crypto.Util.strxor import strxor
  28. from tls.Crypto import Random
  29. class PKCS1OAEP_Cipher:
  30. """Cipher object for PKCS#1 v1.5 OAEP.
  31. Do not create directly: use :func:`new` instead."""
  32. def __init__(self, key, hashAlgo, mgfunc, label, randfunc):
  33. """Initialize this PKCS#1 OAEP cipher object.
  34. :Parameters:
  35. key : an RSA key object
  36. If a private half is given, both encryption and decryption are possible.
  37. If a public half is given, only encryption is possible.
  38. hashAlgo : hash object
  39. The hash function to use. This can be a module under `Crypto.Hash`
  40. or an existing hash object created from any of such modules. If not specified,
  41. `Crypto.Hash.SHA1` is used.
  42. mgfunc : callable
  43. A mask generation function that accepts two parameters: a string to
  44. use as seed, and the lenth of the mask to generate, in bytes.
  45. If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice).
  46. label : bytes/bytearray/memoryview
  47. A label to apply to this particular encryption. If not specified,
  48. an empty string is used. Specifying a label does not improve
  49. security.
  50. randfunc : callable
  51. A function that returns random bytes.
  52. :attention: Modify the mask generation function only if you know what you are doing.
  53. Sender and receiver must use the same one.
  54. """
  55. self._key = key
  56. if hashAlgo:
  57. self._hashObj = hashAlgo
  58. else:
  59. self._hashObj = Crypto.Hash.SHA1
  60. if mgfunc:
  61. self._mgf = mgfunc
  62. else:
  63. self._mgf = lambda x,y: MGF1(x,y,self._hashObj)
  64. self._label = _copy_bytes(None, None, label)
  65. self._randfunc = randfunc
  66. def can_encrypt(self):
  67. """Legacy function to check if you can call :meth:`encrypt`.
  68. .. deprecated:: 3.0"""
  69. return self._key.can_encrypt()
  70. def can_decrypt(self):
  71. """Legacy function to check if you can call :meth:`decrypt`.
  72. .. deprecated:: 3.0"""
  73. return self._key.can_decrypt()
  74. def encrypt(self, message):
  75. """Encrypt a message with PKCS#1 OAEP.
  76. :param message:
  77. The message to encrypt, also known as plaintext. It can be of
  78. variable length, but not longer than the RSA modulus (in bytes)
  79. minus 2, minus twice the hash output size.
  80. For instance, if you use RSA 2048 and SHA-256, the longest message
  81. you can encrypt is 190 byte long.
  82. :type message: bytes/bytearray/memoryview
  83. :returns: The ciphertext, as large as the RSA modulus.
  84. :rtype: bytes
  85. :raises ValueError:
  86. if the message is too long.
  87. """
  88. # See 7.1.1 in RFC3447
  89. modBits = Crypto.Util.number.size(self._key.n)
  90. k = ceil_div(modBits, 8) # Convert from bits to bytes
  91. hLen = self._hashObj.digest_size
  92. mLen = len(message)
  93. # Step 1b
  94. ps_len = k - mLen - 2 * hLen - 2
  95. if ps_len < 0:
  96. raise ValueError("Plaintext is too long.")
  97. # Step 2a
  98. lHash = self._hashObj.new(self._label).digest()
  99. # Step 2b
  100. ps = b'\x00' * ps_len
  101. # Step 2c
  102. db = lHash + ps + b'\x01' + _copy_bytes(None, None, message)
  103. # Step 2d
  104. ros = self._randfunc(hLen)
  105. # Step 2e
  106. dbMask = self._mgf(ros, k-hLen-1)
  107. # Step 2f
  108. maskedDB = strxor(db, dbMask)
  109. # Step 2g
  110. seedMask = self._mgf(maskedDB, hLen)
  111. # Step 2h
  112. maskedSeed = strxor(ros, seedMask)
  113. # Step 2i
  114. em = b'\x00' + maskedSeed + maskedDB
  115. # Step 3a (OS2IP)
  116. em_int = bytes_to_long(em)
  117. # Step 3b (RSAEP)
  118. m_int = self._key._encrypt(em_int)
  119. # Step 3c (I2OSP)
  120. c = long_to_bytes(m_int, k)
  121. return c
  122. def decrypt(self, ciphertext):
  123. """Decrypt a message with PKCS#1 OAEP.
  124. :param ciphertext: The encrypted message.
  125. :type ciphertext: bytes/bytearray/memoryview
  126. :returns: The original message (plaintext).
  127. :rtype: bytes
  128. :raises ValueError:
  129. if the ciphertext has the wrong length, or if decryption
  130. fails the integrity check (in which case, the decryption
  131. key is probably wrong).
  132. :raises TypeError:
  133. if the RSA key has no private half (i.e. you are trying
  134. to decrypt using a public key).
  135. """
  136. # See 7.1.2 in RFC3447
  137. modBits = Crypto.Util.number.size(self._key.n)
  138. k = ceil_div(modBits,8) # Convert from bits to bytes
  139. hLen = self._hashObj.digest_size
  140. # Step 1b and 1c
  141. if len(ciphertext) != k or k<hLen+2:
  142. raise ValueError("Ciphertext with incorrect length.")
  143. # Step 2a (O2SIP)
  144. ct_int = bytes_to_long(ciphertext)
  145. # Step 2b (RSADP)
  146. m_int = self._key._decrypt(ct_int)
  147. # Complete step 2c (I2OSP)
  148. em = long_to_bytes(m_int, k)
  149. # Step 3a
  150. lHash = self._hashObj.new(self._label).digest()
  151. # Step 3b
  152. y = em[0]
  153. # y must be 0, but we MUST NOT check it here in order not to
  154. # allow attacks like Manger's (http://dl.acm.org/citation.cfm?id=704143)
  155. maskedSeed = em[1:hLen+1]
  156. maskedDB = em[hLen+1:]
  157. # Step 3c
  158. seedMask = self._mgf(maskedDB, hLen)
  159. # Step 3d
  160. seed = strxor(maskedSeed, seedMask)
  161. # Step 3e
  162. dbMask = self._mgf(seed, k-hLen-1)
  163. # Step 3f
  164. db = strxor(maskedDB, dbMask)
  165. # Step 3g
  166. one_pos = db[hLen:].find(b'\x01')
  167. lHash1 = db[:hLen]
  168. invalid = bord(y) | int(one_pos < 0)
  169. hash_compare = strxor(lHash1, lHash)
  170. for x in hash_compare:
  171. invalid |= bord(x)
  172. for x in db[hLen:one_pos]:
  173. invalid |= bord(x)
  174. if invalid != 0:
  175. raise ValueError("Incorrect decryption.")
  176. # Step 4
  177. return db[hLen + one_pos + 1:]
  178. def new(key, hashAlgo=None, mgfunc=None, label=b'', randfunc=None):
  179. """Return a cipher object :class:`PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption.
  180. :param key:
  181. The key object to use to encrypt or decrypt the message.
  182. Decryption is only possible with a private RSA key.
  183. :type key: RSA key object
  184. :param hashAlgo:
  185. The hash function to use. This can be a module under `Crypto.Hash`
  186. or an existing hash object created from any of such modules.
  187. If not specified, `Crypto.Hash.SHA1` is used.
  188. :type hashAlgo: hash object
  189. :param mgfunc:
  190. A mask generation function that accepts two parameters: a string to
  191. use as seed, and the lenth of the mask to generate, in bytes.
  192. If not specified, the standard MGF1 consistent with ``hashAlgo`` is used (a safe choice).
  193. :type mgfunc: callable
  194. :param label:
  195. A label to apply to this particular encryption. If not specified,
  196. an empty string is used. Specifying a label does not improve
  197. security.
  198. :type label: bytes/bytearray/memoryview
  199. :param randfunc:
  200. A function that returns random bytes.
  201. The default is `Random.get_random_bytes`.
  202. :type randfunc: callable
  203. """
  204. if randfunc is None:
  205. randfunc = Random.get_random_bytes
  206. return PKCS1OAEP_Cipher(key, hashAlgo, mgfunc, label, randfunc)