From 56b665178a2e1762ec64459b9a3486801f55a964 Mon Sep 17 00:00:00 2001 From: swrup Date: Sat, 18 Oct 2025 01:25:12 +0200 Subject: [PATCH] + define Hash_64 with include --- src/binary_formats.ml | 106 ++++++++++++++++++++++++------------------ src/management.ml | 2 +- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/binary_formats.ml b/src/binary_formats.ml index cc61cb34..94962fda 100644 --- a/src/binary_formats.ml +++ b/src/binary_formats.ml @@ -34,13 +34,15 @@ 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 - !! not strings that are raw-bytes-data-like + ! not strings that are raw-bytes-data-like + ? only for "HashCode" and "ShortHashCode" *) -module UTIL = struct - let int32_size = 4 - let int64_size = 8 +let int32_size = 4 +let int64_size = 8 +(* todo clean up: maybe rm it *) +module UTIL_FUNC = struct (* microseconds since the UNIX Epoch UINT64_MAX represents "never" *) module MK_TIME () = struct @@ -125,21 +127,17 @@ module UTIL = struct |+ field (bytes 64) (fun t -> t.hash) |> sealr end - (* - let to_null_terminated s = - let len = String.length s in - let b = Bytes.create (len + 1) in - Bytes.blit_string s 0 b 0 len; - Bytes.set b len '\x00'; - Bytes.unsafe_to_string b - *) +end - module HASH_64 : sig +module UTIL_HASH = struct + module type H_sig = sig type t val hash : string -> t val bin : t Bin.t - end = struct + end + + module HHH_64 : H_sig = struct type t = { hash: Digestif.SHA512.t } let hash s = @@ -154,9 +152,32 @@ module UTIL = struct let open Bin in record (fun hash -> { hash }) |+ field hash_bin (fun t -> t.hash) |> sealr end + + (* sha512 with string size check *) + module Hash_64 : H_sig = struct + include HHH_64 + + let hash s = + match String.length s = 64 with + | false -> Fmt.failwith "SHA512 failure: data is not 64 bytes" + | true -> hash s + end + + (* sha512 but add a null termination to the given string + this is "HashCode" in GNU TALER + + todo: maybe need to have a cstring.ml *) + module Cstring_hash_64 : H_sig = struct + include HHH_64 + + let hash s = + let s = s ^ "\x00" in + hash s + end end -open UTIL +open UTIL_FUNC +include UTIL_HASH module Taler_signatures = Include.Taler_signatures (* -- Time -- *) @@ -233,7 +254,7 @@ module GNUNET_RsaPublicKey = struct end module DenominationHash = struct - include HASH_64 + include Hash_64 let hash (pub : Mirage_crypto_pk.Rsa.pub) = pub @@ -244,9 +265,6 @@ end (* --- Hashes --- *) -module ShortHashCode = MK_HASH_32 () -module HashCode = MK_HASH_64 () - (* Hash over a full payto://-URI, including receiver-name (and possibly BIC and other optional fields). *) module FullPaytoHash = MK_HASH_32 () @@ -499,7 +517,7 @@ module DenominationKeyAnnouncementPS = struct (* purpose.purpose = TALER_SIGNATURE_SM_DENOMINATION_KEY *) type t = { h_denom_pub: DenominationHash.t; - h_section_name: HashCode.t; + h_section_name: Cstring_hash_64.t; anchor_time: TimeAbsoluteNBO.t; duration_withdraw: TimeRelativeNBO.t; } @@ -512,7 +530,7 @@ module DenominationKeyAnnouncementPS = struct { h_denom_pub; h_section_name; anchor_time; duration_withdraw }) |+ Purpose.field purpose |+ field DenominationHash.bin (fun t -> t.h_denom_pub) - |+ field HashCode.bin (fun t -> t.h_section_name) + |+ field Cstring_hash_64.bin (fun t -> t.h_section_name) |+ field TimeAbsoluteNBO.bin (fun t -> t.anchor_time) |+ field TimeRelativeNBO.bin (fun t -> t.duration_withdraw) |> sealr @@ -562,7 +580,7 @@ module DepositRequestPS = struct amount_with_fee: AmountNBO.t; deposit_fee: AmountNBO.t; merchant: MerchantPublicKeyP.t; - wallet_data_hash: HashCode.t; + wallet_data_hash: Cstring_hash_64.t; } end @@ -613,7 +631,7 @@ module ExchangeKeySetPS = struct (* purpose.purpose = TALER_SIGNATURE_EXCHANGE_KEY_SET *) type t = { list_issue_date: TimeAbsoluteNBO.t; - hc: HashCode.t; + hc: Cstring_hash_64.t; } end @@ -637,16 +655,16 @@ module MasterWireDetailsPS = struct (* purpose.purpose = TALER_SIGNATURE_MASTER_WIRE_DETAILS *) type t = { h_wire_details: FullPaytoHash.t; - h_conversion_url: HashCode.t; - h_credit_restrictions: HashCode.t; - h_debit_restrictions: HashCode.t; + h_conversion_url: Cstring_hash_64.t; + h_credit_restrictions: Cstring_hash_64.t; + h_debit_restrictions: Cstring_hash_64.t; } end module MasterWireFeePS = struct (* purpose.purpose = TALER_SIGNATURE_MASTER_WIRE_FEES *) type t = { - h_wire_method: HashCode.t; + h_wire_method: Cstring_hash_64.t; start_date: TimeAbsoluteNBO.t; end_date: TimeAbsoluteNBO.t; wire_fee: AmountNBO.t; @@ -676,7 +694,7 @@ module MasterDrainProfitPS = struct wtid: WireTransferIdentifierRawP.t; date: TimeAbsoluteNBO.t; amount: AmountNBO.t; - h_section: HashCode.t; + h_section: Cstring_hash_64.t; h_payto: FullPaytoHash.t; } end @@ -707,14 +725,14 @@ module WireDepositDataPS = struct wire_fee: AmountNBO.t; merchant_pub: MerchantPublicKeyP.t; h_wire: MerchantWireHash.t; - h_details: HashCode.t; + h_details: Cstring_hash_64.t; } end module ExchangeKeyValidityPS = struct (* purpose.purpose = TALER_SIGNATURE_AUDITOR_EXCHANGE_KEYS *) type t = { - auditor_url_hash: HashCode.t; + auditor_url_hash: Cstring_hash_64.t; master: MasterPublicKeyP.t; start: TimeAbsoluteNBO.t; expire_withdraw: TimeAbsoluteNBO.t; @@ -785,7 +803,7 @@ end module MerchantRefundConfirmationPS = struct (* purpose.purpose = TALER_SIGNATURE_MERCHANT_REFUND_OK *) (* Hash of the order ID (a string), hashed without the 0-termination. *) - type t = { h_order_id: HashCode.t } + type t = { h_order_id: Cstring_hash_64.t } end module RecoupRequestPS = struct @@ -910,7 +928,7 @@ module PurseDepositSignaturePS = struct h_denom_pub: DenominationHash.t; h_age_commitment: AgeCommitmentHash.t; purse_pub: PursePublicKey.t; - h_exchange_base_url: HashCode.t; + h_exchange_base_url: Cstring_hash_64.t; } end @@ -977,7 +995,7 @@ module WadDataSignaturePS = struct type t = { wad_execution_time: TimeAbsoluteNBO.t; total_amount: AmountNBO.t; - h_items: HashCode.t; + h_items: Cstring_hash_64.t; wad_id: WadId.t; } end @@ -985,7 +1003,7 @@ end module WadPartnerSignaturePS = struct (* purpose.purpose = TALER_SIGNATURE_MASTER_PARTNER_DETAILS *) type t = { - h_partner_base_url: HashCode.t; + h_partner_base_url: Cstring_hash_64.t; master_public_key: MasterPublicKeyP.t; start_date: TimeAbsoluteNBO.t; end_date: TimeAbsoluteNBO.t; @@ -1034,7 +1052,7 @@ module MasterAddAuditorPS = struct type t = { start_date: TimeAbsoluteNBO.t; auditor_pub: AuditorPublicKeyP.t; - h_auditor_url: HashCode.t; + h_auditor_url: Cstring_hash_64.t; } end @@ -1051,9 +1069,9 @@ module MasterAddWirePS = struct type t = { start_date: TimeAbsoluteNBO.t; h_wire: FullPaytoHash.t; - h_conversion_url: HashCode.t; - h_credit_restrictions: HashCode.t; - h_debit_restrictions: HashCode.t; + h_conversion_url: Cstring_hash_64.t; + h_credit_restrictions: Cstring_hash_64.t; + h_debit_restrictions: Cstring_hash_64.t; } end @@ -1070,7 +1088,7 @@ module MasterAmlOfficerStatusPS = struct type t = { change_date: TimestampNBO.t; officer_pub: AmlOfficerPublicKeyP.t; - h_officer_name: HashCode.t; + h_officer_name: Cstring_hash_64.t; is_active: int; } end @@ -1078,11 +1096,11 @@ end module AmlDecisionPS = struct (* purpose.purpose = TALER_SIGNATURE_AML_DECISION *) type t = { - h_justification: HashCode.t; + h_justification: Cstring_hash_64.t; decision_time: TimestampNBO.t; new_threshold: AmountNBO.t; h_payto: NormalizedPaytoHash.t; - h_kyc_requirements: HashCode.t; + h_kyc_requirements: Cstring_hash_64.t; new_state: int; } end @@ -1095,7 +1113,7 @@ module PartnerConfigurationPS = struct end_date: TimestampNBO.t; wad_frequency: TimeRelativeNBO.t; wad_fee: AmountNBO.t; - h_url: HashCode.t; + h_url: Cstring_hash_64.t; } end @@ -1121,7 +1139,7 @@ module ReserveAttestRequestPS = struct (* purpose.purpose = TALER_SIGNATURE_WALLET_ATTEST_REQUEST *) type t = { request_timestamp: TimestampNBO.t; - h_details: HashCode.t; + h_details: Cstring_hash_64.t; } end @@ -1131,6 +1149,6 @@ module ExchangeAttestPS = struct attest_timestamp: TimestampNBO.t; expiration_time: TimestampNBO.t; reserve_pub: ReservePublicKeyP.t; - h_attributes: HashCode.t; + h_attributes: Cstring_hash_64.t; } end diff --git a/src/management.ml b/src/management.ml index 00d548a0..19704629 100644 --- a/src/management.ml +++ b/src/management.ml @@ -29,7 +29,7 @@ let mk_future_denom denom_secmod_sign_f let denom_secmod_sig = let open Binary_formats in let h_denom_pub = DenominationHash.hash pub in - let h_section_name = HashCode.of_string section_name in + let h_section_name = Cstring_hash_64.hash section_name in let anchor_time = TimeAbsoluteNBO.{ v= Util.ptime_to_int64_us stamp_start } in