Module Nocrypto.Dh

Diffie-Hellman, MODP version.

Diffie-Hellman key exchange

exception Invalid_public_key

Raised if the public key is degenerate. Implies either badly malfunctioning DH on the other side, or an attack attempt.

type group = {
p : Z.t;

modulus

gg : Z.t;

generator

q : Z.t option;

subgroup order; potentially unknown

}

A DH group.

Sexplib convertible.

type secret = private {
x : Z.t;
}

A private secret.

Sexplib convertible.

val modulus_size : group -> int

Bit size of the modulus.

val key_of_secret : group -> s:Cstruct.t -> secret * Cstruct.t

key_of_secret group s is the secret and the corresponding public key which use s as the secret exponent.

raises Invalid_public_key

if s is degenerate.

val gen_key : ?⁠g:Rng.g -> ?⁠bits:int -> group -> secret * Cstruct.t

Generate a random secret and the corresponding public key. bits is the exact bit-size of secret and defaults to a value dependent on the group's p.

val shared : group -> secret -> Cstruct.t -> Cstruct.t option

shared group secret message is Some key, the shared key, given a group, a previously generated secret and the other party's public message. It is None if message is degenerate.

val gen_group : ?⁠g:Rng.g -> int -> group

gen_group bits generates a random group with modulus size bits. Uses a safe prime p = 2q + 1 (with q prime) for the modulus and 2 for the generator, such that 2^q = 1 mod p. Runtime is on the order of minute for 1024 bits.

raises Invalid_argument

if bits is ridiculously small.

module Group : sig ... end

A small catalog of standardized groups.