Skip to content

Commit

Permalink
Merge pull request #208 from hannesm/pk-no-sexp
Browse files Browse the repository at this point in the history
mirage-crypto-pk: remove s-expression converters and sexplib0 dependency
  • Loading branch information
hannesm authored Feb 28, 2024
2 parents 9cb2ebd + 8b6f90c commit ec3e5ba
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 185 deletions.
1 change: 0 additions & 1 deletion mirage-crypto-pk.opam
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ depends: [
"cstruct" {>="6.00"}
"mirage-crypto" {=version}
"mirage-crypto-rng" {=version}
"sexplib0"
"zarith" {>= "1.4"}
"eqaf" {>= "0.8"}
]
Expand Down
4 changes: 0 additions & 4 deletions pk/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ let rec until p f = let r = f () in if p r then r else until p f
let guard p err = if p then Ok () else Error err

let ( let* ) = Result.bind

open Sexplib0.Sexp_conv
let sexp_of_z z = sexp_of_string (Z.to_string z)
let z_of_sexp s = Z.of_string (string_of_sexp s)
50 changes: 0 additions & 50 deletions pk/dh.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Mirage_crypto.Uncommon
open Sexplib0.Sexp_conv

open Common

Expand All @@ -11,25 +10,6 @@ type group = {
q : Z.t option ; (* `gg`'s order, maybe *)
}

let sexp_of_group g =
Sexplib0.Sexp.List
[
sexp_of_pair sexp_of_string sexp_of_z ("p", g.p) ;
sexp_of_pair sexp_of_string sexp_of_z ("gg", g.gg) ;
sexp_of_pair sexp_of_string (sexp_of_option sexp_of_z) ("q", g.q)
]

let group_of_sexp = function
| Sexplib0.Sexp.List [ p ; gg ; q ] as s ->
let p_str, p = pair_of_sexp string_of_sexp z_of_sexp p in
let gg_str, gg = pair_of_sexp string_of_sexp z_of_sexp gg in
let q_str, q = pair_of_sexp string_of_sexp (option_of_sexp z_of_sexp) q in
if p_str = "p" && gg_str = "gg" && q_str = "q" then
{ p ; gg ; q }
else
raise (Of_sexp_error (Failure "expected p, gg, and q", s))
| s -> raise (Of_sexp_error (Failure "expected a list with 3 elements", s))

let group ~p ~gg ?q () =
let* () =
guard (Z.(p > zero && is_odd p) && Z_extra.pseudoprime p)
Expand All @@ -40,38 +20,8 @@ let group ~p ~gg ?q () =
in
Ok { p ; gg ; q }

let group_of_sexp s =
let g = group_of_sexp s in
match group ~p:g.p ~gg:g.gg ?q:g.q () with
| Error (`Msg m) -> invalid_arg "bad group: %s" m
| Ok g -> g

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

let sexp_of_secret s =
Sexplib0.Sexp.List
[
sexp_of_pair sexp_of_string sexp_of_group ("group", s.group) ;
sexp_of_pair sexp_of_string sexp_of_z ("x", s.x) ;
]

let secret_of_sexp = function
| Sexplib0.Sexp.List [ group ; x ] as s ->
let group_str, group = pair_of_sexp string_of_sexp group_of_sexp group in
let x_str, x = pair_of_sexp string_of_sexp z_of_sexp x in
if group_str = "group" && x_str = "x" then
{ group ; x }
else
raise (Of_sexp_error (Failure "expected group and x", s))
| s -> raise (Of_sexp_error (Failure "expected a list with 2 elements", s))

let secret_of_sexp sexp =
let s = secret_of_sexp sexp in
if Z.(one < s.x && s.x < s.group.p) then
s
else
invalid_arg "bad secret"

(*
* Estimates of equivalent-strength exponent sizes for the moduli sizes.
* 2048-8192 are taken from "Negotiated FF DHE Parameters for TLS."
Expand Down
31 changes: 0 additions & 31 deletions pk/dsa.ml
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
open Mirage_crypto.Uncommon
open Sexplib0.Sexp_conv

open Common

type pub = { p : Z.t ; q : Z.t ; gg : Z.t ; y : Z.t }

let sexp_of_pub { p ; q ; gg ; y } =
sexp_of_list (sexp_of_pair sexp_of_string sexp_of_z)
[ "p", p; "q", q; "gg", gg; "y", y ]

let pub_of_sexp s =
match list_of_sexp (pair_of_sexp string_of_sexp z_of_sexp) s with
| [ "p", p; "q", q; "gg", gg; "y", y ] -> { p ; q ; gg ; y }
| _ -> raise (Of_sexp_error (Failure "expected p, q, gg, and y'", s))

let pub ?(fips = false) ~p ~q ~gg ~y () =
let* () = guard Z.(one < gg && gg < p) (`Msg "bad generator") in
let* () = guard (Z_extra.pseudoprime q) (`Msg "q is not prime") in
Expand All @@ -31,36 +21,15 @@ let pub ?(fips = false) ~p ~q ~gg ~y () =
in
Ok { p ; q ; gg ; y }

let pub_of_sexp s =
let p = pub_of_sexp s in
match pub ?fips:None ~p:p.p ~q:p.q ~gg:p.gg ~y:p.y () with
| Ok p -> p
| Error (`Msg m) -> invalid_arg "bad public %s" m

type priv =
{ p : Z.t ; q : Z.t ; gg : Z.t ; x : Z.t ; y : Z.t }

let sexp_of_priv { p ; q ; gg ; x ; y } =
sexp_of_list (sexp_of_pair sexp_of_string sexp_of_z)
[ "p", p; "q", q; "gg", gg; "x", x; "y", y ]

let priv_of_sexp s =
match list_of_sexp (pair_of_sexp string_of_sexp z_of_sexp) s with
| [ "p", p; "q", q; "gg", gg; "x", x; "y", y ] -> { p ; q ; gg ; x ; y }
| _ -> raise (Of_sexp_error (Failure "expected p, q, gg, x, and y'", s))

let priv ?fips ~p ~q ~gg ~x ~y () =
let* _ = pub ?fips ~p ~q ~gg ~y () in
let* () = guard Z.(zero < x && x < q) (`Msg "x not in 1..q-1") in
let* () = guard Z.(y = powm gg x p) (`Msg "y <> g ^ x mod p") in
Ok { p ; q ; gg ; x ; y }

let priv_of_sexp s =
let p = priv_of_sexp s in
match priv ?fips:None ~p:p.p ~q:p.q ~gg:p.gg ~x:p.x ~y:p.y () with
| Ok p -> p
| Error (`Msg m) -> invalid_arg "bad private %s" m

let pub_of_priv { p; q; gg; y; _ } = { p; q; gg; y }

type keysize = [ `Fips1024 | `Fips2048 | `Fips3072 | `Exactly of int * int ]
Expand Down
2 changes: 1 addition & 1 deletion pk/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name mirage_crypto_pk)
(public_name mirage-crypto-pk)
(libraries cstruct zarith mirage-crypto mirage-crypto-rng sexplib0 eqaf.cstruct)
(libraries cstruct zarith mirage-crypto mirage-crypto-rng eqaf.cstruct)
(private_modules common dh dsa rsa z_extra))
51 changes: 6 additions & 45 deletions pk/mirage_crypto_pk.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ module Rsa : sig
e : Z.t ; (** Public exponent *)
n : Z.t ; (** Modulus *)
}
(** The public portion of the key.
{e [Sexplib] convertible}. *)
(** The public portion of the key. *)

val pub : e:Z.t -> n:Z.t -> (pub, [> `Msg of string ]) result
(** [pub ~e ~n] validates the public key: [1 < e < n], [n > 0],
Expand All @@ -55,9 +53,7 @@ module Rsa : sig
Some systems assume otherwise. When using keys produced by a system that
computes [u = p^(-1) mod q], either exchange [p] with [q] and [dp] with
[dq], or re-generate the full private key using
{{!priv_of_primes}[priv_of_primes]}.
{e [Sexplib] convertible}. *)
{{!priv_of_primes}[priv_of_primes]}. *)

