@scriptc/runtime 0.0.0 → 0.0.2

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 (416) hide show
  1. package/LICENSE +202 -0
  2. package/package.json +16 -6
  3. package/src/scr_array.c +488 -0
  4. package/src/scr_assert.c +1393 -0
  5. package/src/scr_async.c +2593 -0
  6. package/src/scr_async_dyn.c +607 -0
  7. package/src/scr_bytes.c +1425 -0
  8. package/src/scr_bytes_io.c +161 -0
  9. package/src/scr_child.c +3587 -0
  10. package/src/scr_closure.c +214 -0
  11. package/src/scr_console.c +143 -0
  12. package/src/scr_cycle.c +218 -0
  13. package/src/scr_dc.c +805 -0
  14. package/src/scr_dgram.c +1007 -0
  15. package/src/scr_dyn_handle.c +154 -0
  16. package/src/scr_dyn_invoke.c +644 -0
  17. package/src/scr_error.c +332 -0
  18. package/src/scr_events.c +714 -0
  19. package/src/scr_events_emitter.c +699 -0
  20. package/src/scr_exception.c +242 -0
  21. package/src/scr_fetch.c +1130 -0
  22. package/src/scr_fetch_curl.c +654 -0
  23. package/src/scr_http.c +3482 -0
  24. package/src/scr_http2.c +3030 -0
  25. package/src/scr_inspect.c +803 -0
  26. package/src/scr_inspect_island.c +51 -0
  27. package/src/scr_island.c +9363 -0
  28. package/src/scr_json.c +2151 -0
  29. package/src/scr_lib.c +3612 -0
  30. package/src/scr_loop_epoll.c +241 -0
  31. package/src/scr_loop_kqueue.c +121 -0
  32. package/src/scr_loop_wsapoll.c +208 -0
  33. package/src/scr_map.c +591 -0
  34. package/src/scr_net.c +3017 -0
  35. package/src/scr_net_island.c +458 -0
  36. package/src/scr_number.c +115 -0
  37. package/src/scr_object.c +25 -0
  38. package/src/scr_path.c +1266 -0
  39. package/src/scr_platform.h +119 -0
  40. package/src/scr_readline.c +287 -0
  41. package/src/scr_regex.c +1080 -0
  42. package/src/scr_runtime.h +5046 -0
  43. package/src/scr_stream.c +2877 -0
  44. package/src/scr_string.c +1264 -0
  45. package/src/scr_symbol.c +132 -0
  46. package/src/scr_test.c +662 -0
  47. package/src/scr_tls.c +1520 -0
  48. package/src/scr_union.c +105 -0
  49. package/src/scr_url.c +911 -0
  50. package/src/scr_url_internal.h +29 -0
  51. package/src/scr_url_params.c +561 -0
  52. package/src/scr_watch.c +568 -0
  53. package/src/scr_web.c +1778 -0
  54. package/src/scr_win.c +76 -0
  55. package/src/scr_zlib.c +197 -0
  56. package/src/scr_zlib_island.c +15 -0
  57. package/vendor/README.md +52 -0
  58. package/vendor/curl/COPYRIGHT +534 -0
  59. package/vendor/curl/include/curl/curl.h +3214 -0
  60. package/vendor/curl/include/curl/curlver.h +79 -0
  61. package/vendor/curl/include/curl/easy.h +125 -0
  62. package/vendor/curl/include/curl/header.h +74 -0
  63. package/vendor/curl/include/curl/mprintf.h +52 -0
  64. package/vendor/curl/include/curl/multi.h +460 -0
  65. package/vendor/curl/include/curl/options.h +70 -0
  66. package/vendor/curl/include/curl/stdcheaders.h +35 -0
  67. package/vendor/curl/include/curl/system.h +508 -0
  68. package/vendor/curl/include/curl/typecheck-gcc.h +716 -0
  69. package/vendor/curl/include/curl/urlapi.h +149 -0
  70. package/vendor/curl/include/curl/websockets.h +84 -0
  71. package/vendor/mbedtls/LICENSE +553 -0
  72. package/vendor/mbedtls/include/CMakeLists.txt +22 -0
  73. package/vendor/mbedtls/include/mbedtls/aes.h +631 -0
  74. package/vendor/mbedtls/include/mbedtls/aria.h +343 -0
  75. package/vendor/mbedtls/include/mbedtls/asn1.h +642 -0
  76. package/vendor/mbedtls/include/mbedtls/asn1write.h +390 -0
  77. package/vendor/mbedtls/include/mbedtls/base64.h +82 -0
  78. package/vendor/mbedtls/include/mbedtls/bignum.h +1088 -0
  79. package/vendor/mbedtls/include/mbedtls/block_cipher.h +76 -0
  80. package/vendor/mbedtls/include/mbedtls/build_info.h +194 -0
  81. package/vendor/mbedtls/include/mbedtls/camellia.h +305 -0
  82. package/vendor/mbedtls/include/mbedtls/ccm.h +526 -0
  83. package/vendor/mbedtls/include/mbedtls/chacha20.h +209 -0
  84. package/vendor/mbedtls/include/mbedtls/chachapoly.h +351 -0
  85. package/vendor/mbedtls/include/mbedtls/check_config.h +1149 -0
  86. package/vendor/mbedtls/include/mbedtls/cipher.h +1250 -0
  87. package/vendor/mbedtls/include/mbedtls/cmac.h +246 -0
  88. package/vendor/mbedtls/include/mbedtls/compat-2.x.h +46 -0
  89. package/vendor/mbedtls/include/mbedtls/config_adjust_legacy_crypto.h +578 -0
  90. package/vendor/mbedtls/include/mbedtls/config_adjust_legacy_from_psa.h +873 -0
  91. package/vendor/mbedtls/include/mbedtls/config_adjust_psa_from_legacy.h +359 -0
  92. package/vendor/mbedtls/include/mbedtls/config_adjust_psa_superset_legacy.h +145 -0
  93. package/vendor/mbedtls/include/mbedtls/config_adjust_ssl.h +91 -0
  94. package/vendor/mbedtls/include/mbedtls/config_adjust_x509.h +35 -0
  95. package/vendor/mbedtls/include/mbedtls/config_psa.h +61 -0
  96. package/vendor/mbedtls/include/mbedtls/constant_time.h +36 -0
  97. package/vendor/mbedtls/include/mbedtls/ctr_drbg.h +596 -0
  98. package/vendor/mbedtls/include/mbedtls/debug.h +156 -0
  99. package/vendor/mbedtls/include/mbedtls/des.h +385 -0
  100. package/vendor/mbedtls/include/mbedtls/dhm.h +972 -0
  101. package/vendor/mbedtls/include/mbedtls/ecdh.h +455 -0
  102. package/vendor/mbedtls/include/mbedtls/ecdsa.h +674 -0
  103. package/vendor/mbedtls/include/mbedtls/ecjpake.h +298 -0
  104. package/vendor/mbedtls/include/mbedtls/ecp.h +1517 -0
  105. package/vendor/mbedtls/include/mbedtls/entropy.h +274 -0
  106. package/vendor/mbedtls/include/mbedtls/error.h +201 -0
  107. package/vendor/mbedtls/include/mbedtls/gcm.h +387 -0
  108. package/vendor/mbedtls/include/mbedtls/hkdf.h +124 -0
  109. package/vendor/mbedtls/include/mbedtls/hmac_drbg.h +434 -0
  110. package/vendor/mbedtls/include/mbedtls/lms.h +440 -0
  111. package/vendor/mbedtls/include/mbedtls/mbedtls_config.h +4446 -0
  112. package/vendor/mbedtls/include/mbedtls/md.h +526 -0
  113. package/vendor/mbedtls/include/mbedtls/md5.h +190 -0
  114. package/vendor/mbedtls/include/mbedtls/memory_buffer_alloc.h +142 -0
  115. package/vendor/mbedtls/include/mbedtls/net_sockets.h +299 -0
  116. package/vendor/mbedtls/include/mbedtls/nist_kw.h +166 -0
  117. package/vendor/mbedtls/include/mbedtls/oid.h +727 -0
  118. package/vendor/mbedtls/include/mbedtls/pem.h +160 -0
  119. package/vendor/mbedtls/include/mbedtls/pk.h +1303 -0
  120. package/vendor/mbedtls/include/mbedtls/pkcs12.h +186 -0
  121. package/vendor/mbedtls/include/mbedtls/pkcs5.h +198 -0
  122. package/vendor/mbedtls/include/mbedtls/pkcs7.h +252 -0
  123. package/vendor/mbedtls/include/mbedtls/platform.h +516 -0
  124. package/vendor/mbedtls/include/mbedtls/platform_time.h +79 -0
  125. package/vendor/mbedtls/include/mbedtls/platform_util.h +247 -0
  126. package/vendor/mbedtls/include/mbedtls/poly1305.h +168 -0
  127. package/vendor/mbedtls/include/mbedtls/private_access.h +20 -0
  128. package/vendor/mbedtls/include/mbedtls/psa_util.h +207 -0
  129. package/vendor/mbedtls/include/mbedtls/ripemd160.h +136 -0
  130. package/vendor/mbedtls/include/mbedtls/rsa.h +1170 -0
  131. package/vendor/mbedtls/include/mbedtls/sha1.h +219 -0
  132. package/vendor/mbedtls/include/mbedtls/sha256.h +200 -0
  133. package/vendor/mbedtls/include/mbedtls/sha3.h +172 -0
  134. package/vendor/mbedtls/include/mbedtls/sha512.h +208 -0
  135. package/vendor/mbedtls/include/mbedtls/ssl.h +5887 -0
  136. package/vendor/mbedtls/include/mbedtls/ssl_cache.h +187 -0
  137. package/vendor/mbedtls/include/mbedtls/ssl_ciphersuites.h +482 -0
  138. package/vendor/mbedtls/include/mbedtls/ssl_cookie.h +106 -0
  139. package/vendor/mbedtls/include/mbedtls/ssl_ticket.h +199 -0
  140. package/vendor/mbedtls/include/mbedtls/threading.h +167 -0
  141. package/vendor/mbedtls/include/mbedtls/timing.h +94 -0
  142. package/vendor/mbedtls/include/mbedtls/version.h +78 -0
  143. package/vendor/mbedtls/include/mbedtls/x509.h +500 -0
  144. package/vendor/mbedtls/include/mbedtls/x509_crl.h +184 -0
  145. package/vendor/mbedtls/include/mbedtls/x509_crt.h +1208 -0
  146. package/vendor/mbedtls/include/mbedtls/x509_csr.h +382 -0
  147. package/vendor/mbedtls/include/psa/build_info.h +20 -0
  148. package/vendor/mbedtls/include/psa/crypto.h +4998 -0
  149. package/vendor/mbedtls/include/psa/crypto_adjust_auto_enabled.h +31 -0
  150. package/vendor/mbedtls/include/psa/crypto_adjust_config_dependencies.h +51 -0
  151. package/vendor/mbedtls/include/psa/crypto_adjust_config_key_pair_types.h +101 -0
  152. package/vendor/mbedtls/include/psa/crypto_adjust_config_synonyms.h +49 -0
  153. package/vendor/mbedtls/include/psa/crypto_builtin_composites.h +214 -0
  154. package/vendor/mbedtls/include/psa/crypto_builtin_key_derivation.h +118 -0
  155. package/vendor/mbedtls/include/psa/crypto_builtin_primitives.h +114 -0
  156. package/vendor/mbedtls/include/psa/crypto_compat.h +230 -0
  157. package/vendor/mbedtls/include/psa/crypto_config.h +145 -0
  158. package/vendor/mbedtls/include/psa/crypto_driver_common.h +44 -0
  159. package/vendor/mbedtls/include/psa/crypto_driver_contexts_composites.h +151 -0
  160. package/vendor/mbedtls/include/psa/crypto_driver_contexts_key_derivation.h +52 -0
  161. package/vendor/mbedtls/include/psa/crypto_driver_contexts_primitives.h +105 -0
  162. package/vendor/mbedtls/include/psa/crypto_extra.h +2145 -0
  163. package/vendor/mbedtls/include/psa/crypto_legacy.h +88 -0
  164. package/vendor/mbedtls/include/psa/crypto_platform.h +102 -0
  165. package/vendor/mbedtls/include/psa/crypto_se_driver.h +1383 -0
  166. package/vendor/mbedtls/include/psa/crypto_sizes.h +1319 -0
  167. package/vendor/mbedtls/include/psa/crypto_struct.h +527 -0
  168. package/vendor/mbedtls/include/psa/crypto_types.h +508 -0
  169. package/vendor/mbedtls/include/psa/crypto_values.h +2782 -0
  170. package/vendor/mbedtls/library/aes.c +2294 -0
  171. package/vendor/mbedtls/library/aesce.c +624 -0
  172. package/vendor/mbedtls/library/aesce.h +136 -0
  173. package/vendor/mbedtls/library/aesni.c +846 -0
  174. package/vendor/mbedtls/library/aesni.h +162 -0
  175. package/vendor/mbedtls/library/alignment.h +704 -0
  176. package/vendor/mbedtls/library/aria.c +969 -0
  177. package/vendor/mbedtls/library/asn1parse.c +468 -0
  178. package/vendor/mbedtls/library/asn1write.c +440 -0
  179. package/vendor/mbedtls/library/base64.c +322 -0
  180. package/vendor/mbedtls/library/base64_internal.h +45 -0
  181. package/vendor/mbedtls/library/bignum.c +2583 -0
  182. package/vendor/mbedtls/library/bignum_core.c +1240 -0
  183. package/vendor/mbedtls/library/bignum_core.h +872 -0
  184. package/vendor/mbedtls/library/bignum_core_invasive.h +38 -0
  185. package/vendor/mbedtls/library/bignum_internal.h +122 -0
  186. package/vendor/mbedtls/library/bignum_mod.c +394 -0
  187. package/vendor/mbedtls/library/bignum_mod.h +452 -0
  188. package/vendor/mbedtls/library/bignum_mod_raw.c +276 -0
  189. package/vendor/mbedtls/library/bignum_mod_raw.h +416 -0
  190. package/vendor/mbedtls/library/bignum_mod_raw_invasive.h +34 -0
  191. package/vendor/mbedtls/library/block_cipher.c +207 -0
  192. package/vendor/mbedtls/library/block_cipher_internal.h +99 -0
  193. package/vendor/mbedtls/library/bn_mul.h +1094 -0
  194. package/vendor/mbedtls/library/camellia.c +1058 -0
  195. package/vendor/mbedtls/library/ccm.c +777 -0
  196. package/vendor/mbedtls/library/chacha20.c +563 -0
  197. package/vendor/mbedtls/library/chacha20_internal.h +23 -0
  198. package/vendor/mbedtls/library/chachapoly.c +487 -0
  199. package/vendor/mbedtls/library/check_crypto_config.h +136 -0
  200. package/vendor/mbedtls/library/cipher.c +1712 -0
  201. package/vendor/mbedtls/library/cipher_invasive.h +28 -0
  202. package/vendor/mbedtls/library/cipher_wrap.c +2482 -0
  203. package/vendor/mbedtls/library/cipher_wrap.h +178 -0
  204. package/vendor/mbedtls/library/cmac.c +1067 -0
  205. package/vendor/mbedtls/library/common.h +487 -0
  206. package/vendor/mbedtls/library/constant_time.c +247 -0
  207. package/vendor/mbedtls/library/constant_time_impl.h +541 -0
  208. package/vendor/mbedtls/library/constant_time_internal.h +579 -0
  209. package/vendor/mbedtls/library/ctr.h +35 -0
  210. package/vendor/mbedtls/library/ctr_drbg.c +1016 -0
  211. package/vendor/mbedtls/library/debug.c +475 -0
  212. package/vendor/mbedtls/library/debug_internal.h +185 -0
  213. package/vendor/mbedtls/library/des.c +1042 -0
  214. package/vendor/mbedtls/library/dhm.c +700 -0
  215. package/vendor/mbedtls/library/ecdh.c +696 -0
  216. package/vendor/mbedtls/library/ecdsa.c +858 -0
  217. package/vendor/mbedtls/library/ecjpake.c +1216 -0
  218. package/vendor/mbedtls/library/ecp.c +3674 -0
  219. package/vendor/mbedtls/library/ecp_curves.c +6125 -0
  220. package/vendor/mbedtls/library/ecp_curves_new.c +9 -0
  221. package/vendor/mbedtls/library/ecp_internal_alt.h +287 -0
  222. package/vendor/mbedtls/library/ecp_invasive.h +305 -0
  223. package/vendor/mbedtls/library/entropy.c +680 -0
  224. package/vendor/mbedtls/library/entropy_poll.c +233 -0
  225. package/vendor/mbedtls/library/entropy_poll.h +64 -0
  226. package/vendor/mbedtls/library/error.c +878 -0
  227. package/vendor/mbedtls/library/gcm.c +1330 -0
  228. package/vendor/mbedtls/library/hkdf.c +161 -0
  229. package/vendor/mbedtls/library/hmac_drbg.c +633 -0
  230. package/vendor/mbedtls/library/lmots.c +789 -0
  231. package/vendor/mbedtls/library/lmots.h +288 -0
  232. package/vendor/mbedtls/library/lms.c +778 -0
  233. package/vendor/mbedtls/library/md.c +1108 -0
  234. package/vendor/mbedtls/library/md5.c +426 -0
  235. package/vendor/mbedtls/library/md_psa.h +26 -0
  236. package/vendor/mbedtls/library/md_wrap.h +46 -0
  237. package/vendor/mbedtls/library/memory_buffer_alloc.c +751 -0
  238. package/vendor/mbedtls/library/mps_common.h +181 -0
  239. package/vendor/mbedtls/library/mps_error.h +89 -0
  240. package/vendor/mbedtls/library/mps_reader.c +538 -0
  241. package/vendor/mbedtls/library/mps_reader.h +366 -0
  242. package/vendor/mbedtls/library/mps_trace.c +112 -0
  243. package/vendor/mbedtls/library/mps_trace.h +154 -0
  244. package/vendor/mbedtls/library/net_sockets.c +694 -0
  245. package/vendor/mbedtls/library/nist_kw.c +729 -0
  246. package/vendor/mbedtls/library/oid.c +1166 -0
  247. package/vendor/mbedtls/library/padlock.c +157 -0
  248. package/vendor/mbedtls/library/padlock.h +111 -0
  249. package/vendor/mbedtls/library/pem.c +554 -0
  250. package/vendor/mbedtls/library/pk.c +1602 -0
  251. package/vendor/mbedtls/library/pk_ecc.c +261 -0
  252. package/vendor/mbedtls/library/pk_internal.h +241 -0
  253. package/vendor/mbedtls/library/pk_wrap.c +1618 -0
  254. package/vendor/mbedtls/library/pk_wrap.h +138 -0
  255. package/vendor/mbedtls/library/pkcs12.c +437 -0
  256. package/vendor/mbedtls/library/pkcs5.c +500 -0
  257. package/vendor/mbedtls/library/pkcs7.c +787 -0
  258. package/vendor/mbedtls/library/pkparse.c +1392 -0
  259. package/vendor/mbedtls/library/pkwrite.c +631 -0
  260. package/vendor/mbedtls/library/pkwrite.h +121 -0
  261. package/vendor/mbedtls/library/platform.c +402 -0
  262. package/vendor/mbedtls/library/platform_util.c +258 -0
  263. package/vendor/mbedtls/library/poly1305.c +492 -0
  264. package/vendor/mbedtls/library/psa_crypto.c +9532 -0
  265. package/vendor/mbedtls/library/psa_crypto_aead.c +646 -0
  266. package/vendor/mbedtls/library/psa_crypto_aead.h +499 -0
  267. package/vendor/mbedtls/library/psa_crypto_cipher.c +747 -0
  268. package/vendor/mbedtls/library/psa_crypto_cipher.h +316 -0
  269. package/vendor/mbedtls/library/psa_crypto_client.c +22 -0
  270. package/vendor/mbedtls/library/psa_crypto_core.h +983 -0
  271. package/vendor/mbedtls/library/psa_crypto_core_common.h +52 -0
  272. package/vendor/mbedtls/library/psa_crypto_driver_wrappers.h +2896 -0
  273. package/vendor/mbedtls/library/psa_crypto_driver_wrappers_no_static.c +256 -0
  274. package/vendor/mbedtls/library/psa_crypto_driver_wrappers_no_static.h +31 -0
  275. package/vendor/mbedtls/library/psa_crypto_ecp.c +594 -0
  276. package/vendor/mbedtls/library/psa_crypto_ecp.h +267 -0
  277. package/vendor/mbedtls/library/psa_crypto_ffdh.c +361 -0
  278. package/vendor/mbedtls/library/psa_crypto_ffdh.h +131 -0
  279. package/vendor/mbedtls/library/psa_crypto_hash.c +470 -0
  280. package/vendor/mbedtls/library/psa_crypto_hash.h +211 -0
  281. package/vendor/mbedtls/library/psa_crypto_invasive.h +92 -0
  282. package/vendor/mbedtls/library/psa_crypto_its.h +131 -0
  283. package/vendor/mbedtls/library/psa_crypto_mac.c +505 -0
  284. package/vendor/mbedtls/library/psa_crypto_mac.h +264 -0
  285. package/vendor/mbedtls/library/psa_crypto_pake.c +571 -0
  286. package/vendor/mbedtls/library/psa_crypto_pake.h +159 -0
  287. package/vendor/mbedtls/library/psa_crypto_random.c +181 -0
  288. package/vendor/mbedtls/library/psa_crypto_random.h +72 -0
  289. package/vendor/mbedtls/library/psa_crypto_random_impl.h +200 -0
  290. package/vendor/mbedtls/library/psa_crypto_rsa.c +714 -0
  291. package/vendor/mbedtls/library/psa_crypto_rsa.h +321 -0
  292. package/vendor/mbedtls/library/psa_crypto_se.c +373 -0
  293. package/vendor/mbedtls/library/psa_crypto_se.h +192 -0
  294. package/vendor/mbedtls/library/psa_crypto_slot_management.c +1137 -0
  295. package/vendor/mbedtls/library/psa_crypto_slot_management.h +344 -0
  296. package/vendor/mbedtls/library/psa_crypto_storage.c +481 -0
  297. package/vendor/mbedtls/library/psa_crypto_storage.h +392 -0
  298. package/vendor/mbedtls/library/psa_its_file.c +254 -0
  299. package/vendor/mbedtls/library/psa_util.c +614 -0
  300. package/vendor/mbedtls/library/psa_util_internal.h +100 -0
  301. package/vendor/mbedtls/library/ripemd160.c +490 -0
  302. package/vendor/mbedtls/library/rsa.c +3121 -0
  303. package/vendor/mbedtls/library/rsa_alt_helpers.c +455 -0
  304. package/vendor/mbedtls/library/rsa_alt_helpers.h +209 -0
  305. package/vendor/mbedtls/library/rsa_internal.h +188 -0
  306. package/vendor/mbedtls/library/sha1.c +480 -0
  307. package/vendor/mbedtls/library/sha256.c +983 -0
  308. package/vendor/mbedtls/library/sha3.c +721 -0
  309. package/vendor/mbedtls/library/sha512.c +1115 -0
  310. package/vendor/mbedtls/library/ssl_cache.c +410 -0
  311. package/vendor/mbedtls/library/ssl_ciphersuites.c +2050 -0
  312. package/vendor/mbedtls/library/ssl_ciphersuites_internal.h +154 -0
  313. package/vendor/mbedtls/library/ssl_client.c +1023 -0
  314. package/vendor/mbedtls/library/ssl_client.h +22 -0
  315. package/vendor/mbedtls/library/ssl_cookie.c +384 -0
  316. package/vendor/mbedtls/library/ssl_debug_helpers.h +85 -0
  317. package/vendor/mbedtls/library/ssl_debug_helpers_generated.c +251 -0
  318. package/vendor/mbedtls/library/ssl_misc.h +3198 -0
  319. package/vendor/mbedtls/library/ssl_msg.c +6619 -0
  320. package/vendor/mbedtls/library/ssl_ticket.c +556 -0
  321. package/vendor/mbedtls/library/ssl_tls.c +10234 -0
  322. package/vendor/mbedtls/library/ssl_tls12_client.c +3688 -0
  323. package/vendor/mbedtls/library/ssl_tls12_server.c +4311 -0
  324. package/vendor/mbedtls/library/ssl_tls13_client.c +3208 -0
  325. package/vendor/mbedtls/library/ssl_tls13_generic.c +1793 -0
  326. package/vendor/mbedtls/library/ssl_tls13_invasive.h +23 -0
  327. package/vendor/mbedtls/library/ssl_tls13_keys.c +1918 -0
  328. package/vendor/mbedtls/library/ssl_tls13_keys.h +668 -0
  329. package/vendor/mbedtls/library/ssl_tls13_server.c +3624 -0
  330. package/vendor/mbedtls/library/threading.c +193 -0
  331. package/vendor/mbedtls/library/threading_internal.h +28 -0
  332. package/vendor/mbedtls/library/timing.c +152 -0
  333. package/vendor/mbedtls/library/version.c +32 -0
  334. package/vendor/mbedtls/library/version_features.c +856 -0
  335. package/vendor/mbedtls/library/x509.c +1776 -0
  336. package/vendor/mbedtls/library/x509_create.c +570 -0
  337. package/vendor/mbedtls/library/x509_crl.c +708 -0
  338. package/vendor/mbedtls/library/x509_crt.c +3311 -0
  339. package/vendor/mbedtls/library/x509_csr.c +648 -0
  340. package/vendor/mbedtls/library/x509_internal.h +101 -0
  341. package/vendor/mbedtls/library/x509write.c +174 -0
  342. package/vendor/mbedtls/library/x509write_crt.c +688 -0
  343. package/vendor/mbedtls/library/x509write_csr.c +336 -0
  344. package/vendor/quickjs-ng/CMakeLists.txt +566 -0
  345. package/vendor/quickjs-ng/LICENSE +24 -0
  346. package/vendor/quickjs-ng/README.md +24 -0
  347. package/vendor/quickjs-ng/api-test.c +1195 -0
  348. package/vendor/quickjs-ng/builtin-array-fromasync.h +119 -0
  349. package/vendor/quickjs-ng/builtin-iterator-zip-keyed.h +332 -0
  350. package/vendor/quickjs-ng/builtin-iterator-zip.h +337 -0
  351. package/vendor/quickjs-ng/cutils.h +1998 -0
  352. package/vendor/quickjs-ng/dtoa.c +1619 -0
  353. package/vendor/quickjs-ng/dtoa.h +87 -0
  354. package/vendor/quickjs-ng/gen/function_source.c +81 -0
  355. package/vendor/quickjs-ng/gen/hello.c +53 -0
  356. package/vendor/quickjs-ng/gen/hello_module.c +106 -0
  357. package/vendor/quickjs-ng/gen/repl.c +3036 -0
  358. package/vendor/quickjs-ng/gen/standalone.c +323 -0
  359. package/vendor/quickjs-ng/gen/test_fib.c +81 -0
  360. package/vendor/quickjs-ng/libregexp-opcode.h +73 -0
  361. package/vendor/quickjs-ng/libregexp.c +3474 -0
  362. package/vendor/quickjs-ng/libregexp.h +101 -0
  363. package/vendor/quickjs-ng/libunicode-table.h +5173 -0
  364. package/vendor/quickjs-ng/libunicode.c +2069 -0
  365. package/vendor/quickjs-ng/libunicode.h +172 -0
  366. package/vendor/quickjs-ng/list.h +107 -0
  367. package/vendor/quickjs-ng/lre-test.c +52 -0
  368. package/vendor/quickjs-ng/qjs.c +762 -0
  369. package/vendor/quickjs-ng/qjsc.c +673 -0
  370. package/vendor/quickjs-ng/quickjs-atom.h +280 -0
  371. package/vendor/quickjs-ng/quickjs-c-atomics.h +54 -0
  372. package/vendor/quickjs-ng/quickjs-libc.c +5037 -0
  373. package/vendor/quickjs-ng/quickjs-libc.h +87 -0
  374. package/vendor/quickjs-ng/quickjs-opcode.h +377 -0
  375. package/vendor/quickjs-ng/quickjs.c +64041 -0
  376. package/vendor/quickjs-ng/quickjs.h +1447 -0
  377. package/vendor/quickjs-ng/run-test262.c +2374 -0
  378. package/vendor/quickjs-ng/unicode_gen.c +3121 -0
  379. package/vendor/quickjs-ng/unicode_gen_def.h +310 -0
  380. package/vendor/ryu/LICENSE-Boost +23 -0
  381. package/vendor/ryu/common.h +114 -0
  382. package/vendor/ryu/d2s.c +509 -0
  383. package/vendor/ryu/d2s_full_table.h +367 -0
  384. package/vendor/ryu/d2s_intrinsics.h +357 -0
  385. package/vendor/ryu/d2s_small_table.h +186 -0
  386. package/vendor/ryu/digit_table.h +35 -0
  387. package/vendor/ryu/ryu.h +46 -0
  388. package/vendor/zlib/LICENSE +22 -0
  389. package/vendor/zlib/adler32.c +164 -0
  390. package/vendor/zlib/compress.c +75 -0
  391. package/vendor/zlib/crc32.c +1049 -0
  392. package/vendor/zlib/crc32.h +9446 -0
  393. package/vendor/zlib/deflate.c +2139 -0
  394. package/vendor/zlib/deflate.h +377 -0
  395. package/vendor/zlib/gzclose.c +23 -0
  396. package/vendor/zlib/gzguts.h +214 -0
  397. package/vendor/zlib/gzlib.c +582 -0
  398. package/vendor/zlib/gzread.c +602 -0
  399. package/vendor/zlib/gzwrite.c +631 -0
  400. package/vendor/zlib/infback.c +628 -0
  401. package/vendor/zlib/inffast.c +320 -0
  402. package/vendor/zlib/inffast.h +11 -0
  403. package/vendor/zlib/inffixed.h +94 -0
  404. package/vendor/zlib/inflate.c +1526 -0
  405. package/vendor/zlib/inflate.h +126 -0
  406. package/vendor/zlib/inftrees.c +299 -0
  407. package/vendor/zlib/inftrees.h +62 -0
  408. package/vendor/zlib/trees.c +1117 -0
  409. package/vendor/zlib/trees.h +128 -0
  410. package/vendor/zlib/uncompr.c +85 -0
  411. package/vendor/zlib/zconf.h +543 -0
  412. package/vendor/zlib/zlib.h +1938 -0
  413. package/vendor/zlib/zutil.c +299 -0
  414. package/vendor/zlib/zutil.h +254 -0
  415. package/README.md +0 -3
  416. package/index.js +0 -2
