From patchwork Sat Apr 14 20:50:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabio Estevam X-Patchwork-Id: 10341429 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C73D2602C2 for ; Sat, 14 Apr 2018 20:52:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B5C501FE8B for ; Sat, 14 Apr 2018 20:52:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AA565205A9; Sat, 14 Apr 2018 20:52:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E27D51FE8B for ; Sat, 14 Apr 2018 20:52:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751670AbeDNUwV (ORCPT ); Sat, 14 Apr 2018 16:52:21 -0400 Received: from mail-qt0-f196.google.com ([209.85.216.196]:37744 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751630AbeDNUwU (ORCPT ); Sat, 14 Apr 2018 16:52:20 -0400 Received: by mail-qt0-f196.google.com with SMTP id w12so11507994qti.4; Sat, 14 Apr 2018 13:52:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=sdIqA+1vUUyWyrmHCaqspMN3wxz7IFrZTPwbcmq/FBQ=; b=smOTIN9UJQZxWNL69SFOHYPCXDtuCHZdsThIc6gCTPZFsLV8JmagY0jqsat2kx1e1S CracTE4lDKLA4Xo2YPilx/eJUzTCFq79U71fKw2KKFwBIGH/NBKxc5hGZl0nhq9C9HfH qkE0LP40Bu2snqlxn7h3yNfXsKfrTZSGjZMvRTP+7rAZRSoBL3euc4cm2bDyY7F2oMWP HSyuwUu+wS3V4VxxM1+OBobEeMqFt1UsQKmbxAdsspPS045AuI4jTJ81OmQE0yP7d/g1 7L6WNXvxYanHGt3fA2xOQDExTELrcdfOtWJ2Z94vaSpqYZE3fw9HzQ9MLJzlBwpom3YE BFsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=sdIqA+1vUUyWyrmHCaqspMN3wxz7IFrZTPwbcmq/FBQ=; b=Xh7I+oa+fHgukd2wrU/PvMjE81prIMWs/2NpN+MN2a+s5qqibeO0tK3fRpH3cHiOCV DVx3/s+xaEl4RFolZ5YmTaU7B6pNYSnvex23JBITRKyqp2eM4oQmaWchdOwiHOjaBgkd 7i/0YdiyQ8HtlTptX/zBnYPfSF/cOGX+6W2JR4+8DCU0XR7wx3mp1+FmYUzxoJvYRTaU J0OeBZ1bJgJO/mELyXTS7qlMMWYEjvGcE9Az11fK2p5KGP/8TYaBRv1jsvGjDbcxPoE9 /mr9D2A9W9hifJIGs98tFtEhzLU3KL4DnN8IzKPXxLGKLFR7xSW4rxrzC+RwLlJ49bBP rEBw== X-Gm-Message-State: ALQs6tDXAf73SsXaCxS4zRVf24RvKNZZkT5LJ9l840ta431dPFSIjsY4 rljOphPnWYFsoSbsMrEwj/8= X-Google-Smtp-Source: AIpwx4+ALHH3sTa7rJjWAYCygldqF6sRgqI/j7RjcTHJkQzcmGJ2A6JQOdbp1TvDnrP+zxETC33msA== X-Received: by 10.200.56.234 with SMTP id g39mr6728031qtc.245.1523739139630; Sat, 14 Apr 2018 13:52:19 -0700 (PDT) Received: from localhost.localdomain ([187.180.183.211]) by smtp.gmail.com with ESMTPSA id w21sm6711434qto.19.2018.04.14.13.52.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 14 Apr 2018 13:52:19 -0700 (PDT) From: Fabio Estevam To: herbert@gondor.apana.org.au Cc: horia.geanta@nxp.com, aymen.sghaier@nxp.com, mtownsend1973@gmail.com, breno.lima@nxp.com, bryan.odonoghue@linaro.org, linux-crypto@vger.kernel.org, Fabio Estevam , stable@vger.kernel.org Subject: [PATCH] crypto: caam: Drop leading zero from input buffer Date: Sat, 14 Apr 2018 17:50:00 -0300 Message-Id: <1523739000-27093-1-git-send-email-festevam@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Fabio Estevam imx6ul and imx7 report the following error: caam_jr 2142000.jr1: 40000789: DECO: desc idx 7: Protocol Size Error - A protocol has seen an error in size. When running RSA, pdb size N < (size of F) when no formatting is used; or pdb size N < (F + 11) when formatting is used. ------------[ cut here ]------------ WARNING: CPU: 0 PID: 1 at crypto/asymmetric_keys/public_key.c:148 public_key_verify_signature+0x27c/0x2b0 This error happens because the signature contains 257 bytes, including a leading zero as the first element. Fix the problem by striping off the leading zero from input data before feeding it to the CAAM accelerator. Fixes: 8c419778ab57e497b5 ("crypto: caam - add support for RSA algorithm") Cc: Reported-by: Martin Townsend Signed-off-by: Fabio Estevam --- drivers/crypto/caam/caampkc.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c index 7a897209..d2ad547 100644 --- a/drivers/crypto/caam/caampkc.c +++ b/drivers/crypto/caam/caampkc.c @@ -166,6 +166,14 @@ static void rsa_priv_f3_done(struct device *dev, u32 *desc, u32 err, akcipher_request_complete(req, err); } +static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes) +{ + while (!**ptr && *nbytes) { + (*ptr)++; + (*nbytes)--; + } +} + static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, size_t desclen) { @@ -178,7 +186,34 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, int sgc; int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes; int src_nents, dst_nents; + const u8 *buffer; + size_t len; + + buffer = kzalloc(req->src_len, GFP_ATOMIC); + if (!buffer) + return ERR_PTR(-ENOMEM); + + sg_copy_to_buffer(req->src, sg_nents(req->src), + (void *)buffer, req->src_len); + len = req->src_len; + /* + * Check if the buffer contains leading zero and if + * it does, drop the leading zero + */ + if (buffer[0] == 0) { + caam_rsa_drop_leading_zeros(&buffer, &len); + if (!buffer) { + kfree(buffer); + return ERR_PTR(-ENOMEM); + } + + req->src_len -= 1; + sg_copy_from_buffer(req->src, sg_nents(req->src), + (void *)buffer, req->src_len); + } + + kfree(buffer); src_nents = sg_nents_for_len(req->src, req->src_len); dst_nents = sg_nents_for_len(req->dst, req->dst_len); @@ -683,14 +718,6 @@ static void caam_rsa_free_key(struct caam_rsa_key *key) memset(key, 0, sizeof(*key)); } -static void caam_rsa_drop_leading_zeros(const u8 **ptr, size_t *nbytes) -{ - while (!**ptr && *nbytes) { - (*ptr)++; - (*nbytes)--; - } -} - /** * caam_read_rsa_crt - Used for reading dP, dQ, qInv CRT members. * dP, dQ and qInv could decode to less than corresponding p, q length, as the