From 95009f48e05d06fb77a9127731d7f5f2de489055 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sat, 13 Dec 2014 13:38:50 -0500 Subject: Add code to use Cryptech TRNG in place of Cryptlib's default entropy sources. This may need further attention, particularly once we start working with buses other than the current I2C kludge. --- tests/test_trng.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/test_trng.py (limited to 'tests') diff --git a/tests/test_trng.py b/tests/test_trng.py new file mode 100644 index 0000000..5207eda --- /dev/null +++ b/tests/test_trng.py @@ -0,0 +1,48 @@ +# Trivial test of cryptech trng and noise cores via cryptlib python interface. +# Might upgrade to Python's unittest framework eventually. + +import atexit +import os + +print "[Loading cryptlib]" +from cryptlib_py import * + +if os.getenv("SET_GDB_BREAKPOINTS"): + print "[Sending SIGINT to self to throw to gdb]" + os.kill(os.getpid(), 2) + print "[Continuing]" + +print "[Initializing cryptlib]" +cryptInit() +atexit.register(cryptEnd) + +print "[Opening device]" +dev = cryptDeviceOpen(CRYPT_UNUSED, CRYPT_DEVICE_HARDWARE, None) +atexit.register(cryptDeviceClose, dev) + +use_dev_context = False + +def generate_key(i): + label = "RSA-%04d" % i + print "[Generating key %s]" % label + ctx = None + try: + if use_dev_context: + ctx = cryptDeviceCreateContext(dev, CRYPT_ALGO_RSA) + else: + ctx = cryptCreateContext(CRYPT_UNUSED, CRYPT_ALGO_RSA) + ctx.CTXINFO_LABEL = label + ctx.CTXINFO_KEYSIZE = 2048 / 8 + cryptGenerateKey(ctx) + finally: + if ctx is not None: + cryptDestroyContext(ctx) + +have_i2c = os.path.exists("/dev/i2c-2") + +if not have_i2c: + print + print "[I2C device not found, so testing software only, no hardware cores tested]" + +for i in xrange(100): + generate_key(i) -- cgit v1.2.3