Esempio n. 1
0
    def raw_check_key(self, privkey, pubkey_x, pubkey_y, curve=None):
        if curve == None: curve = self.curve
        elif type(curve) == str: curve = openssl.get_curve(curve)
        else: curve = curve
        try:
            key = openssl.EC_KEY_new_by_curve_name(curve)
            if key == 0:
                raise Exception("[OpenSSL] EC_KEY_new_by_curve_name FAIL ...")
            if privkey != None:
                priv_key = openssl.BN_bin2bn(privkey, len(privkey), 0)
            pub_key_x = openssl.BN_bin2bn(pubkey_x, len(pubkey_x), 0)
            pub_key_y = openssl.BN_bin2bn(pubkey_y, len(pubkey_y), 0)

            if privkey != None:
                if (openssl.EC_KEY_set_private_key(key, priv_key)) == 0:
                    raise Exception("[OpenSSL] EC_KEY_set_private_key FAIL ...")

            group = openssl.EC_KEY_get0_group(key)
            pub_key = openssl.EC_POINT_new(group)

            if (openssl.EC_POINT_set_affine_coordinates_GFp(group, pub_key, pub_key_x, pub_key_y, 0)) == 0:
                raise Exception("[OpenSSL] EC_POINT_set_affine_coordinates_GFp FAIL ...")
            if (openssl.EC_KEY_set_public_key(key, pub_key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_set_public_key FAIL ...")
            if (openssl.EC_KEY_check_key(key)) == 0:
                raise Exception("[OpenSSL] EC_KEY_check_key FAIL ...")
            return 0

        finally:
            openssl.EC_KEY_free(key)
            openssl.BN_free(pub_key_x)
            openssl.BN_free(pub_key_y)
            openssl.EC_POINT_free(pub_key)
            if privkey != None: openssl.BN_free(priv_key)
Esempio n. 2
0
    def __init__(self, pubkey = None, privkey = None, pubkey_x = None, pubkey_y = None, raw_privkey = None, curve = 'sect283r1'):
        """
        For a normal and High level use, specifie pubkey, privkey (if you need) and the curve
        """
        if type(curve) == str:
            self.curve = openssl.get_curve(curve)
        else: self.curve = curve

        if pubkey_x != None and pubkey_y != None:
            self._set_keys(pubkey_x, pubkey_y, raw_privkey)
        elif pubkey != None:
            curve, pubkey_x, pubkey_y, i = ecc._decode_pubkey(pubkey)
            if privkey != None:
                curve2, raw_privkey, i = ecc._decode_privkey(privkey)
                if curve != curve2: raise Exception("Bad ECC keys ...")
            self.curve = curve
            self._set_keys(pubkey_x, pubkey_y, raw_privkey)
        else:
            self.privkey, self.pubkey_x, self.pubkey_y = self._generate()