diff --git a/vendors/ocaml-bip39/bip39.opam b/vendors/ocaml-bip39/bip39.opam index 9c7501a4c..e772d957c 100644 --- a/vendors/ocaml-bip39/bip39.opam +++ b/vendors/ocaml-bip39/bip39.opam @@ -15,7 +15,8 @@ depends: [ "dune" {build & = "1.0.1"} "base" {build & >= "v0.10.0"} "stdio" {build & >= "v0.10.0"} - "nocrypto" {>= "0.5.4"} + "hacl" + "bigstring" {>= "0.2"} "pbkdf" {>= "0.2.0"} "hex" {test & >= "1.2.0"} "alcotest" {test & >= "0.8.1"} diff --git a/vendors/ocaml-bip39/src/bip39.ml b/vendors/ocaml-bip39/src/bip39.ml index 03fbe0dd1..45232ec0e 100644 --- a/vendors/ocaml-bip39/src/bip39.ml +++ b/vendors/ocaml-bip39/src/bip39.ml @@ -8,14 +8,14 @@ open StdLabels let acceptable_num_words = [12 ; 15 ; 18 ; 21 ; 24] type entropy = { - bytes : Cstruct.t ; + bytes : Bigstring.t ; length : int ; digest_length : int ; num_words : int ; } let entropy_of_bytes bytes = - match Cstruct.len bytes with + match Bigstring.length bytes with | 16 -> Some { bytes ; length = 16 ; digest_length = 4 ; num_words = 12 } | 20 -> Some { bytes ; length = 20 ; digest_length = 5 ; num_words = 15 } | 24 -> Some { bytes ; length = 24 ; digest_length = 6 ; num_words = 18 } @@ -112,16 +112,16 @@ let of_entropy entropy = match entropy_of_bytes entropy with | None -> invalid_arg "Bip39.of_entropy: wrong entropy length" | Some { bytes ; digest_length ; _ } -> - let digest = Cstruct.get_char (Nocrypto.Hash.SHA256.digest entropy) 0 in + let digest = Bigstring.get (Hacl.Hash.SHA256.digest entropy) 0 in let digest = list_sub (bits_of_char digest) digest_length in - let entropy = bits_of_bytes (Cstruct.to_string bytes) @ digest in + let entropy = bits_of_bytes (Bigstring.to_string bytes) @ digest in List.map (pack entropy 11) ~f:int_of_bits -let to_seed ?(passphrase="") t = +let to_seed ?(passphrase=Bigstring.empty) t = let words = to_words t in - let password = Cstruct.of_string (String.concat ~sep:" " words) in - let salt = Cstruct.of_string ("mnemonic" ^ passphrase) in - Pbkdf.pbkdf2 ~prf:`SHA512 ~password ~salt ~count:2048 ~dk_len:64l + let password = Bigstring.of_string (String.concat ~sep:" " words) in + let salt = Bigstring.(concat "" [of_string "mnemonic" ; passphrase]) in + Pbkdf.SHA512.pbkdf2 ~password ~salt ~count:2048 ~dk_len:64l (*--------------------------------------------------------------------------- Copyright (c) 2017 Vincent Bernardoff diff --git a/vendors/ocaml-bip39/src/bip39.mli b/vendors/ocaml-bip39/src/bip39.mli index fc2b4bd56..87f7236b5 100644 --- a/vendors/ocaml-bip39/src/bip39.mli +++ b/vendors/ocaml-bip39/src/bip39.mli @@ -28,13 +28,13 @@ val to_words : t -> string list (** [to_words mnemonic] is the list of words corresponding to [mnemonic]. *) -val of_entropy : Cstruct.t -> t +val of_entropy : Bigstring.t -> t (** [of_entropy bytes] is the mnemonic derived from [bytes]. @raises [Invalid_argument] is [List.length bytes] is not in { 16, 20, 24, 28, 32 }. *) -val to_seed : ?passphrase:string -> t -> Cstruct.t +val to_seed : ?passphrase:Bigstring.t -> t -> Bigstring.t (** [to_seed ?passphrase mnemonic] is 64 bytes derived from a BIP39 mnemonic [mnemonic], using the optional passphrase [passphrase] if provided. *) diff --git a/vendors/ocaml-bip39/src/dune b/vendors/ocaml-bip39/src/dune index 06b0d9d82..06bda597c 100644 --- a/vendors/ocaml-bip39/src/dune +++ b/vendors/ocaml-bip39/src/dune @@ -2,7 +2,7 @@ (name bip39) (public_name bip39) (modules english bip39) - (libraries nocrypto pbkdf)) + (libraries bigstring hacl pbkdf)) (rule (targets english.ml) diff --git a/vendors/ocaml-bip39/test/test.ml b/vendors/ocaml-bip39/test/test.ml index 0fbef0880..efb30cbb7 100644 --- a/vendors/ocaml-bip39/test/test.ml +++ b/vendors/ocaml-bip39/test/test.ml @@ -121,11 +121,12 @@ let pp_diff ppf (l1, l2) = let vectors () = ListLabels.iteri vectors ~f:begin fun i { entropy ; words ; seed } -> let words = String.split_on_char ' ' words in - let mnemonic = Bip39.of_entropy (Cstruct.of_string (Hex.to_string entropy)) in + let mnemonic = Bip39.of_entropy (Cstruct.to_bigarray (Hex.to_cstruct entropy)) in let words_computed = Bip39.to_words mnemonic in assert (words = words_computed) ; - let seed_computed = Bip39.to_seed ~passphrase:"TREZOR" mnemonic in - assert ((Hex.to_cstruct seed) = seed_computed) + let seed_computed = + Bip39.to_seed ~passphrase:(Bigstring.of_string "TREZOR") mnemonic in + assert (Cstruct.to_bigarray (Hex.to_cstruct seed) = seed_computed) end let basic = [