From patchwork Sun Apr 15 14:12:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabio Estevam X-Patchwork-Id: 10341555 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 D1FDD601C2 for ; Sun, 15 Apr 2018 14:15:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9832228445 for ; Sun, 15 Apr 2018 14:15:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8820D28449; Sun, 15 Apr 2018 14:15:05 +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 EF47428445 for ; Sun, 15 Apr 2018 14:15:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752523AbeDOOPD (ORCPT ); Sun, 15 Apr 2018 10:15:03 -0400 Received: from mail-qk0-f194.google.com ([209.85.220.194]:37014 "EHLO mail-qk0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752486AbeDOOPC (ORCPT ); Sun, 15 Apr 2018 10:15:02 -0400 Received: by mail-qk0-f194.google.com with SMTP id w6so13933367qkb.4; Sun, 15 Apr 2018 07:15:02 -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=oaTgOpt0VndfZx6pG24T4pOQbDShNaARxnuCwVMzaYY=; b=CJptmNjCwSbaowp6UZ1bVaCltMdciEtNvxUSI4JFCKAjAhVgsTAbTFoMkJRDm8ibjL gE/eof6fZpd4GQKN067uwUzIfebWBvAnHxH8Wmhz/gpAkLnRComKtKRpW4FI+j8IbNul NzbvEbf5dGaW85rY1PWXVHdiG/XFu3N0zVYPE/8bHBP+E72/CSlWH6B+gaWsrG/zkFhJ GHdGYo1gIQqYf3dg0FnUmIaGVyDWq5jvcYbI9hktIhnr0WORJhVxnd/Rf8Rd+sLoEHQ9 fTA0zp3SBvsTLcDKGv3+817j5UIRNusEh77cDqD54LKjaVA2QzAzWMJdKQ0J1jOUEz4B bWHw== 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=oaTgOpt0VndfZx6pG24T4pOQbDShNaARxnuCwVMzaYY=; b=CYPrMLCm4cJAEjxcUjLgSp+xAZLVgwzLjHEfGgtx+yE1UEbPjpoC6FhjL0zH8JaRmw UZVbm8ck/HhgFPsIZQSlZ/+7KYMRT9vB7aphBJ6UNdlWHHELoS0wi3TZBgUymsr9cR1z TwDTH1Ih34ifgGqOv/lkEh+X79R4o3KpI3MOxxu2W6a99r0QdyB9a2HhXGFctHBW86pp IAPOAwsatJArzt5iN8gbRWMVi3FTvqMjrnRMVFAbEpWPFBI7CecElO8Oa4LRLtGPXLrW /n0/C6KUbVXVUzz5GTqawZ3gwfeh1LhLVF0p4gQtIl+27FFpS3P1UhB+UX8YIevgwWBW 5Pww== X-Gm-Message-State: ALQs6tDxZZ/27YQUR1SCGSLXV6tAk5QvSjesUBoqGhTKztcuRGfOj27a FKAR3awi6Ya2RgkySn4tBmw= X-Google-Smtp-Source: AIpwx49kZNXkiuNRk8jdAXW1BRCsaUUO/1u2zspAFvV8Vzf/+W91MWjmSoFRbRIX152xqMl3E290/A== X-Received: by 10.55.227.17 with SMTP id y17mr9958641qki.12.1523801701639; Sun, 15 Apr 2018 07:15:01 -0700 (PDT) Received: from localhost.localdomain ([187.180.183.211]) by smtp.gmail.com with ESMTPSA id y2sm7869334qkj.32.2018.04.15.07.14.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 15 Apr 2018 07:15:00 -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 v3] crypto: caam: Drop leading zero from input buffer Date: Sun, 15 Apr 2018 11:12:38 -0300 Message-Id: <1523801558-21647-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 stripping 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 --- Changes since v2: - Check if the lenght is zero after calling caam_rsa_drop_leading_zeros() drivers/crypto/caam/caampkc.c | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c index 7a897209..47467ff 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,36 @@ 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 *temp; + void *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), + buffer, req->src_len); + temp = (u8 *)buffer; + len = req->src_len; + /* + * Check if the buffer contains leading zeros and if + * it does, drop the leading zeros + */ + if (temp[0] == 0) { + caam_rsa_drop_leading_zeros(&temp, &len); + if (!len) { + kfree(buffer); + return ERR_PTR(-ENOMEM); + } + + req->src_len = len; + sg_copy_from_buffer(req->src, sg_nents(req->src), + (void *)temp, 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 +720,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