2323from Crypto .Signature .pss import MGF1
2424import Crypto .Hash .SHA1
2525
26- from Crypto .Util .py3compat import bord , _copy_bytes
26+ from Crypto .Util .py3compat import _copy_bytes
2727import Crypto .Util .number
28- from Crypto .Util .number import ceil_div , bytes_to_long , long_to_bytes
29- from Crypto .Util .strxor import strxor
28+ from Crypto .Util .number import ceil_div , bytes_to_long , long_to_bytes
29+ from Crypto .Util .strxor import strxor
3030from Crypto import Random
31+ from ._pkcs1_oaep_decode import oaep_decode
32+
3133
3234class PKCS1OAEP_Cipher :
3335 """Cipher object for PKCS#1 v1.5 OAEP.
@@ -68,7 +70,7 @@ def __init__(self, key, hashAlgo, mgfunc, label, randfunc):
6870 if mgfunc :
6971 self ._mgf = mgfunc
7072 else :
71- self ._mgf = lambda x ,y : MGF1 (x ,y , self ._hashObj )
73+ self ._mgf = lambda x , y : MGF1 (x , y , self ._hashObj )
7274
7375 self ._label = _copy_bytes (None , None , label )
7476 self ._randfunc = randfunc
@@ -105,7 +107,7 @@ def encrypt(self, message):
105107
106108 # See 7.1.1 in RFC3447
107109 modBits = Crypto .Util .number .size (self ._key .n )
108- k = ceil_div (modBits , 8 ) # Convert from bits to bytes
110+ k = ceil_div (modBits , 8 ) # Convert from bits to bytes
109111 hLen = self ._hashObj .digest_size
110112 mLen = len (message )
111113
@@ -159,20 +161,18 @@ def decrypt(self, ciphertext):
159161
160162 # See 7.1.2 in RFC3447
161163 modBits = Crypto .Util .number .size (self ._key .n )
162- k = ceil_div (modBits ,8 ) # Convert from bits to bytes
164+ k = ceil_div (modBits , 8 ) # Convert from bits to bytes
163165 hLen = self ._hashObj .digest_size
164166
165167 # Step 1b and 1c
166- if len (ciphertext ) != k or k < hLen + 2 :
168+ if len (ciphertext ) != k or k < hLen + 2 :
167169 raise ValueError ("Ciphertext with incorrect length." )
168170 # Step 2a (O2SIP)
169171 ct_int = bytes_to_long (ciphertext )
170172 # Step 2b (RSADP) and step 2c (I2OSP)
171173 em = self ._key ._decrypt_to_bytes (ct_int )
172174 # Step 3a
173175 lHash = self ._hashObj .new (self ._label ).digest ()
174- # Step 3b
175- y = em [0 ]
176176 # y must be 0, but we MUST NOT check it here in order not to
177177 # allow attacks like Manger's (http://dl.acm.org/citation.cfm?id=704143)
178178 maskedSeed = em [1 :hLen + 1 ]
@@ -185,22 +185,17 @@ def decrypt(self, ciphertext):
185185 dbMask = self ._mgf (seed , k - hLen - 1 )
186186 # Step 3f
187187 db = strxor (maskedDB , dbMask )
188- # Step 3g
189- one_pos = hLen + db [hLen :].find (b'\x01 ' )
190- lHash1 = db [:hLen ]
191- invalid = bord (y ) | int (one_pos < hLen )
192- hash_compare = strxor (lHash1 , lHash )
193- for x in hash_compare :
194- invalid |= bord (x )
195- for x in db [hLen :one_pos ]:
196- invalid |= bord (x )
197- if invalid != 0 :
188+ # Step 3b + 3g
189+ res = oaep_decode (em , lHash , db )
190+ if res <= 0 :
198191 raise ValueError ("Incorrect decryption." )
199192 # Step 4
200- return db [one_pos + 1 :]
193+ return db [res :]
194+
201195
202196def new (key , hashAlgo = None , mgfunc = None , label = b'' , randfunc = None ):
203- """Return a cipher object :class:`PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption.
197+ """Return a cipher object :class:`PKCS1OAEP_Cipher`
198+ that can be used to perform PKCS#1 OAEP encryption or decryption.
204199
205200 :param key:
206201 The key object to use to encrypt or decrypt the message.
@@ -234,4 +229,3 @@ def new(key, hashAlgo=None, mgfunc=None, label=b'', randfunc=None):
234229 if randfunc is None :
235230 randfunc = Random .get_random_bytes
236231 return PKCS1OAEP_Cipher (key , hashAlgo , mgfunc , label , randfunc )
237-
0 commit comments