From 2a246c59d7e5efcc8e2612f4c5d045f3162172e0 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Mon, 13 Jun 2016 15:43:45 -0400 Subject: Add test for loading an externally generated keypair via C_CreateObject(). --- unit_tests.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/unit_tests.py b/unit_tests.py index ef7b980..10b77ea 100644 --- a/unit_tests.py +++ b/unit_tests.py @@ -432,6 +432,81 @@ class TestKeys(unittest.TestCase): p11.C_VerifyInit(self.session, CKM_RSA_PKCS, public_key) p11.C_Verify(self.session, tralala, sig) + def test_load_sign_verify_rsa_3416(self): + from Crypto.Util.number import long_to_bytes, inverse + from Crypto.PublicKey import RSA + from textwrap import dedent + k = RSA.importKey(dedent('''\ + -----BEGIN PRIVATE KEY----- + MIIHvgIBADANBgkqhkiG9w0BAQEFAASCB6gwggekAgEAAoIBrADgnFnBx2/mTFGM + pEQ891GLfN8lnCJsMTN3zkQKCAV1iQpXXoOzq+mFDudpYsBZRq15AZxPc6ZejD5Q + P8PTIPdNWquC7u5mUsxLc12iVvXn3OBvxQyf/U+8S3Y2OsuVr9oTAU/PS4lO/bct + GgTmGnuRgWSgKl+tqsmABqEDOvEGIK7MHiwL7XbFgxPTV9nhP6Qaox0/eBD1Cq0K + pQ2DmwVCMglQl2s2kmmqT9HV/iZD/WuvxpnRYpGLtUQLgVbCO50spH/PSrsnaiIk + DMzPreaRjNVhKz2cVAJysCdGygY0vUtZILlA9gngL/arQYV2eSwTyvpzZwiJOcVV + d2A8Beebo0bWG2pnBnWNBlp20s+UQRheYJZapIgd5tmHLb9sJLeC6QRJzgCLweO3 + jaGzwN96q5/Wgjldn5a/eW1w0anwx34BVOkOJxvcvvhgleI7vlpZ93tuWsJ8xqjU + 0mRB3NhRhlVxS1UJbgF1LbciLvcLJ7QpCM5ExS1tpZFBeSY+sov+UQo05T66ZapY + Wwbh7AqDI0F0J+j5pPUG0+Whkju4oxB0FUd69ggMm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quaYP26sKWk6ollrPrtObppgpPtmKFVrdah9GMtPG0/ArrecP1g5 + 6JF0KXX2bR/7lr1QK7ETMcopaTW9zTVhTA9go1aoVP99J1O/Bxq/AoHWBM495NEt + laN4VXip4hiwU1Y8zAPm/vbX25UByjRAW6cvROy26HegZC42TU4VeLL0fH0RbB/j + 6C13x+L1vHDFQUEpJBwCXSSxnMTG9iczScEVE26of19oKMLB5s2Khn/ikrPKY/1r + n0U2UCq26EC1rT+G9Y34PGLzDgoOe4pJV8MIgAN12U4Bj8GbpBU/FbrhzdOmMquO + tFkwYaBagxuZ62faJ2KyW5oZZNrXKW55EGRXlHme4JLLxrCRUwZLO7cu5SA6O8e4 + cmkdL5Z6uLmuA2U5FsayeQKB1glMG8ySchP5yjHYr0j/mZkDhFQL8o+P4VcPK30+ + IlcGfivR+CVccz5ggsVKb9f67p7Rm4q1iwFecX1uaaT6kZKT8S+UrQeLE2WecK10 + uPSUvkxY76lZgwl265LD1ZMV73BcH4TwRCWmcK95UCrgKG+FlvGKRtkpk9+YpaC6 + NB4uFrRU42GXGGcrMwUkqTBzghfVqiL89QvqnsOG6a72OEpWHFMlb/LOvIpABPij + 40N+EpmX2SLpbIidkd2J6E5NUAUkgw4Zbbm4WZ4mAJs93+kEMZn2qCMCgdYLpnPB + uZ6pDpmXxKmfGRqzZVCwYJk9myya2ZcAmjfEHGpLHcy99mDSgb9BaUnceGizgSnG + KQCxAnX7xFXshz52DIfGZqKANyuuCRXv34aB0PxHozmlmAjuU5Y8I8PcioOqNLh3 + ZEcXNEVhhaZKGi9yz7QXZsauxjYGjgsGvaV+yPrzWcnIWsKW0X0aC3eIDOaw8yCC + F9qQXfT559lNaH3+aBCVlDL17HtOkax8J04vI1gEbqIyd9vn34+iFBcC5TBq9qZT + BvUE7g/dCNw3ISPEAgVJZUHJ + -----END PRIVATE KEY----- + ''')) + public_key = p11.C_CreateObject( + self.session, CKA_TOKEN = True, CKA_CLASS = CKO_PUBLIC_KEY, CKA_KEY_TYPE = CKK_RSA, CKA_ID = "RSA-3416", + CKA_VERIFY = True, + CKA_MODULUS = k.n, + CKA_PUBLIC_EXPONENT = k.e) + private_key = p11.C_CreateObject( + self.session, CKA_TOKEN = True, CKA_CLASS = CKO_PRIVATE_KEY, CKA_KEY_TYPE = CKK_RSA, CKA_ID = "RSA-3416", + CKA_SIGN = True, + CKA_MODULUS = k.n, + CKA_PUBLIC_EXPONENT = k.e, + CKA_PRIVATE_EXPONENT= k.d, + CKA_PRIME_1 = k.p, + CKA_PRIME_2 = k.q, + CKA_COEFFICIENT = inverse(k.q, k.p), + CKA_EXPONENT_1 = k.d % (k.p - 1), + CKA_EXPONENT_2 = k.d % (k.q - 1)) + print "Public key 0x{:x} private key 0x{:x}".format(public_key, private_key) + self.assertIsKeypair(public_key, private_key) + hamster = "Your mother was a hamster" + p11.C_SignInit(self.session, CKM_SHA512_RSA_PKCS, private_key) + sig = p11.C_Sign(self.session, hamster) + self.assertIsInstance(sig, str) + p11.C_VerifyInit(self.session, CKM_SHA512_RSA_PKCS, public_key) + p11.C_Verify(self.session, hamster, sig) + def test_gen_sign_verify_rsa_3416(self): public_key, private_key = p11.C_GenerateKeyPair( self.session, CKM_RSA_PKCS_KEY_PAIR_GEN, CKA_MODULUS_BITS = 3416, -- cgit v1.2.3