@onekeyfe/react-native-lite-card 6.7.0-alpha.409 → 6.7.0-alpha.412
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -1
- package/ReactNativeLiteCard.podspec +25 -2
- package/android/build.gradle +7 -0
- package/android/src/main/cpp/CMakeLists.txt +6 -2
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +5 -1
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +21 -7
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +4 -1
- package/ios/Classes/OKNFCBridge.mm +1 -1
- package/ios/Classes/OKNFTLite/OKLiteV1.m +6 -3
- package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
- package/package.json +2 -2
- package/sdk/CMakeLists.txt +120 -0
- package/sdk/README.md +175 -0
- package/sdk/android/implJni.cpp +177 -0
- package/sdk/deps/jsoncpp/LICENSE +55 -0
- package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
- package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
- package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
- package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
- package/sdk/deps/jsoncpp/include/json/config.h +187 -0
- package/sdk/deps/jsoncpp/include/json/features.h +61 -0
- package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
- package/sdk/deps/jsoncpp/include/json/json.h +15 -0
- package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
- package/sdk/deps/jsoncpp/include/json/value.h +888 -0
- package/sdk/deps/jsoncpp/include/json/version.h +20 -0
- package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
- package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
- package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
- package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
- package/sdk/deps/trezor-crypto/AUTHORS +2 -0
- package/sdk/deps/trezor-crypto/LICENSE +22 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
- package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
- package/sdk/deps/trezor-crypto/src/address.c +87 -0
- package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
- package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
- package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
- package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
- package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
- package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
- package/sdk/deps/trezor-crypto/src/base32.c +233 -0
- package/sdk/deps/trezor-crypto/src/base58.c +298 -0
- package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
- package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
- package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
- package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
- package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
- package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
- package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
- package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
- package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
- package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
- package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
- package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
- package/sdk/deps/trezor-crypto/src/curves.c +48 -0
- package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
- package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
- package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
- package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
- package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
- package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
- package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
- package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
- package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
- package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
- package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
- package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
- package/sdk/deps/trezor-crypto/src/nano.c +151 -0
- package/sdk/deps/trezor-crypto/src/nem.c +569 -0
- package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
- package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
- package/sdk/deps/trezor-crypto/src/options.h +43 -0
- package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
- package/sdk/deps/trezor-crypto/src/rand.c +51 -0
- package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
- package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
- package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
- package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
- package/sdk/deps/trezor-crypto/src/script.c +64 -0
- package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
- package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
- package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
- package/sdk/deps/trezor-crypto/src/setup.py +38 -0
- package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
- package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
- package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
- package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
- package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
- package/sdk/gpc/include/scp03/scp03.hpp +80 -0
- package/sdk/gpc/include/scp11/scp11.hpp +492 -0
- package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
- package/sdk/gpc/include/tag.h +68 -0
- package/sdk/gpc/include/tlv.hpp +94 -0
- package/sdk/gpc/include/tlvparse.hpp +24 -0
- package/sdk/gpc/src/scp03.cpp +243 -0
- package/sdk/gpc/src/scp11.cpp +798 -0
- package/sdk/gpc/src/scp11c.cpp +136 -0
- package/sdk/gpc/src/tlv.cpp +159 -0
- package/sdk/gpc/src/tlvparse.c +174 -0
- package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
- package/sdk/include/GPChannelSDKCore.h +10 -0
- package/sdk/include/context/BaseContext.h +56 -0
- package/sdk/include/device/ApduBuilder.hpp +72 -0
- package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
- package/sdk/include/utility/Apdu.hpp +172 -0
- package/sdk/include/utility/Debug.hpp +3 -0
- package/sdk/include/utility/util.h +104 -0
- package/sdk/src/ApduBuilder.cpp +332 -0
- package/sdk/src/GPChannelSDK.cpp +252 -0
- package/sdk/src/random.c +25 -0
- package/sdk/src/version.c +2 -0
- package/sdk/tests/android/GPChannelProxy.java +55 -0
- package/sdk/tests/android_verify.py +121 -0
- package/sdk/tests/safety.cpp +603 -0
- package/sdk/tests/verify.py +267 -0
- package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
- package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
- package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
- package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2012-2014 Luke Dashjr
|
|
3
|
+
* Copyright (c) 2013-2014 Pavol Rusnak
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
* a copy of this software and associated documentation files (the "Software"),
|
|
7
|
+
* to deal in the Software without restriction, including without limitation
|
|
8
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
9
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
|
10
|
+
* Software is furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included
|
|
13
|
+
* in all copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
16
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
18
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
19
|
+
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
20
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
21
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
#include <string.h>
|
|
25
|
+
#include <stdbool.h>
|
|
26
|
+
#include <sys/types.h>
|
|
27
|
+
#include <TrezorCrypto/base58.h>
|
|
28
|
+
#include <TrezorCrypto/ripple/base58.h>
|
|
29
|
+
#include <TrezorCrypto/sha2.h>
|
|
30
|
+
#include <TrezorCrypto/ripemd160.h>
|
|
31
|
+
#include <TrezorCrypto/memzero.h>
|
|
32
|
+
|
|
33
|
+
const char xrp_b58digits[] = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
|
|
34
|
+
const int8_t xrp_b58digits_map[] = {
|
|
35
|
+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
36
|
+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
37
|
+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
38
|
+
-1,50,33,7,21,41,40,27,45,8,-1,-1,-1,-1,-1,-1,
|
|
39
|
+
-1,54,10,38,12,14,47,15,16,-1,17,18,19,20,13,-1,
|
|
40
|
+
22,23,24,25,26,11,28,29,30,31,32,-1,-1,-1,-1,-1,
|
|
41
|
+
-1,5,34,35,36,37,6,39,3,49,42,43,-1,44,4,46,
|
|
42
|
+
1,48,0,2,51,52,53,9,55,56,57,-1,-1,-1,-1,-1,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
int xrp_base58_encode_check(const uint8_t *data, int datalen, HasherType hasher_type, char *str, int strsize)
|
|
46
|
+
{
|
|
47
|
+
if (datalen > 128) {
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
uint8_t buf[datalen + 32];
|
|
51
|
+
uint8_t *hash = buf + datalen;
|
|
52
|
+
memcpy(buf, data, datalen);
|
|
53
|
+
hasher_Raw(hasher_type, data, datalen, hash);
|
|
54
|
+
size_t res = strsize;
|
|
55
|
+
bool success = base58_encode(str, &res, buf, datalen + 4, xrp_b58digits);
|
|
56
|
+
memzero(buf, sizeof(buf));
|
|
57
|
+
return success ? res : 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
int xrp_base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data, int datalen)
|
|
61
|
+
{
|
|
62
|
+
if (datalen > 128) {
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
uint8_t d[datalen + 4];
|
|
66
|
+
size_t res = datalen + 4;
|
|
67
|
+
if (base58_to_bin(d, &res, str, xrp_b58digits, xrp_b58digits_map) != true) {
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
uint8_t *nd = d + datalen + 4 - res;
|
|
71
|
+
if (base58_check(nd, res, hasher_type, str, xrp_b58digits) < 0) {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
memcpy(data, nd, res - 4);
|
|
75
|
+
return res - 4;
|
|
76
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2019 Anatolii Kurotych
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the "Software"),
|
|
6
|
+
* to deal in the Software without restriction, including without limitation
|
|
7
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
8
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
|
9
|
+
* Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included
|
|
12
|
+
* in all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
15
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
17
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
18
|
+
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
19
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
20
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
#include "TrezorCrypto/bignum.h"
|
|
24
|
+
#include "TrezorCrypto/ecdsa.h"
|
|
25
|
+
#include "TrezorCrypto/sha2.h"
|
|
26
|
+
#include <TrezorCrypto/schnorr.h>
|
|
27
|
+
#include <stdbool.h>
|
|
28
|
+
#include <stdint.h>
|
|
29
|
+
|
|
30
|
+
bool has_even_y(const curve_point *P) { return bn_is_even(&P->y); }
|
|
31
|
+
|
|
32
|
+
bool lift_x(const ecdsa_curve *curve, const uint8_t *pk, curve_point *P) {
|
|
33
|
+
if (!ecdsa_read_pubkey(curve, pk, P)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
if (!has_even_y(P)) {
|
|
37
|
+
bn_subtract(&curve->prime, &P->y, &P->y);
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// r = hash-BIP0340/nonce
|
|
43
|
+
static void calc_r(bignum256 *t, curve_point *P, const uint8_t *m, bignum256 *r) {
|
|
44
|
+
SHA256_CTX ctx;
|
|
45
|
+
uint8_t x[32];
|
|
46
|
+
uint8_t digest[SHA256_DIGEST_LENGTH];
|
|
47
|
+
|
|
48
|
+
// tag
|
|
49
|
+
sha256_Raw((uint8_t *)"BIP0340/nonce", 13, digest);
|
|
50
|
+
|
|
51
|
+
sha256_Init(&ctx);
|
|
52
|
+
sha256_Update(&ctx, digest, SHA256_DIGEST_LENGTH);
|
|
53
|
+
sha256_Update(&ctx, digest, SHA256_DIGEST_LENGTH);
|
|
54
|
+
bn_write_be(t, x);
|
|
55
|
+
sha256_Update(&ctx, x, 32);
|
|
56
|
+
bn_write_be(&P->x, x);
|
|
57
|
+
sha256_Update(&ctx, x, 32);
|
|
58
|
+
sha256_Update(&ctx, m, 32);
|
|
59
|
+
sha256_Final(&ctx, digest);
|
|
60
|
+
|
|
61
|
+
// Convert the raw bigendian 256 bit value to a normalized, partly reduced bignum
|
|
62
|
+
bn_read_be(digest, r);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// e = hash-BIP0340/challenge(R||P||m)
|
|
66
|
+
static void calc_e(const bignum256 *r, const curve_point *P, const uint8_t *m, bignum256 *e) {
|
|
67
|
+
SHA256_CTX ctx;
|
|
68
|
+
uint8_t digest[SHA256_DIGEST_LENGTH];
|
|
69
|
+
uint8_t x[32];
|
|
70
|
+
// tag
|
|
71
|
+
sha256_Raw((uint8_t *)"BIP0340/challenge", 17, digest);
|
|
72
|
+
|
|
73
|
+
sha256_Init(&ctx);
|
|
74
|
+
sha256_Update(&ctx, digest, SHA256_DIGEST_LENGTH);
|
|
75
|
+
sha256_Update(&ctx, digest, SHA256_DIGEST_LENGTH);
|
|
76
|
+
bn_write_be(r, x);
|
|
77
|
+
sha256_Update(&ctx, x, 32);
|
|
78
|
+
bn_write_be(&P->x, x);
|
|
79
|
+
sha256_Update(&ctx, x, 32);
|
|
80
|
+
sha256_Update(&ctx, m, 32);
|
|
81
|
+
sha256_Final(&ctx, digest);
|
|
82
|
+
bn_read_be(digest, e);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// t = d ^ hash-BIP0340/aux(k)
|
|
86
|
+
static void calc_t(const bignum256 *d, const bignum256 *k, bignum256 *t) {
|
|
87
|
+
SHA256_CTX ctx;
|
|
88
|
+
uint8_t digest[SHA256_DIGEST_LENGTH];
|
|
89
|
+
uint8_t kbs[32];
|
|
90
|
+
|
|
91
|
+
bn_write_be(k, kbs);
|
|
92
|
+
// tag
|
|
93
|
+
sha256_Raw((uint8_t *)"BIP0340/aux", 11, digest);
|
|
94
|
+
|
|
95
|
+
// hash-BIP0340/aux(k)
|
|
96
|
+
sha256_Init(&ctx);
|
|
97
|
+
sha256_Update(&ctx, digest, SHA256_DIGEST_LENGTH);
|
|
98
|
+
sha256_Update(&ctx, digest, SHA256_DIGEST_LENGTH);
|
|
99
|
+
sha256_Update(&ctx, kbs, 32);
|
|
100
|
+
sha256_Final(&ctx, digest);
|
|
101
|
+
|
|
102
|
+
bn_read_be(digest, t);
|
|
103
|
+
// d ^ t
|
|
104
|
+
bn_xor(t, d, t);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Returns 0 if signing succeeded
|
|
108
|
+
int schnorr_sign(const ecdsa_curve *curve, const uint8_t *sk, const bignum256 *k, const uint8_t *m,
|
|
109
|
+
schnorr_sign_pair *result) {
|
|
110
|
+
curve_point P;
|
|
111
|
+
curve_point R;
|
|
112
|
+
bignum256 d;
|
|
113
|
+
bignum256 t;
|
|
114
|
+
bignum256 s;
|
|
115
|
+
bignum256 k2; /* k' */
|
|
116
|
+
|
|
117
|
+
// d = int(sk)
|
|
118
|
+
bn_read_be(sk, &d);
|
|
119
|
+
|
|
120
|
+
// P = d*G
|
|
121
|
+
scalar_multiply(curve, &d, &P);
|
|
122
|
+
|
|
123
|
+
// d = d if has_even_y(P) else d = n - d
|
|
124
|
+
if (!has_even_y(&P))
|
|
125
|
+
bn_subtract(&curve->order, &d, &d);
|
|
126
|
+
|
|
127
|
+
// t = d ^ hash-BIP0340/aux(k)
|
|
128
|
+
calc_t(&d, k, &t);
|
|
129
|
+
|
|
130
|
+
// rand = hash-BIP0340/nonce(t, P, m)
|
|
131
|
+
calc_r(&t, &P, m, &k2);
|
|
132
|
+
|
|
133
|
+
// k' = rand % n
|
|
134
|
+
bn_fast_mod(&k2, &curve->order);
|
|
135
|
+
|
|
136
|
+
if (bn_is_zero(&k2))
|
|
137
|
+
return 1;
|
|
138
|
+
|
|
139
|
+
// R = k'*G
|
|
140
|
+
scalar_multiply(curve, &k2, &R);
|
|
141
|
+
|
|
142
|
+
// k = k' if has_even_y(R) else n - k'
|
|
143
|
+
if (!has_even_y(&R))
|
|
144
|
+
bn_subtract(&curve->order, &k2, &k2);
|
|
145
|
+
|
|
146
|
+
// e = hash-BIP0340/challenge(R,P,m)
|
|
147
|
+
calc_e(&R.x, &P, m, &s);
|
|
148
|
+
// e %= n
|
|
149
|
+
bn_fast_mod(&s, &curve->order);
|
|
150
|
+
|
|
151
|
+
// s = (k+ed)mod n
|
|
152
|
+
bn_multiply(&d, &s, &curve->order);
|
|
153
|
+
bn_addmod(&s, &k2, &curve->order);
|
|
154
|
+
|
|
155
|
+
if (bn_is_zero(&R.x) || bn_is_zero(&s))
|
|
156
|
+
return 1;
|
|
157
|
+
bn_write_be(&R.x, result->r);
|
|
158
|
+
bn_write_be(&s, result->s);
|
|
159
|
+
return 0;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Returns 0 if verification succeeded
|
|
163
|
+
int schnorr_verify(const ecdsa_curve *curve, const uint8_t *pk, const uint8_t *m, const schnorr_sign_pair *sig) {
|
|
164
|
+
curve_point P;
|
|
165
|
+
curve_point R;
|
|
166
|
+
bignum256 r;
|
|
167
|
+
bignum256 s;
|
|
168
|
+
bignum256 e;
|
|
169
|
+
|
|
170
|
+
bn_read_be(sig->r, &r);
|
|
171
|
+
bn_read_be(sig->s, &s);
|
|
172
|
+
|
|
173
|
+
if (bn_is_zero(&r))
|
|
174
|
+
return 1;
|
|
175
|
+
if (bn_is_zero(&s))
|
|
176
|
+
return 2;
|
|
177
|
+
if (!bn_is_less(&r, &curve->prime))
|
|
178
|
+
return 3;
|
|
179
|
+
if (!bn_is_less(&s, &curve->order))
|
|
180
|
+
return 4;
|
|
181
|
+
if (!lift_x(curve, pk, &P)) {
|
|
182
|
+
return 5;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// e = hash-BIP0340/challenge(r, P, m)
|
|
186
|
+
calc_e(&r, &P, m, &e);
|
|
187
|
+
// e %= n
|
|
188
|
+
bn_fast_mod(&e, &curve->order);
|
|
189
|
+
|
|
190
|
+
// R = s*G - e*P
|
|
191
|
+
bn_subtract(&curve->order, &e, &e);
|
|
192
|
+
scalar_multiply(curve, &s, &R);
|
|
193
|
+
point_multiply(curve, &e, &P, &P);
|
|
194
|
+
point_add(curve, &P, &R);
|
|
195
|
+
|
|
196
|
+
if (point_is_infinity(&R)) {
|
|
197
|
+
return 6;
|
|
198
|
+
}
|
|
199
|
+
if (!has_even_y(&R)) {
|
|
200
|
+
return 7;
|
|
201
|
+
}
|
|
202
|
+
if (!bn_is_equal(&r, &R.x)) {
|
|
203
|
+
return 8; // success
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// success
|
|
207
|
+
return 0;
|
|
208
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2016 Pavol Rusnak
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
5
|
+
* a copy of this software and associated documentation files (the "Software"),
|
|
6
|
+
* to deal in the Software without restriction, including without limitation
|
|
7
|
+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
8
|
+
* and/or sell copies of the Software, and to permit persons to whom the
|
|
9
|
+
* Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included
|
|
12
|
+
* in all copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
15
|
+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
17
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
18
|
+
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
19
|
+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
20
|
+
* OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
#include <string.h>
|
|
24
|
+
#include <TrezorCrypto/base58.h>
|
|
25
|
+
#include <TrezorCrypto/script.h>
|
|
26
|
+
|
|
27
|
+
int script_output_to_address(const uint8_t *script, int scriptlen, char *addr, int addrsize)
|
|
28
|
+
{
|
|
29
|
+
uint8_t raw[35];
|
|
30
|
+
|
|
31
|
+
// P2PKH
|
|
32
|
+
if (scriptlen == 25 && script[0] == 0x76 && script[1] == 0xA9 && script[2] == 0x14 && script[23] == 0x88 && script[24] == 0xAC) {
|
|
33
|
+
raw[0] = 0x00;
|
|
34
|
+
memcpy(raw + 1, script + 3, 20);
|
|
35
|
+
return base58_encode_check(raw, 1 + 20, HASHER_SHA2D, addr, addrsize);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// P2SH
|
|
39
|
+
if (scriptlen == 23 && script[0] == 0xA9 && script[1] == 0x14 && script[22] == 0x87) {
|
|
40
|
+
raw[0] = 0x05;
|
|
41
|
+
memcpy(raw + 1, script + 2, 20);
|
|
42
|
+
return base58_encode_check(raw, 1 + 20, HASHER_SHA2D, addr, addrsize);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// P2WPKH
|
|
46
|
+
if (scriptlen == 22 && script[0] == 0x00 && script[1] == 0x14) {
|
|
47
|
+
raw[0] = 0x06;
|
|
48
|
+
raw[1] = 0x00;
|
|
49
|
+
raw[2] = 0x00;
|
|
50
|
+
memcpy(raw + 3, script + 2, 20);
|
|
51
|
+
return base58_encode_check(raw, 3 + 20, HASHER_SHA2D, addr, addrsize);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// P2WSH
|
|
55
|
+
if (scriptlen == 34 && script[0] == 0x00 && script[1] == 0x20) {
|
|
56
|
+
raw[0] = 0x0A;
|
|
57
|
+
raw[1] = 0x00;
|
|
58
|
+
raw[2] = 0x00;
|
|
59
|
+
memcpy(raw + 3, script + 2, 32);
|
|
60
|
+
return base58_encode_check(raw, 3 + 32, HASHER_SHA2D, addr, addrsize);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/*-
|
|
2
|
+
* Copyright 2009 Colin Percival
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions
|
|
7
|
+
* are met:
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright
|
|
9
|
+
* notice, this list of conditions and the following disclaimer.
|
|
10
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
* documentation and/or other materials provided with the distribution.
|
|
13
|
+
*
|
|
14
|
+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
15
|
+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
17
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
18
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
19
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
20
|
+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
21
|
+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
22
|
+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
23
|
+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
24
|
+
* SUCH DAMAGE.
|
|
25
|
+
*
|
|
26
|
+
* This file was originally written by Colin Percival as part of the Tarsnap
|
|
27
|
+
* online backup system.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
#include <TrezorCrypto/scrypt.h>
|
|
31
|
+
|
|
32
|
+
#include <TrezorCrypto/sha2.h>
|
|
33
|
+
#include <TrezorCrypto/endian.h>
|
|
34
|
+
#include <TrezorCrypto/pbkdf2.h>
|
|
35
|
+
|
|
36
|
+
#include <sys/types.h>
|
|
37
|
+
#ifndef _WIN32
|
|
38
|
+
#include <sys/mman.h>
|
|
39
|
+
#endif
|
|
40
|
+
#include <errno.h>
|
|
41
|
+
#include <stdint.h>
|
|
42
|
+
#include <stdlib.h>
|
|
43
|
+
#include <string.h>
|
|
44
|
+
|
|
45
|
+
static void blkcpy(void *, void *, size_t);
|
|
46
|
+
static void blkxor(void *, void *, size_t);
|
|
47
|
+
static void salsa20_8(uint32_t[16]);
|
|
48
|
+
static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t);
|
|
49
|
+
static uint64_t integerify(void *, size_t);
|
|
50
|
+
static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *);
|
|
51
|
+
|
|
52
|
+
static void
|
|
53
|
+
blkcpy(void * dest, void * src, size_t len)
|
|
54
|
+
{
|
|
55
|
+
size_t * D = dest;
|
|
56
|
+
size_t * S = src;
|
|
57
|
+
size_t L = len / sizeof(size_t);
|
|
58
|
+
size_t i;
|
|
59
|
+
|
|
60
|
+
for (i = 0; i < L; i++)
|
|
61
|
+
D[i] = S[i];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static void
|
|
65
|
+
blkxor(void * dest, void * src, size_t len)
|
|
66
|
+
{
|
|
67
|
+
size_t * D = dest;
|
|
68
|
+
size_t * S = src;
|
|
69
|
+
size_t L = len / sizeof(size_t);
|
|
70
|
+
size_t i;
|
|
71
|
+
|
|
72
|
+
for (i = 0; i < L; i++)
|
|
73
|
+
D[i] ^= S[i];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* salsa20_8(B):
|
|
78
|
+
* Apply the salsa20/8 core to the provided block.
|
|
79
|
+
*/
|
|
80
|
+
static void
|
|
81
|
+
salsa20_8(uint32_t B[16])
|
|
82
|
+
{
|
|
83
|
+
uint32_t x[16];
|
|
84
|
+
size_t i;
|
|
85
|
+
|
|
86
|
+
blkcpy(x, B, 64);
|
|
87
|
+
for (i = 0; i < 8; i += 2) {
|
|
88
|
+
#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
|
|
89
|
+
/* Operate on columns. */
|
|
90
|
+
x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
|
|
91
|
+
x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
|
|
92
|
+
|
|
93
|
+
x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
|
|
94
|
+
x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
|
|
95
|
+
|
|
96
|
+
x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
|
|
97
|
+
x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
|
|
98
|
+
|
|
99
|
+
x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
|
|
100
|
+
x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
|
|
101
|
+
|
|
102
|
+
/* Operate on rows. */
|
|
103
|
+
x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
|
|
104
|
+
x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
|
|
105
|
+
|
|
106
|
+
x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
|
|
107
|
+
x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
|
|
108
|
+
|
|
109
|
+
x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
|
|
110
|
+
x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
|
|
111
|
+
|
|
112
|
+
x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
|
|
113
|
+
x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
|
|
114
|
+
#undef R
|
|
115
|
+
}
|
|
116
|
+
for (i = 0; i < 16; i++)
|
|
117
|
+
B[i] += x[i];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* blockmix_salsa8(Bin, Bout, X, r):
|
|
122
|
+
* Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
|
|
123
|
+
* bytes in length; the output Bout must also be the same size. The
|
|
124
|
+
* temporary space X must be 64 bytes.
|
|
125
|
+
*/
|
|
126
|
+
static void
|
|
127
|
+
blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
|
|
128
|
+
{
|
|
129
|
+
size_t i;
|
|
130
|
+
|
|
131
|
+
/* 1: X <-- B_{2r - 1} */
|
|
132
|
+
blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
|
|
133
|
+
|
|
134
|
+
/* 2: for i = 0 to 2r - 1 do */
|
|
135
|
+
for (i = 0; i < 2 * r; i += 2) {
|
|
136
|
+
/* 3: X <-- H(X \xor B_i) */
|
|
137
|
+
blkxor(X, &Bin[i * 16], 64);
|
|
138
|
+
salsa20_8(X);
|
|
139
|
+
|
|
140
|
+
/* 4: Y_i <-- X */
|
|
141
|
+
/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
|
|
142
|
+
blkcpy(&Bout[i * 8], X, 64);
|
|
143
|
+
|
|
144
|
+
/* 3: X <-- H(X \xor B_i) */
|
|
145
|
+
blkxor(X, &Bin[i * 16 + 16], 64);
|
|
146
|
+
salsa20_8(X);
|
|
147
|
+
|
|
148
|
+
/* 4: Y_i <-- X */
|
|
149
|
+
/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
|
|
150
|
+
blkcpy(&Bout[i * 8 + r * 16], X, 64);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* integerify(B, r):
|
|
156
|
+
* Return the result of parsing B_{2r-1} as a little-endian integer.
|
|
157
|
+
*/
|
|
158
|
+
static uint64_t
|
|
159
|
+
integerify(void * B, size_t r)
|
|
160
|
+
{
|
|
161
|
+
uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
|
|
162
|
+
|
|
163
|
+
return (((uint64_t)(X[1]) << 32) + X[0]);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* smix(B, r, N, V, XY):
|
|
168
|
+
* Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
|
|
169
|
+
* the temporary storage V must be 128rN bytes in length; the temporary
|
|
170
|
+
* storage XY must be 256r + 64 bytes in length. The value N must be a
|
|
171
|
+
* power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
|
|
172
|
+
* multiple of 64 bytes.
|
|
173
|
+
*/
|
|
174
|
+
static void
|
|
175
|
+
smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
|
|
176
|
+
{
|
|
177
|
+
uint32_t * X = XY;
|
|
178
|
+
uint32_t * Y = &XY[32 * r];
|
|
179
|
+
uint32_t * Z = &XY[64 * r];
|
|
180
|
+
uint64_t i;
|
|
181
|
+
uint64_t j;
|
|
182
|
+
size_t k;
|
|
183
|
+
|
|
184
|
+
/* 1: X <-- B */
|
|
185
|
+
for (k = 0; k < 32 * r; k++)
|
|
186
|
+
X[k] = le32dec(&B[4 * k]);
|
|
187
|
+
|
|
188
|
+
/* 2: for i = 0 to N - 1 do */
|
|
189
|
+
for (i = 0; i < N; i += 2) {
|
|
190
|
+
/* 3: V_i <-- X */
|
|
191
|
+
blkcpy(&V[i * (32 * r)], X, 128 * r);
|
|
192
|
+
|
|
193
|
+
/* 4: X <-- H(X) */
|
|
194
|
+
blockmix_salsa8(X, Y, Z, r);
|
|
195
|
+
|
|
196
|
+
/* 3: V_i <-- X */
|
|
197
|
+
blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
|
|
198
|
+
|
|
199
|
+
/* 4: X <-- H(X) */
|
|
200
|
+
blockmix_salsa8(Y, X, Z, r);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/* 6: for i = 0 to N - 1 do */
|
|
204
|
+
for (i = 0; i < N; i += 2) {
|
|
205
|
+
/* 7: j <-- Integerify(X) mod N */
|
|
206
|
+
j = integerify(X, r) & (N - 1);
|
|
207
|
+
|
|
208
|
+
/* 8: X <-- H(X \xor V_j) */
|
|
209
|
+
blkxor(X, &V[j * (32 * r)], 128 * r);
|
|
210
|
+
blockmix_salsa8(X, Y, Z, r);
|
|
211
|
+
|
|
212
|
+
/* 7: j <-- Integerify(X) mod N */
|
|
213
|
+
j = integerify(Y, r) & (N - 1);
|
|
214
|
+
|
|
215
|
+
/* 8: X <-- H(X \xor V_j) */
|
|
216
|
+
blkxor(Y, &V[j * (32 * r)], 128 * r);
|
|
217
|
+
blockmix_salsa8(Y, X, Z, r);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* 10: B' <-- X */
|
|
221
|
+
for (k = 0; k < 32 * r; k++)
|
|
222
|
+
le32enc(&B[4 * k], X[k]);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
|
227
|
+
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
|
|
228
|
+
* p, buflen) and write the result into buf. The parameters r, p, and buflen
|
|
229
|
+
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
|
|
230
|
+
* must be a power of 2 greater than 1.
|
|
231
|
+
*
|
|
232
|
+
* Return 0 on success; or -1 on error
|
|
233
|
+
*/
|
|
234
|
+
int
|
|
235
|
+
scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
236
|
+
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
|
|
237
|
+
uint8_t * buf, size_t buflen)
|
|
238
|
+
{
|
|
239
|
+
void * B0, * V0, * XY0;
|
|
240
|
+
uint8_t * B;
|
|
241
|
+
uint32_t * V;
|
|
242
|
+
uint32_t * XY;
|
|
243
|
+
uint32_t i;
|
|
244
|
+
|
|
245
|
+
/* Sanity-check parameters. */
|
|
246
|
+
#if SIZE_MAX > UINT32_MAX
|
|
247
|
+
if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
|
|
248
|
+
errno = EFBIG;
|
|
249
|
+
goto err0;
|
|
250
|
+
}
|
|
251
|
+
#endif
|
|
252
|
+
if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
|
|
253
|
+
errno = EFBIG;
|
|
254
|
+
goto err0;
|
|
255
|
+
}
|
|
256
|
+
if (r == 0 || p == 0) {
|
|
257
|
+
errno = EINVAL;
|
|
258
|
+
goto err0;
|
|
259
|
+
}
|
|
260
|
+
if (((N & (N - 1)) != 0) || (N < 2)) {
|
|
261
|
+
errno = EINVAL;
|
|
262
|
+
goto err0;
|
|
263
|
+
}
|
|
264
|
+
if ((r > SIZE_MAX / 128 / p) ||
|
|
265
|
+
#if SIZE_MAX / 256 <= UINT32_MAX
|
|
266
|
+
(r > SIZE_MAX / 256) ||
|
|
267
|
+
#endif
|
|
268
|
+
(N > SIZE_MAX / 128 / r)) {
|
|
269
|
+
errno = ENOMEM;
|
|
270
|
+
goto err0;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/* Allocate memory. */
|
|
274
|
+
#ifdef HAVE_POSIX_MEMALIGN
|
|
275
|
+
if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
|
|
276
|
+
goto err0;
|
|
277
|
+
B = (uint8_t *)(B0);
|
|
278
|
+
if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
|
|
279
|
+
goto err1;
|
|
280
|
+
XY = (uint32_t *)(XY0);
|
|
281
|
+
#ifndef MAP_ANON
|
|
282
|
+
if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
|
|
283
|
+
goto err2;
|
|
284
|
+
V = (uint32_t *)(V0);
|
|
285
|
+
#endif
|
|
286
|
+
#else
|
|
287
|
+
if ((B0 = malloc(128 * r * p + 63)) == NULL)
|
|
288
|
+
goto err0;
|
|
289
|
+
B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
|
|
290
|
+
if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
|
|
291
|
+
goto err1;
|
|
292
|
+
XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
|
|
293
|
+
#ifndef MAP_ANON
|
|
294
|
+
if ((V0 = malloc(128 * r * N + 63)) == NULL)
|
|
295
|
+
goto err2;
|
|
296
|
+
V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
|
|
297
|
+
#endif
|
|
298
|
+
#endif
|
|
299
|
+
#ifdef MAP_ANON
|
|
300
|
+
if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
|
|
301
|
+
#ifdef MAP_NOCORE
|
|
302
|
+
MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
|
|
303
|
+
#else
|
|
304
|
+
MAP_ANON | MAP_PRIVATE,
|
|
305
|
+
#endif
|
|
306
|
+
-1, 0)) == MAP_FAILED)
|
|
307
|
+
goto err2;
|
|
308
|
+
V = (uint32_t *)(V0);
|
|
309
|
+
#endif
|
|
310
|
+
|
|
311
|
+
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
|
|
312
|
+
pbkdf2_hmac_sha256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
|
|
313
|
+
|
|
314
|
+
/* 2: for i = 0 to p - 1 do */
|
|
315
|
+
for (i = 0; i < p; i++) {
|
|
316
|
+
/* 3: B_i <-- MF(B_i, N) */
|
|
317
|
+
smix(&B[i * 128 * r], r, N, V, XY);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
|
|
321
|
+
pbkdf2_hmac_sha256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
|
|
322
|
+
|
|
323
|
+
/* Free memory. */
|
|
324
|
+
#ifdef MAP_ANON
|
|
325
|
+
if (munmap(V0, 128 * r * N))
|
|
326
|
+
goto err2;
|
|
327
|
+
#else
|
|
328
|
+
free(V0);
|
|
329
|
+
#endif
|
|
330
|
+
free(XY0);
|
|
331
|
+
free(B0);
|
|
332
|
+
|
|
333
|
+
/* Success! */
|
|
334
|
+
return (0);
|
|
335
|
+
|
|
336
|
+
err2:
|
|
337
|
+
free(XY0);
|
|
338
|
+
err1:
|
|
339
|
+
free(B0);
|
|
340
|
+
err0:
|
|
341
|
+
/* Failure! */
|
|
342
|
+
return (-1);
|
|
343
|
+
}
|