2018-01-07 05:05:16 +00:00
|
|
|
// Copyright (c) 2014-2018, The Monero Project
|
2014-07-23 13:03:52 +00:00
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
|
|
// materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
|
|
// used to endorse or promote products derived from this software without specific
|
|
|
|
// prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2014-09-27 12:50:15 +00:00
|
|
|
/*!
|
|
|
|
* \file electrum-words.cpp
|
|
|
|
*
|
|
|
|
* \brief Mnemonic seed generation and wallet restoration from them.
|
|
|
|
*
|
2014-06-04 22:59:47 +00:00
|
|
|
* This file and its header file are for translating Electrum-style word lists
|
|
|
|
* into their equivalent byte representations for cross-compatibility with
|
|
|
|
* that method of "backing up" one's wallet keys.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
2014-06-05 03:08:35 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
2014-10-02 12:45:18 +00:00
|
|
|
#include <unordered_map>
|
2018-07-06 23:03:15 +00:00
|
|
|
#include "wipeable_string.h"
|
|
|
|
#include "misc_language.h"
|
2018-11-18 10:17:24 +00:00
|
|
|
#include "int-util.h"
|
2014-09-23 11:34:04 +00:00
|
|
|
#include "mnemonics/electrum-words.h"
|
2014-09-27 20:59:25 +00:00
|
|
|
#include <boost/crc.hpp>
|
2014-09-23 13:04:39 +00:00
|
|
|
|
2017-04-16 10:24:40 +00:00
|
|
|
#include "chinese_simplified.h"
|
2014-10-01 15:52:13 +00:00
|
|
|
#include "english.h"
|
2017-03-24 19:35:25 +00:00
|
|
|
#include "dutch.h"
|
2017-03-23 16:46:31 +00:00
|
|
|
#include "french.h"
|
2015-07-22 20:00:06 +00:00
|
|
|
#include "italian.h"
|
|
|
|
#include "german.h"
|
2014-10-01 15:52:13 +00:00
|
|
|
#include "spanish.h"
|
|
|
|
#include "portuguese.h"
|
|
|
|
#include "japanese.h"
|
2015-07-24 05:52:57 +00:00
|
|
|
#include "russian.h"
|
2017-08-13 14:52:33 +00:00
|
|
|
#include "esperanto.h"
|
2017-09-25 22:42:17 +00:00
|
|
|
#include "lojban.h"
|
2017-04-24 19:27:12 +00:00
|
|
|
#include "english_old.h"
|
2014-10-02 12:45:18 +00:00
|
|
|
#include "language_base.h"
|
|
|
|
#include "singleton.h"
|
2014-10-01 15:52:13 +00:00
|
|
|
|
2018-04-24 16:06:25 +00:00
|
|
|
#undef MONERO_DEFAULT_LOG_CATEGORY
|
|
|
|
#define MONERO_DEFAULT_LOG_CATEGORY "mnemonic"
|
|
|
|
|
2018-07-22 10:28:11 +00:00
|
|
|
namespace crypto
|
|
|
|
{
|
|
|
|
namespace ElectrumWords
|
|
|
|
{
|
|
|
|
std::vector<const Language::Base*> get_language_list();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:04:39 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
uint32_t create_checksum_index(const std::vector<epee::wipeable_string> &word_list,
|
2019-01-11 01:36:59 +00:00
|
|
|
const Language::Base *language);
|
|
|
|
bool checksum_test(std::vector<epee::wipeable_string> seed, const Language::Base *language);
|
2014-09-27 20:59:25 +00:00
|
|
|
|
2014-09-27 12:50:15 +00:00
|
|
|
/*!
|
2014-10-02 12:45:18 +00:00
|
|
|
* \brief Finds the word list that contains the seed words and puts the indices
|
|
|
|
* where matches occured in matched_indices.
|
|
|
|
* \param seed List of words to match.
|
2017-01-15 10:48:44 +00:00
|
|
|
* \param has_checksum The seed has a checksum word (maybe not checked).
|
2014-10-02 12:45:18 +00:00
|
|
|
* \param matched_indices The indices where the seed words were found are added to this.
|
2014-10-07 07:19:36 +00:00
|
|
|
* \param language Language instance pointer to write to after it is found.
|
2014-10-02 12:45:18 +00:00
|
|
|
* \return true if all the words were present in some language false if not.
|
2014-09-27 12:50:15 +00:00
|
|
|
*/
|
2018-07-06 23:03:15 +00:00
|
|
|
bool find_seed_language(const std::vector<epee::wipeable_string> &seed,
|
2014-10-07 07:19:36 +00:00
|
|
|
bool has_checksum, std::vector<uint32_t> &matched_indices, Language::Base **language)
|
2014-09-23 13:04:39 +00:00
|
|
|
{
|
2014-10-02 12:45:18 +00:00
|
|
|
// If there's a new language added, add an instance of it here.
|
|
|
|
std::vector<Language::Base*> language_instances({
|
2017-04-16 10:24:40 +00:00
|
|
|
Language::Singleton<Language::Chinese_Simplified>::instance(),
|
2014-10-02 12:45:18 +00:00
|
|
|
Language::Singleton<Language::English>::instance(),
|
2017-03-24 19:35:25 +00:00
|
|
|
Language::Singleton<Language::Dutch>::instance(),
|
2017-03-23 16:46:31 +00:00
|
|
|
Language::Singleton<Language::French>::instance(),
|
2014-10-02 12:45:18 +00:00
|
|
|
Language::Singleton<Language::Spanish>::instance(),
|
2015-07-22 20:00:06 +00:00
|
|
|
Language::Singleton<Language::German>::instance(),
|
|
|
|
Language::Singleton<Language::Italian>::instance(),
|
2014-10-02 12:45:18 +00:00
|
|
|
Language::Singleton<Language::Portuguese>::instance(),
|
|
|
|
Language::Singleton<Language::Japanese>::instance(),
|
2015-07-24 05:52:57 +00:00
|
|
|
Language::Singleton<Language::Russian>::instance(),
|
2017-08-13 14:52:33 +00:00
|
|
|
Language::Singleton<Language::Esperanto>::instance(),
|
2017-09-25 22:42:17 +00:00
|
|
|
Language::Singleton<Language::Lojban>::instance(),
|
2017-04-24 19:27:12 +00:00
|
|
|
Language::Singleton<Language::EnglishOld>::instance()
|
2014-10-02 12:45:18 +00:00
|
|
|
});
|
2017-01-15 10:48:44 +00:00
|
|
|
Language::Base *fallback = NULL;
|
2014-10-02 16:05:27 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
std::vector<epee::wipeable_string>::const_iterator it2;
|
|
|
|
matched_indices.reserve(seed.size());
|
|
|
|
|
2014-10-02 12:45:18 +00:00
|
|
|
// Iterate through all the languages and find a match
|
|
|
|
for (std::vector<Language::Base*>::iterator it1 = language_instances.begin();
|
|
|
|
it1 != language_instances.end(); it1++)
|
2014-09-26 18:04:35 +00:00
|
|
|
{
|
2019-01-11 01:36:59 +00:00
|
|
|
const std::unordered_map<epee::wipeable_string, uint32_t, Language::WordHash, Language::WordEqual> &word_map = (*it1)->get_word_map();
|
|
|
|
const std::unordered_map<epee::wipeable_string, uint32_t, Language::WordHash, Language::WordEqual> &trimmed_word_map = (*it1)->get_trimmed_word_map();
|
2014-10-02 12:45:18 +00:00
|
|
|
// To iterate through seed words
|
|
|
|
bool full_match = true;
|
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
epee::wipeable_string trimmed_word;
|
2014-10-02 12:45:18 +00:00
|
|
|
// Iterate through all the words and see if they're all present
|
2014-10-07 07:19:36 +00:00
|
|
|
for (it2 = seed.begin(); it2 != seed.end(); it2++)
|
2014-09-30 18:00:27 +00:00
|
|
|
{
|
2014-10-02 12:45:18 +00:00
|
|
|
if (has_checksum)
|
2014-09-30 18:00:27 +00:00
|
|
|
{
|
2015-06-21 10:28:16 +00:00
|
|
|
trimmed_word = Language::utf8prefix(*it2, (*it1)->get_unique_prefix_length());
|
2014-10-02 12:45:18 +00:00
|
|
|
// Use the trimmed words and map
|
2014-10-07 07:19:36 +00:00
|
|
|
if (trimmed_word_map.count(trimmed_word) == 0)
|
2014-10-02 12:45:18 +00:00
|
|
|
{
|
|
|
|
full_match = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-07 07:19:36 +00:00
|
|
|
matched_indices.push_back(trimmed_word_map.at(trimmed_word));
|
2014-09-30 18:00:27 +00:00
|
|
|
}
|
2014-10-02 12:45:18 +00:00
|
|
|
else
|
2014-09-30 18:00:27 +00:00
|
|
|
{
|
2014-10-02 12:45:18 +00:00
|
|
|
if (word_map.count(*it2) == 0)
|
|
|
|
{
|
|
|
|
full_match = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-02 17:06:02 +00:00
|
|
|
matched_indices.push_back(word_map.at(*it2));
|
2014-09-30 18:00:27 +00:00
|
|
|
}
|
2014-09-26 18:04:35 +00:00
|
|
|
}
|
2014-10-02 12:45:18 +00:00
|
|
|
if (full_match)
|
2017-01-15 10:48:44 +00:00
|
|
|
{
|
|
|
|
// if we were using prefix only, and we have a checksum, check it now
|
|
|
|
// to avoid false positives due to prefix set being too common
|
|
|
|
if (has_checksum)
|
2019-01-11 01:36:59 +00:00
|
|
|
if (!checksum_test(seed, *it1))
|
2017-01-15 10:48:44 +00:00
|
|
|
{
|
|
|
|
fallback = *it1;
|
|
|
|
full_match = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (full_match)
|
2014-10-02 12:45:18 +00:00
|
|
|
{
|
2014-10-07 07:19:36 +00:00
|
|
|
*language = *it1;
|
2018-04-24 16:06:25 +00:00
|
|
|
MINFO("Full match for language " << (*language)->get_english_language_name());
|
2014-10-02 12:45:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Some didn't match. Clear the index array.
|
2018-07-06 23:03:15 +00:00
|
|
|
memwipe(matched_indices.data(), matched_indices.size() * sizeof(matched_indices[0]));
|
2014-10-02 12:45:18 +00:00
|
|
|
matched_indices.clear();
|
2014-09-26 18:04:35 +00:00
|
|
|
}
|
2017-01-15 10:48:44 +00:00
|
|
|
|
|
|
|
// if we get there, we've not found a good match, but we might have a fallback,
|
|
|
|
// if we detected a match which did not fit the checksum, which might be a badly
|
|
|
|
// typed/transcribed seed in the right language
|
|
|
|
if (fallback)
|
|
|
|
{
|
|
|
|
*language = fallback;
|
2018-04-24 16:06:25 +00:00
|
|
|
MINFO("Fallback match for language " << (*language)->get_english_language_name());
|
2017-01-15 10:48:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:06:25 +00:00
|
|
|
MINFO("No match found");
|
2018-07-06 23:03:15 +00:00
|
|
|
memwipe(matched_indices.data(), matched_indices.size() * sizeof(matched_indices[0]));
|
2014-10-02 12:45:18 +00:00
|
|
|
return false;
|
2014-09-26 18:04:35 +00:00
|
|
|
}
|
2014-09-27 20:59:25 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Creates a checksum index in the word list array on the list of words.
|
2014-10-07 07:19:36 +00:00
|
|
|
* \param word_list Vector of words
|
|
|
|
* \param unique_prefix_length the prefix length of each word to use for checksum
|
|
|
|
* \return Checksum index
|
2014-09-27 20:59:25 +00:00
|
|
|
*/
|
2018-07-06 23:03:15 +00:00
|
|
|
uint32_t create_checksum_index(const std::vector<epee::wipeable_string> &word_list,
|
2019-01-11 01:36:59 +00:00
|
|
|
const Language::Base *language)
|
2014-09-27 20:59:25 +00:00
|
|
|
{
|
2019-01-11 01:36:59 +00:00
|
|
|
epee::wipeable_string trimmed_words = "", word;
|
2014-09-30 18:00:27 +00:00
|
|
|
|
2019-01-11 01:36:59 +00:00
|
|
|
const auto &word_map = language->get_word_map();
|
|
|
|
const auto &trimmed_word_map = language->get_trimmed_word_map();
|
|
|
|
const uint32_t unique_prefix_length = language->get_unique_prefix_length();
|
2018-07-06 23:03:15 +00:00
|
|
|
for (std::vector<epee::wipeable_string>::const_iterator it = word_list.begin(); it != word_list.end(); it++)
|
2014-09-30 18:00:27 +00:00
|
|
|
{
|
2019-01-11 01:36:59 +00:00
|
|
|
word = Language::utf8prefix(*it, unique_prefix_length);
|
|
|
|
auto it2 = trimmed_word_map.find(word);
|
|
|
|
if (it2 == trimmed_word_map.end())
|
|
|
|
throw std::runtime_error("Word \"" + std::string(word.data(), word.size()) + "\" not found in trimmed word map in " + language->get_english_language_name());
|
|
|
|
trimmed_words += it2->first;
|
2014-09-30 18:00:27 +00:00
|
|
|
}
|
2014-09-27 20:59:25 +00:00
|
|
|
boost::crc_32_type result;
|
2014-10-02 12:45:18 +00:00
|
|
|
result.process_bytes(trimmed_words.data(), trimmed_words.length());
|
2018-10-21 17:46:13 +00:00
|
|
|
return result.checksum() % word_list.size();
|
2014-09-27 20:59:25 +00:00
|
|
|
}
|
2014-10-02 12:45:18 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Does the checksum test on the seed passed.
|
2014-10-07 07:19:36 +00:00
|
|
|
* \param seed Vector of seed words
|
|
|
|
* \param unique_prefix_length the prefix length of each word to use for checksum
|
|
|
|
* \return True if the test passed false if not.
|
2014-10-02 12:45:18 +00:00
|
|
|
*/
|
2019-01-11 01:36:59 +00:00
|
|
|
bool checksum_test(std::vector<epee::wipeable_string> seed, const Language::Base *language)
|
2014-10-02 12:45:18 +00:00
|
|
|
{
|
2017-12-11 22:36:58 +00:00
|
|
|
if (seed.empty())
|
|
|
|
return false;
|
2014-10-02 12:45:18 +00:00
|
|
|
// The last word is the checksum.
|
2018-07-06 23:03:15 +00:00
|
|
|
epee::wipeable_string last_word = seed.back();
|
2014-10-02 12:45:18 +00:00
|
|
|
seed.pop_back();
|
|
|
|
|
2019-01-11 01:36:59 +00:00
|
|
|
const uint32_t unique_prefix_length = language->get_unique_prefix_length();
|
|
|
|
|
|
|
|
auto idx = create_checksum_index(seed, language);
|
|
|
|
epee::wipeable_string checksum = seed[idx];
|
2014-10-02 12:45:18 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
epee::wipeable_string trimmed_checksum = checksum.length() > unique_prefix_length ? Language::utf8prefix(checksum, unique_prefix_length) :
|
2014-10-02 12:45:18 +00:00
|
|
|
checksum;
|
2018-07-06 23:03:15 +00:00
|
|
|
epee::wipeable_string trimmed_last_word = last_word.length() > unique_prefix_length ? Language::utf8prefix(last_word, unique_prefix_length) :
|
2014-10-02 12:45:18 +00:00
|
|
|
last_word;
|
2019-01-11 01:36:59 +00:00
|
|
|
bool ret = Language::WordEqual()(trimmed_checksum, trimmed_last_word);
|
2018-07-06 23:03:15 +00:00
|
|
|
MINFO("Checksum is " << (ret ? "valid" : "invalid"));
|
2018-04-24 16:06:25 +00:00
|
|
|
return ret;
|
2014-10-02 12:45:18 +00:00
|
|
|
}
|
2014-09-23 13:04:39 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 12:50:15 +00:00
|
|
|
/*!
|
|
|
|
* \namespace crypto
|
2014-09-27 20:59:25 +00:00
|
|
|
*
|
|
|
|
* \brief crypto namespace.
|
2014-09-27 12:50:15 +00:00
|
|
|
*/
|
2014-06-04 22:59:47 +00:00
|
|
|
namespace crypto
|
|
|
|
{
|
2014-09-27 12:50:15 +00:00
|
|
|
/*!
|
2014-09-27 20:59:25 +00:00
|
|
|
* \namespace crypto::ElectrumWords
|
2014-09-27 12:50:15 +00:00
|
|
|
*
|
2014-09-27 20:59:25 +00:00
|
|
|
* \brief Mnemonic seed word generation and wallet restoration helper functions.
|
2014-09-27 12:50:15 +00:00
|
|
|
*/
|
2014-06-04 22:59:47 +00:00
|
|
|
namespace ElectrumWords
|
|
|
|
{
|
2014-09-27 12:50:15 +00:00
|
|
|
/*!
|
|
|
|
* \brief Converts seed words to bytes (secret key).
|
2014-10-02 12:45:18 +00:00
|
|
|
* \param words String containing the words separated by spaces.
|
2017-11-09 10:56:53 +00:00
|
|
|
* \param dst To put the secret data restored from the words.
|
|
|
|
* \param len The number of bytes to expect, 0 if unknown
|
|
|
|
* \param duplicate If true and len is not zero, we accept half the data, and duplicate it
|
2014-10-02 12:45:18 +00:00
|
|
|
* \param language_name Language of the seed as found gets written here.
|
|
|
|
* \return false if not a multiple of 3 words, or if word is not in the words list
|
2014-06-05 03:08:35 +00:00
|
|
|
*/
|
2018-07-06 23:03:15 +00:00
|
|
|
bool words_to_bytes(const epee::wipeable_string &words, epee::wipeable_string& dst, size_t len, bool duplicate,
|
2014-10-02 12:45:18 +00:00
|
|
|
std::string &language_name)
|
2014-06-04 22:59:47 +00:00
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
std::vector<epee::wipeable_string> seed;
|
2014-09-26 18:04:35 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
words.split(seed);
|
2014-09-26 18:04:35 +00:00
|
|
|
|
2017-11-09 10:56:53 +00:00
|
|
|
if (len % 4)
|
2018-04-24 16:06:25 +00:00
|
|
|
{
|
|
|
|
MERROR("Invalid seed: not a multiple of 4");
|
2014-10-02 12:45:18 +00:00
|
|
|
return false;
|
2018-04-24 16:06:25 +00:00
|
|
|
}
|
2014-09-30 18:00:27 +00:00
|
|
|
|
2017-11-09 10:56:53 +00:00
|
|
|
bool has_checksum = true;
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
// error on non-compliant word list
|
|
|
|
const size_t expected = len * 8 * 3 / 32;
|
|
|
|
if (seed.size() != expected/2 && seed.size() != expected &&
|
|
|
|
seed.size() != expected + 1)
|
|
|
|
{
|
2018-04-24 16:06:25 +00:00
|
|
|
MERROR("Invalid seed: unexpected number of words");
|
2017-11-09 10:56:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is seed with a checksum.
|
|
|
|
has_checksum = seed.size() == (expected + 1);
|
|
|
|
}
|
2014-10-07 07:19:36 +00:00
|
|
|
|
|
|
|
std::vector<uint32_t> matched_indices;
|
2018-07-06 23:03:15 +00:00
|
|
|
auto wiper = epee::misc_utils::create_scope_leave_handler([&](){memwipe(matched_indices.data(), matched_indices.size() * sizeof(matched_indices[0]));});
|
2014-10-07 07:19:36 +00:00
|
|
|
Language::Base *language;
|
|
|
|
if (!find_seed_language(seed, has_checksum, matched_indices, &language))
|
|
|
|
{
|
2018-04-24 16:06:25 +00:00
|
|
|
MERROR("Invalid seed: language not found");
|
2014-10-07 07:19:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
language_name = language->get_language_name();
|
|
|
|
uint32_t word_list_length = language->get_word_list().size();
|
|
|
|
|
2014-09-30 18:00:27 +00:00
|
|
|
if (has_checksum)
|
2014-09-27 20:59:25 +00:00
|
|
|
{
|
2019-01-11 01:36:59 +00:00
|
|
|
if (!checksum_test(seed, language))
|
2014-09-27 20:59:25 +00:00
|
|
|
{
|
|
|
|
// Checksum fail
|
2018-04-24 16:06:25 +00:00
|
|
|
MERROR("Invalid seed: invalid checksum");
|
2014-09-27 20:59:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-10-02 15:44:29 +00:00
|
|
|
seed.pop_back();
|
2014-10-07 19:14:26 +00:00
|
|
|
}
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2014-10-02 12:45:18 +00:00
|
|
|
for (unsigned int i=0; i < seed.size() / 3; i++)
|
2014-06-05 03:08:35 +00:00
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
uint32_t w[4];
|
|
|
|
w[1] = matched_indices[i*3];
|
|
|
|
w[2] = matched_indices[i*3 + 1];
|
|
|
|
w[3] = matched_indices[i*3 + 2];
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
w[0]= w[1] + word_list_length * (((word_list_length - w[1]) + w[2]) % word_list_length) +
|
|
|
|
word_list_length * word_list_length * (((word_list_length - w[2]) + w[3]) % word_list_length);
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
if (!(w[0]% word_list_length == w[1]))
|
2018-04-24 16:06:25 +00:00
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
memwipe(w, sizeof(w));
|
2018-04-24 16:06:25 +00:00
|
|
|
MERROR("Invalid seed: mumble mumble");
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2018-10-25 20:22:13 +00:00
|
|
|
w[0] = SWAP32LE(w[0]);
|
2018-07-06 23:03:15 +00:00
|
|
|
dst.append((const char*)&w[0], 4); // copy 4 bytes to position
|
|
|
|
memwipe(w, sizeof(w));
|
2014-06-05 03:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 10:56:53 +00:00
|
|
|
if (len > 0 && duplicate)
|
2014-06-05 03:08:35 +00:00
|
|
|
{
|
2017-11-09 10:56:53 +00:00
|
|
|
const size_t expected = len * 3 / 32;
|
|
|
|
if (seed.size() == expected/2)
|
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
dst += ' '; // if electrum 12-word seed, duplicate
|
|
|
|
dst += dst; // if electrum 12-word seed, duplicate
|
|
|
|
dst.pop_back(); // trailing space
|
2017-11-09 10:56:53 +00:00
|
|
|
}
|
2014-06-05 03:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-06-04 22:59:47 +00:00
|
|
|
}
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2017-11-09 10:56:53 +00:00
|
|
|
/*!
|
|
|
|
* \brief Converts seed words to bytes (secret key).
|
|
|
|
* \param words String containing the words separated by spaces.
|
|
|
|
* \param dst To put the secret key restored from the words.
|
|
|
|
* \param language_name Language of the seed as found gets written here.
|
|
|
|
* \return false if not a multiple of 3 words, or if word is not in the words list
|
|
|
|
*/
|
2018-07-06 23:03:15 +00:00
|
|
|
bool words_to_bytes(const epee::wipeable_string &words, crypto::secret_key& dst,
|
2017-11-09 10:56:53 +00:00
|
|
|
std::string &language_name)
|
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
epee::wipeable_string s;
|
2017-11-09 10:56:53 +00:00
|
|
|
if (!words_to_bytes(words, s, sizeof(dst), true, language_name))
|
2018-04-24 16:06:25 +00:00
|
|
|
{
|
|
|
|
MERROR("Invalid seed: failed to convert words to bytes");
|
2017-11-09 10:56:53 +00:00
|
|
|
return false;
|
2018-04-24 16:06:25 +00:00
|
|
|
}
|
2017-11-09 10:56:53 +00:00
|
|
|
if (s.size() != sizeof(dst))
|
2018-04-24 16:06:25 +00:00
|
|
|
{
|
|
|
|
MERROR("Invalid seed: wrong output size");
|
2017-11-09 10:56:53 +00:00
|
|
|
return false;
|
2018-04-24 16:06:25 +00:00
|
|
|
}
|
2017-11-09 10:56:53 +00:00
|
|
|
dst = *(const crypto::secret_key*)s.data();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-27 12:50:15 +00:00
|
|
|
/*!
|
|
|
|
* \brief Converts bytes (secret key) to seed words.
|
2014-10-02 12:45:18 +00:00
|
|
|
* \param src Secret key
|
|
|
|
* \param words Space delimited concatenated words get written here.
|
|
|
|
* \param language_name Seed language name
|
|
|
|
* \return true if successful false if not. Unsuccessful if wrong key size.
|
2014-06-05 03:08:35 +00:00
|
|
|
*/
|
2018-07-06 23:03:15 +00:00
|
|
|
bool bytes_to_words(const char *src, size_t len, epee::wipeable_string& words,
|
2014-10-02 12:45:18 +00:00
|
|
|
const std::string &language_name)
|
2014-06-04 22:59:47 +00:00
|
|
|
{
|
2014-10-02 12:45:18 +00:00
|
|
|
|
2017-11-09 10:56:53 +00:00
|
|
|
if (len % 4 != 0 || len == 0) return false;
|
2014-10-02 12:45:18 +00:00
|
|
|
|
2018-07-22 10:28:11 +00:00
|
|
|
const Language::Base *language = NULL;
|
|
|
|
const std::vector<const Language::Base*> language_list = crypto::ElectrumWords::get_language_list();
|
|
|
|
for (const Language::Base *l: language_list)
|
2014-10-02 12:45:18 +00:00
|
|
|
{
|
2018-07-22 10:28:11 +00:00
|
|
|
if (language_name == l->get_language_name() || language_name == l->get_english_language_name())
|
|
|
|
language = l;
|
2014-10-02 12:45:18 +00:00
|
|
|
}
|
2018-07-22 10:28:11 +00:00
|
|
|
if (!language)
|
2014-10-02 12:45:18 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-02 17:06:02 +00:00
|
|
|
const std::vector<std::string> &word_list = language->get_word_list();
|
2014-09-27 20:59:25 +00:00
|
|
|
// To store the words for random access to add the checksum word later.
|
2018-07-06 23:03:15 +00:00
|
|
|
std::vector<epee::wipeable_string> words_store;
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2014-10-02 12:45:18 +00:00
|
|
|
uint32_t word_list_length = word_list.size();
|
2017-12-25 10:57:38 +00:00
|
|
|
// 4 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626
|
2018-07-06 23:03:15 +00:00
|
|
|
for (unsigned int i=0; i < len/4; i++, words.push_back(' '))
|
2014-06-05 03:08:35 +00:00
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
uint32_t w[4];
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2018-08-04 16:20:44 +00:00
|
|
|
w[0] = SWAP32LE(*(const uint32_t*)(src + (i * 4)));
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
w[1] = w[0] % word_list_length;
|
|
|
|
w[2] = ((w[0] / word_list_length) + w[1]) % word_list_length;
|
|
|
|
w[3] = (((w[0] / word_list_length) / word_list_length) + w[2]) % word_list_length;
|
2014-06-05 03:08:35 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
words += word_list[w[1]];
|
2014-06-05 03:08:35 +00:00
|
|
|
words += ' ';
|
2018-07-06 23:03:15 +00:00
|
|
|
words += word_list[w[2]];
|
2014-06-05 03:08:35 +00:00
|
|
|
words += ' ';
|
2018-07-06 23:03:15 +00:00
|
|
|
words += word_list[w[3]];
|
|
|
|
|
|
|
|
words_store.push_back(word_list[w[1]]);
|
|
|
|
words_store.push_back(word_list[w[2]]);
|
|
|
|
words_store.push_back(word_list[w[3]]);
|
2014-09-27 20:59:25 +00:00
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
memwipe(w, sizeof(w));
|
2014-06-05 03:08:35 +00:00
|
|
|
}
|
2014-09-27 20:59:25 +00:00
|
|
|
|
2019-01-11 01:36:59 +00:00
|
|
|
words += words_store[create_checksum_index(words_store, language)];
|
2014-11-06 22:36:36 +00:00
|
|
|
return true;
|
2014-06-04 22:59:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 23:03:15 +00:00
|
|
|
bool bytes_to_words(const crypto::secret_key& src, epee::wipeable_string& words,
|
2017-11-09 10:56:53 +00:00
|
|
|
const std::string &language_name)
|
|
|
|
{
|
|
|
|
return bytes_to_words(src.data, sizeof(src), words, language_name);
|
|
|
|
}
|
|
|
|
|
2018-03-17 22:46:41 +00:00
|
|
|
std::vector<const Language::Base*> get_language_list()
|
2014-09-25 12:34:30 +00:00
|
|
|
{
|
2018-03-17 22:46:41 +00:00
|
|
|
static const std::vector<const Language::Base*> language_instances({
|
2017-04-16 11:34:49 +00:00
|
|
|
Language::Singleton<Language::German>::instance(),
|
2014-10-02 12:45:18 +00:00
|
|
|
Language::Singleton<Language::English>::instance(),
|
|
|
|
Language::Singleton<Language::Spanish>::instance(),
|
2017-04-16 11:34:49 +00:00
|
|
|
Language::Singleton<Language::French>::instance(),
|
2015-07-22 20:00:06 +00:00
|
|
|
Language::Singleton<Language::Italian>::instance(),
|
2017-04-16 11:34:49 +00:00
|
|
|
Language::Singleton<Language::Dutch>::instance(),
|
2014-10-02 12:45:18 +00:00
|
|
|
Language::Singleton<Language::Portuguese>::instance(),
|
2015-07-24 05:52:57 +00:00
|
|
|
Language::Singleton<Language::Russian>::instance(),
|
2017-04-16 11:34:49 +00:00
|
|
|
Language::Singleton<Language::Japanese>::instance(),
|
2017-08-13 14:52:33 +00:00
|
|
|
Language::Singleton<Language::Chinese_Simplified>::instance(),
|
2017-09-25 22:42:17 +00:00
|
|
|
Language::Singleton<Language::Esperanto>::instance(),
|
|
|
|
Language::Singleton<Language::Lojban>::instance()
|
2014-10-02 12:45:18 +00:00
|
|
|
});
|
2018-03-17 22:46:41 +00:00
|
|
|
return language_instances;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Gets a list of seed languages that are supported.
|
|
|
|
* \param languages The vector is set to the list of languages.
|
|
|
|
*/
|
|
|
|
void get_language_list(std::vector<std::string> &languages, bool english)
|
|
|
|
{
|
|
|
|
const std::vector<const Language::Base*> language_instances = get_language_list();
|
|
|
|
for (std::vector<const Language::Base*>::const_iterator it = language_instances.begin();
|
2014-10-02 12:45:18 +00:00
|
|
|
it != language_instances.end(); it++)
|
2014-09-27 20:59:25 +00:00
|
|
|
{
|
2018-03-17 22:46:41 +00:00
|
|
|
languages.push_back(english ? (*it)->get_english_language_name() : (*it)->get_language_name());
|
2014-09-27 20:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Tells if the seed passed is an old style seed or not.
|
|
|
|
* \param seed The seed to check (a space delimited concatenated word list)
|
|
|
|
* \return true if the seed passed is a old style seed false if not.
|
|
|
|
*/
|
2018-07-06 23:03:15 +00:00
|
|
|
bool get_is_old_style_seed(const epee::wipeable_string &seed)
|
2014-09-27 20:59:25 +00:00
|
|
|
{
|
2018-07-06 23:03:15 +00:00
|
|
|
std::vector<epee::wipeable_string> word_list;
|
|
|
|
seed.split(word_list);
|
2014-10-02 12:45:18 +00:00
|
|
|
return word_list.size() != (seed_length + 1);
|
2014-09-27 20:59:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 22:46:41 +00:00
|
|
|
std::string get_english_name_for(const std::string &name)
|
|
|
|
{
|
|
|
|
const std::vector<const Language::Base*> language_instances = get_language_list();
|
|
|
|
for (std::vector<const Language::Base*>::const_iterator it = language_instances.begin();
|
|
|
|
it != language_instances.end(); it++)
|
|
|
|
{
|
|
|
|
if ((*it)->get_language_name() == name)
|
|
|
|
return (*it)->get_english_language_name();
|
|
|
|
}
|
|
|
|
return "<language not found>";
|
|
|
|
}
|
|
|
|
|
2014-09-27 12:50:15 +00:00
|
|
|
}
|
2014-06-04 22:59:47 +00:00
|
|
|
|
2014-09-27 12:50:15 +00:00
|
|
|
}
|