Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp769893rdb; Thu, 15 Feb 2024 15:17:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXetSGFjYvOWgDEQQcRFYoTRAvEFt3TV4BzlyT30KT4o2MoqoXqh6/YKEZjWytWfyQg2IP8fmsGZuXM4YTM/gSDdQapw3i+1ENJBWvEkw== X-Google-Smtp-Source: AGHT+IF2fpg5v0y/fSzo1uC6n0SzFUm3kysh4AiNr3Tzf4mYanyJ49PzW+Bwz3KVv4na3tqxi3nC X-Received: by 2002:a05:6871:580c:b0:21e:afb:364e with SMTP id oj12-20020a056871580c00b0021e0afb364emr3425675oac.36.1708039027561; Thu, 15 Feb 2024 15:17:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708039027; cv=pass; d=google.com; s=arc-20160816; b=wWUf5rfk/OIqc7juZP+YtK+bUUXfv6xmjeezHltjm7WQTXcobWHD21QyNYp8llDGwZ LxBezFr8+U4acyd3CQNk3Qg0AjfPE6W6Wcuyxo/ZqYSNf4gX8peR1KXu1k4CXOlqZjma PzvIahHfPwgg+un9OdIWEftnMBCfC1jE8TeJ1GHMpKXmtHJ6f1VcV9s/3aFU7mDZpVGv d2MpwTO2Lpyj3g9TjgRidYovPM/6GB5u42I+BBJJQvgV1h38vLwX+phqSBcaNqF6Ts1Q +dPMIEJj1PH2Uxt12mywL0cdycH9y5NaTF1rsKuxT3LZuE+/zk2NNvt3JsSIDSkTwUcy zY1g== 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=Is42epJuqDkaMYz2Oihwq/tbxsl1pHT2ufrRdGeSgiSffYtuzLqne8p0ECJt8HoBB9 dDWqa0Sce+ex99H5cy/dxM+0gW3iLl7Q/ytNzhyTOxa+IhP1K/doUzw855F8p0zrAbYK V5mKEvTaUxR9TCiTacMK1+rIYFiflF6jMRLgrv07v6UwSj227JvmiLsmI0LWIxRyNXBP sljf3NCdb3XWsQou5+F7Ug1zNegUOqRE3i2fJqqORw2M9kup564kBLDGheYEW1s+3lDh 8Goqf+RcrGFl9MWSMe1lgYdZF0qm86B0+2S+fM7IBUV8nP/LQn/c49LM2uLC76qOh1XB ox2Q==; 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-crypto+bounces-2101-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-crypto+bounces-2101-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d17-20020a637351000000b005d5434a5732si1866018pgn.456.2024.02.15.15.17.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 15:17:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-2101-linux.lists.archive=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="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-crypto+bounces-2101-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-crypto+bounces-2101-linux.lists.archive=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 3E331281230 for ; Thu, 15 Feb 2024 23:17:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF849145349; 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="l0t/KbJD" X-Original-To: linux-crypto@vger.kernel.org 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-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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 * -- 2.43.0