@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,1137 @@
1
+ /*
2
+ * PSA crypto layer on top of Mbed TLS crypto
3
+ */
4
+ /*
5
+ * Copyright The Mbed TLS Contributors
6
+ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7
+ */
8
+
9
+ #include "common.h"
10
+
11
+ #if defined(MBEDTLS_PSA_CRYPTO_C)
12
+
13
+ #include "psa/crypto.h"
14
+
15
+ #include "psa_crypto_core.h"
16
+ #include "psa_crypto_driver_wrappers_no_static.h"
17
+ #include "psa_crypto_slot_management.h"
18
+ #include "psa_crypto_storage.h"
19
+ #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
20
+ #include "psa_crypto_se.h"
21
+ #endif
22
+
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+ #include "mbedtls/platform.h"
26
+ #if defined(MBEDTLS_THREADING_C)
27
+ #include "mbedtls/threading.h"
28
+ #endif
29
+
30
+
31
+
32
+ /* Make sure we have distinct ranges of key identifiers for distinct
33
+ * purposes. */
34
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MIN < PSA_KEY_ID_USER_MAX,
35
+ "Empty user key ID range");
36
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN < PSA_KEY_ID_VENDOR_MAX,
37
+ "Empty vendor key ID range");
38
+ MBEDTLS_STATIC_ASSERT(MBEDTLS_PSA_KEY_ID_BUILTIN_MIN <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX,
39
+ "Empty builtin key ID range");
40
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MIN <= PSA_KEY_ID_VOLATILE_MAX,
41
+ "Empty volatile key ID range");
42
+
43
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_USER_MAX < PSA_KEY_ID_VENDOR_MIN ||
44
+ PSA_KEY_ID_VENDOR_MAX < PSA_KEY_ID_USER_MIN,
45
+ "Overlap between user key IDs and vendor key IDs");
46
+
47
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN &&
48
+ MBEDTLS_PSA_KEY_ID_BUILTIN_MAX <= PSA_KEY_ID_VENDOR_MAX,
49
+ "Builtin key identifiers are not in the vendor range");
50
+
51
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VENDOR_MIN <= PSA_KEY_ID_VOLATILE_MIN &&
52
+ PSA_KEY_ID_VOLATILE_MAX <= PSA_KEY_ID_VENDOR_MAX,
53
+ "Volatile key identifiers are not in the vendor range");
54
+
55
+ MBEDTLS_STATIC_ASSERT(PSA_KEY_ID_VOLATILE_MAX < MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ||
56
+ MBEDTLS_PSA_KEY_ID_BUILTIN_MAX < PSA_KEY_ID_VOLATILE_MIN,
57
+ "Overlap between builtin key IDs and volatile key IDs");
58
+
59
+
60
+
61
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
62
+
63
+ /* Dynamic key store.
64
+ *
65
+ * The key store consists of multiple slices.
66
+ *
67
+ * The volatile keys are stored in variable-sized tables called slices.
68
+ * Slices are allocated on demand and deallocated when possible.
69
+ * The size of slices increases exponentially, so the average overhead
70
+ * (number of slots that are allocated but not used) is roughly
71
+ * proportional to the number of keys (with a factor that grows
72
+ * when the key store is fragmented).
73
+ *
74
+ * One slice is dedicated to the cache of persistent and built-in keys.
75
+ * For simplicity, they are separated from volatile keys. This cache
76
+ * slice has a fixed size and has the slice index KEY_SLOT_CACHE_SLICE_INDEX,
77
+ * located after the slices for volatile keys.
78
+ */
79
+
80
+ /* Size of the last slice containing the cache of persistent and built-in keys. */
81
+ #define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
82
+
83
+ /* Volatile keys are stored in slices 0 through
84
+ * (KEY_SLOT_VOLATILE_SLICE_COUNT - 1) inclusive.
85
+ * Each slice is twice the size of the previous slice.
86
+ * Volatile key identifiers encode the slice number as follows:
87
+ * bits 30..31: 0b10 (mandated by the PSA Crypto specification).
88
+ * bits 25..29: slice index (0...KEY_SLOT_VOLATILE_SLICE_COUNT-1)
89
+ * bits 0..24: slot index in slice
90
+ */
91
+ #define KEY_ID_SLOT_INDEX_WIDTH 25u
92
+ #define KEY_ID_SLICE_INDEX_WIDTH 5u
93
+
94
+ #define KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH 16u
95
+ #define KEY_SLOT_VOLATILE_SLICE_COUNT 22u
96
+ #define KEY_SLICE_COUNT (KEY_SLOT_VOLATILE_SLICE_COUNT + 1u)
97
+ #define KEY_SLOT_CACHE_SLICE_INDEX KEY_SLOT_VOLATILE_SLICE_COUNT
98
+
99
+
100
+ /* Check that the length of the largest slice (calculated as
101
+ * KEY_SLICE_LENGTH_MAX below) does not overflow size_t. We use
102
+ * an indirect method in case the calculation of KEY_SLICE_LENGTH_MAX
103
+ * itself overflows uintmax_t: if (BASE_LENGTH << c)
104
+ * overflows size_t then BASE_LENGTH > SIZE_MAX >> c.
105
+ */
106
+ #if (KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH > \
107
+ SIZE_MAX >> (KEY_SLOT_VOLATILE_SLICE_COUNT - 1))
108
+ #error "Maximum slice length overflows size_t"
109
+ #endif
110
+
111
+ #if KEY_ID_SLICE_INDEX_WIDTH + KEY_ID_SLOT_INDEX_WIDTH > 30
112
+ #error "Not enough room in volatile key IDs for slice index and slot index"
113
+ #endif
114
+ #if KEY_SLOT_VOLATILE_SLICE_COUNT > (1 << KEY_ID_SLICE_INDEX_WIDTH)
115
+ #error "Too many slices to fit the slice index in a volatile key ID"
116
+ #endif
117
+ #define KEY_SLICE_LENGTH_MAX \
118
+ (KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << (KEY_SLOT_VOLATILE_SLICE_COUNT - 1))
119
+ #if KEY_SLICE_LENGTH_MAX > 1 << KEY_ID_SLOT_INDEX_WIDTH
120
+ #error "Not enough room in volatile key IDs for a slot index in the largest slice"
121
+ #endif
122
+ #if KEY_ID_SLICE_INDEX_WIDTH > 8
123
+ #error "Slice index does not fit in uint8_t for psa_key_slot_t::slice_index"
124
+ #endif
125
+
126
+
127
+ /* Calculate the volatile key id to use for a given slot.
128
+ * This function assumes valid parameter values. */
129
+ static psa_key_id_t volatile_key_id_of_index(size_t slice_idx,
130
+ size_t slot_idx)
131
+ {
132
+ /* We assert above that the slice and slot indexes fit in separate
133
+ * bit-fields inside psa_key_id_t, which is a 32-bit type per the
134
+ * PSA Cryptography specification. */
135
+ return (psa_key_id_t) (0x40000000u |
136
+ (slice_idx << KEY_ID_SLOT_INDEX_WIDTH) |
137
+ slot_idx);
138
+ }
139
+
140
+ /* Calculate the slice containing the given volatile key.
141
+ * This function assumes valid parameter values. */
142
+ static size_t slice_index_of_volatile_key_id(psa_key_id_t key_id)
143
+ {
144
+ size_t mask = (1LU << KEY_ID_SLICE_INDEX_WIDTH) - 1;
145
+ return (key_id >> KEY_ID_SLOT_INDEX_WIDTH) & mask;
146
+ }
147
+
148
+ /* Calculate the index of the slot containing the given volatile key.
149
+ * This function assumes valid parameter values. */
150
+ static size_t slot_index_of_volatile_key_id(psa_key_id_t key_id)
151
+ {
152
+ return key_id & ((1LU << KEY_ID_SLOT_INDEX_WIDTH) - 1);
153
+ }
154
+
155
+ /* In global_data.first_free_slot_index, use this special value to
156
+ * indicate that the slice is full. */
157
+ #define FREE_SLOT_INDEX_NONE ((size_t) -1)
158
+
159
+ #if defined(MBEDTLS_TEST_HOOKS)
160
+ size_t psa_key_slot_volatile_slice_count(void)
161
+ {
162
+ return KEY_SLOT_VOLATILE_SLICE_COUNT;
163
+ }
164
+ #endif
165
+
166
+ #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
167
+
168
+ /* Static key store.
169
+ *
170
+ * All the keys (volatile or persistent) are in a single slice.
171
+ * We only use slices as a concept to allow some differences between
172
+ * static and dynamic key store management to be buried in auxiliary
173
+ * functions.
174
+ */
175
+
176
+ #define PERSISTENT_KEY_CACHE_COUNT MBEDTLS_PSA_KEY_SLOT_COUNT
177
+ #define KEY_SLICE_COUNT 1u
178
+ #define KEY_SLOT_CACHE_SLICE_INDEX 0
179
+
180
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
181
+
182
+
183
+ typedef struct {
184
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
185
+ psa_key_slot_t *key_slices[KEY_SLICE_COUNT];
186
+ size_t first_free_slot_index[KEY_SLOT_VOLATILE_SLICE_COUNT];
187
+ #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
188
+ psa_key_slot_t key_slots[MBEDTLS_PSA_KEY_SLOT_COUNT];
189
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
190
+ uint8_t key_slots_initialized;
191
+ } psa_global_data_t;
192
+
193
+ static psa_global_data_t global_data;
194
+
195
+ static uint8_t psa_get_key_slots_initialized(void)
196
+ {
197
+ uint8_t initialized;
198
+
199
+ #if defined(MBEDTLS_THREADING_C)
200
+ mbedtls_mutex_lock(&mbedtls_threading_psa_globaldata_mutex);
201
+ #endif /* defined(MBEDTLS_THREADING_C) */
202
+
203
+ initialized = global_data.key_slots_initialized;
204
+
205
+ #if defined(MBEDTLS_THREADING_C)
206
+ mbedtls_mutex_unlock(&mbedtls_threading_psa_globaldata_mutex);
207
+ #endif /* defined(MBEDTLS_THREADING_C) */
208
+
209
+ return initialized;
210
+ }
211
+
212
+
213
+
214
+ /** The length of the given slice in the key slot table.
215
+ *
216
+ * \param slice_idx The slice number. It must satisfy
217
+ * 0 <= slice_idx < KEY_SLICE_COUNT.
218
+ *
219
+ * \return The number of elements in the given slice.
220
+ */
221
+ static inline size_t key_slice_length(size_t slice_idx);
222
+
223
+ /** Get a pointer to the slot where the given volatile key is located.
224
+ *
225
+ * \param key_id The key identifier. It must be a valid volatile key
226
+ * identifier.
227
+ * \return A pointer to the only slot that the given key
228
+ * can be in. Note that the slot may be empty or
229
+ * contain a different key.
230
+ */
231
+ static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id);
232
+
233
+ /** Get a pointer to an entry in the persistent key cache.
234
+ *
235
+ * \param slot_idx The index in the table. It must satisfy
236
+ * 0 <= slot_idx < PERSISTENT_KEY_CACHE_COUNT.
237
+ * \return A pointer to the slot containing the given
238
+ * persistent key cache entry.
239
+ */
240
+ static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx);
241
+
242
+ /** Get a pointer to a slot given by slice and index.
243
+ *
244
+ * \param slice_idx The slice number. It must satisfy
245
+ * 0 <= slice_idx < KEY_SLICE_COUNT.
246
+ * \param slot_idx An index in the given slice. It must satisfy
247
+ * 0 <= slot_idx < key_slice_length(slice_idx).
248
+ *
249
+ * \return A pointer to the given slot.
250
+ */
251
+ static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx);
252
+
253
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
254
+
255
+ #if defined(MBEDTLS_TEST_HOOKS)
256
+ size_t (*mbedtls_test_hook_psa_volatile_key_slice_length)(size_t slice_idx) = NULL;
257
+ #endif
258
+
259
+ static inline size_t key_slice_length(size_t slice_idx)
260
+ {
261
+ if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) {
262
+ return PERSISTENT_KEY_CACHE_COUNT;
263
+ } else {
264
+ #if defined(MBEDTLS_TEST_HOOKS)
265
+ if (mbedtls_test_hook_psa_volatile_key_slice_length != NULL) {
266
+ return mbedtls_test_hook_psa_volatile_key_slice_length(slice_idx);
267
+ }
268
+ #endif
269
+ return KEY_SLOT_VOLATILE_SLICE_BASE_LENGTH << slice_idx;
270
+ }
271
+ }
272
+
273
+ static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
274
+ {
275
+ size_t slice_idx = slice_index_of_volatile_key_id(key_id);
276
+ if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) {
277
+ return NULL;
278
+ }
279
+ size_t slot_idx = slot_index_of_volatile_key_id(key_id);
280
+ if (slot_idx >= key_slice_length(slice_idx)) {
281
+ return NULL;
282
+ }
283
+ psa_key_slot_t *slice = global_data.key_slices[slice_idx];
284
+ if (slice == NULL) {
285
+ return NULL;
286
+ }
287
+ return &slice[slot_idx];
288
+ }
289
+
290
+ static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
291
+ {
292
+ return &global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX][slot_idx];
293
+ }
294
+
295
+ static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
296
+ {
297
+ return &global_data.key_slices[slice_idx][slot_idx];
298
+ }
299
+
300
+ #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
301
+
302
+ static inline size_t key_slice_length(size_t slice_idx)
303
+ {
304
+ (void) slice_idx;
305
+ return ARRAY_LENGTH(global_data.key_slots);
306
+ }
307
+
308
+ static inline psa_key_slot_t *get_volatile_key_slot(psa_key_id_t key_id)
309
+ {
310
+ MBEDTLS_STATIC_ASSERT(ARRAY_LENGTH(global_data.key_slots) <=
311
+ PSA_KEY_ID_VOLATILE_MAX - PSA_KEY_ID_VOLATILE_MIN + 1,
312
+ "The key slot array is larger than the volatile key ID range");
313
+ return &global_data.key_slots[key_id - PSA_KEY_ID_VOLATILE_MIN];
314
+ }
315
+
316
+ static inline psa_key_slot_t *get_persistent_key_slot(size_t slot_idx)
317
+ {
318
+ return &global_data.key_slots[slot_idx];
319
+ }
320
+
321
+ static inline psa_key_slot_t *get_key_slot(size_t slice_idx, size_t slot_idx)
322
+ {
323
+ (void) slice_idx;
324
+ return &global_data.key_slots[slot_idx];
325
+ }
326
+
327
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
328
+
329
+
330
+
331
+ int psa_is_valid_key_id(mbedtls_svc_key_id_t key, int vendor_ok)
332
+ {
333
+ psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
334
+
335
+ if ((PSA_KEY_ID_USER_MIN <= key_id) &&
336
+ (key_id <= PSA_KEY_ID_USER_MAX)) {
337
+ return 1;
338
+ }
339
+
340
+ if (vendor_ok &&
341
+ (PSA_KEY_ID_VENDOR_MIN <= key_id) &&
342
+ (key_id <= PSA_KEY_ID_VENDOR_MAX)) {
343
+ return 1;
344
+ }
345
+
346
+ return 0;
347
+ }
348
+
349
+ /** Get the description in memory of a key given its identifier and lock it.
350
+ *
351
+ * The descriptions of volatile keys and loaded persistent keys are
352
+ * stored in key slots. This function returns a pointer to the key slot
353
+ * containing the description of a key given its identifier.
354
+ *
355
+ * The function searches the key slots containing the description of the key
356
+ * with \p key identifier. The function does only read accesses to the key
357
+ * slots. The function does not load any persistent key thus does not access
358
+ * any storage.
359
+ *
360
+ * For volatile key identifiers, only one key slot is queried as a volatile
361
+ * key with identifier key_id can only be stored in slot of index
362
+ * ( key_id - #PSA_KEY_ID_VOLATILE_MIN ).
363
+ *
364
+ * On success, the function locks the key slot. It is the responsibility of
365
+ * the caller to unlock the key slot when it does not access it anymore.
366
+ *
367
+ * If multi-threading is enabled, the caller must hold the
368
+ * global key slot mutex.
369
+ *
370
+ * \param key Key identifier to query.
371
+ * \param[out] p_slot On success, `*p_slot` contains a pointer to the
372
+ * key slot containing the description of the key
373
+ * identified by \p key.
374
+ *
375
+ * \retval #PSA_SUCCESS
376
+ * The pointer to the key slot containing the description of the key
377
+ * identified by \p key was returned.
378
+ * \retval #PSA_ERROR_INVALID_HANDLE
379
+ * \p key is not a valid key identifier.
380
+ * \retval #PSA_ERROR_DOES_NOT_EXIST
381
+ * There is no key with key identifier \p key in the key slots.
382
+ */
383
+ static psa_status_t psa_get_and_lock_key_slot_in_memory(
384
+ mbedtls_svc_key_id_t key, psa_key_slot_t **p_slot)
385
+ {
386
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
387
+ psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key);
388
+ size_t slot_idx;
389
+ psa_key_slot_t *slot = NULL;
390
+
391
+ if (psa_key_id_is_volatile(key_id)) {
392
+ slot = get_volatile_key_slot(key_id);
393
+
394
+ /* Check if both the PSA key identifier key_id and the owner
395
+ * identifier of key match those of the key slot. */
396
+ if (slot != NULL &&
397
+ slot->state == PSA_SLOT_FULL &&
398
+ mbedtls_svc_key_id_equal(key, slot->attr.id)) {
399
+ status = PSA_SUCCESS;
400
+ } else {
401
+ status = PSA_ERROR_DOES_NOT_EXIST;
402
+ }
403
+ } else {
404
+ if (!psa_is_valid_key_id(key, 1)) {
405
+ return PSA_ERROR_INVALID_HANDLE;
406
+ }
407
+
408
+ for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
409
+ slot = get_persistent_key_slot(slot_idx);
410
+ /* Only consider slots which are in a full state. */
411
+ if ((slot->state == PSA_SLOT_FULL) &&
412
+ (mbedtls_svc_key_id_equal(key, slot->attr.id))) {
413
+ break;
414
+ }
415
+ }
416
+ status = (slot_idx < MBEDTLS_PSA_KEY_SLOT_COUNT) ?
417
+ PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
418
+ }
419
+
420
+ if (status == PSA_SUCCESS) {
421
+ status = psa_register_read(slot);
422
+ if (status == PSA_SUCCESS) {
423
+ *p_slot = slot;
424
+ }
425
+ }
426
+
427
+ return status;
428
+ }
429
+
430
+ psa_status_t psa_initialize_key_slots(void)
431
+ {
432
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
433
+ global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] =
434
+ mbedtls_calloc(PERSISTENT_KEY_CACHE_COUNT,
435
+ sizeof(*global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX]));
436
+ if (global_data.key_slices[KEY_SLOT_CACHE_SLICE_INDEX] == NULL) {
437
+ return PSA_ERROR_INSUFFICIENT_MEMORY;
438
+ }
439
+ #else /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
440
+ /* Nothing to do: program startup and psa_wipe_all_key_slots() both
441
+ * guarantee that the key slots are initialized to all-zero, which
442
+ * means that all the key slots are in a valid, empty state. The global
443
+ * data mutex is already held when calling this function, so no need to
444
+ * lock it here, to set the flag. */
445
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
446
+
447
+ global_data.key_slots_initialized = 1;
448
+ return PSA_SUCCESS;
449
+ }
450
+
451
+ void psa_wipe_all_key_slots(void)
452
+ {
453
+ for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
454
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
455
+ if (global_data.key_slices[slice_idx] == NULL) {
456
+ continue;
457
+ }
458
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
459
+ for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
460
+ psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
461
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
462
+ /* When MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled, calling
463
+ * psa_wipe_key_slot() on an unused slot is useless, but it
464
+ * happens to work (because we flip the state to PENDING_DELETION).
465
+ *
466
+ * When MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled,
467
+ * psa_wipe_key_slot() needs to have a valid slice_index
468
+ * field, but that value might not be correct in a
469
+ * free slot, so we must not call it.
470
+ *
471
+ * Bypass the call to psa_wipe_key_slot() if the slot is empty,
472
+ * but only if MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, to save
473
+ * a few bytes of code size otherwise.
474
+ */
475
+ if (slot->state == PSA_SLOT_EMPTY) {
476
+ continue;
477
+ }
478
+ #endif
479
+ slot->var.occupied.registered_readers = 1;
480
+ slot->state = PSA_SLOT_PENDING_DELETION;
481
+ (void) psa_wipe_key_slot(slot);
482
+ }
483
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
484
+ mbedtls_free(global_data.key_slices[slice_idx]);
485
+ global_data.key_slices[slice_idx] = NULL;
486
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
487
+ }
488
+
489
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
490
+ for (size_t slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) {
491
+ global_data.first_free_slot_index[slice_idx] = 0;
492
+ }
493
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
494
+
495
+ /* The global data mutex is already held when calling this function. */
496
+ global_data.key_slots_initialized = 0;
497
+ }
498
+
499
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
500
+
501
+ static psa_status_t psa_allocate_volatile_key_slot(psa_key_id_t *key_id,
502
+ psa_key_slot_t **p_slot)
503
+ {
504
+ size_t slice_idx;
505
+ for (slice_idx = 0; slice_idx < KEY_SLOT_VOLATILE_SLICE_COUNT; slice_idx++) {
506
+ if (global_data.first_free_slot_index[slice_idx] != FREE_SLOT_INDEX_NONE) {
507
+ break;
508
+ }
509
+ }
510
+ if (slice_idx == KEY_SLOT_VOLATILE_SLICE_COUNT) {
511
+ return PSA_ERROR_INSUFFICIENT_MEMORY;
512
+ }
513
+
514
+ if (global_data.key_slices[slice_idx] == NULL) {
515
+ global_data.key_slices[slice_idx] =
516
+ mbedtls_calloc(key_slice_length(slice_idx),
517
+ sizeof(psa_key_slot_t));
518
+ if (global_data.key_slices[slice_idx] == NULL) {
519
+ return PSA_ERROR_INSUFFICIENT_MEMORY;
520
+ }
521
+ }
522
+ psa_key_slot_t *slice = global_data.key_slices[slice_idx];
523
+
524
+ size_t slot_idx = global_data.first_free_slot_index[slice_idx];
525
+ *key_id = volatile_key_id_of_index(slice_idx, slot_idx);
526
+
527
+ psa_key_slot_t *slot = &slice[slot_idx];
528
+ size_t next_free = slot_idx + 1 + slot->var.free.next_free_relative_to_next;
529
+ if (next_free >= key_slice_length(slice_idx)) {
530
+ next_free = FREE_SLOT_INDEX_NONE;
531
+ }
532
+ global_data.first_free_slot_index[slice_idx] = next_free;
533
+ /* The .next_free field is not meaningful when the slot is not free,
534
+ * so give it the same content as freshly initialized memory. */
535
+ slot->var.free.next_free_relative_to_next = 0;
536
+
537
+ psa_status_t status = psa_key_slot_state_transition(slot,
538
+ PSA_SLOT_EMPTY,
539
+ PSA_SLOT_FILLING);
540
+ if (status != PSA_SUCCESS) {
541
+ /* The only reason for failure is if the slot state was not empty.
542
+ * This indicates that something has gone horribly wrong.
543
+ * In this case, we leave the slot out of the free list, and stop
544
+ * modifying it. This minimizes any further corruption. The slot
545
+ * is a memory leak, but that's a lesser evil. */
546
+ return status;
547
+ }
548
+
549
+ *p_slot = slot;
550
+ /* We assert at compile time that the slice index fits in uint8_t. */
551
+ slot->slice_index = (uint8_t) slice_idx;
552
+ return PSA_SUCCESS;
553
+ }
554
+
555
+ psa_status_t psa_free_key_slot(size_t slice_idx,
556
+ psa_key_slot_t *slot)
557
+ {
558
+
559
+ if (slice_idx == KEY_SLOT_CACHE_SLICE_INDEX) {
560
+ /* This is a cache entry. We don't maintain a free list, so
561
+ * there's nothing to do. */
562
+ return PSA_SUCCESS;
563
+ }
564
+ if (slice_idx >= KEY_SLOT_VOLATILE_SLICE_COUNT) {
565
+ return PSA_ERROR_CORRUPTION_DETECTED;
566
+ }
567
+
568
+ psa_key_slot_t *slice = global_data.key_slices[slice_idx];
569
+ psa_key_slot_t *slice_end = slice + key_slice_length(slice_idx);
570
+ if (slot < slice || slot >= slice_end) {
571
+ /* The slot isn't actually in the slice! We can't detect that
572
+ * condition for sure, because the pointer comparison itself is
573
+ * undefined behavior in that case. That same condition makes the
574
+ * subtraction to calculate the slot index also UB.
575
+ * Give up now to avoid causing further corruption.
576
+ */
577
+ return PSA_ERROR_CORRUPTION_DETECTED;
578
+ }
579
+ size_t slot_idx = slot - slice;
580
+
581
+ size_t next_free = global_data.first_free_slot_index[slice_idx];
582
+ if (next_free >= key_slice_length(slice_idx)) {
583
+ /* The slot was full. The newly freed slot thus becomes the
584
+ * end of the free list. */
585
+ next_free = key_slice_length(slice_idx);
586
+ }
587
+ global_data.first_free_slot_index[slice_idx] = slot_idx;
588
+ slot->var.free.next_free_relative_to_next =
589
+ (int32_t) next_free - (int32_t) slot_idx - 1;
590
+
591
+ return PSA_SUCCESS;
592
+ }
593
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
594
+
595
+ psa_status_t psa_reserve_free_key_slot(psa_key_id_t *volatile_key_id,
596
+ psa_key_slot_t **p_slot)
597
+ {
598
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
599
+ size_t slot_idx;
600
+ psa_key_slot_t *selected_slot, *unused_persistent_key_slot;
601
+
602
+ if (!psa_get_key_slots_initialized()) {
603
+ status = PSA_ERROR_BAD_STATE;
604
+ goto error;
605
+ }
606
+
607
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
608
+ if (volatile_key_id != NULL) {
609
+ return psa_allocate_volatile_key_slot(volatile_key_id, p_slot);
610
+ }
611
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
612
+
613
+ /* With a dynamic key store, allocate an entry in the cache slice,
614
+ * applicable only to non-volatile keys that get cached in RAM.
615
+ * With a static key store, allocate an entry in the sole slice,
616
+ * applicable to all keys. */
617
+ selected_slot = unused_persistent_key_slot = NULL;
618
+ for (slot_idx = 0; slot_idx < PERSISTENT_KEY_CACHE_COUNT; slot_idx++) {
619
+ psa_key_slot_t *slot = get_key_slot(KEY_SLOT_CACHE_SLICE_INDEX, slot_idx);
620
+ if (slot->state == PSA_SLOT_EMPTY) {
621
+ selected_slot = slot;
622
+ break;
623
+ }
624
+
625
+ if ((unused_persistent_key_slot == NULL) &&
626
+ (slot->state == PSA_SLOT_FULL) &&
627
+ (!psa_key_slot_has_readers(slot)) &&
628
+ (!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime))) {
629
+ unused_persistent_key_slot = slot;
630
+ }
631
+ }
632
+
633
+ /*
634
+ * If there is no unused key slot and there is at least one unlocked key
635
+ * slot containing the description of a persistent key, recycle the first
636
+ * such key slot we encountered. If we later need to operate on the
637
+ * persistent key we are evicting now, we will reload its description from
638
+ * storage.
639
+ */
640
+ if ((selected_slot == NULL) &&
641
+ (unused_persistent_key_slot != NULL)) {
642
+ selected_slot = unused_persistent_key_slot;
643
+ psa_register_read(selected_slot);
644
+ status = psa_wipe_key_slot(selected_slot);
645
+ if (status != PSA_SUCCESS) {
646
+ goto error;
647
+ }
648
+ }
649
+
650
+ if (selected_slot != NULL) {
651
+ status = psa_key_slot_state_transition(selected_slot, PSA_SLOT_EMPTY,
652
+ PSA_SLOT_FILLING);
653
+ if (status != PSA_SUCCESS) {
654
+ goto error;
655
+ }
656
+
657
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
658
+ selected_slot->slice_index = KEY_SLOT_CACHE_SLICE_INDEX;
659
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
660
+
661
+ #if !defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
662
+ if (volatile_key_id != NULL) {
663
+ /* Refresh slot_idx, for when the slot is not the original
664
+ * selected_slot but rather unused_persistent_key_slot. */
665
+ slot_idx = selected_slot - global_data.key_slots;
666
+ *volatile_key_id = PSA_KEY_ID_VOLATILE_MIN + (psa_key_id_t) slot_idx;
667
+ }
668
+ #endif
669
+ *p_slot = selected_slot;
670
+
671
+ return PSA_SUCCESS;
672
+ }
673
+ status = PSA_ERROR_INSUFFICIENT_MEMORY;
674
+
675
+ error:
676
+ *p_slot = NULL;
677
+
678
+ return status;
679
+ }
680
+
681
+ #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
682
+ static psa_status_t psa_load_persistent_key_into_slot(psa_key_slot_t *slot)
683
+ {
684
+ psa_status_t status = PSA_SUCCESS;
685
+ uint8_t *key_data = NULL;
686
+ size_t key_data_length = 0;
687
+ psa_key_id_t key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
688
+
689
+ /* Do not try to load a persistent key whose ID is in the volatile range. */
690
+ if ((key_id >= PSA_KEY_ID_VOLATILE_MIN) && (key_id <= PSA_KEY_ID_VOLATILE_MAX)) {
691
+ return PSA_ERROR_DOES_NOT_EXIST;
692
+ }
693
+
694
+ status = psa_load_persistent_key(&slot->attr,
695
+ &key_data, &key_data_length);
696
+ if (status != PSA_SUCCESS) {
697
+ goto exit;
698
+ }
699
+
700
+ #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
701
+ /* Special handling is required for loading keys associated with a
702
+ * dynamically registered SE interface. */
703
+ const psa_drv_se_t *drv;
704
+ psa_drv_se_context_t *drv_context;
705
+ if (psa_get_se_driver(slot->attr.lifetime, &drv, &drv_context)) {
706
+ psa_se_key_data_storage_t *data;
707
+
708
+ if (key_data_length != sizeof(*data)) {
709
+ status = PSA_ERROR_DATA_INVALID;
710
+ goto exit;
711
+ }
712
+ data = (psa_se_key_data_storage_t *) key_data;
713
+ status = psa_copy_key_material_into_slot(
714
+ slot, data->slot_number, sizeof(data->slot_number));
715
+ goto exit;
716
+ }
717
+ #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
718
+
719
+ status = psa_copy_key_material_into_slot(slot, key_data, key_data_length);
720
+ if (status != PSA_SUCCESS) {
721
+ goto exit;
722
+ }
723
+
724
+ exit:
725
+ psa_free_persistent_key_data(key_data, key_data_length);
726
+ return status;
727
+ }
728
+ #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
729
+
730
+ #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
731
+
732
+ static psa_status_t psa_load_builtin_key_into_slot(psa_key_slot_t *slot)
733
+ {
734
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
735
+ psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
736
+ psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_VOLATILE;
737
+ psa_drv_slot_number_t slot_number = 0;
738
+ size_t key_buffer_size = 0;
739
+ size_t key_buffer_length = 0;
740
+
741
+ if (!psa_key_id_is_builtin(
742
+ MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id))) {
743
+ return PSA_ERROR_DOES_NOT_EXIST;
744
+ }
745
+
746
+ /* Check the platform function to see whether this key actually exists */
747
+ status = mbedtls_psa_platform_get_builtin_key(
748
+ slot->attr.id, &lifetime, &slot_number);
749
+ if (status != PSA_SUCCESS) {
750
+ return status;
751
+ }
752
+
753
+ /* Set required key attributes to ensure get_builtin_key can retrieve the
754
+ * full attributes. */
755
+ psa_set_key_id(&attributes, slot->attr.id);
756
+ psa_set_key_lifetime(&attributes, lifetime);
757
+
758
+ /* Get the full key attributes from the driver in order to be able to
759
+ * calculate the required buffer size. */
760
+ status = psa_driver_wrapper_get_builtin_key(
761
+ slot_number, &attributes,
762
+ NULL, 0, NULL);
763
+ if (status != PSA_ERROR_BUFFER_TOO_SMALL) {
764
+ /* Builtin keys cannot be defined by the attributes alone */
765
+ if (status == PSA_SUCCESS) {
766
+ status = PSA_ERROR_CORRUPTION_DETECTED;
767
+ }
768
+ return status;
769
+ }
770
+
771
+ /* If the key should exist according to the platform, then ask the driver
772
+ * what its expected size is. */
773
+ status = psa_driver_wrapper_get_key_buffer_size(&attributes,
774
+ &key_buffer_size);
775
+ if (status != PSA_SUCCESS) {
776
+ return status;
777
+ }
778
+
779
+ /* Allocate a buffer of the required size and load the builtin key directly
780
+ * into the (now properly sized) slot buffer. */
781
+ status = psa_allocate_buffer_to_slot(slot, key_buffer_size);
782
+ if (status != PSA_SUCCESS) {
783
+ return status;
784
+ }
785
+
786
+ status = psa_driver_wrapper_get_builtin_key(
787
+ slot_number, &attributes,
788
+ slot->key.data, slot->key.bytes, &key_buffer_length);
789
+ if (status != PSA_SUCCESS) {
790
+ goto exit;
791
+ }
792
+
793
+ /* Copy actual key length and core attributes into the slot on success */
794
+ slot->key.bytes = key_buffer_length;
795
+ slot->attr = attributes;
796
+ exit:
797
+ if (status != PSA_SUCCESS) {
798
+ psa_remove_key_data_from_memory(slot);
799
+ }
800
+ return status;
801
+ }
802
+ #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
803
+
804
+ psa_status_t psa_get_and_lock_key_slot(mbedtls_svc_key_id_t key,
805
+ psa_key_slot_t **p_slot)
806
+ {
807
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
808
+
809
+ *p_slot = NULL;
810
+ if (!psa_get_key_slots_initialized()) {
811
+ return PSA_ERROR_BAD_STATE;
812
+ }
813
+
814
+ #if defined(MBEDTLS_THREADING_C)
815
+ /* We need to set status as success, otherwise CORRUPTION_DETECTED
816
+ * would be returned if the lock fails. */
817
+ status = PSA_SUCCESS;
818
+ /* If the key is persistent and not loaded, we cannot unlock the mutex
819
+ * between checking if the key is loaded and setting the slot as FULL,
820
+ * as otherwise another thread may load and then destroy the key
821
+ * in the meantime. */
822
+ PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
823
+ &mbedtls_threading_key_slot_mutex));
824
+ #endif
825
+ /*
826
+ * On success, the pointer to the slot is passed directly to the caller
827
+ * thus no need to unlock the key slot here.
828
+ */
829
+ status = psa_get_and_lock_key_slot_in_memory(key, p_slot);
830
+ if (status != PSA_ERROR_DOES_NOT_EXIST) {
831
+ #if defined(MBEDTLS_THREADING_C)
832
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
833
+ &mbedtls_threading_key_slot_mutex));
834
+ #endif
835
+ return status;
836
+ }
837
+
838
+ /* Loading keys from storage requires support for such a mechanism */
839
+ #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
840
+ defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
841
+
842
+ status = psa_reserve_free_key_slot(NULL, p_slot);
843
+ if (status != PSA_SUCCESS) {
844
+ #if defined(MBEDTLS_THREADING_C)
845
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
846
+ &mbedtls_threading_key_slot_mutex));
847
+ #endif
848
+ return status;
849
+ }
850
+
851
+ (*p_slot)->attr.id = key;
852
+ (*p_slot)->attr.lifetime = PSA_KEY_LIFETIME_PERSISTENT;
853
+
854
+ status = PSA_ERROR_DOES_NOT_EXIST;
855
+ #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
856
+ /* Load keys in the 'builtin' range through their own interface */
857
+ status = psa_load_builtin_key_into_slot(*p_slot);
858
+ #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
859
+
860
+ #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
861
+ if (status == PSA_ERROR_DOES_NOT_EXIST) {
862
+ status = psa_load_persistent_key_into_slot(*p_slot);
863
+ }
864
+ #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
865
+
866
+ if (status != PSA_SUCCESS) {
867
+ psa_wipe_key_slot(*p_slot);
868
+
869
+ /* If the key does not exist, we need to return
870
+ * PSA_ERROR_INVALID_HANDLE. */
871
+ if (status == PSA_ERROR_DOES_NOT_EXIST) {
872
+ status = PSA_ERROR_INVALID_HANDLE;
873
+ }
874
+ } else {
875
+ /* Add implicit usage flags. */
876
+ psa_extend_key_usage_flags(&(*p_slot)->attr.policy.usage);
877
+
878
+ psa_key_slot_state_transition((*p_slot), PSA_SLOT_FILLING,
879
+ PSA_SLOT_FULL);
880
+ status = psa_register_read(*p_slot);
881
+ }
882
+
883
+ #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
884
+ status = PSA_ERROR_INVALID_HANDLE;
885
+ #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
886
+
887
+ if (status != PSA_SUCCESS) {
888
+ *p_slot = NULL;
889
+ }
890
+ #if defined(MBEDTLS_THREADING_C)
891
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
892
+ &mbedtls_threading_key_slot_mutex));
893
+ #endif
894
+ return status;
895
+ }
896
+
897
+ psa_status_t psa_unregister_read(psa_key_slot_t *slot)
898
+ {
899
+ if (slot == NULL) {
900
+ return PSA_SUCCESS;
901
+ }
902
+ if ((slot->state != PSA_SLOT_FULL) &&
903
+ (slot->state != PSA_SLOT_PENDING_DELETION)) {
904
+ return PSA_ERROR_CORRUPTION_DETECTED;
905
+ }
906
+
907
+ /* If we are the last reader and the slot is marked for deletion,
908
+ * we must wipe the slot here. */
909
+ if ((slot->state == PSA_SLOT_PENDING_DELETION) &&
910
+ (slot->var.occupied.registered_readers == 1)) {
911
+ return psa_wipe_key_slot(slot);
912
+ }
913
+
914
+ if (psa_key_slot_has_readers(slot)) {
915
+ slot->var.occupied.registered_readers--;
916
+ return PSA_SUCCESS;
917
+ }
918
+
919
+ /*
920
+ * As the return error code may not be handled in case of multiple errors,
921
+ * do our best to report if there are no registered readers. Assert with
922
+ * MBEDTLS_TEST_HOOK_TEST_ASSERT that there are registered readers:
923
+ * if the MBEDTLS_TEST_HOOKS configuration option is enabled and
924
+ * the function is called as part of the execution of a test suite, the
925
+ * execution of the test suite is stopped in error if the assertion fails.
926
+ */
927
+ MBEDTLS_TEST_HOOK_TEST_ASSERT(psa_key_slot_has_readers(slot));
928
+ return PSA_ERROR_CORRUPTION_DETECTED;
929
+ }
930
+
931
+ psa_status_t psa_unregister_read_under_mutex(psa_key_slot_t *slot)
932
+ {
933
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
934
+ #if defined(MBEDTLS_THREADING_C)
935
+ /* We need to set status as success, otherwise CORRUPTION_DETECTED
936
+ * would be returned if the lock fails. */
937
+ status = PSA_SUCCESS;
938
+ PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
939
+ &mbedtls_threading_key_slot_mutex));
940
+ #endif
941
+ status = psa_unregister_read(slot);
942
+ #if defined(MBEDTLS_THREADING_C)
943
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
944
+ &mbedtls_threading_key_slot_mutex));
945
+ #endif
946
+ return status;
947
+ }
948
+
949
+ psa_status_t psa_validate_key_location(psa_key_lifetime_t lifetime,
950
+ psa_se_drv_table_entry_t **p_drv)
951
+ {
952
+ if (psa_key_lifetime_is_external(lifetime)) {
953
+ #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
954
+ /* Check whether a driver is registered against this lifetime */
955
+ psa_se_drv_table_entry_t *driver = psa_get_se_driver_entry(lifetime);
956
+ if (driver != NULL) {
957
+ if (p_drv != NULL) {
958
+ *p_drv = driver;
959
+ }
960
+ return PSA_SUCCESS;
961
+ }
962
+ #else /* MBEDTLS_PSA_CRYPTO_SE_C */
963
+ (void) p_drv;
964
+ #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
965
+
966
+ /* Key location for external keys gets checked by the wrapper */
967
+ return PSA_SUCCESS;
968
+ } else {
969
+ /* Local/internal keys are always valid */
970
+ return PSA_SUCCESS;
971
+ }
972
+ }
973
+
974
+ psa_status_t psa_validate_key_persistence(psa_key_lifetime_t lifetime)
975
+ {
976
+ if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
977
+ /* Volatile keys are always supported */
978
+ return PSA_SUCCESS;
979
+ } else {
980
+ /* Persistent keys require storage support */
981
+ #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
982
+ if (PSA_KEY_LIFETIME_IS_READ_ONLY(lifetime)) {
983
+ return PSA_ERROR_INVALID_ARGUMENT;
984
+ } else {
985
+ return PSA_SUCCESS;
986
+ }
987
+ #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
988
+ return PSA_ERROR_NOT_SUPPORTED;
989
+ #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
990
+ }
991
+ }
992
+
993
+ psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
994
+ {
995
+ #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) || \
996
+ defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
997
+ psa_status_t status;
998
+ psa_key_slot_t *slot;
999
+
1000
+ status = psa_get_and_lock_key_slot(key, &slot);
1001
+ if (status != PSA_SUCCESS) {
1002
+ *handle = PSA_KEY_HANDLE_INIT;
1003
+ if (status == PSA_ERROR_INVALID_HANDLE) {
1004
+ status = PSA_ERROR_DOES_NOT_EXIST;
1005
+ }
1006
+
1007
+ return status;
1008
+ }
1009
+
1010
+ *handle = key;
1011
+
1012
+ return psa_unregister_read_under_mutex(slot);
1013
+
1014
+ #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
1015
+ (void) key;
1016
+ *handle = PSA_KEY_HANDLE_INIT;
1017
+ return PSA_ERROR_NOT_SUPPORTED;
1018
+ #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C || MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
1019
+ }
1020
+
1021
+ psa_status_t psa_close_key(psa_key_handle_t handle)
1022
+ {
1023
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1024
+ psa_key_slot_t *slot;
1025
+
1026
+ if (psa_key_handle_is_null(handle)) {
1027
+ return PSA_SUCCESS;
1028
+ }
1029
+
1030
+ #if defined(MBEDTLS_THREADING_C)
1031
+ /* We need to set status as success, otherwise CORRUPTION_DETECTED
1032
+ * would be returned if the lock fails. */
1033
+ status = PSA_SUCCESS;
1034
+ PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1035
+ &mbedtls_threading_key_slot_mutex));
1036
+ #endif
1037
+ status = psa_get_and_lock_key_slot_in_memory(handle, &slot);
1038
+ if (status != PSA_SUCCESS) {
1039
+ if (status == PSA_ERROR_DOES_NOT_EXIST) {
1040
+ status = PSA_ERROR_INVALID_HANDLE;
1041
+ }
1042
+ #if defined(MBEDTLS_THREADING_C)
1043
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1044
+ &mbedtls_threading_key_slot_mutex));
1045
+ #endif
1046
+ return status;
1047
+ }
1048
+
1049
+ if (slot->var.occupied.registered_readers == 1) {
1050
+ status = psa_wipe_key_slot(slot);
1051
+ } else {
1052
+ status = psa_unregister_read(slot);
1053
+ }
1054
+ #if defined(MBEDTLS_THREADING_C)
1055
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1056
+ &mbedtls_threading_key_slot_mutex));
1057
+ #endif
1058
+
1059
+ return status;
1060
+ }
1061
+
1062
+ psa_status_t psa_purge_key(mbedtls_svc_key_id_t key)
1063
+ {
1064
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1065
+ psa_key_slot_t *slot;
1066
+
1067
+ #if defined(MBEDTLS_THREADING_C)
1068
+ /* We need to set status as success, otherwise CORRUPTION_DETECTED
1069
+ * would be returned if the lock fails. */
1070
+ status = PSA_SUCCESS;
1071
+ PSA_THREADING_CHK_RET(mbedtls_mutex_lock(
1072
+ &mbedtls_threading_key_slot_mutex));
1073
+ #endif
1074
+ status = psa_get_and_lock_key_slot_in_memory(key, &slot);
1075
+ if (status != PSA_SUCCESS) {
1076
+ #if defined(MBEDTLS_THREADING_C)
1077
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1078
+ &mbedtls_threading_key_slot_mutex));
1079
+ #endif
1080
+ return status;
1081
+ }
1082
+
1083
+ if ((!PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) &&
1084
+ (slot->var.occupied.registered_readers == 1)) {
1085
+ status = psa_wipe_key_slot(slot);
1086
+ } else {
1087
+ status = psa_unregister_read(slot);
1088
+ }
1089
+ #if defined(MBEDTLS_THREADING_C)
1090
+ PSA_THREADING_CHK_RET(mbedtls_mutex_unlock(
1091
+ &mbedtls_threading_key_slot_mutex));
1092
+ #endif
1093
+
1094
+ return status;
1095
+ }
1096
+
1097
+ void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats)
1098
+ {
1099
+ memset(stats, 0, sizeof(*stats));
1100
+
1101
+ for (size_t slice_idx = 0; slice_idx < KEY_SLICE_COUNT; slice_idx++) {
1102
+ #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
1103
+ if (global_data.key_slices[slice_idx] == NULL) {
1104
+ continue;
1105
+ }
1106
+ #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
1107
+ for (size_t slot_idx = 0; slot_idx < key_slice_length(slice_idx); slot_idx++) {
1108
+ const psa_key_slot_t *slot = get_key_slot(slice_idx, slot_idx);
1109
+ if (slot->state == PSA_SLOT_EMPTY) {
1110
+ ++stats->empty_slots;
1111
+ continue;
1112
+ }
1113
+ if (psa_key_slot_has_readers(slot)) {
1114
+ ++stats->locked_slots;
1115
+ }
1116
+ if (PSA_KEY_LIFETIME_IS_VOLATILE(slot->attr.lifetime)) {
1117
+ ++stats->volatile_slots;
1118
+ } else {
1119
+ psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
1120
+ ++stats->persistent_slots;
1121
+ if (id > stats->max_open_internal_key_id) {
1122
+ stats->max_open_internal_key_id = id;
1123
+ }
1124
+ }
1125
+ if (PSA_KEY_LIFETIME_GET_LOCATION(slot->attr.lifetime) !=
1126
+ PSA_KEY_LOCATION_LOCAL_STORAGE) {
1127
+ psa_key_id_t id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID(slot->attr.id);
1128
+ ++stats->external_slots;
1129
+ if (id > stats->max_open_external_key_id) {
1130
+ stats->max_open_external_key_id = id;
1131
+ }
1132
+ }
1133
+ }
1134
+ }
1135
+ }
1136
+
1137
+ #endif /* MBEDTLS_PSA_CRYPTO_C */