From 16b953a22abf2092f6d428f04141f3e5c9513ce9 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 29 Mar 2014 12:27:46 -0500 Subject: prelim OAEP and PKCS1v15 for openssl 1.0.0+ and 0.9.8. decryption only --- tests/hazmat/primitives/test_rsa.py | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 84d0f805..70ae20dc 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1225,3 +1225,66 @@ class TestMGF1(object): mgf = padding.MGF1(algorithm, padding.MGF1.MAX_LENGTH) assert mgf._algorithm == algorithm assert mgf._salt_length == padding.MGF1.MAX_LENGTH + + +@pytest.mark.rsa +class TestRSADecryption(object): + @pytest.mark.parametrize( + "vector", + _flatten_pkcs1_examples(load_vectors_from_file( + os.path.join( + "asymmetric", "RSA", "pkcs-1v2-1d2-vec", "oaep-vect.txt"), + load_pkcs1_vectors + )) + ) + def test_decrypt_oaep_vectors(self, vector, backend): + private, public, example = vector + skey = rsa.RSAPrivateKey( + p=private["p"], + q=private["q"], + private_exponent=private["private_exponent"], + dmp1=private["dmp1"], + dmq1=private["dmq1"], + iqmp=private["iqmp"], + public_exponent=private["public_exponent"], + modulus=private["modulus"] + ) + message = backend.rsa_decrypt( + skey, + binascii.unhexlify(example["encryption"]), + # TODO: handle MGF1 here + padding.OAEP( + padding.MGF1( + algorithm=hashes.SHA1(), + salt_length=padding.MGF1.MAX_LENGTH + ) + ) + ) + assert message == binascii.unhexlify(example["message"]) + + @pytest.mark.parametrize( + "vector", + _flatten_pkcs1_examples(load_vectors_from_file( + os.path.join( + "asymmetric", "RSA", "pkcs1v15crypt-vectors.txt"), + load_pkcs1_vectors + )) + ) + def test_decrypt_pkcs1v15_vectors(self, vector, backend): + private, public, example = vector + skey = rsa.RSAPrivateKey( + p=private["p"], + q=private["q"], + private_exponent=private["private_exponent"], + dmp1=private["dmp1"], + dmq1=private["dmq1"], + iqmp=private["iqmp"], + public_exponent=private["public_exponent"], + modulus=private["modulus"] + ) + message = backend.rsa_decrypt( + skey, + binascii.unhexlify(example["encryption"]), + padding.PKCS1v15() + ) + assert message == binascii.unhexlify(example["message"]) -- cgit v1.2.3 From 4c0a374dd90cd48c21267e4d8be1ddef8288b29c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 5 Apr 2014 19:51:00 -0500 Subject: docs, tests, general huge improvements to RSA decryption --- tests/hazmat/primitives/test_rsa.py | 70 +++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 11 deletions(-) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 70ae20dc..7b658b69 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1227,6 +1227,17 @@ class TestMGF1(object): assert mgf._salt_length == padding.MGF1.MAX_LENGTH +class TestOAEP(object): + def test_invalid_algorithm(self): + mgf = padding.MGF1(hashes.SHA1()) + with pytest.raises(TypeError): + padding.OAEP( + mgf=mgf, + algorithm=b"", + label=None + ) + + @pytest.mark.rsa class TestRSADecryption(object): @pytest.mark.parametrize( @@ -1249,16 +1260,14 @@ class TestRSADecryption(object): public_exponent=private["public_exponent"], modulus=private["modulus"] ) - message = backend.rsa_decrypt( - skey, + message = skey.decrypt( binascii.unhexlify(example["encryption"]), - # TODO: handle MGF1 here padding.OAEP( - padding.MGF1( - algorithm=hashes.SHA1(), - salt_length=padding.MGF1.MAX_LENGTH - ) - ) + mgf=padding.MGF1(algorithm=hashes.SHA1()), + algorithm=hashes.SHA1(), + label=None + ), + backend ) assert message == binascii.unhexlify(example["message"]) @@ -1282,9 +1291,48 @@ class TestRSADecryption(object): public_exponent=private["public_exponent"], modulus=private["modulus"] ) - message = backend.rsa_decrypt( - skey, + message = skey.decrypt( binascii.unhexlify(example["encryption"]), - padding.PKCS1v15() + padding.PKCS1v15(), + backend ) assert message == binascii.unhexlify(example["message"]) + + def test_unsupported_padding(self, backend): + private_key = rsa.RSAPrivateKey.generate( + public_exponent=65537, + key_size=512, + backend=backend + ) + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING): + private_key.decrypt(b"somedata", DummyPadding(), backend) + + def test_unsupported_oaep_mgf(self, backend): + private_key = rsa.RSAPrivateKey.generate( + public_exponent=65537, + key_size=512, + backend=backend + ) + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_MGF): + private_key.decrypt( + b"ciphertext", + padding.OAEP( + mgf=DummyMGF(), + algorithm=hashes.SHA1(), + label=None + ), + backend + ) + + def test_decrypt_invalid_decrypt(self, backend): + private_key = rsa.RSAPrivateKey.generate( + public_exponent=65537, + key_size=512, + backend=backend + ) + with pytest.raises(exceptions.InternalError): + private_key.decrypt( + b"\x00" * 64, + padding.PKCS1v15(), + backend + ) -- cgit v1.2.3 From af9a2cc7bc73129fcd807ac890be59dcc9672a4c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 7 Apr 2014 22:15:38 -0500 Subject: add InvalidDecryption exception, check for ct > key size --- tests/hazmat/primitives/test_rsa.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 7b658b69..9c6d6f87 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1336,3 +1336,16 @@ class TestRSADecryption(object): padding.PKCS1v15(), backend ) + + def test_decrypt_ciphertext_too_large(self, backend): + private_key = rsa.RSAPrivateKey.generate( + public_exponent=65537, + key_size=512, + backend=backend + ) + with pytest.raises(ValueError): + private_key.decrypt( + b"\x00" * 65, + padding.PKCS1v15(), + backend + ) -- cgit v1.2.3 From 9a32ad6eed73674fecdd9c757a9842dc45c78fe4 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 7 Apr 2014 23:39:43 -0500 Subject: test should check for the right exception --- tests/hazmat/primitives/test_rsa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 9c6d6f87..d8b25cbb 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1330,7 +1330,7 @@ class TestRSADecryption(object): key_size=512, backend=backend ) - with pytest.raises(exceptions.InternalError): + with pytest.raises(exceptions.InvalidDecryption): private_key.decrypt( b"\x00" * 64, padding.PKCS1v15(), -- cgit v1.2.3 From 67feca0acd64a5c19fa56efd754430d4213e9f8b Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Wed, 9 Apr 2014 14:11:42 -0500 Subject: more testing for rsa decrypt --- tests/hazmat/primitives/test_rsa.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index d8b25cbb..38fab6ec 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1349,3 +1349,20 @@ class TestRSADecryption(object): padding.PKCS1v15(), backend ) + + def test_decrypt_ciphertext_too_small(self, backend): + private_key = rsa.RSAPrivateKey.generate( + public_exponent=65537, + key_size=512, + backend=backend + ) + ct = binascii.unhexlify( + b"50b4c14136bd198c2f3c3ed243fce036e168d56517984a263cd66492b80804f1" + b"69d210f2b9bdfb48b12f9ea05009c77da257cc600ccefe3a6283789d8ea0" + ) + with pytest.raises(exceptions.InvalidDecryption): + private_key.decrypt( + ct, + padding.PKCS1v15(), + backend + ) -- cgit v1.2.3 From 34ce33859b87df620e1edf27ba88db5b7e151a25 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 14 Apr 2014 16:04:44 -0400 Subject: cover a missing line --- tests/hazmat/primitives/test_rsa.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 38fab6ec..c43fd0b5 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1366,3 +1366,14 @@ class TestRSADecryption(object): padding.PKCS1v15(), backend ) + + def test_rsa_decrypt_invalid_backend(self, backend): + pretend_backend = object() + private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) + + with raises_unsupported_algorithm(_Reasons.BACKEND_MISSING_INTERFACE): + private_key.decrypt( + b"irrelevant", + padding.PKCS1v15(), + pretend_backend + ) -- cgit v1.2.3 From 7bdcdc175675bc78edaa7e0f931676652ab7a427 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 15 Apr 2014 14:16:35 -0400 Subject: remove OAEP decryption to simplify review --- tests/hazmat/primitives/test_rsa.py | 59 ------------------------------------- 1 file changed, 59 deletions(-) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index c43fd0b5..74a0c111 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1227,50 +1227,8 @@ class TestMGF1(object): assert mgf._salt_length == padding.MGF1.MAX_LENGTH -class TestOAEP(object): - def test_invalid_algorithm(self): - mgf = padding.MGF1(hashes.SHA1()) - with pytest.raises(TypeError): - padding.OAEP( - mgf=mgf, - algorithm=b"", - label=None - ) - - @pytest.mark.rsa class TestRSADecryption(object): - @pytest.mark.parametrize( - "vector", - _flatten_pkcs1_examples(load_vectors_from_file( - os.path.join( - "asymmetric", "RSA", "pkcs-1v2-1d2-vec", "oaep-vect.txt"), - load_pkcs1_vectors - )) - ) - def test_decrypt_oaep_vectors(self, vector, backend): - private, public, example = vector - skey = rsa.RSAPrivateKey( - p=private["p"], - q=private["q"], - private_exponent=private["private_exponent"], - dmp1=private["dmp1"], - dmq1=private["dmq1"], - iqmp=private["iqmp"], - public_exponent=private["public_exponent"], - modulus=private["modulus"] - ) - message = skey.decrypt( - binascii.unhexlify(example["encryption"]), - padding.OAEP( - mgf=padding.MGF1(algorithm=hashes.SHA1()), - algorithm=hashes.SHA1(), - label=None - ), - backend - ) - assert message == binascii.unhexlify(example["message"]) - @pytest.mark.parametrize( "vector", _flatten_pkcs1_examples(load_vectors_from_file( @@ -1307,23 +1265,6 @@ class TestRSADecryption(object): with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING): private_key.decrypt(b"somedata", DummyPadding(), backend) - def test_unsupported_oaep_mgf(self, backend): - private_key = rsa.RSAPrivateKey.generate( - public_exponent=65537, - key_size=512, - backend=backend - ) - with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_MGF): - private_key.decrypt( - b"ciphertext", - padding.OAEP( - mgf=DummyMGF(), - algorithm=hashes.SHA1(), - label=None - ), - backend - ) - def test_decrypt_invalid_decrypt(self, backend): private_key = rsa.RSAPrivateKey.generate( public_exponent=65537, -- cgit v1.2.3 From 8ab7a360330daa195ea1c0cf70d606dc7dce88c8 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 19 Apr 2014 09:34:56 -0500 Subject: remove InvalidDecryption and replace with ValueError --- tests/hazmat/primitives/test_rsa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 74a0c111..69fd2933 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1271,7 +1271,7 @@ class TestRSADecryption(object): key_size=512, backend=backend ) - with pytest.raises(exceptions.InvalidDecryption): + with pytest.raises(ValueError): private_key.decrypt( b"\x00" * 64, padding.PKCS1v15(), @@ -1301,7 +1301,7 @@ class TestRSADecryption(object): b"50b4c14136bd198c2f3c3ed243fce036e168d56517984a263cd66492b80804f1" b"69d210f2b9bdfb48b12f9ea05009c77da257cc600ccefe3a6283789d8ea0" ) - with pytest.raises(exceptions.InvalidDecryption): + with pytest.raises(ValueError): private_key.decrypt( ct, padding.PKCS1v15(), -- cgit v1.2.3 From 8e764396471beb13d0cdfbc9a299b9445f96abb2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 20 Apr 2014 10:25:48 -0500 Subject: more key length checks, docs update --- tests/hazmat/primitives/test_rsa.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 69fd2933..a5266d57 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1249,8 +1249,10 @@ class TestRSADecryption(object): public_exponent=private["public_exponent"], modulus=private["modulus"] ) + ciphertext = binascii.unhexlify(example["encryption"]) + assert len(ciphertext) == math.ceil(skey.key_size / 8.0) message = skey.decrypt( - binascii.unhexlify(example["encryption"]), + ciphertext, padding.PKCS1v15(), backend ) -- cgit v1.2.3