Server 2016 SQL Always Encrypted Integration Guide
Server 2016 SQL Always Encrypted Integration Guide
Data protected by Always Encrypted remains in an encrypted state until it has reached the on–premises client
application server, this effectively mitigates man in the middle attacks and provides assurances against unauthorized
activity from rogue DBAs or admins with access to Azure / SQL server Databases. Always Encrypted was designed
to be used in conjunction with Transparent Data Encryption however; TDE is NOT a requisite for implementing
Always Encrypted.
Configuring Always Encrypted involves creating and provisioning cryptographic keys, specifically:
A Column Master Key – The CMK, is an asymmetric RSA encryption key of size 2048 bits
One or more Column Encryption Key(s) - A CEK, is a symmetric AES key of size 256 bits.
The CEK is responsible for encrypting the database column data while the CMK is protected by the HSM and is
responsible for wrapping (encrypting) the CEK.
The table below shows current support for the different data operations.
The Column Master Key is generated using the Thales nCipher CNG provider via the HSM and the key(s) stored in
an encrypted state on the on-premises client application server in the %NFAST_KMDATA%\local folder.
Note: It is recommended that the server configured with Always Encrypted be located on a different server than
that on which the database resides.
Always Encrypted supports two named types of encryption, Deterministic and Randomized. Selecting deterministic
encryption means that the same encrypted value will be produced from the same plaintext value each time
encryption occurs, this allows for point lookups, equality joins, grouping and indexing on encrypted columns.
However, this has implications on the security of the data as it potentially allows an attacker to ‘guess’ the plaintext
from the recurring cipher text through emerging patterns within the encrypted columns. Deterministic encryption
should not really be used where a small set of values are presented, e.g. True / False, Yes / No etc. Randomized
encryption is more secure, as it produces different cipher text values from the same plaintext every time the data is
encrypted, eliminating the predictable aspects associated with deterministic encryption, however, this also removes
the ability to perform any search operations on the encrypted data in situ.
* Module protection utilises an AES 256 bit symmetric key with 128 bit security secured by the Security World Module
key which is stored in the HSM hardware FIPS 140-2 level 3 boundary.
N.B. Always backup you database before performing any activity that may compromise database availability.
The integration was performed and tested using the following configuration:
You must have at least .NET Framework 4.6.1 on the on-premises client server before installing SQL Server
Management Studio (SSMS). The download for.NET framework 4.6.1 can be obtained via the link below:
https://www.microsoft.com/en-us/download/details.aspx?id=49982
The Always Encrypted integration process requires administrator level access to both the “on-premises” client server
and target database server for initial configuration of the Column Master Key and Column Encryption Key, thereafter
Column Encryption is performed entirely via the Client Application server.
each client server wanting access to the contents of data encrypted with a given Column Encryption key
protected by a specific Column Master Key that the server must have access to an HSM in the same
Security World
and have a copy of the Column Master Key Application key token stored on its local drive in “C:\Program
Data\nCipher\Key Management Data\local” (%NFAST_LOCAL%).
By default “C:\Program Data” is a hidden folder. To view this folder open an explorer window go to the “View” tab
and tick the check box named “Hidden items”.
Configuring a Thales nShield HSM, see the Installation Guide for your HSM available on the DVD supplied
with the HSM.
Security World Configuration, see the appropriate User Guide for your HSM.
Database Permissions
There are four permissions required for Always Encrypted:
Operation Description
ALTER ANY COLUMN MASTER KEY Required to create and delete a column master key
ALTER ANY COLUMN ENCRYPTION KEY Required to create and delete a column encryption key
Required to access and read the metadata of the column
VIEW ANY COLUMN MASTER KEY
master keys to manage keys or query encrypted columns
Required to access and read the metadata of the column
VIEW ANY COLUMN ENCRYPTION KEY
encryption key to manage keys or query encrypted columns
The Thales Security World Software must be installed onto the “on-premises” client application server(s) utilizing the
SQL Server 2016 Always Encrypted feature.
Note: If you are running TDE with nShield HSMs the same Security World can be used or if preferred an entirely
different Security World can be implemented. If you prefer to use a different Security World you will need
further HSMs as the nShield HSM can only host a single Security World instance at any one time.
Double click the CNG configuration wizard. (If the User Access Control prompt pops up click “YES” to continue.)
If you already have a Security World that you intend to use for Always Encrypted the next screen will allow you to
select to “Use the existing security world”. If you do not currently have a Security World or would like to create a
new Security World then check the “Create a new Security World” radio button and click “Next” (for the purposes of
this integration guide we have chosen to use an existing Security World).
Note: If you are creating a new Security World please refer to the Thales nShield documentation for details on
creating and configuring a new Security World.
Ensure that the Set Module States show the available modules as:
Mode = initialisation
State = (pre)- initialisation
Click “Next”.
Leave the “Enable this module as a remote target.” un-checked (Figure 6). (Please be aware that this is not to be
confused with the nShield Remote Administration* utility).
Click “Next”. If you are using an existing Security World you must have the “World” file in the
%NFAST_KMDATA%\local folder. Be prepared to present the quorum of Administrator cards.
When the ACS quorum has been presented, and the Security World loaded / created, return the HSM to
“Operational” mode and choose the appropriate protection method for the Column Master Key. (Figure 8: Set Key
Protection)
* For details on Remote Administration setup and configuration please refer to the nShield Documentation on the DVD
that came with your Thales HSM.
Proceed to create an Operator Card Set by selecting “Operator Card Set protection” and enter a name for your card
set; ensure that the “Always use the wizard when creating or importing keys” is de-selected. Enter the card set
name (this field is mandatory) then enter the required K of N value; (consult your security policy document for details
on correct values to enter here). Carefully consider which of the optional values to set for the Operator Card Set.
Please refer to the description in the table below for further details. Please note that by default the OCS is created
as non-persistent.
Term Definition
Card set name Card set name must be supplied, unlike naming of individual cards which is
optional.
Number of cards required This relates to K of N where the value [K] = the necessary number of cards
required to complete authentication (the quorum) and [N] = the total number of
cards available. The value for K should be less than N. We do not recommend
creating card sets in which K is equal to N because an error on one card
would render the whole card set unusable.
Card set has a time-out This allows a specified period of time, in seconds, where keys protected by
any given OCS remain loaded in the HSM for use by your application. Once
the time period has expired, all keys loaded under the OCS will be forcibly
removed from the HSM such that they are no longer available. Time-outs
operate independently of OCS persistence
Persistent Keys protected by a persistent card set can be used for as long as the
application that loaded the OCS remains connected to the hardware security
device (unless that application removes the keys). A key protected with a
persistent OCS card does not need the card to be present in the slot once the
key is loaded.
Non-persistent Keys protected by a non-persistent card set can only be used while the last
required card of the quorum remains loaded in the smart card reader of the
Thales hardware security device. The keys protected by this card are removed
from the memory of the device as soon as the card is removed from the smart
card reader.
Usable remotely The Remote Operator feature enables the contents of a smart card inserted
into the slot of one module (the attended module, such as a client module) to
be securely transmitted and loaded onto another module (an unattended
module, such as the nShield Connect). This is useful when you need to load
an OCS-protected key onto a machine to which you do not have physical
access (because, for example, it is in a secure area). This feature is
deprecated in favour of Remote Administration which was launched with
version 12.00 of the Thales nShield Security World software.
Recoverable PP The option allows the recovery of a lost or forgotten pass phrase. For further
details on recovery operations and Security World settings please refer to the
HSM documentation supplied on the Security World media disk.
Note: You must have the N value of cards (where N is the total number of cards in the Set) available for this
operation before you commence. Insert a card into the attached HSM card reader or the TVD (Trusted
Verification Device) if you are using the Remote Administration feature, when you are prompted to do so.
You do not have to give individual cards names, but if you wish, enter the name of the card in the appropriate field.
Similarly, you do not have to give the cards a pass phase, but enter one if appropriate for your security policy. Click
“Next”.
Note: If you are using Remote Administration you may need to select the <Back button and then return via Next >
to the ESN confirmation screen. Re-select “OK” on the TVD to continue creating the OCS.
>cnglist.exe --list-providers
PS C:\WINDOWS\system32>
You should see the “nCipher Security World key Storage Provider” listed (Circled in red, above). You will find the
provider in the registry at this location:
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Cryptography\Providers\nCipherSecurityWorldKeyStorageProvider
From the “Apps by name” desktop environment, select the Microsoft SQL Server Management Studio and connect
to the desired database. Once connected to the database the first thing you will need to do is create a Column
Master Key. This key will encrypt all subsequent Column Encryption keys (CEKs).
Using Object Explorer, select the Security directory under the desired Database (In the example below this can be
seen as “TestDatabase”). Click to expand “Always Encrypted Keys”.
Select: <Your_database> > Security > Always Encrypted Keys > Column Master Keys. Right click on “Column
Master Keys” and select > New Column Master Key… the “New Column Master Key” dialogue box will open (Figure
13).
The “nCipher Key Storage Provider – Create key” dialogue will open.
The following screen will prompt you to select which Operator Card Set to use for the CMK. (If you have multiple
OCS all currently available Operator Card sets will be listed) Operator Card sets will be listed in the left hand field
(Figure 16).
Select the OCS that you want to use and click “Finish”. The next two screens will prompt you to enter the pass-
phrase for the selected OCS, if one exists, and confirm that card reading completed successfully. (Ensure that you
have the correct OCS available)
You will now have a Column Master Key called MyCMK protected by the card set, AESQL. The newly generated
CMK will be visible in the Name field.
nfkminfo.exe -k
For further information about the key, including its name, and protection (i.e.. Module or Operator Card Set) run
>nfkminfo with the <AppName> and <Ident> as reported by >nfkminfo –k, above.
Example Key information:
Click OK, the database now has a Column Master Key protected by the Security World under OCS protection.
To view the new Column Master Key use the SQL Object Explorer. Navigate to the relevant database and expand
by clicking the + sign. Expand the “Security” folder and then expand the “Always Encrypted Keys” Folder. You will
find two folders, one for the Column Master Key(s) and one for the Column Encryption Key(s).
If you don’t want the Introduction screen presented each time you run the wizard, check the “Do not show this page
again” box. Click “Next”.
The Column Selection screen allows you to choose the type of Column Encryption Key and specify the columns you
want to encrypt.
Note: The “Apply one key to all checked columns” is shaded out until such time as you have two or more CEKs
available. You will then also have the option to select the CEK for any given column via the drop down list
beneath the “Encryption Key” option.
The process of encrypting your database records can take a considerable amount of time, depending on the size /
quantity of data. To mitigate the possibility of data corruption occurring as records are encrypted whilst being
updated, it is advisable to back up the database and to only perform this activity when the database is off-line.
In this case we will continue and run the encryption straight away. Select the radio button, “Proceed to finish now”
this will begin the process of creating the CEK and using it to encrypt the specified column in the database. Click
“Next” to view the Summary page.
This page allows you to verify your configuration choices and amend if necessary.
The next operation requires the Operator Card Set quorum to be available.
Before you can create a CEK you must first load the CMK. The following screen (Figure 26) will prompt you to
present the OCS protecting the Column Master Key. Present the OCS quorum and enter the passphrase, continue
by clicking “Finish”.
Click “Finish” to complete the loading of the CMK into the memory of the HSM this will allow it to securely encrypt
the Column Encryption Key.
Insert the quorum from the Operator Card set and enter the passphrase(s) when prompted.
Providing the Operator Card(s) where correctly read the CEK will have been created.
The Results page will report that the “CEK was generated and the requested / specified columns are now encrypted.
You can now click “Close” to exit the Always Encrypted Column Encryption Key wizard.
To show the encrypted columns in plaintext (i.e. Decrypted) you should disconnect from the database and reconnect
with the given additional connection parameter. This is entered from the “Connect to Database Engine” logon
screen. Select the required server name and click on “Options>>” Go to “additional Connection Parameters” and add
the connection string “Column Encryption Setting = enabled” (without parenthesis “”) and then click “Connect”.
When you now run the query on the table you will now see the original values decrypted by the Column Encryption
Key.
Right click on the required database and in the “Tasks” menu select “Encrypt Columns”.
From the Introduction screen, select “Next” to get to the Column Selection page. Click on the field “Encryption Type”
to enter your preferred option for this value.
From the drop down list select “Plaintext” then click “Next”.
The following page will provide a review summary for the requested operations.
Check to ensure that the correct Decrypt column(s) are listed and click “Finish”. The “Performing encryption
operations” should show as “Passed”.
You have successfully removed Always Encrypted column encryption from you database. When you next log into
the database you can remove the Column Encryption Setting = enabled string from the “Additional Connection
Parameters” field of the database login screen. When you now view your database table via, “Select Top 1000
Rows” you should see all columns in plaintext (i.e. an unencrypted state).
Decide on the level of security required around the running of scripts and change the ExecutionPolicy setting
accordingly. Before updating PowerShellGet or PackageManagement, install the latest Nuget provider.
Then download and install the SqlServer module to configure Always Encrypted using Power Shell.
Note: The “-AllowClobber” parameter allows you to import the specified commands if it exists in the current session.
(Once installed, If you are using PowerShell ISE refresh the Commands pane if you are using PowerShell open a
new session. Confirm the install by running:
Note: if using an existing Security World ensure that the “Use existing Security World” check box is ticked.
Note: Always ensure that you check and confirm all values are adjusted according to your environment; the values
in this integration guide are example values only The bespoke values have been highlighted throughout in
orange.
Generate a CNG RSA key pair for use as a Column Master Key:
The above example will generate a 2048 bit RSA key pair with Name AECMK (highlighted orange). The resulting
key is encrypted whilst in the HSM and then pushed to the requesting On-Premise Client server where it is stored as
an Application Key Token in the %NFAST_KMDATA%\local folder (C:\ProgramData\nCipher\Key Management
Data\local).
## Specify the Column Master Key settings for importing into the database:
$CmkSettings = New-SqlCngColumnMasterKeySettings -CngProviderName "nCipher Security World Key
Storage Provider" -KeyName "AECMK"
The resulting Column Encryption Key (CEK) is a 256 bit symmetric key protected by the Column Master Key (CMK)
this is achieved by calling the EncryptColumnEncryptionKey method of the SqlColumnEncryptionCNGProvider
provider class.
Deterministic
Randomized
Plaintext (only available to revert encrypted columns to an unencrypted state)
$encryptionChanges = @()
N.B. If the database is live at this point, you should first take it off-line before proceeding to remove the column
encryption.
Import-Module SqlServer
# Set up connection and database SMO objects
$encryptionChanges = @()
The Always Encrypted encrypted data will revert to plaintext. (If your database is protected by TDE then the data is
still being encrypted whilst at rest). When you next log into the database you can remove the Column Encryption
Setting = enabled string from the “Additional Connection Parameters” field of the database login screen. When you
now view your database table via, “Select Top 1000 Rows” you should see all columns in plaintext (i.e. an
unencrypted state).
Note: When removing Always Encryption from your database columns, ensure that all columns appear in plaintext.
You must delete any Column Encryption Keys (CEK) before you can drop the Column Master Key(s) (CMK)
When initiating a connection to the database, select “Options” -> Select “Additional Connection Parameters” and
enter: Column Encryption Setting = enabled in the provided field.
Note: Always ensure that you check and confirm all values are adjusted according to your environment; the values
in this integration guide are example values only. The bespoke values have been highlighted throughout in
orange.
Then download and install the SqlServer module to configure Always Encrypted using Power Shell.
Note: The “-AllowClobber” parameter allows you to import the specified commands if it exists in the current session.
Once installed, If you are using PowerShell ISE refresh the Commands pane if you are using PowerShell open a
new session. Confirm the install by running:
cnglist.exe –list-providers
Creating the Always Encrypted Column Master Key using the nCipher KSP
Once you have successfully installed the nCipher CNG Key Storage Provider and registered for use with Module
protection you can begin to configure Always Encrypted.
The Security Officer must have administrator rights on the Client Server being configured to use Always Encrypted.
The following sections are divided between the Security Administrator and the Database Administrator. The DBA
should not have access to the Client server.
Generate a CNG RSA key pair for use as a Column Master Key: Choose a suitable name for the Column Master
Key ($cngKeyName highlighted orange in the example, below) for your particular deployment.
The above will generate a 2048 bit RSA key pair. The resulting key is encrypted whilst in the HSM and then pushed
to the requesting On-Premise Client server, where it is stored as an Application Key Token in the
%NFAST_KMDATA%\local folder (C:\ProgramData\nCipher\Key Management Data\local).
nfkminfo.exe -k
Key list - 1 keys
AppName caping Ident s-1-5-21-1277476411-3880915791-1682396242-1002--
7cbbd9d5477b6d2ed4b6df83e3fa50ac3745b855
The Security Administrator can now create the SqlCngColumnMasterKeySettings object which will reference the
CMK application key token created using the nCipher KSP
Finally invoke the New-SqlColumnEncryptionKeyEncryptedValue cmdlet. This will produce the encrypted value of
the CMK which will need to be passed to the Database Administrator and is required for generating a Column
Encryption Key.
0x01620000016E00630069007000680065007200200073006500630075007200690074007900200077006F0072006C0
0640020006B00650079002000730074006F0072006100670065002000700072006F00760069006400650072002F0061
00650063006D006B00989CD11831C74395E380A8F16A251014D03E98390BD8AF12252062B653AE80A00C7CFA1FC284
914A38CBA659796C55CF59B131BDC1BF8A862B032F14303B2E6EF1BAC2441EEB4B10CBF9DBB6523E34E64F7EE0B3F6A
538228EF1964C5E2A1E4ED8056700AA2B25BFB9834679C99A84D7B275F0945429E97639558F79EB3EB363497857BBB6
21C2D8FC822464FCF1F2178A24087F2C5E9ACBECF02DB495D4357C8ED886F1CC8A3070BF0FA9F430389D738AA2CEE1
7880EE7210CA18437FA3FC6333075A33F286239CDA869B4C017F3ADA3D67F7ECB6ECEF58F6815648665DD36A67FC4CA
B4D3D699130D33852B642A6E6ED2B2FE1692010943E3252530727306035822BDC2CADBDAC13F48748686BADAD72086A
505BA57D482C904A3BE39EE603B7CC26398D5CE589AB6597EA5D577C28C34BC6AA96F17B69F1F053BC148B763C27CD
EE1307C9A46F12CFBC8CF309A417364BFA78C5D9A8FEFC877FCDCC4ACC06AF750436A962C757BAC3ADE9CB3C0DAEE78
002ACE2F36DCF9C1BAE69B39053875910CE4F77E51450C844CC32DF062309CC9B2F29F92A8DDAD883B632C8D3F36620
318175B93763DEF696D5EB9DC37F51655E181207165A896AEFA60271B705B2883BD96BEC6487454A65EFE9D7F48AF5
4FFE1B7FEB0598AC47CA306B23201B8B9B89DFAA574688CA18F5B16FFEAFAF9FA87072CE0E853675E2369CE3ED912F6
7
KeyStoreProviderName KeyPath
-------------------- -------
MSSQL_CNG_STORE nCipher Security World Key Storage Provider/AECMK
Name
----
AECMK
You can confirm the presence of the newly imported CMK using Object Explorer in SSMS.
The New-SqlColumnEncryptionKey cmdlet creates a column encryption key object in the form of an “Encrypted
Value” in the database. A Column Encryption Key object encapsulates an encrypted value of a symmetric
cryptographic key, i.e. the Column Encryption Key; this can be subsequently used to encrypt database columns
using the Always Encrypted feature. When attempting to copy the encrypted value, ensure that the entire value is
written to a single line, appended with the path to the database.
Name
----
AECEK1
The screenshot above shows the output confirming the creation of a new CEK, called “AECEK1” this was generated
and then encrypted using the encapsulated CMK metadata provided to the DBA by the Security Administrator. (It is
advisable to use a word editor to align the encrypted value to a single line before copying to the PowerShell CLI)
Note: When adding the –Path you should append this after the encrypted value:
You can confirm the presence of the CEK using SSMS Object Explorer to view the new Always Encrypted key.
This concludes role separation activities performed by the Data Base Administrator. The Security Administrator can
now connect to the Database and use the defined keys to encrypt the desired columns using Always Encrypted.
These permissions can be found by right clicking on the relevant database and selecting “Properties” then select
“permissions” (The example below has been edited for convenience of illustration).
To encrypt a column using the pre-defined Column Encryption Key (given in the example as AECEK1) use one of
the following for –EncryptionType value.
Deterministic
Randomized
Plaintext (only available to revert encrypted columns to an unencrypted state)
Run the below with the relevant server name and database inserted appropriately and your chosen table / column
intended for encryption, specify also the Column Encryption Key to use.
$encryptionChanges = @()
Note: As the Column Master Key Application Key Token only exists in the NFAST-KMDATA%\local directory of a
participating Always Encrypted client server, the facility to decrypt a column is dependent on the presence of
both the CMK Application Key Token in the NFAST-KMDATA%\local directory and the availability of a
correctly configured HSM, thereby rendering all encrypted data available only to those computers configured
with and with access too, these requisite resources.
N.B. If the database is live at this point, you should first take it off-line before proceeding to remove the column
encryption.
$encryptionChanges = @()
The Always Encrypted encrypted data will revert to plaintext. (If your database is protected by TDE then the data is
still being encrypted whilst at rest). When you next log into the database you can remove the Column Encryption
Setting = enabled value from the “Additional Connection Parameters” field of the database login screen. When you
now view your database table via, “Select Top 1000 Rows” you should see all columns in plaintext (i.e. an
unencrypted state).
When initiating a connection to the database, select “Options” -> Select “Additional Connection Parameters” and
enter: Column Encryption Setting = enabled in the provided field.
Alternatively, the full list, along with any additions to the SqlServer module released after this guides publication, can
be found via the link below:
https://blogs.technet.microsoft.com/dataplatforminsider/2016/06/30/sql-powershell-july-2016-update/
https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/always-encrypted-database-engine
For further information on Always Encrypted operational capabilities please consult the website via the above URL.
Queries can perform equality comparison on columns encrypted using deterministic encryption, but no other
operations (e.g. greater/less than, pattern matching using the LIKE operator, or arithmetical operations).
Queries on columns encrypted by using randomized encryption cannot perform operations on any of those
columns. Indexing columns encrypted using randomized encryption is not supported.
A column encryption key can have up to two different encrypted values, each encrypted with a different
column master key. This facilitates column master key rotation.
Deterministic encryption requires a column to have one of the binary2 collations.
After changing the definition of an encrypted object, execute sp_refresh_parameter_encryption to update
the Always Encrypted metadata for the object.
Always Encrypted is not supported for the columns with the below characteristics (e.g. the Encrypted WITH clause
cannot be used in CREATE TABLE/ALTER TABLE for a column, if any of the following conditions apply to the
column):
Columns using one of the following datatypes: xml, timestamp/rowversion, image, ntext, text, sql_variant,
hierarchyid, geography, geometry, alias, user defined-types.
FILESTREAM columns
Columns with the IDENTITY property
Columns with ROWGUIDCOL property
String (varchar, char, etc.) columns with non-bin2 collations
Columns that are keys for nonclustered indices using a randomized encrypted column as a key column
(deterministic encrypted columns are fine)
Columns that are keys for clustered indices using a randomized encrypted column as a key column
(deterministic encrypted columns are fine)
Columns that are keys for fulltext indices containing encrypted columns both randomized and deterministic
Columns referenced by computed columns (when the expression does unsupported operations for Always
Encrypted)
Sparse column set
Columns that are referenced by statistics
Columns using alias type
Partitioning columns
Columns with default constraints
Columns referenced by unique constraints when using randomized encryption (deterministic encryption is
supported)
Primary key columns when using randomized encryption (deterministic encryption is supported)
Referencing columns in foreign key constraints when using randomized encryption or when using
deterministic encryption, if the referenced and referencing columns use different keys or algorithms
Columns referenced by check constraints
Columns in tables that use change data capture
Primary key columns on tables that have change tracking
Columns that are masked (using Dynamic Data Masking)
Columns in Stretch Database tables. (Tables with columns encrypted with Always Encrypted can be
enabled for Stretch.)
Columns in external (PolyBase) tables (note: using external tables and tables with encrypted columns in the
same query is supported)
Table-valued parameters targeting encrypted columns are not supported.
FOR XML
FOR JSON PATH
END OF DOCUMENT