psa_crypto_cipher.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * PSA cipher driver entry points
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. #include "common.h"
  9. #if defined(MBEDTLS_PSA_CRYPTO_C)
  10. #include "psa_crypto_cipher.h"
  11. #include "psa_crypto_core.h"
  12. #include "psa_crypto_random_impl.h"
  13. #include "constant_time_internal.h"
  14. #include "mbedtls/cipher.h"
  15. #include "mbedtls/error.h"
  16. #include <string.h>
  17. /* mbedtls_cipher_values_from_psa() below only checks if the proper build symbols
  18. * are enabled, but it does not provide any compatibility check between them
  19. * (i.e. if the specified key works with the specified algorithm). This helper
  20. * function is meant to provide this support.
  21. * mbedtls_cipher_info_from_psa() might be used for the same purpose, but it
  22. * requires CIPHER_C to be enabled.
  23. */
  24. static psa_status_t mbedtls_cipher_validate_values(
  25. psa_algorithm_t alg,
  26. psa_key_type_t key_type)
  27. {
  28. /* Reduce code size - hinting to the compiler about what it can assume allows the compiler to
  29. eliminate bits of the logic below. */
  30. #if !defined(PSA_WANT_KEY_TYPE_AES)
  31. MBEDTLS_ASSUME(key_type != PSA_KEY_TYPE_AES);
  32. #endif
  33. #if !defined(PSA_WANT_KEY_TYPE_ARIA)
  34. MBEDTLS_ASSUME(key_type != PSA_KEY_TYPE_ARIA);
  35. #endif
  36. #if !defined(PSA_WANT_KEY_TYPE_CAMELLIA)
  37. MBEDTLS_ASSUME(key_type != PSA_KEY_TYPE_CAMELLIA);
  38. #endif
  39. #if !defined(PSA_WANT_KEY_TYPE_CHACHA20)
  40. MBEDTLS_ASSUME(key_type != PSA_KEY_TYPE_CHACHA20);
  41. #endif
  42. #if !defined(PSA_WANT_KEY_TYPE_DES)
  43. MBEDTLS_ASSUME(key_type != PSA_KEY_TYPE_DES);
  44. #endif
  45. #if !defined(PSA_WANT_ALG_CCM)
  46. MBEDTLS_ASSUME(alg != PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0));
  47. #endif
  48. #if !defined(PSA_WANT_ALG_GCM)
  49. MBEDTLS_ASSUME(alg != PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0));
  50. #endif
  51. #if !defined(PSA_WANT_ALG_STREAM_CIPHER)
  52. MBEDTLS_ASSUME(alg != PSA_ALG_STREAM_CIPHER);
  53. #endif
  54. #if !defined(PSA_WANT_ALG_CHACHA20_POLY1305)
  55. MBEDTLS_ASSUME(alg != PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0));
  56. #endif
  57. #if !defined(PSA_WANT_ALG_CCM_STAR_NO_TAG)
  58. MBEDTLS_ASSUME(alg != PSA_ALG_CCM_STAR_NO_TAG);
  59. #endif
  60. #if !defined(PSA_WANT_ALG_CTR)
  61. MBEDTLS_ASSUME(alg != PSA_ALG_CTR);
  62. #endif
  63. #if !defined(PSA_WANT_ALG_CFB)
  64. MBEDTLS_ASSUME(alg != PSA_ALG_CFB);
  65. #endif
  66. #if !defined(PSA_WANT_ALG_OFB)
  67. MBEDTLS_ASSUME(alg != PSA_ALG_OFB);
  68. #endif
  69. #if !defined(PSA_WANT_ALG_ECB_NO_PADDING)
  70. MBEDTLS_ASSUME(alg != PSA_ALG_ECB_NO_PADDING);
  71. #endif
  72. #if !defined(PSA_WANT_ALG_CBC_NO_PADDING)
  73. MBEDTLS_ASSUME(alg != PSA_ALG_CBC_NO_PADDING);
  74. #endif
  75. #if !defined(PSA_WANT_ALG_CBC_PKCS7)
  76. MBEDTLS_ASSUME(alg != PSA_ALG_CBC_PKCS7);
  77. #endif
  78. #if !defined(PSA_WANT_ALG_CMAC)
  79. MBEDTLS_ASSUME(alg != PSA_ALG_CMAC);
  80. #endif
  81. if (alg == PSA_ALG_STREAM_CIPHER ||
  82. alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0)) {
  83. if (key_type == PSA_KEY_TYPE_CHACHA20) {
  84. return PSA_SUCCESS;
  85. }
  86. }
  87. if (alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0) ||
  88. alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0) ||
  89. alg == PSA_ALG_CCM_STAR_NO_TAG) {
  90. if (key_type == PSA_KEY_TYPE_AES ||
  91. key_type == PSA_KEY_TYPE_ARIA ||
  92. key_type == PSA_KEY_TYPE_CAMELLIA) {
  93. return PSA_SUCCESS;
  94. }
  95. }
  96. if (alg == PSA_ALG_CTR ||
  97. alg == PSA_ALG_CFB ||
  98. alg == PSA_ALG_OFB ||
  99. alg == PSA_ALG_XTS ||
  100. alg == PSA_ALG_ECB_NO_PADDING ||
  101. alg == PSA_ALG_CBC_NO_PADDING ||
  102. alg == PSA_ALG_CBC_PKCS7 ||
  103. alg == PSA_ALG_CMAC) {
  104. if (key_type == PSA_KEY_TYPE_AES ||
  105. key_type == PSA_KEY_TYPE_ARIA ||
  106. key_type == PSA_KEY_TYPE_DES ||
  107. key_type == PSA_KEY_TYPE_CAMELLIA) {
  108. return PSA_SUCCESS;
  109. }
  110. }
  111. return PSA_ERROR_NOT_SUPPORTED;
  112. }
  113. psa_status_t mbedtls_cipher_values_from_psa(
  114. psa_algorithm_t alg,
  115. psa_key_type_t key_type,
  116. size_t *key_bits,
  117. mbedtls_cipher_mode_t *mode,
  118. mbedtls_cipher_id_t *cipher_id)
  119. {
  120. mbedtls_cipher_id_t cipher_id_tmp;
  121. /* Only DES modifies key_bits */
  122. #if !defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  123. (void) key_bits;
  124. #endif
  125. if (PSA_ALG_IS_AEAD(alg)) {
  126. alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0);
  127. }
  128. if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg)) {
  129. switch (alg) {
  130. #if defined(MBEDTLS_PSA_BUILTIN_ALG_STREAM_CIPHER)
  131. case PSA_ALG_STREAM_CIPHER:
  132. *mode = MBEDTLS_MODE_STREAM;
  133. break;
  134. #endif
  135. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CTR)
  136. case PSA_ALG_CTR:
  137. *mode = MBEDTLS_MODE_CTR;
  138. break;
  139. #endif
  140. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CFB)
  141. case PSA_ALG_CFB:
  142. *mode = MBEDTLS_MODE_CFB;
  143. break;
  144. #endif
  145. #if defined(MBEDTLS_PSA_BUILTIN_ALG_OFB)
  146. case PSA_ALG_OFB:
  147. *mode = MBEDTLS_MODE_OFB;
  148. break;
  149. #endif
  150. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
  151. case PSA_ALG_ECB_NO_PADDING:
  152. *mode = MBEDTLS_MODE_ECB;
  153. break;
  154. #endif
  155. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING)
  156. case PSA_ALG_CBC_NO_PADDING:
  157. *mode = MBEDTLS_MODE_CBC;
  158. break;
  159. #endif
  160. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
  161. case PSA_ALG_CBC_PKCS7:
  162. *mode = MBEDTLS_MODE_CBC;
  163. break;
  164. #endif
  165. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM_STAR_NO_TAG)
  166. case PSA_ALG_CCM_STAR_NO_TAG:
  167. *mode = MBEDTLS_MODE_CCM_STAR_NO_TAG;
  168. break;
  169. #endif
  170. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM)
  171. case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0):
  172. *mode = MBEDTLS_MODE_CCM;
  173. break;
  174. #endif
  175. #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM)
  176. case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0):
  177. *mode = MBEDTLS_MODE_GCM;
  178. break;
  179. #endif
  180. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305)
  181. case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0):
  182. *mode = MBEDTLS_MODE_CHACHAPOLY;
  183. break;
  184. #endif
  185. default:
  186. return PSA_ERROR_NOT_SUPPORTED;
  187. }
  188. } else if (alg == PSA_ALG_CMAC) {
  189. *mode = MBEDTLS_MODE_ECB;
  190. } else {
  191. return PSA_ERROR_NOT_SUPPORTED;
  192. }
  193. switch (key_type) {
  194. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
  195. case PSA_KEY_TYPE_AES:
  196. cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
  197. break;
  198. #endif
  199. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARIA)
  200. case PSA_KEY_TYPE_ARIA:
  201. cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA;
  202. break;
  203. #endif
  204. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  205. case PSA_KEY_TYPE_DES:
  206. /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
  207. * and 192 for three-key Triple-DES. */
  208. if (*key_bits == 64) {
  209. cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
  210. } else {
  211. cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
  212. }
  213. /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
  214. * but two-key Triple-DES is functionally three-key Triple-DES
  215. * with K1=K3, so that's how we present it to mbedtls. */
  216. if (*key_bits == 128) {
  217. *key_bits = 192;
  218. }
  219. break;
  220. #endif
  221. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
  222. case PSA_KEY_TYPE_CAMELLIA:
  223. cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
  224. break;
  225. #endif
  226. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
  227. case PSA_KEY_TYPE_CHACHA20:
  228. cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
  229. break;
  230. #endif
  231. default:
  232. return PSA_ERROR_NOT_SUPPORTED;
  233. }
  234. if (cipher_id != NULL) {
  235. *cipher_id = cipher_id_tmp;
  236. }
  237. return mbedtls_cipher_validate_values(alg, key_type);
  238. }
  239. #if defined(MBEDTLS_CIPHER_C)
  240. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
  241. psa_algorithm_t alg,
  242. psa_key_type_t key_type,
  243. size_t key_bits,
  244. mbedtls_cipher_id_t *cipher_id)
  245. {
  246. mbedtls_cipher_mode_t mode;
  247. psa_status_t status;
  248. mbedtls_cipher_id_t cipher_id_tmp = MBEDTLS_CIPHER_ID_NONE;
  249. status = mbedtls_cipher_values_from_psa(alg, key_type, &key_bits, &mode, &cipher_id_tmp);
  250. if (status != PSA_SUCCESS) {
  251. return NULL;
  252. }
  253. if (cipher_id != NULL) {
  254. *cipher_id = cipher_id_tmp;
  255. }
  256. return mbedtls_cipher_info_from_values(cipher_id_tmp, (int) key_bits, mode);
  257. }
  258. #endif /* MBEDTLS_CIPHER_C */
  259. #if defined(MBEDTLS_PSA_BUILTIN_CIPHER)
  260. static psa_status_t psa_cipher_setup(
  261. mbedtls_psa_cipher_operation_t *operation,
  262. const psa_key_attributes_t *attributes,
  263. const uint8_t *key_buffer, size_t key_buffer_size,
  264. psa_algorithm_t alg,
  265. mbedtls_operation_t cipher_operation)
  266. {
  267. int ret = 0;
  268. size_t key_bits;
  269. const mbedtls_cipher_info_t *cipher_info = NULL;
  270. psa_key_type_t key_type = attributes->type;
  271. (void) key_buffer_size;
  272. mbedtls_cipher_init(&operation->ctx.cipher);
  273. operation->alg = alg;
  274. key_bits = attributes->bits;
  275. cipher_info = mbedtls_cipher_info_from_psa(alg, key_type,
  276. key_bits, NULL);
  277. if (cipher_info == NULL) {
  278. return PSA_ERROR_NOT_SUPPORTED;
  279. }
  280. ret = mbedtls_cipher_setup(&operation->ctx.cipher, cipher_info);
  281. if (ret != 0) {
  282. goto exit;
  283. }
  284. #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
  285. if (key_type == PSA_KEY_TYPE_DES && key_bits == 128) {
  286. /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
  287. uint8_t keys[24];
  288. memcpy(keys, key_buffer, 16);
  289. memcpy(keys + 16, key_buffer, 8);
  290. ret = mbedtls_cipher_setkey(&operation->ctx.cipher,
  291. keys,
  292. 192, cipher_operation);
  293. } else
  294. #endif
  295. {
  296. ret = mbedtls_cipher_setkey(&operation->ctx.cipher, key_buffer,
  297. (int) key_bits, cipher_operation);
  298. }
  299. if (ret != 0) {
  300. goto exit;
  301. }
  302. #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
  303. defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
  304. switch (alg) {
  305. case PSA_ALG_CBC_NO_PADDING:
  306. ret = mbedtls_cipher_set_padding_mode(&operation->ctx.cipher,
  307. MBEDTLS_PADDING_NONE);
  308. break;
  309. case PSA_ALG_CBC_PKCS7:
  310. ret = mbedtls_cipher_set_padding_mode(&operation->ctx.cipher,
  311. MBEDTLS_PADDING_PKCS7);
  312. break;
  313. default:
  314. /* The algorithm doesn't involve padding. */
  315. ret = 0;
  316. break;
  317. }
  318. if (ret != 0) {
  319. goto exit;
  320. }
  321. #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING ||
  322. MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
  323. operation->block_length = (PSA_ALG_IS_STREAM_CIPHER(alg) ? 1 :
  324. PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type));
  325. operation->iv_length = PSA_CIPHER_IV_LENGTH(key_type, alg);
  326. exit:
  327. return mbedtls_to_psa_error(ret);
  328. }
  329. psa_status_t mbedtls_psa_cipher_encrypt_setup(
  330. mbedtls_psa_cipher_operation_t *operation,
  331. const psa_key_attributes_t *attributes,
  332. const uint8_t *key_buffer, size_t key_buffer_size,
  333. psa_algorithm_t alg)
  334. {
  335. return psa_cipher_setup(operation, attributes,
  336. key_buffer, key_buffer_size,
  337. alg, MBEDTLS_ENCRYPT);
  338. }
  339. psa_status_t mbedtls_psa_cipher_decrypt_setup(
  340. mbedtls_psa_cipher_operation_t *operation,
  341. const psa_key_attributes_t *attributes,
  342. const uint8_t *key_buffer, size_t key_buffer_size,
  343. psa_algorithm_t alg)
  344. {
  345. return psa_cipher_setup(operation, attributes,
  346. key_buffer, key_buffer_size,
  347. alg, MBEDTLS_DECRYPT);
  348. }
  349. psa_status_t mbedtls_psa_cipher_set_iv(
  350. mbedtls_psa_cipher_operation_t *operation,
  351. const uint8_t *iv, size_t iv_length)
  352. {
  353. if (iv_length != operation->iv_length) {
  354. return PSA_ERROR_INVALID_ARGUMENT;
  355. }
  356. return mbedtls_to_psa_error(
  357. mbedtls_cipher_set_iv(&operation->ctx.cipher,
  358. iv, iv_length));
  359. }
  360. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
  361. /** Process input for which the algorithm is set to ECB mode.
  362. *
  363. * This requires manual processing, since the PSA API is defined as being
  364. * able to process arbitrary-length calls to psa_cipher_update() with ECB mode,
  365. * but the underlying mbedtls_cipher_update only takes full blocks.
  366. *
  367. * \param ctx The mbedtls cipher context to use. It must have been
  368. * set up for ECB.
  369. * \param[in] input The input plaintext or ciphertext to process.
  370. * \param input_length The number of bytes to process from \p input.
  371. * This does not need to be aligned to a block boundary.
  372. * If there is a partial block at the end of the input,
  373. * it is stored in \p ctx for future processing.
  374. * \param output The buffer where the output is written. It must be
  375. * at least `BS * floor((p + input_length) / BS)` bytes
  376. * long, where `p` is the number of bytes in the
  377. * unprocessed partial block in \p ctx (with
  378. * `0 <= p <= BS - 1`) and `BS` is the block size.
  379. * \param output_length On success, the number of bytes written to \p output.
  380. * \c 0 on error.
  381. *
  382. * \return #PSA_SUCCESS or an error from a hardware accelerator
  383. */
  384. static psa_status_t psa_cipher_update_ecb(
  385. mbedtls_cipher_context_t *ctx,
  386. const uint8_t *input,
  387. size_t input_length,
  388. uint8_t *output,
  389. size_t *output_length)
  390. {
  391. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  392. size_t block_size = mbedtls_cipher_info_get_block_size(ctx->cipher_info);
  393. size_t internal_output_length = 0;
  394. *output_length = 0;
  395. if (input_length == 0) {
  396. status = PSA_SUCCESS;
  397. goto exit;
  398. }
  399. if (ctx->unprocessed_len > 0) {
  400. /* Fill up to block size, and run the block if there's a full one. */
  401. size_t bytes_to_copy = block_size - ctx->unprocessed_len;
  402. if (input_length < bytes_to_copy) {
  403. bytes_to_copy = input_length;
  404. }
  405. memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]),
  406. input, bytes_to_copy);
  407. input_length -= bytes_to_copy;
  408. input += bytes_to_copy;
  409. ctx->unprocessed_len += bytes_to_copy;
  410. if (ctx->unprocessed_len == block_size) {
  411. status = mbedtls_to_psa_error(
  412. mbedtls_cipher_update(ctx,
  413. ctx->unprocessed_data,
  414. block_size,
  415. output, &internal_output_length));
  416. if (status != PSA_SUCCESS) {
  417. goto exit;
  418. }
  419. output += internal_output_length;
  420. *output_length += internal_output_length;
  421. ctx->unprocessed_len = 0;
  422. }
  423. }
  424. while (input_length >= block_size) {
  425. /* Run all full blocks we have, one by one */
  426. status = mbedtls_to_psa_error(
  427. mbedtls_cipher_update(ctx, input,
  428. block_size,
  429. output, &internal_output_length));
  430. if (status != PSA_SUCCESS) {
  431. goto exit;
  432. }
  433. input_length -= block_size;
  434. input += block_size;
  435. output += internal_output_length;
  436. *output_length += internal_output_length;
  437. }
  438. if (input_length > 0) {
  439. /* Save unprocessed bytes for later processing */
  440. memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]),
  441. input, input_length);
  442. ctx->unprocessed_len += input_length;
  443. }
  444. status = PSA_SUCCESS;
  445. exit:
  446. return status;
  447. }
  448. #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING */
  449. psa_status_t mbedtls_psa_cipher_update(
  450. mbedtls_psa_cipher_operation_t *operation,
  451. const uint8_t *input, size_t input_length,
  452. uint8_t *output, size_t output_size, size_t *output_length)
  453. {
  454. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  455. size_t expected_output_size;
  456. if (!PSA_ALG_IS_STREAM_CIPHER(operation->alg)) {
  457. /* Take the unprocessed partial block left over from previous
  458. * update calls, if any, plus the input to this call. Remove
  459. * the last partial block, if any. You get the data that will be
  460. * output in this call. */
  461. expected_output_size =
  462. (operation->ctx.cipher.unprocessed_len + input_length)
  463. / operation->block_length * operation->block_length;
  464. } else {
  465. expected_output_size = input_length;
  466. }
  467. if (output_size < expected_output_size) {
  468. return PSA_ERROR_BUFFER_TOO_SMALL;
  469. }
  470. #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING)
  471. if (operation->alg == PSA_ALG_ECB_NO_PADDING) {
  472. /* mbedtls_cipher_update has an API inconsistency: it will only
  473. * process a single block at a time in ECB mode. Abstract away that
  474. * inconsistency here to match the PSA API behaviour. */
  475. status = psa_cipher_update_ecb(&operation->ctx.cipher,
  476. input,
  477. input_length,
  478. output,
  479. output_length);
  480. } else
  481. #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING */
  482. if (input_length == 0) {
  483. /* There is no input, nothing to be done */
  484. *output_length = 0;
  485. status = PSA_SUCCESS;
  486. } else {
  487. status = mbedtls_to_psa_error(
  488. mbedtls_cipher_update(&operation->ctx.cipher, input,
  489. input_length, output, output_length));
  490. if (*output_length > output_size) {
  491. return PSA_ERROR_CORRUPTION_DETECTED;
  492. }
  493. }
  494. return status;
  495. }
  496. psa_status_t mbedtls_psa_cipher_finish(
  497. mbedtls_psa_cipher_operation_t *operation,
  498. uint8_t *output, size_t output_size, size_t *output_length)
  499. {
  500. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  501. size_t invalid_padding = 0;
  502. /* We will copy output_size bytes from temp_output_buffer to the
  503. * output buffer. We can't use *output_length to determine how
  504. * much to copy because we must not leak that value through timing
  505. * when doing decryption with unpadding. But the underlying function
  506. * is not guaranteed to write beyond *output_length. To ensure we don't
  507. * leak the former content of the stack to the caller, wipe that
  508. * former content. */
  509. uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH] = { 0 };
  510. if (output_size > sizeof(temp_output_buffer)) {
  511. output_size = sizeof(temp_output_buffer);
  512. }
  513. if (operation->ctx.cipher.unprocessed_len != 0) {
  514. if (operation->alg == PSA_ALG_ECB_NO_PADDING ||
  515. operation->alg == PSA_ALG_CBC_NO_PADDING) {
  516. status = PSA_ERROR_INVALID_ARGUMENT;
  517. goto exit;
  518. }
  519. }
  520. status = mbedtls_to_psa_error(
  521. mbedtls_cipher_finish_padded(&operation->ctx.cipher,
  522. temp_output_buffer,
  523. output_length,
  524. &invalid_padding));
  525. if (status != PSA_SUCCESS) {
  526. goto exit;
  527. }
  528. if (output_size == 0) {
  529. ; /* Nothing to copy. Note that output may be NULL in this case. */
  530. } else {
  531. /* Do not use the value of *output_length to determine how much
  532. * to copy. When decrypting a padded cipher, the output length is
  533. * sensitive, and leaking it could allow a padding oracle attack. */
  534. memcpy(output, temp_output_buffer, output_size);
  535. }
  536. status = mbedtls_ct_error_if_else_0(invalid_padding,
  537. PSA_ERROR_INVALID_PADDING);
  538. mbedtls_ct_condition_t buffer_too_small =
  539. mbedtls_ct_uint_lt(output_size, *output_length);
  540. status = mbedtls_ct_error_if(buffer_too_small,
  541. PSA_ERROR_BUFFER_TOO_SMALL,
  542. status);
  543. exit:
  544. mbedtls_platform_zeroize(temp_output_buffer,
  545. sizeof(temp_output_buffer));
  546. return status;
  547. }
  548. psa_status_t mbedtls_psa_cipher_abort(
  549. mbedtls_psa_cipher_operation_t *operation)
  550. {
  551. /* Sanity check (shouldn't happen: operation->alg should
  552. * always have been initialized to a valid value). */
  553. if (!PSA_ALG_IS_CIPHER(operation->alg)) {
  554. return PSA_ERROR_BAD_STATE;
  555. }
  556. mbedtls_cipher_free(&operation->ctx.cipher);
  557. return PSA_SUCCESS;
  558. }
  559. psa_status_t mbedtls_psa_cipher_encrypt(
  560. const psa_key_attributes_t *attributes,
  561. const uint8_t *key_buffer,
  562. size_t key_buffer_size,
  563. psa_algorithm_t alg,
  564. const uint8_t *iv,
  565. size_t iv_length,
  566. const uint8_t *input,
  567. size_t input_length,
  568. uint8_t *output,
  569. size_t output_size,
  570. size_t *output_length)
  571. {
  572. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  573. mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
  574. size_t update_output_length, finish_output_length;
  575. status = mbedtls_psa_cipher_encrypt_setup(&operation, attributes,
  576. key_buffer, key_buffer_size,
  577. alg);
  578. if (status != PSA_SUCCESS) {
  579. goto exit;
  580. }
  581. if (iv_length > 0) {
  582. status = mbedtls_psa_cipher_set_iv(&operation, iv, iv_length);
  583. if (status != PSA_SUCCESS) {
  584. goto exit;
  585. }
  586. }
  587. status = mbedtls_psa_cipher_update(&operation, input, input_length,
  588. output, output_size,
  589. &update_output_length);
  590. if (status != PSA_SUCCESS) {
  591. goto exit;
  592. }
  593. status = mbedtls_psa_cipher_finish(
  594. &operation,
  595. mbedtls_buffer_offset(output, update_output_length),
  596. output_size - update_output_length, &finish_output_length);
  597. if (status != PSA_SUCCESS) {
  598. goto exit;
  599. }
  600. *output_length = update_output_length + finish_output_length;
  601. exit:
  602. if (status == PSA_SUCCESS) {
  603. status = mbedtls_psa_cipher_abort(&operation);
  604. } else {
  605. mbedtls_psa_cipher_abort(&operation);
  606. }
  607. return status;
  608. }
  609. psa_status_t mbedtls_psa_cipher_decrypt(
  610. const psa_key_attributes_t *attributes,
  611. const uint8_t *key_buffer,
  612. size_t key_buffer_size,
  613. psa_algorithm_t alg,
  614. const uint8_t *input,
  615. size_t input_length,
  616. uint8_t *output,
  617. size_t output_size,
  618. size_t *output_length)
  619. {
  620. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  621. mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT;
  622. size_t olength, accumulated_length;
  623. status = mbedtls_psa_cipher_decrypt_setup(&operation, attributes,
  624. key_buffer, key_buffer_size,
  625. alg);
  626. if (status != PSA_SUCCESS) {
  627. goto exit;
  628. }
  629. if (operation.iv_length > 0) {
  630. status = mbedtls_psa_cipher_set_iv(&operation,
  631. input, operation.iv_length);
  632. if (status != PSA_SUCCESS) {
  633. goto exit;
  634. }
  635. }
  636. status = mbedtls_psa_cipher_update(
  637. &operation,
  638. mbedtls_buffer_offset_const(input, operation.iv_length),
  639. input_length - operation.iv_length,
  640. output, output_size, &olength);
  641. if (status != PSA_SUCCESS) {
  642. goto exit;
  643. }
  644. accumulated_length = olength;
  645. status = mbedtls_psa_cipher_finish(
  646. &operation,
  647. mbedtls_buffer_offset(output, accumulated_length),
  648. output_size - accumulated_length, &olength);
  649. *output_length = accumulated_length + olength;
  650. exit:
  651. /* C99 doesn't allow a declaration to follow a label */;
  652. psa_status_t abort_status = mbedtls_psa_cipher_abort(&operation);
  653. /* Normally abort shouldn't fail unless the operation is in a bad
  654. * state, in which case we'd expect finish to fail with the same error.
  655. * So it doesn't matter much which call's error code we pick when both
  656. * fail. However, in unauthenticated decryption specifically, the
  657. * distinction between PSA_SUCCESS and PSA_ERROR_INVALID_PADDING is
  658. * security-sensitive (risk of a padding oracle attack), so here we
  659. * must not have a code path that depends on the value of status. */
  660. if (abort_status != PSA_SUCCESS) {
  661. status = abort_status;
  662. }
  663. return status;
  664. }
  665. #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */
  666. #endif /* MBEDTLS_PSA_CRYPTO_C */