using System;
using System.Text;
using Xylem.Common.Cryptology.Security;
namespace Xylem.ServiceFwUpdate.Common.FwUpdateSafe
{
///
/// Class for FW-Update specific cryptology.
///
[Serializable]
public static class FwUpdateCrypt
{
///
/// Separator for domain to user login name
///
private const String StrDomainSeparator = "\\";
///
/// Separator for key fields
///
private const String StrKeyFieldSeparator = ":";
///
/// Nonce size
///
private const Int32 NonceSize = 16;
///
/// Key size
///
private const Int32 KeySize = 16;
///
/// Salt size for en- and decryption key
///
private const Int32 SaltSize = 8;
///
/// Build primary key sources to byte array from input
///
/// byte array containing primary key
///
/// - Initial.
///
public static Byte[] BuildPrimaryKey(String hardwareId, String domain, String userLoginName,
String userPwdHash)
{
var strPrimaryKey = hardwareId + StrKeyFieldSeparator +
domain + StrDomainSeparator +
userLoginName + StrKeyFieldSeparator +
userPwdHash;
return Encoding.ASCII.GetBytes(strPrimaryKey);
}
///
/// FW-Update specific encryption of raw data buffer
///
/// raw data buffer needed to be encrypted
/// primary key
/// encrypted data buffer
///
/// - Initial Dummy implementation as base for FW-Update SW password extraction.
///
///
/// - AES-GCM Encryption using bouncy Castle nuget package based on code snipped from Ray Savarda.
///
///
/// - Primary key as optional input, if this is null, the key is going to be build locally.
///
///
/// - Primary key is not optional anymore.
///
public static Byte[] Encrypt(Byte[] rawData, Byte[] primaryKey)
{
var encryptedData = Cryptology.AesGcmEncryption(SaltSize, NonceSize, KeySize, primaryKey, rawData);
return encryptedData;
}
///
/// FW-Update specific decryption of encrypted data buffer
///
/// encrypted data buffer = nonce | salt | Cipher text
/// optional primary key
/// decrypted raw data buffer
///
/// - Initial Dummy implementation as base for FW-Update SW password extraction.
///
///
/// - AES-GCM Decryption using bouncy Castle nuget package based on code snipped from Ray Savarda.
///
///
/// - Primary key is not optional anymore.
///
public static Byte[] Decrypt(Byte[] encryptedData, Byte[] primaryKey)
{
var rawData = Cryptology.AesGcmDecryption(SaltSize, NonceSize, KeySize, primaryKey, encryptedData);
return rawData;
}
}
}