From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72EB74A9F for ; Thu, 17 Mar 2022 20:56:22 +0000 (UTC) Received: by mail-wr1-f50.google.com with SMTP id h23so8409317wrb.8 for ; Thu, 17 Mar 2022 13:56:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VPG5xdEe7CwrsSmHCV/4usRyLbFbgTbMpwY3twMAA2c=; b=YoZ5/aT2zO6zC4OhNOsVH9dqiz98KfdADE+JUelGribOhOMJN6b9ZnmRTC9PERTvse LfaI32yvJxMGJoFUymSyQmkrs7fWNqGHayYWSMFlFENjGhgRSLCVCEHDx/n0c43qbAyQ fkWA67FW/tL8EOz5YtoMUqiUnquIS6aJdZu+xHJfoiLk4A7UHXQpYhOorYHpoIt7lv4G IEDIoP34BLGcv4aQZ40Q3+ZBXPMuvO/9S37RQkQt3zm7KzJZqxG5ittaDzBLEJDQCRaC vBvwgckYJOmwTYER7SoDrdc4PY0haLWK7KOaZR7XV/pnW3KCqgDH8tzbYF2F6VtFLYyj ZlXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VPG5xdEe7CwrsSmHCV/4usRyLbFbgTbMpwY3twMAA2c=; b=kEvByQfPQE7y9NTkcfbr6c+uFv7+prVebP3YmGLLsX4IrNnh2aPBr79/cMZ4pM24ry 5RVW8OyL3qqEDkOszotNlDfvrz7FVhA91vL6xhty1nwOb+AFrTgdL1QkV+MN7qSpM35e ZCA6/H1+uJudY1jJ3UuPrsVafbC06IpEMvtuBC+5wf1oyORthaXuMKva7GFlRQuilbGV BfG0EJk6hw8VmV1/dp5ZpQdU7An3jzy+R8aExsU2S7SWbwI05SQisuXoQ6iEMuD0oItF xkFgCzLS0Wq1FEqfuWopITzwCT3nBZc/DETUDGa3YreXIebMb1m4OZlJNkC9OTU3/k6U nggw== X-Gm-Message-State: AOAM530CCwBFlQJRrWXXNyDTCvx0HWFHt8XnLJoJdzYKxIMOP9cLAlCq Xql2jrseKXwgI0lB2of6WaUWXA== X-Google-Smtp-Source: ABdhPJxfg+Ya5WTbS08Kg64X3jLz38a3MEx09xKt5tFJFO95djJRsRFQg2KJ0+opA5SEpZjf+jS25w== X-Received: by 2002:a5d:47c8:0:b0:1ef:8e97:2b8c with SMTP id o8-20020a5d47c8000000b001ef8e972b8cmr5733811wrc.545.1647550580732; Thu, 17 Mar 2022 13:56:20 -0700 (PDT) Received: from localhost.localdomain (laubervilliers-658-1-213-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.googlemail.com with ESMTPSA id r4-20020a05600c35c400b00389f368cf1esm3695424wmq.40.2022.03.17.13.56.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Mar 2022 13:56:20 -0700 (PDT) From: Corentin Labbe To: herbert@gondor.apana.org.au, jernej.skrabec@gmail.com, samuel@sholland.org, wens@csie.org Cc: linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sunxi@lists.linux.dev, Corentin Labbe Subject: [PATCH 13/19] crypto: sun8i-ss: Add function for handling hash padding Date: Thu, 17 Mar 2022 20:55:59 +0000 Message-Id: <20220317205605.3924836-14-clabbe@baylibre.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220317205605.3924836-1-clabbe@baylibre.com> References: <20220317205605.3924836-1-clabbe@baylibre.com> Precedence: bulk X-Mailing-List: linux-sunxi@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Move all padding work to a dedicated function. Signed-off-by: Corentin Labbe --- .../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 87 ++++++++++++++----- 1 file changed, 65 insertions(+), 22 deletions(-) diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c index cb510ec21ec4..6adb6a0428b7 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c @@ -328,6 +328,64 @@ int sun8i_ss_hash_digest(struct ahash_request *areq) return crypto_transfer_hash_request_to_engine(engine, areq); } +static u64 hash_pad(u32 *buf, unsigned int bufsize, u64 padi, u64 byte_count, bool le, int bs) +{ + u64 fill, min_fill, j, k; + __be64 *bebits; + __le64 *lebits; + + j = padi; + buf[j++] = cpu_to_le32(0x80); + + if (bs == 64) { + fill = 64 - (byte_count % 64); + min_fill = 2 * sizeof(u32) + sizeof(u32); + } else { + fill = 128 - (byte_count % 128); + min_fill = 4 * sizeof(u32) + sizeof(u32); + } + + if (fill < min_fill) + fill += bs; + + k = j; + j += (fill - min_fill) / sizeof(u32); + if (j * 4 > bufsize) { + pr_err("%s OVERFLOW %llu\n", __func__, j); + return 0; + } + for (; k < j; k++) + buf[k] = 0; + + if (le) { + /* MD5 */ + lebits = (__le64 *)&buf[j]; + *lebits = cpu_to_le64(byte_count << 3); + j += 2; + } else { + if (bs == 64) { + /* sha1 sha224 sha256 */ + bebits = (__be64 *)&buf[j]; + *bebits = cpu_to_be64(byte_count << 3); + j += 2; + } else { + /* sha384 sha512*/ + bebits = (__be64 *)&buf[j]; + *bebits = cpu_to_be64(byte_count >> 61); + j += 2; + bebits = (__be64 *)&buf[j]; + *bebits = cpu_to_be64(byte_count << 3); + j += 2; + } + } + if (j * 4 > bufsize) { + pr_err("%s OVERFLOW %llu\n", __func__, j); + return 0; + } + + return j; +} + /* sun8i_ss_hash_run - run an ahash request * Send the data of the request to the SS along with an extra SG with padding */ @@ -342,11 +400,9 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) struct scatterlist *sg; int nr_sgs, err, digestsize; unsigned int len; - u64 fill, min_fill, byte_count; + u64 byte_count; void *pad, *result; int j, i, k, todo; - __be64 *bebits; - __le64 *lebits; dma_addr_t addr_res, addr_pad; __le32 *bf; @@ -421,33 +477,20 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) i--; byte_count = areq->nbytes; - bf[j++] = cpu_to_le32(0x80); - - fill = 64 - (byte_count % 64); - min_fill = 3 * sizeof(u32); - - if (fill < min_fill) - fill += 64; - - k = j; - j += (fill - min_fill) / sizeof(u32); - for (; k < j; k++) - bf[k] = 0; - switch (algt->ss_algo_id) { case SS_ID_HASH_MD5: - lebits = (__le64 *)&bf[j]; - *lebits = cpu_to_le64(byte_count << 3); - j += 2; + j = hash_pad(bf, 4096, j, byte_count, true, bs); break; case SS_ID_HASH_SHA1: case SS_ID_HASH_SHA224: case SS_ID_HASH_SHA256: - bebits = (__be64 *)&bf[j]; - *bebits = cpu_to_be64(byte_count << 3); - j += 2; + j = hash_pad(bf, 4096, j, byte_count, false, bs); break; } + if (!j) { + err = -EINVAL; + goto theend; + } addr_pad = dma_map_single(ss->dev, pad, j * 4, DMA_TO_DEVICE); if (dma_mapping_error(ss->dev, addr_pad)) { -- 2.34.1