abstr type for signature

This commit is contained in:
swrup 2025-10-17 16:50:21 +02:00
parent a9a2409a12
commit 9929a4a324
5 changed files with 74 additions and 57 deletions

View file

@ -73,30 +73,34 @@ module RelativeTime = struct
|> Jsont.Object.finish |> Jsont.Object.finish
end end
let amount_jsont = module Amount = struct
Jsont.of_of_string ~kind:"Amount" Amount.of_string ~enc:Amount.to_string open Amount
let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string
end
module EddsaPublicKey = struct module EddsaPublicKey = struct
open EddsaPublicKey open EddsaPublicKey
(* todo?: rewrite with of_of_string *) let jsont = Jsont.of_of_string ~kind:"EddsaPublicKey" of_b32 ~enc:to_b32
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 ())
end end
module EddsaSignature = struct module EddsaSignature = struct
let jsont = Jsont.string open EddsaSignature
let jsont = Jsont.of_of_string ~kind:"EddsaSignature" of_b32 ~enc:to_b32
end end
module RsaPublicKey = struct module RsaPublicKey = struct
open RsaPublicKey open RsaPublicKey
let jsont = let jsont = Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32
let dec = Jsont.Base.dec_result of_string in end
let enc = Jsont.Base.enc to_string in
Jsont.Base.string (Jsont.Base.map ~kind:"RsaPublicKey" ~dec ~enc ()) module RsaSignature = struct
open RsaSignature
let jsont = Jsont.of_of_string ~kind:"RsaSignature" of_b32 ~enc:to_b32
end end
module RsaDenominationKey = struct module RsaDenominationKey = struct
@ -149,7 +153,6 @@ module FutureSignKey = struct
open FutureSignKey open FutureSignKey
let jsont = let jsont =
(* TODO ppx? *)
let make key stamp_start stamp_expire stamp_end signkey_secmod_sig = let make key stamp_start stamp_expire stamp_end signkey_secmod_sig =
{ key; stamp_start; stamp_expire; stamp_end; signkey_secmod_sig } { key; stamp_start; stamp_expire; stamp_end; signkey_secmod_sig }
in in

View file

@ -32,7 +32,7 @@ let mk_future_denom denom_secmod_sign_f
let open Binary_formats in let open Binary_formats in
let h_denom_pub = let h_denom_pub =
(* TODO hash *) (* TODO hash *)
let v = pub |> Types.RsaPublicKey.to_string in let v = pub |> Types.RsaPublicKey.to_b32 in
DenominationHash.{ v= { v } } DenominationHash.{ v= { v } }
in in
let h_section_name = let h_section_name =
@ -85,7 +85,7 @@ let mk_future_sign_key signkey_secmod_sign_f
to not mixup hashed values with others to not mixup hashed values with others
- have a make function for wrapped HashCode structs - have a make function for wrapped HashCode structs
make it take the relevant type *) make it take the relevant type *)
let v = EddsaPublicKey.to_string pub in let v = EddsaPublicKey.to_b32 pub in
Binary_formats.ExchangePublicKeyP.{ v= { v } } Binary_formats.ExchangePublicKeyP.{ v= { v } }
in in
let signkey_secmod_sig = let signkey_secmod_sig =

View file