val priv : e:Z.t -> d:Z.t -> n:Z.t -> p:Z.t -> q:Z.t -> dp:Z.t -> dq:Z.t ->
q':Z.t -> (priv, [> `Msg of string ]) result
Expand Down Expand Up @@ -282,15 +278,6 @@ module Rsa : sig
@raise Invalid_argument if message is a [`Digest] of the wrong size. *)
end

(**/**)
val pub_of_sexp : Sexplib0.Sexp.t -> pub
val sexp_of_pub : pub -> Sexplib0.Sexp.t

val priv_of_sexp : Sexplib0.Sexp.t -> priv
val sexp_of_priv : priv -> Sexplib0.Sexp.t
(**/**)

end


Expand All @@ -306,9 +293,7 @@ module Dsa : sig
x : Z.t ; (** Private key proper *)
y : Z.t ; (** Public component *)
}
(** Private key. [p], [q] and [gg] comprise {i domain parameters}.
{e [Sexplib] convertible}. *)
(** Private key. [p], [q] and [gg] comprise {i domain parameters}. *)

val priv : ?fips:bool -> p:Z.t -> q:Z.t -> gg:Z.t -> x:Z.t -> y:Z.t -> unit ->
(priv, [> `Msg of string ]) result
Expand All @@ -323,9 +308,7 @@ module Dsa : sig
gg : Z.t ;
y : Z.t ;
}
(** Public key, a subset of {{!type-priv}private key}.
{e [Sexplib] convertible}. *)
(** Public key, a subset of {{!type-priv}private key}. *)

