@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.
Files changed (223) hide show
  1. package/README.md +9 -1
  2. package/ReactNativeLiteCard.podspec +25 -2
  3. package/android/build.gradle +7 -0
  4. package/android/src/main/cpp/CMakeLists.txt +6 -2
  5. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
  6. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +5 -1
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +21 -7
  9. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
  10. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +4 -1
  11. package/ios/Classes/OKNFCBridge.mm +1 -1
  12. package/ios/Classes/OKNFTLite/OKLiteV1.m +6 -3
  13. package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
  14. package/package.json +2 -2
  15. package/sdk/CMakeLists.txt +120 -0
  16. package/sdk/README.md +175 -0
  17. package/sdk/android/implJni.cpp +177 -0
  18. package/sdk/deps/jsoncpp/LICENSE +55 -0
  19. package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
  20. package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
  21. package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
  22. package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
  23. package/sdk/deps/jsoncpp/include/json/config.h +187 -0
  24. package/sdk/deps/jsoncpp/include/json/features.h +61 -0
  25. package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
  26. package/sdk/deps/jsoncpp/include/json/json.h +15 -0
  27. package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
  28. package/sdk/deps/jsoncpp/include/json/value.h +888 -0
  29. package/sdk/deps/jsoncpp/include/json/version.h +20 -0
  30. package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
  31. package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
  32. package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
  33. package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
  34. package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
  35. package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
  36. package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
  37. package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
  38. package/sdk/deps/trezor-crypto/AUTHORS +2 -0
  39. package/sdk/deps/trezor-crypto/LICENSE +22 -0
  40. package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
  41. package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
  42. package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
  43. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
  44. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
  45. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
  46. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
  47. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
  48. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
  49. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
  50. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
  51. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
  52. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
  53. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
  54. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
  55. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
  56. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
  57. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
  58. package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
  59. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
  60. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
  61. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
  62. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
  63. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
  64. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
  65. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
  66. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
  67. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
  68. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
  69. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
  70. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
  71. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
  72. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
  73. package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
  74. package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
  75. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
  76. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
  77. package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
  78. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
  79. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
  80. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
  81. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
  82. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
  83. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
  84. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
  85. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
  86. package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
  87. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
  88. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
  89. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
  90. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
  91. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
  92. package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
  93. package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
  94. package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
  95. package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
  96. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
  97. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
  98. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
  99. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
  100. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
  101. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
  102. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
  103. package/sdk/deps/trezor-crypto/src/address.c +87 -0
  104. package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
  105. package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
  106. package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
  107. package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
  108. package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
  109. package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
  110. package/sdk/deps/trezor-crypto/src/base32.c +233 -0
  111. package/sdk/deps/trezor-crypto/src/base58.c +298 -0
  112. package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
  113. package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
  114. package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
  115. package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
  116. package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
  117. package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
  118. package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
  119. package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
  120. package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
  121. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
  122. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
  123. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
  124. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
  125. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
  126. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
  127. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
  128. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
  129. package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
  130. package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
  131. package/sdk/deps/trezor-crypto/src/curves.c +48 -0
  132. package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
  133. package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
  134. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
  135. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
  136. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
  137. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
  138. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
  139. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
  140. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
  141. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
  142. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
  143. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
  144. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
  145. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
  146. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
  147. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
  148. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
  149. package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
  150. package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
  151. package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
  152. package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
  153. package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
  154. package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
  155. package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
  156. package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
  157. package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
  158. package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
  159. package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
  160. package/sdk/deps/trezor-crypto/src/nano.c +151 -0
  161. package/sdk/deps/trezor-crypto/src/nem.c +569 -0
  162. package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
  163. package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
  164. package/sdk/deps/trezor-crypto/src/options.h +43 -0
  165. package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
  166. package/sdk/deps/trezor-crypto/src/rand.c +51 -0
  167. package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
  168. package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
  169. package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
  170. package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
  171. package/sdk/deps/trezor-crypto/src/script.c +64 -0
  172. package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
  173. package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
  174. package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
  175. package/sdk/deps/trezor-crypto/src/setup.py +38 -0
  176. package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
  177. package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
  178. package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
  179. package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
  180. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
  181. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
  182. package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
  183. package/sdk/gpc/include/scp03/scp03.hpp +80 -0
  184. package/sdk/gpc/include/scp11/scp11.hpp +492 -0
  185. package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
  186. package/sdk/gpc/include/tag.h +68 -0
  187. package/sdk/gpc/include/tlv.hpp +94 -0
  188. package/sdk/gpc/include/tlvparse.hpp +24 -0
  189. package/sdk/gpc/src/scp03.cpp +243 -0
  190. package/sdk/gpc/src/scp11.cpp +798 -0
  191. package/sdk/gpc/src/scp11c.cpp +136 -0
  192. package/sdk/gpc/src/tlv.cpp +159 -0
  193. package/sdk/gpc/src/tlvparse.c +174 -0
  194. package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
  195. package/sdk/include/GPChannelSDKCore.h +10 -0
  196. package/sdk/include/context/BaseContext.h +56 -0
  197. package/sdk/include/device/ApduBuilder.hpp +72 -0
  198. package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
  199. package/sdk/include/utility/Apdu.hpp +172 -0
  200. package/sdk/include/utility/Debug.hpp +3 -0
  201. package/sdk/include/utility/util.h +104 -0
  202. package/sdk/src/ApduBuilder.cpp +332 -0
  203. package/sdk/src/GPChannelSDK.cpp +252 -0
  204. package/sdk/src/random.c +25 -0
  205. package/sdk/src/version.c +2 -0
  206. package/sdk/tests/android/GPChannelProxy.java +55 -0
  207. package/sdk/tests/android_verify.py +121 -0
  208. package/sdk/tests/safety.cpp +603 -0
  209. package/sdk/tests/verify.py +267 -0
  210. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  211. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  212. package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
  213. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  214. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  215. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  216. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  217. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
  218. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  219. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
  220. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  221. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  222. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  223. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
