Security, Cryptography & Tenant Isolation
Security across the EricksonLopez.* ecosystem follows Principle 5 (Secure defaults over permissive defaults). EricksonLopez.Security combines authenticated encryption (AesGcmEncryptionEngine, ChaCha20Poly1305EncryptionEngine), versioned key management (KeyRing), memory-hard password hashing (Argon2idPasswordHasher), and constant-time comparison (ConstantTimeComparer).
Authenticated Encryption & Envelope Binding (AesGcmSecretProtector)
Section titled “Authenticated Encryption & Envelope Binding (AesGcmSecretProtector)”AesGcmSecretProtector encrypts secrets with 256-bit AES-GCM (AesGcmEncryptionEngine.Shared), resolves active keys via IEncryptionKeyProvider / KeyRing, disposes SecretBuffer immediately after encryption so key bytes are zeroed via CryptographicOperations.ZeroMemory, and binds Authenticated Associated Data (AuthenticatedContext) to prevent cross-tenant ciphertext confusion:
using System;using System.Text;using System.Threading;using System.Threading.Tasks;using EricksonLopez.Result;using EricksonLopez.Security.Abstractions.Cryptography;using EricksonLopez.Security.Abstractions.KeyManagement;using EricksonLopez.Security.Abstractions.Primitives;using EricksonLopez.Security.Secrets;
namespace Example.Security;
public static class TenantSecretHelper{ public static ValueTask<Result<string>> ProtectTenantTokenAsync( AesGcmSecretProtector protector, string tenantId, string rawApiToken, CancellationToken cancellationToken) { ReadOnlySpan<byte> plaintext = Encoding.UTF8.GetBytes(rawApiToken); var aad = AuthenticatedContext.ForTenant(tenantId);
Result<string> protectedEnvelope = protector.ProtectToBase64Url( secret: plaintext, purpose: KeyPurpose.SecretProtection, expectedAssociatedData: aad);
return ValueTask.FromResult(protectedEnvelope); }}Note: On decryption (
UnprotectFromBase64Url/UnprotectToSecretBufferAsync), callers must supply the exact matchingAuthenticatedContext(tenant:{tenantId}); otherwiseAesGcmSecretProtectorreturnsSecurityError.AssociatedDataMismatch.
Argon2idPasswordHasher(RFC 9106): Defaults to 64 MiB memory (65536KiB),3iterations,4parallelism lanes, and 16-byte random salt, with bounded verification parameters (MaxAllowedMemoryCost = 65536,MaxAllowedTimeCost = 10) to prevent denial-of-service exhaustion.ConstantTimeComparer/TimingSafeString: Prevents timing side-channel leaks when comparing API keys, HMAC tags, or webhook signatures.- Zero-Trust ABAC (
EricksonLopez.Security.ZeroTrust):AbacPolicyEngineevaluates explicitAbacContextattributes rather than trusting ambient state.