Lesson 2 of 2 Data Encryption

Always Encrypted and Secure Enclaves

This lesson explains Always Encrypted keys, deterministic and randomized encryption, secure enclaves and the requirements for applications.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the roles of the column encryption key and column master key.
  • Distinguish between deterministic and randomized encryption.
  • Explain what secure enclaves add and what applications must change.

Keys and roles

Always Encrypted uses two levels of keys. A column encryption key (CEK) encrypts the data in one or more columns, and the database stores only CEK values encrypted by a column master key (CMK). The CMK is stored outside the database, in Azure Key Vault, a managed HSM or a Windows certificate store, and the database stores only metadata that points to it. The client driver retrieves the encrypted CEK, decrypts it with the CMK (so the client identity needs unwrap and verify permissions on the key in Key Vault), and encrypts parameters and decrypts results. This separates duties: database administrators manage the database but have no access to the CMK, while security administrators manage keys but do not need database access.

The following diagram shows where each key lives and who uses it.

Always Encrypted keys and roles. A column encryption key encrypts the data in one or more columns, and the database stores it only in encrypted form. The column master key that encrypts it is kept outside the database, for example in Azure Key Vault, and the database stores only metadata pointing to it. The client driver fetches the encrypted column encryption key, unwraps it with the master key, encrypts parameters and decrypts results. Database administrators never hold the master key.
The master key never enters the database.
Match the pairs

Always Encrypted keys

Exam skills 2.1.1

Match each Always Encrypted element with its role.

Log in to try this activity Interactive activities are free with an account.

Deterministic and randomized encryption

Deterministic encryption always produces the same ciphertext for the same value, which allows equality comparisons, joins, GROUP BY and indexes, but can reveal patterns in columns with few distinct values, such as gender. Randomized encryption produces different ciphertext each time and is more secure, but allows no computation without enclaves. Deterministic encryption of character columns requires a binary2 (BIN2) collation.

CREATE TABLE HR.Employee
(
    EmployeeID int PRIMARY KEY,
    NationalID char(11) COLLATE Latin1_General_BIN2
        ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CEK_HR,
                        ENCRYPTION_TYPE = DETERMINISTIC,
                        ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL,
    Salary decimal(12,2)
        ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CEK_HR,
                        ENCRYPTION_TYPE = RANDOMIZED,
                        ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL
);
Sort into groups

Deterministic or randomized?

Exam skills 2.1.1

Choose the encryption type for each column.

Log in to try this activity Interactive activities are free with an account.

Secure enclaves

Always Encrypted with secure enclaves lets the database engine compute on encrypted data inside a protected memory region that the rest of the engine and the operating system cannot read. Azure SQL Database uses VBS enclaves (virtualization-based security), and SQL Server can also use VBS enclaves. Enclave-enabled keys allow range comparisons, LIKE pattern matching and sorting on randomized columns, and in-place encryption of existing columns with ALTER TABLE instead of moving data through a client tool. Clients enable enclave computations with an attestation protocol setting in the connection string.

Select all that apply

What enclaves add

Exam skills 2.1.1

Identify what Always Encrypted with secure enclaves allows.

Log in to try this activity Interactive activities are free with an account.

Application requirements and limits

Applications use a supported driver (such as Microsoft.Data.SqlClient, JDBC or ODBC) with Column Encryption Setting=Enabled, and they must pass values for encrypted columns as parameters, because the driver can only encrypt parameter values, not literals in the query text. Several features do not work with encrypted columns, as the following list explains.

  • The vector data type is not supported, so embeddings cannot be stored in an Always Encrypted column.
  • Dynamic Data Masking, full-text indexes and many built-in functions cannot operate on encrypted columns.
  • Change event streaming does not support tables with Always Encrypted columns.
  • Data virtualization queries can fail when SSMS parameterization for Always Encrypted is enabled.
True or false

Always Encrypted limits

Exam skills 2.1.1

Decide whether each statement about Always Encrypted is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

Searching an encrypted column

An application stores tax IDs with Always Encrypted and randomized encryption. A new feature must find customers whose tax ID starts with a given prefix, and the data must stay protected from administrators. The database is in Azure SQL Database. What should the developer implement?

Use Always Encrypted with secure enclaves (VBS enclaves) with an enclave-enabled column master key, and query with a parameterized LIKE predicate from an enclave-enabled connection. Randomized encryption alone allows no comparisons, and switching to deterministic encryption allows only equality, not prefix matching. Decrypting all rows in the application would work functionally but scales poorly.

Check yourself

Why must values for Always Encrypted columns be passed as parameters?

Show answer

The client driver can encrypt only parameter values; literals in the query text reach the server unencrypted and cannot be compared with ciphertext.

Exam Tips

Remember for the exam

  • Keys: CEK encrypts data; CMK, outside the database, protects the CEK.
  • Equality lookups and joins: deterministic; strongest protection: randomized.
  • Range, LIKE and in-place encryption on encrypted data: secure enclaves.
  • Values for encrypted columns must be passed as parameters.