package hacl_x25519

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Diffie-Hellman key exchange over Curve25519 (also known as X25519).

This implementation uses C code from Project Everest, an effort to build and deploy a verified HTTPS stack.

type secret

Key material. In elliptic curve terms, a scalar.

To generate a key pair, use gen_key.

In the usual setting, the private key only be generated and used for key exchange. But it can be useful to create values of type secret with a known value, for example to check against test vectors. One can use the following pattern to do this:

let (secret, _) = gen_key ~rng:(fun _ -> known_data) 
val gen_key : rng:(int -> Cstruct.t) -> secret * Cstruct.t

Generate a key pair. rng should return a Cstruct.t with the specified key length (in bytes) and fill it with random bytes.

If the cstruct returned by rng does not have the correct length, raises Failure _.

type error = [
  1. | `Invalid_length
  2. | `Low_order
]

Kind of errors.

val pp_error : Stdlib.Format.formatter -> error -> unit

Pretty printer for errors

val key_exchange : secret -> Cstruct.t -> (Cstruct.t, error) Stdlib.result

Perform Diffie-Hellman key exchange between a private part and a public part.

It checks length of the pub key and returns an error if it has an incorrect length.

In DH terms, the private part corresponds to a scalar, and the public part corresponds to a point, and this computes the scalar multiplication.

The resulting shared secret is not truncated.

As described in RFC 7748, section 6.1, this function might internally generate an all-zero value. If this is the case Error `Low_order will be returned instead. This check is necessary in the context of TLS 1.3, but might not in other protocols.

OCaml

Innovation. Community. Security.