Explanation on how HDCP keys work

HDCP works with a master key in a matrix. Imagine a table that is 40 numbers wide and 40 numbers tall. Each number is 56 bit long. totalling 1600 values with a total of 11200 bytes for the matrix.

Creating a key

The difference between a source and sink key is the matrix. The source key uses the matrix as-is. The sink key uses a transposed version of the matrix. Transposing in this case is done by simply flipping the x and y coordinates.

Possible number of keys

The KSV is a 40 bit number with exactly 20 bits set. In mathematics, this is known as a combination (pick n out of m without caring about the order of items). This specific rule allows for a total of 137'846'528'820 different keys to ever exist.
The formula for this is n!/(k!*((n-k)!)). In our case that's 40!/(20!*20!), which is 40!/20!2. A single key is 40*56/8=280 bytes long. Each key exists twice, once as source key and once as sink key, resulting in 280*2=560 bytes. Multiplying with the number of keys gives us 560*137'846'528'820=77'194'056'139'200 bytes of possible key data.

Other implementations

In this chapter you can find other implementations. They should work but consider them all untested.
"Runnable" means that the code includes the bits to use it as-is. If it's not marked as runnable, it's only the key generator and you have to code something to use its functions yourself. "Includes Key" means that the key is embedded in the source code and doesn't needs to be loaded in at runtime.

Unless otherwise specified, they are licensed under the MIT

CAUTION! These scripts have been developed on a machine with an intel CPU. If you find that your device generates invalid keys (especially in the C version), try swapping the bits of the KSV before you go crazy. The reason you should try this is this.

Language Runnable Includes Key Notes
JavaScript (BigInt) × Requires native BigInt support which is not yet standardized. Works in Firefox and Chrome, as well as nodeJS 12.14.1 (LTS) and newer.
JavaScript (Compatible) × The version run by this generator. It's a compatible version that depends on a 3rd party bigint library but has the advantage of working in older browsers. It has been tested down to Internet Explorer 11.
C99 Only uses standard C libraries. Requires 64 bit integer support. Probably the most suitable for hardware. Tested in GCC (tdm64-1) 4.9.2 with -Wall -Wextra -Werror -pedantic
C# Very clear, fully commented. Should run in .NET 3.5 and newer. Doesn't uses any OS specific features and thus should also work in .NET core.
PHP Requires PHP 7
Python Ported from Python 2 to version 3. I don't know python, I just copied the error into a search engine and used whatever solution popped up first that worked.
<None> × Not actual source code, but the key itself in binary format.
More Services