Verified Commit b9eba66b authored by Vincent Bernardoff's avatar Vincent Bernardoff Committed by Grégoire Henry

Vendors/BIP39: drop `nocrypto`, use new `pbkdf`

parent 5294a144
......@@ -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"}
......
......@@ -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
......
......@@ -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. *)
......
......@@ -2,7 +2,7 @@
(name bip39)
(public_name bip39)
(modules english bip39)
(libraries nocrypto pbkdf))
(libraries bigstring hacl pbkdf))
(rule
(targets english.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 = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment