| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | #include "gtest/gtest.h" |
| 30 | #include "wipeable_string.h" |
| 31 | #include "mnemonics/language_base.h" |
| 32 | #include "mnemonics/electrum-words.h" |
| 33 | #include "crypto/crypto.h" |
| 34 | #include <stdlib.h> |
| 35 | #include <vector> |
| 36 | #include <time.h> |
| 37 | #include <iostream> |
| 38 | #include <boost/algorithm/string.hpp> |
| 39 | #include "mnemonics/chinese_simplified.h" |
| 40 | #include "mnemonics/english.h" |
| 41 | #include "mnemonics/spanish.h" |
| 42 | #include "mnemonics/portuguese.h" |
| 43 | #include "mnemonics/japanese.h" |
| 44 | #include "mnemonics/german.h" |
| 45 | #include "mnemonics/italian.h" |
| 46 | #include "mnemonics/russian.h" |
| 47 | #include "mnemonics/french.h" |
| 48 | #include "mnemonics/dutch.h" |
| 49 | #include "mnemonics/esperanto.h" |
| 50 | #include "mnemonics/lojban.h" |
| 51 | #include "mnemonics/english_old.h" |
| 52 | #include "mnemonics/singleton.h" |
| 53 | |
| 54 | namespace |
| 55 | { |
| 56 | /*! |
| 57 | * \brief Compares vectors for equality |
| 58 | * \param expected expected vector |
| 59 | * \param present current vector |
| 60 | */ |
| 61 | void compare_vectors(const std::vector<std::string> &expected, const std::vector<std::string> &present) |
| 62 | { |
| 63 | std::vector<std::string>::const_iterator it1, it2; |
| 64 | for (it1 = expected.begin(), it2 = present.begin(); it1 != expected.end() && it2 != present.end(); |
| 65 | it1++, it2++) |
| 66 | { |
| 67 | ASSERT_STREQ(it1->c_str(), it2->c_str()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /*! |
| 72 | * \brief Tests the given language mnemonics. |
| 73 | * \param language A Language instance to test |
| 74 | */ |
| 75 | void test_language(const Language::Base &language) |
| 76 | { |
| 77 | epee::wipeable_string w_seed = "", w_return_seed = ""; |
| 78 | std::string seed, return_seed; |
| 79 | // Generate a random seed without checksum |
| 80 | crypto::secret_key randkey; |
| 81 | for (size_t ii = 0; ii < sizeof(randkey); ++ii) |
| 82 | { |
| 83 | randkey.data[ii] = crypto::rand<uint8_t>(); |
| 84 | } |
| 85 | crypto::ElectrumWords::bytes_to_words(randkey, w_seed, language.get_language_name()); |
| 86 | seed = std::string(w_seed.data(), w_seed.size()); |
| 87 | // remove the checksum word |
| 88 | const char *space = strrchr(seed.c_str(), ' '); |
| 89 | ASSERT_TRUE(space != NULL); |
| 90 | seed = std::string(seed.c_str(), space-seed.c_str()); |
| 91 | |
| 92 | std::cout << "Test seed without checksum:\n"; |
| 93 | std::cout << seed << std::endl; |
| 94 | |
| 95 | crypto::secret_key key; |
| 96 | std::string language_name; |
| 97 | bool res; |
| 98 | std::vector<std::string> seed_vector, return_seed_vector; |
| 99 | std::string checksum_word; |
| 100 | |
| 101 | // Convert it to secret key |
| 102 | res = crypto::ElectrumWords::words_to_bytes(seed, key, language_name); |
| 103 | ASSERT_EQ(true, res); |
| 104 | std::cout << "Detected language: " << language_name << std::endl; |
| 105 | ASSERT_STREQ(language.get_language_name().c_str(), language_name.c_str()); |
| 106 | |
| 107 | // Convert the secret key back to seed |
| 108 | crypto::ElectrumWords::bytes_to_words(key, w_return_seed, language.get_language_name()); |
| 109 | return_seed = std::string(w_return_seed.data(), w_return_seed.size()); |
| 110 | ASSERT_EQ(true, res); |
| 111 | std::cout << "Returned seed:\n"; |
| 112 | std::cout << return_seed << std::endl; |
| 113 | boost::split(seed_vector, seed, boost::is_any_of(" ")); |
| 114 | boost::split(return_seed_vector, return_seed, boost::is_any_of(" ")); |
| 115 | |
| 116 | // Extract the checksum word |
| 117 | checksum_word = return_seed_vector.back(); |
| 118 | return_seed_vector.pop_back(); |
| 119 | ASSERT_EQ(seed_vector.size(), return_seed_vector.size()); |
| 120 | // Ensure that the rest of it is same |
| 121 | compare_vectors(seed_vector, return_seed_vector); |
| 122 | |
| 123 | // Append the checksum word to repeat the entire process with a seed with checksum |
| 124 | seed += (" " + checksum_word); |
| 125 | std::cout << "Test seed with checksum:\n"; |
| 126 | std::cout << seed << std::endl; |
| 127 | res = crypto::ElectrumWords::words_to_bytes(seed, key, language_name); |
| 128 | ASSERT_EQ(true, res); |
| 129 | std::cout << "Detected language: " << language_name << std::endl; |
| 130 | ASSERT_STREQ(language.get_language_name().c_str(), language_name.c_str()); |
| 131 | |
| 132 | w_return_seed = ""; |
| 133 | crypto::ElectrumWords::bytes_to_words(key, w_return_seed, language.get_language_name()); |
| 134 | return_seed = std::string(w_return_seed.data(), w_return_seed.size()); |
| 135 | ASSERT_EQ(true, res); |
| 136 | std::cout << "Returned seed:\n"; |
| 137 | std::cout << return_seed << std::endl; |
| 138 | |
| 139 | seed_vector.clear(); |
| 140 | return_seed_vector.clear(); |
| 141 | boost::split(seed_vector, seed, boost::is_any_of(" ")); |
| 142 | boost::split(return_seed_vector, return_seed, boost::is_any_of(" ")); |
| 143 | ASSERT_EQ(seed_vector.size(), return_seed_vector.size()); |
| 144 | compare_vectors(seed_vector, return_seed_vector); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | TEST(mnemonics, consistency) |
| 149 | { |
| 150 | try { |
| 151 | std::vector<std::string> language_list; |
| 152 | crypto::ElectrumWords::get_language_list(language_list); |
| 153 | } |
| 154 | catch(const std::exception &e) |
| 155 | { |
| 156 | std::cout << "Error initializing mnemonics: " << e.what() << std::endl; |
| 157 | ASSERT_TRUE(false); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | TEST(mnemonics, all_languages) |
| 162 | { |
| 163 | srand(time(NULL)); |
| 164 | std::vector<Language::Base*> languages({ |
| 165 | Language::Singleton<Language::Chinese_Simplified>::instance(), |
| 166 | Language::Singleton<Language::English>::instance(), |
| 167 | Language::Singleton<Language::Spanish>::instance(), |
| 168 | Language::Singleton<Language::Portuguese>::instance(), |
| 169 | Language::Singleton<Language::Japanese>::instance(), |
| 170 | Language::Singleton<Language::German>::instance(), |
| 171 | Language::Singleton<Language::Italian>::instance(), |
| 172 | Language::Singleton<Language::Russian>::instance(), |
| 173 | Language::Singleton<Language::French>::instance(), |
| 174 | Language::Singleton<Language::Dutch>::instance(), |
| 175 | Language::Singleton<Language::Esperanto>::instance(), |
| 176 | Language::Singleton<Language::Lojban>::instance() |
| 177 | }); |
| 178 | |
| 179 | for (std::vector<Language::Base*>::iterator it = languages.begin(); it != languages.end(); it++) |
| 180 | { |
| 181 | try { |
| 182 | test_language(*(*it)); |
| 183 | } |
| 184 | catch (const std::exception &e) { |
| 185 | std::cout << "Error testing " << (*it)->get_language_name() << " language: " << e.what() << std::endl; |
| 186 | ASSERT_TRUE(false); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | TEST(mnemonics, language_detection_with_bad_checksum) |
| 192 | { |
| 193 | crypto::secret_key key; |
| 194 | std::string language_name; |
| 195 | bool res; |
| 196 | |
| 197 | // This Portuguese (4-prefix) seed has all its words with 3-prefix that's also present in English |
| 198 | const std::string base_seed = "cinzento luxuriante leonardo gnostico digressao cupula fifa broxar iniquo louvor ovario dorsal ideologo besuntar decurso rosto susto lemure unheiro pagodeiro nitroglicerina eclusa mazurca bigorna"; |
| 199 | const std::string real_checksum = "gnostico"; |
| 200 | |
| 201 | res = crypto::ElectrumWords::words_to_bytes(base_seed, key, language_name); |
| 202 | ASSERT_EQ(true, res); |
| 203 | ASSERT_STREQ(language_name.c_str(), "Português"); |
| 204 | |
| 205 | res = crypto::ElectrumWords::words_to_bytes(base_seed + " " + real_checksum, key, language_name); |
| 206 | ASSERT_EQ(true, res); |
| 207 | ASSERT_STREQ(language_name.c_str(), "Português"); |
| 208 | } |
| 209 | |
| 210 | TEST(mnemonics, utf8prefix) |
| 211 | { |
| 212 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("foo"), 0) == ""); |
| 213 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("foo"), 1) == "f"); |
| 214 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("foo"), 2) == "fo"); |
| 215 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("foo"), 3) == "foo"); |
| 216 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("foo"), 4) == "foo"); |
| 217 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("æon"), 0) == ""); |
| 218 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("æon"), 1) == "æ"); |
| 219 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("æon"), 2) == "æo"); |
| 220 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("æon"), 3) == "æon"); |
| 221 | ASSERT_TRUE(Language::utf8prefix(epee::wipeable_string("æon"), 4) == "æon"); |
| 222 | } |
| 223 | |
| 224 | TEST(mnemonics, case_tolerance) |
| 225 | { |
| 226 | bool res; |
| 227 | // |
| 228 | crypto::secret_key key_1; |
| 229 | std::string language_name_1; |
| 230 | const std::string seed_1 = "Neubau umarmen Abart umarmen Turban feilen Brett Bargeld Episode Milchkuh Substanz Jahr Armband Maibaum Tand Grünalge Tabak erziehen Federboa Lobrede Tenor Leuchter Curry Diskurs Tenor"; |
| 231 | res = crypto::ElectrumWords::words_to_bytes(seed_1, key_1, language_name_1); |
| 232 | ASSERT_EQ(true, res); |
| 233 | ASSERT_STREQ(language_name_1.c_str(), "Deutsch"); |
| 234 | // |
| 235 | crypto::secret_key key_2; |
| 236 | std::string language_name_2; |
| 237 | // neubau is capitalized in the word list, but the language detection code should be able to detect it as Deutsch |
| 238 | std::string seed_2 = "neubau umarmen Abart umarmen Turban feilen Brett Bargeld Episode Milchkuh Substanz Jahr Armband Maibaum Tand Grünalge Tabak erziehen Federboa Lobrede Tenor Leuchter Curry Diskurs tenor"; |
| 239 | boost::algorithm::to_lower(seed_2); |
| 240 | res = crypto::ElectrumWords::words_to_bytes(seed_2, key_2, language_name_2); |
| 241 | ASSERT_EQ(true, res); |
| 242 | ASSERT_STREQ(language_name_2.c_str(), "Deutsch"); |
| 243 | // |
| 244 | ASSERT_TRUE(key_1 == key_2); |
| 245 | } |
| 246 | |
| 247 | TEST(mnemonics, partial_word_tolerance) |
| 248 | { |
| 249 | bool res; |
| 250 | // |
| 251 | crypto::secret_key key_1; |
| 252 | std::string language_name_1; |
| 253 | const std::string seed_1 = "crim bam scamp gna limi woma wron tuit birth mundane donuts square cohesive dolphin titans narrate fue saved wrap aloof magic mirr toget upda wra"; |
| 254 | res = crypto::ElectrumWords::words_to_bytes(seed_1, key_1, language_name_1); |
| 255 | ASSERT_EQ(true, res); |
| 256 | ASSERT_STREQ(language_name_1.c_str(), "English"); |
| 257 | } |