cipher.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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 */