psa_crypto_rsa.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * PSA RSA layer on top of Mbed TLS crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. #include "common.h"
  9. #if defined(MBEDTLS_PSA_CRYPTO_C)
  10. #include <psa/crypto.h>
  11. #include "psa/crypto_values.h"
  12. #include "psa_crypto_core.h"
  13. #include "psa_crypto_random_impl.h"
  14. #include "psa_crypto_rsa.h"
  15. #include "psa_crypto_hash.h"
  16. #include "mbedtls/psa_util.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "mbedtls/platform.h"
  20. #include <mbedtls/rsa.h>
  21. #include <mbedtls/error.h>
  22. #include "rsa_internal.h"
  23. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  24. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
  25. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  26. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
  27. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
  28. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
  29. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  30. /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
  31. * that are not a multiple of 8) well. For example, there is only
  32. * mbedtls_rsa_get_len(), which returns a number of bytes, and no
  33. * way to return the exact bit size of a key.
  34. * To keep things simple, reject non-byte-aligned key sizes. */
  35. static psa_status_t psa_check_rsa_key_byte_aligned(
  36. const mbedtls_rsa_context *rsa)
  37. {
  38. mbedtls_mpi n;
  39. psa_status_t status;
  40. mbedtls_mpi_init(&n);
  41. status = mbedtls_to_psa_error(
  42. mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
  43. if (status == PSA_SUCCESS) {
  44. if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
  45. status = PSA_ERROR_NOT_SUPPORTED;
  46. }
  47. }
  48. mbedtls_mpi_free(&n);
  49. return status;
  50. }
  51. psa_status_t mbedtls_psa_rsa_load_representation(
  52. psa_key_type_t type, const uint8_t *data, size_t data_length,
  53. mbedtls_rsa_context **p_rsa)
  54. {
  55. psa_status_t status;
  56. size_t bits;
  57. *p_rsa = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
  58. if (*p_rsa == NULL) {
  59. return PSA_ERROR_INSUFFICIENT_MEMORY;
  60. }
  61. mbedtls_rsa_init(*p_rsa);
  62. /* Parse the data. */
  63. if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
  64. status = mbedtls_to_psa_error(mbedtls_rsa_parse_key(*p_rsa, data, data_length));
  65. } else {
  66. status = mbedtls_to_psa_error(mbedtls_rsa_parse_pubkey(*p_rsa, data, data_length));
  67. }
  68. if (status != PSA_SUCCESS) {
  69. goto exit;
  70. }
  71. /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
  72. * supports non-byte-aligned key sizes, but not well. For example,
  73. * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
  74. bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(*p_rsa));
  75. if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
  76. status = PSA_ERROR_NOT_SUPPORTED;
  77. goto exit;
  78. }
  79. status = psa_check_rsa_key_byte_aligned(*p_rsa);
  80. if (status != PSA_SUCCESS) {
  81. goto exit;
  82. }
  83. exit:
  84. return status;
  85. }
  86. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  87. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
  88. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  89. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
  90. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
  91. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
  92. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  93. #if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
  94. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
  95. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  96. psa_status_t mbedtls_psa_rsa_import_key(
  97. const psa_key_attributes_t *attributes,
  98. const uint8_t *data, size_t data_length,
  99. uint8_t *key_buffer, size_t key_buffer_size,
  100. size_t *key_buffer_length, size_t *bits)
  101. {
  102. psa_status_t status;
  103. mbedtls_rsa_context *rsa = NULL;
  104. /* Parse input */
  105. status = mbedtls_psa_rsa_load_representation(attributes->type,
  106. data,
  107. data_length,
  108. &rsa);
  109. if (status != PSA_SUCCESS) {
  110. goto exit;
  111. }
  112. *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
  113. /* Re-export the data to PSA export format, such that we can store export
  114. * representation in the key slot. Export representation in case of RSA is
  115. * the smallest representation that's allowed as input, so a straight-up
  116. * allocation of the same size as the input buffer will be large enough. */
  117. status = mbedtls_psa_rsa_export_key(attributes->type,
  118. rsa,
  119. key_buffer,
  120. key_buffer_size,
  121. key_buffer_length);
  122. exit:
  123. /* Always free the RSA object */
  124. mbedtls_rsa_free(rsa);
  125. mbedtls_free(rsa);
  126. return status;
  127. }
  128. #endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
  129. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
  130. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  131. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
  132. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  133. psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
  134. mbedtls_rsa_context *rsa,
  135. uint8_t *data,
  136. size_t data_size,
  137. size_t *data_length)
  138. {
  139. int ret;
  140. uint8_t *end = data + data_size;
  141. /* PSA Crypto API defines the format of an RSA key as a DER-encoded
  142. * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
  143. * private key and of the RFC3279 RSAPublicKey for a public key. */
  144. if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
  145. ret = mbedtls_rsa_write_key(rsa, data, &end);
  146. } else {
  147. ret = mbedtls_rsa_write_pubkey(rsa, data, &end);
  148. }
  149. if (ret < 0) {
  150. /* Clean up in case pk_write failed halfway through. */
  151. memset(data, 0, data_size);
  152. return mbedtls_to_psa_error(ret);
  153. }
  154. /* The mbedtls_pk_xxx functions write to the end of the buffer.
  155. * Move the data to the beginning and erase remaining data
  156. * at the original location. */
  157. if (2 * (size_t) ret <= data_size) {
  158. memcpy(data, data + data_size - ret, ret);
  159. memset(data + data_size - ret, 0, ret);
  160. } else if ((size_t) ret < data_size) {
  161. memmove(data, data + data_size - ret, ret);
  162. memset(data + ret, 0, data_size - ret);
  163. }
  164. *data_length = ret;
  165. return PSA_SUCCESS;
  166. }
  167. psa_status_t mbedtls_psa_rsa_export_public_key(
  168. const psa_key_attributes_t *attributes,
  169. const uint8_t *key_buffer, size_t key_buffer_size,
  170. uint8_t *data, size_t data_size, size_t *data_length)
  171. {
  172. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  173. mbedtls_rsa_context *rsa = NULL;
  174. status = mbedtls_psa_rsa_load_representation(
  175. attributes->type, key_buffer, key_buffer_size, &rsa);
  176. if (status == PSA_SUCCESS) {
  177. status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
  178. rsa,
  179. data,
  180. data_size,
  181. data_length);
  182. }
  183. mbedtls_rsa_free(rsa);
  184. mbedtls_free(rsa);
  185. return status;
  186. }
  187. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
  188. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  189. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
  190. static psa_status_t psa_rsa_read_exponent(const uint8_t *e_bytes,
  191. size_t e_length,
  192. int *exponent)
  193. {
  194. size_t i;
  195. uint32_t acc = 0;
  196. /* Mbed TLS encodes the public exponent as an int. For simplicity, only
  197. * support values that fit in a 32-bit integer, which is larger than
  198. * int on just about every platform anyway. */
  199. if (e_length > sizeof(acc)) {
  200. return PSA_ERROR_NOT_SUPPORTED;
  201. }
  202. for (i = 0; i < e_length; i++) {
  203. acc = (acc << 8) | e_bytes[i];
  204. }
  205. if (acc > INT_MAX) {
  206. return PSA_ERROR_NOT_SUPPORTED;
  207. }
  208. *exponent = acc;
  209. return PSA_SUCCESS;
  210. }
  211. psa_status_t mbedtls_psa_rsa_generate_key(
  212. const psa_key_attributes_t *attributes,
  213. const uint8_t *custom_data, size_t custom_data_length,
  214. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
  215. {
  216. psa_status_t status;
  217. mbedtls_rsa_context rsa;
  218. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  219. int exponent = 65537;
  220. if (custom_data_length != 0) {
  221. status = psa_rsa_read_exponent(custom_data, custom_data_length,
  222. &exponent);
  223. if (status != PSA_SUCCESS) {
  224. return status;
  225. }
  226. }
  227. mbedtls_rsa_init(&rsa);
  228. ret = mbedtls_rsa_gen_key(&rsa,
  229. mbedtls_psa_get_random,
  230. MBEDTLS_PSA_RANDOM_STATE,
  231. (unsigned int) attributes->bits,
  232. exponent);
  233. if (ret != 0) {
  234. mbedtls_rsa_free(&rsa);
  235. return mbedtls_to_psa_error(ret);
  236. }
  237. status = mbedtls_psa_rsa_export_key(attributes->type,
  238. &rsa, key_buffer, key_buffer_size,
  239. key_buffer_length);
  240. mbedtls_rsa_free(&rsa);
  241. return status;
  242. }
  243. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
  244. /****************************************************************/
  245. /* Sign/verify hashes */
  246. /****************************************************************/
  247. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  248. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  249. /* Decode the hash algorithm from alg and store the mbedtls encoding in
  250. * md_alg. Verify that the hash length is acceptable. */
  251. static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
  252. size_t hash_length,
  253. mbedtls_md_type_t *md_alg)
  254. {
  255. psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
  256. *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
  257. /* The Mbed TLS RSA module uses an unsigned int for hash length
  258. * parameters. Validate that it fits so that we don't risk an
  259. * overflow later. */
  260. #if SIZE_MAX > UINT_MAX
  261. if (hash_length > UINT_MAX) {
  262. return PSA_ERROR_INVALID_ARGUMENT;
  263. }
  264. #endif
  265. /* For signatures using a hash, the hash length must be correct. */
  266. if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
  267. if (*md_alg == MBEDTLS_MD_NONE) {
  268. return PSA_ERROR_NOT_SUPPORTED;
  269. }
  270. if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
  271. return PSA_ERROR_INVALID_ARGUMENT;
  272. }
  273. }
  274. return PSA_SUCCESS;
  275. }
  276. psa_status_t mbedtls_psa_rsa_sign_hash(
  277. const psa_key_attributes_t *attributes,
  278. const uint8_t *key_buffer, size_t key_buffer_size,
  279. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  280. uint8_t *signature, size_t signature_size, size_t *signature_length)
  281. {
  282. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  283. mbedtls_rsa_context *rsa = NULL;
  284. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  285. mbedtls_md_type_t md_alg;
  286. status = mbedtls_psa_rsa_load_representation(attributes->type,
  287. key_buffer,
  288. key_buffer_size,
  289. &rsa);
  290. if (status != PSA_SUCCESS) {
  291. goto exit;
  292. }
  293. status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
  294. if (status != PSA_SUCCESS) {
  295. goto exit;
  296. }
  297. if (signature_size < mbedtls_rsa_get_len(rsa)) {
  298. status = PSA_ERROR_BUFFER_TOO_SMALL;
  299. goto exit;
  300. }
  301. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
  302. if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
  303. ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
  304. MBEDTLS_MD_NONE);
  305. if (ret == 0) {
  306. ret = mbedtls_rsa_pkcs1_sign(rsa,
  307. mbedtls_psa_get_random,
  308. MBEDTLS_PSA_RANDOM_STATE,
  309. md_alg,
  310. (unsigned int) hash_length,
  311. hash,
  312. signature);
  313. }
  314. } else
  315. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
  316. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  317. if (PSA_ALG_IS_RSA_PSS(alg)) {
  318. ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
  319. if (ret == 0) {
  320. ret = mbedtls_rsa_rsassa_pss_sign(rsa,
  321. mbedtls_psa_get_random,
  322. MBEDTLS_PSA_RANDOM_STATE,
  323. MBEDTLS_MD_NONE,
  324. (unsigned int) hash_length,
  325. hash,
  326. signature);
  327. }
  328. } else
  329. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
  330. {
  331. status = PSA_ERROR_INVALID_ARGUMENT;
  332. goto exit;
  333. }
  334. if (ret == 0) {
  335. *signature_length = mbedtls_rsa_get_len(rsa);
  336. }
  337. status = mbedtls_to_psa_error(ret);
  338. exit:
  339. mbedtls_rsa_free(rsa);
  340. mbedtls_free(rsa);
  341. return status;
  342. }
  343. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  344. static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
  345. const mbedtls_rsa_context *rsa,
  346. size_t hash_length)
  347. {
  348. if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
  349. return MBEDTLS_RSA_SALT_LEN_ANY;
  350. }
  351. /* Otherwise: standard salt length, i.e. largest possible salt length
  352. * up to the hash length. */
  353. int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
  354. int hlen = (int) hash_length; // known to fit
  355. int room = klen - 2 - hlen;
  356. if (room < 0) {
  357. return 0; // there is no valid signature in this case anyway
  358. } else if (room > hlen) {
  359. return hlen;
  360. } else {
  361. return room;
  362. }
  363. }
  364. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
  365. psa_status_t mbedtls_psa_rsa_verify_hash(
  366. const psa_key_attributes_t *attributes,
  367. const uint8_t *key_buffer, size_t key_buffer_size,
  368. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  369. const uint8_t *signature, size_t signature_length)
  370. {
  371. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  372. mbedtls_rsa_context *rsa = NULL;
  373. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  374. mbedtls_md_type_t md_alg;
  375. status = mbedtls_psa_rsa_load_representation(attributes->type,
  376. key_buffer,
  377. key_buffer_size,
  378. &rsa);
  379. if (status != PSA_SUCCESS) {
  380. goto exit;
  381. }
  382. status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
  383. if (status != PSA_SUCCESS) {
  384. goto exit;
  385. }
  386. if (signature_length != mbedtls_rsa_get_len(rsa)) {
  387. status = PSA_ERROR_INVALID_SIGNATURE;
  388. goto exit;
  389. }
  390. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
  391. if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
  392. ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
  393. MBEDTLS_MD_NONE);
  394. if (ret == 0) {
  395. ret = mbedtls_rsa_pkcs1_verify(rsa,
  396. md_alg,
  397. (unsigned int) hash_length,
  398. hash,
  399. signature);
  400. }
  401. } else
  402. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
  403. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  404. if (PSA_ALG_IS_RSA_PSS(alg)) {
  405. ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
  406. if (ret == 0) {
  407. int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
  408. ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
  409. md_alg,
  410. (unsigned) hash_length,
  411. hash,
  412. md_alg,
  413. slen,
  414. signature);
  415. }
  416. } else
  417. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
  418. {
  419. status = PSA_ERROR_INVALID_ARGUMENT;
  420. goto exit;
  421. }
  422. /* Mbed TLS distinguishes "invalid padding" from "valid padding but
  423. * the rest of the signature is invalid". This has little use in
  424. * practice and PSA doesn't report this distinction. */
  425. status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
  426. PSA_ERROR_INVALID_SIGNATURE :
  427. mbedtls_to_psa_error(ret);
  428. exit:
  429. mbedtls_rsa_free(rsa);
  430. mbedtls_free(rsa);
  431. return status;
  432. }
  433. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  434. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
  435. /****************************************************************/
  436. /* Asymmetric cryptography */
  437. /****************************************************************/
  438. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  439. static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
  440. mbedtls_rsa_context *rsa)
  441. {
  442. psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
  443. mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
  444. /* Just to get the error status right, as rsa_set_padding() doesn't
  445. * distinguish between "bad RSA algorithm" and "unknown hash". */
  446. if (mbedtls_md_info_from_type(md_alg) == NULL) {
  447. return PSA_ERROR_NOT_SUPPORTED;
  448. }
  449. return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
  450. }
  451. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  452. psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
  453. const uint8_t *key_buffer,
  454. size_t key_buffer_size,
  455. psa_algorithm_t alg,
  456. const uint8_t *input,
  457. size_t input_length,
  458. const uint8_t *salt,
  459. size_t salt_length,
  460. uint8_t *output,
  461. size_t output_size,
  462. size_t *output_length)
  463. {
  464. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  465. (void) key_buffer;
  466. (void) key_buffer_size;
  467. (void) input;
  468. (void) input_length;
  469. (void) salt;
  470. (void) salt_length;
  471. (void) output;
  472. (void) output_size;
  473. (void) output_length;
  474. if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
  475. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  476. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  477. mbedtls_rsa_context *rsa = NULL;
  478. status = mbedtls_psa_rsa_load_representation(attributes->type,
  479. key_buffer,
  480. key_buffer_size,
  481. &rsa);
  482. if (status != PSA_SUCCESS) {
  483. goto rsa_exit;
  484. }
  485. if (output_size < mbedtls_rsa_get_len(rsa)) {
  486. status = PSA_ERROR_BUFFER_TOO_SMALL;
  487. goto rsa_exit;
  488. }
  489. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  490. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  491. if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
  492. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
  493. status = mbedtls_to_psa_error(
  494. mbedtls_rsa_pkcs1_encrypt(rsa,
  495. mbedtls_psa_get_random,
  496. MBEDTLS_PSA_RANDOM_STATE,
  497. input_length,
  498. input,
  499. output));
  500. #else
  501. status = PSA_ERROR_NOT_SUPPORTED;
  502. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
  503. } else
  504. if (PSA_ALG_IS_RSA_OAEP(alg)) {
  505. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  506. status = mbedtls_to_psa_error(
  507. psa_rsa_oaep_set_padding_mode(alg, rsa));
  508. if (status != PSA_SUCCESS) {
  509. goto rsa_exit;
  510. }
  511. status = mbedtls_to_psa_error(
  512. mbedtls_rsa_rsaes_oaep_encrypt(rsa,
  513. mbedtls_psa_get_random,
  514. MBEDTLS_PSA_RANDOM_STATE,
  515. salt, salt_length,
  516. input_length,
  517. input,
  518. output));
  519. #else
  520. status = PSA_ERROR_NOT_SUPPORTED;
  521. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
  522. } else {
  523. status = PSA_ERROR_INVALID_ARGUMENT;
  524. }
  525. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  526. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  527. rsa_exit:
  528. if (status == PSA_SUCCESS) {
  529. *output_length = mbedtls_rsa_get_len(rsa);
  530. }
  531. mbedtls_rsa_free(rsa);
  532. mbedtls_free(rsa);
  533. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  534. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  535. } else {
  536. status = PSA_ERROR_NOT_SUPPORTED;
  537. }
  538. return status;
  539. }
  540. psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
  541. const uint8_t *key_buffer,
  542. size_t key_buffer_size,
  543. psa_algorithm_t alg,
  544. const uint8_t *input,
  545. size_t input_length,
  546. const uint8_t *salt,
  547. size_t salt_length,
  548. uint8_t *output,
  549. size_t output_size,
  550. size_t *output_length)
  551. {
  552. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  553. (void) key_buffer;
  554. (void) key_buffer_size;
  555. (void) input;
  556. (void) input_length;
  557. (void) salt;
  558. (void) salt_length;
  559. (void) output;
  560. (void) output_size;
  561. (void) output_length;
  562. *output_length = 0;
  563. if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
  564. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  565. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  566. mbedtls_rsa_context *rsa = NULL;
  567. status = mbedtls_psa_rsa_load_representation(attributes->type,
  568. key_buffer,
  569. key_buffer_size,
  570. &rsa);
  571. if (status != PSA_SUCCESS) {
  572. goto rsa_exit;
  573. }
  574. if (input_length != mbedtls_rsa_get_len(rsa)) {
  575. status = PSA_ERROR_INVALID_ARGUMENT;
  576. goto rsa_exit;
  577. }
  578. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  579. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  580. if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
  581. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
  582. status = mbedtls_to_psa_error(
  583. mbedtls_rsa_pkcs1_decrypt(rsa,
  584. mbedtls_psa_get_random,
  585. MBEDTLS_PSA_RANDOM_STATE,
  586. output_length,
  587. input,
  588. output,
  589. output_size));
  590. #else
  591. status = PSA_ERROR_NOT_SUPPORTED;
  592. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
  593. } else
  594. if (PSA_ALG_IS_RSA_OAEP(alg)) {
  595. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  596. status = mbedtls_to_psa_error(
  597. psa_rsa_oaep_set_padding_mode(alg, rsa));
  598. if (status != PSA_SUCCESS) {
  599. goto rsa_exit;
  600. }
  601. status = mbedtls_to_psa_error(
  602. mbedtls_rsa_rsaes_oaep_decrypt(rsa,
  603. mbedtls_psa_get_random,
  604. MBEDTLS_PSA_RANDOM_STATE,
  605. salt, salt_length,
  606. output_length,
  607. input,
  608. output,
  609. output_size));
  610. #else
  611. status = PSA_ERROR_NOT_SUPPORTED;
  612. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
  613. } else {
  614. status = PSA_ERROR_INVALID_ARGUMENT;
  615. }
  616. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
  617. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
  618. rsa_exit:
  619. mbedtls_rsa_free(rsa);
  620. mbedtls_free(rsa);
  621. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  622. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
  623. } else {
  624. status = PSA_ERROR_NOT_SUPPORTED;
  625. }
  626. return status;
  627. }
  628. #endif /* MBEDTLS_PSA_CRYPTO_C */