Exemplo n.º 1
0
    def hash(cls, *crypto_items, params: UmbralParameters) -> 'CurveBN':
        # 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 = openssl._bytes_to_bn(blake2b.finalize())

        _1 = backend._lib.BN_value_one()

        order_minus_1 = openssl._get_new_BN()
        res = backend._lib.BN_sub(order_minus_1, params.curve.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, params.curve)
Exemplo n.º 2
0
    def from_bytes(cls, data: bytes, curve: Optional[Curve] = None) -> 'CurveBN':
        """
        Returns a CurveBN object from the given byte data that's within the size
        of the provided curve's order.
        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()

        size = backend._lib.BN_num_bytes(curve.order)
        if len(data) != size:
            raise ValueError("Expected {} B for CurveBNs".format(size))
        bignum = openssl._bytes_to_bn(data)
        return cls(bignum, curve)
Exemplo n.º 3
0
def hash_to_curvebn(*crypto_items,
                    params: UmbralParameters,
                    customization_string: bytes = b'',
                    hash_class: Type[Hash] = Blake2b) -> CurveBN:

    customization_string = b'hash_to_curvebn' + customization_string
    hash_function = hash_class(customization_string=customization_string)

    for item in crypto_items:
        try:
            item_bytes = item.to_bytes()
        except AttributeError:
            if isinstance(item, bytes):
                item_bytes = item
            else:
                raise TypeError("Input with type {} not accepted".format(
                    type(item)))
        hash_function.update(item_bytes)

    hash_digest = openssl._bytes_to_bn(hash_function.finalize())

    one = backend._lib.BN_value_one()

    order_minus_1 = openssl._get_new_BN()
    res = backend._lib.BN_sub(order_minus_1, params.curve.order, one)
    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, one)
    backend.openssl_assert(res == 1)

    return CurveBN(bignum, params.curve)