Exemplo n.º 1
0
    def _reconstruct_shamirs_secret(
            self, priv_b: Union[UmbralPrivateKey, CurveBN]) -> None:
        g = self._umbral_params.g

        if isinstance(priv_b, UmbralPrivateKey):
            pub_b = priv_b.get_pubkey()
            priv_b = priv_b.bn_key
        else:
            pub_b = priv_b * g

        cfrag_0 = self._attached_cfrags[0]
        id_0 = cfrag_0._kfrag_id
        ni = cfrag_0._point_noninteractive
        xcoord = cfrag_0._point_xcoord

        dh_xcoord = priv_b * xcoord

        blake2b = hashes.Hash(hashes.BLAKE2b(64), backend=backend)
        blake2b.update(xcoord.to_bytes())
        blake2b.update(pub_b.to_bytes())
        blake2b.update(dh_xcoord.to_bytes())
        hashed_dh_tuple = blake2b.finalize()

        if len(self._attached_cfrags) > 1:
            xs = [
                CurveBN.hash(cfrag._kfrag_id,
                             hashed_dh_tuple,
                             params=self._umbral_params)
                for cfrag in self._attached_cfrags
            ]
            x_0 = CurveBN.hash(id_0,
                               hashed_dh_tuple,
                               params=self._umbral_params)
            lambda_0 = lambda_coeff(x_0, xs)
            e = lambda_0 * cfrag_0._point_e1
            v = lambda_0 * cfrag_0._point_v1

            for cfrag in self._attached_cfrags[1:]:
                if (ni, xcoord) != (cfrag._point_noninteractive,
                                    cfrag._point_xcoord):
                    raise ValueError(
                        "Attached CFrags are not pairwise consistent")

                x_i = CurveBN.hash(cfrag._kfrag_id,
                                   hashed_dh_tuple,
                                   params=self._umbral_params)
                lambda_i = lambda_coeff(x_i, xs)
                e = e + (lambda_i * cfrag._point_e1)
                v = v + (lambda_i * cfrag._point_v1)
        else:
            e = cfrag_0._point_e1
            v = cfrag_0._point_v1

        self._point_e_prime = e
        self._point_v_prime = v
        self._point_noninteractive = ni
Exemplo n.º 2
0
def _decapsulate_reencrypted(receiving_privkey: UmbralPrivateKey,
                             capsule: Capsule,
                             key_length: int = DEM_KEYSIZE) -> bytes:
    """Derive the same symmetric encapsulated_key"""

    params = capsule.params

    pub_key = receiving_privkey.get_pubkey().point_key
    priv_key = receiving_privkey.bn_key

    precursor = capsule._attached_cfrags[0]._point_precursor
    dh_point = priv_key * precursor

    from constant_sorrow import constants

    # Combination of CFrags via Shamir's Secret Sharing reconstruction
    if len(capsule._attached_cfrags) > 1:
        xs = [
            CurveBN.hash(precursor,
                         pub_key,
                         dh_point,
                         bytes(constants.X_COORDINATE),
                         cfrag._kfrag_id,
                         params=params) for cfrag in capsule._attached_cfrags
        ]

        e_summands, v_summands = list(), list()
        for cfrag, x in zip(capsule._attached_cfrags, xs):
            if precursor != cfrag._point_precursor:
                raise ValueError("Attached CFrags are not pairwise consistent")

            lambda_i = lambda_coeff(x, xs)
            e_summands.append(lambda_i * cfrag._point_e1)
            v_summands.append(lambda_i * cfrag._point_v1)

        e_prime = sum(e_summands[1:], e_summands[0])
        v_prime = sum(v_summands[1:], v_summands[0])
    else:
        e_prime = capsule._attached_cfrags[0]._point_e1
        v_prime = capsule._attached_cfrags[0]._point_v1

    # Secret value 'd' allows to make Umbral non-interactive
    d = CurveBN.hash(precursor,
                     pub_key,
                     dh_point,
                     bytes(constants.NON_INTERACTIVE),
                     params=params)

    e, v, s = capsule.components()
    h = CurveBN.hash(e, v, params=params)

    orig_pub_key = capsule.get_correctness_keys(
    )['delegating'].point_key  # type: ignore

    if not (s / d) * orig_pub_key == (h * e_prime) + v_prime:
        raise GenericUmbralError()

    shared_key = d * (e_prime + v_prime)
    encapsulated_key = kdf(shared_key, key_length)
    return encapsulated_key
