def verify(self, mhash, S):
"""Verify that a certain PKCS#1 PSS signature is authentic.
This function checks if the party holding the private half of the given
RSA key has really signed the message.
This function is called ``RSASSA-PSS-VERIFY``, and is specified in section
8.1.2 of RFC3447.
:Parameters:
mhash : hash object
The hash that was carried out over the message. This is an object
belonging to the `Crypto.Hash` module.
S : string
The signature that needs to be validated.
:Return: True if verification is correct. False otherwise.
"""
# TODO: Verify the key is RSA
# Set defaults for salt length and mask generation function
if self._saltLen == None:
sLen = mhash.digest_size
else:
sLen = self._saltLen
if self._mgfunc:
mgf = self._mgfunc
else:
mgf = lambda x,y: MGF1(x,y,mhash)
modBits = Crypto.Util.number.size(self._key.n)
# See 8.1.2 in RFC3447
k = ceil_div(modBits,8) # Convert from bits to bytes
# Step 1
if len(S) != k:
return False
# Step 2a (O2SIP), 2b (RSAVP1), and partially 2c (I2OSP)
# Note that signature must be smaller than the module
# but RSA.py won't complain about it.
# TODO: Fix RSA object; don't do it here.
em = self._key.encrypt(S, 0)[0]
# Step 2c
emLen = ceil_div(modBits-1,8)
em = bchr(0x00)*(emLen-len(em)) + em
# Step 3
try:
result = EMSA_PSS_VERIFY(mhash, em, modBits-1, mgf, sLen)
except ValueError:
return False
# Step 4
return result
评论列表
文章目录