From patchwork Thu Feb 15 23:14:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201828 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp180381dyb; Thu, 15 Feb 2024 15:18:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXHRdadyu1s44uXFdmDpfvQ8yNsZ9EV2hzc3fF9yBuDpPn/sl9RmbH0ixvE4RCxvk4KEUAi4egbYXDs8qLZ+6Ew+dSDFQ== X-Google-Smtp-Source: AGHT+IHJt0Edcub9iUU4P4EkZwuyyF7mqZQTyvVFJgSvgS8B0DKPOHfMiep1vKLLS0lTnC2Gr5IE X-Received: by 2002:a17:903:32d0:b0:1d8:d6bf:144b with SMTP id i16-20020a17090332d000b001d8d6bf144bmr1076574plr.4.1708039113532; Thu, 15 Feb 2024 15:18:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039113; cv=pass; d=google.com; s=arc-20160816; b=DHH7hvCmqcQB4Wqj4UDU+XAYUyi9/1/nUMDH4+UGGqudGiRT3m7X0TIR1mD7iUwy1Y dUP45X52GYZ5T5QDlzuO38xrQaJIGQHcvPPcQwMTRrQLit0Z4HWoyyRzVWbvvlJklIz+ uZJQJ5zlBHnBnq/jo3YUg6ErAHtxY/J7zWLiy/pBz0HEK92oIs3gAzgyRdODPhrhUCKv sL5d0c72WOLAQuoAlj8uu2+jwqXCFh5QmQ6qh/FsmZNjQWZT58m2seovGLols+u0fecf Mx/ra0C9fTHC34az6JN6D+/EEyY4KTQpatz6fo2kisTdVk9+qEspcIqyh94vAf3qouag IXtg== 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=lkJWvWemqT3GohDbcDvoFQIa/ot2xv4+0jDQwX7U5pE=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=ROtuNiM3xIbawfmZVn8JFW8mBVWLLyVhUvFFgYrrIeRP12PhNYumpbx9jkC7dm0qMs SnFDR9b2wkUCQuL3Kd07TopEyR5VfbeznY2yVbGhhyZ9mQSbWpEoo86VjPzi3Wqj16d2 F4kkL2744/h3PEbZbS5zIs4mqFZOKKZ/CPnpbbEkTl7hAy0/uFfKOtNyoneIKZnp9Fd+ v1Q484iDNUfUW2fMMh8mablsVruvyZZSg1SNlUx6zxfITnEqpE5OWRo8Ktzq4Gb86jHR UgAn4jFXUyYpTQ5KGNJafwd/EQDdzcI5mFZxtDl0XAbawaLdZoNTh/ur7+OpiILQOTLh 6bwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=dOuxiO6b; 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-67800-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67800-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. [147.75.48.161]) by mx.google.com with ESMTPS id n12-20020a170903110c00b001da2b5252dasi1852323plh.319.2024.02.15.15.18.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:18:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67800-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=dOuxiO6b; 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-67800-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67800-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 D126CB2386E for ; Thu, 15 Feb 2024 23:15:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A27E91468E0; Thu, 15 Feb 2024 23:14:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="dOuxiO6b" 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 D538813AA4F; Thu, 15 Feb 2024 23:14:30 +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=1708038872; cv=none; b=RRjMqbdhVzLFY/BwdpEKUYdreYhPuKfZdJTCK6R9Z9EBdPBN6q65QjU7JLKmKDEEymGLLAbOq9bM6KtyyfCFoLucj0z77iz6XKaWwtzk/3VehTGFmki5+Ox+m5NA6eyicgULYocAnofdtcejOA1HPdutpC4Tq9tx86KhKEGPu7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038872; c=relaxed/simple; bh=FBnzCjcZIbwyWZ5sPWAYV2N+5U9ntGSsKofEExDldOs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZOfS9jTEBYZivBY5aIwEPIalM8k5Ra5ZRC5Ucm4pXkgVaw3kICnTGW5NQgF+cp9tiIkhy/wp83gEfmu6AHW5oAL5gpn/tLfphtmoGadLimp59Vp0E4qOeA9n29MkuxzQ6xWhbr/NG6L9c+oRbKnGxcU3NGmYH9Uo/MinBtsB3w8= 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=dOuxiO6b; 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 41FMtx0q014225; 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=lkJWvWemqT3GohDbcDvoFQIa/ot2xv4+0jDQwX7U5pE=; b=dOuxiO6bIojAz9f5tiQEfZLAp1Wp9KCOmciaaxLzJvpGOl1ZOOBpWB5UQZ+yeyWnkuxq QeXvEZ0kz+6gmrkHHOU+rITbIzBEpFnNmY0MXe9IgqJKsH4I0MalpUF/hvoOmhpKyvWH hnP5QNfaKusQeQVIcV/etXH1LV0R31beGhRHKPmerueSsx3/PV3Q2/SWVU15Mr3o0LK2 M0+KVuV0Pco+ZnIP5NgWXqjshmnN8pRJngtqjBkeC5wEaOB8IpNL8hH1KGq3CXqY41xn ARNgPpsq4Gys0y2t0fXtlzZ38vn6mOXWat2XhFttS3YH/4JnBT4jVK0yVlQaC9QKN4Ju lw== Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9ug41649-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:24 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FN7pxA016307; Thu, 15 Feb 2024 23:14:23 GMT Received: from smtprelay04.wdc07v.mail.ibm.com ([172.16.1.71]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6mymyter-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:23 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay04.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNELCt50266442 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:23 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 29E1758054; Thu, 15 Feb 2024 23:14:21 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 793AD58056; Thu, 15 Feb 2024 23:14:20 +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:20 +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 01/14] crypto: ecdsa - Convert byte arrays with key coordinates to digits Date: Thu, 15 Feb 2024 18:14:00 -0500 Message-ID: <20240215231414.3857320-2-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-GUID: Dba34ExGB9hNBSaxTOIKkZCFSlGdylqI X-Proofpoint-ORIG-GUID: Dba34ExGB9hNBSaxTOIKkZCFSlGdylqI 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 suspectscore=0 phishscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 clxscore=1015 spamscore=0 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: 1791008821511331198 X-GMAIL-MSGID: 1791008821511331198 For NIST P192/256/384 the public key's x and y parameters could be copied directly from a given array since both parameters filled 'ndigits' of digits (a 'digit' is a u64). For support of NIST P521 the key parameters first have to be copied into a temporary byte array with leading zeros and can then be copied into the final digit array using ecc_swap_digits. Implement ecc_digits_from_bytes to convert a byte array into an array of digits and use this function in ecdsa_set_pub_key where an input byte array needs to be converted into digits. Signed-off-by: Stefan Berger --- crypto/ecdsa.c | 14 +++++++++----- include/crypto/internal/ecc.h | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c index fbd76498aba8..ba8fb76fd165 100644 --- a/crypto/ecdsa.c +++ b/crypto/ecdsa.c @@ -222,9 +222,8 @@ static int ecdsa_ecc_ctx_reset(struct ecc_ctx *ctx) static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm); + unsigned int digitlen, ndigits; const unsigned char *d = key; - const u64 *digits = (const u64 *)&d[1]; - unsigned int ndigits; int ret; ret = ecdsa_ecc_ctx_reset(ctx); @@ -238,12 +237,17 @@ static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsig return -EINVAL; keylen--; - ndigits = (keylen >> 1) / sizeof(u64); + digitlen = keylen >> 1; + + ndigits = digitlen / sizeof(u64); if (ndigits != ctx->curve->g.ndigits) return -EINVAL; - ecc_swap_digits(digits, ctx->pub_key.x, ndigits); - ecc_swap_digits(&digits[ndigits], ctx->pub_key.y, ndigits); + d++; + + ecc_digits_from_bytes(d, digitlen, ctx->pub_key.x, ndigits); + ecc_digits_from_bytes(&d[digitlen], digitlen, ctx->pub_key.y, ndigits); + ret = ecc_is_pubkey_valid_full(ctx->curve, &ctx->pub_key); ctx->pub_key_set = ret == 0; diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h index 4f6c1a68882f..bee3329af7de 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -56,6 +56,25 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit out[i] = get_unaligned_be64(&src[ndigits - 1 - i]); } +/** + * ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array + * @in: Input byte array + * @nbytes Size of input byte array + * @out Output digits array + * @ndigits: Number of digits to create from byte array + */ +static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes, + u64 *out, unsigned int ndigits) +{ + unsigned int sz = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; + u8 tmp[ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT]; + unsigned int o = sz - nbytes; + + memset(tmp, 0, o); + memcpy(&tmp[o], in, nbytes); + ecc_swap_digits(tmp, out, ndigits); +} + /** * ecc_is_key_valid() - Validate a given ECDH private key * From patchwork Thu Feb 15 23:14:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201818 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179183dyb; Thu, 15 Feb 2024 15:15:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUjIai7WZbYaYkGaz0bCQ1oEXlIzlfKmAA+weKlUbEQvczFF1j+3WdCp43GBypGx6wfQlw7Y/ztxzeSPwOOQ7Oo6j6yZQ== X-Google-Smtp-Source: AGHT+IF8s+b7IJbRfU9aNGoeRE7SKF9K/UJrpYYtORV91hWWIvzyjW1HH8rvbBfOl+B8TJGYSQ5q X-Received: by 2002:a05:620a:4611:b0:781:5e3a:a85f with SMTP id br17-20020a05620a461100b007815e3aa85fmr5217146qkb.17.1708038949187; Thu, 15 Feb 2024 15:15:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708038949; cv=pass; d=google.com; s=arc-20160816; b=qS30OLRc5ClY7ft5KRWL3NHKPVsUgjJbMLuuj6Be7XX++lMA7lwpEtSth8iAZBzgDq X5q8iWSZwdMlRcGI/9fVlIixpLJZ+uVQ1yHx8fAd6z7ZwfHR/LO3AX5rbWlc36MLDl6o Mk/29lgmPxg9IU6NXnc6HDn8zt48aEEz8XtN3tGqWCzWDefsiEOAG9fb2lyXiVZYTDTn EBD5/faN/uC+GYWj7m9Apfwelf6Lb3q6dwtq+XHIwJ+CHAvatTdv1H1vSo3hG0ZtrQjd AlKssHTQ0CWKVHpW2Tn29SPK3945eAHXGSnbdXEDL/gj0lpXgH25x8YSySUcNwvK88qC R2Kg== 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=UfCoxw1GwPsVnPVgicCJh55bEckUIM/iCOGiq6BVP00=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=j8EzJPgKkJ5ykBxi+bYrvspj37HObvEjfTRwnt9ja/k7s2Lj1OjHdnjTU91d/P0uBF c6ZW/FXPuY+OjTQuDVtlP9ReJynqoAUMW6NWTghSxDPvLjgUNmew41AdTHkRSDS/rQ0A W61ThnURLAPipZJrP7i1DAOWs0hatLOac70DNh1YOSAFj9WcS/JwJedDUIGMtgcwQSWC AMBQVEuQ0DS1T6R8Axz6IsBbb4M0siOaw6bkIpNbvudL7rqYPyygeOZsc/H4F58rTBbG +kyFrPNEhyV+m97HRcwvaVVp+liah98bJQTyWEn/Cw1JufvLKT+h7jkUmF3/kpnPm9OK dsWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="FQmD/QGF"; 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-67801-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67801-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 z16-20020a05620a261000b007873b0181fdsi1813318qko.69.2024.02.15.15.15.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:15:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67801-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="FQmD/QGF"; 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-67801-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67801-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 8C5801C22B61 for ; Thu, 15 Feb 2024 23:15:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13B141468F2; 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="FQmD/QGF" 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 461F71419B5; 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=hDK3ONLt0cS3VBXYL0/jkw2srIFDc3qJNpd0bmAn5Hbrv6OFdP0g7rtkhPVGTbG2rU5zzvOx0URbHUvUStFgppRe6fDDnTRDc26cflM8CeR5GGMMPVFiDNsUdQwqXZsrSFbvKGW0Re+pb3GA2Yz8gLjQmxZ1ZDYNFO01Ca55EsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038873; c=relaxed/simple; bh=lUuzPnZtqYdFvF6hiy1auTq+Az0+wC1o9vh1IKFDu8U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VGW+BYlyyhakrorXitK5HHqQqFGbRZRYqsCSVI56HR/Px8kButTFnZzJIFNn9CIPC/4oxINt+AIkgg4qQWXV+R3KJTfcbmfHKXFmizjk5JRhISajCEMy4lbvSFu5fHTfKSqCM2VoHzsQYzWaghB0YYI/4LLlNdH6ocrIUshszJ8= 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=FQmD/QGF; 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 (m0353723.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMQYo6030816; Thu, 15 Feb 2024 23:14:25 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=UfCoxw1GwPsVnPVgicCJh55bEckUIM/iCOGiq6BVP00=; b=FQmD/QGFsBdmIQGtulFC9X4pN4IqutKevyiC/RF1Y7wQho4LPU6TR8OGsAPMLb1AGSqd yYOiqy6WCrlLWzrgLl4TYNWlKVeAj5sWzzVf6pK5FBYtUWadUAEsiAhMgoHy9fj3vAs+ Qzym7XVmNV5EmtF7hXSxNfJ+M5f0gXNNBpaJpsNGEXfYW0JzGlC9kSmL9LYCNAIrPMn+ AEG8W7Vrte08VNe2XY7i640fLckREOOie+t0cgfUL3Qca6BkzabzV+jGqDhRDw9eMnLv Ts3HZP7oa+ZHXfOD+JVa3Hlv1HG+uvfmKl8sW8cVpKi7mNUT3bv5OFi3UjsaW9197Jd4 ag== Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9q8n7j36-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:25 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FN7pxC016307; Thu, 15 Feb 2024 23:14:24 GMT Received: from smtprelay05.wdc07v.mail.ibm.com ([172.16.1.72]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6mymytet-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:24 +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 41FNEMdl6619688 (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 F17F658050; Thu, 15 Feb 2024 23:14:21 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4D56358056; Thu, 15 Feb 2024 23:14:21 +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:21 +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 02/14] crypto: ecdsa - Adjust tests on length of key parameters Date: Thu, 15 Feb 2024 18:14:01 -0500 Message-ID: <20240215231414.3857320-3-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-GUID: 3_KZMTl1k5YMPE4lG2TSjpxfLQoDrU_M X-Proofpoint-ORIG-GUID: 3_KZMTl1k5YMPE4lG2TSjpxfLQoDrU_M 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 clxscore=1015 mlxlogscore=999 impostorscore=0 adultscore=0 suspectscore=0 bulkscore=0 spamscore=0 lowpriorityscore=0 malwarescore=0 priorityscore=1501 mlxscore=0 phishscore=0 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: 1791008648974899360 X-GMAIL-MSGID: 1791008648974899360 In preparation for support of NIST P521, adjust the basic tests on the length of the provided key parameters to only ensure that the length of the x plus y coordinates parameter array is not an odd number and that each coordinate fits into an array of 'ndigits' digits. Mathematical tests on the key's parameters are then done in ecc_is_pubkey_valid_full rejecting invalid keys. The change is necessary since NIST P521 keys do not have keys with coordinates that each fully require 'full' digits (= u64), unlike NIST P192/256/384 that all require multiple 'full' digits. Signed-off-by: Stefan Berger --- crypto/ecdsa.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c index ba8fb76fd165..64e1e69d53ba 100644 --- a/crypto/ecdsa.c +++ b/crypto/ecdsa.c @@ -230,7 +230,7 @@ static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsig if (ret < 0) return ret; - if (keylen < 1 || (((keylen - 1) >> 1) % sizeof(u64)) != 0) + if (keylen < 1 || ((keylen - 1) & 1) != 0) return -EINVAL; /* we only accept uncompressed format indicated by '4' */ if (d[0] != 4) @@ -239,7 +239,7 @@ static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsig keylen--; digitlen = keylen >> 1; - ndigits = digitlen / sizeof(u64); + ndigits = DIV_ROUND_UP(digitlen, sizeof(u64)); if (ndigits != ctx->curve->g.ndigits) return -EINVAL; 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)) From patchwork Thu Feb 15 23:14:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201821 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179512dyb; Thu, 15 Feb 2024 15:16:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUsQRTFihftwPopvDa0puiC06v/GzMkwlkzsHInQoRmx5iTP11+iMRBTmyqLGwcGxmgZsPSxryGWYXazPUhVG+Np0pMXw== X-Google-Smtp-Source: AGHT+IHTPUOASNMTW9x0jAqq7r0e8T33Vn/10TybUflgClLSkUa1/+mcXZz9ZpFupfu9EgmCw0Rz X-Received: by 2002:a05:6102:b04:b0:46e:e97d:d119 with SMTP id b4-20020a0561020b0400b0046ee97dd119mr4384132vst.1.1708038989349; Thu, 15 Feb 2024 15:16:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708038989; cv=pass; d=google.com; s=arc-20160816; b=veyBpESwvvJmwiInZCGyA57lysqE85xP+ERPO0mIZWFAdoCw+4O7zTrz+mCMeayrLN VGbCpyMQUjKKfWZ+POVN81TdGNOG3Q2Ac4xZ7FyRPTeeyzuVEXFgarfAl5MHYuu4BaPE 9PLJI2W+cbAGJXU9v5YGNo2+7gsVEvkF/CjIreGrjDmfUomhcIdjtPe51OhrTBznRbkT GVgkk76CL0ZRzsLHIrsH/emYAde3Ytjsp0kwczQlbGhT2FrrwoF5YDpB13yWN2cnB2iS dHDW7zE0LUPQsfAquVXmJt10hxxCKEg08T80FiD406rXOofeZO7RWpNgBvMp+V3PMGv6 X9kA== 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=vCotrLlllJbV3W12rV0W2qDDfKes5T/PPwflmMS/7oU=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=j4yrZyrPclxJEABXoelps7mzXlCVrRj67IR0XFIUfMD5n8m6Fiv2nH9k7hPU4J1yMh eXJVRqP1slPNMG5uwp8wSV7lfb3qLTZrZUqps+DBFsP1qUzHPCpD5O5cTxUfxq4G/Yf4 ddf5+ZUR1e2t2RGcCfZmYmq433zsDKtKWEfOZ+LOCpHU4wRmXrbvDqyAY1AGBnLSjg4O PHbgxglGMGVjpZpZ7wHVYcOV8tmtZpKUDyjK7dXOp2yDWYjxDBOS4tharpVOCCfZTZjH mMzIMiX5SifOWKTxMzu4kTszVqDciqffupbxt+2MFRS0ZSiJvuEGUWzFGGDXtKSHfcry j/rA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=qV+4Hr+E; 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-67805-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67805-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 s2-20020a056102108200b0046d2ebb47casi449133vsr.763.2024.02.15.15.16.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:16:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67805-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=qV+4Hr+E; 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-67805-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67805-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 13B841C20F09 for ; Thu, 15 Feb 2024 23:16:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A507E14830F; Thu, 15 Feb 2024 23:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="qV+4Hr+E" 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 6B047145B0C; Thu, 15 Feb 2024 23:14:33 +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=1708038876; cv=none; b=YKnBVZWdq5yUIvjitH9lr8NB2BYl8kDVPG9+a/wB0h1YqdLzn2Tn6ZZ5e0co9UZk3SWaLgpcvlH14iV8vO6CXIrAsOfouvyLivWq8RL53RMVZaHZpnR5IeVb1AdF5oDpsdRqEA1Ga5X/WhYgFhwdECBIMd8t9yf7laIi8yMZbCo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038876; c=relaxed/simple; bh=QZBk3pnFefPDh4cDeQB1N0Rf1cYuyeJKwTH++kOetoE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MRG+L2caRojelfO6DvzI9YS0YiV61UhxP1hhtcNM/I3rMPE/MoJfnFTB5Qp6NAyGcOfssgEAhvDvhVaZct8qi2Ss4q0g1KbLUb8TbsTuuJ33MQd7IuO9fKx2tLShyhGfj29oLal0JshweN4MNnolgQdTPt5A5I3uNpgFSKiAaXI= 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=qV+4Hr+E; 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 (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLtvNE015749; Thu, 15 Feb 2024 23:14:27 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=vCotrLlllJbV3W12rV0W2qDDfKes5T/PPwflmMS/7oU=; b=qV+4Hr+EXLpo4MNh6lzcqjUNjSKGzDFh2/kSNjNadmhVx5HaZofs0+H8YrwGKh9d3sYo IbljQYlYZ9YnI2hKP4j6sB/mFiaPJva+HuoCovqBXsyNmTKt4cBFYY1bs9PxWQDuZgc1 eeIKIoSzeaePFzdlyEkOxCdZCqcLlQMZIQxA5X/XdMelAHNwsxf4PQ38Uv7I2vddoERT jIu7EhPsDkPFAWiR1H5w9/VEg2i5W+fWL3H0w8sUOb3lfqpdD8I+5bRmMt9DYHfKIPQH PDhHaTc4GbyWjYQ3x7VhjhAbuUd441RtoklTSY8U9Xv4/PhkhrY5cbpRvufGzM2HYmPh Zw== Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9rmnx127-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:27 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMu1VD016479; Thu, 15 Feb 2024 23:14:26 GMT Received: from smtprelay06.wdc07v.mail.ibm.com ([172.16.1.73]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6mymytf0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:26 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay06.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNENvl20054588 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:25 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 99DB15805E; Thu, 15 Feb 2024 23:14:23 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E8F5D58061; 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 04/14] crypto: ecc - Implement vli_mmod_fast_521 for NIST p521 Date: Thu, 15 Feb 2024 18:14:03 -0500 Message-ID: <20240215231414.3857320-5-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-GUID: r1KrhpsjBVgajI9yNUGFCL9s1Ou8qys_ X-Proofpoint-ORIG-GUID: r1KrhpsjBVgajI9yNUGFCL9s1Ou8qys_ 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 mlxscore=0 adultscore=0 phishscore=0 mlxlogscore=751 spamscore=0 priorityscore=1501 suspectscore=0 impostorscore=0 malwarescore=0 clxscore=1015 bulkscore=0 lowpriorityscore=0 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: 1791008691325914377 X-GMAIL-MSGID: 1791008691325914377 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 bee3329af7de..b8ca5023b3b5 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 From patchwork Thu Feb 15 23:14:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201820 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179377dyb; Thu, 15 Feb 2024 15:16:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVRTLIZHkVOmJzjDrZ2fgWE1Sf1i8+3HXxQBZdIonEAWUpGNmuHkpwWGLp9M4b8ituAEis5x1kP1pfaqWZGgjY9qmLiEg== X-Google-Smtp-Source: AGHT+IHSDb6xXdIWOqMi7AY9wofTwfjxvnB0bXqC8ZGO+VlwoiBC4mCMRYxqqKwb0dgCai5jGlxZ X-Received: by 2002:a92:ca90:0:b0:363:c4fc:2ab7 with SMTP id t16-20020a92ca90000000b00363c4fc2ab7mr3081430ilo.19.1708038971538; Thu, 15 Feb 2024 15:16:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708038971; cv=pass; d=google.com; s=arc-20160816; b=xaWFmA3flRZFFJ6dq9IdXvA/3H7j+ooCV2nC1nag21l+f3VgsVfsTJCVmpAw+vsV/s /Bj+0SQVjPhOK8dXm6IJD2YjoYVsAEHZK9Pw97B9v0u37xdT9CkYdAPDX5sDQDwF0RO6 ONBccWGST1jl6MFoQe+b6/Opa7tHO6uAnlQenksRlitBeRwWVTPsF6i0fgncAEdxNiOv Tlsz31qcHi6Nd3ftV40s4jctkyMjbq9Xrr5SXWkUzQs8griUinws16yPWSZw75FiMxty azXkDQoh4bOvueqdmDtnzf0t6zzvnacw3f/NTnulP1G0ew6YkzYPv0jKgq0oQJCypOuw 2tfQ== 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=CVFTJNtYaFsMOYtJ9BYMmc6KkF18ql+KPoVKGYyY/VQ=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=iNmlIAzgS1W8vIh5Wg2Dw3pL1pVZ+OcTaC4vm2AZSxHPxZ1G7yPiDJIBiDGyBoITVI tlVwbCyUiK9CuALNGBZssje5OfAZjpENDWkU8KZ8MvUIerqtmX0C5DCMfkKI8w/hS8uL rtk9YUSt4mfGVW+fDpivVaioC9GuIKpePD9VuUMIx+WHj/xmKKw08otSMIYz1Kr36VX7 GwyxykvzfQdqww2Tbz2hYdtTxrFB01kse35tsGuI2hIaCnlPcKwgzxMe8/aSwlqx3bBR 9TjrC84YYklPbNmuBRDrzwo5eTOZ5IxuvRg2o4EVpa+lDjZOLEn94/fe8BBuxOUcdVwh k1WA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="pa/H6iWS"; 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-67803-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67803-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y65-20020a638a44000000b005d8bbd208desi1787676pgd.163.2024.02.15.15.16.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:16:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67803-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="pa/H6iWS"; 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-67803-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67803-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 7034B281555 for ; Thu, 15 Feb 2024 23:16:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E47111474C2; Thu, 15 Feb 2024 23:14:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="pa/H6iWS" 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 A5B4B145B17; Thu, 15 Feb 2024 23:14:33 +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=1708038875; cv=none; b=o5H8ravn/wRClk4feYkVhnhQtZWwp/GUf5mAca4dhAHZ2DQE8XC2Wh6TxF9T4/Dnqqc4ONJgA7JBEYIcohvSm00pfh/AqJGFRWuKSrrULqy+yTXDyGE6zcTkgYxgYvSAjEuGI1cLQJAO4MmD499pxfShrXTtfs7GgKKM0UFhaU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038875; c=relaxed/simple; bh=m2TaemDn6VOPXxzBXON39efWcNC70vFPbuEBVxWVBh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G4gVkguvySzQ64yEVMrw+LP8J2BqOoMYWSWSkHL2SxBjs6w5JB0QrOtj0eb4zwo0GCELP6FGqma0z9BsueBezpwhk6XCNAu+vioSpoKKA12aBlE/azFFfCxhtb556V85UISkzdl5mqDRuc5+BPMgtKDsBYZENVcPFQZZO669Y/g= 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=pa/H6iWS; 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 (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMWXGZ031925; Thu, 15 Feb 2024 23:14:28 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=CVFTJNtYaFsMOYtJ9BYMmc6KkF18ql+KPoVKGYyY/VQ=; b=pa/H6iWSWC+ENjrfbGadILIf84x8YDMSTIFkQNrtQUtkBuUyKCdHy86bASVoOjccEg/g nza2ASRdhXZRGldJb+eDzEI3iMUFLsg+lMKeUujYsxDoOWrd2SkV6+T7ye9qDuW1qvym 6O2dLUFNWongKyM00HyPcfOorrYmpbhVHhXP9gdl/PJX1M+7x9u16qKGEiNaFqUcXn0h yYjvBAN2zcndHQ8l5KYxRqvDVkrb4LVvwlAdlVRRbmDYI5rSLKjTIF11Gi+RMNFS+PDa DuSRe+9CUBpPdgfnd0N8ssWgLd/QKUUhY23e4cIWMTJ6xW1/4S/GTKgloi4PZCQxF7sM Ow== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9rmnx12v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:28 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLA9VG009886; Thu, 15 Feb 2024 23:14:27 GMT Received: from smtprelay07.wdc07v.mail.ibm.com ([172.16.1.74]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6p637dus-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:27 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay07.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEOj419661504 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:26 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7823D58050; Thu, 15 Feb 2024 23:14:24 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BDBBE58061; Thu, 15 Feb 2024 23:14:23 +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:23 +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 05/14] crypto: ecc - For NIST P521 use vli_num_bits to get number of bits Date: Thu, 15 Feb 2024 18:14:04 -0500 Message-ID: <20240215231414.3857320-6-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-GUID: toA10TGl9uVo4R5tqWNEMpEeJ2DrZx-6 X-Proofpoint-ORIG-GUID: toA10TGl9uVo4R5tqWNEMpEeJ2DrZx-6 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 mlxscore=0 adultscore=0 phishscore=0 mlxlogscore=999 spamscore=0 priorityscore=1501 suspectscore=0 impostorscore=0 malwarescore=0 clxscore=1015 bulkscore=0 lowpriorityscore=0 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: 1791008672931375250 X-GMAIL-MSGID: 1791008672931375250 In ecc_point_mult use vli_num_bits to determine the number of bits when using NIST P521. The change is required specifically for NIST P521 to pass mathematical tests on the public key. Signed-off-by: Stefan Berger --- crypto/ecc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index ea7b28b5e00e..0734cea284a4 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -1326,7 +1326,10 @@ static void ecc_point_mult(struct ecc_point *result, carry = vli_add(sk[0], scalar, curve->n, ndigits); vli_add(sk[1], sk[0], curve->n, ndigits); scalar = sk[!carry]; - num_bits = sizeof(u64) * ndigits * 8 + 1; + if (ndigits == 9 && !strcmp(curve->name, "nist_521")) + num_bits = vli_num_bits(scalar, ndigits); + else + num_bits = sizeof(u64) * ndigits * 8 + 1; vli_set(rx[1], point->x, ndigits); vli_set(ry[1], point->y, ndigits); From patchwork Thu Feb 15 23:14:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201822 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179540dyb; Thu, 15 Feb 2024 15:16:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVndMqGu7e0aqnauFp36HKbDv6RbseffcCaKmBZBTRE34dsYeqDPy0Y/229G+neFeghQN/YfRQbVVjHGWcOtNn9HJoBcA== X-Google-Smtp-Source: AGHT+IEfj2avWLA71jEu2GAcI6BXSSWK0a1admIOM0zuzo/w4Vxc9bJhdoSb7kXcDw4kxCXETgkX X-Received: by 2002:a92:d0ce:0:b0:365:44:1ed6 with SMTP id y14-20020a92d0ce000000b0036500441ed6mr1671253ila.5.1708038992455; Thu, 15 Feb 2024 15:16:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708038992; cv=pass; d=google.com; s=arc-20160816; b=YCN74XHxz2AHiQFKeqKbwIvj+6IOZrN8b48VTsLr6u1jyK8gO01WZrCthfb4CQUGa+ Ec8vmNbuolsAeuVsxtvm79nNYLzE47W470Fyqy9lL3pIWbFacT65qxzVY++0fpzWGbnX PAegXKJBvQMRi5VOURXtYBLrq+IG47ROv5ggVX6XOJF8JJ0WdTLzQnlrV/LfTYvyJu6a sRw/H2Gpd2GWUTbMz3hbb2MbBo57Z3SpUHHTgnrbVgwwY0Z3IAb4GM2ijL6nFYq/OcgT AAeG4V0S2qgiycFL4ayZDkZVIQ6dmVBbM2eu1tjXN4sqmQa6nBmBygeop4bppeRAu0hU geoQ== 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=h0e7AaxL4FWmhcgwWJTFxrpBd4ZzW9N+CXKvJr6JwDU=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=xwnytfXC3mT6O1KY4kAAmVaqH936hx2MqVXhTqCeDQCESNtM+geH4QCSGYu/QAnHE7 XO+d5R/OpEeiKaSo1juDjwkKlvPQtKLrZlTEuhTrveIsvgG9Hh2BGXtpEvf9TTNCc6t/ ih7K2HHVH0xbplnB6GCK44VzW+Yef/MMFzTd74i6POFIbyzJNjFqv4Xz4bYPxKrn7mzK zdecO/x4lzqBQA/h3XLF3I1kFAxPFpbfBm35boovt78V/EU2uDfDPc7vD3JVPKZpWyzy aRt4PfN/zDOwhPUNzjs3b+HWYehUX4/7DBKqQ0pAmk4Ani3rCs3hXIvENuXy68PIDuEa uqCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=FdW0TsX7; 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-67804-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67804-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id h21-20020a633855000000b005dc14cf6b6dsi1819076pgn.155.2024.02.15.15.16.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:16:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67804-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=FdW0TsX7; 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-67804-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67804-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9CD2B2815C4 for ; Thu, 15 Feb 2024 23:16:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C0832148317; Thu, 15 Feb 2024 23:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="FdW0TsX7" 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 B2260145FF1; Thu, 15 Feb 2024 23:14:34 +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=1708038876; cv=none; b=ty/HXTPp5trifbC9gAPZuz0za4tUGo67FoFK488UMHr7BYMemhjL+0Xm8VKb9roo216oa21d1n3cFB+c1tnlpGCkEIhCI/0Bw7I14gSftTVwTtTkNSDIyIYLFQ+fxw5EZutO4eIfMmDYGNw0GRTlwQ6d1zjzu4X+4N465LeumKg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038876; c=relaxed/simple; bh=iDLqYOvXqv0xVY5Kd4dar94Z4Qe+w4rHq4TTjrgyKf4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DXyAE9DLD8jegsWHkFwf6uKxckykz/dNv+kWyYpMnuhTAq+BJr3nVHyYs9PYUx4gkE/dTOFa7A8kx0AVdTKk/6bT8BpAMAmTWu6k76iL8o2mPlYsx4fUs6h+sa9stoEoZUG8chwM5BBVBlv1GzMYIw2KVjZfxw0YmtMEZjWJSLk= 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=FdW0TsX7; 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 41FMgbc2003069; Thu, 15 Feb 2024 23:14:29 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=h0e7AaxL4FWmhcgwWJTFxrpBd4ZzW9N+CXKvJr6JwDU=; b=FdW0TsX7rlmfrNAdUJzlqBz3bYpnszLg/g7mln2UEx+w8tnp+Xd1MsKf1pasjIB/oXee d/RpQnI22QOXA5TRqGiUk7Cjrlp6OhUGJhLZ4Q8r0oWkOcVhO0RLMFrHBXhpRJPDoaAN /z55VAr3RwjCSCeZJjEhdq2IDdObPkdCXqWdI+gmKyBbJkwOlB1UrWGwVcR/WPIsEgv0 6T5UtB/slGoXX7hM1iU5oM8A8F89QZJHhenEHMg529QTxo8PdHCMrRB1WD8fKYSTzjLz 2KCSAoEEHMGKnFzRSZrYHhjF9MJGWnEk7hujghcHwhZ/TASB15ak3NEAUIeOsYQUDRYX 6Q== 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 3w9un5gtct-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:28 +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 41FKZ6cc009680; Thu, 15 Feb 2024 23:14:28 GMT Received: from smtprelay02.dal12v.mail.ibm.com ([172.16.1.4]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6npm7h4k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:28 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay02.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEPp435586566 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:27 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 42BD558054; Thu, 15 Feb 2024 23:14:25 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9BA8058052; Thu, 15 Feb 2024 23:14:24 +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:24 +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 06/14] crypto: ecc - Add NIST P521 curve parameters Date: Thu, 15 Feb 2024 18:14:05 -0500 Message-ID: <20240215231414.3857320-7-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: ulpZXqmcveskDY-dWGRGes1t6B6rQCyO X-Proofpoint-GUID: ulpZXqmcveskDY-dWGRGes1t6B6rQCyO 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=955 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: 1791008694704723858 X-GMAIL-MSGID: 1791008694704723858 Add the parameters for the NIST P521 curve and define a new curve ID for it. Make the curve available in ecc_get_curve. Signed-off-by: Stefan Berger --- crypto/ecc.c | 2 ++ crypto/ecc_curve_defs.h | 44 +++++++++++++++++++++++++++++++++++++++++ include/crypto/ecdh.h | 1 + 3 files changed, 47 insertions(+) diff --git a/crypto/ecc.c b/crypto/ecc.c index 0734cea284a4..73fbbfc8d69c 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -60,6 +60,8 @@ const struct ecc_curve *ecc_get_curve(unsigned int curve_id) return &nist_p256; case ECC_CURVE_NIST_P384: return &nist_p384; + case ECC_CURVE_NIST_P521: + return &nist_p521; default: return NULL; } diff --git a/crypto/ecc_curve_defs.h b/crypto/ecc_curve_defs.h index 9719934c9428..93a47a5d460a 100644 --- a/crypto/ecc_curve_defs.h +++ b/crypto/ecc_curve_defs.h @@ -86,6 +86,50 @@ static struct ecc_curve nist_p384 = { .b = nist_p384_b }; +/* NIST P-521 */ +static u64 nist_p521_g_x[] = { 0xf97e7e31c2e5bd66ull, 0x3348b3c1856a429bull, + 0xfe1dc127a2ffa8deull, 0xa14b5e77efe75928ull, + 0xf828af606b4d3dbaull, 0x9c648139053fb521ull, + 0x9e3ecb662395b442ull, 0x858e06b70404e9cdull, + 0xc6ull }; +static u64 nist_p521_g_y[] = { 0x88be94769fd16650ull, 0x353c7086a272c240ull, + 0xc550b9013fad0761ull, 0x97ee72995ef42640ull, + 0x17afbd17273e662cull, 0x98f54449579b4468ull, + 0x5c8a5fb42c7d1bd9ull, 0x39296a789a3bc004ull, + 0x118ull }; +static u64 nist_p521_p[] = { 0xffffffffffffffffull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0x1ffull }; +static u64 nist_p521_n[] = { 0xbb6fb71e91386409ull, 0x3bb5c9b8899c47aeull, + 0x7fcc0148f709a5d0ull, 0x51868783bf2f966bull, + 0xfffffffffffffffaull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0x1ffull }; +static u64 nist_p521_a[] = { 0xfffffffffffffffcull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0x1ffull }; +static u64 nist_p521_b[] = { 0xef451fd46b503f00ull, 0x3573df883d2c34f1ull, + 0x1652c0bd3bb1bf07ull, 0x56193951ec7e937bull, + 0xb8b489918ef109e1ull, 0xa2da725b99b315f3ull, + 0x929a21a0b68540eeull, 0x953eb9618e1c9a1full, + 0x051ull }; +static struct ecc_curve nist_p521 = { + .name = "nist_521", + .g = { + .x = nist_p521_g_x, + .y = nist_p521_g_y, + .ndigits = 9, + }, + .p = nist_p521_p, + .n = nist_p521_n, + .a = nist_p521_a, + .b = nist_p521_b +}; + /* curve25519 */ static u64 curve25519_g_x[] = { 0x0000000000000009, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000 }; diff --git a/include/crypto/ecdh.h b/include/crypto/ecdh.h index a9f98078d29c..9784ecdd2fb4 100644 --- a/include/crypto/ecdh.h +++ b/include/crypto/ecdh.h @@ -26,6 +26,7 @@ #define ECC_CURVE_NIST_P192 0x0001 #define ECC_CURVE_NIST_P256 0x0002 #define ECC_CURVE_NIST_P384 0x0003 +#define ECC_CURVE_NIST_P521 0x0004 /** * struct ecdh - define an ECDH private key From patchwork Thu Feb 15 23:14:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201825 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179913dyb; Thu, 15 Feb 2024 15:17:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVOVoRVTi0Lj3O1lzKLV6s+IwkeO7V0iM9OpFuaL3cjNUxw0Y4sgGbbgJpzgpaGZbODmidVFFff0GU9G3jykT24SFkl0w== X-Google-Smtp-Source: AGHT+IEZUuxmexsj9BijNO4soHEzLTdF1vYoalFXncE3XbOf/QSRinsIs2YB7t4Yh+WsjlS0mS+U X-Received: by 2002:a17:902:cec7:b0:1db:a332:f426 with SMTP id d7-20020a170902cec700b001dba332f426mr1009143plg.6.1708039046297; Thu, 15 Feb 2024 15:17:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039046; cv=pass; d=google.com; s=arc-20160816; b=kdhL/bxi5yVxzlyczUqtYN6B1ShPN5pCxkC8NKijgNFX6U4nMJKKe5RqPAI+jxJVBQ QgQZQlpm0wCdy6QRCCvMQffDjos2k5p+vIX7NibP192s6HwUdqOmvFW3ij/Sdd2O7SNX 9uCcG24rnvb3odNOaJrTtz7SWsi7b+haQk3Vmyxu2iExZgnK0rVguWJUeaR4vHEDVIuk +TQUjOKSFkjdaNf5VVZC8cROqL7Q4qrr197HdftMRwNtkiI+4jtrfEezjW+3vlg2rehm TykroBmsPRL+Hdz81pxbemPbIPnkGrlMYXHuxSck6vi/ZH41uLoI8WskrNkNoKEvG9uC 8LOw== 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=1X3Ovo/8ptLDlDqvyLSLb5QjXzNXFfmcaP+fjkFJtDE=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=WSjxH/obTKPwfzdmr6v+GhcGssYYOHy7iVi3bCbKVjVuPazoWnb7hfW5RMhFSogoA7 O2Bqv38TtyD8jnLyKHfUy39m90u5QGIQSkF/k0lUHfEp93kv388rgcSTSEPdkH/JY2by TPPVPSmUeZaMwJwb+xxb6w69oPDQSGga5YQp0j0sbq0xZ7DQACnqtVum8O2SiAtw5wPk wl31GYaevO+WQZa/jm0mg6w5/8NdqhN3MNRhflKJyqjKqj/lvdU07OJnOcbslXCwzfTR PCEn08Ye4HHchBzgdgEvu6VS2FhLkDMXdOsqmG/SUBHe8Sf5JkwqzvtVcF1iT5AjiPHo nrow==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ehbGodaU; 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-67806-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67806-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ma15-20020a170903094f00b001d909321c08si1882539plb.596.2024.02.15.15.17.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:17:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67806-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ehbGodaU; 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-67806-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67806-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 1CF30281AF0 for ; Thu, 15 Feb 2024 23:17:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7EB8714A086; Thu, 15 Feb 2024 23:14:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="ehbGodaU" 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 A16E514601F; Thu, 15 Feb 2024 23:14:35 +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=1708038877; cv=none; b=r3r6ZHGxNX/aixpV1hJ95N9zIRNwmkD/GnNNkJQb4gTdEDjfF0gRlHiJg14qppsEmtkDq1opq7KE2c1WmVoXMpravhhRynmh0vVfBEfIiYioYmAYujMPJGij3goW07fCOnQECY/FI4t9ZH3pau7OIZ0qAt7ZwzQ73e3MghTqQO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038877; c=relaxed/simple; bh=+6nx9wQKEZJEfXA+oTjDXm30hpgDfBuMWMrmRPfQymQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KMNJzLGW6aKD78vi2UHB9X7oNAZ/7EJDBZ25iAV+y1L5jIj5ul3pY2i6VIf6ofnVvVKyI+4vhgnynEd/hj4KIojpXE0NfkiUI7/CH5C5KzXBefyhLB0IWCgfR/ffCki7Ppg0AHF+csHq7Gj1ltkAYFQKXTQgrdkoXWq0niR8xtM= 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=ehbGodaU; 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 41FMVjpv029382; Thu, 15 Feb 2024 23:14:30 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=1X3Ovo/8ptLDlDqvyLSLb5QjXzNXFfmcaP+fjkFJtDE=; b=ehbGodaULJdkWSUskv1oEpo7KT3SNkj5yL7cep8R86h/6/9Os/H24XhD5h/8ucVtLI8b 9u1z8s7it6CRFAZBl7Ux0ju6wXU+UMA+ouzDp7HTJHErDB75MHAyGErY8gyiIUw5BO2E NBZCuekN+gj8HbdaGw8eqqfEPSUzyXeDc1l8+jCG6BjO4kMWX5bVzQgLfjLtvhvAZowV UabF1c1d37VLhVN1R9Z3vRU8MM4NcgFkQWSbeqZk5H+SG3UShLnuRRIaNj5k3ybpQvt+ dP4AXBK/OYPS5hOlHizfQ+XbjE+0OlL7dEBMBsrx2XMxGzUvMQdmcuxXw3QMydV0tp+r WA== Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9ug4165s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:30 +0000 Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLuPM1004258; Thu, 15 Feb 2024 23:14:29 GMT Received: from smtprelay03.dal12v.mail.ibm.com ([172.16.1.5]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6kv0r3pd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:29 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay03.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEQtU9503290 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:28 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 215B758060; Thu, 15 Feb 2024 23:14:26 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 70ABA58052; Thu, 15 Feb 2024 23:14:25 +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:25 +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 07/14] crypto: ecdsa - Register NIST P521 and extend test suite Date: Thu, 15 Feb 2024 18:14:06 -0500 Message-ID: <20240215231414.3857320-8-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-GUID: 0ydFAb_AgM2c6Z5VOO8dx39wKGX27rNt X-Proofpoint-ORIG-GUID: 0ydFAb_AgM2c6Z5VOO8dx39wKGX27rNt 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 suspectscore=0 phishscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 clxscore=1015 spamscore=0 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: 1791008751183159473 X-GMAIL-MSGID: 1791008751183159473 Register NIST P521 as an akcipher and extend the testmgr with NIST P521-specific test vectors. Signed-off-by: Stefan Berger --- crypto/ecdsa.c | 30 ++++++++++ crypto/testmgr.c | 7 +++ crypto/testmgr.h | 146 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c index 1814f009f971..faa55c692d2a 100644 --- a/crypto/ecdsa.c +++ b/crypto/ecdsa.c @@ -269,6 +269,28 @@ static unsigned int ecdsa_max_size(struct crypto_akcipher *tfm) return ctx->pub_key.ndigits << ECC_DIGITS_TO_BYTES_SHIFT; } +static int ecdsa_nist_p521_init_tfm(struct crypto_akcipher *tfm) +{ + struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm); + + return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P521); +} + +static struct akcipher_alg ecdsa_nist_p521 = { + .verify = ecdsa_verify, + .set_pub_key = ecdsa_set_pub_key, + .max_size = ecdsa_max_size, + .init = ecdsa_nist_p521_init_tfm, + .exit = ecdsa_exit_tfm, + .base = { + .cra_name = "ecdsa-nist-p521", + .cra_driver_name = "ecdsa-nist-p521-generic", + .cra_priority = 100, + .cra_module = THIS_MODULE, + .cra_ctxsize = sizeof(struct ecc_ctx), + }, +}; + static int ecdsa_nist_p384_init_tfm(struct crypto_akcipher *tfm) { struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm); @@ -352,8 +374,15 @@ static int __init ecdsa_init(void) if (ret) goto nist_p384_error; + ret = crypto_register_akcipher(&ecdsa_nist_p521); + if (ret) + goto nist_p521_error; + return 0; +nist_p521_error: + crypto_unregister_akcipher(&ecdsa_nist_p384); + nist_p384_error: crypto_unregister_akcipher(&ecdsa_nist_p256); @@ -369,6 +398,7 @@ static void __exit ecdsa_exit(void) crypto_unregister_akcipher(&ecdsa_nist_p192); crypto_unregister_akcipher(&ecdsa_nist_p256); crypto_unregister_akcipher(&ecdsa_nist_p384); + crypto_unregister_akcipher(&ecdsa_nist_p521); } subsys_initcall(ecdsa_init); diff --git a/crypto/testmgr.c b/crypto/testmgr.c index 3dddd288ca02..00f5a6cf341a 100644 --- a/crypto/testmgr.c +++ b/crypto/testmgr.c @@ -5097,6 +5097,13 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .akcipher = __VECS(ecdsa_nist_p384_tv_template) } + }, { + .alg = "ecdsa-nist-p521", + .test = alg_test_akcipher, + .fips_allowed = 1, + .suite = { + .akcipher = __VECS(ecdsa_nist_p521_tv_template) + } }, { .alg = "ecrdsa", .test = alg_test_akcipher, diff --git a/crypto/testmgr.h b/crypto/testmgr.h index 986f331a5fc2..9bde04be8df9 100644 --- a/crypto/testmgr.h +++ b/crypto/testmgr.h @@ -991,6 +991,152 @@ static const struct akcipher_testvec ecdsa_nist_p384_tv_template[] = { }, }; +static const struct akcipher_testvec ecdsa_nist_p521_tv_template[] = { + { + .key = /* secp521r1(sha224) */ + "\x04\x01\x4f\x43\x18\xb6\xa9\xc9\x5d\x68\xd3\xa9\x42\xf8\x98\xc0" + "\xd2\xd1\xa9\x50\x3b\xe8\xc4\x40\xe6\x11\x78\x88\x4b\xbd\x76\xa7" + "\x9a\xe0\xdd\x31\xa4\x67\x78\x45\x33\x9e\x8c\xd1\xc7\x44\xac\x61" + "\x68\xc8\x04\xe7\x5c\x79\xb1\xf1\x41\x0c\x71\xc0\x53\xa8\xbc\xfb" + "\xf5\xca\xd4\x01\x40\xfd\xa3\x45\xda\x08\xe0\xb4\xcb\x28\x3b\x0a" + "\x02\x35\x5f\x02\x9f\x3f\xcd\xef\x08\x22\x40\x97\x74\x65\xb7\x76" + "\x85\xc7\xc0\x5c\xfb\x81\xe1\xa5\xde\x0c\x4e\x8b\x12\x31\xb6\x47" + "\xed\x37\x0f\x99\x3f\x26\xba\xa3\x8e\xff\x79\x34\x7c\x3a\xfe\x1f" + "\x3b\x83\x82\x2f\x14", + .key_len = 133, + .params = + "\x30\x10\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x05\x2b\x81\x04" + "\x00\x23", + .param_len = 18, + .m = + "\xa2\x3a\x6a\x8c\x7b\x3c\xf2\x51\xf8\xbe\x5f\x4f\x3b\x15\x05\xc4" + "\xb5\xbc\x19\xe7\x21\x85\xe9\x23\x06\x33\x62\xfb", + .m_size = 28, + .algo = OID_id_ecdsa_with_sha224, + .c = + "\x30\x81\x86\x02\x41\x01\xd6\x43\xe7\xff\x42\xb2\xba\x74\x35\xf6" + "\xdc\x6d\x02\x7b\x22\xac\xe2\xef\x07\x92\xee\x60\x94\x06\xf8\x3f" + "\x59\x0f\x74\xf0\x3f\xd8\x18\xc6\x37\x8a\xcb\xa7\xd8\x7d\x98\x85" + "\x29\x88\xff\x0b\x94\x94\x6c\xa6\x9b\x89\x8b\x1e\xfd\x09\x46\x6b" + "\xc7\xaf\x7a\xb9\x19\x0a\x02\x41\x3a\x26\x0d\x55\xcd\x23\x1e\x7d" + "\xa0\x5e\xf9\x88\xf3\xd2\x32\x90\x57\x0f\xf8\x65\x97\x6b\x09\x4d" + "\x22\x26\x0b\x5f\x49\x32\x6b\x91\x99\x30\x90\x0f\x1c\x8f\x78\xd3" + "\x9f\x0e\x64\xcc\xc4\xe8\x43\xd9\x0e\x1c\xad\x22\xda\x82\x00\x35" + "\xa3\x50\xb1\xa5\x98\x92\x2a\xa5\x52", + .c_size = 137, + .public_key_vec = true, + .siggen_sigver_test = true, + }, + { + .key = /* secp521r1(sha256) */ + "\x04\x01\x05\x3a\x6b\x3b\x5a\x0f\xa7\xb9\xb7\x32\x53\x4e\xe2\xae" + "\x0a\x52\xc5\xda\xdd\x5a\x79\x1c\x30\x2d\x33\x07\x79\xd5\x70\x14" + "\x61\x0c\xec\x26\x4d\xd8\x35\x57\x04\x1d\x88\x33\x4d\xce\x05\x36" + "\xa5\xaf\x56\x84\xfa\x0b\x9e\xff\x7b\x30\x4b\x92\x1d\x06\xf8\x81" + "\x24\x1e\x51\x00\x09\x21\x51\xf7\x46\x0a\x77\xdb\xb5\x0c\xe7\x9c" + "\xff\x27\x3c\x02\x71\xd7\x85\x36\xf1\xaa\x11\x59\xd8\xb8\xdc\x09" + "\xdc\x6d\x5a\x6f\x63\x07\x6c\xe1\xe5\x4d\x6e\x0f\x6e\xfb\x7c\x05" + "\x8a\xe9\x53\xa8\xcf\xce\x43\x0e\x82\x20\x86\xbc\x88\x9c\xb7\xe3" + "\xe6\x77\x1e\x1f\x8a", + .key_len = 133, + .params = + "\x30\x10\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x05\x2b\x81\x04" + "\x00\x23", + .param_len = 18, + .m = + "\xcc\x97\x73\x0c\x73\xa2\x53\x2b\xfa\xd7\x83\x1d\x0c\x72\x1b\x39" + "\x80\x71\x8d\xdd\xc5\x9b\xff\x55\x32\x98\x25\xa2\x58\x2e\xb7\x73", + .m_size = 32, + .algo = OID_id_ecdsa_with_sha256, + .c = + "\x30\x81\x88\x02\x42\x00\xcd\xa5\x5f\x57\x52\x27\x78\x3a\xb5\x06" + "\x0f\xfd\x83\xfc\x0e\xd9\xce\x50\x9f\x7d\x1f\xca\x8b\xa8\x2d\x56" + "\x3c\xf6\xf0\xd8\xe1\xb7\x5d\x95\x35\x6f\x02\x0e\xaf\xe1\x4c\xae" + "\xce\x54\x76\x9a\xc2\x8f\xb8\x38\x1f\x46\x0b\x04\x64\x34\x79\xde" + "\x7e\xd7\x59\x10\xe9\xd9\xd5\x02\x42\x01\xcf\x50\x85\x38\xf9\x15" + "\x83\x18\x04\x6b\x35\xae\x65\xb5\x99\x12\x0a\xa9\x79\x24\xb9\x37" + "\x35\xdd\xa0\xe0\x87\x2c\x44\x4b\x5a\xee\xaf\xfa\x10\xdd\x9b\xfb" + "\x36\x1a\x31\x03\x42\x02\x5f\x50\xf0\xa2\x0d\x1c\x57\x56\x8f\x12" + "\xb7\x1d\x91\x55\x38\xb6\xf6\x34\x65\xc7\xbd", + .c_size = 139, + .public_key_vec = true, + .siggen_sigver_test = true, + }, + { + .key = /* secp521r1(sha384) */ + "\x04\x00\x2e\xd6\x21\x04\x75\xc3\xdc\x7d\xff\x0e\xf3\x70\x25\x2b" + "\xad\x72\xfc\x5a\x91\xf1\xd5\x9c\x64\xf3\x1f\x47\x11\x10\x62\x33" + "\xfd\x2e\xe8\x32\xca\x9e\x6f\x0a\x4c\x5b\x35\x9a\x46\xc5\xe7\xd4" + "\x38\xda\xb2\xf0\xf4\x87\xf3\x86\xf4\xea\x70\xad\x1e\xd4\x78\x8c" + "\x36\x18\x17\x00\xa2\xa0\x34\x1b\x2e\x6a\xdf\x06\xd6\x99\x2d\x47" + "\x50\x92\x1a\x8a\x72\x9c\x23\x44\xfa\xa7\xa9\xed\xa6\xef\x26\x14" + "\xb3\x9d\xfe\x5e\xa3\x8c\xd8\x29\xf8\xdf\xad\xa6\xab\xfc\xdd\x46" + "\x22\x6e\xd7\x35\xc7\x23\xb7\x13\xae\xb6\x34\xff\xd7\x80\xe5\x39" + "\xb3\x3b\x5b\x1b\x94", + .key_len = 133, + .params = + "\x30\x10\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x05\x2b\x81\x04" + "\x00\x23", + .param_len = 18, + .m = + "\x36\x98\xd6\x82\xfa\xad\xed\x3c\xb9\x40\xb6\x4d\x9e\xb7\x04\x26" + "\xad\x72\x34\x44\xd2\x81\xb4\x9b\xbe\x01\x04\x7a\xd8\x50\xf8\x59" + "\xba\xad\x23\x85\x6b\x59\xbe\xfb\xf6\x86\xd4\x67\xa8\x43\x28\x76", + .m_size = 48, + .algo = OID_id_ecdsa_with_sha384, + .c = + "\x30\x81\x88\x02\x42\x00\x93\x96\x76\x3c\x27\xea\xaa\x9c\x26\xec" + "\x51\xdc\xe8\x35\x5e\xae\x16\xf2\x4b\x64\x98\xf7\xec\xda\xc7\x7e" + "\x42\x71\x86\x57\x2d\xf1\x7d\xe4\xdf\x9b\x7d\x9e\x47\xca\x33\x32" + "\x76\x06\xd0\xf9\xc0\xe4\xe6\x84\x59\xfd\x1a\xc4\x40\xdd\x43\xb8" + "\x6a\xdd\xfb\xe6\x63\x4e\x28\x02\x42\x00\xff\xc3\x6a\x87\x6e\xb5" + "\x13\x1f\x20\x55\xce\x37\x97\xc9\x05\x51\xe5\xe4\x3c\xbc\x93\x65" + "\x57\x1c\x30\xda\xa7\xcd\x26\x28\x76\x3b\x52\xdf\xc4\xc0\xdb\x54" + "\xdb\x8a\x0d\x6a\xc3\xf3\x7a\xd1\xfa\xe7\xa7\xe5\x5a\x94\x56\xcf" + "\x8f\xb4\x22\xc6\x4f\xab\x2b\x62\xc1\x42\xb1", + .c_size = 139, + .public_key_vec = true, + .siggen_sigver_test = true, + }, + { + .key = /* secp521r1(sha512) */ + "\x04\x00\xc7\x65\xee\x0b\x86\x7d\x8f\x02\xf1\x74\x5b\xb0\x4c\x3f" + "\xa6\x35\x60\x9f\x55\x23\x11\xcc\xdf\xb8\x42\x99\xee\x6c\x96\x6a" + "\x27\xa2\x56\xb2\x2b\x03\xad\x0f\xe7\x97\xde\x09\x5d\xb4\xc5\x5f" + "\xbd\x87\x37\xbf\x5a\x16\x35\x56\x08\xfd\x6f\x06\x1a\x1c\x84\xee" + "\xc3\x64\xb3\x00\x9e\xbd\x6e\x60\x76\xee\x69\xfd\x3a\xb8\xcd\x7e" + "\x91\x68\x53\x57\x44\x13\x2e\x77\x09\x2a\xbe\x48\xbd\x91\xd8\xf6" + "\x21\x16\x53\x99\xd5\xf0\x40\xad\xa6\xf8\x58\x26\xb6\x9a\xf8\x77" + "\xfe\x3a\x05\x1a\xdb\xa9\x0f\xc0\x6c\x76\x30\x8c\xd8\xde\x44\xae" + "\xd0\x17\xdf\x49\x6a", + .key_len = 133, + .params = + "\x30\x10\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\x06\x05\x2b\x81\x04" + "\x00\x23", + .param_len = 18, + .m = + "\x5c\xa6\xbc\x79\xb8\xa0\x1e\x11\x83\xf7\xe9\x05\xdf\xba\xf7\x69" + "\x97\x22\x32\xe4\x94\x7c\x65\xbd\x74\xc6\x9a\x8b\xbd\x0d\xdc\xed" + "\xf5\x9c\xeb\xe1\xc5\x68\x40\xf2\xc7\x04\xde\x9e\x0d\x76\xc5\xa3" + "\xf9\x3c\x6c\x98\x08\x31\xbd\x39\xe8\x42\x7f\x80\x39\x6f\xfe\x68", + .m_size = 64, + .algo = OID_id_ecdsa_with_sha512, + .c = + "\x30\x81\x88\x02\x42\x01\x5c\x71\x86\x96\xac\x21\x33\x7e\x4e\xaa" + "\x86\xec\xa8\x05\x03\x52\x56\x63\x0e\x02\xcc\x94\xa9\x05\xb9\xfb" + "\x62\x1e\x42\x03\x6c\x74\x8a\x1f\x12\x3e\xb7\x7e\x51\xff\x7f\x27" + "\x93\xe8\x6c\x49\x7d\x28\xfc\x80\xa6\x13\xfc\xb6\x90\xf7\xbb\x28" + "\xb5\x04\xb0\xb6\x33\x1c\x7e\x02\x42\x01\x70\x43\x52\x1d\xe3\xc6" + "\xbd\x5a\x40\x95\x35\x89\x4f\x41\x5f\x9e\x19\x88\x05\x3e\x43\x39" + "\x01\xbd\xb7\x7a\x76\x37\x51\x47\x49\x98\x12\x71\xd0\xe9\xca\xa7" + "\xc0\xcb\xaa\x00\x55\xbb\x6a\xb4\x73\x00\xd2\x72\x74\x13\x63\x39" + "\xa6\xe5\x25\x46\x1e\x77\x44\x78\xe0\xd1\x04", + .c_size = 139, + .public_key_vec = true, + .siggen_sigver_test = true, + }, +}; + /* * EC-RDSA test vectors are generated by gost-engine. */ From patchwork Thu Feb 15 23:14:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201824 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179858dyb; Thu, 15 Feb 2024 15:17:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUgGWlASQ6pl0HiVcyT4v4cBXLqduc6fKysY5o2yHGqCmy4eX7pjB4zjV7IEKO2EKsEnw0bHURVFo5U8+3WJYOdj0IALA== X-Google-Smtp-Source: AGHT+IE5Oi7QoMlo5IynNUPmoApwyJ0H67J/atRGwTnDw7XaJIAZ8rpjfasqKjcIFwU+neSO9Yi6 X-Received: by 2002:a17:902:e74b:b0:1db:5e6b:c520 with SMTP id p11-20020a170902e74b00b001db5e6bc520mr3917086plf.6.1708039035733; Thu, 15 Feb 2024 15:17:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039035; cv=pass; d=google.com; s=arc-20160816; b=QOoTjBRYDl1wyO38fMRUMyjCxnFaSe8C0AmRYLFrVyhy9BBXNFgxj51vTVeEZZxZDx qoliFbXmec82AS3u2G4QlNAmBfvLBxukIskwtr/os47VnZ/gmtE95piFaeXLBMVJwdVg MluARWX49JuMreL7kAFT+8lsqcNq5qvloEdsNuLfOqlU2jEue2JZPh+8Q0oQX83jKLl9 9YKVHAVsDqnaQznuLaOY4G/yG/FrUOhXpQOJ8t64cxfld061IvTb0CXDfN9WmlqT2Oaq 2vAHi5Tf8LY/VGbN5U1+20mZM0rZ+giTyqcEqLXXQVzRpvxAaLR4sEMhG2zhd75PgCYN tMRA== 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=oMikrV2Be8nChSoWAP37zjvL3Jd3Ki9N5SP4cHk5r8Y=; fh=5qrAmJIJ/drdgfATGzUXDVJ5j7UmDotCYqhuANWkuiI=; b=nursKWbuJmZlJfRMqllwphjD0SQdjcjHzOOpKcaSXTSw9iIpZXAWRNzD8GBmGg/57p 7Go35kFXzko8548+KrXTNJuOXijhXuEZoK5Y7z+XBVnBIcLaA+4xxZaYtj9E8EkzWdXD das/3h/1SOnviP8QLi9xl83136pqYlyJcVLIiAOMeZ3KQ3O1Ph6MBXkvlmRkwX9U4ExH Lv79r2rBWMa4PVYylUkrxXn86kKhJ7coffKUUkytEQBDwLtnRDtPTi3fbX1hCoDa6JoQ kSLZdWRBr/bokcEd3wux08Tm1tqk/FyYopcnAKZOPYRq/9vZ58HiSH4VbBjEKkTyE069 5rNg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=fjBkSMdL; 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-67808-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67808-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j15-20020a170903024f00b001db29cbeb07si1860676plh.302.2024.02.15.15.17.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:17:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67808-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=fjBkSMdL; 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-67808-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67808-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 84F6F281843 for ; Thu, 15 Feb 2024 23:17:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 20C5814900F; Thu, 15 Feb 2024 23:14:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="fjBkSMdL" 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 2FFA51474AC; Thu, 15 Feb 2024 23:14:37 +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=1708038878; cv=none; b=ps5m5bo9URP/UkEmVVEN/+/CkPtHMhIdOd5R4NQnDuDBUvdq3B1jByCSTuvkVqnQeVLz1I1SBHBZkaEstY0CzS/PIqfFaevB97AA35F1JPS1w4e5titRXQQoxCVChDwrC0c9KiJUhLKON66wTYgtCYfVteZeIKWw+dODJ9k62d8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038878; c=relaxed/simple; bh=3YD2nLoVeWDrWiPNpx65a68F+wXSuNVNDDyl/CAP0AY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sIrLFUFqu/EDqkcAvxfktFhLm3E/N+GbQrfxFqdQ6coAShfKIK4DBsMJ3rOEn9JF+k42p4u9UBYtdGUZDVZN92uGXxH76NRZLswIeHfMAYr9kkrIrgekNE/gvYbYA/c6X3j1uLh4bEGQgyeu6cN5qkruN5y3kkoCSNX6GmGujFU= 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=fjBkSMdL; 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 (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMl9AR019045; Thu, 15 Feb 2024 23:14:31 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=oMikrV2Be8nChSoWAP37zjvL3Jd3Ki9N5SP4cHk5r8Y=; b=fjBkSMdLfIixWTE6cKHLDEWd7pImTVLGQuJ/mzOymH8jOK6Kz3xpoQFAKP79j3gESJ7I +mo0MSbONnj8KWvpXftEdtweCsN+LrjY6gG5N6TVCLyNvzepqjIPoTUU+XzkFLKEMI2a zkcGEPi5fg5OmEKzSPLdPka11QxOFnRnkdTXcZ5LtOdSB/N3a4hU4nnbamQ8bRl/HX33 B5ljpqUEd+lLUC5p4xgxeCy6voWErRGSzW87hiHzMvWfVJ+TYukEvOYwOx8qNhyFdRbT Cp+sEj5xx6cs51hWi3yrxEM3TxqBt/dfqmr2I2LfsOZpk47JdoAzuOupGX4B2R3HtEJz ig== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9ugch28f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:31 +0000 Received: from m0360072.ppops.net (m0360072.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 41FMv3ir020543; Thu, 15 Feb 2024 23:14:30 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9ugch283-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:30 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FN4UEk016197; Thu, 15 Feb 2024 23:14:30 GMT Received: from smtprelay04.dal12v.mail.ibm.com ([172.16.1.6]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6mymytf7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:30 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay04.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNER6T7406198 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:29 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 069205805E; Thu, 15 Feb 2024 23:14:27 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 451DD58064; Thu, 15 Feb 2024 23:14:26 +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:26 +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 , David Howells Subject: [PATCH v2 08/14] x509: Add OID for NIST P521 and extend parser for it Date: Thu, 15 Feb 2024 18:14:07 -0500 Message-ID: <20240215231414.3857320-9-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: AdisvOOwo8mWwEg3iMnmrtTk_sCyfOct X-Proofpoint-GUID: SVvE5rvIYFIWirxByalv5RUWXVg7imVI 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 malwarescore=0 suspectscore=0 spamscore=0 mlxscore=0 clxscore=1015 impostorscore=0 phishscore=0 priorityscore=1501 adultscore=0 bulkscore=0 mlxlogscore=905 lowpriorityscore=0 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: 1791008739894337641 X-GMAIL-MSGID: 1791008739894337641 Prepare the x509 parser to accept NIST P521 certificates and add the OID for ansip521r1, which is the identifier for NIST P521. Cc: David Howells Signed-off-by: Stefan Berger --- crypto/asymmetric_keys/x509_cert_parser.c | 3 +++ include/linux/oid_registry.h | 1 + 2 files changed, 4 insertions(+) diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c index 487204d39426..99f809b7910b 100644 --- a/crypto/asymmetric_keys/x509_cert_parser.c +++ b/crypto/asymmetric_keys/x509_cert_parser.c @@ -538,6 +538,9 @@ int x509_extract_key_data(void *context, size_t hdrlen, case OID_id_ansip384r1: ctx->cert->pub->pkey_algo = "ecdsa-nist-p384"; break; + case OID_id_ansip521r1: + ctx->cert->pub->pkey_algo = "ecdsa-nist-p521"; + break; default: return -ENOPKG; } diff --git a/include/linux/oid_registry.h b/include/linux/oid_registry.h index 3921fbed0b28..af16d96fbbf2 100644 --- a/include/linux/oid_registry.h +++ b/include/linux/oid_registry.h @@ -65,6 +65,7 @@ enum OID { OID_Scram, /* 1.3.6.1.5.5.14 */ OID_certAuthInfoAccess, /* 1.3.6.1.5.5.7.1.1 */ OID_id_ansip384r1, /* 1.3.132.0.34 */ + OID_id_ansip521r1, /* 1.3.132.0.35 */ OID_sha256, /* 2.16.840.1.101.3.4.2.1 */ OID_sha384, /* 2.16.840.1.101.3.4.2.2 */ OID_sha512, /* 2.16.840.1.101.3.4.2.3 */ From patchwork Thu Feb 15 23:14:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201823 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179843dyb; Thu, 15 Feb 2024 15:17:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXa1CiRVS2msc7/7G77taxqOm/m4+sVKQMZTjo2gsrc/HrpNJX/obqetmuuj6/yprjg85mYIJeZk6o7BJH6cZedGaOazw== X-Google-Smtp-Source: AGHT+IETxpdkBOiKlYXtHK/r20XABJVsIXLhH4P/MPQWs1GM7TEqz+MxxmyyVQ/oBJj39G50Hv2z X-Received: by 2002:a05:6358:7e04:b0:176:9bfd:d092 with SMTP id o4-20020a0563587e0400b001769bfdd092mr3529194rwm.18.1708039033932; Thu, 15 Feb 2024 15:17:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039033; cv=pass; d=google.com; s=arc-20160816; b=ILAQ5f8U2TFVDjj3nzPsIzD9/bs48i4wuTZXhoLfIY3244NRTKQ3sBXQ98/Z2gZh+Y pljGue3FjQyveDfcr+g022pG1mJ+sEOrojAzSfoce+p2d6Z48WcfJV1UpeG0YO1kNGLo C94o8zHQ8ImL2YsDz0/5SST2MuN1Xm1F41npA+r7AQHr45CoheUhm9R1l1sFBTmmqZp8 gcHgtIv+za5w0Tmsf4oHzKrfBohuYtWlxx7EK3TIfLCRENCsIieTLEyGnruI25AE28Aq YbCNz1dNIPbLJLTr5rae6U7xRSJXoJPTfo2/K4azxtVb/ZCMLMtBluV7iUO23CAdPzEh VCdw== 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=gXqWIgZmXlBKbikHv5ne67VayakspN2QeljIzb45JEc=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=fvA7EBJwia4epYRVxSXul1LbfSckoemokSYodsHMt6gHXRfoUCFCWs2+F/jn7FohTK CClrTe91FQRx2pqw6HAX7vtFOfskdNdsAGTiFTrOD4c9VUpmJce7q1nrECyxR/nRkYKB ahlcyYjtexLiM/y/YdhOGOly/mrVuuyjxFckLYCwAxBSSv6TODV4OVf50jz+TyqpAcpU O3Cjr5K2UbSD/neg5iaOut58QA08Rpss4acNDr/S5og47Lxo6D21golnf1hRtZc7UmFN nvOrVzOLBIxSiLV+M/RPxybAQYZGmIF8k/qeGE6WUuO3O6HPGQAUeQsXmpQONM+fq4v3 iiUw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=kJCS3n4D; 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-67807-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67807-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id h21-20020a633855000000b005dc14cf6b6dsi1819076pgn.155.2024.02.15.15.17.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:17:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67807-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=kJCS3n4D; 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-67807-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67807-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id B5DA828151C for ; Thu, 15 Feb 2024 23:17:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F063B149006; Thu, 15 Feb 2024 23:14:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="kJCS3n4D" 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 1410D1474A4; Thu, 15 Feb 2024 23:14:36 +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=1708038878; cv=none; b=Mlx/ego0ZV4SKJfUUHL53VO/0gP3KSLVT9ajUZnkMQXSWCwYIbVrYGNdxiKP8tH/dd3svQwPgyrPO/74RPQn6U3D/9cMKhInFUK90Q30P1cfxp9epOssyLPKhJuh2bStVncw138AcvODIoBoxOvwPmr/pEqYe8SJ6jVE1FXlkko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038878; c=relaxed/simple; bh=6qLIPqSabW6RW0OWsYRTTEWkOu71GbSE4Aw7FmVAmlc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RoKz6NSRPlK43T7iet7ITFdMLBy8Yrw0vJsEqsRvWzJLkXNq4IVL94Bya6laYddxyEgxTcbzboQKQktIOMLVaYGMPqwBudre/0ybBeyu5Ip6ayDTvDushlxjkJMF90kxmH71irykLvEWNdwND6lNskMMQbLJulFjqMbed7eg/oI= 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=kJCS3n4D; 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 (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMkmQI018163; Thu, 15 Feb 2024 23:14:31 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=gXqWIgZmXlBKbikHv5ne67VayakspN2QeljIzb45JEc=; b=kJCS3n4DMUtvYkKSf3Nfc0RPaE/oiv/K6CNuCsnMqQQqyeHiA9Bg1FQY3JVHleg0K3my k9TiOl4PGPUdxCI7y/Utg5Zi2Da8dQTnXgOEXFORq1eAWujBqrvmE+HrLm+e4xP1Wyge kLZFCH8sNQXKLYpXrAVYeb/G7STL36uPb2muF/elXu4jDYVuYYckiOaVxTJoAbseRvhv eaXrzPD8Rz+KIh3bkH7rqFacXw3e1fAjTdqI3Nqp3vYIJwJBcz6Vn0IkXtFABY3iicXr N/D4bhzLAoQOaZ7abzsjLat+EXBqsZxq960JL0Exwhrcw3uUEkySw0Djxm3ebWrp2Kfn Nw== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9ugch28a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:31 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLHWEQ009935; Thu, 15 Feb 2024 23:14:30 GMT Received: from smtprelay04.dal12v.mail.ibm.com ([172.16.1.6]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6p637dv8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:30 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay04.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNER0V14877348 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:30 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CE3E458045; Thu, 15 Feb 2024 23:14:27 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2A14358050; Thu, 15 Feb 2024 23:14:27 +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:27 +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 09/14] crypto: ecdh - Use properly formatted digits to check for valid key Date: Thu, 15 Feb 2024 18:14:08 -0500 Message-ID: <20240215231414.3857320-10-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: COvWtIQGoIFyGOrtHwxszZih0RV0XK8_ X-Proofpoint-GUID: COvWtIQGoIFyGOrtHwxszZih0RV0XK8_ 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 malwarescore=0 suspectscore=0 spamscore=0 mlxscore=0 clxscore=1015 impostorscore=0 phishscore=0 priorityscore=1501 adultscore=0 bulkscore=0 mlxlogscore=999 lowpriorityscore=0 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: 1791008738397607625 X-GMAIL-MSGID: 1791008738397607625 ecc_is_key_valid expects a key with the most significant digit in the last entry of the digit array. Currently a reverse key is passed to ecc_is_key_valid that then passes the rather simple test checking whether the private key is in range [2, n-3]. For all current ecdh-supported curves (NIST P192/256/384) the 'n' parameter is a rather large number, therefore easily passing this test. Fix the format of the digit by using ecc_digits_from_bytes to create a temporary private key and pass it to the test function. Keep the swapped key in ctx->private_key. Signed-off-by: Stefan Berger --- crypto/ecdh.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crypto/ecdh.c b/crypto/ecdh.c index 80afee3234fb..f187365db7b6 100644 --- a/crypto/ecdh.c +++ b/crypto/ecdh.c @@ -27,6 +27,8 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, unsigned int len) { struct ecdh_ctx *ctx = ecdh_get_ctx(tfm); + u64 priv[ECC_MAX_DIGITS]; + unsigned int nbytes; struct ecdh params; if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0 || @@ -37,10 +39,13 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, return ecc_gen_privkey(ctx->curve_id, ctx->ndigits, ctx->private_key); - memcpy(ctx->private_key, params.key, params.key_size); + nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT; + + memcpy(ctx->private_key, params.key, nbytes); + ecc_swap_digits(ctx->private_key, priv, ctx->ndigits); if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits, - ctx->private_key, params.key_size) < 0) { + priv, params.key_size) < 0) { memzero_explicit(ctx->private_key, params.key_size); return -EINVAL; } From patchwork Thu Feb 15 23:14:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201826 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp179942dyb; Thu, 15 Feb 2024 15:17:29 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWLLQskN5ItVUilba+6MEf7u8VvVB0SMmqZD/ZbuBuk7TsNyWANY091DXMzBRBMx5DqxlG8z3TP4I2EmMVl1iU4ZppyMA== X-Google-Smtp-Source: AGHT+IFsra497nBoE2ohOETkrkj8T2E3RsBsPAneL0/IOsxU0NVk+62wDAKVS26llmJUvs38oiUN X-Received: by 2002:a17:902:e74b:b0:1db:5e6b:c520 with SMTP id p11-20020a170902e74b00b001db5e6bc520mr3917636plf.6.1708039049421; Thu, 15 Feb 2024 15:17:29 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039049; cv=pass; d=google.com; s=arc-20160816; b=bzLc8veQoB0pMq9Y7wjNkekiVY3CM61Fa6Kah9jcV8GM5+2DTbmUZCnvU9YtUcITua EsYGVsUbTmwM9PbcYKUZmgKVLnuAtVWNxWbbFvw8v4Fi+oZDlXtiRrku32j/ItTak42E 5MASA2D2yWArPouVV0+CERdtOSqNZKWlVvEmhTTbWY28TxTJUEW5juN8PpwogYPOQBFB iCusML3l+iIy/5bjLMJodV+OigdZvQUG6EB/lUVmAr06bSwo18yu3rkZSC/tHwe8otWL ZKgUHBq5n0LEd7Ad6Sum9nLkD2GJDaDqI7DV3uhgd0css1hU15/MQuhEAssoQFSf0c0Z D2Ng== 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=fvkw34qL4rk0HGg2B+0ywRlkEipg9FXOfBEaoEyELT4=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=tUl+uUwQ9NT7eJ+RlEkkOZwv+ITIyWYn0lnfwB23DHiYTHBPnwVRy+eYs0sRFwT2v8 M69o04HapC3DNDe86lb7HAIo2vTPLl+gTwZ/owk4l3Ax7rCBWNM9duZzhNP2xtL0435J FZbBN4tMbaEHyQusIEa3C0ZM+Mbeod7QwomaVjAjLbM8ZJJ+Vb3zH8AkbaAI/GH4aZs9 BE7kfNMStyiCVb93gzkKE5ptjg05GJv6py75oiM/YP7ZFUZjhcMT3JNr6wEpnOj30qQG XkFNtXJNESkWfID9ZLR7St3l4CUy0hak22aRbyvwCCs8BRBWV+iUiTYhziZzprxJ7hTx 9YJQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=Kg34Uy9J; 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-67809-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67809-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id f10-20020a170902684a00b001db7e45c0e4si1865048pln.37.2024.02.15.15.17.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:17:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67809-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=Kg34Uy9J; 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-67809-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67809-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 2B472281A15 for ; Thu, 15 Feb 2024 23:17:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B7E2B14A092; Thu, 15 Feb 2024 23:14:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="Kg34Uy9J" 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 00ED81474C4; Thu, 15 Feb 2024 23:14:37 +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=1708038879; cv=none; b=qLAjBmL/AK8AM4r7l0SZva9OJnXnzgCjEZahkalm5TdwGeDIbVKD/VBfgPWU8U7ajzKgp5LWWgGkjXQvfDoku0dh2MlqhVD0MPt0dRBh/Qke9lX4F6sDAD8jyCrTSWiXTJaEuO/zS5i94MgjyEHjGlCyq53aI9ysg/p3B9JVcvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038879; c=relaxed/simple; bh=+wrKDJnCE2zkQQ/ejXAW3OkteKB8kBUl4MJe6OLpHRw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X4JKi00hp/Sai44027UU4M/hH3c2dlmQNcpmQv80D2V4lBwkG12ql6t4ThIiNr6ih7FVmT2qmptJ7mrSb5L/T1XGRHGSTA8mGo6hZ6u6UyPvlfpiFrA0Rqmfv6GhTPTDg+KC+I3hXqOUp8gvLp75rTXx7l/Bk4VBDJ5+cpDI1Ak= 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=Kg34Uy9J; 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 (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMl39w018886; Thu, 15 Feb 2024 23:14:32 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=fvkw34qL4rk0HGg2B+0ywRlkEipg9FXOfBEaoEyELT4=; b=Kg34Uy9Joo0QI1QdShFbxf+ZAfdDkXCtNEZMfNsj0+TsaGxWKLuNEBSp4oLfwtUA4SUF ebKzw5hJkcPorEWBaq06tl6hv+T9H6f93fBDrU22v8IxavQFO/twE4NBPmsKbMYpTf1t oabgRhUXvV10T1jIvDP8llvcfGJ8SSRJaRmNhDxEbfO6VD0VbBhpdHMMpCiAYnbbtZT6 9OpTwE8JV5Q9dZL4AXgSFkx82YcOhuNa1gvt5davYoh4YxNAsxMMxCfecIbFHklDssSY i8i+tNqvh5gBYItvbDjoDnPGoh79vyeUUZVAS48LGVWmlV6h0xYfHrcXoKf/fDhWIplF iw== 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 3w9ugch28s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:32 +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 41FLB5H4010083; Thu, 15 Feb 2024 23:14:31 GMT Received: from smtprelay05.dal12v.mail.ibm.com ([172.16.1.7]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6npm7h4u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:31 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay05.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEStd20709930 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:31 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AB69E58054; Thu, 15 Feb 2024 23:14:28 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F1C6058050; Thu, 15 Feb 2024 23:14:27 +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:27 +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 10/14] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array Date: Thu, 15 Feb 2024 18:14:09 -0500 Message-ID: <20240215231414.3857320-11-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: cTTdaRoLdlx0xsX8gtPrGlw2PQ6VnaQg X-Proofpoint-GUID: cTTdaRoLdlx0xsX8gtPrGlw2PQ6VnaQg 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 malwarescore=0 suspectscore=0 spamscore=0 mlxscore=0 clxscore=1015 impostorscore=0 phishscore=0 priorityscore=1501 adultscore=0 bulkscore=0 mlxlogscore=999 lowpriorityscore=0 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: 1791008754518220289 X-GMAIL-MSGID: 1791008754518220289 Implement ecc_digits_to_bytes to convert an array of digits into an nbytes-sized byte array. Signed-off-by: Stefan Berger --- include/crypto/internal/ecc.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h index b8ca5023b3b5..6229aa3f3218 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -75,6 +75,24 @@ static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes, ecc_swap_digits(tmp, out, ndigits); } +/** + * ecc_digits_to_bytes() - Copy digits into a byte array of size nbytes + * @in: Input digits array + * @ndigits: Number of digits in input digits array + * @out: Output byte array + * @nbytes: Number of bytes to copy into byte array + */ +static inline void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits, + u8 *out, unsigned int nbytes) +{ + unsigned int sz = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; + u8 tmp[ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT]; + unsigned int o = sz - nbytes; + + ecc_swap_digits(in, (u64 *)tmp, ndigits); + memcpy(out, &tmp[o], nbytes); +} + /** * ecc_is_key_valid() - Validate a given ECDH private key * From patchwork Thu Feb 15 23:14:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201838 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp181909dyb; Thu, 15 Feb 2024 15:22:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUdTaJzRjZmz1+lthtHoFCW1vKzz+AGEe51zTXyH0Zd0mhVmY+V1pEMO92mO2lAb6vWXvBQZkVUgWDq7oVSJYz//qgr7w== X-Google-Smtp-Source: AGHT+IEnl+azrrbNDu+9KKu44XH31djRnwycJUTqAb0GjX2W+UudssTEmykfLZMtcftztlRP8KaI X-Received: by 2002:a17:90a:a008:b0:297:24b8:6dd4 with SMTP id q8-20020a17090aa00800b0029724b86dd4mr3098055pjp.21.1708039323383; Thu, 15 Feb 2024 15:22:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039323; cv=pass; d=google.com; s=arc-20160816; b=ClezrBfOXGhY2cgWwJBdIjf/O/tb62K3397CWD24e7UOM8fn5uy6zXgUq2VIfXUt+V 9VT6Y8jPl8nSvCZWjPnrjXUqCUtvrxMS7IKUY0oJKpV+b7KMW8o4GzujYQ3eEc1elgr5 rMQBuLBym9+/RrrvInyEfGElGLaRKtzbYmZbrMrODwJPGezBNBQ3Y1Hw3fmOdc/jhEmT hNe8HuwWwaxG28EUdkgG0Hh2YtQwlnFNcVkMd6kTpgsV+JJhl0q1XDPQR0EDbVy9wedK hTBDPvir2unDP347w4sGRbKBVnGZp6HNwlcBvYmMKrsBHRCFTL9IkKcjwbGSdFA/5mZN jG9Q== 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=MinIzDKfCKQy6B0slQpidM9BwLl++KcE0MImdVgZ1ZI=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=exLDvKjkUSYLJza86XzYfpHSQnbuggBdiknjXZ6PwRcRWFEbIzAwGUQ+9nvOAqBiyj pqvQ9L3xui/lMGeAA8nYGK1qRuNE16wKYXXE6wSQxzxBND1iOtsprGUeO23zB49b0C+g 3onCxYfAZ93VMLXOBH9Rr7r2k5pmZUTcfPCy6OBJQKhWUGozA/vFRNApz+aKNzH3aw/K pPCN0m9Z/OrWdVy41130V5+zblBWdSgzlxTCITCls1U0Gn/+sdmeFcwftMnCvLvc2/6g aMUKuS0vTHbYWrFiDQenjfrU1IStjc1a+dSh4g/DjJpbRsPXXVQOLF8xvys/jZUon8ld 8qVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=GCabvGSt; 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-67810-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67810-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. [147.75.48.161]) by mx.google.com with ESMTPS id ot15-20020a17090b3b4f00b0029934baabd0si52034pjb.68.2024.02.15.15.22.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:22:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67810-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=GCabvGSt; 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-67810-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67810-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 2924FB211E3 for ; Thu, 15 Feb 2024 23:17:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B83E514A4C7; Thu, 15 Feb 2024 23:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="GCabvGSt" 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 D1A87145340; Thu, 15 Feb 2024 23:14:38 +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=1708038880; cv=none; b=kGK1r+GVAyYd4Dz/dju6K8s7Wpgb9OI1VlT/WPv4xxYhaZalYDfdhM4RpADglvlwGD4acI5V7lfcHi9eoIktgTqu5y6sn4Bo+4SXAGMjI/NFiJL56EK2hVLsuX4NG0eemeilPYVlqoYCdVSOUvUt4pIxWjy+HJPH/NRig0RRIK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038880; c=relaxed/simple; bh=4gmnt0N+ElRQcbrv32rTMnC0LI2f7oMrwo91K3z9nhY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qL9SIb8ALREVdkfcq3GOTHqXgnBJxA0A699Q1quzsO+8Zt2CQVSTdr/ezVh9FkYy4TaHPjXSd6RRXhnNWxLQSeTeOcN8rT4IyFjk8rGJr+2l20tWYmNIPrnTIQzkaK1ps0GX/6BcRTmR5cRiRhwN7upBazKb2U42xw1lDZQ4D8c= 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=GCabvGSt; 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 (m0353727.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMQV7h004908; Thu, 15 Feb 2024 23:14:34 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=MinIzDKfCKQy6B0slQpidM9BwLl++KcE0MImdVgZ1ZI=; b=GCabvGStxnUyUHLASCWPT6P/DZIgj/VmgpLR0keqS0hw34z561dq4Jtre994N66OGewv V2IU7n10rA9XpcfS+4Ehq/3R1JEzj1N/iTrASkkOTUrzgm9IULkEY6iMayNHXMTVPAlI wqObly0L8ByU7Yd/g3I+o+MONb/OY8KVRSkZ3N616N0UGKCdVsOC/IXh8xKiksafWK8q Br1szEzwjFPR8iAV6a54xZuAH2wBmEptjz8FbZMrmT5qvtC1OtZb6X/4k/TqKrFZrDIp AmOfCT6VZYvr8L5Nmo1AFCKqXeO71EhwDErGVO0NZs9IQaJo8g738Y1szs1PIa0wrvlI 4g== Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9u52a565-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:33 +0000 Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLuPM4004258; Thu, 15 Feb 2024 23:14:32 GMT Received: from smtprelay06.dal12v.mail.ibm.com ([172.16.1.8]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6kv0r3pn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:32 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay06.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNETLn6357966 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:31 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7FBB158061; Thu, 15 Feb 2024 23:14:29 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CF3E158050; Thu, 15 Feb 2024 23:14:28 +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:28 +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 11/14] crypto: Add nbits field to ecc_curve structure Date: Thu, 15 Feb 2024 18:14:10 -0500 Message-ID: <20240215231414.3857320-12-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: CsLwuN1bCkc0LPLu-6Og_En6AG20EVR2 X-Proofpoint-GUID: CsLwuN1bCkc0LPLu-6Og_En6AG20EVR2 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 suspectscore=0 bulkscore=0 mlxlogscore=945 impostorscore=0 mlxscore=0 clxscore=1015 malwarescore=0 priorityscore=1501 lowpriorityscore=0 phishscore=0 adultscore=0 spamscore=0 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: 1791009041799755963 X-GMAIL-MSGID: 1791009041799755963 Add the number of bits a curve has to the ecc_curve definition. This field only needs to be set for curves that don't fill up all bytes in their digits, such as NIST P521 which has only 9 bits in the most significant digit. This field will be used to determine the number of bytes a curve requires for its key coordinates for example. Signed-off-by: Stefan Berger --- crypto/ecc_curve_defs.h | 1 + include/crypto/ecc_curve.h | 3 +++ 2 files changed, 4 insertions(+) diff --git a/crypto/ecc_curve_defs.h b/crypto/ecc_curve_defs.h index 93a47a5d460a..09a221657c31 100644 --- a/crypto/ecc_curve_defs.h +++ b/crypto/ecc_curve_defs.h @@ -119,6 +119,7 @@ static u64 nist_p521_b[] = { 0xef451fd46b503f00ull, 0x3573df883d2c34f1ull, 0x051ull }; static struct ecc_curve nist_p521 = { .name = "nist_521", + .nbits = 521, .g = { .x = nist_p521_g_x, .y = nist_p521_g_y, diff --git a/include/crypto/ecc_curve.h b/include/crypto/ecc_curve.h index 70964781eb68..337a44956926 100644 --- a/include/crypto/ecc_curve.h +++ b/include/crypto/ecc_curve.h @@ -23,6 +23,8 @@ struct ecc_point { * struct ecc_curve - definition of elliptic curve * * @name: Short name of the curve. + * @nbits: Curves that do not use all bits in their ndigits must specify + * their number of bits here, otherwise can leave at 0. * @g: Generator point of the curve. * @p: Prime number, if Barrett's reduction is used for this curve * pre-calculated value 'mu' is appended to the @p after ndigits. @@ -34,6 +36,7 @@ struct ecc_point { */ struct ecc_curve { char *name; + unsigned int nbits; struct ecc_point g; u64 *p; u64 *n; From patchwork Thu Feb 15 23:14:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201827 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp180204dyb; Thu, 15 Feb 2024 15:18:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXBsgXKPprtiiJYUmxmvfA8k1rXvegs+3aHCpgcfggbMIeQGkX7B1fVYiwxHLB38msLG3JZmTg3ogrMd38LQco9N3GgPA== X-Google-Smtp-Source: AGHT+IEszxyeDswBxZdqCDdpAep+EJ7RVut2aFLBNBecMLLHG+ErIus6T4B/Wz5S1V2f9t+wzhAM X-Received: by 2002:a05:620a:178c:b0:787:2848:8e88 with SMTP id ay12-20020a05620a178c00b0078728488e88mr3886710qkb.7.1708039087126; Thu, 15 Feb 2024 15:18:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039087; cv=pass; d=google.com; s=arc-20160816; b=qY2UEyqBziAsFl/zb2NCgVTRo5GyE3cKSA/Qhf/TYqefUtTYuhMYJbcITCpZmszeu+ w3JTOGbHFID1lXDIYUMkxgDhEjvZ0ScwQopeX4/A1Rltcx8ntkypzz9sBxgmRZhxYkNa rk0qgtB3WfFFH2V+4b8g5iFqvPpmO/2F+Hx9PoWebaBxiO8FM/D6znaK1P4aHvcSHMR2 p17MwMZSrnLiWdXfQrTbkfLLJspkwNEXxnHyF3AWt89u6LN0ugtrZ2yYnOU74e+raWs1 BUwR3Tqoxfgpz5urUhYk1BFcCw96G3H0mQW6YtFTviwL8ScnT+IP0INtDYaWzd8By+vT ILIA== 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=mYgdjqbNen7QY26guvqSRH2EI5oLC0HipXuyG4L6aUg=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=hLjU+qC7gKmvGAhO59LMhZwciEb0ruLPDmXpvkFIg247IOsVeL1uNW+wNN5YnQPMM6 o8ETfEqwlbf8uf+yCyjJ0csyeA/1WeBPY9/kR85jqebOA1yzrinY458c9FDBRzhRF4HH uxpLa1sKK8DWHYCYEEE+PE0dEqPAgIT5CZJdzKvAqwZ8XZBvvv73qrjdBbTPuGSMv7kj f3UeKVsWmACPNRMiydNEcwK4WWOqEu6y6BptLNDTGDvhIRmdmZLfTcMpRz0Ih39OHITS rWJHnuEp6ZhZ2FZpGVi2rQlcMmlHBSCPExL8FXBnU9hGufIgSSh8EKruKoRI1NB9YWFF OnoA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="l0t/KbJD"; 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-67811-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67811-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 s27-20020a05620a16bb00b00785d1447e6bsi2607051qkj.757.2024.02.15.15.18.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:18:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67811-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="l0t/KbJD"; 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-67811-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67811-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 E5B0E1C210E3 for ; Thu, 15 Feb 2024 23:18:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3625145340; Thu, 15 Feb 2024 23:14:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="l0t/KbJD" 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 D550614831F; Thu, 15 Feb 2024 23:14:39 +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=1708038881; cv=none; b=uKzcDdsENHXt9idApPsapKGjT8PbYINQq/k2nzhNiz40qGB7fs4ubVEzkdLNRqCHldWJvkBTN/bA6l45sWS5kuPuOvoMTVNY1P0BmrFzRsGQ+nG6SJ5IRSoFBB5/YgKAYcWsozK+7iWLTVd1LX9scTsgSj/k4GLpsX7TbYtuaxc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038881; c=relaxed/simple; bh=Z3OEL+EPx//2PPX5akfH6NjY88NSSeAdRzhgVXlU2FY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YPGzabfRwSNaSIcmICw6BPR5i25qFjUtETH8YYF4MJIcvJAZbLnKDd9WOiJqbySKSRCQ4JF6EM78siZlHLAN7lxjgGTaQ1oX64gnoFP+jzpjg3efLlXn7F6mtoGgO6AqXy0ym2Au4xClcS0ZQ6+6VPokIrWz4Z7zA4B3xSx/tt0= 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=l0t/KbJD; 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 41FMVlZk029466; Thu, 15 Feb 2024 23:14:34 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=mYgdjqbNen7QY26guvqSRH2EI5oLC0HipXuyG4L6aUg=; b=l0t/KbJDbNWRLKk455fieLXtavaZR51pzGJRXubExor826DNo8xjmcTpEG/o4362YCAB ys4/Ynhvj0W2bDLadGsClABAKZGkGH0jsU0yNvU8t/nFZAqBhW7A6KhHoxpWrfrOgoY1 xJgvxgMDoKiRRamJz+w1DTVf6Dw/3zKUwzukZsnO9wePwC7aPkpJRwZOFjH+pRsxm8zh mfVBH9TH/tWcr4eO4f9X1o3Mz/UspEzHDk3XhoNbyg6GDuJutG9fI9JbKd528g29M2fi V0jn5AZAW70Ah0aaqYIEdEGlM8ewC7q2ugUBPRL1cSPNZQwjwAQzzggId6eIFc4wVZHs Kw== 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 3w9ug4166k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:34 +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 41FKZ6ce009680; Thu, 15 Feb 2024 23:14:33 GMT Received: from smtprelay07.dal12v.mail.ibm.com ([172.16.1.9]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6npm7h4x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:33 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay07.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEUPL52626034 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:32 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5B7F458066; Thu, 15 Feb 2024 23:14:30 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A3B8458050; Thu, 15 Feb 2024 23:14:29 +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:29 +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 12/14] crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes Date: Thu, 15 Feb 2024 18:14:11 -0500 Message-ID: <20240215231414.3857320-13-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-GUID: 7mzzmiueXgSosQUZrfe6zxpT-pAbo5TH X-Proofpoint-ORIG-GUID: 7mzzmiueXgSosQUZrfe6zxpT-pAbo5TH 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 suspectscore=0 phishscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 priorityscore=1501 impostorscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 clxscore=1015 spamscore=0 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: 1791008794182138462 X-GMAIL-MSGID: 1791008794182138462 Implement ecc_curve_get_nbytes to get a curve's number of bytes (nbytes). The number of bytes can be derived from the nbits field of a curve, if set, otherwise from the ndigits field. Signed-off-by: Stefan Berger --- crypto/ecc.c | 6 ++---- include/crypto/internal/ecc.h | 11 +++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index 73fbbfc8d69c..f643719450b8 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -1478,10 +1478,8 @@ static int __ecc_is_key_valid(const struct ecc_curve *curve, int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, const u64 *private_key, unsigned int private_key_len) { - int nbytes; const struct ecc_curve *curve = ecc_get_curve(curve_id); - - nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; + int nbytes = ecc_curve_get_nbytes(curve); if (private_key_len != nbytes) return -EINVAL; @@ -1506,7 +1504,7 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey) { const struct ecc_curve *curve = ecc_get_curve(curve_id); u64 priv[ECC_MAX_DIGITS]; - unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; + unsigned int nbytes = ecc_curve_get_nbytes(curve); unsigned int nbits = vli_num_bits(curve->n, ndigits); int err; diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h index 6229aa3f3218..5d485d3221d3 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -93,6 +93,17 @@ static inline void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits, memcpy(out, &tmp[o], nbytes); } +/** + * ecc_curve_get_nbytes() - Get the number of bytes the curve requires + * @curve: The curve + */ +static inline unsigned int ecc_curve_get_nbytes(const struct ecc_curve *curve) +{ + if (curve->nbits) + return DIV_ROUND_UP(curve->nbits, 8); + return curve->g.ndigits << ECC_DIGITS_TO_BYTES_SHIFT; +} + /** * ecc_is_key_valid() - Validate a given ECDH private key * From patchwork Thu Feb 15 23:14:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201832 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp180737dyb; Thu, 15 Feb 2024 15:19:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX1dRqzQK1HPARRFjazoT2nO0/CVqwca9s/8oaaFFFOMGKYMSSjnp6ckK3MryOKmwplU++vTgzkMIG2g5X4DEfb6UM01Q== X-Google-Smtp-Source: AGHT+IG3bu+vegMzxlEB7sjoVQJm0sKfZv9tEKRa7pfbSgThECWSCWlIORWzO3oulJmfOP5WI6D/ X-Received: by 2002:a05:6871:318e:b0:21a:9f35:e13c with SMTP id lv14-20020a056871318e00b0021a9f35e13cmr2554741oac.18.1708039164879; Thu, 15 Feb 2024 15:19:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039164; cv=pass; d=google.com; s=arc-20160816; b=kR5MlU08T0PAwT9Ft8xebJL+8X6mn/2iR1KsXgA5NktlGw5LpKG5Ll0ks7W753RSVk Rsu8G90p0/hjw9iPCENGl9BUetCp3ugfO4M0Z7sWJLzaqP1Z02PfYk4Rq2aeoxaRJ6L+ evtZuWcDvfzMGl6kIXJpqStYa3fdvxVFCXzCJhY0gKSrn9vk4OZKNoq7Kibk0D4DAvFp xA5M6205oKvViznrEUtG2wLh4iEJ5R4pTBjX7yPjNN48W/kLw9AwtXK/LGV6kbTw1w2z WE/brbucGc6jH/Il+/NwnIubY7kvpD7IT4E5WrkYGucpnCHb/J7PIyqYar9NLxMk2Zne C2CQ== 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=dArPa5/gW/FG93KPR96YEJvHNQv0WSEXUvrhkMfIgv0=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=o8lJmQkSkiPFRQp08d569w+y8UvqMOFRzR4RCmCS9WEoApJnFNm96unU7XSujtxcX/ r0xTs9Nkfnd9mBOK+GTjhyvQCsXdnEgGjdpBkbIVZMJcIHzJCEEKwmSK+QCdW3XQVCFC ytDaTfD7+xeHURtL9VlaUNBGQ82C4qpIzh1iVGuAFxf3v12m1jyQWivoIz8+7/Mg6blE f20FJYjiXGU01voino4YImNRNLWX3DCJ86c9zYaexkdNZ+lV/ChVcLheKeAYuZ0FYdpn rEhvnPkcMamGzSqBfk0xeabzfmtuHDqdsor1CTkvFVxtSNzF2QFZF05m4C3UgYfoV64j e6Pg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="mt4LEgG/"; 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-67817-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67817-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id x4-20020a656aa4000000b005d4af343858si1804457pgu.361.2024.02.15.15.19.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:19:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67817-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="mt4LEgG/"; 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-67817-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67817-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 986B82827EC for ; Thu, 15 Feb 2024 23:19:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B55C414E2C9; Thu, 15 Feb 2024 23:14:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="mt4LEgG/" 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 D7AE3145347; Thu, 15 Feb 2024 23:14:40 +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=1708038884; cv=none; b=UizmpQtsnlkKhks1CEUVHc8FFIA5rliQp9p9MkSuywzy/haKgCQtRvRZtVi6e8JEtd/Qw7MpTOCtcV6UIipn89LN+6PlgXww+J4kdvD0cH7UfkzBKLsQ2B+eQLmUKQqOpIhW7u1PHiSKMHVes/X05gzov5x90yNmBQNDJ6q2ZxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038884; c=relaxed/simple; bh=MJtbtakGRx/ECuD1utzjsbGjrmdKesauv5SvppvQjL8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DdPKr2KNF3cI1/ml1Hvhnvh/RB6M/+mFUzfcQ35X9e/RtWV61Z3iLxG+Y4rSKOqtMGCQwRW1MN++8fkBq8HxvklZJQ6jCnBjZbStwD/3SpJaKFcw3Z7nvD02iF7foozhyslaqrzPLKsnwjHg3rTjHAkQ5cEModT6UdOmfomo9RE= 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=mt4LEgG/; 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 (m0353723.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLFagF007912; Thu, 15 Feb 2024 23:14:35 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=dArPa5/gW/FG93KPR96YEJvHNQv0WSEXUvrhkMfIgv0=; b=mt4LEgG/oLQRGdK6SH3OucwrZIcJ+M+E4uveRjSsGTptgzgc8qfOhYH6OAzGZhPHlfj4 G8V1vneyEUn9gl0rqRCrQgSEQ3bdeHuhwLHr6JsjuJaPNMhsFH0VDN4W3jNFJQ/upuIf ILYbwQB8C01ODxObwuDA3lmvoQZhA0/hhZKtwO/R5pVsEkeDIwXnSSjBFp8kCcFxRhTI 8OKNYHuZ/JRkZMRa/qPWPYJ6Fguv5i1cdPM4BbLrNUi/8cQy/oD2IPyNmLJ2s32uEpOR ifhC/4KNx75t8gmaLaIyXZpHmbHCd0sqMUVaUmxZGr6Gd0wh708irUkfc/ndAXmiFQyJ BA== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9q8n7j5y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:34 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLA9VI009886; Thu, 15 Feb 2024 23:14:34 GMT Received: from smtprelay01.wdc07v.mail.ibm.com ([172.16.1.68]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3w6p637dvc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:33 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay01.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEVws58065340 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:33 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3008758052; Thu, 15 Feb 2024 23:14:31 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7FC3358050; Thu, 15 Feb 2024 23:14:30 +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:30 +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 13/14] crypto: ecdh - Use functions to copy digits from and to byte array Date: Thu, 15 Feb 2024 18:14:12 -0500 Message-ID: <20240215231414.3857320-14-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-GUID: D_J4uA7UYiN8AlODo15ZK1sxJL__kLi0 X-Proofpoint-ORIG-GUID: D_J4uA7UYiN8AlODo15ZK1sxJL__kLi0 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 clxscore=1015 mlxlogscore=999 impostorscore=0 adultscore=0 suspectscore=0 bulkscore=0 spamscore=0 lowpriorityscore=0 malwarescore=0 priorityscore=1501 mlxscore=0 phishscore=0 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: 1791008875015419542 X-GMAIL-MSGID: 1791008875015419542 All curves supported so far provide full 64bit digit arrays to convert coordinates from and to. For NIST P521 only 8 digits and 2 bytes will be given per coordinate so that conversion from 9 digits does not work since some bytes are missing. Therefore, regard the input (output) arrays as byte arrays that need to be converted to digits (from digits). Use ecc_digits_from_bytes to convert a byte array to digits and ecc_digits_to_bytes to convert digits to a byte array. crypt_ecdh_shared_secret creates 'rand_z' from 'nbytes' of random bytes. Also convert this array to fill a byte array with 'nbytes' of random bytes and create the rand_z digits from the byte array. The most significant digit of rand_z needs to be adjusted to mask out unnecessary bits beyond the 521 bits of NIST P521. Therefore, apply the appropriate mask to the most significant digit keeping only the 9 most significant bits. Signed-off-by: Stefan Berger --- crypto/ecc.c | 27 +++++++++++++++------------ crypto/ecdh.c | 26 +++++++++++++++----------- include/crypto/internal/ecc.h | 10 +++++++--- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index f643719450b8..ced77eb6e533 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -1542,7 +1542,8 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey) EXPORT_SYMBOL(ecc_gen_privkey); int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits, - const u64 *private_key, u64 *public_key) + const u64 *private_key, u8 *public_key, + unsigned int nbytes) { int ret = 0; struct ecc_point *pk; @@ -1570,8 +1571,8 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits, goto err_free_point; } - ecc_swap_digits(pk->x, public_key, ndigits); - ecc_swap_digits(pk->y, &public_key[ndigits], ndigits); + ecc_digits_to_bytes(pk->x, ndigits, public_key, nbytes); + ecc_digits_to_bytes(pk->y, ndigits, &public_key[nbytes], nbytes); err_free_point: ecc_free_point(pk); @@ -1641,14 +1642,14 @@ int ecc_is_pubkey_valid_full(const struct ecc_curve *curve, EXPORT_SYMBOL(ecc_is_pubkey_valid_full); int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, - const u64 *private_key, const u64 *public_key, - u64 *secret) + const u64 *private_key, const u8 *public_key, + unsigned int nbytes, u8 *secret, u64 msd_mask) { int ret = 0; struct ecc_point *product, *pk; u64 priv[ECC_MAX_DIGITS]; u64 rand_z[ECC_MAX_DIGITS]; - unsigned int nbytes; + u8 tmp[ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT]; const struct ecc_curve *curve = ecc_get_curve(curve_id); if (!private_key || !public_key || !curve || @@ -1657,9 +1658,10 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto out; } - nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; - - get_random_bytes(rand_z, nbytes); + get_random_bytes(tmp, nbytes); + ecc_digits_from_bytes(tmp, nbytes, rand_z, ndigits); + if (msd_mask) + rand_z[ndigits - 1] &= msd_mask; pk = ecc_alloc_point(ndigits); if (!pk) { @@ -1667,8 +1669,9 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto out; } - ecc_swap_digits(public_key, pk->x, ndigits); - ecc_swap_digits(&public_key[ndigits], pk->y, ndigits); + ecc_digits_from_bytes(public_key, nbytes, pk->x, ndigits); + ecc_digits_from_bytes(&public_key[nbytes], nbytes, pk->y, ndigits); + ret = ecc_is_pubkey_valid_partial(curve, pk); if (ret) goto err_alloc_product; @@ -1688,7 +1691,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto err_validity; } - ecc_swap_digits(product->x, secret, ndigits); + ecc_digits_to_bytes(product->x, ndigits, secret, nbytes); err_validity: memzero_explicit(priv, sizeof(priv)); diff --git a/crypto/ecdh.c b/crypto/ecdh.c index f187365db7b6..e64133428552 100644 --- a/crypto/ecdh.c +++ b/crypto/ecdh.c @@ -15,6 +15,8 @@ struct ecdh_ctx { unsigned int curve_id; unsigned int ndigits; + unsigned int nbytes; + u64 msd_mask; u64 private_key[ECC_MAX_DIGITS]; }; @@ -28,7 +30,6 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, { struct ecdh_ctx *ctx = ecdh_get_ctx(tfm); u64 priv[ECC_MAX_DIGITS]; - unsigned int nbytes; struct ecdh params; if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0 || @@ -39,10 +40,8 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, return ecc_gen_privkey(ctx->curve_id, ctx->ndigits, ctx->private_key); - nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT; - - memcpy(ctx->private_key, params.key, nbytes); - ecc_swap_digits(ctx->private_key, priv, ctx->ndigits); + ecc_digits_from_bytes(params.key, ctx->nbytes, priv, ctx->ndigits); + ecc_swap_digits(priv, ctx->private_key, ctx->ndigits); if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits, priv, params.key_size) < 0) { @@ -56,13 +55,13 @@ static int ecdh_compute_value(struct kpp_request *req) { struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); struct ecdh_ctx *ctx = ecdh_get_ctx(tfm); - u64 *public_key; - u64 *shared_secret = NULL; + unsigned int nbytes = ctx->nbytes; + u8 *public_key; + u8 *shared_secret = NULL; void *buf; - size_t copied, nbytes, public_key_sz; + size_t copied, public_key_sz; int ret = -ENOMEM; - nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT; /* Public part is a point thus it has both coordinates */ public_key_sz = 2 * nbytes; @@ -91,12 +90,14 @@ static int ecdh_compute_value(struct kpp_request *req) ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits, ctx->private_key, public_key, - shared_secret); + nbytes, shared_secret, + ctx->msd_mask); buf = shared_secret; } else { ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits, - ctx->private_key, public_key); + ctx->private_key, public_key, + nbytes); buf = public_key; nbytes = public_key_sz; } @@ -134,6 +135,7 @@ static int ecdh_nist_p192_init_tfm(struct crypto_kpp *tfm) ctx->curve_id = ECC_CURVE_NIST_P192; ctx->ndigits = ECC_CURVE_NIST_P192_DIGITS; + ctx->nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT; return 0; } @@ -159,6 +161,7 @@ static int ecdh_nist_p256_init_tfm(struct crypto_kpp *tfm) ctx->curve_id = ECC_CURVE_NIST_P256; ctx->ndigits = ECC_CURVE_NIST_P256_DIGITS; + ctx->nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT; return 0; } @@ -184,6 +187,7 @@ static int ecdh_nist_p384_init_tfm(struct crypto_kpp *tfm) ctx->curve_id = ECC_CURVE_NIST_P384; ctx->ndigits = ECC_CURVE_NIST_P384_DIGITS; + ctx->nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT; return 0; } diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h index 5d485d3221d3..328a82bd35c8 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -138,12 +138,14 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey); * @ndigits: curve's number of digits * @private_key: pregenerated private key for the given curve * @public_key: buffer for storing the generated public key + * @nbytes: number of bytes per coordinate of public key * * Returns 0 if the public key was generated successfully, a negative value * if an error occurred. */ int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits, - const u64 *private_key, u64 *public_key); + const u64 *private_key, u8 *public_key, + unsigned int nbytes); /** * crypto_ecdh_shared_secret() - Compute a shared secret @@ -152,7 +154,9 @@ int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits, * @ndigits: curve's number of digits * @private_key: private key of part A * @public_key: public key of counterpart B + * @nbytes: number of bytes per coordinate of public key * @secret: buffer for storing the calculated shared secret + * @msd_mask: optional mask to apply to the most significant digit * * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret * before using it for symmetric encryption or HMAC. @@ -161,8 +165,8 @@ int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits, * if an error occurred. */ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, - const u64 *private_key, const u64 *public_key, - u64 *secret); + const u64 *private_key, const u8 *public_key, + unsigned int nbytes, u8 *secret, u64 msd_mask); /** * ecc_is_pubkey_valid_partial() - Partial public key validation From patchwork Thu Feb 15 23:14:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Berger X-Patchwork-Id: 201829 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp180455dyb; Thu, 15 Feb 2024 15:18:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWv1odTLMYXLP/ZP2bX94I8qQEJDoUot3oeds4HZa0PcYZLglw2Q5v3xCTaWJPq4BVjggnXrFB9QoQuQGTq7z8zsd02Ng== X-Google-Smtp-Source: AGHT+IHxkYJir5DyDGb9eHIBzAnZIUJQDXuI1nwtSrHT+2wgwp4cBRSPxVGbkiCP6fu7geIoUK7l X-Received: by 2002:a17:906:d7bb:b0:a3d:704:d688 with SMTP id pk27-20020a170906d7bb00b00a3d0704d688mr2293873ejb.47.1708039124947; Thu, 15 Feb 2024 15:18:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039124; cv=pass; d=google.com; s=arc-20160816; b=MouO5mhUxRE8OYs1BinhEAabH/LJEMr+ykXBNfz7YKHh6EVK327pVTX3ErJQsNmi/X jN4dHC02nca2MRUd1vviv1tyuyJpuinKVL2+CtOr0vldTHk9ewDhErzTBq0pXiRAcKo7 RnoaXJTFwLIkadEFoRFdiP51nG8vmv2LLTkhGclHoPikxv1mj+MzXzaCKeGbnGeD0StE l09XSYC9ioXcTIkYzlaL4fr/N4JhsDFAJxHayPlnshTRtYa1yQNuCKQ/3/gsDLsRLJmP aeF5YfbnRuWoLI4HNHNsABQFN0H8Sb0QCbD205nhxooZd5U0i6ucb8QOPeSWsC8DN03k O7iQ== 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=TgFtvWyefJsqJg/5/Y/sVXi8wg7VifvdDt4fnDvR9gU=; fh=nKmjVxPwCa773nyq5JxrCa37ZALdOpLJMBES7dZaGuw=; b=RNpXCw8SN8INz4rbMRa0aTNRcWAxRjSifHCGhPrsdrobtkJtTsEAxvGEQrzFLvjwQU BTL0qRLTl7vaQfTPGHd9PAbj28bYPB1sgCXsl21nzT12rprYhbCNAe5YvGJnK8WLz3Xn 3lp4qsrmTze4qmtC8tWze0rv5qYMqeO+6PrqnDKOt8Yi+bcZtytZjoA7M5WnHWVqcyLE WtPdqatbARuUEyyZOa0aHFNOb2Xjx+NCQw9ART43JFZwzDjAmr2GnxMKkGKfpDLc9COe PqgNwHYaUJY/8tbOth+rbfownEYERDomyp9hzLQTAY0ooFVRp8F52IMP9OPlBOQ9nYMi zIIA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=mZaJsbaS; 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-67813-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67813-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id m23-20020a17090607d700b00a3dd255ac9fsi102879ejc.952.2024.02.15.15.18.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:18:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67813-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=mZaJsbaS; 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-67813-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67813-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 68C291F21703 for ; Thu, 15 Feb 2024 23:18:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9E58214C5A0; Thu, 15 Feb 2024 23:14:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="mZaJsbaS" 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 CC1CE1487EC; Thu, 15 Feb 2024 23:14:40 +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=1708038882; cv=none; b=dg253uG5McEZPGPJlXaO+iUDCognse3ck2PED45ueuVSAVSxM9qPvpjw010vyFRp1XOpLabwJc5G3oSUMYEG9b+fS5WzEWPFVYUZDalERxD22kjCPh9cY3f8vVBJFnunY4TcnNM6pYEsccnXuYxUGhvBtRQ6+9TvD+liIHqt9oc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708038882; c=relaxed/simple; bh=mhU0bmPHBeF08SYolZF/y+HVVTOZOcSu5BU7EGiPuBk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sm00lbOGSpPp6azJU9hevJx5eViFBZTqh9L+zpOYPk7jLAMlSzOnB4GPzaB2Gf+xbGrA2hjAUSsd9EJwWJM2g/oEsEJdy6bWto1jRe3ScXdJDj8JnNvJC6Qr/gzAJGrHn9t9SC3CpjZV+FMrmTBU9pRXvbIcef2c1NobaVog7CA= 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=mZaJsbaS; 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 (m0353726.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 41FMW496027152; Thu, 15 Feb 2024 23:14:36 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=TgFtvWyefJsqJg/5/Y/sVXi8wg7VifvdDt4fnDvR9gU=; b=mZaJsbaS9hvzuKtfWE9zWQBVAtc9VmD4X2oQplx4OXRRfa3pQyKabytpypYdaadfAMAp OhV0o5Fex8m+VXkCMMwaydG6nF8ti8iit00aMNA3OW91HB26lk1yRXNh8xQC0gYcUL0z YZq1FZK1UEu9CNJS92lJjq5QBFlVzOQ2KvfUgRM9/JSTnRR6Y6WBKhO56YErQOnYxdM3 u/fLA1dJ6zb4cYAxAmYGDPtYL47XSHOBrKJfKSuIyewB5mxI4+ttquIe6HYRiDjbiZC0 3dwVWpvdf1h2d6F9MtO6Kb/67kbBvZfPccw+5GMWrwIpZFO2pxrkenwBzRrRTYWHS+vZ fg== Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3w9ug4h7j1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:35 +0000 Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41FLuPM5004258; Thu, 15 Feb 2024 23:14:34 GMT Received: from smtprelay02.wdc07v.mail.ibm.com ([172.16.1.69]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3w6kv0r3ps-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Feb 2024 23:14:34 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay02.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 41FNEW2h22086358 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Feb 2024 23:14:34 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0469F58054; Thu, 15 Feb 2024 23:14:32 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5423558050; Thu, 15 Feb 2024 23:14:31 +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:31 +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 14/14] crypto: ecdh - Add support for NIST P521 and add test case Date: Thu, 15 Feb 2024 18:14:13 -0500 Message-ID: <20240215231414.3857320-15-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-GUID: m5dwGDNmozOUT__2uOtN-Myn6G0HnLIU X-Proofpoint-ORIG-GUID: m5dwGDNmozOUT__2uOtN-Myn6G0HnLIU 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 malwarescore=0 suspectscore=0 lowpriorityscore=0 bulkscore=0 mlxscore=0 phishscore=0 mlxlogscore=999 clxscore=1015 priorityscore=1501 impostorscore=0 spamscore=0 adultscore=0 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: 1791008833409809358 X-GMAIL-MSGID: 1791008833409809358 Implement ecdh support with NIST P521 and add a test case from RFC5903. Signed-off-by: Stefan Berger --- crypto/ecdh.c | 34 +++++++++++++++ crypto/testmgr.c | 7 ++++ crypto/testmgr.h | 79 +++++++++++++++++++++++++++++++++++ include/crypto/internal/ecc.h | 1 + 4 files changed, 121 insertions(+) diff --git a/crypto/ecdh.c b/crypto/ecdh.c index e64133428552..be87832e172e 100644 --- a/crypto/ecdh.c +++ b/crypto/ecdh.c @@ -207,6 +207,32 @@ static struct kpp_alg ecdh_nist_p384 = { }, }; +static int ecdh_nist_p521_init_tfm(struct crypto_kpp *tfm) +{ + struct ecdh_ctx *ctx = ecdh_get_ctx(tfm); + + ctx->curve_id = ECC_CURVE_NIST_P521; + ctx->ndigits = ECC_CURVE_NIST_P521_DIGITS; + ctx->nbytes = DIV_ROUND_UP(521, 8); + ctx->msd_mask = 0x1ff; + + return 0; +} +static struct kpp_alg ecdh_nist_p521 = { + .set_secret = ecdh_set_secret, + .generate_public_key = ecdh_compute_value, + .compute_shared_secret = ecdh_compute_value, + .max_size = ecdh_max_size, + .init = ecdh_nist_p521_init_tfm, + .base = { + .cra_name = "ecdh-nist-p521", + .cra_driver_name = "ecdh-nist-p521-generic", + .cra_priority = 100, + .cra_module = THIS_MODULE, + .cra_ctxsize = sizeof(struct ecdh_ctx), + }, +}; + static bool ecdh_nist_p192_registered; static int __init ecdh_init(void) @@ -225,8 +251,15 @@ static int __init ecdh_init(void) if (ret) goto nist_p384_error; + ret = crypto_register_kpp(&ecdh_nist_p521); + if (ret) + goto nist_p521_error; + return 0; +nist_p521_error: + crypto_unregister_kpp(&ecdh_nist_p384); + nist_p384_error: crypto_unregister_kpp(&ecdh_nist_p256); @@ -242,6 +275,7 @@ static void __exit ecdh_exit(void) crypto_unregister_kpp(&ecdh_nist_p192); crypto_unregister_kpp(&ecdh_nist_p256); crypto_unregister_kpp(&ecdh_nist_p384); + crypto_unregister_kpp(&ecdh_nist_p521); } subsys_initcall(ecdh_init); diff --git a/crypto/testmgr.c b/crypto/testmgr.c index 00f5a6cf341a..91f39f5a438b 100644 --- a/crypto/testmgr.c +++ b/crypto/testmgr.c @@ -5077,6 +5077,13 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .kpp = __VECS(ecdh_p384_tv_template) } + }, { + .alg = "ecdh-nist-p521", + .test = alg_test_kpp, + .fips_allowed = 1, + .suite = { + .kpp = __VECS(ecdh_p521_tv_template) + } }, { .alg = "ecdsa-nist-p192", .test = alg_test_akcipher, diff --git a/crypto/testmgr.h b/crypto/testmgr.h index 9bde04be8df9..dc9a2b30b5fd 100644 --- a/crypto/testmgr.h +++ b/crypto/testmgr.h @@ -4468,6 +4468,85 @@ static const struct kpp_testvec ecdh_p384_tv_template[] = { } }; +/* + * NIST P521 test vectors from RFC5903 + */ +static const struct kpp_testvec ecdh_p521_tv_template[] = { + { + .secret = +#ifdef __LITTLE_ENDIAN + "\x02\x00" /* type */ + "\x48\x00" /* len */ + "\x42\x00" /* key_size */ +#else + "\x00\x02" /* type */ + "\x00\x48" /* len */ + "\x00\x42" /* key_size */ +#endif + "\x00\x37\xAD\xE9\x31\x9A\x89\xF4" + "\xDA\xBD\xB3\xEF\x41\x1A\xAC\xCC" + "\xA5\x12\x3C\x61\xAC\xAB\x57\xB5" + "\x39\x3D\xCE\x47\x60\x81\x72\xA0" + "\x95\xAA\x85\xA3\x0F\xE1\xC2\x95" + "\x2C\x67\x71\xD9\x37\xBA\x97\x77" + "\xF5\x95\x7B\x26\x39\xBA\xB0\x72" + "\x46\x2F\x68\xC2\x7A\x57\x38\x2D" + "\x4A\x52", + .b_public = + "\x00\xD0\xB3\x97\x5A\xC4\xB7\x99" + "\xF5\xBE\xA1\x6D\x5E\x13\xE9\xAF" + "\x97\x1D\x5E\x9B\x98\x4C\x9F\x39" + "\x72\x8B\x5E\x57\x39\x73\x5A\x21" + "\x9B\x97\xC3\x56\x43\x6A\xDC\x6E" + "\x95\xBB\x03\x52\xF6\xBE\x64\xA6" + "\xC2\x91\x2D\x4E\xF2\xD0\x43\x3C" + "\xED\x2B\x61\x71\x64\x00\x12\xD9" + "\x46\x0F" + "\x01\x5C\x68\x22\x63\x83\x95\x6E" + "\x3B\xD0\x66\xE7\x97\xB6\x23\xC2" + "\x7C\xE0\xEA\xC2\xF5\x51\xA1\x0C" + "\x2C\x72\x4D\x98\x52\x07\x7B\x87" + "\x22\x0B\x65\x36\xC5\xC4\x08\xA1" + "\xD2\xAE\xBB\x8E\x86\xD6\x78\xAE" + "\x49\xCB\x57\x09\x1F\x47\x32\x29" + "\x65\x79\xAB\x44\xFC\xD1\x7F\x0F" + "\xC5\x6A", + .expected_a_public = + "\x00\x15\x41\x7E\x84\xDB\xF2\x8C" + "\x0A\xD3\xC2\x78\x71\x33\x49\xDC" + "\x7D\xF1\x53\xC8\x97\xA1\x89\x1B" + "\xD9\x8B\xAB\x43\x57\xC9\xEC\xBE" + "\xE1\xE3\xBF\x42\xE0\x0B\x8E\x38" + "\x0A\xEA\xE5\x7C\x2D\x10\x75\x64" + "\x94\x18\x85\x94\x2A\xF5\xA7\xF4" + "\x60\x17\x23\xC4\x19\x5D\x17\x6C" + "\xED\x3E" + "\x01\x7C\xAE\x20\xB6\x64\x1D\x2E" + "\xEB\x69\x57\x86\xD8\xC9\x46\x14" + "\x62\x39\xD0\x99\xE1\x8E\x1D\x5A" + "\x51\x4C\x73\x9D\x7C\xB4\xA1\x0A" + "\xD8\xA7\x88\x01\x5A\xC4\x05\xD7" + "\x79\x9D\xC7\x5E\x7B\x7D\x5B\x6C" + "\xF2\x26\x1A\x6A\x7F\x15\x07\x43" + "\x8B\xF0\x1B\xEB\x6C\xA3\x92\x6F" + "\x95\x82", + .expected_ss = + "\x01\x14\x4C\x7D\x79\xAE\x69\x56" + "\xBC\x8E\xDB\x8E\x7C\x78\x7C\x45" + "\x21\xCB\x08\x6F\xA6\x44\x07\xF9" + "\x78\x94\xE5\xE6\xB2\xD7\x9B\x04" + "\xD1\x42\x7E\x73\xCA\x4B\xAA\x24" + "\x0A\x34\x78\x68\x59\x81\x0C\x06" + "\xB3\xC7\x15\xA3\xA8\xCC\x31\x51" + "\xF2\xBE\xE4\x17\x99\x6D\x19\xF3" + "\xDD\xEA", + .secret_size = 72, + .b_public_size = 132, + .expected_a_public_size = 132, + .expected_ss_size = 66 + } +}; + /* * MD4 test vectors from RFC1320 */ diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h index 328a82bd35c8..a04b313f2871 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -33,6 +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_CURVE_NIST_P521_DIGITS 9 #define ECC_MAX_DIGITS (576 / 64) /* due to NIST P521 */ #define ECC_DIGITS_TO_BYTES_SHIFT 3