#NEWLINE# def EMSA_PSS_VERIFY( mhash , em , emBits , mgf , sLen ) : #NEWLINE# #INDENT# '\n    Implement the ``EMSA-PSS-VERIFY`` function, as defined\n    in PKCS#1 v2.1 (RFC3447, 9.1.2).\n\n    ``EMSA-PSS-VERIFY`` actually accepts the message ``M`` as input,\n    and hash it internally. Here, we expect that the message has already\n    been hashed instead.\n\n    :Parameters:\n     mhash : hash object\n            The hash object that holds the digest of the message to be verified.\n     em : string\n            The signature to verify, therefore proving that the sender really signed\n            the message that was received.\n     emBits : int\n            Length of the final encoding (em), in bits.\n     mgf : callable\n            A mask generation function that accepts two parameters: a string to\n            use as seed, and the lenth of the mask to generate, in bytes.\n     sLen : int\n            Length of the salt, in bytes.\n\n    :Return: 0 if the encoding is consistent, 1 if it is inconsistent.\n\n    :Raise ValueError:\n        When digest or salt length are too big.\n    ' #NEWLINE# emLen = ceil_div ( emBits , 8 ) #NEWLINE# lmask = 0 #NEWLINE# for i in xrange ( ( ( 8 * emLen ) - emBits ) ) : #NEWLINE# #INDENT# lmask = ( ( lmask >> 1 ) | 128 ) #NEWLINE# #UNINDENT# if ( emLen < ( ( mhash . digest_size + sLen ) + 2 ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# if ( ord ( em [ ( - 1 ) : ] ) != 188 ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# maskedDB = em [ : ( ( emLen - mhash . digest_size ) - 1 ) ] #NEWLINE# h = em [ ( ( emLen - mhash . digest_size ) - 1 ) : ( - 1 ) ] #NEWLINE# if ( lmask & bord ( salt [ 0 ] ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# dbMask = mgf ( h , ( ( emLen - mhash . digest_size ) - 1 ) ) #NEWLINE# db = strxor ( maskedDB , dbMask ) #NEWLINE# db = ( bchr ( ( bord ( db [ 0 ] ) & ( ~ lmask ) ) ) + db [ 1 : ] ) #NEWLINE# if ( not db . startswith ( ( ( bchr ( 0 ) * ( ( ( emLen - mhash . digest_size ) - sLen ) - 2 ) ) + bchr ( 1 ) ) ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# salt = b ( '' ) #NEWLINE# if sLen : #NEWLINE# #INDENT# salt = db [ ( - sLen ) : ] #NEWLINE# #UNINDENT# hp = mhash . new ( ( ( ( bchr ( 0 ) * 8 ) + mhash . digest ( ) ) + salt ) ) . digest ( ) #NEWLINE# if ( h != hp ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# return True