From 4f382b383005d09f6056371d0fecdc8b6ce37a08 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Wed, 4 Jun 2014 18:59:47 -0400 Subject: most functions prototyped/modified for wallet recovery --- src/crypto/electrum-words.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/crypto/electrum-words.cpp (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp new file mode 100644 index 000000000..cb01c9203 --- /dev/null +++ b/src/crypto/electrum-words.cpp @@ -0,0 +1,29 @@ +/* + * 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 +#include +#include "crypto/crypto.h" // for declaration of crypto::secret_key + +#include "crypto/electrum-words.h" + +namespace crypto +{ + namespace ElectrumWords + { + + bool words_to_bytes(const std::string& words, crypto::secret_key& dst) + { + return false; + } + bool bytes_to_words(const crypto::secret_key& src, std::string& words) + { + return false; + } + + } // namespace ElectrumWords + +} // namespace crypto -- cgit v1.2.3 From cd631325029bf8df53e2ec813a3d340a841f73e2 Mon Sep 17 00:00:00 2001 From: tom Date: Wed, 4 Jun 2014 23:08:35 -0400 Subject: compile issues, resolve later. commiting because switching machines --- src/crypto/electrum-words.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index cb01c9203..9818791b9 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -6,6 +6,9 @@ #include #include +#include +#include +#include #include "crypto/crypto.h" // for declaration of crypto::secret_key #include "crypto/electrum-words.h" @@ -15,12 +18,84 @@ namespace crypto namespace ElectrumWords { + /* convert words to bytes, 3 words -> 4 bytes + * returns: + * false if not a multiple of 3 words, or if a words is not in the + * words list + * + * true otherwise + */ bool words_to_bytes(const std::string& words, crypto::secret_key& dst) { - return false; + int n = NUMWORDS; // hardcoded because this is what electrum uses + + std::vector wlist; + + boost::split(wlist, words, boost::is_any_of(" ")); + + // error on non-compliant word list + if (wlist.size() != 12 && wlist.size() != 24) return false; + + for (int i=0; i < wlist.size() / 3; i++) + { + uint32_t val; + uint32_t w1, w2, w3; + + // verify all three words exist in the word list + if (wordsMap.count(wlist[i*3]) == 0 || + wordsMap.count(wlist[i*3 + 1]) == 0 || + wordsMap.count(wlist[i*3 + 2]) == 0) + { + return false; + } + + w1 = wordsMap.at(wlist[i*3]); + w2 = wordsMap(wlist[i*3 + 1]); + w3 = wordsMap(wlist[i*3 + 2]); + + val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); + + memcpy(dst.data + i * 4, val, 4); // copy 4 bytes to position + } + + if (wlist.size() == 12) + { + memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate + } + + return true; } + + /* convert bytes to words, 4 bytes-> 3 words + * returns: + * false if wrong number of bytes (shouldn't be possible) + * true otherwise + */ bool bytes_to_words(const crypto::secret_key& src, std::string& words) { + int n = NUMWORDS; // hardcoded because this is what electrum uses + + if (sizeof(src.data) % 4 != 0) return false; + + // 8 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626 + for (int i=0; i < sizeof(src.data)/4; i++, words += ' ') + { + uint32_t w1, w2, w3; + + uint32_t val; + + memcpy(val, src, 4); + + w1 = val % n; + w2 = ((val / n) + w1) % n; + w3 = (((val / n) / n) + w2) % n; + + words += wordsArray[w1]; + words += ' '; + words += wordsArray[w2]; + words += ' '; + words += wordsArray[w3]; + } return false; } -- cgit v1.2.3 From 8661f9a970aca5812d0838bc3fb33fbae0ec4cd2 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 08:20:54 -0400 Subject: missing '.at' on two map calls. built on some machines, very strange... --- src/crypto/electrum-words.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index 9818791b9..b4580e01f 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -50,8 +50,8 @@ namespace crypto } w1 = wordsMap.at(wlist[i*3]); - w2 = wordsMap(wlist[i*3 + 1]); - w3 = wordsMap(wlist[i*3 + 2]); + w2 = wordsMap.at(wlist[i*3 + 1]); + w3 = wordsMap.at(wlist[i*3 + 2]); val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); -- cgit v1.2.3 From d22e458c6c680f4b5dcf56a58a37a5f79912e65c Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 14:18:11 -0400 Subject: builds, but doesn't link. other than that, electrum-style recovery implemented (but not tested\!) --- src/crypto/electrum-words.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index b4580e01f..048aa403f 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -36,7 +36,7 @@ namespace crypto // error on non-compliant word list if (wlist.size() != 12 && wlist.size() != 24) return false; - for (int i=0; i < wlist.size() / 3; i++) + for (unsigned int i=0; i < wlist.size() / 3; i++) { uint32_t val; uint32_t w1, w2, w3; @@ -55,12 +55,12 @@ namespace crypto val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); - memcpy(dst.data + i * 4, val, 4); // copy 4 bytes to position + memcpy(&dst.data + i * 4, &val, 4); // copy 4 bytes to position } if (wlist.size() == 12) { - memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate + memcpy(&dst.data, &dst.data + 16, 16); // if electrum 12-word seed, duplicate } return true; @@ -78,13 +78,13 @@ namespace crypto if (sizeof(src.data) % 4 != 0) return false; // 8 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626 - for (int i=0; i < sizeof(src.data)/4; i++, words += ' ') + for (unsigned int i=0; i < sizeof(src.data)/4; i++, words += ' ') { uint32_t w1, w2, w3; uint32_t val; - memcpy(val, src, 4); + memcpy(&val, &src.data, 4); w1 = val % n; w2 = ((val / n) + w1) % n; -- cgit v1.2.3 From 72c3f36ca44a4be4d237095fd14e1ff862af5864 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 15:45:21 -0400 Subject: fixed some pointer- and loop-based derps --- src/crypto/electrum-words.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index 048aa403f..5489d5034 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -84,7 +84,7 @@ namespace crypto uint32_t val; - memcpy(&val, &src.data, 4); + memcpy(&val, (src.data) + (i * 4), 4); w1 = val % n; w2 = ((val / n) + w1) % n; -- cgit v1.2.3 From 8bc032ed092e65e944e9032bc6042ae118346fa9 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 16:31:04 -0400 Subject: more pointer-based derp --- src/crypto/electrum-words.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index 5489d5034..a11d287b2 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -55,14 +56,22 @@ namespace crypto val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); - memcpy(&dst.data + i * 4, &val, 4); // copy 4 bytes to position + memcpy(dst.data + i * 4, &val, 4); // copy 4 bytes to position } + std::string wlist_copy = words; if (wlist.size() == 12) { - memcpy(&dst.data, &dst.data + 16, 16); // if electrum 12-word seed, duplicate + memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate + wlist_copy += ' '; + wlist_copy += words; } + std::string back_to_words; + bytes_to_words(dst, back_to_words); + + assert(wlist_copy == back_to_words); // sanity check + return true; } -- cgit v1.2.3 From 537b911af1db1258a6db226de3b7c55cb5779e21 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Fri, 6 Jun 2014 17:18:57 -0400 Subject: stupid signed arithmetic. --- src/crypto/electrum-words.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/crypto/electrum-words.cpp') diff --git a/src/crypto/electrum-words.cpp b/src/crypto/electrum-words.cpp index a11d287b2..96a392d04 100644 --- a/src/crypto/electrum-words.cpp +++ b/src/crypto/electrum-words.cpp @@ -54,7 +54,9 @@ namespace crypto w2 = wordsMap.at(wlist[i*3 + 1]); w3 = wordsMap.at(wlist[i*3 + 2]); - val = w1 + n * ((w2 - w1) % n) + n * n * ((w3 - w2) % n); + val = w1 + n * (((n - w1) + w2) % n) + n * n * (((n - w2) + w3) % n); + + if (!(val % n == w1)) return false; memcpy(dst.data + i * 4, &val, 4); // copy 4 bytes to position } @@ -67,11 +69,6 @@ namespace crypto wlist_copy += words; } - std::string back_to_words; - bytes_to_words(dst, back_to_words); - - assert(wlist_copy == back_to_words); // sanity check - return true; } -- cgit v1.2.3