summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-04-05 14:49:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-04-05 14:49:12 +0700
commit524db20702dbeeac2dd9b48558e0a18d8c0199df (patch)
tree32b34fc6afcea9afd03c826e57b3c3c74e5c9bb7
parentTLS: Use more appropriate method to check for ECDH curve (diff)
downloadBouncyCastle.NET-ed25519-524db20702dbeeac2dd9b48558e0a18d8c0199df.tar.xz
Fix CCM input length check
-rw-r--r--crypto/src/crypto/modes/CcmBlockCipher.cs24
1 files changed, 22 insertions, 2 deletions
diff --git a/crypto/src/crypto/modes/CcmBlockCipher.cs b/crypto/src/crypto/modes/CcmBlockCipher.cs
index fa583fdef..2f9ec216f 100644
--- a/crypto/src/crypto/modes/CcmBlockCipher.cs
+++ b/crypto/src/crypto/modes/CcmBlockCipher.cs
@@ -274,7 +274,17 @@ namespace Org.BouncyCastle.Crypto.Modes
             if (q < 4)
             {
                 int limitLen = 1 << (8 * q);
-                if (inLen >= limitLen)
+
+                // no input length adjustment for encryption
+                int inputAdjustment = 0;
+
+                if (!forEncryption)
+                {
+                    // input includes 16 additional bytes: CCM flags and n+q values.
+                    inputAdjustment = 1 /* flags */ + 15 /* n + q */;
+                }
+
+                if (inLen - inputAdjustment >= limitLen)
                     throw new InvalidOperationException("CCM packet too large for choice of q.");
             }
 
@@ -375,7 +385,17 @@ namespace Org.BouncyCastle.Crypto.Modes
             if (q < 4)
             {
                 int limitLen = 1 << (8 * q);
-                if (inLen >= limitLen)
+
+                // no input length adjustment for encryption
+                int inputAdjustment = 0;
+
+                if (!forEncryption)
+                {
+                    // input includes 16 additional bytes: CCM flags and n+q values.
+                    inputAdjustment = 1 /* flags */ + 15 /* n + q */;
+                }
+
+                if (inLen - inputAdjustment >= limitLen)
                     throw new InvalidOperationException("CCM packet too large for choice of q.");
             }