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:
Rich Wareham
2010-09-17 13:25:00 +01:00
parent 83cf4e347e
commit 27f805e237
2 changed files with 46 additions and 0 deletions

4
README
View File

@@ -12,6 +12,7 @@ Options:
-k, --sink generate a sink key rather than a source key
--ksv=KSV use a specific KSV expressed in hexadecimal
-j, --json output key and KSV as JSON
-t, --test generate source and sink keys and test they work
Examples:
@@ -22,4 +23,7 @@ Examples:
# human-readable form
./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

View File

@@ -50,12 +50,21 @@ def main():
parser.add_option('-j', '--json', action='store_true',
dest='output_json', default=False,
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()
# read the 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
if options.ksv is not None:
ksv = int(options.ksv, 16)
@@ -74,6 +83,39 @@ def main():
else:
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):
"""Read a HDCP master key from a key file.