cipher.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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 "cipher_wrap.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_PLATFORM_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. if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context*)ctx->cipher_ctx,
  319. iv,
  320. 0U ) ) /* Initial counter value */
  321. {
  322. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  323. }
  324. }
  325. #endif
  326. #if defined(MBEDTLS_GCM_C)
  327. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  328. {
  329. return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx,
  330. ctx->operation,
  331. iv, iv_len ) );
  332. }
  333. #endif
  334. #if defined(MBEDTLS_CCM_C)
  335. if( MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode )
  336. {
  337. int set_lengths_result;
  338. int ccm_star_mode;
  339. set_lengths_result = mbedtls_ccm_set_lengths(
  340. (mbedtls_ccm_context *) ctx->cipher_ctx,
  341. 0, 0, 0 );
  342. if( set_lengths_result != 0 )
  343. return set_lengths_result;
  344. if( ctx->operation == MBEDTLS_DECRYPT )
  345. ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT;
  346. else if( ctx->operation == MBEDTLS_ENCRYPT )
  347. ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT;
  348. else
  349. return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  350. return( mbedtls_ccm_starts( (mbedtls_ccm_context *) ctx->cipher_ctx,
  351. ccm_star_mode,
  352. iv, iv_len ) );
  353. }
  354. #endif
  355. if ( actual_iv_size != 0 )
  356. {
  357. memcpy( ctx->iv, iv, actual_iv_size );
  358. ctx->iv_size = actual_iv_size;
  359. }
  360. return( 0 );
  361. }
  362. int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
  363. {
  364. CIPHER_VALIDATE_RET( ctx != 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. /* We don't support resetting PSA-based
  371. * cipher contexts, yet. */
  372. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  373. }
  374. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  375. ctx->unprocessed_len = 0;
  376. return( 0 );
  377. }
  378. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  379. int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
  380. const unsigned char *ad, size_t ad_len )
  381. {
  382. CIPHER_VALIDATE_RET( ctx != NULL );
  383. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  384. if( ctx->cipher_info == NULL )
  385. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  386. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  387. if( ctx->psa_enabled == 1 )
  388. {
  389. /* While PSA Crypto has an API for multipart
  390. * operations, we currently don't make it
  391. * accessible through the cipher layer. */
  392. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  393. }
  394. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  395. #if defined(MBEDTLS_GCM_C)
  396. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  397. {
  398. return( mbedtls_gcm_update_ad( (mbedtls_gcm_context *) ctx->cipher_ctx,
  399. ad, ad_len ) );
  400. }
  401. #endif
  402. #if defined(MBEDTLS_CHACHAPOLY_C)
  403. if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  404. {
  405. int result;
  406. mbedtls_chachapoly_mode_t mode;
  407. mode = ( ctx->operation == MBEDTLS_ENCRYPT )
  408. ? MBEDTLS_CHACHAPOLY_ENCRYPT
  409. : MBEDTLS_CHACHAPOLY_DECRYPT;
  410. result = mbedtls_chachapoly_starts( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  411. ctx->iv,
  412. mode );
  413. if ( result != 0 )
  414. return( result );
  415. return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  416. ad, ad_len ) );
  417. }
  418. #endif
  419. return( 0 );
  420. }
  421. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  422. int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
  423. size_t ilen, unsigned char *output, size_t *olen )
  424. {
  425. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  426. size_t block_size;
  427. CIPHER_VALIDATE_RET( ctx != NULL );
  428. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  429. CIPHER_VALIDATE_RET( output != NULL );
  430. CIPHER_VALIDATE_RET( olen != NULL );
  431. if( ctx->cipher_info == NULL )
  432. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  433. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  434. if( ctx->psa_enabled == 1 )
  435. {
  436. /* While PSA Crypto has an API for multipart
  437. * operations, we currently don't make it
  438. * accessible through the cipher layer. */
  439. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  440. }
  441. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  442. *olen = 0;
  443. block_size = mbedtls_cipher_get_block_size( ctx );
  444. if ( 0 == block_size )
  445. {
  446. return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
  447. }
  448. if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
  449. {
  450. if( ilen != block_size )
  451. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  452. *olen = ilen;
  453. if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx,
  454. ctx->operation, input, output ) ) )
  455. {
  456. return( ret );
  457. }
  458. return( 0 );
  459. }
  460. #if defined(MBEDTLS_GCM_C)
  461. if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
  462. {
  463. return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx,
  464. input, ilen,
  465. output, ilen, olen ) );
  466. }
  467. #endif
  468. #if defined(MBEDTLS_CCM_C)
  469. if( ctx->cipher_info->mode == MBEDTLS_MODE_CCM_STAR_NO_TAG )
  470. {
  471. return( mbedtls_ccm_update( (mbedtls_ccm_context *) ctx->cipher_ctx,
  472. input, ilen,
  473. output, ilen, olen ) );
  474. }
  475. #endif
  476. #if defined(MBEDTLS_CHACHAPOLY_C)
  477. if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
  478. {
  479. *olen = ilen;
  480. return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
  481. ilen, input, output ) );
  482. }
  483. #endif
  484. if( input == output &&
  485. ( ctx->unprocessed_len != 0 || ilen % block_size ) )
  486. {
  487. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  488. }
  489. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  490. if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
  491. {
  492. size_t copy_len = 0;
  493. /*
  494. * If there is not enough data for a full block, cache it.
  495. */
  496. if( ( ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
  497. ilen <= block_size - ctx->unprocessed_len ) ||
  498. ( ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
  499. ilen < block_size - ctx->unprocessed_len ) ||
  500. ( ctx->operation == MBEDTLS_ENCRYPT &&
  501. ilen < block_size - ctx->unprocessed_len ) )
  502. {
  503. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  504. ilen );
  505. ctx->unprocessed_len += ilen;
  506. return( 0 );
  507. }
  508. /*
  509. * Process cached data first
  510. */
  511. if( 0 != ctx->unprocessed_len )
  512. {
  513. copy_len = block_size - ctx->unprocessed_len;
  514. memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
  515. copy_len );
  516. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  517. ctx->operation, block_size, ctx->iv,
  518. ctx->unprocessed_data, output ) ) )
  519. {
  520. return( ret );
  521. }
  522. *olen += block_size;
  523. output += block_size;
  524. ctx->unprocessed_len = 0;
  525. input += copy_len;
  526. ilen -= copy_len;
  527. }
  528. /*
  529. * Cache final, incomplete block
  530. */
  531. if( 0 != ilen )
  532. {
  533. /* Encryption: only cache partial blocks
  534. * Decryption w/ padding: always keep at least one whole block
  535. * Decryption w/o padding: only cache partial blocks
  536. */
  537. copy_len = ilen % block_size;
  538. if( copy_len == 0 &&
  539. ctx->operation == MBEDTLS_DECRYPT &&
  540. NULL != ctx->add_padding)
  541. {
  542. copy_len = block_size;
  543. }
  544. memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
  545. copy_len );
  546. ctx->unprocessed_len += copy_len;
  547. ilen -= copy_len;
  548. }
  549. /*
  550. * Process remaining full blocks
  551. */
  552. if( ilen )
  553. {
  554. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  555. ctx->operation, ilen, ctx->iv, input, output ) ) )
  556. {
  557. return( ret );
  558. }
  559. *olen += ilen;
  560. }
  561. return( 0 );
  562. }
  563. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  564. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  565. if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
  566. {
  567. if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
  568. ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
  569. input, output ) ) )
  570. {
  571. return( ret );
  572. }
  573. *olen = ilen;
  574. return( 0 );
  575. }
  576. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  577. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  578. if( ctx->cipher_info->mode == MBEDTLS_MODE_OFB )
  579. {
  580. if( 0 != ( ret = ctx->cipher_info->base->ofb_func( ctx->cipher_ctx,
  581. ilen, &ctx->unprocessed_len, ctx->iv, input, output ) ) )
  582. {
  583. return( ret );
  584. }
  585. *olen = ilen;
  586. return( 0 );
  587. }
  588. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  589. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  590. if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
  591. {
  592. if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
  593. ilen, &ctx->unprocessed_len, ctx->iv,
  594. ctx->unprocessed_data, input, output ) ) )
  595. {
  596. return( ret );
  597. }
  598. *olen = ilen;
  599. return( 0 );
  600. }
  601. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  602. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  603. if( ctx->cipher_info->mode == MBEDTLS_MODE_XTS )
  604. {
  605. if( ctx->unprocessed_len > 0 ) {
  606. /* We can only process an entire data unit at a time. */
  607. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  608. }
  609. ret = ctx->cipher_info->base->xts_func( ctx->cipher_ctx,
  610. ctx->operation, ilen, ctx->iv, input, output );
  611. if( ret != 0 )
  612. {
  613. return( ret );
  614. }
  615. *olen = ilen;
  616. return( 0 );
  617. }
  618. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  619. #if defined(MBEDTLS_CIPHER_MODE_STREAM)
  620. if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
  621. {
  622. if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
  623. ilen, input, output ) ) )
  624. {
  625. return( ret );
  626. }
  627. *olen = ilen;
  628. return( 0 );
  629. }
  630. #endif /* MBEDTLS_CIPHER_MODE_STREAM */
  631. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  632. }
  633. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  634. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  635. /*
  636. * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  637. */
  638. static void add_pkcs_padding( unsigned char *output, size_t output_len,
  639. size_t data_len )
  640. {
  641. size_t padding_len = output_len - data_len;
  642. unsigned char i;
  643. for( i = 0; i < padding_len; i++ )
  644. output[data_len + i] = (unsigned char) padding_len;
  645. }
  646. static int get_pkcs_padding( unsigned char *input, size_t input_len,
  647. size_t *data_len )
  648. {
  649. size_t i, pad_idx;
  650. unsigned char padding_len, bad = 0;
  651. if( NULL == input || NULL == data_len )
  652. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  653. padding_len = input[input_len - 1];
  654. *data_len = input_len - padding_len;
  655. /* Avoid logical || since it results in a branch */
  656. bad |= padding_len > input_len;
  657. bad |= padding_len == 0;
  658. /* The number of bytes checked must be independent of padding_len,
  659. * so pick input_len, which is usually 8 or 16 (one block) */
  660. pad_idx = input_len - padding_len;
  661. for( i = 0; i < input_len; i++ )
  662. bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
  663. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  664. }
  665. #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
  666. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  667. /*
  668. * One and zeros padding: fill with 80 00 ... 00
  669. */
  670. static void add_one_and_zeros_padding( unsigned char *output,
  671. size_t output_len, size_t data_len )
  672. {
  673. size_t padding_len = output_len - data_len;
  674. unsigned char i = 0;
  675. output[data_len] = 0x80;
  676. for( i = 1; i < padding_len; i++ )
  677. output[data_len + i] = 0x00;
  678. }
  679. static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
  680. size_t *data_len )
  681. {
  682. size_t i;
  683. unsigned char done = 0, prev_done, bad;
  684. if( NULL == input || NULL == data_len )
  685. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  686. bad = 0x80;
  687. *data_len = 0;
  688. for( i = input_len; i > 0; i-- )
  689. {
  690. prev_done = done;
  691. done |= ( input[i - 1] != 0 );
  692. *data_len |= ( i - 1 ) * ( done != prev_done );
  693. bad ^= input[i - 1] * ( done != prev_done );
  694. }
  695. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  696. }
  697. #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
  698. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  699. /*
  700. * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  701. */
  702. static void add_zeros_and_len_padding( unsigned char *output,
  703. size_t output_len, size_t data_len )
  704. {
  705. size_t padding_len = output_len - data_len;
  706. unsigned char i = 0;
  707. for( i = 1; i < padding_len; i++ )
  708. output[data_len + i - 1] = 0x00;
  709. output[output_len - 1] = (unsigned char) padding_len;
  710. }
  711. static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
  712. size_t *data_len )
  713. {
  714. size_t i, pad_idx;
  715. unsigned char padding_len, bad = 0;
  716. if( NULL == input || NULL == data_len )
  717. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  718. padding_len = input[input_len - 1];
  719. *data_len = input_len - padding_len;
  720. /* Avoid logical || since it results in a branch */
  721. bad |= padding_len > input_len;
  722. bad |= padding_len == 0;
  723. /* The number of bytes checked must be independent of padding_len */
  724. pad_idx = input_len - padding_len;
  725. for( i = 0; i < input_len - 1; i++ )
  726. bad |= input[i] * ( i >= pad_idx );
  727. return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
  728. }
  729. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
  730. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  731. /*
  732. * Zero padding: fill with 00 ... 00
  733. */
  734. static void add_zeros_padding( unsigned char *output,
  735. size_t output_len, size_t data_len )
  736. {
  737. size_t i;
  738. for( i = data_len; i < output_len; i++ )
  739. output[i] = 0x00;
  740. }
  741. static int get_zeros_padding( unsigned char *input, size_t input_len,
  742. size_t *data_len )
  743. {
  744. size_t i;
  745. unsigned char done = 0, prev_done;
  746. if( NULL == input || NULL == data_len )
  747. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  748. *data_len = 0;
  749. for( i = input_len; i > 0; i-- )
  750. {
  751. prev_done = done;
  752. done |= ( input[i-1] != 0 );
  753. *data_len |= i * ( done != prev_done );
  754. }
  755. return( 0 );
  756. }
  757. #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
  758. /*
  759. * No padding: don't pad :)
  760. *
  761. * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  762. * but a trivial get_padding function
  763. */
  764. static int get_no_padding( unsigned char *input, size_t input_len,
  765. size_t *data_len )
  766. {
  767. if( NULL == input || NULL == data_len )
  768. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  769. *data_len = input_len;
  770. return( 0 );
  771. }
  772. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  773. int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
  774. unsigned char *output, size_t *olen )
  775. {
  776. CIPHER_VALIDATE_RET( ctx != NULL );
  777. CIPHER_VALIDATE_RET( output != NULL );
  778. CIPHER_VALIDATE_RET( olen != NULL );
  779. if( ctx->cipher_info == NULL )
  780. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  781. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  782. if( ctx->psa_enabled == 1 )
  783. {
  784. /* While PSA Crypto has an API for multipart
  785. * operations, we currently don't make it
  786. * accessible through the cipher layer. */
  787. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  788. }
  789. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  790. *olen = 0;
  791. if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
  792. MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
  793. MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
  794. MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
  795. MBEDTLS_MODE_CCM_STAR_NO_TAG == ctx->cipher_info->mode ||
  796. MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
  797. MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
  798. {
  799. return( 0 );
  800. }
  801. if ( ( MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type ) ||
  802. ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type ) )
  803. {
  804. return( 0 );
  805. }
  806. if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
  807. {
  808. if( ctx->unprocessed_len != 0 )
  809. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  810. return( 0 );
  811. }
  812. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  813. if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
  814. {
  815. int ret = 0;
  816. if( MBEDTLS_ENCRYPT == ctx->operation )
  817. {
  818. /* check for 'no padding' mode */
  819. if( NULL == ctx->add_padding )
  820. {
  821. if( 0 != ctx->unprocessed_len )
  822. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  823. return( 0 );
  824. }
  825. ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
  826. ctx->unprocessed_len );
  827. }
  828. else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
  829. {
  830. /*
  831. * For decrypt operations, expect a full block,
  832. * or an empty block if no padding
  833. */
  834. if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
  835. return( 0 );
  836. return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
  837. }
  838. /* cipher block */
  839. if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
  840. ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
  841. ctx->unprocessed_data, output ) ) )
  842. {
  843. return( ret );
  844. }
  845. /* Set output size for decryption */
  846. if( MBEDTLS_DECRYPT == ctx->operation )
  847. return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
  848. olen ) );
  849. /* Set output size for encryption */
  850. *olen = mbedtls_cipher_get_block_size( ctx );
  851. return( 0 );
  852. }
  853. #else
  854. ((void) output);
  855. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  856. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  857. }
  858. #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
  859. int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
  860. mbedtls_cipher_padding_t mode )
  861. {
  862. CIPHER_VALIDATE_RET( ctx != NULL );
  863. if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
  864. {
  865. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  866. }
  867. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  868. if( ctx->psa_enabled == 1 )
  869. {
  870. /* While PSA Crypto knows about CBC padding
  871. * schemes, we currently don't make them
  872. * accessible through the cipher layer. */
  873. if( mode != MBEDTLS_PADDING_NONE )
  874. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  875. return( 0 );
  876. }
  877. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  878. switch( mode )
  879. {
  880. #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
  881. case MBEDTLS_PADDING_PKCS7:
  882. ctx->add_padding = add_pkcs_padding;
  883. ctx->get_padding = get_pkcs_padding;
  884. break;
  885. #endif
  886. #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
  887. case MBEDTLS_PADDING_ONE_AND_ZEROS:
  888. ctx->add_padding = add_one_and_zeros_padding;
  889. ctx->get_padding = get_one_and_zeros_padding;
  890. break;
  891. #endif
  892. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
  893. case MBEDTLS_PADDING_ZEROS_AND_LEN:
  894. ctx->add_padding = add_zeros_and_len_padding;
  895. ctx->get_padding = get_zeros_and_len_padding;
  896. break;
  897. #endif
  898. #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
  899. case MBEDTLS_PADDING_ZEROS:
  900. ctx->add_padding = add_zeros_padding;
  901. ctx->get_padding = get_zeros_padding;
  902. break;
  903. #endif
  904. case MBEDTLS_PADDING_NONE:
  905. ctx->add_padding = NULL;
  906. ctx->get_padding = get_no_padding;
  907. break;
  908. default:
  909. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  910. }
  911. return( 0 );
  912. }
  913. #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
  914. #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
  915. int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
  916. unsigned char *tag, size_t tag_len )
  917. {
  918. CIPHER_VALIDATE_RET( ctx != NULL );
  919. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  920. if( ctx->cipher_info == NULL )
  921. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  922. if( MBEDTLS_ENCRYPT != ctx->operation )
  923. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  924. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  925. if( ctx->psa_enabled == 1 )
  926. {
  927. /* While PSA Crypto has an API for multipart
  928. * operations, we currently don't make it
  929. * accessible through the cipher layer. */
  930. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  931. }
  932. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  933. #if defined(MBEDTLS_GCM_C)
  934. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  935. {
  936. size_t output_length;
  937. /* The code here doesn't yet support alternative implementations
  938. * that can delay up to a block of output. */
  939. return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
  940. NULL, 0, &output_length,
  941. tag, tag_len ) );
  942. }
  943. #endif
  944. #if defined(MBEDTLS_CHACHAPOLY_C)
  945. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  946. {
  947. /* Don't allow truncated MAC for Poly1305 */
  948. if ( tag_len != 16U )
  949. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  950. return( mbedtls_chachapoly_finish(
  951. (mbedtls_chachapoly_context*) ctx->cipher_ctx, tag ) );
  952. }
  953. #endif
  954. return( 0 );
  955. }
  956. int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
  957. const unsigned char *tag, size_t tag_len )
  958. {
  959. unsigned char check_tag[16];
  960. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  961. CIPHER_VALIDATE_RET( ctx != NULL );
  962. CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
  963. if( ctx->cipher_info == NULL )
  964. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  965. if( MBEDTLS_DECRYPT != ctx->operation )
  966. {
  967. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  968. }
  969. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  970. if( ctx->psa_enabled == 1 )
  971. {
  972. /* While PSA Crypto has an API for multipart
  973. * operations, we currently don't make it
  974. * accessible through the cipher layer. */
  975. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  976. }
  977. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  978. #if defined(MBEDTLS_GCM_C)
  979. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  980. {
  981. size_t output_length;
  982. /* The code here doesn't yet support alternative implementations
  983. * that can delay up to a block of output. */
  984. if( tag_len > sizeof( check_tag ) )
  985. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  986. if( 0 != ( ret = mbedtls_gcm_finish(
  987. (mbedtls_gcm_context *) ctx->cipher_ctx,
  988. NULL, 0, &output_length,
  989. check_tag, tag_len ) ) )
  990. {
  991. return( ret );
  992. }
  993. /* Check the tag in "constant-time" */
  994. if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
  995. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  996. return( 0 );
  997. }
  998. #endif /* MBEDTLS_GCM_C */
  999. #if defined(MBEDTLS_CHACHAPOLY_C)
  1000. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  1001. {
  1002. /* Don't allow truncated MAC for Poly1305 */
  1003. if ( tag_len != sizeof( check_tag ) )
  1004. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1005. ret = mbedtls_chachapoly_finish(
  1006. (mbedtls_chachapoly_context*) ctx->cipher_ctx, check_tag );
  1007. if ( ret != 0 )
  1008. {
  1009. return( ret );
  1010. }
  1011. /* Check the tag in "constant-time" */
  1012. if( mbedtls_ct_memcmp( tag, check_tag, tag_len ) != 0 )
  1013. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  1014. return( 0 );
  1015. }
  1016. #endif /* MBEDTLS_CHACHAPOLY_C */
  1017. return( 0 );
  1018. }
  1019. #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
  1020. /*
  1021. * Packet-oriented wrapper for non-AEAD modes
  1022. */
  1023. int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
  1024. const unsigned char *iv, size_t iv_len,
  1025. const unsigned char *input, size_t ilen,
  1026. unsigned char *output, size_t *olen )
  1027. {
  1028. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1029. size_t finish_olen;
  1030. CIPHER_VALIDATE_RET( ctx != NULL );
  1031. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  1032. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  1033. CIPHER_VALIDATE_RET( output != NULL );
  1034. CIPHER_VALIDATE_RET( olen != NULL );
  1035. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1036. if( ctx->psa_enabled == 1 )
  1037. {
  1038. /* As in the non-PSA case, we don't check that
  1039. * a key has been set. If not, the key slot will
  1040. * still be in its default state of 0, which is
  1041. * guaranteed to be invalid, hence the PSA-call
  1042. * below will gracefully fail. */
  1043. mbedtls_cipher_context_psa * const cipher_psa =
  1044. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  1045. psa_status_t status;
  1046. psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
  1047. size_t part_len;
  1048. if( ctx->operation == MBEDTLS_DECRYPT )
  1049. {
  1050. status = psa_cipher_decrypt_setup( &cipher_op,
  1051. cipher_psa->slot,
  1052. cipher_psa->alg );
  1053. }
  1054. else if( ctx->operation == MBEDTLS_ENCRYPT )
  1055. {
  1056. status = psa_cipher_encrypt_setup( &cipher_op,
  1057. cipher_psa->slot,
  1058. cipher_psa->alg );
  1059. }
  1060. else
  1061. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1062. /* In the following, we can immediately return on an error,
  1063. * because the PSA Crypto API guarantees that cipher operations
  1064. * are terminated by unsuccessful calls to psa_cipher_update(),
  1065. * and by any call to psa_cipher_finish(). */
  1066. if( status != PSA_SUCCESS )
  1067. return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
  1068. if( ctx->cipher_info->mode != MBEDTLS_MODE_ECB )
  1069. {
  1070. status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
  1071. if( status != PSA_SUCCESS )
  1072. return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
  1073. }
  1074. status = psa_cipher_update( &cipher_op,
  1075. input, ilen,
  1076. output, ilen, olen );
  1077. if( status != PSA_SUCCESS )
  1078. return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
  1079. status = psa_cipher_finish( &cipher_op,
  1080. output + *olen, ilen - *olen,
  1081. &part_len );
  1082. if( status != PSA_SUCCESS )
  1083. return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
  1084. *olen += part_len;
  1085. return( 0 );
  1086. }
  1087. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1088. if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
  1089. return( ret );
  1090. if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
  1091. return( ret );
  1092. if( ( ret = mbedtls_cipher_update( ctx, input, ilen,
  1093. output, olen ) ) != 0 )
  1094. return( ret );
  1095. if( ( ret = mbedtls_cipher_finish( ctx, output + *olen,
  1096. &finish_olen ) ) != 0 )
  1097. return( ret );
  1098. *olen += finish_olen;
  1099. return( 0 );
  1100. }
  1101. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  1102. /*
  1103. * Packet-oriented encryption for AEAD modes: internal function used by
  1104. * mbedtls_cipher_auth_encrypt_ext().
  1105. */
  1106. static int mbedtls_cipher_aead_encrypt( mbedtls_cipher_context_t *ctx,
  1107. const unsigned char *iv, size_t iv_len,
  1108. const unsigned char *ad, size_t ad_len,
  1109. const unsigned char *input, size_t ilen,
  1110. unsigned char *output, size_t *olen,
  1111. unsigned char *tag, size_t tag_len )
  1112. {
  1113. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1114. if( ctx->psa_enabled == 1 )
  1115. {
  1116. /* As in the non-PSA case, we don't check that
  1117. * a key has been set. If not, the key slot will
  1118. * still be in its default state of 0, which is
  1119. * guaranteed to be invalid, hence the PSA-call
  1120. * below will gracefully fail. */
  1121. mbedtls_cipher_context_psa * const cipher_psa =
  1122. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  1123. psa_status_t status;
  1124. /* PSA Crypto API always writes the authentication tag
  1125. * at the end of the encrypted message. */
  1126. if( output == NULL || tag != output + ilen )
  1127. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1128. status = psa_aead_encrypt( cipher_psa->slot,
  1129. cipher_psa->alg,
  1130. iv, iv_len,
  1131. ad, ad_len,
  1132. input, ilen,
  1133. output, ilen + tag_len, olen );
  1134. if( status != PSA_SUCCESS )
  1135. return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
  1136. *olen -= tag_len;
  1137. return( 0 );
  1138. }
  1139. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1140. #if defined(MBEDTLS_GCM_C)
  1141. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  1142. {
  1143. *olen = ilen;
  1144. return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
  1145. ilen, iv, iv_len, ad, ad_len,
  1146. input, output, tag_len, tag ) );
  1147. }
  1148. #endif /* MBEDTLS_GCM_C */
  1149. #if defined(MBEDTLS_CCM_C)
  1150. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  1151. {
  1152. *olen = ilen;
  1153. return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
  1154. iv, iv_len, ad, ad_len, input, output,
  1155. tag, tag_len ) );
  1156. }
  1157. #endif /* MBEDTLS_CCM_C */
  1158. #if defined(MBEDTLS_CHACHAPOLY_C)
  1159. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  1160. {
  1161. /* ChachaPoly has fixed length nonce and MAC (tag) */
  1162. if ( ( iv_len != ctx->cipher_info->iv_size ) ||
  1163. ( tag_len != 16U ) )
  1164. {
  1165. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1166. }
  1167. *olen = ilen;
  1168. return( mbedtls_chachapoly_encrypt_and_tag( ctx->cipher_ctx,
  1169. ilen, iv, ad, ad_len, input, output, tag ) );
  1170. }
  1171. #endif /* MBEDTLS_CHACHAPOLY_C */
  1172. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1173. }
  1174. /*
  1175. * Packet-oriented encryption for AEAD modes: internal function used by
  1176. * mbedtls_cipher_auth_encrypt_ext().
  1177. */
  1178. static int mbedtls_cipher_aead_decrypt( mbedtls_cipher_context_t *ctx,
  1179. const unsigned char *iv, size_t iv_len,
  1180. const unsigned char *ad, size_t ad_len,
  1181. const unsigned char *input, size_t ilen,
  1182. unsigned char *output, size_t *olen,
  1183. const unsigned char *tag, size_t tag_len )
  1184. {
  1185. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1186. if( ctx->psa_enabled == 1 )
  1187. {
  1188. /* As in the non-PSA case, we don't check that
  1189. * a key has been set. If not, the key slot will
  1190. * still be in its default state of 0, which is
  1191. * guaranteed to be invalid, hence the PSA-call
  1192. * below will gracefully fail. */
  1193. mbedtls_cipher_context_psa * const cipher_psa =
  1194. (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
  1195. psa_status_t status;
  1196. /* PSA Crypto API always writes the authentication tag
  1197. * at the end of the encrypted message. */
  1198. if( input == NULL || tag != input + ilen )
  1199. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1200. status = psa_aead_decrypt( cipher_psa->slot,
  1201. cipher_psa->alg,
  1202. iv, iv_len,
  1203. ad, ad_len,
  1204. input, ilen + tag_len,
  1205. output, ilen, olen );
  1206. if( status == PSA_ERROR_INVALID_SIGNATURE )
  1207. return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
  1208. else if( status != PSA_SUCCESS )
  1209. return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
  1210. return( 0 );
  1211. }
  1212. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1213. #if defined(MBEDTLS_GCM_C)
  1214. if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
  1215. {
  1216. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1217. *olen = ilen;
  1218. ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
  1219. iv, iv_len, ad, ad_len,
  1220. tag, tag_len, input, output );
  1221. if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
  1222. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1223. return( ret );
  1224. }
  1225. #endif /* MBEDTLS_GCM_C */
  1226. #if defined(MBEDTLS_CCM_C)
  1227. if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
  1228. {
  1229. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1230. *olen = ilen;
  1231. ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
  1232. iv, iv_len, ad, ad_len,
  1233. input, output, tag, tag_len );
  1234. if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
  1235. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1236. return( ret );
  1237. }
  1238. #endif /* MBEDTLS_CCM_C */
  1239. #if defined(MBEDTLS_CHACHAPOLY_C)
  1240. if ( MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type )
  1241. {
  1242. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1243. /* ChachaPoly has fixed length nonce and MAC (tag) */
  1244. if ( ( iv_len != ctx->cipher_info->iv_size ) ||
  1245. ( tag_len != 16U ) )
  1246. {
  1247. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1248. }
  1249. *olen = ilen;
  1250. ret = mbedtls_chachapoly_auth_decrypt( ctx->cipher_ctx, ilen,
  1251. iv, ad, ad_len, tag, input, output );
  1252. if( ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED )
  1253. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  1254. return( ret );
  1255. }
  1256. #endif /* MBEDTLS_CHACHAPOLY_C */
  1257. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1258. }
  1259. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  1260. #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
  1261. /*
  1262. * Packet-oriented encryption for AEAD/NIST_KW: public function.
  1263. */
  1264. int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
  1265. const unsigned char *iv, size_t iv_len,
  1266. const unsigned char *ad, size_t ad_len,
  1267. const unsigned char *input, size_t ilen,
  1268. unsigned char *output, size_t output_len,
  1269. size_t *olen, size_t tag_len )
  1270. {
  1271. CIPHER_VALIDATE_RET( ctx != NULL );
  1272. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  1273. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  1274. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  1275. CIPHER_VALIDATE_RET( output != NULL );
  1276. CIPHER_VALIDATE_RET( olen != NULL );
  1277. #if defined(MBEDTLS_NIST_KW_C)
  1278. if(
  1279. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1280. ctx->psa_enabled == 0 &&
  1281. #endif
  1282. ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
  1283. MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
  1284. {
  1285. mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
  1286. MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
  1287. /* There is no iv, tag or ad associated with KW and KWP,
  1288. * so these length should be 0 as documented. */
  1289. if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
  1290. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1291. (void) iv;
  1292. (void) ad;
  1293. return( mbedtls_nist_kw_wrap( ctx->cipher_ctx, mode, input, ilen,
  1294. output, olen, output_len ) );
  1295. }
  1296. #endif /* MBEDTLS_NIST_KW_C */
  1297. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  1298. /* AEAD case: check length before passing on to shared function */
  1299. if( output_len < ilen + tag_len )
  1300. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1301. int ret = mbedtls_cipher_aead_encrypt( ctx, iv, iv_len, ad, ad_len,
  1302. input, ilen, output, olen,
  1303. output + ilen, tag_len );
  1304. *olen += tag_len;
  1305. return( ret );
  1306. #else
  1307. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1308. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  1309. }
  1310. /*
  1311. * Packet-oriented decryption for AEAD/NIST_KW: public function.
  1312. */
  1313. int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
  1314. const unsigned char *iv, size_t iv_len,
  1315. const unsigned char *ad, size_t ad_len,
  1316. const unsigned char *input, size_t ilen,
  1317. unsigned char *output, size_t output_len,
  1318. size_t *olen, size_t tag_len )
  1319. {
  1320. CIPHER_VALIDATE_RET( ctx != NULL );
  1321. CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
  1322. CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
  1323. CIPHER_VALIDATE_RET( ilen == 0 || input != NULL );
  1324. CIPHER_VALIDATE_RET( output_len == 0 || output != NULL );
  1325. CIPHER_VALIDATE_RET( olen != NULL );
  1326. #if defined(MBEDTLS_NIST_KW_C)
  1327. if(
  1328. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1329. ctx->psa_enabled == 0 &&
  1330. #endif
  1331. ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ||
  1332. MBEDTLS_MODE_KWP == ctx->cipher_info->mode ) )
  1333. {
  1334. mbedtls_nist_kw_mode_t mode = ( MBEDTLS_MODE_KW == ctx->cipher_info->mode ) ?
  1335. MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP;
  1336. /* There is no iv, tag or ad associated with KW and KWP,
  1337. * so these length should be 0 as documented. */
  1338. if( iv_len != 0 || tag_len != 0 || ad_len != 0 )
  1339. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1340. (void) iv;
  1341. (void) ad;
  1342. return( mbedtls_nist_kw_unwrap( ctx->cipher_ctx, mode, input, ilen,
  1343. output, olen, output_len ) );
  1344. }
  1345. #endif /* MBEDTLS_NIST_KW_C */
  1346. #if defined(MBEDTLS_CIPHER_MODE_AEAD)
  1347. /* AEAD case: check length before passing on to shared function */
  1348. if( ilen < tag_len || output_len < ilen - tag_len )
  1349. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  1350. return( mbedtls_cipher_aead_decrypt( ctx, iv, iv_len, ad, ad_len,
  1351. input, ilen - tag_len, output, olen,
  1352. input + ilen - tag_len, tag_len ) );
  1353. #else
  1354. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  1355. #endif /* MBEDTLS_CIPHER_MODE_AEAD */
  1356. }
  1357. #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
  1358. #endif /* MBEDTLS_CIPHER_C */