/
bip39_mnemonic.go
278 lines (238 loc) · 7.41 KB
/
bip39_mnemonic.go
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
// Copyright (c) 2020 Emanuele Bellocchia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This file cointains mnemonic generation/validation for bip39 package.
//
package bip39
//
// Imports
//
import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"strconv"
"strings"
"golang.org/x/crypto/pbkdf2"
)
//
// Constants
//
const (
// Words number
WordsNum12 = 12
WordsNum15 = 15
WordsNum18 = 18
WordsNum21 = 21
WordsNum24 = 24
// Word bit length
wordBitLen = 11
// Modified for seed salt
seedSaltMod = "mnemonic"
// Key length for PBKDF2 algorithm
seedPbkdf2KeyLen = 64
// Number of round for PBKDF2 algorithm
seedPbkdf2Round = 2048
)
//
// Variables
//
var (
// ErrWordsNum is returned when trying to generate mnemonic with invalid words number
ErrWordsNum = errors.New("The specified words number is not valid for mnemonic generation")
// ErrInvalidWord is returned when trying to get entropy or validating a mnemonic with invalid words
ErrInvalidWord = errors.New("The mnemonic contains an invalid word")
// ErrChecksum is returned when trying to get entropy or validating a mnemonic with invalid checksum
ErrChecksum = errors.New("The checksum of the mnemonic is not valid")
// Helper map for checking words number validity
wordsNumMap = map[int]bool {
WordsNum12 : true,
WordsNum15 : true,
WordsNum18 : true,
WordsNum21 : true,
WordsNum24 : true,
}
)
//
// Types
//
// Structure for mnemonic
type Mnemonic struct {
Words string
}
//
// Exported functions
//
// Generate mnemonic from the specified words number.
// A random entropy is used for generating mnemonic.
func MnemonicFromWordsNum(wordsNum int) (*Mnemonic, error) {
// Validate words number
err := validateWordsNum(wordsNum)
if err != nil {
return nil, err
}
// Get entropy bit length from words number
entropyBitLen := (wordsNum * 11) - (wordsNum / 3)
// Generate entropy
entropy, _ := GenerateEntropy(entropyBitLen)
// Generate mnemonic from entropy
return MnemonicFromEntropy(entropy)
}
// Generate mnemonic from the specific entropy.
// The entropy slice shall be of a valid length.
func MnemonicFromEntropy(entropy []byte) (*Mnemonic, error) {
// Validate entropy bit length
err := validateEntropyBitLen(len(entropy) * 8)
if err != nil {
return nil, err
}
// Convert entropy to binary string
entropyBinStr := bytesToBinaryString(entropy)
// Compute checksum as binary string
chksumBinStr := entropyChecksumBinStr(entropy)
// Append it to entropy
mnemonicBinStr := entropyBinStr + chksumBinStr
// Create slice for mnemonic
mnemonicLen := len(mnemonicBinStr) / wordBitLen
mnemonic := make([]string, 0, mnemonicLen)
// Split binary string in groups of 11-bit and map them to the words list
for i := 0; i < mnemonicLen; i++ {
// Get current word binary string
wordStrBin := mnemonicBinStr[i * wordBitLen: (i + 1) * wordBitLen]
// Convert to integer
wordIdx, _ := strconv.ParseInt(wordStrBin, 2, 16)
// Append the correspondent word
mnemonic = append(mnemonic, wordsListEn[wordIdx])
}
return &Mnemonic {
Words: strings.Join(mnemonic, " "),
}, nil
}
// Create mnemonic object from a mnemonic string.
func MnemonicFromString(mnemonic string) (*Mnemonic) {
return &Mnemonic {
Words: mnemonic,
}
}
// Convert a mnemonic back to entropy bytes.
// Error is returned if mnemonic or checksum is not valid.
func (mnemonic *Mnemonic) ToEntropy() ([]byte, error) {
// Get binary strings from mnemonic
entropyBinStr, chksumBinStr, err := mnemonic.getBinaryStrings()
if err != nil {
return nil, err
}
// Get entropy bytes
entropy, _ := binaryStringToBytes(entropyBinStr)
// Compute checksum
chksumComp := entropyChecksumBinStr(entropy)
// Compare checksum
if chksumComp != chksumBinStr {
return nil, ErrChecksum
}
return entropy, nil
}
// Validate a mnemonic.
// For being valid, all the mnemonic words shall exists in the words list and the checksum shall be valid.
func (mnemonic *Mnemonic) Validate() error {
// Get binary strings from mnemonic
entropyBinStr, chksumBinStr, err := mnemonic.getBinaryStrings()
if err != nil {
return err
}
// Get entropy bytes
entropy, _ := binaryStringToBytes(entropyBinStr)
// Compute checksum
chksumComp := entropyChecksumBinStr(entropy)
// Compare checksum
if chksumComp != chksumBinStr {
return ErrChecksum
}
return nil
}
// Get if a mnemonic is valid.
// It's the same of the Validate method but returns bool instead of error.
func (mnemonic *Mnemonic) IsValid() bool {
return mnemonic.Validate() == nil
}
// Generate the seed from a mnemonic using the specified passphrase for protection.
func (mnemonic *Mnemonic) GenerateSeed(passphrase string) ([]byte, error) {
// Validate mnemonic
err := mnemonic.Validate()
if err != nil {
return nil, err
}
// Get salt
salt := seedSaltMod + passphrase
// Generate seed
return pbkdf2.Key([]byte(mnemonic.Words), []byte(salt), seedPbkdf2Round, seedPbkdf2KeyLen, sha512.New), nil
}
//
// Not-exported functions
//
// Validate the specified words number.
func validateWordsNum(wordsNum int) error {
if !wordsNumMap[wordsNum] {
return ErrWordsNum
}
return nil
}
// Compute checksum of the specified entropy bytes, returned as a binary string.
func entropyChecksumBinStr(slice []byte) string {
// Compute SHA256
hash := sha256.Sum256(slice)
// Convert to binary string
hashStr := bytesToBinaryString(hash[:])
// Get checksum length in bits
chksumBitLen := len(slice) / 4
return hashStr[:chksumBitLen]
}
// Get the binary strings back from a mnemonic.
// The function returns both entropy and checksum parts.
func (mnemonic *Mnemonic) getBinaryStrings() (string, string, error) {
// Get word list
wordsList := strings.Split(mnemonic.Words, " ")
// Validate words number
err := validateWordsNum(len(wordsList))
if err != nil {
return "", "", err
}
// Build the binary string by converting each word index
var strBuf bytes.Buffer
for _, word := range wordsList {
// Use binary search for getting the word index
wordIdx := stringBinarySearch(wordsListEn, word)
// Error if not found
if wordIdx == -1 {
return "", "", ErrInvalidWord
}
// Convert the index to 11-bit binary string
strBuf.WriteString(fmt.Sprintf("%.11b", wordIdx))
}
// Get mnemonic binary string
mnemonicBinStr := strBuf.String()
// Compute checksum length and index
chksumLen := len(mnemonicBinStr) / 33
chksumIdx := len(mnemonicBinStr) - chksumLen
// Split mnemonic
return mnemonicBinStr[:chksumIdx], mnemonicBinStr[chksumIdx:], nil
}