psa_crypto_rsa.c 25 KB

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