psa_crypto_pake.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * PSA PAKE layer on top of Mbed TLS software crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include <psa/crypto.h>
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_pake.h"
  25. #include "psa_crypto_slot_management.h"
  26. #include <mbedtls/ecjpake.h>
  27. #include <mbedtls/psa_util.h>
  28. #include <mbedtls/platform.h>
  29. #include <mbedtls/error.h>
  30. #include <string.h>
  31. /*
  32. * State sequence:
  33. *
  34. * psa_pake_setup()
  35. * |
  36. * |-- In any order:
  37. * | | psa_pake_set_password_key()
  38. * | | psa_pake_set_user()
  39. * | | psa_pake_set_peer()
  40. * | | psa_pake_set_role()
  41. * |
  42. * |--- In any order: (First round input before or after first round output)
  43. * | |
  44. * | |------ In Order
  45. * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
  46. * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
  47. * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
  48. * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
  49. * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
  50. * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
  51. * | |
  52. * | |------ In Order:
  53. * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
  54. * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
  55. * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
  56. * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
  57. * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
  58. * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
  59. * |
  60. * |--- In any order: (Second round input before or after second round output)
  61. * | |
  62. * | |------ In Order
  63. * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
  64. * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
  65. * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
  66. * | |
  67. * | |------ In Order:
  68. * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
  69. * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
  70. * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
  71. * |
  72. * psa_pake_get_implicit_key()
  73. * psa_pake_abort()
  74. */
  75. /*
  76. * The first PAKE step shares the same sequences of the second PAKE step
  77. * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
  78. * It's simpler to share the same sequences numbers of the first
  79. * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
  80. *
  81. * State sequence with step, state & sequence enums:
  82. * => Input & Output Step = PSA_PAKE_STEP_INVALID
  83. * => state = PSA_PAKE_STATE_INVALID
  84. * psa_pake_setup()
  85. * => Input & Output Step = PSA_PAKE_STEP_X1_X2
  86. * => state = PSA_PAKE_STATE_SETUP
  87. * => sequence = PSA_PAKE_SEQ_INVALID
  88. * |
  89. * |--- In any order: (First round input before or after first round output)
  90. * | | First call of psa_pake_output() or psa_pake_input() sets
  91. * | | state = PSA_PAKE_STATE_READY
  92. * | |
  93. * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
  94. * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
  95. * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
  96. * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
  97. * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
  98. * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
  99. * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
  100. * | | | => state = PSA_PAKE_STATE_READY
  101. * | | | => sequence = PSA_PAKE_SEQ_INVALID
  102. * | | | => Output Step = PSA_PAKE_STEP_X2S
  103. * | |
  104. * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
  105. * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
  106. * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
  107. * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
  108. * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
  109. * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
  110. * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
  111. * | | | => state = PSA_PAKE_STATE_READY
  112. * | | | => sequence = PSA_PAKE_SEQ_INVALID
  113. * | | | => Output Step = PSA_PAKE_INPUT_X4S
  114. * |
  115. * |--- In any order: (Second round input before or after second round output)
  116. * | |
  117. * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
  118. * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
  119. * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
  120. * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
  121. * | | | => state = PSA_PAKE_STATE_READY
  122. * | | | => sequence = PSA_PAKE_SEQ_INVALID
  123. * | | | => Output Step = PSA_PAKE_STEP_DERIVE
  124. * | |
  125. * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
  126. * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
  127. * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
  128. * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
  129. * | | | => state = PSA_PAKE_STATE_READY
  130. * | | | => sequence = PSA_PAKE_SEQ_INVALID
  131. * | | | => Output Step = PSA_PAKE_STEP_DERIVE
  132. * |
  133. * psa_pake_get_implicit_key()
  134. * => Input & Output Step = PSA_PAKE_STEP_INVALID
  135. */
  136. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  137. static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
  138. {
  139. switch (ret) {
  140. case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
  141. case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
  142. case MBEDTLS_ERR_ECP_INVALID_KEY:
  143. case MBEDTLS_ERR_ECP_VERIFY_FAILED:
  144. return PSA_ERROR_DATA_INVALID;
  145. case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
  146. case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
  147. return PSA_ERROR_BUFFER_TOO_SMALL;
  148. case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
  149. return PSA_ERROR_NOT_SUPPORTED;
  150. case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
  151. return PSA_ERROR_CORRUPTION_DETECTED;
  152. default:
  153. return PSA_ERROR_GENERIC_ERROR;
  154. }
  155. }
  156. #endif
  157. #if defined(MBEDTLS_PSA_BUILTIN_PAKE)
  158. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  159. static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
  160. {
  161. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  162. mbedtls_ecjpake_role role = (operation->role == PSA_PAKE_ROLE_CLIENT) ?
  163. MBEDTLS_ECJPAKE_CLIENT : MBEDTLS_ECJPAKE_SERVER;
  164. mbedtls_ecjpake_init(&operation->ctx.jpake);
  165. ret = mbedtls_ecjpake_setup(&operation->ctx.jpake,
  166. role,
  167. MBEDTLS_MD_SHA256,
  168. MBEDTLS_ECP_DP_SECP256R1,
  169. operation->password,
  170. operation->password_len);
  171. mbedtls_platform_zeroize(operation->password, operation->password_len);
  172. if (ret != 0) {
  173. return mbedtls_ecjpake_to_psa_error(ret);
  174. }
  175. return PSA_SUCCESS;
  176. }
  177. #endif
  178. psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
  179. const psa_crypto_driver_pake_inputs_t *inputs)
  180. {
  181. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  182. size_t password_len = 0;
  183. psa_pake_role_t role = PSA_PAKE_ROLE_NONE;
  184. psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
  185. size_t actual_password_len = 0;
  186. status = psa_crypto_driver_pake_get_password_len(inputs, &password_len);
  187. if (status != PSA_SUCCESS) {
  188. return status;
  189. }
  190. status = psa_crypto_driver_pake_get_role(inputs, &role);
  191. if (status != PSA_SUCCESS) {
  192. return status;
  193. }
  194. status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite);
  195. if (status != PSA_SUCCESS) {
  196. return status;
  197. }
  198. operation->password = mbedtls_calloc(1, password_len);
  199. if (operation->password == NULL) {
  200. return PSA_ERROR_INSUFFICIENT_MEMORY;
  201. }
  202. status = psa_crypto_driver_pake_get_password(inputs, operation->password,
  203. password_len, &actual_password_len);
  204. if (status != PSA_SUCCESS) {
  205. goto error;
  206. }
  207. operation->password_len = actual_password_len;
  208. operation->alg = cipher_suite.algorithm;
  209. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  210. if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
  211. if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
  212. cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
  213. cipher_suite.bits != 256 ||
  214. cipher_suite.hash != PSA_ALG_SHA_256) {
  215. status = PSA_ERROR_NOT_SUPPORTED;
  216. goto error;
  217. }
  218. operation->role = role;
  219. operation->buffer_length = 0;
  220. operation->buffer_offset = 0;
  221. status = psa_pake_ecjpake_setup(operation);
  222. if (status != PSA_SUCCESS) {
  223. goto error;
  224. }
  225. return PSA_SUCCESS;
  226. } else
  227. #else
  228. (void) operation;
  229. (void) inputs;
  230. #endif
  231. { status = PSA_ERROR_NOT_SUPPORTED; }
  232. error:
  233. /* In case of failure of the setup of a multipart operation, the PSA driver interface
  234. * specifies that the core does not call any other driver entry point thus does not
  235. * call mbedtls_psa_pake_abort(). Therefore call it here to do the needed clean
  236. * up like freeing the memory that may have been allocated to store the password.
  237. */
  238. mbedtls_psa_pake_abort(operation);
  239. return status;
  240. }
  241. static psa_status_t mbedtls_psa_pake_output_internal(
  242. mbedtls_psa_pake_operation_t *operation,
  243. psa_crypto_driver_pake_step_t step,
  244. uint8_t *output,
  245. size_t output_size,
  246. size_t *output_length)
  247. {
  248. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  249. size_t length;
  250. (void) step; // Unused parameter
  251. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  252. /*
  253. * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
  254. * handling of output sequencing.
  255. *
  256. * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
  257. * at once, on the other side the PSA CRYPTO PAKE api requires
  258. * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
  259. * retrieved in sequence.
  260. *
  261. * In order to achieve API compatibility, the whole X1+X2 or X2S steps
  262. * data is stored in an intermediate buffer at first step output call,
  263. * and data is sliced down by parsing the ECPoint records in order
  264. * to return the right parts on each step.
  265. */
  266. if (operation->alg == PSA_ALG_JPAKE) {
  267. /* Initialize & write round on KEY_SHARE sequences */
  268. if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) {
  269. ret = mbedtls_ecjpake_write_round_one(&operation->ctx.jpake,
  270. operation->buffer,
  271. sizeof(operation->buffer),
  272. &operation->buffer_length,
  273. mbedtls_psa_get_random,
  274. MBEDTLS_PSA_RANDOM_STATE);
  275. if (ret != 0) {
  276. return mbedtls_ecjpake_to_psa_error(ret);
  277. }
  278. operation->buffer_offset = 0;
  279. } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) {
  280. ret = mbedtls_ecjpake_write_round_two(&operation->ctx.jpake,
  281. operation->buffer,
  282. sizeof(operation->buffer),
  283. &operation->buffer_length,
  284. mbedtls_psa_get_random,
  285. MBEDTLS_PSA_RANDOM_STATE);
  286. if (ret != 0) {
  287. return mbedtls_ecjpake_to_psa_error(ret);
  288. }
  289. operation->buffer_offset = 0;
  290. }
  291. /*
  292. * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
  293. * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
  294. * that the data for each step is prepended with a length byte, and
  295. * then they're concatenated. Additionally, the server's second round
  296. * output is prepended with a 3-bytes ECParameters structure.
  297. *
  298. * In PSA, we output each step separately, and don't prepend the
  299. * output with a length byte, even less a curve identifier, as that
  300. * information is already available.
  301. */
  302. if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE &&
  303. operation->role == PSA_PAKE_ROLE_SERVER) {
  304. /* Skip ECParameters, with is 3 bytes (RFC 8422) */
  305. operation->buffer_offset += 3;
  306. }
  307. /* Read the length byte then move past it to the data */
  308. length = operation->buffer[operation->buffer_offset];
  309. operation->buffer_offset += 1;
  310. if (operation->buffer_offset + length > operation->buffer_length) {
  311. return PSA_ERROR_DATA_CORRUPT;
  312. }
  313. if (output_size < length) {
  314. return PSA_ERROR_BUFFER_TOO_SMALL;
  315. }
  316. memcpy(output,
  317. operation->buffer + operation->buffer_offset,
  318. length);
  319. *output_length = length;
  320. operation->buffer_offset += length;
  321. /* Reset buffer after ZK_PROOF sequence */
  322. if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) ||
  323. (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) {
  324. mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
  325. operation->buffer_length = 0;
  326. operation->buffer_offset = 0;
  327. }
  328. return PSA_SUCCESS;
  329. } else
  330. #else
  331. (void) step;
  332. (void) output;
  333. (void) output_size;
  334. (void) output_length;
  335. #endif
  336. { return PSA_ERROR_NOT_SUPPORTED; }
  337. }
  338. psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
  339. psa_crypto_driver_pake_step_t step,
  340. uint8_t *output,
  341. size_t output_size,
  342. size_t *output_length)
  343. {
  344. psa_status_t status = mbedtls_psa_pake_output_internal(
  345. operation, step, output, output_size, output_length);
  346. return status;
  347. }
  348. static psa_status_t mbedtls_psa_pake_input_internal(
  349. mbedtls_psa_pake_operation_t *operation,
  350. psa_crypto_driver_pake_step_t step,
  351. const uint8_t *input,
  352. size_t input_length)
  353. {
  354. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  355. (void) step; // Unused parameter
  356. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  357. /*
  358. * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
  359. * handling of input sequencing.
  360. *
  361. * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
  362. * at once as input, on the other side the PSA CRYPTO PAKE api requires
  363. * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
  364. * given in sequence.
  365. *
  366. * In order to achieve API compatibility, each X1+X2 or X4S step data
  367. * is stored sequentially in an intermediate buffer and given to the
  368. * MbedTLS JPAKE API on the last step.
  369. *
  370. * This causes any input error to be only detected on the last step.
  371. */
  372. if (operation->alg == PSA_ALG_JPAKE) {
  373. /*
  374. * Copy input to local buffer and format it as the Mbed TLS API
  375. * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
  376. * The summary is that the data for each step is prepended with a
  377. * length byte, and then they're concatenated. Additionally, the
  378. * server's second round output is prepended with a 3-bytes
  379. * ECParameters structure - which means we have to prepend that when
  380. * we're a client.
  381. */
  382. if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE &&
  383. operation->role == PSA_PAKE_ROLE_CLIENT) {
  384. /* We only support secp256r1. */
  385. /* This is the ECParameters structure defined by RFC 8422. */
  386. unsigned char ecparameters[3] = {
  387. 3, /* named_curve */
  388. 0, 23 /* secp256r1 */
  389. };
  390. if (operation->buffer_length + sizeof(ecparameters) >
  391. sizeof(operation->buffer)) {
  392. return PSA_ERROR_BUFFER_TOO_SMALL;
  393. }
  394. memcpy(operation->buffer + operation->buffer_length,
  395. ecparameters, sizeof(ecparameters));
  396. operation->buffer_length += sizeof(ecparameters);
  397. }
  398. /*
  399. * The core checks that input_length is smaller than
  400. * PSA_PAKE_INPUT_MAX_SIZE.
  401. * Thus no risk of integer overflow here.
  402. */
  403. if (operation->buffer_length + input_length + 1 > sizeof(operation->buffer)) {
  404. return PSA_ERROR_BUFFER_TOO_SMALL;
  405. }
  406. /* Write the length byte */
  407. operation->buffer[operation->buffer_length] = (uint8_t) input_length;
  408. operation->buffer_length += 1;
  409. /* Finally copy the data */
  410. memcpy(operation->buffer + operation->buffer_length,
  411. input, input_length);
  412. operation->buffer_length += input_length;
  413. /* Load buffer at each last round ZK_PROOF */
  414. if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) {
  415. ret = mbedtls_ecjpake_read_round_one(&operation->ctx.jpake,
  416. operation->buffer,
  417. operation->buffer_length);
  418. mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
  419. operation->buffer_length = 0;
  420. if (ret != 0) {
  421. return mbedtls_ecjpake_to_psa_error(ret);
  422. }
  423. } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) {
  424. ret = mbedtls_ecjpake_read_round_two(&operation->ctx.jpake,
  425. operation->buffer,
  426. operation->buffer_length);
  427. mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
  428. operation->buffer_length = 0;
  429. if (ret != 0) {
  430. return mbedtls_ecjpake_to_psa_error(ret);
  431. }
  432. }
  433. return PSA_SUCCESS;
  434. } else
  435. #else
  436. (void) step;
  437. (void) input;
  438. (void) input_length;
  439. #endif
  440. { return PSA_ERROR_NOT_SUPPORTED; }
  441. }
  442. psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
  443. psa_crypto_driver_pake_step_t step,
  444. const uint8_t *input,
  445. size_t input_length)
  446. {
  447. psa_status_t status = mbedtls_psa_pake_input_internal(
  448. operation, step, input, input_length);
  449. return status;
  450. }
  451. psa_status_t mbedtls_psa_pake_get_implicit_key(
  452. mbedtls_psa_pake_operation_t *operation,
  453. uint8_t *output, size_t output_size,
  454. size_t *output_length)
  455. {
  456. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  457. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  458. if (operation->alg == PSA_ALG_JPAKE) {
  459. ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.jpake,
  460. output,
  461. output_size,
  462. output_length,
  463. mbedtls_psa_get_random,
  464. MBEDTLS_PSA_RANDOM_STATE);
  465. if (ret != 0) {
  466. return mbedtls_ecjpake_to_psa_error(ret);
  467. }
  468. return PSA_SUCCESS;
  469. } else
  470. #else
  471. (void) output;
  472. #endif
  473. { return PSA_ERROR_NOT_SUPPORTED; }
  474. }
  475. psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
  476. {
  477. mbedtls_platform_zeroize(operation->password, operation->password_len);
  478. mbedtls_free(operation->password);
  479. operation->password = NULL;
  480. operation->password_len = 0;
  481. #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
  482. if (operation->alg == PSA_ALG_JPAKE) {
  483. operation->role = PSA_PAKE_ROLE_NONE;
  484. mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
  485. operation->buffer_length = 0;
  486. operation->buffer_offset = 0;
  487. mbedtls_ecjpake_free(&operation->ctx.jpake);
  488. }
  489. #endif
  490. operation->alg = PSA_ALG_NONE;
  491. return PSA_SUCCESS;
  492. }
  493. #endif /* MBEDTLS_PSA_BUILTIN_PAKE */
  494. #endif /* MBEDTLS_PSA_CRYPTO_C */