summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorDavid Hook <dgh@bouncycastle.org>2017-07-11 10:50:43 +1000
committerDavid Hook <dgh@bouncycastle.org>2017-07-11 10:50:43 +1000
commit7e55ab06a7857d6ac8d11cf842fbefbd7b8d67e2 (patch)
treeece7231eb49a0355ea0446ad3b681c011b3ff240 /crypto
parentfixed reset issue (diff)
downloadBouncyCastle.NET-ed25519-7e55ab06a7857d6ac8d11cf842fbefbd7b8d67e2.tar.xz
CCM update
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/modes/KCcmBlockCipher.cs30
-rw-r--r--crypto/test/src/crypto/test/DSTU7624Test.cs14
2 files changed, 26 insertions, 18 deletions
diff --git a/crypto/src/crypto/modes/KCcmBlockCipher.cs b/crypto/src/crypto/modes/KCcmBlockCipher.cs
index 8f1ed9c6f..1911ba53b 100644
--- a/crypto/src/crypto/modes/KCcmBlockCipher.cs
+++ b/crypto/src/crypto/modes/KCcmBlockCipher.cs
@@ -36,16 +36,12 @@ namespace Org.BouncyCastle.Crypto.Modes
         private readonly MemoryStream data = new MemoryStream();
 
         /*
-        *  Nb is a parameter specified in CCM mode of DSTU7624 standard.
-        *  This parameter specifies maximum possible length of input. It should
-        *  be calculated as follows: Nb = 1/8 * (-3 + log[2]Nmax) + 1,
-        *  where Nmax - length of input message in bits. For practical reasons
-        *  Nmax usually less than 4Gb, e.g. for Nmax = 2^32 - 1, Nb = 4.
+        *  
         *
         */
         private int Nb_ = 4;
 
-        public void setNb(int Nb)
+        private void setNb(int Nb)
         {
             if (Nb == 4 || Nb == 6 || Nb == 8)
             {
@@ -57,7 +53,26 @@ namespace Org.BouncyCastle.Crypto.Modes
             }
         }
 
-        public KCcmBlockCipher(IBlockCipher engine)
+        /// <summary>
+        /// Base constructor. Nb value is set to 4.
+        /// </summary>
+        /// <param name="engine">base cipher to use under CCM.</param>
+        public KCcmBlockCipher(IBlockCipher engine): this(engine, 4)
+        {
+        }
+
+        /// <summary>
+        /// Constructor allowing Nb configuration.
+        /// 
+        /// Nb is a parameter specified in CCM mode of DSTU7624 standard.
+        /// This parameter specifies maximum possible length of input.It should
+        /// be calculated as follows: Nb = 1 / 8 * (-3 + log[2]Nmax) + 1,
+        /// where Nmax - length of input message in bits.For practical reasons
+        /// Nmax usually less than 4Gb, e.g. for Nmax = 2^32 - 1, Nb = 4.
+        /// </summary>
+        /// <param name="engine">base cipher to use under CCM.</param>
+        /// <param name="Nb">Nb value to use.</param>
+        public KCcmBlockCipher(IBlockCipher engine, int Nb)
         {
             this.engine = engine;
             this.macSize = engine.GetBlockSize();
@@ -69,6 +84,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             this.buffer = new byte[engine.GetBlockSize()];
             this.s = new byte[engine.GetBlockSize()];
             this.counter = new byte[engine.GetBlockSize()];
+            setNb(Nb);
         }
 
         public virtual void Init(bool forEncryption, ICipherParameters parameters)
diff --git a/crypto/test/src/crypto/test/DSTU7624Test.cs b/crypto/test/src/crypto/test/DSTU7624Test.cs
index 599a3d61f..12ee46e5d 100644
--- a/crypto/test/src/crypto/test/DSTU7624Test.cs
+++ b/crypto/test/src/crypto/test/DSTU7624Test.cs
@@ -482,7 +482,6 @@ namespace Org.BouncyCastle.Crypto.Tests
 
             KCcmBlockCipher dstu7624ccm = new KCcmBlockCipher(new Dstu7624Engine(128));
 
-            dstu7624ccm.setNb(4);
             dstu7624ccm.Init(true, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -508,7 +507,6 @@ namespace Org.BouncyCastle.Crypto.Tests
                     + " got " + Hex.ToHexString(encrypted));
             }
 
-            dstu7624ccm.setNb(4);
             dstu7624ccm.Init(false, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -546,7 +544,6 @@ namespace Org.BouncyCastle.Crypto.Tests
 
             dstu7624ccm = new KCcmBlockCipher(new Dstu7624Engine(256));
 
-            dstu7624ccm.setNb(4);
             dstu7624ccm.Init(true, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -570,8 +567,7 @@ namespace Org.BouncyCastle.Crypto.Tests
                     + Hex.ToHexString(expectedEncrypted)
                     + " got " + Hex.ToHexString(encrypted));
             }
-
-            dstu7624ccm.setNb(4);
+            
             dstu7624ccm.Init(false, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -607,9 +603,8 @@ namespace Org.BouncyCastle.Crypto.Tests
 
             param = new AeadParameters(new KeyParameter(key), 256, iv);
 
-            dstu7624ccm = new KCcmBlockCipher(new Dstu7624Engine(256));
+            dstu7624ccm = new KCcmBlockCipher(new Dstu7624Engine(256), 6);
 
-            dstu7624ccm.setNb(6);
             dstu7624ccm.Init(true, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -634,7 +629,6 @@ namespace Org.BouncyCastle.Crypto.Tests
                     + " got " + Hex.ToHexString(encrypted));
             }
 
-            dstu7624ccm.setNb(6);
             dstu7624ccm.Init(false, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -670,9 +664,8 @@ namespace Org.BouncyCastle.Crypto.Tests
 
             param = new AeadParameters(new KeyParameter(key), 512, iv);
 
-            dstu7624ccm = new KCcmBlockCipher(new Dstu7624Engine(512));
+            dstu7624ccm = new KCcmBlockCipher(new Dstu7624Engine(512), 8);
 
-            dstu7624ccm.setNb(8);
             dstu7624ccm.Init(true, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);
@@ -697,7 +690,6 @@ namespace Org.BouncyCastle.Crypto.Tests
                     + " got " + Hex.ToHexString(encrypted));
             }
 
-            dstu7624ccm.setNb(8);
             dstu7624ccm.Init(false, param);
 
             dstu7624ccm.ProcessAadBytes(authText, 0, authText.Length);