Esempio n. 1
0
 public static Blake2BTreeConfig CreateInterleaved(int _Parallel)
 {
     Blake2BTreeConfig _Result = new Blake2BTreeConfig
     {
         FanOut = _Parallel, MaxHeight = 2, IntermediateHashSize = 64
     };
     return _Result;
 }
Esempio n. 2
0
        // ReSharper disable once UnusedMember.Global

        /// <summary>
        /// Create an instance of the <see cref="Blake2BTreeConfig"/> for parallel hash computation.
        /// </summary>
        /// <param name="parallelism">
        /// The amount of parallelism to invoke when generating the hash.
        /// </param>
        /// <returns>
        /// An instance of the <see cref="Blake2BTreeConfig"/> suitable for generating a hash.
        /// </returns>
        public static Blake2BTreeConfig CreateInterleaved(int parallelism)
        {
            var result = new Blake2BTreeConfig
            {
                FanOut               = parallelism,
                MaxHeight            = 2,
                IntermediateHashSize = 64
            };

            return(result);
        }
Esempio n. 3
0
        private Blake2B Blake2BPCreateRoot()
        {
            IBlake2BConfig blake2BConfig = new Blake2BConfig(HashSize);

            blake2BConfig.Key = Key.DeepCopy();

            IBlake2BTreeConfig blake2BTreeConfig = new Blake2BTreeConfig();

            blake2BTreeConfig.FanOut        = (byte)ParallelismDegree;
            blake2BTreeConfig.MaxDepth      = 2;
            blake2BTreeConfig.NodeDepth     = 1;
            blake2BTreeConfig.LeafSize      = 0;
            blake2BTreeConfig.NodeOffset    = 0;
            blake2BTreeConfig.InnerHashSize = (byte)OutSizeInBytes;
            blake2BTreeConfig.IsLastNode    = true;

            return(new Blake2B(blake2BConfig, blake2BTreeConfig, false));
        }
Esempio n. 4
0
        private Blake2B Blake2BPCreateRoot()
        {
            var config = new Blake2BConfig(HashSize)
            {
                Key = _key
            };

            var treeConfig = new Blake2BTreeConfig
            {
                FanOut        = ParallelismDegree,
                MaxDepth      = 2,
                NodeDepth     = 1,
                LeafSize      = 0,
                NodeOffset    = 0,
                InnerHashSize = OutSizeInBytes,
                IsLastNode    = true
            };

            return(new Blake2B(config, treeConfig, false));
        }
Esempio n. 5
0
        private Blake2B Blake2BPCreateLeaf(UInt64 a_Offset)
        {
            IBlake2BConfig blake2BConfig = new Blake2BConfig(HashSize);

            blake2BConfig.Key = Key.DeepCopy();

            IBlake2BTreeConfig blake2BTreeConfig = new Blake2BTreeConfig();

            blake2BTreeConfig.FanOut        = (byte)ParallelismDegree;
            blake2BTreeConfig.MaxDepth      = 2;
            blake2BTreeConfig.NodeDepth     = 0;
            blake2BTreeConfig.LeafSize      = 0;
            blake2BTreeConfig.NodeOffset    = a_Offset;
            blake2BTreeConfig.InnerHashSize = (byte)OutSizeInBytes;

            if (a_Offset == (UInt64)(ParallelismDegree - 1))
            {
                blake2BTreeConfig.IsLastNode = true;
            }

            return(Blake2BPCreateLeafParam(blake2BConfig, blake2BTreeConfig));
        }
