aboutsummaryrefslogtreecommitdiff
path: root/src/mnemonic.rs
blob: 02f9f25b115538c3c4fe42aefe6028cbc14ab750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! BIP-0039 mnemonic phrase implementation.

mod language;
mod wordlist;

pub use self::{language::Language, wordlist::WORD_COUNT};
use crate::{hash, rand};
use anyhow::{anyhow, ensure, Result};
use hmac::Hmac;
use sha2::Sha512;
use std::{
    convert::TryInto,
    fmt::{self, Display, Formatter},
    mem,
    ops::Deref,
    str::FromStr,
};
use unicode_normalization::UnicodeNormalization as _;

/// A seed generated from a BIP-0039 mnemonic used for an HD wallet.
#[derive(Clone, Debug)]
pub struct Mnemonic {
    /// The language for the mnemonic phrase.
    language: Language,
    /// A buffer containing the mnemonic's binary representation and hash used
    /// for checksums.
    buf: [u8; 64],
    /// The length of the seed bytes without the 32 byte hash.
    len: usize,
}

/// Masking value for a mnemonic word.
const WORD_MASK: usize = WORD_COUNT - 1;
/// The number of bits represented by each mnemonic word.
const WORD_BITS: usize = WORD_MASK.count_ones() as _;

impl Mnemonic {
    /// Generates a new cryptographically random seed for the specified mnemonic
    /// word length.
    ///
    /// This method returns an error if it fails if the specified mnemonic word
    /// length is invalid (it must be in the range `12..=24`) or if there is an
    /// error reading cryptographically strong entropy from the operating
    /// system.
    pub fn random(language: Language, mnemonic_length: usize) -> Result<Self> {
        let len = mnemonic_to_byte_length(mnemonic_length)?;
        let buf = {
            let mut buf = [0; 64];
            let (seed, hash) = buf.split_at_mut(len);

            rand::get_entropy(&mut *seed)?;
            hash[..32].copy_from_slice(&hash::sha256(seed));

            buf
        };

        Ok(Self { language, buf, len })
    }

    /// Parses a mnemonic from a phrase.
    pub fn from_phrase(mnemonic: impl AsRef<str>) -> Result<Self> {
        let (language, words) = Language::split(mnemonic.as_ref())?;

        let len = mnemonic_to_byte_length(words.len())?;
        let buf = {
            let wordlist = language.wordlist();

            let mut buf = [0; 64];
            let (seed, hash) = buf.split_at_mut(len);

            let mut acc = 0;
            let mut bit_offset = 0;
            let mut byte_offset = 0;
            for word in &words {
                let index = wordlist
                    .search(word)
                    .ok_or_else(|| anyhow!("invalid BIP-0039 {} word '{}'", language, word))?;
                acc = (acc << WORD_BITS) | index;

                bit_offset += WORD_BITS;
                while bit_offset > 8 {
                    bit_offset -= 8;
                    seed[byte_offset] = ((acc >> bit_offset) & 0xff) as _;
                    byte_offset += 1;
                }
            }

            // NOTE: The remaining bits are checksum bits that we need to
            // verify now.
            debug_assert_eq!(len * 8 + bit_offset, words.len() * WORD_BITS);
            debug_assert_eq!(byte_offset, len);

            hash[..32].copy_from_slice(&hash::sha256(seed));

            let checksum_mask = (1 << bit_offset) - 1;
            ensure!(
                hash[0] >> (8 - bit_offset) == (acc & checksum_mask) as _,
                "mnemonic checksum verification failure",
            );

            buf
        };

        Ok(Self { language, buf, len })
    }

    /// Gets the mnemonic's binary representation as a slice of bytes.
    #[cfg(test)]
    pub fn as_bytes(&self) -> &[u8] {
        &self.buf[..self.len]
    }

    /// Gets the BIP-0039 mnemonic word length.
    pub fn mnemonic_length(&self) -> usize {
        ((self.len * 8) / WORD_BITS) + 1
    }

    /// Returns the BIP-0039 mnemonic phrase.
    pub fn to_phrase(&self) -> String {
        let wordlist = self.language.wordlist();
        let separator = self.language.separator();

        let mut buf = String::new();
        for i in 0..self.mnemonic_length() {
            let bit_offset = i * WORD_BITS;

            let offset = bit_offset / 8;
            let shift = mem::size_of::<usize>() * 8 - WORD_BITS - bit_offset % 8;
            let index = (usize::from_be_bytes(
                self.buf[offset..][..mem::size_of::<usize>()]
                    .try_into()
                    .unwrap(),
            ) >> shift)
                & WORD_MASK;

            buf.push_str(wordlist.word(index as _));
            buf.push(separator);
        }

        buf.pop();
        buf
    }

