react-native-nitro-bsx 0.0.0-stage → 0.2.35

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 (931) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +7 -2
  3. package/android/CMakeLists.txt +28 -0
  4. package/android/build.gradle +99 -0
  5. package/android/src/main/AndroidManifest.xml +35 -0
  6. package/android/src/main/cpp/cpp-adapter.cpp +12 -0
  7. package/android/src/main/java/com/nitrobsx/BSXAlarmReceiver.kt +29 -0
  8. package/android/src/main/java/com/nitrobsx/BSXAlarmScheduler.kt +112 -0
  9. package/android/src/main/java/com/nitrobsx/BSXForegroundService.kt +78 -0
  10. package/android/src/main/java/com/nitrobsx/BSXNotifications.kt +47 -0
  11. package/android/src/main/java/com/nitrobsx/BSXServiceController.kt +90 -0
  12. package/android/src/main/java/com/nitrobsx/BSXWakeWorker.kt +136 -0
  13. package/android/src/main/java/com/nitrobsx/HeadlessJsTaskSupportShim.kt +41 -0
  14. package/android/src/main/java/com/nitrobsx/NitroBSXPackage.kt +28 -0
  15. package/android/src/main/res/drawable/bsx_ic_stat_swap.xml +9 -0
  16. package/cpp/BSXContext.cpp +263 -0
  17. package/cpp/BSXContext.h +49 -0
  18. package/cpp/BSXModule.cpp +21191 -0
  19. package/cpp/BSXModule.h +1523 -0
  20. package/cpp/CMakeLists.txt +463 -0
  21. package/cpp/HybridNitroBSX.cpp +1483 -0
  22. package/cpp/HybridNitroBSX.hpp +173 -0
  23. package/cpp/arith_uint256.cpp +232 -0
  24. package/cpp/arith_uint256.h +277 -0
  25. package/cpp/bsx/event.cpp +48 -0
  26. package/cpp/bsx/event.h +29 -0
  27. package/cpp/bsx/swap.h +293 -0
  28. package/cpp/bsx/types.cpp +486 -0
  29. package/cpp/bsx/types.h +308 -0
  30. package/cpp/bsx_db.cpp +4951 -0
  31. package/cpp/bsx_db.h +465 -0
  32. package/cpp/chain_client.h +143 -0
  33. package/cpp/cmake/GenVersion.cmake +52 -0
  34. package/cpp/coin_config.h.in +12 -0
  35. package/cpp/coins/block_index.cpp +33 -0
  36. package/cpp/coins/block_index.h +35 -0
  37. package/cpp/coins/btc/chainparams.cpp +70 -0
  38. package/cpp/coins/btc/chainparams.h +23 -0
  39. package/cpp/coins/btc/interface.h +30 -0
  40. package/cpp/coins/btc_base/interface.cpp +2200 -0
  41. package/cpp/coins/btc_base/interface.h +225 -0
  42. package/cpp/coins/btc_base/primitives.cpp +560 -0
  43. package/cpp/coins/btc_base/primitives.h +157 -0
  44. package/cpp/coins/btc_base/psbt.cpp +150 -0
  45. package/cpp/coins/btc_base/psbt.h +52 -0
  46. package/cpp/coins/btc_base/script.cpp +304 -0
  47. package/cpp/coins/btc_base/script.h +181 -0
  48. package/cpp/coins/chainparams.cpp +47 -0
  49. package/cpp/coins/chainparams.h +58 -0
  50. package/cpp/coins/firo/chainparams.cpp +52 -0
  51. package/cpp/coins/firo/chainparams.h +24 -0
  52. package/cpp/coins/firo/firo.cpp +118 -0
  53. package/cpp/coins/firo/firo.h +79 -0
  54. package/cpp/coins/firo/primitives.cpp +109 -0
  55. package/cpp/coins/firo/primitives.h +47 -0
  56. package/cpp/coins/interface.cpp +1815 -0
  57. package/cpp/coins/interface.h +554 -0
  58. package/cpp/coins/ltc/chainparams.cpp +70 -0
  59. package/cpp/coins/ltc/chainparams.h +22 -0
  60. package/cpp/coins/ltc/ltc.cpp +69 -0
  61. package/cpp/coins/ltc/ltc.h +82 -0
  62. package/cpp/coins/ltc/primitives.cpp +20 -0
  63. package/cpp/coins/ltc/primitives.h +18 -0
  64. package/cpp/coins/part/chainparams.cpp +89 -0
  65. package/cpp/coins/part/chainparams.h +47 -0
  66. package/cpp/coins/part/part.cpp +905 -0
  67. package/cpp/coins/part/part.h +164 -0
  68. package/cpp/coins/part/primitives.cpp +548 -0
  69. package/cpp/coins/part/primitives.h +187 -0
  70. package/cpp/coins/part/stake.cpp +330 -0
  71. package/cpp/coins/part/stake.h +92 -0
  72. package/cpp/coins/primitives.h +125 -0
  73. package/cpp/coins/registry.cpp +99 -0
  74. package/cpp/coins/registry.h +32 -0
  75. package/cpp/coins/script.h +18 -0
  76. package/cpp/coins/types.cpp +60 -0
  77. package/cpp/coins/types.h +41 -0
  78. package/cpp/coins/xmr/chainparams.cpp +28 -0
  79. package/cpp/coins/xmr/chainparams.h +24 -0
  80. package/cpp/coins/xmr/tx.cpp +670 -0
  81. package/cpp/coins/xmr/tx.h +77 -0
  82. package/cpp/coins/xmr/xmr.cpp +639 -0
  83. package/cpp/coins/xmr/xmr.h +223 -0
  84. package/cpp/compat/byteswap.h +74 -0
  85. package/cpp/compat/endian.h +98 -0
  86. package/cpp/compat/mbedtls/threading_alt.h +20 -0
  87. package/cpp/compat/sockets.h +160 -0
  88. package/cpp/contrib/mbedtls/CMakeLists.txt +566 -0
  89. package/cpp/contrib/mbedtls/LICENSE +553 -0
  90. package/cpp/contrib/mbedtls/cmake/MbedTLSConfig.cmake.in +3 -0
  91. package/cpp/contrib/mbedtls/doxygen/input/doc_encdec.h +52 -0
  92. package/cpp/contrib/mbedtls/doxygen/input/doc_hashing.h +30 -0
  93. package/cpp/contrib/mbedtls/doxygen/input/doc_mainpage.h +52 -0
  94. package/cpp/contrib/mbedtls/doxygen/input/doc_rng.h +27 -0
  95. package/cpp/contrib/mbedtls/doxygen/input/doc_ssltls.h +37 -0
  96. package/cpp/contrib/mbedtls/doxygen/input/doc_tcpip.h +32 -0
  97. package/cpp/contrib/mbedtls/doxygen/input/doc_x509.h +31 -0
  98. package/cpp/contrib/mbedtls/doxygen/mbedtls.doxyfile +56 -0
  99. package/cpp/contrib/mbedtls/framework/CMakeLists.txt +1 -0
  100. package/cpp/contrib/mbedtls/framework/LICENSE +553 -0
  101. package/cpp/contrib/mbedtls/include/CMakeLists.txt +21 -0
  102. package/cpp/contrib/mbedtls/include/mbedtls/build_info.h +93 -0
  103. package/cpp/contrib/mbedtls/include/mbedtls/debug.h +120 -0
  104. package/cpp/contrib/mbedtls/include/mbedtls/error.h +39 -0
  105. package/cpp/contrib/mbedtls/include/mbedtls/mbedtls_config.h +1189 -0
  106. package/cpp/contrib/mbedtls/include/mbedtls/net_sockets.h +299 -0
  107. package/cpp/contrib/mbedtls/include/mbedtls/oid.h +304 -0
  108. package/cpp/contrib/mbedtls/include/mbedtls/pkcs7.h +250 -0
  109. package/cpp/contrib/mbedtls/include/mbedtls/private/config_adjust_ssl.h +84 -0
  110. package/cpp/contrib/mbedtls/include/mbedtls/private/config_adjust_x509.h +35 -0
  111. package/cpp/contrib/mbedtls/include/mbedtls/ssl.h +5622 -0
  112. package/cpp/contrib/mbedtls/include/mbedtls/ssl_cache.h +187 -0
  113. package/cpp/contrib/mbedtls/include/mbedtls/ssl_ciphersuites.h +303 -0
  114. package/cpp/contrib/mbedtls/include/mbedtls/ssl_cookie.h +90 -0
  115. package/cpp/contrib/mbedtls/include/mbedtls/ssl_ticket.h +186 -0
  116. package/cpp/contrib/mbedtls/include/mbedtls/timing.h +105 -0
  117. package/cpp/contrib/mbedtls/include/mbedtls/version.h +69 -0
  118. package/cpp/contrib/mbedtls/include/mbedtls/x509.h +503 -0
  119. package/cpp/contrib/mbedtls/include/mbedtls/x509_crl.h +180 -0
  120. package/cpp/contrib/mbedtls/include/mbedtls/x509_crt.h +1174 -0
  121. package/cpp/contrib/mbedtls/include/mbedtls/x509_csr.h +368 -0
  122. package/cpp/contrib/mbedtls/library/CMakeLists.txt +375 -0
  123. package/cpp/contrib/mbedtls/library/Makefile +334 -0
  124. package/cpp/contrib/mbedtls/library/debug.c +285 -0
  125. package/cpp/contrib/mbedtls/library/debug_internal.h +108 -0
  126. package/cpp/contrib/mbedtls/library/error.c +505 -0
  127. package/cpp/contrib/mbedtls/library/mbedtls_check_config.h +376 -0
  128. package/cpp/contrib/mbedtls/library/mbedtls_common.h +51 -0
  129. package/cpp/contrib/mbedtls/library/mbedtls_config.c +40 -0
  130. package/cpp/contrib/mbedtls/library/mbedtls_config_check_before.h +1769 -0
  131. package/cpp/contrib/mbedtls/library/mbedtls_config_check_final.h +12 -0
  132. package/cpp/contrib/mbedtls/library/mbedtls_config_check_user.h +2436 -0
  133. package/cpp/contrib/mbedtls/library/mbedtls_platform_requirements.h +55 -0
  134. package/cpp/contrib/mbedtls/library/mbedtls_utils.h +23 -0
  135. package/cpp/contrib/mbedtls/library/mps_common.h +181 -0
  136. package/cpp/contrib/mbedtls/library/mps_error.h +89 -0
  137. package/cpp/contrib/mbedtls/library/mps_reader.c +538 -0
  138. package/cpp/contrib/mbedtls/library/mps_reader.h +366 -0
  139. package/cpp/contrib/mbedtls/library/mps_trace.c +112 -0
  140. package/cpp/contrib/mbedtls/library/mps_trace.h +154 -0
  141. package/cpp/contrib/mbedtls/library/net_sockets.c +684 -0
  142. package/cpp/contrib/mbedtls/library/pkcs7.c +784 -0
  143. package/cpp/contrib/mbedtls/library/ssl_cache.c +409 -0
  144. package/cpp/contrib/mbedtls/library/ssl_ciphersuites.c +996 -0
  145. package/cpp/contrib/mbedtls/library/ssl_ciphersuites_internal.h +108 -0
  146. package/cpp/contrib/mbedtls/library/ssl_client.c +1019 -0
  147. package/cpp/contrib/mbedtls/library/ssl_client.h +18 -0
  148. package/cpp/contrib/mbedtls/library/ssl_cookie.c +251 -0
  149. package/cpp/contrib/mbedtls/library/ssl_debug_helpers.h +83 -0
  150. package/cpp/contrib/mbedtls/library/ssl_debug_helpers_generated.c +243 -0
  151. package/cpp/contrib/mbedtls/library/ssl_misc.h +2851 -0
  152. package/cpp/contrib/mbedtls/library/ssl_msg.c +6275 -0
  153. package/cpp/contrib/mbedtls/library/ssl_ticket.c +453 -0
  154. package/cpp/contrib/mbedtls/library/ssl_tls.c +9048 -0
  155. package/cpp/contrib/mbedtls/library/ssl_tls12_client.c +3054 -0
  156. package/cpp/contrib/mbedtls/library/ssl_tls12_server.c +3546 -0
  157. package/cpp/contrib/mbedtls/library/ssl_tls13_client.c +3202 -0
  158. package/cpp/contrib/mbedtls/library/ssl_tls13_generic.c +1773 -0
  159. package/cpp/contrib/mbedtls/library/ssl_tls13_invasive.h +23 -0
  160. package/cpp/contrib/mbedtls/library/ssl_tls13_keys.c +1860 -0
  161. package/cpp/contrib/mbedtls/library/ssl_tls13_keys.h +668 -0
  162. package/cpp/contrib/mbedtls/library/ssl_tls13_server.c +3602 -0
  163. package/cpp/contrib/mbedtls/library/timing.c +76 -0
  164. package/cpp/contrib/mbedtls/library/version.c +30 -0
  165. package/cpp/contrib/mbedtls/library/version_features.c +244 -0
  166. package/cpp/contrib/mbedtls/library/x509.c +1848 -0
  167. package/cpp/contrib/mbedtls/library/x509_create.c +747 -0
  168. package/cpp/contrib/mbedtls/library/x509_crl.c +701 -0
  169. package/cpp/contrib/mbedtls/library/x509_crt.c +3278 -0
  170. package/cpp/contrib/mbedtls/library/x509_csr.c +642 -0
  171. package/cpp/contrib/mbedtls/library/x509_internal.h +97 -0
  172. package/cpp/contrib/mbedtls/library/x509_oid.c +603 -0
  173. package/cpp/contrib/mbedtls/library/x509_oid.h +150 -0
  174. package/cpp/contrib/mbedtls/library/x509write.c +171 -0
  175. package/cpp/contrib/mbedtls/library/x509write_crt.c +637 -0
  176. package/cpp/contrib/mbedtls/library/x509write_csr.c +317 -0
  177. package/cpp/contrib/mbedtls/pkgconfig/CMakeLists.txt +25 -0
  178. package/cpp/contrib/mbedtls/pkgconfig/JoinPaths.cmake +27 -0
  179. package/cpp/contrib/mbedtls/pkgconfig/mbedcrypto.pc.in +10 -0
  180. package/cpp/contrib/mbedtls/pkgconfig/mbedtls.pc.in +11 -0
  181. package/cpp/contrib/mbedtls/pkgconfig/mbedx509.pc.in +11 -0
  182. package/cpp/contrib/mbedtls/tf-psa-crypto/CMakeLists.txt +584 -0
  183. package/cpp/contrib/mbedtls/tf-psa-crypto/LICENSE +552 -0
  184. package/cpp/contrib/mbedtls/tf-psa-crypto/cmake/TF-PSA-CryptoConfig.cmake.in +3 -0
  185. package/cpp/contrib/mbedtls/tf-psa-crypto/core/CMakeLists.txt +193 -0
  186. package/cpp/contrib/mbedtls/tf-psa-crypto/core/alignment.h +700 -0
  187. package/cpp/contrib/mbedtls/tf-psa-crypto/core/check_crypto_config.h +111 -0
  188. package/cpp/contrib/mbedtls/tf-psa-crypto/core/crypto-library.make +57 -0
  189. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto.c +9820 -0
  190. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_client.c +22 -0
  191. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_core.h +972 -0
  192. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_core_common.h +52 -0
  193. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h +3395 -0
  194. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c +247 -0
  195. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_invasive.h +85 -0
  196. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_its.h +131 -0
  197. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random.c +168 -0
  198. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random.h +72 -0
  199. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random_impl.h +215 -0
  200. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_slot_management.c +998 -0
  201. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_slot_management.h +330 -0
  202. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_storage.c +400 -0
  203. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_storage.h +201 -0
  204. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_its_file.c +254 -0
  205. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_util.c +295 -0
  206. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_check_config.h +598 -0
  207. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_common.h +628 -0
  208. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config.c +72 -0
  209. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_before.h +12 -0
  210. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_final.h +12 -0
  211. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_user.h +1220 -0
  212. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_platform_requirements.h +62 -0
  213. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_version.c +28 -0
  214. package/cpp/contrib/mbedtls/tf-psa-crypto/dispatch/psa_crypto_driver_wrappers_no_static.h +31 -0
  215. package/cpp/contrib/mbedtls/tf-psa-crypto/doxygen/input/doc_mainpage.h.in +51 -0
  216. package/cpp/contrib/mbedtls/tf-psa-crypto/doxygen/tfpsacrypto.doxyfile.in +54 -0
  217. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/CMakeLists.txt +142 -0
  218. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/CMakeLists.txt +3 -0
  219. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/aes.h +607 -0
  220. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/aria.h +342 -0
  221. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h +1137 -0
  222. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/block_cipher.h +76 -0
  223. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/camellia.h +302 -0
  224. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ccm.h +523 -0
  225. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/chacha20.h +230 -0
  226. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h +339 -0
  227. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/cipher.h +1173 -0
  228. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/cmac.h +209 -0
  229. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/config_adjust_test_accelerators.h +111 -0
  230. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_adjust_config_enable_builtins.h +820 -0
  231. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_adjust_config_tweak_builtins.h +203 -0
  232. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_composites.h +278 -0
  233. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_key_derivation.h +120 -0
  234. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_primitives.h +136 -0
  235. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ctr_drbg.h +554 -0
  236. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecdsa.h +667 -0
  237. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecjpake.h +296 -0
  238. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecp.h +1452 -0
  239. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/entropy.h +253 -0
  240. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/error_common.h +79 -0
  241. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/gcm.h +382 -0
  242. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/hmac_drbg.h +426 -0
  243. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/md5.h +169 -0
  244. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h +123 -0
  245. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/poly1305.h +165 -0
  246. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ripemd160.h +120 -0
  247. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/rsa.h +821 -0
  248. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha1.h +196 -0
  249. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha256.h +180 -0
  250. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha3.h +189 -0
  251. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha512.h +188 -0
  252. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private_access.h +53 -0
  253. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c +2236 -0
  254. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.c +624 -0
  255. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h +135 -0
  256. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesni.c +846 -0
  257. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesni.h +162 -0
  258. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aria.c +966 -0
  259. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c +2635 -0
  260. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c +1242 -0
  261. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h +885 -0
  262. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core_invasive.h +38 -0
  263. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_internal.h +164 -0
  264. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.c +394 -0
  265. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h +452 -0
  266. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw.c +276 -0
  267. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw.h +416 -0
  268. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw_invasive.h +34 -0
  269. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/block_cipher.c +207 -0
  270. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/block_cipher_internal.h +99 -0
  271. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bn_mul.h +1094 -0
  272. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/camellia.c +1055 -0
  273. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ccm.c +773 -0
  274. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20.c +576 -0
  275. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20_neon.c +294 -0
  276. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20_neon.h +62 -0
  277. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chachapoly.c +479 -0
  278. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c +1126 -0
  279. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_invasive.h +28 -0
  280. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_wrap.c +1937 -0
  281. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_wrap.h +173 -0
  282. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cmac.c +911 -0
  283. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ctr.h +35 -0
  284. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ctr_drbg.c +1017 -0
  285. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c +826 -0
  286. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecjpake.c +1206 -0
  287. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp.c +3478 -0
  288. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c +4798 -0
  289. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h +220 -0
  290. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy.c +677 -0
  291. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy_poll.c +89 -0
  292. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy_poll.h +44 -0
  293. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c +1314 -0
  294. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/hmac_drbg.c +639 -0
  295. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c +419 -0
  296. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/md_psa.h +26 -0
  297. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/poly1305.c +488 -0
  298. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_aead.c +648 -0
  299. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_aead.h +499 -0
  300. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.c +752 -0
  301. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.h +314 -0
  302. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +921 -0
  303. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h +516 -0
  304. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ffdh.c +781 -0
  305. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ffdh.h +131 -0
  306. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.c +458 -0
  307. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.h +211 -0
  308. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_mac.c +495 -0
  309. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_mac.h +264 -0
  310. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_pake.c +568 -0
  311. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_pake.h +159 -0
  312. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c +698 -0
  313. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.h +321 -0
  314. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_xof.c +149 -0
  315. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_xof.h +155 -0
  316. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_util_internal.c +282 -0
  317. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_util_internal.h +132 -0
  318. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c +482 -0
  319. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa.c +2720 -0
  320. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_alt_helpers.c +294 -0
  321. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_alt_helpers.h +185 -0
  322. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_internal.h +172 -0
  323. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_invasive.h +57 -0
  324. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c +473 -0
  325. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c +956 -0
  326. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c +951 -0
  327. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha512.c +1089 -0
  328. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/driver.cmake +54 -0
  329. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/CMakeLists.txt +45 -0
  330. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/Makefile.inc +5 -0
  331. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/README.md +5 -0
  332. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/Hacl_Curve25519.h +25 -0
  333. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib/FStar_UInt128.h +124 -0
  334. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib/FStar_UInt64_FStar_UInt32_FStar_UInt16_FStar_UInt8.h +280 -0
  335. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib.h +29 -0
  336. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/c_endianness.h +206 -0
  337. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/builtin.h +16 -0
  338. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/callconv.h +48 -0
  339. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/compat.h +34 -0
  340. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/debug.h +57 -0
  341. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/target.h +102 -0
  342. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/types.h +61 -0
  343. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/wasmsupport.h +5 -0
  344. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/vs2013/Hacl_Curve25519.h +21 -0
  345. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/x25519.h +48 -0
  346. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/Hacl_Curve25519.c +760 -0
  347. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/Hacl_Curve25519_joined.c +50 -0
  348. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/kremlib/FStar_UInt128_extracted.c +413 -0
  349. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/kremlib/FStar_UInt64_FStar_UInt32_FStar_UInt16_FStar_UInt8.c +100 -0
  350. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/legacy/Hacl_Curve25519.c +805 -0
  351. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/x25519.c +52 -0
  352. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/CMakeLists.txt +36 -0
  353. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/Makefile.inc +5 -0
  354. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/README.md +4 -0
  355. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/README.md +544 -0
  356. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/p256-m.c +1514 -0
  357. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/p256-m.h +135 -0
  358. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m_driver_entrypoints.c +312 -0
  359. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m_driver_entrypoints.h +219 -0
  360. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/CMakeLists.txt +14 -0
  361. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/Makefile.inc +6 -0
  362. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/include/tf-psa-crypto/private/crypto_struct_pqcp.h +52 -0
  363. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/fips202_psa.h +117 -0
  364. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/pqcp-config.h +68 -0
  365. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/psa_crypto_mldsa.c +528 -0
  366. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/psa_crypto_mldsa.h +301 -0
  367. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/wrap_mldsa_native.c +66 -0
  368. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/wrap_mldsa_native.h +37 -0
  369. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/CMakeLists.txt +3 -0
  370. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lmots.c +768 -0
  371. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lmots.h +272 -0
  372. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lms.c +772 -0
  373. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/md.c +1182 -0
  374. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/md_wrap.h +46 -0
  375. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/nist_kw.c +458 -0
  376. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk.c +1464 -0
  377. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_ecc.c +175 -0
  378. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_internal.h +188 -0
  379. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_rsa.c +106 -0
  380. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_wrap.c +667 -0
  381. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_wrap.h +101 -0
  382. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkparse.c +1351 -0
  383. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkwrite.c +574 -0
  384. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkwrite.h +101 -0
  385. package/cpp/contrib/mbedtls/tf-psa-crypto/framework/CMakeLists.txt +1 -0
  386. package/cpp/contrib/mbedtls/tf-psa-crypto/framework/LICENSE +553 -0
  387. package/cpp/contrib/mbedtls/tf-psa-crypto/include/CMakeLists.txt +41 -0
  388. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/asn1.h +623 -0
  389. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/asn1write.h +397 -0
  390. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/base64.h +81 -0
  391. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/compat-3-crypto.h +43 -0
  392. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h +36 -0
  393. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/lms.h +431 -0
  394. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/md.h +538 -0
  395. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/memory_buffer_alloc.h +142 -0
  396. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/nist_kw.h +106 -0
  397. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/pem.h +157 -0
  398. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/pk.h +888 -0
  399. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform.h +513 -0
  400. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform_time.h +79 -0
  401. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform_util.h +197 -0
  402. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/private/pk_private.h +150 -0
  403. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/psa_util.h +199 -0
  404. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/threading.h +412 -0
  405. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto.h +6082 -0
  406. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_compat.h +74 -0
  407. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_config.h +2073 -0
  408. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_common.h +42 -0
  409. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_composites.h +182 -0
  410. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_key_derivation.h +54 -0
  411. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_primitives.h +135 -0
  412. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_random.h +49 -0
  413. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h +2115 -0
  414. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_platform.h +94 -0
  415. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_sizes.h +1301 -0
  416. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h +682 -0
  417. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_types.h +466 -0
  418. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_values.h +2827 -0
  419. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h +193 -0
  420. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_auto_enabled.h +23 -0
  421. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_dependencies.h +58 -0
  422. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_derived.h +135 -0
  423. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_key_pair_types.h +93 -0
  424. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_support.h +211 -0
  425. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_synonyms.h +41 -0
  426. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/version.h +44 -0
  427. package/cpp/contrib/mbedtls/tf-psa-crypto/objlib.cmake +71 -0
  428. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/CMakeLists.txt +15 -0
  429. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/JoinPaths.cmake +27 -0
  430. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/tfpsacrypto.pc.in +10 -0
  431. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/CMakeLists.txt +3 -0
  432. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/memory_buffer_alloc.c +751 -0
  433. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/platform.c +362 -0
  434. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/platform_util.c +428 -0
  435. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/threading.c +311 -0
  436. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/threading_internal.h +75 -0
  437. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/CMakeLists.txt +3 -0
  438. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/asn1parse.c +483 -0
  439. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/asn1write.c +465 -0
  440. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/base64.c +322 -0
  441. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/base64_internal.h +45 -0
  442. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time.c +247 -0
  443. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h +514 -0
  444. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h +540 -0
  445. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/crypto_oid.h +354 -0
  446. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/oid.c +469 -0
  447. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/pem.c +458 -0
  448. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/pkcs5.c +475 -0
  449. package/cpp/contrib/monero/LICENSE +41 -0
  450. package/cpp/contrib/monero/bsx/common/perf_timer.h +15 -0
  451. package/cpp/contrib/monero/bsx/common/threadpool.h +43 -0
  452. package/cpp/contrib/monero/bsx/hex.h +25 -0
  453. package/cpp/contrib/monero/bsx/misc_log_ex.h +61 -0
  454. package/cpp/contrib/monero/bsx/random.cpp +20 -0
  455. package/cpp/contrib/monero/contrib/epee/include/int-util.h +320 -0
  456. package/cpp/contrib/monero/contrib/epee/include/memwipe.h +81 -0
  457. package/cpp/contrib/monero/contrib/epee/include/misc_language.h +109 -0
  458. package/cpp/contrib/monero/contrib/epee/include/mlocker.h +89 -0
  459. package/cpp/contrib/monero/contrib/epee/include/span.h +189 -0
  460. package/cpp/contrib/monero/contrib/epee/include/warnings.h +31 -0
  461. package/cpp/contrib/monero/contrib/epee/src/memwipe.c +115 -0
  462. package/cpp/contrib/monero/contrib/epee/src/mlocker.cpp +197 -0
  463. package/cpp/contrib/monero/src/common/aligned.c +139 -0
  464. package/cpp/contrib/monero/src/common/aligned.h +42 -0
  465. package/cpp/contrib/monero/src/common/base58.cpp +259 -0
  466. package/cpp/contrib/monero/src/common/base58.h +46 -0
  467. package/cpp/contrib/monero/src/common/pod-class.h +43 -0
  468. package/cpp/contrib/monero/src/common/varint.h +138 -0
  469. package/cpp/contrib/monero/src/crypto/chacha.h +96 -0
  470. package/cpp/contrib/monero/src/crypto/crypto-ops-data.c +881 -0
  471. package/cpp/contrib/monero/src/crypto/crypto-ops.c +3891 -0
  472. package/cpp/contrib/monero/src/crypto/crypto-ops.h +167 -0
  473. package/cpp/contrib/monero/src/crypto/crypto.cpp +792 -0
  474. package/cpp/contrib/monero/src/crypto/crypto.h +359 -0
  475. package/cpp/contrib/monero/src/crypto/generic-ops.h +103 -0
  476. package/cpp/contrib/monero/src/crypto/hash-ops.h +105 -0
  477. package/cpp/contrib/monero/src/crypto/hash.c +57 -0
  478. package/cpp/contrib/monero/src/crypto/hash.h +110 -0
  479. package/cpp/contrib/monero/src/crypto/keccak.c +239 -0
  480. package/cpp/contrib/monero/src/crypto/keccak.h +40 -0
  481. package/cpp/contrib/monero/src/crypto/random.h +46 -0
  482. package/cpp/contrib/monero/src/crypto/tree-hash.c +257 -0
  483. package/cpp/contrib/monero/src/crypto/wallet/crypto.h +57 -0
  484. package/cpp/contrib/monero/src/crypto/wallet/ops.h +32 -0
  485. package/cpp/contrib/monero/src/cryptonote_config.h +365 -0
  486. package/cpp/contrib/monero/src/device/device.cpp +105 -0
  487. package/cpp/contrib/monero/src/device/device.hpp +274 -0
  488. package/cpp/contrib/monero/src/device/device_default.cpp +343 -0
  489. package/cpp/contrib/monero/src/device/device_default.hpp +156 -0
  490. package/cpp/contrib/monero/src/ringct/bulletproofs.cc +1097 -0
  491. package/cpp/contrib/monero/src/ringct/bulletproofs.h +51 -0
  492. package/cpp/contrib/monero/src/ringct/bulletproofs_plus.cc +1120 -0
  493. package/cpp/contrib/monero/src/ringct/bulletproofs_plus.h +49 -0
  494. package/cpp/contrib/monero/src/ringct/multiexp.cc +708 -0
  495. package/cpp/contrib/monero/src/ringct/multiexp.h +72 -0
  496. package/cpp/contrib/monero/src/ringct/rctCryptoOps.c +221 -0
  497. package/cpp/contrib/monero/src/ringct/rctCryptoOps.h +37 -0
  498. package/cpp/contrib/monero/src/ringct/rctOps.cpp +727 -0
  499. package/cpp/contrib/monero/src/ringct/rctOps.h +191 -0
  500. package/cpp/contrib/monero/src/ringct/rctSigs.cpp +1652 -0
  501. package/cpp/contrib/monero/src/ringct/rctSigs.h +143 -0
  502. package/cpp/contrib/monero/src/ringct/rctTypes.cpp +338 -0
  503. package/cpp/contrib/monero/src/ringct/rctTypes.h +842 -0
  504. package/cpp/contrib/monero/src/serialization/binary_archive.h +243 -0
  505. package/cpp/contrib/monero/src/serialization/container.h +146 -0
  506. package/cpp/contrib/monero/src/serialization/containers.h +127 -0
  507. package/cpp/contrib/monero/src/serialization/crypto.h +97 -0
  508. package/cpp/contrib/monero/src/serialization/debug_archive.h +53 -0
  509. package/cpp/contrib/monero/src/serialization/json_archive.h +193 -0
  510. package/cpp/contrib/monero/src/serialization/serialization.h +328 -0
  511. package/cpp/contrib/monero/src/serialization/variant.h +50 -0
  512. package/cpp/contrib/nlohmann/LICENSE.MIT +21 -0
  513. package/cpp/contrib/nlohmann/json.hpp +25526 -0
  514. package/cpp/contrib/nlohmann/json_fwd.hpp +187 -0
  515. package/cpp/contrib/notes.txt +96 -0
  516. package/cpp/contrib/secp256k1/CHANGELOG.md +214 -0
  517. package/cpp/contrib/secp256k1/CMakeLists.txt +392 -0
  518. package/cpp/contrib/secp256k1/CMakePresets.json +18 -0
  519. package/cpp/contrib/secp256k1/CONTRIBUTING.md +111 -0
  520. package/cpp/contrib/secp256k1/COPYING +19 -0
  521. package/cpp/contrib/secp256k1/Makefile.am +332 -0
  522. package/cpp/contrib/secp256k1/README.md +176 -0
  523. package/cpp/contrib/secp256k1/SECURITY.md +15 -0
  524. package/cpp/contrib/secp256k1/autogen.sh +3 -0
  525. package/cpp/contrib/secp256k1/ci/ci.sh +147 -0
  526. package/cpp/contrib/secp256k1/ci/linux-debian.Dockerfile +84 -0
  527. package/cpp/contrib/secp256k1/ci/travis.sh +68 -0
  528. package/cpp/contrib/secp256k1/cmake/CheckArm32Assembly.cmake +6 -0
  529. package/cpp/contrib/secp256k1/cmake/CheckMemorySanitizer.cmake +18 -0
  530. package/cpp/contrib/secp256k1/cmake/CheckStringOptionValue.cmake +10 -0
  531. package/cpp/contrib/secp256k1/cmake/CheckX86_64Assembly.cmake +15 -0
  532. package/cpp/contrib/secp256k1/cmake/DiscoverTests.cmake +71 -0
  533. package/cpp/contrib/secp256k1/cmake/FindValgrind.cmake +41 -0
  534. package/cpp/contrib/secp256k1/cmake/GeneratePkgConfigFile.cmake +8 -0
  535. package/cpp/contrib/secp256k1/cmake/TryAppendCFlags.cmake +24 -0
  536. package/cpp/contrib/secp256k1/cmake/arm-linux-gnueabihf.toolchain.cmake +3 -0
  537. package/cpp/contrib/secp256k1/cmake/config.cmake.in +5 -0
  538. package/cpp/contrib/secp256k1/cmake/source_arm32.s +9 -0
  539. package/cpp/contrib/secp256k1/cmake/x86_64-w64-mingw32.toolchain.cmake +3 -0
  540. package/cpp/contrib/secp256k1/configure.ac +590 -0
  541. package/cpp/contrib/secp256k1/contrib/lax_der_parsing.c +148 -0
  542. package/cpp/contrib/secp256k1/contrib/lax_der_parsing.h +97 -0
  543. package/cpp/contrib/secp256k1/contrib/lax_der_privatekey_parsing.c +112 -0
  544. package/cpp/contrib/secp256k1/contrib/lax_der_privatekey_parsing.h +95 -0
  545. package/cpp/contrib/secp256k1/doc/ellswift.md +483 -0
  546. package/cpp/contrib/secp256k1/doc/musig.md +54 -0
  547. package/cpp/contrib/secp256k1/doc/release-process.md +94 -0
  548. package/cpp/contrib/secp256k1/doc/safegcd_implementation.md +819 -0
  549. package/cpp/contrib/secp256k1/examples/CMakeLists.txt +33 -0
  550. package/cpp/contrib/secp256k1/examples/EXAMPLES_COPYING +121 -0
  551. package/cpp/contrib/secp256k1/examples/ecdh.c +121 -0
  552. package/cpp/contrib/secp256k1/examples/ecdsa.c +138 -0
  553. package/cpp/contrib/secp256k1/examples/ellswift.c +122 -0
  554. package/cpp/contrib/secp256k1/examples/examples_util.h +108 -0
  555. package/cpp/contrib/secp256k1/examples/musig.c +261 -0
  556. package/cpp/contrib/secp256k1/examples/schnorr.c +154 -0
  557. package/cpp/contrib/secp256k1/include/secp256k1.h +877 -0
  558. package/cpp/contrib/secp256k1/include/secp256k1_dleag.h +150 -0
  559. package/cpp/contrib/secp256k1/include/secp256k1_ecdh.h +63 -0
  560. package/cpp/contrib/secp256k1/include/secp256k1_ecdsaotves.h +55 -0
  561. package/cpp/contrib/secp256k1/include/secp256k1_ed25519.h +239 -0
  562. package/cpp/contrib/secp256k1/include/secp256k1_ellswift.h +200 -0
  563. package/cpp/contrib/secp256k1/include/secp256k1_extrakeys.h +250 -0
  564. package/cpp/contrib/secp256k1/include/secp256k1_generator.h +106 -0
  565. package/cpp/contrib/secp256k1/include/secp256k1_hash.h +45 -0
  566. package/cpp/contrib/secp256k1/include/secp256k1_musig.h +588 -0
  567. package/cpp/contrib/secp256k1/include/secp256k1_preallocated.h +134 -0
  568. package/cpp/contrib/secp256k1/include/secp256k1_recovery.h +123 -0
  569. package/cpp/contrib/secp256k1/include/secp256k1_schnorrsig.h +190 -0
  570. package/cpp/contrib/secp256k1/include/visibility.h +58 -0
  571. package/cpp/contrib/secp256k1/libsecp256k1.pc.in +12 -0
  572. package/cpp/contrib/secp256k1/sage/gen_exhaustive_groups.sage +156 -0
  573. package/cpp/contrib/secp256k1/sage/gen_split_lambda_constants.sage +123 -0
  574. package/cpp/contrib/secp256k1/sage/group_prover.sage +353 -0
  575. package/cpp/contrib/secp256k1/sage/prove_group_implementations.sage +285 -0
  576. package/cpp/contrib/secp256k1/sage/secp256k1_params.sage +39 -0
  577. package/cpp/contrib/secp256k1/sage/weierstrass_prover.sage +275 -0
  578. package/cpp/contrib/secp256k1/src/CMakeLists.txt +253 -0
  579. package/cpp/contrib/secp256k1/src/asm/field_10x26_arm.s +916 -0
  580. package/cpp/contrib/secp256k1/src/assumptions.h +87 -0
  581. package/cpp/contrib/secp256k1/src/checkmem.h +117 -0
  582. package/cpp/contrib/secp256k1/src/ecdsa.h +21 -0
  583. package/cpp/contrib/secp256k1/src/ecdsa_impl.h +312 -0
  584. package/cpp/contrib/secp256k1/src/eckey.h +28 -0
  585. package/cpp/contrib/secp256k1/src/eckey_impl.h +94 -0
  586. package/cpp/contrib/secp256k1/src/ecmult.h +64 -0
  587. package/cpp/contrib/secp256k1/src/ecmult_compute_table.h +16 -0
  588. package/cpp/contrib/secp256k1/src/ecmult_compute_table_impl.h +49 -0
  589. package/cpp/contrib/secp256k1/src/ecmult_const.h +38 -0
  590. package/cpp/contrib/secp256k1/src/ecmult_const_impl.h +402 -0
  591. package/cpp/contrib/secp256k1/src/ecmult_gen.h +144 -0
  592. package/cpp/contrib/secp256k1/src/ecmult_gen_compute_table.h +14 -0
  593. package/cpp/contrib/secp256k1/src/ecmult_gen_compute_table_impl.h +108 -0
  594. package/cpp/contrib/secp256k1/src/ecmult_gen_impl.h +341 -0
  595. package/cpp/contrib/secp256k1/src/ecmult_impl.h +869 -0
  596. package/cpp/contrib/secp256k1/src/field.h +354 -0
  597. package/cpp/contrib/secp256k1/src/field_10x26.h +57 -0
  598. package/cpp/contrib/secp256k1/src/field_10x26_impl.h +1234 -0
  599. package/cpp/contrib/secp256k1/src/field_5x52.h +62 -0
  600. package/cpp/contrib/secp256k1/src/field_5x52_impl.h +524 -0
  601. package/cpp/contrib/secp256k1/src/field_5x52_int128_impl.h +274 -0
  602. package/cpp/contrib/secp256k1/src/field_impl.h +462 -0
  603. package/cpp/contrib/secp256k1/src/group.h +224 -0
  604. package/cpp/contrib/secp256k1/src/group_impl.h +1024 -0
  605. package/cpp/contrib/secp256k1/src/hash.h +54 -0
  606. package/cpp/contrib/secp256k1/src/hash_impl.h +360 -0
  607. package/cpp/contrib/secp256k1/src/hsort.h +33 -0
  608. package/cpp/contrib/secp256k1/src/hsort_impl.h +125 -0
  609. package/cpp/contrib/secp256k1/src/int128.h +90 -0
  610. package/cpp/contrib/secp256k1/src/int128_impl.h +18 -0
  611. package/cpp/contrib/secp256k1/src/int128_native.h +19 -0
  612. package/cpp/contrib/secp256k1/src/int128_native_impl.h +94 -0
  613. package/cpp/contrib/secp256k1/src/int128_struct.h +14 -0
  614. package/cpp/contrib/secp256k1/src/int128_struct_impl.h +205 -0
  615. package/cpp/contrib/secp256k1/src/modinv32.h +43 -0
  616. package/cpp/contrib/secp256k1/src/modinv32_impl.h +725 -0
  617. package/cpp/contrib/secp256k1/src/modinv64.h +47 -0
  618. package/cpp/contrib/secp256k1/src/modinv64_impl.h +780 -0
  619. package/cpp/contrib/secp256k1/src/modules/dleag/Makefile.am.include +3 -0
  620. package/cpp/contrib/secp256k1/src/modules/dleag/main_impl.h +1073 -0
  621. package/cpp/contrib/secp256k1/src/modules/dleag/tests_impl.h +68 -0
  622. package/cpp/contrib/secp256k1/src/modules/ecdh/Makefile.am.include +5 -0
  623. package/cpp/contrib/secp256k1/src/modules/ecdh/bench_impl.h +54 -0
  624. package/cpp/contrib/secp256k1/src/modules/ecdh/main_impl.h +79 -0
  625. package/cpp/contrib/secp256k1/src/modules/ecdh/tests_impl.h +218 -0
  626. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/Makefile.am.include +3 -0
  627. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/main_impl.h +365 -0
  628. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/tests_impl.h +49 -0
  629. package/cpp/contrib/secp256k1/src/modules/ed25519/Makefile.am.include +16 -0
  630. package/cpp/contrib/secp256k1/src/modules/ed25519/common.h +68 -0
  631. package/cpp/contrib/secp256k1/src/modules/ed25519/core_ed25519.h +229 -0
  632. package/cpp/contrib/secp256k1/src/modules/ed25519/main_impl.h +98 -0
  633. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/common.h +21 -0
  634. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10.h +2698 -0
  635. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10_fe_25_5.h +1056 -0
  636. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10_fe_51.h +518 -0
  637. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/base.h +1344 -0
  638. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/base2.h +40 -0
  639. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/constants.h +40 -0
  640. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/fe.h +220 -0
  641. package/cpp/contrib/secp256k1/src/modules/ed25519/scalarmult_ed25519_ref10.h +417 -0
  642. package/cpp/contrib/secp256k1/src/modules/ed25519/sha512/hash_sha512.h +14 -0
  643. package/cpp/contrib/secp256k1/src/modules/ed25519/sha512/hash_sha512_cp.h +287 -0
  644. package/cpp/contrib/secp256k1/src/modules/ed25519/tests_impl.h +109 -0
  645. package/cpp/contrib/secp256k1/src/modules/ed25519/utils.h +97 -0
  646. package/cpp/contrib/secp256k1/src/modules/ellswift/Makefile.am.include +5 -0
  647. package/cpp/contrib/secp256k1/src/modules/ellswift/bench_impl.h +106 -0
  648. package/cpp/contrib/secp256k1/src/modules/ellswift/main_impl.h +581 -0
  649. package/cpp/contrib/secp256k1/src/modules/ellswift/tests_exhaustive_impl.h +39 -0
  650. package/cpp/contrib/secp256k1/src/modules/ellswift/tests_impl.h +544 -0
  651. package/cpp/contrib/secp256k1/src/modules/extrakeys/Makefile.am.include +4 -0
  652. package/cpp/contrib/secp256k1/src/modules/extrakeys/main_impl.h +285 -0
  653. package/cpp/contrib/secp256k1/src/modules/extrakeys/tests_exhaustive_impl.h +68 -0
  654. package/cpp/contrib/secp256k1/src/modules/extrakeys/tests_impl.h +484 -0
  655. package/cpp/contrib/secp256k1/src/modules/generator/Makefile.am.include +9 -0
  656. package/cpp/contrib/secp256k1/src/modules/generator/main_impl.h +271 -0
  657. package/cpp/contrib/secp256k1/src/modules/generator/tests_impl.h +227 -0
  658. package/cpp/contrib/secp256k1/src/modules/musig/Makefile.am.include +8 -0
  659. package/cpp/contrib/secp256k1/src/modules/musig/keyagg.h +32 -0
  660. package/cpp/contrib/secp256k1/src/modules/musig/keyagg_impl.h +275 -0
  661. package/cpp/contrib/secp256k1/src/modules/musig/main_impl.h +12 -0
  662. package/cpp/contrib/secp256k1/src/modules/musig/session.h +24 -0
  663. package/cpp/contrib/secp256k1/src/modules/musig/session_impl.h +795 -0
  664. package/cpp/contrib/secp256k1/src/modules/musig/tests_impl.h +1161 -0
  665. package/cpp/contrib/secp256k1/src/modules/musig/vectors.h +346 -0
  666. package/cpp/contrib/secp256k1/src/modules/recovery/Makefile.am.include +5 -0
  667. package/cpp/contrib/secp256k1/src/modules/recovery/bench_impl.h +62 -0
  668. package/cpp/contrib/secp256k1/src/modules/recovery/main_impl.h +159 -0
  669. package/cpp/contrib/secp256k1/src/modules/recovery/tests_exhaustive_impl.h +148 -0
  670. package/cpp/contrib/secp256k1/src/modules/recovery/tests_impl.h +339 -0
  671. package/cpp/contrib/secp256k1/src/modules/schnorrsig/Makefile.am.include +5 -0
  672. package/cpp/contrib/secp256k1/src/modules/schnorrsig/bench_impl.h +104 -0
  673. package/cpp/contrib/secp256k1/src/modules/schnorrsig/main_impl.h +263 -0
  674. package/cpp/contrib/secp256k1/src/modules/schnorrsig/tests_exhaustive_impl.h +214 -0
  675. package/cpp/contrib/secp256k1/src/modules/schnorrsig/tests_impl.h +1010 -0
  676. package/cpp/contrib/secp256k1/src/precompute_ecmult.c +91 -0
  677. package/cpp/contrib/secp256k1/src/precompute_ecmult_gen.c +101 -0
  678. package/cpp/contrib/secp256k1/src/precomputed_ecmult.c +16456 -0
  679. package/cpp/contrib/secp256k1/src/precomputed_ecmult.h +40 -0
  680. package/cpp/contrib/secp256k1/src/precomputed_ecmult_gen.c +1779 -0
  681. package/cpp/contrib/secp256k1/src/precomputed_ecmult_gen.h +28 -0
  682. package/cpp/contrib/secp256k1/src/scalar.h +108 -0
  683. package/cpp/contrib/secp256k1/src/scalar_4x64.h +19 -0
  684. package/cpp/contrib/secp256k1/src/scalar_4x64_impl.h +1010 -0
  685. package/cpp/contrib/secp256k1/src/scalar_8x32.h +19 -0
  686. package/cpp/contrib/secp256k1/src/scalar_8x32_impl.h +830 -0
  687. package/cpp/contrib/secp256k1/src/scalar_impl.h +321 -0
  688. package/cpp/contrib/secp256k1/src/scalar_low.h +24 -0
  689. package/cpp/contrib/secp256k1/src/scalar_low_impl.h +215 -0
  690. package/cpp/contrib/secp256k1/src/scratch.h +44 -0
  691. package/cpp/contrib/secp256k1/src/scratch_impl.h +99 -0
  692. package/cpp/contrib/secp256k1/src/secp256k1.c +896 -0
  693. package/cpp/contrib/secp256k1/src/selftest.h +35 -0
  694. package/cpp/contrib/secp256k1/src/testrand.h +45 -0
  695. package/cpp/contrib/secp256k1/src/testrand_impl.h +162 -0
  696. package/cpp/contrib/secp256k1/src/testutil.h +161 -0
  697. package/cpp/contrib/secp256k1/src/unit_test.c +479 -0
  698. package/cpp/contrib/secp256k1/src/unit_test.h +147 -0
  699. package/cpp/contrib/secp256k1/src/util.h +469 -0
  700. package/cpp/contrib/secp256k1/src/util_local_visibility.h +12 -0
  701. package/cpp/contrib/secp256k1/tools/check-abi.sh +67 -0
  702. package/cpp/contrib/secp256k1/tools/symbol-check.py +72 -0
  703. package/cpp/contrib/secp256k1/tools/test_vectors_musig2_generate.py +656 -0
  704. package/cpp/contrib/secp256k1/tools/tests_wycheproof_generate_ecdh.py +166 -0
  705. package/cpp/contrib/secp256k1/tools/tests_wycheproof_generate_ecdsa.py +111 -0
  706. package/cpp/contrib/secp256k1/tools/wycheproof_utils.py +12 -0
  707. package/cpp/contrib/secp256k1/wasm/test_module.html +320 -0
  708. package/cpp/contrib/secp256k1/wasm/wrap.cpp +331 -0
  709. package/cpp/contrib/sqlite3.h +14349 -0
  710. package/cpp/contrib/sqlite3mc_amalgamation.c +369890 -0
  711. package/cpp/contrib/sqlite3mc_amalgamation.h +15548 -0
  712. package/cpp/contrib/unilib/uninorms.cpp +393 -0
  713. package/cpp/contrib/unilib/uninorms.h +56 -0
  714. package/cpp/contrib/unilib/utf8.cpp +78 -0
  715. package/cpp/contrib/unilib/utf8.h +260 -0
  716. package/cpp/contrib/update_mbedtls.sh +85 -0
  717. package/cpp/crypto/aes.cpp +141 -0
  718. package/cpp/crypto/aes.h +67 -0
  719. package/cpp/crypto/chacha20.cpp +342 -0
  720. package/cpp/crypto/chacha20.h +82 -0
  721. package/cpp/crypto/chacha20poly1305.cpp +91 -0
  722. package/cpp/crypto/chacha20poly1305.h +46 -0
  723. package/cpp/crypto/common.h +85 -0
  724. package/cpp/crypto/ctaes/COPYING +21 -0
  725. package/cpp/crypto/ctaes/README.md +41 -0
  726. package/cpp/crypto/ctaes/bench.c +170 -0
  727. package/cpp/crypto/ctaes/ctaes.c +556 -0
  728. package/cpp/crypto/ctaes/ctaes.h +41 -0
  729. package/cpp/crypto/ctaes/test.c +110 -0
  730. package/cpp/crypto/extkey.cpp +257 -0
  731. package/cpp/crypto/extkey.h +66 -0
  732. package/cpp/crypto/hmac_sha512.cpp +37 -0
  733. package/cpp/crypto/hmac_sha512.h +32 -0
  734. package/cpp/crypto/merkle.cpp +47 -0
  735. package/cpp/crypto/merkle.h +20 -0
  736. package/cpp/crypto/poly1305.cpp +223 -0
  737. package/cpp/crypto/poly1305.h +59 -0
  738. package/cpp/crypto/ripemd160.cpp +292 -0
  739. package/cpp/crypto/ripemd160.h +28 -0
  740. package/cpp/crypto/scrypt-sse2.cpp +141 -0
  741. package/cpp/crypto/scrypt.cpp +336 -0
  742. package/cpp/crypto/scrypt.h +49 -0
  743. package/cpp/crypto/sealed_key.cpp +62 -0
  744. package/cpp/crypto/sealed_key.h +34 -0
  745. package/cpp/crypto/secure_bytes.h +16 -0
  746. package/cpp/crypto/sha512.cpp +206 -0
  747. package/cpp/crypto/sha512.h +29 -0
  748. package/cpp/crypto/util.cpp +77 -0
  749. package/cpp/crypto/util.h +32 -0
  750. package/cpp/debug_rpc.cpp +703 -0
  751. package/cpp/debug_rpc.h +63 -0
  752. package/cpp/electrum.cpp +931 -0
  753. package/cpp/electrum.h +169 -0
  754. package/cpp/engine/xmr_engine.cpp +463 -0
  755. package/cpp/engine/xmr_engine.h +84 -0
  756. package/cpp/engine/xmr_engine_c.cpp +141 -0
  757. package/cpp/engine/xmr_engine_c.h +35 -0
  758. package/cpp/fees.cpp +270 -0
  759. package/cpp/fees.h +60 -0
  760. package/cpp/height_search.cpp +64 -0
  761. package/cpp/height_search.h +27 -0
  762. package/cpp/main.cpp +146 -0
  763. package/cpp/messages_npb.cpp +701 -0
  764. package/cpp/messages_npb.h +667 -0
  765. package/cpp/mnemonic/mnemonic.cpp +715 -0
  766. package/cpp/mnemonic/mnemonic.h +52 -0
  767. package/cpp/mnemonic/wordlists/english.h +1103 -0
  768. package/cpp/mnemonic/wordlists/japanese.h +2212 -0
  769. package/cpp/network/bridge.cpp +232 -0
  770. package/cpp/network/bridge.h +115 -0
  771. package/cpp/network/ca_roots.cpp +114 -0
  772. package/cpp/network/ca_roots.h +12 -0
  773. package/cpp/network/http_client.cpp +261 -0
  774. package/cpp/network/http_client.h +33 -0
  775. package/cpp/network/pem.cpp +84 -0
  776. package/cpp/network/pem.h +22 -0
  777. package/cpp/network/psa_user_config.h +15 -0
  778. package/cpp/network/socks5.cpp +180 -0
  779. package/cpp/network/socks5.h +26 -0
  780. package/cpp/network/tls_transport.cpp +502 -0
  781. package/cpp/network/tls_transport.h +62 -0
  782. package/cpp/network/transport.cpp +169 -0
  783. package/cpp/network/transport.h +115 -0
  784. package/cpp/network/url.cpp +88 -0
  785. package/cpp/network/url.h +25 -0
  786. package/cpp/network/websocket.cpp +253 -0
  787. package/cpp/network/websocket.h +68 -0
  788. package/cpp/network/ws_transport.cpp +361 -0
  789. package/cpp/network/ws_transport.h +72 -0
  790. package/cpp/node_client.cpp +1196 -0
  791. package/cpp/node_client.h +209 -0
  792. package/cpp/node_scan.h +35 -0
  793. package/cpp/schema.cpp +1249 -0
  794. package/cpp/schema.h +132 -0
  795. package/cpp/smsg.cpp +453 -0
  796. package/cpp/smsg.h +43 -0
  797. package/cpp/support/allocators/secure.h +85 -0
  798. package/cpp/support/cleanse.cpp +35 -0
  799. package/cpp/support/cleanse.h +15 -0
  800. package/cpp/support/coop.cpp +319 -0
  801. package/cpp/support/coop.h +157 -0
  802. package/cpp/support/lockedpool.cpp +406 -0
  803. package/cpp/support/lockedpool.h +240 -0
  804. package/cpp/uint256.cpp +62 -0
  805. package/cpp/uint256.h +121 -0
  806. package/cpp/util/base58.cpp +181 -0
  807. package/cpp/util/base58.h +45 -0
  808. package/cpp/util/bech32.cpp +578 -0
  809. package/cpp/util/bech32.h +96 -0
  810. package/cpp/util/debug.cpp +23 -0
  811. package/cpp/util/debug.h +12 -0
  812. package/cpp/util/int.cpp +118 -0
  813. package/cpp/util/int.h +18 -0
  814. package/cpp/util/json.h +26 -0
  815. package/cpp/util/log.cpp +137 -0
  816. package/cpp/util/log.h +37 -0
  817. package/cpp/util/mutex.h +89 -0
  818. package/cpp/util/platform.cpp +90 -0
  819. package/cpp/util/platform.h +17 -0
  820. package/cpp/util/random.cpp +136 -0
  821. package/cpp/util/random.h +28 -0
  822. package/cpp/util/rpc.cpp +192 -0
  823. package/cpp/util/rpc.h +454 -0
  824. package/cpp/util/str.cpp +254 -0
  825. package/cpp/util/str.h +54 -0
  826. package/cpp/util/thread.cpp +17 -0
  827. package/cpp/util/thread.h +12 -0
  828. package/cpp/util/time.cpp +30 -0
  829. package/cpp/util/time.h +30 -0
  830. package/cpp/util/tinyformat.h +1163 -0
  831. package/cpp/util/vector.h +83 -0
  832. package/cpp/version.h.in +12 -0
  833. package/cpp/view_key_chain.h +283 -0
  834. package/cpp/wallet/account_wallet.cpp +668 -0
  835. package/cpp/wallet/account_wallet.h +136 -0
  836. package/cpp/wallet/base_wallet.cpp +302 -0
  837. package/cpp/wallet/base_wallet.h +129 -0
  838. package/cpp/wallet/debug_wallet.cpp +40 -0
  839. package/cpp/wallet/debug_wallet.h +36 -0
  840. package/cpp/wallet/encrypted_seed.cpp +96 -0
  841. package/cpp/wallet/encrypted_seed.h +41 -0
  842. package/cpp/wallet/host_wallet.cpp +85 -0
  843. package/cpp/wallet/host_wallet.h +51 -0
  844. package/cpp/wallet/native_wallet.cpp +34 -0
  845. package/cpp/wallet/native_wallet.h +28 -0
  846. package/cpp/wallet/xmr/account_wallet.cpp +803 -0
  847. package/cpp/wallet/xmr/account_wallet.h +139 -0
  848. package/cpp/wallet/xmr/builder.cpp +568 -0
  849. package/cpp/wallet/xmr/builder.h +65 -0
  850. package/cpp/wallet/xmr/chain.cpp +1437 -0
  851. package/cpp/wallet/xmr/chain.h +213 -0
  852. package/cpp/wallet/xmr/daemon.cpp +583 -0
  853. package/cpp/wallet/xmr/daemon.h +153 -0
  854. package/cpp/wallet/xmr/decoys.cpp +197 -0
  855. package/cpp/wallet/xmr/decoys.h +64 -0
  856. package/cpp/wallet/xmr/epee.cpp +448 -0
  857. package/cpp/wallet/xmr/epee.h +25 -0
  858. package/cpp/wallet/xmr/fee.cpp +169 -0
  859. package/cpp/wallet/xmr/fee.h +61 -0
  860. package/cpp/wallet/xmr/reserve_proof.cpp +169 -0
  861. package/cpp/wallet/xmr/reserve_proof.h +24 -0
  862. package/cpp/wallet/xmr/scanner.cpp +860 -0
  863. package/cpp/wallet/xmr/scanner.h +239 -0
  864. package/lib/commonjs/NativeNitroBSX.js +128 -0
  865. package/lib/commonjs/NativeNitroBSX.js.map +1 -0
  866. package/lib/commonjs/format.js +21 -0
  867. package/lib/commonjs/format.js.map +1 -0
  868. package/lib/commonjs/index.js +50 -0
  869. package/lib/commonjs/index.js.map +1 -0
  870. package/lib/commonjs/service.js +75 -0
  871. package/lib/commonjs/service.js.map +1 -0
  872. package/lib/commonjs/specs/NitroBSX.nitro.js +6 -0
  873. package/lib/commonjs/specs/NitroBSX.nitro.js.map +1 -0
  874. package/lib/module/NativeNitroBSX.js +124 -0
  875. package/lib/module/NativeNitroBSX.js.map +1 -0
  876. package/lib/module/format.js +15 -0
  877. package/lib/module/format.js.map +1 -0
  878. package/lib/module/index.js +7 -0
  879. package/lib/module/index.js.map +1 -0
  880. package/lib/module/service.js +66 -0
  881. package/lib/module/service.js.map +1 -0
  882. package/lib/module/specs/NitroBSX.nitro.js +4 -0
  883. package/lib/module/specs/NitroBSX.nitro.js.map +1 -0
  884. package/lib/typescript/commonjs/package.json +1 -0
  885. package/lib/typescript/commonjs/src/NativeNitroBSX.d.ts +94 -0
  886. package/lib/typescript/commonjs/src/NativeNitroBSX.d.ts.map +1 -0
  887. package/lib/typescript/commonjs/src/format.d.ts +3 -0
  888. package/lib/typescript/commonjs/src/format.d.ts.map +1 -0
  889. package/lib/typescript/commonjs/src/index.d.ts +5 -0
  890. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  891. package/lib/typescript/commonjs/src/service.d.ts +28 -0
  892. package/lib/typescript/commonjs/src/service.d.ts.map +1 -0
  893. package/lib/typescript/commonjs/src/specs/NitroBSX.nitro.d.ts +251 -0
  894. package/lib/typescript/commonjs/src/specs/NitroBSX.nitro.d.ts.map +1 -0
  895. package/lib/typescript/module/package.json +1 -0
  896. package/lib/typescript/module/src/NativeNitroBSX.d.ts +94 -0
  897. package/lib/typescript/module/src/NativeNitroBSX.d.ts.map +1 -0
  898. package/lib/typescript/module/src/format.d.ts +3 -0
  899. package/lib/typescript/module/src/format.d.ts.map +1 -0
  900. package/lib/typescript/module/src/index.d.ts +5 -0
  901. package/lib/typescript/module/src/index.d.ts.map +1 -0
  902. package/lib/typescript/module/src/service.d.ts +28 -0
  903. package/lib/typescript/module/src/service.d.ts.map +1 -0
  904. package/lib/typescript/module/src/specs/NitroBSX.nitro.d.ts +251 -0
  905. package/lib/typescript/module/src/specs/NitroBSX.nitro.d.ts.map +1 -0
  906. package/nitro.json +19 -0
  907. package/nitrogen/generated/android/NitroBSX+autolinking.cmake +81 -0
  908. package/nitrogen/generated/android/NitroBSX+autolinking.gradle +27 -0
  909. package/nitrogen/generated/android/NitroBSXOnLoad.cpp +49 -0
  910. package/nitrogen/generated/android/NitroBSXOnLoad.hpp +34 -0
  911. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrobsx/NitroBSXOnLoad.kt +35 -0
  912. package/nitrogen/generated/ios/NitroBSX+autolinking.rb +62 -0
  913. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Bridge.cpp +17 -0
  914. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Bridge.hpp +27 -0
  915. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Umbrella.hpp +38 -0
  916. package/nitrogen/generated/ios/NitroBSXAutolinking.mm +35 -0
  917. package/nitrogen/generated/ios/NitroBSXAutolinking.swift +16 -0
  918. package/nitrogen/generated/shared/c++/HybridNitroBSXSpec.cpp +91 -0
  919. package/nitrogen/generated/shared/c++/HybridNitroBSXSpec.hpp +142 -0
  920. package/nitrogen/generated/shared/c++/NotificationPair.hpp +87 -0
  921. package/nitrogen/generated/shared/c++/SignOutcome.hpp +80 -0
  922. package/nitrogen/generated/shared/c++/SignTxResult.hpp +89 -0
  923. package/package.json +172 -4
  924. package/react-native-nitro-bsx.podspec +33 -0
  925. package/react-native.config.js +26 -0
  926. package/scripts/build-ios.sh +49 -0
  927. package/src/NativeNitroBSX.ts +203 -0
  928. package/src/format.ts +14 -0
  929. package/src/index.ts +4 -0
  930. package/src/service.ts +77 -0
  931. package/src/specs/NitroBSX.nitro.ts +300 -0
