Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp24524lqg; Thu, 29 Feb 2024 18:21:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVTYO0aBFbZ7lPqVaY/3Rk7cTRdr/2nA+o8koD8RWwl97ZdL3eLer444PbEq5BBvEpDEA56QYD38PDzE6zQ6fxfu70xxi3Ui/vmPreqXg== X-Google-Smtp-Source: AGHT+IEr4G2qk5NcfpsGW6HXsBrDdG/2Sy99+PVpuREhQI1+lESuabku6rdYBEiCOzBNcDDKx+8H X-Received: by 2002:a17:90b:1946:b0:299:7824:6a06 with SMTP id nk6-20020a17090b194600b0029978246a06mr462081pjb.8.1709259699932; Thu, 29 Feb 2024 18:21:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709259699; cv=pass; d=google.com; s=arc-20160816; b=ZMpGgcfrlQqcl2G1lJCz3PkQwmO6iHkhb/8NyBy7fnhoxhcaDLumf5QU/OwWGqdefw yfnSFUuL76KuKWRphTMTSVne1mAX60RBj8qrb4p0bpz3pUdhgGiOkeZpt2WYkQmZmsDA msIC5fESnOupn6m/ti+bM7nJbHLS6YQa8YREJfogui7rD+IOr7rrIfyLjR1OSBrjX9jX qMxts0C8so2ct8ThoGl494uHlKJiQceiziIZ5qxYUuTJwawQQyBURByrBAL9ntE4+YlK 712L6lNGa+HYyXN/fGJrlIAYN8Gx6prjTPab3IL4AssMLM202Arp8/LLVbMOcR2XR6bx 86kA== 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=jyCSD/SD6Do0oEV7VM7M2eWAjErVj5gVDWzEBgNvx6k=; fh=rcM6ok5GFu5IIT2QdH7dFGIfgvD+dlFEBXZav0jM/0I=; b=JGd6Rof84yjUn1g3qiBW32u7BlYDaeUSK+H45vpvJZ3Jfer13thlY4nvlSNEdB5RlS cIsC9HCQVHGjr70QHLZ8q1aI2c1WddIF2yDYC4dp4aiuexYFBY5htEoygNcerEuvgHtH rj+JdagHBru39DmmJ1AeHJHchMi0H4m9HUa6lfeyhR5lnyF+fZms8BlvwNjh2yA2PSNh 3vFAZJ5rmih76xa/nBgvILEeNDoLFryNKXY8CUFOnIMRqheVOWGj/ef2bmF2rNSfmN9g waR+4R3SZcW41dAoeGpGcD4JLwzHtGmQowm3yC7wX23zj7xlOPA2hWKrY2l3Yr4qYAp1 D5Sw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=nKTsHlky; 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-2413-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-2413-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ph14-20020a17090b3bce00b0029a909d33easi2727865pjb.21.2024.02.29.18.21.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 18:21:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-2413-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=nKTsHlky; 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-2413-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-2413-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C26BAB2376A for ; Fri, 1 Mar 2024 02:21:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DABEF3B2BE; Fri, 1 Mar 2024 02:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="nKTsHlky" X-Original-To: linux-crypto@vger.kernel.org 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 88B1E3A8C7; Fri, 1 Mar 2024 02:20:20 +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=1709259623; cv=none; b=FCcWsdYfsDGcTfTMoeXphuWCHCiazj8fCTMpgWyutbVvntkw+TKRqIA6jCxIpQnNCiarqZiSrViwCRTOVrX4dxBHQKYWJ9WQGa58vl3VfvmzMPj5+OElQ/UohsTzpsve7osYIqNTy2zntnkAiZItoecWEf/BkH8aJdqrps3Bm1A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709259623; c=relaxed/simple; bh=sFcvSdKygidwLtw/uzuN+NfCipI2tHKBw5kMEwhOhRU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JOiWfgfbiEznnM76ppL/NdQy5colWO5BLEx5cRtiJ5knMKn7jfZDNCfLSp8mgip8Qqc6aoqQyqiKXvGXo+p5yhcq3tu4KwmloyKIYT+4ZOB+r6ttYGT7XzMDeLfCbzW29BbA9iLAvo9RiEtqLpPPljKxltmvzvmtyVG5PcuaJsc= 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=nKTsHlky; 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 4212HfYU024976; Fri, 1 Mar 2024 02:20:13 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=jyCSD/SD6Do0oEV7VM7M2eWAjErVj5gVDWzEBgNvx6k=; b=nKTsHlkyNIi7LIfNZ7KbnBEcGeo755HE9uUB7pukjjGpx1Q1gYjZ/JWfRaBx7RVlEsCY 9KNFwqW1+gNiTNANSYIHpYi4QNOGuX8+PsqjJS8e3erocqjEB3lwBSh5La38mCR+CjGh Py4dCxKBWqtQG0IRSXY44F1WL/fNjZN3PSA8XViBidNOTFsatAB7dyYK+9XBKlkpuRcY I5E81vpkxYrP2aG4T5yjaKDXe4M6Lwe0vqmm06J99pou7fEoC1ofBdvBNspY0hWBLLJ8 bUjrXoG3I0pi8KoHv4ul3Qz1/e/0ywJioRbwqZZUBPxsF+BAxJkKPxGX+zAz747RbaN3 lA== Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3wk63qg1vm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Mar 2024 02:20:13 +0000 Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 41TNwnoB008827; Fri, 1 Mar 2024 02:20:12 GMT Received: from smtprelay05.dal12v.mail.ibm.com ([172.16.1.7]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 3wftsu1w62-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Mar 2024 02:20:12 +0000 Received: from smtpav05.dal12v.mail.ibm.com (smtpav05.dal12v.mail.ibm.com [10.241.53.104]) by smtprelay05.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4212KAM740042974 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 1 Mar 2024 02:20:12 GMT Received: from smtpav05.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 133115805D; Fri, 1 Mar 2024 02:20:10 +0000 (GMT) Received: from smtpav05.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9192058069; Fri, 1 Mar 2024 02:20:09 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by smtpav05.dal12v.mail.ibm.com (Postfix) with ESMTP; Fri, 1 Mar 2024 02:20:09 +0000 (GMT) From: Stefan Berger To: keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net Cc: linux-kernel@vger.kernel.org, saulo.alessandre@tse.jus.br, lukas@wunner.de, Stefan Berger Subject: [PATCH v4 01/12] crypto: ecdsa - Convert byte arrays with key coordinates to digits Date: Thu, 29 Feb 2024 21:19:56 -0500 Message-ID: <20240301022007.344948-2-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240301022007.344948-1-stefanb@linux.ibm.com> References: <20240301022007.344948-1-stefanb@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-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-ORIG-GUID: Mg0t73Q_vcJOM8BCzjgSpJ3JThHjHUaj X-Proofpoint-GUID: Mg0t73Q_vcJOM8BCzjgSpJ3JThHjHUaj X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-29_08,2024-02-29_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 priorityscore=1501 adultscore=0 impostorscore=0 bulkscore=0 phishscore=0 mlxscore=0 malwarescore=0 clxscore=1015 spamscore=0 suspectscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2403010018 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 need to have leading zeros prepended to the most significant digit since only 2 bytes of the most significant digit are provided. Therefore, 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 | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c index fbd76498aba8..6653dec17327 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 = DIV_ROUND_UP(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..48a04605da7f 100644 --- a/include/crypto/internal/ecc.h +++ b/include/crypto/internal/ecc.h @@ -56,6 +56,31 @@ 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 o = nbytes & 7; + u64 msd = 0; + size_t i; + + if (o == 0) { + ecc_swap_digits(in, out, ndigits); + } else { + /* if key length is not a multiple of 64 bits (NIST P521) */ + for (i = 0; i < o; i++) + msd = (msd << 8) | in[i]; + out[ndigits - 1] = msd; + ecc_swap_digits(&in[o], out, (nbytes - o) >> 3); + } +} + /** * ecc_is_key_valid() - Validate a given ECDH private key * -- 2.43.0