Lines Matching refs:ctx

201 void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
203 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
206 void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
208 if (ctx == NULL) {
213 if (ctx->psa_enabled == 1) {
214 if (ctx->cipher_ctx != NULL) {
216 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
226 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
232 if (ctx->cmac_ctx) {
233 mbedtls_zeroize_and_free(ctx->cmac_ctx,
238 if (ctx->cipher_ctx) {
239 mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx);
242 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
245 int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx,
252 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
255 ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func();
256 if (ctx->cipher_ctx == NULL) {
261 ctx->cipher_info = cipher_info;
267 int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx,
274 if (NULL == cipher_info || NULL == ctx) {
288 memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
295 ctx->cipher_ctx = cipher_psa;
296 ctx->cipher_info = cipher_info;
297 ctx->psa_enabled = 1;
302 int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx,
310 if (ctx->cipher_info == NULL) {
314 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) &&
321 if (ctx->psa_enabled == 1) {
323 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
342 ((mbedtls_cipher_type_t) ctx->cipher_info->type));
372 ctx->key_bitlen = key_bitlen;
373 ctx->operation = operation;
378 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
379 (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) {
383 ctx->key_bitlen = key_bitlen;
384 ctx->operation = operation;
391 MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
392 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
393 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
394 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
395 ctx->key_bitlen);
399 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key,
400 ctx->key_bitlen);
404 return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key,
405 ctx->key_bitlen);
412 int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
418 if (ctx->cipher_info == NULL) {
422 if (ctx->psa_enabled == 1) {
430 /* avoid buffer overflow in ctx->iv */
435 if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) {
438 actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info);
447 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) {
455 if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx,
462 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 &&
470 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
471 return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx,
472 ctx->operation,
478 if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
483 (mbedtls_ccm_context *) ctx->cipher_ctx,
489 if (ctx->operation == MBEDTLS_DECRYPT) {
491 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
497 return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx,
504 memcpy(ctx->iv, iv, actual_iv_size);
505 ctx->iv_size = actual_iv_size;
511 int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
513 if (ctx->cipher_info == NULL) {
518 if (ctx->psa_enabled == 1) {
525 ctx->unprocessed_len = 0;
531 int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
534 if (ctx->cipher_info == NULL) {
539 if (ctx->psa_enabled == 1) {
548 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
549 return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx,
555 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
559 mode = (ctx->operation == MBEDTLS_ENCRYPT)
563 result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx,
564 ctx->iv,
570 return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx,
579 int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
585 if (ctx->cipher_info == NULL) {
590 if (ctx->psa_enabled == 1) {
599 block_size = mbedtls_cipher_get_block_size(ctx);
604 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) {
611 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx,
612 ctx->operation, input,
621 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) {
622 return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx,
629 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) {
630 return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx,
637 if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
639 return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx,
645 (ctx->unprocessed_len != 0 || ilen % block_size)) {
650 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) {
656 if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
657 ilen <= block_size - ctx->unprocessed_len) ||
658 (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
659 ilen < block_size - ctx->unprocessed_len) ||
660 (ctx->operation == MBEDTLS_ENCRYPT &&
661 ilen < block_size - ctx->unprocessed_len)) {
662 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
665 ctx->unprocessed_len += ilen;
672 if (0 != ctx->unprocessed_len) {
673 copy_len = block_size - ctx->unprocessed_len;
675 memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
678 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
679 ctx->operation,
680 block_size, ctx->iv,
681 ctx->
689 ctx->unprocessed_len = 0;
705 ctx->operation == MBEDTLS_DECRYPT &&
706 NULL != ctx->add_padding) {
710 memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]),
713 ctx->unprocessed_len += copy_len;
721 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
722 ctx->operation,
723 ilen, ctx->iv,
737 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) {
738 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx,
739 ctx->operation, ilen,
740 &ctx->unprocessed_len,
741 ctx->iv,
753 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) {
754 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx,
756 &ctx->unprocessed_len,
757 ctx->iv,
769 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) {
770 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx,
772 &ctx->unprocessed_len,
773 ctx->iv,
774 ctx->unprocessed_data,
786 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) {
787 if (ctx->unprocessed_len > 0) {
792 ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx,
793 ctx->operation,
795 ctx->iv,
809 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) {
810 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx,
1011 int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
1014 if (ctx->cipher_info == NULL) {
1019 if (ctx->psa_enabled == 1) {
1032 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1033 if (ctx->get_padding == NULL) {
1039 if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1040 MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1041 MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1042 MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1043 MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1044 MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1045 MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1049 if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) ||
1050 (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) {
1054 if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1055 if (ctx->unprocessed_len != 0) {
1063 if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1066 if (MBEDTLS_ENCRYPT == ctx->operation) {
1068 if (NULL == ctx->add_padding) {
1069 if (0 != ctx->unprocessed_len) {
1076 ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx),
1077 ctx->unprocessed_len);
1078 } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) {
1083 if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) {
1091 if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx,
1092 ctx->operation,
1094 ctx),
1095 ctx->iv,
1096 ctx->unprocessed_data,
1102 if (MBEDTLS_DECRYPT == ctx->operation) {
1103 return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx),
1108 *olen = mbedtls_cipher_get_block_size(ctx);
1119 int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx,
1122 if (NULL == ctx->cipher_info ||
1123 MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1128 if (ctx->psa_enabled == 1) {
1143 ctx->add_padding = add_pkcs_padding;
1144 ctx->get_padding = get_pkcs_padding;
1149 ctx->add_padding = add_one_and_zeros_padding;
1150 ctx->get_padding = get_one_and_zeros_padding;
1155 ctx->add_padding = add_zeros_and_len_padding;
1156 ctx->get_padding = get_zeros_and_len_padding;
1161 ctx->add_padding = add_zeros_padding;
1162 ctx->get_padding = get_zeros_padding;
1166 ctx->add_padding = NULL;
1167 ctx->get_padding = get_no_padding;
1179 int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
1182 if (ctx->cipher_info == NULL) {
1186 if (MBEDTLS_ENCRYPT != ctx->operation) {
1191 if (ctx->psa_enabled == 1) {
1200 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1204 return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx,
1211 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
1218 (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag);
1225 int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
1231 if (ctx->cipher_info == NULL) {
1235 if (MBEDTLS_DECRYPT != ctx->operation) {
1240 if (ctx->psa_enabled == 1) {
1252 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1262 (mbedtls_gcm_context *) ctx->cipher_ctx,
1277 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
1284 (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag);
1306 int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
1315 if (ctx->psa_enabled == 1) {
1322 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1328 if (ctx->operation == MBEDTLS_DECRYPT) {
1332 } else if (ctx->operation == MBEDTLS_ENCRYPT) {
1348 if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) {
1374 if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) {
1378 if ((ret = mbedtls_cipher_reset(ctx)) != 0) {
1382 if ((ret = mbedtls_cipher_update(ctx, input, ilen,
1387 if ((ret = mbedtls_cipher_finish(ctx, output + *olen,
1402 static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx,
1410 if (ctx->psa_enabled == 1) {
1417 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1443 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1445 return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT,
1451 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1453 return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen,
1459 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
1461 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
1467 return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx,
1479 static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx,
1487 if (ctx->psa_enabled == 1) {
1494 (mbedtls_cipher_context_psa *) ctx->cipher_ctx;
1521 if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1525 ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen,
1537 if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) {
1541 ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen,
1553 if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) {
1557 if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) ||
1563 ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen,
1582 int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx,
1592 ctx->psa_enabled == 0 &&
1594 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1595 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1597 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1609 return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen,
1620 int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len,
1633 int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx,
1643 ctx->psa_enabled == 0 &&
1645 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) ||
1646 MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) {
1648 (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ?
1660 return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen,
1671 return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len,