psa_crypto_cipher.c 24 KB

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