package mirage-crypto

  1. Overview
  2. Docs
type key
val of_secret : string -> key

Construct the encryption key corresponding to secret.

  • raises Invalid_argument

    if the length of secret is not in key_sizes.

val key_sizes : int array

Key sizes allowed with this cipher.

val block_size : int

The size of a single block.

type ctr = int64
val add_ctr : ctr -> int64 -> ctr

add_ctr ctr n adds n to ctr.

val next_ctr : ?off:int -> string -> ctr:ctr -> ctr

next_ctr ~off msg ~ctr is the state of the counter after encrypting or decrypting msg at offset off with the counter ctr.

For protocols which perform inter-message chaining, this is the counter for the next message.

It is computed as C.add ctr (ceil (len msg / block_size)). Note that if len msg1 = k * block_size,

encrypt ~ctr msg1 || encrypt ~ctr:(next_ctr ~ctr msg1) msg2
== encrypt ~ctr (msg1 || msg2)
val ctr_of_octets : string -> ctr

ctr_of_octets buf converts the value of buf into a counter.

val stream : key:key -> ctr:ctr -> int -> string

stream ~key ~ctr n is the raw keystream.

Keystream is the concatenation of successive encrypted counter states. If E(x) is the single block x encrypted under key, then keystream is the first n bytes of E(ctr) || E(add ctr 1) || E(add ctr 2) || ....

Note that

stream ~key ~ctr (k * block_size) || stream ~key ~ctr:(add ctr k) x
== stream ~key ~ctr (k * block_size + x)

In other words, it is possible to restart a keystream at block_size boundaries by manipulating the counter.

val encrypt : key:key -> ctr:ctr -> string -> string

encrypt ~key ~ctr msg is stream ~key ~ctr (len msg) lxor msg.

val decrypt : key:key -> ctr:ctr -> string -> string

decrypt is encrypt.

val stream_into : key:key -> ctr:ctr -> bytes -> off:int -> int -> unit

stream_into ~key ~ctr dst ~off len is the raw key stream put into dst starting at off.

  • raises Invalid_argument

    if Bytes.length dst - off < len.

val encrypt_into : key:key -> ctr:ctr -> string -> src_off:int -> bytes -> dst_off:int -> int -> unit

encrypt_into ~key ~ctr src ~src_off dst ~dst_off len produces the key stream into dst at dst_off, and then xors it with src at src_off.

  • raises Invalid_argument

    if dst_off < 0 || Bytes.length dst - dst_off < len.

  • raises Invalid_argument

    if src_off < 0 || String.length src - src_off < len.

val decrypt_into : key:key -> ctr:ctr -> string -> src_off:int -> bytes -> dst_off:int -> int -> unit

decrypt_into is encrypt_into.

OCaml

Innovation. Community. Security.