@onekeyfe/react-native-lite-card 6.7.0-6 → 6.7.0-61

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,1355 @@
1
+ /**
2
+ * Copyright (c) 2013-2014 Tomas Dzetkulic
3
+ * Copyright (c) 2013-2014 Pavol Rusnak
4
+ * Copyright (c) 2015 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
+ #include <stdio.h>
25
+ #include <stdint.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include <assert.h>
29
+
30
+ #include "options.h"
31
+
32
+ #include <TrezorCrypto/address.h>
33
+ #include <TrezorCrypto/base58.h>
34
+ #include <TrezorCrypto/bignum.h>
35
+ #include <TrezorCrypto/ecdsa.h>
36
+ #include <TrezorCrypto/hmac.h>
37
+ #include <TrezorCrypto/memzero.h>
38
+ #include <TrezorCrypto/rand.h>
39
+ #include <TrezorCrypto/rfc6979.h>
40
+ #include <TrezorCrypto/secp256k1.h>
41
+ #include <TrezorCrypto/schnorr.h>
42
+
43
+ // Set cp2 = cp1
44
+ void point_copy(const curve_point *cp1, curve_point *cp2)
45
+ {
46
+ *cp2 = *cp1;
47
+ }
48
+
49
+ // cp2 = cp1 + cp2
50
+ void point_add(const ecdsa_curve *curve, const curve_point *cp1, curve_point *cp2)
51
+ {
52
+ bignum256 lambda, inv, xr, yr;
53
+
54
+ if (point_is_infinity(cp1)) {
55
+ return;
56
+ }
57
+ if (point_is_infinity(cp2)) {
58
+ point_copy(cp1, cp2);
59
+ return;
60
+ }
61
+ if (point_is_equal(cp1, cp2)) {
62
+ point_double(curve, cp2);
63
+ return;
64
+ }
65
+ if (point_is_negative_of(cp1, cp2)) {
66
+ point_set_infinity(cp2);
67
+ return;
68
+ }
69
+
70
+ bn_subtractmod(&(cp2->x), &(cp1->x), &inv, &curve->prime);
71
+ bn_inverse(&inv, &curve->prime);
72
+ bn_subtractmod(&(cp2->y), &(cp1->y), &lambda, &curve->prime);
73
+ bn_multiply(&inv, &lambda, &curve->prime);
74
+
75
+ // xr = lambda^2 - x1 - x2
76
+ xr = lambda;
77
+ bn_multiply(&xr, &xr, &curve->prime);
78
+ yr = cp1->x;
79
+ bn_addmod(&yr, &(cp2->x), &curve->prime);
80
+ bn_subtractmod(&xr, &yr, &xr, &curve->prime);
81
+ bn_fast_mod(&xr, &curve->prime);
82
+ bn_mod(&xr, &curve->prime);
83
+
84
+ // yr = lambda (x1 - xr) - y1
85
+ bn_subtractmod(&(cp1->x), &xr, &yr, &curve->prime);
86
+ bn_multiply(&lambda, &yr, &curve->prime);
87
+ bn_subtractmod(&yr, &(cp1->y), &yr, &curve->prime);
88
+ bn_fast_mod(&yr, &curve->prime);
89
+ bn_mod(&yr, &curve->prime);
90
+
91
+ cp2->x = xr;
92
+ cp2->y = yr;
93
+ }
94
+
95
+ // cp = cp + cp
96
+ void point_double(const ecdsa_curve *curve, curve_point *cp)
97
+ {
98
+ bignum256 lambda, xr, yr;
99
+
100
+ if (point_is_infinity(cp)) {
101
+ return;
102
+ }
103
+ if (bn_is_zero(&(cp->y))) {
104
+ point_set_infinity(cp);
105
+ return;
106
+ }
107
+
108
+ // lambda = (3 x^2 + a) / (2 y)
109
+ lambda = cp->y;
110
+ bn_mult_k(&lambda, 2, &curve->prime);
111
+ bn_inverse(&lambda, &curve->prime);
112
+
113
+ xr = cp->x;
114
+ bn_multiply(&xr, &xr, &curve->prime);
115
+ bn_mult_k(&xr, 3, &curve->prime);
116
+ bn_subi(&xr, -curve->a, &curve->prime);
117
+ bn_multiply(&xr, &lambda, &curve->prime);
118
+
119
+ // xr = lambda^2 - 2*x
120
+ xr = lambda;
121
+ bn_multiply(&xr, &xr, &curve->prime);
122
+ yr = cp->x;
123
+ bn_lshift(&yr);
124
+ bn_subtractmod(&xr, &yr, &xr, &curve->prime);
125
+ bn_fast_mod(&xr, &curve->prime);
126
+ bn_mod(&xr, &curve->prime);
127
+
128
+ // yr = lambda (x - xr) - y
129
+ bn_subtractmod(&(cp->x), &xr, &yr, &curve->prime);
130
+ bn_multiply(&lambda, &yr, &curve->prime);
131
+ bn_subtractmod(&yr, &(cp->y), &yr, &curve->prime);
132
+ bn_fast_mod(&yr, &curve->prime);
133
+ bn_mod(&yr, &curve->prime);
134
+
135
+ cp->x = xr;
136
+ cp->y = yr;
137
+ }
138
+
139
+ // set point to internal representation of point at infinity
140
+ void point_set_infinity(curve_point *p)
141
+ {
142
+ bn_zero(&(p->x));
143
+ bn_zero(&(p->y));
144
+ }
145
+
146
+ // return true iff p represent point at infinity
147
+ // both coords are zero in internal representation
148
+ int point_is_infinity(const curve_point *p)
149
+ {
150
+ return bn_is_zero(&(p->x)) && bn_is_zero(&(p->y));
151
+ }
152
+
153
+ // return true iff both points are equal
154
+ int point_is_equal(const curve_point *p, const curve_point *q)
155
+ {
156
+ return bn_is_equal(&(p->x), &(q->x)) && bn_is_equal(&(p->y), &(q->y));
157
+ }
158
+
159
+ // returns true iff p == -q
160
+ // expects p and q be valid points on curve other than point at infinity
161
+ int point_is_negative_of(const curve_point *p, const curve_point *q)
162
+ {
163
+ // if P == (x, y), then -P would be (x, -y) on this curve
164
+ if (!bn_is_equal(&(p->x), &(q->x))) {
165
+ return 0;
166
+ }
167
+
168
+ // we shouldn't hit this for a valid point
169
+ if (bn_is_zero(&(p->y))) {
170
+ return 0;
171
+ }
172
+
173
+ return !bn_is_equal(&(p->y), &(q->y));
174
+ }
175
+
176
+ // Negate a (modulo prime) if cond is 0xffffffff, keep it if cond is 0.
177
+ // The timing of this function does not depend on cond.
178
+ void conditional_negate(uint32_t cond, bignum256 *a, const bignum256 *prime)
179
+ {
180
+ int j;
181
+ uint32_t tmp = 1;
182
+ assert(a->val[8] < 0x20000);
183
+ for (j = 0; j < 8; j++) {
184
+ tmp += 0x3fffffff + 2*prime->val[j] - a->val[j];
185
+ a->val[j] = ((tmp & 0x3fffffff) & cond) | (a->val[j] & ~cond);
186
+ tmp >>= 30;
187
+ }
188
+ tmp += 0x3fffffff + 2*prime->val[j] - a->val[j];
189
+ a->val[j] = ((tmp & 0x3fffffff) & cond) | (a->val[j] & ~cond);
190
+ assert(a->val[8] < 0x20000);
191
+ }
192
+
193
+ typedef struct jacobian_curve_point {
194
+ bignum256 x, y, z;
195
+ } jacobian_curve_point;
196
+
197
+ // generate random K for signing/side-channel noise
198
+ static void generate_k_random(bignum256 *k, const bignum256 *prime) {
199
+ do {
200
+ int i;
201
+ for (i = 0; i < 8; i++) {
202
+ k->val[i] = random32() & 0x3FFFFFFF;
203
+ }
204
+ k->val[8] = random32() & 0xFFFF;
205
+ // check that k is in range and not zero.
206
+ } while (bn_is_zero(k) || !bn_is_less(k, prime));
207
+ }
208
+
209
+ void curve_to_jacobian(const curve_point *p, jacobian_curve_point *jp, const bignum256 *prime) {
210
+ // randomize z coordinate
211
+ generate_k_random(&jp->z, prime);
212
+
213
+ jp->x = jp->z;
214
+ bn_multiply(&jp->z, &jp->x, prime);
215
+ // x = z^2
216
+ jp->y = jp->x;
217
+ bn_multiply(&jp->z, &jp->y, prime);
218
+ // y = z^3
219
+
220
+ bn_multiply(&p->x, &jp->x, prime);
221
+ bn_multiply(&p->y, &jp->y, prime);
222
+ }
223
+
224
+ void jacobian_to_curve(const jacobian_curve_point *jp, curve_point *p, const bignum256 *prime) {
225
+ p->y = jp->z;
226
+ bn_inverse(&p->y, prime);
227
+ // p->y = z^-1
228
+ p->x = p->y;
229
+ bn_multiply(&p->x, &p->x, prime);
230
+ // p->x = z^-2
231
+ bn_multiply(&p->x, &p->y, prime);
232
+ // p->y = z^-3
233
+ bn_multiply(&jp->x, &p->x, prime);
234
+ // p->x = jp->x * z^-2
235
+ bn_multiply(&jp->y, &p->y, prime);
236
+ // p->y = jp->y * z^-3
237
+ bn_mod(&p->x, prime);
238
+ bn_mod(&p->y, prime);
239
+ }
240
+
241
+ void point_jacobian_add(const curve_point *p1, jacobian_curve_point *p2, const ecdsa_curve *curve) {
242
+ bignum256 r, h, r2;
243
+ bignum256 hcby, hsqx;
244
+ bignum256 xz, yz, az;
245
+ int is_doubling;
246
+ const bignum256 *prime = &curve->prime;
247
+ int a = curve->a;
248
+
249
+ assert (-3 <= a && a <= 0);
250
+
251
+ /* First we bring p1 to the same denominator:
252
+ * x1' := x1 * z2^2
253
+ * y1' := y1 * z2^3
254
+ */
255
+ /*
256
+ * lambda = ((y1' - y2)/z2^3) / ((x1' - x2)/z2^2)
257
+ * = (y1' - y2) / (x1' - x2) z2
258
+ * x3/z3^2 = lambda^2 - (x1' + x2)/z2^2
259
+ * y3/z3^3 = 1/2 lambda * (2x3/z3^2 - (x1' + x2)/z2^2) + (y1'+y2)/z2^3
260
+ *
261
+ * For the special case x1=x2, y1=y2 (doubling) we have
262
+ * lambda = 3/2 ((x2/z2^2)^2 + a) / (y2/z2^3)
263
+ * = 3/2 (x2^2 + a*z2^4) / y2*z2)
264
+ *
265
+ * to get rid of fraction we write lambda as
266
+ * lambda = r / (h*z2)
267
+ * with r = is_doubling ? 3/2 x2^2 + az2^4 : (y1 - y2)
268
+ * h = is_doubling ? y1+y2 : (x1 - x2)
269
+ *
270
+ * With z3 = h*z2 (the denominator of lambda)
271
+ * we get x3 = lambda^2*z3^2 - (x1' + x2)/z2^2*z3^2
272
+ * = r^2 - h^2 * (x1' + x2)
273
+ * and y3 = 1/2 r * (2x3 - h^2*(x1' + x2)) + h^3*(y1' + y2)
274
+ */
275
+
276
+ /* h = x1 - x2
277
+ * r = y1 - y2
278
+ * x3 = r^2 - h^3 - 2*h^2*x2
279
+ * y3 = r*(h^2*x2 - x3) - h^3*y2
280
+ * z3 = h*z2
281
+ */
282
+
283
+ xz = p2->z;
284
+ bn_multiply(&xz, &xz, prime); // xz = z2^2
285
+ yz = p2->z;
286
+ bn_multiply(&xz, &yz, prime); // yz = z2^3
287
+
288
+ if (a != 0) {
289
+ az = xz;
290
+ bn_multiply(&az, &az, prime); // az = z2^4
291
+ bn_mult_k(&az, -a, prime); // az = -az2^4
292
+ }
293
+
294
+ bn_multiply(&p1->x, &xz, prime); // xz = x1' = x1*z2^2;
295
+ h = xz;
296
+ bn_subtractmod(&h, &p2->x, &h, prime);
297
+ bn_fast_mod(&h, prime);
298
+ // h = x1' - x2;
299
+
300
+ bn_add(&xz, &p2->x);
301
+ // xz = x1' + x2
302
+
303
+ // check for h == 0 % prime. Note that h never normalizes to
304
+ // zero, since h = x1' + 2*prime - x2 > 0 and a positive
305
+ // multiple of prime is always normalized to prime by
306
+ // bn_fast_mod.
307
+ is_doubling = bn_is_equal(&h, prime);
308
+
309
+ bn_multiply(&p1->y, &yz, prime); // yz = y1' = y1*z2^3;
310
+ bn_subtractmod(&yz, &p2->y, &r, prime);
311
+ // r = y1' - y2;
312
+
313
+ bn_add(&yz, &p2->y);
314
+ // yz = y1' + y2
315
+
316
+ r2 = p2->x;
317
+ bn_multiply(&r2, &r2, prime);
318
+ bn_mult_k(&r2, 3, prime);
319
+
320
+ if (a != 0) {
321
+ // subtract -a z2^4, i.e, add a z2^4
322
+ bn_subtractmod(&r2, &az, &r2, prime);
323
+ }
324
+ bn_cmov(&r, is_doubling, &r2, &r);
325
+ bn_cmov(&h, is_doubling, &yz, &h);
326
+
327
+
328
+ // hsqx = h^2
329
+ hsqx = h;
330
+ bn_multiply(&hsqx, &hsqx, prime);
331
+
332
+ // hcby = h^3
333
+ hcby = h;
334
+ bn_multiply(&hsqx, &hcby, prime);
335
+
336
+ // hsqx = h^2 * (x1 + x2)
337
+ bn_multiply(&xz, &hsqx, prime);
338
+
339
+ // hcby = h^3 * (y1 + y2)
340
+ bn_multiply(&yz, &hcby, prime);
341
+
342
+ // z3 = h*z2
343
+ bn_multiply(&h, &p2->z, prime);
344
+
345
+ // x3 = r^2 - h^2 (x1 + x2)
346
+ p2->x = r;
347
+ bn_multiply(&p2->x, &p2->x, prime);
348
+ bn_subtractmod(&p2->x, &hsqx, &p2->x, prime);
349
+ bn_fast_mod(&p2->x, prime);
350
+
351
+ // y3 = 1/2 (r*(h^2 (x1 + x2) - 2x3) - h^3 (y1 + y2))
352
+ bn_subtractmod(&hsqx, &p2->x, &p2->y, prime);
353
+ bn_subtractmod(&p2->y, &p2->x, &p2->y, prime);
354
+ bn_multiply(&r, &p2->y, prime);
355
+ bn_subtractmod(&p2->y, &hcby, &p2->y, prime);
356
+ bn_mult_half(&p2->y, prime);
357
+ bn_fast_mod(&p2->y, prime);
358
+ }
359
+
360
+ void point_jacobian_double(jacobian_curve_point *p, const ecdsa_curve *curve) {
361
+ bignum256 az4, m, msq, ysq, xysq;
362
+ const bignum256 *prime = &curve->prime;
363
+
364
+ assert (-3 <= curve->a && curve->a <= 0);
365
+ /* usual algorithm:
366
+ *
367
+ * lambda = (3((x/z^2)^2 + a) / 2y/z^3) = (3x^2 + az^4)/2yz
368
+ * x3/z3^2 = lambda^2 - 2x/z^2
369
+ * y3/z3^3 = lambda * (x/z^2 - x3/z3^2) - y/z^3
370
+ *
371
+ * to get rid of fraction we set
372
+ * m = (3 x^2 + az^4) / 2
373
+ * Hence,
374
+ * lambda = m / yz = m / z3
375
+ *
376
+ * With z3 = yz (the denominator of lambda)
377
+ * we get x3 = lambda^2*z3^2 - 2*x/z^2*z3^2
378
+ * = m^2 - 2*xy^2
379
+ * and y3 = (lambda * (x/z^2 - x3/z3^2) - y/z^3) * z3^3
380
+ * = m * (xy^2 - x3) - y^4
381
+ */
382
+
383
+ /* m = (3*x^2 + a z^4) / 2
384
+ * x3 = m^2 - 2*xy^2
385
+ * y3 = m*(xy^2 - x3) - 8y^4
386
+ * z3 = y*z
387
+ */
388
+
389
+ m = p->x;
390
+ bn_multiply(&m, &m, prime);
391
+ bn_mult_k(&m, 3, prime);
392
+
393
+ az4 = p->z;
394
+ bn_multiply(&az4, &az4, prime);
395
+ bn_multiply(&az4, &az4, prime);
396
+ bn_mult_k(&az4, -curve->a, prime);
397
+ bn_subtractmod(&m, &az4, &m, prime);
398
+ bn_mult_half(&m, prime);
399
+
400
+ // msq = m^2
401
+ msq = m;
402
+ bn_multiply(&msq, &msq, prime);
403
+ // ysq = y^2
404
+ ysq = p->y;
405
+ bn_multiply(&ysq, &ysq, prime);
406
+ // xysq = xy^2
407
+ xysq = p->x;
408
+ bn_multiply(&ysq, &xysq, prime);
409
+
410
+ // z3 = yz
411
+ bn_multiply(&p->y, &p->z, prime);
412
+
413
+ // x3 = m^2 - 2*xy^2
414
+ p->x = xysq;
415
+ bn_lshift(&p->x);
416
+ bn_fast_mod(&p->x, prime);
417
+ bn_subtractmod(&msq, &p->x, &p->x, prime);
418
+ bn_fast_mod(&p->x, prime);
419
+
420
+ // y3 = m*(xy^2 - x3) - y^4
421
+ bn_subtractmod(&xysq, &p->x, &p->y, prime);
422
+ bn_multiply(&m, &p->y, prime);
423
+ bn_multiply(&ysq, &ysq, prime);
424
+ bn_subtractmod(&p->y, &ysq, &p->y, prime);
425
+ bn_fast_mod(&p->y, prime);
426
+ }
427
+
428
+ // res = k * p
429
+ void point_multiply(const ecdsa_curve *curve, const bignum256 *k, const curve_point *p, curve_point *res)
430
+ {
431
+ // this algorithm is loosely based on
432
+ // Katsuyuki Okeya and Tsuyoshi Takagi, The Width-w NAF Method Provides
433
+ // Small Memory and Fast Elliptic Scalar Multiplications Secure against
434
+ // Side Channel Attacks.
435
+ assert (bn_is_less(k, &curve->order));
436
+
437
+ int i, j;
438
+ CONFIDENTIAL bignum256 a;
439
+ uint32_t *aptr;
440
+ uint32_t abits;
441
+ int ashift;
442
+ uint32_t is_even = (k->val[0] & 1) - 1;
443
+ uint32_t bits, sign, nsign;
444
+ CONFIDENTIAL jacobian_curve_point jres;
445
+ curve_point pmult[8];
446
+ const bignum256 *prime = &curve->prime;
447
+
448
+ // is_even = 0xffffffff if k is even, 0 otherwise.
449
+
450
+ // add 2^256.
451
+ // make number odd: subtract curve->order if even
452
+ uint32_t tmp = 1;
453
+ uint32_t is_non_zero = 0;
454
+ for (j = 0; j < 8; j++) {
455
+ is_non_zero |= k->val[j];
456
+ tmp += 0x3fffffff + k->val[j] - (curve->order.val[j] & is_even);
457
+ a.val[j] = tmp & 0x3fffffff;
458
+ tmp >>= 30;
459
+ }
460
+ is_non_zero |= k->val[j];
461
+ a.val[j] = tmp + 0xffff + k->val[j] - (curve->order.val[j] & is_even);
462
+ assert((a.val[0] & 1) != 0);
463
+
464
+ // special case 0*p: just return zero. We don't care about constant time.
465
+ if (!is_non_zero) {
466
+ point_set_infinity(res);
467
+ return;
468
+ }
469
+
470
+ // Now a = k + 2^256 (mod curve->order) and a is odd.
471
+ //
472
+ // The idea is to bring the new a into the form.
473
+ // sum_{i=0..64} a[i] 16^i, where |a[i]| < 16 and a[i] is odd.
474
+ // a[0] is odd, since a is odd. If a[i] would be even, we can
475
+ // add 1 to it and subtract 16 from a[i-1]. Afterwards,
476
+ // a[64] = 1, which is the 2^256 that we added before.
477
+ //
478
+ // Since k = a - 2^256 (mod curve->order), we can compute
479
+ // k*p = sum_{i=0..63} a[i] 16^i * p
480
+ //
481
+ // We compute |a[i]| * p in advance for all possible
482
+ // values of |a[i]| * p. pmult[i] = (2*i+1) * p
483
+ // We compute p, 3*p, ..., 15*p and store it in the table pmult.
484
+ // store p^2 temporarily in pmult[7]
485
+ pmult[7] = *p;
486
+ point_double(curve, &pmult[7]);
487
+ // compute 3*p, etc by repeatedly adding p^2.
488
+ pmult[0] = *p;
489
+ for (i = 1; i < 8; i++) {
490
+ pmult[i] = pmult[7];
491
+ point_add(curve, &pmult[i-1], &pmult[i]);
492
+ }
493
+
494
+ // now compute res = sum_{i=0..63} a[i] * 16^i * p step by step,
495
+ // starting with i = 63.
496
+ // initialize jres = |a[63]| * p.
497
+ // Note that a[i] = a>>(4*i) & 0xf if (a&0x10) != 0
498
+ // and - (16 - (a>>(4*i) & 0xf)) otherwise. We can compute this as
499
+ // ((a ^ (((a >> 4) & 1) - 1)) & 0xf) >> 1
500
+ // since a is odd.
501
+ aptr = &a.val[8];
502
+ abits = *aptr;
503
+ ashift = 12;
504
+ bits = abits >> ashift;
505
+ sign = (bits >> 4) - 1;
506
+ bits ^= sign;
507
+ bits &= 15;
508
+ curve_to_jacobian(&pmult[bits>>1], &jres, prime);
509
+ for (i = 62; i >= 0; i--) {
510
+ // sign = sign(a[i+1]) (0xffffffff for negative, 0 for positive)
511
+ // invariant jres = (-1)^sign sum_{j=i+1..63} (a[j] * 16^{j-i-1} * p)
512
+ // abits >> (ashift - 4) = lowbits(a >> (i*4))
513
+
514
+ point_jacobian_double(&jres, curve);
515
+ point_jacobian_double(&jres, curve);
516
+ point_jacobian_double(&jres, curve);
517
+ point_jacobian_double(&jres, curve);
518
+
519
+ // get lowest 5 bits of a >> (i*4).
520
+ ashift -= 4;
521
+ if (ashift < 0) {
522
+ // the condition only depends on the iteration number and
523
+ // leaks no private information to a side-channel.
524
+ bits = abits << (-ashift);
525
+ abits = *(--aptr);
526
+ ashift += 30;
527
+ bits |= abits >> ashift;
528
+ } else {
529
+ bits = abits >> ashift;
530
+ }
531
+ bits &= 31;
532
+ nsign = (bits >> 4) - 1;
533
+ bits ^= nsign;
534
+ bits &= 15;
535
+
536
+ // negate last result to make signs of this round and the
537
+ // last round equal.
538
+ conditional_negate(sign ^ nsign, &jres.z, prime);
539
+
540
+ // add odd factor
541
+ point_jacobian_add(&pmult[bits >> 1], &jres, curve);
542
+ sign = nsign;
543
+ }
544
+ conditional_negate(sign, &jres.z, prime);
545
+ jacobian_to_curve(&jres, res, prime);
546
+ memzero(&a, sizeof(a));
547
+ memzero(&jres, sizeof(jres));
548
+ }
549
+
550
+ // res = k * G
551
+ // k must be a normalized number with 0 <= k < curve->order
552
+ void scalar_multiply(const ecdsa_curve *curve, const bignum256 *k, curve_point *res)
553
+ {
554
+ assert (bn_is_less(k, &curve->order));
555
+
556
+ int i, j;
557
+ CONFIDENTIAL bignum256 a;
558
+ uint32_t is_even = (k->val[0] & 1) - 1;
559
+ uint32_t lowbits;
560
+ CONFIDENTIAL jacobian_curve_point jres;
561
+ const bignum256 *prime = &curve->prime;
562
+
563
+ // is_even = 0xffffffff if k is even, 0 otherwise.
564
+
565
+ // add 2^256.
566
+ // make number odd: subtract curve->order if even
567
+ uint32_t tmp = 1;
568
+ uint32_t is_non_zero = 0;
569
+ for (j = 0; j < 8; j++) {
570
+ is_non_zero |= k->val[j];
571
+ tmp += 0x3fffffff + k->val[j] - (curve->order.val[j] & is_even);
572
+ a.val[j] = tmp & 0x3fffffff;
573
+ tmp >>= 30;
574
+ }
575
+ is_non_zero |= k->val[j];
576
+ a.val[j] = tmp + 0xffff + k->val[j] - (curve->order.val[j] & is_even);
577
+ assert((a.val[0] & 1) != 0);
578
+
579
+ // special case 0*G: just return zero. We don't care about constant time.
580
+ if (!is_non_zero) {
581
+ point_set_infinity(res);
582
+ return;
583
+ }
584
+
585
+ // Now a = k + 2^256 (mod curve->order) and a is odd.
586
+ //
587
+ // The idea is to bring the new a into the form.
588
+ // sum_{i=0..64} a[i] 16^i, where |a[i]| < 16 and a[i] is odd.
589
+ // a[0] is odd, since a is odd. If a[i] would be even, we can
590
+ // add 1 to it and subtract 16 from a[i-1]. Afterwards,
591
+ // a[64] = 1, which is the 2^256 that we added before.
592
+ //
593
+ // Since k = a - 2^256 (mod curve->order), we can compute
594
+ // k*G = sum_{i=0..63} a[i] 16^i * G
595
+ //
596
+ // We have a big table curve->cp that stores all possible
597
+ // values of |a[i]| 16^i * G.
598
+ // curve->cp[i][j] = (2*j+1) * 16^i * G
599
+
600
+ // now compute res = sum_{i=0..63} a[i] * 16^i * G step by step.
601
+ // initial res = |a[0]| * G. Note that a[0] = a & 0xf if (a&0x10) != 0
602
+ // and - (16 - (a & 0xf)) otherwise. We can compute this as
603
+ // ((a ^ (((a >> 4) & 1) - 1)) & 0xf) >> 1
604
+ // since a is odd.
605
+ lowbits = a.val[0] & ((1 << 5) - 1);
606
+ lowbits ^= (lowbits >> 4) - 1;
607
+ lowbits &= 15;
608
+ curve_to_jacobian(&curve->cp[0][lowbits >> 1], &jres, prime);
609
+ for (i = 1; i < 64; i ++) {
610
+ // invariant res = sign(a[i-1]) sum_{j=0..i-1} (a[j] * 16^j * G)
611
+
612
+ // shift a by 4 places.
613
+ for (j = 0; j < 8; j++) {
614
+ a.val[j] = (a.val[j] >> 4) | ((a.val[j + 1] & 0xf) << 26);
615
+ }
616
+ a.val[j] >>= 4;
617
+ // a = old(a)>>(4*i)
618
+ // a is even iff sign(a[i-1]) = -1
619
+
620
+ lowbits = a.val[0] & ((1 << 5) - 1);
621
+ lowbits ^= (lowbits >> 4) - 1;
622
+ lowbits &= 15;
623
+ // negate last result to make signs of this round and the
624
+ // last round equal.
625
+ conditional_negate((lowbits & 1) - 1, &jres.y, prime);
626
+
627
+ // add odd factor
628
+ point_jacobian_add(&curve->cp[i][lowbits >> 1], &jres, curve);
629
+ }
630
+ conditional_negate(((a.val[0] >> 4) & 1) - 1, &jres.y, prime);
631
+ jacobian_to_curve(&jres, res, prime);
632
+ memzero(&a, sizeof(a));
633
+ memzero(&jres, sizeof(jres));
634
+ }
635
+
636
+ int ecdh_multiply(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *pub_key, uint8_t *session_key)
637
+ {
638
+ curve_point point;
639
+ if (!ecdsa_read_pubkey(curve, pub_key, &point)) {
640
+ return 1;
641
+ }
642
+
643
+ bignum256 k;
644
+ bn_read_be(priv_key, &k);
645
+ point_multiply(curve, &k, &point, &point);
646
+ memzero(&k, sizeof(k));
647
+
648
+ session_key[0] = 0x04;
649
+ bn_write_be(&point.x, session_key + 1);
650
+ bn_write_be(&point.y, session_key + 33);
651
+ memzero(&point, sizeof(point));
652
+
653
+ return 0;
654
+ }
655
+
656
+ void init_rfc6979(const uint8_t *priv_key, const uint8_t *hash, rfc6979_state *state) {
657
+ uint8_t bx[2*32];
658
+ uint8_t buf[32 + 1 + 2*32];
659
+
660
+ memcpy(bx, priv_key, 32);
661
+ memcpy(bx+32, hash, 32);
662
+
663
+ memset(state->v, 1, sizeof(state->v));
664
+ memset(state->k, 0, sizeof(state->k));
665
+
666
+ memcpy(buf, state->v, sizeof(state->v));
667
+ buf[sizeof(state->v)] = 0x00;
668
+ memcpy(buf + sizeof(state->v) + 1, bx, 64);
669
+ hmac_sha256(state->k, sizeof(state->k), buf, sizeof(buf), state->k);
670
+ hmac_sha256(state->k, sizeof(state->k), state->v, sizeof(state->v), state->v);
671
+
672
+ memcpy(buf, state->v, sizeof(state->v));
673
+ buf[sizeof(state->v)] = 0x01;
674
+ memcpy(buf + sizeof(state->v) + 1, bx, 64);
675
+ hmac_sha256(state->k, sizeof(state->k), buf, sizeof(buf), state->k);
676
+ hmac_sha256(state->k, sizeof(state->k), state->v, sizeof(state->v), state->v);
677
+
678
+ memzero(bx, sizeof(bx));
679
+ memzero(buf, sizeof(buf));
680
+ }
681
+
682
+ // generate next number from deterministic random number generator
683
+ void generate_rfc6979(uint8_t rnd[32], rfc6979_state *state)
684
+ {
685
+ uint8_t buf[32 + 1];
686
+
687
+ hmac_sha256(state->k, sizeof(state->k), state->v, sizeof(state->v), state->v);
688
+ memcpy(buf, state->v, sizeof(state->v));
689
+ buf[sizeof(state->v)] = 0x00;
690
+ hmac_sha256(state->k, sizeof(state->k), buf, sizeof(state->v) + 1, state->k);
691
+ hmac_sha256(state->k, sizeof(state->k), state->v, sizeof(state->v), state->v);
692
+ memcpy(rnd, buf, 32);
693
+ memzero(buf, sizeof(buf));
694
+ }
695
+
696
+ // generate K in a deterministic way, according to RFC6979
697
+ // http://tools.ietf.org/html/rfc6979
698
+ void generate_k_rfc6979(bignum256 *k, rfc6979_state *state)
699
+ {
700
+ uint8_t buf[32];
701
+ generate_rfc6979(buf, state);
702
+ bn_read_be(buf, k);
703
+ memzero(buf, sizeof(buf));
704
+ }
705
+
706
+ // msg is a data to be signed
707
+ // msg_len is the message length
708
+ int ecdsa_sign(const ecdsa_curve *curve, HasherType hasher_sign, const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
709
+ {
710
+ uint8_t hash[32];
711
+ hasher_Raw(hasher_sign, msg, msg_len, hash);
712
+ int res = ecdsa_sign_digest(curve, priv_key, hash, sig, pby, is_canonical);
713
+ memzero(hash, sizeof(hash));
714
+ return res;
715
+
716
+ }
717
+
718
+ // uses secp256k1 curve
719
+ // priv_key is a 32 byte big endian stored number
720
+ // sig is 64 bytes long array for the signature
721
+ // digest is 32 bytes of digest
722
+ // is_canonical is an optional function that checks if the signature
723
+ // conforms to additional coin-specific rules.
724
+ int ecdsa_sign_digest(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *digest, uint8_t *sig, uint8_t *pby, int (*is_canonical)(uint8_t by, uint8_t sig[64]))
725
+ {
726
+ int i;
727
+ curve_point R;
728
+ bignum256 k, z, randk;
729
+ bignum256 *s = &R.y;
730
+ uint8_t by; // signature recovery byte
731
+
732
+ rfc6979_state rng;
733
+ init_rfc6979(priv_key, digest, &rng);
734
+
735
+ bn_read_be(digest, &z);
736
+
737
+ for (i = 0; i < 10000; i++) {
738
+
739
+ // generate K deterministically
740
+ generate_k_rfc6979(&k, &rng);
741
+ // if k is too big or too small, we don't like it
742
+ if (bn_is_zero(&k) || !bn_is_less(&k, &curve->order)) {
743
+ continue;
744
+ }
745
+
746
+ // compute k*G
747
+ scalar_multiply(curve, &k, &R);
748
+ by = R.y.val[0] & 1;
749
+ // r = (rx mod n)
750
+ if (!bn_is_less(&R.x, &curve->order)) {
751
+ bn_subtract(&R.x, &curve->order, &R.x);
752
+ by |= 2;
753
+ }
754
+ // if r is zero, we retry
755
+ if (bn_is_zero(&R.x)) {
756
+ continue;
757
+ }
758
+
759
+ // randomize operations to counter side-channel attacks
760
+ generate_k_random(&randk, &curve->order);
761
+ bn_multiply(&randk, &k, &curve->order); // k*rand
762
+ bn_inverse(&k, &curve->order); // (k*rand)^-1
763
+ bn_read_be(priv_key, s); // priv
764
+ bn_multiply(&R.x, s, &curve->order); // R.x*priv
765
+ bn_add(s, &z); // R.x*priv + z
766
+ bn_multiply(&k, s, &curve->order); // (k*rand)^-1 (R.x*priv + z)
767
+ bn_multiply(&randk, s, &curve->order); // k^-1 (R.x*priv + z)
768
+ bn_mod(s, &curve->order);
769
+ // if s is zero, we retry
770
+ if (bn_is_zero(s)) {
771
+ continue;
772
+ }
773
+
774
+ // if S > order/2 => S = -S
775
+ if (bn_is_less(&curve->order_half, s)) {
776
+ bn_subtract(&curve->order, s, s);
777
+ by ^= 1;
778
+ }
779
+ // we are done, R.x and s is the result signature
780
+ bn_write_be(&R.x, sig);
781
+ bn_write_be(s, sig + 32);
782
+
783
+ // check if the signature is acceptable or retry
784
+ if (is_canonical && !is_canonical(by, sig)) {
785
+ continue;
786
+ }
787
+
788
+ if (pby) {
789
+ *pby = by;
790
+ }
791
+
792
+ memzero(&k, sizeof(k));
793
+ memzero(&randk, sizeof(randk));
794
+ memzero(&rng, sizeof(rng));
795
+ return 0;
796
+ }
797
+
798
+ // Too many retries without a valid signature
799
+ // -> fail with an error
800
+ memzero(&k, sizeof(k));
801
+ memzero(&randk, sizeof(randk));
802
+ memzero(&rng, sizeof(rng));
803
+ return -1;
804
+ }
805
+
806
+ void ecdsa_get_public_key33(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key)
807
+ {
808
+ curve_point R;
809
+ bignum256 k;
810
+
811
+ bn_read_be(priv_key, &k);
812
+ // compute k*G
813
+ scalar_multiply(curve, &k, &R);
814
+ pub_key[0] = 0x02 | (R.y.val[0] & 0x01);
815
+ bn_write_be(&R.x, pub_key + 1);
816
+ memzero(&R, sizeof(R));
817
+ memzero(&k, sizeof(k));
818
+ }
819
+
820
+ void ecdsa_get_public_key65(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key)
821
+ {
822
+ curve_point R;
823
+ bignum256 k;
824
+
825
+ bn_read_be(priv_key, &k);
826
+ // compute k*G
827
+ scalar_multiply(curve, &k, &R);
828
+ pub_key[0] = 0x04;
829
+ bn_write_be(&R.x, pub_key + 1);
830
+ bn_write_be(&R.y, pub_key + 33);
831
+ memzero(&R, sizeof(R));
832
+ memzero(&k, sizeof(k));
833
+ }
834
+
835
+ int ecdsa_uncompress_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, uint8_t *uncompressed)
836
+ {
837
+ curve_point pub;
838
+
839
+ if (!ecdsa_read_pubkey(curve, pub_key, &pub)) {
840
+ return 0;
841
+ }
842
+
843
+ uncompressed[0] = 4;
844
+ bn_write_be(&pub.x, uncompressed + 1);
845
+ bn_write_be(&pub.y, uncompressed + 33);
846
+
847
+ return 1;
848
+ }
849
+
850
+ void ecdsa_get_pubkeyhash(const uint8_t *pub_key, HasherType hasher_pubkey, uint8_t *pubkeyhash)
851
+ {
852
+ uint8_t h[HASHER_DIGEST_LENGTH];
853
+ if (pub_key[0] == 0x04) { // uncompressed format
854
+ hasher_Raw(hasher_pubkey, pub_key, 65, h);
855
+ } else if (pub_key[0] == 0x00) { // point at infinity
856
+ hasher_Raw(hasher_pubkey, pub_key, 1, h);
857
+ } else { // expecting compressed format
858
+ hasher_Raw(hasher_pubkey, pub_key, 33, h);
859
+ }
860
+ memcpy(pubkeyhash, h, 20);
861
+ memzero(h, sizeof(h));
862
+ }
863
+
864
+ void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, HasherType hasher_pubkey, uint8_t *addr_raw)
865
+ {
866
+ size_t prefix_len = address_prefix_bytes_len(version);
867
+ address_write_prefix_bytes(version, addr_raw);
868
+ ecdsa_get_pubkeyhash(pub_key, hasher_pubkey, addr_raw + prefix_len);
869
+ }
870
+
871
+ void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, HasherType hasher_pubkey, HasherType hasher_base58, char *addr, int addrsize)
872
+ {
873
+ uint8_t raw[MAX_ADDR_RAW_SIZE];
874
+ size_t prefix_len = address_prefix_bytes_len(version);
875
+ ecdsa_get_address_raw(pub_key, version, hasher_pubkey, raw);
876
+ base58_encode_check(raw, 20 + prefix_len, hasher_base58, addr, addrsize);
877
+ // not as important to clear this one, but we might as well
878
+ memzero(raw, sizeof(raw));
879
+ }
880
+
881
+ void ecdsa_get_address_segwit_p2sh_raw(const uint8_t *pub_key, uint32_t version, HasherType hasher_pubkey, uint8_t *addr_raw)
882
+ {
883
+ uint8_t buf[32 + 2];
884
+ buf[0] = 0; // version byte
885
+ buf[1] = 20; // push 20 bytes
886
+ ecdsa_get_pubkeyhash(pub_key, hasher_pubkey, buf + 2);
887
+ size_t prefix_len = address_prefix_bytes_len(version);
888
+ address_write_prefix_bytes(version, addr_raw);
889
+ hasher_Raw(hasher_pubkey, buf, 22, addr_raw + prefix_len);
890
+ }
891
+
892
+ void ecdsa_get_address_segwit_p2sh(const uint8_t *pub_key, uint32_t version, HasherType hasher_pubkey, HasherType hasher_base58, char *addr, int addrsize)
893
+ {
894
+ uint8_t raw[MAX_ADDR_RAW_SIZE];
895
+ size_t prefix_len = address_prefix_bytes_len(version);
896
+ ecdsa_get_address_segwit_p2sh_raw(pub_key, version, hasher_pubkey, raw);
897
+ base58_encode_check(raw, prefix_len + 20, hasher_base58, addr, addrsize);
898
+ memzero(raw, sizeof(raw));
899
+ }
900
+
901
+ void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, HasherType hasher_base58, char *wif, int wifsize)
902
+ {
903
+ uint8_t wif_raw[MAX_WIF_RAW_SIZE];
904
+ size_t prefix_len = address_prefix_bytes_len(version);
905
+ address_write_prefix_bytes(version, wif_raw);
906
+ memcpy(wif_raw + prefix_len, priv_key, 32);
907
+ wif_raw[prefix_len + 32] = 0x01;
908
+ base58_encode_check(wif_raw, prefix_len + 32 + 1, hasher_base58, wif, wifsize);
909
+ // private keys running around our stack can cause trouble
910
+ memzero(wif_raw, sizeof(wif_raw));
911
+ }
912
+
913
+ int ecdsa_address_decode(const char *addr, uint32_t version, HasherType hasher_base58, uint8_t *out)
914
+ {
915
+ if (!addr) return 0;
916
+ int prefix_len = address_prefix_bytes_len(version);
917
+ return base58_decode_check(addr, hasher_base58, out, 20 + prefix_len) == 20 + prefix_len
918
+ && address_check_prefix(out, version);
919
+ }
920
+
921
+ void compress_coords(const curve_point *cp, uint8_t *compressed) {
922
+ compressed[0] = bn_is_odd(&cp->y) ? 0x03 : 0x02;
923
+ bn_write_be(&cp->x, compressed + 1);
924
+ }
925
+
926
+ void uncompress_coords(const ecdsa_curve *curve, uint8_t odd, const bignum256 *x, bignum256 *y)
927
+ {
928
+ // y^2 = x^3 + a*x + b
929
+ memcpy(y, x, sizeof(bignum256)); // y is x
930
+ bn_multiply(x, y, &curve->prime); // y is x^2
931
+ bn_subi(y, -curve->a, &curve->prime); // y is x^2 + a
932
+ bn_multiply(x, y, &curve->prime); // y is x^3 + ax
933
+ bn_add(y, &curve->b); // y is x^3 + ax + b
934
+ bn_sqrt(y, &curve->prime); // y = sqrt(y)
935
+ if ((odd & 0x01) != (y->val[0] & 1)) {
936
+ bn_subtract(&curve->prime, y, y); // y = -y
937
+ }
938
+ }
939
+
940
+ int ecdsa_read_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, curve_point *pub)
941
+ {
942
+ if (!curve) {
943
+ curve = &secp256k1;
944
+ }
945
+ if (pub_key[0] == 0x04) {
946
+ bn_read_be(pub_key + 1, &(pub->x));
947
+ bn_read_be(pub_key + 33, &(pub->y));
948
+ return ecdsa_validate_pubkey(curve, pub);
949
+ }
950
+ if (pub_key[0] == 0x02 || pub_key[0] == 0x03) { // compute missing y coords
951
+ bn_read_be(pub_key + 1, &(pub->x));
952
+ uncompress_coords(curve, pub_key[0], &(pub->x), &(pub->y));
953
+ return ecdsa_validate_pubkey(curve, pub);
954
+ }
955
+ // error
956
+ return 0;
957
+ }
958
+
959
+ // Verifies that:
960
+ // - pub is not the point at infinity.
961
+ // - pub->x and pub->y are in range [0,p-1].
962
+ // - pub is on the curve.
963
+
964
+ int ecdsa_validate_pubkey(const ecdsa_curve *curve, const curve_point *pub)
965
+ {
966
+ bignum256 y_2, x3_ax_b;
967
+
968
+ if (point_is_infinity(pub)) {
969
+ return 0;
970
+ }
971
+
972
+ if (!bn_is_less(&(pub->x), &curve->prime) || !bn_is_less(&(pub->y), &curve->prime)) {
973
+ return 0;
974
+ }
975
+
976
+ memcpy(&y_2, &(pub->y), sizeof(bignum256));
977
+ memcpy(&x3_ax_b, &(pub->x), sizeof(bignum256));
978
+
979
+ // y^2
980
+ bn_multiply(&(pub->y), &y_2, &curve->prime);
981
+ bn_mod(&y_2, &curve->prime);
982
+
983
+ // x^3 + ax + b
984
+ bn_multiply(&(pub->x), &x3_ax_b, &curve->prime); // x^2
985
+ bn_subi(&x3_ax_b, -curve->a, &curve->prime); // x^2 + a
986
+ bn_multiply(&(pub->x), &x3_ax_b, &curve->prime); // x^3 + ax
987
+ bn_addmod(&x3_ax_b, &curve->b, &curve->prime); // x^3 + ax + b
988
+ bn_mod(&x3_ax_b, &curve->prime);
989
+
990
+ if (!bn_is_equal(&x3_ax_b, &y_2)) {
991
+ return 0;
992
+ }
993
+
994
+ return 1;
995
+ }
996
+
997
+ // uses secp256k1 curve
998
+ // pub_key - 65 bytes uncompressed key
999
+ // signature - 64 bytes signature
1000
+ // msg is a data that was signed
1001
+ // msg_len is the message length
1002
+
1003
+ int ecdsa_verify(const ecdsa_curve *curve, HasherType hasher_sign, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len)
1004
+ {
1005
+ uint8_t hash[32];
1006
+ hasher_Raw(hasher_sign, msg, msg_len, hash);
1007
+ int res = ecdsa_verify_digest(curve, pub_key, sig, hash);
1008
+ memzero(hash, sizeof(hash));
1009
+ return res;
1010
+ }
1011
+
1012
+ // Compute public key from signature and recovery id.
1013
+ // returns 0 if the key is successfully recovered
1014
+ int ecdsa_recover_pub_from_sig(const ecdsa_curve *curve, uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest, int recid)
1015
+ {
1016
+ bignum256 r, s, e;
1017
+ curve_point cp, cp2;
1018
+
1019
+ // read r and s
1020
+ bn_read_be(sig, &r);
1021
+ bn_read_be(sig + 32, &s);
1022
+ if (!bn_is_less(&r, &curve->order) || bn_is_zero(&r)) {
1023
+ return 1;
1024
+ }
1025
+ if (!bn_is_less(&s, &curve->order) || bn_is_zero(&s)) {
1026
+ return 1;
1027
+ }
1028
+ // cp = R = k * G (k is secret nonce when signing)
1029
+ memcpy(&cp.x, &r, sizeof(bignum256));
1030
+ if (recid & 2) {
1031
+ bn_add(&cp.x, &curve->order);
1032
+ if (!bn_is_less(&cp.x, &curve->prime)) {
1033
+ return 1;
1034
+ }
1035
+ }
1036
+ // compute y from x
1037
+ uncompress_coords(curve, recid & 1, &cp.x, &cp.y);
1038
+ if (!ecdsa_validate_pubkey(curve, &cp)) {
1039
+ return 1;
1040
+ }
1041
+ // e = -digest
1042
+ bn_read_be(digest, &e);
1043
+ bn_subtractmod(&curve->order, &e, &e, &curve->order);
1044
+ bn_fast_mod(&e, &curve->order);
1045
+ bn_mod(&e, &curve->order);
1046
+ // r := r^-1
1047
+ bn_inverse(&r, &curve->order);
1048
+ // cp := s * R = s * k *G
1049
+ point_multiply(curve, &s, &cp, &cp);
1050
+ // cp2 := -digest * G
1051
+ scalar_multiply(curve, &e, &cp2);
1052
+ // cp := (s * k - digest) * G = (r*priv) * G = r * Pub
1053
+ point_add(curve, &cp2, &cp);
1054
+ // cp := r^{-1} * r * Pub = Pub
1055
+ point_multiply(curve, &r, &cp, &cp);
1056
+ pub_key[0] = 0x04;
1057
+ bn_write_be(&cp.x, pub_key + 1);
1058
+ bn_write_be(&cp.y, pub_key + 33);
1059
+ return 0;
1060
+ }
1061
+
1062
+ // JuBiter-defined
1063
+ // check recover id, if it's wrong, recover it.
1064
+ // returns 0 if recover succeeded
1065
+ int ecdsa_recover_sig(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest, int *recid)
1066
+ {
1067
+ uint8_t findBy = 0;
1068
+ bool bRecover = false;
1069
+ for (int index=0; index<4; ++index) {
1070
+ uint8_t recover[MAX_ADDR_RAW_SIZE] = {0x00,};
1071
+ if (0 != ecdsa_recover_pub_from_sig(curve, recover, sig, digest, index)) {
1072
+ continue;
1073
+ }
1074
+
1075
+ for (int i=0; i<MAX_ADDR_RAW_SIZE; ++i) {
1076
+ if (recover[i] == pub_key[i]) {
1077
+ if (i == (MAX_ADDR_RAW_SIZE-1)) {
1078
+ bRecover = true;
1079
+ }
1080
+ else {
1081
+ continue;
1082
+ }
1083
+ }
1084
+ else {
1085
+ bRecover = false;
1086
+ break;
1087
+ }
1088
+ }
1089
+ if (bRecover) {
1090
+ findBy = index;
1091
+ break;
1092
+ }
1093
+ }
1094
+ if (!bRecover) {
1095
+ return -1;
1096
+ }
1097
+
1098
+ *recid = findBy;
1099
+
1100
+ return 0;
1101
+ }
1102
+
1103
+ // returns 0 if verification succeeded
1104
+ int ecdsa_verify_digest(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest)
1105
+ {
1106
+ curve_point pub, res;
1107
+ bignum256 r, s, z;
1108
+
1109
+ if (!ecdsa_read_pubkey(curve, pub_key, &pub)) {
1110
+ return 1;
1111
+ }
1112
+
1113
+ bn_read_be(sig, &r);
1114
+ bn_read_be(sig + 32, &s);
1115
+
1116
+ bn_read_be(digest, &z);
1117
+
1118
+ if (bn_is_zero(&r) || bn_is_zero(&s) ||
1119
+ (!bn_is_less(&r, &curve->order)) ||
1120
+ (!bn_is_less(&s, &curve->order))) return 2;
1121
+
1122
+ bn_inverse(&s, &curve->order); // s^-1
1123
+ bn_multiply(&s, &z, &curve->order); // z*s^-1
1124
+ bn_mod(&z, &curve->order);
1125
+ bn_multiply(&r, &s, &curve->order); // r*s^-1
1126
+ bn_mod(&s, &curve->order);
1127
+
1128
+ int result = 0;
1129
+ if (bn_is_zero(&z)) {
1130
+ // our message hashes to zero
1131
+ // I don't expect this to happen any time soon
1132
+ result = 3;
1133
+ } else {
1134
+ scalar_multiply(curve, &z, &res);
1135
+ }
1136
+
1137
+ if (result == 0) {
1138
+ // both pub and res can be infinity, can have y = 0 OR can be equal -> false negative
1139
+ point_multiply(curve, &s, &pub, &pub);
1140
+ point_add(curve, &pub, &res);
1141
+ bn_mod(&(res.x), &curve->order);
1142
+ // signature does not match
1143
+ if (!bn_is_equal(&res.x, &r)) {
1144
+ result = 5;
1145
+ }
1146
+ }
1147
+
1148
+ memzero(&pub, sizeof(pub));
1149
+ memzero(&res, sizeof(res));
1150
+ memzero(&r, sizeof(r));
1151
+ memzero(&s, sizeof(s));
1152
+ memzero(&z, sizeof(z));
1153
+
1154
+ // all OK
1155
+ return result;
1156
+ }
1157
+
1158
+ int ecdsa_sig_to_der(const uint8_t *sig, uint8_t *der)
1159
+ {
1160
+ int i;
1161
+ uint8_t *p = der, *len, *len1, *len2;
1162
+ *p = 0x30; p++; // sequence
1163
+ *p = 0x00; len = p; p++; // len(sequence)
1164
+
1165
+ *p = 0x02; p++; // integer
1166
+ *p = 0x00; len1 = p; p++; // len(integer)
1167
+
1168
+ // process R
1169
+ i = 0;
1170
+ while (sig[i] == 0 && i < 32) { i++; } // skip leading zeroes
1171
+ if (sig[i] >= 0x80) { // put zero in output if MSB set
1172
+ *p = 0x00; p++; *len1 = *len1 + 1;
1173
+ }
1174
+ while (i < 32) { // copy bytes to output
1175
+ *p = sig[i]; p++; *len1 = *len1 + 1; i++;
1176
+ }
1177
+
1178
+ *p = 0x02; p++; // integer
1179
+ *p = 0x00; len2 = p; p++; // len(integer)
1180
+
1181
+ // process S
1182
+ i = 32;
1183
+ while (sig[i] == 0 && i < 64) { i++; } // skip leading zeroes
1184
+ if (sig[i] >= 0x80) { // put zero in output if MSB set
1185
+ *p = 0x00; p++; *len2 = *len2 + 1;
1186
+ }
1187
+ while (i < 64) { // copy bytes to output
1188
+ *p = sig[i]; p++; *len2 = *len2 + 1; i++;
1189
+ }
1190
+
1191
+ *len = *len1 + *len2 + 4;
1192
+ return *len + 2;
1193
+ }
1194
+
1195
+ // JuBiter-defined
1196
+ int der_to_ecdsa_sig(const uint8_t *der, uint8_t *sig)
1197
+ {
1198
+ int i;
1199
+ // uint8_t *p = sig, len, len1, len2, len1Zero, len2Zero;
1200
+ uint8_t *p = sig, len1, len2, len1Zero, len2Zero;
1201
+
1202
+ uint8_t *q = (uint8_t*)der;
1203
+ if (0x30 != *q) { // sequence
1204
+ return 0;
1205
+ }
1206
+ ++q;
1207
+ // len = *q; // len(sequence)
1208
+ q++;
1209
+ if (0x02 != *q) { // integer
1210
+ return 0;
1211
+ }
1212
+ q++;
1213
+ len1 = *q; // len(integer)
1214
+ q++;
1215
+
1216
+ // process R
1217
+ len1Zero = len1 - 32;
1218
+ i = 0;
1219
+ while (i < len1Zero) {
1220
+ q++;
1221
+ i++;
1222
+ }
1223
+ i = len1Zero;
1224
+ while (i < len1) { // copy bytes to output
1225
+ *p = *q;
1226
+ p++;
1227
+
1228
+ q++;
1229
+ i++;
1230
+ }
1231
+
1232
+ if (0x02 != *q) { // integer
1233
+ return 0;
1234
+ }
1235
+ q++;
1236
+ len2 = *q; // len(integer)
1237
+ q++; // integer
1238
+
1239
+ // process S
1240
+ len2Zero = len2 - 32;
1241
+ i = 0;
1242
+ while (i < len2Zero) {
1243
+ q++;
1244
+ i++;
1245
+ }
1246
+ i = len2Zero;
1247
+ while (i < len2) { // copy bytes to output
1248
+ *p = *q;
1249
+ p++;
1250
+
1251
+ q++;
1252
+ i++;
1253
+ }
1254
+
1255
+ return (len1 - len1Zero + len2 - len2Zero);
1256
+ }
1257
+
1258
+ // JuBiter-defined
1259
+ int ecdsa_der_to_sig(const uint8_t *der, uint8_t *sig)
1260
+ {
1261
+ int length;
1262
+ int offset = 2;
1263
+ int delta = 0;
1264
+ if (der[offset + 2] == 0)
1265
+ {
1266
+ length = der[offset + 1] - 1;
1267
+ offset += 3;
1268
+ }
1269
+ else
1270
+ {
1271
+ length = der[offset + 1];
1272
+ offset += 2;
1273
+ }
1274
+ if ((length < 0) || (length > 32))
1275
+ {
1276
+ return 0;
1277
+ }
1278
+ while ((length + delta) < 32)
1279
+ {
1280
+ sig[delta++] = 0;
1281
+ }
1282
+ memmove(sig + delta, der + offset, length);
1283
+
1284
+ delta = 0;
1285
+ offset += length;
1286
+ if (der[offset + 2] == 0)
1287
+ {
1288
+ length = der[offset + 1] - 1;
1289
+ offset += 3;
1290
+ }
1291
+ else
1292
+ {
1293
+ length = der[offset + 1];
1294
+ offset += 2;
1295
+ }
1296
+ if ((length < 0) || (length > 32))
1297
+ {
1298
+ return 0;
1299
+ }
1300
+ while ((length + delta) < 32)
1301
+ {
1302
+ sig[32 + delta++] = 0;
1303
+ }
1304
+ memmove(sig + 32 + delta, der + offset, length);
1305
+
1306
+ return 1;
1307
+ }
1308
+
1309
+ int zil_schnorr_sign(const ecdsa_curve *curve, const uint8_t *priv_key, const uint8_t *digest, uint8_t *sig)
1310
+ {
1311
+ int i;
1312
+ bignum256 k;
1313
+
1314
+ rfc6979_state rng;
1315
+ init_rfc6979(priv_key, digest, &rng);
1316
+
1317
+ for (i = 0; i < 10000; i++) {
1318
+ // generate K deterministically
1319
+ generate_k_rfc6979(&k, &rng);
1320
+ // if k is too big or too small, we don't like it
1321
+ if (bn_is_zero(&k) || !bn_is_less(&k, &curve->order)) {
1322
+ continue;
1323
+ }
1324
+
1325
+ schnorr_sign_pair sign;
1326
+ if (schnorr_sign(curve, priv_key, &k, digest, &sign) != 0) {
1327
+ continue;
1328
+ }
1329
+
1330
+ // we're done
1331
+ memcpy(sig, sign.r, 32);
1332
+ memcpy(sig + 32, sign.s, 32);
1333
+
1334
+ memzero(&k, sizeof(k));
1335
+ memzero(&rng, sizeof(rng));
1336
+ memzero(&sign, sizeof(sign));
1337
+ return 0;
1338
+ }
1339
+
1340
+ // Too many retries without a valid signature
1341
+ // -> fail with an error
1342
+ memzero(&k, sizeof(k));
1343
+ memzero(&rng, sizeof(rng));
1344
+ return -1;
1345
+ }
1346
+
1347
+ int zil_schnorr_verify(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *digest)
1348
+ {
1349
+ schnorr_sign_pair sign;
1350
+
1351
+ memcpy(sign.r, sig, 32);
1352
+ memcpy(sign.s, sig + 32, 32);
1353
+
1354
+ return schnorr_verify(curve, pub_key, digest, &sign);
1355
+ }