From 09328bbfb3306d3c7b02a44f12ecd22ce3ec700e Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Wed, 12 Feb 2014 23:57:27 -0600 Subject: add crt coefficients to RSAPrivateKey constructor and update tests --- tests/hazmat/primitives/test_rsa.py | 163 +++++++++++++++++++++++++++++------- 1 file changed, 135 insertions(+), 28 deletions(-) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 0e930e44..9dc1a9e4 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -24,6 +24,20 @@ from cryptography.hazmat.primitives.asymmetric import rsa from ...utils import load_pkcs1_vectors, load_vectors_from_file +def _egcd(a, b): + if a == 0: + return (b, 0, 1) + else: + g, y, x = _egcd(b % a, a) + return (g, x - (b // a) * y, y) + + +def _modinv(a, m): + g, x, y = _egcd(a, m) + assert g == 1 + return x % m + + def _check_rsa_private_key(skey): assert skey assert skey.modulus @@ -31,6 +45,9 @@ def _check_rsa_private_key(skey): assert skey.private_exponent assert skey.p * skey.q == skey.modulus assert skey.key_size + assert skey._dmp1 == skey.d % (skey.p - 1) + assert skey._dmq1 == skey.d % (skey.q - 1) + assert skey._iqmp == _modinv(skey.q, skey.p) pkey = skey.public_key() assert pkey @@ -115,22 +132,26 @@ class TestRSA(object): def test_invalid_private_key_argument_types(self): with pytest.raises(TypeError): - rsa.RSAPrivateKey(None, None, None, None, None) + rsa.RSAPrivateKey(None, None, None, None, None, None, None, None) def test_invalid_public_key_argument_types(self): with pytest.raises(TypeError): rsa.RSAPublicKey(None, None) def test_invalid_private_key_argument_values(self): - # Start with p=3, q=5, private_exponent=14, public_exponent=7, - # modulus=15. Then change one value at a time to test the bounds. + # Start with p=3, q=11, private_exponent=3, public_exponent=7, + # modulus=33, dmp1=1, dmq1=3, iqmp=2. Then change one value at + # a time to test the bounds. # Test a modulus < 3. with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=5, - private_exponent=14, + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, public_exponent=7, modulus=2 ) @@ -139,70 +160,156 @@ class TestRSA(object): with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=5, - private_exponent=14, + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, public_exponent=7, - modulus=16 + modulus=35 ) # Test a p > modulus. with pytest.raises(ValueError): rsa.RSAPrivateKey( - p=16, - q=5, - private_exponent=14, + p=37, + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, public_exponent=7, - modulus=15 + modulus=33 ) # Test a q > modulus. with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=16, - private_exponent=14, + q=37, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_exponent=7, + modulus=33 + ) + + # Test a dmp1 > modulus. + with pytest.raises(ValueError): + rsa.RSAPrivateKey( + p=3, + q=11, + private_exponent=3, + dmp1=35, + dmq1=3, + iqmp=2, + public_exponent=7, + modulus=33 + ) + + # Test a dmq1 > modulus. + with pytest.raises(ValueError): + rsa.RSAPrivateKey( + p=3, + q=11, + private_exponent=3, + dmp1=1, + dmq1=35, + iqmp=2, + public_exponent=7, + modulus=33 + ) + + # Test an iqmp > modulus. + with pytest.raises(ValueError): + rsa.RSAPrivateKey( + p=3, + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=35, public_exponent=7, - modulus=15 + modulus=33 ) # Test a private_exponent > modulus with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=5, - private_exponent=16, + q=11, + private_exponent=37, + dmp1=1, + dmq1=3, + iqmp=2, public_exponent=7, - modulus=15 + modulus=33 ) # Test a public_exponent < 3 with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=5, - private_exponent=14, + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, public_exponent=1, - modulus=15 + modulus=33 ) # Test a public_exponent > modulus with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=5, - private_exponent=14, - public_exponent=17, - modulus=15 + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_exponent=65537, + modulus=33 ) # Test a public_exponent that is not odd. with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, - q=5, - private_exponent=14, + q=11, + private_exponent=3, + dmp1=1, + dmq1=3, + iqmp=2, public_exponent=6, - modulus=15 + modulus=33 + ) + + # Test a dmp1 that is not odd. + with pytest.raises(ValueError): + rsa.RSAPrivateKey( + p=3, + q=11, + private_exponent=3, + dmp1=2, + dmq1=3, + iqmp=2, + public_exponent=7, + modulus=33 + ) + + # Test a dmq1 that is not odd. + with pytest.raises(ValueError): + rsa.RSAPrivateKey( + p=3, + q=11, + private_exponent=3, + dmp1=1, + dmq1=4, + iqmp=2, + public_exponent=7, + modulus=33 ) def test_invalid_public_key_argument_values(self): -- cgit v1.2.3 From 8e9c984d5d85db83c5fa45f6f2083d6b5e15326b Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 13 Feb 2014 12:23:27 -0600 Subject: expose dmp1, dmq1, iqmp getters on RSAPrivateKey --- tests/hazmat/primitives/test_rsa.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 9dc1a9e4..312d5a6f 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -45,9 +45,9 @@ def _check_rsa_private_key(skey): assert skey.private_exponent assert skey.p * skey.q == skey.modulus assert skey.key_size - assert skey._dmp1 == skey.d % (skey.p - 1) - assert skey._dmq1 == skey.d % (skey.q - 1) - assert skey._iqmp == _modinv(skey.q, skey.p) + assert skey.dmp1 == skey.d % (skey.p - 1) + assert skey.dmq1 == skey.d % (skey.q - 1) + assert skey.iqmp == _modinv(skey.q, skey.p) pkey = skey.public_key() assert pkey -- cgit v1.2.3 From c9f48f67da5694600c6149621e384924c836c322 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 14 Feb 2014 10:20:52 -0600 Subject: add egcd and modinv unit tests --- tests/hazmat/primitives/test_rsa.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 312d5a6f..56ce7032 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -56,6 +56,42 @@ def _check_rsa_private_key(skey): assert skey.key_size == pkey.key_size +def test_egcd(): + assert _egcd(0, 1) == (1, 0, 1) + val = _egcd( + int("c08e63c2fdf20bd2a05bd448e34a892cc8718c13ac5f8fb3f55cff7bc1a7f891", + 16), + int("e525d5ee4fe6d6142812fe12a7b531ebfaa033be349e4c3680d264f41d82008b", + 16) + ) + assert val[0] == 1L + assert val[1] == int("6caa1a8fae63ab2b8d4051101828985ef93fb0ffc14a955f93fd" + "9c4a3fd8c7b1", 16) + assert val[2] == int("-5b4ff48d0c09936986bc3c544f1e7dd684f8c0f09ae36a7819e" + "c95017c4be1c0", 16) + + +def test_modular_inverse(): + p = int( + "d1f9f6c09fd3d38987f7970247b85a6da84907753d42ec52bc23b745093f4fff5cff3" + "617ce43d00121a9accc0051f519c76e08cf02fc18acfe4c9e6aea18da470a2b611d2e" + "56a7b35caa2c0239bc041a53cc5875ca0b668ae6377d4b23e932d8c995fd1e58ecfd8" + "c4b73259c0d8a54d691cca3f6fb85c8a5c1baf588e898d481", 16 + ) + q = int( + "d1519255eb8f678c86cfd06802d1fbef8b664441ac46b73d33d13a8404580a33a8e74" + "cb2ea2e2963125b3d454d7a922cef24dd13e55f989cbabf64255a736671f4629a47b5" + "b2347cfcd669133088d1c159518531025297c2d67c9da856a12e80222cd03b4c6ec0f" + "86c957cb7bb8de7a127b645ec9e820aa94581e4762e209f01", 16 + ) + assert _modinv(q, p) == int( + "0275e06afa722999315f8f322275483e15e2fb46d827b17800f99110b269a6732748f" + "624a382fa2ed1ec68c99f7fc56fb60e76eea51614881f497ba7034c17dde955f92f15" + "772f8b2b41f3e56d88b1e096cdd293eba4eae1e82db815e0fadea0c4ec971bc6fd875" + "c20e67e48c31a611e98d32c6213ae4c4d7b53023b2f80c538", 16 + ) + + @pytest.mark.rsa class TestRSA(object): @pytest.mark.parametrize( -- cgit v1.2.3 From e7fac4f893274098f153683a70a57da9f930428b Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 14 Feb 2014 10:35:37 -0600 Subject: python3 compatibility is good --- tests/hazmat/primitives/test_rsa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 56ce7032..9cb015b7 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -64,7 +64,7 @@ def test_egcd(): int("e525d5ee4fe6d6142812fe12a7b531ebfaa033be349e4c3680d264f41d82008b", 16) ) - assert val[0] == 1L + assert val[0] == 1 assert val[1] == int("6caa1a8fae63ab2b8d4051101828985ef93fb0ffc14a955f93fd" "9c4a3fd8c7b1", 16) assert val[2] == int("-5b4ff48d0c09936986bc3c544f1e7dd684f8c0f09ae36a7819e" -- cgit v1.2.3 From b557045ae353e98e869088714e4f433383f87ce5 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 14 Feb 2014 21:58:37 -0600 Subject: switch to more compact modular multiplicative inverse --- tests/hazmat/primitives/test_rsa.py | 38 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 9cb015b7..a413f10b 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -24,18 +24,17 @@ from cryptography.hazmat.primitives.asymmetric import rsa from ...utils import load_pkcs1_vectors, load_vectors_from_file -def _egcd(a, b): - if a == 0: - return (b, 0, 1) - else: - g, y, x = _egcd(b % a, a) - return (g, x - (b // a) * y, y) - - -def _modinv(a, m): - g, x, y = _egcd(a, m) - assert g == 1 - return x % m +def _modinv(e, m): + """ + Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1 + """ + x1, y1, x2, y2 = 1, 0, 0, 1 + a, b = e, m + while b > 0: + q, r = divmod(a, b) + xn, yn = x1 - q * x2, y1 - q * y2 + a, b, x1, y1, x2, y2 = b, r, x2, y2, xn, yn + return x1 % m def _check_rsa_private_key(skey): @@ -56,21 +55,6 @@ def _check_rsa_private_key(skey): assert skey.key_size == pkey.key_size -def test_egcd(): - assert _egcd(0, 1) == (1, 0, 1) - val = _egcd( - int("c08e63c2fdf20bd2a05bd448e34a892cc8718c13ac5f8fb3f55cff7bc1a7f891", - 16), - int("e525d5ee4fe6d6142812fe12a7b531ebfaa033be349e4c3680d264f41d82008b", - 16) - ) - assert val[0] == 1 - assert val[1] == int("6caa1a8fae63ab2b8d4051101828985ef93fb0ffc14a955f93fd" - "9c4a3fd8c7b1", 16) - assert val[2] == int("-5b4ff48d0c09936986bc3c544f1e7dd684f8c0f09ae36a7819e" - "c95017c4be1c0", 16) - - def test_modular_inverse(): p = int( "d1f9f6c09fd3d38987f7970247b85a6da84907753d42ec52bc23b745093f4fff5cff3" -- cgit v1.2.3