@@ -0,0 +1,3478 @@
1
+ /*
2
+ * Elliptic curves over GF(p): generic functions
3
+ *
4
+ * Copyright The Mbed TLS Contributors
5
+ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
+ */
7
+
8
+ /*
9
+ * References:
10
+ *
11
+ * SEC1 https://www.secg.org/sec1-v2.pdf
12
+ * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
13
+ * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
14
+ * RFC 4492 for the related TLS structures and constants
15
+ * - https://www.rfc-editor.org/rfc/rfc4492
16
+ * RFC 7748 for the Curve448 and Curve25519 curve definitions
17
+ * - https://www.rfc-editor.org/rfc/rfc7748
18
+ *
19
+ * [Curve25519] https://cr.yp.to/ecdh/curve25519-20060209.pdf
20
+ *
21
+ * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
22
+ * for elliptic curve cryptosystems. In : Cryptographic Hardware and
23
+ * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
24
+ * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
25
+ *
26
+ * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
27
+ * render ECC resistant against Side Channel Attacks. IACR Cryptology
28
+ * ePrint Archive, 2004, vol. 2004, p. 342.
29
+ * <http://eprint.iacr.org/2004/342.pdf>
30
+ */
31
+
32
+ #include "tf_psa_crypto_common.h"
33
+
34
+ #if defined(MBEDTLS_ECP_LIGHT)
35
+
36
+ #include "mbedtls/private/ecp.h"
37
+ #include "mbedtls/threading.h"
38
+ #include "mbedtls/platform_util.h"
39
+ #include "mbedtls/private/error_common.h"
40
+
41
+ #include "bn_mul.h"
42
+ #include "bignum_internal.h"
43
+ #include "bignum_core.h"
44
+ #include "ecp_invasive.h"
45
+
46
+ #include <string.h>
47
+
48
+ #include "mbedtls/platform.h"
49
+
50
+ #if defined(MBEDTLS_SELF_TEST)
51
+ /*
52
+ * Counts of point addition and doubling, and field multiplications.
53
+ * Used to test resistance of point multiplication to simple timing attacks.
54
+ */
55
+ #if defined(MBEDTLS_ECP_C)
56
+ static unsigned long add_count, dbl_count;
57
+ #endif /* MBEDTLS_ECP_C */
58
+ static unsigned long mul_count;
59
+ #endif
60
+
61
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
62
+ /*
63
+ * Maximum number of "basic operations" to be done in a row.
64
+ *
65
+ * Default value 0 means that ECC operations will not yield.
66
+ * Note that regardless of the value of ecp_max_ops, always at
67
+ * least one step is performed before yielding.
68
+ *
69
+ * Setting ecp_max_ops=1 can be suitable for testing purposes
70
+ * as it will interrupt computation at all possible points.
71
+ */
72
+ static unsigned ecp_max_ops = 0;
73
+
74
+ /*
75
+ * Set ecp_max_ops
76
+ */
77
+ void mbedtls_ecp_set_max_ops(unsigned max_ops)
78
+ {
79
+ ecp_max_ops = max_ops;
80
+ }
81
+
82
+ /*
83
+ * Check if restart is enabled
84
+ */
85
+ int mbedtls_ecp_restart_is_enabled(void)
86
+ {
87
+ return ecp_max_ops != 0;
88
+ }
89
+
90
+ /*
91
+ * Restart sub-context for ecp_mul_comb()
92
+ */
93
+ struct mbedtls_ecp_restart_mul {
94
+ mbedtls_ecp_point R; /* current intermediate result */
95
+ size_t i; /* current index in various loops, 0 outside */
96
+ mbedtls_ecp_point *T; /* table for precomputed points */
97
+ unsigned char T_size; /* number of points in table T */
98
+ enum { /* what were we doing last time we returned? */
99
+ ecp_rsm_init = 0, /* nothing so far, dummy initial state */
100
+ ecp_rsm_pre_dbl, /* precompute 2^n multiples */
101
+ ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
102
+ ecp_rsm_pre_add, /* precompute remaining points by adding */
103
+ ecp_rsm_pre_norm_add, /* normalize all precomputed points */
104
+ ecp_rsm_comb_core, /* ecp_mul_comb_core() */
105
+ ecp_rsm_final_norm, /* do the final normalization */
106
+ } state;
107
+ };
108
+
109
+ /*
110
+ * Init restart_mul sub-context
111
+ */
112
+ static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx)
113
+ {
114
+ mbedtls_ecp_point_init(&ctx->R);
115
+ ctx->i = 0;
116
+ ctx->T = NULL;
117
+ ctx->T_size = 0;
118
+ ctx->state = ecp_rsm_init;
119
+ }
120
+
121
+ /*
122
+ * Free the components of a restart_mul sub-context
123
+ */
124
+ static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
125
+ {
126
+ unsigned char i;
127
+
128
+ if (ctx == NULL) {
129
+ return;
130
+ }
131
+
132
+ mbedtls_ecp_point_free(&ctx->R);
133
+
134
+ if (ctx->T != NULL) {
135
+ for (i = 0; i < ctx->T_size; i++) {
136
+ mbedtls_ecp_point_free(ctx->T + i);
137
+ }
138
+ mbedtls_free(ctx->T);
139
+ }
140
+
141
+ ecp_restart_rsm_init(ctx);
142
+ }
143
+
144
+ /*
145
+ * Restart context for ecp_muladd()
146
+ */
147
+ struct mbedtls_ecp_restart_muladd {
148
+ mbedtls_ecp_point mP; /* mP value */
149
+ mbedtls_ecp_point R; /* R intermediate result */
150
+ enum { /* what should we do next? */
151
+ ecp_rsma_mul1 = 0, /* first multiplication */
152
+ ecp_rsma_mul2, /* second multiplication */
153
+ ecp_rsma_add, /* addition */
154
+ ecp_rsma_norm, /* normalization */
155
+ } state;
156
+ };
157
+
158
+ /*
159
+ * Init restart_muladd sub-context
160
+ */
161
+ static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx)
162
+ {
163
+ mbedtls_ecp_point_init(&ctx->mP);
164
+ mbedtls_ecp_point_init(&ctx->R);
165
+ ctx->state = ecp_rsma_mul1;
166
+ }
167
+
168
+ /*
169
+ * Free the components of a restart_muladd sub-context
170
+ */
171
+ static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
172
+ {
173
+ if (ctx == NULL) {
174
+ return;
175
+ }
176
+
177
+ mbedtls_ecp_point_free(&ctx->mP);
178
+ mbedtls_ecp_point_free(&ctx->R);
179
+
180
+ ecp_restart_ma_init(ctx);
181
+ }
182
+
183
+ /*
184
+ * Initialize a restart context
185
+ */
186
+ void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
187
+ {
188
+ ctx->ops_done = 0;
189
+ ctx->depth = 0;
190
+ ctx->rsm = NULL;
191
+ ctx->ma = NULL;
192
+ }
193
+
194
+ /*
195
+ * Free the components of a restart context
196
+ */
197
+ void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
198
+ {
199
+ if (ctx == NULL) {
200
+ return;
201
+ }
202
+
203
+ ecp_restart_rsm_free(ctx->rsm);
204
+ mbedtls_free(ctx->rsm);
205
+
206
+ ecp_restart_ma_free(ctx->ma);
207
+ mbedtls_free(ctx->ma);
208
+
209
+ mbedtls_ecp_restart_init(ctx);
210
+ }
211
+
212
+ /*
213
+ * Check if we can do the next step
214
+ */
215
+ int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
216
+ mbedtls_ecp_restart_ctx *rs_ctx,
217
+ unsigned ops)
218
+ {
219
+ if (rs_ctx != NULL && ecp_max_ops != 0) {
220
+ /* scale depending on curve size: the chosen reference is 256-bit,
221
+ * and multiplication is quadratic. Round to the closest integer. */
222
+ if (grp->pbits >= 512) {
223
+ ops *= 4;
224
+ } else if (grp->pbits >= 384) {
225
+ ops *= 2;
226
+ }
227
+
228
+ /* Avoid infinite loops: always allow first step.
229
+ * Because of that, however, it's not generally true
230
+ * that ops_done <= ecp_max_ops, so the check
231
+ * ops_done > ecp_max_ops below is mandatory. */
232
+ if ((rs_ctx->ops_done != 0) &&
233
+ (rs_ctx->ops_done > ecp_max_ops ||
234
+ ops > ecp_max_ops - rs_ctx->ops_done)) {
235
+ return MBEDTLS_ERR_ECP_IN_PROGRESS;
236
+ }
237
+
238
+ /* update running count */
239
+ rs_ctx->ops_done += ops;
240
+ }
241
+
242
+ return 0;
243
+ }
244
+
245
+ /* Call this when entering a function that needs its own sub-context */
246
+ #define ECP_RS_ENTER(SUB) do { \
247
+ /* reset ops count for this call if top-level */ \
248
+ if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
249
+ rs_ctx->ops_done = 0; \
250
+ \
251
+ /* set up our own sub-context if needed */ \
252
+ if (mbedtls_ecp_restart_is_enabled() && \
253
+ rs_ctx != NULL && rs_ctx->SUB == NULL) \
254
+ { \
255
+ rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
256
+ if (rs_ctx->SUB == NULL) \
257
+ return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
258
+ \
259
+ ecp_restart_## SUB ##_init(rs_ctx->SUB); \
260
+ } \
261
+ } while (0)
262
+
263
+ /* Call this when leaving a function that needs its own sub-context */
264
+ #define ECP_RS_LEAVE(SUB) do { \
265
+ /* clear our sub-context when not in progress (done or error) */ \
266
+ if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
267
+ ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
268
+ { \
269
+ ecp_restart_## SUB ##_free(rs_ctx->SUB); \
270
+ mbedtls_free(rs_ctx->SUB); \
271
+ rs_ctx->SUB = NULL; \
272
+ } \
273
+ \
274
+ if (rs_ctx != NULL) \
275
+ rs_ctx->depth--; \
276
+ } while (0)
277
+
278
+ #else /* MBEDTLS_ECP_RESTARTABLE */
279
+
280
+ #define ECP_RS_ENTER(sub) (void) rs_ctx;
281
+ #define ECP_RS_LEAVE(sub) (void) rs_ctx;
282
+
283
+ #endif /* MBEDTLS_ECP_RESTARTABLE */
284
+
285
+ #if defined(MBEDTLS_ECP_C)
286
+ static void mpi_init_many(mbedtls_mpi *arr, size_t size)
287
+ {
288
+ while (size--) {
289
+ mbedtls_mpi_init(arr++);
290
+ }
291
+ }
292
+
293
+ static void mpi_free_many(mbedtls_mpi *arr, size_t size)
294
+ {
295
+ while (size--) {
296
+ mbedtls_mpi_free(arr++);
297
+ }
298
+ }
299
+ #endif /* MBEDTLS_ECP_C */
300
+
301
+ /*
302
+ * List of supported curves:
303
+ * - internal ID
304
+ * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
305
+ * - size in bits
306
+ * - readable name
307
+ *
308
+ * Curves are listed in order: largest curves first, and for a given size,
309
+ * fastest curves first.
310
+ *
311
+ * Reminder: update profiles in x509_crt.c and ssl_tls.c when adding a new curve!
312
+ */
313
+ static const mbedtls_ecp_curve_info ecp_supported_curves[] =
314
+ {
315
+ #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
316
+ { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
317
+ #endif
318
+ #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
319
+ { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
320
+ #endif
321
+ #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
322
+ { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
323
+ #endif
324
+ #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
325
+ { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
326
+ #endif
327
+ #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
328
+ { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
329
+ #endif
330
+ #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
331
+ { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
332
+ #endif
333
+ #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
334
+ { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
335
+ #endif
336
+ #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
337
+ { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
338
+ #endif
339
+ #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
340
+ { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
341
+ #endif
342
+ { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
343
+ };
344
+
345
+ #define ECP_NB_CURVES sizeof(ecp_supported_curves) / \
346
+ sizeof(ecp_supported_curves[0])
347
+
348
+ static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
349
+
350
+ /*
351
+ * List of supported curves and associated info
352
+ */
353
+ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
354
+ {
355
+ return ecp_supported_curves;
356
+ }
357
+
358
+ /*
359
+ * List of supported curves, group ID only
360
+ */
361
+ const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
362
+ {
363
+ static int init_done = 0;
364
+
365
+ if (!init_done) {
366
+ size_t i = 0;
367
+ const mbedtls_ecp_curve_info *curve_info;
368
+
369
+ for (curve_info = mbedtls_ecp_curve_list();
370
+ curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
371
+ curve_info++) {
372
+ ecp_supported_grp_id[i++] = curve_info->grp_id;
373
+ }
374
+ ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
375
+
376
+ init_done = 1;
377
+ }
378
+
379
+ return ecp_supported_grp_id;
380
+ }
381
+
382
+ /*
383
+ * Get the curve info for the internal identifier
384
+ */
385
+ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
386
+ {
387
+ const mbedtls_ecp_curve_info *curve_info;
388
+
389
+ for (curve_info = mbedtls_ecp_curve_list();
390
+ curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
391
+ curve_info++) {
392
+ if (curve_info->grp_id == grp_id) {
393
+ return curve_info;
394
+ }
395
+ }
396
+
397
+ return NULL;
398
+ }
399
+
400
+ /*
401
+ * Get the curve info from the TLS identifier
402
+ */
403
+ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
404
+ {
405
+ const mbedtls_ecp_curve_info *curve_info;
406
+
407
+ for (curve_info = mbedtls_ecp_curve_list();
408
+ curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
409
+ curve_info++) {
410
+ if (curve_info->tls_id == tls_id) {
411
+ return curve_info;
412
+ }
413
+ }
414
+
415
+ return NULL;
416
+ }
417
+
418
+ /*
419
+ * Get the curve info from the name
420
+ */
421
+ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
422
+ {
423
+ const mbedtls_ecp_curve_info *curve_info;
424
+
425
+ if (name == NULL) {
426
+ return NULL;
427
+ }
428
+
429
+ for (curve_info = mbedtls_ecp_curve_list();
430
+ curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
431
+ curve_info++) {
432
+ if (strcmp(curve_info->name, name) == 0) {
433
+ return curve_info;
434
+ }
435
+ }
436
+
437
+ return NULL;
438
+ }
439
+
440
+ /*
441
+ * Get the type of a curve
442
+ */
443
+ mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp)
444
+ {
445
+ if (grp->G.X.p == NULL) {
446
+ return MBEDTLS_ECP_TYPE_NONE;
447
+ }
448
+
449
+ if (grp->G.Y.p == NULL) {
450
+ return MBEDTLS_ECP_TYPE_MONTGOMERY;
451
+ } else {
452
+ return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
453
+ }
454
+ }
455
+
456
+ /*
457
+ * Initialize (the components of) a point
458
+ */
459
+ void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
460
+ {
461
+ mbedtls_mpi_init(&pt->X);
462
+ mbedtls_mpi_init(&pt->Y);
463
+ mbedtls_mpi_init(&pt->Z);
464
+ }
465
+
466
+ /*
467
+ * Initialize (the components of) a group
468
+ */
469
+ void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
470
+ {
471
+ grp->id = MBEDTLS_ECP_DP_NONE;
472
+ mbedtls_mpi_init(&grp->P);
473
+ mbedtls_mpi_init(&grp->A);
474
+ mbedtls_mpi_init(&grp->B);
475
+ mbedtls_ecp_point_init(&grp->G);
476
+ mbedtls_mpi_init(&grp->N);
477
+ grp->pbits = 0;
478
+ grp->nbits = 0;
479
+ grp->h = 0;
480
+ grp->modp = NULL;
481
+ grp->t_pre = NULL;
482
+ grp->t_post = NULL;
483
+ grp->t_data = NULL;
484
+ grp->T = NULL;
485
+ grp->T_size = 0;
486
+ }
487
+
488
+ /*
489
+ * Initialize (the components of) a key pair
490
+ */
491
+ void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
492
+ {
493
+ mbedtls_ecp_group_init(&key->grp);
494
+ mbedtls_mpi_init(&key->d);
495
+ mbedtls_ecp_point_init(&key->Q);
496
+ }
497
+
498
+ /*
499
+ * Unallocate (the components of) a point
500
+ */
501
+ void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
502
+ {
503
+ if (pt == NULL) {
504
+ return;
505
+ }
506
+
507
+ mbedtls_mpi_free(&(pt->X));
508
+ mbedtls_mpi_free(&(pt->Y));
509
+ mbedtls_mpi_free(&(pt->Z));
510
+ }
511
+
512
+ /*
513
+ * Check that the comb table (grp->T) is static initialized.
514
+ */
515
+ static int ecp_group_is_static_comb_table(const mbedtls_ecp_group *grp)
516
+ {
517
+ #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
518
+ return grp->T != NULL && grp->T_size == 0;
519
+ #else
520
+ (void) grp;
521
+ return 0;
522
+ #endif
523
+ }
524
+
525
+ /*
526
+ * Unallocate (the components of) a group
527
+ */
528
+ void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
529
+ {
530
+ size_t i;
531
+
532
+ if (grp == NULL) {
533
+ return;
534
+ }
535
+
536
+ if (grp->h != 1) {
537
+ mbedtls_mpi_free(&grp->A);
538
+ mbedtls_mpi_free(&grp->B);
539
+ mbedtls_ecp_point_free(&grp->G);
540
+
541
+ if (grp->h != 2) {
542
+ mbedtls_mpi_free(&grp->N);
543
+ mbedtls_mpi_free(&grp->P);
544
+ }
545
+ }
546
+
547
+ if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) {
548
+ for (i = 0; i < grp->T_size; i++) {
549
+ mbedtls_ecp_point_free(&grp->T[i]);
550
+ }
551
+ mbedtls_free(grp->T);
552
+ }
553
+
554
+ mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
555
+ }
556
+
557
+ /*
558
+ * Unallocate (the components of) a key pair
559
+ */
560
+ void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
561
+ {
562
+ if (key == NULL) {
563
+ return;
564
+ }
565
+
566
+ mbedtls_ecp_group_free(&key->grp);
567
+ mbedtls_mpi_free(&key->d);
568
+ mbedtls_ecp_point_free(&key->Q);
569
+ }
570
+
571
+ /*
572
+ * Copy the contents of a point
573
+ */
574
+ int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
575
+ {
576
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
577
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
578
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y));
579
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z));
580
+
581
+ cleanup:
582
+ return ret;
583
+ }
584
+
585
+ /*
586
+ * Copy the contents of a group object
587
+ */
588
+ int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
589
+ {
590
+ return mbedtls_ecp_group_load(dst, src->id);
591
+ }
592
+
593
+ /*
594
+ * Set point to zero
595
+ */
596
+ int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
597
+ {
598
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
599
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
600
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1));
601
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0));
602
+
603
+ cleanup:
604
+ return ret;
605
+ }
606
+
607
+ /*
608
+ * Tell if a point is zero
609
+ */
610
+ int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
611
+ {
612
+ return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
613
+ }
614
+
615
+ /*
616
+ * Compare two points lazily
617
+ */
618
+ int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
619
+ const mbedtls_ecp_point *Q)
620
+ {
621
+ if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
622
+ mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
623
+ mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
624
+ return 0;
625
+ }
626
+
627
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
628
+ }
629
+
630
+ /*
631
+ * Import a non-zero point from ASCII strings
632
+ */
633
+ int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
634
+ const char *x, const char *y)
635
+ {
636
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
637
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
638
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y));
639
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
640
+
641
+ cleanup:
642
+ return ret;
643
+ }
644
+
645
+ /*
646
+ * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
647
+ */
648
+ int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
649
+ const mbedtls_ecp_point *P,
650
+ int format, size_t *olen,
651
+ unsigned char *buf, size_t buflen)
652
+ {
653
+ int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
654
+ size_t plen;
655
+ if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
656
+ format != MBEDTLS_ECP_PF_COMPRESSED) {
657
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
658
+ }
659
+
660
+ plen = mbedtls_mpi_size(&grp->P);
661
+
662
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
663
+ (void) format; /* Montgomery curves always use the same point format */
664
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
665
+ *olen = plen;
666
+ if (buflen < *olen) {
667
+ return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
668
+ }
669
+
670
+ MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
671
+ }
672
+ #endif
673
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
674
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
675
+ /*
676
+ * Common case: P == 0
677
+ */
678
+ if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
679
+ if (buflen < 1) {
680
+ return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
681
+ }
682
+
683
+ buf[0] = 0x00;
684
+ *olen = 1;
685
+
686
+ return 0;
687
+ }
688
+
689
+ if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
690
+ *olen = 2 * plen + 1;
691
+
692
+ if (buflen < *olen) {
693
+ return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
694
+ }
695
+
696
+ buf[0] = 0x04;
697
+ MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
698
+ MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen));
699
+ } else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
700
+ *olen = plen + 1;
701
+
702
+ if (buflen < *olen) {
703
+ return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
704
+ }
705
+
706
+ buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
707
+ MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
708
+ }
709
+ }
710
+ #endif
711
+
712
+ cleanup:
713
+ return ret;
714
+ }
715
+
716
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
717
+ static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
718
+ const mbedtls_mpi *X,
719
+ mbedtls_mpi *Y,
720
+ int parity_bit);
721
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
722
+
723
+ /*
724
+ * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
725
+ */
726
+ int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
727
+ mbedtls_ecp_point *pt,
728
+ const unsigned char *buf, size_t ilen)
729
+ {
730
+ int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
731
+ size_t plen;
732
+ if (ilen < 1) {
733
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
734
+ }
735
+
736
+ plen = mbedtls_mpi_size(&grp->P);
737
+
738
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
739
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
740
+ if (plen != ilen) {
741
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
742
+ }
743
+
744
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
745
+ mbedtls_mpi_free(&pt->Y);
746
+
747
+ if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
748
+ /* Set most significant bit to 0 as prescribed in RFC7748 §5 */
749
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
750
+ }
751
+
752
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
753
+ }
754
+ #endif
755
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
756
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
757
+ if (buf[0] == 0x00) {
758
+ if (ilen == 1) {
759
+ return mbedtls_ecp_set_zero(pt);
760
+ } else {
761
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
762
+ }
763
+ }
764
+
765
+ if (ilen < 1 + plen) {
766
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
767
+ }
768
+
769
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
770
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
771
+
772
+ if (buf[0] == 0x04) {
773
+ /* format == MBEDTLS_ECP_PF_UNCOMPRESSED */
774
+ if (ilen != 1 + plen * 2) {
775
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
776
+ }
777
+ return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen);
778
+ } else if (buf[0] == 0x02 || buf[0] == 0x03) {
779
+ /* format == MBEDTLS_ECP_PF_COMPRESSED */
780
+ if (ilen != 1 + plen) {
781
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
782
+ }
783
+ return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y,
784
+ (buf[0] & 1));
785
+ } else {
786
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
787
+ }
788
+ }
789
+ #endif
790
+
791
+ cleanup:
792
+ return ret;
793
+ }
794
+
795
+ /*
796
+ * Import a point from a TLS ECPoint record (RFC 4492)
797
+ * struct {
798
+ * opaque point <1..2^8-1>;
799
+ * } ECPoint;
800
+ */
801
+ int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
802
+ mbedtls_ecp_point *pt,
803
+ const unsigned char **buf, size_t buf_len)
804
+ {
805
+ unsigned char data_len;
806
+ const unsigned char *buf_start;
807
+ /*
808
+ * We must have at least two bytes (1 for length, at least one for data)
809
+ */
810
+ if (buf_len < 2) {
811
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
812
+ }
813
+
814
+ data_len = *(*buf)++;
815
+ if (data_len < 1 || data_len > buf_len - 1) {
816
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
817
+ }
818
+
819
+ /*
820
+ * Save buffer start for read_binary and update buf
821
+ */
822
+ buf_start = *buf;
823
+ *buf += data_len;
824
+
825
+ return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
826
+ }
827
+
828
+ /*
829
+ * Export a point as a TLS ECPoint record (RFC 4492)
830
+ * struct {
831
+ * opaque point <1..2^8-1>;
832
+ * } ECPoint;
833
+ */
834
+ int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
835
+ int format, size_t *olen,
836
+ unsigned char *buf, size_t blen)
837
+ {
838
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
839
+ if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
840
+ format != MBEDTLS_ECP_PF_COMPRESSED) {
841
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
842
+ }
843
+
844
+ /*
845
+ * buffer length must be at least one, for our length byte
846
+ */
847
+ if (blen < 1) {
848
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
849
+ }
850
+
851
+ if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
852
+ olen, buf + 1, blen - 1)) != 0) {
853
+ return ret;
854
+ }
855
+
856
+ /*
857
+ * write length to the first byte and update total length
858
+ */
859
+ buf[0] = (unsigned char) *olen;
860
+ ++*olen;
861
+
862
+ return 0;
863
+ }
864
+
865
+ /*
866
+ * Set a group from an ECParameters record (RFC 4492)
867
+ */
868
+ int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
869
+ const unsigned char **buf, size_t len)
870
+ {
871
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
872
+ mbedtls_ecp_group_id grp_id;
873
+ if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
874
+ return ret;
875
+ }
876
+
877
+ return mbedtls_ecp_group_load(grp, grp_id);
878
+ }
879
+
880
+ /*
881
+ * Read a group id from an ECParameters record (RFC 4492) and convert it to
882
+ * mbedtls_ecp_group_id.
883
+ */
884
+ int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
885
+ const unsigned char **buf, size_t len)
886
+ {
887
+ uint16_t tls_id;
888
+ const mbedtls_ecp_curve_info *curve_info;
889
+ /*
890
+ * We expect at least three bytes (see below)
891
+ */
892
+ if (len < 3) {
893
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
894
+ }
895
+
896
+ /*
897
+ * First byte is curve_type; only named_curve is handled
898
+ */
899
+ if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
900
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
901
+ }
902
+
903
+ /*
904
+ * Next two bytes are the namedcurve value
905
+ */
906
+ tls_id = MBEDTLS_GET_UINT16_BE(*buf, 0);
907
+ *buf += 2;
908
+
909
+ if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) {
910
+ return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
911
+ }
912
+
913
+ *grp = curve_info->grp_id;
914
+
915
+ return 0;
916
+ }
917
+
918
+ /*
919
+ * Write the ECParameters record corresponding to a group (RFC 4492)
920
+ */
921
+ int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
922
+ unsigned char *buf, size_t blen)
923
+ {
924
+ const mbedtls_ecp_curve_info *curve_info;
925
+ if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
926
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
927
+ }
928
+
929
+ /*
930
+ * We are going to write 3 bytes (see below)
931
+ */
932
+ *olen = 3;
933
+ if (blen < *olen) {
934
+ return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
935
+ }
936
+
937
+ /*
938
+ * First byte is curve_type, always named_curve
939
+ */
940
+ *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
941
+
942
+ /*
943
+ * Next two bytes are the namedcurve value
944
+ */
945
+ MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
946
+
947
+ return 0;
948
+ }
949
+
950
+ /*
951
+ * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
952
+ * See the documentation of struct mbedtls_ecp_group.
953
+ *
954
+ * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
955
+ */
956
+ static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
957
+ {
958
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
959
+
960
+ if (grp->modp == NULL) {
961
+ return mbedtls_mpi_mod_mpi(N, N, &grp->P);
962
+ }
963
+
964
+ /* N->s < 0 is a much faster test, which fails only if N is 0 */
965
+ if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
966
+ mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
967
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
968
+ }
969
+
970
+ MBEDTLS_MPI_CHK(grp->modp(N));
971
+
972
+ /* The previous call left N in the range [0, 2P) with no more limbs than P
973
+ * (see documentation of mbedtls_ecp_mod_pXXX_raw() in ecp_invasive.h),
974
+ * so we can bring it into the range [0, P) in constant time. */
975
+ mbedtls_mpi_uint c = mbedtls_mpi_core_sub(N->p, N->p, grp->P.p, grp->P.n);
976
+ (void) mbedtls_mpi_core_add_if(N->p, grp->P.p, grp->P.n, (unsigned) c);
977
+
978
+ cleanup:
979
+ return ret;
980
+ }
981
+
982
+ /*
983
+ * Fast mod-p functions expect their argument to be in the 0..p^2 range.
984
+ *
985
+ * In order to guarantee that, we need to ensure that operands of
986
+ * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
987
+ * bring the result back to this range.
988
+ *
989
+ * The following macros are shortcuts for doing that.
990
+ */
991
+
992
+ /*
993
+ * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
994
+ */
995
+ #if defined(MBEDTLS_SELF_TEST)
996
+ #define INC_MUL_COUNT mul_count++;
997
+ #else
998
+ #define INC_MUL_COUNT
999
+ #endif
1000
+
1001
+ #define MOD_MUL(N) \
1002
+ do \
1003
+ { \
1004
+ MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \
1005
+ INC_MUL_COUNT \
1006
+ } while (0)
1007
+
1008
+ static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp,
1009
+ mbedtls_mpi *X,
1010
+ const mbedtls_mpi *A,
1011
+ const mbedtls_mpi *B)
1012
+ {
1013
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1014
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B));
1015
+ MOD_MUL(*X);
1016
+ cleanup:
1017
+ return ret;
1018
+ }
1019
+
1020
+ /*
1021
+ * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
1022
+ * N->s < 0 is a very fast test, which fails only if N is 0
1023
+ */
1024
+ #define MOD_SUB(N) \
1025
+ do { \
1026
+ while ((N)->s < 0 && mbedtls_mpi_cmp_int((N), 0) != 0) \
1027
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi((N), (N), &grp->P)); \
1028
+ } while (0)
1029
+
1030
+ MBEDTLS_MAYBE_UNUSED
1031
+ static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp,
1032
+ mbedtls_mpi *X,
1033
+ const mbedtls_mpi *A,
1034
+ const mbedtls_mpi *B)
1035
+ {
1036
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1037
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B));
1038
+ MOD_SUB(X);
1039
+ cleanup:
1040
+ return ret;
1041
+ }
1042
+
1043
+ /*
1044
+ * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
1045
+ * We known P, N and the result are positive, so sub_abs is correct, and
1046
+ * a bit faster.
1047
+ */
1048
+ #define MOD_ADD(N) \
1049
+ while (mbedtls_mpi_cmp_mpi((N), &grp->P) >= 0) \
1050
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs((N), (N), &grp->P))
1051
+
1052
+ static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
1053
+ mbedtls_mpi *X,
1054
+ const mbedtls_mpi *A,
1055
+ const mbedtls_mpi *B)
1056
+ {
1057
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1058
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
1059
+ MOD_ADD(X);
1060
+ cleanup:
1061
+ return ret;
1062
+ }
1063
+
1064
+ MBEDTLS_MAYBE_UNUSED
1065
+ static inline int mbedtls_mpi_mul_int_mod(const mbedtls_ecp_group *grp,
1066
+ mbedtls_mpi *X,
1067
+ const mbedtls_mpi *A,
1068
+ mbedtls_mpi_uint c)
1069
+ {
1070
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1071
+
1072
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(X, A, c));
1073
+ MOD_ADD(X);
1074
+ cleanup:
1075
+ return ret;
1076
+ }
1077
+
1078
+ MBEDTLS_MAYBE_UNUSED
1079
+ static inline int mbedtls_mpi_sub_int_mod(const mbedtls_ecp_group *grp,
1080
+ mbedtls_mpi *X,
1081
+ const mbedtls_mpi *A,
1082
+ mbedtls_mpi_uint c)
1083
+ {
1084
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1085
+
1086
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(X, A, c));
1087
+ MOD_SUB(X);
1088
+ cleanup:
1089
+ return ret;
1090
+ }
1091
+
1092
+ #define MPI_ECP_SUB_INT(X, A, c) \
1093
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int_mod(grp, X, A, c))
1094
+
1095
+ MBEDTLS_MAYBE_UNUSED
1096
+ static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp,
1097
+ mbedtls_mpi *X,
1098
+ size_t count)
1099
+ {
1100
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1101
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count));
1102
+ MOD_ADD(X);
1103
+ cleanup:
1104
+ return ret;
1105
+ }
1106
+
1107
+ /*
1108
+ * Macro wrappers around ECP modular arithmetic
1109
+ *
1110
+ * Currently, these wrappers are defined via the bignum module.
1111
+ */
1112
+
1113
+ #define MPI_ECP_ADD(X, A, B) \
1114
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, X, A, B))
1115
+
1116
+ #define MPI_ECP_SUB(X, A, B) \
1117
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, X, A, B))
1118
+
1119
+ #define MPI_ECP_MUL(X, A, B) \
1120
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, B))
1121
+
1122
+ #define MPI_ECP_SQR(X, A) \
1123
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, A))
1124
+
1125
+ #define MPI_ECP_MUL_INT(X, A, c) \
1126
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int_mod(grp, X, A, c))
1127
+
1128
+ #define MPI_ECP_INV(dst, src) \
1129
+ MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(NULL, (dst), (src), &grp->P))
1130
+
1131
+ #define MPI_ECP_MOV(X, A) \
1132
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A))
1133
+
1134
+ #define MPI_ECP_SHIFT_L(X, count) \
1135
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, X, count))
1136
+
1137
+ #define MPI_ECP_LSET(X, c) \
1138
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, c))
1139
+
1140
+ #define MPI_ECP_CMP_INT(X, c) \
1141
+ mbedtls_mpi_cmp_int(X, c)
1142
+
1143
+ #define MPI_ECP_CMP(X, Y) \
1144
+ mbedtls_mpi_cmp_mpi(X, Y)
1145
+
1146
+ /* Needs f_rng, p_rng to be defined. */
1147
+ #define MPI_ECP_RAND(X) \
1148
+ MBEDTLS_MPI_CHK(mbedtls_mpi_random((X), 2, &grp->P, f_rng, p_rng))
1149
+
1150
+ /* Conditional negation
1151
+ * Needs grp and a temporary MPI tmp to be defined. */
1152
+ #define MPI_ECP_COND_NEG(X, cond) \
1153
+ do \
1154
+ { \
1155
+ unsigned char nonzero = mbedtls_mpi_cmp_int((X), 0) != 0; \
1156
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&tmp, &grp->P, (X))); \
1157
+ MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), &tmp, \
1158
+ nonzero & cond)); \
1159
+ } while (0)
1160
+
1161
+ #define MPI_ECP_NEG(X) MPI_ECP_COND_NEG((X), 1)
1162
+
1163
+ #define MPI_ECP_VALID(X) \
1164
+ ((X)->p != NULL)
1165
+
1166
+ #define MPI_ECP_COND_ASSIGN(X, Y, cond) \
1167
+ MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), (Y), (cond)))
1168
+
1169
+ #define MPI_ECP_COND_SWAP(X, Y, cond) \
1170
+ MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap((X), (Y), (cond)))
1171
+
1172
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
1173
+
1174
+ /*
1175
+ * Computes the right-hand side of the Short Weierstrass equation
1176
+ * RHS = X^3 + A X + B
1177
+ */
1178
+ static int ecp_sw_rhs(const mbedtls_ecp_group *grp,
1179
+ mbedtls_mpi *rhs,
1180
+ const mbedtls_mpi *X)
1181
+ {
1182
+ int ret;
1183
+
1184
+ /* Compute X^3 + A X + B as X (X^2 + A) + B */
1185
+ MPI_ECP_SQR(rhs, X);
1186
+
1187
+ /* Special case for A = -3 */
1188
+ if (mbedtls_ecp_group_a_is_minus_3(grp)) {
1189
+ MPI_ECP_SUB_INT(rhs, rhs, 3);
1190
+ } else {
1191
+ MPI_ECP_ADD(rhs, rhs, &grp->A);
1192
+ }
1193
+
1194
+ MPI_ECP_MUL(rhs, rhs, X);
1195
+ MPI_ECP_ADD(rhs, rhs, &grp->B);
1196
+
1197
+ cleanup:
1198
+ return ret;
1199
+ }
1200
+
1201
+ /*
1202
+ * Derive Y from X and a parity bit
1203
+ */
1204
+ static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
1205
+ const mbedtls_mpi *X,
1206
+ mbedtls_mpi *Y,
1207
+ int parity_bit)
1208
+ {
1209
+ /* w = y^2 = x^3 + ax + b
1210
+ * y = sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4)
1211
+ *
1212
+ * Note: this method for extracting square root does not validate that w
1213
+ * was indeed a square so this function will return garbage in Y if X
1214
+ * does not correspond to a point on the curve.
1215
+ */
1216
+
1217
+ /* Check prerequisite p = 3 mod 4 */
1218
+ if (mbedtls_mpi_get_bit(&grp->P, 0) != 1 ||
1219
+ mbedtls_mpi_get_bit(&grp->P, 1) != 1) {
1220
+ return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
1221
+ }
1222
+
1223
+ int ret;
1224
+ mbedtls_mpi exp;
1225
+ mbedtls_mpi_init(&exp);
1226
+
1227
+ /* use Y to store intermediate result, actually w above */
1228
+ MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, Y, X));
1229
+
1230
+ /* w = y^2 */ /* Y contains y^2 intermediate result */
1231
+ /* exp = ((p+1)/4) */
1232
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&exp, &grp->P, 1));
1233
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&exp, 2));
1234
+ /* sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4) */
1235
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(Y, Y /*y^2*/, &exp, &grp->P, NULL));
1236
+
1237
+ /* check parity bit match or else invert Y */
1238
+ /* This quick inversion implementation is valid because Y != 0 for all
1239
+ * Short Weierstrass curves supported by mbedtls, as each supported curve
1240
+ * has an order that is a large prime, so each supported curve does not
1241
+ * have any point of order 2, and a point with Y == 0 would be of order 2 */
1242
+ if (mbedtls_mpi_get_bit(Y, 0) != parity_bit) {
1243
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(Y, &grp->P, Y));
1244
+ }
1245
+
1246
+ cleanup:
1247
+
1248
+ mbedtls_mpi_free(&exp);
1249
+ return ret;
1250
+ }
1251
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
1252
+
1253
+ #if defined(MBEDTLS_ECP_C)
1254
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
1255
+ /*
1256
+ * For curves in short Weierstrass form, we do all the internal operations in
1257
+ * Jacobian coordinates.
1258
+ *
1259
+ * For multiplication, we'll use a comb method with countermeasures against
1260
+ * SPA, hence timing attacks.
1261
+ */
1262
+
1263
+ /*
1264
+ * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
1265
+ * Cost: 1N := 1I + 3M + 1S
1266
+ */
1267
+ static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
1268
+ {
1269
+ if (MPI_ECP_CMP_INT(&pt->Z, 0) == 0) {
1270
+ return 0;
1271
+ }
1272
+
1273
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1274
+ mbedtls_mpi T;
1275
+ mbedtls_mpi_init(&T);
1276
+
1277
+ MPI_ECP_INV(&T, &pt->Z); /* T <- 1 / Z */
1278
+ MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y' <- Y*T = Y / Z */
1279
+ MPI_ECP_SQR(&T, &T); /* T <- T^2 = 1 / Z^2 */
1280
+ MPI_ECP_MUL(&pt->X, &pt->X, &T); /* X <- X * T = X / Z^2 */
1281
+ MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y'' <- Y' * T = Y / Z^3 */
1282
+
1283
+ MPI_ECP_LSET(&pt->Z, 1);
1284
+
1285
+ cleanup:
1286
+
1287
+ mbedtls_mpi_free(&T);
1288
+
1289
+ return ret;
1290
+ }
1291
+
1292
+ /*
1293
+ * Normalize jacobian coordinates of an array of (pointers to) points,
1294
+ * using Montgomery's trick to perform only one inversion mod P.
1295
+ * (See for example Cohen's "A Course in Computational Algebraic Number
1296
+ * Theory", Algorithm 10.3.4.)
1297
+ *
1298
+ * Warning: fails (returning an error) if one of the points is zero!
1299
+ * This should never happen, see choice of w in ecp_mul_comb().
1300
+ *
1301
+ * Cost: 1N(t) := 1I + (6t - 3)M + 1S
1302
+ */
1303
+ static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
1304
+ mbedtls_ecp_point *T[], size_t T_size)
1305
+ {
1306
+ if (T_size < 2) {
1307
+ return ecp_normalize_jac(grp, *T);
1308
+ }
1309
+
1310
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1311
+ size_t i;
1312
+ mbedtls_mpi *c, t;
1313
+
1314
+ if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) {
1315
+ return MBEDTLS_ERR_ECP_ALLOC_FAILED;
1316
+ }
1317
+
1318
+ mbedtls_mpi_init(&t);
1319
+
1320
+ mpi_init_many(c, T_size);
1321
+ /*
1322
+ * c[i] = Z_0 * ... * Z_i, i = 0,..,n := T_size-1
1323
+ */
1324
+ MPI_ECP_MOV(&c[0], &T[0]->Z);
1325
+ for (i = 1; i < T_size; i++) {
1326
+ MPI_ECP_MUL(&c[i], &c[i-1], &T[i]->Z);
1327
+ }
1328
+
1329
+ /*
1330
+ * c[n] = 1 / (Z_0 * ... * Z_n) mod P
1331
+ */
1332
+ MPI_ECP_INV(&c[T_size-1], &c[T_size-1]);
1333
+
1334
+ for (i = T_size - 1;; i--) {
1335
+ /* At the start of iteration i (note that i decrements), we have
1336
+ * - c[j] = Z_0 * .... * Z_j for j < i,
1337
+ * - c[j] = 1 / (Z_0 * .... * Z_j) for j == i,
1338
+ *
1339
+ * This is maintained via
1340
+ * - c[i-1] <- c[i] * Z_i
1341
+ *
1342
+ * We also derive 1/Z_i = c[i] * c[i-1] for i>0 and use that
1343
+ * to do the actual normalization. For i==0, we already have
1344
+ * c[0] = 1 / Z_0.
1345
+ */
1346
+
1347
+ if (i > 0) {
1348
+ /* Compute 1/Z_i and establish invariant for the next iteration. */
1349
+ MPI_ECP_MUL(&t, &c[i], &c[i-1]);
1350
+ MPI_ECP_MUL(&c[i-1], &c[i], &T[i]->Z);
1351
+ } else {
1352
+ MPI_ECP_MOV(&t, &c[0]);
1353
+ }
1354
+
1355
+ /* Now t holds 1 / Z_i; normalize as in ecp_normalize_jac() */
1356
+ MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
1357
+ MPI_ECP_SQR(&t, &t);
1358
+ MPI_ECP_MUL(&T[i]->X, &T[i]->X, &t);
1359
+ MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
1360
+
1361
+ /*
1362
+ * Post-precessing: reclaim some memory by shrinking coordinates
1363
+ * - not storing Z (always 1)
1364
+ * - shrinking other coordinates, but still keeping the same number of
1365
+ * limbs as P, as otherwise it will too likely be regrown too fast.
1366
+ */
1367
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n));
1368
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n));
1369
+
1370
+ MPI_ECP_LSET(&T[i]->Z, 1);
1371
+
1372
+ if (i == 0) {
1373
+ break;
1374
+ }
1375
+ }
1376
+
1377
+ cleanup:
1378
+
1379
+ mbedtls_mpi_free(&t);
1380
+ mpi_free_many(c, T_size);
1381
+ mbedtls_free(c);
1382
+
1383
+ return ret;
1384
+ }
1385
+
1386
+ /*
1387
+ * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1388
+ * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1389
+ */
1390
+ static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
1391
+ mbedtls_ecp_point *Q,
1392
+ unsigned char inv)
1393
+ {
1394
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1395
+ mbedtls_mpi tmp;
1396
+ mbedtls_mpi_init(&tmp);
1397
+
1398
+ MPI_ECP_COND_NEG(&Q->Y, inv);
1399
+
1400
+ cleanup:
1401
+ mbedtls_mpi_free(&tmp);
1402
+ return ret;
1403
+ }
1404
+
1405
+ /*
1406
+ * Point doubling R = 2 P, Jacobian coordinates
1407
+ *
1408
+ * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
1409
+ *
1410
+ * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1411
+ * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1412
+ *
1413
+ * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1414
+ *
1415
+ * Cost: 1D := 3M + 4S (A == 0)
1416
+ * 4M + 4S (A == -3)
1417
+ * 3M + 6S + 1a otherwise
1418
+ */
1419
+ static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1420
+ const mbedtls_ecp_point *P,
1421
+ mbedtls_mpi tmp[4])
1422
+ {
1423
+ #if defined(MBEDTLS_SELF_TEST)
1424
+ dbl_count++;
1425
+ #endif
1426
+
1427
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1428
+
1429
+ /* Special case for A = -3 */
1430
+ if (mbedtls_ecp_group_a_is_minus_3(grp)) {
1431
+ /* tmp[0] <- M = 3(X + Z^2)(X - Z^2) */
1432
+ MPI_ECP_SQR(&tmp[1], &P->Z);
1433
+ MPI_ECP_ADD(&tmp[2], &P->X, &tmp[1]);
1434
+ MPI_ECP_SUB(&tmp[3], &P->X, &tmp[1]);
1435
+ MPI_ECP_MUL(&tmp[1], &tmp[2], &tmp[3]);
1436
+ MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
1437
+ } else {
1438
+ /* tmp[0] <- M = 3.X^2 + A.Z^4 */
1439
+ MPI_ECP_SQR(&tmp[1], &P->X);
1440
+ MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
1441
+
1442
+ /* Optimize away for "koblitz" curves with A = 0 */
1443
+ if (MPI_ECP_CMP_INT(&grp->A, 0) != 0) {
1444
+ /* M += A.Z^4 */
1445
+ MPI_ECP_SQR(&tmp[1], &P->Z);
1446
+ MPI_ECP_SQR(&tmp[2], &tmp[1]);
1447
+ MPI_ECP_MUL(&tmp[1], &tmp[2], &grp->A);
1448
+ MPI_ECP_ADD(&tmp[0], &tmp[0], &tmp[1]);
1449
+ }
1450
+ }
1451
+
1452
+ /* tmp[1] <- S = 4.X.Y^2 */
1453
+ MPI_ECP_SQR(&tmp[2], &P->Y);
1454
+ MPI_ECP_SHIFT_L(&tmp[2], 1);
1455
+ MPI_ECP_MUL(&tmp[1], &P->X, &tmp[2]);
1456
+ MPI_ECP_SHIFT_L(&tmp[1], 1);
1457
+
1458
+ /* tmp[3] <- U = 8.Y^4 */
1459
+ MPI_ECP_SQR(&tmp[3], &tmp[2]);
1460
+ MPI_ECP_SHIFT_L(&tmp[3], 1);
1461
+
1462
+ /* tmp[2] <- T = M^2 - 2.S */
1463
+ MPI_ECP_SQR(&tmp[2], &tmp[0]);
1464
+ MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
1465
+ MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
1466
+
1467
+ /* tmp[1] <- S = M(S - T) - U */
1468
+ MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[2]);
1469
+ MPI_ECP_MUL(&tmp[1], &tmp[1], &tmp[0]);
1470
+ MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[3]);
1471
+
1472
+ /* tmp[3] <- U = 2.Y.Z */
1473
+ MPI_ECP_MUL(&tmp[3], &P->Y, &P->Z);
1474
+ MPI_ECP_SHIFT_L(&tmp[3], 1);
1475
+
1476
+ /* Store results */
1477
+ MPI_ECP_MOV(&R->X, &tmp[2]);
1478
+ MPI_ECP_MOV(&R->Y, &tmp[1]);
1479
+ MPI_ECP_MOV(&R->Z, &tmp[3]);
1480
+
1481
+ cleanup:
1482
+
1483
+ return ret;
1484
+ }
1485
+
1486
+ /*
1487
+ * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
1488
+ *
1489
+ * The coordinates of Q must be normalized (= affine),
1490
+ * but those of P don't need to. R is not normalized.
1491
+ *
1492
+ * P,Q,R may alias, but only at the level of EC points: they must be either
1493
+ * equal as pointers, or disjoint (including the coordinate data buffers).
1494
+ * Fine-grained aliasing at the level of coordinates is not supported.
1495
+ *
1496
+ * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
1497
+ * None of these cases can happen as intermediate step in ecp_mul_comb():
1498
+ * - at each step, P, Q and R are multiples of the base point, the factor
1499
+ * being less than its order, so none of them is zero;
1500
+ * - Q is an odd multiple of the base point, P an even multiple,
1501
+ * due to the choice of precomputed points in the modified comb method.
1502
+ * So branches for these cases do not leak secret information.
1503
+ *
1504
+ * Cost: 1A := 8M + 3S
1505
+ */
1506
+ static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1507
+ const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
1508
+ mbedtls_mpi tmp[4])
1509
+ {
1510
+ #if defined(MBEDTLS_SELF_TEST)
1511
+ add_count++;
1512
+ #endif
1513
+
1514
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1515
+
1516
+ /* NOTE: Aliasing between input and output is allowed, so one has to make
1517
+ * sure that at the point X,Y,Z are written, {P,Q}->{X,Y,Z} are no
1518
+ * longer read from. */
1519
+ mbedtls_mpi * const X = &R->X;
1520
+ mbedtls_mpi * const Y = &R->Y;
1521
+ mbedtls_mpi * const Z = &R->Z;
1522
+
1523
+ if (!MPI_ECP_VALID(&Q->Z)) {
1524
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1525
+ }
1526
+
1527
+ /*
1528
+ * Trivial cases: P == 0 or Q == 0 (case 1)
1529
+ */
1530
+ if (MPI_ECP_CMP_INT(&P->Z, 0) == 0) {
1531
+ return mbedtls_ecp_copy(R, Q);
1532
+ }
1533
+
1534
+ if (MPI_ECP_CMP_INT(&Q->Z, 0) == 0) {
1535
+ return mbedtls_ecp_copy(R, P);
1536
+ }
1537
+
1538
+ /*
1539
+ * Make sure Q coordinates are normalized
1540
+ */
1541
+ if (MPI_ECP_CMP_INT(&Q->Z, 1) != 0) {
1542
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
1543
+ }
1544
+
1545
+ MPI_ECP_SQR(&tmp[0], &P->Z);
1546
+ MPI_ECP_MUL(&tmp[1], &tmp[0], &P->Z);
1547
+ MPI_ECP_MUL(&tmp[0], &tmp[0], &Q->X);
1548
+ MPI_ECP_MUL(&tmp[1], &tmp[1], &Q->Y);
1549
+ MPI_ECP_SUB(&tmp[0], &tmp[0], &P->X);
1550
+ MPI_ECP_SUB(&tmp[1], &tmp[1], &P->Y);
1551
+
1552
+ /* Special cases (2) and (3) */
1553
+ if (MPI_ECP_CMP_INT(&tmp[0], 0) == 0) {
1554
+ if (MPI_ECP_CMP_INT(&tmp[1], 0) == 0) {
1555
+ ret = ecp_double_jac(grp, R, P, tmp);
1556
+ goto cleanup;
1557
+ } else {
1558
+ ret = mbedtls_ecp_set_zero(R);
1559
+ goto cleanup;
1560
+ }
1561
+ }
1562
+
1563
+ /* {P,Q}->Z no longer used, so OK to write to Z even if there's aliasing. */
1564
+ MPI_ECP_MUL(Z, &P->Z, &tmp[0]);
1565
+ MPI_ECP_SQR(&tmp[2], &tmp[0]);
1566
+ MPI_ECP_MUL(&tmp[3], &tmp[2], &tmp[0]);
1567
+ MPI_ECP_MUL(&tmp[2], &tmp[2], &P->X);
1568
+
1569
+ MPI_ECP_MOV(&tmp[0], &tmp[2]);
1570
+ MPI_ECP_SHIFT_L(&tmp[0], 1);
1571
+
1572
+ /* {P,Q}->X no longer used, so OK to write to X even if there's aliasing. */
1573
+ MPI_ECP_SQR(X, &tmp[1]);
1574
+ MPI_ECP_SUB(X, X, &tmp[0]);
1575
+ MPI_ECP_SUB(X, X, &tmp[3]);
1576
+ MPI_ECP_SUB(&tmp[2], &tmp[2], X);
1577
+ MPI_ECP_MUL(&tmp[2], &tmp[2], &tmp[1]);
1578
+ MPI_ECP_MUL(&tmp[3], &tmp[3], &P->Y);
1579
+ /* {P,Q}->Y no longer used, so OK to write to Y even if there's aliasing. */
1580
+ MPI_ECP_SUB(Y, &tmp[2], &tmp[3]);
1581
+
1582
+ cleanup:
1583
+
1584
+ return ret;
1585
+ }
1586
+
1587
+ /*
1588
+ * Randomize jacobian coordinates:
1589
+ * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
1590
+ * This is sort of the reverse operation of ecp_normalize_jac().
1591
+ *
1592
+ * This countermeasure was first suggested in [2].
1593
+ */
1594
+ static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
1595
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1596
+ {
1597
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1598
+ mbedtls_mpi l;
1599
+
1600
+ mbedtls_mpi_init(&l);
1601
+
1602
+ /* Generate l such that 1 < l < p */
1603
+ MPI_ECP_RAND(&l);
1604
+
1605
+ /* Z' = l * Z */
1606
+ MPI_ECP_MUL(&pt->Z, &pt->Z, &l);
1607
+
1608
+ /* Y' = l * Y */
1609
+ MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
1610
+
1611
+ /* X' = l^2 * X */
1612
+ MPI_ECP_SQR(&l, &l);
1613
+ MPI_ECP_MUL(&pt->X, &pt->X, &l);
1614
+
1615
+ /* Y'' = l^2 * Y' = l^3 * Y */
1616
+ MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
1617
+
1618
+ cleanup:
1619
+ mbedtls_mpi_free(&l);
1620
+
1621
+ if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
1622
+ ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
1623
+ }
1624
+ return ret;
1625
+ }
1626
+
1627
+ /*
1628
+ * Check and define parameters used by the comb method (see below for details)
1629
+ */
1630
+ #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1631
+ #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
1632
+ #endif
1633
+
1634
+ /* d = ceil( n / w ) */
1635
+ #define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2
1636
+
1637
+ /* number of precomputed points */
1638
+ #define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
1639
+
1640
+ /*
1641
+ * Compute the representation of m that will be used with our comb method.
1642
+ *
1643
+ * The basic comb method is described in GECC 3.44 for example. We use a
1644
+ * modified version that provides resistance to SPA by avoiding zero
1645
+ * digits in the representation as in [3]. We modify the method further by
1646
+ * requiring that all K_i be odd, which has the small cost that our
1647
+ * representation uses one more K_i, due to carries, but saves on the size of
1648
+ * the precomputed table.
1649
+ *
1650
+ * Summary of the comb method and its modifications:
1651
+ *
1652
+ * - The goal is to compute m*P for some w*d-bit integer m.
1653
+ *
1654
+ * - The basic comb method splits m into the w-bit integers
1655
+ * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1656
+ * index has residue i modulo d, and computes m * P as
1657
+ * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1658
+ * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1659
+ *
1660
+ * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1661
+ * .. + 2^{i-1} S[x[i-1]] - 2^i S[x[i]] + 2^{i+1} S[x[i]] + 2^{i+2} S[x[i+2]] ..,
1662
+ * thereby successively converting it into a form where all summands
1663
+ * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1664
+ *
1665
+ * - More generally, even if x[i+1] != 0, we can first transform the sum as
1666
+ * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1667
+ * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1668
+ * Performing and iterating this procedure for those x[i] that are even
1669
+ * (keeping track of carry), we can transform the original sum into one of the form
1670
+ * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1671
+ * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1672
+ * which is why we are only computing half of it in the first place in
1673
+ * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1674
+ *
1675
+ * - For the sake of compactness, only the seven low-order bits of x[i]
1676
+ * are used to represent its absolute value (K_i in the paper), and the msb
1677
+ * of x[i] encodes the sign (s_i in the paper): it is set if and only if
1678
+ * if s_i == -1;
1679
+ *
1680
+ * Calling conventions:
1681
+ * - x is an array of size d + 1
1682
+ * - w is the size, ie number of teeth, of the comb, and must be between
1683
+ * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
1684
+ * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1685
+ * (the result will be incorrect if these assumptions are not satisfied)
1686
+ */
1687
+ static void ecp_comb_recode_core(unsigned char x[], size_t d,
1688
+ unsigned char w, const mbedtls_mpi *m)
1689
+ {
1690
+ size_t i, j;
1691
+ unsigned char c, cc, adjust;
1692
+
1693
+ memset(x, 0, d+1);
1694
+
1695
+ /* First get the classical comb values (except for x_d = 0) */
1696
+ for (i = 0; i < d; i++) {
1697
+ for (j = 0; j < w; j++) {
1698
+ x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j;
1699
+ }
1700
+ }
1701
+
1702
+ /* Now make sure x_1 .. x_d are odd */
1703
+ c = 0;
1704
+ for (i = 1; i <= d; i++) {
1705
+ /* Add carry and update it */
1706
+ cc = x[i] & c;
1707
+ x[i] = x[i] ^ c;
1708
+ c = cc;
1709
+
1710
+ /* Adjust if needed, avoiding branches */
1711
+ adjust = 1 - (x[i] & 0x01);
1712
+ c |= x[i] & (x[i-1] * adjust);
1713
+ x[i] = x[i] ^ (x[i-1] * adjust);
1714
+ x[i-1] |= adjust << 7;
1715
+ }
1716
+ }
1717
+
1718
+ /*
1719
+ * Precompute points for the adapted comb method
1720
+ *
1721
+ * Assumption: T must be able to hold 2^{w - 1} elements.
1722
+ *
1723
+ * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1724
+ * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
1725
+ *
1726
+ * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
1727
+ *
1728
+ * Note: Even comb values (those where P would be omitted from the
1729
+ * sum defining T[i] above) are not needed in our adaption
1730
+ * the comb method. See ecp_comb_recode_core().
1731
+ *
1732
+ * This function currently works in four steps:
1733
+ * (1) [dbl] Computation of intermediate T[i] for 2-power values of i
1734
+ * (2) [norm_dbl] Normalization of coordinates of these T[i]
1735
+ * (3) [add] Computation of all T[i]
1736
+ * (4) [norm_add] Normalization of all T[i]
1737
+ *
1738
+ * Step 1 can be interrupted but not the others; together with the final
1739
+ * coordinate normalization they are the largest steps done at once, depending
1740
+ * on the window size. Here are operation counts for P-256:
1741
+ *
1742
+ * step (2) (3) (4)
1743
+ * w = 5 142 165 208
1744
+ * w = 4 136 77 160
1745
+ * w = 3 130 33 136
1746
+ * w = 2 124 11 124
1747
+ *
1748
+ * So if ECC operations are blocking for too long even with a low max_ops
1749
+ * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1750
+ * to minimize maximum blocking time.
1751
+ */
1752
+ static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
1753
+ mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
1754
+ unsigned char w, size_t d,
1755
+ mbedtls_ecp_restart_ctx *rs_ctx)
1756
+ {
1757
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1758
+ unsigned char i;
1759
+ size_t j = 0;
1760
+ const unsigned char T_size = 1U << (w - 1);
1761
+ mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1] = { NULL };
1762
+
1763
+ mbedtls_mpi tmp[4];
1764
+
1765
+ mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
1766
+
1767
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1768
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1769
+ if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
1770
+ goto dbl;
1771
+ }
1772
+ if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) {
1773
+ goto norm_dbl;
1774
+ }
1775
+ if (rs_ctx->rsm->state == ecp_rsm_pre_add) {
1776
+ goto add;
1777
+ }
1778
+ if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) {
1779
+ goto norm_add;
1780
+ }
1781
+ }
1782
+ #else
1783
+ (void) rs_ctx;
1784
+ #endif
1785
+
1786
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1787
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1788
+ rs_ctx->rsm->state = ecp_rsm_pre_dbl;
1789
+
1790
+ /* initial state for the loop */
1791
+ rs_ctx->rsm->i = 0;
1792
+ }
1793
+
1794
+ dbl:
1795
+ #endif
1796
+ /*
1797
+ * Set T[0] = P and
1798
+ * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
1799
+ */
1800
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P));
1801
+
1802
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1803
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
1804
+ j = rs_ctx->rsm->i;
1805
+ } else
1806
+ #endif
1807
+ j = 0;
1808
+
1809
+ for (; j < d * (w - 1); j++) {
1810
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL);
1811
+
1812
+ i = 1U << (j / d);
1813
+ cur = T + i;
1814
+
1815
+ if (j % d == 0) {
1816
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1)));
1817
+ }
1818
+
1819
+ MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur, tmp));
1820
+ }
1821
+
1822
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1823
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1824
+ rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
1825
+ }
1826
+
1827
+ norm_dbl:
1828
+ #endif
1829
+ /*
1830
+ * Normalize current elements in T to allow them to be used in
1831
+ * ecp_add_mixed() below, which requires one normalized input.
1832
+ *
1833
+ * As T has holes, use an auxiliary array of pointers to elements in T.
1834
+ *
1835
+ */
1836
+ j = 0;
1837
+ for (i = 1; i < T_size; i <<= 1) {
1838
+ TT[j++] = T + i;
1839
+ }
1840
+
1841
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
1842
+
1843
+ MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
1844
+
1845
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1846
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1847
+ rs_ctx->rsm->state = ecp_rsm_pre_add;
1848
+ }
1849
+
1850
+ add:
1851
+ #endif
1852
+ /*
1853
+ * Compute the remaining ones using the minimal number of additions
1854
+ * Be careful to update T[2^l] only after using it!
1855
+ */
1856
+ MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD);
1857
+
1858
+ for (i = 1; i < T_size; i <<= 1) {
1859
+ j = i;
1860
+ while (j--) {
1861
+ MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i], tmp));
1862
+ }
1863
+ }
1864
+
1865
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1866
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
1867
+ rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
1868
+ }
1869
+
1870
+ norm_add:
1871
+ #endif
1872
+ /*
1873
+ * Normalize final elements in T. Even though there are no holes now, we
1874
+ * still need the auxiliary array for homogeneity with the previous
1875
+ * call. Also, skip T[0] which is already normalised, being a copy of P.
1876
+ */
1877
+ for (j = 0; j + 1 < T_size; j++) {
1878
+ TT[j] = T + j + 1;
1879
+ }
1880
+
1881
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
1882
+
1883
+ MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
1884
+
1885
+ /* Free Z coordinate (=1 after normalization) to save RAM.
1886
+ * This makes T[i] invalid as mbedtls_ecp_points, but this is OK
1887
+ * since from this point onwards, they are only accessed indirectly
1888
+ * via the getter function ecp_select_comb() which does set the
1889
+ * target's Z coordinate to 1. */
1890
+ for (i = 0; i < T_size; i++) {
1891
+ mbedtls_mpi_free(&T[i].Z);
1892
+ }
1893
+
1894
+ cleanup:
1895
+
1896
+ mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
1897
+
1898
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1899
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
1900
+ ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
1901
+ if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
1902
+ rs_ctx->rsm->i = j;
1903
+ }
1904
+ }
1905
+ #endif
1906
+
1907
+ return ret;
1908
+ }
1909
+
1910
+ /*
1911
+ * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
1912
+ *
1913
+ * See ecp_comb_recode_core() for background
1914
+ */
1915
+ static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1916
+ const mbedtls_ecp_point T[], unsigned char T_size,
1917
+ unsigned char i)
1918
+ {
1919
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1920
+ unsigned char ii, j;
1921
+
1922
+ /* Ignore the "sign" bit and scale down */
1923
+ ii = (i & 0x7Fu) >> 1;
1924
+
1925
+ /* Read the whole table to thwart cache-based timing attacks */
1926
+ for (j = 0; j < T_size; j++) {
1927
+ MPI_ECP_COND_ASSIGN(&R->X, &T[j].X, j == ii);
1928
+ MPI_ECP_COND_ASSIGN(&R->Y, &T[j].Y, j == ii);
1929
+ }
1930
+
1931
+ /* Safely invert result if i is "negative" */
1932
+ MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7));
1933
+
1934
+ MPI_ECP_LSET(&R->Z, 1);
1935
+
1936
+ cleanup:
1937
+ return ret;
1938
+ }
1939
+
1940
+ /*
1941
+ * Core multiplication algorithm for the (modified) comb method.
1942
+ * This part is actually common with the basic comb method (GECC 3.44)
1943
+ *
1944
+ * Cost: d A + d D + 1 R
1945
+ */
1946
+ static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1947
+ const mbedtls_ecp_point T[], unsigned char T_size,
1948
+ const unsigned char x[], size_t d,
1949
+ int (*f_rng)(void *, unsigned char *, size_t),
1950
+ void *p_rng,
1951
+ mbedtls_ecp_restart_ctx *rs_ctx)
1952
+ {
1953
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1954
+ mbedtls_ecp_point Txi;
1955
+ mbedtls_mpi tmp[4];
1956
+ size_t i;
1957
+
1958
+ mbedtls_ecp_point_init(&Txi);
1959
+ mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
1960
+
1961
+ #if !defined(MBEDTLS_ECP_RESTARTABLE)
1962
+ (void) rs_ctx;
1963
+ #endif
1964
+
1965
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
1966
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
1967
+ rs_ctx->rsm->state != ecp_rsm_comb_core) {
1968
+ rs_ctx->rsm->i = 0;
1969
+ rs_ctx->rsm->state = ecp_rsm_comb_core;
1970
+ }
1971
+
1972
+ /* new 'if' instead of nested for the sake of the 'else' branch */
1973
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
1974
+ /* restore current index (R already pointing to rs_ctx->rsm->R) */
1975
+ i = rs_ctx->rsm->i;
1976
+ } else
1977
+ #endif
1978
+ {
1979
+ /* Start with a non-zero point and randomize its coordinates */
1980
+ i = d;
1981
+ MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i]));
1982
+ if (f_rng != 0) {
1983
+ MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng));
1984
+ }
1985
+ }
1986
+
1987
+ while (i != 0) {
1988
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD);
1989
+ --i;
1990
+
1991
+ MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R, tmp));
1992
+ MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i]));
1993
+ MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi, tmp));
1994
+ }
1995
+
1996
+ cleanup:
1997
+
1998
+ mbedtls_ecp_point_free(&Txi);
1999
+ mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2000
+
2001
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2002
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
2003
+ ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2004
+ rs_ctx->rsm->i = i;
2005
+ /* no need to save R, already pointing to rs_ctx->rsm->R */
2006
+ }
2007
+ #endif
2008
+
2009
+ return ret;
2010
+ }
2011
+
2012
+ /*
2013
+ * Recode the scalar to get constant-time comb multiplication
2014
+ *
2015
+ * As the actual scalar recoding needs an odd scalar as a starting point,
2016
+ * this wrapper ensures that by replacing m by N - m if necessary, and
2017
+ * informs the caller that the result of multiplication will be negated.
2018
+ *
2019
+ * This works because we only support large prime order for Short Weierstrass
2020
+ * curves, so N is always odd hence either m or N - m is.
2021
+ *
2022
+ * See ecp_comb_recode_core() for background.
2023
+ */
2024
+ static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp,
2025
+ const mbedtls_mpi *m,
2026
+ unsigned char k[COMB_MAX_D + 1],
2027
+ size_t d,
2028
+ unsigned char w,
2029
+ unsigned char *parity_trick)
2030
+ {
2031
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2032
+ mbedtls_mpi M, mm;
2033
+
2034
+ mbedtls_mpi_init(&M);
2035
+ mbedtls_mpi_init(&mm);
2036
+
2037
+ /* N is always odd (see above), just make extra sure */
2038
+ if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) {
2039
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2040
+ }
2041
+
2042
+ /* do we need the parity trick? */
2043
+ *parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0);
2044
+
2045
+ /* execute parity fix in constant time */
2046
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m));
2047
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m));
2048
+ MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick));
2049
+
2050
+ /* actual scalar recoding */
2051
+ ecp_comb_recode_core(k, d, w, &M);
2052
+
2053
+ cleanup:
2054
+ mbedtls_mpi_free(&mm);
2055
+ mbedtls_mpi_free(&M);
2056
+
2057
+ return ret;
2058
+ }
2059
+
2060
+ /*
2061
+ * Perform comb multiplication (for short Weierstrass curves)
2062
+ * once the auxiliary table has been pre-computed.
2063
+ *
2064
+ * Scalar recoding may use a parity trick that makes us compute -m * P,
2065
+ * if that is the case we'll need to recover m * P at the end.
2066
+ */
2067
+ static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp,
2068
+ mbedtls_ecp_point *R,
2069
+ const mbedtls_mpi *m,
2070
+ const mbedtls_ecp_point *T,
2071
+ unsigned char T_size,
2072
+ unsigned char w,
2073
+ size_t d,
2074
+ int (*f_rng)(void *, unsigned char *, size_t),
2075
+ void *p_rng,
2076
+ mbedtls_ecp_restart_ctx *rs_ctx)
2077
+ {
2078
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2079
+ unsigned char parity_trick;
2080
+ unsigned char k[COMB_MAX_D + 1];
2081
+ mbedtls_ecp_point *RR = R;
2082
+
2083
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2084
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
2085
+ RR = &rs_ctx->rsm->R;
2086
+
2087
+ if (rs_ctx->rsm->state == ecp_rsm_final_norm) {
2088
+ goto final_norm;
2089
+ }
2090
+ }
2091
+ #endif
2092
+
2093
+ MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w,
2094
+ &parity_trick));
2095
+ MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d,
2096
+ f_rng, p_rng, rs_ctx));
2097
+ MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick));
2098
+
2099
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2100
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
2101
+ rs_ctx->rsm->state = ecp_rsm_final_norm;
2102
+ }
2103
+
2104
+ final_norm:
2105
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
2106
+ #endif
2107
+ MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR));
2108
+
2109
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2110
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
2111
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR));
2112
+ }
2113
+ #endif
2114
+
2115
+ cleanup:
2116
+ return ret;
2117
+ }
2118
+
2119
+ /*
2120
+ * Pick window size based on curve size and whether we optimize for base point
2121
+ */
2122
+ static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp,
2123
+ unsigned char p_eq_g)
2124
+ {
2125
+ unsigned char w;
2126
+
2127
+ /*
2128
+ * Minimize the number of multiplications, that is minimize
2129
+ * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
2130
+ * (see costs of the various parts, with 1S = 1M)
2131
+ */
2132
+ w = grp->nbits >= 384 ? 5 : 4;
2133
+
2134
+ /*
2135
+ * If P == G, pre-compute a bit more, since this may be re-used later.
2136
+ * Just adding one avoids upping the cost of the first mul too much,
2137
+ * and the memory cost too.
2138
+ */
2139
+ if (p_eq_g) {
2140
+ w++;
2141
+ }
2142
+
2143
+ /*
2144
+ * If static comb table may not be used (!p_eq_g) or static comb table does
2145
+ * not exists, make sure w is within bounds.
2146
+ * (The last test is useful only for very small curves in the test suite.)
2147
+ *
2148
+ * The user reduces MBEDTLS_ECP_WINDOW_SIZE does not changes the size of
2149
+ * static comb table, because the size of static comb table is fixed when
2150
+ * it is generated.
2151
+ */
2152
+ #if (MBEDTLS_ECP_WINDOW_SIZE < 6)
2153
+ if ((!p_eq_g || !ecp_group_is_static_comb_table(grp)) && w > MBEDTLS_ECP_WINDOW_SIZE) {
2154
+ w = MBEDTLS_ECP_WINDOW_SIZE;
2155
+ }
2156
+ #endif
2157
+ if (w >= grp->nbits) {
2158
+ w = 2;
2159
+ }
2160
+
2161
+ return w;
2162
+ }
2163
+
2164
+ /*
2165
+ * Multiplication using the comb method - for curves in short Weierstrass form
2166
+ *
2167
+ * This function is mainly responsible for administrative work:
2168
+ * - managing the restart context if enabled
2169
+ * - managing the table of precomputed points (passed between the below two
2170
+ * functions): allocation, computation, ownership transfer, freeing.
2171
+ *
2172
+ * It delegates the actual arithmetic work to:
2173
+ * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
2174
+ *
2175
+ * See comments on ecp_comb_recode_core() regarding the computation strategy.
2176
+ */
2177
+ static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2178
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2179
+ int (*f_rng)(void *, unsigned char *, size_t),
2180
+ void *p_rng,
2181
+ mbedtls_ecp_restart_ctx *rs_ctx)
2182
+ {
2183
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2184
+ unsigned char w, p_eq_g, i;
2185
+ size_t d;
2186
+ unsigned char T_size = 0, T_ok = 0;
2187
+ mbedtls_ecp_point *T = NULL;
2188
+
2189
+ ECP_RS_ENTER(rsm);
2190
+
2191
+ /* Is P the base point ? */
2192
+ #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
2193
+ p_eq_g = (MPI_ECP_CMP(&P->Y, &grp->G.Y) == 0 &&
2194
+ MPI_ECP_CMP(&P->X, &grp->G.X) == 0);
2195
+ #else
2196
+ p_eq_g = 0;
2197
+ #endif
2198
+
2199
+ /* Pick window size and deduce related sizes */
2200
+ w = ecp_pick_window_size(grp, p_eq_g);
2201
+ T_size = 1U << (w - 1);
2202
+ d = (grp->nbits + w - 1) / w;
2203
+
2204
+ /* Pre-computed table: do we have it already for the base point? */
2205
+ if (p_eq_g && grp->T != NULL) {
2206
+ /* second pointer to the same table, will be deleted on exit */
2207
+ T = grp->T;
2208
+ T_ok = 1;
2209
+ } else
2210
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2211
+ /* Pre-computed table: do we have one in progress? complete? */
2212
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) {
2213
+ /* transfer ownership of T from rsm to local function */
2214
+ T = rs_ctx->rsm->T;
2215
+ rs_ctx->rsm->T = NULL;
2216
+ rs_ctx->rsm->T_size = 0;
2217
+
2218
+ /* This effectively jumps to the call to mul_comb_after_precomp() */
2219
+ T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
2220
+ } else
2221
+ #endif
2222
+ /* Allocate table if we didn't have any */
2223
+ {
2224
+ T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point));
2225
+ if (T == NULL) {
2226
+ ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
2227
+ goto cleanup;
2228
+ }
2229
+
2230
+ for (i = 0; i < T_size; i++) {
2231
+ mbedtls_ecp_point_init(&T[i]);
2232
+ }
2233
+
2234
+ T_ok = 0;
2235
+ }
2236
+
2237
+ /* Compute table (or finish computing it) if not done already */
2238
+ if (!T_ok) {
2239
+ MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx));
2240
+
2241
+ if (p_eq_g) {
2242
+ /* almost transfer ownership of T to the group, but keep a copy of
2243
+ * the pointer to use for calling the next function more easily */
2244
+ grp->T = T;
2245
+ grp->T_size = T_size;
2246
+ }
2247
+ }
2248
+
2249
+ /* Actual comb multiplication using precomputed points */
2250
+ MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m,
2251
+ T, T_size, w, d,
2252
+ f_rng, p_rng, rs_ctx));
2253
+
2254
+ cleanup:
2255
+
2256
+ /* does T belong to the group? */
2257
+ if (T == grp->T) {
2258
+ T = NULL;
2259
+ }
2260
+
2261
+ /* does T belong to the restart context? */
2262
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2263
+ if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) {
2264
+ /* transfer ownership of T from local function to rsm */
2265
+ rs_ctx->rsm->T_size = T_size;
2266
+ rs_ctx->rsm->T = T;
2267
+ T = NULL;
2268
+ }
2269
+ #endif
2270
+
2271
+ /* did T belong to us? then let's destroy it! */
2272
+ if (T != NULL) {
2273
+ for (i = 0; i < T_size; i++) {
2274
+ mbedtls_ecp_point_free(&T[i]);
2275
+ }
2276
+ mbedtls_free(T);
2277
+ }
2278
+
2279
+ /* prevent caller from using invalid value */
2280
+ int should_free_R = (ret != 0);
2281
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2282
+ /* don't free R while in progress in case R == P */
2283
+ if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2284
+ should_free_R = 0;
2285
+ }
2286
+ #endif
2287
+ if (should_free_R) {
2288
+ mbedtls_ecp_point_free(R);
2289
+ }
2290
+
2291
+ ECP_RS_LEAVE(rsm);
2292
+
2293
+ return ret;
2294
+ }
2295
+
2296
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2297
+
2298
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2299
+ /*
2300
+ * For Montgomery curves, we do all the internal arithmetic in projective
2301
+ * coordinates. Import/export of points uses only the x coordinates, which is
2302
+ * internally represented as X / Z.
2303
+ *
2304
+ * For scalar multiplication, we'll use a Montgomery ladder.
2305
+ */
2306
+
2307
+ /*
2308
+ * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
2309
+ * Cost: 1M + 1I
2310
+ */
2311
+ static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
2312
+ {
2313
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2314
+ MPI_ECP_INV(&P->Z, &P->Z);
2315
+ MPI_ECP_MUL(&P->X, &P->X, &P->Z);
2316
+ MPI_ECP_LSET(&P->Z, 1);
2317
+
2318
+ cleanup:
2319
+ return ret;
2320
+ }
2321
+
2322
+ /*
2323
+ * Randomize projective x/z coordinates:
2324
+ * (X, Z) -> (l X, l Z) for random l
2325
+ * This is sort of the reverse operation of ecp_normalize_mxz().
2326
+ *
2327
+ * This countermeasure was first suggested in [2].
2328
+ * Cost: 2M
2329
+ */
2330
+ static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
2331
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
2332
+ {
2333
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2334
+ mbedtls_mpi l;
2335
+ mbedtls_mpi_init(&l);
2336
+
2337
+ /* Generate l such that 1 < l < p */
2338
+ MPI_ECP_RAND(&l);
2339
+
2340
+ MPI_ECP_MUL(&P->X, &P->X, &l);
2341
+ MPI_ECP_MUL(&P->Z, &P->Z, &l);
2342
+
2343
+ cleanup:
2344
+ mbedtls_mpi_free(&l);
2345
+
2346
+ if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
2347
+ ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
2348
+ }
2349
+ return ret;
2350
+ }
2351
+
2352
+ /*
2353
+ * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2354
+ * for Montgomery curves in x/z coordinates.
2355
+ *
2356
+ * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2357
+ * with
2358
+ * d = X1
2359
+ * P = (X2, Z2)
2360
+ * Q = (X3, Z3)
2361
+ * R = (X4, Z4)
2362
+ * S = (X5, Z5)
2363
+ * and eliminating temporary variables tO, ..., t4.
2364
+ *
2365
+ * Cost: 5M + 4S
2366
+ */
2367
+ static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
2368
+ mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2369
+ const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2370
+ const mbedtls_mpi *d,
2371
+ mbedtls_mpi T[4])
2372
+ {
2373
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2374
+
2375
+ MPI_ECP_ADD(&T[0], &P->X, &P->Z); /* Pp := PX + PZ */
2376
+ MPI_ECP_SUB(&T[1], &P->X, &P->Z); /* Pm := PX - PZ */
2377
+ MPI_ECP_ADD(&T[2], &Q->X, &Q->Z); /* Qp := QX + XZ */
2378
+ MPI_ECP_SUB(&T[3], &Q->X, &Q->Z); /* Qm := QX - QZ */
2379
+ MPI_ECP_MUL(&T[3], &T[3], &T[0]); /* Qm * Pp */
2380
+ MPI_ECP_MUL(&T[2], &T[2], &T[1]); /* Qp * Pm */
2381
+ MPI_ECP_SQR(&T[0], &T[0]); /* Pp^2 */
2382
+ MPI_ECP_SQR(&T[1], &T[1]); /* Pm^2 */
2383
+ MPI_ECP_MUL(&R->X, &T[0], &T[1]); /* Pp^2 * Pm^2 */
2384
+ MPI_ECP_SUB(&T[0], &T[0], &T[1]); /* Pp^2 - Pm^2 */
2385
+ MPI_ECP_MUL(&R->Z, &grp->A, &T[0]); /* A * (Pp^2 - Pm^2) */
2386
+ MPI_ECP_ADD(&R->Z, &T[1], &R->Z); /* [ A * (Pp^2-Pm^2) ] + Pm^2 */
2387
+ MPI_ECP_ADD(&S->X, &T[3], &T[2]); /* Qm*Pp + Qp*Pm */
2388
+ MPI_ECP_SQR(&S->X, &S->X); /* (Qm*Pp + Qp*Pm)^2 */
2389
+ MPI_ECP_SUB(&S->Z, &T[3], &T[2]); /* Qm*Pp - Qp*Pm */
2390
+ MPI_ECP_SQR(&S->Z, &S->Z); /* (Qm*Pp - Qp*Pm)^2 */
2391
+ MPI_ECP_MUL(&S->Z, d, &S->Z); /* d * ( Qm*Pp - Qp*Pm )^2 */
2392
+ MPI_ECP_MUL(&R->Z, &T[0], &R->Z); /* [A*(Pp^2-Pm^2)+Pm^2]*(Pp^2-Pm^2) */
2393
+
2394
+ cleanup:
2395
+
2396
+ return ret;
2397
+ }
2398
+
2399
+ /*
2400
+ * Multiplication with Montgomery ladder in x/z coordinates,
2401
+ * for curves in Montgomery form
2402
+ */
2403
+ static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2404
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2405
+ int (*f_rng)(void *, unsigned char *, size_t),
2406
+ void *p_rng)
2407
+ {
2408
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2409
+ size_t i;
2410
+ unsigned char b;
2411
+ mbedtls_ecp_point RP;
2412
+ mbedtls_mpi PX;
2413
+ mbedtls_mpi tmp[4];
2414
+ mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX);
2415
+
2416
+ mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2417
+
2418
+ if (f_rng == NULL) {
2419
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2420
+ }
2421
+
2422
+ /* Save PX and read from P before writing to R, in case P == R */
2423
+ MPI_ECP_MOV(&PX, &P->X);
2424
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P));
2425
+
2426
+ /* Set R to zero in modified x/z coordinates */
2427
+ MPI_ECP_LSET(&R->X, 1);
2428
+ MPI_ECP_LSET(&R->Z, 0);
2429
+ mbedtls_mpi_free(&R->Y);
2430
+
2431
+ /* RP.X might be slightly larger than P, so reduce it */
2432
+ MOD_ADD(&RP.X);
2433
+
2434
+ /* Randomize coordinates of the starting point */
2435
+ MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng));
2436
+
2437
+ /* Loop invariant: R = result so far, RP = R + P */
2438
+ i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */
2439
+ while (i-- > 0) {
2440
+ b = mbedtls_mpi_get_bit(m, i);
2441
+ /*
2442
+ * if (b) R = 2R + P else R = 2R,
2443
+ * which is:
2444
+ * if (b) double_add( RP, R, RP, R )
2445
+ * else double_add( R, RP, R, RP )
2446
+ * but using safe conditional swaps to avoid leaks
2447
+ */
2448
+ MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
2449
+ MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
2450
+ MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX, tmp));
2451
+ MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
2452
+ MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
2453
+ }
2454
+
2455
+ MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R));
2456
+
2457
+ cleanup:
2458
+ mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX);
2459
+
2460
+ mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2461
+ return ret;
2462
+ }
2463
+
2464
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2465
+
2466
+ /*
2467
+ * Restartable multiplication R = m * P
2468
+ *
2469
+ * This internal function can be called without an RNG in case where we know
2470
+ * the inputs are not sensitive.
2471
+ */
2472
+ static int ecp_mul_restartable_internal(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2473
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2474
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2475
+ mbedtls_ecp_restart_ctx *rs_ctx)
2476
+ {
2477
+ int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2478
+
2479
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2480
+ /* reset ops count for this call if top-level */
2481
+ if (rs_ctx != NULL && rs_ctx->depth++ == 0) {
2482
+ rs_ctx->ops_done = 0;
2483
+ }
2484
+ #else
2485
+ (void) rs_ctx;
2486
+ #endif
2487
+
2488
+ int restarting = 0;
2489
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2490
+ restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
2491
+ #endif
2492
+ /* skip argument check when restarting */
2493
+ if (!restarting) {
2494
+ /* check_privkey is free */
2495
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK);
2496
+
2497
+ /* Common sanity checks */
2498
+ MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m));
2499
+ MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2500
+ }
2501
+
2502
+ ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2503
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2504
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2505
+ MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng));
2506
+ }
2507
+ #endif
2508
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2509
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2510
+ MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx));
2511
+ }
2512
+ #endif
2513
+
2514
+ cleanup:
2515
+
2516
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2517
+ if (rs_ctx != NULL) {
2518
+ rs_ctx->depth--;
2519
+ }
2520
+ #endif
2521
+
2522
+ return ret;
2523
+ }
2524
+
2525
+ /*
2526
+ * Restartable multiplication R = m * P
2527
+ */
2528
+ int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2529
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2530
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2531
+ mbedtls_ecp_restart_ctx *rs_ctx)
2532
+ {
2533
+ if (f_rng == NULL) {
2534
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2535
+ }
2536
+
2537
+ return ecp_mul_restartable_internal(grp, R, m, P, f_rng, p_rng, rs_ctx);
2538
+ }
2539
+
2540
+ /*
2541
+ * Multiplication R = m * P
2542
+ */
2543
+ int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2544
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2545
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
2546
+ {
2547
+ return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL);
2548
+ }
2549
+ #endif /* MBEDTLS_ECP_C */
2550
+
2551
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2552
+ /*
2553
+ * Check that an affine point is valid as a public key,
2554
+ * short weierstrass curves (SEC1 3.2.3.1)
2555
+ */
2556
+ static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
2557
+ {
2558
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2559
+ mbedtls_mpi YY, RHS;
2560
+
2561
+ /* pt coordinates must be normalized for our checks */
2562
+ if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
2563
+ mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
2564
+ mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
2565
+ mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) {
2566
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2567
+ }
2568
+
2569
+ mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS);
2570
+
2571
+ /*
2572
+ * YY = Y^2
2573
+ * RHS = X^3 + A X + B
2574
+ */
2575
+ MPI_ECP_SQR(&YY, &pt->Y);
2576
+ MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, &RHS, &pt->X));
2577
+
2578
+ if (MPI_ECP_CMP(&YY, &RHS) != 0) {
2579
+ ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2580
+ }
2581
+
2582
+ cleanup:
2583
+
2584
+ mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS);
2585
+
2586
+ return ret;
2587
+ }
2588
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2589
+
2590
+ #if defined(MBEDTLS_ECP_C)
2591
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2592
+ /*
2593
+ * R = m * P with shortcuts for m == 0, m == 1 and m == -1
2594
+ * NOT constant-time - ONLY for short Weierstrass!
2595
+ */
2596
+ static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
2597
+ mbedtls_ecp_point *R,
2598
+ const mbedtls_mpi *m,
2599
+ const mbedtls_ecp_point *P,
2600
+ mbedtls_ecp_restart_ctx *rs_ctx)
2601
+ {
2602
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2603
+ mbedtls_mpi tmp;
2604
+ mbedtls_mpi_init(&tmp);
2605
+
2606
+ if (mbedtls_mpi_cmp_int(m, 0) == 0) {
2607
+ MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2608
+ MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R));
2609
+ } else if (mbedtls_mpi_cmp_int(m, 1) == 0) {
2610
+ MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2611
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2612
+ } else if (mbedtls_mpi_cmp_int(m, -1) == 0) {
2613
+ MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
2614
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
2615
+ MPI_ECP_NEG(&R->Y);
2616
+ } else {
2617
+ MBEDTLS_MPI_CHK(ecp_mul_restartable_internal(grp, R, m, P,
2618
+ NULL, NULL, rs_ctx));
2619
+ }
2620
+
2621
+ cleanup:
2622
+ mbedtls_mpi_free(&tmp);
2623
+
2624
+ return ret;
2625
+ }
2626
+
2627
+ /*
2628
+ * Restartable linear combination
2629
+ * NOT constant-time
2630
+ */
2631
+ int mbedtls_ecp_muladd_restartable(
2632
+ mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2633
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2634
+ const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2635
+ mbedtls_ecp_restart_ctx *rs_ctx)
2636
+ {
2637
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2638
+ mbedtls_ecp_point mP;
2639
+ mbedtls_ecp_point *pmP = &mP;
2640
+ mbedtls_ecp_point *pR = R;
2641
+ mbedtls_mpi tmp[4];
2642
+ if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2643
+ return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
2644
+ }
2645
+
2646
+ mbedtls_ecp_point_init(&mP);
2647
+ mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2648
+
2649
+ ECP_RS_ENTER(ma);
2650
+
2651
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2652
+ if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2653
+ /* redirect intermediate results to restart context */
2654
+ pmP = &rs_ctx->ma->mP;
2655
+ pR = &rs_ctx->ma->R;
2656
+
2657
+ /* jump to next operation */
2658
+ if (rs_ctx->ma->state == ecp_rsma_mul2) {
2659
+ goto mul2;
2660
+ }
2661
+ if (rs_ctx->ma->state == ecp_rsma_add) {
2662
+ goto add;
2663
+ }
2664
+ if (rs_ctx->ma->state == ecp_rsma_norm) {
2665
+ goto norm;
2666
+ }
2667
+ }
2668
+ #endif /* MBEDTLS_ECP_RESTARTABLE */
2669
+
2670
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx));
2671
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2672
+ if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2673
+ rs_ctx->ma->state = ecp_rsma_mul2;
2674
+ }
2675
+
2676
+ mul2:
2677
+ #endif
2678
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx));
2679
+
2680
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2681
+ if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2682
+ rs_ctx->ma->state = ecp_rsma_add;
2683
+ }
2684
+
2685
+ add:
2686
+ #endif
2687
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD);
2688
+ MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR, tmp));
2689
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2690
+ if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2691
+ rs_ctx->ma->state = ecp_rsma_norm;
2692
+ }
2693
+
2694
+ norm:
2695
+ #endif
2696
+ MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
2697
+ MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR));
2698
+
2699
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2700
+ if (rs_ctx != NULL && rs_ctx->ma != NULL) {
2701
+ MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR));
2702
+ }
2703
+ #endif
2704
+
2705
+ cleanup:
2706
+
2707
+ mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
2708
+
2709
+ mbedtls_ecp_point_free(&mP);
2710
+
2711
+ ECP_RS_LEAVE(ma);
2712
+
2713
+ return ret;
2714
+ }
2715
+
2716
+ /*
2717
+ * Linear combination
2718
+ * NOT constant-time
2719
+ */
2720
+ int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2721
+ const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2722
+ const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
2723
+ {
2724
+ return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL);
2725
+ }
2726
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2727
+ #endif /* MBEDTLS_ECP_C */
2728
+
2729
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2730
+ #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
2731
+ #define ECP_MPI_INIT(_p, _n) { .p = (mbedtls_mpi_uint *) (_p), .s = 1, .n = (_n) }
2732
+ #define ECP_MPI_INIT_ARRAY(x) \
2733
+ ECP_MPI_INIT(x, sizeof(x) / sizeof(mbedtls_mpi_uint))
2734
+ /*
2735
+ * Constants for the two points other than 0, 1, -1 (mod p) in
2736
+ * https://cr.yp.to/ecdh.html#validate
2737
+ * See ecp_check_pubkey_x25519().
2738
+ */
2739
+ static const mbedtls_mpi_uint x25519_bad_point_1[] = {
2740
+ MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae),
2741
+ MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a),
2742
+ MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd),
2743
+ MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00),
2744
+ };
2745
+ static const mbedtls_mpi_uint x25519_bad_point_2[] = {
2746
+ MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24),
2747
+ MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b),
2748
+ MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86),
2749
+ MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57),
2750
+ };
2751
+ static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
2752
+ x25519_bad_point_1);
2753
+ static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
2754
+ x25519_bad_point_2);
2755
+ #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
2756
+
2757
+ /*
2758
+ * Check that the input point is not one of the low-order points.
2759
+ * This is recommended by the "May the Fourth" paper:
2760
+ * https://eprint.iacr.org/2017/806.pdf
2761
+ * Those points are never sent by an honest peer.
2762
+ */
2763
+ static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P,
2764
+ const mbedtls_ecp_group_id grp_id)
2765
+ {
2766
+ int ret;
2767
+ mbedtls_mpi XmP;
2768
+
2769
+ mbedtls_mpi_init(&XmP);
2770
+
2771
+ /* Reduce X mod P so that we only need to check values less than P.
2772
+ * We know X < 2^256 so we can proceed by subtraction. */
2773
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X));
2774
+ while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) {
2775
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P));
2776
+ }
2777
+
2778
+ /* Check against the known bad values that are less than P. For Curve448
2779
+ * these are 0, 1 and -1. For Curve25519 we check the values less than P
2780
+ * from the following list: https://cr.yp.to/ecdh.html#validate */
2781
+ if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { /* takes care of 0 and 1 */
2782
+ ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2783
+ goto cleanup;
2784
+ }
2785
+
2786
+ #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
2787
+ if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
2788
+ if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) {
2789
+ ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2790
+ goto cleanup;
2791
+ }
2792
+
2793
+ if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) {
2794
+ ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2795
+ goto cleanup;
2796
+ }
2797
+ }
2798
+ #else
2799
+ (void) grp_id;
2800
+ #endif
2801
+
2802
+ /* Final check: check if XmP + 1 is P (final because it changes XmP!) */
2803
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1));
2804
+ if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) {
2805
+ ret = MBEDTLS_ERR_ECP_INVALID_KEY;
2806
+ goto cleanup;
2807
+ }
2808
+
2809
+ ret = 0;
2810
+
2811
+ cleanup:
2812
+ mbedtls_mpi_free(&XmP);
2813
+
2814
+ return ret;
2815
+ }
2816
+
2817
+ /*
2818
+ * Check validity of a public key for Montgomery curves with x-only schemes
2819
+ */
2820
+ static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
2821
+ {
2822
+ /* [Curve25519 p. 5] Just check X is the correct number of bytes */
2823
+ /* Allow any public value, if it's too big then we'll just reduce it mod p
2824
+ * (RFC 7748 sec. 5 para. 3). */
2825
+ if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) {
2826
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2827
+ }
2828
+
2829
+ /* Implicit in all standards (as they don't consider negative numbers):
2830
+ * X must be non-negative. This is normally ensured by the way it's
2831
+ * encoded for transmission, but let's be extra sure. */
2832
+ if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) {
2833
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2834
+ }
2835
+
2836
+ return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id);
2837
+ }
2838
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2839
+
2840
+ /*
2841
+ * Check that a point is valid as a public key
2842
+ */
2843
+ int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
2844
+ const mbedtls_ecp_point *pt)
2845
+ {
2846
+ /* Must use affine coordinates */
2847
+ if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) {
2848
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2849
+ }
2850
+
2851
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2852
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2853
+ return ecp_check_pubkey_mx(grp, pt);
2854
+ }
2855
+ #endif
2856
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2857
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2858
+ return ecp_check_pubkey_sw(grp, pt);
2859
+ }
2860
+ #endif
2861
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2862
+ }
2863
+
2864
+ /*
2865
+ * Check that an mbedtls_mpi is valid as a private key
2866
+ */
2867
+ int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
2868
+ const mbedtls_mpi *d)
2869
+ {
2870
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2871
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2872
+ /* see RFC 7748 sec. 5 para. 5 */
2873
+ if (mbedtls_mpi_get_bit(d, 0) != 0 ||
2874
+ mbedtls_mpi_get_bit(d, 1) != 0 ||
2875
+ mbedtls_mpi_bitlen(d) != grp->nbits + 1) { /* mbedtls_mpi_bitlen is one-based! */
2876
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2877
+ }
2878
+
2879
+ /* see [Curve25519] page 5 */
2880
+ if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) {
2881
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2882
+ }
2883
+
2884
+ return 0;
2885
+ }
2886
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2887
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2888
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2889
+ /* see SEC1 3.2 */
2890
+ if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
2891
+ mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
2892
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
2893
+ } else {
2894
+ return 0;
2895
+ }
2896
+ }
2897
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2898
+
2899
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2900
+ }
2901
+
2902
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2903
+ MBEDTLS_STATIC_TESTABLE
2904
+ int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
2905
+ mbedtls_mpi *d,
2906
+ int (*f_rng)(void *, unsigned char *, size_t),
2907
+ void *p_rng)
2908
+ {
2909
+ int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2910
+ size_t n_random_bytes = high_bit / 8 + 1;
2911
+
2912
+ /* [Curve25519] page 5 */
2913
+ /* Generate a (high_bit+1)-bit random number by generating just enough
2914
+ * random bytes, then shifting out extra bits from the top (necessary
2915
+ * when (high_bit+1) is not a multiple of 8). */
2916
+ MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes,
2917
+ f_rng, p_rng));
2918
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1));
2919
+
2920
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1));
2921
+
2922
+ /* Make sure the last two bits are unset for Curve448, three bits for
2923
+ Curve25519 */
2924
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0));
2925
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0));
2926
+ if (high_bit == 254) {
2927
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0));
2928
+ }
2929
+
2930
+ cleanup:
2931
+ return ret;
2932
+ }
2933
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2934
+
2935
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2936
+ static int mbedtls_ecp_gen_privkey_sw(
2937
+ const mbedtls_mpi *N, mbedtls_mpi *d,
2938
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
2939
+ {
2940
+ int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng);
2941
+ switch (ret) {
2942
+ case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
2943
+ return MBEDTLS_ERR_ECP_RANDOM_FAILED;
2944
+ default:
2945
+ return ret;
2946
+ }
2947
+ }
2948
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2949
+
2950
+ /*
2951
+ * Generate a private key
2952
+ */
2953
+ int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
2954
+ mbedtls_mpi *d,
2955
+ int (*f_rng)(void *, unsigned char *, size_t),
2956
+ void *p_rng)
2957
+ {
2958
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
2959
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
2960
+ return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng);
2961
+ }
2962
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
2963
+
2964
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
2965
+ if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
2966
+ return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng);
2967
+ }
2968
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
2969
+
2970
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
2971
+ }
2972
+
2973
+ #if defined(MBEDTLS_ECP_C)
2974
+ /*
2975
+ * Generate a keypair with configurable base point
2976
+ */
2977
+ int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
2978
+ const mbedtls_ecp_point *G,
2979
+ mbedtls_mpi *d, mbedtls_ecp_point *Q,
2980
+ int (*f_rng)(void *, unsigned char *, size_t),
2981
+ void *p_rng)
2982
+ {
2983
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2984
+ MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
2985
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng));
2986
+
2987
+ cleanup:
2988
+ return ret;
2989
+ }
2990
+
2991
+ /*
2992
+ * Generate key pair, wrapper for conventional base point
2993
+ */
2994
+ int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
2995
+ mbedtls_mpi *d, mbedtls_ecp_point *Q,
2996
+ int (*f_rng)(void *, unsigned char *, size_t),
2997
+ void *p_rng)
2998
+ {
2999
+ return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng);
3000
+ }
3001
+
3002
+ /*
3003
+ * Generate a keypair, prettier wrapper
3004
+ */
3005
+ int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3006
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
3007
+ {
3008
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3009
+ if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3010
+ return ret;
3011
+ }
3012
+
3013
+ return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng);
3014
+ }
3015
+ #endif /* MBEDTLS_ECP_C */
3016
+
3017
+ int mbedtls_ecp_set_public_key(mbedtls_ecp_group_id grp_id,
3018
+ mbedtls_ecp_keypair *key,
3019
+ const mbedtls_ecp_point *Q)
3020
+ {
3021
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3022
+
3023
+ if (key->grp.id == MBEDTLS_ECP_DP_NONE) {
3024
+ /* Group not set yet */
3025
+ if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3026
+ return ret;
3027
+ }
3028
+ } else if (key->grp.id != grp_id) {
3029
+ /* Group mismatch */
3030
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
3031
+ }
3032
+ return mbedtls_ecp_copy(&key->Q, Q);
3033
+ }
3034
+
3035
+
3036
+ #define ECP_CURVE25519_KEY_SIZE 32
3037
+ #define ECP_CURVE448_KEY_SIZE 56
3038
+ /*
3039
+ * Read a private key.
3040
+ */
3041
+ int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
3042
+ const unsigned char *buf, size_t buflen)
3043
+ {
3044
+ int ret = 0;
3045
+
3046
+ if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
3047
+ return ret;
3048
+ }
3049
+
3050
+ ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
3051
+
3052
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3053
+ if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3054
+ /*
3055
+ * Mask the key as mandated by RFC7748 for Curve25519 and Curve448.
3056
+ */
3057
+ if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
3058
+ if (buflen != ECP_CURVE25519_KEY_SIZE) {
3059
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
3060
+ }
3061
+
3062
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
3063
+
3064
+ /* Set the three least significant bits to 0 */
3065
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3066
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
3067
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0));
3068
+
3069
+ /* Set the most significant bit to 0 */
3070
+ MBEDTLS_MPI_CHK(
3071
+ mbedtls_mpi_set_bit(&key->d,
3072
+ ECP_CURVE25519_KEY_SIZE * 8 - 1, 0)
3073
+ );
3074
+
3075
+ /* Set the second most significant bit to 1 */
3076
+ MBEDTLS_MPI_CHK(
3077
+ mbedtls_mpi_set_bit(&key->d,
3078
+ ECP_CURVE25519_KEY_SIZE * 8 - 2, 1)
3079
+ );
3080
+ } else if (grp_id == MBEDTLS_ECP_DP_CURVE448) {
3081
+ if (buflen != ECP_CURVE448_KEY_SIZE) {
3082
+ return MBEDTLS_ERR_ECP_INVALID_KEY;
3083
+ }
3084
+
3085
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
3086
+
3087
+ /* Set the two least significant bits to 0 */
3088
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
3089
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
3090
+
3091
+ /* Set the most significant bit to 1 */
3092
+ MBEDTLS_MPI_CHK(
3093
+ mbedtls_mpi_set_bit(&key->d,
3094
+ ECP_CURVE448_KEY_SIZE * 8 - 1, 1)
3095
+ );
3096
+ }
3097
+ }
3098
+ #endif
3099
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3100
+ if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3101
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen));
3102
+ }
3103
+ #endif
3104
+
3105
+ if (ret == 0) {
3106
+ MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d));
3107
+ }
3108
+
3109
+ cleanup:
3110
+
3111
+ if (ret != 0) {
3112
+ mbedtls_mpi_free(&key->d);
3113
+ }
3114
+
3115
+ return ret;
3116
+ }
3117
+
3118
+ int mbedtls_ecp_write_key_ext(const mbedtls_ecp_keypair *key,
3119
+ size_t *olen, unsigned char *buf, size_t buflen)
3120
+ {
3121
+ size_t len = (key->grp.nbits + 7) / 8;
3122
+ if (len > buflen) {
3123
+ /* For robustness, ensure *olen <= buflen even on error. */
3124
+ *olen = 0;
3125
+ return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3126
+ }
3127
+ *olen = len;
3128
+
3129
+ /* Private key not set */
3130
+ if (key->d.n == 0) {
3131
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
3132
+ }
3133
+
3134
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3135
+ if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
3136
+ return mbedtls_mpi_write_binary_le(&key->d, buf, len);
3137
+ }
3138
+ #endif
3139
+
3140
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3141
+ if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
3142
+ return mbedtls_mpi_write_binary(&key->d, buf, len);
3143
+ }
3144
+ #endif
3145
+
3146
+ /* Private key set but no recognized curve type? This shouldn't happen. */
3147
+ return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3148
+ }
3149
+
3150
+ /*
3151
+ * Write a public key.
3152
+ */
3153
+ int mbedtls_ecp_write_public_key(const mbedtls_ecp_keypair *key,
3154
+ int format, size_t *olen,
3155
+ unsigned char *buf, size_t buflen)
3156
+ {
3157
+ return mbedtls_ecp_point_write_binary(&key->grp, &key->Q,
3158
+ format, olen, buf, buflen);
3159
+ }
3160
+
3161
+
3162
+ #if defined(MBEDTLS_ECP_C)
3163
+ /*
3164
+ * Check a public-private key pair
3165
+ */
3166
+ int mbedtls_ecp_check_pub_priv(
3167
+ const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
3168
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
3169
+ {
3170
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3171
+ mbedtls_ecp_point Q;
3172
+ mbedtls_ecp_group grp;
3173
+ if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
3174
+ pub->grp.id != prv->grp.id ||
3175
+ mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
3176
+ mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
3177
+ mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
3178
+ return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
3179
+ }
3180
+
3181
+ mbedtls_ecp_point_init(&Q);
3182
+ mbedtls_ecp_group_init(&grp);
3183
+
3184
+ /* mbedtls_ecp_mul() needs a non-const group... */
3185
+ mbedtls_ecp_group_copy(&grp, &prv->grp);
3186
+
3187
+ /* Also checks d is valid */
3188
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, f_rng, p_rng));
3189
+
3190
+ if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
3191
+ mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
3192
+ mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
3193
+ ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
3194
+ goto cleanup;
3195
+ }
3196
+
3197
+ cleanup:
3198
+ mbedtls_ecp_point_free(&Q);
3199
+ mbedtls_ecp_group_free(&grp);
3200
+
3201
+ return ret;
3202
+ }
3203
+
3204
+ int mbedtls_ecp_keypair_calc_public(mbedtls_ecp_keypair *key,
3205
+ int (*f_rng)(void *, unsigned char *, size_t),
3206
+ void *p_rng)
3207
+ {
3208
+ return mbedtls_ecp_mul(&key->grp, &key->Q, &key->d, &key->grp.G,
3209
+ f_rng, p_rng);
3210
+ }
3211
+ #endif /* MBEDTLS_ECP_C */
3212
+
3213
+ mbedtls_ecp_group_id mbedtls_ecp_keypair_get_group_id(
3214
+ const mbedtls_ecp_keypair *key)
3215
+ {
3216
+ return key->grp.id;
3217
+ }
3218
+
3219
+ /*
3220
+ * Export generic key-pair parameters.
3221
+ */
3222
+ int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
3223
+ mbedtls_mpi *d, mbedtls_ecp_point *Q)
3224
+ {
3225
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3226
+
3227
+ if (grp != NULL && (ret = mbedtls_ecp_group_copy(grp, &key->grp)) != 0) {
3228
+ return ret;
3229
+ }
3230
+
3231
+ if (d != NULL && (ret = mbedtls_mpi_copy(d, &key->d)) != 0) {
3232
+ return ret;
3233
+ }
3234
+
3235
+ if (Q != NULL && (ret = mbedtls_ecp_copy(Q, &key->Q)) != 0) {
3236
+ return ret;
3237
+ }
3238
+
3239
+ return 0;
3240
+ }
3241
+
3242
+ #if defined(MBEDTLS_SELF_TEST)
3243
+
3244
+ #if defined(MBEDTLS_ECP_C)
3245
+ /*
3246
+ * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
3247
+ *
3248
+ * This is the linear congruential generator from numerical recipes,
3249
+ * except we only use the low byte as the output. See
3250
+ * https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
3251
+ */
3252
+ static int self_test_rng(void *ctx, unsigned char *out, size_t len)
3253
+ {
3254
+ static uint32_t state = 42;
3255
+
3256
+ (void) ctx;
3257
+
3258
+ for (size_t i = 0; i < len; i++) {
3259
+ state = state * 1664525u + 1013904223u;
3260
+ out[i] = (unsigned char) state;
3261
+ }
3262
+
3263
+ return 0;
3264
+ }
3265
+
3266
+ /* Adjust the exponent to be a valid private point for the specified curve.
3267
+ * This is sometimes necessary because we use a single set of exponents
3268
+ * for all curves but the validity of values depends on the curve. */
3269
+ static int self_test_adjust_exponent(const mbedtls_ecp_group *grp,
3270
+ mbedtls_mpi *m)
3271
+ {
3272
+ int ret = 0;
3273
+ switch (grp->id) {
3274
+ /* If Curve25519 is available, then that's what we use for the
3275
+ * Montgomery test, so we don't need the adjustment code. */
3276
+ #if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3277
+ #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3278
+ case MBEDTLS_ECP_DP_CURVE448:
3279
+ /* Move highest bit from 254 to N-1. Setting bit N-1 is
3280
+ * necessary to enforce the highest-bit-set constraint. */
3281
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0));
3282
+ MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1));
3283
+ /* Copy second-highest bit from 253 to N-2. This is not
3284
+ * necessary but improves the test variety a bit. */
3285
+ MBEDTLS_MPI_CHK(
3286
+ mbedtls_mpi_set_bit(m, grp->nbits - 1,
3287
+ mbedtls_mpi_get_bit(m, 253)));
3288
+ break;
3289
+ #endif
3290
+ #endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
3291
+ default:
3292
+ /* Non-Montgomery curves and Curve25519 need no adjustment. */
3293
+ (void) grp;
3294
+ (void) m;
3295
+ goto cleanup;
3296
+ }
3297
+ cleanup:
3298
+ return ret;
3299
+ }
3300
+
3301
+ /* Calculate R = m.P for each m in exponents. Check that the number of
3302
+ * basic operations doesn't depend on the value of m. */
3303
+ static int self_test_point(int verbose,
3304
+ mbedtls_ecp_group *grp,
3305
+ mbedtls_ecp_point *R,
3306
+ mbedtls_mpi *m,
3307
+ const mbedtls_ecp_point *P,
3308
+ const char *const *exponents,
3309
+ size_t n_exponents)
3310
+ {
3311
+ int ret = 0;
3312
+ size_t i = 0;
3313
+ unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
3314
+ add_count = 0;
3315
+ dbl_count = 0;
3316
+ mul_count = 0;
3317
+
3318
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0]));
3319
+ MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3320
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
3321
+
3322
+ for (i = 1; i < n_exponents; i++) {
3323
+ add_c_prev = add_count;
3324
+ dbl_c_prev = dbl_count;
3325
+ mul_c_prev = mul_count;
3326
+ add_count = 0;
3327
+ dbl_count = 0;
3328
+ mul_count = 0;
3329
+
3330
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i]));
3331
+ MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
3332
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
3333
+
3334
+ if (add_count != add_c_prev ||
3335
+ dbl_count != dbl_c_prev ||
3336
+ mul_count != mul_c_prev) {
3337
+ ret = 1;
3338
+ break;
3339
+ }
3340
+ }
3341
+
3342
+ cleanup:
3343
+ if (verbose != 0) {
3344
+ if (ret != 0) {
3345
+ mbedtls_printf("failed (%u)\n", (unsigned int) i);
3346
+ } else {
3347
+ mbedtls_printf("passed\n");
3348
+ }
3349
+ }
3350
+ return ret;
3351
+ }
3352
+ #endif /* MBEDTLS_ECP_C */
3353
+
3354
+ /*
3355
+ * Checkup routine
3356
+ */
3357
+ int mbedtls_ecp_self_test(int verbose)
3358
+ {
3359
+ #if defined(MBEDTLS_ECP_C)
3360
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3361
+ mbedtls_ecp_group grp;
3362
+ mbedtls_ecp_point R, P;
3363
+ mbedtls_mpi m;
3364
+
3365
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3366
+ const char *sw_exponents[] =
3367
+ {
3368
+ "000000000000000000000000000000000000000000000001", /* one */
3369
+ "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
3370
+ "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
3371
+ "400000000000000000000000000000000000000000000000", /* one and zeros */
3372
+ "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
3373
+ "555555555555555555555555555555555555555555555555", /* 101010... */
3374
+ };
3375
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3376
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3377
+ const char *m_exponents[] =
3378
+ {
3379
+ /* Valid private values for Curve25519. In a build with Curve448
3380
+ * but not Curve25519, they will be adjusted in
3381
+ * self_test_adjust_exponent(). */
3382
+ "4000000000000000000000000000000000000000000000000000000000000000",
3383
+ "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
3384
+ "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
3385
+ "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
3386
+ "5555555555555555555555555555555555555555555555555555555555555550",
3387
+ "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
3388
+ };
3389
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3390
+
3391
+ mbedtls_ecp_group_init(&grp);
3392
+ mbedtls_ecp_point_init(&R);
3393
+ mbedtls_ecp_point_init(&P);
3394
+ mbedtls_mpi_init(&m);
3395
+
3396
+ #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
3397
+ /* Use secp256r1 if available, or any available curve */
3398
+ #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
3399
+ MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1));
3400
+ #else
3401
+ MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id));
3402
+ #endif
3403
+
3404
+ if (verbose != 0) {
3405
+ mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): ");
3406
+ }
3407
+ /* Do a dummy multiplication first to trigger precomputation */
3408
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2));
3409
+ MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, self_test_rng, NULL));
3410
+ ret = self_test_point(verbose,
3411
+ &grp, &R, &m, &grp.G,
3412
+ sw_exponents,
3413
+ sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3414
+ if (ret != 0) {
3415
+ goto cleanup;
3416
+ }
3417
+
3418
+ if (verbose != 0) {
3419
+ mbedtls_printf(" ECP SW test #2 (constant op_count, other point): ");
3420
+ }
3421
+ /* We computed P = 2G last time, use it */
3422
+ ret = self_test_point(verbose,
3423
+ &grp, &R, &m, &P,
3424
+ sw_exponents,
3425
+ sizeof(sw_exponents) / sizeof(sw_exponents[0]));
3426
+ if (ret != 0) {
3427
+ goto cleanup;
3428
+ }
3429
+
3430
+ mbedtls_ecp_group_free(&grp);
3431
+ mbedtls_ecp_point_free(&R);
3432
+ #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
3433
+
3434
+ #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
3435
+ if (verbose != 0) {
3436
+ mbedtls_printf(" ECP Montgomery test (constant op_count): ");
3437
+ }
3438
+ #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
3439
+ MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519));
3440
+ #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
3441
+ MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448));
3442
+ #else
3443
+ #error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
3444
+ #endif
3445
+ ret = self_test_point(verbose,
3446
+ &grp, &R, &m, &grp.G,
3447
+ m_exponents,
3448
+ sizeof(m_exponents) / sizeof(m_exponents[0]));
3449
+ if (ret != 0) {
3450
+ goto cleanup;
3451
+ }
3452
+ #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
3453
+
3454
+ cleanup:
3455
+
3456
+ if (ret < 0 && verbose != 0) {
3457
+ mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
3458
+ }
3459
+
3460
+ mbedtls_ecp_group_free(&grp);
3461
+ mbedtls_ecp_point_free(&R);
3462
+ mbedtls_ecp_point_free(&P);
3463
+ mbedtls_mpi_free(&m);
3464
+
3465
+ if (verbose != 0) {
3466
+ mbedtls_printf("\n");
3467
+ }
3468
+
3469
+ return ret;
3470
+ #else /* MBEDTLS_ECP_C */
3471
+ (void) verbose;
3472
+ return 0;
3473
+ #endif /* MBEDTLS_ECP_C */
3474
+ }
3475
+
3476
+ #endif /* MBEDTLS_SELF_TEST */
3477
+
3478
+ #endif /* MBEDTLS_ECP_LIGHT */