103 lines
3.9 KiB
C#
103 lines
3.9 KiB
C#
using System;
|
|
using System.Text;
|
|
using Xylem.Common.Cryptology.Security;
|
|
|
|
namespace Xylem.ServiceFwUpdate.Common.FwUpdateSafe
|
|
{
|
|
/// <summary>
|
|
/// Class for FW-Update specific cryptology.
|
|
/// </summary>
|
|
[Serializable]
|
|
public static class FwUpdateCrypt
|
|
{
|
|
/// <summary>
|
|
/// Separator for domain to user login name
|
|
/// </summary>
|
|
private const String StrDomainSeparator = "\\";
|
|
|
|
/// <summary>
|
|
/// Separator for key fields
|
|
/// </summary>
|
|
private const String StrKeyFieldSeparator = ":";
|
|
|
|
/// <summary>
|
|
/// Nonce size
|
|
/// </summary>
|
|
private const Int32 NonceSize = 16;
|
|
|
|
/// <summary>
|
|
/// Key size
|
|
/// </summary>
|
|
private const Int32 KeySize = 16;
|
|
|
|
/// <summary>
|
|
/// Salt size for en- and decryption key
|
|
/// </summary>
|
|
private const Int32 SaltSize = 8;
|
|
|
|
/// <summary>
|
|
/// Build primary key sources to byte array from input
|
|
/// </summary>
|
|
/// <returns> byte array containing primary key </returns>
|
|
/// <remarks date="2021-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Initial.
|
|
/// </remarks>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// FW-Update specific encryption of raw data buffer
|
|
/// </summary>
|
|
/// <param name="rawData"> raw data buffer needed to be encrypted </param>
|
|
/// <param name="primaryKey">primary key</param>
|
|
/// <returns> encrypted data buffer </returns>
|
|
/// <remarks date="2020-Dec-02" author="Thomas Wiedebusch">
|
|
/// - Initial Dummy implementation as base for FW-Update SW password extraction.
|
|
/// </remarks>
|
|
/// <remarks date="2021-Jan-21" author="Thomas Wiedebusch">
|
|
/// - AES-GCM Encryption using bouncy Castle nuget package based on code snipped from Ray Savarda.
|
|
/// </remarks>
|
|
/// <remarks date="2021-Feb-03" author="Thomas Wiedebusch">
|
|
/// - Primary key as optional input, if this is null, the key is going to be build locally.
|
|
/// </remarks>
|
|
/// <remarks date="2021-Mar-15" author="Thomas Wiedebusch">
|
|
/// - Primary key is not optional anymore.
|
|
/// </remarks>
|
|
public static Byte[] Encrypt(Byte[] rawData, Byte[] primaryKey)
|
|
{
|
|
var encryptedData = Cryptology.AesGcmEncryption(SaltSize, NonceSize, KeySize, primaryKey, rawData);
|
|
|
|
return encryptedData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// FW-Update specific decryption of encrypted data buffer
|
|
/// </summary>
|
|
/// <param name="encryptedData"> encrypted data buffer = nonce | salt | Cipher text </param>
|
|
/// <param name="primaryKey">optional primary key</param>
|
|
/// <returns> decrypted raw data buffer </returns>
|
|
/// <remarks date="2020-Dec-02" author="Thomas Wiedebusch">
|
|
/// - Initial Dummy implementation as base for FW-Update SW password extraction.
|
|
/// </remarks>
|
|
/// <remarks date="2021-Jan-21" author="Thomas Wiedebusch">
|
|
/// - AES-GCM Decryption using bouncy Castle nuget package based on code snipped from Ray Savarda.
|
|
/// </remarks>
|
|
/// <remarks date="2021-Mar-15" author="Thomas Wiedebusch">
|
|
/// - Primary key is not optional anymore.
|
|
/// </remarks>
|
|
public static Byte[] Decrypt(Byte[] encryptedData, Byte[] primaryKey)
|
|
{
|
|
var rawData = Cryptology.AesGcmDecryption(SaltSize, NonceSize, KeySize, primaryKey, encryptedData);
|
|
|
|
return rawData;
|
|
}
|
|
}
|
|
}
|