From a46497e62e8628a03e2d46a3751aa8baae7ba731 Mon Sep 17 00:00:00 2001 From: swrup Date: Fri, 17 Oct 2025 16:50:21 +0200 Subject: [PATCH] abstr type for signature --- src/json.ml | 29 ++++++++++++++------------ src/management.ml | 2 +- src/types.ml | 52 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/src/json.ml b/src/json.ml index 495bbd89..2a5e54f1 100644 --- a/src/json.ml +++ b/src/json.ml @@ -73,30 +73,34 @@ module RelativeTime = struct |> Jsont.Object.finish end -let amount_jsont = - Jsont.of_of_string ~kind:"Amount" Amount.of_string ~enc:Amount.to_string +module Amount = struct + open Amount + + let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string +end module EddsaPublicKey = struct open EddsaPublicKey - (* todo?: rewrite with of_of_string *) - let jsont = - let dec = Jsont.Base.dec_result of_string in - let enc = Jsont.Base.enc to_string in - Jsont.Base.string (Jsont.Base.map ~kind:"EddsaPublicKey" ~dec ~enc ()) + let jsont = Jsont.of_of_string ~kind:"EddsaPublicKey" of_string ~enc:to_string end module EddsaSignature = struct - let jsont = Jsont.string + open EddsaSignature + + let jsont = Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32 end module RsaPublicKey = struct open RsaPublicKey - let jsont = - let dec = Jsont.Base.dec_result of_string in - let enc = Jsont.Base.enc to_string in - Jsont.Base.string (Jsont.Base.map ~kind:"RsaPublicKey" ~dec ~enc ()) + let jsont = Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32 +end + +module RsaSignature = struct + open RsaSignature + + let jsont = Jsont.of_of_string ~kind:"RsaSignature" of_b32 ~enc:to_b32 end module RsaDenominationKey = struct @@ -149,7 +153,6 @@ module FutureSignKey = struct open FutureSignKey let jsont = - (* TODO ppx? *) let make key stamp_start stamp_expire stamp_end signkey_secmod_sig = { key; stamp_start; stamp_expire; stamp_end; signkey_secmod_sig } in diff --git a/src/management.ml b/src/management.ml index d0afe432..10d2f327 100644 --- a/src/management.ml +++ b/src/management.ml @@ -32,7 +32,7 @@ let mk_future_denom denom_secmod_sign_f let open Binary_formats in let h_denom_pub = (* TODO hash *) - let v = pub |> Types.RsaPublicKey.to_string in + let v = pub |> Types.RsaPublicKey.to_b32 in DenominationHash.{ v= { v } } in let h_section_name = diff --git a/src/types.ml b/src/types.ml index e80498e6..9c444ecf 100644 --- a/src/types.ml +++ b/src/types.ml @@ -73,22 +73,24 @@ module EddsaPublicKey = struct type t = Mirage_crypto_ec.Ed25519.pub let of_string s = + let open Syntax in let open Mirage_crypto_ec in - match B32.decode s with - | Error _ as err -> err - | Ok octets -> ( - match Ed25519.pub_of_octets octets with - | Error e -> Fmt.error "%a" pp_error e - | Ok pub -> Ok pub) + let* octets = B32.decode s in + match Ed25519.pub_of_octets octets with + | Error e -> Fmt.error "%a" pp_error e + | Ok pub -> Ok pub let to_string pub = pub |> Mirage_crypto_ec.Ed25519.pub_to_octets |> B32.encode end -(* TODO sig type - abstract type - same for RsaSignature *) -module EddsaSignature = struct +module EddsaSignature : sig + type t + + val sign : key:Mirage_crypto_ec.Ed25519.priv -> string -> t + val of_b32 : string -> (t, string) result + val to_b32 : t -> string +end = struct (* EdDSA signatures are transmitted as 64-bytes base32 binary-encoded objects with just the R and S values (base32_ binary-only). @@ -101,20 +103,26 @@ module EddsaSignature = struct 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 + + let of_b32 s = B32.decode s + + let to_b32 t = + let s = B32.encode t in + assert (String.length s = 64); + s end module RsaPublicKey = struct - (* RSA public key converted to Crockford Base32. *) type t = Mirage_crypto_pk.Rsa.pub - let of_string s = + let of_b32 s = let open Syntax in let* s = B32.decode s in let* v = Util.bin_of_string Binary_formats.RsaPublicKey.bin s in let+ v = Mirage_crypto_pk.Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in v - let to_string ({ n; e } : Mirage_crypto_pk.Rsa.pub) = + let to_b32 ({ n; e } : Mirage_crypto_pk.Rsa.pub) = let open Binary_formats.RsaPublicKey in let header = { n_len= Z.size n; e_len= Z.size e } in let v = { header; n; e } in @@ -123,6 +131,24 @@ module RsaPublicKey = struct s end +module RsaSignature : sig + type t + + val sign : key:Mirage_crypto_pk.Rsa.priv -> string -> t + val of_b32 : string -> (t, string) result + val to_b32 : t -> string +end = struct + type t = string + + (* no Rsa.sign(?): + decrypt is equivalent to sign *) + let sign ~key s = + Mirage_crypto_pk.Rsa.decrypt ~crt_hardening:true ~mask:`Yes ~key s + + let of_b32 s = B32.decode s + let to_b32 t = B32.encode t +end + module HashCode = struct (* 32-byte value representing a point on Curve25519. *) type cs25519Point = string