diff --git a/src/Lumina.Infrastructure/Core/Security/CryptographyService.cs b/src/Lumina.Infrastructure/Core/Security/CryptographyService.cs index c45c5c3b..89c933e6 100644 --- a/src/Lumina.Infrastructure/Core/Security/CryptographyService.cs +++ b/src/Lumina.Infrastructure/Core/Security/CryptographyService.cs @@ -3,6 +3,7 @@ using Lumina.Application.Common.Infrastructure.Security; using Lumina.Infrastructure.Common.Models.Configuration; using Microsoft.Extensions.Options; using System; +using System.Linq; using System.Security.Cryptography; using System.Text; #endregion @@ -10,7 +11,7 @@ using System.Text; namespace Lumina.Infrastructure.Core.Security; /// -/// Service for cryptographic concerns. +/// Provides encryption and decryption services using AES encryption. /// public sealed class CryptographyService : ICryptographyService, IDisposable { @@ -26,17 +27,17 @@ public sealed class CryptographyService : ICryptographyService, IDisposable } /// - /// Encrypts a plaintext string using AES encryption with a randomly generated initialization vector (IV). + /// Encrypts a plaintext string using AES encryption with a randomly generated initialization vector (IV) and appends a SHA-256 checksum. /// /// The plaintext string to encrypt. - /// A Base64-encoded string of the combined IV and encrypted ciphertext. + /// A Base64-encoded string containing the IV, encrypted ciphertext, and checksum. /// Thrown when is or empty. public string Encrypt(string plaintext) { if (string.IsNullOrEmpty(plaintext)) throw new ArgumentException("Value cannot be null or empty", nameof(plaintext)); - // generate a new, random 16-byte IV for each encryption to ensure unique ciphertexts + // Generate a new, random 16-byte IV for each encryption to ensure unique ciphertexts byte[] iv = new byte[16]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) rng.GetBytes(iv); @@ -47,40 +48,51 @@ public sealed class CryptographyService : ICryptographyService, IDisposable aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; + // Encrypt the plaintext using ICryptoTransform encryptor = aes.CreateEncryptor(); byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); byte[] ciphertext = encryptor.TransformFinalBlock(plaintextBytes, 0, plaintextBytes.Length); - // combine IV and ciphertext - byte[] combined = new byte[iv.Length + ciphertext.Length]; + // Compute SHA-256 checksum of the plaintext + byte[] checksum = SHA256.HashData(plaintextBytes); + + // Combine IV, ciphertext, and checksum + byte[] combined = new byte[iv.Length + ciphertext.Length + checksum.Length]; Buffer.BlockCopy(iv, 0, combined, 0, iv.Length); Buffer.BlockCopy(ciphertext, 0, combined, iv.Length, ciphertext.Length); + Buffer.BlockCopy(checksum, 0, combined, iv.Length + ciphertext.Length, checksum.Length); + // Return the combined data as a Base64-encoded string return Convert.ToBase64String(combined); } /// - /// Decrypts a Base64-encoded string that contains both the IV and ciphertext. + /// Decrypts a Base64-encoded string that contains the IV, encrypted ciphertext, and checksum. /// - /// The Base64-encoded string containing both IV and ciphertext. + /// The Base64-encoded string containing IV, encrypted data, and checksum. /// The decrypted plaintext string. /// Thrown when is , empty, or invalid. + /// Thrown when the checksum validation fails, indicating potential data corruption. public string Decrypt(string ciphertext) { if (string.IsNullOrEmpty(ciphertext)) throw new ArgumentException("Value cannot be null or empty", nameof(ciphertext)); - // decode the combined data (IV + ciphertext) from Base64 + // Decode the combined data (IV + ciphertext + checksum) from Base64 byte[] combined = Convert.FromBase64String(ciphertext); - if (combined.Length < 16) + // Validate the minimum length (16 bytes for IV + 32 bytes for checksum) + if (combined.Length < 16 + 32) throw new ArgumentException("Invalid ciphertext"); - // extract IV and ciphertext + // Extract IV, ciphertext, and checksum from the combined data byte[] iv = new byte[16]; - byte[] encryptedData = new byte[combined.Length - 16]; - Buffer.BlockCopy(combined, 0, iv, 0, 16); - Buffer.BlockCopy(combined, 16, encryptedData, 0, encryptedData.Length); + byte[] checksum = new byte[32]; + byte[] encryptedData = new byte[combined.Length - 16 - 32]; + + Buffer.BlockCopy(combined, 0, iv, 0, iv.Length); + Buffer.BlockCopy(combined, iv.Length, encryptedData, 0, encryptedData.Length); + Buffer.BlockCopy(combined, iv.Length + encryptedData.Length, checksum, 0, checksum.Length); using Aes aes = Aes.Create(); aes.Key = _key; @@ -88,13 +100,23 @@ public sealed class CryptographyService : ICryptographyService, IDisposable aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; + // Decrypt the encrypted data using ICryptoTransform decryptor = aes.CreateDecryptor(); - byte[] decrypted = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length); - return Encoding.UTF8.GetString(decrypted); + byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length); + + // Compute the checksum of the decrypted data and validate against the provided checksum + byte[] computedChecksum = SHA256.HashData(decryptedBytes); + + // If checksums do not match, throw a CryptographicException indicating potential corruption + if (!computedChecksum.SequenceEqual(checksum)) + throw new CryptographicException("Checksum validation failed. The data may be corrupted."); + + // Return the decrypted plaintext as a UTF-8 string + return Encoding.UTF8.GetString(decryptedBytes); } /// - /// Clears sensitive key data when the service is disposed. + /// Clears sensitive data from memory. /// public void Dispose() {