cipher.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  1. /**
  2. * \file cipher.c
  3. *
  4. * \brief Generic cipher wrapper for Mbed TLS
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. *
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  10. */
  11. #include "common.h"
  12. #if defined(MBEDTLS_CIPHER_C)
  13. #include "mbedtls/cipher.h"
  14. #include "mbedtls/cipher_internal.h"
  15. #include "mbedtls/platform_util.h"
  16. #include "mbedtls/error.h"
  17. #include "mbedtls/constant_time.h"
  18. #include "constant_time_internal.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #if defined(MBEDTLS_CHACHAPOLY_C)
  22. #include "mbedtls/chachapoly.h"
  23. #endif
  24. #if defined(MBEDTLS_GCM_C)
  25. #include "mbedtls/gcm.h"
  26. #endif
  27. #if defined(MBEDTLS_CCM_C)
  28. #include "mbedtls/ccm.h"
  29. #endif
  30. #if defined(MBEDTLS_CHACHA20_C)
  31. #include "mbedtls/chacha20.h"
  32. #endif
  33. #if defined(MBEDTLS_CMAC_C)
  34. #include "mbedtls/cmac.h"
  35. #endif
  36. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  37. #include "psa/crypto.h"
  38. #include "mbedtls/psa_util.h"
  39. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  40. #if defined(MBEDTLS_NIST_KW_C)
  41. #include "mbedtls/nist_kw.h"
  42. #endif
  43. #include "mbedtls/platform.h"
  44. #define CIPHER_VALIDATE_RET(cond) \
  45. MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA)
  46. #define CIPHER_VALIDATE(cond) \
  47. MBEDTLS_INTERNAL_VALIDATE(cond)
  48. static int supported_init = 0;
  49. const int *mbedtls_cipher_list(void)
  50. {
  51. const mbedtls_cipher_definition_t *def;
  52. int *type;
  53. if (!supported_init) {
  54. def = mbedtls_cipher_definitions;
  55. type = mbedtls_cipher_supported;
  56. while (def->type != 0) {
  57. *type++ = (*def++).type;
  58. }
  59. *type = 0;
  60. supported_init = 1;
  61. }
  62. return mbedtls_cipher_supported;
  63. }
  64. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(
  65. const mbedtls_cipher_type_t cipher_type)
  66. {
  67. const mbedtls_cipher_definition_t *def;
  68. for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
  69. if (def->type == cipher_type) {
  70. return def->info;
  71. }
  72. }
  73. return NULL;
  74. }
  75. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(
  76. const char *cipher_name)
  77. {
  78. const mbedtls_cipher_definition_t *def;
  79. if (NULL == cipher_name) {
  80. return NULL;
  81. }
  82. for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
  83. if (!strcmp(def->info->name, cipher_name)) {
  84. return def->info;
  85. }
  86. }
  87. return NULL;
  88. }
  89. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(
  90. const mbedtls_cipher_id_t cipher_id,
  91. int key_bitlen,
  92. const mbedtls_cipher_mode_t mode)
  93. {
  94. const mbedtls_cipher_definition_t *def;
  95. for (def = mbedtls_cipher_definitions; def->info != NULL; def++) {
  96. if (def->info->base->cipher == cipher_id &&
  97. def->info->key_bitlen == (unsigned) key_bitlen &&
  98. def->info->mode == mode) {
  99. return def->info;
  100. }
  101. }
  102. return NULL;
  103. }
  104. void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
  105. {
  106. CIPHER_VALIDATE(ctx != NULL);
  107. memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
  108. }
  109. void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
  110. {
  111. if (ctx == NULL) {
  112. return;
  113. }
  114. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  115. if (ctx->psa_enabled == 1) {
  116. if (ctx->cipher_ctx != NULL) {
  117. mbedtls_cipher_context_psa * const cipher_psa =
  118. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  119. if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) {
  120. /* xxx_free() doesn't allow to return failures. */
  121. (void) psa_destroy_key(cipher_psa->slot);
  122. }
  123. mbedtls_platform_zeroize(cipher_psa, sizeof(*cipher_psa));
  124. mbedtls_free(cipher_psa);
  125. }
  126. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
  127. return;
  128. }
  129. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  130. #if defined(MBEDTLS_CMAC_C)
  131. if (ctx->cmac_ctx) {
  132. mbedtls_platform_zeroize(ctx->cmac_ctx,
  133. sizeof(mbedtls_cmac_context_t));
  134. mbedtls_free(ctx->cmac_ctx);
  135. }
  136. #endif
  137. if (ctx->cipher_ctx) {
  138. ctx->cipher_info->base->ctx_free_func(ctx->cipher_ctx);
  139. }
  140. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
  141. }
  142. int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
  143. const mbedtls_cipher_info_t *cipher_info)
  144. {
  145. CIPHER_VALIDATE_RET(ctx != NULL);
  146. if (cipher_info == NULL) {
  147. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  148. }
  149. memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
  150. if (NULL == (ctx->cipher_ctx = cipher_info->base->ctx_alloc_func())) {
  151. return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
  152. }
  153. ctx->cipher_info = cipher_info;
  154. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  155. /*
  156. * Ignore possible errors caused by a cipher mode that doesn't use padding
  157. */
  158. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  159. (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7);
  160. #else
  161. (void) mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_NONE);
  162. #endif
  163. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  164. return 0;
  165. }
  166. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  167. int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
  168. const mbedtls_cipher_info_t *cipher_info,
  169. size_t taglen)
  170. {
  171. psa_algorithm_t alg;
  172. mbedtls_cipher_context_psa *cipher_psa;
  173. if (NULL == cipher_info || NULL == ctx) {
  174. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  175. }
  176. /* Check that the underlying cipher mode and cipher type are
  177. * supported by the underlying PSA Crypto implementation. */
  178. alg = mbedtls_psa_translate_cipher_mode(cipher_info->mode, taglen);
  179. if (alg == 0) {
  180. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  181. }
  182. if (mbedtls_psa_translate_cipher_type(cipher_info->type) == 0) {
  183. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  184. }
  185. memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
  186. cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa));
  187. if (cipher_psa == NULL) {
  188. return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
  189. }
  190. cipher_psa->alg = alg;
  191. ctx->cipher_ctx = cipher_psa;
  192. ctx->cipher_info = cipher_info;
  193. ctx->psa_enabled = 1;
  194. return 0;
  195. }
  196. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  197. int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
  198. const unsigned char *key,
  199. int key_bitlen,
  200. const mbedtls_operation_t operation)
  201. {
  202. CIPHER_VALIDATE_RET(ctx != NULL);
  203. CIPHER_VALIDATE_RET(key != NULL);
  204. CIPHER_VALIDATE_RET(operation == MBEDTLS_ENCRYPT ||
  205. operation == MBEDTLS_DECRYPT);
  206. if (ctx->cipher_info == NULL) {
  207. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  208. }
  209. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  210. if (ctx->psa_enabled == 1) {
  211. mbedtls_cipher_context_psa * const cipher_psa =
  212. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  213. size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8;
  214. psa_status_t status;
  215. psa_key_type_t key_type;
  216. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  217. /* PSA Crypto API only accepts byte-aligned keys. */
  218. if (key_bitlen % 8 != 0) {
  219. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  220. }
  221. /* Don't allow keys to be set multiple times. */
  222. if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) {
  223. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  224. }
  225. key_type = mbedtls_psa_translate_cipher_type(
  226. ctx->cipher_info->type);
  227. if (key_type == 0) {
  228. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  229. }
  230. psa_set_key_type(&attributes, key_type);
  231. /* Mbed TLS' cipher layer doesn't enforce the mode of operation
  232. * (encrypt vs. decrypt): it is possible to setup a key for encryption
  233. * and use it for AEAD decryption. Until tests relying on this
  234. * are changed, allow any usage in PSA. */
  235. psa_set_key_usage_flags(&attributes,
  236. /* mbedtls_psa_translate_cipher_operation( operation ); */
  237. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
  238. psa_set_key_algorithm(&attributes, cipher_psa->alg);
  239. status = psa_import_key(&attributes, key, key_bytelen,
  240. &cipher_psa->slot);
  241. switch (status) {
  242. case PSA_SUCCESS:
  243. break;
  244. case PSA_ERROR_INSUFFICIENT_MEMORY:
  245. return MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
  246. case PSA_ERROR_NOT_SUPPORTED:
  247. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  248. default:
  249. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  250. }
  251. /* Indicate that we own the key slot and need to
  252. * destroy it in mbedtls_cipher_free(). */
  253. cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED;
  254. ctx->key_bitlen = key_bitlen;
  255. ctx->operation = operation;
  256. return 0;
  257. }
  258. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  259. if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
  260. (int) ctx->cipher_info->key_bitlen != key_bitlen) {
  261. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  262. }
  263. ctx->key_bitlen = key_bitlen;
  264. ctx->operation = operation;
  265. /*
  266. * For OFB, CFB and CTR mode always use the encryption key schedule
  267. */
  268. if (MBEDTLS_ENCRYPT == operation ||
  269. MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  270. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  271. MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
  272. return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
  273. ctx->key_bitlen);
  274. }
  275. if (MBEDTLS_DECRYPT == operation) {
  276. return ctx->cipher_info->base->setkey_dec_func(ctx->cipher_ctx, key,
  277. ctx->key_bitlen);
  278. }
  279. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  280. }
  281. int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
  282. const unsigned char *iv,
  283. size_t iv_len)
  284. {
  285. size_t actual_iv_size;
  286. CIPHER_VALIDATE_RET(ctx != NULL);
  287. CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
  288. if (ctx->cipher_info == NULL) {
  289. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  290. }
  291. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  292. if (ctx->psa_enabled == 1) {
  293. /* While PSA Crypto has an API for multipart
  294. * operations, we currently don't make it
  295. * accessible through the cipher layer. */
  296. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  297. }
  298. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  299. /* avoid buffer overflow in ctx->iv */
  300. if (iv_len > MBEDTLS_MAX_IV_LENGTH) {
  301. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  302. }
  303. if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
  304. actual_iv_size = iv_len;
  305. } else {
  306. actual_iv_size = ctx->cipher_info->iv_size;
  307. /* avoid reading past the end of input buffer */
  308. if (actual_iv_size > iv_len) {
  309. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  310. }
  311. }
  312. #if defined(MBEDTLS_CHACHA20_C)
  313. if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20) {
  314. /* Even though the actual_iv_size is overwritten with a correct value
  315. * of 12 from the cipher info, return an error to indicate that
  316. * the input iv_len is wrong. */
  317. if (iv_len != 12) {
  318. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  319. }
  320. if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
  321. iv,
  322. 0U)) { /* Initial counter value */
  323. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  324. }
  325. }
  326. #if defined(MBEDTLS_CHACHAPOLY_C)
  327. if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
  328. iv_len != 12) {
  329. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  330. }
  331. #endif
  332. #endif
  333. if (actual_iv_size != 0) {
  334. memcpy(ctx->iv, iv, actual_iv_size);
  335. ctx->iv_size = actual_iv_size;
  336. }
  337. return 0;
  338. }
  339. int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
  340. {
  341. CIPHER_VALIDATE_RET(ctx != NULL);
  342. if (ctx->cipher_info == NULL) {
  343. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  344. }
  345. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  346. if (ctx->psa_enabled == 1) {
  347. /* We don't support resetting PSA-based
  348. * cipher contexts, yet. */
  349. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  350. }
  351. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  352. ctx->unprocessed_len = 0;
  353. return 0;
  354. }
  355. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  356. int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
  357. const unsigned char *ad, size_t ad_len)
  358. {
  359. CIPHER_VALIDATE_RET(ctx != NULL);
  360. CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
  361. if (ctx->cipher_info == NULL) {
  362. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  363. }
  364. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  365. if (ctx->psa_enabled == 1) {
  366. /* While PSA Crypto has an API for multipart
  367. * operations, we currently don't make it
  368. * accessible through the cipher layer. */
  369. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  370. }
  371. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  372. #if defined(MBEDTLS_GCM_C)
  373. if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
  374. return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
  375. ctx->iv, ctx->iv_size, ad, ad_len);
  376. }
  377. #endif
  378. #if defined(MBEDTLS_CHACHAPOLY_C)
  379. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
  380. int result;
  381. mbedtls_chachapoly_mode_t mode;
  382. mode = (ctx->operation == MBEDTLS_ENCRYPT)
  383. ? MBEDTLS_CHACHAPOLY_ENCRYPT
  384. : MBEDTLS_CHACHAPOLY_DECRYPT;
  385. result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
  386. ctx->iv,
  387. mode);
  388. if (result != 0) {
  389. return result;
  390. }
  391. return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
  392. ad, ad_len);
  393. }
  394. #endif
  395. return 0;
  396. }
  397. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  398. int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
  399. size_t ilen, unsigned char *output, size_t *olen)
  400. {
  401. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  402. size_t block_size;
  403. CIPHER_VALIDATE_RET(ctx != NULL);
  404. CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
  405. CIPHER_VALIDATE_RET(output != NULL);
  406. CIPHER_VALIDATE_RET(olen != NULL);
  407. if (ctx->cipher_info == NULL) {
  408. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  409. }
  410. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  411. if (ctx->psa_enabled == 1) {
  412. /* While PSA Crypto has an API for multipart
  413. * operations, we currently don't make it
  414. * accessible through the cipher layer. */
  415. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  416. }
  417. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  418. *olen = 0;
  419. block_size = mbedtls_cipher_get_block_size(ctx);
  420. if (0 == block_size) {
  421. return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT;
  422. }
  423. if (ctx->cipher_info->mode == MBEDTLS_MODE_ECB) {
  424. if (ilen != block_size) {
  425. return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  426. }
  427. *olen = ilen;
  428. if (0 != (ret = ctx->cipher_info->base->ecb_func(ctx->cipher_ctx,
  429. ctx->operation, input, output))) {
  430. return ret;
  431. }
  432. return 0;
  433. }
  434. #if defined(MBEDTLS_GCM_C)
  435. if (ctx->cipher_info->mode == MBEDTLS_MODE_GCM) {
  436. *olen = ilen;
  437. return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
  438. output);
  439. }
  440. #endif
  441. #if defined(MBEDTLS_CHACHAPOLY_C)
  442. if (ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
  443. *olen = ilen;
  444. return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
  445. ilen, input, output);
  446. }
  447. #endif
  448. if (input == output &&
  449. (ctx->unprocessed_len != 0 || ilen % block_size)) {
  450. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  451. }
  452. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  453. if (ctx->cipher_info->mode == MBEDTLS_MODE_CBC) {
  454. size_t copy_len = 0;
  455. /*
  456. * If there is not enough data for a full block, cache it.
  457. */
  458. if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
  459. ilen <= block_size - ctx->unprocessed_len) ||
  460. (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
  461. ilen < block_size - ctx->unprocessed_len) ||
  462. (ctx->operation == MBEDTLS_ENCRYPT &&
  463. ilen < block_size - ctx->unprocessed_len)) {
  464. memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
  465. ilen);
  466. ctx->unprocessed_len += ilen;
  467. return 0;
  468. }
  469. /*
  470. * Process cached data first
  471. */
  472. if (0 != ctx->unprocessed_len) {
  473. copy_len = block_size - ctx->unprocessed_len;
  474. memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
  475. copy_len);
  476. if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
  477. ctx->operation, block_size, ctx->iv,
  478. ctx->unprocessed_data, output))) {
  479. return ret;
  480. }
  481. *olen += block_size;
  482. output += block_size;
  483. ctx->unprocessed_len = 0;
  484. input += copy_len;
  485. ilen -= copy_len;
  486. }
  487. /*
  488. * Cache final, incomplete block
  489. */
  490. if (0 != ilen) {
  491. /* Encryption: only cache partial blocks
  492. * Decryption w/ padding: always keep at least one whole block
  493. * Decryption w/o padding: only cache partial blocks
  494. */
  495. copy_len = ilen % block_size;
  496. if (copy_len == 0 &&
  497. ctx->operation == MBEDTLS_DECRYPT &&
  498. NULL != ctx->add_padding) {
  499. copy_len = block_size;
  500. }
  501. memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
  502. copy_len);
  503. ctx->unprocessed_len += copy_len;
  504. ilen -= copy_len;
  505. }
  506. /*
  507. * Process remaining full blocks
  508. */
  509. if (ilen) {
  510. if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
  511. ctx->operation, ilen, ctx->iv, input,
  512. output))) {
  513. return ret;
  514. }
  515. *olen += ilen;
  516. }
  517. return 0;
  518. }
  519. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  520. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  521. if (ctx->cipher_info->mode == MBEDTLS_MODE_CFB) {
  522. if (0 != (ret = ctx->cipher_info->base->cfb_func(ctx->cipher_ctx,
  523. ctx->operation, ilen,
  524. &ctx->unprocessed_len, ctx->iv,
  525. input, output))) {
  526. return ret;
  527. }
  528. *olen = ilen;
  529. return 0;
  530. }
  531. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  532. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  533. if (ctx->cipher_info->mode == MBEDTLS_MODE_OFB) {
  534. if (0 != (ret = ctx->cipher_info->base->ofb_func(ctx->cipher_ctx,
  535. ilen, &ctx->unprocessed_len, ctx->iv,
  536. input, output))) {
  537. return ret;
  538. }
  539. *olen = ilen;
  540. return 0;
  541. }
  542. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  543. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  544. if (ctx->cipher_info->mode == MBEDTLS_MODE_CTR) {
  545. if (0 != (ret = ctx->cipher_info->base->ctr_func(ctx->cipher_ctx,
  546. ilen, &ctx->unprocessed_len, ctx->iv,
  547. ctx->unprocessed_data, input, output))) {
  548. return ret;
  549. }
  550. *olen = ilen;
  551. return 0;
  552. }
  553. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  554. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  555. if (ctx->cipher_info->mode == MBEDTLS_MODE_XTS) {
  556. if (ctx->unprocessed_len > 0) {
  557. /* We can only process an entire data unit at a time. */
  558. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  559. }
  560. ret = ctx->cipher_info->base->xts_func(ctx->cipher_ctx,
  561. ctx->operation, ilen, ctx->iv, input, output);
  562. if (ret != 0) {
  563. return ret;
  564. }
  565. *olen = ilen;
  566. return 0;
  567. }
  568. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  569. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  570. if (ctx->cipher_info->mode == MBEDTLS_MODE_STREAM) {
  571. if (0 != (ret = ctx->cipher_info->base->stream_func(ctx->cipher_ctx,
  572. ilen, input, output))) {
  573. return ret;
  574. }
  575. *olen = ilen;
  576. return 0;
  577. }
  578. #endif /* MBEDTLS_CIPHER_MODE_STREAM */
  579. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  580. }
  581. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  582. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  583. /*
  584. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  585. */
  586. static void add_pkcs_padding(unsigned char *output, size_t output_len,
  587. size_t data_len)
  588. {
  589. size_t padding_len = output_len - data_len;
  590. unsigned char i;
  591. for (i = 0; i < padding_len; i++) {
  592. output[data_len + i] = (unsigned char) padding_len;
  593. }
  594. }
  595. static int get_pkcs_padding(unsigned char *input, size_t input_len,
  596. size_t *data_len)
  597. {
  598. size_t i, pad_idx;
  599. unsigned char padding_len, bad = 0;
  600. if (NULL == input || NULL == data_len) {
  601. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  602. }
  603. padding_len = input[input_len - 1];
  604. *data_len = input_len - padding_len;
  605. /* Avoid logical || since it results in a branch */
  606. bad |= ~mbedtls_ct_size_mask_ge(input_len, padding_len);
  607. bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
  608. /* The number of bytes checked must be independent of padding_len,
  609. * so pick input_len, which is usually 8 or 16 (one block) */
  610. pad_idx = input_len - padding_len;
  611. for (i = 0; i < input_len; i++) {
  612. size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
  613. bad |= (input[i] ^ padding_len) & mask;
  614. }
  615. return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
  616. }
  617. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  618. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  619. /*
  620. * One and zeros padding: fill with 80 00 ... 00
  621. */
  622. static void add_one_and_zeros_padding(unsigned char *output,
  623. size_t output_len, size_t data_len)
  624. {
  625. size_t padding_len = output_len - data_len;
  626. unsigned char i = 0;
  627. output[data_len] = 0x80;
  628. for (i = 1; i < padding_len; i++) {
  629. output[data_len + i] = 0x00;
  630. }
  631. }
  632. static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
  633. size_t *data_len)
  634. {
  635. unsigned int bad = 1;
  636. if (NULL == input || NULL == data_len) {
  637. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  638. }
  639. *data_len = 0;
  640. size_t in_padding = ~0;
  641. for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) {
  642. size_t is_nonzero = mbedtls_ct_uint_mask(input[i]);
  643. size_t hit_first_nonzero = is_nonzero & in_padding;
  644. *data_len = (*data_len & ~hit_first_nonzero) | ((size_t) i & hit_first_nonzero);
  645. bad = mbedtls_ct_uint_if((unsigned int) hit_first_nonzero,
  646. !mbedtls_ct_size_bool_eq(input[i], 0x80), bad);
  647. in_padding = in_padding & ~is_nonzero;
  648. }
  649. return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
  650. }
  651. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  652. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  653. /*
  654. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  655. */
  656. static void add_zeros_and_len_padding(unsigned char *output,
  657. size_t output_len, size_t data_len)
  658. {
  659. size_t padding_len = output_len - data_len;
  660. unsigned char i = 0;
  661. for (i = 1; i < padding_len; i++) {
  662. output[data_len + i - 1] = 0x00;
  663. }
  664. output[output_len - 1] = (unsigned char) padding_len;
  665. }
  666. static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
  667. size_t *data_len)
  668. {
  669. size_t i, pad_idx;
  670. unsigned char padding_len, bad = 0;
  671. if (NULL == input || NULL == data_len) {
  672. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  673. }
  674. padding_len = input[input_len - 1];
  675. *data_len = input_len - padding_len;
  676. /* Avoid logical || since it results in a branch */
  677. bad |= mbedtls_ct_size_mask_ge(padding_len, input_len + 1);
  678. bad |= mbedtls_ct_size_bool_eq(padding_len, 0);
  679. /* The number of bytes checked must be independent of padding_len */
  680. pad_idx = input_len - padding_len;
  681. for (i = 0; i < input_len - 1; i++) {
  682. size_t mask = mbedtls_ct_size_mask_ge(i, pad_idx);
  683. bad |= input[i] & mask;
  684. }
  685. return -(int) mbedtls_ct_uint_if(bad, -MBEDTLS_ERR_CIPHER_INVALID_PADDING, 0);
  686. }
  687. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  688. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  689. /*
  690. * Zero padding: fill with 00 ... 00
  691. */
  692. static void add_zeros_padding(unsigned char *output,
  693. size_t output_len, size_t data_len)
  694. {
  695. size_t i;
  696. for (i = data_len; i < output_len; i++) {
  697. output[i] = 0x00;
  698. }
  699. }
  700. static int get_zeros_padding(unsigned char *input, size_t input_len,
  701. size_t *data_len)
  702. {
  703. size_t i;
  704. unsigned char done = 0, prev_done;
  705. if (NULL == input || NULL == data_len) {
  706. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  707. }
  708. *data_len = 0;
  709. for (i = input_len; i > 0; i--) {
  710. prev_done = done;
  711. done |= !mbedtls_ct_size_bool_eq(input[i-1], 0);
  712. size_t mask = mbedtls_ct_size_mask(done ^ prev_done);
  713. *data_len |= i & mask;
  714. }
  715. return 0;
  716. }
  717. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  718. /*
  719. * No padding: don't pad :)
  720. *
  721. * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  722. * but a trivial get_padding function
  723. */
  724. static int get_no_padding(unsigned char *input, size_t input_len,
  725. size_t *data_len)
  726. {
  727. if (NULL == input || NULL == data_len) {
  728. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  729. }
  730. *data_len = input_len;
  731. return 0;
  732. }
  733. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  734. int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
  735. unsigned char *output, size_t *olen)
  736. {
  737. CIPHER_VALIDATE_RET(ctx != NULL);
  738. CIPHER_VALIDATE_RET(output != NULL);
  739. CIPHER_VALIDATE_RET(olen != NULL);
  740. if (ctx->cipher_info == NULL) {
  741. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  742. }
  743. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  744. if (ctx->psa_enabled == 1) {
  745. /* While PSA Crypto has an API for multipart
  746. * operations, we currently don't make it
  747. * accessible through the cipher layer. */
  748. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  749. }
  750. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  751. *olen = 0;
  752. if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  753. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  754. MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
  755. MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
  756. MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
  757. MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
  758. return 0;
  759. }
  760. if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
  761. (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
  762. return 0;
  763. }
  764. if (MBEDTLS_MODE_ECB == ctx->cipher_info->mode) {
  765. if (ctx->unprocessed_len != 0) {
  766. return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  767. }
  768. return 0;
  769. }
  770. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  771. if (MBEDTLS_MODE_CBC == ctx->cipher_info->mode) {
  772. int ret = 0;
  773. if (MBEDTLS_ENCRYPT == ctx->operation) {
  774. /* check for 'no padding' mode */
  775. if (NULL == ctx->add_padding) {
  776. if (0 != ctx->unprocessed_len) {
  777. return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  778. }
  779. return 0;
  780. }
  781. ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
  782. ctx->unprocessed_len);
  783. } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
  784. /*
  785. * For decrypt operations, expect a full block,
  786. * or an empty block if no padding
  787. */
  788. if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
  789. return 0;
  790. }
  791. return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
  792. }
  793. /* cipher block */
  794. if (0 != (ret = ctx->cipher_info->base->cbc_func(ctx->cipher_ctx,
  795. ctx->operation,
  796. mbedtls_cipher_get_block_size(ctx),
  797. ctx->iv,
  798. ctx->unprocessed_data, output))) {
  799. return ret;
  800. }
  801. /* Set output size for decryption */
  802. if (MBEDTLS_DECRYPT == ctx->operation) {
  803. return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
  804. olen);
  805. }
  806. /* Set output size for encryption */
  807. *olen = mbedtls_cipher_get_block_size(ctx);
  808. return 0;
  809. }
  810. #else
  811. ((void) output);
  812. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  813. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  814. }
  815. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  816. int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
  817. mbedtls_cipher_padding_t mode)
  818. {
  819. CIPHER_VALIDATE_RET(ctx != NULL);
  820. if (NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
  821. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  822. }
  823. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  824. if (ctx->psa_enabled == 1) {
  825. /* While PSA Crypto knows about CBC padding
  826. * schemes, we currently don't make them
  827. * accessible through the cipher layer. */
  828. if (mode != MBEDTLS_PADDING_NONE) {
  829. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  830. }
  831. return 0;
  832. }
  833. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  834. switch (mode) {
  835. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  836. case MBEDTLS_PADDING_PKCS7:
  837. ctx->add_padding = add_pkcs_padding;
  838. ctx->get_padding = get_pkcs_padding;
  839. break;
  840. #endif
  841. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  842. case MBEDTLS_PADDING_ONE_AND_ZEROS:
  843. ctx->add_padding = add_one_and_zeros_padding;
  844. ctx->get_padding = get_one_and_zeros_padding;
  845. break;
  846. #endif
  847. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  848. case MBEDTLS_PADDING_ZEROS_AND_LEN:
  849. ctx->add_padding = add_zeros_and_len_padding;
  850. ctx->get_padding = get_zeros_and_len_padding;
  851. break;
  852. #endif
  853. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  854. case MBEDTLS_PADDING_ZEROS:
  855. ctx->add_padding = add_zeros_padding;
  856. ctx->get_padding = get_zeros_padding;
  857. break;
  858. #endif
  859. case MBEDTLS_PADDING_NONE:
  860. ctx->add_padding = NULL;
  861. ctx->get_padding = get_no_padding;
  862. break;
  863. default:
  864. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  865. }
  866. return 0;
  867. }
  868. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  869. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  870. int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
  871. unsigned char *tag, size_t tag_len)
  872. {
  873. CIPHER_VALIDATE_RET(ctx != NULL);
  874. CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
  875. if (ctx->cipher_info == NULL) {
  876. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  877. }
  878. if (MBEDTLS_ENCRYPT != ctx->operation) {
  879. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  880. }
  881. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  882. if (ctx->psa_enabled == 1) {
  883. /* While PSA Crypto has an API for multipart
  884. * operations, we currently don't make it
  885. * accessible through the cipher layer. */
  886. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  887. }
  888. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  889. #if defined(MBEDTLS_GCM_C)
  890. if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
  891. return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
  892. tag, tag_len);
  893. }
  894. #endif
  895. #if defined(MBEDTLS_CHACHAPOLY_C)
  896. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
  897. /* Don't allow truncated MAC for Poly1305 */
  898. if (tag_len != 16U) {
  899. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  900. }
  901. return mbedtls_chachapoly_finish(
  902. (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
  903. }
  904. #endif
  905. return 0;
  906. }
  907. int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
  908. const unsigned char *tag, size_t tag_len)
  909. {
  910. unsigned char check_tag[16];
  911. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  912. CIPHER_VALIDATE_RET(ctx != NULL);
  913. CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
  914. if (ctx->cipher_info == NULL) {
  915. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  916. }
  917. if (MBEDTLS_DECRYPT != ctx->operation) {
  918. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  919. }
  920. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  921. if (ctx->psa_enabled == 1) {
  922. /* While PSA Crypto has an API for multipart
  923. * operations, we currently don't make it
  924. * accessible through the cipher layer. */
  925. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  926. }
  927. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  928. /* Status to return on a non-authenticated algorithm. It would make sense
  929. * to return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT or perhaps
  930. * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, but at the time I write this our
  931. * unit tests assume 0. */
  932. ret = 0;
  933. #if defined(MBEDTLS_GCM_C)
  934. if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
  935. if (tag_len > sizeof(check_tag)) {
  936. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  937. }
  938. if (0 != (ret = mbedtls_gcm_finish(
  939. (mbedtls_gcm_context *) ctx->cipher_ctx,
  940. check_tag, tag_len))) {
  941. return ret;
  942. }
  943. /* Check the tag in "constant-time" */
  944. if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
  945. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  946. goto exit;
  947. }
  948. }
  949. #endif /* MBEDTLS_GCM_C */
  950. #if defined(MBEDTLS_CHACHAPOLY_C)
  951. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
  952. /* Don't allow truncated MAC for Poly1305 */
  953. if (tag_len != sizeof(check_tag)) {
  954. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  955. }
  956. ret = mbedtls_chachapoly_finish(
  957. (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
  958. if (ret != 0) {
  959. return ret;
  960. }
  961. /* Check the tag in "constant-time" */
  962. if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) {
  963. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  964. goto exit;
  965. }
  966. }
  967. #endif /* MBEDTLS_CHACHAPOLY_C */
  968. exit:
  969. mbedtls_platform_zeroize(check_tag, tag_len);
  970. return ret;
  971. }
  972. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  973. /*
  974. * Packet-oriented wrapper for non-AEAD modes
  975. */
  976. int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
  977. const unsigned char *iv, size_t iv_len,
  978. const unsigned char *input, size_t ilen,
  979. unsigned char *output, size_t *olen)
  980. {
  981. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  982. size_t finish_olen;
  983. CIPHER_VALIDATE_RET(ctx != NULL);
  984. CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
  985. CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
  986. CIPHER_VALIDATE_RET(output != NULL);
  987. CIPHER_VALIDATE_RET(olen != NULL);
  988. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  989. if (ctx->psa_enabled == 1) {
  990. /* As in the non-PSA case, we don't check that
  991. * a key has been set. If not, the key slot will
  992. * still be in its default state of 0, which is
  993. * guaranteed to be invalid, hence the PSA-call
  994. * below will gracefully fail. */
  995. mbedtls_cipher_context_psa * const cipher_psa =
  996. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  997. psa_status_t status;
  998. psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
  999. size_t part_len;
  1000. if (ctx->operation == MBEDTLS_DECRYPT) {
  1001. status = psa_cipher_decrypt_setup(&cipher_op,
  1002. cipher_psa->slot,
  1003. cipher_psa->alg);
  1004. } else if (ctx->operation == MBEDTLS_ENCRYPT) {
  1005. status = psa_cipher_encrypt_setup(&cipher_op,
  1006. cipher_psa->slot,
  1007. cipher_psa->alg);
  1008. } else {
  1009. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1010. }
  1011. /* In the following, we can immediately return on an error,
  1012. * because the PSA Crypto API guarantees that cipher operations
  1013. * are terminated by unsuccessful calls to psa_cipher_update(),
  1014. * and by any call to psa_cipher_finish(). */
  1015. if (status != PSA_SUCCESS) {
  1016. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  1017. }
  1018. if (ctx->cipher_info->mode != MBEDTLS_MODE_ECB) {
  1019. status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
  1020. if (status != PSA_SUCCESS) {
  1021. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  1022. }
  1023. }
  1024. status = psa_cipher_update(&cipher_op,
  1025. input, ilen,
  1026. output, ilen, olen);
  1027. if (status != PSA_SUCCESS) {
  1028. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  1029. }
  1030. status = psa_cipher_finish(&cipher_op,
  1031. output + *olen, ilen - *olen,
  1032. &part_len);
  1033. if (status != PSA_SUCCESS) {
  1034. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  1035. }
  1036. *olen += part_len;
  1037. return 0;
  1038. }
  1039. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1040. if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
  1041. return ret;
  1042. }
  1043. if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
  1044. return ret;
  1045. }
  1046. if ((ret = mbedtls_cipher_update(ctx, input, ilen,
  1047. output, olen)) != 0) {
  1048. return ret;
  1049. }
  1050. if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
  1051. &finish_olen)) != 0) {
  1052. return ret;
  1053. }
  1054. *olen += finish_olen;
  1055. return 0;
  1056. }
  1057. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  1058. /*
  1059. * Packet-oriented encryption for AEAD modes: internal function shared by
  1060. * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
  1061. */
  1062. static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
  1063. const unsigned char *iv, size_t iv_len,
  1064. const unsigned char *ad, size_t ad_len,
  1065. const unsigned char *input, size_t ilen,
  1066. unsigned char *output, size_t *olen,
  1067. unsigned char *tag, size_t tag_len)
  1068. {
  1069. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1070. if (ctx->psa_enabled == 1) {
  1071. /* As in the non-PSA case, we don't check that
  1072. * a key has been set. If not, the key slot will
  1073. * still be in its default state of 0, which is
  1074. * guaranteed to be invalid, hence the PSA-call
  1075. * below will gracefully fail. */
  1076. mbedtls_cipher_context_psa * const cipher_psa =
  1077. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  1078. psa_status_t status;
  1079. /* PSA Crypto API always writes the authentication tag
  1080. * at the end of the encrypted message. */
  1081. if (output == NULL || tag != output + ilen) {
  1082. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  1083. }
  1084. status = psa_aead_encrypt(cipher_psa->slot,
  1085. cipher_psa->alg,
  1086. iv, iv_len,
  1087. ad, ad_len,
  1088. input, ilen,
  1089. output, ilen + tag_len, olen);
  1090. if (status != PSA_SUCCESS) {
  1091. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  1092. }
  1093. *olen -= tag_len;
  1094. return 0;
  1095. }
  1096. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1097. #if defined(MBEDTLS_GCM_C)
  1098. if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
  1099. *olen = ilen;
  1100. return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
  1101. ilen, iv, iv_len, ad, ad_len,
  1102. input, output, tag_len, tag);
  1103. }
  1104. #endif /* MBEDTLS_GCM_C */
  1105. #if defined(MBEDTLS_CCM_C)
  1106. if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
  1107. *olen = ilen;
  1108. return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
  1109. iv, iv_len, ad, ad_len, input, output,
  1110. tag, tag_len);
  1111. }
  1112. #endif /* MBEDTLS_CCM_C */
  1113. #if defined(MBEDTLS_CHACHAPOLY_C)
  1114. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
  1115. /* ChachaPoly has fixed length nonce and MAC (tag) */
  1116. if ((iv_len != ctx->cipher_info->iv_size) ||
  1117. (tag_len != 16U)) {
  1118. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1119. }
  1120. *olen = ilen;
  1121. return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
  1122. ilen, iv, ad, ad_len, input, output, tag);
  1123. }
  1124. #endif /* MBEDTLS_CHACHAPOLY_C */
  1125. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  1126. }
  1127. /*
  1128. * Packet-oriented encryption for AEAD modes: internal function shared by
  1129. * mbedtls_cipher_auth_encrypt() and mbedtls_cipher_auth_encrypt_ext().
  1130. */
  1131. static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
  1132. const unsigned char *iv, size_t iv_len,
  1133. const unsigned char *ad, size_t ad_len,
  1134. const unsigned char *input, size_t ilen,
  1135. unsigned char *output, size_t *olen,
  1136. const unsigned char *tag, size_t tag_len)
  1137. {
  1138. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1139. if (ctx->psa_enabled == 1) {
  1140. /* As in the non-PSA case, we don't check that
  1141. * a key has been set. If not, the key slot will
  1142. * still be in its default state of 0, which is
  1143. * guaranteed to be invalid, hence the PSA-call
  1144. * below will gracefully fail. */
  1145. mbedtls_cipher_context_psa * const cipher_psa =
  1146. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  1147. psa_status_t status;
  1148. /* PSA Crypto API always writes the authentication tag
  1149. * at the end of the encrypted message. */
  1150. if (input == NULL || tag != input + ilen) {
  1151. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  1152. }
  1153. status = psa_aead_decrypt(cipher_psa->slot,
  1154. cipher_psa->alg,
  1155. iv, iv_len,
  1156. ad, ad_len,
  1157. input, ilen + tag_len,
  1158. output, ilen, olen);
  1159. if (status == PSA_ERROR_INVALID_SIGNATURE) {
  1160. return MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1161. } else if (status != PSA_SUCCESS) {
  1162. return MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED;
  1163. }
  1164. return 0;
  1165. }
  1166. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1167. #if defined(MBEDTLS_GCM_C)
  1168. if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
  1169. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1170. *olen = ilen;
  1171. ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
  1172. iv, iv_len, ad, ad_len,
  1173. tag, tag_len, input, output);
  1174. if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) {
  1175. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1176. }
  1177. return ret;
  1178. }
  1179. #endif /* MBEDTLS_GCM_C */
  1180. #if defined(MBEDTLS_CCM_C)
  1181. if (MBEDTLS_MODE_CCM == ctx->cipher_info->mode) {
  1182. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1183. *olen = ilen;
  1184. ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
  1185. iv, iv_len, ad, ad_len,
  1186. input, output, tag, tag_len);
  1187. if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) {
  1188. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1189. }
  1190. return ret;
  1191. }
  1192. #endif /* MBEDTLS_CCM_C */
  1193. #if defined(MBEDTLS_CHACHAPOLY_C)
  1194. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
  1195. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1196. /* ChachaPoly has fixed length nonce and MAC (tag) */
  1197. if ((iv_len != ctx->cipher_info->iv_size) ||
  1198. (tag_len != 16U)) {
  1199. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1200. }
  1201. *olen = ilen;
  1202. ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
  1203. iv, ad, ad_len, tag, input, output);
  1204. if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
  1205. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1206. }
  1207. return ret;
  1208. }
  1209. #endif /* MBEDTLS_CHACHAPOLY_C */
  1210. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  1211. }
  1212. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  1213. /*
  1214. * Packet-oriented encryption for AEAD modes: public legacy function.
  1215. */
  1216. int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
  1217. const unsigned char *iv, size_t iv_len,
  1218. const unsigned char *ad, size_t ad_len,
  1219. const unsigned char *input, size_t ilen,
  1220. unsigned char *output, size_t *olen,
  1221. unsigned char *tag, size_t tag_len)
  1222. {
  1223. CIPHER_VALIDATE_RET(ctx != NULL);
  1224. CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
  1225. CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
  1226. CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
  1227. CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
  1228. CIPHER_VALIDATE_RET(olen != NULL);
  1229. CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
  1230. return mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
  1231. input, ilen, output, olen,
  1232. tag, tag_len);
  1233. }
  1234. /*
  1235. * Packet-oriented decryption for AEAD modes: public legacy function.
  1236. */
  1237. int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
  1238. const unsigned char *iv, size_t iv_len,
  1239. const unsigned char *ad, size_t ad_len,
  1240. const unsigned char *input, size_t ilen,
  1241. unsigned char *output, size_t *olen,
  1242. const unsigned char *tag, size_t tag_len)
  1243. {
  1244. CIPHER_VALIDATE_RET(ctx != NULL);
  1245. CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
  1246. CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
  1247. CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
  1248. CIPHER_VALIDATE_RET(ilen == 0 || output != NULL);
  1249. CIPHER_VALIDATE_RET(olen != NULL);
  1250. CIPHER_VALIDATE_RET(tag_len == 0 || tag != NULL);
  1251. return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
  1252. input, ilen, output, olen,
  1253. tag, tag_len);
  1254. }
  1255. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  1256. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  1257. #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
  1258. /*
  1259. * Packet-oriented encryption for AEAD/NIST_KW: public function.
  1260. */
  1261. int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
  1262. const unsigned char *iv, size_t iv_len,
  1263. const unsigned char *ad, size_t ad_len,
  1264. const unsigned char *input, size_t ilen,
  1265. unsigned char *output, size_t output_len,
  1266. size_t *olen, size_t tag_len)
  1267. {
  1268. CIPHER_VALIDATE_RET(ctx != NULL);
  1269. CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
  1270. CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
  1271. CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
  1272. CIPHER_VALIDATE_RET(output != NULL);
  1273. CIPHER_VALIDATE_RET(olen != NULL);
  1274. #if defined(MBEDTLS_NIST_KW_C)
  1275. if (
  1276. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1277. ctx->psa_enabled == 0 &&
  1278. #endif
  1279. (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
  1280. MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
  1281. mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
  1282. MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
  1283. /* There is no iv, tag or ad associated with KW and KWP,
  1284. * so these length should be 0 as documented. */
  1285. if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
  1286. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1287. }
  1288. (void) iv;
  1289. (void) ad;
  1290. return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
  1291. output, olen, output_len);
  1292. }
  1293. #endif /* MBEDTLS_NIST_KW_C */
  1294. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  1295. /* AEAD case: check length before passing on to shared function */
  1296. if (output_len < ilen + tag_len) {
  1297. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1298. }
  1299. int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
  1300. input, ilen, output, olen,
  1301. output + ilen, tag_len);
  1302. *olen += tag_len;
  1303. return ret;
  1304. #else
  1305. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  1306. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  1307. }
  1308. /*
  1309. * Packet-oriented decryption for AEAD/NIST_KW: public function.
  1310. */
  1311. int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
  1312. const unsigned char *iv, size_t iv_len,
  1313. const unsigned char *ad, size_t ad_len,
  1314. const unsigned char *input, size_t ilen,
  1315. unsigned char *output, size_t output_len,
  1316. size_t *olen, size_t tag_len)
  1317. {
  1318. CIPHER_VALIDATE_RET(ctx != NULL);
  1319. CIPHER_VALIDATE_RET(iv_len == 0 || iv != NULL);
  1320. CIPHER_VALIDATE_RET(ad_len == 0 || ad != NULL);
  1321. CIPHER_VALIDATE_RET(ilen == 0 || input != NULL);
  1322. CIPHER_VALIDATE_RET(output_len == 0 || output != NULL);
  1323. CIPHER_VALIDATE_RET(olen != NULL);
  1324. #if defined(MBEDTLS_NIST_KW_C)
  1325. if (
  1326. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1327. ctx->psa_enabled == 0 &&
  1328. #endif
  1329. (MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
  1330. MBEDTLS_MODE_KWP == ctx->cipher_info->mode)) {
  1331. mbedtls_nist_kw_mode_t mode = (MBEDTLS_MODE_KW == ctx->cipher_info->mode) ?
  1332. MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
  1333. /* There is no iv, tag or ad associated with KW and KWP,
  1334. * so these length should be 0 as documented. */
  1335. if (iv_len != 0 || tag_len != 0 || ad_len != 0) {
  1336. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1337. }
  1338. (void) iv;
  1339. (void) ad;
  1340. return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
  1341. output, olen, output_len);
  1342. }
  1343. #endif /* MBEDTLS_NIST_KW_C */
  1344. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  1345. /* AEAD case: check length before passing on to shared function */
  1346. if (ilen < tag_len || output_len < ilen - tag_len) {
  1347. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  1348. }
  1349. return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,
  1350. input, ilen - tag_len, output, olen,
  1351. input + ilen - tag_len, tag_len);
  1352. #else
  1353. return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  1354. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  1355. }
  1356. #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
  1357. #endif /* MBEDTLS_CIPHER_C */