4

I have a piece of data [ 'payload' ] which is Base64 encoded. Then i have a 'signature' which contains the payload's signature. I have a public key. The signature algorithm is SHA512withRSA

How can I verify the authenticity of the data in Python ? I am using the following code to check, but it doesn't seem to be working

import base64
import hashlib
from Crypto.PublicKey import RSA 
from Crypto.Signature import SHA512
from Crypto.Hash import SHA512 
from base64 import b64decode 

# Public Key
key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEpFwIarbm48m6ueG+jhpt2vCGaqXZlwR/HPuL4zH1DQ/eWFbgQtVnrta8QhQz3ywLnbX6s7aecxUzzNJsTtS8VxKAYll4E1lJUqrNdWt8CU+TaUQuFm8vzLoPiYKEXl4bX5rzMQUMqA228gWuYmRFQnpduQTgnYIMO8XVUQXl5wIDAQAB"

# Base64 Encoded payload
payload = "some_string_payload"
decoded_payload = base64.b64decode(payload)

signature = "gw5K+WvO43673XBinZOmwgrZ3ttVuZ17/7SBnzqAAD4pgiwzYbZuEwn2lev6FW01f6TL0d9cNH4WtT53bQnTlhLQOZi4mHTTtM64O7MNljSA5zjJTUl77wXK/cJM+/G6R4YgYAnjydXAZjbMKY4Z9kV0qz2spdnS7Je7Q8I1xaU="
signature_algorithm = "SHA512withRSA"
keytype = "RSA"


m = hashlib.sha512()
m.update( key )
m.update( decoded_payload )
print m
m.hexdigest()
print m


keyDER = b64decode(key)
rsakey = RSA.importKey(keyDER)

signer = SHA512.new(rsakey) 

if signer.verify(m, b64decode(signature)):
    print "Verified"
else:
    print "Not Verified"
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Yogesh Singhal
  • 121
  • 1
  • 7
  • 2
    The signature you provided is 1024 bits long but the signature from SHA512 should only be 512 bits long. That means you either have the wrong signature or the wrong algorithm. Please update your question with new info. Also, would it be possible to show the code that produced the signature? We don't need the private key. – supersam654 Jul 06 '17 at 15:16
  • 1
    That is not even remotely close to how you verify an RSA signature. `signer.verify()` should throw an exception because signer is actually a Hash object and doesn't have a `verify` method. – President James K. Polk Jul 10 '17 at 15:06
  • 1
    Every question with text like "but it doesn't seem to be working" and that fails to include the full description of the problem should be closed as off-topic. – President James K. Polk Jul 10 '17 at 15:10

1 Answers1

0

The code in the question has a couple of mistakes, in order of appearance:

  • there are two different SHA512 implementations imported;
  • the payload is clearly not base 64 encoded, normal base 64 doesn't represent text nor does it contain _ characters (base-64-url however does);
  • the signature_algorithm and keytype variables are not even used;
  • the key should not be hashed to implement a normal PSS signature scheme;
  • the result of m.hexdigest() is ignored;
  • you cannot generate a signature generation class by SHA512.new(rsakey);

As indicated your code doesn't even compile, because SHA512 class cannot sign;

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • `AttributeError: module 'Crypto.Signature.PKCS1_PSS' has no attribute 'sign'` I think this should be `signer.sign(key)`. – monkut Mar 02 '22 at 10:42
  • I've removed the sample code as the question seems to hint at PKCS#1 v1.5 and not PSS. It's a very lazy question, I don't know why it got so many upvotes. I was contemplating to remove my answer entirely. Thanks for the warning, if you have any specific questions or remarks feel free to post them :) – Maarten Bodewes Mar 02 '22 at 10:54