split binary_types + clean up

This commit is contained in:
swrup 2025-11-21 19:29:04 +01:00
parent 9fe1d76dc4
commit 0c5622324c
8 changed files with 449 additions and 515 deletions

View file

@ -10,7 +10,11 @@ module EddsaPublicKey = struct
type t = pub
let to_octets t = pub_to_octets t
let of_octets t = pub_of_octets t
let of_octets t = pub_of_octets t |> Result.get_ok
let bin =
let open Bin in
map (bytes 32) of_octets to_octets
let of_b32 s =
let open Syntax in
@ -23,14 +27,42 @@ module EddsaPublicKey = struct
let jsont = Jsont.of_of_string ~kind:"EddsaPublicKey" of_b32 ~enc:to_b32
end
module EddsaPrivateKey = struct
(* EdDSA and ECDHE public keys always point on Curve25519
and represented using the standard 256 bits Ed25519 compact format,
converted to Crockford Base32. *)
open Mirage_crypto_ec.Ed25519
type t = priv
let to_octets t = priv_to_octets t
let of_octets t = priv_of_octets t |> Result.get_ok
let bin =
let open Bin in
map (bytes 32) of_octets to_octets
let of_b32 s =
let open Syntax in
let* octets = B32.decode s in
match priv_of_octets octets with
| Error e -> Fmt.error "%a" Mirage_crypto_ec.pp_error e
| Ok priv -> Ok priv
let to_b32 t = B32.encode (to_octets t)
let jsont = Jsont.of_of_string ~kind:"EddsaPrivateKey" of_b32 ~enc:to_b32
end
module EddsaSignature : sig
type t
val to_octets : t -> string
val sign : key:Mirage_crypto_ec.Ed25519.priv -> string -> t
val of_b32 : string -> (t, string) result
val to_b32 : t -> string
val to_octets : t -> string
val of_octets : string -> t
val jsont : t Jsont.t
val bin : t Bin.t
end = struct
(* TODO key format
endianess issue? *)
@ -42,6 +74,16 @@ end = struct
let to_octets t = t
let of_octets v =
match String.length v = 64 with
| false ->
Fmt.failwith "EddsaSignature.of_octets failure: data is not 64 bytes."
| true -> v
let bin =
let open Bin in
map (bytes 64) of_octets to_octets
let sign ~key s =
(* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *)
Mirage_crypto_ec.Ed25519.sign ~key s
@ -96,6 +138,8 @@ module RsaPublicKey = struct
| true -> Ok ()
in
fun s ->
Result.get_ok
@@
let open Syntax in
let len = String.length s in
let* () = check (len >= 4) in
@ -115,7 +159,7 @@ module RsaPublicKey = struct
let of_b32 s =
let open Syntax in
let* s = B32.decode s in
let* v = of_octets s in
let v = of_octets s in
let+ v = Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in
v