    /// Gets the PBKDF2 stretched binary seed for this mnemonic.
    pub fn seed(&self, password: impl AsRef<str>) -> Seed {
        const ROUNDS: u32 = 2048;

        let mut buf = [0; 64];
        let salt = format!("mnemonic{}", password.as_ref());
        pbkdf2::pbkdf2::<Hmac<Sha512>>(
            self.to_phrase().as_bytes(),
            salt.nfkd().to_string().as_bytes(),
            ROUNDS,
            &mut buf,
        );

        Seed(buf)
    }
}

impl Display for Mnemonic {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(&self.to_phrase())
    }
}

impl FromStr for Mnemonic {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        Self::from_phrase(s)
    }
}

/// A 64 byte seed derived from a BIP-0039 mnemonic.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Seed([u8; 64]);

impl AsRef<[u8]> for Seed {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl Deref for Seed {
    type Target = [u8; 64];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

fn mnemonic_to_byte_length(len: usize) -> Result<usize> {
    ensure!(matches!(len, 12..=24), "invalid mnemonic length {}", len);

    // NOTE: Derived from the BIP-0039 spec where `CS` is the checksum bit
    // length, `ENT` is the entropy bit length (so `8 * byte_length`) and `MS`
    // is the mnemonic word length.
    // ```
    // CS = ENT / 32
    // MS = (ENT + CS) / 11
    // ```
    // <https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic>
    Ok((len * WORD_BITS * 32 / 33) / 8)
}

#[cfg(test)]
mod tests {
    use super::*;
    use hex_literal::hex;

    #[test]
    fn random_mnemonic() {
        for &(bit_length, mnemonic_length) in
            &[(128, 12), (160, 15), (192, 18), (224, 21), (256, 24)]
        {
            let mnemonic = Mnemonic::random(Language::English, mnemonic_length).unwrap();
            assert_eq!(mnemonic.as_bytes().len() * 8, bit_length);
            let all_zeros = mnemonic.as_bytes().iter().all(|&byte| byte == 0);
            assert!(!all_zeros);
        }
    }

    #[test]
    fn mnemonic_phrases() {
        for &(bytes, phrase, password, seed) in &[
            (
                &hex!("00000000000000000000000000000000")[..],
                "abandon abandon abandon abandon abandon abandon \
                 abandon abandon abandon abandon abandon about",
                "TREZOR",
                hex!(
                    "c552 57c3 60c0 7c72 029a ebc1 b53c 05ed
                     0362 ada3 8ead 3e3e 9efa 3708 e534 9553
                     1f09 a698 7599 d182 64c1 e1c9 2f2c f141
                     630c 7a3c 4ab7 c81b 2f00 1698 e746 3b04"
                ),
            ),
            (
                &hex!("92903465e029df56cab416a53b015396")[..],
                "myth like bonus scare over problem \
                 client lizard pioneer submit female collect",
                "",
                hex!(
                    "15e7 bbc6 ac54 a721 ad44 0f8e f7d1 fa7c
                     4f77 ae5e c71e 2418 7649 e9d2 2802 2655
                     b9e6 fb36 59f8 e4b2 274a c3b1 955b f9e5
                     8f15 0492 c44e 7aa1 6109 5ba0 ad92 6e9e"
                ),
            ),
            (
                &hex!("0000000000000000000000000000000000000000000000000000000000000000")[..],
                "abandon abandon abandon abandon abandon abandon \
                 abandon abandon abandon abandon abandon abandon \
                 abandon abandon abandon abandon abandon abandon \
                 abandon abandon abandon abandon abandon art",
                "TREZOR",
                hex!(
                    "bda8 5446 c684 1370 7090 a520 22ed d26a
                     1c94 6229 5029 f2e6 0cd7 c4f2 bbd3 0971
                     70af 7a4d 7324 5caf a9c3 cca8 d561 a7c3
                     de6f 5d4a 10be 8ed2 a5e6 08d6 8f92 fcc8"
                ),
            ),
            (
                &hex!("f585c11aec520db57dd353c69554b21a89b20fb0650966fa0a9d6f74fd989d8f")[..],
                "void come effort suffer camp survey \
                 warrior heavy shoot primary clutch crush \
                 open amazing screen patrol group space \
                 point ten exist slush involve unfold",
                "TREZOR",
                hex!(
                    "01f5 bced 59de c48e 362f 2c45 b5de 68b9
                     fd6c 92c6 634f 44d6 d40a ab69 0565 06f0
                     e355 24a5 1803 4ddc 1192 e1da cd32 c1ed
                     3eaa 3c3b 131c 88ed 8e7e 54c4 9a5d 0998"
                ),
            ),
        ] {
            let mnemonic = Mnemonic::from_phrase(phrase).unwrap();
            assert_eq!(mnemonic.as_bytes(), bytes);
            assert_eq!(*mnemonic.seed(password), seed);
            assert_eq!(mnemonic.to_phrase(), phrase);
        }
    }
}