From 1cd42f6d3332e1edf78b06bd7dcf51f5a1a7bb23 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Mon, 25 May 2020 19:33:38 -0400 Subject: Untested conversion to support Python 3 --- tests/test-rsa.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'tests/test-rsa.py') diff --git a/tests/test-rsa.py b/tests/test-rsa.py index 6b52eb9..57c554d 100644 --- a/tests/test-rsa.py +++ b/tests/test-rsa.py @@ -35,6 +35,7 @@ Use PyCrypto to generate test data for Cryptech ModExp core. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from argparse import ArgumentParser, FileType +from binascii import hexlify from Crypto import __version__ as PyCryptoVersion from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 @@ -71,32 +72,32 @@ wrapper = TextWrapper(width = 78, initial_indent = " " * 2, subsequent_indent = def printlines(*lines, **kwargs): for line in lines: - args.output.write(line % kwargs + "\n") + args.output.write(line.format(**kwargs) + "\n") def trailing_comma(item, sequence): return "" if item == sequence[-1] else "," def print_hex(name, value, comment): - printlines("static const uint8_t %(name)s[] = { /* %(comment)s, %(length)d bytes */", - wrapper.fill(", ".join("0x%02x" % ord(v) for v in value)), - "};", "", + printlines("static const uint8_t {name}[] = {{ /* {comment}, {length:d} bytes */", + wrapper.fill(", ".join("0x" + hexlify(v) for v in value)), + "}};", "", name = name, comment = comment, length = len(value)) def pad_to_blocksize(value, blocksize): extra = len(value) % blocksize - return value if extra == 0 else ("\x00" * (blocksize - extra)) + value + return value if extra == 0 else (b"\x00" * (blocksize - extra)) + value # Funnily enough, PyCrypto and Cryptlib use exactly the same names for # RSA key components, see Cryptlib documentation pages 186-187 & 339. -h = SHA256.new(plaintext) +h = SHA256.new(plaintext.encode("ascii")) printlines("/*", " * RSA signature test data for Cryptech project, automatically generated by", - " * %(scriptname)s using PyCrypto version %(version)s. Do not edit.", + " * {scriptname} using PyCrypto version {version}. Do not edit.", " *", - " * Plaintext: \"%(plaintext)s\"", - " * SHA-256: %(digest)s", + " * Plaintext: \"{plaintext}\"", + " * SHA-256: {digest}", " */", "", scriptname = scriptname, version = PyCryptoVersion, @@ -122,7 +123,7 @@ for k_len in args.key_lengths: else: blocksize = 4 - printlines("/* %(k_len)d-bit RSA private key (PKCS #%(pkcs)d)", + printlines("/* {k_len:d}-bit RSA private key (PKCS #{pkcs:d})", k.exportKey(format = "PEM", pkcs = args.pkcs_encoding), "*/", "", k_len = k_len, pkcs = args.pkcs_encoding) @@ -143,22 +144,22 @@ for k_len in args.key_lengths: else: value = getattr(k, name) - print_hex("%s_%d" % (name, k_len), + print_hex("{}_{:d}".format(name, k_len), long_to_bytes(value, blocksize = blocksize), - "key component %s" % name) + "key component {}".format(name)) - print_hex("m_%d" % k_len, pad_to_blocksize(m, blocksize), "message to be signed") - print_hex("s_%d" % k_len, pad_to_blocksize(s, blocksize), "signed message") + print_hex("m_{:d}".format(k_len), pad_to_blocksize(m, blocksize), "message to be signed") + print_hex("s_{:d}".format(k_len), pad_to_blocksize(s, blocksize), "signed message") -printlines("typedef struct { const uint8_t *val; size_t len; } rsa_tc_bn_t;", - "typedef struct { size_t size; rsa_tc_bn_t %(fields)s; } rsa_tc_t;", +printlines("typedef struct {{ const uint8_t *val; size_t len; }} rsa_tc_bn_t;", + "typedef struct {{ size_t size; rsa_tc_bn_t {fields}; }} rsa_tc_t;", "", - "static const rsa_tc_t rsa_tc[] = {", + "static const rsa_tc_t rsa_tc[] = {{", fields = ", ".join(fields)) for k_len in args.key_lengths: - printlines(" { %(k_len)d,", k_len = k_len) + printlines(" {{ {k_len:d},", k_len = k_len) for field in fields: - printlines(" { %(field)s_%(k_len)d, sizeof(%(field)s_%(k_len)d) }%(comma)s", + printlines(" {{ {field}_{k_len:d}, sizeof({field}_{k_len:d}) }}{comma}", field = field, k_len = k_len, comma = trailing_comma(field, fields)) - printlines(" }%(comma)s", comma = trailing_comma(k_len, args.key_lengths)) -printlines("};") + printlines(" }}{comma}", comma = trailing_comma(k_len, args.key_lengths)) +printlines("}};") -- cgit v1.2.3