/// <summary> /// Creates a new <see cref="IBlake2B" /> instance with given configuration. /// </summary> /// <param name="config">The configuration to use.</param> /// <returns> /// A <see cref="IBlake2B" /> instance. /// </returns> /// <exception cref="ArgumentNullException">config</exception> public IBlake2B Create(IBlake2BConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } return(new Blake2B_Implementation(config)); }
/// <summary> /// Initializes an instance of the <see cref="Blake2B_Implementation"/> class with the provided /// <paramref name="config"/>. /// </summary> /// <param name="config">The configuration to use for this instance.</param> /// <exception cref="ArgumentNullException"><paramref name="config"/></exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="config"/>.<see cref="IBlake2BConfig.HashSizeInBits"/>;Expected: <see cref="MinHashSizeBits"/> >= <paramref name="config"/>.<see cref="IBlake2BConfig.HashSizeInBits"/> <= <see cref="MaxHashSizeBits"/></exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="config"/>.<see cref="IBlake2BConfig.HashSizeInBits"/>;<paramref name="config"/>.<see cref="IBlake2BConfig.HashSizeInBits"/> must be a multiple of 8.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="config"/>.<see cref="IBlake2BConfig.Key"/>;Expected: <paramref name="config"/>.<see cref="IBlake2BConfig.Key"/>.Count <= <see cref="MaxKeySizeBytes"/></exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="config"/>.<see cref="IBlake2BConfig.Salt"/>;Expected: <paramref name="config"/>.<see cref="IBlake2BConfig.Salt"/>.Count == <see cref="SaltSizeBytes"/></exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="config"/>.<see cref="IBlake2BConfig.Personalization"/>;Expected: <paramref name="config"/>.<see cref="IBlake2BConfig.Personalization"/>.Count == <see cref="PersonalizationSizeBytes"/></exception> public Blake2B_Implementation(IBlake2BConfig config) { if (config == null) { throw new ArgumentNullException(nameof(config)); } _config = config.Clone(); if (_config.HashSizeInBits < MinHashSizeBits || _config.HashSizeInBits > MaxHashSizeBits) { throw new ArgumentOutOfRangeException($"{nameof(config)}.{nameof(config.HashSizeInBits)}", _config.HashSizeInBits, $"Expected: {MinHashSizeBits} >= {nameof(config)}.{nameof(config.HashSizeInBits)} <= {MaxHashSizeBits}"); } if (_config.HashSizeInBits % 8 != 0) { throw new ArgumentOutOfRangeException($"{nameof(config)}.{nameof(config.HashSizeInBits)}", _config.HashSizeInBits, $"{ nameof(config) }.{ nameof(config.HashSizeInBits)} must be a multiple of 8."); } var keyArray = (_config.Key ?? new byte[0]).ToArray(); var saltArray = (_config.Salt ?? new byte[16]).ToArray(); var personalizationArray = (_config.Personalization ?? new byte[16]).ToArray(); if (keyArray.Length > MaxKeySizeBytes) { throw new ArgumentOutOfRangeException($"{nameof(config)}.{nameof(config.Key)}", _config.Key, $"Expected: {nameof(config)}.{nameof(config.Key)}.Count <= {MaxKeySizeBytes}"); } if (saltArray.Length != SaltSizeBytes) { throw new ArgumentOutOfRangeException($"{nameof(config)}.{nameof(config.Salt)}", _config.Salt, $"Expected: {nameof(config)}.{nameof(config.Salt)}.Count == {SaltSizeBytes}"); } if (personalizationArray.Length != PersonalizationSizeBytes) { throw new ArgumentOutOfRangeException($"{nameof(config)}.{nameof(config.Personalization)}", _config.Personalization, $"Expected: {nameof(config)}.{nameof(config.Personalization)}.Count == {PersonalizationSizeBytes}"); } _originalKeyLength = (uint)keyArray.Length; _key = new byte[128]; Array.Copy(keyArray, _key, keyArray.Length); _salt = saltArray; _personalization = personalizationArray; }
private static void VerifyConfigB(IBlake2BConfig a_Config, IBlake2BTreeConfig a_TreeConfig, bool a_IsSequential) { // digest length if ((a_Config.HashSize <= 0) || (a_Config.HashSize > 64)) { throw new ArgumentOutOfRangeHashLibException(InvalidHashSize); } // Key length if (!a_Config.Key.Empty()) { if (a_Config.Key.Length > 64) { throw new ArgumentOutOfRangeHashLibException(InvalidKeyLength); } } // Salt length if (!a_Config.Salt.Empty()) { if (a_Config.Salt.Length != 16) { throw new ArgumentOutOfRangeHashLibException(InvalidSaltLength); } } // Personalisation length if (!a_Config.Personalisation.Empty()) { if (a_Config.Personalisation.Length != 16) { throw new ArgumentOutOfRangeHashLibException(InvalidPersonalisationLength); } } // Tree InnerHashSize if (a_TreeConfig != null) { if ((a_IsSequential) && ((a_TreeConfig.InnerHashSize != 0))) { throw new ArgumentOutOfRangeHashLibException("a_TreeConfig.TreeIntermediateHashSize"); } if (a_TreeConfig.InnerHashSize > 64) { throw new ArgumentOutOfRangeHashLibException(TreeIncorrectInnerHashSize); } } }
public static unsafe UInt64[] ConfigB(IBlake2BConfig a_Config, IBlake2BTreeConfig a_TreeConfig) { bool isSequential; byte[] buffer = new byte[64]; isSequential = a_TreeConfig == null; if (isSequential) { a_TreeConfig = Blake2BTreeConfig.GetSequentialTreeConfig(); } VerifyConfigB(a_Config, a_TreeConfig, isSequential); buffer[0] = (byte)a_Config.HashSize; buffer[1] = (byte)(a_Config.Key?.Length ?? 0);; if (a_TreeConfig != null) { buffer[2] = a_TreeConfig.FanOut; buffer[3] = a_TreeConfig.MaxDepth; Converters.ReadUInt32AsBytesLE(a_TreeConfig.LeafSize, ref buffer, 4); Converters.ReadUInt64AsBytesLE(a_TreeConfig.NodeOffset, ref buffer, 8); buffer[16] = a_TreeConfig.NodeDepth; buffer[17] = a_TreeConfig.InnerHashSize; } if (!a_Config.Salt.Empty()) { Utils.Utils.Memmove(ref buffer, a_Config.Salt, 16 * sizeof(byte), 0, 32); } if (!a_Config.Personalisation.Empty()) { Utils.Utils.Memmove(ref buffer, a_Config.Personalisation, 16 * sizeof(byte), 0, 48); } UInt64[] result = new UInt64[8]; fixed(UInt64 *resultPtr = result) { fixed(byte *bufferPtr = buffer) { Converters.le64_copy((IntPtr)bufferPtr, 0, (IntPtr)resultPtr, 0, buffer.Length * sizeof(byte)); } } return(result); }
} = null; // blake2B tree config object public Blake2XBConfig(IBlake2BConfig a_Blake2BConfig = null, IBlake2BTreeConfig a_Blake2BTreeConfig = null) { Blake2BConfig = a_Blake2BConfig; Blake2BTreeConfig = a_Blake2BTreeConfig; } // end cctr
/////////////////////////////////////////// /// <summary> /// Blake Hash Family /// </summary> //////////////////////////////////////////// /// public static IHash CreateBlake2B(IBlake2BConfig a_Config = null, IBlake2BTreeConfig a_TreeConfig = null) => new Blake2B(a_Config ?? Blake2BConfig.DefaultConfig, a_TreeConfig);
/// <summary> /// <br />Blake2B defaults to setting the expected output length <br /> /// from the <c>HashSize</c> in the <c>Blake2BConfig</c> class. <br />In /// some cases, however, we do not want this, as the output length <br /> /// of these instances is given by <c>Blake2BTreeConfig.InnerSize</c> /// instead. <br /> /// </summary> private Blake2B Blake2BPCreateLeafParam(IBlake2BConfig a_Blake2BConfig, IBlake2BTreeConfig a_Blake2BTreeConfig) { return(new Blake2B(a_Blake2BConfig, a_Blake2BTreeConfig)); }