cipher.c 51 KB

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