/// <summary> /// Create a KeyWrap instance for wrapping or unwrapping /// </summary> /// <param name="salt">A salt. This is required by AxCrypt, although the algorithm supports not using a salt.</param> /// <param name="keyWrapIterations">The number of wrapping iterations, at least 6</param> /// <param name="mode">Use original specification mode or AxCrypt mode (only difference is that 't' is little endian in AxCrypt mode)</param> public KeyWrap(Salt salt, long keyWrapIterations, KeyWrapMode mode) { if (salt == null) { throw new ArgumentNullException("salt"); } if (keyWrapIterations < 6) { throw new InternalErrorException("Key wrap iterations must be at least 6."); } if (mode != KeyWrapMode.Specification && mode != KeyWrapMode.AxCrypt) { throw new InternalErrorException("mode"); } _salt = salt; _mode = mode; _keyWrapIterations = keyWrapIterations; }
/// <summary> /// Create a KeyWrap instance for wrapping or unwrapping /// </summary> /// <param name="key">The key wrapping key</param> /// <param name="salt">An optional salt, or null if none. AxCrypt uses a salt.</param> /// <param name="iterations">The number of wrapping iterations, at least 6</param> /// <param name="mode">Use original specification mode or AxCrypt mode (only difference is that 't' is little endian in AxCrypt mode)</param> public KeyWrap(AesKey key, KeyWrapSalt salt, long iterations, KeyWrapMode mode) { if (key == null) { throw new ArgumentNullException("key"); } if (salt == null) { throw new ArgumentNullException("salt"); } if (salt.Length != 0 && salt.Length != key.Length) { throw new InternalErrorException("salt length is incorrect"); } if (iterations < 6) { throw new InternalErrorException("iterations"); } if (mode != KeyWrapMode.Specification && mode != KeyWrapMode.AxCrypt) { throw new InternalErrorException("mode"); } _mode = mode; _key = key; _salt = salt; _iterations = iterations; byte[] saltedKey = _key.GetBytes(); saltedKey.Xor(_salt.GetBytes()); _aes.Mode = CipherMode.ECB; _aes.KeySize = _key.Length * 8; _aes.Key = saltedKey; _aes.Padding = PaddingMode.None; }
/// <summary> /// Create a KeyWrap instance for wrapping or unwrapping /// </summary> /// <param name="key">The key wrapping key</param> /// <param name="iterations">The number of wrapping iterations, at least 6</param> /// <param name="mode">Use original specification mode or AxCrypt mode (only difference is that 't' is little endian in AxCrypt mode)</param> public KeyWrap(AesKey key, long iterations, KeyWrapMode mode) : this(key, KeyWrapSalt.Zero, iterations, mode) { }
/// <summary> /// Create a KeyWrap instance for wrapping or unwrapping /// </summary> /// <param name="keyWrapIterations">The number of wrapping iterations, at least 6</param> /// <param name="mode">Use original specification mode or AxCrypt mode (only difference is that 't' is little endian in AxCrypt mode)</param> public KeyWrap(long keyWrapIterations, KeyWrapMode mode) : this(Salt.Zero, keyWrapIterations, mode) { }