psa_crypto_cipher.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * PSA cipher driver entry points
  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_cipher.h"
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_random_impl.h"
  25. #include "mbedtls/cipher.h"
  26. #include "mbedtls/error.h"
  27. #include <string.h>
  28. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
  29. psa_algorithm_t alg,
  30. psa_key_type_t key_type,
  31. size_t key_bits,
  32. mbedtls_cipher_id_t* cipher_id )
  33. {
  34. mbedtls_cipher_mode_t mode;
  35. mbedtls_cipher_id_t cipher_id_tmp;
  36. if( PSA_ALG_IS_AEAD( alg ) )
  37. alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
  38. if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
  39. {
  40. switch( alg )
  41. {
  42. case PSA_ALG_STREAM_CIPHER:
  43. mode = MBEDTLS_MODE_STREAM;
  44. break;
  45. case PSA_ALG_CTR:
  46. mode = MBEDTLS_MODE_CTR;
  47. break;
  48. case PSA_ALG_CFB:
  49. mode = MBEDTLS_MODE_CFB;
  50. break;
  51. case PSA_ALG_OFB:
  52. mode = MBEDTLS_MODE_OFB;
  53. break;
  54. case PSA_ALG_ECB_NO_PADDING:
  55. mode = MBEDTLS_MODE_ECB;
  56. break;
  57. case PSA_ALG_CBC_NO_PADDING:
  58. mode = MBEDTLS_MODE_CBC;
  59. break;
  60. case PSA_ALG_CBC_PKCS7:
  61. mode = MBEDTLS_MODE_CBC;
  62. break;
  63. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
  64. mode = MBEDTLS_MODE_CCM;
  65. break;
  66. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
  67. mode = MBEDTLS_MODE_GCM;
  68. break;
  69. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
  70. mode = MBEDTLS_MODE_CHACHAPOLY;
  71. break;
  72. default:
  73. return( NULL );
  74. }
  75. }
  76. else if( alg == PSA_ALG_CMAC )
  77. mode = MBEDTLS_MODE_ECB;
  78. else
  79. return( NULL );
  80. switch( key_type )
  81. {
  82. case PSA_KEY_TYPE_AES:
  83. cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
  84. break;
  85. case PSA_KEY_TYPE_ARIA:
  86. cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA;
  87. break;
  88. case PSA_KEY_TYPE_DES:
  89. /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
  90. * and 192 for three-key Triple-DES. */
  91. if( key_bits == 64 )
  92. cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
  93. else
  94. cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
  95. /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
  96. * but two-key Triple-DES is functionally three-key Triple-DES
  97. * with K1=K3, so that's how we present it to mbedtls. */
  98. if( key_bits == 128 )
  99. key_bits = 192;
  100. break;
  101. case PSA_KEY_TYPE_CAMELLIA:
  102. cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
  103. break;
  104. case PSA_KEY_TYPE_ARC4:
  105. cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
  106. break;
  107. case PSA_KEY_TYPE_CHACHA20:
  108. cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
  109. break;
  110. default:
  111. return( NULL );
  112. }
  113. if( cipher_id != NULL )
  114. *cipher_id = cipher_id_tmp;
  115. return( mbedtls_cipher_info_from_values( cipher_id_tmp,
  116. (int) key_bits, mode ) );
  117. }
  118. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  119. static psa_status_t psa_cipher_setup(
  120. mbedtls_psa_cipher_operation_t *operation,
  121. const psa_key_attributes_t *attributes,
  122. const uint8_t *key_buffer, size_t key_buffer_size,
  123. psa_algorithm_t alg,
  124. mbedtls_operation_t cipher_operation )
  125. {
  126. int ret = 0;
  127. size_t key_bits;
  128. const mbedtls_cipher_info_t *cipher_info = NULL;
  129. psa_key_type_t key_type = attributes->core.type;
  130. (void)key_buffer_size;
  131. mbedtls_cipher_init( &operation->ctx.cipher );
  132. operation->alg = alg;
  133. key_bits = attributes->core.bits;
  134. cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
  135. key_bits, NULL );
  136. if( cipher_info == NULL )
  137. return( PSA_ERROR_NOT_SUPPORTED );
  138. ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
  139. if( ret != 0 )
  140. goto exit;
  141. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  142. if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
  143. {
  144. /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
  145. uint8_t keys[24];
  146. memcpy( keys, key_buffer, 16 );
  147. memcpy( keys + 16, key_buffer, 8 );
  148. ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
  149. keys,
  150. 192, cipher_operation );
  151. }
  152. else
  153. #endif
  154. {
  155. ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer,
  156. (int) key_bits, cipher_operation );
  157. }
  158. if( ret != 0 )
  159. goto exit;
  160. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
  161. defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
  162. switch( alg )
  163. {
  164. case PSA_ALG_CBC_NO_PADDING:
  165. ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
  166. MBEDTLS_PADDING_NONE );
  167. break;
  168. case PSA_ALG_CBC_PKCS7:
  169. ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
  170. MBEDTLS_PADDING_PKCS7 );
  171. break;
  172. default:
  173. /* The algorithm doesn't involve padding. */
  174. ret = 0;
  175. break;
  176. }
  177. if( ret != 0 )
  178. goto exit;
  179. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING ||
  180. MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
  181. operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
  182. PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
  183. operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
  184. exit:
  185. return( mbedtls_to_psa_error( ret ) );
  186. }
  187. psa_status_t mbedtls_psa_cipher_encrypt_setup(
  188. mbedtls_psa_cipher_operation_t *operation,
  189. const psa_key_attributes_t *attributes,
  190. const uint8_t *key_buffer, size_t key_buffer_size,
  191. psa_algorithm_t alg )
  192. {
  193. return( psa_cipher_setup( operation, attributes,
  194. key_buffer, key_buffer_size,
  195. alg, MBEDTLS_ENCRYPT ) );
  196. }
  197. psa_status_t mbedtls_psa_cipher_decrypt_setup(
  198. mbedtls_psa_cipher_operation_t *operation,
  199. const psa_key_attributes_t *attributes,
  200. const uint8_t *key_buffer, size_t key_buffer_size,
  201. psa_algorithm_t alg )
  202. {
  203. return( psa_cipher_setup( operation, attributes,
  204. key_buffer, key_buffer_size,
  205. alg, MBEDTLS_DECRYPT ) );
  206. }
  207. psa_status_t mbedtls_psa_cipher_set_iv(
  208. mbedtls_psa_cipher_operation_t *operation,
  209. const uint8_t *iv, size_t iv_length )
  210. {
  211. if( iv_length != operation->iv_length )
  212. return( PSA_ERROR_INVALID_ARGUMENT );
  213. return( mbedtls_to_psa_error(
  214. mbedtls_cipher_set_iv( &operation->ctx.cipher,
  215. iv, iv_length ) ) );
  216. }
  217. /** Process input for which the algorithm is set to ECB mode.
  218. *
  219. * This requires manual processing, since the PSA API is defined as being
  220. * able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
  221. * but the underlying mbedtls_cipher_update only takes full blocks.
  222. *
  223. * \param ctx The mbedtls cipher context to use. It must have been
  224. * set up for ECB.
  225. * \param[in] input The input plaintext or ciphertext to process.
  226. * \param input_length The number of bytes to process from \p input.
  227. * This does not need to be aligned to a block boundary.
  228. * If there is a partial block at the end of the input,
  229. * it is stored in \p ctx for future processing.
  230. * \param output The buffer where the output is written. It must be
  231. * at least `BS * floor((p + input_length) / BS)` bytes
  232. * long, where `p` is the number of bytes in the
  233. * unprocessed partial block in \p ctx (with
  234. * `0 <= p <= BS - 1`) and `BS` is the block size.
  235. * \param output_length On success, the number of bytes written to \p output.
  236. * \c 0 on error.
  237. *
  238. * \return #PSA_SUCCESS or an error from a hardware accelerator
  239. */
  240. static psa_status_t psa_cipher_update_ecb(
  241. mbedtls_cipher_context_t *ctx,
  242. const uint8_t *input,
  243. size_t input_length,
  244. uint8_t *output,
  245. size_t *output_length )
  246. {
  247. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  248. size_t block_size = ctx->cipher_info->block_size;
  249. size_t internal_output_length = 0;
  250. *output_length = 0;
  251. if( input_length == 0 )
  252. {
  253. status = PSA_SUCCESS;
  254. goto exit;
  255. }
  256. if( ctx->unprocessed_len > 0 )
  257. {
  258. /* Fill up to block size, and run the block if there's a full one. */
  259. size_t bytes_to_copy = block_size - ctx->unprocessed_len;
  260. if( input_length < bytes_to_copy )
  261. bytes_to_copy = input_length;
  262. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
  263. input, bytes_to_copy );
  264. input_length -= bytes_to_copy;
  265. input += bytes_to_copy;
  266. ctx->unprocessed_len += bytes_to_copy;
  267. if( ctx->unprocessed_len == block_size )
  268. {
  269. status = mbedtls_to_psa_error(
  270. mbedtls_cipher_update( ctx,
  271. ctx->unprocessed_data,
  272. block_size,
  273. output, &internal_output_length ) );
  274. if( status != PSA_SUCCESS )
  275. goto exit;
  276. output += internal_output_length;
  277. *output_length += internal_output_length;
  278. ctx->unprocessed_len = 0;
  279. }
  280. }
  281. while( input_length >= block_size )
  282. {
  283. /* Run all full blocks we have, one by one */
  284. status = mbedtls_to_psa_error(
  285. mbedtls_cipher_update( ctx, input,
  286. block_size,
  287. output, &internal_output_length ) );
  288. if( status != PSA_SUCCESS )
  289. goto exit;
  290. input_length -= block_size;
  291. input += block_size;
  292. output += internal_output_length;
  293. *output_length += internal_output_length;
  294. }
  295. if( input_length > 0 )
  296. {
  297. /* Save unprocessed bytes for later processing */
  298. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
  299. input, input_length );
  300. ctx->unprocessed_len += input_length;
  301. }
  302. status = PSA_SUCCESS;
  303. exit:
  304. return( status );
  305. }
  306. psa_status_t mbedtls_psa_cipher_update(
  307. mbedtls_psa_cipher_operation_t *operation,
  308. const uint8_t *input, size_t input_length,
  309. uint8_t *output, size_t output_size, size_t *output_length )
  310. {
  311. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  312. size_t expected_output_size;
  313. if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
  314. {
  315. /* Take the unprocessed partial block left over from previous
  316. * update calls, if any, plus the input to this call. Remove
  317. * the last partial block, if any. You get the data that will be
  318. * output in this call. */
  319. expected_output_size =
  320. ( operation->ctx.cipher.unprocessed_len + input_length )
  321. / operation->block_length * operation->block_length;
  322. }
  323. else
  324. {
  325. expected_output_size = input_length;
  326. }
  327. if( output_size < expected_output_size )
  328. return( PSA_ERROR_BUFFER_TOO_SMALL );
  329. if( operation->alg == PSA_ALG_ECB_NO_PADDING )
  330. {
  331. /* mbedtls_cipher_update has an API inconsistency: it will only
  332. * process a single block at a time in ECB mode. Abstract away that
  333. * inconsistency here to match the PSA API behaviour. */
  334. status = psa_cipher_update_ecb( &operation->ctx.cipher,
  335. input,
  336. input_length,
  337. output,
  338. output_length );
  339. }
  340. else
  341. {
  342. status = mbedtls_to_psa_error(
  343. mbedtls_cipher_update( &operation->ctx.cipher, input,
  344. input_length, output, output_length ) );
  345. if( *output_length > output_size )
  346. return( PSA_ERROR_CORRUPTION_DETECTED );
  347. }
  348. return( status );
  349. }
  350. psa_status_t mbedtls_psa_cipher_finish(
  351. mbedtls_psa_cipher_operation_t *operation,
  352. uint8_t *output, size_t output_size, size_t *output_length )
  353. {
  354. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  355. uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
  356. if( operation->ctx.cipher.unprocessed_len != 0 )
  357. {
  358. if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
  359. operation->alg == PSA_ALG_CBC_NO_PADDING )
  360. {
  361. status = PSA_ERROR_INVALID_ARGUMENT;
  362. goto exit;
  363. }
  364. }
  365. status = mbedtls_to_psa_error(
  366. mbedtls_cipher_finish( &operation->ctx.cipher,
  367. temp_output_buffer,
  368. output_length ) );
  369. if( status != PSA_SUCCESS )
  370. goto exit;
  371. if( *output_length == 0 )
  372. ; /* Nothing to copy. Note that output may be NULL in this case. */
  373. else if( output_size >= *output_length )
  374. memcpy( output, temp_output_buffer, *output_length );
  375. else
  376. status = PSA_ERROR_BUFFER_TOO_SMALL;
  377. exit:
  378. mbedtls_platform_zeroize( temp_output_buffer,
  379. sizeof( temp_output_buffer ) );
  380. return( status );
  381. }
  382. psa_status_t mbedtls_psa_cipher_abort(
  383. mbedtls_psa_cipher_operation_t *operation )
  384. {
  385. /* Sanity check (shouldn't happen: operation->alg should
  386. * always have been initialized to a valid value). */
  387. if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
  388. return( PSA_ERROR_BAD_STATE );
  389. mbedtls_cipher_free( &operation->ctx.cipher );
  390. return( PSA_SUCCESS );
  391. }
  392. psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
  393. const uint8_t *key_buffer,
  394. size_t key_buffer_size,
  395. psa_algorithm_t alg,
  396. const uint8_t *iv,
  397. size_t iv_length,
  398. const uint8_t *input,
  399. size_t input_length,
  400. uint8_t *output,
  401. size_t output_size,
  402. size_t *output_length )
  403. {
  404. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  405. mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
  406. size_t update_output_length, finish_output_length;
  407. status = mbedtls_psa_cipher_encrypt_setup( &operation, attributes,
  408. key_buffer, key_buffer_size,
  409. alg );
  410. if( status != PSA_SUCCESS )
  411. goto exit;
  412. if( iv_length > 0 )
  413. {
  414. status = mbedtls_psa_cipher_set_iv( &operation, iv, iv_length );
  415. if( status != PSA_SUCCESS )
  416. goto exit;
  417. }
  418. status = mbedtls_psa_cipher_update( &operation, input, input_length,
  419. output, output_size, &update_output_length );
  420. if( status != PSA_SUCCESS )
  421. goto exit;
  422. status = mbedtls_psa_cipher_finish( &operation, output + update_output_length,
  423. output_size - update_output_length,
  424. &finish_output_length );
  425. if( status != PSA_SUCCESS )
  426. goto exit;
  427. *output_length = update_output_length + finish_output_length;
  428. exit:
  429. if( status == PSA_SUCCESS )
  430. status = mbedtls_psa_cipher_abort( &operation );
  431. else
  432. mbedtls_psa_cipher_abort( &operation );
  433. return( status );
  434. }
  435. psa_status_t mbedtls_psa_cipher_decrypt(
  436. const psa_key_attributes_t *attributes,
  437. const uint8_t *key_buffer,
  438. size_t key_buffer_size,
  439. psa_algorithm_t alg,
  440. const uint8_t *input,
  441. size_t input_length,
  442. uint8_t *output,
  443. size_t output_size,
  444. size_t *output_length )
  445. {
  446. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  447. mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
  448. size_t olength, accumulated_length;
  449. status = mbedtls_psa_cipher_decrypt_setup( &operation, attributes,
  450. key_buffer, key_buffer_size,
  451. alg );
  452. if( status != PSA_SUCCESS )
  453. goto exit;
  454. if( operation.iv_length > 0 )
  455. {
  456. status = mbedtls_psa_cipher_set_iv( &operation,
  457. input, operation.iv_length );
  458. if( status != PSA_SUCCESS )
  459. goto exit;
  460. }
  461. status = mbedtls_psa_cipher_update( &operation, input + operation.iv_length,
  462. input_length - operation.iv_length,
  463. output, output_size, &olength );
  464. if( status != PSA_SUCCESS )
  465. goto exit;
  466. accumulated_length = olength;
  467. status = mbedtls_psa_cipher_finish( &operation, output + accumulated_length,
  468. output_size - accumulated_length,
  469. &olength );
  470. if( status != PSA_SUCCESS )
  471. goto exit;
  472. *output_length = accumulated_length + olength;
  473. exit:
  474. if ( status == PSA_SUCCESS )
  475. status = mbedtls_psa_cipher_abort( &operation );
  476. else
  477. mbedtls_psa_cipher_abort( &operation );
  478. return( status );
  479. }
  480. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  481. #endif /* MBEDTLS_PSA_CRYPTO_C */