/// <summary> /// Parses base parameters from the provided dictionary. /// </summary> /// <param name="dictionary">The dictionary containing KDF parameters.</param> public KdfParameters(VariantDictionary dictionary) { if (dictionary == null) { throw new ArgumentNullException(nameof(dictionary)); } object uuid = dictionary.GetValue(UuidKey); if (uuid == null) { throw new FormatException("No UUID value in dictionary for KdfParameters"); } byte[] uuidBytes = uuid as byte[]; if (uuidBytes == null) { throw new FormatException("UUID value in KdfParameters was not a byte array"); } if (uuidBytes.Length != 16) { throw new FormatException($"Expected 16 UUID bytes in KdfParameters, got {uuidBytes.Length}"); } this.uuid = new Guid(uuidBytes); }
/// <summary> /// Initializes the parameters given a dictionary. /// </summary> /// <param name="dictionary"></param> public AesParameters(VariantDictionary dictionary) : base(dictionary) { if (Uuid != AesUuid) { throw new FormatException("Wrong UUID for AesParameters"); } ulong?dictRounds = dictionary.GetValue(RoundsKey) as ulong?; Rounds = dictRounds ?? DefaultRounds; if (!(dictionary.GetValue(SeedKey) is byte[] dictSeed)) { throw new FormatException("AesParameters requires a byte[] seed value"); } Seed = dictSeed.AsBuffer(); }
/// <summary> /// Initializes the parameters given a dictionary. /// </summary> /// <param name="dictionary"></param> public Argon2Parameters(VariantDictionary dictionary) : base(dictionary) { this.salt = dictionary.GetValue(SaltKey) as byte[] ?? new byte[0]; this.secretKey = dictionary.GetValue(SecretKey) as byte[] ?? new byte[0]; this.associatedData = dictionary.GetValue(DataKey) as byte[] ?? new byte[0]; uint?dictParallelism = dictionary.GetValue(ParallelismKey) as uint?; if (dictParallelism == null) { throw new FormatException("Could not parse parallelism uint from dictionary"); } this.parallelism = dictParallelism.Value; if (this.parallelism > Argon2d.MaxParallelism || this.parallelism < Argon2d.MinParallelism) { throw new FormatException("Parsed parallelism is out of bounds"); } ulong?dictByteCount = dictionary.GetValue(ByteCountKey) as ulong?; if (dictByteCount == null) { throw new FormatException("Could not parse memory ulong from dictionary"); } this.blockCount = dictByteCount.Value / 1024; if (this.blockCount > (ulong)Argon2d.MaxMemorySize) { throw new FormatException("Parsed block count is higher than allowed"); } ulong?dictIterations = dictionary.GetValue(IterationsKey) as ulong?; if (dictIterations == null) { throw new FormatException("Could not parse iterations ulong from dictionary"); } this.iterations = dictIterations.Value; if (this.iterations < (ulong)Argon2d.MinIterations || this.iterations > (ulong)Argon2d.MaxIterations) { throw new FormatException("Parsed iterations is out of bounds"); } uint?dictVersion = dictionary.GetValue(VersionKey) as uint?; if (dictVersion == null) { throw new FormatException("Argon2 version was not present"); } if (dictVersion.Value != Argon2d.VersionNumber) { throw new FormatException($"Unsupported Argon2 version: {dictVersion.Value}"); } }