summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2016-01-12 21:19:19 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2016-01-12 21:19:19 +0700
commitd5a2eb915cafb5e614ca3e2bbc7f02a7034ac0bc (patch)
tree4339df09d69a02880c5ca16334e03dc3a631c46a
parentUse utility method (diff)
downloadBouncyCastle.NET-ed25519-d5a2eb915cafb5e614ca3e2bbc7f02a7034ac0bc.tar.xz
Add block limit to GCM
-rw-r--r--crypto/src/crypto/modes/GCMBlockCipher.cs8
1 files changed, 8 insertions, 0 deletions
diff --git a/crypto/src/crypto/modes/GCMBlockCipher.cs b/crypto/src/crypto/modes/GCMBlockCipher.cs
index ecebc3de9..9f2869d07 100644
--- a/crypto/src/crypto/modes/GCMBlockCipher.cs
+++ b/crypto/src/crypto/modes/GCMBlockCipher.cs
@@ -34,6 +34,7 @@ namespace Org.BouncyCastle.Crypto.Modes
         private byte[]		macBlock;
         private byte[]      S, S_at, S_atPre;
         private byte[]      counter;
+        private uint        blocksRemaining;
         private int         bufOff;
         private ulong		totalLength;
         private byte[]      atBlock;
@@ -173,6 +174,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             this.atLength = 0;
             this.atLengthPre = 0;
             this.counter = Arrays.Clone(J0);
+            this.blocksRemaining = uint.MaxValue;
             this.bufOff = 0;
             this.totalLength = 0;
 
@@ -447,6 +449,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             atLength = 0;
             atLengthPre = 0;
             counter = Arrays.Clone(J0);
+            blocksRemaining = uint.MaxValue;
             bufOff = 0;
             totalLength = 0;
 
@@ -513,6 +516,11 @@ namespace Org.BouncyCastle.Crypto.Modes
 
         private byte[] GetNextCounterBlock()
         {
+            if (blocksRemaining == 0)
+                throw new InvalidOperationException("Attempt to process too many blocks");
+
+            blocksRemaining--;
+
             uint c = 1;
             c += counter[15]; counter[15] = (byte)c; c >>= 8;
             c += counter[14]; counter[14] = (byte)c; c >>= 8;