From patchwork Fri Mar 1 02:19:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 208606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp813322dyb; Thu, 29 Feb 2024 18:22:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWU6ix4w2tM18X1Tb5mCgrTMVpG8Bfz7zK12DLPLoUsxEZNobuJ0L4Z8/80ZSHeqyvldZICCePlLu4Ve8U3/+VILQR+zA== X-Google-Smtp-Source: AGHT+IFjXCMgMMAOgeBRjAr2g/+Ln8mmpBVftbokvfNaxIRMcCWyy452FfT0mbFNhyv/JCFq6RXw X-Received: by 2002:a05:6e02:1b08:b0:365:1dbc:a4f0 with SMTP id i8-20020a056e021b0800b003651dbca4f0mr462336ilv.31.1709259762312; Thu, 29 Feb 2024 18:22:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709259762; cv=pass; d=google.com; s=arc-20160816; b=aIWGUZPJ1/yeiGjYe0zJTfpFOpu0fRSs+y6mnRNSnKE5hjxNhBbU9DD4l+D1JzKmt4 JsPbteFtRBwwlOc8RxtD+EWZ88Au+QyMSafbLzqmzZf7cLbAzbEd7tDWDoxq1TDC4kBm +7v0CpABe6g8e0RaP9h2PBOHtCtrZmlEnB4g+WLt+gFrBK4q2O7hCyhBxYSpuX1eZeXV q8XYp7AaCHvLrm46FPxQY6CCytRiAajeqFR8k3pVZ4xOIwl/f20zJGnSYq793kRlqwi/ YeQLz/JmdCemcmDYgvGrmB5nYXDKlOV1QjxGN70hhsj8XrS42N/jKqy6fPjHeRTmRMhz 2fxg== 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=uQgCV7ljChfbk9Pg8e9OEwAz+CERRXkVwHVF9QYdDtk=; fh=rcM6ok5GFu5IIT2QdH7dFGIfgvD+dlFEBXZav0jM/0I=; b=aRL2Qq1RHqTrsButsOK+l/soSuMmv0AH9gxvYDxcE5+p0mDj/0WaChytGMKF7Ywc+V yVduJAkKqDdHMCGewTPvuAK0XWfeeVT8Ag/x7re8EP8kEORvtd4P48m9P4ows944IhI0 LlDHdv0ytltdbRjF4ZgIr6BuktuPLnPIXPzf0ST/93WAE6sDSkJ6Dg5ZPnEMLPjrnh7H seJuXtFq0mLhPuU2fhD99a8cRyAz7y8SbB5HH2fZsiRURf46SPzaVxBIj+XVH4licXjc XiG1FBdZ02yljQh0233DYh7sEPfgnk7JL4XOooqGmyQRLjjc7KCid3BVCDIAiIUpj9j3 S0yw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=e5KcZ4U0; 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-87824-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87824-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id c6-20020a631c46000000b005d645323583si2560890pgm.755.2024.02.29.18.22.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 18:22:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87824-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=e5KcZ4U0; 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-87824-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87824-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 00C48B21470 for ; Fri, 1 Mar 2024 02:21:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0A3553C68A; Fri, 1 Mar 2024 02:20:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="e5KcZ4U0" Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 042F03A8CF; Fri, 1 Mar 2024 02:20:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709259622; cv=none; b=gsHYAHht6o2+IGMMYMO5oq7b/+enn1QY4URpMRqIsloPrp2ZoudrToruJjkzM3py3rJdz4wi3kUC+aSk8q7Nnlm9lVpH91s9yrgkP/lu4MGzNh+aFtwaaTdeQrENoIiV2rXptp4VkCBIgGefTj4OnNxfOLq/8R37X4EWMbKl+F4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709259622; c=relaxed/simple; bh=izU+mhVuxmGZLH4923QH4fb8IcKtylcqzLPk1Cqo7hU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U0IcJzIQ+mtv1y+iKX4Mf46bWb4LifkBn31ZTquwKBmAJTj33GHGdL8Cb8Yq2n7W3fIiSjvtwBD/iorNqFulgokYxt42MLwIQxzcQs6HsW0KB7+P560DPci20nhGvleiO8JalZSaSgJa2s6KLcavWacy0irLMCtaBVnOmkMyMOU= 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=e5KcZ4U0; arc=none smtp.client-ip=148.163.156.1 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 (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 4211HFDg007080; Fri, 1 Mar 2024 02:20:15 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=uQgCV7ljChfbk9Pg8e9OEwAz+CERRXkVwHVF9QYdDtk=; b=e5KcZ4U0BIwkpTDujm6gBTt98KUNXzJpnr1hH11vUiBZnvIlHfL1Dc01eX2xh7xnhVc/ Q6o2Ar/SrgDjGU6L8XYEl/iwCTAVhrHL94FCnRHL8JaniEgHqK325uA0PKtQKNmWX9IZ YRacMNn8Gq/2JYb4DOwt0eSW1pTk6E6oww/dJ2EFKZhPfdVDAzU2PqquVmhyVjsNdnf5 Y4dsJ1TQeYdgg47RyujGtWoh2wm6VyXRqWPzUrcvKYRCRaLkLrv2sgxQkod9cvWJiwpA BB/dyUBbNVOqBnHrtZdrEtT77yfa7wS69v/eopfbsDw4fFqZKNGzU14gKSMImRyFY5pw Bw== Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3wk57h1dh7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Mar 2024 02:20:15 +0000 Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41TNwnoD008827; Fri, 1 Mar 2024 02:20:14 GMT Received: from smtprelay06.dal12v.mail.ibm.com ([172.16.1.8]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 3wftsu1w6e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Mar 2024 02:20:14 +0000 Received: from smtpav05.dal12v.mail.ibm.com (smtpav05.dal12v.mail.ibm.com [10.241.53.104]) by smtprelay06.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4212KBYQ5047238 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 1 Mar 2024 02:20:13 GMT Received: from smtpav05.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D079B58065; Fri, 1 Mar 2024 02:20:11 +0000 (GMT) Received: from smtpav05.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5A5D15806D; Fri, 1 Mar 2024 02:20:11 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav05.dal12v.mail.ibm.com (Postfix) with ESMTP; Fri, 1 Mar 2024 02:20:11 +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, lukas@wunner.de, Stefan Berger Subject: [PATCH v4 04/12] crypto: ecc - Implement vli_mmod_fast_521 for NIST p521 Date: Thu, 29 Feb 2024 21:19:59 -0500 Message-ID: <20240301022007.344948-5-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240301022007.344948-1-stefanb@linux.ibm.com> References: <20240301022007.344948-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-GUID: tq80GS16aRkKr-ycQLzZbf_-Qnu-JSak X-Proofpoint-ORIG-GUID: tq80GS16aRkKr-ycQLzZbf_-Qnu-JSak 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-29_08,2024-02-29_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 lowpriorityscore=0 mlxscore=0 adultscore=0 spamscore=0 phishscore=0 priorityscore=1501 impostorscore=0 mlxlogscore=828 clxscore=1015 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2403010018 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792288764702380119 X-GMAIL-MSGID: 1792288764702380119 Implement vli_mmod_fast_521 following the description for how to calculate the modulus for NIST P521 in the NIST publication "Recommendations for Discrete Logarithm-Based Cryptography: Elliptic Curve Domain Parameters" section G.1.4. NIST p521 requires 9 64bit digits, so increase the ECC_MAX_DIGITS so that arrays fit the larger numbers. Signed-off-by: Stefan Berger --- crypto/ecc.c | 31 +++++++++++++++++++++++++++++++ include/crypto/internal/ecc.h | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index f53fb4d6af99..ea7b28b5e00e 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -902,6 +902,31 @@ static void vli_mmod_fast_384(u64 *result, const u64 *product, #undef AND64H #undef AND64L +/* Computes result = product % curve_prime + * from "Recommendations for Discrete Logarithm-Based Cryptography: + * Elliptic Curve Domain Parameters" G.1.4 + */ +static void vli_mmod_fast_521(u64 *result, const u64 *product, + const u64 *curve_prime, u64 *tmp) +{ + const unsigned int ndigits = 9; + size_t i; + + for (i = 0; i < ndigits; i++) + tmp[i] = product[i]; + tmp[8] &= 0x1ff; + + vli_set(result, tmp, ndigits); + + + for (i = 0; i < ndigits; i++) + tmp[i] = (product[8 + i] >> 9) | (product[9 + i] << 55); + tmp[8] &= 0x1ff; + + vli_mod_add(result, result, tmp, curve_prime, ndigits); +} + + /* Computes result = product % curve_prime for different curve_primes. * * Note that curve_primes are distinguished just by heuristic check and @@ -941,6 +966,12 @@ static bool vli_mmod_fast(u64 *result, u64 *product, case 6: vli_mmod_fast_384(result, product, curve_prime, tmp); break; + case 9: + if (!strcmp(curve->name, "nist_521")) { + vli_mmod_fast_521(result, product, curve_prime, tmp); + break; + } + fallthrough; default: pr_err_ratelimited("ecc: unsupported digits size!\n"); return false; diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h index 48a04605da7f..b63238b12204 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -33,7 +33,7 @@ #define ECC_CURVE_NIST_P192_DIGITS 3 #define ECC_CURVE_NIST_P256_DIGITS 4 #define ECC_CURVE_NIST_P384_DIGITS 6 -#define ECC_MAX_DIGITS (512 / 64) /* due to ecrdsa */ +#define ECC_MAX_DIGITS (576 / 64) /* due to NIST P521 */ #define ECC_DIGITS_TO_BYTES_SHIFT 3