Esempio n. 1
0
    def __init__(self, bignum, curve: Curve):
        on_curve = openssl._bn_is_on_curve(bignum, curve)
        if not on_curve:
            raise ValueError("The provided BIGNUM is not on the provided curve.")

        self.bignum = bignum
        self.curve = curve
Esempio n. 2
0
    def __init__(self, bignum, curve_nid, group, order):

        if curve_nid:
            on_curve = openssl._bn_is_on_curve(bignum, curve_nid)
            if not on_curve:
                raise ValueError("The provided BIGNUM is not on the provided curve.")

        self.bignum = bignum
        self.curve_nid = curve_nid
        self.group = group
        self.order = order
Esempio n. 3
0
    def gen_rand(cls, curve: Optional[Curve] = None) -> 'CurveBN':
        """
        Returns a CurveBN object with a cryptographically secure OpenSSL BIGNUM
        based on the given curve.
        By default, the underlying OpenSSL BIGNUM has BN_FLG_CONSTTIME set for
        constant time operations.
        """
        curve = curve if curve is not None else default_curve()

        new_rand_bn = openssl._get_new_BN()
        rand_res = backend._lib.BN_rand_range(new_rand_bn, curve.order)
        backend.openssl_assert(rand_res == 1)

        if not openssl._bn_is_on_curve(new_rand_bn, curve):
            new_rand_bn = cls.gen_rand(curve=curve)
            return new_rand_bn

        return cls(new_rand_bn, curve)
Esempio n. 4
0
    def gen_rand(cls, curve: ec.EllipticCurve = None):
        """
        Returns a CurveBN object with a cryptographically secure OpenSSL BIGNUM
        based on the given curve.
        By default, the underlying OpenSSL BIGNUM has BN_FLG_CONSTTIME set for
        constant time operations.
        """
        curve = curve if curve is not None else default_curve()
        curve_nid = backend._elliptic_curve_to_nid(curve)

        group = openssl._get_ec_group_by_curve_nid(curve_nid)
        order = openssl._get_ec_order_by_curve_nid(curve_nid)

        new_rand_bn = openssl._get_new_BN()
        rand_res = backend._lib.BN_rand_range(new_rand_bn, order)
        backend.openssl_assert(rand_res == 1)

        if not openssl._bn_is_on_curve(new_rand_bn, curve_nid):
            new_rand_bn = cls.gen_rand(curve=curve)
            return new_rand_bn

        return cls(new_rand_bn, curve_nid, group, order)