def generate_rsa_private_key(self, public_exponent, key_size): if public_exponent < 3: raise ValueError("public_exponent must be >= 3") if public_exponent & 1 == 0: raise ValueError("public_exponent must be odd") if key_size < 512: raise ValueError("key_size must be at least 512-bits") ctx = backend._lib.RSA_new() ctx = backend._ffi.gc(ctx, backend._lib.RSA_free) bn = backend._lib.BN_new() assert bn != self._ffi.NULL bn = backend._ffi.gc(bn, backend._lib.BN_free) res = backend._lib.BN_set_word(bn, public_exponent) assert res == 1 res = backend._lib.RSA_generate_key_ex(ctx, key_size, bn, backend._ffi.NULL) assert res == 1 return rsa.RSAPrivateKey( p=self._bn_to_int(ctx.p), q=self._bn_to_int(ctx.q), dmp1=self._bn_to_int(ctx.dmp1), dmq1=self._bn_to_int(ctx.dmq1), iqmp=self._bn_to_int(ctx.iqmp), private_exponent=self._bn_to_int(ctx.d), public_exponent=self._bn_to_int(ctx.e), modulus=self._bn_to_int(ctx.n), )
def test_pss_signing(self, pkcs1_example, backend): private, public, example = pkcs1_example private_key = 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"]) public_key = rsa.RSAPublicKey( public_exponent=public["public_exponent"], modulus=public["modulus"]) signer = private_key.signer( padding.PSS(mgf=padding.MGF1(algorithm=hashes.SHA1()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA1(), backend) signer.update(binascii.unhexlify(example["message"])) signature = signer.finalize() assert len(signature) == math.ceil(private_key.key_size / 8.0) # PSS signatures contain randomness so we can't do an exact # signature check. Instead we'll verify that the signature created # successfully verifies. verifier = public_key.verifier( signature, padding.PSS(mgf=padding.MGF1(algorithm=hashes.SHA1()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA1(), backend) verifier.update(binascii.unhexlify(example["message"])) verifier.verify()
def _rsa_cdata_to_private_key(self, cdata): return rsa.RSAPrivateKey( p=self._bn_to_int(cdata.p), q=self._bn_to_int(cdata.q), dmp1=self._bn_to_int(cdata.dmp1), dmq1=self._bn_to_int(cdata.dmq1), iqmp=self._bn_to_int(cdata.iqmp), private_exponent=self._bn_to_int(cdata.d), public_exponent=self._bn_to_int(cdata.e), modulus=self._bn_to_int(cdata.n), )
def test_pkcs1v15_signing(self, pkcs1_example, backend): private, public, example = pkcs1_example private_key = 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"]) signer = private_key.signer(padding.PKCS1v15(), hashes.SHA1(), backend) signer.update(binascii.unhexlify(example["message"])) signature = signer.finalize() assert binascii.hexlify(signature) == example["signature"]
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"] ) ciphertext = binascii.unhexlify(example["encryption"]) assert len(ciphertext) == math.ceil(skey.key_size / 8.0) message = skey.decrypt( ciphertext, padding.PKCS1v15(), backend ) assert message == binascii.unhexlify(example["message"])
def test_load_pss_vect_example_keys(self, pkcs1_example): secret, public = pkcs1_example skey = rsa.RSAPrivateKey( p=secret["p"], q=secret["q"], private_exponent=secret["private_exponent"], dmp1=secret["dmp1"], dmq1=secret["dmq1"], iqmp=secret["iqmp"], public_exponent=secret["public_exponent"], modulus=secret["modulus"] ) assert skey _check_rsa_private_key(skey) pkey = rsa.RSAPublicKey( public_exponent=public["public_exponent"], modulus=public["modulus"] ) assert pkey pkey2 = skey.public_key() assert pkey2 assert skey.modulus == pkey.modulus assert skey.modulus == skey.n assert skey.public_exponent == pkey.public_exponent assert skey.public_exponent == skey.e assert skey.private_exponent == skey.d assert pkey.modulus assert pkey.modulus == pkey2.modulus assert pkey.modulus == pkey.n assert pkey.public_exponent == pkey2.public_exponent assert pkey.public_exponent == pkey.e assert skey.key_size assert skey.key_size == pkey.key_size assert skey.key_size == pkey2.key_size
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"])
def test_invalid_private_key_argument_values(self): # 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=11, private_exponent=3, dmp1=1, dmq1=3, iqmp=2, public_exponent=7, modulus=2 ) # Test a modulus != p * q. with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, q=11, private_exponent=3, dmp1=1, dmq1=3, iqmp=2, public_exponent=7, modulus=35 ) # Test a p > modulus. with pytest.raises(ValueError): rsa.RSAPrivateKey( p=37, q=11, private_exponent=3, dmp1=1, dmq1=3, iqmp=2, public_exponent=7, modulus=33 ) # Test a q > modulus. with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, 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=33 ) # Test a private_exponent > modulus with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, q=11, private_exponent=37, dmp1=1, dmq1=3, iqmp=2, public_exponent=7, modulus=33 ) # Test a public_exponent < 3 with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, q=11, private_exponent=3, dmp1=1, dmq1=3, iqmp=2, public_exponent=1, modulus=33 ) # Test a public_exponent > modulus with pytest.raises(ValueError): rsa.RSAPrivateKey( p=3, 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=11, private_exponent=3, dmp1=1, dmq1=3, iqmp=2, public_exponent=6, 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_private_key_argument_types(self): with pytest.raises(TypeError): rsa.RSAPrivateKey(None, None, None, None, None, None, None, None)