Skip to content

Commit

Permalink
mirage-crypto-ec: provide Dh.secret_to_octets
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Aug 20, 2024
1 parent 56906c0 commit 35aa1be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ec/mirage_crypto_ec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module type Dh = sig
type secret
val secret_of_octets : ?compress:bool -> string ->
(secret * string, error) result
val secret_to_octets : secret -> string
val gen_key : ?compress:bool -> ?g:Mirage_crypto_rng.g -> unit ->
secret * string
val key_exchange : secret -> string -> (string, error) result
Expand Down Expand Up @@ -505,6 +506,9 @@ module Make_dh (Param : Parameters) (P : Point) (S : Scalar) : Dh = struct
| Ok p -> Ok (p, share ?compress p)
| Error _ as e -> e

let secret_to_octets s =
S.to_octets s

let rec generate_private_key ?g () =
let candidate = Mirage_crypto_rng.generate ?g Param.byte_length in
match S.of_octets candidate with
Expand Down Expand Up @@ -953,6 +957,8 @@ module X25519 = struct
else
Error `Invalid_length

let secret_to_octets s = s

let is_zero =
let zero = String.make key_len '\000' in
fun buf -> String.equal zero buf
Expand Down
4 changes: 4 additions & 0 deletions ec/mirage_crypto_ec.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ module type Dh = sig
the shared part will be compressed. May result in an error if the buffer
had an invalid length or was not in bounds. *)

val secret_to_octets : secret -> string
(** [secret_to_octets secret] encodes the provided secret into a freshly
allocated buffer. *)

val gen_key : ?compress:bool -> ?g:Mirage_crypto_rng.g -> unit ->
secret * string
(** [gen_key ~compress ~g ()] generates a private and a public key for
Expand Down

0 comments on commit 35aa1be

Please sign in to comment.