@onekeyfe/react-native-lite-card 6.7.0-4 → 6.7.0-41
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 +12 -2
- package/ReactNativeLiteCard.podspec +31 -2
- package/android/build.gradle +8 -0
- package/android/consumer-rules.pro +4 -0
- package/android/src/main/cpp/CMakeLists.txt +6 -2
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +60 -49
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +117 -39
- 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 +24 -2
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +46 -19
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +38 -1
- package/ios/Classes/OKNFCBridge.mm +24 -1
- package/ios/Classes/OKNFTLite/OKLiteV1.m +20 -10
- package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
- package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
- package/ios/Classes/OKNFTLite/OKNFCManager.m +110 -18
- package/ios/ReactNativeLiteCard.mm +23 -11
- package/lib/module/index.js +4 -4
- package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +4 -4
- package/lib/typescript/src/index.d.ts +2 -2
- package/package.json +8 -8
- package/sdk/CMakeLists.txt +121 -0
- package/sdk/README.md +190 -0
- package/sdk/android/implJni.cpp +216 -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/scripts/generate-source-id.rb +22 -0
- package/sdk/source-id-manifest.txt +19 -0
- package/sdk/source-id.cmake +31 -0
- package/sdk/src/ApduBuilder.cpp +332 -0
- package/sdk/src/GPChannelSDK.cpp +258 -0
- package/sdk/src/random.c +25 -0
- package/sdk/src/version.c +3 -0
- package/sdk/tests/android/GPChannelProxy.java +55 -0
- package/sdk/tests/android_verify.py +124 -0
- package/sdk/tests/safety.cpp +757 -0
- package/sdk/tests/verify.py +284 -0
- package/src/NativeReactNativeLiteCard.ts +4 -2
- package/src/index.tsx +14 -3
- 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,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2013-2014 Tomas Dzetkulic
|
|
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
|
+
|
|
26
|
+
#include "options.h"
|
|
27
|
+
|
|
28
|
+
#include <TrezorCrypto/hmac.h>
|
|
29
|
+
#include <TrezorCrypto/memzero.h>
|
|
30
|
+
|
|
31
|
+
void hmac_sha256_Init(HMAC_SHA256_CTX *hctx, const uint8_t *key, const uint32_t keylen)
|
|
32
|
+
{
|
|
33
|
+
CONFIDENTIAL uint8_t i_key_pad[SHA256_BLOCK_LENGTH];
|
|
34
|
+
memset(i_key_pad, 0, SHA256_BLOCK_LENGTH);
|
|
35
|
+
if (keylen > SHA256_BLOCK_LENGTH) {
|
|
36
|
+
sha256_Raw(key, keylen, i_key_pad);
|
|
37
|
+
} else {
|
|
38
|
+
memcpy(i_key_pad, key, keylen);
|
|
39
|
+
}
|
|
40
|
+
for (int i = 0; i < SHA256_BLOCK_LENGTH; i++) {
|
|
41
|
+
hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
|
|
42
|
+
i_key_pad[i] ^= 0x36;
|
|
43
|
+
}
|
|
44
|
+
sha256_Init(&(hctx->ctx));
|
|
45
|
+
sha256_Update(&(hctx->ctx), i_key_pad, SHA256_BLOCK_LENGTH);
|
|
46
|
+
memzero(i_key_pad, sizeof(i_key_pad));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void hmac_sha256_Update(HMAC_SHA256_CTX *hctx, const uint8_t *msg, const uint32_t msglen)
|
|
50
|
+
{
|
|
51
|
+
sha256_Update(&(hctx->ctx), msg, msglen);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void hmac_sha256_Final(HMAC_SHA256_CTX *hctx, uint8_t *hmac)
|
|
55
|
+
{
|
|
56
|
+
sha256_Final(&(hctx->ctx), hmac);
|
|
57
|
+
sha256_Init(&(hctx->ctx));
|
|
58
|
+
sha256_Update(&(hctx->ctx), hctx->o_key_pad, SHA256_BLOCK_LENGTH);
|
|
59
|
+
sha256_Update(&(hctx->ctx), hmac, SHA256_DIGEST_LENGTH);
|
|
60
|
+
sha256_Final(&(hctx->ctx), hmac);
|
|
61
|
+
memzero(hctx, sizeof(HMAC_SHA256_CTX));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
|
|
65
|
+
{
|
|
66
|
+
CONFIDENTIAL HMAC_SHA256_CTX hctx;
|
|
67
|
+
hmac_sha256_Init(&hctx, key, keylen);
|
|
68
|
+
hmac_sha256_Update(&hctx, msg, msglen);
|
|
69
|
+
hmac_sha256_Final(&hctx, hmac);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void hmac_sha256_prepare(const uint8_t *key, const uint32_t keylen, uint32_t *opad_digest, uint32_t *ipad_digest)
|
|
73
|
+
{
|
|
74
|
+
CONFIDENTIAL uint32_t key_pad[SHA256_BLOCK_LENGTH/sizeof(uint32_t)];
|
|
75
|
+
|
|
76
|
+
memzero(key_pad, sizeof(key_pad));
|
|
77
|
+
if (keylen > SHA256_BLOCK_LENGTH) {
|
|
78
|
+
CONFIDENTIAL SHA256_CTX context;
|
|
79
|
+
sha256_Init(&context);
|
|
80
|
+
sha256_Update(&context, key, keylen);
|
|
81
|
+
sha256_Final(&context, (uint8_t*)key_pad);
|
|
82
|
+
} else {
|
|
83
|
+
memcpy(key_pad, key, keylen);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/* compute o_key_pad and its digest */
|
|
87
|
+
for (int i = 0; i < SHA256_BLOCK_LENGTH/(int)sizeof(uint32_t); i++) {
|
|
88
|
+
uint32_t data;
|
|
89
|
+
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
90
|
+
REVERSE32(key_pad[i], data);
|
|
91
|
+
#else
|
|
92
|
+
data = key_pad[i];
|
|
93
|
+
#endif
|
|
94
|
+
key_pad[i] = data ^ 0x5c5c5c5c;
|
|
95
|
+
}
|
|
96
|
+
sha256_Transform(sha256_initial_hash_value, key_pad, opad_digest);
|
|
97
|
+
|
|
98
|
+
/* convert o_key_pad to i_key_pad and compute its digest */
|
|
99
|
+
for (int i = 0; i < SHA256_BLOCK_LENGTH/(int)sizeof(uint32_t); i++) {
|
|
100
|
+
key_pad[i] = key_pad[i] ^ 0x5c5c5c5c ^ 0x36363636;
|
|
101
|
+
}
|
|
102
|
+
sha256_Transform(sha256_initial_hash_value, key_pad, ipad_digest);
|
|
103
|
+
memzero(key_pad, sizeof(key_pad));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void hmac_sha512_Init(HMAC_SHA512_CTX *hctx, const uint8_t *key, const uint32_t keylen)
|
|
107
|
+
{
|
|
108
|
+
CONFIDENTIAL uint8_t i_key_pad[SHA512_BLOCK_LENGTH];
|
|
109
|
+
memset(i_key_pad, 0, SHA512_BLOCK_LENGTH);
|
|
110
|
+
if (keylen > SHA512_BLOCK_LENGTH) {
|
|
111
|
+
sha512_Raw(key, keylen, i_key_pad);
|
|
112
|
+
} else {
|
|
113
|
+
memcpy(i_key_pad, key, keylen);
|
|
114
|
+
}
|
|
115
|
+
for (int i = 0; i < SHA512_BLOCK_LENGTH; i++) {
|
|
116
|
+
hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
|
|
117
|
+
i_key_pad[i] ^= 0x36;
|
|
118
|
+
}
|
|
119
|
+
sha512_Init(&(hctx->ctx));
|
|
120
|
+
sha512_Update(&(hctx->ctx), i_key_pad, SHA512_BLOCK_LENGTH);
|
|
121
|
+
memzero(i_key_pad, sizeof(i_key_pad));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
void hmac_sha512_Update(HMAC_SHA512_CTX *hctx, const uint8_t *msg, const uint32_t msglen)
|
|
125
|
+
{
|
|
126
|
+
sha512_Update(&(hctx->ctx), msg, msglen);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
void hmac_sha512_Final(HMAC_SHA512_CTX *hctx, uint8_t *hmac)
|
|
130
|
+
{
|
|
131
|
+
sha512_Final(&(hctx->ctx), hmac);
|
|
132
|
+
sha512_Init(&(hctx->ctx));
|
|
133
|
+
sha512_Update(&(hctx->ctx), hctx->o_key_pad, SHA512_BLOCK_LENGTH);
|
|
134
|
+
sha512_Update(&(hctx->ctx), hmac, SHA512_DIGEST_LENGTH);
|
|
135
|
+
sha512_Final(&(hctx->ctx), hmac);
|
|
136
|
+
memzero(hctx, sizeof(HMAC_SHA512_CTX));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
|
|
140
|
+
{
|
|
141
|
+
HMAC_SHA512_CTX hctx;
|
|
142
|
+
hmac_sha512_Init(&hctx, key, keylen);
|
|
143
|
+
hmac_sha512_Update(&hctx, msg, msglen);
|
|
144
|
+
hmac_sha512_Final(&hctx, hmac);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void hmac_sha512_prepare(const uint8_t *key, const uint32_t keylen, uint64_t *opad_digest, uint64_t *ipad_digest)
|
|
148
|
+
{
|
|
149
|
+
CONFIDENTIAL uint64_t key_pad[SHA512_BLOCK_LENGTH/sizeof(uint64_t)];
|
|
150
|
+
|
|
151
|
+
memzero(key_pad, sizeof(key_pad));
|
|
152
|
+
if (keylen > SHA512_BLOCK_LENGTH) {
|
|
153
|
+
CONFIDENTIAL SHA512_CTX context;
|
|
154
|
+
sha512_Init(&context);
|
|
155
|
+
sha512_Update(&context, key, keylen);
|
|
156
|
+
sha512_Final(&context, (uint8_t*)key_pad);
|
|
157
|
+
} else {
|
|
158
|
+
memcpy(key_pad, key, keylen);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* compute o_key_pad and its digest */
|
|
162
|
+
for (int i = 0; i < SHA512_BLOCK_LENGTH/(int)sizeof(uint64_t); i++) {
|
|
163
|
+
uint64_t data;
|
|
164
|
+
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
165
|
+
REVERSE64(key_pad[i], data);
|
|
166
|
+
#else
|
|
167
|
+
data = key_pad[i];
|
|
168
|
+
#endif
|
|
169
|
+
key_pad[i] = data ^ 0x5c5c5c5c5c5c5c5c;
|
|
170
|
+
}
|
|
171
|
+
sha512_Transform(sha512_initial_hash_value, key_pad, opad_digest);
|
|
172
|
+
|
|
173
|
+
/* convert o_key_pad to i_key_pad and compute its digest */
|
|
174
|
+
for (int i = 0; i < SHA512_BLOCK_LENGTH/(int)sizeof(uint64_t); i++) {
|
|
175
|
+
key_pad[i] = key_pad[i] ^ 0x5c5c5c5c5c5c5c5c ^ 0x3636363636363636;
|
|
176
|
+
}
|
|
177
|
+
sha512_Transform(sha512_initial_hash_value, key_pad, ipad_digest);
|
|
178
|
+
memzero(key_pad, sizeof(key_pad));
|
|
179
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// Copyright (c) 2014-2018, 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
|
+
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
|
30
|
+
|
|
31
|
+
#include <assert.h>
|
|
32
|
+
#include <string.h>
|
|
33
|
+
#include <stdbool.h>
|
|
34
|
+
#include <sys/types.h>
|
|
35
|
+
|
|
36
|
+
#include "int-util.h"
|
|
37
|
+
|
|
38
|
+
#include <TrezorCrypto/monero/base58.h>
|
|
39
|
+
#include <TrezorCrypto/base58.h>
|
|
40
|
+
#include <TrezorCrypto/sha2.h>
|
|
41
|
+
|
|
42
|
+
const size_t alphabet_size = 58; // sizeof(b58digits_ordered) - 1;
|
|
43
|
+
const size_t encoded_block_sizes[] = {0, 2, 3, 5, 6, 7, 9, 10, 11};
|
|
44
|
+
const size_t full_block_size = sizeof(encoded_block_sizes) / sizeof(encoded_block_sizes[0]) - 1;
|
|
45
|
+
const size_t full_encoded_block_size = 11; // encoded_block_sizes[full_block_size];
|
|
46
|
+
const size_t addr_checksum_size = 4;
|
|
47
|
+
const int decoded_block_sizes[] = {0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8};
|
|
48
|
+
#define reverse_alphabet(letter) ((int8_t) b58digits_map[(int)letter])
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
uint64_t uint_8be_to_64(const uint8_t* data, size_t size)
|
|
52
|
+
{
|
|
53
|
+
assert(1 <= size && size <= sizeof(uint64_t));
|
|
54
|
+
|
|
55
|
+
uint64_t res = 0;
|
|
56
|
+
switch (9 - size)
|
|
57
|
+
{
|
|
58
|
+
case 1: res |= *data++; /* FALLTHRU */
|
|
59
|
+
case 2: res <<= 8; res |= *data++; /* FALLTHRU */
|
|
60
|
+
case 3: res <<= 8; res |= *data++; /* FALLTHRU */
|
|
61
|
+
case 4: res <<= 8; res |= *data++; /* FALLTHRU */
|
|
62
|
+
case 5: res <<= 8; res |= *data++; /* FALLTHRU */
|
|
63
|
+
case 6: res <<= 8; res |= *data++; /* FALLTHRU */
|
|
64
|
+
case 7: res <<= 8; res |= *data++; /* FALLTHRU */
|
|
65
|
+
case 8: res <<= 8; res |= *data; break;
|
|
66
|
+
default: assert(false);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return res;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void uint_64_to_8be(uint64_t num, size_t size, uint8_t* data)
|
|
73
|
+
{
|
|
74
|
+
assert(1 <= size && size <= sizeof(uint64_t));
|
|
75
|
+
|
|
76
|
+
uint64_t num_be = SWAP64(num);
|
|
77
|
+
memcpy(data, (uint8_t*)(&num_be) + sizeof(uint64_t) - size, size);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
void encode_block(const char* block, size_t size, char* res)
|
|
81
|
+
{
|
|
82
|
+
assert(1 <= size && size <= full_block_size);
|
|
83
|
+
|
|
84
|
+
uint64_t num = uint_8be_to_64((uint8_t*)(block), size);
|
|
85
|
+
int i = ((int)(encoded_block_sizes[size])) - 1;
|
|
86
|
+
while (0 <= i)
|
|
87
|
+
{
|
|
88
|
+
uint64_t remainder = num % alphabet_size;
|
|
89
|
+
num /= alphabet_size;
|
|
90
|
+
res[i] = b58digits_ordered[remainder];
|
|
91
|
+
--i;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
bool decode_block(const char* block, size_t size, char* res)
|
|
96
|
+
{
|
|
97
|
+
assert(1 <= size && size <= full_encoded_block_size);
|
|
98
|
+
|
|
99
|
+
int res_size = decoded_block_sizes[size];
|
|
100
|
+
if (res_size <= 0)
|
|
101
|
+
return false; // Invalid block size
|
|
102
|
+
|
|
103
|
+
uint64_t res_num = 0;
|
|
104
|
+
uint64_t order = 1;
|
|
105
|
+
for (size_t i = size - 1; i < size; --i)
|
|
106
|
+
{
|
|
107
|
+
int digit = reverse_alphabet(block[i]);
|
|
108
|
+
if (digit < 0)
|
|
109
|
+
return false; // Invalid symbol
|
|
110
|
+
|
|
111
|
+
uint64_t product_hi;
|
|
112
|
+
uint64_t tmp = res_num + mul128(order, (uint64_t) digit, &product_hi);
|
|
113
|
+
if (tmp < res_num || 0 != product_hi)
|
|
114
|
+
return false; // Overflow
|
|
115
|
+
|
|
116
|
+
res_num = tmp;
|
|
117
|
+
order *= alphabet_size; // Never overflows, 58^10 < 2^64
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if ((size_t)res_size < full_block_size && (UINT64_C(1) << (8 * res_size)) <= res_num)
|
|
121
|
+
return false; // Overflow
|
|
122
|
+
|
|
123
|
+
uint_64_to_8be(res_num, res_size, (uint8_t*)(res));
|
|
124
|
+
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data, size_t binsz)
|
|
130
|
+
{
|
|
131
|
+
if (binsz==0)
|
|
132
|
+
return true;
|
|
133
|
+
|
|
134
|
+
const char * data_bin = data;
|
|
135
|
+
size_t full_block_count = binsz / full_block_size;
|
|
136
|
+
size_t last_block_size = binsz % full_block_size;
|
|
137
|
+
size_t res_size = full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size];
|
|
138
|
+
|
|
139
|
+
if (b58sz){
|
|
140
|
+
if (res_size >= *b58sz){
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
*b58sz = res_size;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
for (size_t i = 0; i < full_block_count; ++i)
|
|
147
|
+
{
|
|
148
|
+
encode_block(data_bin + i * full_block_size, full_block_size, b58 + i * full_encoded_block_size);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (0 < last_block_size)
|
|
152
|
+
{
|
|
153
|
+
encode_block(data_bin + full_block_count * full_block_size, last_block_size, b58 + full_block_count * full_encoded_block_size);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
bool xmr_base58_decode(const char *b58, size_t b58sz, void *data, size_t *binsz)
|
|
160
|
+
{
|
|
161
|
+
if (b58sz == 0) {
|
|
162
|
+
*binsz = 0;
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
size_t full_block_count = b58sz / full_encoded_block_size;
|
|
167
|
+
size_t last_block_size = b58sz % full_encoded_block_size;
|
|
168
|
+
int last_block_decoded_size = decoded_block_sizes[last_block_size];
|
|
169
|
+
if (last_block_decoded_size < 0) {
|
|
170
|
+
*binsz = 0;
|
|
171
|
+
return false; // Invalid enc length
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
size_t data_size = full_block_count * full_block_size + last_block_decoded_size;
|
|
175
|
+
if (*binsz < data_size){
|
|
176
|
+
*binsz = 0;
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
char * data_bin = data;
|
|
181
|
+
for (size_t i = 0; i < full_block_count; ++i)
|
|
182
|
+
{
|
|
183
|
+
if (!decode_block(b58 + i * full_encoded_block_size, full_encoded_block_size, data_bin + i * full_block_size))
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (0 < last_block_size)
|
|
188
|
+
{
|
|
189
|
+
if (!decode_block(b58 + full_block_count * full_encoded_block_size, last_block_size,
|
|
190
|
+
data_bin + full_block_count * full_block_size))
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data, size_t binsz, char *b58, size_t b58sz)
|
|
198
|
+
{
|
|
199
|
+
if (binsz > 128 || tag > 127) { // tag varint
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
size_t b58size = b58sz;
|
|
204
|
+
uint8_t buf[binsz + 1 + HASHER_DIGEST_LENGTH];
|
|
205
|
+
uint8_t *hash = buf + binsz + 1;
|
|
206
|
+
buf[0] = (uint8_t) tag;
|
|
207
|
+
memcpy(buf + 1, data, binsz);
|
|
208
|
+
hasher_Raw(HASHER_SHA3K, buf, binsz + 1, hash);
|
|
209
|
+
|
|
210
|
+
bool r = xmr_base58_encode(b58, &b58size, buf, binsz + 1 + addr_checksum_size);
|
|
211
|
+
return (int) (!r ? 0 : b58size);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag, void *data, size_t datalen)
|
|
215
|
+
{
|
|
216
|
+
size_t buflen = 1 + 64 + addr_checksum_size;
|
|
217
|
+
uint8_t buf[buflen];
|
|
218
|
+
uint8_t hash[HASHER_DIGEST_LENGTH];
|
|
219
|
+
|
|
220
|
+
if (!xmr_base58_decode(addr, sz, buf, &buflen)){
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
size_t res_size = buflen - addr_checksum_size - 1;
|
|
225
|
+
if (datalen < res_size){
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (buflen <= addr_checksum_size+1) {
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
hasher_Raw(HASHER_SHA3K, buf, buflen - addr_checksum_size, hash);
|
|
234
|
+
if (memcmp(hash, buf + buflen - addr_checksum_size, addr_checksum_size) != 0){
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
*tag = buf[0];
|
|
239
|
+
if (*tag > 127){
|
|
240
|
+
return false; // varint
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
memcpy(data, buf+1, res_size);
|
|
244
|
+
return (int) res_size;
|
|
245
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Copyright (c) 2014-2018, 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
|
+
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
|
30
|
+
|
|
31
|
+
#pragma once
|
|
32
|
+
|
|
33
|
+
#include <assert.h>
|
|
34
|
+
#include <stdint.h>
|
|
35
|
+
|
|
36
|
+
static inline uint64_t hi_dword(uint64_t val) {
|
|
37
|
+
return val >> 32;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static inline uint64_t lo_dword(uint64_t val) {
|
|
41
|
+
return val & 0xFFFFFFFF;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
|
|
45
|
+
// multiplier = ab = a * 2^32 + b
|
|
46
|
+
// multiplicand = cd = c * 2^32 + d
|
|
47
|
+
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
|
|
48
|
+
uint64_t a = hi_dword(multiplier);
|
|
49
|
+
uint64_t b = lo_dword(multiplier);
|
|
50
|
+
uint64_t c = hi_dword(multiplicand);
|
|
51
|
+
uint64_t d = lo_dword(multiplicand);
|
|
52
|
+
|
|
53
|
+
uint64_t ac = a * c;
|
|
54
|
+
uint64_t ad = a * d;
|
|
55
|
+
uint64_t bc = b * c;
|
|
56
|
+
uint64_t bd = b * d;
|
|
57
|
+
|
|
58
|
+
uint64_t adbc = ad + bc;
|
|
59
|
+
uint64_t adbc_carry = adbc < ad ? 1 : 0;
|
|
60
|
+
|
|
61
|
+
// multiplier * multiplicand = product_hi * 2^64 + product_lo
|
|
62
|
+
uint64_t product_lo = bd + (adbc << 32);
|
|
63
|
+
uint64_t product_lo_carry = product_lo < bd ? 1 : 0;
|
|
64
|
+
*product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry;
|
|
65
|
+
assert(ac <= *product_hi);
|
|
66
|
+
|
|
67
|
+
return product_lo;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#define SWAP64(x) ((((uint64_t) (x) & 0x00000000000000ff) << 56) | \
|
|
71
|
+
(((uint64_t) (x) & 0x000000000000ff00) << 40) | \
|
|
72
|
+
(((uint64_t) (x) & 0x0000000000ff0000) << 24) | \
|
|
73
|
+
(((uint64_t) (x) & 0x00000000ff000000) << 8) | \
|
|
74
|
+
(((uint64_t) (x) & 0x000000ff00000000) >> 8) | \
|
|
75
|
+
(((uint64_t) (x) & 0x0000ff0000000000) >> 24) | \
|
|
76
|
+
(((uint64_t) (x) & 0x00ff000000000000) >> 40) | \
|
|
77
|
+
(((uint64_t) (x) & 0xff00000000000000) >> 56))
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Dusan Klinec on 10/05/2018.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#include <TrezorCrypto/monero/range_proof.h>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
static void xmr_hash_ge25519_to_scalar(bignum256modm r, const ge25519 *p){
|
|
9
|
+
unsigned char buff[32];
|
|
10
|
+
ge25519_pack(buff, p);
|
|
11
|
+
xmr_hash_to_scalar(r, buff, sizeof(buff));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
void xmr_gen_range_sig(xmr_range_sig_t * sig, ge25519 * C, bignum256modm mask, xmr_amount amount, bignum256modm * last_mask){
|
|
15
|
+
bignum256modm ai[64];
|
|
16
|
+
bignum256modm alpha[64];
|
|
17
|
+
xmr_gen_range_sig_ex(sig, C, mask, amount, last_mask, ai, alpha);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
void xmr_gen_range_sig_ex(xmr_range_sig_t * sig, ge25519 * C, bignum256modm mask, xmr_amount amount, bignum256modm * last_mask,
|
|
21
|
+
bignum256modm ai[64], bignum256modm alpha[64])
|
|
22
|
+
{
|
|
23
|
+
const unsigned n = XMR_ATOMS;
|
|
24
|
+
bignum256modm a={0};
|
|
25
|
+
bignum256modm si={0};
|
|
26
|
+
bignum256modm c={0};
|
|
27
|
+
bignum256modm ee={0};
|
|
28
|
+
unsigned char buff[32];
|
|
29
|
+
|
|
30
|
+
Hasher kck;
|
|
31
|
+
xmr_hasher_init(&kck);
|
|
32
|
+
|
|
33
|
+
ge25519 C_acc;
|
|
34
|
+
ge25519 C_h;
|
|
35
|
+
ge25519 C_tmp;
|
|
36
|
+
ge25519 L;
|
|
37
|
+
ge25519 Zero;
|
|
38
|
+
|
|
39
|
+
ge25519_set_neutral(&Zero);
|
|
40
|
+
ge25519_set_neutral(&C_acc);
|
|
41
|
+
ge25519_set_xmr_h(&C_h);
|
|
42
|
+
set256_modm(a, 0);
|
|
43
|
+
|
|
44
|
+
#define BB(i) ((amount>>(i)) & 1)
|
|
45
|
+
|
|
46
|
+
// First pass, generates: ai, alpha, Ci, ee, s1
|
|
47
|
+
for(unsigned ii=0; ii<n; ++ii){
|
|
48
|
+
xmr_random_scalar(ai[ii]);
|
|
49
|
+
if (last_mask != NULL && ii == n - 1){
|
|
50
|
+
sub256_modm(ai[ii], *last_mask, a);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
add256_modm(a, a, ai[ii]); // creating the total mask since you have to pass this to receiver...
|
|
54
|
+
xmr_random_scalar(alpha[ii]);
|
|
55
|
+
|
|
56
|
+
ge25519_scalarmult_base_niels(&L, ge25519_niels_base_multiples, alpha[ii]);
|
|
57
|
+
ge25519_scalarmult_base_niels(&C_tmp, ge25519_niels_base_multiples, ai[ii]);
|
|
58
|
+
|
|
59
|
+
// C_tmp += &Zero if BB(ii) == 0 else &C_h
|
|
60
|
+
ge25519_add(&C_tmp, &C_tmp, BB(ii) == 0 ? &Zero : &C_h, 0);
|
|
61
|
+
ge25519_add(&C_acc, &C_acc, &C_tmp, 0);
|
|
62
|
+
|
|
63
|
+
// Set Ci[ii] to sigs
|
|
64
|
+
ge25519_pack(sig->Ci[ii], &C_tmp);
|
|
65
|
+
|
|
66
|
+
if (BB(ii) == 0) {
|
|
67
|
+
xmr_random_scalar(si);
|
|
68
|
+
xmr_hash_ge25519_to_scalar(c, &L);
|
|
69
|
+
|
|
70
|
+
ge25519_add(&C_tmp, &C_tmp, &C_h, 1); // Ci[ii] -= c_h
|
|
71
|
+
xmr_add_keys2_vartime(&L, si, c, &C_tmp);
|
|
72
|
+
|
|
73
|
+
// Set s1[ii] to sigs
|
|
74
|
+
contract256_modm(sig->asig.s1[ii], si);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
ge25519_pack(buff, &L);
|
|
78
|
+
xmr_hasher_update(&kck, buff, sizeof(buff));
|
|
79
|
+
|
|
80
|
+
ge25519_double(&C_h, &C_h); // c_H = crypto.scalarmult(c_H, 2)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Compute ee
|
|
84
|
+
xmr_hasher_final(&kck, buff);
|
|
85
|
+
expand256_modm(ee, buff, sizeof(buff));
|
|
86
|
+
|
|
87
|
+
ge25519_set_xmr_h(&C_h);
|
|
88
|
+
|
|
89
|
+
// Second pass, s0, s1
|
|
90
|
+
for(unsigned ii=0; ii<n; ++ii){
|
|
91
|
+
if (BB(ii) == 0){
|
|
92
|
+
mulsub256_modm(si, ai[ii], ee, alpha[ii]);
|
|
93
|
+
contract256_modm(sig->asig.s0[ii], si);
|
|
94
|
+
|
|
95
|
+
} else {
|
|
96
|
+
xmr_random_scalar(si);
|
|
97
|
+
contract256_modm(sig->asig.s0[ii], si);
|
|
98
|
+
|
|
99
|
+
ge25519_unpack_vartime(&C_tmp, sig->Ci[ii]);
|
|
100
|
+
xmr_add_keys2_vartime(&L, si, ee, &C_tmp);
|
|
101
|
+
xmr_hash_ge25519_to_scalar(c, &L);
|
|
102
|
+
|
|
103
|
+
mulsub256_modm(si, ai[ii], c, alpha[ii]);
|
|
104
|
+
contract256_modm(sig->asig.s1[ii], si);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
ge25519_double(&C_h, &C_h); // c_H = crypto.scalarmult(c_H, 2)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
ge25519_copy(C, &C_acc);
|
|
111
|
+
copy256_modm(mask, a);
|
|
112
|
+
contract256_modm(sig->asig.ee, ee);
|
|
113
|
+
#undef BB
|
|
114
|
+
}
|
|
115
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Created by Dusan Klinec on 02/05/2018.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#include <TrezorCrypto/monero/serialize.h>
|
|
6
|
+
|
|
7
|
+
int xmr_size_varint(uint64_t num){
|
|
8
|
+
int ctr = 1;
|
|
9
|
+
while (num >= 0x80) {
|
|
10
|
+
++ctr;
|
|
11
|
+
num >>= 7;
|
|
12
|
+
}
|
|
13
|
+
return ctr;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
int xmr_write_varint(uint8_t * buff, size_t buff_size, uint64_t num){
|
|
17
|
+
unsigned ctr = 0;
|
|
18
|
+
while (num >= 0x80 && ctr < buff_size) {
|
|
19
|
+
*buff = (uint8_t) (((num) & 0x7f) | 0x80);
|
|
20
|
+
++buff;
|
|
21
|
+
++ctr;
|
|
22
|
+
num >>= 7;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* writes the last one to dest */
|
|
26
|
+
if (ctr < buff_size) {
|
|
27
|
+
*buff = (uint8_t) num;
|
|
28
|
+
++ctr;
|
|
29
|
+
}
|
|
30
|
+
return ctr <= buff_size ? (int)ctr : -1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
int xmr_read_varint(uint8_t * buff, size_t buff_size, uint64_t *val) {
|
|
34
|
+
unsigned read = 0;
|
|
35
|
+
int finished_ok = 0;
|
|
36
|
+
*val = 0;
|
|
37
|
+
|
|
38
|
+
for (int shift = 0; read < buff_size; shift += 7, ++read) {
|
|
39
|
+
uint8_t byte = buff[read];
|
|
40
|
+
if (byte == 0 && shift != 0) {
|
|
41
|
+
return -1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
*val |= (uint64_t)(byte & 0x7f) << shift;
|
|
45
|
+
|
|
46
|
+
/* If there is no next */
|
|
47
|
+
if ((byte & 0x80) == 0) {
|
|
48
|
+
finished_ok = 1;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return finished_ok ? (int)read + 1 : -2;
|
|
53
|
+
}
|
|
54
|
+
|