bc-csharp icon indicating copy to clipboard operation
bc-csharp copied to clipboard

GCMBlockCipher dup nonce check should use Array.Copy.

Open ohsorry opened this issue 5 years ago • 1 comments

https://github.com/bcgit/bc-csharp/blob/master/crypto/src/crypto/modes/GCMBlockCipher.cs

            if (forEncryption)
            {
                if (nonce != null && Arrays.AreEqual(nonce, newNonce))
                {
                    if (keyParam == null)
                    {
                        throw new ArgumentException("cannot reuse nonce for GCM encryption");
                    }
                    if (lastKey != null && Arrays.AreEqual(lastKey, keyParam.GetKey()))
                    {
                        throw new ArgumentException("cannot reuse nonce for GCM encryption");
                    }
                }
            }

            nonce = newNonce; //<--here, a deep copy will be better. 

👍

ohsorry avatar Feb 24 '20 00:02 ohsorry

This should be implemented as Arrays.AreEqual checks reference equality as well. If IV buffer is reused by the user, an exception will be thrown, which happened in my case.

@peterdettman

aizuon avatar Feb 28 '22 10:02 aizuon