#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Duplicati.Library.Interface;
using System.IO;
using System.Security.Cryptography;
namespace Duplicati.Library.Encryption
{
///
/// Implements AES encryption
///
public class AESEncryption : EncryptionBase
{
///
/// The commandline option supplied if an explicit thread level should be set (--aes-set-threadlevel)
///
private const string COMMANDLINE_SET_THREADLEVEL = "aes-set-threadlevel";
///
/// The key used to encrypt the data
///
private string m_key;
///
/// The cached value for size overhead
///
private static long m_cachedsizeoverhead = -1;
///
/// The thread level to pass to SharpAESCrypt. 0 for default.
///
private static int m_usethreadlevel = 0;
///
/// Default constructor, used to read file extension and supported commands
///
public AESEncryption()
{
}
///
/// Constructs a new AES encryption/decyption instance
///
/// The key used for encryption. The key gets stretched through SHA hashing to fit the key size requirements
public AESEncryption(string passphrase, Dictionary options)
{
if(string.IsNullOrEmpty(passphrase))
throw new ArgumentException(Strings.AESEncryption.EmptyKeyError, "passphrase");
m_key = passphrase;
string strTL;
if (options != null && options.TryGetValue(COMMANDLINE_SET_THREADLEVEL, out strTL))
{
int useTL;
if (int.TryParse(strTL, out useTL))
{
// finally set thread level in a range of 0 (default) to 4
m_usethreadlevel = Math.Max(0, (Math.Min(4, useTL)));
}
}
}
#region IEncryption Members
///
/// The extension that the encryption implementation adds to the filename
///
/// The filename extension.
public override string FilenameExtension { get { return "aes"; } }
///
/// A localized description of the encryption module
///
/// The description.
public override string Description { get { return string.Format(Strings.AESEncryption.Description_v2); } }
///
/// A localized string describing the encryption module with a friendly name
///
/// The display name.
public override string DisplayName { get { return Strings.AESEncryption.DisplayName; } }
///
/// Dispose the specified disposing.
///
/// If set to true disposing.
protected override void Dispose(bool disposing) { m_key = null; }
///
/// Returns the size in bytes of the overhead that will be added to a file of the given size when encrypted
///
/// The size of the file to encrypt
/// The size of the overhead in bytes
public override long SizeOverhead(long filesize)
{
if (m_cachedsizeoverhead != -1)
return m_cachedsizeoverhead;
//If we use 1, we trigger the blocksize.
//As the AES algorithm does not alter the size,
// the results are the same as for the real size,
// but a single byte encryption is much faster.
return m_cachedsizeoverhead = base.SizeOverhead(1);
}
///
/// Encrypts the stream
///
/// The target stream
/// An encrypted stream that can be written to
public override Stream Encrypt(Stream input)
{
var cryptoStream = new SharpAESCrypt.SharpAESCrypt(m_key, input, SharpAESCrypt.OperationMode.Encrypt);
if (m_usethreadlevel != 0) cryptoStream.MaxCryptoThreads = m_usethreadlevel;
return cryptoStream;
}
///
/// Decrypts the stream to the output stream
///
/// The encrypted stream
/// The unencrypted stream
public override Stream Decrypt(Stream input)
{
var cryptoStream = new SharpAESCrypt.SharpAESCrypt(m_key, input, SharpAESCrypt.OperationMode.Decrypt);
if (m_usethreadlevel != 0) cryptoStream.MaxCryptoThreads = m_usethreadlevel;
return cryptoStream;
}
///
/// Gets a list of supported commandline arguments
///
/// The supported commands.
public override IList SupportedCommands
{
get
{
return new List(new ICommandLineArgument[] {
new CommandLineArgument(
COMMANDLINE_SET_THREADLEVEL,
CommandLineArgument.ArgumentType.Enumeration,
Strings.AESEncryption.AessetthreadlevelShort,
Strings.AESEncryption.AessetthreadlevelLong,
"0",
null,
new string[] {"0", "1", "2", "3", "4"}
),
});
}
}
#endregion
}
}