JJ: Description from the destination commit:
implement hash TODO good handling of 0-terminated C-string JJ: Description from source commit: wip
This commit is contained in:
parent
6220bbdae1
commit
e696841b2b
5 changed files with 148 additions and 71 deletions
|
|
@ -24,7 +24,18 @@
|
||||||
- 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
|
||||||
- outdated doc(?)
|
- outdated doc(?)
|
||||||
- some missing struct documentation *)
|
- some missing struct documentation
|
||||||
|
|
||||||
|
- correctly handle endianness *)
|
||||||
|
|
||||||
|
(* 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
|
||||||
|
!! not strings that are raw-bytes-data-like
|
||||||
|
*)
|
||||||
|
|
||||||
module UTIL = struct
|
module UTIL = struct
|
||||||
let int32_size = 4
|
let int32_size = 4
|
||||||
|
|
@ -71,9 +82,21 @@ module UTIL = struct
|
||||||
record (fun v -> { v }) |+ field (bytes 64) (fun t -> t.v) |> sealr
|
record (fun v -> { v }) |+ field (bytes 64) (fun t -> t.v) |> sealr
|
||||||
end
|
end
|
||||||
|
|
||||||
module MK_HASH_32 () = struct
|
module MK_HASH_32 () : sig
|
||||||
|
type t = private { hash: string }
|
||||||
|
|
||||||
|
val of_octets : string -> t
|
||||||
|
val bin : t Bin.t
|
||||||
|
end = struct
|
||||||
type t = { hash: string }
|
type t = { hash: string }
|
||||||
|
|
||||||
|
let of_octets 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 bin =
|
||||||
let open Bin in
|
let open Bin in
|
||||||
record (fun hash -> { hash })
|
record (fun hash -> { hash })
|
||||||
|
|
@ -81,15 +104,56 @@ module UTIL = struct
|
||||||
|> sealr
|
|> sealr
|
||||||
end
|
end
|
||||||
|
|
||||||
module MK_HASH_64 () = struct
|
module MK_HASH_64 () : sig
|
||||||
|
type t = private { hash: string }
|
||||||
|
|
||||||
|
val of_string : string -> t
|
||||||
|
val bin : t Bin.t
|
||||||
|
end = struct
|
||||||
type t = { hash: string }
|
type t = { hash: string }
|
||||||
|
|
||||||
|
let of_string 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 bin =
|
||||||
let open Bin in
|
let open Bin in
|
||||||
record (fun hash -> { hash })
|
record (fun hash -> { hash })
|
||||||
|+ field (bytes 64) (fun t -> t.hash)
|
|+ field (bytes 64) (fun t -> t.hash)
|
||||||
|> sealr
|
|> sealr
|
||||||
end
|
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
|
||||||
|
*)
|
||||||
|
|
||||||
|
module HASH_64 : sig
|
||||||
|
type t
|
||||||
|
|
||||||
|
val hash : string -> t
|
||||||
|
val bin : t Bin.t
|
||||||
|
end = struct
|
||||||
|
type t = { hash: Digestif.SHA512.t }
|
||||||
|
|
||||||
|
let hash s =
|
||||||
|
let hash = Digestif.SHA512.(digest_string s) in
|
||||||
|
{ hash }
|
||||||
|
|
||||||
|
let hash_bin =
|
||||||
|
let open Bin in
|
||||||
|
map (bytes 64) Digestif.SHA512.of_raw_string Digestif.SHA512.to_raw_string
|
||||||
|
|
||||||
|
let bin =
|
||||||
|
let open Bin in
|
||||||
|
record (fun hash -> { hash }) |+ field hash_bin (fun t -> t.hash) |> sealr
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
open UTIL
|
open UTIL
|
||||||
|
|
@ -106,6 +170,78 @@ module TimestampNBO = MK_TIME_NBO ()
|
||||||
|
|
||||||
(* -- Cryptographic primitives -- *)
|
(* -- Cryptographic primitives -- *)
|
||||||
|
|
||||||
|
(* GNUNET_CRYPTO format *)
|
||||||
|
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
|
||||||
|
https://www.gnupg.org/documentation/manuals/gcrypt/MPI-formats.html
|
||||||
|
|
||||||
|
format:
|
||||||
|
- rsa header
|
||||||
|
- modulus
|
||||||
|
- public_exponent
|
||||||
|
|
||||||
|
integer in big-endian format (MSB first).
|
||||||
|
Leading zeroes are stripped unless they are required to keep a value positive.
|
||||||
|
*)
|
||||||
|
type header = {
|
||||||
|
n_len: int;
|
||||||
|
e_len: int;
|
||||||
|
}
|
||||||
|
|
||||||
|
type t = {
|
||||||
|
header: header;
|
||||||
|
n: Z.t;
|
||||||
|
e: Z.t;
|
||||||
|
}
|
||||||
|
|
||||||
|
(* need to strip leading zeros or something? *)
|
||||||
|
let z_to_bigendian_bits v =
|
||||||
|
let s = Z.to_bits v in
|
||||||
|
let len = String.length s in
|
||||||
|
let s = String.init len (fun i -> s.[len - 1 - i]) in
|
||||||
|
s
|
||||||
|
|
||||||
|
let header_bin =
|
||||||
|
let open Bin in
|
||||||
|
record (fun n_len e_len -> { n_len; e_len })
|
||||||
|
|+ field beint16 (fun t -> t.n_len)
|
||||||
|
|+ field beint16 (fun t -> t.e_len)
|
||||||
|
|> sealr
|
||||||
|
|
||||||
|
(* note: this one has a dynamic sizeof *)
|
||||||
|
let bin =
|
||||||
|
let open Bin in
|
||||||
|
record (fun header n e ->
|
||||||
|
let n = Z.of_bits n in
|
||||||
|
let e = Z.of_bits e in
|
||||||
|
{ header; n; e })
|
||||||
|
|+ field header_bin (fun t -> t.header)
|
||||||
|
(* TODO
|
||||||
|
Z.to_bits is in little endian but we need it in big endian *)
|
||||||
|
(* TODO
|
||||||
|
here it should not be [cstring] but [bytes t.header.n_len] *)
|
||||||
|
|+ field cstring (fun t -> z_to_bigendian_bits t.n)
|
||||||
|
|+ field cstring (fun t -> z_to_bigendian_bits t.e)
|
||||||
|
|> sealr
|
||||||
|
|
||||||
|
let of_pub ({ n; e } : Mirage_crypto_pk.Rsa.pub) =
|
||||||
|
let header = { n_len= Z.size n; e_len= Z.size e } in
|
||||||
|
let t = { header; n; e } in
|
||||||
|
t
|
||||||
|
end
|
||||||
|
|
||||||
|
module DenominationHash = struct
|
||||||
|
include HASH_64
|
||||||
|
|
||||||
|
let mk (pub : Mirage_crypto_pk.Rsa.pub) =
|
||||||
|
pub
|
||||||
|
|> GNUNET_RsaPublicKey.of_pub
|
||||||
|
|> Bin.to_string GNUNET_RsaPublicKey.bin
|
||||||
|
|> hash
|
||||||
|
end
|
||||||
|
|
||||||
(* --- Hashes --- *)
|
(* --- Hashes --- *)
|
||||||
|
|
||||||
module ShortHashCode = MK_HASH_32 ()
|
module ShortHashCode = MK_HASH_32 ()
|
||||||
|
|
@ -118,7 +254,6 @@ module FullPaytoHash = MK_HASH_32 ()
|
||||||
(* Hash over a normalized payto://-URI, including all optional
|
(* Hash over a normalized payto://-URI, including all optional
|
||||||
fields and also with account-part canonicalized (so no BIC). *)
|
fields and also with account-part canonicalized (so no BIC). *)
|
||||||
module NormalizedPaytoHash = MK_HASH_32 ()
|
module NormalizedPaytoHash = MK_HASH_32 ()
|
||||||
module DenominationHash = MK_HASH_64 ()
|
|
||||||
module PrivateContractHash = MK_HASH_64 ()
|
module PrivateContractHash = MK_HASH_64 ()
|
||||||
module ExtensionsPolicyHash = MK_HASH_64 ()
|
module ExtensionsPolicyHash = MK_HASH_64 ()
|
||||||
module MerchantWireHash = MK_HASH_64 ()
|
module MerchantWireHash = MK_HASH_64 ()
|
||||||
|
|
@ -182,61 +317,6 @@ module EncryptedLinkSecretP = MK_64 ()
|
||||||
type DenominationBlindingKeyP = string; *)
|
type DenominationBlindingKeyP = string; *)
|
||||||
module DenominationBlindingKeyP = MK_32 ()
|
module DenominationBlindingKeyP = MK_32 ()
|
||||||
|
|
||||||
(* GNUNET_CRYPTO format *)
|
|
||||||
module 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
|
|
||||||
https://www.gnupg.org/documentation/manuals/gcrypt/MPI-formats.html
|
|
||||||
|
|
||||||
format:
|
|
||||||
- rsa header
|
|
||||||
- modulus
|
|
||||||
- public_exponent
|
|
||||||
|
|
||||||
integer in big-endian format (MSB first).
|
|
||||||
Leading zeroes are stripped unless they are required to keep a value positive.
|
|
||||||
*)
|
|
||||||
type header = {
|
|
||||||
n_len: int;
|
|
||||||
e_len: int;
|
|
||||||
}
|
|
||||||
|
|
||||||
type t = {
|
|
||||||
header: header;
|
|
||||||
n: Z.t;
|
|
||||||
e: Z.t;
|
|
||||||
}
|
|
||||||
|
|
||||||
(* need to strip leading zeros or something? *)
|
|
||||||
let z_to_bigendian_bits v =
|
|
||||||
let s = Z.to_bits v in
|
|
||||||
let len = String.length s in
|
|
||||||
let s = String.init len (fun i -> s.[len - 1 - i]) in
|
|
||||||
s
|
|
||||||
|
|
||||||
let header_bin =
|
|
||||||
let open Bin in
|
|
||||||
record (fun n_len e_len -> { n_len; e_len })
|
|
||||||
|+ field beint16 (fun t -> t.n_len)
|
|
||||||
|+ field beint16 (fun t -> t.e_len)
|
|
||||||
|> sealr
|
|
||||||
|
|
||||||
(* note: this one has a dynamic sizeof *)
|
|
||||||
let bin =
|
|
||||||
let open Bin in
|
|
||||||
record (fun header n e ->
|
|
||||||
let n = Z.of_bits n in
|
|
||||||
let e = Z.of_bits e in
|
|
||||||
{ header; n; e })
|
|
||||||
|+ field header_bin (fun t -> t.header)
|
|
||||||
(* TODO
|
|
||||||
Z.to_bits is in little endian but we need it in big endian *)
|
|
||||||
|+ field cstring (fun t -> z_to_bigendian_bits t.n)
|
|
||||||
|+ field cstring (fun t -> z_to_bigendian_bits t.e)
|
|
||||||
|> sealr
|
|
||||||
end
|
|
||||||
|
|
||||||
(* --- Various --- *)
|
(* --- Various --- *)
|
||||||
|
|
||||||
module RefreshCommitmentP = MK_64 ()
|
module RefreshCommitmentP = MK_64 ()
|
||||||
|
|
|
||||||
1
src/dune
1
src/dune
|
|
@ -18,6 +18,7 @@
|
||||||
angstrom
|
angstrom
|
||||||
zarith ;
|
zarith ;
|
||||||
mirage-crypto
|
mirage-crypto
|
||||||
|
digestif
|
||||||
duration
|
duration
|
||||||
vif
|
vif
|
||||||
fmt
|
fmt
|
||||||
|
|
|
||||||
|
|
@ -28,16 +28,8 @@ let mk_future_denom denom_secmod_sign_f
|
||||||
in
|
in
|
||||||
let denom_secmod_sig =
|
let denom_secmod_sig =
|
||||||
let open Binary_formats in
|
let open Binary_formats in
|
||||||
let h_denom_pub =
|
let h_denom_pub = DenominationHash.mk pub in
|
||||||
(* TODO hash *)
|
let h_section_name = HashCode.of_string section_name in
|
||||||
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 anchor_time =
|
let anchor_time =
|
||||||
TimeAbsoluteNBO.{ v= Util.ptime_to_int64_us stamp_start }
|
TimeAbsoluteNBO.{ v= Util.ptime_to_int64_us stamp_start }
|
||||||
in
|
in
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ 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 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 header = { n_len= Z.size n; e_len= Z.size e } in
|
||||||
let v = { header; n; e } in
|
let v = { header; n; e } in
|
||||||
let s = Bin.to_string bin v in
|
let s = Bin.to_string bin v in
|
||||||
|
|
@ -143,7 +143,7 @@ module RsaPublicKey = struct
|
||||||
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
|
||||||
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
|
let+ v = Mirage_crypto_pk.Rsa.pub ~n:v.n ~e:v.e |> unwrap_err_msg in
|
||||||
v
|
v
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,8 @@ let () =
|
||||||
check_bad ", \"foo\"";
|
check_bad ", \"foo\"";
|
||||||
()
|
()
|
||||||
|
|
||||||
|
(*
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let open Binary_formats.WithdrawConfirmationPS in
|
let open Binary_formats.WithdrawConfirmationPS in
|
||||||
let str64 = String.init 64 (fun i -> Char.unsafe_chr (i + 1)) in
|
let str64 = String.init 64 (fun i -> Char.unsafe_chr (i + 1)) in
|
||||||
|
|
@ -88,3 +90,5 @@ let () =
|
||||||
Printf.printf "%s" raw_str;
|
Printf.printf "%s" raw_str;
|
||||||
*)
|
*)
|
||||||
()
|
()
|
||||||
|
|
||||||
|
*)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue