2
$\begingroup$

I am wondering why CMS (PKCS#7) Signed Data contains always a different signature of the original data when the SHA256withRSA signature algorithm is used. (using Bouncy Castle by the way)

The SHA256withRSA is using the SHA-256 as a message digest algorithm and EMSA-PKCS1-v1_5 for encoding the message digest before signature generation operation with the RSA private key.

As these algorithms are deterministic, I would expect that each time I would create a CMS Signed Data the signature of the data will be the same. However, it is not. I can verify it with the opposite process. I will decrypt the signature using the RSA public key, decode EMSA-PKCS1-v1_5 and the resulting hash is different than hash of the original message.

Someone able to explain me how it works, why the signature is always different?

This is what should be equal from my point of view:

Message1 -> generate CMSSignedData(private key)
Generated CMSSignedData -> get signature -> decrypt(signature) -> decode -> MD2

SHA256(Message1) = MD1

MD1 != MD2 <---- I would expect that this will be equal !!!
$\endgroup$

1 Answer 1

2
$\begingroup$

That's likely because the data used for the signature generation actually changes between the signing. That would e.g. be the case if that data contains the signing time. Note that within CMS the data is not directly signed; it is first hashed, put in a structure and then that's what is getting signed.

Also note that inclusion of the local time generated by the system is not the same thing as using a time stamping service.

$\endgroup$
1
  • $\begingroup$ With signedAttrs the hash in the PKCS1v1.5 (RSA) signature is always different from the hash of the data, even without signingTime, because the RSA signature is done over (the hash of) signedAttrs not the data directly. See from just a few days ago crypto.stackexchange.com/questions/87009/… $\endgroup$ Commented Dec 22, 2020 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.