Esempio n. 6
0
        public static ulong[] ConfigB(Blake2BConfig config, Blake2BTreeConfig treeConfig)
        {
            bool isSequential = treeConfig == null;

            if (isSequential)
            {
                treeConfig = SequentialTreeConfig;
            }

            ulong[] rawConfig = new ulong[8];

            rawConfig[0] |= (uint)config.OutputSize;

            if (config.Key != null)
            {
                rawConfig[0] |= (uint)config.Key.Length << 8;
            }

            rawConfig[0] |= (uint)treeConfig.FanOut << 16;
            rawConfig[0] |= (uint)treeConfig.MaxHeight << 24;
            rawConfig[0] |= (ulong)(uint)treeConfig.LeafSize << 32;
            rawConfig[2] |= (uint)treeConfig.IntermediateHashSize << 8;


            if (config.Salt != null)
            {
                rawConfig[4] = Blake2BCore.BytesToUInt64(config.Salt, 0);
                rawConfig[5] = Blake2BCore.BytesToUInt64(config.Salt, 8);
            }

            if (config.Personalization != null)
            {
                rawConfig[6] = Blake2BCore.BytesToUInt64(config.Personalization, 0);
                rawConfig[7] = Blake2BCore.BytesToUInt64(config.Personalization, 8);
            }

            return(rawConfig);
        }
Esempio n. 7
0
        public static ulong[] ConfigB(Blake2BConfig Config, Blake2BTreeConfig TreeConfig)
        {
            bool IsSequential = TreeConfig == null;

            if (IsSequential)
            {
                TreeConfig = Blake2Builder.SequentialTreeConfig;
            }

            ulong[] RawConfig = new ulong[8];

            RawConfig[0] |= (uint)Config.OutputSize;

            if (Config.Key != null)
            {
                RawConfig[0] |= (uint)Config.Key.Length << 8;
            }

            RawConfig[0] |= (uint)TreeConfig.FanOut << 16;
            RawConfig[0] |= (uint)TreeConfig.MaxHeight << 24;
            RawConfig[0] |= (ulong)(uint)TreeConfig.LeafSize << 32;
            RawConfig[2] |= (uint)TreeConfig.IntermediateHashSize << 8;

            if (Config.Salt != null)
            {
                RawConfig[4] = Blake2BCore.BytesToUInt64(Config.Salt, 0);
                RawConfig[5] = Blake2BCore.BytesToUInt64(Config.Salt, 8);
            }

            if (Config.Personalization != null)
            {
                RawConfig[6] = Blake2BCore.BytesToUInt64(Config.Personalization, 0);
                RawConfig[7] = Blake2BCore.BytesToUInt64(Config.Personalization, 8);
            }

            return(RawConfig);
        }
Esempio n. 8
0
        private Blake2B Blake2BPCreateLeaf(ulong offset)
        {
            var config = new Blake2BConfig(OutSizeInBytes)
            {
                Key = _key
            };

            var treeConfig = new Blake2BTreeConfig
            {
                FanOut        = ParallelismDegree,
                MaxDepth      = 2,
                NodeDepth     = 0,
                LeafSize      = 0,
                NodeOffset    = offset,
                InnerHashSize = OutSizeInBytes
            };

            if (offset == ParallelismDegree - 1)
            {
                treeConfig.IsLastNode = true;
            }

            return(Blake2BPCreateLeafParam(config, treeConfig));
        }
Esempio n. 9
0
 public static IHash CreateBlake2B(Blake2BConfig config = null, Blake2BTreeConfig treeConfig = null) =>
 new Blake2B(config ?? Blake2BConfig.DefaultConfig, treeConfig);
