Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change "salt length" parameter in PSS parameters for Bouncycastle

I want to sign a message.

I am using Bouncycastle (more accurately, the Android version, SpongyCastle).

My code is the following :

Signature instance = Signature.getInstance("SHA256withRSA/PSS", "BC");
MGF1ParameterSpec mgf1ParameterSpec = new MGF1ParameterSpec("SHA-256");
PSSParameterSpec pssParameterSpec = new PSSParameterSpec("SHA-256", "MGF1",mgf1ParameterSpec , 512, 1);
instance.setParameter(pssParameterSpec);
instance.initSign(privateKey);
instance.update(msg.getBytes());
byte[] signature = instance.sign();

When trying to check the signature with another device (and another technology), I noticed that the "salt_length" was not '512' but '32'. And more than that, if I modify the PSSParameterSpec constructor, it doesn't matter, the "salt_length" will always be '32', even if I don't use the instance.setParameter(pssParameterSpec).

It looks like instance.setParameter(pssParameterSpec) does nothing. Is it normal ?

Any idea how to change the value of "salt_length" ?

like image 594
Blusky Avatar asked Jan 22 '26 06:01

Blusky


1 Answers

After a lot of head-scratching it appears the order of initSign/initVerify and setParameter does matter on Android.

Using the same algorithm (without BouncyCastle in my case) requires setting the parameters after initialization, otherwise the default values are used:

signature.initVerify(publicKey);
signature.setParameter(new PSSParameterSpec(...));

This of course raises more questions about portability. The only lead I managed to find is this JDK bug report, although in my case the same provider is selected in both cases and still fails when setting the parameters before initialization.

like image 133
aergistal Avatar answered Jan 23 '26 18:01

aergistal