cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

Verification of signature created on ubuntu openssl fails on windows

Open ssrikanta opened this issue 6 years ago • 2 comments

Create hash and sign the with private key on the ubuntu terminal with the follows openssl dgst -sha256 -binary -out hash.bin data.jpg openssl pkeyutl -inkey private.key -in hash.bin -out data.sig -sign I could verify the signautre on the on ubuntu with command openssl pkeyutl -verify -pubin -inkey public.key -in hash.bin -sigfile data.sig But the same verification with cryptography fails on windows

import base64
import cryptography.exceptions
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.serialization import load_pem_public_key

with open("file_to_verify", 'rb') as f:
    payload_contents = f.read()

with open("public_key_filename", 'rb') as f:
    public_key = load_pem_public_key(f.read(), default_backend())

with open("signature_filename", 'rb') as f:
    signature = base64.b64decode(f.read())

try:
    public_key.verify(signature,payload_contents,
                      padding.PSS(mgf = padding.MGF1(hashes.SHA256()),
                                  salt_length = padding.PSS.MAX_LENGTH),
                      hashes.SHA256())

except cryptography.exceptions.InvalidSignature as e:
    print('ERROR: Payload and/or signature files failed verification!')

Platform: windows10 Python Verion: 3.8 cryptography: v3.0

ssrikanta avatar Oct 27 '20 17:10 ssrikanta

That's because pkeyutl does not sign things with PSS, it uses PKCS#1v1.5 if I recall correctly.

alex avatar Oct 27 '20 18:10 alex

@alex I have tried with PKCS#1v1.5 public_key.verify(signature, payload, padding.PKCS1v15(), hashes.SHA256()) Still no luck

ssrikanta avatar Nov 04 '20 05:11 ssrikanta