コード例 #1
0
        protected virtual byte[] Kdf(ECPoint u, byte[] za, byte[] zb, int klen)
        {
            int digestSize = mDigest.GetDigestSize();

            byte[] buf = new byte[System.Math.Max(4, digestSize)];
            byte[] rv  = new byte[(klen + 7) / 8];
            int    off = 0;

            IMemoable memo = mDigest as IMemoable;
            IMemoable copy = null;

            if (memo != null)
            {
                AddFieldElement(mDigest, u.AffineXCoord);
                AddFieldElement(mDigest, u.AffineYCoord);
                mDigest.BlockUpdate(za, 0, za.Length);
                mDigest.BlockUpdate(zb, 0, zb.Length);
                copy = memo.Copy();
            }

            uint ct = 0;

            while (off < rv.Length)
            {
                if (memo != null)
                {
                    memo.Reset(copy);
                }
                else
                {
                    AddFieldElement(mDigest, u.AffineXCoord);
                    AddFieldElement(mDigest, u.AffineYCoord);
                    mDigest.BlockUpdate(za, 0, za.Length);
                    mDigest.BlockUpdate(zb, 0, zb.Length);
                }

                Pack.UInt32_To_BE(++ct, buf, 0);
                mDigest.BlockUpdate(buf, 0, 4);
                mDigest.DoFinal(buf, 0);

                int copyLen = System.Math.Min(digestSize, rv.Length - off);
                Array.Copy(buf, 0, rv, off, copyLen);
                off += copyLen;
            }

            return(rv);
        }
コード例 #2
0
ファイル: SM2Engine.cs プロジェクト: arvindixonos/Zain360
        private void Kdf(IDigest digest, ECPoint c1, byte[] encData)
        {
            int digestSize = digest.GetDigestSize();

            byte[] buf = new byte[System.Math.Max(4, digestSize)];
            int    off = 0;

            IMemoable memo = digest as IMemoable;
            IMemoable copy = null;

            if (memo != null)
            {
                AddFieldElement(digest, c1.AffineXCoord);
                AddFieldElement(digest, c1.AffineYCoord);
                copy = memo.Copy();
            }

            uint ct = 0;

            while (off < encData.Length)
            {
                if (memo != null)
                {
                    memo.Reset(copy);
                }
                else
                {
                    AddFieldElement(digest, c1.AffineXCoord);
                    AddFieldElement(digest, c1.AffineYCoord);
                }

                Pack.UInt32_To_BE(++ct, buf, 0);
                digest.BlockUpdate(buf, 0, 4);
                digest.DoFinal(buf, 0);

                int xorLen = System.Math.Min(digestSize, encData.Length - off);
                Xor(encData, buf, off, xorLen);
                off += xorLen;
            }
        }
