cipher.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. /**
  2. * \file cipher.c
  3. *
  4. * \brief Generic cipher wrapper for mbed TLS
  5. *
  6. * \author Adriaan de Jong <dejong@fox-it.com>
  7. *
  8. * Copyright The Mbed TLS Contributors
  9. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  10. *
  11. * This file is provided under the Apache License 2.0, or the
  12. * GNU General Public License v2.0 or later.
  13. *
  14. * **********
  15. * Apache License 2.0:
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *
  29. * **********
  30. *
  31. * **********
  32. * GNU General Public License v2.0 or later:
  33. *
  34. * This program is free software; you can redistribute it and/or modify
  35. * it under the terms of the GNU General Public License as published by
  36. * the Free Software Foundation; either version 2 of the License, or
  37. * (at your option) any later version.
  38. *
  39. * This program is distributed in the hope that it will be useful,
  40. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42. * GNU General Public License for more details.
  43. *
  44. * You should have received a copy of the GNU General Public License along
  45. * with this program; if not, write to the Free Software Foundation, Inc.,
  46. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  47. *
  48. * **********
  49. */
  50. #if !defined(MBEDTLS_CONFIG_FILE)
  51. #include "mbedtls/config.h"
  52. #else
  53. #include MBEDTLS_CONFIG_FILE
  54. #endif
  55. #if defined(MBEDTLS_CIPHER_C)
  56. #include "mbedtls/cipher.h"
  57. #include "mbedtls/cipher_internal.h"
  58. #include "mbedtls/platform_util.h"
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #if defined(MBEDTLS_CHACHAPOLY_C)
  62. #include "mbedtls/chachapoly.h"
  63. #endif
  64. #if defined(MBEDTLS_GCM_C)
  65. #include "mbedtls/gcm.h"
  66. #endif
  67. #if defined(MBEDTLS_CCM_C)
  68. #include "mbedtls/ccm.h"
  69. #endif
  70. #if defined(MBEDTLS_CHACHA20_C)
  71. #include "mbedtls/chacha20.h"
  72. #endif
  73. #if defined(MBEDTLS_CMAC_C)
  74. #include "mbedtls/cmac.h"
  75. #endif
  76. #if defined(MBEDTLS_PLATFORM_C)
  77. #include "mbedtls/platform.h"
  78. #else
  79. #define mbedtls_calloc calloc
  80. #define mbedtls_free free
  81. #endif
  82. #define CIPHER_VALIDATE_RET( cond ) \
  83. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA )
  84. #define CIPHER_VALIDATE( cond ) \
  85. MBEDTLS_INTERNAL_VALIDATE( cond )
  86. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  87. /* Compare the contents of two buffers in constant time.
  88. * Returns 0 if the contents are bitwise identical, otherwise returns
  89. * a non-zero value.
  90. * This is currently only used by GCM and ChaCha20+Poly1305.
  91. */
  92. static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t len )
  93. {
  94. const unsigned char *p1 = (const unsigned char*) v1;
  95. const unsigned char *p2 = (const unsigned char*) v2;
  96. size_t i;
  97. unsigned char diff;
  98. for( diff = 0, i = 0; i < len; i++ )
  99. diff |= p1[i] ^ p2[i];
  100. return( (int)diff );
  101. }
  102. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  103. static int supported_init = 0;
  104. const int *mbedtls_cipher_list( void )
  105. {
  106. const mbedtls_cipher_definition_t *def;
  107. int *type;
  108. if( ! supported_init )
  109. {
  110. def = mbedtls_cipher_definitions;
  111. type = mbedtls_cipher_supported;
  112. while( def->type != 0 )
  113. *type++ = (*def++).type;
  114. *type = 0;
  115. supported_init = 1;
  116. }
  117. return( mbedtls_cipher_supported );
  118. }
  119. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
  120. {
  121. const mbedtls_cipher_definition_t *def;
  122. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  123. if( def->type == cipher_type )
  124. return( def->info );
  125. return( NULL );
  126. }
  127. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
  128. {
  129. const mbedtls_cipher_definition_t *def;
  130. if( NULL == cipher_name )
  131. return( NULL );
  132. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  133. if( ! strcmp( def->info->name, cipher_name ) )
  134. return( def->info );
  135. return( NULL );
  136. }
  137. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
  138. int key_bitlen,
  139. const mbedtls_cipher_mode_t mode )
  140. {
  141. const mbedtls_cipher_definition_t *def;
  142. for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
  143. if( def->info->base->cipher == cipher_id &&
  144. def->info->key_bitlen == (unsigned) key_bitlen &&
  145. def->info->mode == mode )
  146. return( def->info );
  147. return( NULL );
  148. }
  149. void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
  150. {
  151. CIPHER_VALIDATE( ctx != NULL );
  152. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  153. }
  154. void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
  155. {
  156. if( ctx == NULL )
  157. return;
  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, const mbedtls_cipher_info_t *cipher_info )
  171. {
  172. CIPHER_VALIDATE_RET( ctx != NULL );
  173. if( cipher_info == NULL )
  174. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  175. memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
  176. if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
  177. return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
  178. ctx->cipher_info = cipher_info;
  179. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  180. /*
  181. * Ignore possible errors caused by a cipher mode that doesn't use padding
  182. */
  183. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  184. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
  185. #else
  186. (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
  187. #endif
  188. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  189. return( 0 );
  190. }
  191. int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
  192. const unsigned char *key,
  193. int key_bitlen,
  194. const mbedtls_operation_t operation )
  195. {
  196. CIPHER_VALIDATE_RET( ctx != NULL );
  197. CIPHER_VALIDATE_RET( key != NULL );
  198. CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
  199. operation == MBEDTLS_DECRYPT );
  200. if( ctx->cipher_info == NULL )
  201. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  202. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
  203. (int) ctx->cipher_info->key_bitlen != key_bitlen )
  204. {
  205. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  206. }
  207. ctx->key_bitlen = key_bitlen;
  208. ctx->operation = operation;
  209. /*
  210. * For OFB, CFB and CTR mode always use the encryption key schedule
  211. */
  212. if( MBEDTLS_ENCRYPT == operation ||
  213. MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  214. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  215. MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
  216. {
  217. return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
  218. ctx->key_bitlen ) );
  219. }
  220. if( MBEDTLS_DECRYPT == operation )
  221. return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
  222. ctx->key_bitlen ) );
  223. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  224. }
  225. int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
  226. const unsigned char *iv,
  227. size_t iv_len )
  228. {
  229. size_t actual_iv_size;
  230. CIPHER_VALIDATE_RET( ctx != NULL );
  231. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  232. if( ctx->cipher_info == NULL )
  233. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  234. /* avoid buffer overflow in ctx->iv */
  235. if( iv_len > MBEDTLS_MAX_IV_LENGTH )
  236. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  237. if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
  238. actual_iv_size = iv_len;
  239. else
  240. {
  241. actual_iv_size = ctx->cipher_info->iv_size;
  242. /* avoid reading past the end of input buffer */
  243. if( actual_iv_size > iv_len )
  244. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  245. }
  246. #if defined(MBEDTLS_CHACHA20_C)
  247. if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20 )
  248. {
  249. if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
  250. iv,
  251. 0U ) ) /* Initial counter value */
  252. {
  253. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  254. }
  255. }
  256. #endif
  257. if ( actual_iv_size != 0 )
  258. {
  259. memcpy( ctx->iv, iv, actual_iv_size );
  260. ctx->iv_size = actual_iv_size;
  261. }
  262. return( 0 );
  263. }
  264. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
  265. {
  266. CIPHER_VALIDATE_RET( ctx != NULL );
  267. if( ctx->cipher_info == NULL )
  268. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  269. ctx->unprocessed_len = 0;
  270. return( 0 );
  271. }
  272. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  273. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  274. const unsigned char *ad, size_t ad_len )
  275. {
  276. CIPHER_VALIDATE_RET( ctx != NULL );
  277. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  278. if( ctx->cipher_info == NULL )
  279. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  280. #if defined(MBEDTLS_GCM_C)
  281. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  282. {
  283. return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
  284. ctx->iv, ctx->iv_size, ad, ad_len ) );
  285. }
  286. #endif
  287. #if defined(MBEDTLS_CHACHAPOLY_C)
  288. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  289. {
  290. int result;
  291. mbedtls_chachapoly_mode_t mode;
  292. mode = ( ctx->operation == MBEDTLS_ENCRYPT )
  293. ? MBEDTLS_CHACHAPOLY_ENCRYPT
  294. : MBEDTLS_CHACHAPOLY_DECRYPT;
  295. result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  296. ctx->iv,
  297. mode );
  298. if ( result != 0 )
  299. return( result );
  300. return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  301. ad, ad_len ) );
  302. }
  303. #endif
  304. return( 0 );
  305. }
  306. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  307. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  308. size_t ilen, unsigned char *output, size_t *olen )
  309. {
  310. int ret;
  311. size_t block_size;
  312. CIPHER_VALIDATE_RET( ctx != NULL );
  313. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  314. CIPHER_VALIDATE_RET( output != NULL );
  315. CIPHER_VALIDATE_RET( olen != NULL );
  316. if( ctx->cipher_info == NULL )
  317. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  318. *olen = 0;
  319. block_size = mbedtls_cipher_get_block_size( ctx );
  320. if ( 0 == block_size )
  321. {
  322. return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
  323. }
  324. if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
  325. {
  326. if( ilen != block_size )
  327. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  328. *olen = ilen;
  329. if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
  330. ctx->operation, input, output ) ) )
  331. {
  332. return( ret );
  333. }
  334. return( 0 );
  335. }
  336. #if defined(MBEDTLS_GCM_C)
  337. if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
  338. {
  339. *olen = ilen;
  340. return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
  341. output ) );
  342. }
  343. #endif
  344. #if defined(MBEDTLS_CHACHAPOLY_C)
  345. if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
  346. {
  347. *olen = ilen;
  348. return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  349. ilen, input, output ) );
  350. }
  351. #endif
  352. if( input == output &&
  353. ( ctx->unprocessed_len != 0 || ilen % block_size ) )
  354. {
  355. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  356. }
  357. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  358. if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
  359. {
  360. size_t copy_len = 0;
  361. /*
  362. * If there is not enough data for a full block, cache it.
  363. */
  364. if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
  365. ilen <= block_size - ctx->unprocessed_len ) ||
  366. ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
  367. ilen < block_size - ctx->unprocessed_len ) ||
  368. ( ctx->operation == MBEDTLS_ENCRYPT &&
  369. ilen < block_size - ctx->unprocessed_len ) )
  370. {
  371. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  372. ilen );
  373. ctx->unprocessed_len += ilen;
  374. return( 0 );
  375. }
  376. /*
  377. * Process cached data first
  378. */
  379. if( 0 != ctx->unprocessed_len )
  380. {
  381. copy_len = block_size - ctx->unprocessed_len;
  382. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  383. copy_len );
  384. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  385. ctx->operation, block_size, ctx->iv,
  386. ctx->unprocessed_data, output ) ) )
  387. {
  388. return( ret );
  389. }
  390. *olen += block_size;
  391. output += block_size;
  392. ctx->unprocessed_len = 0;
  393. input += copy_len;
  394. ilen -= copy_len;
  395. }
  396. /*
  397. * Cache final, incomplete block
  398. */
  399. if( 0 != ilen )
  400. {
  401. /* Encryption: only cache partial blocks
  402. * Decryption w/ padding: always keep at least one whole block
  403. * Decryption w/o padding: only cache partial blocks
  404. */
  405. copy_len = ilen % block_size;
  406. if( copy_len == 0 &&
  407. ctx->operation == MBEDTLS_DECRYPT &&
  408. NULL != ctx->add_padding)
  409. {
  410. copy_len = block_size;
  411. }
  412. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  413. copy_len );
  414. ctx->unprocessed_len += copy_len;
  415. ilen -= copy_len;
  416. }
  417. /*
  418. * Process remaining full blocks
  419. */
  420. if( ilen )
  421. {
  422. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  423. ctx->operation, ilen, ctx->iv, input, output ) ) )
  424. {
  425. return( ret );
  426. }
  427. *olen += ilen;
  428. }
  429. return( 0 );
  430. }
  431. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  432. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  433. if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
  434. {
  435. if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
  436. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  437. input, output ) ) )
  438. {
  439. return( ret );
  440. }
  441. *olen = ilen;
  442. return( 0 );
  443. }
  444. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  445. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  446. if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
  447. {
  448. if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
  449. ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
  450. {
  451. return( ret );
  452. }
  453. *olen = ilen;
  454. return( 0 );
  455. }
  456. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  457. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  458. if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
  459. {
  460. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  461. ilen, &ctx->unprocessed_len, ctx->iv,
  462. ctx->unprocessed_data, input, output ) ) )
  463. {
  464. return( ret );
  465. }
  466. *olen = ilen;
  467. return( 0 );
  468. }
  469. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  470. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  471. if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
  472. {
  473. if( ctx->unprocessed_len > 0 ) {
  474. /* We can only process an entire data unit at a time. */
  475. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  476. }
  477. ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
  478. ctx->operation, ilen, ctx->iv, input, output );
  479. if( ret != 0 )
  480. {
  481. return( ret );
  482. }
  483. *olen = ilen;
  484. return( 0 );
  485. }
  486. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  487. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  488. if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
  489. {
  490. if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
  491. ilen, input, output ) ) )
  492. {
  493. return( ret );
  494. }
  495. *olen = ilen;
  496. return( 0 );
  497. }
  498. #endif /* MBEDTLS_CIPHER_MODE_STREAM */
  499. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  500. }
  501. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  502. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  503. /*
  504. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  505. */
  506. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  507. size_t data_len )
  508. {
  509. size_t padding_len = output_len - data_len;
  510. unsigned char i;
  511. for( i = 0; i < padding_len; i++ )
  512. output[data_len + i] = (unsigned char) padding_len;
  513. }
  514. static int get_pkcs_padding( unsigned char *input, size_t input_len,
  515. size_t *data_len )
  516. {
  517. size_t i, pad_idx;
  518. unsigned char padding_len, bad = 0;
  519. if( NULL == input || NULL == data_len )
  520. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  521. padding_len = input[input_len - 1];
  522. *data_len = input_len - padding_len;
  523. /* Avoid logical || since it results in a branch */
  524. bad |= padding_len > input_len;
  525. bad |= padding_len == 0;
  526. /* The number of bytes checked must be independent of padding_len,
  527. * so pick input_len, which is usually 8 or 16 (one block) */
  528. pad_idx = input_len - padding_len;
  529. for( i = 0; i < input_len; i++ )
  530. bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
  531. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  532. }
  533. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  534. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  535. /*
  536. * One and zeros padding: fill with 80 00 ... 00
  537. */
  538. static void add_one_and_zeros_padding( unsigned char *output,
  539. size_t output_len, size_t data_len )
  540. {
  541. size_t padding_len = output_len - data_len;
  542. unsigned char i = 0;
  543. output[data_len] = 0x80;
  544. for( i = 1; i < padding_len; i++ )
  545. output[data_len + i] = 0x00;
  546. }
  547. static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
  548. size_t *data_len )
  549. {
  550. size_t i;
  551. unsigned char done = 0, prev_done, bad;
  552. if( NULL == input || NULL == data_len )
  553. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  554. bad = 0x80;
  555. *data_len = 0;
  556. for( i = input_len; i > 0; i-- )
  557. {
  558. prev_done = done;
  559. done |= ( input[i - 1] != 0 );
  560. *data_len |= ( i - 1 ) * ( done != prev_done );
  561. bad ^= input[i - 1] * ( done != prev_done );
  562. }
  563. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  564. }
  565. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  566. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  567. /*
  568. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  569. */
  570. static void add_zeros_and_len_padding( unsigned char *output,
  571. size_t output_len, size_t data_len )
  572. {
  573. size_t padding_len = output_len - data_len;
  574. unsigned char i = 0;
  575. for( i = 1; i < padding_len; i++ )
  576. output[data_len + i - 1] = 0x00;
  577. output[output_len - 1] = (unsigned char) padding_len;
  578. }
  579. static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
  580. size_t *data_len )
  581. {
  582. size_t i, pad_idx;
  583. unsigned char padding_len, bad = 0;
  584. if( NULL == input || NULL == data_len )
  585. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  586. padding_len = input[input_len - 1];
  587. *data_len = input_len - padding_len;
  588. /* Avoid logical || since it results in a branch */
  589. bad |= padding_len > input_len;
  590. bad |= padding_len == 0;
  591. /* The number of bytes checked must be independent of padding_len */
  592. pad_idx = input_len - padding_len;
  593. for( i = 0; i < input_len - 1; i++ )
  594. bad |= input[i] * ( i >= pad_idx );
  595. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  596. }
  597. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  598. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  599. /*
  600. * Zero padding: fill with 00 ... 00
  601. */
  602. static void add_zeros_padding( unsigned char *output,
  603. size_t output_len, size_t data_len )
  604. {
  605. size_t i;
  606. for( i = data_len; i < output_len; i++ )
  607. output[i] = 0x00;
  608. }
  609. static int get_zeros_padding( unsigned char *input, size_t input_len,
  610. size_t *data_len )
  611. {
  612. size_t i;
  613. unsigned char done = 0, prev_done;
  614. if( NULL == input || NULL == data_len )
  615. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  616. *data_len = 0;
  617. for( i = input_len; i > 0; i-- )
  618. {
  619. prev_done = done;
  620. done |= ( input[i-1] != 0 );
  621. *data_len |= i * ( done != prev_done );
  622. }
  623. return( 0 );
  624. }
  625. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  626. /*
  627. * No padding: don't pad :)
  628. *
  629. * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  630. * but a trivial get_padding function
  631. */
  632. static int get_no_padding( unsigned char *input, size_t input_len,
  633. size_t *data_len )
  634. {
  635. if( NULL == input || NULL == data_len )
  636. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  637. *data_len = input_len;
  638. return( 0 );
  639. }
  640. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  641. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  642. unsigned char *output, size_t *olen )
  643. {
  644. CIPHER_VALIDATE_RET( ctx != NULL );
  645. CIPHER_VALIDATE_RET( output != NULL );
  646. CIPHER_VALIDATE_RET( olen != NULL );
  647. if( ctx->cipher_info == NULL )
  648. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  649. *olen = 0;
  650. if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  651. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  652. MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
  653. MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
  654. MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
  655. MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
  656. {
  657. return( 0 );
  658. }
  659. if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
  660. ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
  661. {
  662. return( 0 );
  663. }
  664. if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
  665. {
  666. if( ctx->unprocessed_len != 0 )
  667. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  668. return( 0 );
  669. }
  670. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  671. if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
  672. {
  673. int ret = 0;
  674. if( MBEDTLS_ENCRYPT == ctx->operation )
  675. {
  676. /* check for 'no padding' mode */
  677. if( NULL == ctx->add_padding )
  678. {
  679. if( 0 != ctx->unprocessed_len )
  680. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  681. return( 0 );
  682. }
  683. ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
  684. ctx->unprocessed_len );
  685. }
  686. else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  687. {
  688. /*
  689. * For decrypt operations, expect a full block,
  690. * or an empty block if no padding
  691. */
  692. if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
  693. return( 0 );
  694. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  695. }
  696. /* cipher block */
  697. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  698. ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
  699. ctx->unprocessed_data, output ) ) )
  700. {
  701. return( ret );
  702. }
  703. /* Set output size for decryption */
  704. if( MBEDTLS_DECRYPT == ctx->operation )
  705. return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
  706. olen ) );
  707. /* Set output size for encryption */
  708. *olen = mbedtls_cipher_get_block_size( ctx );
  709. return( 0 );
  710. }
  711. #else
  712. ((void) output);
  713. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  714. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  715. }
  716. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  717. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
  718. mbedtls_cipher_padding_t mode )
  719. {
  720. CIPHER_VALIDATE_RET( ctx != NULL );
  721. if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
  722. {
  723. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  724. }
  725. switch( mode )
  726. {
  727. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  728. case MBEDTLS_PADDING_PKCS7:
  729. ctx->add_padding = add_pkcs_padding;
  730. ctx->get_padding = get_pkcs_padding;
  731. break;
  732. #endif
  733. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  734. case MBEDTLS_PADDING_ONE_AND_ZEROS:
  735. ctx->add_padding = add_one_and_zeros_padding;
  736. ctx->get_padding = get_one_and_zeros_padding;
  737. break;
  738. #endif
  739. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  740. case MBEDTLS_PADDING_ZEROS_AND_LEN:
  741. ctx->add_padding = add_zeros_and_len_padding;
  742. ctx->get_padding = get_zeros_and_len_padding;
  743. break;
  744. #endif
  745. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  746. case MBEDTLS_PADDING_ZEROS:
  747. ctx->add_padding = add_zeros_padding;
  748. ctx->get_padding = get_zeros_padding;
  749. break;
  750. #endif
  751. case MBEDTLS_PADDING_NONE:
  752. ctx->add_padding = NULL;
  753. ctx->get_padding = get_no_padding;
  754. break;
  755. default:
  756. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  757. }
  758. return( 0 );
  759. }
  760. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  761. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  762. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  763. unsigned char *tag, size_t tag_len )
  764. {
  765. CIPHER_VALIDATE_RET( ctx != NULL );
  766. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  767. if( ctx->cipher_info == NULL )
  768. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  769. if( MBEDTLS_ENCRYPT != ctx->operation )
  770. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  771. #if defined(MBEDTLS_GCM_C)
  772. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  773. return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  774. tag, tag_len ) );
  775. #endif
  776. #if defined(MBEDTLS_CHACHAPOLY_C)
  777. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  778. {
  779. /* Don't allow truncated MAC for Poly1305 */
  780. if ( tag_len != 16U )
  781. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  782. return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  783. tag ) );
  784. }
  785. #endif
  786. return( 0 );
  787. }
  788. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  789. const unsigned char *tag, size_t tag_len )
  790. {
  791. unsigned char check_tag[16];
  792. int ret;
  793. CIPHER_VALIDATE_RET( ctx != NULL );
  794. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  795. if( ctx->cipher_info == NULL )
  796. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  797. if( MBEDTLS_DECRYPT != ctx->operation )
  798. {
  799. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  800. }
  801. #if defined(MBEDTLS_GCM_C)
  802. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  803. {
  804. if( tag_len > sizeof( check_tag ) )
  805. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  806. if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  807. check_tag, tag_len ) ) )
  808. {
  809. return( ret );
  810. }
  811. /* Check the tag in "constant-time" */
  812. if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
  813. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  814. return( 0 );
  815. }
  816. #endif /* MBEDTLS_GCM_C */
  817. #if defined(MBEDTLS_CHACHAPOLY_C)
  818. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  819. {
  820. /* Don't allow truncated MAC for Poly1305 */
  821. if ( tag_len != sizeof( check_tag ) )
  822. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  823. ret = mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  824. check_tag );
  825. if ( ret != 0 )
  826. {
  827. return( ret );
  828. }
  829. /* Check the tag in "constant-time" */
  830. if( mbedtls_constant_time_memcmp( tag, check_tag, tag_len ) != 0 )
  831. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  832. return( 0 );
  833. }
  834. #endif /* MBEDTLS_CHACHAPOLY_C */
  835. return( 0 );
  836. }
  837. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  838. /*
  839. * Packet-oriented wrapper for non-AEAD modes
  840. */
  841. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  842. const unsigned char *iv, size_t iv_len,
  843. const unsigned char *input, size_t ilen,
  844. unsigned char *output, size_t *olen )
  845. {
  846. int ret;
  847. size_t finish_olen;
  848. CIPHER_VALIDATE_RET( ctx != NULL );
  849. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  850. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  851. CIPHER_VALIDATE_RET( output != NULL );
  852. CIPHER_VALIDATE_RET( olen != NULL );
  853. if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
  854. return( ret );
  855. if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
  856. return( ret );
  857. if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
  858. return( ret );
  859. if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
  860. return( ret );
  861. *olen += finish_olen;
  862. return( 0 );
  863. }
  864. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  865. /*
  866. * Packet-oriented encryption for AEAD modes
  867. */
  868. int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
  869. const unsigned char *iv, size_t iv_len,
  870. const unsigned char *ad, size_t ad_len,
  871. const unsigned char *input, size_t ilen,
  872. unsigned char *output, size_t *olen,
  873. unsigned char *tag, size_t tag_len )
  874. {
  875. CIPHER_VALIDATE_RET( ctx != NULL );
  876. CIPHER_VALIDATE_RET( iv != NULL );
  877. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  878. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  879. CIPHER_VALIDATE_RET( output != NULL );
  880. CIPHER_VALIDATE_RET( olen != NULL );
  881. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  882. #if defined(MBEDTLS_GCM_C)
  883. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  884. {
  885. *olen = ilen;
  886. return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
  887. iv, iv_len, ad, ad_len, input, output,
  888. tag_len, tag ) );
  889. }
  890. #endif /* MBEDTLS_GCM_C */
  891. #if defined(MBEDTLS_CCM_C)
  892. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  893. {
  894. *olen = ilen;
  895. return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
  896. iv, iv_len, ad, ad_len, input, output,
  897. tag, tag_len ) );
  898. }
  899. #endif /* MBEDTLS_CCM_C */
  900. #if defined(MBEDTLS_CHACHAPOLY_C)
  901. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  902. {
  903. /* ChachaPoly has fixed length nonce and MAC (tag) */
  904. if ( ( iv_len != ctx->cipher_info->iv_size ) ||
  905. ( tag_len != 16U ) )
  906. {
  907. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  908. }
  909. *olen = ilen;
  910. return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
  911. ilen, iv, ad, ad_len, input, output, tag ) );
  912. }
  913. #endif /* MBEDTLS_CHACHAPOLY_C */
  914. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  915. }
  916. /*
  917. * Packet-oriented decryption for AEAD modes
  918. */
  919. int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
  920. const unsigned char *iv, size_t iv_len,
  921. const unsigned char *ad, size_t ad_len,
  922. const unsigned char *input, size_t ilen,
  923. unsigned char *output, size_t *olen,
  924. const unsigned char *tag, size_t tag_len )
  925. {
  926. CIPHER_VALIDATE_RET( ctx != NULL );
  927. CIPHER_VALIDATE_RET( iv != NULL );
  928. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  929. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  930. CIPHER_VALIDATE_RET( output != NULL );
  931. CIPHER_VALIDATE_RET( olen != NULL );
  932. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  933. #if defined(MBEDTLS_GCM_C)
  934. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  935. {
  936. int ret;
  937. *olen = ilen;
  938. ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
  939. iv, iv_len, ad, ad_len,
  940. tag, tag_len, input, output );
  941. if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
  942. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  943. return( ret );
  944. }
  945. #endif /* MBEDTLS_GCM_C */
  946. #if defined(MBEDTLS_CCM_C)
  947. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  948. {
  949. int ret;
  950. *olen = ilen;
  951. ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
  952. iv, iv_len, ad, ad_len,
  953. input, output, tag, tag_len );
  954. if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
  955. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  956. return( ret );
  957. }
  958. #endif /* MBEDTLS_CCM_C */
  959. #if defined(MBEDTLS_CHACHAPOLY_C)
  960. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  961. {
  962. int ret;
  963. /* ChachaPoly has fixed length nonce and MAC (tag) */
  964. if ( ( iv_len != ctx->cipher_info->iv_size ) ||
  965. ( tag_len != 16U ) )
  966. {
  967. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  968. }
  969. *olen = ilen;
  970. ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
  971. iv, ad, ad_len, tag, input, output );
  972. if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
  973. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  974. return( ret );
  975. }
  976. #endif /* MBEDTLS_CHACHAPOLY_C */
  977. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  978. }
  979. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  980. #endif /* MBEDTLS_CIPHER_C */