Esempio n. 1
0
    def __init__(self, curve, os_key=None, private_key=None):
        '''
        Constructor
        '''
        if not isinstance(curve, ec_curve.Curve):
            raise Exception('Provided curve is not a Curve object')

        self.curve = curve
        self.os_group = curve.os_group

        if os_key is not None:
            self.os_key = os_key
        else:
            self.os_key = OpenSSL.EC_KEY_new()
            OpenSSL.EC_KEY_set_group(self.os_key, self.os_group)
            if private_key is not None:
                privk = ec_bignum.BigNum(decval=private_key)
                pubk = private_key * curve.G
                OpenSSL.EC_KEY_set_private_key(self.os_key, privk.bn)
                OpenSSL.EC_KEY_set_public_key(self.os_key, pubk.os_point)
            else:
                OpenSSL.EC_KEY_generate_key(self.os_key)

        try:
            priv_key = ec_bignum.BigNum(
                OpenSSL.EC_KEY_get0_private_key(self.os_key))
            self.private_key = priv_key.get_value()
            self.public_key = ec_point.Point(
                self.curve,
                openssl_point=OpenSSL.EC_KEY_get0_public_key(self.os_key))
        finally:
            del priv_key
Esempio n. 2
0
    def _generate(self):
        try:
            pub_key_x = OpenSSL.BN_new()
            pub_key_y = OpenSSL.BN_new()

            key = OpenSSL.EC_KEY_new_by_curve_name(self.curve)
            if key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
            if (OpenSSL.EC_KEY_generate_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_generate_key FAIL ...")
            if (OpenSSL.EC_KEY_check_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")
            priv_key = OpenSSL.EC_KEY_get0_private_key(key)

            group = OpenSSL.EC_KEY_get0_group(key)
            pub_key = OpenSSL.EC_KEY_get0_public_key(key)

            if (OpenSSL.EC_POINT_get_affine_coordinates_GFp(
                    group, pub_key, pub_key_x, pub_key_y, 0)) == 0:
                raise Exception(
                    "[OpenSSL] EC_POINT_get_affine_coordinates_GFp FAIL ...")

            privkey = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(priv_key))
            pubkeyx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(pub_key_x))
            pubkeyy = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(pub_key_y))
            OpenSSL.BN_bn2bin(priv_key, privkey)
            privkey = privkey.raw
            OpenSSL.BN_bn2bin(pub_key_x, pubkeyx)
            pubkeyx = pubkeyx.raw
            OpenSSL.BN_bn2bin(pub_key_y, pubkeyy)
            pubkeyy = pubkeyy.raw
            self.raw_check_key(privkey, pubkeyx, pubkeyy)

            return privkey, pubkeyx, pubkeyy

        finally:
            OpenSSL.EC_KEY_free(key)
            OpenSSL.BN_free(pub_key_x)
            OpenSSL.BN_free(pub_key_y)