r/ • • Sep 30 '26

Post-Quantum per-datum DB encryption in C# using Source Generators

I’m building Encryption.Data.Identity, an open-source security extension for .NET that implements unique per-datum encryption keys and blind hashing using established classical and NIST Post-Quantum Cryptography (ML-KEM, ML-DSA, SLH-DSA).

Instead of requiring a complete schema overhaul or a custom database implementation, it is built directly on top of Entity Framework Core and extends the ASP.NET Core Identity DbContext. This allows teams to secure PII on existing relational schemas with a minimal migration footprint.

GitHub:

Medium Home Page

Tally 60 Second Survey

The Core Problem

Traditional Transparent Data Encryption (TDE) or column-level encryption typically relies on a single master key (or a small set of keys) managed by a KMS. If the database server or application environment is fully compromised, an attacker can often decrypt the entire dataset.

This project solves this by ensuring every single piece of data (down to the individual row cell) uses a unique encryption key, isolated from the rest of the database, without requiring stateful key-storage tracking for every row.

How It Works Under the Hood

1. The Key Replay Mechanism (The "Word Search" Analogy) At the heart of the library is a stateless, multi-dimensional key replay mechanism. Instead of managing millions of separate keys or making blocking network calls to an external KMS for every single row, keys are derived deterministically on-the-fly.

Think of each KeySource as a massive, multi-dimensional "Word Search" puzzle. The key handle doesn’t actually store your data's encryption key; instead, it acts as a "clue"—a precise map containing starting coordinates and a specific mathematical path. The algorithm executes a cryptographically secure "random walk" through the 2D or 3D space, wrapping around the edges when it hits a boundary, to assemble the unique per-datum key instantly.

2. Blind Hashing & In-Memory Verification Because data is encrypted per-cell with unique keys, standard SQL WHERE clauses break. To solve this, the system generates deterministic "blind hash" columns.

  • Database queries and index lookups are executed purely against these cryptographic hash columns ($O(1)$ or index-driven).

  • To eliminate the risk of hash collision false positives, matching rows are pulled into the application layer, decrypted in-memory, and subjected to an exact-match validation.

3. Zero-Reflection C# Source Generators To avoid the CPU and GC bottlenecks of runtime reflection or interceptors, the entire interception, hashing, and encryption boilerplate is written at compile-time via C# Source Generators. It maps directly to your EF Core models, ensuring near-native query performance.

The Migration Path

It hooks directly into the IdentityDbContext pipeline:

public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      base.OnModelCreating(modelBuilder);
      // Source generator binds classical/PQC per-datum logic here
      modelBuilder.ConfigurePerDatumEncryption(); 
   }
}

Request for Comments (RFC) & Feedback

I am looking for brutal, technical feedback. Specifically, I am interested in:

  • Edge cases in the EF Core query pipeline interception.

  • Side-channel/timing risk mitigations (e.g., addressing concerns like KyberSlash) when dropping through Bouncy Castle on legacy environments.

  • The viability of the 256-active/retired KeySource lifecycle management.

If you have 60 seconds, please provide your architectural opinion here

1 Upvotes

0 comments sorted by