add a simple self-test
Added a simple self test mode where a source and sink key are generated and the source/sink shared-secret key is generated using the source keys and the sink keys. The test makes sure those keys are identical.
This commit is contained in:
4
README
4
README
@@ -12,6 +12,7 @@ Options:
|
|||||||
-k, --sink generate a sink key rather than a source key
|
-k, --sink generate a sink key rather than a source key
|
||||||
--ksv=KSV use a specific KSV expressed in hexadecimal
|
--ksv=KSV use a specific KSV expressed in hexadecimal
|
||||||
-j, --json output key and KSV as JSON
|
-j, --json output key and KSV as JSON
|
||||||
|
-t, --test generate source and sink keys and test they work
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
@@ -22,4 +23,7 @@ Examples:
|
|||||||
# human-readable form
|
# human-readable form
|
||||||
./generate_key.py
|
./generate_key.py
|
||||||
|
|
||||||
|
# Run a self-test to make sure the source a sink key generation is consistent
|
||||||
|
./generate_key.py -t
|
||||||
|
|
||||||
% vim:spelllang=en_gb:spell
|
% vim:spelllang=en_gb:spell
|
||||||
|
|||||||
@@ -51,11 +51,20 @@ def main():
|
|||||||
dest='output_json', default=False,
|
dest='output_json', default=False,
|
||||||
help='output key and KSV as JSON')
|
help='output key and KSV as JSON')
|
||||||
|
|
||||||
|
parser.add_option('-t', '--test', action='store_true',
|
||||||
|
dest='do_test', default=False,
|
||||||
|
help='generate source and sink keys and test they work')
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
# read the master key file
|
# read the master key file
|
||||||
key_matrix = read_key_file(open(options.master_key_file))
|
key_matrix = read_key_file(open(options.master_key_file))
|
||||||
|
|
||||||
|
# if asked to do a test, do one and exit
|
||||||
|
if options.do_test:
|
||||||
|
do_test(key_matrix)
|
||||||
|
return
|
||||||
|
|
||||||
# generate a ksv if necessary
|
# generate a ksv if necessary
|
||||||
if options.ksv is not None:
|
if options.ksv is not None:
|
||||||
ksv = int(options.ksv, 16)
|
ksv = int(options.ksv, 16)
|
||||||
@@ -74,6 +83,39 @@ def main():
|
|||||||
else:
|
else:
|
||||||
output_human_readable(ksv, key, options.gen_sink)
|
output_human_readable(ksv, key, options.gen_sink)
|
||||||
|
|
||||||
|
def do_test(key_matrix):
|
||||||
|
"""Perform a self-test.
|
||||||
|
|
||||||
|
Generate both a source key and sink key with random (different) KSVs
|
||||||
|
and test that the HDCP shared-key system works."""
|
||||||
|
|
||||||
|
print('Performing self test.')
|
||||||
|
|
||||||
|
# generate source key
|
||||||
|
src_ksv = gen_ksv()
|
||||||
|
src_key = gen_source_key(src_ksv, key_matrix)
|
||||||
|
output_human_readable(src_ksv, src_key, False)
|
||||||
|
|
||||||
|
# generate sink key
|
||||||
|
snk_ksv = gen_ksv()
|
||||||
|
snk_key = gen_sink_key(snk_ksv, key_matrix)
|
||||||
|
output_human_readable(snk_ksv, snk_key, True)
|
||||||
|
|
||||||
|
# add sink keys together according to src ksv
|
||||||
|
key1 = reduce(lambda x, y: (x+y) & 0xffffffffffffff,
|
||||||
|
map(lambda x: x[1], filter(lambda x: src_ksv & (1<<x[0]), zip(range(40), snk_key))))
|
||||||
|
|
||||||
|
# add source keys together according to sink ksv
|
||||||
|
key2 = reduce(lambda x, y: (x+y) & 0xffffffffffffff,
|
||||||
|
map(lambda x: x[1], filter(lambda x: snk_ksv & (1<<x[0]), zip(range(40), src_key))))
|
||||||
|
|
||||||
|
print('\nGenerated keys: sink = %014x, source = %014x' % (key1, key2))
|
||||||
|
|
||||||
|
if key1 == key2:
|
||||||
|
print('Test PASSED')
|
||||||
|
else:
|
||||||
|
print('Test FAILED')
|
||||||
|
|
||||||
def read_key_file(filelike):
|
def read_key_file(filelike):
|
||||||
"""Read a HDCP master key from a key file.
|
"""Read a HDCP master key from a key file.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user