Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp467460ybv; Fri, 7 Feb 2020 02:47:44 -0800 (PST) X-Google-Smtp-Source: APXvYqz0MWr9lMhquDLc/uHHu/ZtNYEPA2AlD12oDD+imbOxurCFqt7xyFnzQsnj/+/N4l8YiMIU X-Received: by 2002:a54:4816:: with SMTP id j22mr1617770oij.179.1581072464267; Fri, 07 Feb 2020 02:47:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581072464; cv=none; d=google.com; s=arc-20160816; b=BTmRmigjgbbXQJPk5wWa/8Enlnyo/WnRDovOHI781hRHVtkE1jNeuNexXMk5DOtr5F GnwBHT+bSwJYJEoa/3nScao1Wv4oo6XHKjK2XdVQdrtvUrkB/TR7dRTkQzXyoVWhPI5J Mw9vd63NJcJ/YOqOrt8eACNkgdu819OCmZtRd7Cx1ujtsSwRmudGdzrmEi0m3Zd1qJfe lVV116is59zmiMSFLZKKz4xeBb2o4wpmpue6X7ziwuqIUsmZgakhDRO8YS2Kf7Pz+DPc lS36S6M5eefWAm58zdgjfnLaHbiNcehRn4MSMileaWq6vW0mJeLb0u/Smkxo1V9t2nGt AFug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=vihhkM3cjNfJ7+zDL+cP7K8N6569xYSRREcWllK4iDk=; b=an/PYBBvcm4QSHr4r7lNKmwyYe0XLut2WrSpN8PfJeUhBxeK4ZSdEhz778f/BuVy1J qUBUsW92BGOrAGS2ynnstG44hXLKDpEsxc3Bhht1eTBQLAO6+YvnFFCPpE0EqzR91vey lmsBXji4McZCqjOjLeBu8GvnHeJBsbSckQ13+3Gzhx1/wve3cau+ao3uOqs2Ou7/6AXg RyZHnqWBiRBKnBxSDgNPd0U7RsZ/tmyokMdD6AVCRZcjgPBa6MHzk7MblfhitLekAsZP gI3XOKKIQRp44kxp6/QsQH+xDtRU6DLCLjMmpWdnLEH/xO/b872ehorpB8KnA7wUy3lr YcJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=cz036zNL; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w17si3540463oiw.127.2020.02.07.02.47.23; Fri, 07 Feb 2020 02:47:44 -0800 (PST) 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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=cz036zNL; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726974AbgBGKrE (ORCPT + 99 others); Fri, 7 Feb 2020 05:47:04 -0500 Received: from mail-wr1-f54.google.com ([209.85.221.54]:40407 "EHLO mail-wr1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726587AbgBGKrE (ORCPT ); Fri, 7 Feb 2020 05:47:04 -0500 Received: by mail-wr1-f54.google.com with SMTP id t3so2070067wru.7; Fri, 07 Feb 2020 02:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=vihhkM3cjNfJ7+zDL+cP7K8N6569xYSRREcWllK4iDk=; b=cz036zNLcNTQwykCoPwezXZPyqpwU18OJIT8S2CVfBz3uJK0kbO2cf0KetM/z7B1rz qiyLiWYhbNdzg72QELONo4iksAILZyy68zADFAytRaGaY1h/l9+CS0pRLPyIDmGI0Yix If6MrvLczQIHQe62VCSvHU2LVg8a6cwdRfJ+iY1f6QHVOdguc6hsUgRHRWNGtsWFqCYH Cmjp5RAItXO/iZvhR07MNkNCcZNSKnzBfiPAc/i47rI0Zf5Wq3azWXmhRqFHESsSDoB6 hwrBve8TOtf8GfBB4qqlQmJjp9rHYTWdLhRL9HHByG9js57yT4lX0o4aXpTcn/OT1Mf0 wZ9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=vihhkM3cjNfJ7+zDL+cP7K8N6569xYSRREcWllK4iDk=; b=E/I1yf5nk1m+E2Gmas3ZEIrHchc/pArYVMyHwc866uCWDgq8iZoFTOBHaOcP/4doLd p2h2fOBm4gA6kJ91WnTAm2q/8yAiVhrmHjkKZKXCbIkoeZRRCQJw4tiJ1xmcAuK0MVYe zyvmlGaBQyF6IoqiD0o6KCSB7wzLthWRnrF+SwwDyBjaBrf+rvWPb204+APJLyHAUHYr I9fIY2lsYH6bmkgxlI2lLjxdtIspzWDTyX2jkCWl9uxg8bOkaOfcbArZ8Rsa5eoXTM3v pE/ibHah5JfVWPkXyuhqoxYR8vQWRUch5Uh5Nq7NFKzPmKkUWvjk6GlFC8E+nCiwGqp0 vpRA== X-Gm-Message-State: APjAAAVnAitbGgMLm9fOohjZq7/5Q/80G2ylbqSh8NqLa9APmWNrIGuZ dODy8FDkK6q5hc46WFmxU+W92sm8 X-Received: by 2002:adf:ab49:: with SMTP id r9mr4175556wrc.351.1581072421527; Fri, 07 Feb 2020 02:47:01 -0800 (PST) Received: from Red ([2a01:cb1d:3d5:a100:2e56:dcff:fed2:c6d6]) by smtp.googlemail.com with ESMTPSA id m9sm2870038wrx.55.2020.02.07.02.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2020 02:47:01 -0800 (PST) Date: Fri, 7 Feb 2020 11:46:59 +0100 From: Corentin Labbe To: Eric Biggers Cc: herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [BUG] crypto: export() overran state buffer on test vector Message-ID: <20200207104659.GA10979@Red> References: <20200206085442.GA5585@Red> <20200207065719.GA8284@sol.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200207065719.GA8284@sol.localdomain> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Thu, Feb 06, 2020 at 10:57:19PM -0800, Eric Biggers wrote: > On Thu, Feb 06, 2020 at 09:54:42AM +0100, Corentin Labbe wrote: > > Hello > > > > When working on adding hash support on sun8i-ce, I made a simple version which always fallback. > > but booting it lead to this: > > [ 52.274278] sun8i-ce 1c15000.crypto: Register sha1 > > [ 52.279286] sun8i-ce 1c15000.crypto: sun8i_hash_crainit statesize is 96 > > [ 52.285933] sun8i-ce 1c15000.crypto: Fallback for sha1-sun8i-ce is sha1-ce > > [ 52.312423] shash_default_export descsize=104 > > [ 52.316021] alg: ahash: sha1-sun8i-ce export() overran state buffer on test vector 0, cfg=\"import/export\" statesize=96 > > [ 52.333189] sun8i-ce 1c15000.crypto: Register sha224 > > [ 52.338387] sun8i-ce 1c15000.crypto: sun8i_hash_crainit statesize is 104 > > [ 52.345097] sun8i-ce 1c15000.crypto: Fallback for sha224-sun8i-ce is sha224-ce > > [ 52.371865] shash_default_export descsize=112 > > [ 52.375459] alg: ahash: sha224-sun8i-ce export() overran state buffer on test vector 0, cfg=\"import/export\" statesize=104 > > [ 52.393039] sun8i-ce 1c15000.crypto: Register sha256 > > [ 52.398219] sun8i-ce 1c15000.crypto: sun8i_hash_crainit statesize is 104 > > [ 52.404937] sun8i-ce 1c15000.crypto: Fallback for sha256-sun8i-ce is sha256-ce > > [ 52.431476] shash_default_export descsize=112 > > [ 52.435073] alg: ahash: sha256-sun8i-ce export() overran state buffer on test vector 0, cfg=\"import/export\" statesize=104 > > > > For sha1, sha224 and sha256, my driver fail to pass the test. > > This is due to the fact that export() (and so shash_async_export/shash_default_export) use crypto_shash_descsize() as length but selftest expect it to be statesize. > > That doesn't appear to actually be the problem. shash_default_export() does > assume descsize == statesize, but it's only used when that's the case. > See shash_prepare_alg(). > > > Just in case, this is my export code: > > int sun8i_hash_crainit(struct crypto_tfm *tfm) > > { > > struct sun8i_hash_tfm_ctx *op = crypto_tfm_ctx(tfm); > > struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg); > > struct sun8i_ce_alg_template *algt; > > > > memset(op, 0, sizeof(struct sun8i_hash_tfm_ctx)); > > > > crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), sizeof(struct sun8i_hash_reqctx)); > > > > op->fallback_tfm = crypto_alloc_ahash(crypto_tfm_alg_name(tfm), 0, CRYPTO_ALG_NEED_FALLBACK); > > if (IS_ERR(op->fallback_tfm)) { > > dev_err(algt->ce->dev, "Fallback driver cound no be loaded\n"); > > return PTR_ERR(op->fallback_tfm); > > } > > dev_info(op->ce->dev, "%s statesize is %u\n", __func__, algt->alg.hash.halg.statesize); > > dev_info(op->ce->dev, "Fallback for %s is %s\n", > > crypto_tfm_alg_driver_name(tfm), > > crypto_tfm_alg_driver_name(&op->fallback_tfm->base)); > > return 0; > > } > > > > int sun8i_hash_init(struct ahash_request *areq) > > { > > struct sun8i_hash_reqctx *rctx = ahash_request_ctx(areq); > > struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); > > struct sun8i_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm); > > > > memset(rctx, 0, sizeof(struct sun8i_hash_reqctx)); > > > > ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm); > > rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP; > > > > return crypto_ahash_init(&rctx->fallback_req); > > } > > > > int sun8i_hash_export(struct ahash_request *areq, void *out) > > { > > struct sun8i_hash_reqctx *rctx = ahash_request_ctx(areq); > > struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); > > struct sun8i_hash_tfm_ctx *tfmctx = crypto_ahash_ctx(tfm); > > > > ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm); > > rctx->fallback_req.base.flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP; > > > > return crypto_ahash_export(&rctx->fallback_req, out); > > } > > It seems the actual problem is that you're doing the export using a fallback > algorithm, which may have a statesize different from the one you're setting. > Should not be a problem since all stuff before export is done by the fallback. > But I'm not sure what you should do here, since the correct statesize can only > be known when a tfm is allocated, not when the algorithm is registered. > > Possibly statesize needs to be made a property of the tfm (struct crypto_ahash > and crypto_shash) rather than the algorithm (struct hash_alg_common). > > But are you sure you actually need a fallback algorithm for any hash algorithms > in your driver in the first place? > My goal is to do like n2-crypto/rk3288crypto/etc..., fallback for init/update/final/finup and only do stuff with digest(). So I have just exactly copied what they do. Regards