/// <summary>
 /// Perform a Blake2 hash on the given buffer using the given Blake2
 /// configuration.
 /// </summary>
 /// <param name="data">
 /// The buffer to hash.
 /// </param>
 /// <param name="start">
 /// The byte in the buffer to start hashing.
 /// </param>
 /// <param name="count">
 /// The number of bytes to hash.
 /// </param>
 /// <param name="config">
 /// The configuration to use.
 /// </param>
 /// <returns>
 /// The hash of the buffer.
 /// </returns>
 public static byte[] ComputeHash(byte[] data, int start, int count, Blake2BConfig config)
 {
     using (var hasher = Create(config))
     {
         hasher.Update(data, start, count);
         return(hasher.Finish());
     }
 }
예제 #2
0
    //[Benchmark(Description = "Isopoh"), BenchmarkCategory("Blake2b")]
    public static byte[] GetHashICB2()
    {
        var cfg = new Isopoh.Cryptography.Blake2b.Blake2BConfig()
        {
            OutputSizeInBytes = BenchConfig.HashBytes,
            Key = BenchConfig.Key
        };

        return(Isopoh.Cryptography.Blake2b.Blake2B.ComputeHash(BenchConfig.Data, cfg, null));
    }
        public Blake2BHasher(Blake2BConfig config)
        {
            if (config == null)
            {
                config = DefaultConfig;
            }
            this.core      = new Blake2BCore(config.LockMemoryPolicy);
            this.rawConfig = Blake2IvBuilder.ConfigB(config, null);
            if (config.Key != null && config.Key.Length != 0)
            {
                this.key = new SecureArray <byte>(128);
                Array.Copy(config.Key, this.key.Buffer, config.Key.Length);
            }

            this.outputSizeInBytes   = config.OutputSizeInBytes;
            this.defaultOutputBuffer = config.Result64ByteBuffer;
            this.Init();
        }
예제 #4
0
        public Blake2BHasher(Blake2BConfig config, SecureArrayCall secureArrayCall)
        {
            if (config == null)
            {
                config = DefaultConfig;
            }
            this.core      = new Blake2BCore(secureArrayCall, config.LockMemoryPolicy);
            this.rawConfig = Blake2IvBuilder.ConfigB(config, null, secureArrayCall);
            if (config.Key != null && config.Key.Length != 0)
            {
                switch (config.LockMemoryPolicy)
                {
                case LockMemoryPolicy.None:
                    this.key = new SecureArray <byte>(128, SecureArrayType.ZeroedAndPinned, secureArrayCall);
                    break;

                case LockMemoryPolicy.BestEffort:
                    try
                    {
                        this.key = new SecureArray <byte>(128, SecureArrayType.ZeroedPinnedAndNoSwap, secureArrayCall);
                    }
                    catch (LockFailException e)
                    {
                        this.key = new SecureArray <byte>(128, SecureArrayType.ZeroedAndPinned, secureArrayCall);
                    }

                    break;

                default:
                    this.key = new SecureArray <byte>(128, SecureArrayType.ZeroedPinnedAndNoSwap, secureArrayCall);
                    break;
                }

                Array.Copy(config.Key, this.key.Buffer, config.Key.Length);
            }

            this.outputSizeInBytes   = config.OutputSizeInBytes;
            this.defaultOutputBuffer = config.Result64ByteBuffer;
            this.Init();
        }
