From 0a24b41d70710c606f3075111006b3ecda90cc70 Mon Sep 17 00:00:00 2001 From: swrup Date: Fri, 17 Oct 2025 22:04:35 +0200 Subject: [PATCH] implement hash TODO good handling of 0-terminated C-string --- src/binary_formats.ml | 39 ++++++++++++++++++++++++++++++++++++--- src/dune | 1 + src/management.ml | 12 ++---------- src/types.ml | 4 ++-- test/test.ml | 4 ++++ 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/binary_formats.ml b/src/binary_formats.ml index 9a17ee57..43c1b409 100644 --- a/src/binary_formats.ml +++ b/src/binary_formats.ml @@ -26,6 +26,15 @@ - outdated doc(?) - some missing struct documentation *) +(* TODO hash and C(ancer)-terminated strings + + - "A JSON object is canonicalized by converting it to an ASCII byte array + with the algorithm specified in RFC 8785. The resulting bytes are + terminated with a single 0-byte and then hashed with SHA512." + - from the code it looks like its the same for all stringy-strings + !! that are not raw bytes data + *) + module UTIL = struct let int32_size = 4 let int64_size = 8 @@ -71,9 +80,21 @@ module UTIL = struct record (fun v -> { v }) |+ field (bytes 64) (fun t -> t.v) |> sealr end - module MK_HASH_32 () = struct + module MK_HASH_32 () : sig + type t = private { hash: string } + + val of_string : string -> t + val bin : t Bin.t + end = struct type t = { hash: string } + let of_string s = + match String.length s = 32 with + | false -> Fmt.failwith "SHA256 failure: data is not 32 bytes" + | true -> + let hash = Digestif.SHA256.(to_raw_string (digest_string s)) in + { hash } + let bin = let open Bin in record (fun hash -> { hash }) @@ -81,9 +102,21 @@ module UTIL = struct |> sealr end - module MK_HASH_64 () = struct + module MK_HASH_64 () : sig + type t = private { hash: string } + + val f : string -> t + val bin : t Bin.t + end = struct type t = { hash: string } + let f s = + match String.length s = 64 with + | false -> Fmt.failwith "SHA512 failure: data is not 64bytes" + | true -> + let hash = Digestif.SHA512.(to_raw_string (digest_string s)) in + { hash } + let bin = let open Bin in record (fun hash -> { hash }) @@ -183,7 +216,7 @@ module EncryptedLinkSecretP = MK_64 () module DenominationBlindingKeyP = MK_32 () (* GNUNET_CRYPTO format *) -module RsaPublicKey = struct +module GNUNET_RsaPublicKey = struct (* libgnuutil format: https://docs.gnunet.org/doxygen/d9/dbe/structGNUNET__CRYPTO__RsaPublicKeyHeaderP.html https://docs.gnunet.org/doxygen/d6/d70/group__libgnunetutil.html#ga9c99a81e8cd649c1c925d23211a0738f diff --git a/src/dune b/src/dune index 168b23c8..8126b6ec 100644 --- a/src/dune +++ b/src/dune @@ -18,6 +18,7 @@ angstrom zarith ; mirage-crypto + digestif duration vif fmt diff --git a/src/management.ml b/src/management.ml index 6fbfc9ee..2d30f027 100644 --- a/src/management.ml +++ b/src/management.ml @@ -28,16 +28,8 @@ let mk_future_denom denom_secmod_sign_f in let denom_secmod_sig = let open Binary_formats in - let h_denom_pub = - (* TODO hash *) - let hash = pub |> Types.RsaPublicKey.to_b32 in - DenominationHash.{ hash } - in - let h_section_name = - (* TODO hash *) - let hash = section_name in - HashCode.{ hash } - in + let h_denom_pub = DenominationHash.f (RsaPublicKey.to_octets pub) in + let h_section_name = HashCode.f section_name in let anchor_time = TimeAbsoluteNBO.{ v= Util.ptime_to_int64_us stamp_start } in diff --git a/src/types.ml b/src/types.ml index 73c500dd..8b97da6c 100644 --- a/src/types.ml +++ b/src/types.ml @@ -134,7 +134,7 @@ module RsaPublicKey = struct type t = Mirage_crypto_pk.Rsa.pub let to_octets ({ n; e } : Mirage_crypto_pk.Rsa.pub) = - let open Binary_formats.RsaPublicKey in + let open Binary_formats.GNUNET_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 @@ -143,7 +143,7 @@ module RsaPublicKey = struct 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 = Util.bin_of_string Binary_formats.GNUNET_RsaPublicKey.bin s in let+ v = Mirage_crypto_pk.Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in v diff --git a/test/test.ml b/test/test.ml index 8d4bd866..26a811ac 100644 --- a/test/test.ml +++ b/test/test.ml @@ -75,6 +75,8 @@ let () = check_bad ", \"foo\""; () +(* + let () = let open Binary_formats.WithdrawConfirmationPS in let str64 = String.init 64 (fun i -> Char.unsafe_chr (i + 1)) in @@ -88,3 +90,5 @@ let () = Printf.printf "%s" raw_str; *) () + + *)