From patchwork Thu Feb 15 23:14:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201819 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179228dyb; Thu, 15 Feb 2024 15:15:54 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUmjPzcIKOPOkeSW4JMUBFB4vu3RSyhfFi+IS5B8eAyzW5htq/LicXMWsol1XaU/GupqoVXYHwboCCN71k221VLUjVISw== X-Google-Smtp-Source: AGHT+IEKYyPT54K6kEcU4LUcbQh7mry+oluzD6KgPrJbI0nLiVUY607xpPapZFrNMt2K+63c047X X-Received: by 2002:a05:622a:1747:b0:42c:6e72:f6d2 with SMTP id l7-20020a05622a174700b0042c6e72f6d2mr3849064qtk.10.1708038953764; Thu, 15 Feb 2024 15:15:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708038953; cv=pass; d=google.com; s=arc-20160816; b=dV/u5gz+qv2BT2ppCGSm4J6yl965tf2qudKQJ86rNNq6i1l+NxdVcMUdvPiBSZJ9gr SMwExtIPOxLup9VPCVgUNri/xNr+z+2mxX+f+2b5vnfKQ2/IFi0L0Xphrrp2Sp3AzrT8 Rk53AaURcwQ9yJDGF7bbqK2kRB3fzWW6OFsr9bOiruOKWhLhklxXV6kyMnwe1h1jmsWq J9RpnDTnykRuuKUc4KDt9ogaPgA0+ugxHGulUCA6lvNaDFPS7OuJ316MgwJ9lvSjY/iw rP/SUwxc4G4Sn95EGTcORdeiPcT9eypK94GU/9M/FhKRBTzsraM5gOkiubOdIoIvYDy2 Vq+w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=2auxadmknAnWxSi7+6MCIOJLf41r1833HhtrUKpLSYg=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=BZmUvQmLtA7BOhCl0jOycNRwCBzOu+at3Pjb7phUGaRCkmi+zqvayn5Dpk8zUww59y rEWmcTi2HkEzQEfLORzNtCwwKuJWSPHyFz5TfNKWJphmPKrA22ioiz3/JHamnRuHOLbB EpgMS6Jr6tqMcLzbllJx7CwFj4md0TVyHCAdZAJFJMy4c6ipyt1c/qwijIGmwqyzOinR XHtOYDoFQkIZ+Q7enGxbjvUJcC332on5OhXSNfkfr2Mk5Y3Eewm7agmHTg+I/xSItlC7 28zTnp+8Kz2NUAii3fhKmpprrREzP8qfe2+OfiP0+t8F3NfG+CZeEeinnr08fpur7TB1 3plw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=hO3BJU39; arc=pass (i=1 spf=pass spfdomain=linux.ibm.com dkim=pass dkdomain=ibm.com dmarc=pass fromdomain=linux.ibm.com); spf=pass (google.com: domain of linux-kernel+bounces-67802-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67802-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id q15-20020a05620a0c8f00b0078735aa2219si2382332qki.198.2024.02.15.15.15.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:15:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67802-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=hO3BJU39; arc=pass (i=1 spf=pass spfdomain=linux.ibm.com dkim=pass dkdomain=ibm.com dmarc=pass fromdomain=linux.ibm.com); spf=pass (google.com: domain of linux-kernel+bounces-67802-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67802-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5BC6A1C213DB for ; Thu, 15 Feb 2024 23:15:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9117B146907; Thu, 15 Feb 2024 23:14:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="hO3BJU39" Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A507145324; Thu, 15 Feb 2024 23:14:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038873; cv=none; b=ee/O0CeEUytm5ICitfbZeG6D56C6yqEh84ANwNWUcrlcfm5A0vJWYb9DLfc3vgn4acc3od1zLrbD9sOTnZX2KQ1P38V1olJ2tGEnX8uQ6jowE+rn0beCD0WBR1BLf9na4S2NQJxfuXmtX+857mIO5iP/JzAHWvcRFUwVhyGYqBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038873; c=relaxed/simple; bh=Mw4S8Mathx6d1wOd81xYFMN9D7V8nkcT9m4+08HY+q8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=drExFfGrim1qhPQpPhq6PHadbYR6shZFPmKWIdhqjYovnKXXUfVVc6jCG2TL8LmEc9dvvDfF9fWoI8ZQU8A7QTohScrlpLMLLfI8wT6yTyeyv8QdU9+HH/WGBPAtvBk59qd/tt4FBhR8V5XGyAfCy/5n3sPwgdCMG+wbpY3VYyE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=hO3BJU39; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMgWWJ002954; Thu, 15 Feb 2024 23:14:26 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=2auxadmknAnWxSi7+6MCIOJLf41r1833HhtrUKpLSYg=; b=hO3BJU39AQ4XDxRWszFhckBrUwzI8wxl8CQ5Q7NcHQtrtbkeSKX1MOZQlYnXrMirg5M1 Npbf1f1taspUT122tC1ecmphEJQCl8jJkJ4Wlqf3+L+DbEz4y2WCDojEvFauVPTrQ1ZX Jl3idpAyIWf2YJaw46p0xetUVaKO86tB2XRrTxRpF3PPaHHTfS3COzy8PqaVLXlN7Umq EdVRXiTzfaif5ucwTdAa6poVuGmyOPHDsNHM/OUYjTMK+wW3E2VzwMKcBu8uXffHoGAx 7WUvB5BnRazbHl5lLNOA2/SGfYIGpYh5eQV3yCs+SghMSj606LpBQcVIk2APvSBcFNCr zQ== Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9un5gtc2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:26 +0000 Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FKhoQl010045; Thu, 15 Feb 2024 23:14:25 GMT Received: from smtprelay05.wdc07v.mail.ibm.com ([172.16.1.72]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6npm7h4g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:25 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay05.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEMhE11666082 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:24 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C50DB58056; Thu, 15 Feb 2024 23:14:22 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 210065805E; Thu, 15 Feb 2024 23:14:22 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Thu, 15 Feb 2024 23:14:22 +0000 (GMT) From: Stefan Berger To: keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net Cc: linux-kernel@vger.kernel.org, saulo.alessandre@tse.jus.br, Stefan Berger Subject: [PATCH v2 03/14] crypto: ecdsa - Extend res.x mod n calculation for NIST P521 Date: Thu, 15 Feb 2024 18:14:02 -0500 Message-ID: <20240215231414.3857320-4-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240215231414.3857320-1-stefanb@linux.ibm.com> References: <20240215231414.3857320-1-stefanb@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: FLCaKdFfBwhDpr3lkiHoE6vxcDk2rR6l X-Proofpoint-GUID: FLCaKdFfBwhDpr3lkiHoE6vxcDk2rR6l X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-15_22,2024-02-14_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 malwarescore=0 phishscore=0 spamscore=0 lowpriorityscore=0 suspectscore=0 bulkscore=0 impostorscore=0 mlxscore=0 adultscore=0 priorityscore=1501 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2402150181 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791008654138555407 X-GMAIL-MSGID: 1791008654138555407 res.x has been calculated by ecc_point_mult_shamir, which uses 'mod curve_prime'. The curve_prime 'p' is typically larger than the curve_order 'n' and therefore it is possible that p > res.x >= n. If res.x >= n then res.x mod n can be calculated by iteratively sub- tracting n from res.x until n > res.x. For NIST P192/256/384 this can be done in a single subtraction. This can also be done in a single subtraction for NIST P521. The mathematical reason why a single subtraction is sufficient is due to the values of 'p' and 'n' of the NIST curves where the following holds true: note: max(res.x) = p - 1 max(res.x) - n < n p - 1 - n < n p - 1 < 2n => true for the NIST curves Signed-off-by: Stefan Berger --- crypto/ecdsa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c index 64e1e69d53ba..1814f009f971 100644 --- a/crypto/ecdsa.c +++ b/crypto/ecdsa.c @@ -122,7 +122,7 @@ static int _ecdsa_verify(struct ecc_ctx *ctx, const u64 *hash, const u64 *r, con /* res.x = res.x mod n (if res.x > order) */ if (unlikely(vli_cmp(res.x, curve->n, ndigits) == 1)) - /* faster alternative for NIST p384, p256 & p192 */ + /* faster alternative for NIST p521, p384, p256 & p192 */ vli_sub(res.x, res.x, curve->n, ndigits); if (!vli_cmp(res.x, r, ndigits))