コード例 #3
0
ファイル: DigestTest.cs プロジェクト: PascalCoin/NPascalCoin
        protected virtual void PerformTest()
        {
            byte[] resBuf = new byte[_digest.GetDigestSize()];

            for (int i = 0; i < _input.Length - 1; i++)
            {
                byte[] msg = toByteArray(_input[i]);

                vectorTest(_digest, i, resBuf, msg, Hex.Decode(_results[i]));
            }

            byte[] lastV      = toByteArray(_input[_input.Length - 1]);
            byte[] lastDigest = Hex.Decode(_results[_input.Length - 1]);

            vectorTest(_digest, _input.Length - 1, resBuf, lastV, Hex.Decode(_results[_input.Length - 1]));

            //
            // clone test
            //
            _digest.BlockUpdate(lastV, 0, lastV.Length / 2);

            // clone the Digest
            IDigest d = CloneDigest(_digest);

            _digest.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            _digest.DoFinal(resBuf, 0);

            Assert.AreEqual(lastDigest, resBuf,
                            string.Format("fail clone vector test, expected {0} but got {1}", _results[_results.Length - 1],
                                          Hex.ToHexString(resBuf)));

            d.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            d.DoFinal(resBuf, 0);

            Assert.AreEqual(lastDigest, resBuf,
                            string.Format("fail second clone vector test, expected {0} but got {1}", _results[_results.Length - 1],
                                          Hex.ToHexString(resBuf)));

            //
            // memo test
            //
            IMemoable m = (IMemoable)_digest;

            _digest.BlockUpdate(lastV, 0, lastV.Length / 2);

            // copy the Digest
            IMemoable copy1 = m.Copy();
            IMemoable copy2 = copy1.Copy();

            _digest.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            _digest.DoFinal(resBuf, 0);

            Assert.AreEqual(lastDigest, resBuf,
                            string.Format("fail memo vector test, expected {0} but got {1}", _results[_results.Length - 1],
                                          Hex.ToHexString(resBuf)));

            m.Reset(copy1);

            _digest.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            _digest.DoFinal(resBuf, 0);

            Assert.AreEqual(lastDigest, resBuf,
                            string.Format("fail memo reset vector test, expected {0} but got {1}", _results[_results.Length - 1],
                                          Hex.ToHexString(resBuf)));

            IDigest md = (IDigest)copy2;

            md.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            md.DoFinal(resBuf, 0);

            Assert.AreEqual(lastDigest, resBuf,
                            string.Format("fail memo copy vector test, expected {0} but got {1}", _results[_results.Length - 1],
                                          Hex.ToHexString(resBuf)));
        }
コード例 #4
0
        public override void PerformTest()
        {
            byte[] resBuf = new byte[digest.GetDigestSize()];

            for (int i = 0; i < input.Length - 1; i++)
            {
                byte[] msg = toByteArray(input[i]);

                vectorTest(digest, i, resBuf, msg, Hex.Decode(results[i]));
            }

            byte[] lastV      = toByteArray(input[input.Length - 1]);
            byte[] lastDigest = Hex.Decode(results[input.Length - 1]);

            vectorTest(digest, input.Length - 1, resBuf, lastV, Hex.Decode(results[input.Length - 1]));

            //
            // clone test
            //
            digest.BlockUpdate(lastV, 0, lastV.Length / 2);

            // clone the Digest
            IDigest d = CloneDigest(digest);

            digest.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            digest.DoFinal(resBuf, 0);

            if (!AreEqual(lastDigest, resBuf))
            {
                Fail("failing clone vector test", results[results.Length - 1], Hex.ToHexString(resBuf));
            }

            d.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            d.DoFinal(resBuf, 0);

            if (!AreEqual(lastDigest, resBuf))
            {
                Fail("failing second clone vector test", results[results.Length - 1], Hex.ToHexString(resBuf));
            }

            //
            // memo test
            //
            IMemoable m = (IMemoable)digest;

            digest.BlockUpdate(lastV, 0, lastV.Length / 2);

            // copy the Digest
            IMemoable copy1 = m.Copy();
            IMemoable copy2 = copy1.Copy();

            digest.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            digest.DoFinal(resBuf, 0);

            if (!AreEqual(lastDigest, resBuf))
            {
                Fail("failing memo vector test", results[results.Length - 1], Hex.ToHexString(resBuf));
            }

            m.Reset(copy1);

            digest.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            digest.DoFinal(resBuf, 0);

            if (!AreEqual(lastDigest, resBuf))
            {
                Fail("failing memo reset vector test", results[results.Length - 1], Hex.ToHexString(resBuf));
            }

            IDigest md = (IDigest)copy2;

            md.BlockUpdate(lastV, lastV.Length / 2, lastV.Length - lastV.Length / 2);
            md.DoFinal(resBuf, 0);

            if (!AreEqual(lastDigest, resBuf))
            {
                Fail("failing memo copy vector test", results[results.Length - 1], Hex.ToHexString(resBuf));
            }
        }