cipher.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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 "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 */