From d53e65751384251e13537affb418d905c8001065 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sat, 10 Jun 2023 23:08:48 +0300 Subject: [PATCH] fix(wallet): truncate mnemonic passphrase instead of crashing --- src/bip39.cpp | 4 ++-- src/bip39.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bip39.cpp b/src/bip39.cpp index 2134f98dec..61c19bdbd6 100644 --- a/src/bip39.cpp +++ b/src/bip39.cpp @@ -147,11 +147,11 @@ bool CMnemonic::Check(SecureString mnemonic) return fResult; } -// passphrase must be at most 256 characters or code may crash +// passphrase must be at most 256 characters otherwise it would be truncated void CMnemonic::ToSeed(SecureString mnemonic, SecureString passphrase, SecureVector& seedRet) { SecureString ssSalt = SecureString("mnemonic") + passphrase; - SecureVector vchSalt(ssSalt.begin(), ssSalt.end()); + SecureVector vchSalt(ssSalt.begin(), ssSalt.begin() + strnlen(ssSalt.data(), 256)); seedRet.resize(64); PKCS5_PBKDF2_HMAC_SHA512(mnemonic.c_str(), mnemonic.size(), vchSalt.data(), vchSalt.size(), 2048, 64, seedRet.data()); } diff --git a/src/bip39.h b/src/bip39.h index 9c6426b244..30f3a6880e 100644 --- a/src/bip39.h +++ b/src/bip39.h @@ -32,7 +32,7 @@ public: static SecureString Generate(int strength); // strength in bits static SecureString FromData(const SecureVector& data, int len); static bool Check(SecureString mnemonic); - // passphrase must be at most 256 characters or code may crash + // passphrase must be at most 256 characters otherwise it would be truncated static void ToSeed(SecureString mnemonic, SecureString passphrase, SecureVector& seedRet); };