diff options
Diffstat (limited to 'tests/hazmat/primitives')
-rw-r--r-- | tests/hazmat/primitives/test_dh.py | 113 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_dsa.py | 8 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_rsa.py | 4 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_serialization.py | 61 |
4 files changed, 147 insertions, 39 deletions
diff --git a/tests/hazmat/primitives/test_dh.py b/tests/hazmat/primitives/test_dh.py new file mode 100644 index 00000000..115f3d8c --- /dev/null +++ b/tests/hazmat/primitives/test_dh.py @@ -0,0 +1,113 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import, division, print_function + +import pytest + +from cryptography.hazmat.primitives.asymmetric import dh + + +def test_dh_parameternumbers(): + params = dh.DHParameterNumbers( + 65537, 3 + ) + + assert params.p == 65537 + assert params.g == 3 + + with pytest.raises(TypeError): + dh.DHParameterNumbers( + None, 3 + ) + + with pytest.raises(TypeError): + dh.DHParameterNumbers( + 65537, None + ) + + with pytest.raises(TypeError): + dh.DHParameterNumbers( + None, None + ) + + +def test_dh_numbers(): + params = dh.DHParameterNumbers( + 65537, 3 + ) + + public = dh.DHPublicNumbers( + 1, params + ) + + assert public.parameter_numbers is params + assert public.y == 1 + + with pytest.raises(TypeError): + dh.DHPublicNumbers( + 1, None + ) + + with pytest.raises(TypeError): + dh.DHPublicNumbers( + None, params + ) + + private = dh.DHPrivateNumbers( + 1, public + ) + + assert private.public_numbers is public + assert private.x == 1 + + with pytest.raises(TypeError): + dh.DHPrivateNumbers( + 1, None + ) + + with pytest.raises(TypeError): + dh.DHPrivateNumbers( + None, public + ) + + +def test_dh_parameter_numbers_equality(): + assert dh.DHParameterNumbers(65537, 3) == dh.DHParameterNumbers(65537, 3) + assert dh.DHParameterNumbers(6, 3) != dh.DHParameterNumbers(65537, 3) + assert dh.DHParameterNumbers(65537, 0) != dh.DHParameterNumbers(65537, 3) + assert dh.DHParameterNumbers(65537, 0) != object() + + +def test_dh_private_numbers_equality(): + params = dh.DHParameterNumbers(65537, 3) + public = dh.DHPublicNumbers(1, params) + private = dh.DHPrivateNumbers(2, public) + + assert private == dh.DHPrivateNumbers(2, public) + assert private != dh.DHPrivateNumbers(0, public) + assert private != dh.DHPrivateNumbers(2, dh.DHPublicNumbers(0, params)) + assert private != dh.DHPrivateNumbers( + 2, dh.DHPublicNumbers(1, dh.DHParameterNumbers(65537, 0)) + ) + assert private != object() + + +def test_dh_public_numbers_equality(): + params = dh.DHParameterNumbers(65537, 3) + public = dh.DHPublicNumbers(1, params) + + assert public == dh.DHPublicNumbers(1, params) + assert public != dh.DHPublicNumbers(0, params) + assert public != dh.DHPublicNumbers(1, dh.DHParameterNumbers(65537, 0)) + assert public != object() diff --git a/tests/hazmat/primitives/test_dsa.py b/tests/hazmat/primitives/test_dsa.py index 8c0fb80c..95164923 100644 --- a/tests/hazmat/primitives/test_dsa.py +++ b/tests/hazmat/primitives/test_dsa.py @@ -10,7 +10,7 @@ import pytest from cryptography.exceptions import AlreadyFinalized, InvalidSignature from cryptography.hazmat.backends.interfaces import DSABackend -from cryptography.hazmat.primitives import hashes, interfaces +from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import dsa from cryptography.hazmat.primitives.asymmetric.utils import ( encode_rfc6979_signature @@ -30,7 +30,7 @@ from ...utils import ( class TestDSA(object): def test_generate_dsa_parameters(self, backend): parameters = dsa.generate_parameters(1024, backend) - assert isinstance(parameters, interfaces.DSAParameters) + assert isinstance(parameters, dsa.DSAParameters) def test_generate_invalid_dsa_parameters(self, backend): with pytest.raises(ValueError): @@ -51,7 +51,7 @@ class TestDSA(object): g=vector['g'] ).parameters(backend) skey = parameters.generate_private_key() - if isinstance(skey, interfaces.DSAPrivateKeyWithNumbers): + if isinstance(skey, dsa.DSAPrivateKeyWithNumbers): numbers = skey.private_numbers() skey_parameters = numbers.public_numbers.parameter_numbers pkey = skey.public_key() @@ -74,7 +74,7 @@ class TestDSA(object): def test_generate_dsa_private_key_and_parameters(self, backend): skey = dsa.generate_private_key(1024, backend) assert skey - if isinstance(skey, interfaces.DSAPrivateKeyWithNumbers): + if isinstance(skey, dsa.DSAPrivateKeyWithNumbers): numbers = skey.private_numbers() skey_parameters = numbers.public_numbers.parameter_numbers assert numbers.public_numbers.y == pow( diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 6d8e6874..74183010 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -16,7 +16,7 @@ from cryptography.exceptions import ( AlreadyFinalized, InvalidSignature, _Reasons ) from cryptography.hazmat.backends.interfaces import RSABackend -from cryptography.hazmat.primitives import hashes, interfaces +from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding, rsa from cryptography.hazmat.primitives.asymmetric.rsa import ( RSAPrivateNumbers, RSAPublicNumbers @@ -91,7 +91,7 @@ class TestRSA(object): skey = rsa.generate_private_key(public_exponent, key_size, backend) assert skey.key_size == key_size - if isinstance(skey, interfaces.RSAPrivateKeyWithNumbers): + if isinstance(skey, rsa.RSAPrivateKeyWithNumbers): _check_rsa_private_numbers(skey.private_numbers()) pkey = skey.public_key() assert isinstance(pkey.public_numbers(), rsa.RSAPublicNumbers) diff --git a/tests/hazmat/primitives/test_serialization.py b/tests/hazmat/primitives/test_serialization.py index a4a91430..a17aac4b 100644 --- a/tests/hazmat/primitives/test_serialization.py +++ b/tests/hazmat/primitives/test_serialization.py @@ -16,12 +16,7 @@ from cryptography.hazmat.backends.interfaces import ( DERSerializationBackend, DSABackend, EllipticCurveBackend, PEMSerializationBackend, RSABackend ) -from cryptography.hazmat.primitives import interfaces -from cryptography.hazmat.primitives.asymmetric import ec -from cryptography.hazmat.primitives.asymmetric.dsa import ( - DSAParameterNumbers, DSAPublicNumbers -) -from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers +from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa from cryptography.hazmat.primitives.serialization import ( load_der_private_key, load_der_public_key, load_pem_private_key, load_pem_public_key, load_ssh_public_key @@ -57,8 +52,8 @@ class TestDERSerialization(object): mode="rb" ) assert key - assert isinstance(key, interfaces.RSAPrivateKey) - if isinstance(key, interfaces.RSAPrivateKeyWithNumbers): + assert isinstance(key, rsa.RSAPrivateKey) + if isinstance(key, rsa.RSAPrivateKeyWithNumbers): _check_rsa_private_numbers(key.private_numbers()) @pytest.mark.requires_backend_interface(interface=DSABackend) @@ -80,8 +75,8 @@ class TestDERSerialization(object): mode="rb" ) assert key - assert isinstance(key, interfaces.DSAPrivateKey) - if isinstance(key, interfaces.DSAPrivateKeyWithNumbers): + assert isinstance(key, dsa.DSAPrivateKey) + if isinstance(key, dsa.DSAPrivateKeyWithNumbers): _check_dsa_private_numbers(key.private_numbers()) @pytest.mark.parametrize( @@ -103,7 +98,7 @@ class TestDERSerialization(object): ) assert key - assert isinstance(key, interfaces.EllipticCurvePrivateKey) + assert isinstance(key, ec.EllipticCurvePrivateKey) assert key.curve.name == "secp256r1" assert key.curve.key_size == 256 @@ -250,8 +245,8 @@ class TestDERSerialization(object): mode="rb" ) assert key - assert isinstance(key, interfaces.RSAPublicKey) - if isinstance(key, interfaces.RSAPublicKeyWithNumbers): + assert isinstance(key, rsa.RSAPublicKey) + if isinstance(key, rsa.RSAPublicKeyWithNumbers): numbers = key.public_numbers() assert numbers.e == 65537 @@ -278,7 +273,7 @@ class TestDERSerialization(object): mode="rb" ) assert key - assert isinstance(key, interfaces.DSAPublicKey) + assert isinstance(key, dsa.DSAPublicKey) @pytest.mark.requires_backend_interface(interface=EllipticCurveBackend) def test_load_ec_public_key(self, backend): @@ -293,7 +288,7 @@ class TestDERSerialization(object): mode="rb" ) assert key - assert isinstance(key, interfaces.EllipticCurvePublicKey) + assert isinstance(key, ec.EllipticCurvePublicKey) assert key.curve.name == "secp256r1" assert key.curve.key_size == 256 @@ -333,8 +328,8 @@ class TestPEMSerialization(object): ) assert key - assert isinstance(key, interfaces.RSAPrivateKey) - if isinstance(key, interfaces.RSAPrivateKeyWithNumbers): + assert isinstance(key, rsa.RSAPrivateKey) + if isinstance(key, rsa.RSAPrivateKeyWithNumbers): _check_rsa_private_numbers(key.private_numbers()) @pytest.mark.parametrize( @@ -355,8 +350,8 @@ class TestPEMSerialization(object): ) ) assert key - assert isinstance(key, interfaces.DSAPrivateKey) - if isinstance(key, interfaces.DSAPrivateKeyWithNumbers): + assert isinstance(key, dsa.DSAPrivateKey) + if isinstance(key, dsa.DSAPrivateKeyWithNumbers): _check_dsa_private_numbers(key.private_numbers()) @pytest.mark.parametrize( @@ -379,7 +374,7 @@ class TestPEMSerialization(object): ) assert key - assert isinstance(key, interfaces.EllipticCurvePrivateKey) + assert isinstance(key, ec.EllipticCurvePrivateKey) assert key.curve.name == "secp256r1" assert key.curve.key_size == 256 @@ -399,8 +394,8 @@ class TestPEMSerialization(object): ) ) assert key - assert isinstance(key, interfaces.RSAPublicKey) - if isinstance(key, interfaces.RSAPublicKeyWithNumbers): + assert isinstance(key, rsa.RSAPublicKey) + if isinstance(key, rsa.RSAPublicKeyWithNumbers): numbers = key.public_numbers() assert numbers.e == 65537 @@ -421,7 +416,7 @@ class TestPEMSerialization(object): ) ) assert key - assert isinstance(key, interfaces.DSAPublicKey) + assert isinstance(key, dsa.DSAPublicKey) @pytest.mark.requires_backend_interface(interface=EllipticCurveBackend) def test_load_ec_public_key(self, backend): @@ -435,7 +430,7 @@ class TestPEMSerialization(object): ) ) assert key - assert isinstance(key, interfaces.EllipticCurvePublicKey) + assert isinstance(key, ec.EllipticCurvePublicKey) assert key.curve.name == "secp256r1" assert key.curve.key_size == 256 @@ -762,12 +757,12 @@ class TestPEMSerialization(object): ) ) assert key - assert isinstance(key, interfaces.DSAPrivateKey) + assert isinstance(key, dsa.DSAPrivateKey) params = key.parameters() - assert isinstance(params, interfaces.DSAParameters) + assert isinstance(params, dsa.DSAParameters) - if isinstance(params, interfaces.DSAParametersWithNumbers): + if isinstance(params, dsa.DSAParametersWithNumbers): num = key.private_numbers() pub = num.public_numbers parameter_numbers = pub.parameter_numbers @@ -917,7 +912,7 @@ class TestRSASSHSerialization(object): key = load_ssh_public_key(ssh_key, backend) assert key is not None - assert isinstance(key, interfaces.RSAPublicKey) + assert isinstance(key, rsa.RSAPublicKey) numbers = key.public_numbers() @@ -934,7 +929,7 @@ class TestRSASSHSerialization(object): '46F8706AB88DDADBD9E8204D48B87789081E074024C8996783B31' '7076A98ABF0A2D8550EAF2097D8CCC7BE76EF', 16) - expected = RSAPublicNumbers(expected_e, expected_n) + expected = rsa.RSAPublicNumbers(expected_e, expected_n) assert numbers == expected @@ -1017,7 +1012,7 @@ class TestDSSSSHSerialization(object): key = load_ssh_public_key(ssh_key, backend) assert key is not None - assert isinstance(key, interfaces.DSAPublicKey) + assert isinstance(key, dsa.DSAPublicKey) numbers = key.public_numbers() @@ -1043,9 +1038,9 @@ class TestDSSSSHSerialization(object): "debb5982fc94d6a8c291f758feae63ad769a5621947221522a2dc31d18ede6f" "b656", 16 ) - expected = DSAPublicNumbers( + expected = dsa.DSAPublicNumbers( expected_y, - DSAParameterNumbers(expected_p, expected_q, expected_g) + dsa.DSAParameterNumbers(expected_p, expected_q, expected_g) ) assert numbers == expected @@ -1062,7 +1057,7 @@ class TestECDSASSHSerialization(object): b"teIg1TO03/FD9hbpBFgBeix3NrCFPls= root@cloud-server-01" ) key = load_ssh_public_key(ssh_key, backend) - assert isinstance(key, interfaces.EllipticCurvePublicKey) + assert isinstance(key, ec.EllipticCurvePublicKey) expected_x = int( "44196257377740326295529888716212621920056478823906609851236662550" |