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
def __set_to_openssl_point(self, point): try: x, y = ec_bignum.BigNum(), ec_bignum.BigNum() # Put X and Y coordinates of public key into x and y vars OpenSSL.EC_POINT_get_affine_coordinates_GFp( self.os_group, point, x.bn, y.bn, None) self.x, self.y = x.get_value(), y.get_value() self.os_point = point finally: del x, y
def __set_to_coordinates(self, x_val, y_val): try: point = OpenSSL.EC_POINT_new(self.os_group) x, y = ec_bignum.BigNum(decval=x_val), ec_bignum.BigNum( decval=y_val) if self.curve.field_type == 'prime': OpenSSL.EC_POINT_set_affine_coordinates_GFp( self.os_group, point, x.bn, y.bn, None) elif self.curve.field_type == 'characteristic-two': OpenSSL.EC_POINT_set_affine_coordinates_GF2m( self.os_group, point, x.bn, y.bn, None) self.x, self.y = x_val, y_val self.os_point = point finally: del x, y
def __mul__(self, other): """ Multiply an EC point by a scalar value and returns the multiplication result """ if isinstance(other, int) or isinstance(other, long): try: o = ec_bignum.BigNum(decval=other) result = OpenSSL.EC_POINT_new(self.os_group) OpenSSL.EC_POINT_mul(self.os_group, result, 0, self.os_point, o.bn, 0) return Point(self.curve, openssl_point=result) finally: del o else: return NotImplemented