Received: by 2002:a05:6500:1b45:b0:1f5:f2ab:c469 with SMTP id cz5csp352672lqb; Tue, 16 Apr 2024 19:32:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVI70dSkJ5lRNLUvvlm4ZXu8pgOIbdjcsta6VkQp6BaYlSCCEBeAW5jVRlogNTyuVsGfRT5tlRm8tUKcuUiuLh5oudAGpXA9aTyOkFCOA== X-Google-Smtp-Source: AGHT+IFJdf2BWiehsr55x4knz1P1TCf0eqZAaxMWWRFeghW+mqK/DxgJSot0ngIiP3/mChuTmrFn X-Received: by 2002:a17:90a:f015:b0:2aa:8d31:fa48 with SMTP id bt21-20020a17090af01500b002aa8d31fa48mr5750304pjb.18.1713321139830; Tue, 16 Apr 2024 19:32:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713321139; cv=pass; d=google.com; s=arc-20160816; b=bztv9jXIHfDlXO6JRa9lghhcQs45Zdop8jJekbYrvZ/VeizyYmEGpDxIj/11D5KQqF Bu+DK49/nOO8o6l2tTanvUOQO5af+6Ar+U4aUG1+e4v19CUv4Vlp6hqbodTxLvsC5wZj 8ct4x7fD1Ig+c6kYXm3vSecssF9USwnwHkLt5VosAMeNWMOgys4ZIbuwldGzW4rov6yu cvbb23k/ID3fRSC9KPxcsOcJ0o7lT0lnWjiT7lyDxG2G2S+PH0RPoQxG7PHEAGSEpCgm jrlHBArw/BINAgyQWyd75WMDlnB/eTbwCkdhaKp5GN3y9/tnSTZNPG9Ys98kKnIQPGZX OzGA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=7dJE3cf/q7to4rsvlLaRvJk1ImXMlM5MdrO+aUuojGQ=; fh=Vo9EUlxiTxdgyPEe288Hyfmmo5zN6kBUn+3Wt9lTAzI=; b=ZO5/0bKZHnqAu6r5wVv14l4SAZEWHyb6teJtH/NYpSg39BeXKbLzFw2Uzc6/y8zuCo RHC9II+JXbc5ZUBOXLDpozFivDFJwelCJeKAUQEwo0jx2nG1/UcwOj5/hTOlgWCgmjoM xtgbfmt3GBU17XkAkVHhvfzSDZdyRgUv9XRJ7/LY8MCo03VVUT3k0q9HWQGqWMOQJ2NV 2RMBZLHKmnSGY3AtEdHrzipE1ujlkjZCeG0p4UsrSTkjdA0JOLlraa/JqZWRX9uYbPj4 EADLl21lEV0VzSomLekcn3eSJV9NK9UnXo6n53y57EMrLzGSIIxguqnLx8+TEOsZnF2p MO7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b="Em/FVgv2"; 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-3592-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-3592-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l7-20020a17090ac58700b002a2d9b88dc1si440308pjt.163.2024.04.16.19.32.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Apr 2024 19:32:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto+bounces-3592-linux.lists.archive=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="Em/FVgv2"; 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-3592-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-3592-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 58B45281A3A for ; Wed, 17 Apr 2024 02:32:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA72B4685; Wed, 17 Apr 2024 02:32:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="Em/FVgv2" 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 1DF1D63A; Wed, 17 Apr 2024 02:31:58 +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=1713321121; cv=none; b=X388fxoJBZY3brVAypUIiMpac7PT2IIG80xn6e1HwzyyWK/NDC1ok8eCM3zOYcxyjFi2qgs2R9foEFUap7ulkgAZwIo1cb4+LvhbbrJxlTVcG3C3sZpfxZdCADTs1S0VGIIYHoktrY16l7BVaEeWzWOypNMJ1hwwe+snsnCKvv8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713321121; c=relaxed/simple; bh=pJXEiAyGZemue94rYtVKwPx+ofEz0A4fLee0Q/RCSew=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=GNTL39JguDMCL5oz64rTFGA5NPApWJa70Pmof2ViZzlHj34RIX9yd/O8RPl0f2b8WtavcpxQq7qEIM2gVDJJcCr21cgt182f43coNR6N/d7yjPiQkSY3T2UPsbq+0lnZPZB6rYGwnksQ1m31nxhar0giePpqLwwGY68C7zqpUWc= 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=Em/FVgv2; 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 43H2RQ7E026409; Wed, 17 Apr 2024 02:31:49 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : subject : to : cc : references : from : in-reply-to : content-type : content-transfer-encoding; s=pp1; bh=7dJE3cf/q7to4rsvlLaRvJk1ImXMlM5MdrO+aUuojGQ=; b=Em/FVgv2bmUQHB28yHxZaBaaDALPEXffshRrDmjVnbK/wBMdzaQacTg7rn8lmlIl70kj O9bGIdXTOTUAaYcwh1TngnqjvBjSmbU1N3zUBNsf7Rw9JP/p9MRUPONtDjeS5xC5TlRK eLNB/y9J3Zc/k7UvEu5qzK6wHbdDhpMU6+xq0VtTDDzJLq0RJJz1WqcqlhKh/qZrpNSQ vAX2iqXXU1BLcV9VkSkCU3G386+LQjwQAd1RScqNJwbhRPEF8jC+n0ErY8EE7bgrrS2H LBtTLi43B2lykcPJOXzejeYUFIprYO3DsOPIYVRcs/alKlq3zdWdbYuAYWFkCyJH0Yly qQ== 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 3xj5nb0063-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 17 Apr 2024 02:31:49 +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 43GNMLCg021351; Wed, 17 Apr 2024 02:31:48 GMT Received: from smtprelay05.dal12v.mail.ibm.com ([172.16.1.7]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3xg6kkhd8e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 17 Apr 2024 02:31:48 +0000 Received: from smtpav01.wdc07v.mail.ibm.com (smtpav01.wdc07v.mail.ibm.com [10.39.53.228]) by smtprelay05.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 43H2VjIi27656774 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 17 Apr 2024 02:31:48 GMT Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B8E8A5804B; Wed, 17 Apr 2024 02:31:45 +0000 (GMT) Received: from smtpav01.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0E5A95806A; Wed, 17 Apr 2024 02:31:45 +0000 (GMT) Received: from [9.47.158.152] (unknown [9.47.158.152]) by smtpav01.wdc07v.mail.ibm.com (Postfix) with ESMTP; Wed, 17 Apr 2024 02:31:44 +0000 (GMT) Message-ID: <1ec8e98f-6b65-49b4-8429-26d598fc305f@linux.ibm.com> Date: Tue, 16 Apr 2024 22:31:44 -0400 Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] crypto: ecdh - Pass private key in proper byte order to check valid key Content-Language: en-US To: Joachim Vandersmissen Cc: linux-kernel@vger.kernel.org, ardb@kernel.org, salvatore.benedetto@intel.com, davem@davemloft.net, Jarkko Sakkinen , linux-crypto@vger.kernel.org, herbert@gondor.apana.org.au References: <20240415003026.2661270-1-stefanb@linux.ibm.com> <20240415003026.2661270-2-stefanb@linux.ibm.com> <6442f387-e45e-4019-8af0-6ca309e4ce4f@linux.ibm.com> <04f5265f-77e6-45cc-856c-45cd80c278c2@jvdsn.com> From: Stefan Berger In-Reply-To: <04f5265f-77e6-45cc-856c-45cd80c278c2@jvdsn.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: l3lTfBWc92C1SOASiagGej4FLd4rSARx X-Proofpoint-GUID: l3lTfBWc92C1SOASiagGej4FLd4rSARx 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-04-17_01,2024-04-16_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 suspectscore=0 bulkscore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 spamscore=0 mlxlogscore=999 clxscore=1011 mlxscore=0 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404170016 On 4/16/24 22:12, Joachim Vandersmissen wrote: > Hi, > > Apologies for hijacking this thread, but I don't have access to the > older emails. > > Should the new priv variable not be zeroized before the end of the > function? As it contains private keying material? Yes, fixed in v2. Stefan > > Kind regards, > Joachim > > On 4/16/24 9:25 AM, Jarkko Sakkinen wrote: >> On Tue Apr 16, 2024 at 3:51 AM EEST, Stefan Berger wrote: >>> >>> On 4/15/24 14:53, Jarkko Sakkinen wrote: >>>> On Mon Apr 15, 2024 at 3:30 AM EEST, Stefan Berger wrote: >>>>> ecc_is_key_valid expects a key with the most significant digit in >>>>> the last >>>>> entry of the digit array. Currently ecdh_set_secret passes a >>>>> reversed key >>>>> 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. >>>>> >>>>> Throughout the ecdh and ecc codebase the variable 'priv' is used for a >>>>> private_key holding the bytes in proper byte order. Therefore, >>>>> introduce >>>>> priv in ecdh_set_secret and copy the bytes from ctx->private_key into >>>>> priv in proper byte order by using ecc_swap_digits. Pass priv to >>>>> ecc_is_valid_key. >>>>> >>>>> Cc: Ard Biesheuvel >>>>> Cc: Salvatore Benedetto >>>>> Signed-off-by: Stefan Berger >>>>> --- >>>>>    crypto/ecdh.c | 4 +++- >>>>>    1 file changed, 3 insertions(+), 1 deletion(-) >>>>> >>>>> diff --git a/crypto/ecdh.c b/crypto/ecdh.c >>>>> index 3049f147e011..a73853bd44de 100644 >>>>> --- a/crypto/ecdh.c >>>>> +++ b/crypto/ecdh.c >>>>> @@ -27,6 +27,7 @@ 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]; >>>>>        struct ecdh params; >>>>>        if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0 || >>>>> @@ -40,9 +41,10 @@ static int ecdh_set_secret(struct crypto_kpp >>>>> *tfm, const void *buf, >>>>>                           ctx->private_key); >>>>>        memcpy(ctx->private_key, params.key, params.key_size); >>>>> +    ecc_swap_digits(ctx->private_key, priv, ctx->ndigits); >>>> Does swapping speed up the test that follows are what effect does it >>>> have to the ecc_is_key_valid() call? >>> The goal of this particular patch is to fix an issue with the byte order >>> (as description says) and, as you can see in the 2nd patch, private key >>> is always copied into priv using ecc_swap_digits before priv is being >>> used instead of ctx->private_key (or whatever it is called in the >>> function it was passed to). This patch here has nothing to do with speed >>> up but a) fixing an issue and b) using priv here as well, so fixing this >>> 'outlier' here. The speed-up comes in the 2nd patch when the bytes in >>> ctx->private_key are put into proper order right away and we can get rid >>> if priv, taking the swapped bytes of ctx->private_key, everywhere and we >>> can use ctx->private_key directly. >>> >>> The test harness (testmgr.c) runs through part of this code here >>> providing the private key that is copied into ctx->private_key, so it's >>> being used and when you make a mistake (making the changes I did) the >>> ecdh test cases will fail. >> OK, thanks for the explanation :-) No opposition on the change itself. >> >> Acked-by: Jarkko Sakkinen >> >> BR, Jarkko >>