cipher.c 52 KB

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