cipher.c 52 KB

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