cipher.c 58 KB

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