cipher.c 50 KB

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