psa_crypto_rsa.c 24 KB

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