Vendors/BIP39: drop nocrypto, use new pbkdf

This commit is contained in:
Vincent Bernardoff 2018-07-02 11:50:24 +02:00 committed by Grégoire Henry
parent 5294a144b1
commit b9eba66b4c
No known key found for this signature in database
GPG Key ID: 50D984F20BD445D2
5 changed files with 17 additions and 15 deletions

View File

@ -15,7 +15,8 @@ depends: [
"dune" {build & = "1.0.1"} "dune" {build & = "1.0.1"}
"base" {build & >= "v0.10.0"} "base" {build & >= "v0.10.0"}
"stdio" {build & >= "v0.10.0"} "stdio" {build & >= "v0.10.0"}
"nocrypto" {>= "0.5.4"} "hacl"
"bigstring" {>= "0.2"}
"pbkdf" {>= "0.2.0"} "pbkdf" {>= "0.2.0"}
"hex" {test & >= "1.2.0"} "hex" {test & >= "1.2.0"}
"alcotest" {test & >= "0.8.1"} "alcotest" {test & >= "0.8.1"}

View File

@ -8,14 +8,14 @@ open StdLabels
let acceptable_num_words = [12 ; 15 ; 18 ; 21 ; 24] let acceptable_num_words = [12 ; 15 ; 18 ; 21 ; 24]
type entropy = { type entropy = {
bytes : Cstruct.t ; bytes : Bigstring.t ;
length : int ; length : int ;
digest_length : int ; digest_length : int ;
num_words : int ; num_words : int ;
} }
let entropy_of_bytes bytes = 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 } | 16 -> Some { bytes ; length = 16 ; digest_length = 4 ; num_words = 12 }
| 20 -> Some { bytes ; length = 20 ; digest_length = 5 ; num_words = 15 } | 20 -> Some { bytes ; length = 20 ; digest_length = 5 ; num_words = 15 }
| 24 -> Some { bytes ; length = 24 ; digest_length = 6 ; num_words = 18 } | 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 match entropy_of_bytes entropy with
| None -> invalid_arg "Bip39.of_entropy: wrong entropy length" | None -> invalid_arg "Bip39.of_entropy: wrong entropy length"
| Some { bytes ; digest_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 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 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 words = to_words t in
let password = Cstruct.of_string (String.concat ~sep:" " words) in let password = Bigstring.of_string (String.concat ~sep:" " words) in
let salt = Cstruct.of_string ("mnemonic" ^ passphrase) in let salt = Bigstring.(concat "" [of_string "mnemonic" ; passphrase]) in
Pbkdf.pbkdf2 ~prf:`SHA512 ~password ~salt ~count:2048 ~dk_len:64l Pbkdf.SHA512.pbkdf2 ~password ~salt ~count:2048 ~dk_len:64l
(*--------------------------------------------------------------------------- (*---------------------------------------------------------------------------
Copyright (c) 2017 Vincent Bernardoff Copyright (c) 2017 Vincent Bernardoff

View File

@ -28,13 +28,13 @@ val to_words : t -> string list
(** [to_words mnemonic] is the list of words corresponding to (** [to_words mnemonic] is the list of words corresponding to
[mnemonic]. *) [mnemonic]. *)
val of_entropy : Cstruct.t -> t val of_entropy : Bigstring.t -> t
(** [of_entropy bytes] is the mnemonic derived from [bytes]. (** [of_entropy bytes] is the mnemonic derived from [bytes].
@raises [Invalid_argument] is [List.length bytes] is not in { 16, @raises [Invalid_argument] is [List.length bytes] is not in { 16,
20, 24, 28, 32 }. *) 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 (** [to_seed ?passphrase mnemonic] is 64 bytes derived from a BIP39
mnemonic [mnemonic], using the optional passphrase [passphrase] if mnemonic [mnemonic], using the optional passphrase [passphrase] if
provided. *) provided. *)

View File

@ -2,7 +2,7 @@
(name bip39) (name bip39)
(public_name bip39) (public_name bip39)
(modules english bip39) (modules english bip39)
(libraries nocrypto pbkdf)) (libraries bigstring hacl pbkdf))
(rule (rule
(targets english.ml) (targets english.ml)

View File

@ -121,11 +121,12 @@ let pp_diff ppf (l1, l2) =
let vectors () = let vectors () =
ListLabels.iteri vectors ~f:begin fun i { entropy ; words ; seed } -> ListLabels.iteri vectors ~f:begin fun i { entropy ; words ; seed } ->
let words = String.split_on_char ' ' words in 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 let words_computed = Bip39.to_words mnemonic in
assert (words = words_computed) ; assert (words = words_computed) ;
let seed_computed = Bip39.to_seed ~passphrase:"TREZOR" mnemonic in let seed_computed =
assert ((Hex.to_cstruct seed) = seed_computed) Bip39.to_seed ~passphrase:(Bigstring.of_string "TREZOR") mnemonic in
assert (Cstruct.to_bigarray (Hex.to_cstruct seed) = seed_computed)
end end
let basic = [ let basic = [