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