bcgit / bc-csharp

BouncyCastle.NET Cryptography Library (Mirror)
https://www.bouncycastle.org/csharp
MIT License
1.68k stars 558 forks source link

FIx method Write(ReadOnlySpan<byte>) in LimitedBuffer #524

Closed harrison314 closed 8 months ago

harrison314 commented 8 months ago

Fix LimitedBuffer.Write(ReadOnlySpan<byte>) method.

The error manifested as System.InvalidOperationException: Incorrect prehash size when using RSA PSS.

Code for replication fixed bug:

SecureRandom secureRandom = new SecureRandom();
Org.BouncyCastle.Crypto.Generators.RsaKeyPairGenerator generator = new Org.BouncyCastle.Crypto.Generators.RsaKeyPairGenerator();
generator.Init(new RsaKeyGenerationParameters(new Org.BouncyCastle.Math.BigInteger(1, new byte[] { 1, 0, 1 }),
    secureRandom,
    2048,
    100));

var keyPair = generator.GenerateKeyPair();

IDigest contentDigest = new Sha256Digest();
IDigest mgfDigest = new Sha256Digest();

ISigner signer = PssSigner.CreateRawSigner(new RsaBlindedEngine(),
     contentDigest,
     mgfDigest,
     32,
     PssSigner.TrailerImplicit);

byte[] hashToSign = new byte[32];
secureRandom.NextBytes(hashToSign);

signer.Init(true, keyPair.Private);
signer.BlockUpdate(hashToSign);
byte[] signature = signer.GenerateSignature();
peterdettman commented 8 months ago

Merged, thanks. Also added a regression test.