-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Crypto]: Update TrezorCrypto/scrypt
#3482
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use Beyond Compare to view the diff, the changes look ok to me
@@ -25,6 +25,8 @@ | |||
* | |||
* This file was originally written by Colin Percival as part of the Tarsnap | |||
* online backup system. | |||
* | |||
* This file was copied from https://github.com/Tarsnap/scrypt/blob/fbd5b105b75da42cbdf91ca55387724e312499b5/lib/crypto/crypto_scrypt-ref.c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use code from https://github.com/Tarsnap/scrypt/releases/tag/1.3.2? easier to track and diff later
I believe the original scrypt code is from https://github.com/technion/libscrypt/blob/master/crypto_scrypt-nosse.c |
I ran the tests in Debug and Release modes to compare performance of different implementations: RustCrypto/scrypt, updated Tarsnap/scrypt/lib/crypto/crypto_scrypt-ref.c and original WalletCore/TrezorCrypto/scrypt.c. Critical bug in WalletCore/TrezorCrypto/scrypt.cI found a critical bug related to the current implementation: WalletCore/TrezorCrypto/scrypt.c function derives different keys in -00/-03 and -0s optimisation levels using the same parameters - at least on my M1 local machine and when I run iOS tests (in debug and release). set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-Os")
set(CMAKE_CXX_FLAGS_RELEASE "-Os")
set(CMAKE_C_FLAGS "-Wall -Wextra")
set(CMAKE_C_FLAGS_DEBUG "-Os")
set(CMAKE_C_FLAGS_RELEASE "-Os") However, I used TrustWallet/ios/TrustKeystoreTests::testImportMnemonicKeystore and TrustWallet/android/TestKeyStoreAdapter::testExportPrivateKey tests to figure out which optimisation level is used in our Wallet Core release artefacts. Given the fact that the current implementation is not robust to the optimization level, I'd strongly recommend to switch to a new Alternative SolutionsRustCrypto/scryptRustCrypto/scrypt is a Pure Rust implementation of the scrypt key derivation function.
Tarsnap/scryptTarsnap/scrypt/lib/crypto/crypto_scrypt-ref.c is represented in this Pull Request.
Performance Metrics
Please also note that:
|
Description
Fixes: #3391
The problem in the mentioned issue is related to an incorrect working of the
scrypt
function.When we compile WalletCore using Xcode 14.3.1 in Release mode,
scrypt
starts computing weird password hashes. Moreover, it seems to change a global context, so each function call with the same arguments returns a predictable but different hash.For example,
Output:
To fix the issue, the
TrezorCrypto/crypto/scrypt.c
file needs to be updated with the latest Tarsnap/scrypt/lib/crypto/crypto_scrypt-ref.c.How to test
Run C++, Android, iOS in Debug and Release modes.
Types of changes
Checklist
If you're adding a new blockchain