gmpy2_pkcs10aep_cipher.py 10 KB

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