@onekeyfe/react-native-lite-card 6.7.0-5 → 6.7.0-50

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 (237) hide show
  1. package/README.md +134 -43
  2. package/ReactNativeLiteCard.podspec +31 -2
  3. package/android/build.gradle +8 -0
  4. package/android/consumer-rules.pro +4 -0
  5. package/android/src/main/cpp/CMakeLists.txt +6 -2
  6. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +60 -49
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +117 -39
  9. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
  10. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +24 -2
  11. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +46 -19
  12. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
  13. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +38 -1
  14. package/ios/Classes/OKNFCBridge.mm +24 -1
  15. package/ios/Classes/OKNFTLite/OKLiteV1.m +20 -10
  16. package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
  17. package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
  18. package/ios/Classes/OKNFTLite/OKNFCManager.m +110 -18
  19. package/ios/ReactNativeLiteCard.mm +23 -11
  20. package/lib/module/index.js +4 -4
  21. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +4 -4
  22. package/lib/typescript/src/index.d.ts +2 -2
  23. package/package.json +9 -8
  24. package/sdk/CMakeLists.txt +121 -0
  25. package/sdk/README.md +190 -0
  26. package/sdk/android/implJni.cpp +216 -0
  27. package/sdk/deps/jsoncpp/LICENSE +55 -0
  28. package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
  29. package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
  30. package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
  31. package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
  32. package/sdk/deps/jsoncpp/include/json/config.h +187 -0
  33. package/sdk/deps/jsoncpp/include/json/features.h +61 -0
  34. package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
  35. package/sdk/deps/jsoncpp/include/json/json.h +15 -0
  36. package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
  37. package/sdk/deps/jsoncpp/include/json/value.h +888 -0
  38. package/sdk/deps/jsoncpp/include/json/version.h +20 -0
  39. package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
  40. package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
  41. package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
  42. package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
  43. package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
  44. package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
  45. package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
  46. package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
  47. package/sdk/deps/trezor-crypto/AUTHORS +2 -0
  48. package/sdk/deps/trezor-crypto/LICENSE +22 -0
  49. package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
  50. package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
  51. package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
  52. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
  53. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
  54. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
  55. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
  56. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
  57. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
  58. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
  59. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
  60. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
  61. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
  62. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
  63. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
  64. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
  65. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
  66. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
  67. package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
  68. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
  69. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
  70. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
  71. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
  72. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
  73. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
  74. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
  75. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
  76. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
  77. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
  78. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
  79. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
  80. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
  81. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
  82. package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
  83. package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
  84. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
  85. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
  86. package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
  87. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
  88. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
  89. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
  90. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
  91. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
  92. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
  93. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
  94. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
  95. package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
  96. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
  97. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
  98. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
  99. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
  100. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
  101. package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
  102. package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
  103. package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
  104. package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
  105. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
  106. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
  107. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
  108. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
  109. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
  110. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
  111. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
  112. package/sdk/deps/trezor-crypto/src/address.c +87 -0
  113. package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
  114. package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
  115. package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
  116. package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
  117. package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
  118. package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
  119. package/sdk/deps/trezor-crypto/src/base32.c +233 -0
  120. package/sdk/deps/trezor-crypto/src/base58.c +298 -0
  121. package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
  122. package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
  123. package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
  124. package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
  125. package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
  126. package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
  127. package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
  128. package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
  129. package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
  130. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
  131. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
  132. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
  133. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
  134. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
  135. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
  136. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
  137. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
  138. package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
  139. package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
  140. package/sdk/deps/trezor-crypto/src/curves.c +48 -0
  141. package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
  142. package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
  143. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
  144. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
  145. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
  146. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
  147. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
  148. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
  149. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
  150. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
  151. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
  152. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
  153. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
  154. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
  155. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
  156. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
  157. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
  158. package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
  159. package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
  160. package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
  161. package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
  162. package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
  163. package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
  164. package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
  165. package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
  166. package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
  167. package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
  168. package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
  169. package/sdk/deps/trezor-crypto/src/nano.c +151 -0
  170. package/sdk/deps/trezor-crypto/src/nem.c +569 -0
  171. package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
  172. package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
  173. package/sdk/deps/trezor-crypto/src/options.h +43 -0
  174. package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
  175. package/sdk/deps/trezor-crypto/src/rand.c +51 -0
  176. package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
  177. package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
  178. package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
  179. package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
  180. package/sdk/deps/trezor-crypto/src/script.c +64 -0
  181. package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
  182. package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
  183. package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
  184. package/sdk/deps/trezor-crypto/src/setup.py +38 -0
  185. package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
  186. package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
  187. package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
  188. package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
  189. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
  190. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
  191. package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
  192. package/sdk/gpc/include/scp03/scp03.hpp +80 -0
  193. package/sdk/gpc/include/scp11/scp11.hpp +492 -0
  194. package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
  195. package/sdk/gpc/include/tag.h +68 -0
  196. package/sdk/gpc/include/tlv.hpp +94 -0
  197. package/sdk/gpc/include/tlvparse.hpp +24 -0
  198. package/sdk/gpc/src/scp03.cpp +243 -0
  199. package/sdk/gpc/src/scp11.cpp +798 -0
  200. package/sdk/gpc/src/scp11c.cpp +136 -0
  201. package/sdk/gpc/src/tlv.cpp +159 -0
  202. package/sdk/gpc/src/tlvparse.c +174 -0
  203. package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
  204. package/sdk/include/GPChannelSDKCore.h +10 -0
  205. package/sdk/include/context/BaseContext.h +56 -0
  206. package/sdk/include/device/ApduBuilder.hpp +72 -0
  207. package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
  208. package/sdk/include/utility/Apdu.hpp +172 -0
  209. package/sdk/include/utility/Debug.hpp +3 -0
  210. package/sdk/include/utility/util.h +104 -0
  211. package/sdk/scripts/generate-source-id.rb +22 -0
  212. package/sdk/source-id-manifest.txt +19 -0
  213. package/sdk/source-id.cmake +31 -0
  214. package/sdk/src/ApduBuilder.cpp +332 -0
  215. package/sdk/src/GPChannelSDK.cpp +258 -0
  216. package/sdk/src/random.c +25 -0
  217. package/sdk/src/version.c +3 -0
  218. package/sdk/tests/android/GPChannelProxy.java +55 -0
  219. package/sdk/tests/android_verify.py +124 -0
  220. package/sdk/tests/safety.cpp +757 -0
  221. package/sdk/tests/verify.py +284 -0
  222. package/src/NativeReactNativeLiteCard.ts +4 -2
  223. package/src/index.tsx +14 -3
  224. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  225. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  226. package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
  227. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  228. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  229. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  230. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  231. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
  232. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  233. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
  234. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  235. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  236. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  237. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Copyright (c) 2017 Saleem Rashid
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, E1PRESS
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/base32.h>
24
+
25
+ #include <string.h>
26
+
27
+ const char *BASE32_ALPHABET_RFC4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789";
28
+
29
+ static inline void base32_5to8(const uint8_t *in, uint8_t length, uint8_t *out);
30
+ static inline bool base32_8to5(const uint8_t *in, uint8_t length, uint8_t *out, const char *alphabet);
31
+ static inline void base32_8to5_raw(const uint8_t *in, uint8_t length, uint8_t *out);
32
+
33
+ static inline int base32_encode_character(uint8_t decoded, const char *alphabet);
34
+ static inline int base32_decode_character(char encoded, const char *alphabet);
35
+
36
+ char *base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen, const char *alphabet) {
37
+ size_t length = base32_encoded_length(inlen);
38
+ if (outlen <= length) {
39
+ return NULL;
40
+ }
41
+
42
+ base32_encode_unsafe(in, inlen, (uint8_t *) out);
43
+
44
+ for (size_t i = 0; i < length; i++) {
45
+ int ret = base32_encode_character(out[i], alphabet);
46
+
47
+ if (ret == -1) {
48
+ return false;
49
+ } else {
50
+ out[i] = ret;
51
+ }
52
+ }
53
+
54
+ out[length] = '\0';
55
+ return &out[length];
56
+ }
57
+
58
+ uint8_t *base32_decode(const char *in, size_t inlen, uint8_t *out, size_t outlen, const char *alphabet) {
59
+ size_t length = base32_decoded_length(inlen);
60
+ if (outlen < length) {
61
+ return NULL;
62
+ }
63
+
64
+ if (!base32_decode_unsafe((uint8_t *) in, inlen, (uint8_t *) out, alphabet)) {
65
+ return NULL;
66
+ }
67
+
68
+ return &out[length];
69
+ }
70
+
71
+ void base32_encode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out) {
72
+ uint8_t remainder = inlen % 5;
73
+ size_t limit = inlen - remainder;
74
+
75
+ size_t i, j;
76
+ for (i = 0, j = 0; i < limit; i += 5, j += 8) {
77
+ base32_5to8(&in[i], 5, &out[j]);
78
+ }
79
+
80
+ if (remainder) base32_5to8(&in[i], remainder, &out[j]);
81
+ }
82
+
83
+ bool base32_decode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out, const char *alphabet) {
84
+ uint8_t remainder = inlen % 8;
85
+ size_t limit = inlen - remainder;
86
+
87
+ size_t i, j;
88
+ for (i = 0, j = 0; i < limit; i += 8, j += 5) {
89
+ if (!base32_8to5(&in[i], 8, &out[j], alphabet)) {
90
+ return false;
91
+ }
92
+ }
93
+
94
+ if (remainder && !base32_8to5(&in[i], remainder, &out[j], alphabet)) {
95
+ return false;
96
+ }
97
+
98
+ return true;
99
+ }
100
+
101
+ size_t base32_encoded_length(size_t inlen) {
102
+ uint8_t remainder = inlen % 5;
103
+
104
+ return (inlen / 5) * 8 + (remainder * 8 + 4) / 5;
105
+ }
106
+
107
+ size_t base32_decoded_length(size_t inlen) {
108
+ uint8_t remainder = inlen % 8;
109
+
110
+ return (inlen / 8) * 5 + (remainder * 5) / 8;
111
+ }
112
+
113
+ void base32_5to8(const uint8_t *in, uint8_t length, uint8_t *out) {
114
+ if (length >= 1) {
115
+ out[0] = (in[0] >> 3);
116
+ out[1] = (in[0] & 7) << 2;
117
+ }
118
+
119
+ if (length >= 2) {
120
+ out[1] |= (in[1] >> 6);
121
+ out[2] = (in[1] >> 1) & 31;
122
+ out[3] = (in[1] & 1) << 4;
123
+ }
124
+
125
+ if (length >= 3) {
126
+ out[3] |= (in[2] >> 4);
127
+ out[4] = (in[2] & 15) << 1;
128
+ }
129
+
130
+ if (length >= 4) {
131
+ out[4] |= (in[3] >> 7);
132
+ out[5] = (in[3] >> 2) & 31;
133
+ out[6] = (in[3] & 3) << 3;
134
+ }
135
+
136
+ if (length >= 5) {
137
+ out[6] |= (in[4] >> 5);
138
+ out[7] = (in[4] & 31);
139
+ }
140
+ }
141
+
142
+ bool base32_8to5(const uint8_t *in, uint8_t length, uint8_t *out, const char *alphabet) {
143
+ if (length == 1 || length == 3 || length == 6 || length > 8) {
144
+ return false;
145
+ }
146
+
147
+ if (alphabet) {
148
+ uint8_t decoded[length];
149
+
150
+ for (size_t i = 0; i < length; i++) {
151
+ int ret = base32_decode_character(in[i], alphabet);
152
+
153
+ if (ret == -1) {
154
+ return false;
155
+ } else {
156
+ decoded[i] = ret;
157
+ }
158
+ }
159
+
160
+ base32_8to5_raw(decoded, length, out);
161
+ } else {
162
+ base32_8to5_raw(in, length, out);
163
+ }
164
+
165
+ return true;
166
+ }
167
+
168
+ void base32_8to5_raw(const uint8_t *in, uint8_t length, uint8_t *out) {
169
+ if (length >= 2) {
170
+ out[0] = (in[0] << 3);
171
+ out[0] |= (in[1] >> 2);
172
+ }
173
+
174
+ if (length >= 4) {
175
+ out[1] = (in[1] & 3) << 6;
176
+ out[1] |= (in[2] << 1);
177
+ out[1] |= (in[3] >> 4);
178
+ }
179
+
180
+ if (length >= 5) {
181
+ out[2] = (in[3] & 15) << 4;
182
+ out[2] |= (in[4] >> 1);
183
+ }
184
+
185
+ if (length >= 7) {
186
+ out[3] = (in[4] & 1) << 7;
187
+ out[3] |= (in[5] << 2);
188
+ out[3] |= (in[6] >> 3);
189
+ }
190
+
191
+ if (length >= 8) {
192
+ out[4] = (in[6] & 7) << 5;
193
+ out[4] |= (in[7] & 31);
194
+ }
195
+ }
196
+
197
+ int base32_encode_character(uint8_t decoded, const char *alphabet) {
198
+ if (decoded >> 5) {
199
+ return -1;
200
+ }
201
+
202
+ if (alphabet == BASE32_ALPHABET_RFC4648) {
203
+ if (decoded < 26) {
204
+ return 'A' + decoded;
205
+ } else {
206
+ return '2' - 26 + decoded;
207
+ }
208
+ }
209
+
210
+ return alphabet[decoded];
211
+ }
212
+
213
+ int base32_decode_character(char encoded, const char *alphabet) {
214
+ if (alphabet == BASE32_ALPHABET_RFC4648) {
215
+ if (encoded >= 'A' && encoded <= 'Z') {
216
+ return encoded - 'A';
217
+ } else if (encoded >= 'a' && encoded <= 'z') {
218
+ return encoded - 'a';
219
+ } else if (encoded >= '2' && encoded <= '7') {
220
+ return encoded - '2' + 26;
221
+ } else {
222
+ return -1;
223
+ }
224
+ }
225
+
226
+ const char *occurrence = strchr(alphabet, encoded);
227
+
228
+ if (occurrence) {
229
+ return occurrence - alphabet;
230
+ } else {
231
+ return -1;
232
+ }
233
+ }
@@ -0,0 +1,298 @@
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/sha2.h>
29
+ #include <TrezorCrypto/ripemd160.h>
30
+ #include <TrezorCrypto/memzero.h>
31
+
32
+ const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
33
+ const int8_t b58digits_map[] = {
34
+ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
38
+ -1, 9,10,11,12,13,14,15,16,-1,17,18,19,20,21,-1,
39
+ 22,23,24,25,26,27,28,29,30,31,32,-1,-1,-1,-1,-1,
40
+ -1,33,34,35,36,37,38,39,40,41,42,43,-1,44,45,46,
41
+ 47,48,49,50,51,52,53,54,55,56,57,-1,-1,-1,-1,-1,
42
+ };
43
+
44
+ bool base58_to_bin(void *bin, size_t *binszp, const char *b58, const char digits[], const int8_t digits_map[])
45
+ {
46
+ size_t binsz = *binszp;
47
+
48
+ if (binsz == 0) {
49
+ return false;
50
+ }
51
+
52
+ const unsigned char *b58u = (const unsigned char*)b58;
53
+ unsigned char *binu = bin;
54
+ size_t outisz = (binsz + 3) / 4;
55
+ uint32_t outi[outisz];
56
+ uint64_t t;
57
+ uint32_t c;
58
+ size_t i, j;
59
+ uint8_t bytesleft = binsz % 4;
60
+ uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
61
+ unsigned zerocount = 0;
62
+ size_t b58sz;
63
+
64
+ b58sz = strlen(b58);
65
+
66
+ memset(outi, 0, outisz * sizeof(*outi));
67
+
68
+ // Leading zeros, just count
69
+ for (i = 0; i < b58sz && b58u[i] == digits[0]; ++i)
70
+ ++zerocount;
71
+
72
+ for ( ; i < b58sz; ++i)
73
+ {
74
+ if (b58u[i] & 0x80)
75
+ // High-bit set on invalid digit
76
+ return false;
77
+ if (digits_map[b58u[i]] == -1)
78
+ // Invalid base58 digit
79
+ return false;
80
+ c = (unsigned)digits_map[b58u[i]];
81
+ for (j = outisz; j--; )
82
+ {
83
+ t = ((uint64_t)outi[j]) * 58 + c;
84
+ c = (t & 0x3f00000000) >> 32;
85
+ outi[j] = t & 0xffffffff;
86
+ }
87
+ if (c)
88
+ // Output number too big (carry to the next int32)
89
+ return false;
90
+ if (outi[0] & zeromask)
91
+ // Output number too big (last int32 filled too far)
92
+ return false;
93
+ }
94
+
95
+ j = 0;
96
+ switch (bytesleft) {
97
+ case 3:
98
+ *(binu++) = (outi[0] & 0xff0000) >> 16;
99
+ //-fallthrough
100
+ case 2:
101
+ *(binu++) = (outi[0] & 0xff00) >> 8;
102
+ //-fallthrough
103
+ case 1:
104
+ *(binu++) = (outi[0] & 0xff);
105
+ ++j;
106
+ //-fallthrough
107
+ default:
108
+ break;
109
+ }
110
+
111
+ for (; j < outisz; ++j)
112
+ {
113
+ *(binu++) = (outi[j] >> 0x18) & 0xff;
114
+ *(binu++) = (outi[j] >> 0x10) & 0xff;
115
+ *(binu++) = (outi[j] >> 8) & 0xff;
116
+ *(binu++) = (outi[j] >> 0) & 0xff;
117
+ }
118
+
119
+ // Count canonical base58 byte count
120
+ binu = bin;
121
+ for (i = 0; i < binsz; ++i)
122
+ {
123
+ if (binu[i]) {
124
+ if (zerocount > i) {
125
+ /* result too large */
126
+ return false;
127
+ }
128
+ break;
129
+ }
130
+ --*binszp;
131
+ }
132
+ *binszp += zerocount;
133
+
134
+ return true;
135
+ }
136
+
137
+ bool b58tobin(void *bin, size_t *binszp, const char *b58)
138
+ {
139
+ return base58_to_bin(bin, binszp, b58, b58digits_ordered, b58digits_map);
140
+ }
141
+
142
+ int base58_check(const void *bin, size_t binsz, HasherType hasher_type, const char *base58str, const char digits[])
143
+ {
144
+ unsigned char buf[32];
145
+ const uint8_t *binc = bin;
146
+ unsigned i;
147
+ if (binsz < 4)
148
+ return -4;
149
+ hasher_Raw(hasher_type, bin, binsz - 4, buf);
150
+ if (memcmp(&binc[binsz - 4], buf, 4))
151
+ return -1;
152
+
153
+ // Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end)
154
+ for (i = 0; binc[i] == '\0' && base58str[i] == digits[0]; ++i)
155
+ {} // Just finding the end of zeros, nothing to do in loop
156
+ if (binc[i] == '\0' || base58str[i] == digits[0])
157
+ return -3;
158
+
159
+ return binc[0];
160
+ }
161
+
162
+ int b58check(const void *bin, size_t binsz, HasherType hasher_type, const char *base58str)
163
+ {
164
+ return base58_check(bin, binsz, hasher_type, base58str, b58digits_ordered);
165
+ }
166
+
167
+ bool base58_encode(char *b58, size_t *b58sz, const void *data, size_t binsz, const char digits[])
168
+ {
169
+ const uint8_t *bin = data;
170
+ int carry;
171
+ ssize_t i, j, high, zcount = 0;
172
+ size_t size;
173
+
174
+ while (zcount < (ssize_t)binsz && !bin[zcount])
175
+ ++zcount;
176
+
177
+ size = (binsz - zcount) * 138 / 100 + 1;
178
+ uint8_t buf[size];
179
+ memset(buf, 0, size);
180
+
181
+ for (i = zcount, high = size - 1; i < (ssize_t)binsz; ++i, high = j)
182
+ {
183
+ for (carry = bin[i], j = size - 1; (j > high) || carry; --j)
184
+ {
185
+ carry += 256 * buf[j];
186
+ buf[j] = carry % 58;
187
+ carry /= 58;
188
+ }
189
+ }
190
+
191
+ for (j = 0; j < (ssize_t)size && !buf[j]; ++j);
192
+
193
+ if (*b58sz <= zcount + size - j)
194
+ {
195
+ *b58sz = zcount + size - j + 1;
196
+ return false;
197
+ }
198
+
199
+ if (zcount)
200
+ memset(b58, digits[0], zcount);
201
+ for (i = zcount; j < (ssize_t)size; ++i, ++j)
202
+ b58[i] = digits[buf[j]];
203
+ b58[i] = '\0';
204
+ *b58sz = i + 1;
205
+
206
+ return true;
207
+ }
208
+
209
+ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
210
+ {
211
+ return base58_encode(b58, b58sz, data, binsz, b58digits_ordered);
212
+ }
213
+
214
+ int base58_encode_check(const uint8_t *data, int datalen, HasherType hasher_type, char *str, int strsize)
215
+ {
216
+ if (datalen > 128) {
217
+ return 0;
218
+ }
219
+ uint8_t buf[datalen + 32];
220
+ uint8_t *hash = buf + datalen;
221
+ memcpy(buf, data, datalen);
222
+ hasher_Raw(hasher_type, data, datalen, hash);
223
+ size_t res = strsize;
224
+ bool success = b58enc(str, &res, buf, datalen + 4);
225
+ memzero(buf, sizeof(buf));
226
+ return success ? res : 0;
227
+ }
228
+
229
+ int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data, int datalen)
230
+ {
231
+ if (datalen > 128) {
232
+ return 0;
233
+ }
234
+ uint8_t d[datalen + 4];
235
+ size_t res = datalen + 4;
236
+ if (b58tobin(d, &res, str) != true) {
237
+ return 0;
238
+ }
239
+ uint8_t *nd = d + datalen + 4 - res;
240
+ if (b58check(nd, res, hasher_type, str) < 0) {
241
+ return 0;
242
+ }
243
+ memcpy(data, nd, res - 4);
244
+ return res - 4;
245
+ }
246
+
247
+ int b58gphcheck(const void *bin, size_t binsz, const char *base58str)
248
+ {
249
+ unsigned char buf[32];
250
+ const uint8_t *binc = bin;
251
+ unsigned i;
252
+ if (binsz < 4)
253
+ return -4;
254
+ ripemd160(bin, binsz - 4, buf); // No double SHA256, but a single RIPEMD160
255
+ if (memcmp(&binc[binsz - 4], buf, 4))
256
+ return -1;
257
+
258
+ // Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end)
259
+ for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i)
260
+ {} // Just finding the end of zeros, nothing to do in loop
261
+ if (binc[i] == '\0' || base58str[i] == '1')
262
+ return -3;
263
+
264
+ return binc[0];
265
+ }
266
+
267
+ int base58gph_encode_check(const uint8_t *data, int datalen, char *str, int strsize)
268
+ {
269
+ if (datalen > 128) {
270
+ return 0;
271
+ }
272
+ uint8_t buf[datalen + 32];
273
+ uint8_t *hash = buf + datalen;
274
+ memcpy(buf, data, datalen);
275
+ ripemd160(data, datalen, hash); // No double SHA256, but a single RIPEMD160
276
+ size_t res = strsize;
277
+ bool success = b58enc(str, &res, buf, datalen + 4);
278
+ memzero(buf, sizeof(buf));
279
+ return success ? res : 0;
280
+ }
281
+
282
+ int base58gph_decode_check(const char *str, uint8_t *data, int datalen)
283
+ {
284
+ if (datalen > 128) {
285
+ return 0;
286
+ }
287
+ uint8_t d[datalen + 4];
288
+ size_t res = datalen + 4;
289
+ if (b58tobin(d, &res, str) != true) {
290
+ return 0;
291
+ }
292
+ uint8_t *nd = d + datalen + 4 - res;
293
+ if (b58gphcheck(nd, res, str) < 0) {
294
+ return 0;
295
+ }
296
+ memcpy(data, nd, res - 4);
297
+ return res - 4;
298
+ }