def __init__(self, rawPublicKey, ec_key=None): self.ec_key = None # In case of early destruction assert (rawPublicKey is not None) assert (len(rawPublicKey) == 64) self.rawPublicKey = rawPublicKey if ec_key: self.ec_key = o.EC_KEY_dup(ec_key) else: self.ec_key = self._constructEcFromRawKey(self.rawPublicKey)
def _constructEcFromRawKey(self, rawPrivateKey): privBignum, ec_key = None, None try: ec_key = o.EC_KEY_new_by_curve_name(o.OBJ_txt2nid(b"prime256v1")) privBuf = bytesToC(rawPrivateKey) privBignum = o.BN_new() o.BN_bin2bn(privBuf, 32, privBignum) o.EC_KEY_set_private_key(ec_key, privBignum) return o.EC_KEY_dup(ec_key) finally: if privBignum: o.BN_free(privBignum) if ec_key: o.EC_KEY_free(ec_key)
def _constructEcFromRawKey(self, rawPublicKey): ec_key, ec_group, ec_point = None, None, None try: ec_key = o.EC_KEY_new_by_curve_name(o.OBJ_txt2nid(b"prime256v1")) ec_group = o.EC_GROUP_new_by_curve_name( o.OBJ_txt2nid(b"prime256v1")) ec_point = o.EC_POINT_new(ec_group) # Add 0x04 byte to signal "uncompressed" public key pubBuf = bytesToC(bytearray([0x04]) + rawPublicKey) o.EC_POINT_oct2point(ec_group, ec_point, pubBuf, 65, None) o.EC_KEY_set_public_key(ec_key, ec_point) return o.EC_KEY_dup(ec_key) finally: if ec_key: o.EC_KEY_free(ec_key) if ec_group: o.EC_GROUP_free(ec_group) if ec_point: o.EC_POINT_free(ec_point)