def get_order_from_curve(cls, curve: ec.EllipticCurve=None): """ Returns the order from the given curve as a CurveBN. """ curve = curve if curve is not None else default_curve() try: curve_nid = backend._elliptic_curve_to_nid(curve) except AttributeError: # Presume that the user passed in the curve_nid curve_nid = curve group = openssl._get_ec_group_by_curve_nid(curve_nid) order = openssl._get_ec_order_by_curve_nid(curve_nid) return CurveBN(order, curve_nid, group, order)
def __init__(self, curve: ec.EllipticCurve): from umbral.point import Point, unsafe_hash_to_point from umbral.utils import get_curve_keysize_bytes self.curve = curve curve_nid = backend._elliptic_curve_to_nid(curve) self.g = Point.get_generator_from_curve(self.curve) self.order = openssl._get_ec_order_by_curve_nid(curve_nid) g_bytes = self.g.to_bytes(is_compressed=True) self.CURVE_KEY_SIZE_BYTES = get_curve_keysize_bytes(self.curve) parameters_seed = b'NuCypherKMS/UmbralParameters/' self.u = unsafe_hash_to_point(g_bytes, self, parameters_seed + b'u')
def hash(cls, *crypto_items, params=None): params = params if params is not None else default_params() curve_nid = backend._elliptic_curve_to_nid(params.curve) order = openssl._get_ec_order_by_curve_nid(curve_nid) group = openssl._get_ec_group_by_curve_nid(curve_nid) # TODO: Clean this in an upcoming cleanup of pyUmbral blake2b = hashes.Hash(hashes.BLAKE2b(64), backend=backend) for item in crypto_items: try: item_bytes = item.to_bytes() except AttributeError: if isinstance(item, bytes): item_bytes = item else: raise TypeError( "{} is not acceptable type, received {}".format( item, type(item))) blake2b.update(item_bytes) hash_digest = blake2b.finalize() hash_digest = int.from_bytes(hash_digest, byteorder='big', signed=False) hash_digest = openssl._int_to_bn(hash_digest) _1 = backend._lib.BN_value_one() order_minus_1 = openssl._get_new_BN() res = backend._lib.BN_sub(order_minus_1, order, _1) backend.openssl_assert(res == 1) bignum = openssl._get_new_BN() with backend._tmp_bn_ctx() as bn_ctx: res = backend._lib.BN_mod(bignum, hash_digest, order_minus_1, bn_ctx) backend.openssl_assert(res == 1) res = backend._lib.BN_add(bignum, bignum, _1) backend.openssl_assert(res == 1) return cls(bignum, curve_nid, group, order)
def from_int(cls, num, curve: ec.EllipticCurve = None): """ Returns a CurveBN object from a given integer on a 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() try: curve_nid = backend._elliptic_curve_to_nid(curve) except AttributeError: # Presume that the user passed in the curve_nid curve_nid = curve group = openssl._get_ec_group_by_curve_nid(curve_nid) order = openssl._get_ec_order_by_curve_nid(curve_nid) conv_bn = openssl._int_to_bn(num, curve_nid) return cls(conv_bn, curve_nid, group, order)
def to_bytes(self, is_compressed=True): """ Returns the Point serialized as bytes. It will return a compressed form if is_compressed is set to True. """ affine_x, affine_y = self.to_affine() # Get size of curve via order order = openssl._get_ec_order_by_curve_nid(self.curve_nid) key_size = backend._lib.BN_num_bytes(order) if is_compressed: y_bit = (affine_y & 1) + 2 data = int.to_bytes(y_bit, 1, 'big') data += int.to_bytes(affine_x, key_size, 'big') else: data = b'\x04' data += int.to_bytes(affine_x, key_size, 'big') data += int.to_bytes(affine_y, key_size, 'big') return data
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)