Skip to content

Commit 17189ae

Browse files
committed
hkd32: Merge code from tiny-bip39
All of the `bip39` code is high-sensitivity in that it deals with master derivation passwords for secret keys. Unfortunately, the `bip39` crate seems abandoned, which is not a good state for such a high-sensitivity crate to be in. This commit merges in the code from `tiny-bip39` as of the following git commit bumping the version to v0.6.2: > Version bump maciejhirsz/tiny-bip39@dfcb9c9 This commit makes no other changes to the incorporated files, and they have not been "wired up" to the rest of `hkd32` yet.
1 parent 7304b2f commit 17189ae

File tree

15 files changed

+3584
-11
lines changed

15 files changed

+3584
-11
lines changed

hkd32/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ homepage = "https://github.com/iqlusioninc/crates/"
1414
repository = "https://github.com/iqlusioninc/crates/tree/develop/hkd32"
1515
readme = "README.md"
1616
categories = ["cryptography", "no-std"]
17-
keywords = ["bip32", "derivation", "hd", "hmac", "key"]
17+
keywords = ["crypto", "bip32", "bip39", "derivation", "mnemonic"]
1818

1919
[badges]
2020
maintenance = { status = "passively-maintained" }

hkd32/LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2017-2018 Stephen Oliver
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
this software and associated documentation files (the "Software"), to deal in
6+
the Software without restriction, including without limitation the rights to
7+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
the Software, and to permit persons to whom the Software is furnished to do so,
9+
subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

hkd32/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Crate][crate-image]][crate-link]
55
[![Docs][docs-image]][docs-link]
66
[![Apache 2.0 Licensed][license-image]][license-link]
7-
![Rust 1.36+][rustc-image]
7+
![MSRV][rustc-image]
88
[![Build Status][build-image]][build-link]
99
[![Gitter Chat][gitter-image]][gitter-link]
1010

@@ -26,17 +26,14 @@ an initial 32-bytes of input key material.
2626

2727
Copyright © 2019 iqlusion
2828

29-
Licensed under the Apache License, Version 2.0 (the "License");
30-
you may not use this file except in compliance with the License.
31-
You may obtain a copy of the License at
29+
Includes code from the `bip39` crate. Copyright © 2017-2018 Stephen Oliver,
30+
with contributions by Maciej Hirsz.
3231

33-
https://www.apache.org/licenses/LICENSE-2.0
32+
**hkd32** is distributed under the terms of either the MIT license
33+
or the Apache License (Version 2.0), at your option.
3434

35-
Unless required by applicable law or agreed to in writing, software
36-
distributed under the License is distributed on an "AS IS" BASIS,
37-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38-
See the License for the specific language governing permissions and
39-
limitations under the License.
35+
See [LICENSE] (Apache License, Version 2.0) file in the `iqlusioninc/crates`
36+
toplevel directory of this repository or [LICENSE-MIT] for details.
4037

4138
## Contribution
4239

@@ -62,3 +59,5 @@ without any additional terms or conditions.
6259

6360
[bip32]: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
6461
[bip39]: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
62+
[LICENSE]: https://github.com/iqlusioninc/crates/blob/develop/LICENSE
63+
[LICENSE-MIT]: https://github.com/iqlusioninc/crates/blob/develop/hkd32/LICENSE-MIT

hkd32/src/mnemonic/crypto.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! These are internal helper functions used when creating a new [`Mnemonic`][Mnemonic], and when turning a [`Mnemonic`][Mnemonic]
2+
//! into a [`Seed`][Seed].
3+
//!
4+
//! [Mnemonic]: ../mnemonic/struct.Mnemonic.html
5+
//! [Seed]: ../seed/struct.Seed.html
6+
//!
7+
8+
extern crate rand;
9+
use self::rand::{ thread_rng, RngCore };
10+
use sha2::Digest;
11+
use hmac::Hmac;
12+
13+
const PBKDF2_ROUNDS: usize = 2048;
14+
const PBKDF2_BYTES: usize = 64;
15+
16+
/// SHA256 helper function, internal to the crate
17+
///
18+
pub(crate) fn sha256_first_byte(input: &[u8]) -> u8 {
19+
sha2::Sha256::digest(input).as_ref()[0]
20+
}
21+
22+
/// Random byte generator, used to create new mnemonics
23+
///
24+
pub(crate) fn gen_random_bytes(byte_length: usize) -> Vec<u8> {
25+
let mut rng = thread_rng();
26+
let mut bytes = vec![0u8; byte_length];
27+
28+
rng.fill_bytes(&mut bytes);
29+
30+
bytes
31+
}
32+
/// PBKDF2 helper, used to generate [`Seed`][Seed] from [`Mnemonic`][Mnemonic]
33+
///
34+
/// [Mnemonic]: ../mnemonic/struct.Mnemonic.html
35+
/// [Seed]: ../seed/struct.Seed.html
36+
///
37+
pub(crate) fn pbkdf2(input: &[u8], salt: &str) -> Vec<u8> {
38+
let mut seed = vec![0u8; PBKDF2_BYTES];
39+
40+
pbkdf2::pbkdf2::<Hmac<sha2::Sha512>>(input, salt.as_bytes(), PBKDF2_ROUNDS, &mut seed);
41+
42+
seed
43+
}

hkd32/src/mnemonic/error.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use mnemonic_type::MnemonicType;
2+
3+
#[derive(Debug, Fail)]
4+
pub enum ErrorKind {
5+
#[fail(display = "invalid checksum")]
6+
InvalidChecksum,
7+
#[fail(display = "invalid word in phrase")]
8+
InvalidWord,
9+
#[fail(display = "invalid keysize: {}", _0)]
10+
InvalidKeysize(usize),
11+
#[fail(display = "invalid number of words in phrase: {}", _0)]
12+
InvalidWordLength(usize),
13+
#[fail(display = "invalid entropy length {}bits for mnemonic type {:?}", _0, _1)]
14+
InvalidEntropyLength(usize, MnemonicType),
15+
}

0 commit comments

Comments
 (0)