Esempio n. 10
0
        // https://docs.python.org/3/library/hashlib.html#tree-mode modified for Blake2B
        public void TestBlake2BTreeHashingMode()
        {
            const byte   FAN_OUT    = 2;
            const byte   DEPTH      = 2; // MaxDepth
            const UInt32 LEAF_SIZE  = 4096;
            const byte   INNER_SIZE = 64;

            IBlake2BTreeConfig Blake2BTreeConfigh00, Blake2BTreeConfigh01, Blake2BTreeConfigh10;
            IHash h00, h01, h10;

            byte[] LBuffer = new byte[6000];

            // Left leaf
            Blake2BTreeConfigh00               = new Blake2BTreeConfig();
            Blake2BTreeConfigh00.FanOut        = FAN_OUT;
            Blake2BTreeConfigh00.MaxDepth      = DEPTH;
            Blake2BTreeConfigh00.LeafSize      = LEAF_SIZE;
            Blake2BTreeConfigh00.InnerHashSize = INNER_SIZE;
            Blake2BTreeConfigh00.NodeOffset    = 0;
            Blake2BTreeConfigh00.NodeDepth     = 0;
            Blake2BTreeConfigh00.IsLastNode    = false;
            h00 = HashFactory.Crypto.CreateBlake2B(new Blake2BConfig() as IBlake2BConfig, Blake2BTreeConfigh00);
            h00.Initialize();

            // Right leaf
            Blake2BTreeConfigh01               = new Blake2BTreeConfig();
            Blake2BTreeConfigh01.FanOut        = FAN_OUT;
            Blake2BTreeConfigh01.MaxDepth      = DEPTH;
            Blake2BTreeConfigh01.LeafSize      = LEAF_SIZE;
            Blake2BTreeConfigh01.InnerHashSize = INNER_SIZE;
            Blake2BTreeConfigh01.NodeOffset    = 1;
            Blake2BTreeConfigh01.NodeDepth     = 0;
            Blake2BTreeConfigh01.IsLastNode    = true;
            h01 = HashFactory.Crypto.CreateBlake2B(new Blake2BConfig() as IBlake2BConfig, Blake2BTreeConfigh01);
            h01.Initialize();

            // Root node
            Blake2BTreeConfigh10               = new Blake2BTreeConfig();
            Blake2BTreeConfigh10.FanOut        = FAN_OUT;
            Blake2BTreeConfigh10.MaxDepth      = DEPTH;
            Blake2BTreeConfigh10.LeafSize      = LEAF_SIZE;
            Blake2BTreeConfigh10.InnerHashSize = INNER_SIZE;
            Blake2BTreeConfigh10.NodeOffset    = 0;
            Blake2BTreeConfigh10.NodeDepth     = 1;
            Blake2BTreeConfigh10.IsLastNode    = true;
            h10 = HashFactory.Crypto.CreateBlake2B(new Blake2BConfig(32) as IBlake2BConfig, Blake2BTreeConfigh10);
            h10.Initialize();

            byte[] temp = new byte[LEAF_SIZE];
            Utils.Utils.Memcopy(ref temp, LBuffer, (Int32)LEAF_SIZE);

            h10.TransformBytes(h00.ComputeBytes(temp).GetBytes());

            temp = new byte[LBuffer.Length - LEAF_SIZE];
            Utils.Utils.Memcopy(ref temp, LBuffer, (Int32)(LBuffer.Length - LEAF_SIZE), (Int32)LEAF_SIZE);

            h10.TransformBytes(h01.ComputeBytes(temp).GetBytes());

            string ActualString   = h10.TransformFinal().ToString();
            string ExpectedString = Blake2BTreeHashingMode;

            Assert.AreEqual(ExpectedString, ActualString,
                            String.Format("Expected {0} but got {1}.",
                                          ExpectedString, ActualString));
        }
 public static Blake2XBConfig CreateBlake2XBConfig(Blake2BConfig config, Blake2BTreeConfig treeConfig) =>
 new Blake2XBConfig(config, treeConfig);
 private Blake2XBConfig(Blake2BConfig config, Blake2BTreeConfig treeConfig)
 {
     Config     = config;
     TreeConfig = treeConfig;
 }
Esempio n. 13
0
 /// <summary>
 /// <br />Blake2B defaults to setting the expected output length <br />
 /// from <c>Config.HashSize</c>. <br />In some cases, however,
 /// we do not want this, as the output length <br />
 /// of these instances is given by <c>TreeConfig.InnerSize</c>
 /// instead. <br />
 /// </summary>
 private static Blake2B Blake2BPCreateLeafParam(Blake2BConfig config, Blake2BTreeConfig treeConfig) =>
 new Blake2B(config, treeConfig);