First off, I'm still new to crypto/signing so bear with any misuse of terms please.
I need to create a signature in C# that is getting verified by a Python library. In Python, it's a simple chunk of code to decode/verify the signature:
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
public_key.verify(signature, payload_contents,
padding.PSS(mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH,
), hashes.SHA256(), )
My current C# code looks like this:
private static string CreateSignature(byte[] data, string privateKeyFileLocation)
{
var cert = new X509Certificate2(privateKeyFileLocation);
byte[] signedBytes;
using (var rsa = cert.GetRSAPrivateKey())
{
signedBytes = rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
}
var finalString = Convert.ToBase64String(signedBytes);
return finalString;
}
However, the signature is failing the verification check in the Python code. It looks like PSS padding in the .Net libraries defaults to using MGF1. However, I believe I'm having issues due to the mask generation function (MGF1) using a 256-bit hash in the Python code, but defaulting to SHA1 in C#. I've waded through the .Net C# documentation and it looks like there is no way to override this. I looked into Bouncy Castle's C# documentation and am just having trouble finding any kind of similar example of how to set padding with custom params. Does anyone have experience in this area and can lend a few hints?