cipher.c 52 KB

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