psa_crypto_cipher.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) || \
  29. ( defined(PSA_CRYPTO_DRIVER_TEST) && \
  30. defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_DES) ) )
  31. #define BUILTIN_KEY_TYPE_DES 1
  32. #endif
  33. #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
  34. ( defined(PSA_CRYPTO_DRIVER_TEST) && \
  35. defined(MBEDTLS_PSA_ACCEL_ALG_CBC_NO_PADDING) ) )
  36. #define BUILTIN_ALG_CBC_NO_PADDING 1
  37. #endif
  38. #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) || \
  39. ( defined(PSA_CRYPTO_DRIVER_TEST) && \
  40. defined(MBEDTLS_PSA_ACCEL_ALG_CBC_PKCS7) ) )
  41. #define BUILTIN_ALG_CBC_PKCS7 1
  42. #endif
  43. #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) || \
  44. ( defined(PSA_CRYPTO_DRIVER_TEST) && \
  45. defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_CHACHA20) ) )
  46. #define BUILTIN_KEY_TYPE_CHACHA20 1
  47. #endif
  48. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
  49. psa_algorithm_t alg,
  50. psa_key_type_t key_type,
  51. size_t key_bits,
  52. mbedtls_cipher_id_t* cipher_id )
  53. {
  54. mbedtls_cipher_mode_t mode;
  55. mbedtls_cipher_id_t cipher_id_tmp;
  56. if( PSA_ALG_IS_AEAD( alg ) )
  57. alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
  58. if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
  59. {
  60. switch( alg )
  61. {
  62. case PSA_ALG_STREAM_CIPHER:
  63. mode = MBEDTLS_MODE_STREAM;
  64. break;
  65. case PSA_ALG_CTR:
  66. mode = MBEDTLS_MODE_CTR;
  67. break;
  68. case PSA_ALG_CFB:
  69. mode = MBEDTLS_MODE_CFB;
  70. break;
  71. case PSA_ALG_OFB:
  72. mode = MBEDTLS_MODE_OFB;
  73. break;
  74. case PSA_ALG_ECB_NO_PADDING:
  75. mode = MBEDTLS_MODE_ECB;
  76. break;
  77. case PSA_ALG_CBC_NO_PADDING:
  78. mode = MBEDTLS_MODE_CBC;
  79. break;
  80. case PSA_ALG_CBC_PKCS7:
  81. mode = MBEDTLS_MODE_CBC;
  82. break;
  83. case PSA_ALG_CCM_STAR_NO_TAG:
  84. mode = MBEDTLS_MODE_CCM_STAR_NO_TAG;
  85. break;
  86. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
  87. mode = MBEDTLS_MODE_CCM;
  88. break;
  89. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
  90. mode = MBEDTLS_MODE_GCM;
  91. break;
  92. case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
  93. mode = MBEDTLS_MODE_CHACHAPOLY;
  94. break;
  95. default:
  96. return( NULL );
  97. }
  98. }
  99. else if( alg == PSA_ALG_CMAC )
  100. mode = MBEDTLS_MODE_ECB;
  101. else
  102. return( NULL );
  103. switch( key_type )
  104. {
  105. case PSA_KEY_TYPE_AES:
  106. cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
  107. break;
  108. case PSA_KEY_TYPE_ARIA:
  109. cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA;
  110. break;
  111. case PSA_KEY_TYPE_DES:
  112. /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
  113. * and 192 for three-key Triple-DES. */
  114. if( key_bits == 64 )
  115. cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
  116. else
  117. cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
  118. /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
  119. * but two-key Triple-DES is functionally three-key Triple-DES
  120. * with K1=K3, so that's how we present it to mbedtls. */
  121. if( key_bits == 128 )
  122. key_bits = 192;
  123. break;
  124. case PSA_KEY_TYPE_CAMELLIA:
  125. cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
  126. break;
  127. case PSA_KEY_TYPE_CHACHA20:
  128. cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
  129. break;
  130. default:
  131. return( NULL );
  132. }
  133. if( cipher_id != NULL )
  134. *cipher_id = cipher_id_tmp;
  135. return( mbedtls_cipher_info_from_values( cipher_id_tmp,
  136. (int) key_bits, mode ) );
  137. }
  138. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) || defined(PSA_CRYPTO_DRIVER_TEST)
  139. static psa_status_t cipher_setup(
  140. mbedtls_psa_cipher_operation_t *operation,
  141. const psa_key_attributes_t *attributes,
  142. const uint8_t *key_buffer, size_t key_buffer_size,
  143. psa_algorithm_t alg,
  144. mbedtls_operation_t cipher_operation )
  145. {
  146. int ret = 0;
  147. size_t key_bits;
  148. const mbedtls_cipher_info_t *cipher_info = NULL;
  149. psa_key_type_t key_type = attributes->core.type;
  150. (void)key_buffer_size;
  151. mbedtls_cipher_init( &operation->ctx.cipher );
  152. operation->alg = alg;
  153. key_bits = attributes->core.bits;
  154. cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
  155. key_bits, NULL );
  156. if( cipher_info == NULL )
  157. return( PSA_ERROR_NOT_SUPPORTED );
  158. ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
  159. if( ret != 0 )
  160. goto exit;
  161. #if defined(BUILTIN_KEY_TYPE_DES)
  162. if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
  163. {
  164. /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
  165. uint8_t keys[24];
  166. memcpy( keys, key_buffer, 16 );
  167. memcpy( keys + 16, key_buffer, 8 );
  168. ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
  169. keys,
  170. 192, cipher_operation );
  171. }
  172. else
  173. #endif
  174. {
  175. ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer,
  176. (int) key_bits, cipher_operation );
  177. }
  178. if( ret != 0 )
  179. goto exit;
  180. #if defined(BUILTIN_ALG_CBC_NO_PADDING) || \
  181. defined(BUILTIN_ALG_CBC_PKCS7)
  182. switch( alg )
  183. {
  184. case PSA_ALG_CBC_NO_PADDING:
  185. ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
  186. MBEDTLS_PADDING_NONE );
  187. break;
  188. case PSA_ALG_CBC_PKCS7:
  189. ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
  190. MBEDTLS_PADDING_PKCS7 );
  191. break;
  192. default:
  193. /* The algorithm doesn't involve padding. */
  194. ret = 0;
  195. break;
  196. }
  197. if( ret != 0 )
  198. goto exit;
  199. #endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */
  200. operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
  201. PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) );
  202. operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
  203. exit:
  204. return( mbedtls_to_psa_error( ret ) );
  205. }
  206. static psa_status_t cipher_encrypt_setup(
  207. mbedtls_psa_cipher_operation_t *operation,
  208. const psa_key_attributes_t *attributes,
  209. const uint8_t *key_buffer, size_t key_buffer_size,
  210. psa_algorithm_t alg )
  211. {
  212. return( cipher_setup( operation, attributes,
  213. key_buffer, key_buffer_size,
  214. alg, MBEDTLS_ENCRYPT ) );
  215. }
  216. static psa_status_t cipher_decrypt_setup(
  217. mbedtls_psa_cipher_operation_t *operation,
  218. const psa_key_attributes_t *attributes,
  219. const uint8_t *key_buffer, size_t key_buffer_size,
  220. psa_algorithm_t alg )
  221. {
  222. return( cipher_setup( operation, attributes,
  223. key_buffer, key_buffer_size,
  224. alg, MBEDTLS_DECRYPT ) );
  225. }
  226. static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
  227. const uint8_t *iv, size_t iv_length )
  228. {
  229. if( iv_length != operation->iv_length )
  230. return( PSA_ERROR_INVALID_ARGUMENT );
  231. return( mbedtls_to_psa_error(
  232. mbedtls_cipher_set_iv( &operation->ctx.cipher,
  233. iv, iv_length ) ) );
  234. }
  235. /** Process input for which the algorithm is set to ECB mode.
  236. *
  237. * This requires manual processing, since the PSA API is defined as being
  238. * able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
  239. * but the underlying mbedtls_cipher_update only takes full blocks.
  240. *
  241. * \param ctx The mbedtls cipher context to use. It must have been
  242. * set up for ECB.
  243. * \param[in] input The input plaintext or ciphertext to process.
  244. * \param input_length The number of bytes to process from \p input.
  245. * This does not need to be aligned to a block boundary.
  246. * If there is a partial block at the end of the input,
  247. * it is stored in \p ctx for future processing.
  248. * \param output The buffer where the output is written. It must be
  249. * at least `BS * floor((p + input_length) / BS)` bytes
  250. * long, where `p` is the number of bytes in the
  251. * unprocessed partial block in \p ctx (with
  252. * `0 <= p <= BS - 1`) and `BS` is the block size.
  253. * \param output_length On success, the number of bytes written to \p output.
  254. * \c 0 on error.
  255. *
  256. * \return #PSA_SUCCESS or an error from a hardware accelerator
  257. */
  258. static psa_status_t psa_cipher_update_ecb(
  259. mbedtls_cipher_context_t *ctx,
  260. const uint8_t *input,
  261. size_t input_length,
  262. uint8_t *output,
  263. size_t *output_length )
  264. {
  265. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  266. size_t block_size = ctx->cipher_info->block_size;
  267. size_t internal_output_length = 0;
  268. *output_length = 0;
  269. if( input_length == 0 )
  270. {
  271. status = PSA_SUCCESS;
  272. goto exit;
  273. }
  274. if( ctx->unprocessed_len > 0 )
  275. {
  276. /* Fill up to block size, and run the block if there's a full one. */
  277. size_t bytes_to_copy = block_size - ctx->unprocessed_len;
  278. if( input_length < bytes_to_copy )
  279. bytes_to_copy = input_length;
  280. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
  281. input, bytes_to_copy );
  282. input_length -= bytes_to_copy;
  283. input += bytes_to_copy;
  284. ctx->unprocessed_len += bytes_to_copy;
  285. if( ctx->unprocessed_len == block_size )
  286. {
  287. status = mbedtls_to_psa_error(
  288. mbedtls_cipher_update( ctx,
  289. ctx->unprocessed_data,
  290. block_size,
  291. output, &internal_output_length ) );
  292. if( status != PSA_SUCCESS )
  293. goto exit;
  294. output += internal_output_length;
  295. *output_length += internal_output_length;
  296. ctx->unprocessed_len = 0;
  297. }
  298. }
  299. while( input_length >= block_size )
  300. {
  301. /* Run all full blocks we have, one by one */
  302. status = mbedtls_to_psa_error(
  303. mbedtls_cipher_update( ctx, input,
  304. block_size,
  305. output, &internal_output_length ) );
  306. if( status != PSA_SUCCESS )
  307. goto exit;
  308. input_length -= block_size;
  309. input += block_size;
  310. output += internal_output_length;
  311. *output_length += internal_output_length;
  312. }
  313. if( input_length > 0 )
  314. {
  315. /* Save unprocessed bytes for later processing */
  316. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
  317. input, input_length );
  318. ctx->unprocessed_len += input_length;
  319. }
  320. status = PSA_SUCCESS;
  321. exit:
  322. return( status );
  323. }
  324. static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation,
  325. const uint8_t *input,
  326. size_t input_length,
  327. uint8_t *output,
  328. size_t output_size,
  329. size_t *output_length )
  330. {
  331. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  332. size_t expected_output_size;
  333. if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
  334. {
  335. /* Take the unprocessed partial block left over from previous
  336. * update calls, if any, plus the input to this call. Remove
  337. * the last partial block, if any. You get the data that will be
  338. * output in this call. */
  339. expected_output_size =
  340. ( operation->ctx.cipher.unprocessed_len + input_length )
  341. / operation->block_length * operation->block_length;
  342. }
  343. else
  344. {
  345. expected_output_size = input_length;
  346. }
  347. if( output_size < expected_output_size )
  348. return( PSA_ERROR_BUFFER_TOO_SMALL );
  349. if( operation->alg == PSA_ALG_ECB_NO_PADDING )
  350. {
  351. /* mbedtls_cipher_update has an API inconsistency: it will only
  352. * process a single block at a time in ECB mode. Abstract away that
  353. * inconsistency here to match the PSA API behaviour. */
  354. status = psa_cipher_update_ecb( &operation->ctx.cipher,
  355. input,
  356. input_length,
  357. output,
  358. output_length );
  359. }
  360. else
  361. {
  362. status = mbedtls_to_psa_error(
  363. mbedtls_cipher_update( &operation->ctx.cipher, input,
  364. input_length, output, output_length ) );
  365. if( *output_length > output_size )
  366. return( PSA_ERROR_CORRUPTION_DETECTED );
  367. }
  368. return( status );
  369. }
  370. static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation,
  371. uint8_t *output,
  372. size_t output_size,
  373. size_t *output_length )
  374. {
  375. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  376. uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
  377. if( operation->ctx.cipher.unprocessed_len != 0 )
  378. {
  379. if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
  380. operation->alg == PSA_ALG_CBC_NO_PADDING )
  381. {
  382. status = PSA_ERROR_INVALID_ARGUMENT;
  383. goto exit;
  384. }
  385. }
  386. status = mbedtls_to_psa_error(
  387. mbedtls_cipher_finish( &operation->ctx.cipher,
  388. temp_output_buffer,
  389. output_length ) );
  390. if( status != PSA_SUCCESS )
  391. goto exit;
  392. if( *output_length == 0 )
  393. ; /* Nothing to copy. Note that output may be NULL in this case. */
  394. else if( output_size >= *output_length )
  395. memcpy( output, temp_output_buffer, *output_length );
  396. else
  397. status = PSA_ERROR_BUFFER_TOO_SMALL;
  398. exit:
  399. mbedtls_platform_zeroize( temp_output_buffer,
  400. sizeof( temp_output_buffer ) );
  401. return( status );
  402. }
  403. static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation )
  404. {
  405. /* Sanity check (shouldn't happen: operation->alg should
  406. * always have been initialized to a valid value). */
  407. if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
  408. return( PSA_ERROR_BAD_STATE );
  409. mbedtls_cipher_free( &operation->ctx.cipher );
  410. return( PSA_SUCCESS );
  411. }
  412. static psa_status_t cipher_encrypt( const psa_key_attributes_t *attributes,
  413. const uint8_t *key_buffer,
  414. size_t key_buffer_size,
  415. psa_algorithm_t alg,
  416. const uint8_t *input,
  417. size_t input_length,
  418. uint8_t *output,
  419. size_t output_size,
  420. size_t *output_length )
  421. {
  422. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  423. mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
  424. size_t olength, accumulated_length;
  425. status = cipher_encrypt_setup( &operation, attributes,
  426. key_buffer, key_buffer_size, alg );
  427. if( status != PSA_SUCCESS )
  428. goto exit;
  429. accumulated_length = 0;
  430. if( operation.iv_length > 0 )
  431. {
  432. status = cipher_set_iv( &operation, output, operation.iv_length );
  433. if( status != PSA_SUCCESS )
  434. goto exit;
  435. accumulated_length = operation.iv_length;
  436. }
  437. status = cipher_update( &operation, input, input_length,
  438. output + operation.iv_length,
  439. output_size - operation.iv_length,
  440. &olength );
  441. if( status != PSA_SUCCESS )
  442. goto exit;
  443. accumulated_length += olength;
  444. status = cipher_finish( &operation, output + accumulated_length,
  445. output_size - accumulated_length, &olength );
  446. if( status != PSA_SUCCESS )
  447. goto exit;
  448. *output_length = accumulated_length + olength;
  449. exit:
  450. if( status == PSA_SUCCESS )
  451. status = cipher_abort( &operation );
  452. else
  453. cipher_abort( &operation );
  454. return( status );
  455. }
  456. static psa_status_t cipher_decrypt( const psa_key_attributes_t *attributes,
  457. const uint8_t *key_buffer,
  458. size_t key_buffer_size,
  459. psa_algorithm_t alg,
  460. const uint8_t *input,
  461. size_t input_length,
  462. uint8_t *output,
  463. size_t output_size,
  464. size_t *output_length )
  465. {
  466. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  467. mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
  468. size_t olength, accumulated_length;
  469. status = cipher_decrypt_setup( &operation, attributes,
  470. key_buffer, key_buffer_size, alg );
  471. if( status != PSA_SUCCESS )
  472. goto exit;
  473. if( operation.iv_length > 0 )
  474. {
  475. status = cipher_set_iv( &operation, input, operation.iv_length );
  476. if( status != PSA_SUCCESS )
  477. goto exit;
  478. }
  479. status = cipher_update( &operation, input + operation.iv_length,
  480. input_length - operation.iv_length,
  481. output, output_size, &olength );
  482. if( status != PSA_SUCCESS )
  483. goto exit;
  484. accumulated_length = olength;
  485. status = cipher_finish( &operation, output + accumulated_length,
  486. output_size - accumulated_length, &olength );
  487. if( status != PSA_SUCCESS )
  488. goto exit;
  489. *output_length = accumulated_length + olength;
  490. exit:
  491. if ( status == PSA_SUCCESS )
  492. status = cipher_abort( &operation );
  493. else
  494. cipher_abort( &operation );
  495. return( status );
  496. }
  497. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER || PSA_CRYPTO_DRIVER_TEST */
  498. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  499. psa_status_t mbedtls_psa_cipher_encrypt_setup(
  500. mbedtls_psa_cipher_operation_t *operation,
  501. const psa_key_attributes_t *attributes,
  502. const uint8_t *key_buffer, size_t key_buffer_size,
  503. psa_algorithm_t alg )
  504. {
  505. return( cipher_encrypt_setup(
  506. operation, attributes, key_buffer, key_buffer_size, alg ) );
  507. }
  508. psa_status_t mbedtls_psa_cipher_decrypt_setup(
  509. mbedtls_psa_cipher_operation_t *operation,
  510. const psa_key_attributes_t *attributes,
  511. const uint8_t *key_buffer, size_t key_buffer_size,
  512. psa_algorithm_t alg )
  513. {
  514. return( cipher_decrypt_setup(
  515. operation, attributes, key_buffer, key_buffer_size, alg ) );
  516. }
  517. psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation,
  518. const uint8_t *iv,
  519. size_t iv_length )
  520. {
  521. return( cipher_set_iv( operation, iv, iv_length ) );
  522. }
  523. psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation,
  524. const uint8_t *input,
  525. size_t input_length,
  526. uint8_t *output,
  527. size_t output_size,
  528. size_t *output_length )
  529. {
  530. return( cipher_update( operation, input, input_length,
  531. output, output_size, output_length ) );
  532. }
  533. psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation,
  534. uint8_t *output,
  535. size_t output_size,
  536. size_t *output_length )
  537. {
  538. return( cipher_finish( operation, output, output_size, output_length ) );
  539. }
  540. psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation )
  541. {
  542. return( cipher_abort( operation ) );
  543. }
  544. psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
  545. const uint8_t *key_buffer,
  546. size_t key_buffer_size,
  547. psa_algorithm_t alg,
  548. const uint8_t *input,
  549. size_t input_length,
  550. uint8_t *output,
  551. size_t output_size,
  552. size_t *output_length )
  553. {
  554. return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
  555. alg, input, input_length,
  556. output, output_size, output_length ) );
  557. }
  558. psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes,
  559. const uint8_t *key_buffer,
  560. size_t key_buffer_size,
  561. psa_algorithm_t alg,
  562. const uint8_t *input,
  563. size_t input_length,
  564. uint8_t *output,
  565. size_t output_size,
  566. size_t *output_length )
  567. {
  568. return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
  569. alg, input, input_length,
  570. output, output_size, output_length ) );
  571. }
  572. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  573. /*
  574. * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
  575. */
  576. #if defined(PSA_CRYPTO_DRIVER_TEST)
  577. psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
  578. mbedtls_psa_cipher_operation_t *operation,
  579. const psa_key_attributes_t *attributes,
  580. const uint8_t *key_buffer, size_t key_buffer_size,
  581. psa_algorithm_t alg )
  582. {
  583. return( cipher_encrypt_setup(
  584. operation, attributes, key_buffer, key_buffer_size, alg ) );
  585. }
  586. psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
  587. mbedtls_psa_cipher_operation_t *operation,
  588. const psa_key_attributes_t *attributes,
  589. const uint8_t *key_buffer, size_t key_buffer_size,
  590. psa_algorithm_t alg )
  591. {
  592. return( cipher_decrypt_setup(
  593. operation, attributes, key_buffer, key_buffer_size, alg ) );
  594. }
  595. psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
  596. mbedtls_psa_cipher_operation_t *operation,
  597. const uint8_t *iv, size_t iv_length )
  598. {
  599. return( cipher_set_iv( operation, iv, iv_length ) );
  600. }
  601. psa_status_t mbedtls_transparent_test_driver_cipher_update(
  602. mbedtls_psa_cipher_operation_t *operation,
  603. const uint8_t *input, size_t input_length,
  604. uint8_t *output, size_t output_size, size_t *output_length )
  605. {
  606. return( cipher_update( operation, input, input_length,
  607. output, output_size, output_length ) );
  608. }
  609. psa_status_t mbedtls_transparent_test_driver_cipher_finish(
  610. mbedtls_psa_cipher_operation_t *operation,
  611. uint8_t *output, size_t output_size, size_t *output_length )
  612. {
  613. return( cipher_finish( operation, output, output_size, output_length ) );
  614. }
  615. psa_status_t mbedtls_transparent_test_driver_cipher_abort(
  616. mbedtls_psa_cipher_operation_t *operation )
  617. {
  618. return( cipher_abort( operation ) );
  619. }
  620. psa_status_t mbedtls_transparent_test_driver_cipher_encrypt(
  621. const psa_key_attributes_t *attributes,
  622. const uint8_t *key_buffer,
  623. size_t key_buffer_size,
  624. psa_algorithm_t alg,
  625. const uint8_t *input,
  626. size_t input_length,
  627. uint8_t *output,
  628. size_t output_size,
  629. size_t *output_length )
  630. {
  631. return( cipher_encrypt( attributes, key_buffer, key_buffer_size,
  632. alg, input, input_length,
  633. output, output_size, output_length ) );
  634. }
  635. psa_status_t mbedtls_transparent_test_driver_cipher_decrypt(
  636. const psa_key_attributes_t *attributes,
  637. const uint8_t *key_buffer,
  638. size_t key_buffer_size,
  639. psa_algorithm_t alg,
  640. const uint8_t *input,
  641. size_t input_length,
  642. uint8_t *output,
  643. size_t output_size,
  644. size_t *output_length )
  645. {
  646. return( cipher_decrypt( attributes, key_buffer, key_buffer_size,
  647. alg, input, input_length,
  648. output, output_size, output_length ) );
  649. }
  650. #endif /* PSA_CRYPTO_DRIVER_TEST */
  651. #endif /* MBEDTLS_PSA_CRYPTO_C */