val pub : ?fips:bool -> p:Z.t -> q:Z.t -> gg:Z.t -> y:Z.t -> unit ->
(pub, [> `Msg of string ]) result
Expand Down Expand Up @@ -396,15 +379,6 @@ module Dsa : sig
(** [generate key digest] deterministically takes the given private key and
message digest to a [k] suitable for seeding the signing process. *)
end

(**/**)
val pub_of_sexp : Sexplib0.Sexp.t -> pub
val sexp_of_pub : pub -> Sexplib0.Sexp.t

val priv_of_sexp : Sexplib0.Sexp.t -> priv
val sexp_of_priv : priv -> Sexplib0.Sexp.t
(**/**)

end


Expand All @@ -425,19 +399,15 @@ module Dh : sig
gg : Z.t ; (** generator *)
q : Z.t option ; (** subgroup order; potentially unknown *)
}
(** A DH group.
{e [Sexplib] convertible}. *)
(** A DH group. *)

val group : p:Z.t -> gg:Z.t -> ?q:Z.t -> unit ->
(group, [> `Msg of string ]) result
(** [group ~p ~gg ~q ()] constructs a group if [p] is odd, a prime number,
and greater than [zero]. [gg] must be in the range [1 < gg < p]. *)

type secret = private { group : group ; x : Z.t }
(** A private key.
{e [Sexplib] convertible.} *)
(** A private key. *)

val modulus_size : group -> bits
(** Bit size of the modulus. *)
Expand Down Expand Up @@ -505,15 +475,6 @@ module Dh : sig
val ffdhe8192 : group

end

(**/**)
val group_of_sexp : Sexplib0.Sexp.t -> group
val sexp_of_group : group -> Sexplib0.Sexp.t

val secret_of_sexp : Sexplib0.Sexp.t -> secret
val sexp_of_secret : secret -> Sexplib0.Sexp.t
(**/**)

end

(** {b Z} Convert Z to big endian Cstruct.t and generate random Z values. *)
Expand Down
33 changes: 0 additions & 33 deletions pk/rsa.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Mirage_crypto.Uncommon
open Sexplib0.Sexp_conv

open Common

Expand Down Expand Up @@ -35,15 +34,6 @@ exception Insufficient_key

type pub = { e : Z.t ; n : Z.t }

let sexp_of_pub { e ; n } =
sexp_of_list (sexp_of_pair sexp_of_string sexp_of_z)
[ "e", e ; "n" , n ]

let pub_of_sexp s =
match list_of_sexp (pair_of_sexp string_of_sexp z_of_sexp) s with
| [ "e", e ; "n", n ] -> { e ; n }
| _ -> raise (Of_sexp_error (Failure "expected e and n", s))

(* due to PKCS1 *)
let minimum_octets = 12
let minimum_bits = 8 * minimum_octets - 7
Expand All @@ -65,28 +55,11 @@ let pub ~e ~n =
these are not requirements, neither for RSA nor for powm_sec *)
Ok { e ; n }

let pub_of_sexp s =
let p = pub_of_sexp s in
match pub ~e:p.e ~n:p.n with
| Ok p -> p
| Error (`Msg m) -> failwith "bad public key: %s" m

type priv = {
e : Z.t ; d : Z.t ; n : Z.t ;
p : Z.t ; q : Z.t ; dp : Z.t ; dq : Z.t ; q' : Z.t
}

let sexp_of_priv { e ; d ; n ; p ; q ; dp ; dq ; q' } =
sexp_of_list (sexp_of_pair sexp_of_string sexp_of_z)
[ "e", e; "d", d; "n", n; "p", p; "q", q; "dp", dp; "dq", dq; "q'", q' ]

let priv_of_sexp s =
match list_of_sexp (pair_of_sexp string_of_sexp z_of_sexp) s with
| [ "e", e; "d", d; "n", n; "p", p; "q", q; "dp", dp; "dq", dq; "q'", q' ] ->
{ e ; d ; n ; p ; q ; dp ; dq ; q' }
| _ ->
raise (Of_sexp_error (Failure "expected e, d, n, p, q, dp, dq, and q'", s))

let valid_prime name p =
guard Z.(p > zero && is_odd p && Z_extra.pseudoprime p)
(`Msg ("invalid prime " ^ name))
Expand Down Expand Up @@ -120,12 +93,6 @@ let priv ~e ~d ~n ~p ~q ~dp ~dq ~q' =
in
Ok { e ; d ; n ; p ; q ; dp ; dq ; q' }

let priv_of_sexp s =
let p = priv_of_sexp s in
match priv ~e:p.e ~d:p.d ~n:p.n ~p:p.p ~q:p.q ~dp:p.dp ~dq:p.dq ~q':p.q' with
| Error (`Msg m) -> failwith "invalid private key %s" m
| Ok p -> p

let priv_of_primes ~e ~p ~q =
let* () = valid_prime "p" p in
let* () = valid_prime "q" q in
Expand Down
Loading

0 comments on commit ec3e5ba

Please sign in to comment.