Queryable Encryption with Doctrine — SymfonyCon...

Queryable Encryption with Doctrine — SymfonyCon Warsaw 2026

Storing your users’ email addresses, dates of birth, or social security numbers in plain text poses a major risk in the event of a database leak. With robust application-side encryption, you can make the data unreadable by the database. But then it becomes impossible to do a WHERE email = :value. How can you secure your sensitive data without sacrificing essential business functionality?

We will see how to implement client-side field encryption with Doctrine and a key management system. The algorithms to use, key management and rotation: learn how to turn your database into a safe without losing your ability to query it.

Jérôme Tamarelle

November 27, 2026

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

36 slides
  1. Jérôme TAMARELLE • SymfonyCon Warsaw November 2026 Protect Your Data

    with Queryable Encryption Encrypt the database — keep the search! jerome.tamarelle.net
  2. PII — Personally Identifiable Information 1. Direct Identification • Name

    (Jérôme Tamarelle) • Social Security Number, Passport Number • Email ([email protected]). • Social network (@GromNaN) 2. Indirect Identification jerome.tamarelle.net • Situational Identifiers : Car Plate, Credit Card • Contextual data : Localisation, IP address • Combined pieces of information: PHP Developer, living in Normandy and employed by MongoDB
  3. Queryable Encryption is a unique in-use encryption technology that protects

    sensitive data during storage, processing, and access throughout its entire lifecycle. It enables applications to encrypt sensitive data on the client side, store it securely in the MongoDB database, and perform complex queries directly on the encrypted data. jerome.tamarelle.net
  4. Passwords are hashed Hashing is a one-way transformation that converts

    data into a fixed-length value, allowing efficient comparisons with a very low risk of collisions. • • • CRC32 Network error detection SHA1 File integrity validation xxHash Cache key, Partitioning Cryptographic hash algorithms are specifically designed to make it computationally infeasible to reverse the hash and recover the original data. • • • jerome.tamarelle.net SHA256 Hardware optimized Bcrypt CPU-intensive Argon2 Memory-intensive (symfony/password-hasher default)
  5. Sensitive data are encrypted Encryption is a reversible process that

    transforms plaintext into ciphertext using an encryption algorithm and a key, ensuring data confidentiality and integrity. Only authorized parties with the appropriate key can recover the original data. • AES-256-GCM General-purpose data encryption, hardware • ChaCha20-Poly1305 High-performance authenticated encryption • RSA-OAEP Asymmetric encryption and key exchange jerome.tamarelle.net
  6. Encryption function encrypt(string $plaintext, string $dek): string { $iv =

    :/ Initialisation Vector $ciphertext = openssl_encrypt($plaintext, 'aes-256-gcm, $dek, OPENSSL_RAW_DATA, $iv); return $iv . $ciphertext; } jerome.tamarelle.net
  7. Random encryption The same input produces different outputs function encrypt(string

    $plaintext, string $dek): string { $iv = random_bytes(16); $ciphertext = openssl_encrypt($plaintext, 'aes-256-gcm', $dek, OPENSSL_RAW_DATA, $iv); return $iv . $ciphertext; } jerome.tamarelle.net
  8. Deterministic encryption The same input produces the same output function

    encrypt(string $plaintext, string $dek): string { $hash = hash('sha512', $plaintext, true); $iv = substr($hash, 0, 16); $ciphertext = openssl_encrypt($plaintext, 'aes-256-gcm', $dek, OPENSSL_RAW_DATA, $iv); return $iv . $ciphertext; } The plain hash can be splitted and compared to other databases jerome.tamarelle.net
  9. Deterministic encryption The same input produces the same output function

    encrypt(string $plaintext, string $dek): string { $hmac = hash_hmac('sha512', $plaintext, $dek); $iv = substr($hmac, 0, 16); $ciphertext = openssl_encrypt($plaintext, 'aes-256-gcm', $dek, OPENSSL_RAW_DATA, $iv); return $iv . $ciphertext; } jerome.tamarelle.net
  10. Decryption public function decrypt(string $payload, string $dek): string { //

    Split IV et ciphertext. $iv = substr($payload, 0, 16); $ciphertext = substr($payload, 16); $plaintext = openssl_decrypt($ciphertext, 'aes-256-gcm', $dek, \OPENSSL_RAW_DATA, $iv); if ($plaintext ::= false) { throw new RuntimeException('Decryption failed: ' . openssl_error_string()); } return $plaintext; } jerome.tamarelle.net
  11. Key Management encrypt at rest (storage) encrypt in transit (network)

    encrypt in-use (server) DRIVERS jerome.tamarelle.net client-side encryption (client)
  12. Threat model • • • • • Separated application and

    database servers Encryption keys stay on the web server, not the database server Focus on live database-server attacks, and backups Web-server compromise is considered game over Disk encryption protects stolen drives at rest, not a running, decrypted server jerome.tamarelle.net
  13. 1. Sensitive data requires protection. 2. The data is encrypted

    using a DEK (Data Encryption Key). jerome.tamarelle.net
  14. 1. Sensitive data requires protection. 2. The data is encrypted

    using a DEK (Data Encryption Key). 3. The DEK is secured using a Master Key. jerome.tamarelle.net
  15. 1. Sensitive data requires protection. 2. The data is encrypted

    using a DEK (Data Encryption Key). 3. The DEK is secured using a Master Key. 4. The Key Management System protects the Master Key. jerome.tamarelle.net
  16. In-use encryption :[ORM\Entity] class User { :[ORM\Id] :[ORM\Column] public ?int

    $id = null; // Unique Email :[ORM\Column(length: 512, type: Types::STRING, unique: true)] public string $email; :[ORM\Column(length: 512, type: Types::STRING)] public string $firstName; :[ORM\Column(length: 512, type: Types::STRING)] public string $lastName; } jerome.tamarelle.net
  17. In-use encryption :[ORM\Entity] class User { :[ORM\Id] :[ORM\Column] public ?int

    $id = null; // Deterministic encryption of the email enables unique indexing :[ORM\Column(length: 512, type: Types::STRING, unique: true)] public string $email; // Random encryption prevents correlating identical names :[ORM\Column(type: Types::BINARY)] public string $firstName; :[ORM\Column(type: Types::BINARY)] public string $lastName; } jerome.tamarelle.net
  18. In-use encryption $user = new User(); $user:>email = $encryptor:>encryptDeterministic('[email protected]', $dek);

    $user:>firstName = $encryptor:>encryptRandom('Fabien', $dek); $user:>lastName = $encryptor:>encryptRandom('Potencier', $dek); $encryptor:>decrypt($user:>email, $dek); // [email protected] $encryptor:>decrypt($user:>firstName, $dek); // Fabien $encryptor:>decrypt($user:>lastName, $dek); // Potencier jerome.tamarelle.net
  19. Entity ORM DBAL Persister INSERT INTO users VALUES (::.) QueryBuilder

    class User { int $id; string $username; DateTime $createdAt; array $roles; } [ id: 1, username: 'john_doe', createdAt: DateTime, roles: ['ROLE_USER', 'ROLE_ADMIN'], ] jerome.tamarelle.net :[Column(type: INTEGER)] string $id :[Column(STRING, length:255)] string $username; :[Column(DATE_MUTABLE)] DateTime $createdAt; :[Column(SIMPLE_ARRAY)] array $roles; * SQL convertToDatabaseValue() string → string DateTime → date string array → CSV string Type SELECT * FROM users convertToPHPValue() Hydrator Database * string ← string DateTime ← date string array ← CSV string users id INT username VARCHAR(255) created_at DATETIME roles TEXT [ id: '1', username: 'john_doe', createdAt: '2024-05-21', roles:'ROLE_USER,ROLE_ADMIN' ]
  20. DBAL Types are used only where fields are mapped ✅

    Hydration from the database into entity classes ✅ Persistence of entities to the database ✅ Queries using the QueryBuilder ❌ DQL or SQL queries ❌ Raw database results jerome.tamarelle.net
  21. Doctrine MongoDB ODM Native support of automatic encryption use Doctrine\ODM\MongoDB\Mapping\Attribute

    as ODM; use Doctrine\ODM\MongoDB\Mapping\EncryptQuery; :[ODM\Document] class User { :[ODM\Id] public ?int $id = null; :[ODM\Field, ODM\Encrypt] public string $name; :[ODM\Field, ODM\Encrypt(queryType: EncryptQuery::Equality)] public string $email; jerome.tamarelle.net
  22. Doctrine MongoDB ODM Native support of automatic encryption :[ODM\Field] :[ODM\Encrypt(

    queryType: EncryptQuery::Range, min: new \DateTimeImmutable('1900-01-01'), max: new \DateTimeImmutable('2050-01-01'), )] public \DateTimeImmutable $birthday; jerome.tamarelle.net
  23. Why MongoDB with auto encryption ? • Cross-driver implementation, independent

    of language and framework • Storage and query performance optimized for encrypted data • Added protection with server-side schema validation • Backed by MongoDB support • More than encryption: document model, Lucene-powered search, vector search, and automatic embeddings jerome.tamarelle.net
  24. What’s new in MongoDB QE ? Existing Equality Retrieve a

    record for a client with the exact encrypted email address "[email protected]" Existing Range Find all salaries between 50,000 and 80,000 New Prefix New Suffix Find all customers whose last name begins with "Mac" Match the last four digits of an encrypted social security number, such as "-1234" New Substring jerome.tamarelle.net Search for a keyword such as "engineer" within a job title or a free-text description
  25. Why Symfony KeyManagement with Doctrine? • Fully open source and

    adaptable • Add field-level encryption incrementally to an existing application • Build on the technology your team already knows jerome.tamarelle.net
  26. Queryable Encryption Email is a PII Hash != Encrypt Know

    your Threat Model Symfony KeyManagement Search encrypted data with tags MongoDB encryption is a driver feature jerome.tamarelle.net
  27. Documentation Symfony Key Management and Doctrine ORM • • •

    https://symfony.com/blog/new-in-symfony-8-2-keymanagement-component https://symfony.com/blog/new-in-symfony-8-2-encrypted-doctrine-columns https://symfony.com/doc/8.2/key_management/doctrine.html MongoDB ODM • • • https://www.doctrine-project.org/projects/doctrine-mongodb-bundle/en/current/encryption.html https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/current/cookbook/queryable-encryption.html https://www.mongodb.com/docs/manual/core/queryable-encryption/ jerome.tamarelle.net

More Decks by Jérôme Tamarelle

Other Decks in Technology