@@ -0,0 +1,1240 @@
1
+ /*
2
+ * Core bignum functions
3
+ *
4
+ * Copyright The Mbed TLS Contributors
5
+ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
+ */
7
+
8
+ #include "common.h"
9
+
10
+ #if defined(MBEDTLS_BIGNUM_C)
11
+
12
+ #include <string.h>
13
+
14
+ #include "mbedtls/error.h"
15
+ #include "mbedtls/platform_util.h"
16
+ #include "constant_time_internal.h"
17
+
18
+ #include "mbedtls/platform.h"
19
+
20
+ #include "bignum_core.h"
21
+ #include "bignum_core_invasive.h"
22
+ #include "bn_mul.h"
23
+ #include "constant_time_internal.h"
24
+
25
+ size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
26
+ {
27
+ #if defined(__has_builtin)
28
+ #if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz)
29
+ #define core_clz __builtin_clz
30
+ #elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl)
31
+ #define core_clz __builtin_clzl
32
+ #elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll)
33
+ #define core_clz __builtin_clzll
34
+ #endif
35
+ #endif
36
+ #if defined(core_clz)
37
+ return (size_t) core_clz(a);
38
+ #else
39
+ size_t j;
40
+ mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
41
+
42
+ for (j = 0; j < biL; j++) {
43
+ if (a & mask) {
44
+ break;
45
+ }
46
+
47
+ mask >>= 1;
48
+ }
49
+
50
+ return j;
51
+ #endif
52
+ }
53
+
54
+ size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
55
+ {
56
+ int i;
57
+ size_t j;
58
+
59
+ for (i = ((int) A_limbs) - 1; i >= 0; i--) {
60
+ if (A[i] != 0) {
61
+ j = biL - mbedtls_mpi_core_clz(A[i]);
62
+ return (i * biL) + j;
63
+ }
64
+ }
65
+
66
+ return 0;
67
+ }
68
+
69
+ static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
70
+ {
71
+ if (MBEDTLS_IS_BIG_ENDIAN) {
72
+ /* Nothing to do on bigendian systems. */
73
+ return a;
74
+ } else {
75
+ #if defined(MBEDTLS_HAVE_INT32)
76
+ return (mbedtls_mpi_uint) MBEDTLS_BSWAP32(a);
77
+ #elif defined(MBEDTLS_HAVE_INT64)
78
+ return (mbedtls_mpi_uint) MBEDTLS_BSWAP64(a);
79
+ #endif
80
+ }
81
+ }
82
+
83
+ void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
84
+ size_t A_limbs)
85
+ {
86
+ mbedtls_mpi_uint *cur_limb_left;
87
+ mbedtls_mpi_uint *cur_limb_right;
88
+ if (A_limbs == 0) {
89
+ return;
90
+ }
91
+
92
+ /*
93
+ * Traverse limbs and
94
+ * - adapt byte-order in each limb
95
+ * - swap the limbs themselves.
96
+ * For that, simultaneously traverse the limbs from left to right
97
+ * and from right to left, as long as the left index is not bigger
98
+ * than the right index (it's not a problem if limbs is odd and the
99
+ * indices coincide in the last iteration).
100
+ */
101
+ for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
102
+ cur_limb_left <= cur_limb_right;
103
+ cur_limb_left++, cur_limb_right--) {
104
+ mbedtls_mpi_uint tmp;
105
+ /* Note that if cur_limb_left == cur_limb_right,
106
+ * this code effectively swaps the bytes only once. */
107
+ tmp = mpi_bigendian_to_host(*cur_limb_left);
108
+ *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
109
+ *cur_limb_right = tmp;
110
+ }
111
+ }
112
+
113
+ /* Whether min <= A, in constant time.
114
+ * A_limbs must be at least 1. */
115
+ mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
116
+ const mbedtls_mpi_uint *A,
117
+ size_t A_limbs)
118
+ {
119
+ /* min <= least significant limb? */
120
+ mbedtls_ct_condition_t min_le_lsl = mbedtls_ct_uint_ge(A[0], min);
121
+
122
+ /* limbs other than the least significant one are all zero? */
123
+ mbedtls_ct_condition_t msll_mask = MBEDTLS_CT_FALSE;
124
+ for (size_t i = 1; i < A_limbs; i++) {
125
+ msll_mask = mbedtls_ct_bool_or(msll_mask, mbedtls_ct_bool(A[i]));
126
+ }
127
+
128
+ /* min <= A iff the lowest limb of A is >= min or the other limbs
129
+ * are not all zero. */
130
+ return mbedtls_ct_bool_or(msll_mask, min_le_lsl);
131
+ }
132
+
133
+ mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
134
+ const mbedtls_mpi_uint *B,
135
+ size_t limbs)
136
+ {
137
+ mbedtls_ct_condition_t ret = MBEDTLS_CT_FALSE, cond = MBEDTLS_CT_FALSE, done = MBEDTLS_CT_FALSE;
138
+
139
+ for (size_t i = limbs; i > 0; i--) {
140
+ /*
141
+ * If B[i - 1] < A[i - 1] then A < B is false and the result must
142
+ * remain 0.
143
+ *
144
+ * Again even if we can make a decision, we just mark the result and
145
+ * the fact that we are done and continue looping.
146
+ */
147
+ cond = mbedtls_ct_uint_lt(B[i - 1], A[i - 1]);
148
+ done = mbedtls_ct_bool_or(done, cond);
149
+
150
+ /*
151
+ * If A[i - 1] < B[i - 1] then A < B is true.
152
+ *
153
+ * Again even if we can make a decision, we just mark the result and
154
+ * the fact that we are done and continue looping.
155
+ */
156
+ cond = mbedtls_ct_uint_lt(A[i - 1], B[i - 1]);
157
+ ret = mbedtls_ct_bool_or(ret, mbedtls_ct_bool_and(cond, mbedtls_ct_bool_not(done)));
158
+ done = mbedtls_ct_bool_or(done, cond);
159
+ }
160
+
161
+ /*
162
+ * If all the limbs were equal, then the numbers are equal, A < B is false
163
+ * and leaving the result 0 is correct.
164
+ */
165
+
166
+ return ret;
167
+ }
168
+
169
+ void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
170
+ const mbedtls_mpi_uint *A,
171
+ size_t limbs,
172
+ mbedtls_ct_condition_t assign)
173
+ {
174
+ if (X == A) {
175
+ return;
176
+ }
177
+
178
+ /* This function is very performance-sensitive for RSA. For this reason
179
+ * we have the loop below, instead of calling mbedtls_ct_memcpy_if
180
+ * (this is more optimal since here we don't have to handle the case where
181
+ * we copy awkwardly sized data).
182
+ */
183
+ for (size_t i = 0; i < limbs; i++) {
184
+ X[i] = mbedtls_ct_mpi_uint_if(assign, A[i], X[i]);
185
+ }
186
+ }
187
+
188
+ void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
189
+ mbedtls_mpi_uint *Y,
190
+ size_t limbs,
191
+ mbedtls_ct_condition_t swap)
192
+ {
193
+ if (X == Y) {
194
+ return;
195
+ }
196
+
197
+ for (size_t i = 0; i < limbs; i++) {
198
+ mbedtls_mpi_uint tmp = X[i];
199
+ X[i] = mbedtls_ct_mpi_uint_if(swap, Y[i], X[i]);
200
+ Y[i] = mbedtls_ct_mpi_uint_if(swap, tmp, Y[i]);
201
+ }
202
+ }
203
+
204
+ int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
205
+ size_t X_limbs,
206
+ const unsigned char *input,
207
+ size_t input_length)
208
+ {
209
+ const size_t limbs = CHARS_TO_LIMBS(input_length);
210
+
211
+ if (X_limbs < limbs) {
212
+ return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
213
+ }
214
+
215
+ if (X != NULL) {
216
+ memset(X, 0, X_limbs * ciL);
217
+
218
+ for (size_t i = 0; i < input_length; i++) {
219
+ size_t offset = ((i % ciL) << 3);
220
+ X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
221
+ }
222
+ }
223
+
224
+ return 0;
225
+ }
226
+
227
+ int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
228
+ size_t X_limbs,
229
+ const unsigned char *input,
230
+ size_t input_length)
231
+ {
232
+ const size_t limbs = CHARS_TO_LIMBS(input_length);
233
+
234
+ if (X_limbs < limbs) {
235
+ return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
236
+ }
237
+
238
+ /* If X_limbs is 0, input_length must also be 0 (from previous test).
239
+ * Nothing to do. */
240
+ if (X_limbs == 0) {
241
+ return 0;
242
+ }
243
+
244
+ memset(X, 0, X_limbs * ciL);
245
+
246
+ /* memcpy() with (NULL, 0) is undefined behaviour */
247
+ if (input_length != 0) {
248
+ size_t overhead = (X_limbs * ciL) - input_length;
249
+ unsigned char *Xp = (unsigned char *) X;
250
+ memcpy(Xp + overhead, input, input_length);
251
+ }
252
+
253
+ mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
254
+
255
+ return 0;
256
+ }
257
+
258
+ int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
259
+ size_t A_limbs,
260
+ unsigned char *output,
261
+ size_t output_length)
262
+ {
263
+ size_t stored_bytes = A_limbs * ciL;
264
+ size_t bytes_to_copy;
265
+
266
+ if (stored_bytes < output_length) {
267
+ bytes_to_copy = stored_bytes;
268
+ } else {
269
+ bytes_to_copy = output_length;
270
+
271
+ /* The output buffer is smaller than the allocated size of A.
272
+ * However A may fit if its leading bytes are zero. */
273
+ for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
274
+ if (GET_BYTE(A, i) != 0) {
275
+ return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
276
+ }
277
+ }
278
+ }
279
+
280
+ for (size_t i = 0; i < bytes_to_copy; i++) {
281
+ output[i] = GET_BYTE(A, i);
282
+ }
283
+
284
+ if (stored_bytes < output_length) {
285
+ /* Write trailing 0 bytes */
286
+ memset(output + stored_bytes, 0, output_length - stored_bytes);
287
+ }
288
+
289
+ return 0;
290
+ }
291
+
292
+ int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
293
+ size_t X_limbs,
294
+ unsigned char *output,
295
+ size_t output_length)
296
+ {
297
+ size_t stored_bytes;
298
+ size_t bytes_to_copy;
299
+ unsigned char *p;
300
+
301
+ stored_bytes = X_limbs * ciL;
302
+
303
+ if (stored_bytes < output_length) {
304
+ /* There is enough space in the output buffer. Write initial
305
+ * null bytes and record the position at which to start
306
+ * writing the significant bytes. In this case, the execution
307
+ * trace of this function does not depend on the value of the
308
+ * number. */
309
+ bytes_to_copy = stored_bytes;
310
+ p = output + output_length - stored_bytes;
311
+ memset(output, 0, output_length - stored_bytes);
312
+ } else {
313
+ /* The output buffer is smaller than the allocated size of X.
314
+ * However X may fit if its leading bytes are zero. */
315
+ bytes_to_copy = output_length;
316
+ p = output;
317
+ for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
318
+ if (GET_BYTE(X, i) != 0) {
319
+ return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
320
+ }
321
+ }
322
+ }
323
+
324
+ for (size_t i = 0; i < bytes_to_copy; i++) {
325
+ p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
326
+ }
327
+
328
+ return 0;
329
+ }
330
+
331
+ void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
332
+ size_t count)
333
+ {
334
+ size_t i, v0, v1;
335
+ mbedtls_mpi_uint r0 = 0, r1;
336
+
337
+ v0 = count / biL;
338
+ v1 = count & (biL - 1);
339
+
340
+ if (v0 > limbs || (v0 == limbs && v1 > 0)) {
341
+ memset(X, 0, limbs * ciL);
342
+ return;
343
+ }
344
+
345
+ /*
346
+ * shift by count / limb_size
347
+ */
348
+ if (v0 > 0) {
349
+ for (i = 0; i < limbs - v0; i++) {
350
+ X[i] = X[i + v0];
351
+ }
352
+
353
+ for (; i < limbs; i++) {
354
+ X[i] = 0;
355
+ }
356
+ }
357
+
358
+ /*
359
+ * shift by count % limb_size
360
+ */
361
+ if (v1 > 0) {
362
+ for (i = limbs; i > 0; i--) {
363
+ r1 = X[i - 1] << (biL - v1);
364
+ X[i - 1] >>= v1;
365
+ X[i - 1] |= r0;
366
+ r0 = r1;
367
+ }
368
+ }
369
+ }
370
+
371
+ void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
372
+ size_t count)
373
+ {
374
+ size_t i, v0, v1;
375
+ mbedtls_mpi_uint r0 = 0, r1;
376
+
377
+ v0 = count / (biL);
378
+ v1 = count & (biL - 1);
379
+
380
+ /*
381
+ * shift by count / limb_size
382
+ */
383
+ if (v0 > 0) {
384
+ for (i = limbs; i > v0; i--) {
385
+ X[i - 1] = X[i - v0 - 1];
386
+ }
387
+
388
+ for (; i > 0; i--) {
389
+ X[i - 1] = 0;
390
+ }
391
+ }
392
+
393
+ /*
394
+ * shift by count % limb_size
395
+ */
396
+ if (v1 > 0) {
397
+ for (i = v0; i < limbs; i++) {
398
+ r1 = X[i] >> (biL - v1);
399
+ X[i] <<= v1;
400
+ X[i] |= r0;
401
+ r0 = r1;
402
+ }
403
+ }
404
+ }
405
+
406
+ mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
407
+ const mbedtls_mpi_uint *A,
408
+ const mbedtls_mpi_uint *B,
409
+ size_t limbs)
410
+ {
411
+ mbedtls_mpi_uint c = 0;
412
+
413
+ for (size_t i = 0; i < limbs; i++) {
414
+ mbedtls_mpi_uint t = c + A[i];
415
+ c = (t < A[i]);
416
+ t += B[i];
417
+ c += (t < B[i]);
418
+ X[i] = t;
419
+ }
420
+
421
+ return c;
422
+ }
423
+
424
+ mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
425
+ const mbedtls_mpi_uint *A,
426
+ size_t limbs,
427
+ unsigned cond)
428
+ {
429
+ mbedtls_mpi_uint c = 0;
430
+
431
+ mbedtls_ct_condition_t do_add = mbedtls_ct_bool(cond);
432
+
433
+ for (size_t i = 0; i < limbs; i++) {
434
+ mbedtls_mpi_uint add = mbedtls_ct_mpi_uint_if_else_0(do_add, A[i]);
435
+ mbedtls_mpi_uint t = c + X[i];
436
+ c = (t < X[i]);
437
+ t += add;
438
+ c += (t < add);
439
+ X[i] = t;
440
+ }
441
+
442
+ return c;
443
+ }
444
+
445
+ mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
446
+ const mbedtls_mpi_uint *A,
447
+ const mbedtls_mpi_uint *B,
448
+ size_t limbs)
449
+ {
450
+ mbedtls_mpi_uint c = 0;
451
+
452
+ for (size_t i = 0; i < limbs; i++) {
453
+ mbedtls_mpi_uint z = (A[i] < c);
454
+ mbedtls_mpi_uint t = A[i] - c;
455
+ c = (t < B[i]) + z;
456
+ X[i] = t - B[i];
457
+ }
458
+
459
+ return c;
460
+ }
461
+
462
+ mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
463
+ const mbedtls_mpi_uint *s, size_t s_len,
464
+ mbedtls_mpi_uint b)
465
+ {
466
+ mbedtls_mpi_uint c = 0; /* carry */
467
+ /*
468
+ * It is a documented precondition of this function that d_len >= s_len.
469
+ * If that's not the case, we swap these round: this turns what would be
470
+ * a buffer overflow into an incorrect result.
471
+ */
472
+ if (d_len < s_len) {
473
+ s_len = d_len;
474
+ }
475
+ size_t excess_len = d_len - s_len;
476
+ size_t steps_x8 = s_len / 8;
477
+ size_t steps_x1 = s_len & 7;
478
+
479
+ while (steps_x8--) {
480
+ MULADDC_X8_INIT
481
+ MULADDC_X8_CORE
482
+ MULADDC_X8_STOP
483
+ }
484
+
485
+ while (steps_x1--) {
486
+ MULADDC_X1_INIT
487
+ MULADDC_X1_CORE
488
+ MULADDC_X1_STOP
489
+ }
490
+
491
+ while (excess_len--) {
492
+ *d += c;
493
+ c = (*d < c);
494
+ d++;
495
+ }
496
+
497
+ return c;
498
+ }
499
+
500
+ void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
501
+ const mbedtls_mpi_uint *A, size_t A_limbs,
502
+ const mbedtls_mpi_uint *B, size_t B_limbs)
503
+ {
504
+ memset(X, 0, (A_limbs + B_limbs) * ciL);
505
+
506
+ for (size_t i = 0; i < B_limbs; i++) {
507
+ (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
508
+ }
509
+ }
510
+
511
+ /*
512
+ * Fast Montgomery initialization (thanks to Tom St Denis).
513
+ */
514
+ mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
515
+ {
516
+ mbedtls_mpi_uint x = N[0];
517
+
518
+ x += ((N[0] + 2) & 4) << 1;
519
+
520
+ for (unsigned int i = biL; i >= 8; i /= 2) {
521
+ x *= (2 - (N[0] * x));
522
+ }
523
+
524
+ return ~x + 1;
525
+ }
526
+
527
+ void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
528
+ const mbedtls_mpi_uint *A,
529
+ const mbedtls_mpi_uint *B,
530
+ size_t B_limbs,
531
+ const mbedtls_mpi_uint *N,
532
+ size_t AN_limbs,
533
+ mbedtls_mpi_uint mm,
534
+ mbedtls_mpi_uint *T)
535
+ {
536
+ memset(T, 0, (2 * AN_limbs + 1) * ciL);
537
+
538
+ for (size_t i = 0; i < AN_limbs; i++) {
539
+ /* T = (T + u0*B + u1*N) / 2^biL */
540
+ mbedtls_mpi_uint u0 = A[i];
541
+ mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
542
+
543
+ (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
544
+ (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
545
+
546
+ T++;
547
+ }
548
+
549
+ /*
550
+ * The result we want is (T >= N) ? T - N : T.
551
+ *
552
+ * For better constant-time properties in this function, we always do the
553
+ * subtraction, with the result in X.
554
+ *
555
+ * We also look to see if there was any carry in the final additions in the
556
+ * loop above.
557
+ */
558
+
559
+ mbedtls_mpi_uint carry = T[AN_limbs];
560
+ mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
561
+
562
+ /*
563
+ * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
564
+ *
565
+ * T can be in one of 3 ranges:
566
+ *
567
+ * 1) T < N : (carry, borrow) = (0, 1): we want T
568
+ * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
569
+ * 3) T >= R : (carry, borrow) = (1, 1): we want X
570
+ *
571
+ * and (carry, borrow) = (1, 0) can't happen.
572
+ *
573
+ * So the correct return value is already in X if (carry ^ borrow) = 0,
574
+ * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
575
+ */
576
+ mbedtls_ct_memcpy_if(mbedtls_ct_bool(carry ^ borrow),
577
+ (unsigned char *) X,
578
+ (unsigned char *) T,
579
+ NULL,
580
+ AN_limbs * sizeof(mbedtls_mpi_uint));
581
+ }
582
+
583
+ int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
584
+ const mbedtls_mpi *N)
585
+ {
586
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
587
+
588
+ MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
589
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
590
+ MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
591
+ MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
592
+
593
+ cleanup:
594
+ return ret;
595
+ }
596
+
597
+ MBEDTLS_STATIC_TESTABLE
598
+ void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
599
+ const mbedtls_mpi_uint *table,
600
+ size_t limbs,
601
+ size_t count,
602
+ size_t index)
603
+ {
604
+ for (size_t i = 0; i < count; i++, table += limbs) {
605
+ mbedtls_ct_condition_t assign = mbedtls_ct_uint_eq(i, index);
606
+ mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
607
+ }
608
+ }
609
+
610
+ /* Fill X with n_bytes random bytes.
611
+ * X must already have room for those bytes.
612
+ * The ordering of the bytes returned from the RNG is suitable for
613
+ * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
614
+ * mbedtls_mpi_core_random()).
615
+ */
616
+ int mbedtls_mpi_core_fill_random(
617
+ mbedtls_mpi_uint *X, size_t X_limbs,
618
+ size_t n_bytes,
619
+ int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
620
+ {
621
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
622
+ const size_t limbs = CHARS_TO_LIMBS(n_bytes);
623
+ const size_t overhead = (limbs * ciL) - n_bytes;
624
+
625
+ if (X_limbs < limbs) {
626
+ return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
627
+ }
628
+
629
+ memset(X, 0, overhead);
630
+ memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
631
+ MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
632
+ mbedtls_mpi_core_bigendian_to_host(X, limbs);
633
+
634
+ cleanup:
635
+ return ret;
636
+ }
637
+
638
+ int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
639
+ mbedtls_mpi_uint min,
640
+ const mbedtls_mpi_uint *N,
641
+ size_t limbs,
642
+ int (*f_rng)(void *, unsigned char *, size_t),
643
+ void *p_rng)
644
+ {
645
+ mbedtls_ct_condition_t ge_lower = MBEDTLS_CT_TRUE, lt_upper = MBEDTLS_CT_FALSE;
646
+ size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
647
+ size_t n_bytes = (n_bits + 7) / 8;
648
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
649
+
650
+ /*
651
+ * When min == 0, each try has at worst a probability 1/2 of failing
652
+ * (the msb has a probability 1/2 of being 0, and then the result will
653
+ * be < N), so after 30 tries failure probability is a most 2**(-30).
654
+ *
655
+ * When N is just below a power of 2, as is the case when generating
656
+ * a random scalar on most elliptic curves, 1 try is enough with
657
+ * overwhelming probability. When N is just above a power of 2,
658
+ * as when generating a random scalar on secp224k1, each try has
659
+ * a probability of failing that is almost 1/2.
660
+ *
661
+ * The probabilities are almost the same if min is nonzero but negligible
662
+ * compared to N. This is always the case when N is crypto-sized, but
663
+ * it's convenient to support small N for testing purposes. When N
664
+ * is small, use a higher repeat count, otherwise the probability of
665
+ * failure is macroscopic.
666
+ */
667
+ int count = (n_bytes > 4 ? 30 : 250);
668
+
669
+ /*
670
+ * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
671
+ * when f_rng is a suitably parametrized instance of HMAC_DRBG:
672
+ * - use the same byte ordering;
673
+ * - keep the leftmost n_bits bits of the generated octet string;
674
+ * - try until result is in the desired range.
675
+ * This also avoids any bias, which is especially important for ECDSA.
676
+ */
677
+ do {
678
+ MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
679
+ n_bytes,
680
+ f_rng, p_rng));
681
+ mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
682
+
683
+ if (--count == 0) {
684
+ ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
685
+ goto cleanup;
686
+ }
687
+
688
+ ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
689
+ lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
690
+ } while (mbedtls_ct_bool_and(ge_lower, lt_upper) == MBEDTLS_CT_FALSE);
691
+
692
+ cleanup:
693
+ return ret;
694
+ }
695
+
696
+ static size_t exp_mod_get_window_size(size_t Ebits)
697
+ {
698
+ #if MBEDTLS_MPI_WINDOW_SIZE >= 6
699
+ return (Ebits > 671) ? 6 : (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
700
+ #elif MBEDTLS_MPI_WINDOW_SIZE == 5
701
+ return (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
702
+ #elif MBEDTLS_MPI_WINDOW_SIZE > 1
703
+ return (Ebits > 79) ? MBEDTLS_MPI_WINDOW_SIZE : 1;
704
+ #else
705
+ (void) Ebits;
706
+ return 1;
707
+ #endif
708
+ }
709
+
710
+ size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
711
+ {
712
+ const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
713
+ const size_t welem = ((size_t) 1) << wsize;
714
+
715
+ /* How big does each part of the working memory pool need to be? */
716
+ const size_t table_limbs = welem * AN_limbs;
717
+ const size_t select_limbs = AN_limbs;
718
+ const size_t temp_limbs = 2 * AN_limbs + 1;
719
+
720
+ return table_limbs + select_limbs + temp_limbs;
721
+ }
722
+
723
+ static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
724
+ const mbedtls_mpi_uint *N,
725
+ size_t AN_limbs,
726
+ mbedtls_mpi_uint mm,
727
+ const mbedtls_mpi_uint *RR,
728
+ size_t welem,
729
+ mbedtls_mpi_uint *Wtable,
730
+ mbedtls_mpi_uint *temp)
731
+ {
732
+ /* W[0] = 1 (in Montgomery presentation) */
733
+ memset(Wtable, 0, AN_limbs * ciL);
734
+ Wtable[0] = 1;
735
+ mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
736
+
737
+ /* W[1] = A (already in Montgomery presentation) */
738
+ mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
739
+ memcpy(W1, A, AN_limbs * ciL);
740
+
741
+ /* W[i+1] = W[i] * W[1], i >= 2 */
742
+ mbedtls_mpi_uint *Wprev = W1;
743
+ for (size_t i = 2; i < welem; i++) {
744
+ mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
745
+ mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
746
+ Wprev = Wcur;
747
+ }
748
+ }
749
+
750
+ #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
751
+ void (*mbedtls_safe_codepath_hook)(void) = NULL;
752
+ void (*mbedtls_unsafe_codepath_hook)(void) = NULL;
753
+ #endif
754
+
755
+ /*
756
+ * This function calculates the indices of the exponent where the exponentiation algorithm should
757
+ * start processing.
758
+ *
759
+ * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value,
760
+ * this function is not constant time with respect to the exponent (parameter E).
761
+ */
762
+ static inline void exp_mod_calc_first_bit_optionally_safe(const mbedtls_mpi_uint *E,
763
+ size_t E_limbs,
764
+ int E_public,
765
+ size_t *E_limb_index,
766
+ size_t *E_bit_index)
767
+ {
768
+ if (E_public == MBEDTLS_MPI_IS_PUBLIC) {
769
+ /*
770
+ * Skip leading zero bits.
771
+ */
772
+ size_t E_bits = mbedtls_mpi_core_bitlen(E, E_limbs);
773
+ if (E_bits == 0) {
774
+ /*
775
+ * If E is 0 mbedtls_mpi_core_bitlen() returns 0. Even if that is the case, we will want
776
+ * to represent it as a single 0 bit and as such the bitlength will be 1.
777
+ */
778
+ E_bits = 1;
779
+ }
780
+
781
+ *E_limb_index = E_bits / biL;
782
+ *E_bit_index = E_bits % biL;
783
+
784
+ #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
785
+ if (mbedtls_unsafe_codepath_hook != NULL) {
786
+ mbedtls_unsafe_codepath_hook();
787
+ }
788
+ #endif
789
+ } else {
790
+ /*
791
+ * Here we need to be constant time with respect to E and can't do anything better than
792
+ * start at the first allocated bit.
793
+ */
794
+ *E_limb_index = E_limbs;
795
+ *E_bit_index = 0;
796
+ #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
797
+ if (mbedtls_safe_codepath_hook != NULL) {
798
+ mbedtls_safe_codepath_hook();
799
+ }
800
+ #endif
801
+ }
802
+ }
803
+
804
+ /*
805
+ * Warning! If the parameter window_public has MBEDTLS_MPI_IS_PUBLIC as its value, this function is
806
+ * not constant time with respect to the window parameter and consequently the exponent of the
807
+ * exponentiation (parameter E of mbedtls_mpi_core_exp_mod_optionally_safe).
808
+ */
809
+ static inline void exp_mod_table_lookup_optionally_safe(mbedtls_mpi_uint *Wselect,
810
+ mbedtls_mpi_uint *Wtable,
811
+ size_t AN_limbs, size_t welem,
812
+ mbedtls_mpi_uint window,
813
+ int window_public)
814
+ {
815
+ if (window_public == MBEDTLS_MPI_IS_PUBLIC) {
816
+ memcpy(Wselect, Wtable + window * AN_limbs, AN_limbs * ciL);
817
+ #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
818
+ if (mbedtls_unsafe_codepath_hook != NULL) {
819
+ mbedtls_unsafe_codepath_hook();
820
+ }
821
+ #endif
822
+ } else {
823
+ /* Select Wtable[window] without leaking window through
824
+ * memory access patterns. */
825
+ mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
826
+ AN_limbs, welem, window);
827
+ #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
828
+ if (mbedtls_safe_codepath_hook != NULL) {
829
+ mbedtls_safe_codepath_hook();
830
+ }
831
+ #endif
832
+ }
833
+ }
834
+
835
+ /* Exponentiation: X := A^E mod N.
836
+ *
837
+ * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value,
838
+ * this function is not constant time with respect to the exponent (parameter E).
839
+ *
840
+ * A must already be in Montgomery form.
841
+ *
842
+ * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
843
+ *
844
+ * RR must contain 2^{2*biL} mod N.
845
+ *
846
+ * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
847
+ * (The difference is that the body in our loop processes a single bit instead
848
+ * of a full window.)
849
+ */
850
+ static void mbedtls_mpi_core_exp_mod_optionally_safe(mbedtls_mpi_uint *X,
851
+ const mbedtls_mpi_uint *A,
852
+ const mbedtls_mpi_uint *N,
853
+ size_t AN_limbs,
854
+ const mbedtls_mpi_uint *E,
855
+ size_t E_limbs,
856
+ int E_public,
857
+ const mbedtls_mpi_uint *RR,
858
+ mbedtls_mpi_uint *T)
859
+ {
860
+ /* We'll process the bits of E from most significant
861
+ * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
862
+ * (limb_index=0, E_bit_index=0). */
863
+ size_t E_limb_index = E_limbs;
864
+ size_t E_bit_index = 0;
865
+ exp_mod_calc_first_bit_optionally_safe(E, E_limbs, E_public,
866
+ &E_limb_index, &E_bit_index);
867
+
868
+ const size_t wsize = exp_mod_get_window_size(E_limb_index * biL);
869
+ const size_t welem = ((size_t) 1) << wsize;
870
+
871
+ /* This is how we will use the temporary storage T, which must have space
872
+ * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
873
+ const size_t table_limbs = welem * AN_limbs;
874
+ const size_t select_limbs = AN_limbs;
875
+
876
+ /* Pointers to specific parts of the temporary working memory pool */
877
+ mbedtls_mpi_uint *const Wtable = T;
878
+ mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
879
+ mbedtls_mpi_uint *const temp = Wselect + select_limbs;
880
+
881
+ /*
882
+ * Window precomputation
883
+ */
884
+
885
+ const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
886
+
887
+ /* Set Wtable[i] = A^i (in Montgomery representation) */
888
+ exp_mod_precompute_window(A, N, AN_limbs,
889
+ mm, RR,
890
+ welem, Wtable, temp);
891
+
892
+ /*
893
+ * Fixed window exponentiation
894
+ */
895
+
896
+ /* X = 1 (in Montgomery presentation) initially */
897
+ memcpy(X, Wtable, AN_limbs * ciL);
898
+
899
+ /* At any given time, window contains window_bits bits from E.
900
+ * window_bits can go up to wsize. */
901
+ size_t window_bits = 0;
902
+ mbedtls_mpi_uint window = 0;
903
+
904
+ do {
905
+ /* Square */
906
+ mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
907
+
908
+ /* Move to the next bit of the exponent */
909
+ if (E_bit_index == 0) {
910
+ --E_limb_index;
911
+ E_bit_index = biL - 1;
912
+ } else {
913
+ --E_bit_index;
914
+ }
915
+ /* Insert next exponent bit into window */
916
+ ++window_bits;
917
+ window <<= 1;
918
+ window |= (E[E_limb_index] >> E_bit_index) & 1;
919
+
920
+ /* Clear window if it's full. Also clear the window at the end,
921
+ * when we've finished processing the exponent. */
922
+ if (window_bits == wsize ||
923
+ (E_bit_index == 0 && E_limb_index == 0)) {
924
+
925
+ exp_mod_table_lookup_optionally_safe(Wselect, Wtable, AN_limbs, welem,
926
+ window, E_public);
927
+ /* Multiply X by the selected element. */
928
+ mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
929
+ temp);
930
+ window = 0;
931
+ window_bits = 0;
932
+ }
933
+ } while (!(E_bit_index == 0 && E_limb_index == 0));
934
+ }
935
+
936
+ void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
937
+ const mbedtls_mpi_uint *A,
938
+ const mbedtls_mpi_uint *N, size_t AN_limbs,
939
+ const mbedtls_mpi_uint *E, size_t E_limbs,
940
+ const mbedtls_mpi_uint *RR,
941
+ mbedtls_mpi_uint *T)
942
+ {
943
+ mbedtls_mpi_core_exp_mod_optionally_safe(X,
944
+ A,
945
+ N,
946
+ AN_limbs,
947
+ E,
948
+ E_limbs,
949
+ MBEDTLS_MPI_IS_SECRET,
950
+ RR,
951
+ T);
952
+ }
953
+
954
+ void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
955
+ const mbedtls_mpi_uint *A,
956
+ const mbedtls_mpi_uint *N, size_t AN_limbs,
957
+ const mbedtls_mpi_uint *E, size_t E_limbs,
958
+ const mbedtls_mpi_uint *RR,
959
+ mbedtls_mpi_uint *T)
960
+ {
961
+ mbedtls_mpi_core_exp_mod_optionally_safe(X,
962
+ A,
963
+ N,
964
+ AN_limbs,
965
+ E,
966
+ E_limbs,
967
+ MBEDTLS_MPI_IS_PUBLIC,
968
+ RR,
969
+ T);
970
+ }
971
+
972
+ mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
973
+ const mbedtls_mpi_uint *A,
974
+ mbedtls_mpi_uint c, /* doubles as carry */
975
+ size_t limbs)
976
+ {
977
+ for (size_t i = 0; i < limbs; i++) {
978
+ mbedtls_mpi_uint s = A[i];
979
+ mbedtls_mpi_uint t = s - c;
980
+ c = (t > s);
981
+ X[i] = t;
982
+ }
983
+
984
+ return c;
985
+ }
986
+
987
+ mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
988
+ size_t limbs)
989
+ {
990
+ volatile const mbedtls_mpi_uint *force_read_A = A;
991
+ mbedtls_mpi_uint bits = 0;
992
+
993
+ for (size_t i = 0; i < limbs; i++) {
994
+ bits |= force_read_A[i];
995
+ }
996
+
997
+ return mbedtls_ct_bool(bits);
998
+ }
999
+
1000
+ void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
1001
+ const mbedtls_mpi_uint *A,
1002
+ const mbedtls_mpi_uint *N,
1003
+ size_t AN_limbs,
1004
+ mbedtls_mpi_uint mm,
1005
+ const mbedtls_mpi_uint *rr,
1006
+ mbedtls_mpi_uint *T)
1007
+ {
1008
+ mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
1009
+ }
1010
+
1011
+ void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
1012
+ const mbedtls_mpi_uint *A,
1013
+ const mbedtls_mpi_uint *N,
1014
+ size_t AN_limbs,
1015
+ mbedtls_mpi_uint mm,
1016
+ mbedtls_mpi_uint *T)
1017
+ {
1018
+ const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
1019
+
1020
+ mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
1021
+ }
1022
+
1023
+ /*
1024
+ * Compute X = A - B mod N.
1025
+ * Both A and B must be in [0, N) and so will the output.
1026
+ */
1027
+ static void mpi_core_sub_mod(mbedtls_mpi_uint *X,
1028
+ const mbedtls_mpi_uint *A,
1029
+ const mbedtls_mpi_uint *B,
1030
+ const mbedtls_mpi_uint *N,
1031
+ size_t limbs)
1032
+ {
1033
+ mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, limbs);
1034
+ (void) mbedtls_mpi_core_add_if(X, N, limbs, (unsigned) c);
1035
+ }
1036
+
1037
+ /*
1038
+ * Divide X by 2 mod N in place, assuming N is odd.
1039
+ * The input must be in [0, N) and so will the output.
1040
+ */
1041
+ MBEDTLS_STATIC_TESTABLE
1042
+ void mbedtls_mpi_core_div2_mod_odd(mbedtls_mpi_uint *X,
1043
+ const mbedtls_mpi_uint *N,
1044
+ size_t limbs)
1045
+ {
1046
+ /* If X is odd, add N to make it even before shifting. */
1047
+ unsigned odd = (unsigned) X[0] & 1;
1048
+ mbedtls_mpi_uint c = mbedtls_mpi_core_add_if(X, N, limbs, odd);
1049
+ mbedtls_mpi_core_shift_r(X, limbs, 1);
1050
+ X[limbs - 1] |= c << (biL - 1);
1051
+ }
1052
+
1053
+ /*
1054
+ * Constant-time GCD and modular inversion - odd modulus.
1055
+ *
1056
+ * Pre-conditions: see public documentation.
1057
+ *
1058
+ * See https://www.jstage.jst.go.jp/article/transinf/E106.D/9/E106.D_2022ICP0009/_pdf
1059
+ *
1060
+ * The paper gives two computationally equivalent algorithms: Alg 7 (readable)
1061
+ * and Alg 8 (constant-time). We use a third version that's hopefully both:
1062
+ *
1063
+ * u, v = A, N # N is called p in the paper but doesn't have to be prime
1064
+ * q, r = 0, 1
1065
+ * repeat bits(A_limbs + N_limbs) times:
1066
+ * d = v - u # t1 in Alg 7
1067
+ * t1 = (u and v both odd) ? u : d # t1 in Alg 8
1068
+ * t2 = (u and v both odd) ? d : (u odd) ? v : u # t2 in Alg 8
1069
+ * t2 >>= 1
1070
+ * swap = t1 > t2 # similar to s, z in Alg 8
1071
+ * u, v = (swap) ? t2, t1 : t1, t2
1072
+ *
1073
+ * d = r - q mod N # t2 in Alg 7
1074
+ * t1 = (u and v both odd) ? q : d # t3 in Alg 8
1075
+ * t2 = (u and v both odd) ? d : (u odd) ? r : q # t4 Alg 8
1076
+ * t2 /= 2 mod N # see below (pre_com)
1077
+ * q, r = (swap) ? t2, t1 : t1, t2
1078
+ * return v, q # v: GCD, see Alg 6; q: no mult by pre_com, see below
1079
+ *
1080
+ * The ternary operators in the above pseudo-code need to be realised in a
1081
+ * constant-time fashion. We use conditional assign for t1, t2 and conditional
1082
+ * swap for the final update. (Note: the similarity between branches of Alg 7
1083
+ * are highlighted in tables 2 and 3 and the surrounding text.)
1084
+ *
1085
+ * Also, we re-order operations, grouping things related to the inverse, which
1086
+ * facilitates making its computation optional, and requires fewer temporaries.
1087
+ *
1088
+ * The only actual change from the paper is dropping the trick with pre_com,
1089
+ * which I think complicates things for no benefit.
1090
+ * See the comment on the big I != NULL block below for details.
1091
+ */
1092
+ void mbedtls_mpi_core_gcd_modinv_odd(mbedtls_mpi_uint *G,
1093
+ mbedtls_mpi_uint *I,
1094
+ const mbedtls_mpi_uint *A,
1095
+ size_t A_limbs,
1096
+ const mbedtls_mpi_uint *N,
1097
+ size_t N_limbs,
1098
+ mbedtls_mpi_uint *T)
1099
+ {
1100
+ /* GCD and modinv, names common to Alg 7 and Alg 8 */
1101
+ mbedtls_mpi_uint *u = T + 0 * N_limbs;
1102
+ mbedtls_mpi_uint *v = G;
1103
+
1104
+ /* GCD and modinv, my name (t1, t2 from Alg 7) */
1105
+ mbedtls_mpi_uint *d = T + 1 * N_limbs;
1106
+
1107
+ /* GCD and modinv, names from Alg 8 (note: t1, t2 from Alg 7 are d above) */
1108
+ mbedtls_mpi_uint *t1 = T + 2 * N_limbs;
1109
+ mbedtls_mpi_uint *t2 = T + 3 * N_limbs;
1110
+
1111
+ /* modinv only, names common to Alg 7 and Alg 8 */
1112
+ mbedtls_mpi_uint *q = I;
1113
+ mbedtls_mpi_uint *r = I != NULL ? T + 4 * N_limbs : NULL;
1114
+
1115
+ /*
1116
+ * Initial values:
1117
+ * u, v = A, N
1118
+ * q, r = 0, 1
1119
+ *
1120
+ * We only write to G (aka v) after reading from inputs (A and N), which
1121
+ * allows aliasing, except with N when I != NULL, as then we'll be operating
1122
+ * mod N on q and r later - see the public documentation.
1123
+ */
1124
+ if (A_limbs > N_limbs) {
1125
+ /* Violating this precondition should not result in memory errors. */
1126
+ A_limbs = N_limbs;
1127
+ }
1128
+ memcpy(u, A, A_limbs * ciL);
1129
+ memset((char *) u + A_limbs * ciL, 0, (N_limbs - A_limbs) * ciL);
1130
+
1131
+ /* Avoid possible UB with memcpy when src == dst. */
1132
+ if (v != N) {
1133
+ memcpy(v, N, N_limbs * ciL);
1134
+ }
1135
+
1136
+ if (I != NULL) {
1137
+ memset(q, 0, N_limbs * ciL);
1138
+
1139
+ memset(r, 0, N_limbs * ciL);
1140
+ r[0] = 1;
1141
+ }
1142
+
1143
+ /*
1144
+ * At each step, out of u, v, v - u we keep one, shift another, and discard
1145
+ * the third, then update (u, v) with the ordered result.
1146
+ * Then we mirror those actions with q, r, r - q mod N.
1147
+ *
1148
+ * Loop invariants:
1149
+ * u <= v (on entry: A <= N)
1150
+ * GCD(u, v) == GCD(A, N) (on entry: trivial)
1151
+ * v = A * q mod N (on entry: N = A * 0 mod N)
1152
+ * u = A * r mod N (on entry: A = A * 1 mod N)
1153
+ * q, r in [0, N) (on entry: 0, 1)
1154
+ *
1155
+ * On exit:
1156
+ * u = 0
1157
+ * v = GCD(A, N) = A * q mod N
1158
+ * if v == 1 then 1 = A * q mod N ie q is A's inverse mod N
1159
+ * r = 0
1160
+ *
1161
+ * The exit state is a fixed point of the loop's body.
1162
+ * Alg 7 and Alg 8 use 2 * bitlen(N) iterations but Theorem 2 (above in the
1163
+ * paper) says bitlen(A) + bitlen(N) is actually enough.
1164
+ */
1165
+ for (size_t i = 0; i < (A_limbs + N_limbs) * biL; i++) {
1166
+ /* s, z in Alg 8 - use meaningful names instead */
1167
+ mbedtls_ct_condition_t u_odd = mbedtls_ct_bool(u[0] & 1);
1168
+ mbedtls_ct_condition_t v_odd = mbedtls_ct_bool(v[0] & 1);
1169
+
1170
+ /* Other conditions that will be useful below */
1171
+ mbedtls_ct_condition_t u_odd_v_odd = mbedtls_ct_bool_and(u_odd, v_odd);
1172
+ mbedtls_ct_condition_t v_even = mbedtls_ct_bool_not(v_odd);
1173
+ mbedtls_ct_condition_t u_odd_v_even = mbedtls_ct_bool_and(u_odd, v_even);
1174
+
1175
+ /* This is called t1 in Alg 7 (no name in Alg 8).
1176
+ * We know that u <= v so there is no carry */
1177
+ (void) mbedtls_mpi_core_sub(d, v, u, N_limbs);
1178
+
1179
+ /* t1 (the thing that's kept) can be d (default) or u (if t2 is d) */
1180
+ memcpy(t1, d, N_limbs * ciL);
1181
+ mbedtls_mpi_core_cond_assign(t1, u, N_limbs, u_odd_v_odd);
1182
+
1183
+ /* t2 (the thing that's shifted) can be u (if even), or v (if even),
1184
+ * or d (which is even if both u and v were odd) */
1185
+ memcpy(t2, u, N_limbs * ciL);
1186
+ mbedtls_mpi_core_cond_assign(t2, v, N_limbs, u_odd_v_even);
1187
+ mbedtls_mpi_core_cond_assign(t2, d, N_limbs, u_odd_v_odd);
1188
+
1189
+ mbedtls_mpi_core_shift_r(t2, N_limbs, 1); // t2 is even
1190
+
1191
+ /* Update u, v and re-order them if needed */
1192
+ memcpy(u, t1, N_limbs * ciL);
1193
+ memcpy(v, t2, N_limbs * ciL);
1194
+ mbedtls_ct_condition_t swap = mbedtls_mpi_core_lt_ct(v, u, N_limbs);
1195
+ mbedtls_mpi_core_cond_swap(u, v, N_limbs, swap);
1196
+
1197
+ /* Now, if modinv was requested, do the same with q, r, but:
1198
+ * - decisions still based on u and v (their initial values);
1199
+ * - operations are now mod N;
1200
+ * - we re-use t1, t2 for what the paper calls t3, t4 in Alg 8.
1201
+ *
1202
+ * Here we slightly diverge from the paper and instead do the obvious
1203
+ * thing that preserves the invariants involving q and r: mirror
1204
+ * operations on u and v, ie also divide by 2 here (mod N).
1205
+ *
1206
+ * The paper uses a trick where it replaces division by 2 with
1207
+ * multiplication by 2 here, and compensates in the end by multiplying
1208
+ * by pre_com, which is probably intended as an optimisation.
1209
+ *
1210
+ * However I believe it's not actually an optimisation, since
1211
+ * constant-time modular multiplication by 2 (left-shift + conditional
1212
+ * subtract) is just as costly as constant-time modular division by 2
1213
+ * (conditional add + right-shift). So, skip it and keep things simple.
1214
+ */
1215
+ if (I != NULL) {
1216
+ /* This is called t2 in Alg 7 (no name in Alg 8). */
1217
+ mpi_core_sub_mod(d, q, r, N, N_limbs);
1218
+
1219
+ /* t3 (the thing that's kept) */
1220
+ memcpy(t1, d, N_limbs * ciL);
1221
+ mbedtls_mpi_core_cond_assign(t1, r, N_limbs, u_odd_v_odd);
1222
+
1223
+ /* t4 (the thing that's shifted) */
1224
+ memcpy(t2, r, N_limbs * ciL);
1225
+ mbedtls_mpi_core_cond_assign(t2, q, N_limbs, u_odd_v_even);
1226
+ mbedtls_mpi_core_cond_assign(t2, d, N_limbs, u_odd_v_odd);
1227
+
1228
+ mbedtls_mpi_core_div2_mod_odd(t2, N, N_limbs);
1229
+
1230
+ /* Update and possibly swap */
1231
+ memcpy(r, t1, N_limbs * ciL);
1232
+ memcpy(q, t2, N_limbs * ciL);
1233
+ mbedtls_mpi_core_cond_swap(r, q, N_limbs, swap);
1234
+ }
1235
+ }
1236
+
1237
+ /* G and I already hold the correct values by virtue of being aliased */
1238
+ }
1239
+
1240
+ #endif /* MBEDTLS_BIGNUM_C */