psa_crypto_rsa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_ALG_RSA_PKCS1V15_CRYPT) || \
  39. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
  40. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  41. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
  42. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  43. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  44. /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
  45. * that are not a multiple of 8) well. For example, there is only
  46. * mbedtls_rsa_get_len(), which returns a number of bytes, and no
  47. * way to return the exact bit size of a key.
  48. * To keep things simple, reject non-byte-aligned key sizes. */
  49. static psa_status_t psa_check_rsa_key_byte_aligned(
  50. const mbedtls_rsa_context *rsa )
  51. {
  52. mbedtls_mpi n;
  53. psa_status_t status;
  54. mbedtls_mpi_init( &n );
  55. status = mbedtls_to_psa_error(
  56. mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
  57. if( status == PSA_SUCCESS )
  58. {
  59. if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
  60. status = PSA_ERROR_NOT_SUPPORTED;
  61. }
  62. mbedtls_mpi_free( &n );
  63. return( status );
  64. }
  65. psa_status_t mbedtls_psa_rsa_load_representation(
  66. psa_key_type_t type, const uint8_t *data, size_t data_length,
  67. mbedtls_rsa_context **p_rsa )
  68. {
  69. psa_status_t status;
  70. mbedtls_pk_context ctx;
  71. size_t bits;
  72. mbedtls_pk_init( &ctx );
  73. /* Parse the data. */
  74. if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
  75. status = mbedtls_to_psa_error(
  76. mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
  77. else
  78. status = mbedtls_to_psa_error(
  79. mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
  80. if( status != PSA_SUCCESS )
  81. goto exit;
  82. /* We have something that the pkparse module recognizes. If it is a
  83. * valid RSA key, store it. */
  84. if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
  85. {
  86. status = PSA_ERROR_INVALID_ARGUMENT;
  87. goto exit;
  88. }
  89. /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
  90. * supports non-byte-aligned key sizes, but not well. For example,
  91. * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
  92. bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
  93. if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
  94. {
  95. status = PSA_ERROR_NOT_SUPPORTED;
  96. goto exit;
  97. }
  98. status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
  99. if( status != PSA_SUCCESS )
  100. goto exit;
  101. /* Copy out the pointer to the RSA context, and reset the PK context
  102. * such that pk_free doesn't free the RSA context we just grabbed. */
  103. *p_rsa = mbedtls_pk_rsa( ctx );
  104. ctx.pk_info = NULL;
  105. exit:
  106. mbedtls_pk_free( &ctx );
  107. return( status );
  108. }
  109. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
  110. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
  111. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  112. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
  113. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  114. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  115. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
  116. defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
  117. psa_status_t mbedtls_psa_rsa_import_key(
  118. const psa_key_attributes_t *attributes,
  119. const uint8_t *data, size_t data_length,
  120. uint8_t *key_buffer, size_t key_buffer_size,
  121. size_t *key_buffer_length, size_t *bits )
  122. {
  123. psa_status_t status;
  124. mbedtls_rsa_context *rsa = NULL;
  125. /* Parse input */
  126. status = mbedtls_psa_rsa_load_representation( attributes->core.type,
  127. data,
  128. data_length,
  129. &rsa );
  130. if( status != PSA_SUCCESS )
  131. goto exit;
  132. *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
  133. /* Re-export the data to PSA export format, such that we can store export
  134. * representation in the key slot. Export representation in case of RSA is
  135. * the smallest representation that's allowed as input, so a straight-up
  136. * allocation of the same size as the input buffer will be large enough. */
  137. status = mbedtls_psa_rsa_export_key( attributes->core.type,
  138. rsa,
  139. key_buffer,
  140. key_buffer_size,
  141. key_buffer_length );
  142. exit:
  143. /* Always free the RSA object */
  144. mbedtls_rsa_free( rsa );
  145. mbedtls_free( rsa );
  146. return( status );
  147. }
  148. psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
  149. mbedtls_rsa_context *rsa,
  150. uint8_t *data,
  151. size_t data_size,
  152. size_t *data_length )
  153. {
  154. #if defined(MBEDTLS_PK_WRITE_C)
  155. int ret;
  156. mbedtls_pk_context pk;
  157. uint8_t *pos = data + data_size;
  158. mbedtls_pk_init( &pk );
  159. pk.pk_info = &mbedtls_rsa_info;
  160. pk.pk_ctx = rsa;
  161. /* PSA Crypto API defines the format of an RSA key as a DER-encoded
  162. * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
  163. * private key and of the RFC3279 RSAPublicKey for a public key. */
  164. if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
  165. ret = mbedtls_pk_write_key_der( &pk, data, data_size );
  166. else
  167. ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
  168. if( ret < 0 )
  169. {
  170. /* Clean up in case pk_write failed halfway through. */
  171. memset( data, 0, data_size );
  172. return( mbedtls_to_psa_error( ret ) );
  173. }
  174. /* The mbedtls_pk_xxx functions write to the end of the buffer.
  175. * Move the data to the beginning and erase remaining data
  176. * at the original location. */
  177. if( 2 * (size_t) ret <= data_size )
  178. {
  179. memcpy( data, data + data_size - ret, ret );
  180. memset( data + data_size - ret, 0, ret );
  181. }
  182. else if( (size_t) ret < data_size )
  183. {
  184. memmove( data, data + data_size - ret, ret );
  185. memset( data + ret, 0, data_size - ret );
  186. }
  187. *data_length = ret;
  188. return( PSA_SUCCESS );
  189. #else
  190. (void) type;
  191. (void) rsa;
  192. (void) data;
  193. (void) data_size;
  194. (void) data_length;
  195. return( PSA_ERROR_NOT_SUPPORTED );
  196. #endif /* MBEDTLS_PK_WRITE_C */
  197. }
  198. psa_status_t mbedtls_psa_rsa_export_public_key(
  199. const psa_key_attributes_t *attributes,
  200. const uint8_t *key_buffer, size_t key_buffer_size,
  201. uint8_t *data, size_t data_size, size_t *data_length )
  202. {
  203. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  204. mbedtls_rsa_context *rsa = NULL;
  205. status = mbedtls_psa_rsa_load_representation(
  206. attributes->core.type, key_buffer, key_buffer_size, &rsa );
  207. if( status != PSA_SUCCESS )
  208. return( status );
  209. status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
  210. rsa,
  211. data,
  212. data_size,
  213. data_length );
  214. mbedtls_rsa_free( rsa );
  215. mbedtls_free( rsa );
  216. return( status );
  217. }
  218. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
  219. * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
  220. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
  221. defined(MBEDTLS_GENPRIME)
  222. static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
  223. size_t domain_parameters_size,
  224. int *exponent )
  225. {
  226. size_t i;
  227. uint32_t acc = 0;
  228. if( domain_parameters_size == 0 )
  229. {
  230. *exponent = 65537;
  231. return( PSA_SUCCESS );
  232. }
  233. /* Mbed TLS encodes the public exponent as an int. For simplicity, only
  234. * support values that fit in a 32-bit integer, which is larger than
  235. * int on just about every platform anyway. */
  236. if( domain_parameters_size > sizeof( acc ) )
  237. return( PSA_ERROR_NOT_SUPPORTED );
  238. for( i = 0; i < domain_parameters_size; i++ )
  239. acc = ( acc << 8 ) | domain_parameters[i];
  240. if( acc > INT_MAX )
  241. return( PSA_ERROR_NOT_SUPPORTED );
  242. *exponent = acc;
  243. return( PSA_SUCCESS );
  244. }
  245. psa_status_t mbedtls_psa_rsa_generate_key(
  246. const psa_key_attributes_t *attributes,
  247. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
  248. {
  249. psa_status_t status;
  250. mbedtls_rsa_context rsa;
  251. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  252. int exponent;
  253. status = psa_rsa_read_exponent( attributes->domain_parameters,
  254. attributes->domain_parameters_size,
  255. &exponent );
  256. if( status != PSA_SUCCESS )
  257. return( status );
  258. mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
  259. ret = mbedtls_rsa_gen_key( &rsa,
  260. mbedtls_psa_get_random,
  261. MBEDTLS_PSA_RANDOM_STATE,
  262. (unsigned int)attributes->core.bits,
  263. exponent );
  264. if( ret != 0 )
  265. return( mbedtls_to_psa_error( ret ) );
  266. status = mbedtls_psa_rsa_export_key( attributes->core.type,
  267. &rsa, key_buffer, key_buffer_size,
  268. key_buffer_length );
  269. mbedtls_rsa_free( &rsa );
  270. return( status );
  271. }
  272. #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
  273. * defined(MBEDTLS_GENPRIME) */
  274. /****************************************************************/
  275. /* Sign/verify hashes */
  276. /****************************************************************/
  277. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
  278. defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  279. /* Decode the hash algorithm from alg and store the mbedtls encoding in
  280. * md_alg. Verify that the hash length is acceptable. */
  281. static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
  282. size_t hash_length,
  283. mbedtls_md_type_t *md_alg )
  284. {
  285. psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
  286. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
  287. *md_alg = mbedtls_md_get_type( md_info );
  288. /* The Mbed TLS RSA module uses an unsigned int for hash length
  289. * parameters. Validate that it fits so that we don't risk an
  290. * overflow later. */
  291. #if SIZE_MAX > UINT_MAX
  292. if( hash_length > UINT_MAX )
  293. return( PSA_ERROR_INVALID_ARGUMENT );
  294. #endif
  295. /* For signatures using a hash, the hash length must be correct. */
  296. if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
  297. {
  298. if( md_info == NULL )
  299. return( PSA_ERROR_NOT_SUPPORTED );
  300. if( mbedtls_md_get_size( md_info ) != hash_length )
  301. return( PSA_ERROR_INVALID_ARGUMENT );
  302. }
  303. return( PSA_SUCCESS );
  304. }
  305. psa_status_t mbedtls_psa_rsa_sign_hash(
  306. const psa_key_attributes_t *attributes,
  307. const uint8_t *key_buffer, size_t key_buffer_size,
  308. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  309. uint8_t *signature, size_t signature_size, size_t *signature_length )
  310. {
  311. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  312. mbedtls_rsa_context *rsa = NULL;
  313. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  314. mbedtls_md_type_t md_alg;
  315. status = mbedtls_psa_rsa_load_representation( attributes->core.type,
  316. key_buffer,
  317. key_buffer_size,
  318. &rsa );
  319. if( status != PSA_SUCCESS )
  320. return( status );
  321. status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
  322. if( status != PSA_SUCCESS )
  323. goto exit;
  324. if( signature_size < mbedtls_rsa_get_len( rsa ) )
  325. {
  326. status = PSA_ERROR_BUFFER_TOO_SMALL;
  327. goto exit;
  328. }
  329. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
  330. if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
  331. {
  332. mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
  333. MBEDTLS_MD_NONE );
  334. ret = mbedtls_rsa_pkcs1_sign( rsa,
  335. mbedtls_psa_get_random,
  336. MBEDTLS_PSA_RANDOM_STATE,
  337. MBEDTLS_RSA_PRIVATE,
  338. md_alg,
  339. (unsigned int) hash_length,
  340. hash,
  341. signature );
  342. }
  343. else
  344. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
  345. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  346. if( PSA_ALG_IS_RSA_PSS( alg ) )
  347. {
  348. mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
  349. ret = mbedtls_rsa_rsassa_pss_sign( rsa,
  350. mbedtls_psa_get_random,
  351. MBEDTLS_PSA_RANDOM_STATE,
  352. MBEDTLS_RSA_PRIVATE,
  353. MBEDTLS_MD_NONE,
  354. (unsigned int) hash_length,
  355. hash,
  356. signature );
  357. }
  358. else
  359. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
  360. {
  361. status = PSA_ERROR_INVALID_ARGUMENT;
  362. goto exit;
  363. }
  364. if( ret == 0 )
  365. *signature_length = mbedtls_rsa_get_len( rsa );
  366. status = mbedtls_to_psa_error( ret );
  367. exit:
  368. mbedtls_rsa_free( rsa );
  369. mbedtls_free( rsa );
  370. return( status );
  371. }
  372. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  373. static int rsa_pss_expected_salt_len( psa_algorithm_t alg,
  374. const mbedtls_rsa_context *rsa,
  375. size_t hash_length )
  376. {
  377. if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) )
  378. return( MBEDTLS_RSA_SALT_LEN_ANY );
  379. /* Otherwise: standard salt length, i.e. largest possible salt length
  380. * up to the hash length. */
  381. int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit
  382. int hlen = (int) hash_length; // known to fit
  383. int room = klen - 2 - hlen;
  384. if( room < 0 )
  385. return( 0 ); // there is no valid signature in this case anyway
  386. else if( room > hlen )
  387. return( hlen );
  388. else
  389. return( room );
  390. }
  391. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
  392. psa_status_t mbedtls_psa_rsa_verify_hash(
  393. const psa_key_attributes_t *attributes,
  394. const uint8_t *key_buffer, size_t key_buffer_size,
  395. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  396. const uint8_t *signature, size_t signature_length )
  397. {
  398. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  399. mbedtls_rsa_context *rsa = NULL;
  400. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  401. mbedtls_md_type_t md_alg;
  402. status = mbedtls_psa_rsa_load_representation( attributes->core.type,
  403. key_buffer,
  404. key_buffer_size,
  405. &rsa );
  406. if( status != PSA_SUCCESS )
  407. goto exit;
  408. status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
  409. if( status != PSA_SUCCESS )
  410. goto exit;
  411. if( signature_length != mbedtls_rsa_get_len( rsa ) )
  412. {
  413. status = PSA_ERROR_INVALID_SIGNATURE;
  414. goto exit;
  415. }
  416. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
  417. if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
  418. {
  419. mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
  420. MBEDTLS_MD_NONE );
  421. ret = mbedtls_rsa_pkcs1_verify( rsa,
  422. mbedtls_psa_get_random,
  423. MBEDTLS_PSA_RANDOM_STATE,
  424. MBEDTLS_RSA_PUBLIC,
  425. md_alg,
  426. (unsigned int) hash_length,
  427. hash,
  428. signature );
  429. }
  430. else
  431. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
  432. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
  433. if( PSA_ALG_IS_RSA_PSS( alg ) )
  434. {
  435. int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length );
  436. mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
  437. ret = mbedtls_rsa_rsassa_pss_verify_ext( 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. md_alg,
  445. slen,
  446. signature );
  447. }
  448. else
  449. #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
  450. {
  451. status = PSA_ERROR_INVALID_ARGUMENT;
  452. goto exit;
  453. }
  454. /* Mbed TLS distinguishes "invalid padding" from "valid padding but
  455. * the rest of the signature is invalid". This has little use in
  456. * practice and PSA doesn't report this distinction. */
  457. status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
  458. PSA_ERROR_INVALID_SIGNATURE :
  459. mbedtls_to_psa_error( ret );
  460. exit:
  461. mbedtls_rsa_free( rsa );
  462. mbedtls_free( rsa );
  463. return( status );
  464. }
  465. #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
  466. * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
  467. #endif /* MBEDTLS_PSA_CRYPTO_C */