summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-09-16 18:32:37 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-09-16 18:32:37 +0700
commit86a7490aaf8f0d9b35cc2fb094efae8409c095a6 (patch)
treef3e420b768d7265c398d90f8fd37d901359e5108 /crypto
parentPort of SM2Engine from Java (diff)
downloadBouncyCastle.NET-ed25519-86a7490aaf8f0d9b35cc2fb094efae8409c095a6.tar.xz
Port of ConcatenationKdfGenerator from Java
Diffstat (limited to 'crypto')
-rw-r--r--crypto/BouncyCastle.Android.csproj1
-rw-r--r--crypto/BouncyCastle.csproj1
-rw-r--r--crypto/BouncyCastle.iOS.csproj1
-rw-r--r--crypto/crypto.csproj5
-rw-r--r--crypto/src/crypto/agreement/kdf/ConcatenationKdfGenerator.cs100
5 files changed, 108 insertions, 0 deletions
diff --git a/crypto/BouncyCastle.Android.csproj b/crypto/BouncyCastle.Android.csproj
index 1c7463c3b..9c05a5df4 100644
--- a/crypto/BouncyCastle.Android.csproj
+++ b/crypto/BouncyCastle.Android.csproj
@@ -704,6 +704,7 @@
     <Compile Include="src\crypto\agreement\jpake\JPakeRound2Payload.cs" />
     <Compile Include="src\crypto\agreement\jpake\JPakeRound3Payload.cs" />
     <Compile Include="src\crypto\agreement\jpake\JPakeUtilities.cs" />
+    <Compile Include="src\crypto\agreement\kdf\ConcatenationKdfGenerator.cs" />
     <Compile Include="src\crypto\agreement\kdf\DHKdfParameters.cs" />
     <Compile Include="src\crypto\agreement\kdf\DHKekGenerator.cs" />
     <Compile Include="src\crypto\agreement\kdf\ECDHKekGenerator.cs" />
diff --git a/crypto/BouncyCastle.csproj b/crypto/BouncyCastle.csproj
index d5840b942..f1de777c6 100644
--- a/crypto/BouncyCastle.csproj
+++ b/crypto/BouncyCastle.csproj
@@ -698,6 +698,7 @@
     <Compile Include="src\crypto\agreement\jpake\JPakeRound2Payload.cs" />
     <Compile Include="src\crypto\agreement\jpake\JPakeRound3Payload.cs" />
     <Compile Include="src\crypto\agreement\jpake\JPakeUtilities.cs" />
+    <Compile Include="src\crypto\agreement\kdf\ConcatenationKdfGenerator.cs" />
     <Compile Include="src\crypto\agreement\kdf\DHKdfParameters.cs" />
     <Compile Include="src\crypto\agreement\kdf\DHKekGenerator.cs" />
     <Compile Include="src\crypto\agreement\kdf\ECDHKekGenerator.cs" />
diff --git a/crypto/BouncyCastle.iOS.csproj b/crypto/BouncyCastle.iOS.csproj
index 5e2ced2ad..e655422b8 100644
--- a/crypto/BouncyCastle.iOS.csproj
+++ b/crypto/BouncyCastle.iOS.csproj
@@ -699,6 +699,7 @@
     <Compile Include="src\crypto\agreement\jpake\JPakeRound2Payload.cs" />
     <Compile Include="src\crypto\agreement\jpake\JPakeRound3Payload.cs" />
     <Compile Include="src\crypto\agreement\jpake\JPakeUtilities.cs" />
+    <Compile Include="src\crypto\agreement\kdf\ConcatenationKdfGenerator.cs" />
     <Compile Include="src\crypto\agreement\kdf\DHKdfParameters.cs" />
     <Compile Include="src\crypto\agreement\kdf\DHKekGenerator.cs" />
     <Compile Include="src\crypto\agreement\kdf\ECDHKekGenerator.cs" />
diff --git a/crypto/crypto.csproj b/crypto/crypto.csproj
index f872911e1..85af6ef1a 100644
--- a/crypto/crypto.csproj
+++ b/crypto/crypto.csproj
@@ -3274,6 +3274,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "src\crypto\agreement\kdf\ConcatenationKdfGenerator.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "src\crypto\agreement\kdf\DHKdfParameters.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
diff --git a/crypto/src/crypto/agreement/kdf/ConcatenationKdfGenerator.cs b/crypto/src/crypto/agreement/kdf/ConcatenationKdfGenerator.cs
new file mode 100644
index 000000000..d88f4dfdb
--- /dev/null
+++ b/crypto/src/crypto/agreement/kdf/ConcatenationKdfGenerator.cs
@@ -0,0 +1,100 @@
+using System;
+
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Crypto.Utilities;
+
+namespace Org.BouncyCastle.Crypto.Agreement.Kdf
+{
+    /**
+     * Generator for Concatenation Key Derivation Function defined in NIST SP 800-56A, Sect 5.8.1
+     */
+    public class ConcatenationKdfGenerator
+        :   IDerivationFunction
+    {
+        private readonly IDigest mDigest;
+
+        private byte[] mShared;
+        private byte[] mOtherInfo;
+        private int mHLen;
+
+        /**
+         * @param digest the digest to be used as the source of generated bytes
+         */
+        public ConcatenationKdfGenerator(IDigest digest)
+        {
+            this.mDigest = digest;
+            this.mHLen = digest.GetDigestSize();
+        }
+
+        public virtual void Init(IDerivationParameters param)
+        {
+            if (!(param is KdfParameters))
+                throw new ArgumentException("KDF parameters required for ConcatenationKdfGenerator");
+
+            KdfParameters p = (KdfParameters)param;
+
+            mShared = p.GetSharedSecret();
+            mOtherInfo = p.GetIV();
+        }
+
+        /**
+         * return the underlying digest.
+         */
+        public virtual IDigest Digest
+        {
+            get { return mDigest; }
+        }
+
+        /**
+         * fill len bytes of the output buffer with bytes generated from
+         * the derivation function.
+         *
+         * @throws DataLengthException if the out buffer is too small.
+         */
+        public virtual int GenerateBytes(byte[]	outBytes, int outOff, int len)
+        {
+            if ((outBytes.Length - len) < outOff)
+                throw new DataLengthException("output buffer too small");
+
+            byte[] hashBuf = new byte[mHLen];
+            byte[] C = new byte[4];
+            uint counter = 1;
+            int outputLen = 0;
+
+            mDigest.Reset();
+
+            if (len > mHLen)
+            {
+                do
+                {
+                    Pack.UInt32_To_BE(counter, C);
+
+                    mDigest.BlockUpdate(C, 0, C.Length);
+                    mDigest.BlockUpdate(mShared, 0, mShared.Length);
+                    mDigest.BlockUpdate(mOtherInfo, 0, mOtherInfo.Length);
+
+                    mDigest.DoFinal(hashBuf, 0);
+
+                    Array.Copy(hashBuf, 0, outBytes, outOff + outputLen, mHLen);
+                    outputLen += mHLen;
+                }
+                while ((counter++) < (len / mHLen));
+            }
+
+            if (outputLen < len)
+            {
+                Pack.UInt32_To_BE(counter, C);
+
+                mDigest.BlockUpdate(C, 0, C.Length);
+                mDigest.BlockUpdate(mShared, 0, mShared.Length);
+                mDigest.BlockUpdate(mOtherInfo, 0, mOtherInfo.Length);
+
+                mDigest.DoFinal(hashBuf, 0);
+
+                Array.Copy(hashBuf, 0, outBytes, outOff + outputLen, len - outputLen);
+            }
+
+            return len;
+        }
+    }
+}