Skip to content

Always Encrypted | Align reads of CekMdVersion and EkValueCount with TDS specification#4240

Open
edwardneal wants to merge 3 commits intodotnet:mainfrom
edwardneal:perf/read-ulong-keymdversion
Open

Always Encrypted | Align reads of CekMdVersion and EkValueCount with TDS specification#4240
edwardneal wants to merge 3 commits intodotnet:mainfrom
edwardneal:perf/read-ulong-keymdversion

Conversation

@edwardneal
Copy link
Copy Markdown
Contributor

Description

SqlClient contains a the structure of a table of CIPHER_INFO entries. The table is represented as a SqlTceCipherInfoTable and each entry is represented as a SqlTceCipherInfoEntry instance. This corresponds to an EK_INFO structure in the TDS specification. A table of EK_INFO entries appears in the COLMETADATA structure.

In the TDS specification, the EK_INFO structure's CekMdVersion field is defined as a ULONGLONG and the COLMETADATA structure's EkValueCount field is defined as a USHORT.

This PR aligns SqlClient with these type definitions. The cekMdVersion field on SqlTceCipherInfoTable is currently defined as an eight byte array, and is redefined as a ulong. This eliminates one allocation, so it technically improves performance (marginally.) The tableSize is currently defined a short, and is redefined as a ushort. This is a correctness improvement and a bugfix in an edge case where the same column is encrypted with an unreasonably large number of encryption keys.

One slight edge case lies in SqlCommand.Encryption.cs, where we read the CekMdVersion from a byte array as a little-endian ulong. This byte array is the result of reading the column_encryption_key_metadata_version column from the first result set of sp_describe_parameter_encryption, and this column is declared as binary(8). I've used BinaryPrimitives.ReadUInt64LittleEndian to bridge this gap between the stored procedure's result set and the TDS specification.

Issues

None.

Testing

Automated Always Encrypted tests continue to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

1 participant