@ -1,3 +1,5 @@
open Types
type rsa_denom = { type rsa_denom = {
section_name: string; section_name: string;
value: Amount.t; value: Amount.t;
@ -9,14 +11,8 @@ type rsa_denom = {
fee_deposit: Amount.t; fee_deposit: Amount.t;
fee_refresh: Amount.t; fee_refresh: Amount.t;
fee_refund: Amount.t; fee_refund: Amount.t;
pub: Types.RsaPublicKey.t; pub: RsaPublicKey.t;
sign: sign: string -> RsaSignature.t;
(* TODO sig type
should be
string -> RsaSignature.t
+ b32 *)
string ->
string;
} }
let hash_pub _pub = "todo public key converted to Crockford Base32" let hash_pub _pub = "todo public key converted to Crockford Base32"
@ -36,7 +32,7 @@ let make_rsa_denom
rsa_keysize; rsa_keysize;
age_restricted= _; age_restricted= _;
} : } :
Types.Config_types.Coin.t) = Config_types.Coin.t) =
assert (cipher = `RSA); assert (cipher = `RSA);
let stamp_start = Ptime_clock.now () in let stamp_start = Ptime_clock.now () in
@ -51,7 +47,7 @@ let make_rsa_denom
let open Mirage_crypto_pk.Rsa in let open Mirage_crypto_pk.Rsa in
let priv = generate ~bits:rsa_keysize () in let priv = generate ~bits:rsa_keysize () in
let pub = pub_of_priv priv in let pub = pub_of_priv priv in
let sign = decrypt ~crt_hardening:true ~mask:`Yes ~key:priv in let sign = RsaSignature.sign ~key:priv in
{ {
section_name; section_name;
value; value;

View file

@ -1,15 +1,11 @@
open Types
type t = { type t = {
pub: Mirage_crypto_ec.Ed25519.pub;
sign:
(* TODO sig type
should be
string -> EddsaSignature.t
+ b32 *)
string ->
string;
stamp_start: Ptime.t; stamp_start: Ptime.t;
stamp_expire: Ptime.t; stamp_expire: Ptime.t;
stamp_end: Ptime.t; stamp_end: Ptime.t;
pub: EddsaPublicKey.t;
sign: string -> EddsaSignature.t;
} }
let make ~name = let make ~name =
@ -30,13 +26,8 @@ let make ~name =
in in
let stamp_end = stamp_expire in let stamp_end = stamp_expire in
let priv, pub = Mirage_crypto_ec.Ed25519.generate () in let priv, pub = Mirage_crypto_ec.Ed25519.generate () in
{ let sign s = EddsaSignature.sign ~key:priv s in
pub; { stamp_start; stamp_expire; stamp_end; pub; sign }
sign= Mirage_crypto_ec.Ed25519.sign ~key:priv;
stamp_start;
stamp_expire;
stamp_end;
}
let signkey_device = make ~name:"signkey" let signkey_device = make ~name:"signkey"

View file

@ -65,30 +65,33 @@ end
module Amount = Amount module Amount = Amount
(* TODO key format *) (* TODO key format
- check what is the exact format in GNUNET
- endianess issue? *)
module EddsaPublicKey = struct module EddsaPublicKey = struct
(* EdDSA and ECDHE public keys always point on Curve25519 (* EdDSA and ECDHE public keys always point on Curve25519
and represented using the standard 256 bits Ed25519 compact format, and represented using the standard 256 bits Ed25519 compact format,
converted to Crockford Base32. *) converted to Crockford Base32. *)
type t = Mirage_crypto_ec.Ed25519.pub type t = Mirage_crypto_ec.Ed25519.pub
let of_string s = let of_b32 s =
let open Syntax in
let open Mirage_crypto_ec in let open Mirage_crypto_ec in
match B32.decode s with let* octets = B32.decode s in
| Error _ as err -> err match Ed25519.pub_of_octets octets with
| Ok octets -> ( | Error e -> Fmt.error "%a" pp_error e
match Ed25519.pub_of_octets octets with | Ok pub -> Ok pub
| Error e -> Fmt.error "%a" pp_error e
| Ok pub -> Ok pub)
let to_string pub = let to_b32 pub = pub |> Mirage_crypto_ec.Ed25519.pub_to_octets |> B32.encode
pub |> Mirage_crypto_ec.Ed25519.pub_to_octets |> B32.encode
end end
(* TODO sig type module EddsaSignature : sig
abstract type type t
same for RsaSignature *)
module EddsaSignature = struct 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 (* EdDSA signatures are transmitted as 64-bytes base32
binary-encoded objects with just the R and S values (base32_ binary-only). binary-encoded objects with just the R and S values (base32_ binary-only).
@ -101,20 +104,26 @@ module EddsaSignature = struct
let sign ~key s = let sign ~key s =
(* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *) (* mirage_crypto: "The result is the concatenation of r and s, as specified in RFC 8032." *)
Mirage_crypto_ec.Ed25519.sign ~key s 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 end
module RsaPublicKey = struct module RsaPublicKey = struct
(* RSA public key converted to Crockford Base32. *)
type t = Mirage_crypto_pk.Rsa.pub type t = Mirage_crypto_pk.Rsa.pub
let of_string s = let of_b32 s =
let open Syntax in let open Syntax in
let* s = B32.decode s in let* s = B32.decode s in
let* v = Util.bin_of_string Binary_formats.RsaPublicKey.bin 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 let+ v = Mirage_crypto_pk.Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in
v 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 open Binary_formats.RsaPublicKey in
let header = { n_len= Z.size n; e_len= Z.size e } in let header = { n_len= Z.size n; e_len= Z.size e } in
let v = { header; n; e } in let v = { header; n; e } in
@ -123,6 +132,24 @@ module RsaPublicKey = struct
s s
end 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 module HashCode = struct
(* 32-byte value representing a point on Curve25519. *) (* 32-byte value representing a point on Curve25519. *)
type cs25519Point = string type cs25519Point = string