예제 #5
0
        public static SecureArray <ulong> ConfigB(Blake2BConfig config, Blake2BTreeConfig treeConfig)
        {
            bool isSequential = treeConfig == null;

            if (isSequential)
            {
                treeConfig = SequentialTreeConfig;
            }

            SecureArray <ulong> rawConfig;

            try
            {
                rawConfig = new SecureArray <ulong>(8, SecureArrayType.ZeroedPinnedAndNoSwap);
            }
            catch (LockFailException)
            {
                rawConfig = new SecureArray <ulong>(8, SecureArrayType.ZeroedAndPinned);
            }

            //digest length
            if (config.OutputSizeInBytes <= 0 | config.OutputSizeInBytes > 64)
            {
                throw new ArgumentOutOfRangeException(
                          nameof(config),
                          $"Expected 0 < config.OutputSizeInBytes <= 64, got {config.OutputSizeInBytes}");
            }

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

            //Key length
            if (config.Key != null)
            {
                if (config.Key.Length > 64)
                {
                    throw new ArgumentException($"Expected key length <= 64, got {config.Key.Length}", nameof(config));
                }

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

            // FanOut
            rawConfig[0] |= (uint)treeConfig.FanOut << 16;

            // Depth
            rawConfig[0] |= (uint)treeConfig.MaxHeight << 24;

            // Leaf length
            rawConfig[0] |= ((ulong)(uint)treeConfig.LeafSize) << 32;

            // Inner length
            if (!isSequential && (treeConfig.IntermediateHashSize <= 0 || treeConfig.IntermediateHashSize > 64))
            {
                throw new ArgumentOutOfRangeException(
                          nameof(treeConfig),
                          $"Expected 0 < treeConfig.IntermediateHashSize <= 64, got {treeConfig.IntermediateHashSize}");
            }

            rawConfig[2] |= (uint)treeConfig.IntermediateHashSize << 8;

            // Salt
            if (config.Salt != null)
            {
                if (config.Salt.Length != 16)
                {
                    throw new ArgumentException("config.Salt has invalid length");
                }

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

            // Personalization
            if (config.Personalization != null)
            {
                if (config.Personalization.Length != 16)
                {
                    throw new ArgumentException(
                              $"Expected config.Personalization == 16, got {config.Personalization.Length}",
                              nameof(config));
                }

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

            return(rawConfig);
        }
 /// <summary>
 /// Create a Blake2 hash with the given configuration.
 /// </summary>
 /// <param name="config">
 /// The configuration to use.
 /// </param>
 /// <returns>
 /// A <see cref="Hasher"/> that can be converted to a <see cref="HashAlgorithm"/>.
 /// </returns>
 public static Hasher Create(Blake2BConfig config)
 {
     return(new Blake2BHasher(config));
 }
 /// <summary>
 /// Perform a Blake2 hash on the given buffer using the given Blake2
 /// configuration.
 /// </summary>
 /// <param name="data">
 /// The buffer to hash.
 /// </param>
 /// <param name="config">
 /// The configuration to use.
 /// </param>
 /// <returns>
 /// The hash of the buffer.
 /// </returns>
 public static byte[] ComputeHash(byte[] data, Blake2BConfig config)
 {
     return(ComputeHash(data, 0, data.Length, config));
 }
예제 #8
0
 /// <summary>
 /// Create a Blake2 hash with the given configuration.
 /// </summary>
 /// <param name="config">
 /// The configuration to use.
 /// </param>
 /// <param name="secureArrayCall">
 /// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
 /// </param>
 /// <returns>
 /// A <see cref="Hasher"/> that can be converted to a <see cref="HashAlgorithm"/>.
 /// </returns>
 public static Hasher Create(Blake2BConfig config, SecureArrayCall secureArrayCall)
 {
     return(new Blake2BHasher(config, secureArrayCall));
 }
예제 #9
0
 /// <summary>
 /// Perform a Blake2 hash on the given buffer using the given Blake2
 /// configuration.
 /// </summary>
 /// <param name="data">
 /// The buffer to hash.
 /// </param>
 /// <param name="config">
 /// The configuration to use.
 /// </param>
 /// <param name="secureArrayCall">
 /// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
 /// </param>
 /// <returns>
 /// The hash of the buffer.
 /// </returns>
 public static byte[] ComputeHash(byte[] data, Blake2BConfig config, SecureArrayCall secureArrayCall)
 {
     return(ComputeHash(data, 0, data.Length, config, secureArrayCall));
 }
예제 #10
0
 /// <summary>
 /// Perform a Blake2 hash on the given buffer using the given Blake2
 /// configuration.
 /// </summary>
 /// <param name="data">
 /// The buffer to hash.
 /// </param>
 /// <param name="start">
 /// The byte in the buffer to start hashing.
 /// </param>
 /// <param name="count">
 /// The number of bytes to hash.
 /// </param>
 /// <param name="config">
 /// The configuration to use.
 /// </param>
 /// <param name="secureArrayCall">
 /// The methods that get called to secure arrays. A null value defaults to <see cref="SecureArray"/>.<see cref="SecureArray.DefaultCall"/>.
 /// </param>
 /// <returns>
 /// The hash of the buffer.
 /// </returns>
 public static byte[] ComputeHash(byte[] data, int start, int count, Blake2BConfig config, SecureArrayCall secureArrayCall)
 {
     using var hasher = Create(config, secureArrayCall);
     hasher.Update(data, start, count);
     return(hasher.Finish());
 }