Exemplo n.º 3
0
    def _reconstruct_shamirs_secret(self,
                                    pub_a: Union[UmbralPublicKey, Point],
                                    priv_b: Union[UmbralPrivateKey, BigNum],
                                    params: UmbralParameters = None) -> None:

        params = params if params is not None else default_params()

        if isinstance(priv_b, UmbralPrivateKey):
            priv_b = priv_b.bn_key

        if isinstance(pub_a, UmbralPublicKey):
            pub_a = pub_a.point_key

        g = params.g
        pub_b = priv_b * g
        g_ab = priv_b * pub_a

        blake2b = hashes.Hash(hashes.BLAKE2b(64), backend=backend)
        blake2b.update(pub_a.to_bytes())
        blake2b.update(pub_b.to_bytes())
        blake2b.update(g_ab.to_bytes())
        hashed_dh_tuple = blake2b.finalize()

        id_cfrag_pairs = list(self._attached_cfrags.items())
        id_0, cfrag_0 = id_cfrag_pairs[0]
        x_0 = BigNum.hash_to_bn(id_0, hashed_dh_tuple, params=params)
        if len(id_cfrag_pairs) > 1:
            xs = [
                BigNum.hash_to_bn(_id, hashed_dh_tuple, params=params)
                for _id in self._attached_cfrags.keys()
            ]
            lambda_0 = lambda_coeff(x_0, xs)
            e = lambda_0 * cfrag_0.point_eph_e1
            v = lambda_0 * cfrag_0.point_eph_v1

            for id_i, cfrag in id_cfrag_pairs[1:]:
                x_i = BigNum.hash_to_bn(id_i, hashed_dh_tuple, params=params)
                lambda_i = lambda_coeff(x_i, xs)
                e = e + (lambda_i * cfrag.point_eph_e1)
                v = v + (lambda_i * cfrag.point_eph_v1)
        else:
            e = cfrag_0.point_eph_e1
            v = cfrag_0.point_eph_v1

        self._point_eph_e_prime = e
        self._point_eph_v_prime = v
        self._point_noninteractive = cfrag_0.point_eph_ni
def recover_secret(shares):
    points = [share[0] for share in shares]
    summands = []
    for point, value in shares:
        lambda_i = utils.lambda_coeff(point, points)
        summands.append(lambda_i * value)

    return sum(summands[1:], summands[0])
Exemplo n.º 5
0
    def _reconstruct_shamirs_secret(self, 
                                    pub_a: Union[UmbralPublicKey, Point], 
                                    priv_b: Union[UmbralPrivateKey, CurveBN],
                                    params: UmbralParameters=None) -> None:

        params = params if params is not None else default_params()

        if isinstance(priv_b, UmbralPrivateKey):
            priv_b = priv_b.bn_key

        if isinstance(pub_a, UmbralPublicKey):
            pub_a = pub_a.point_key

        g = params.g
        pub_b = priv_b * g
        g_ab = priv_b * pub_a

        blake2b = hashes.Hash(hashes.BLAKE2b(64), backend=backend)
        blake2b.update(pub_a.to_bytes())
        blake2b.update(pub_b.to_bytes())
        blake2b.update(g_ab.to_bytes())
        hashed_dh_tuple = blake2b.finalize()

        cfrag_0 = self._attached_cfrags[0]
        id_0 = cfrag_0._bn_kfrag_id
        x_0 = CurveBN.hash(id_0, hashed_dh_tuple, params=params)
        if len(self._attached_cfrags) > 1:
            xs = [CurveBN.hash(cfrag._bn_kfrag_id, hashed_dh_tuple, params=params)
                    for cfrag in self._attached_cfrags]
            lambda_0 = lambda_coeff(x_0, xs)
            e = lambda_0 * cfrag_0._point_e1
            v = lambda_0 * cfrag_0._point_v1

            for cfrag in self._attached_cfrags[1:]:
                x_i = CurveBN.hash(cfrag._bn_kfrag_id, hashed_dh_tuple, params=params)
                lambda_i = lambda_coeff(x_i, xs)
                e = e + (lambda_i * cfrag._point_e1)
                v = v + (lambda_i * cfrag._point_v1)
        else:
            e = cfrag_0._point_e1
            v = cfrag_0._point_v1

        self._point_e_prime = e
        self._point_v_prime = v
        self._point_noninteractive = cfrag_0._point_noninteractive
Exemplo n.º 6
0
    def _reconstruct_shamirs_secret(self) -> None:
        id_cfrag_pairs = list(self._attached_cfrags.items())
        id_0, cfrag_0 = id_cfrag_pairs[0]
        if len(id_cfrag_pairs) > 1:
            ids = self._attached_cfrags.keys()
            lambda_0 = lambda_coeff(id_0, ids)
            e = lambda_0 * cfrag_0.point_eph_e1
            v = lambda_0 * cfrag_0.point_eph_v1

            for id_i, cfrag in id_cfrag_pairs[1:]:
                lambda_i = lambda_coeff(id_i, ids)
                e = e + (lambda_i * cfrag.point_eph_e1)
                v = v + (lambda_i * cfrag.point_eph_v1)
        else:
            e = cfrag_0.point_eph_e1
            v = cfrag_0.point_eph_v1

        self._point_eph_e_prime = e
        self._point_eph_v_prime = v
        self._point_noninteractive = cfrag_0.point_eph_ni