Python cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers() Examples

The following are 8 code examples of cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateNumbers(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cryptography.hazmat.primitives.asymmetric.ec , or try the search function .
Example #1
Source File: encrypted_queries_tools.py    From github-token with GNU General Public License v3.0 6 votes vote down vote up
def hex_to_priv_key(priv_key_hex, public_key_hex):
    priv_key_value = long(priv_key_hex, 16)
    public_key = hex_to_key(public_key_hex)
    public_numbers = public_key.public_numbers()
    private_numbers = ec.EllipticCurvePrivateNumbers(priv_key_value, public_numbers)
    priv_key = private_numbers.private_key(backend)
    return priv_key

# Hybrid Encryption Scheme:
# - We perform a Elliptic Curves Diffie-Hellman Key Exchange using:
#    - SECP256K1 as curve for key generation
#    - ANSI X9.63 KDF as Key Derivation Function to derive the shared secret
# - The symmetric cipher is an AES256.MODE_GCM with authentication tag 16-byte
#   of length. Since the key is used only once, we can pick the known nonce/iv
#  '000000000000' (96 bits of length). We return concatenation of the encoded
#   point, the tag and the ciphertext 
Example #2
Source File: cryptography_backend.py    From python-jose with MIT License 6 votes vote down vote up
def _process_jwk(self, jwk_dict):
        if not jwk_dict.get('kty') == 'EC':
            raise JWKError("Incorrect key type. Expected: 'EC', Received: %s" % jwk_dict.get('kty'))

        if not all(k in jwk_dict for k in ['x', 'y', 'crv']):
            raise JWKError('Mandatory parameters are missing')

        x = base64_to_long(jwk_dict.get('x'))
        y = base64_to_long(jwk_dict.get('y'))
        curve = {
            'P-256': ec.SECP256R1,
            'P-384': ec.SECP384R1,
            'P-521': ec.SECP521R1,
        }[jwk_dict['crv']]

        public = ec.EllipticCurvePublicNumbers(x, y, curve())

        if 'd' in jwk_dict:
            d = base64_to_long(jwk_dict.get('d'))
            private = ec.EllipticCurvePrivateNumbers(d, public)

            return private.private_key(self.cryptography_backend())
        else:
            return public.public_key(self.cryptography_backend()) 
Example #3
Source File: jwk.py    From jwcrypto with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _ec_pri(self, k, curve):
        return ec.EllipticCurvePrivateNumbers(self._decode_int(k['d']),
                                              self._ec_pub(k, curve)) 
Example #4
Source File: keys.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _fromECComponents(cls, x, y, curve, privateValue=None):
        """
        Build a key from EC components.

        @param x: The affine x component of the public point used for verifying.
        @type x: L{int}

        @param y: The affine y component of the public point used for verifying.
        @type y: L{int}

        @param curve: NIST name of elliptic curve.
        @type curve: L{bytes}

        @param privateValue: The private value.
        @type privateValue: L{int}
        """

        publicNumbers = ec.EllipticCurvePublicNumbers(
            x=x, y=y, curve=_curveTable[curve])
        if privateValue is None:
            # We have public components.
            keyObject = publicNumbers.public_key(default_backend())
        else:
            privateNumbers = ec.EllipticCurvePrivateNumbers(
                private_value=privateValue, public_numbers=publicNumbers)
            keyObject = privateNumbers.private_key(default_backend())

        return cls(keyObject) 
Example #5
Source File: keys.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _fromECComponents(cls, x, y, curve, privateValue=None):
        """
        Build a key from EC components.

        @param x: The affine x component of the public point used for verifying.
        @type x: L{int}

        @param y: The affine y component of the public point used for verifying.
        @type y: L{int}

        @param curve: NIST name of elliptic curve.
        @type curve: L{bytes}

        @param privateValue: The private value.
        @type privateValue: L{int}
        """

        publicNumbers = ec.EllipticCurvePublicNumbers(
            x=x, y=y, curve=_curveTable[curve])
        if privateValue is None:
            # We have public components.
            keyObject = publicNumbers.public_key(default_backend())
        else:
            privateNumbers = ec.EllipticCurvePrivateNumbers(
                private_value=privateValue, public_numbers=publicNumbers)
            keyObject = privateNumbers.private_key(default_backend())

        return cls(keyObject) 
Example #6
Source File: fields.py    From PGPy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __privkey__(self):
        ecp = ec.EllipticCurvePublicNumbers(self.p.x, self.p.y, self.oid.curve())
        return ec.EllipticCurvePrivateNumbers(self.s, ecp).private_key(default_backend()) 
Example #7
Source File: test_kex.py    From python-hpedockerplugin with Apache License 2.0 5 votes vote down vote up
def dummy_generate_key_pair(obj):
    private_key_value = 94761803665136558137557783047955027733968423115106677159790289642479432803037
    public_key_numbers = "042bdab212fa8ba1b7c843301682a4db424d307246c7e1e6083c41d9ca7b098bf30b3d63e2ec6278488c135360456cc054b3444ecc45998c08894cbc1370f5f989"
    public_key_numbers_obj = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256R1(), unhexlify(public_key_numbers))
    obj.P = ec.EllipticCurvePrivateNumbers(private_value=private_key_value, public_numbers=public_key_numbers_obj).private_key(default_backend())
    if obj.transport.server_mode:
        obj.Q_S = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256R1(), unhexlify(public_key_numbers)).public_key(default_backend())
        return
    obj.Q_C = ec.EllipticCurvePublicNumbers.from_encoded_point(ec.SECP256R1(), unhexlify(public_key_numbers)).public_key(default_backend()) 
Example #8
Source File: test_ece.py    From encrypted-content-encoding with MIT License 4 votes vote down vote up
def _run(self, mode):
        if mode == 'encrypt':
            func = ece.encrypt
            local = 'sender'
            inp = 'input'
            outp = 'encrypted'
        else:
            func = ece.decrypt
            local = 'receiver'
            inp = 'encrypted'
            outp = 'input'

        for data in self.legacy_data:
            logmsg('%s: %s' % (mode, data['test']))
            p = data['params'][mode]

            if 'pad' in p and mode == 'encrypt':
                # This library doesn't pad in exactly the same way.
                continue

            if 'keys' in data:
                key = None
                decode_pub = ec.EllipticCurvePublicNumbers.from_encoded_point
                pubnum = decode_pub(ec.SECP256R1(),
                                    b64d(data['keys'][local]['public']))
                d = 0
                dbin = b64d(data['keys'][local]['private'])
                for i in range(0, len(dbin), 4):
                    d = (d << 32) + struct.unpack('!L', dbin[i:i + 4])[0]
                privnum = ec.EllipticCurvePrivateNumbers(d, pubnum)
                private_key = privnum.private_key(default_backend())
            else:
                key = b64d(p['key'])
                private_key = None

            if 'authSecret' in p:
                auth_secret = b64d(p['authSecret'])
            else:
                auth_secret = None
            if 'dh' in p:
                dh = b64d(p['dh'])
            else:
                dh = None

            result = func(
                b64d(data[inp]),
                salt=b64d(p['salt']),
                key=key,
                dh=dh,
                auth_secret=auth_secret,
                keyid=p.get('keyid'),
                private_key=private_key,
                rs=p.get('rs', 4096),
                version=p['version'],
            )
            eq_(b64d(data[outp]), result)