Merge pull request #4738 from positiveblue/fix-wordlist-check

aezeed: fix mnemonic word validation
This commit is contained in:
Olaoluwa Osuntokun 2020-11-06 13:19:23 -08:00 committed by GitHub
commit 4f4adab1dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

@ -6,7 +6,6 @@ import (
"encoding/binary" "encoding/binary"
"hash/crc32" "hash/crc32"
"io" "io"
"strings"
"time" "time"
"github.com/Yawning/aez" "github.com/Yawning/aez"
@ -506,8 +505,13 @@ func (m *Mnemonic) Decipher(pass []byte) ([DecipheredCipherSeedSize]byte, error)
// Before we attempt to map the mnemonic back to the original // Before we attempt to map the mnemonic back to the original
// ciphertext, we'll ensure that all the word are actually a part of // ciphertext, we'll ensure that all the word are actually a part of
// the current default word list. // the current default word list.
wordDict := make(map[string]struct{}, len(defaultWordList))
for _, word := range defaultWordList {
wordDict[word] = struct{}{}
}
for i, word := range m { for i, word := range m {
if !strings.Contains(englishWordList, word) { if _, ok := wordDict[word]; !ok {
emptySeed := [DecipheredCipherSeedSize]byte{} emptySeed := [DecipheredCipherSeedSize]byte{}
return emptySeed, ErrUnknownMnenomicWord{ return emptySeed, ErrUnknownMnenomicWord{
Word: word, Word: word,

@ -550,6 +550,22 @@ func TestDecipherUnknownMnenomicWord(t *testing.T) {
t.Fatalf("wrong index detected: expected %v, got %v", t.Fatalf("wrong index detected: expected %v, got %v",
randIndex, wordErr.Index) randIndex, wordErr.Index)
} }
// If the mnemonic includes a word that is not in the englishList
// it fails, even when it is a substring of a valid word
// Example: `heart` is in the list, `hear` is not
mnemonic[randIndex] = "hear"
// If we attempt to map back to the original cipher seed now, then we
// should get ErrUnknownMnenomicWord.
_, err = mnemonic.ToCipherSeed(pass)
if err == nil {
t.Fatalf("expected ErrUnknownMnenomicWord error")
}
_, ok = err.(ErrUnknownMnenomicWord)
if !ok {
t.Fatalf("expected ErrUnknownMnenomicWord instead got %T", err)
}
} }
// TestDecipherIncorrectMnemonic tests that if we obtain a cipherseed, but then // TestDecipherIncorrectMnemonic tests that if we obtain a cipherseed, but then
@ -582,7 +598,6 @@ func TestDecipherIncorrectMnemonic(t *testing.T) {
if err != ErrIncorrectMnemonic { if err != ErrIncorrectMnemonic {
t.Fatalf("expected ErrIncorrectMnemonic error") t.Fatalf("expected ErrIncorrectMnemonic error")
} }
} }
// TODO(roasbeef): add test failure checksum fail is modified, new error // TODO(roasbeef): add test failure checksum fail is modified, new error