add to_octets

This commit is contained in:
swrup 2025-10-17 17:29:29 +02:00
parent 78b08dcb4e
commit da6058f406
3 changed files with 24 additions and 21 deletions

View file

@ -20,7 +20,12 @@
- test them - test them
- some purpose (`TALER_SIGNATURE_XXX`) are missing - some purpose (`TALER_SIGNATURE_XXX`) are missing
exchange and gana master branch are not in sync exchange and gana master branch are not in sync
and we should use a specific git tag instead *) and we should use a specific git tag instead
- clean up functor mess
don't have not hash thing be HashCode like
- have a make function for wrapped HashCode structs
make it take the relevant type *)
module Taler_signatures = Include.Taler_signatures module Taler_signatures = Include.Taler_signatures

View file

@ -76,17 +76,7 @@ let mk_future_denom denom_secmod_sign_f
let mk_future_sign_key signkey_secmod_sign_f let mk_future_sign_key signkey_secmod_sign_f
({ pub; sign= _; stamp_start; stamp_expire; stamp_end } : Secmod_keys.t) = ({ pub; sign= _; stamp_start; stamp_expire; stamp_end } : Secmod_keys.t) =
let exchange_pub = let exchange_pub =
(* TODO Binary_formats.ExchangePublicKeyP.{ v= { v= EddsaPublicKey.to_octets pub } }
- I don't think its in crockford base 32 here
maybe SHA-512?
or maybe SHA-512 in crockford base 32
-> no, its not a hash here
need to change ugly binary_formats functor
to not mixup hashed values with others
- have a make function for wrapped HashCode structs
make it take the relevant type *)
let v = EddsaPublicKey.to_b32 pub in
Binary_formats.ExchangePublicKeyP.{ v= { v } }
in in
let signkey_secmod_sig = let signkey_secmod_sig =
let open Binary_formats in let open Binary_formats in

View file

@ -82,6 +82,8 @@ module EddsaPublicKey = struct
converted to Crockford Base32. *) converted to Crockford Base32. *)
type t = Mirage_crypto_ec.Ed25519.pub type t = Mirage_crypto_ec.Ed25519.pub
let to_octets t = Mirage_crypto_ec.Ed25519.pub_to_octets t
let of_b32 s = let of_b32 s =
let open Syntax in let open Syntax in
let open Mirage_crypto_ec in let open Mirage_crypto_ec in
@ -90,13 +92,14 @@ module EddsaPublicKey = struct
| Error e -> Fmt.error "%a" pp_error e | Error e -> Fmt.error "%a" pp_error e
| Ok pub -> Ok pub | Ok pub -> Ok pub
let to_b32 pub = pub |> Mirage_crypto_ec.Ed25519.pub_to_octets |> B32.encode let to_b32 t = B32.encode (to_octets t)
let jsont = Jsont.of_of_string ~kind:"EddsaPublicKey" of_b32 ~enc:to_b32 let jsont = Jsont.of_of_string ~kind:"EddsaPublicKey" of_b32 ~enc:to_b32
end end
module EddsaSignature : sig module EddsaSignature : sig
type t type t
val to_octets : t -> string
val sign : key:Mirage_crypto_ec.Ed25519.priv -> string -> t val sign : key:Mirage_crypto_ec.Ed25519.priv -> string -> t
val of_b32 : string -> (t, string) result val of_b32 : string -> (t, string) result
val to_b32 : t -> string val to_b32 : t -> string
@ -108,6 +111,8 @@ end = struct
They are signature over a c-struct like `TALER_xxxPS` + with a purpose *) They are signature over a c-struct like `TALER_xxxPS` + with a purpose *)
type t = string type t = string
let to_octets t = t
(* TODO key format (* TODO key format
is it exactly like in GNU_CRYPTO? is it exactly like in GNU_CRYPTO?
endianess issue? *) endianess issue? *)
@ -128,6 +133,13 @@ end
module RsaPublicKey = struct module RsaPublicKey = struct
type t = Mirage_crypto_pk.Rsa.pub type t = Mirage_crypto_pk.Rsa.pub
let to_octets ({ 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
let s = Bin.to_string bin v in
s
let of_b32 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
@ -135,20 +147,14 @@ module RsaPublicKey = struct
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_b32 ({ n; e } : Mirage_crypto_pk.Rsa.pub) = let to_b32 t = B32.encode (to_octets t)
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
let s = Bin.to_string bin v in
let s = B32.encode s in
s
let jsont = Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32 let jsont = Jsont.of_of_string ~kind:"RsaPublicKey" of_b32 ~enc:to_b32
end end
module RsaSignature : sig module RsaSignature : sig
type t type t
val to_octets : t -> string
val sign : key:Mirage_crypto_pk.Rsa.priv -> string -> t val sign : key:Mirage_crypto_pk.Rsa.priv -> string -> t
val of_b32 : string -> (t, string) result val of_b32 : string -> (t, string) result
val to_b32 : t -> string val to_b32 : t -> string
@ -156,6 +162,8 @@ module RsaSignature : sig
end = struct end = struct
type t = string type t = string
let to_octets t = t
(* no Rsa.sign(?): (* no Rsa.sign(?):
decrypt is equivalent to sign *) decrypt is equivalent to sign *)
let sign ~key s = let sign ~key s =