Protecting sensitive information in a database requires more than restricting access to tables. Applications often store personally identifiable information, financial records, healthcare data, and other confidential values that should remain protected even when database administrators or infrastructure operators can access the database server.
SQL Server Always Encrypted addresses this problem by encrypting sensitive data on the client side. The application encrypts values before sending them to SQL Server, and decryption occurs on the client when the application is authorized to access the encryption keys.
This approach reduces the database server's ability to access plaintext data. However, it also introduces a limitation: SQL Server cannot perform every operation on encrypted values because it does not normally have access to their plaintext representation.
For example, searching for a specific encrypted value may be supported under certain encryption configurations, while operations such as pattern matching, range comparisons, and sorting can be restricted.
Always Encrypted with secure enclaves extends this model by allowing selected operations on encrypted data inside a protected execution environment on the database server.
Understanding how secure enclaves work helps developers choose the appropriate encryption configuration, design queries that remain compatible with encryption, and protect sensitive information without unnecessarily limiting application functionality.
What Is SQL Server Always Encrypted?
Always Encrypted is a SQL Server feature designed to protect sensitive column data from unauthorized access, including access by database administrators who do not possess the required encryption keys.
Traditional database encryption mechanisms, such as Transparent Data Encryption (TDE), primarily protect database files and related storage against certain forms of unauthorized access. Once the database engine has opened the database, authorized SQL Server operations can generally access the stored column values in plaintext.
Always Encrypted uses a different model.
The application encrypts sensitive values before sending them to the database. SQL Server stores encrypted values rather than the original plaintext values.
For example, suppose an application stores customer email addresses:
CREATE TABLE Customers
(
CustomerId INT PRIMARY KEY,
CustomerName NVARCHAR(100),
EmailAddress NVARCHAR(200)
);
Without column encryption, the database stores the email address as readable text.
With Always Encrypted configured for EmailAddress, the client driver encrypts the value before transmitting it to SQL Server. The database stores the encrypted representation.
The encryption process depends on encryption keys and column encryption metadata. Applications must be configured to access the appropriate keys through a supported key store.
How Always Encrypted Works
The process involves several components:
The application or client driver identifies encrypted columns through database metadata.
The client obtains the required key material through the configured key store.
The client encrypts parameter values before sending them to SQL Server.
SQL Server stores and processes the encrypted representations according to the supported encryption operations.
When an authorized application reads encrypted data, the client driver decrypts the returned values.
Two key types are central to this design.
Key | Purpose |
|---|
Column Encryption Key (CEK) | Encrypts and decrypts the actual column data. |
Column Master Key (CMK) | Protects the column encryption key and is managed through a supported key store. |
The database contains metadata describing the keys, but it does not need to contain the plaintext column encryption key.
This separation is important because possession of database access alone should not automatically provide access to the protected plaintext values.
Why Traditional Always Encrypted Restricts Queries
Always Encrypted has an important security boundary: SQL Server does not ordinarily receive the plaintext values needed to perform arbitrary operations on encrypted columns.
The supported query operations depend on the encryption type, the driver, and the SQL Server version.
Always Encrypted supports two primary encryption types: deterministic encryption and randomized encryption.
Deterministic Encryption
Deterministic encryption produces the same encrypted value when the same plaintext is encrypted using the same key and relevant encryption configuration.
This behavior can support equality comparisons because matching plaintext values produce matching ciphertext values.
For example, an application might search for a customer by an encrypted account identifier:
SELECT CustomerId, CustomerName
FROM Customers
WHERE AccountNumber = @AccountNumber;
If AccountNumber uses deterministic encryption and the client driver supports the parameter operation, the application can encrypt the parameter and SQL Server can compare the encrypted representations.
However, deterministic encryption can reveal equality patterns. An observer who can inspect ciphertext values may learn that multiple rows contain the same underlying value, even without knowing the plaintext.
Randomized Encryption
Randomized encryption uses randomness so that encrypting the same plaintext multiple times can produce different ciphertext values.
This reduces the equality information exposed by the encrypted representation.
However, because equal plaintext values generally do not produce identical ciphertext values, ordinary equality searches are not supported in the same way as they are with deterministic encryption.
Randomized encryption is therefore appropriate when minimizing information leakage is more important than supporting equality-based searches directly on the encrypted column.
Query Support Comparison
Operation | Traditional Always Encrypted | Always Encrypted with secure enclaves |
|---|
Insert encrypted values | Supported with compatible clients and configuration | Supported with compatible clients and configuration |
Read and decrypt values in the client | Supported for authorized clients | Supported for authorized clients |
Equality comparisons | Supported for suitable deterministic-encryption scenarios | Supported, with additional capabilities depending on configuration |
Pattern matching, such as LIKE | Generally restricted for encrypted columns | Supported for eligible operations and configurations |
Range comparisons | Generally restricted | Supported for eligible operations and configurations |
Sorting and richer operations on encrypted values | Restricted | Additional operations are possible when supported by the enclave configuration |
Server-side computation on plaintext values | Generally unavailable | Selected operations can execute inside the secure enclave |
This table describes broad capability differences rather than a guarantee that every SQL expression works with every encrypted column. Actual support depends on the SQL Server release, secure enclave technology, encryption configuration, client driver, and specific operation.
What Is a Secure Enclave?
A secure enclave is a protected execution environment that can process sensitive information while limiting the information exposed to the surrounding operating system and database engine.
Always Encrypted with secure enclaves extends the original encryption model by allowing selected operations on decrypted data inside the enclave.
The database engine can pass the necessary encrypted values into the protected environment. The enclave can then perform supported operations without exposing plaintext values to the ordinary database execution environment.
For example, consider a business application that needs to find customers whose encrypted account balances fall within a particular range.
Without enclave support, the database engine cannot ordinarily compare randomized encrypted balances as normal numeric values because it does not have their plaintext representation.
With an appropriately configured secure enclave, SQL Server can perform supported comparisons inside the protected execution environment.
The result is additional query functionality without abandoning the security objectives of Always Encrypted.
How the Enclave Changes Query Processing
The high-level process works as follows:
The application submits a parameterized query through a compatible client driver.
The driver and SQL Server use encryption metadata to determine the relevant encrypted columns and operations.
SQL Server passes the required encrypted data and instructions into the secure enclave.
The enclave obtains access to the necessary cryptographic material through the configured key-protection and attestation mechanisms.
The enclave decrypts and processes the data for supported operations.
SQL Server receives the results needed to complete query execution, while the enclave is designed to keep plaintext values isolated from the ordinary database engine.
The exact implementation depends on the enclave technology and deployment configuration. Not every Always Encrypted deployment supports secure enclaves, and the required setup must be verified for the target SQL Server environment.
Implementing Always Encrypted With Secure Enclaves
Before using secure enclaves, verify that the selected SQL Server version and deployment environment support the required features.
You must also configure the column encryption keys, column master key, key store, enclave-related settings, and client driver appropriately.
A simplified implementation begins with a table containing a sensitive column.
Step 1: Identify the Sensitive Data
Suppose an application stores customer information and needs to search by a sensitive government-issued identifier.
The table might contain:
CREATE TABLE CustomerRecords
(
CustomerId INT PRIMARY KEY,
FullName NVARCHAR(150) NOT NULL,
GovernmentId NVARCHAR(50) NOT NULL
);
In a production environment, sensitive identifiers should not be stored as ordinary plaintext if the application requires Always Encrypted protection.
The encryption configuration must be designed for the required operations. For example, a column that requires equality searches has different requirements from a column that needs range comparisons or pattern matching.
Step 2: Configure the Encrypted Column
Always Encrypted uses column encryption metadata to associate a column with its encryption key and encryption type.
In supported SQL Server tooling, the configuration can be performed through the Always Encrypted wizard or by generating appropriate DDL for the selected environment.
The resulting column definition conceptually resembles:
GovernmentId NVARCHAR(50)
COLLATE Latin1_General_BIN2
ENCRYPTED WITH
(
COLUMN_ENCRYPTION_KEY = CEK_CustomerRecords,
ENCRYPTION_TYPE = Randomized,
ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256'
)
NOT NULL
This fragment illustrates the structure of an encrypted column definition. It is not a complete standalone deployment script: the column encryption key must already exist, the column master key must be configured, and the table definition must match the requirements of the target SQL Server version.
Always Encrypted requires compatible encryption metadata and supported data types. Some configurations also impose collation requirements.
Randomized encryption is appropriate when minimizing equality-pattern leakage is a priority. However, it does not support ordinary equality searching in the same manner as deterministic encryption.
If the application requires searching or comparing encrypted values, choose the encryption type and enclave configuration according to the exact operation requirements.
Step 3: Configure the Client Connection
A .NET application using Microsoft.Data.SqlClient can enable Always Encrypted through its connection string.
For example:
using Microsoft.Data.SqlClient;
var connectionString =
"Server=YOUR_SERVER;" +
"Database=CustomerDatabase;" +
"Integrated Security=True;" +
"Encrypt=True;" +
"TrustServerCertificate=False;" +
"Column Encryption Setting=Enabled;";
using var connection = new SqlConnection(connectionString);
connection.Open();
The example uses integrated authentication and assumes that the application runs in an environment where the configured identity has the required database permissions.
In production, use an appropriate authentication mechanism, securely manage credentials, and configure the certificate trust chain correctly.
The Column Encryption Setting=Enabled option enables Always Encrypted processing in the client driver. It does not, by itself, configure secure enclaves or guarantee that enclave-dependent operations will work.
The driver, SQL Server deployment, key store, and enclave configuration must all support the intended query.
Querying Encrypted Data With Parameters
Always Encrypted works most predictably when applications use parameterized SQL statements and compatible client drivers.
For example, an application might retrieve a customer's record by its identifier:
using Microsoft.Data.SqlClient;
using System.Data;
const string sql = """
SELECT CustomerId, FullName, GovernmentId
FROM CustomerRecords
WHERE GovernmentId = @GovernmentId;
""";
using var connection = new SqlConnection(connectionString);
connection.Open();
using var command = new SqlCommand(sql, connection);
command.Parameters.Add(
"@GovernmentId",
SqlDbType.NVarChar,
50
).Value = governmentId;
using var reader = command.ExecuteReader();
while (reader.Read())
{
int customerId = reader.GetInt32(0);
string fullName = reader.GetString(1);
string decryptedGovernmentId = reader.GetString(2);
Console.WriteLine($"{customerId}: {fullName}");
}
This example demonstrates client-side parameter handling and reading decrypted results.
Whether the equality predicate works depends on the column's encryption type and the supported client and server configuration. A randomized encrypted column does not automatically become searchable merely because the application uses parameters.
For enclave-enabled operations, the application must also meet the applicable enclave configuration and attestation requirements.
The example assumes that the application identity has access to the required key material. If the driver cannot access the column master key or decrypt the returned data, the operation may fail even when the SQL statement itself is valid.
Using Secure Enclaves for Range Queries
One important advantage of secure enclaves is the ability to support selected operations that are difficult or unavailable with traditional Always Encrypted.
Consider an application that needs to search sensitive salary information within a range.
SELECT EmployeeId, Department
FROM EmployeeRecords
WHERE Salary >= @MinimumSalary
AND Salary <= @MaximumSalary;
If the salary column is encrypted using a supported enclave-enabled configuration, the enclave may perform the required comparisons on decrypted values within the protected environment.
This can be useful for financial analysis, employee reporting, and other applications that require filtering sensitive information.
However, developers should verify the precise operation support for their SQL Server version and enclave configuration. Not every comparison, function, aggregation, or query expression is automatically supported.
Why Randomized Encryption Matters
Randomized encryption limits equality-pattern leakage because the same plaintext can produce different ciphertext values.
Secure enclaves can expand the range of supported operations without requiring developers to switch every sensitive column to deterministic encryption.
This allows applications to preserve stronger confidentiality properties where the chosen enclave technology supports the required operation.
Nevertheless, secure enclaves do not eliminate every possible information leak. Query results, access patterns, execution behavior, and other observable details may reveal information depending on the threat model.
Encryption design should therefore consider both the confidentiality requirements and the application's query needs.
Understanding Attestation and Key Protection
Secure enclave deployments require careful consideration of trust.
Attestation is the process used to verify that the intended protected execution environment is running and meets the required security conditions.
Depending on the SQL Server version and enclave technology, Always Encrypted with secure enclaves can use different attestation mechanisms. The supported mechanism and setup requirements must be confirmed for the target deployment.
Attestation helps the client establish trust before allowing sensitive key material to be used by the enclave.
Key management remains essential. The column master key should be protected in an appropriate key store, such as a supported certificate store or key management service.
The database administrator should not automatically gain access to the plaintext column encryption key simply because the administrator can query the database.
For production deployments, document:
Where the column master key is stored.
Which identities can access the key store.
How enclave attestation is configured.
How keys are rotated and recovered.
Which SQL Server and client driver versions are supported.
How access to plaintext results is controlled and audited.
A secure enclave is one part of the security architecture, not a substitute for sound key management or application authorization.
Common Mistakes When Using Secure Enclaves
Assuming That Enabling Always Encrypted Enables Enclaves
The client connection setting enables Always Encrypted processing, but enclave support requires additional server-side and key-management configuration.
Verify that the deployment supports the required enclave technology and that the client driver is configured accordingly.
Choosing the Encryption Type Without Considering Query Requirements
Randomized encryption provides stronger protection against equality-pattern analysis, but traditional equality searches are restricted.
Deterministic encryption supports certain equality operations but reveals equality patterns.
Select the encryption configuration based on the actual queries the application must execute and the confidentiality requirements of the data.
Assuming Every SQL Operation Is Supported
Secure enclaves expand query support, but they do not make all SQL expressions compatible with encrypted columns.
Test the actual predicates, functions, sorting operations, and query plans used by the application. Consult the supported feature set for the specific SQL Server version and enclave technology.
Storing Keys Alongside the Database
Placing key material in an unprotected location undermines the security model.
Use an appropriate key store, restrict key access to authorized identities, and maintain a documented recovery and rotation process.
Ignoring Client Driver Compatibility
Always Encrypted processing depends on client driver capabilities.
A query that works with one supported driver version may behave differently or fail when used with an older driver. Standardize and test driver versions across application environments.
Assuming Enclaves Remove All Security Risks
Secure enclaves help isolate sensitive processing, but they do not eliminate risks involving compromised application identities, excessive permissions, poorly protected keys, or unauthorized access to decrypted results.
Apply defense in depth, including least-privilege database permissions, secure authentication, logging, and appropriate application-level access controls.
Best Practices for Production Applications
Use the following practices when implementing Always Encrypted with secure enclaves:
Identify the sensitive columns and the exact query operations required before choosing an encryption configuration.
Verify SQL Server, client driver, and deployment support before designing the solution.
Use parameterized queries and supported client drivers.
Protect column master keys using an approved key-management mechanism.
Configure and validate enclave attestation according to the selected technology.
Test query behavior with realistic data volumes and representative parameters.
Measure query latency and resource consumption before and after enabling enclave-dependent operations.
Apply least-privilege access controls to database objects and key stores.
Document key rotation, disaster recovery, and application migration procedures.
Revalidate query compatibility after SQL Server or client driver upgrades.
These practices help teams introduce additional query functionality without weakening the security controls that motivated encryption in the first place.
Advantages and Disadvantages of Always Encrypted With Secure Enclaves
Advantages
Enables selected operations on encrypted data that are restricted under traditional Always Encrypted.
Helps preserve client-side key protection while supporting more flexible queries.
Can reduce the need to choose deterministic encryption solely to support certain searches.
Supports applications that need to filter or compare sensitive information while limiting plaintext exposure to the ordinary database engine.
Provides a way to balance data confidentiality with application query requirements.
Disadvantages
Requires additional configuration and operational knowledge.
Depends on compatible SQL Server versions, client drivers, key stores, and enclave technology.
Introduces attestation and key-management requirements.
Does not support every SQL operation on every encrypted column.
Can add complexity to deployment, troubleshooting, testing, and application upgrades.
Does not eliminate information leakage through query patterns, results, or other observable behavior.
Conclusion
SQL Server Always Encrypted protects sensitive column values by moving encryption and decryption responsibilities to the client. This model limits the database engine's ability to access plaintext data, but it also restricts the operations that SQL Server can perform on encrypted values.
Always Encrypted with secure enclaves extends the model by allowing selected operations to run inside a protected execution environment. Depending on the supported configuration, applications can perform additional searches and comparisons without exposing plaintext values to the ordinary database execution environment.
Successful implementation requires more than enabling a connection-string option. Developers must select the appropriate encryption type, configure keys and attestation, verify driver compatibility, and test the SQL operations the application actually uses.
For applications that need both strong protection for sensitive data and richer database queries, secure enclaves can provide a useful middle ground. The right design depends on the threat model, supported SQL operations, deployment environment, and operational requirements.
Join the conversation! Your thoughts help the community grow.