From patchwork Wed Aug 23 07:41:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gilad Ben-Yossef X-Patchwork-Id: 110725 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp3761690qge; Wed, 23 Aug 2017 00:41:22 -0700 (PDT) X-Received: by 10.99.140.13 with SMTP id m13mr1728950pgd.333.1503474082258; Wed, 23 Aug 2017 00:41:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503474082; cv=none; d=google.com; s=arc-20160816; b=IOLnd0wqOLs0FyIbVvSXGuTUfT5klv3vPpDqYDVEU9RlBGgnLJeO2PmXssbiAjHgGD VgZYkDMLAxO5mRlAJJ5nLBz42V8EaErXd9AH6q+ZKjM4YH4lc6/VizDE1vIQ+5mmWC1l BmH0CSeYtvDgMguwrqN/cJyY+Tr82nTBgjWgVTu3ROkSU9JKmQQ9ztI5wzxet9BJ1m2N PLrLY58zzxZfmYbXbuK//6T5f53KOLFpPi8nLvJ9EowzyCDLq+9DEYhF67mOB8kVJhbh Pj/Mhz3kLNVjYUXAB80XV6VBvtVs7UzMRjmivl9TJ47D/Bhn4+4me1LnYpi95T3UB9ui Imuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=aDvu1tnOCQnlOpVA+u3pDf2lyhfFw4jQN1K3Lu2j994=; b=ufwg007pC+eIfkIUbG2647seZMMi1QGHffK4BoNA93saIJo158WSQT1Cz/OkcakrZR 6zbxjuzOPNuJa7Z4ZfnRQShjWSv360g5wO/uZWEBF1VIAqviYSMwdt2IH1Tlezd27Oaa KdRFRhjDLHQiXoerPQfbwV+GjFpfx1W9aNrStbgVZZRhLPTm9wZCcDyuegg8CL5rUyr9 eEG13DNGh/EyO4ZWTRh/e/uq3IYrw13ZPYE2eElaHbcG5Tt8mPXqusz7187/fDwaMxEM wiOlPDCXbY9Kwfa/OuxSjws+pIjxV4Ku1C+jc9Z8jHBnniC74GSZDft8//3Pjjux6pXb uvLw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f16si694379plk.484.2017.08.23.00.41.22; Wed, 23 Aug 2017 00:41:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753430AbdHWHlU (ORCPT + 1 other); Wed, 23 Aug 2017 03:41:20 -0400 Received: from foss.arm.com ([217.140.101.70]:52866 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753329AbdHWHlU (ORCPT ); Wed, 23 Aug 2017 03:41:20 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D51C313D5; Wed, 23 Aug 2017 00:41:19 -0700 (PDT) Received: from localhost.localdomain (usa-sjc-mx-foss1.foss.arm.com [217.140.101.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1BFA73F577; Wed, 23 Aug 2017 00:41:17 -0700 (PDT) From: Gilad Ben-Yossef To: Greg Kroah-Hartman , linux-crypto@vger.kernel.org, driverdev-devel@linuxdriverproject.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Cc: Ofir Drang Subject: [BUGFIX PATCH] staging: ccree: save ciphertext for CTS IV Date: Wed, 23 Aug 2017 10:41:11 +0300 Message-Id: <1503474071-7049-1-git-send-email-gilad@benyossef.com> X-Mailer: git-send-email 2.1.4 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The crypto API requires saving the last blocks of ciphertext in req->info for use as IV for CTS mode. The ccree driver was not doing this. This patch fixes that. The bug was manifested with cts(cbc(aes)) mode in tcrypt tests. Fixes: 302ef8ebb4b2 ("Add CryptoCell skcipher support") Signed-off-by: Gilad Ben-Yossef --- drivers/staging/ccree/ssi_cipher.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) -- 2.1.4 diff --git a/drivers/staging/ccree/ssi_cipher.c b/drivers/staging/ccree/ssi_cipher.c index af9afea..01011a2 100644 --- a/drivers/staging/ccree/ssi_cipher.c +++ b/drivers/staging/ccree/ssi_cipher.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "ssi_config.h" #include "ssi_driver.h" @@ -697,6 +698,7 @@ static int ssi_blkcipher_complete(struct device *dev, { int completion_error = 0; u32 inflight_counter; + struct ablkcipher_request *req = (struct ablkcipher_request *)areq; ssi_buffer_mgr_unmap_blkcipher_request(dev, req_ctx, ivsize, src, dst); @@ -707,6 +709,22 @@ static int ssi_blkcipher_complete(struct device *dev, ctx_p->drvdata->inflight_counter--; if (areq) { + /* + * The crypto API expects us to set the req->info to the last + * ciphertext block. For encrypt, simply copy from the result. + * For decrypt, we must copy from a saved buffer since this + * could be an in-place decryption operation and the src is + * lost by this point. + */ + if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) { + memcpy(req->info, req_ctx->backup_info, ivsize); + kfree(req_ctx->backup_info); + } else { + scatterwalk_map_and_copy(req->info, req->dst, + (req->nbytes - ivsize), + ivsize, 0); + } + ablkcipher_request_complete(areq, completion_error); return 0; } @@ -858,7 +876,6 @@ static int ssi_ablkcipher_encrypt(struct ablkcipher_request *req) struct blkcipher_req_ctx *req_ctx = ablkcipher_request_ctx(req); unsigned int ivsize = crypto_ablkcipher_ivsize(ablk_tfm); - req_ctx->backup_info = req->info; req_ctx->is_giv = false; return ssi_blkcipher_process(tfm, req_ctx, req->dst, req->src, req->nbytes, req->info, ivsize, (void *)req, DRV_CRYPTO_DIRECTION_ENCRYPT); @@ -871,8 +888,18 @@ static int ssi_ablkcipher_decrypt(struct ablkcipher_request *req) struct blkcipher_req_ctx *req_ctx = ablkcipher_request_ctx(req); unsigned int ivsize = crypto_ablkcipher_ivsize(ablk_tfm); - req_ctx->backup_info = req->info; + /* + * Allocate and save the last IV sized bytes of the source, which will + * be lost in case of in-place decryption and might be needed for CTS. + */ + req_ctx->backup_info = kmalloc(ivsize, GFP_KERNEL); + if (!req_ctx->backup_info) + return -ENOMEM; + + scatterwalk_map_and_copy(req_ctx->backup_info, req->src, + (req->nbytes - ivsize), ivsize, 0); req_ctx->is_giv = false; + return ssi_blkcipher_process(tfm, req_ctx, req->dst, req->src, req->nbytes, req->info, ivsize, (void *)req, DRV_CRYPTO_DIRECTION_DECRYPT); }