@@ -0,0 +1,852 @@
1
+ /**
2
+ * Copyright (c) 2013-2016 Tomas Dzetkulic
3
+ * Copyright (c) 2013-2016 Pavol Rusnak
4
+ * Copyright (c) 2015-2016 Jochen Hoenicke
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining
7
+ * a copy of this software and associated documentation files (the "Software"),
8
+ * to deal in the Software without restriction, including without limitation
9
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
+ * and/or sell copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included
14
+ * in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
20
+ * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ * OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ #include <string.h>
26
+ #include <stdbool.h>
27
+
28
+ #include "options.h"
29
+
30
+ #include <TrezorCrypto/address.h>
31
+ #include <TrezorCrypto/aes.h>
32
+ #include <TrezorCrypto/base58.h>
33
+ #include <TrezorCrypto/bignum.h>
34
+ #include <TrezorCrypto/bip32.h>
35
+ #include <TrezorCrypto/curves.h>
36
+ #include <TrezorCrypto/ecdsa.h>
37
+ #include <TrezorCrypto/ed25519-donna/ed25519-blake2b.h>
38
+ #include <TrezorCrypto/ed25519-donna/ed25519-keccak.h>
39
+ #include <TrezorCrypto/ed25519-donna/ed25519-sha3.h>
40
+ #include <TrezorCrypto/ed25519.h>
41
+ #include <TrezorCrypto/hmac.h>
42
+ #include <TrezorCrypto/memzero.h>
43
+ #include <TrezorCrypto/nem.h>
44
+ #include <TrezorCrypto/nist256p1.h>
45
+ #include <TrezorCrypto/pbkdf2.h>
46
+ #include <TrezorCrypto/secp256k1.h>
47
+ #include <TrezorCrypto/sha2.h>
48
+ #include <TrezorCrypto/sha3.h>
49
+ #include <TrezorCrypto/sodium/keypair.h>
50
+
51
+ const curve_info ed25519_info = {
52
+ .bip32_name = "ed25519 seed",
53
+ .params = NULL,
54
+ .hasher_base58 = HASHER_SHA2D,
55
+ .hasher_sign = HASHER_SHA2D,
56
+ .hasher_pubkey = HASHER_SHA2_RIPEMD,
57
+ .hasher_script = HASHER_SHA2,
58
+ };
59
+
60
+ const curve_info ed25519_cardano_info = {
61
+ .bip32_name = "ed25519 cardano seed",
62
+ .params = NULL,
63
+ .hasher_base58 = HASHER_SHA2D,
64
+ .hasher_sign = HASHER_SHA2D,
65
+ .hasher_pubkey = HASHER_SHA2_RIPEMD,
66
+ .hasher_script = HASHER_SHA2,
67
+ };
68
+
69
+ const curve_info ed25519_blake2b_nano_info = {
70
+ .bip32_name = "ed25519 seed",
71
+ .params = NULL,
72
+ .hasher_base58 = HASHER_SHA2D,
73
+ .hasher_sign = HASHER_SHA2D,
74
+ .hasher_pubkey = HASHER_SHA2_RIPEMD,
75
+ .hasher_script = HASHER_SHA2,
76
+ };
77
+
78
+ const curve_info ed25519_sha3_info = {
79
+ .bip32_name = "ed25519-sha3 seed",
80
+ .params = NULL,
81
+ .hasher_base58 = HASHER_SHA2D,
82
+ .hasher_sign = HASHER_SHA2D,
83
+ .hasher_pubkey = HASHER_SHA2_RIPEMD,
84
+ .hasher_script = HASHER_SHA2,
85
+ };
86
+
87
+ const curve_info ed25519_keccak_info = {
88
+ .bip32_name = "ed25519-keccak seed",
89
+ .params = NULL,
90
+ .hasher_base58 = HASHER_SHA2D,
91
+ .hasher_sign = HASHER_SHA2D,
92
+ .hasher_pubkey = HASHER_SHA2_RIPEMD,
93
+ .hasher_script = HASHER_SHA2,
94
+ };
95
+
96
+ const curve_info curve25519_info = {
97
+ .bip32_name = "curve25519 seed",
98
+ .params = NULL,
99
+ .hasher_base58 = HASHER_SHA2D,
100
+ .hasher_sign = HASHER_SHA2D,
101
+ .hasher_pubkey = HASHER_SHA2_RIPEMD,
102
+ .hasher_script = HASHER_SHA2,
103
+ };
104
+
105
+ int hdnode_from_xpub(uint32_t depth, uint32_t child_num, const uint8_t *chain_code, const uint8_t *public_key, const char* curve, HDNode *out)
106
+ {
107
+ const curve_info *info = get_curve_by_name(curve);
108
+ if (info == 0) {
109
+ return 0;
110
+ }
111
+ if (public_key[0] != 0x02 && public_key[0] != 0x03) { // invalid pubkey
112
+ return 0;
113
+ }
114
+ out->curve = info;
115
+ out->depth = depth;
116
+ out->child_num = child_num;
117
+ memcpy(out->chain_code, chain_code, 32);
118
+ memzero(out->private_key, 32);
119
+ memzero(out->private_key_extension,32);
120
+ memcpy(out->public_key, public_key, 33);
121
+ return 1;
122
+ }
123
+
124
+ int hdnode_from_xprv(uint32_t depth, uint32_t child_num, const uint8_t *chain_code, const uint8_t *private_key, const char* curve, HDNode *out)
125
+ {
126
+ bool failed = false;
127
+ const curve_info *info = get_curve_by_name(curve);
128
+ if (info == 0) {
129
+ failed = true;
130
+ } else if (info->params) {
131
+ bignum256 a;
132
+ bn_read_be(private_key, &a);
133
+ if (bn_is_zero(&a)) { // == 0
134
+ failed = true;
135
+ } else {
136
+ if (!bn_is_less(&a, &info->params->order)) { // >= order
137
+ failed = true;
138
+ }
139
+ }
140
+ memzero(&a, sizeof(a));
141
+ }
142
+
143
+ if (failed) {
144
+ return 0;
145
+ }
146
+
147
+ out->curve = info;
148
+ out->depth = depth;
149
+ out->child_num = child_num;
150
+ memcpy(out->chain_code, chain_code, 32);
151
+ memcpy(out->private_key, private_key, 32);
152
+ memzero(out->public_key, sizeof(out->public_key));
153
+ memzero(out->private_key_extension, sizeof(out->private_key_extension));
154
+ return 1;
155
+ }
156
+
157
+ int hdnode_from_seed(const uint8_t *seed, int seed_len, const char* curve, HDNode *out)
158
+ {
159
+ CONFIDENTIAL uint8_t I[32 + 32];
160
+ memset(out, 0, sizeof(HDNode));
161
+ out->depth = 0;
162
+ out->child_num = 0;
163
+ out->curve = get_curve_by_name(curve);
164
+ if (out->curve == 0) {
165
+ return 0;
166
+ }
167
+ CONFIDENTIAL HMAC_SHA512_CTX ctx;
168
+ hmac_sha512_Init(&ctx, (const uint8_t*) out->curve->bip32_name, strlen(out->curve->bip32_name));
169
+ hmac_sha512_Update(&ctx, seed, seed_len);
170
+ hmac_sha512_Final(&ctx, I);
171
+
172
+ if (out->curve->params) {
173
+ bignum256 a;
174
+ while (true) {
175
+ bn_read_be(I, &a);
176
+ if (!bn_is_zero(&a) // != 0
177
+ && bn_is_less(&a, &out->curve->params->order)) { // < order
178
+ break;
179
+ }
180
+ hmac_sha512_Init(&ctx, (const uint8_t*) out->curve->bip32_name, strlen(out->curve->bip32_name));
181
+ hmac_sha512_Update(&ctx, I, sizeof(I));
182
+ hmac_sha512_Final(&ctx, I);
183
+ }
184
+ memzero(&a, sizeof(a));
185
+ }
186
+ memcpy(out->private_key, I, 32);
187
+ memcpy(out->chain_code, I + 32, 32);
188
+ memzero(out->public_key, sizeof(out->public_key));
189
+ memzero(I, sizeof(I));
190
+ return 1;
191
+ }
192
+
193
+ uint32_t hdnode_fingerprint(HDNode *node)
194
+ {
195
+ uint8_t digest[32];
196
+ uint32_t fingerprint;
197
+
198
+ hdnode_fill_public_key(node);
199
+ hasher_Raw(node->curve->hasher_pubkey, node->public_key, 33, digest);
200
+ fingerprint = ((uint32_t) digest[0] << 24) + (digest[1] << 16) + (digest[2] << 8) + digest[3];
201
+ memzero(digest, sizeof(digest));
202
+ return fingerprint;
203
+ }
204
+
205
+ int hdnode_private_ckd(HDNode *inout, uint32_t i)
206
+ {
207
+ CONFIDENTIAL uint8_t data[1 + 32 + 4];
208
+ CONFIDENTIAL uint8_t I[32 + 32];
209
+ CONFIDENTIAL bignum256 a, b;
210
+
211
+ if (i & 0x80000000) { // private derivation
212
+ data[0] = 0;
213
+ memcpy(data + 1, inout->private_key, 32);
214
+ } else { // public derivation
215
+ if (!inout->curve->params) {
216
+ return 0;
217
+ }
218
+ hdnode_fill_public_key(inout);
219
+ memcpy(data, inout->public_key, 33);
220
+ }
221
+ write_be(data + 33, i);
222
+
223
+ bn_read_be(inout->private_key, &a);
224
+
225
+ CONFIDENTIAL HMAC_SHA512_CTX ctx;
226
+ hmac_sha512_Init(&ctx, inout->chain_code, 32);
227
+ hmac_sha512_Update(&ctx, data, sizeof(data));
228
+ hmac_sha512_Final(&ctx, I);
229
+
230
+ if (inout->curve->params) {
231
+ while (true) {
232
+ bool failed = false;
233
+ bn_read_be(I, &b);
234
+ if (!bn_is_less(&b, &inout->curve->params->order)) { // >= order
235
+ failed = true;
236
+ } else {
237
+ bn_add(&b, &a);
238
+ bn_mod(&b, &inout->curve->params->order);
239
+ if (bn_is_zero(&b)) {
240
+ failed = true;
241
+ }
242
+ }
243
+
244
+ if (!failed) {
245
+ bn_write_be(&b, inout->private_key);
246
+ break;
247
+ }
248
+
249
+ data[0] = 1;
250
+ memcpy(data + 1, I + 32, 32);
251
+ hmac_sha512_Init(&ctx, inout->chain_code, 32);
252
+ hmac_sha512_Update(&ctx, data, sizeof(data));
253
+ hmac_sha512_Final(&ctx, I);
254
+ }
255
+ } else {
256
+ memcpy(inout->private_key, I, 32);
257
+ }
258
+
259
+ memcpy(inout->chain_code, I + 32, 32);
260
+ inout->depth++;
261
+ inout->child_num = i;
262
+ memzero(inout->public_key, sizeof(inout->public_key));
263
+
264
+ // making sure to wipe our memory
265
+ memzero(&a, sizeof(a));
266
+ memzero(&b, sizeof(b));
267
+ memzero(I, sizeof(I));
268
+ memzero(data, sizeof(data));
269
+ return 1;
270
+ }
271
+
272
+ static void scalar_multiply8(const uint8_t *src, int bytes, uint8_t *dst)
273
+ {
274
+ uint8_t prev_acc = 0;
275
+ for (int i = 0; i < bytes; i++) {
276
+ dst[i] = (src[i] << 3) + (prev_acc & 0x7);
277
+ prev_acc = src[i] >> 5;
278
+ }
279
+ dst[bytes] = src[bytes - 1] >> 5;
280
+ }
281
+
282
+ static void scalar_add_256bits(const uint8_t *src1, const uint8_t *src2, uint8_t *dst)
283
+ {
284
+ uint16_t r = 0;
285
+ for (int i = 0; i < 32; i++) {
286
+ r = r + (uint16_t)src1[i] + (uint16_t)src2[i];
287
+ dst[i] = r & 0xff;
288
+ r >>= 8;
289
+ }
290
+ }
291
+
292
+ int hdnode_private_ckd_cardano(HDNode *inout, uint32_t index)
293
+ {
294
+ // checks for hardened/non-hardened derivation, keysize 32 means we are dealing with public key and thus non-h, keysize 64 is for private key
295
+ int keysize = 32;
296
+ if (index & 0x80000000) {
297
+ keysize = 64;
298
+ }
299
+
300
+ CONFIDENTIAL uint8_t data[1 + 64 + 4];
301
+ CONFIDENTIAL uint8_t z[32 + 32];
302
+ CONFIDENTIAL uint8_t priv_key[64];
303
+ CONFIDENTIAL uint8_t res_key[64];
304
+
305
+ write_le(data + keysize + 1, index);
306
+
307
+ memcpy(priv_key, inout->private_key, 32);
308
+ memcpy(priv_key + 32, inout->private_key_extension, 32);
309
+
310
+ if (keysize == 64) { // private derivation
311
+ data[0] = 0;
312
+ memcpy(data + 1, inout->private_key, 32);
313
+ memcpy(data + 1 + 32, inout->private_key_extension, 32);
314
+ } else { // public derivation
315
+ hdnode_fill_public_key(inout);
316
+ data[0] = 2;
317
+ memcpy(data + 1, inout->public_key + 1, 32);
318
+ }
319
+
320
+ CONFIDENTIAL HMAC_SHA512_CTX ctx;
321
+ hmac_sha512_Init(&ctx, inout->chain_code, 32);
322
+ hmac_sha512_Update(&ctx, data, 1 + keysize + 4);
323
+ hmac_sha512_Final(&ctx, z);
324
+
325
+ CONFIDENTIAL uint8_t zl8[32];
326
+ memset(zl8, 0, 32);
327
+
328
+ /* get 8 * Zl */
329
+ scalar_multiply8(z, 28, zl8);
330
+ /* Kl = 8*Zl + parent(K)l */
331
+ scalar_add_256bits(zl8, priv_key, res_key);
332
+
333
+ /* Kr = Zr + parent(K)r */
334
+ scalar_add_256bits(z + 32, priv_key + 32, res_key + 32);
335
+
336
+ memcpy(inout->private_key, res_key, 32);
337
+ memcpy(inout->private_key_extension, res_key + 32, 32);
338
+
339
+ if (keysize == 64) {
340
+ data[0] = 1;
341
+ } else {
342
+ data[0] = 3;
343
+ }
344
+ hmac_sha512_Init(&ctx, inout->chain_code, 32);
345
+ hmac_sha512_Update(&ctx, data, 1 + keysize + 4);
346
+ hmac_sha512_Final(&ctx, z);
347
+
348
+ memcpy(inout->chain_code, z + 32, 32);
349
+ inout->depth++;
350
+ inout->child_num = index;
351
+ memzero(inout->public_key, sizeof(inout->public_key));
352
+
353
+ // making sure to wipe our memory
354
+ memzero(z, sizeof(z));
355
+ memzero(data, sizeof(data));
356
+ memzero(priv_key, sizeof(priv_key));
357
+ memzero(res_key, sizeof(res_key));
358
+ return 1;
359
+ }
360
+
361
+ int hdnode_from_seed_cardano(const uint8_t *pass, int pass_len, const uint8_t *seed, int seed_len, HDNode *out) {
362
+ CONFIDENTIAL uint8_t secret[96];
363
+ pbkdf2_hmac_sha512(pass, pass_len, seed, seed_len, 4096, secret, 96);
364
+
365
+ secret[0] &= 248;
366
+ secret[31] &= 31;
367
+ secret[31] |= 64;
368
+
369
+ memset(out, 0, sizeof(HDNode));
370
+ out->depth = 0;
371
+ out->child_num = 0;
372
+ out->curve = get_curve_by_name(ED25519_CARDANO_NAME);
373
+
374
+ memcpy(out->private_key, secret, 32);
375
+ memcpy(out->private_key_extension, secret + 32, 32);
376
+
377
+ out->public_key[0] = 0;
378
+ hdnode_fill_public_key(out);
379
+
380
+ memcpy(out->chain_code, secret + 64, 32);
381
+ memzero(secret, sizeof(secret));
382
+
383
+ return 1;
384
+ }
385
+
386
+ int hdnode_public_ckd_cp(const ecdsa_curve *curve, const curve_point *parent, const uint8_t *parent_chain_code, uint32_t i, curve_point *child, uint8_t *child_chain_code) {
387
+ uint8_t data[1 + 32 + 4];
388
+ uint8_t I[32 + 32];
389
+ bignum256 c;
390
+
391
+ if (i & 0x80000000) { // private derivation
392
+ return 0;
393
+ }
394
+
395
+ data[0] = 0x02 | (parent->y.val[0] & 0x01);
396
+ bn_write_be(&parent->x, data + 1);
397
+ write_be(data + 33, i);
398
+
399
+ while (true) {
400
+ hmac_sha512(parent_chain_code, 32, data, sizeof(data), I);
401
+ bn_read_be(I, &c);
402
+ if (bn_is_less(&c, &curve->order)) { // < order
403
+ scalar_multiply(curve, &c, child); // b = c * G
404
+ point_add(curve, parent, child); // b = a + b
405
+ if (!point_is_infinity(child)) {
406
+ if (child_chain_code) {
407
+ memcpy(child_chain_code, I + 32, 32);
408
+ }
409
+
410
+ // Wipe all stack data.
411
+ memzero(data, sizeof(data));
412
+ memzero(I, sizeof(I));
413
+ memzero(&c, sizeof(c));
414
+ return 1;
415
+ }
416
+ }
417
+
418
+ data[0] = 1;
419
+ memcpy(data + 1, I + 32, 32);
420
+ }
421
+ }
422
+
423
+ int hdnode_public_ckd(HDNode *inout, uint32_t i)
424
+ {
425
+ curve_point parent, child;
426
+
427
+ if (!ecdsa_read_pubkey(inout->curve->params, inout->public_key, &parent)) {
428
+ return 0;
429
+ }
430
+ if (!hdnode_public_ckd_cp(inout->curve->params, &parent, inout->chain_code, i, &child, inout->chain_code)) {
431
+ return 0;
432
+ }
433
+ memzero(inout->private_key, 32);
434
+ inout->depth++;
435
+ inout->child_num = i;
436
+ inout->public_key[0] = 0x02 | (child.y.val[0] & 0x01);
437
+ bn_write_be(&child.x, inout->public_key + 1);
438
+
439
+ // Wipe all stack data.
440
+ memzero(&parent, sizeof(parent));
441
+ memzero(&child, sizeof(child));
442
+
443
+ return 1;
444
+ }
445
+
446
+ void hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *chain_code, uint32_t i, uint32_t version, HasherType hasher_pubkey, HasherType hasher_base58, char *addr, int addrsize, int addrformat)
447
+ {
448
+ uint8_t child_pubkey[33];
449
+ curve_point b;
450
+
451
+ hdnode_public_ckd_cp(&secp256k1, pub, chain_code, i, &b, NULL);
452
+ child_pubkey[0] = 0x02 | (b.y.val[0] & 0x01);
453
+ bn_write_be(&b.x, child_pubkey + 1);
454
+
455
+ switch (addrformat) {
456
+ case 1: // Segwit-in-P2SH
457
+ ecdsa_get_address_segwit_p2sh(child_pubkey, version, hasher_pubkey, hasher_base58, addr, addrsize);
458
+ break;
459
+ default: // normal address
460
+ ecdsa_get_address(child_pubkey, version, hasher_pubkey, hasher_base58, addr, addrsize);
461
+ break;
462
+ }
463
+ }
464
+
465
+ void hdnode_get_address_raw(HDNode *node, uint32_t version, uint8_t *addr_raw)
466
+ {
467
+ hdnode_fill_public_key(node);
468
+ ecdsa_get_address_raw(node->public_key, version, node->curve->hasher_pubkey, addr_raw);
469
+ }
470
+
471
+ void hdnode_get_address(HDNode *node, uint32_t version, char *addr, int addrsize)
472
+ {
473
+ hdnode_fill_public_key(node);
474
+ ecdsa_get_address(node->public_key, version, node->curve->hasher_pubkey, node->curve->hasher_base58, addr, addrsize);
475
+ }
476
+
477
+ void hdnode_fill_public_key(HDNode *node)
478
+ {
479
+ if (node->public_key[0] != 0)
480
+ return;
481
+
482
+ #if USE_BIP32_25519_CURVES
483
+ if (node->curve->params) {
484
+ ecdsa_get_public_key33(node->curve->params, node->private_key, node->public_key);
485
+ } else {
486
+ node->public_key[0] = 1;
487
+ if (node->curve == &ed25519_info) {
488
+ ed25519_publickey(node->private_key, node->public_key + 1);
489
+ } else if (node->curve == &ed25519_blake2b_nano_info) {
490
+ ed25519_publickey_blake2b(node->private_key, node->public_key + 1);
491
+ } else if (node->curve == &ed25519_sha3_info) {
492
+ ed25519_publickey_sha3(node->private_key, node->public_key + 1);
493
+ } else if (node->curve == &ed25519_keccak_info) {
494
+ ed25519_publickey_keccak(node->private_key, node->public_key + 1);
495
+ } else if (node->curve == &curve25519_info) {
496
+ curve25519_scalarmult_basepoint(node->public_key + 1, node->private_key);
497
+ } else if (node->curve == &ed25519_cardano_info) {
498
+ ed25519_publickey_ext(node->private_key, node->private_key_extension, node->public_key + 1);
499
+ }
500
+ }
501
+ #else
502
+
503
+ ecdsa_get_public_key33(node->curve->params, node->private_key, node->public_key);
504
+ #endif
505
+ }
506
+
507
+ // JuBiter-defined
508
+ int pubkey_get_ethereum_pubkeyhash(const uint8_t *uncompressed_pubkey,
509
+ size_t uncompressed_pubkey_len,
510
+ uint8_t *pubkeyhash)
511
+ {
512
+ uint8_t buf[65];
513
+ SHA3_CTX ctx;
514
+
515
+ /* get uncompressed public key */
516
+ // JuBiter-modified
517
+ // ecdsa_get_public_key65(node->curve->params, node->private_key, buf);
518
+ memcpy(buf, uncompressed_pubkey, uncompressed_pubkey_len);
519
+
520
+ /* compute sha3 of x and y coordinate without 04 prefix */
521
+ sha3_256_Init(&ctx);
522
+ sha3_Update(&ctx, buf + 1, 64);
523
+ keccak_Final(&ctx, buf);
524
+
525
+ /* result are the least significant 160 bits */
526
+ memcpy(pubkeyhash, buf + 12, 20);
527
+
528
+ return 1;
529
+ }
530
+
531
+ // JuBiter-modified
532
+ int hdnode_get_ethereum_pubkeyhash(const HDNode *node, uint8_t *pubkeyhash)
533
+ {
534
+ uint8_t buf[65];
535
+ // SHA3_CTX ctx;
536
+
537
+ /* get uncompressed public key */
538
+ ecdsa_get_public_key65(node->curve->params, node->private_key, buf);
539
+
540
+ // /* compute sha3 of x and y coordinate without 04 prefix */
541
+ // sha3_256_Init(&ctx);
542
+ // sha3_Update(&ctx, buf + 1, 64);
543
+ // keccak_Final(&ctx, buf);
544
+ //
545
+ // /* result are the least significant 160 bits */
546
+ // memcpy(pubkeyhash, buf + 12, 20);
547
+ //
548
+ // return 1;
549
+ return pubkey_get_ethereum_pubkeyhash(buf, 65, pubkeyhash);
550
+ }
551
+
552
+ // JuBiter-defined
553
+ int hdnode_get_tron_pubkeyhash(const HDNode *node, uint8_t *pubkeyhash) {
554
+ uint8_t buf[65] = {
555
+ 0,
556
+ };
557
+ SHA3_CTX ctx;// = {0};
558
+
559
+ /* get uncompressed public key */
560
+ if (1 != ecdsa_uncompress_pubkey(&secp256k1, node->public_key, buf)) {
561
+ return 0;
562
+ }
563
+
564
+ /* compute sha3 of x and y coordinate without 04 prefix */
565
+ keccak_256_Init(&ctx);
566
+ keccak_Update(&ctx, buf + 1, 64);
567
+ keccak_Final(&ctx, buf);
568
+
569
+ /* result are the least significant 160 bits */
570
+ pubkeyhash[0] = 0x41;
571
+ memcpy(pubkeyhash + 1, buf + 12, 20);
572
+
573
+ return 1;
574
+ }
575
+
576
+ int hdnode_get_nem_address(HDNode *node, uint8_t version, char *address) {
577
+ if (node->curve != &ed25519_keccak_info) {
578
+ return 0;
579
+ }
580
+
581
+ hdnode_fill_public_key(node);
582
+ return nem_get_address(&node->public_key[1], version, address);
583
+ }
584
+
585
+ int hdnode_get_nem_shared_key(const HDNode *node, const ed25519_public_key peer_public_key, const uint8_t *salt, ed25519_public_key mul, uint8_t *shared_key) {
586
+ if (node->curve != &ed25519_keccak_info) {
587
+ return 0;
588
+ }
589
+
590
+ // sizeof(ed25519_public_key) == SHA3_256_DIGEST_LENGTH
591
+ if (mul == NULL) mul = shared_key;
592
+
593
+ if (ed25519_scalarmult_keccak(mul, node->private_key, peer_public_key)) {
594
+ return 0;
595
+ }
596
+
597
+ for (size_t i = 0; i < 32; i++) {
598
+ shared_key[i] = mul[i] ^ salt[i];
599
+ }
600
+
601
+ keccak_256(shared_key, 32, shared_key);
602
+ return 1;
603
+ }
604
+
605
+ int hdnode_nem_encrypt(const HDNode *node, const ed25519_public_key public_key, const uint8_t *iv_immut, const uint8_t *salt, const uint8_t *payload, size_t size, uint8_t *buffer) {
606
+ uint8_t last_block[AES_BLOCK_SIZE];
607
+ uint8_t remainder = size % AES_BLOCK_SIZE;
608
+
609
+ // Round down to last whole block
610
+ size -= remainder;
611
+ // Copy old last block
612
+ memcpy(last_block, &payload[size], remainder);
613
+ // Pad new last block with number of missing bytes
614
+ memset(&last_block[remainder], AES_BLOCK_SIZE - remainder, AES_BLOCK_SIZE - remainder);
615
+
616
+ // the IV gets mutated, so we make a copy not to touch the original
617
+ uint8_t iv[AES_BLOCK_SIZE];
618
+ memcpy(iv, iv_immut, AES_BLOCK_SIZE);
619
+
620
+ uint8_t shared_key[SHA3_256_DIGEST_LENGTH];
621
+ if (!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
622
+ return 0;
623
+ }
624
+
625
+ aes_encrypt_ctx ctx;
626
+
627
+ int ret = aes_encrypt_key256(shared_key, &ctx);
628
+ memzero(shared_key, sizeof(shared_key));
629
+
630
+ if (ret != EXIT_SUCCESS) {
631
+ return 0;
632
+ }
633
+
634
+ if (aes_cbc_encrypt(payload, buffer, size, iv, &ctx) != EXIT_SUCCESS) {
635
+ return 0;
636
+ }
637
+
638
+ if (aes_cbc_encrypt(last_block, &buffer[size], sizeof(last_block), iv, &ctx) != EXIT_SUCCESS) {
639
+ return 0;
640
+ }
641
+
642
+ return 1;
643
+ }
644
+
645
+ int hdnode_nem_decrypt(const HDNode *node, const ed25519_public_key public_key, uint8_t *iv, const uint8_t *salt, const uint8_t *payload, size_t size, uint8_t *buffer) {
646
+ uint8_t shared_key[SHA3_256_DIGEST_LENGTH];
647
+
648
+ if (!hdnode_get_nem_shared_key(node, public_key, salt, NULL, shared_key)) {
649
+ return 0;
650
+ }
651
+
652
+ aes_decrypt_ctx ctx;
653
+
654
+ int ret = aes_decrypt_key256(shared_key, &ctx);
655
+ memzero(shared_key, sizeof(shared_key));
656
+
657
+ if (ret != EXIT_SUCCESS) {
658
+ return 0;
659
+ }
660
+
661
+ if (aes_cbc_decrypt(payload, buffer, size, iv, &ctx) != EXIT_SUCCESS) {
662
+ return 0;
663
+ }
664
+
665
+ return 1;
666
+ }
667
+
668
+ // msg is a data to be signed
669
+ // msg_len is the message length
670
+ int hdnode_sign(HDNode *node, const uint8_t *msg, uint32_t msg_len, HasherType hasher_sign, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
671
+ {
672
+ if (node->curve->params) {
673
+ return ecdsa_sign(node->curve->params, hasher_sign, node->private_key, msg, msg_len, sig, pby, is_canonical);
674
+ } else {
675
+ hdnode_fill_public_key(node);
676
+ if (node->curve == &ed25519_info) {
677
+ ed25519_sign(msg, msg_len, node->private_key, node->public_key + 1, sig);
678
+ } else if (node->curve == &ed25519_blake2b_nano_info) {
679
+ ed25519_sign_blake2b(msg, msg_len, node->private_key, node->public_key + 1, sig);
680
+ } else if (node->curve == &ed25519_sha3_info) {
681
+ ed25519_sign_sha3(msg, msg_len, node->private_key, node->public_key + 1, sig);
682
+ } else if (node->curve == &ed25519_keccak_info) {
683
+ ed25519_sign_keccak(msg, msg_len, node->private_key, node->public_key + 1, sig);
684
+ } else if (node->curve == &curve25519_info) {
685
+ uint8_t _ed25519_public_key[32];
686
+ memset(_ed25519_public_key, 0, 32);
687
+ curve25519_pk_to_ed25519(_ed25519_public_key, node->public_key + 1);
688
+ ed25519_sign(msg, msg_len, node->private_key, _ed25519_public_key, sig);
689
+ const uint8_t sign_bit = _ed25519_public_key[31] & 0x80;
690
+ sig[63] = sig[63] & 127;
691
+ sig[63] |= sign_bit;
692
+ }
693
+ return 0;
694
+ }
695
+ }
696
+
697
+ int hdnode_sign_digest(HDNode *node, const uint8_t *digest, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
698
+ {
699
+ if (node->curve->params) {
700
+ return ecdsa_sign_digest(node->curve->params, node->private_key, digest, sig, pby, is_canonical);
701
+ } else {
702
+ return hdnode_sign(node, digest, 32, 0, sig, pby, is_canonical);
703
+ }
704
+ }
705
+
706
+ int hdnode_get_shared_key(const HDNode *node, const uint8_t *peer_public_key, uint8_t *session_key, int *result_size)
707
+ {
708
+ // Use elliptic curve Diffie-Helman to compute shared session key
709
+ if (node->curve->params) {
710
+ if (ecdh_multiply(node->curve->params, node->private_key, peer_public_key, session_key) != 0) {
711
+ return 1;
712
+ }
713
+ *result_size = 65;
714
+ return 0;
715
+ } else if (node->curve == &curve25519_info) {
716
+ session_key[0] = 0x04;
717
+ if (peer_public_key[0] != 0x40) {
718
+ return 1; // Curve25519 public key should start with 0x40 byte.
719
+ }
720
+ curve25519_scalarmult(session_key + 1, node->private_key, peer_public_key + 1);
721
+ *result_size = 33;
722
+ return 0;
723
+ } else {
724
+ *result_size = 0;
725
+ return 1; // ECDH is not supported
726
+ }
727
+ }
728
+
729
+ static int hdnode_serialize(const HDNode *node, uint32_t fingerprint, uint32_t version, char use_public, char *str, int strsize)
730
+ {
731
+ uint8_t node_data[78];
732
+ write_be(node_data, version);
733
+ node_data[4] = node->depth;
734
+ write_be(node_data + 5, fingerprint);
735
+ write_be(node_data + 9, node->child_num);
736
+ memcpy(node_data + 13, node->chain_code, 32);
737
+ if (use_public) {
738
+ memcpy(node_data + 45, node->public_key, 33);
739
+ } else {
740
+ node_data[45] = 0;
741
+ memcpy(node_data + 46, node->private_key, 32);
742
+ }
743
+ int ret = base58_encode_check(node_data, sizeof(node_data), node->curve->hasher_base58, str, strsize);
744
+ memzero(node_data, sizeof(node_data));
745
+ return ret;
746
+ }
747
+
748
+ int hdnode_serialize_public(const HDNode *node, uint32_t fingerprint, uint32_t version, char *str, int strsize)
749
+ {
750
+ return hdnode_serialize(node, fingerprint, version, 1, str, strsize);
751
+ }
752
+
753
+ int hdnode_serialize_private(const HDNode *node, uint32_t fingerprint, uint32_t version, char *str, int strsize)
754
+ {
755
+ return hdnode_serialize(node, fingerprint, version, 0, str, strsize);
756
+ }
757
+
758
+ // check for validity of curve point in case of public data not performed
759
+ int hdnode_deserialize(const char *str, uint32_t version_public, uint32_t version_private, const char *curve, HDNode *node, uint32_t *fingerprint)
760
+ {
761
+ uint8_t node_data[78];
762
+ memset(node, 0, sizeof(HDNode));
763
+ node->curve = get_curve_by_name(curve);
764
+ if (base58_decode_check(str, node->curve->hasher_base58, node_data, sizeof(node_data)) != sizeof(node_data)) {
765
+ return -1;
766
+ }
767
+ uint32_t version = read_be(node_data);
768
+ if (version == version_public) {
769
+ memzero(node->private_key, sizeof(node->private_key));
770
+ memcpy(node->public_key, node_data + 45, 33);
771
+ } else if (version == version_private) { // private node
772
+ if (node_data[45]) { // invalid data
773
+ return -2;
774
+ }
775
+ memcpy(node->private_key, node_data + 46, 32);
776
+ memzero(node->public_key, sizeof(node->public_key));
777
+ } else {
778
+ return -3; // invalid version
779
+ }
780
+ node->depth = node_data[4];
781
+ if (fingerprint) {
782
+ *fingerprint = read_be(node_data + 5);
783
+ }
784
+ node->child_num = read_be(node_data + 9);
785
+ memcpy(node->chain_code, node_data + 13, 32);
786
+ return 0;
787
+ }
788
+
789
+ const curve_info *get_curve_by_name(const char *curve_name) {
790
+ if (curve_name == 0) {
791
+ return 0;
792
+ }
793
+ if (strcmp(curve_name, SECP256K1_NAME) == 0) {
794
+ return &secp256k1_info;
795
+ }
796
+ // JuBiter-defined
797
+ if (strcmp(curve_name, SECP256K1_ETH_NAME) == 0) {
798
+ return &secp256k1_ethereum_info;
799
+ }
800
+ // JuBiter-defined
801
+ if (strcmp(curve_name, SECP256K1_XRP_NAME) == 0) {
802
+ return &secp256k1_ripple_info;
803
+ }
804
+ // JuBiter-defined
805
+ if (strcmp(curve_name, SECP256K1_HCASH_NAME) == 0) {
806
+ return &secp256k1_hcash_info;
807
+ }
808
+ // JuBiter-defined
809
+ if (strcmp(curve_name, SECP256K1_EOS_NAME) == 0) {
810
+ return &secp256k1_eos_info;
811
+ }
812
+ // JuBiter-defined
813
+ if (strcmp(curve_name, SECP256K1_TRX_NAME) == 0) {
814
+ return &secp256k1_trx_info;
815
+ }
816
+ // JuBiter-defined
817
+ if (strcmp(curve_name, SECP256K1_FIL_NAME) == 0) {
818
+ return &secp256k1_fil_info;
819
+ }
820
+ // JuBiter-defined
821
+ if (strcmp(curve_name, SECP256K1_CKB_NAME) == 0) {
822
+ return &secp256k1_ckb_info;
823
+ }
824
+ if (strcmp(curve_name, SECP256K1_DECRED_NAME) == 0) {
825
+ return &secp256k1_decred_info;
826
+ }
827
+ if (strcmp(curve_name, SECP256K1_SMART_NAME) == 0) {
828
+ return &secp256k1_smart_info;
829
+ }
830
+ if (strcmp(curve_name, NIST256P1_NAME) == 0) {
831
+ return &nist256p1_info;
832
+ }
833
+ if (strcmp(curve_name, ED25519_NAME) == 0) {
834
+ return &ed25519_info;
835
+ }
836
+ if (strcmp(curve_name, ED25519_CARDANO_NAME) == 0) {
837
+ return &ed25519_cardano_info;
838
+ }
839
+ if (strcmp(curve_name, ED25519_BLAKE2B_NANO_NAME) == 0) {
840
+ return &ed25519_blake2b_nano_info;
841
+ }
842
+ if (strcmp(curve_name, ED25519_SHA3_NAME) == 0) {
843
+ return &ed25519_sha3_info;
844
+ }
845
+ if (strcmp(curve_name, ED25519_KECCAK_NAME) == 0) {
846
+ return &ed25519_keccak_info;
847
+ }
848
+ if (strcmp(curve_name, CURVE25519_NAME) == 0) {
849
+ return &curve25519_info;
850
+ }
851
+ return 0;
852
+ }