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,2720 @@
1
+ /*
2
+ * The RSA public-key cryptosystem
3
+ *
4
+ * Copyright The Mbed TLS Contributors
5
+ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
+ */
7
+
8
+ /*
9
+ * The following sources were referenced in the design of this implementation
10
+ * of the RSA algorithm:
11
+ *
12
+ * [1] A method for obtaining digital signatures and public-key cryptosystems
13
+ * R Rivest, A Shamir, and L Adleman
14
+ * http://people.csail.mit.edu/rivest/pubs.html#RSA78
15
+ *
16
+ * [2] Handbook of Applied Cryptography - 1997, Chapter 8
17
+ * Menezes, van Oorschot and Vanstone
18
+ *
19
+ * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
20
+ * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
21
+ * Stefan Mangard
22
+ * https://arxiv.org/abs/1702.08719v2
23
+ *
24
+ */
25
+
26
+ #include "tf_psa_crypto_common.h"
27
+
28
+ #if defined(MBEDTLS_RSA_C)
29
+
30
+ #include "mbedtls/private/rsa.h"
31
+ #include "bignum_core.h"
32
+ #include "bignum_internal.h"
33
+ #include "rsa_alt_helpers.h"
34
+ #include "rsa_internal.h"
35
+ #include "rsa_invasive.h"
36
+ #include "crypto_oid.h"
37
+ #include "mbedtls/asn1write.h"
38
+ #include "mbedtls/platform_util.h"
39
+ #include "mbedtls/private/error_common.h"
40
+ #include "constant_time_internal.h"
41
+ #include "mbedtls/constant_time.h"
42
+ #include "md_psa.h"
43
+
44
+ #include <string.h>
45
+
46
+ #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) && !defined(__NetBSD__)
47
+ #include <stdlib.h>
48
+ #endif
49
+
50
+ #include "mbedtls/platform.h"
51
+
52
+ #if defined(MBEDTLS_TEST_HOOKS)
53
+ void (*mbedtls_rsa_cf_secret)(const void *ptr, size_t size) = NULL;
54
+ #endif
55
+
56
+ /*
57
+ * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
58
+ *
59
+ * The value zero is never a valid value for an RSA parameter.
60
+ */
61
+ static int asn1_get_nonzero_mpi(unsigned char **p,
62
+ const unsigned char *end,
63
+ mbedtls_mpi *X)
64
+ {
65
+ int ret;
66
+
67
+ ret = mbedtls_asn1_get_mpi(p, end, X);
68
+ if (ret != 0) {
69
+ return ret;
70
+ }
71
+
72
+ if (mbedtls_mpi_cmp_int(X, 0) == 0) {
73
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
74
+ }
75
+
76
+ return 0;
77
+ }
78
+
79
+ int mbedtls_rsa_parse_key(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
80
+ {
81
+ int ret, version;
82
+ size_t len, bits;
83
+ unsigned char *p, *end;
84
+
85
+ #if defined(MBEDTLS_RSA_NO_CRT)
86
+ mbedtls_mpi T;
87
+ mbedtls_mpi_init(&T);
88
+ #endif /* !MBEDTLS_RSA_NO_CRT */
89
+
90
+ p = (unsigned char *) key;
91
+ end = p + keylen;
92
+
93
+ /*
94
+ * This function parses the RSAPrivateKey (PKCS#1)
95
+ *
96
+ * RSAPrivateKey ::= SEQUENCE {
97
+ * version Version,
98
+ * modulus INTEGER, -- n
99
+ * publicExponent INTEGER, -- e
100
+ * privateExponent INTEGER, -- d
101
+ * prime1 INTEGER, -- p
102
+ * prime2 INTEGER, -- q
103
+ * exponent1 INTEGER, -- d mod (p-1)
104
+ * exponent2 INTEGER, -- d mod (q-1)
105
+ * coefficient INTEGER, -- (inverse of q) mod p
106
+ * otherPrimeInfos OtherPrimeInfos OPTIONAL
107
+ * }
108
+ */
109
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
110
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
111
+ return ret;
112
+ }
113
+
114
+ if (end != p + len) {
115
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
116
+ }
117
+
118
+ if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) {
119
+ return ret;
120
+ }
121
+
122
+ if (version != 0) {
123
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
124
+ }
125
+
126
+ /* Import N */
127
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->N)) != 0) {
128
+ goto cleanup;
129
+ }
130
+
131
+ rsa->len = mbedtls_mpi_size(&rsa->N);
132
+
133
+ /* Import E */
134
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->E)) != 0) {
135
+ goto cleanup;
136
+ }
137
+
138
+ /* Import D */
139
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->D)) != 0) {
140
+ goto cleanup;
141
+ }
142
+
143
+ /* Import P */
144
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->P)) != 0) {
145
+ goto cleanup;
146
+ }
147
+
148
+ /* Import Q */
149
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->Q)) != 0) {
150
+ goto cleanup;
151
+ }
152
+
153
+ #if !defined(MBEDTLS_RSA_NO_CRT)
154
+ /*
155
+ * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
156
+ * that they can be easily recomputed from D, P and Q. However by
157
+ * parsing them from the PKCS1 structure it is possible to avoid
158
+ * recalculating them which both reduces the overhead of loading
159
+ * RSA private keys into memory and also avoids side channels which
160
+ * can arise when computing those values, since all of D, P, and Q
161
+ * are secret. See https://eprint.iacr.org/2020/055 for a
162
+ * description of one such attack.
163
+ */
164
+
165
+ /* Import DP */
166
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->DP)) != 0) {
167
+ goto cleanup;
168
+ }
169
+
170
+ /* Import DQ */
171
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->DQ)) != 0) {
172
+ goto cleanup;
173
+ }
174
+
175
+ /* Import QP */
176
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &rsa->QP)) != 0) {
177
+ goto cleanup;
178
+ }
179
+
180
+ #else
181
+ /* Verify existence of the CRT params */
182
+ if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
183
+ (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 ||
184
+ (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) {
185
+ goto cleanup;
186
+ }
187
+ #endif
188
+
189
+ /* This check here is a duplication of the one in "mbedtls_psa_rsa_load_representation"
190
+ * in "psa_crypto_rsa.c". The reason for which this is needed here is explained
191
+ * in issue tf-psa-crypto#562. */
192
+ bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
193
+ if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
194
+ ret = PSA_ERROR_NOT_SUPPORTED;
195
+ goto cleanup;
196
+ }
197
+
198
+ if ((ret = mbedtls_rsa_check_privkey(rsa)) != 0) {
199
+ goto cleanup;
200
+ }
201
+
202
+ if (p != end) {
203
+ ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
204
+ }
205
+
206
+ cleanup:
207
+ #if defined(MBEDTLS_RSA_NO_CRT)
208
+ mbedtls_mpi_free(&T);
209
+ #endif /* MBEDTLS_RSA_NO_CRT */
210
+
211
+ if (ret != 0) {
212
+ mbedtls_rsa_free(rsa);
213
+ }
214
+
215
+ return ret;
216
+ }
217
+
218
+ int mbedtls_rsa_parse_pubkey(mbedtls_rsa_context *rsa, const unsigned char *key, size_t keylen)
219
+ {
220
+ unsigned char *p = (unsigned char *) key;
221
+ unsigned char *end = (unsigned char *) (key + keylen);
222
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
223
+ size_t len;
224
+
225
+ /*
226
+ * RSAPublicKey ::= SEQUENCE {
227
+ * modulus INTEGER, -- n
228
+ * publicExponent INTEGER -- e
229
+ * }
230
+ */
231
+ mbedtls_mpi_init(&rsa->N);
232
+ mbedtls_mpi_init(&rsa->E);
233
+
234
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
235
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
236
+ goto exit;
237
+ }
238
+
239
+ if (end != p + len) {
240
+ ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
241
+ goto exit;
242
+ }
243
+
244
+ /* Import N */
245
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
246
+ goto exit;
247
+ }
248
+
249
+ if ((ret = mbedtls_mpi_read_binary(&rsa->N, p, len)) != 0) {
250
+ ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
251
+ goto exit;
252
+ }
253
+
254
+ rsa->len = mbedtls_mpi_size(&rsa->N);
255
+
256
+ p += len;
257
+
258
+ /* Import E */
259
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) {
260
+ goto exit;
261
+ }
262
+
263
+ if ((ret = mbedtls_mpi_read_binary(&rsa->E, p, len)) != 0) {
264
+ ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
265
+ goto exit;
266
+ }
267
+
268
+ p += len;
269
+
270
+ if (mbedtls_rsa_check_pubkey(rsa) != 0) {
271
+ ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
272
+ goto exit;
273
+ }
274
+
275
+ if (p != end) {
276
+ ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
277
+ }
278
+
279
+ exit:
280
+ if (ret != 0) {
281
+ mbedtls_mpi_free(&rsa->N);
282
+ mbedtls_mpi_free(&rsa->E);
283
+ }
284
+
285
+ return ret;
286
+ }
287
+
288
+ #define MBEDTLS_RSA_WRITE_MPI(m) \
289
+ do { \
290
+ if ((ret = mbedtls_asn1_write_mpi(p, start, m)) < 0) { \
291
+ goto end_of_export; \
292
+ } \
293
+ len += ret; \
294
+ } while (0)
295
+
296
+ int mbedtls_rsa_write_key(const mbedtls_rsa_context *rsa, unsigned char *start,
297
+ unsigned char **p)
298
+ {
299
+ size_t len = 0;
300
+ int ret;
301
+ #if defined(MBEDTLS_RSA_NO_CRT)
302
+ mbedtls_mpi DP, DQ, QP;
303
+
304
+ mbedtls_mpi_init(&DP); mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
305
+ #endif /* MBEDTLS_RSA_NO_CRT */
306
+
307
+ /*
308
+ * Export the parameters one after another to avoid simultaneous copies.
309
+ */
310
+
311
+ /* Export QP, DQ, DP */
312
+ #if !defined(MBEDTLS_RSA_NO_CRT)
313
+ MBEDTLS_RSA_WRITE_MPI(&rsa->QP);
314
+ MBEDTLS_RSA_WRITE_MPI(&rsa->DQ);
315
+ MBEDTLS_RSA_WRITE_MPI(&rsa->DP);
316
+ #else /* MBEDTLS_RSA_NO_CRT */
317
+ if ((ret = mbedtls_rsa_deduce_crt(&rsa->P, &rsa->Q, &rsa->D, &DP, &DQ, &QP)) != 0) {
318
+ goto end_of_export;
319
+ }
320
+ MBEDTLS_RSA_WRITE_MPI(&QP);
321
+ MBEDTLS_RSA_WRITE_MPI(&DQ);
322
+ MBEDTLS_RSA_WRITE_MPI(&DP);
323
+ #endif /* MBEDTLS_RSA_NO_CRT */
324
+
325
+ /* Export Q, P, D, E, N */
326
+ MBEDTLS_RSA_WRITE_MPI(&rsa->Q);
327
+ MBEDTLS_RSA_WRITE_MPI(&rsa->P);
328
+ MBEDTLS_RSA_WRITE_MPI(&rsa->D);
329
+ MBEDTLS_RSA_WRITE_MPI(&rsa->E);
330
+ MBEDTLS_RSA_WRITE_MPI(&rsa->N);
331
+
332
+ end_of_export:
333
+
334
+ #if defined(MBEDTLS_RSA_NO_CRT)
335
+ mbedtls_mpi_free(&DP); mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
336
+ #endif /* MBEDTLS_RSA_NO_CRT */
337
+
338
+ if (ret < 0) {
339
+ return ret;
340
+ }
341
+
342
+ MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
343
+ MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
344
+ MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
345
+ MBEDTLS_ASN1_CONSTRUCTED |
346
+ MBEDTLS_ASN1_SEQUENCE));
347
+
348
+ return (int) len;
349
+ }
350
+
351
+ /*
352
+ * RSAPublicKey ::= SEQUENCE {
353
+ * modulus INTEGER, -- n
354
+ * publicExponent INTEGER -- e
355
+ * }
356
+ */
357
+ int mbedtls_rsa_write_pubkey(const mbedtls_rsa_context *rsa, unsigned char *start,
358
+ unsigned char **p)
359
+ {
360
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
361
+ size_t len = 0;
362
+
363
+ /* Export E, N */
364
+ MBEDTLS_RSA_WRITE_MPI(&rsa->E);
365
+ MBEDTLS_RSA_WRITE_MPI(&rsa->N);
366
+
367
+ end_of_export:
368
+
369
+ if (ret < 0) {
370
+ return ret;
371
+ }
372
+
373
+ MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
374
+ MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
375
+ MBEDTLS_ASN1_SEQUENCE));
376
+
377
+ return (int) len;
378
+ }
379
+
380
+ #if defined(MBEDTLS_PKCS1_V15) && defined(MBEDTLS_RSA_C)
381
+
382
+ /*
383
+ * EME-PKCS1-v1_5 decoding, see documentation in rsa_internal.h
384
+ */
385
+ MBEDTLS_STATIC_TESTABLE int mbedtls_ct_rsaes_pkcs1_v15_unpadding(
386
+ unsigned char *input, size_t ilen,
387
+ unsigned char *output, size_t output_max_len, size_t *olen)
388
+ {
389
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
390
+ size_t i, plaintext_max_size;
391
+
392
+ /* The following variables take sensitive values: their value must
393
+ * not leak into the observable behavior of the function other than
394
+ * the designated outputs (output, olen, return value). Otherwise
395
+ * this would open the execution of the function to
396
+ * side-channel-based variants of the Bleichenbacher padding oracle
397
+ * attack. Potential side channels include overall timing, memory
398
+ * access patterns (especially visible to an adversary who has access
399
+ * to a shared memory cache), and branches (especially visible to
400
+ * an adversary who has access to a shared code cache or to a shared
401
+ * branch predictor). */
402
+ size_t pad_count = 0;
403
+ mbedtls_ct_condition_t bad;
404
+ mbedtls_ct_condition_t pad_done;
405
+ size_t plaintext_size = 0;
406
+ mbedtls_ct_condition_t output_too_large;
407
+
408
+ plaintext_max_size = (output_max_len > ilen - 11) ? ilen - 11
409
+ : output_max_len;
410
+
411
+ /* Check and get padding length in constant time and constant
412
+ * memory trace. The first byte must be 0. */
413
+ bad = mbedtls_ct_bool(input[0]);
414
+
415
+
416
+ /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00
417
+ * where PS must be at least 8 nonzero bytes. */
418
+ bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(input[1], MBEDTLS_RSA_CRYPT));
419
+
420
+ /* Read the whole buffer. Set pad_done to nonzero if we find
421
+ * the 0x00 byte and remember the padding length in pad_count. */
422
+ pad_done = MBEDTLS_CT_FALSE;
423
+ for (i = 2; i < ilen; i++) {
424
+ mbedtls_ct_condition_t found = mbedtls_ct_uint_eq(input[i], 0);
425
+ pad_done = mbedtls_ct_bool_or(pad_done, found);
426
+ pad_count += mbedtls_ct_uint_if_else_0(mbedtls_ct_bool_not(pad_done), 1);
427
+ }
428
+
429
+ /* If pad_done is still zero, there's no data, only unfinished padding. */
430
+ bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_not(pad_done));
431
+
432
+ /* There must be at least 8 bytes of padding. */
433
+ bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_gt(8, pad_count));
434
+
435
+ /* If the padding is valid, set plaintext_size to the number of
436
+ * remaining bytes after stripping the padding. If the padding
437
+ * is invalid, avoid leaking this fact through the size of the
438
+ * output: use the maximum message size that fits in the output
439
+ * buffer. Do it without branches to avoid leaking the padding
440
+ * validity through timing. RSA keys are small enough that all the
441
+ * size_t values involved fit in unsigned int. */
442
+ plaintext_size = mbedtls_ct_uint_if(
443
+ bad, (unsigned) plaintext_max_size,
444
+ (unsigned) (ilen - pad_count - 3));
445
+
446
+ /* Set output_too_large to 0 if the plaintext fits in the output
447
+ * buffer and to 1 otherwise. */
448
+ output_too_large = mbedtls_ct_uint_gt(plaintext_size,
449
+ plaintext_max_size);
450
+
451
+ /* Set ret without branches to avoid timing attacks. Return:
452
+ * - INVALID_PADDING if the padding is bad (bad != 0).
453
+ * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
454
+ * plaintext does not fit in the output buffer.
455
+ * - 0 if the padding is correct. */
456
+ ret = mbedtls_ct_error_if(
457
+ bad,
458
+ PSA_ERROR_INVALID_PADDING,
459
+ mbedtls_ct_error_if_else_0(output_too_large, PSA_ERROR_BUFFER_TOO_SMALL)
460
+ );
461
+
462
+ /* If the padding is bad or the plaintext is too large, zero the
463
+ * data that we're about to copy to the output buffer.
464
+ * We need to copy the same amount of data
465
+ * from the same buffer whether the padding is good or not to
466
+ * avoid leaking the padding validity through overall timing or
467
+ * through memory or cache access patterns. */
468
+ mbedtls_ct_zeroize_if(mbedtls_ct_bool_or(bad, output_too_large), input + 11, ilen - 11);
469
+
470
+ /* If the plaintext is too large, truncate it to the buffer size.
471
+ * Copy anyway to avoid revealing the length through timing, because
472
+ * revealing the length is as bad as revealing the padding validity
473
+ * for a Bleichenbacher attack. */
474
+ plaintext_size = mbedtls_ct_uint_if(output_too_large,
475
+ (unsigned) plaintext_max_size,
476
+ (unsigned) plaintext_size);
477
+
478
+ /* Move the plaintext to the leftmost position where it can start in
479
+ * the working buffer, i.e. make it start plaintext_max_size from
480
+ * the end of the buffer. Do this with a memory access trace that
481
+ * does not depend on the plaintext size. After this move, the
482
+ * starting location of the plaintext is no longer sensitive
483
+ * information. */
484
+ mbedtls_ct_memmove_left(input + ilen - plaintext_max_size,
485
+ plaintext_max_size,
486
+ plaintext_max_size - plaintext_size);
487
+
488
+ /* Finally copy the decrypted plaintext plus trailing zeros into the output
489
+ * buffer. If output_max_len is 0, then output may be an invalid pointer
490
+ * and the result of memcpy() would be undefined; prevent undefined
491
+ * behavior making sure to depend only on output_max_len (the size of the
492
+ * user-provided output buffer), which is independent from plaintext
493
+ * length, validity of padding, success of the decryption, and other
494
+ * secrets. */
495
+ if (output_max_len != 0) {
496
+ memcpy(output, input + ilen - plaintext_max_size, plaintext_max_size);
497
+ }
498
+
499
+ /* Report the amount of data we copied to the output buffer. In case
500
+ * of errors (bad padding or output too large), the value of *olen
501
+ * when this function returns is not specified. Making it equivalent
502
+ * to the good case limits the risks of leaking the padding validity. */
503
+ *olen = plaintext_size;
504
+
505
+ return ret;
506
+ }
507
+
508
+ #endif /* MBEDTLS_PKCS1_V15 && MBEDTLS_RSA_C */
509
+
510
+ /*
511
+ * Checks whether the context fields are set in such a way
512
+ * that the RSA primitives will be able to execute without error.
513
+ * It does *not* make guarantees for consistency of the parameters.
514
+ */
515
+ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
516
+ int blinding_needed)
517
+ {
518
+ #if !defined(MBEDTLS_RSA_NO_CRT)
519
+ /* blinding_needed is only used for NO_CRT to decide whether
520
+ * P,Q need to be present or not. */
521
+ ((void) blinding_needed);
522
+ #endif
523
+
524
+ if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
525
+ ctx->len > MBEDTLS_MPI_MAX_SIZE) {
526
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
527
+ }
528
+
529
+ /*
530
+ * 1. Modular exponentiation needs positive, odd moduli.
531
+ */
532
+
533
+ /* Modular exponentiation wrt. N is always used for
534
+ * RSA public key operations. */
535
+ if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
536
+ mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
537
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
538
+ }
539
+
540
+ #if !defined(MBEDTLS_RSA_NO_CRT)
541
+ /* Modular exponentiation for P and Q is only
542
+ * used for private key operations and if CRT
543
+ * is used. */
544
+ if (is_priv &&
545
+ (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
546
+ mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
547
+ mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
548
+ mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
549
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
550
+ }
551
+ #endif /* !MBEDTLS_RSA_NO_CRT */
552
+
553
+ /*
554
+ * 2. Exponents must be positive
555
+ */
556
+
557
+ /* Always need E for public key operations */
558
+ if (mbedtls_mpi_cmp_int(&ctx->E, 0) <= 0) {
559
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
560
+ }
561
+
562
+ #if defined(MBEDTLS_RSA_NO_CRT)
563
+ /* For private key operations, use D or DP & DQ
564
+ * as (unblinded) exponents. */
565
+ if (is_priv && mbedtls_mpi_cmp_int(&ctx->D, 0) <= 0) {
566
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
567
+ }
568
+ #else
569
+ if (is_priv &&
570
+ (mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
571
+ mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
572
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
573
+ }
574
+ #endif /* MBEDTLS_RSA_NO_CRT */
575
+
576
+ /* Blinding shouldn't make exponents negative either,
577
+ * so check that P, Q >= 1 if that hasn't yet been
578
+ * done as part of 1. */
579
+ #if defined(MBEDTLS_RSA_NO_CRT)
580
+ if (is_priv && blinding_needed &&
581
+ (mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
582
+ mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
583
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
584
+ }
585
+ #endif
586
+
587
+ /* It wouldn't lead to an error if it wasn't satisfied,
588
+ * but check for QP >= 1 nonetheless. */
589
+ #if !defined(MBEDTLS_RSA_NO_CRT)
590
+ if (is_priv &&
591
+ mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
592
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
593
+ }
594
+ #endif
595
+
596
+ return 0;
597
+ }
598
+
599
+ /*
600
+ * Initialize an RSA context
601
+ */
602
+ void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
603
+ {
604
+ memset(ctx, 0, sizeof(mbedtls_rsa_context));
605
+
606
+ ctx->padding = MBEDTLS_RSA_PKCS_V15;
607
+ ctx->hash_id = MBEDTLS_MD_NONE;
608
+ }
609
+
610
+ /*
611
+ * Set padding for an existing RSA context
612
+ */
613
+ int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
614
+ mbedtls_md_type_t hash_id)
615
+ {
616
+ switch (padding) {
617
+ #if defined(MBEDTLS_PKCS1_V15)
618
+ case MBEDTLS_RSA_PKCS_V15:
619
+ break;
620
+ #endif
621
+
622
+ #if defined(MBEDTLS_PKCS1_V21)
623
+ case MBEDTLS_RSA_PKCS_V21:
624
+ break;
625
+ #endif
626
+ default:
627
+ return PSA_ERROR_INVALID_PADDING;
628
+ }
629
+
630
+ #if defined(MBEDTLS_PKCS1_V21)
631
+ if ((padding == MBEDTLS_RSA_PKCS_V21) &&
632
+ (hash_id != MBEDTLS_MD_NONE)) {
633
+ /* Just make sure this hash is supported in this build. */
634
+ if (mbedtls_md_info_from_type(hash_id) == NULL) {
635
+ return PSA_ERROR_INVALID_PADDING;
636
+ }
637
+ }
638
+ #endif /* MBEDTLS_PKCS1_V21 */
639
+
640
+ ctx->padding = padding;
641
+ ctx->hash_id = hash_id;
642
+
643
+ return 0;
644
+ }
645
+
646
+ /*
647
+ * Get padding mode of initialized RSA context
648
+ */
649
+ int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
650
+ {
651
+ return ctx->padding;
652
+ }
653
+
654
+ /*
655
+ * Get hash identifier of mbedtls_md_type_t type
656
+ */
657
+ int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
658
+ {
659
+ return ctx->hash_id;
660
+ }
661
+
662
+ /*
663
+ * Get length in bits of RSA modulus
664
+ */
665
+ size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
666
+ {
667
+ return mbedtls_mpi_bitlen(&ctx->N);
668
+ }
669
+
670
+ /*
671
+ * Get length in bytes of RSA modulus
672
+ */
673
+ size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
674
+ {
675
+ return ctx->len;
676
+ }
677
+
678
+ #if defined(MBEDTLS_GENPRIME)
679
+
680
+ /*
681
+ * Generate an RSA keypair
682
+ *
683
+ * This generation method follows the RSA key pair generation procedure of
684
+ * FIPS 186-4 if 2^16 < exponent < 2^256 and nbits = 2048 or nbits = 3072.
685
+ */
686
+ int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
687
+ int (*f_rng)(void *, unsigned char *, size_t),
688
+ void *p_rng,
689
+ unsigned int nbits, int exponent)
690
+ {
691
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
692
+ mbedtls_mpi H;
693
+ int prime_quality = 0;
694
+
695
+ /*
696
+ * If the modulus is 1024 bit long or shorter, then the security strength of
697
+ * the RSA algorithm is less than or equal to 80 bits and therefore an error
698
+ * rate of 2^-80 is sufficient.
699
+ */
700
+ if (nbits > 1024) {
701
+ prime_quality = MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR;
702
+ }
703
+
704
+ mbedtls_mpi_init(&H);
705
+
706
+ if (exponent < 3 || nbits % 2 != 0) {
707
+ ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
708
+ goto cleanup;
709
+ }
710
+
711
+ if (nbits < MBEDTLS_RSA_GEN_KEY_MIN_BITS) {
712
+ ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
713
+ goto cleanup;
714
+ }
715
+
716
+ /*
717
+ * find primes P and Q with Q < P so that:
718
+ * 1. |P-Q| > 2^( nbits / 2 - 100 )
719
+ * 2. GCD( E, (P-1)*(Q-1) ) == 1
720
+ * 3. E^-1 mod LCM(P-1, Q-1) > 2^( nbits / 2 )
721
+ */
722
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->E, exponent));
723
+
724
+ do {
725
+ MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->P, nbits >> 1,
726
+ prime_quality, f_rng, p_rng));
727
+
728
+ MBEDTLS_MPI_CHK(mbedtls_mpi_gen_prime(&ctx->Q, nbits >> 1,
729
+ prime_quality, f_rng, p_rng));
730
+
731
+ /* make sure the difference between p and q is not too small (FIPS 186-4 §B.3.3 step 5.4) */
732
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&H, &ctx->P, &ctx->Q));
733
+ if (mbedtls_mpi_bitlen(&H) <= ((nbits >= 200) ? ((nbits >> 1) - 99) : 0)) {
734
+ continue;
735
+ }
736
+
737
+ /* not required by any standards, but some users rely on the fact that P > Q */
738
+ if (H.s < 0) {
739
+ mbedtls_mpi_swap(&ctx->P, &ctx->Q);
740
+ }
741
+
742
+ /* Compute D = E^-1 mod LCM(P-1, Q-1) (FIPS 186-4 §B.3.1 criterion 3(b))
743
+ * if it exists (FIPS 186-4 §B.3.1 criterion 2(a)) */
744
+ ret = mbedtls_rsa_deduce_private_exponent(&ctx->P, &ctx->Q, &ctx->E, &ctx->D);
745
+ if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
746
+ continue;
747
+ }
748
+ if (ret != 0) {
749
+ goto cleanup;
750
+ }
751
+
752
+ /* (FIPS 186-4 §B.3.1 criterion 3(a)) */
753
+ if (mbedtls_mpi_bitlen(&ctx->D) <= ((nbits + 1) / 2)) {
754
+ continue;
755
+ }
756
+
757
+ break;
758
+ } while (1);
759
+
760
+
761
+ /* N = P * Q */
762
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->N, &ctx->P, &ctx->Q));
763
+ ctx->len = mbedtls_mpi_size(&ctx->N);
764
+
765
+ #if !defined(MBEDTLS_RSA_NO_CRT)
766
+ /*
767
+ * DP = D mod (P - 1)
768
+ * DQ = D mod (Q - 1)
769
+ * QP = Q^-1 mod P
770
+ */
771
+ MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&ctx->P, &ctx->Q, &ctx->D,
772
+ &ctx->DP, &ctx->DQ, &ctx->QP));
773
+ #endif /* MBEDTLS_RSA_NO_CRT */
774
+
775
+ /* Double-check */
776
+ MBEDTLS_MPI_CHK(mbedtls_rsa_check_privkey(ctx));
777
+
778
+ cleanup:
779
+
780
+ mbedtls_mpi_free(&H);
781
+
782
+ if (ret != 0) {
783
+ mbedtls_rsa_free(ctx);
784
+
785
+ if ((-ret & ~0x7f) == 0) {
786
+ ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_KEY_GEN_FAILED, ret);
787
+ }
788
+ return ret;
789
+ }
790
+
791
+ return 0;
792
+ }
793
+
794
+ #endif /* MBEDTLS_GENPRIME */
795
+
796
+ /*
797
+ * Check a public RSA key
798
+ */
799
+ int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
800
+ {
801
+ if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0) {
802
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
803
+ }
804
+
805
+ if (mbedtls_mpi_bitlen(&ctx->N) < 128) {
806
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
807
+ }
808
+
809
+ if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
810
+ mbedtls_mpi_bitlen(&ctx->E) < 2 ||
811
+ mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
812
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
813
+ }
814
+
815
+ return 0;
816
+ }
817
+
818
+ /*
819
+ * Check for the consistency of all fields in an RSA private key context
820
+ */
821
+ int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
822
+ {
823
+ if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
824
+ rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
825
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
826
+ }
827
+
828
+ if (mbedtls_rsa_validate_params(&ctx->N, &ctx->P, &ctx->Q,
829
+ &ctx->D, &ctx->E, NULL, NULL) != 0) {
830
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
831
+ }
832
+
833
+ #if !defined(MBEDTLS_RSA_NO_CRT)
834
+ else if (mbedtls_rsa_validate_crt(&ctx->P, &ctx->Q, &ctx->D,
835
+ &ctx->DP, &ctx->DQ, &ctx->QP) != 0) {
836
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
837
+ }
838
+ #endif
839
+
840
+ return 0;
841
+ }
842
+
843
+ /*
844
+ * Check if contexts holding a public and private key match
845
+ */
846
+ int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
847
+ const mbedtls_rsa_context *prv)
848
+ {
849
+ if (mbedtls_rsa_check_pubkey(pub) != 0 ||
850
+ mbedtls_rsa_check_privkey(prv) != 0) {
851
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
852
+ }
853
+
854
+ if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
855
+ mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
856
+ return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
857
+ }
858
+
859
+ return 0;
860
+ }
861
+
862
+ /*
863
+ * Do an RSA public key operation
864
+ */
865
+ int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
866
+ const unsigned char *input,
867
+ unsigned char *output)
868
+ {
869
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
870
+ size_t olen;
871
+ mbedtls_mpi T;
872
+
873
+ if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */)) {
874
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
875
+ }
876
+
877
+ mbedtls_mpi_init(&T);
878
+
879
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
880
+
881
+ if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
882
+ ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
883
+ goto cleanup;
884
+ }
885
+
886
+ olen = ctx->len;
887
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod_unsafe(&T, &T, &ctx->E, &ctx->N, &ctx->RN));
888
+ MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
889
+
890
+ cleanup:
891
+
892
+ mbedtls_mpi_free(&T);
893
+
894
+ if (ret != 0) {
895
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PUBLIC_FAILED, ret);
896
+ }
897
+
898
+ return 0;
899
+ }
900
+
901
+ #if !defined(MBEDTLS_RSA_NO_CRT)
902
+ /*
903
+ * Compute T such that T = TP mod P and T = TQ mod Q.
904
+ * (This is the Chinese Remainder Theorem - CRT.)
905
+ */
906
+ static int rsa_apply_crt(mbedtls_mpi *T,
907
+ const mbedtls_mpi *TP,
908
+ const mbedtls_mpi *TQ,
909
+ const mbedtls_rsa_context *ctx)
910
+ {
911
+ int ret;
912
+
913
+ /*
914
+ * Set T = ((TP - TQ) * (Q^-1 mod P) mod P) * Q + TQ
915
+ *
916
+ * That way we have both:
917
+ * mod P: T = (TP - TQ) * (Q^-1 * Q) + TQ = (TP - TQ) * 1 + TQ = TP
918
+ * mod Q: T = (...) * Q + TQ = TQ
919
+ */
920
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(T, TP, TQ)); // T = TP - TQ
921
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(T, T, &ctx->QP)); // T *= Q^-1 mod P
922
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(T, T, &ctx->P)); // T %= P
923
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(T, T, &ctx->Q)); // T *= Q
924
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(T, T, TQ)); // T += TQ
925
+
926
+ cleanup:
927
+ return ret;
928
+ }
929
+ #endif
930
+
931
+ /* Generate random A and B such that A^-1 = B mod N */
932
+ static int rsa_gen_rand_with_inverse(const mbedtls_rsa_context *ctx,
933
+ mbedtls_mpi *A,
934
+ mbedtls_mpi *B,
935
+ int (*f_rng)(void *, unsigned char *, size_t),
936
+ void *p_rng)
937
+ {
938
+ #if defined(MBEDTLS_RSA_NO_CRT)
939
+ int ret;
940
+ mbedtls_mpi G;
941
+
942
+ mbedtls_mpi_init(&G);
943
+
944
+ MBEDTLS_MPI_CHK(mbedtls_mpi_random(A, 1, &ctx->N, f_rng, p_rng));
945
+ MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, B, A, &ctx->N));
946
+
947
+ if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
948
+ /* This happens if we're unlucky enough to draw a multiple of P or Q,
949
+ * or if (at least) one of them is not a prime, and we drew a multiple
950
+ * of one of its factors. */
951
+ ret = MBEDTLS_ERR_RSA_RNG_FAILED;
952
+ goto cleanup;
953
+ }
954
+
955
+ cleanup:
956
+ mbedtls_mpi_free(&G);
957
+
958
+ return ret;
959
+ #else
960
+ int ret;
961
+ mbedtls_mpi Ap, Aq, Bp, Bq, G;
962
+
963
+ mbedtls_mpi_init(&Ap); mbedtls_mpi_init(&Aq);
964
+ mbedtls_mpi_init(&Bp); mbedtls_mpi_init(&Bq);
965
+ mbedtls_mpi_init(&G);
966
+
967
+ /*
968
+ * Instead of generating A, B = A^-1 (mod N) directly, generate one Ap, Bp
969
+ * pair (mod P) and one pair (mod Q) and use Chinese Remainder Theorem to
970
+ * construct an A and B from those.
971
+ *
972
+ * This works because the CRT correspondence is a ring isomorphism between
973
+ * Z/NZ (integers mod N) and Z/PZ x Z/QZ (pairs of integers mod P and Q):
974
+ * - it is a bijection (one-to-one correspondence);
975
+ * - doing a ring operation (modular +, -, *, ^-1 when possible) on one side is
976
+ * the same as doing it on the other side.
977
+ * So, drawing uniformly at random an invertible A mod N is the same as
978
+ * drawing uniformly at random pairs of invertible Ap mod P, Aq mod Q.
979
+ */
980
+
981
+ /* Generate Ap in [1, P) and compute Bp = Ap^-1 mod P */
982
+ MBEDTLS_MPI_CHK(mbedtls_mpi_random(&Ap, 1, &ctx->P, f_rng, p_rng));
983
+ MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &Bp, &Ap, &ctx->P));
984
+ if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
985
+ /* This can only happen if P was not a prime. */
986
+ ret = MBEDTLS_ERR_RSA_RNG_FAILED;
987
+ goto cleanup;
988
+ }
989
+
990
+ /* Generate Aq in [1, Q) and compute Bq = Aq^-1 mod Q */
991
+ MBEDTLS_MPI_CHK(mbedtls_mpi_random(&Aq, 1, &ctx->Q, f_rng, p_rng));
992
+ MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &Bq, &Aq, &ctx->Q));
993
+ if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
994
+ /* This can only happen if Q was not a prime. */
995
+ ret = MBEDTLS_ERR_RSA_RNG_FAILED;
996
+ goto cleanup;
997
+ }
998
+
999
+ /* Reconstruct A and B */
1000
+ MBEDTLS_MPI_CHK(rsa_apply_crt(A, &Ap, &Aq, ctx));
1001
+ MBEDTLS_MPI_CHK(rsa_apply_crt(B, &Bp, &Bq, ctx));
1002
+
1003
+ cleanup:
1004
+ mbedtls_mpi_free(&Ap); mbedtls_mpi_free(&Aq);
1005
+ mbedtls_mpi_free(&Bp); mbedtls_mpi_free(&Bq);
1006
+ mbedtls_mpi_free(&G);
1007
+
1008
+ return ret;
1009
+ #endif
1010
+ }
1011
+
1012
+ /*
1013
+ * Generate or update blinding values, see section 10 of:
1014
+ * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
1015
+ * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
1016
+ * Berlin Heidelberg, 1996. p. 104-113.
1017
+ */
1018
+ static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
1019
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
1020
+ {
1021
+ int ret;
1022
+
1023
+ if (ctx->Vf.p != NULL) {
1024
+ /* We already have blinding values, just update them by squaring */
1025
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
1026
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
1027
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
1028
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));
1029
+ goto cleanup;
1030
+ }
1031
+
1032
+ /* Unblinding value: Vf = random number, invertible mod N */
1033
+ MBEDTLS_MPI_CHK(rsa_gen_rand_with_inverse(ctx, &ctx->Vf, &ctx->Vi, f_rng, p_rng));
1034
+
1035
+ /* Blinding value: Vi = Vf^(-e) mod N
1036
+ * (Vi already contains Vf^-1 at this point) */
1037
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN));
1038
+
1039
+ cleanup:
1040
+ return ret;
1041
+ }
1042
+
1043
+ /*
1044
+ * Unblind
1045
+ * T = T * Vf mod N
1046
+ */
1047
+ static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
1048
+ {
1049
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1050
+ const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
1051
+ const size_t nlimbs = N->n;
1052
+ const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
1053
+ mbedtls_mpi RR, M_T;
1054
+
1055
+ mbedtls_mpi_init(&RR);
1056
+ mbedtls_mpi_init(&M_T);
1057
+
1058
+ MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
1059
+ MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
1060
+
1061
+ MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
1062
+ MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
1063
+
1064
+ /* T = T * Vf mod N
1065
+ * Reminder: montmul(A, B, N) = A * B * R^-1 mod N
1066
+ * Usually both operands are multiplied by R mod N beforehand (by calling
1067
+ * `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
1068
+ * "in the Montgomery domain"). Here we only multiply one operand by R mod
1069
+ * N, so the result is directly what we want - no need to call
1070
+ * `from_mont_rep()` on it. */
1071
+ mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
1072
+ mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1073
+
1074
+ cleanup:
1075
+
1076
+ mbedtls_mpi_free(&RR);
1077
+ mbedtls_mpi_free(&M_T);
1078
+
1079
+ return ret;
1080
+ }
1081
+
1082
+ /*
1083
+ * Exponent blinding supposed to prevent side-channel attacks using multiple
1084
+ * traces of measurements to recover the RSA key. The more collisions are there,
1085
+ * the more bits of the key can be recovered. See [3].
1086
+ *
1087
+ * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
1088
+ * observations on average.
1089
+ *
1090
+ * For example with 28 byte blinding to achieve 2 collisions the adversary has
1091
+ * to make 2^112 observations on average.
1092
+ *
1093
+ * (With the currently (as of 2017 April) known best algorithms breaking 2048
1094
+ * bit RSA requires approximately as much time as trying out 2^112 random keys.
1095
+ * Thus in this sense with 28 byte blinding the security is not reduced by
1096
+ * side-channel attacks like the one in [3])
1097
+ *
1098
+ * This countermeasure does not help if the key recovery is possible with a
1099
+ * single trace.
1100
+ */
1101
+ #define RSA_EXPONENT_BLINDING 28
1102
+
1103
+ /*
1104
+ * Do an RSA private key operation
1105
+ */
1106
+ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1107
+ int (*f_rng)(void *, unsigned char *, size_t),
1108
+ void *p_rng,
1109
+ const unsigned char *input,
1110
+ unsigned char *output)
1111
+ {
1112
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1113
+ size_t olen;
1114
+
1115
+ /* Temporary holding the result */
1116
+ mbedtls_mpi T;
1117
+
1118
+ /* Temporaries holding P-1, Q-1 and the
1119
+ * exponent blinding factor, respectively. */
1120
+ mbedtls_mpi P1, Q1, R;
1121
+
1122
+ #if !defined(MBEDTLS_RSA_NO_CRT)
1123
+ /* Temporaries holding the results mod p resp. mod q. */
1124
+ mbedtls_mpi TP, TQ;
1125
+
1126
+ /* Temporaries holding the blinded exponents for
1127
+ * the mod p resp. mod q computation (if used). */
1128
+ mbedtls_mpi DP_blind, DQ_blind;
1129
+ #else
1130
+ /* Temporary holding the blinded exponent (if used). */
1131
+ mbedtls_mpi D_blind;
1132
+ #endif /* MBEDTLS_RSA_NO_CRT */
1133
+
1134
+ /* Temporaries holding the initial input and the double
1135
+ * checked result; should be the same in the end. */
1136
+ mbedtls_mpi input_blinded, check_result_blinded;
1137
+
1138
+ if (f_rng == NULL) {
1139
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1140
+ }
1141
+
1142
+ if (rsa_check_context(ctx, 1 /* private key checks */,
1143
+ 1 /* blinding on */) != 0) {
1144
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1145
+ }
1146
+
1147
+ /* MPI Initialization */
1148
+ mbedtls_mpi_init(&T);
1149
+
1150
+ mbedtls_mpi_init(&P1);
1151
+ mbedtls_mpi_init(&Q1);
1152
+ mbedtls_mpi_init(&R);
1153
+
1154
+ #if defined(MBEDTLS_RSA_NO_CRT)
1155
+ mbedtls_mpi_init(&D_blind);
1156
+ #else
1157
+ mbedtls_mpi_init(&DP_blind);
1158
+ mbedtls_mpi_init(&DQ_blind);
1159
+ #endif
1160
+
1161
+ #if !defined(MBEDTLS_RSA_NO_CRT)
1162
+ mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
1163
+ #endif
1164
+
1165
+ mbedtls_mpi_init(&input_blinded);
1166
+ mbedtls_mpi_init(&check_result_blinded);
1167
+
1168
+ /* End of MPI initialization */
1169
+
1170
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&T, input, ctx->len));
1171
+ if (mbedtls_mpi_cmp_mpi(&T, &ctx->N) >= 0) {
1172
+ ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1173
+ goto cleanup;
1174
+ }
1175
+
1176
+ /*
1177
+ * Blinding
1178
+ * T = T * Vi mod N
1179
+ */
1180
+ MBEDTLS_MPI_CHK(rsa_prepare_blinding(ctx, f_rng, p_rng));
1181
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
1182
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
1183
+
1184
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
1185
+
1186
+ /*
1187
+ * Exponent blinding
1188
+ */
1189
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&P1, &ctx->P, 1));
1190
+ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&Q1, &ctx->Q, 1));
1191
+
1192
+ #if defined(MBEDTLS_RSA_NO_CRT)
1193
+ /*
1194
+ * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
1195
+ */
1196
+ MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1197
+ f_rng, p_rng));
1198
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
1199
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
1200
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
1201
+ #else
1202
+ /*
1203
+ * DP_blind = ( P - 1 ) * R + DP
1204
+ */
1205
+ MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1206
+ f_rng, p_rng));
1207
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DP_blind, &P1, &R));
1208
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
1209
+ &ctx->DP));
1210
+
1211
+ /*
1212
+ * DQ_blind = ( Q - 1 ) * R + DQ
1213
+ */
1214
+ MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&R, RSA_EXPONENT_BLINDING,
1215
+ f_rng, p_rng));
1216
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
1217
+ MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
1218
+ &ctx->DQ));
1219
+ #endif /* MBEDTLS_RSA_NO_CRT */
1220
+
1221
+ #if defined(MBEDTLS_RSA_NO_CRT)
1222
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
1223
+ #else
1224
+ /*
1225
+ * Faster decryption using the CRT
1226
+ *
1227
+ * TP = input ^ dP mod P
1228
+ * TQ = input ^ dQ mod Q
1229
+ */
1230
+
1231
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1232
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
1233
+ MBEDTLS_MPI_CHK(rsa_apply_crt(&T, &TP, &TQ, ctx));
1234
+ #endif /* MBEDTLS_RSA_NO_CRT */
1235
+
1236
+ /* Verify the result to prevent glitching attacks. */
1237
+ MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
1238
+ &ctx->N, &ctx->RN));
1239
+ if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
1240
+ ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
1241
+ goto cleanup;
1242
+ }
1243
+
1244
+ /*
1245
+ * Unblind
1246
+ * T = T * Vf mod N
1247
+ */
1248
+ MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1249
+
1250
+ olen = ctx->len;
1251
+ MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
1252
+
1253
+ cleanup:
1254
+
1255
+ mbedtls_mpi_free(&P1);
1256
+ mbedtls_mpi_free(&Q1);
1257
+ mbedtls_mpi_free(&R);
1258
+
1259
+ #if defined(MBEDTLS_RSA_NO_CRT)
1260
+ mbedtls_mpi_free(&D_blind);
1261
+ #else
1262
+ mbedtls_mpi_free(&DP_blind);
1263
+ mbedtls_mpi_free(&DQ_blind);
1264
+ #endif
1265
+
1266
+ mbedtls_mpi_free(&T);
1267
+
1268
+ #if !defined(MBEDTLS_RSA_NO_CRT)
1269
+ mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
1270
+ #endif
1271
+
1272
+ mbedtls_mpi_free(&check_result_blinded);
1273
+ mbedtls_mpi_free(&input_blinded);
1274
+
1275
+ if (ret != 0 && ret >= -0x007f) {
1276
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);
1277
+ }
1278
+
1279
+ return ret;
1280
+ }
1281
+
1282
+ #if defined(MBEDTLS_PKCS1_V21)
1283
+ /**
1284
+ * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
1285
+ *
1286
+ * \param dst buffer to mask
1287
+ * \param dlen length of destination buffer
1288
+ * \param src source of the mask generation
1289
+ * \param slen length of the source buffer
1290
+ * \param md_alg message digest to use
1291
+ */
1292
+ static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
1293
+ size_t slen, mbedtls_md_type_t md_alg)
1294
+ {
1295
+ unsigned char counter[4];
1296
+ unsigned char *p;
1297
+ unsigned int hlen;
1298
+ size_t i, use_len;
1299
+ unsigned char mask[MBEDTLS_MD_MAX_SIZE];
1300
+ int ret = 0;
1301
+ const mbedtls_md_info_t *md_info;
1302
+ mbedtls_md_context_t md_ctx;
1303
+
1304
+ mbedtls_md_init(&md_ctx);
1305
+ md_info = mbedtls_md_info_from_type(md_alg);
1306
+ if (md_info == NULL) {
1307
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1308
+ }
1309
+
1310
+ mbedtls_md_init(&md_ctx);
1311
+ if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
1312
+ goto exit;
1313
+ }
1314
+
1315
+ hlen = mbedtls_md_get_size(md_info);
1316
+
1317
+ memset(mask, 0, sizeof(mask));
1318
+ memset(counter, 0, 4);
1319
+
1320
+ /* Generate and apply dbMask */
1321
+ p = dst;
1322
+
1323
+ while (dlen > 0) {
1324
+ use_len = hlen;
1325
+ if (dlen < hlen) {
1326
+ use_len = dlen;
1327
+ }
1328
+
1329
+ if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
1330
+ goto exit;
1331
+ }
1332
+ if ((ret = mbedtls_md_update(&md_ctx, src, slen)) != 0) {
1333
+ goto exit;
1334
+ }
1335
+ if ((ret = mbedtls_md_update(&md_ctx, counter, 4)) != 0) {
1336
+ goto exit;
1337
+ }
1338
+ if ((ret = mbedtls_md_finish(&md_ctx, mask)) != 0) {
1339
+ goto exit;
1340
+ }
1341
+
1342
+ for (i = 0; i < use_len; ++i) {
1343
+ *p++ ^= mask[i];
1344
+ }
1345
+
1346
+ counter[3]++;
1347
+
1348
+ dlen -= use_len;
1349
+ }
1350
+
1351
+ exit:
1352
+ mbedtls_platform_zeroize(mask, sizeof(mask));
1353
+ mbedtls_md_free(&md_ctx);
1354
+
1355
+ return ret;
1356
+ }
1357
+
1358
+ /**
1359
+ * Generate Hash(M') as in RFC 8017 page 43 points 5 and 6.
1360
+ *
1361
+ * \param hash the input hash
1362
+ * \param hlen length of the input hash
1363
+ * \param salt the input salt
1364
+ * \param slen length of the input salt
1365
+ * \param out the output buffer - must be large enough for \p md_alg
1366
+ * \param md_alg message digest to use
1367
+ */
1368
+ static int hash_mprime(const unsigned char *hash, size_t hlen,
1369
+ const unsigned char *salt, size_t slen,
1370
+ unsigned char *out, mbedtls_md_type_t md_alg)
1371
+ {
1372
+ const unsigned char zeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1373
+
1374
+ mbedtls_md_context_t md_ctx;
1375
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1376
+
1377
+ const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
1378
+ if (md_info == NULL) {
1379
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1380
+ }
1381
+
1382
+ mbedtls_md_init(&md_ctx);
1383
+ if ((ret = mbedtls_md_setup(&md_ctx, md_info, 0)) != 0) {
1384
+ goto exit;
1385
+ }
1386
+ if ((ret = mbedtls_md_starts(&md_ctx)) != 0) {
1387
+ goto exit;
1388
+ }
1389
+ if ((ret = mbedtls_md_update(&md_ctx, zeros, sizeof(zeros))) != 0) {
1390
+ goto exit;
1391
+ }
1392
+ if ((ret = mbedtls_md_update(&md_ctx, hash, hlen)) != 0) {
1393
+ goto exit;
1394
+ }
1395
+ if ((ret = mbedtls_md_update(&md_ctx, salt, slen)) != 0) {
1396
+ goto exit;
1397
+ }
1398
+ if ((ret = mbedtls_md_finish(&md_ctx, out)) != 0) {
1399
+ goto exit;
1400
+ }
1401
+
1402
+ exit:
1403
+ mbedtls_md_free(&md_ctx);
1404
+
1405
+ return ret;
1406
+ }
1407
+
1408
+ /**
1409
+ * Compute a hash.
1410
+ *
1411
+ * \param md_alg algorithm to use
1412
+ * \param input input message to hash
1413
+ * \param ilen input length
1414
+ * \param output the output buffer - must be large enough for \p md_alg
1415
+ */
1416
+ static int compute_hash(mbedtls_md_type_t md_alg,
1417
+ const unsigned char *input, size_t ilen,
1418
+ unsigned char *output)
1419
+ {
1420
+ const mbedtls_md_info_t *md_info;
1421
+
1422
+ md_info = mbedtls_md_info_from_type(md_alg);
1423
+ if (md_info == NULL) {
1424
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1425
+ }
1426
+
1427
+ return mbedtls_md(md_info, input, ilen, output);
1428
+ }
1429
+ #endif /* MBEDTLS_PKCS1_V21 */
1430
+
1431
+ #if defined(MBEDTLS_PKCS1_V21)
1432
+ /*
1433
+ * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
1434
+ */
1435
+ int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
1436
+ int (*f_rng)(void *, unsigned char *, size_t),
1437
+ void *p_rng,
1438
+ const unsigned char *label, size_t label_len,
1439
+ size_t ilen,
1440
+ const unsigned char *input,
1441
+ unsigned char *output)
1442
+ {
1443
+ size_t olen;
1444
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1445
+ unsigned char *p = output;
1446
+ unsigned int hlen;
1447
+
1448
+ if (f_rng == NULL) {
1449
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1450
+ }
1451
+
1452
+ hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
1453
+ if (hlen == 0) {
1454
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1455
+ }
1456
+
1457
+ olen = ctx->len;
1458
+
1459
+ /* first comparison checks for overflow */
1460
+ if (ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2) {
1461
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1462
+ }
1463
+
1464
+ memset(output, 0, olen);
1465
+
1466
+ *p++ = 0;
1467
+
1468
+ /* Generate a random octet string seed */
1469
+ if ((ret = f_rng(p_rng, p, hlen)) != 0) {
1470
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1471
+ }
1472
+
1473
+ p += hlen;
1474
+
1475
+ /* Construct DB */
1476
+ ret = compute_hash((mbedtls_md_type_t) ctx->hash_id, label, label_len, p);
1477
+ if (ret != 0) {
1478
+ return ret;
1479
+ }
1480
+ p += hlen;
1481
+ p += olen - 2 * hlen - 2 - ilen;
1482
+ *p++ = 1;
1483
+ if (ilen != 0) {
1484
+ memcpy(p, input, ilen);
1485
+ }
1486
+
1487
+ /* maskedDB: Apply dbMask to DB */
1488
+ if ((ret = mgf_mask(output + hlen + 1, olen - hlen - 1, output + 1, hlen,
1489
+ (mbedtls_md_type_t) ctx->hash_id)) != 0) {
1490
+ return ret;
1491
+ }
1492
+
1493
+ /* maskedSeed: Apply seedMask to seed */
1494
+ if ((ret = mgf_mask(output + 1, hlen, output + hlen + 1, olen - hlen - 1,
1495
+ (mbedtls_md_type_t) ctx->hash_id)) != 0) {
1496
+ return ret;
1497
+ }
1498
+
1499
+ return mbedtls_rsa_public(ctx, output, output);
1500
+ }
1501
+ #endif /* MBEDTLS_PKCS1_V21 */
1502
+
1503
+ #if defined(MBEDTLS_PKCS1_V15)
1504
+ /*
1505
+ * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
1506
+ */
1507
+ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
1508
+ int (*f_rng)(void *, unsigned char *, size_t),
1509
+ void *p_rng, size_t ilen,
1510
+ const unsigned char *input,
1511
+ unsigned char *output)
1512
+ {
1513
+ size_t nb_pad, olen;
1514
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1515
+ unsigned char *p = output;
1516
+
1517
+ olen = ctx->len;
1518
+
1519
+ /* first comparison checks for overflow */
1520
+ if (ilen + 11 < ilen || olen < ilen + 11) {
1521
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1522
+ }
1523
+
1524
+ nb_pad = olen - 3 - ilen;
1525
+
1526
+ *p++ = 0;
1527
+
1528
+ if (f_rng == NULL) {
1529
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1530
+ }
1531
+
1532
+ *p++ = MBEDTLS_RSA_CRYPT;
1533
+
1534
+ while (nb_pad-- > 0) {
1535
+ int rng_dl = 100;
1536
+
1537
+ do {
1538
+ ret = f_rng(p_rng, p, 1);
1539
+ } while (*p == 0 && --rng_dl && ret == 0);
1540
+
1541
+ /* Check if RNG failed to generate data */
1542
+ if (rng_dl == 0 || ret != 0) {
1543
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1544
+ }
1545
+
1546
+ p++;
1547
+ }
1548
+
1549
+ *p++ = 0;
1550
+ if (ilen != 0) {
1551
+ memcpy(p, input, ilen);
1552
+ }
1553
+
1554
+ return mbedtls_rsa_public(ctx, output, output);
1555
+ }
1556
+ #endif /* MBEDTLS_PKCS1_V15 */
1557
+
1558
+ /*
1559
+ * Add the message padding, then do an RSA operation
1560
+ */
1561
+ int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
1562
+ int (*f_rng)(void *, unsigned char *, size_t),
1563
+ void *p_rng,
1564
+ size_t ilen,
1565
+ const unsigned char *input,
1566
+ unsigned char *output)
1567
+ {
1568
+ switch (ctx->padding) {
1569
+ #if defined(MBEDTLS_PKCS1_V15)
1570
+ case MBEDTLS_RSA_PKCS_V15:
1571
+ return mbedtls_rsa_rsaes_pkcs1_v15_encrypt(ctx, f_rng, p_rng,
1572
+ ilen, input, output);
1573
+ #endif
1574
+
1575
+ #if defined(MBEDTLS_PKCS1_V21)
1576
+ case MBEDTLS_RSA_PKCS_V21:
1577
+ return mbedtls_rsa_rsaes_oaep_encrypt(ctx, f_rng, p_rng, NULL, 0,
1578
+ ilen, input, output);
1579
+ #endif
1580
+
1581
+ default:
1582
+ return PSA_ERROR_INVALID_PADDING;
1583
+ }
1584
+ }
1585
+
1586
+ #if defined(MBEDTLS_PKCS1_V21)
1587
+ /*
1588
+ * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
1589
+ */
1590
+ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
1591
+ int (*f_rng)(void *, unsigned char *, size_t),
1592
+ void *p_rng,
1593
+ const unsigned char *label, size_t label_len,
1594
+ size_t *olen,
1595
+ const unsigned char *input,
1596
+ unsigned char *output,
1597
+ size_t output_max_len)
1598
+ {
1599
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1600
+ size_t ilen, i, pad_len;
1601
+ unsigned char *p;
1602
+ mbedtls_ct_condition_t bad, in_padding;
1603
+ unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1604
+ unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
1605
+ unsigned int hlen;
1606
+
1607
+ /*
1608
+ * Parameters sanity checks
1609
+ */
1610
+ if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1611
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1612
+ }
1613
+
1614
+ ilen = ctx->len;
1615
+
1616
+ if (ilen < 16 || ilen > sizeof(buf)) {
1617
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1618
+ }
1619
+
1620
+ hlen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) ctx->hash_id);
1621
+ if (hlen == 0) {
1622
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1623
+ }
1624
+
1625
+ // checking for integer underflow
1626
+ if (2 * hlen + 2 > ilen) {
1627
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1628
+ }
1629
+
1630
+ /*
1631
+ * RSA operation
1632
+ */
1633
+ ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
1634
+
1635
+ if (ret != 0) {
1636
+ goto cleanup;
1637
+ }
1638
+
1639
+ /*
1640
+ * Unmask data and generate lHash
1641
+ */
1642
+ /* seed: Apply seedMask to maskedSeed */
1643
+ if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
1644
+ (mbedtls_md_type_t) ctx->hash_id)) != 0 ||
1645
+ /* DB: Apply dbMask to maskedDB */
1646
+ (ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
1647
+ (mbedtls_md_type_t) ctx->hash_id)) != 0) {
1648
+ goto cleanup;
1649
+ }
1650
+
1651
+ /* Generate lHash */
1652
+ ret = compute_hash((mbedtls_md_type_t) ctx->hash_id,
1653
+ label, label_len, lhash);
1654
+ if (ret != 0) {
1655
+ goto cleanup;
1656
+ }
1657
+
1658
+ /*
1659
+ * Check contents, in "constant-time"
1660
+ */
1661
+ p = buf;
1662
+
1663
+ bad = mbedtls_ct_bool(*p++); /* First byte must be 0 */
1664
+
1665
+ p += hlen; /* Skip seed */
1666
+
1667
+ /* Check lHash */
1668
+ bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool(mbedtls_ct_memcmp(lhash, p, hlen)));
1669
+ p += hlen;
1670
+
1671
+ /* Get zero-padding len, but always read till end of buffer
1672
+ * (minus one, for the 01 byte) */
1673
+ pad_len = 0;
1674
+ in_padding = MBEDTLS_CT_TRUE;
1675
+ for (i = 0; i < ilen - 2 * hlen - 2; i++) {
1676
+ in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_uint_eq(p[i], 0));
1677
+ pad_len += mbedtls_ct_uint_if_else_0(in_padding, 1);
1678
+ }
1679
+
1680
+ p += pad_len;
1681
+ bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_ne(*p++, 0x01));
1682
+
1683
+ /*
1684
+ * The only information "leaked" is whether the padding was correct or not
1685
+ * (eg, no data is copied if it was not correct). This meets the
1686
+ * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
1687
+ * the different error conditions.
1688
+ */
1689
+ if (bad != MBEDTLS_CT_FALSE) {
1690
+ ret = PSA_ERROR_INVALID_PADDING;
1691
+ goto cleanup;
1692
+ }
1693
+
1694
+ if (ilen - ((size_t) (p - buf)) > output_max_len) {
1695
+ ret = PSA_ERROR_BUFFER_TOO_SMALL;
1696
+ goto cleanup;
1697
+ }
1698
+
1699
+ *olen = ilen - ((size_t) (p - buf));
1700
+ if (*olen != 0) {
1701
+ memcpy(output, p, *olen);
1702
+ }
1703
+ ret = 0;
1704
+
1705
+ cleanup:
1706
+ mbedtls_platform_zeroize(buf, sizeof(buf));
1707
+ mbedtls_platform_zeroize(lhash, sizeof(lhash));
1708
+
1709
+ return ret;
1710
+ }
1711
+ #endif /* MBEDTLS_PKCS1_V21 */
1712
+
1713
+ #if defined(MBEDTLS_PKCS1_V15)
1714
+ /*
1715
+ * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1716
+ */
1717
+ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt_ext(mbedtls_rsa_context *ctx,
1718
+ int (*f_rng)(void *, unsigned char *, size_t),
1719
+ void *p_rng,
1720
+ size_t *olen,
1721
+ const unsigned char *input,
1722
+ unsigned char *output,
1723
+ size_t output_max_len,
1724
+ int *sensitive_ret)
1725
+ {
1726
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1727
+ size_t ilen;
1728
+ unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1729
+
1730
+ ilen = ctx->len;
1731
+
1732
+ *sensitive_ret = 0;
1733
+
1734
+ if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
1735
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1736
+ }
1737
+
1738
+ if (ilen < 16 || ilen > sizeof(buf)) {
1739
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1740
+ }
1741
+
1742
+ ret = mbedtls_rsa_private(ctx, f_rng, p_rng, input, buf);
1743
+ if (ret != 0) {
1744
+ goto cleanup;
1745
+ }
1746
+
1747
+ #if defined(MBEDTLS_TEST_HOOKS)
1748
+ if (mbedtls_rsa_cf_secret != NULL) {
1749
+ mbedtls_rsa_cf_secret(buf, sizeof(buf));
1750
+ }
1751
+ #endif
1752
+
1753
+ *sensitive_ret = mbedtls_ct_rsaes_pkcs1_v15_unpadding(
1754
+ buf, ilen, output, output_max_len, olen);
1755
+
1756
+ cleanup:
1757
+ mbedtls_platform_zeroize(buf, sizeof(buf));
1758
+
1759
+ return ret;
1760
+ }
1761
+
1762
+ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
1763
+ int (*f_rng)(void *, unsigned char *, size_t),
1764
+ void *p_rng,
1765
+ size_t *olen,
1766
+ const unsigned char *input,
1767
+ unsigned char *output,
1768
+ size_t output_max_len)
1769
+ {
1770
+ int sensitive_ret = 0;
1771
+ int ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt_ext(ctx, f_rng, p_rng, olen,
1772
+ input, output,
1773
+ output_max_len,
1774
+ &sensitive_ret);
1775
+ return ret | sensitive_ret;
1776
+ }
1777
+ #endif /* MBEDTLS_PKCS1_V15 */
1778
+
1779
+ /*
1780
+ * Do an RSA operation, then remove the message padding
1781
+ */
1782
+ int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
1783
+ int (*f_rng)(void *, unsigned char *, size_t),
1784
+ void *p_rng,
1785
+ size_t *olen,
1786
+ const unsigned char *input,
1787
+ unsigned char *output,
1788
+ size_t output_max_len)
1789
+ {
1790
+ switch (ctx->padding) {
1791
+ #if defined(MBEDTLS_PKCS1_V15)
1792
+ case MBEDTLS_RSA_PKCS_V15:
1793
+ return mbedtls_rsa_rsaes_pkcs1_v15_decrypt(ctx, f_rng, p_rng, olen,
1794
+ input, output, output_max_len);
1795
+ #endif
1796
+
1797
+ #if defined(MBEDTLS_PKCS1_V21)
1798
+ case MBEDTLS_RSA_PKCS_V21:
1799
+ return mbedtls_rsa_rsaes_oaep_decrypt(ctx, f_rng, p_rng, NULL, 0,
1800
+ olen, input, output,
1801
+ output_max_len);
1802
+ #endif
1803
+
1804
+ default:
1805
+ return PSA_ERROR_INVALID_PADDING;
1806
+ }
1807
+ }
1808
+
1809
+ #if defined(MBEDTLS_PKCS1_V21)
1810
+ static int rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
1811
+ int (*f_rng)(void *, unsigned char *, size_t),
1812
+ void *p_rng,
1813
+ mbedtls_md_type_t md_alg,
1814
+ unsigned int hashlen,
1815
+ const unsigned char *hash,
1816
+ int saltlen,
1817
+ unsigned char *sig)
1818
+ {
1819
+ size_t olen;
1820
+ unsigned char *p = sig;
1821
+ unsigned char *salt = NULL;
1822
+ size_t slen, min_slen, hlen, offset = 0;
1823
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1824
+ size_t msb;
1825
+ mbedtls_md_type_t hash_id;
1826
+
1827
+ if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
1828
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1829
+ }
1830
+
1831
+ if (f_rng == NULL) {
1832
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1833
+ }
1834
+
1835
+ olen = ctx->len;
1836
+
1837
+ if (md_alg != MBEDTLS_MD_NONE) {
1838
+ /* Gather length of hash to sign */
1839
+ size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
1840
+ if (exp_hashlen == 0) {
1841
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1842
+ }
1843
+
1844
+ if (hashlen != exp_hashlen) {
1845
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1846
+ }
1847
+ }
1848
+
1849
+ hash_id = (mbedtls_md_type_t) ctx->hash_id;
1850
+ if (hash_id == MBEDTLS_MD_NONE) {
1851
+ hash_id = md_alg;
1852
+ }
1853
+ hlen = mbedtls_md_get_size_from_type(hash_id);
1854
+ if (hlen == 0) {
1855
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1856
+ }
1857
+
1858
+ if (saltlen == MBEDTLS_RSA_SALT_LEN_ANY) {
1859
+ /* Calculate the largest possible salt length, up to the hash size.
1860
+ * Normally this is the hash length, which is the maximum salt length
1861
+ * according to FIPS 185-4 §5.5 (e) and common practice. If there is not
1862
+ * enough room, use the maximum salt length that fits. The constraint is
1863
+ * that the hash length plus the salt length plus 2 bytes must be at most
1864
+ * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017
1865
+ * (PKCS#1 v2.2) §9.1.1 step 3. */
1866
+ min_slen = hlen - 2;
1867
+ if (olen < hlen + min_slen + 2) {
1868
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1869
+ } else if (olen >= hlen + hlen + 2) {
1870
+ slen = hlen;
1871
+ } else {
1872
+ slen = olen - hlen - 2;
1873
+ }
1874
+ } else if ((saltlen < 0) || (saltlen + hlen + 2 > olen)) {
1875
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1876
+ } else {
1877
+ slen = (size_t) saltlen;
1878
+ }
1879
+
1880
+ memset(sig, 0, olen);
1881
+
1882
+ /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
1883
+ msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1884
+ p += olen - hlen - slen - 2;
1885
+ *p++ = 0x01;
1886
+
1887
+ /* Generate salt of length slen in place in the encoded message */
1888
+ salt = p;
1889
+ if ((ret = f_rng(p_rng, salt, slen)) != 0) {
1890
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_RNG_FAILED, ret);
1891
+ }
1892
+
1893
+ p += slen;
1894
+
1895
+ /* Generate H = Hash( M' ) */
1896
+ ret = hash_mprime(hash, hashlen, salt, slen, p, hash_id);
1897
+ if (ret != 0) {
1898
+ return ret;
1899
+ }
1900
+
1901
+ /* Compensate for boundary condition when applying mask */
1902
+ if (msb % 8 == 0) {
1903
+ offset = 1;
1904
+ }
1905
+
1906
+ /* maskedDB: Apply dbMask to DB */
1907
+ ret = mgf_mask(sig + offset, olen - hlen - 1 - offset, p, hlen, hash_id);
1908
+ if (ret != 0) {
1909
+ return ret;
1910
+ }
1911
+
1912
+ msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
1913
+ sig[0] &= 0xFF >> (olen * 8 - msb);
1914
+
1915
+ p += hlen;
1916
+ *p++ = 0xBC;
1917
+
1918
+ return mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig);
1919
+ }
1920
+
1921
+ static int rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
1922
+ int (*f_rng)(void *, unsigned char *, size_t),
1923
+ void *p_rng,
1924
+ mbedtls_md_type_t md_alg,
1925
+ unsigned int hashlen,
1926
+ const unsigned char *hash,
1927
+ int saltlen,
1928
+ unsigned char *sig)
1929
+ {
1930
+ if (ctx->padding != MBEDTLS_RSA_PKCS_V21) {
1931
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1932
+ }
1933
+ if ((ctx->hash_id == MBEDTLS_MD_NONE) && (md_alg == MBEDTLS_MD_NONE)) {
1934
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1935
+ }
1936
+ return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg, hashlen, hash, saltlen,
1937
+ sig);
1938
+ }
1939
+
1940
+ int mbedtls_rsa_rsassa_pss_sign_no_mode_check(mbedtls_rsa_context *ctx,
1941
+ int (*f_rng)(void *, unsigned char *, size_t),
1942
+ void *p_rng,
1943
+ mbedtls_md_type_t md_alg,
1944
+ unsigned int hashlen,
1945
+ const unsigned char *hash,
1946
+ unsigned char *sig)
1947
+ {
1948
+ return rsa_rsassa_pss_sign_no_mode_check(ctx, f_rng, p_rng, md_alg,
1949
+ hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
1950
+ }
1951
+
1952
+ /*
1953
+ * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function with
1954
+ * the option to pass in the salt length.
1955
+ */
1956
+ int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
1957
+ int (*f_rng)(void *, unsigned char *, size_t),
1958
+ void *p_rng,
1959
+ mbedtls_md_type_t md_alg,
1960
+ unsigned int hashlen,
1961
+ const unsigned char *hash,
1962
+ int saltlen,
1963
+ unsigned char *sig)
1964
+ {
1965
+ return rsa_rsassa_pss_sign(ctx, f_rng, p_rng, md_alg,
1966
+ hashlen, hash, saltlen, sig);
1967
+ }
1968
+ #endif /* MBEDTLS_PKCS1_V21 */
1969
+
1970
+ #if defined(MBEDTLS_PKCS1_V15)
1971
+ /*
1972
+ * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1973
+ */
1974
+
1975
+ /* Construct a PKCS v1.5 encoding of a hashed message
1976
+ *
1977
+ * This is used both for signature generation and verification.
1978
+ *
1979
+ * Parameters:
1980
+ * - md_alg: Identifies the hash algorithm used to generate the given hash;
1981
+ * MBEDTLS_MD_NONE if raw data is signed.
1982
+ * - hashlen: Length of hash. Must match md_alg if that's not NONE.
1983
+ * - hash: Buffer containing the hashed message or the raw data.
1984
+ * - dst_len: Length of the encoded message.
1985
+ * - dst: Buffer to hold the encoded message.
1986
+ *
1987
+ * Assumptions:
1988
+ * - hash has size hashlen.
1989
+ * - dst points to a buffer of size at least dst_len.
1990
+ *
1991
+ */
1992
+ static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
1993
+ unsigned int hashlen,
1994
+ const unsigned char *hash,
1995
+ size_t dst_len,
1996
+ unsigned char *dst)
1997
+ {
1998
+ size_t oid_size = 0;
1999
+ size_t nb_pad = dst_len;
2000
+ unsigned char *p = dst;
2001
+ const char *oid = NULL;
2002
+
2003
+ /* Are we signing hashed or raw data? */
2004
+ if (md_alg != MBEDTLS_MD_NONE) {
2005
+ unsigned char md_size = mbedtls_md_get_size_from_type(md_alg);
2006
+ if (md_size == 0) {
2007
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2008
+ }
2009
+
2010
+ if (mbedtls_oid_get_oid_by_md(md_alg, &oid, &oid_size) != 0) {
2011
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2012
+ }
2013
+
2014
+ if (hashlen != md_size) {
2015
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2016
+ }
2017
+
2018
+ /* Double-check that 8 + hashlen + oid_size can be used as a
2019
+ * 1-byte ASN.1 length encoding and that there's no overflow. */
2020
+ if (8 + hashlen + oid_size >= 0x80 ||
2021
+ 10 + hashlen < hashlen ||
2022
+ 10 + hashlen + oid_size < 10 + hashlen) {
2023
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2024
+ }
2025
+
2026
+ /*
2027
+ * Static bounds check:
2028
+ * - Need 10 bytes for five tag-length pairs.
2029
+ * (Insist on 1-byte length encodings to protect against variants of
2030
+ * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification)
2031
+ * - Need hashlen bytes for hash
2032
+ * - Need oid_size bytes for hash alg OID.
2033
+ */
2034
+ if (nb_pad < 10 + hashlen + oid_size) {
2035
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2036
+ }
2037
+ nb_pad -= 10 + hashlen + oid_size;
2038
+ } else {
2039
+ if (nb_pad < hashlen) {
2040
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2041
+ }
2042
+
2043
+ nb_pad -= hashlen;
2044
+ }
2045
+
2046
+ /* Need space for signature header and padding delimiter (3 bytes),
2047
+ * and 8 bytes for the minimal padding */
2048
+ if (nb_pad < 3 + 8) {
2049
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2050
+ }
2051
+ nb_pad -= 3;
2052
+
2053
+ /* Now nb_pad is the amount of memory to be filled
2054
+ * with padding, and at least 8 bytes long. */
2055
+
2056
+ /* Write signature header and padding */
2057
+ *p++ = 0;
2058
+ *p++ = MBEDTLS_RSA_SIGN;
2059
+ memset(p, 0xFF, nb_pad);
2060
+ p += nb_pad;
2061
+ *p++ = 0;
2062
+
2063
+ /* Are we signing raw data? */
2064
+ if (md_alg == MBEDTLS_MD_NONE) {
2065
+ memcpy(p, hash, hashlen);
2066
+ return 0;
2067
+ }
2068
+
2069
+ /* Signing hashed data, add corresponding ASN.1 structure
2070
+ *
2071
+ * DigestInfo ::= SEQUENCE {
2072
+ * digestAlgorithm DigestAlgorithmIdentifier,
2073
+ * digest Digest }
2074
+ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
2075
+ * Digest ::= OCTET STRING
2076
+ *
2077
+ * Schematic:
2078
+ * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ]
2079
+ * TAG-NULL + LEN [ NULL ] ]
2080
+ * TAG-OCTET + LEN [ HASH ] ]
2081
+ */
2082
+ *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
2083
+ *p++ = (unsigned char) (0x08 + oid_size + hashlen);
2084
+ *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
2085
+ *p++ = (unsigned char) (0x04 + oid_size);
2086
+ *p++ = MBEDTLS_ASN1_OID;
2087
+ *p++ = (unsigned char) oid_size;
2088
+ memcpy(p, oid, oid_size);
2089
+ p += oid_size;
2090
+ *p++ = MBEDTLS_ASN1_NULL;
2091
+ *p++ = 0x00;
2092
+ *p++ = MBEDTLS_ASN1_OCTET_STRING;
2093
+ *p++ = (unsigned char) hashlen;
2094
+ memcpy(p, hash, hashlen);
2095
+ p += hashlen;
2096
+
2097
+ /* Just a sanity-check, should be automatic
2098
+ * after the initial bounds check. */
2099
+ if (p != dst + dst_len) {
2100
+ mbedtls_platform_zeroize(dst, dst_len);
2101
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2102
+ }
2103
+
2104
+ return 0;
2105
+ }
2106
+
2107
+ /*
2108
+ * Do an RSA operation to sign the message digest
2109
+ */
2110
+ int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
2111
+ int (*f_rng)(void *, unsigned char *, size_t),
2112
+ void *p_rng,
2113
+ mbedtls_md_type_t md_alg,
2114
+ unsigned int hashlen,
2115
+ const unsigned char *hash,
2116
+ unsigned char *sig)
2117
+ {
2118
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2119
+ unsigned char *sig_try = NULL, *verif = NULL;
2120
+
2121
+ if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
2122
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2123
+ }
2124
+
2125
+ if (ctx->padding != MBEDTLS_RSA_PKCS_V15) {
2126
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2127
+ }
2128
+
2129
+ /*
2130
+ * Prepare PKCS1-v1.5 encoding (padding and hash identifier)
2131
+ */
2132
+
2133
+ if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash,
2134
+ ctx->len, sig)) != 0) {
2135
+ return ret;
2136
+ }
2137
+
2138
+ /* Private key operation
2139
+ *
2140
+ * In order to prevent Lenstra's attack, make the signature in a
2141
+ * temporary buffer and check it before returning it.
2142
+ */
2143
+
2144
+ sig_try = mbedtls_calloc(1, ctx->len);
2145
+ if (sig_try == NULL) {
2146
+ return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2147
+ }
2148
+
2149
+ verif = mbedtls_calloc(1, ctx->len);
2150
+ if (verif == NULL) {
2151
+ mbedtls_free(sig_try);
2152
+ return MBEDTLS_ERR_MPI_ALLOC_FAILED;
2153
+ }
2154
+
2155
+ MBEDTLS_MPI_CHK(mbedtls_rsa_private(ctx, f_rng, p_rng, sig, sig_try));
2156
+ MBEDTLS_MPI_CHK(mbedtls_rsa_public(ctx, sig_try, verif));
2157
+
2158
+ if (mbedtls_ct_memcmp(verif, sig, ctx->len) != 0) {
2159
+ ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
2160
+ goto cleanup;
2161
+ }
2162
+
2163
+ memcpy(sig, sig_try, ctx->len);
2164
+
2165
+ cleanup:
2166
+ mbedtls_zeroize_and_free(sig_try, ctx->len);
2167
+ mbedtls_zeroize_and_free(verif, ctx->len);
2168
+
2169
+ if (ret != 0) {
2170
+ memset(sig, '!', ctx->len);
2171
+ }
2172
+ return ret;
2173
+ }
2174
+ #endif /* MBEDTLS_PKCS1_V15 */
2175
+
2176
+ /*
2177
+ * Do an RSA operation to sign the message digest
2178
+ */
2179
+ int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
2180
+ int (*f_rng)(void *, unsigned char *, size_t),
2181
+ void *p_rng,
2182
+ mbedtls_md_type_t md_alg,
2183
+ unsigned int hashlen,
2184
+ const unsigned char *hash,
2185
+ unsigned char *sig)
2186
+ {
2187
+ if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
2188
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2189
+ }
2190
+
2191
+ switch (ctx->padding) {
2192
+ #if defined(MBEDTLS_PKCS1_V15)
2193
+ case MBEDTLS_RSA_PKCS_V15:
2194
+ return mbedtls_rsa_rsassa_pkcs1_v15_sign(ctx, f_rng, p_rng,
2195
+ md_alg, hashlen, hash, sig);
2196
+ #endif
2197
+
2198
+ #if defined(MBEDTLS_PKCS1_V21)
2199
+ case MBEDTLS_RSA_PKCS_V21:
2200
+ return mbedtls_rsa_rsassa_pss_sign_ext(ctx, f_rng, p_rng, md_alg,
2201
+ hashlen, hash, MBEDTLS_RSA_SALT_LEN_ANY, sig);
2202
+ #endif
2203
+
2204
+ default:
2205
+ return PSA_ERROR_INVALID_PADDING;
2206
+ }
2207
+ }
2208
+
2209
+ #if defined(MBEDTLS_PKCS1_V21)
2210
+ /*
2211
+ * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
2212
+ */
2213
+ int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
2214
+ mbedtls_md_type_t md_alg,
2215
+ unsigned int hashlen,
2216
+ const unsigned char *hash,
2217
+ mbedtls_md_type_t mgf1_hash_id,
2218
+ int expected_salt_len,
2219
+ const unsigned char *sig)
2220
+ {
2221
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2222
+ size_t siglen;
2223
+ unsigned char *p;
2224
+ unsigned char *hash_start;
2225
+ unsigned char result[MBEDTLS_MD_MAX_SIZE];
2226
+ unsigned int hlen;
2227
+ size_t observed_salt_len, msb;
2228
+ unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { 0 };
2229
+
2230
+ if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
2231
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2232
+ }
2233
+
2234
+ siglen = ctx->len;
2235
+
2236
+ if (siglen < 16 || siglen > sizeof(buf)) {
2237
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2238
+ }
2239
+
2240
+ ret = mbedtls_rsa_public(ctx, sig, buf);
2241
+
2242
+ if (ret != 0) {
2243
+ return ret;
2244
+ }
2245
+
2246
+ p = buf;
2247
+
2248
+ if (buf[siglen - 1] != 0xBC) {
2249
+ return PSA_ERROR_INVALID_PADDING;
2250
+ }
2251
+
2252
+ if (md_alg != MBEDTLS_MD_NONE) {
2253
+ /* Gather length of hash to sign */
2254
+ size_t exp_hashlen = mbedtls_md_get_size_from_type(md_alg);
2255
+ if (exp_hashlen == 0) {
2256
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2257
+ }
2258
+
2259
+ if (hashlen != exp_hashlen) {
2260
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2261
+ }
2262
+ }
2263
+
2264
+ hlen = mbedtls_md_get_size_from_type(mgf1_hash_id);
2265
+ if (hlen == 0) {
2266
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2267
+ }
2268
+
2269
+ /*
2270
+ * Note: EMSA-PSS verification is over the length of N - 1 bits
2271
+ */
2272
+ msb = mbedtls_mpi_bitlen(&ctx->N) - 1;
2273
+
2274
+ if (buf[0] >> (8 - siglen * 8 + msb)) {
2275
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2276
+ }
2277
+
2278
+ /* Compensate for boundary condition when applying mask */
2279
+ if (msb % 8 == 0) {
2280
+ p++;
2281
+ siglen -= 1;
2282
+ }
2283
+
2284
+ if (siglen < hlen + 2) {
2285
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2286
+ }
2287
+ hash_start = p + siglen - hlen - 1;
2288
+
2289
+ ret = mgf_mask(p, siglen - hlen - 1, hash_start, hlen, mgf1_hash_id);
2290
+ if (ret != 0) {
2291
+ return ret;
2292
+ }
2293
+
2294
+ buf[0] &= 0xFF >> (siglen * 8 - msb);
2295
+
2296
+ while (p < hash_start - 1 && *p == 0) {
2297
+ p++;
2298
+ }
2299
+
2300
+ if (*p++ != 0x01) {
2301
+ return PSA_ERROR_INVALID_PADDING;
2302
+ }
2303
+
2304
+ observed_salt_len = (size_t) (hash_start - p);
2305
+
2306
+ if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
2307
+ observed_salt_len != (size_t) expected_salt_len) {
2308
+ return PSA_ERROR_INVALID_PADDING;
2309
+ }
2310
+
2311
+ /*
2312
+ * Generate H = Hash( M' )
2313
+ */
2314
+ ret = hash_mprime(hash, hashlen, p, observed_salt_len,
2315
+ result, mgf1_hash_id);
2316
+ if (ret != 0) {
2317
+ return ret;
2318
+ }
2319
+
2320
+ if (memcmp(hash_start, result, hlen) != 0) {
2321
+ return MBEDTLS_ERR_RSA_VERIFY_FAILED;
2322
+ }
2323
+
2324
+ return 0;
2325
+ }
2326
+ #endif /* MBEDTLS_PKCS1_V21 */
2327
+
2328
+ #if defined(MBEDTLS_PKCS1_V15)
2329
+ /*
2330
+ * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
2331
+ */
2332
+ int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
2333
+ mbedtls_md_type_t md_alg,
2334
+ unsigned int hashlen,
2335
+ const unsigned char *hash,
2336
+ const unsigned char *sig)
2337
+ {
2338
+ int ret = 0;
2339
+ size_t sig_len;
2340
+ unsigned char *encoded = NULL, *encoded_expected = NULL;
2341
+
2342
+ if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
2343
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2344
+ }
2345
+
2346
+ sig_len = ctx->len;
2347
+
2348
+ /*
2349
+ * Prepare expected PKCS1 v1.5 encoding of hash.
2350
+ */
2351
+
2352
+ if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
2353
+ (encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
2354
+ ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
2355
+ goto cleanup;
2356
+ }
2357
+
2358
+ if ((ret = rsa_rsassa_pkcs1_v15_encode(md_alg, hashlen, hash, sig_len,
2359
+ encoded_expected)) != 0) {
2360
+ goto cleanup;
2361
+ }
2362
+
2363
+ /*
2364
+ * Apply RSA primitive to get what should be PKCS1 encoded hash.
2365
+ */
2366
+
2367
+ ret = mbedtls_rsa_public(ctx, sig, encoded);
2368
+ if (ret != 0) {
2369
+ goto cleanup;
2370
+ }
2371
+
2372
+ /*
2373
+ * Compare
2374
+ */
2375
+
2376
+ if ((ret = mbedtls_ct_memcmp(encoded, encoded_expected,
2377
+ sig_len)) != 0) {
2378
+ ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
2379
+ goto cleanup;
2380
+ }
2381
+
2382
+ cleanup:
2383
+
2384
+ if (encoded != NULL) {
2385
+ mbedtls_zeroize_and_free(encoded, sig_len);
2386
+ }
2387
+
2388
+ if (encoded_expected != NULL) {
2389
+ mbedtls_zeroize_and_free(encoded_expected, sig_len);
2390
+ }
2391
+
2392
+ return ret;
2393
+ }
2394
+ #endif /* MBEDTLS_PKCS1_V15 */
2395
+
2396
+ /*
2397
+ * Do an RSA operation and check the message digest
2398
+ */
2399
+ int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
2400
+ mbedtls_md_type_t md_alg,
2401
+ unsigned int hashlen,
2402
+ const unsigned char *hash,
2403
+ const unsigned char *sig)
2404
+ {
2405
+ if ((md_alg != MBEDTLS_MD_NONE || hashlen != 0) && hash == NULL) {
2406
+ return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2407
+ }
2408
+
2409
+ #if defined(MBEDTLS_PKCS1_V21)
2410
+ mbedtls_md_type_t mgf1_hash_id;
2411
+ #endif
2412
+
2413
+ switch (ctx->padding) {
2414
+ #if defined(MBEDTLS_PKCS1_V15)
2415
+ case MBEDTLS_RSA_PKCS_V15:
2416
+ return mbedtls_rsa_rsassa_pkcs1_v15_verify(ctx, md_alg,
2417
+ hashlen, hash, sig);
2418
+ #endif
2419
+
2420
+ #if defined(MBEDTLS_PKCS1_V21)
2421
+ case MBEDTLS_RSA_PKCS_V21:
2422
+ mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
2423
+ ? (mbedtls_md_type_t) ctx->hash_id
2424
+ : md_alg;
2425
+ return mbedtls_rsa_rsassa_pss_verify_ext(ctx, md_alg,
2426
+ hashlen, hash, mgf1_hash_id,
2427
+ MBEDTLS_RSA_SALT_LEN_ANY,
2428
+ sig);
2429
+ #endif
2430
+
2431
+ default:
2432
+ return PSA_ERROR_INVALID_PADDING;
2433
+ }
2434
+ }
2435
+
2436
+ /*
2437
+ * Copy the components of an RSA key
2438
+ */
2439
+ int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
2440
+ {
2441
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2442
+
2443
+ dst->len = src->len;
2444
+
2445
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->N, &src->N));
2446
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->E, &src->E));
2447
+
2448
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->D, &src->D));
2449
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->P, &src->P));
2450
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Q, &src->Q));
2451
+
2452
+ #if !defined(MBEDTLS_RSA_NO_CRT)
2453
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DP, &src->DP));
2454
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->DQ, &src->DQ));
2455
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->QP, &src->QP));
2456
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RP, &src->RP));
2457
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RQ, &src->RQ));
2458
+ #endif
2459
+
2460
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->RN, &src->RN));
2461
+
2462
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vi, &src->Vi));
2463
+ MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&dst->Vf, &src->Vf));
2464
+
2465
+ dst->padding = src->padding;
2466
+ dst->hash_id = src->hash_id;
2467
+
2468
+ cleanup:
2469
+ if (ret != 0) {
2470
+ mbedtls_rsa_free(dst);
2471
+ }
2472
+
2473
+ return ret;
2474
+ }
2475
+
2476
+ /*
2477
+ * Free the components of an RSA key
2478
+ */
2479
+ void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
2480
+ {
2481
+ if (ctx == NULL) {
2482
+ return;
2483
+ }
2484
+
2485
+ mbedtls_mpi_free(&ctx->Vi);
2486
+ mbedtls_mpi_free(&ctx->Vf);
2487
+ mbedtls_mpi_free(&ctx->RN);
2488
+ mbedtls_mpi_free(&ctx->D);
2489
+ mbedtls_mpi_free(&ctx->Q);
2490
+ mbedtls_mpi_free(&ctx->P);
2491
+ mbedtls_mpi_free(&ctx->E);
2492
+ mbedtls_mpi_free(&ctx->N);
2493
+
2494
+ #if !defined(MBEDTLS_RSA_NO_CRT)
2495
+ mbedtls_mpi_free(&ctx->RQ);
2496
+ mbedtls_mpi_free(&ctx->RP);
2497
+ mbedtls_mpi_free(&ctx->QP);
2498
+ mbedtls_mpi_free(&ctx->DQ);
2499
+ mbedtls_mpi_free(&ctx->DP);
2500
+ #endif /* MBEDTLS_RSA_NO_CRT */
2501
+ }
2502
+
2503
+ #if defined(MBEDTLS_SELF_TEST)
2504
+
2505
+
2506
+ /*
2507
+ * Example RSA-1024 keypair, for test purposes
2508
+ */
2509
+ #define KEY_LEN 128
2510
+
2511
+ #define RSA_N "9292758453063D803DD603D5E777D788" \
2512
+ "8ED1D5BF35786190FA2F23EBC0848AEA" \
2513
+ "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
2514
+ "7130B9CED7ACDF54CFC7555AC14EEBAB" \
2515
+ "93A89813FBF3C4F8066D2D800F7C38A8" \
2516
+ "1AE31942917403FF4946B0A83D3D3E05" \
2517
+ "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
2518
+ "5E94BB77B07507233A0BC7BAC8F90F79"
2519
+
2520
+ #define RSA_E "10001"
2521
+
2522
+ #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
2523
+ "66CA472BC44D253102F8B4A9D3BFA750" \
2524
+ "91386C0077937FE33FA3252D28855837" \
2525
+ "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
2526
+ "DF79C5CE07EE72C7F123142198164234" \
2527
+ "CABB724CF78B8173B9F880FC86322407" \
2528
+ "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
2529
+ "071513A1E85B5DFA031F21ECAE91A34D"
2530
+
2531
+ #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
2532
+ "2C01CAD19EA484A87EA4377637E75500" \
2533
+ "FCB2005C5C7DD6EC4AC023CDA285D796" \
2534
+ "C3D9E75E1EFC42488BB4F1D13AC30A57"
2535
+
2536
+ #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
2537
+ "E211C2B9E5DB1ED0BF61D0D9899620F4" \
2538
+ "910E4168387E3C30AA1E00C339A79508" \
2539
+ "8452DD96A9A5EA5D9DCA68DA636032AF"
2540
+
2541
+ #define PT_LEN 24
2542
+ #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
2543
+ "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
2544
+
2545
+ #if defined(MBEDTLS_PKCS1_V15)
2546
+ static int myrand(void *rng_state, unsigned char *output, size_t len)
2547
+ {
2548
+ #if !defined(__OpenBSD__) && !defined(__NetBSD__)
2549
+ size_t i;
2550
+
2551
+ if (rng_state != NULL) {
2552
+ rng_state = NULL;
2553
+ }
2554
+
2555
+ for (i = 0; i < len; ++i) {
2556
+ output[i] = rand();
2557
+ }
2558
+ #else
2559
+ if (rng_state != NULL) {
2560
+ rng_state = NULL;
2561
+ }
2562
+
2563
+ arc4random_buf(output, len);
2564
+ #endif /* !OpenBSD && !NetBSD */
2565
+
2566
+ return 0;
2567
+ }
2568
+ #endif /* MBEDTLS_PKCS1_V15 */
2569
+
2570
+ /*
2571
+ * Checkup routine
2572
+ */
2573
+ int mbedtls_rsa_self_test(int verbose)
2574
+ {
2575
+ int ret = 0;
2576
+ #if defined(MBEDTLS_PKCS1_V15)
2577
+ size_t len;
2578
+ mbedtls_rsa_context rsa;
2579
+ unsigned char rsa_plaintext[PT_LEN];
2580
+ unsigned char rsa_decrypted[PT_LEN];
2581
+ unsigned char rsa_ciphertext[KEY_LEN];
2582
+ #if defined(PSA_WANT_ALG_SHA_1)
2583
+ unsigned char sha1sum[20];
2584
+ #endif
2585
+
2586
+ mbedtls_mpi K;
2587
+
2588
+ mbedtls_mpi_init(&K);
2589
+ mbedtls_rsa_init(&rsa);
2590
+
2591
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&rsa.N, 16, RSA_N));
2592
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&rsa.P, 16, RSA_P));
2593
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&rsa.Q, 16, RSA_Q));
2594
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&rsa.D, 16, RSA_D));
2595
+ MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&rsa.E, 16, RSA_E));
2596
+ rsa.len = mbedtls_mpi_size(&rsa.N);
2597
+
2598
+ #if !defined(MBEDTLS_RSA_NO_CRT)
2599
+ MBEDTLS_MPI_CHK(mbedtls_rsa_deduce_crt(&rsa.P, &rsa.Q, &rsa.D, &rsa.DP, &rsa.DQ, &rsa.QP));
2600
+ #endif /* !MBEDTLS_RSA_NO_CRT */
2601
+
2602
+ if (verbose != 0) {
2603
+ mbedtls_printf(" RSA key validation: ");
2604
+ }
2605
+
2606
+ if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
2607
+ mbedtls_rsa_check_privkey(&rsa) != 0) {
2608
+ if (verbose != 0) {
2609
+ mbedtls_printf("failed\n");
2610
+ }
2611
+
2612
+ ret = 1;
2613
+ goto cleanup;
2614
+ }
2615
+
2616
+ if (verbose != 0) {
2617
+ mbedtls_printf("passed\n PKCS#1 encryption : ");
2618
+ }
2619
+
2620
+ memcpy(rsa_plaintext, RSA_PT, PT_LEN);
2621
+
2622
+ if (mbedtls_rsa_pkcs1_encrypt(&rsa, myrand, NULL,
2623
+ PT_LEN, rsa_plaintext,
2624
+ rsa_ciphertext) != 0) {
2625
+ if (verbose != 0) {
2626
+ mbedtls_printf("failed\n");
2627
+ }
2628
+
2629
+ ret = 1;
2630
+ goto cleanup;
2631
+ }
2632
+
2633
+ if (verbose != 0) {
2634
+ mbedtls_printf("passed\n PKCS#1 decryption : ");
2635
+ }
2636
+
2637
+ if (mbedtls_rsa_pkcs1_decrypt(&rsa, myrand, NULL,
2638
+ &len, rsa_ciphertext, rsa_decrypted,
2639
+ sizeof(rsa_decrypted)) != 0) {
2640
+ if (verbose != 0) {
2641
+ mbedtls_printf("failed\n");
2642
+ }
2643
+
2644
+ ret = 1;
2645
+ goto cleanup;
2646
+ }
2647
+
2648
+ if (memcmp(rsa_decrypted, rsa_plaintext, len) != 0) {
2649
+ if (verbose != 0) {
2650
+ mbedtls_printf("failed\n");
2651
+ }
2652
+
2653
+ ret = 1;
2654
+ goto cleanup;
2655
+ }
2656
+
2657
+ if (verbose != 0) {
2658
+ mbedtls_printf("passed\n");
2659
+ }
2660
+
2661
+ #if defined(PSA_WANT_ALG_SHA_1)
2662
+ if (verbose != 0) {
2663
+ mbedtls_printf(" PKCS#1 data sign : ");
2664
+ }
2665
+
2666
+ if (mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
2667
+ rsa_plaintext, PT_LEN, sha1sum) != 0) {
2668
+ if (verbose != 0) {
2669
+ mbedtls_printf("failed\n");
2670
+ }
2671
+
2672
+ return 1;
2673
+ }
2674
+
2675
+ if (mbedtls_rsa_pkcs1_sign(&rsa, myrand, NULL,
2676
+ MBEDTLS_MD_SHA1, 20,
2677
+ sha1sum, rsa_ciphertext) != 0) {
2678
+ if (verbose != 0) {
2679
+ mbedtls_printf("failed\n");
2680
+ }
2681
+
2682
+ ret = 1;
2683
+ goto cleanup;
2684
+ }
2685
+
2686
+ if (verbose != 0) {
2687
+ mbedtls_printf("passed\n PKCS#1 sig. verify: ");
2688
+ }
2689
+
2690
+ if (mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA1, 20,
2691
+ sha1sum, rsa_ciphertext) != 0) {
2692
+ if (verbose != 0) {
2693
+ mbedtls_printf("failed\n");
2694
+ }
2695
+
2696
+ ret = 1;
2697
+ goto cleanup;
2698
+ }
2699
+
2700
+ if (verbose != 0) {
2701
+ mbedtls_printf("passed\n");
2702
+ }
2703
+ #endif /* PSA_WANT_ALG_SHA_1 */
2704
+
2705
+ if (verbose != 0) {
2706
+ mbedtls_printf("\n");
2707
+ }
2708
+
2709
+ cleanup:
2710
+ mbedtls_mpi_free(&K);
2711
+ mbedtls_rsa_free(&rsa);
2712
+ #else /* MBEDTLS_PKCS1_V15 */
2713
+ ((void) verbose);
2714
+ #endif /* MBEDTLS_PKCS1_V15 */
2715
+ return ret;
2716
+ }
2717
+
2718
+ #endif /* MBEDTLS_SELF_TEST */
2719
+
2720
+ #endif /* MBEDTLS_RSA_C */