Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2307505lqb; Mon, 27 May 2024 15:31:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU3TYX0BLkqPXpbwvI2x3pTJHaJTu+NSwq8uHazlscIsUfd5glUakbsl9QBrfqGpZ/1DY51KiXDe5JK8jxv2Ku8RMgi9134FRTWkDzRcw== X-Google-Smtp-Source: AGHT+IEz0uCzTYNkYQ3gPju9LRMZ6ndjSKbPRslLDrYcApZSn8Do9o28jLRn7oBB/JiOfYNGa/hR X-Received: by 2002:a17:906:391a:b0:a59:a83b:d438 with SMTP id a640c23a62f3a-a62641b9ae6mr696702666b.23.1716849100322; Mon, 27 May 2024 15:31:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716849100; cv=pass; d=google.com; s=arc-20160816; b=GXPZGMqg6k0/0DNp/r5nvuONtKCO93qn2cpDlDOzQIPQP0GMg9u4HjEnFlolxO7x0q 8FbhdIByjFWUTVa2E8YsiKIKkQqceRg8qqUZuQHoUzmi13Os8jJHMgcI2+9oOWM+7ipR 4iBqZD8aZXwtQ/DLxZ/5Lhp10uXuhfugiOlnS464SohjssqlxOrVNYAwSYYyzjgRmAf0 nReZ2pYTDXYX0ftLtppcI2mmwIvHfkQhYzVE6qVQzSjoKBqC8OCEQq5vAXbs+/wI+9FY jheXkE2+V3FvtV8iIfFhW7me7z6S3vRP4B9kEk5wQgZET4mwnhfGFrzdMlEwKcycGqJY x1Jg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=7LRyHOaN1X6SHIZL4yzelzvGmX9eRi9nasQfXK/1Vjo=; fh=D7XnYPMNjRyf4PksgQ1kQS8cdYEtBqdv6WgeMQugROE=; b=Qex8al31OWMJk8EFmMzkkzDur4/HStAgmJnBFjXd2oeq5yP3H0XwObesY+aoKJg2NT 3vzAdskoH/ZItdmljO2qGQHQAJlGT3jwOJkEyPfOq/uDRjXiKphHZSNXx6t8hqG9yAxQ vvRGwZqZcKD77/b2bG7y2jnHLmwaDACLF8UhYB47Slc2M7JuEMVVEd8GrIjCieOhOX1k 2y30XKh/xwGpRi3naLc2yrF1kMz4jkbxjZ+c13iHe2g5zgDgm6VFwfX1xG1BM0M2feXs xTxgE+ty/zZ46sZHKqKeyAVuArSr1IWAH3P4NPzQDyKfiRsyYgmCavk+zpbARYZSMsI/ Zz+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CVwWWkqI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-crypto+bounces-4429-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-crypto+bounces-4429-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a626cdc50e0si413989066b.904.2024.05.27.15.31.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 May 2024 15:31:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto+bounces-4429-linux.lists.archive=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=@kernel.org header.s=k20201202 header.b=CVwWWkqI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-crypto+bounces-4429-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-crypto+bounces-4429-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 1619C1F22036 for ; Mon, 27 May 2024 22:31:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A1838167270; Mon, 27 May 2024 22:31:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CVwWWkqI" X-Original-To: linux-crypto@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 5C2B7161338; Mon, 27 May 2024 22:31:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716849097; cv=none; b=dVX6j5wVX1tw+lcb4HjlfadT/gdUKZ/ukQ8c5Y3RyDGj4aKumZiAXOIM3ggxSl33b/6ZHs2fIZXORdWVPuUlRi0UVCviyoYMB07oWksHRza006qRkWiu94SVSS/YMIC9inKO1J7JW89lnFJfQq5a0IV9zfAtP2sgET1eTCb4Lyo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716849097; c=relaxed/simple; bh=qIiHGScAf9xK3Q5Jsuj5LAVARfVcmDIP34BjvR6r7Ac=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=Bo4jpQOKmbmeXPKUKX+i+mzAg6kVVMFq/eLU7IO7QsIdnDBudGKWmIctv3Ez96I5sxTtHUpRJD0YLWzdecVh1f+xiMEw/sElOYmjGn5F6y2ShhApS1X03OSSYATeiPnKeK16tGFYaxJ8bjR/uZ6CkEd/MXvQSIdJvV5NyYGJzGQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CVwWWkqI; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 81C25C2BBFC; Mon, 27 May 2024 22:31:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1716849096; bh=qIiHGScAf9xK3Q5Jsuj5LAVARfVcmDIP34BjvR6r7Ac=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=CVwWWkqIZ1ZUflGbUlGo8Md8OT4PzyY1MQiaPbUn2g5i/OoXhdLVGqP8ArXg9IQ8p 3qagIdTe84AXmCVP0CLwN5rcsxYwpz68as3cgnM0hgGN5E5wfiTkneRafKOSK9flAe iCh8KJlRDCMnMF0yfzUNygbM79yhzIPxa4rDB+NJq6dHibERg4HwYjkWn1pi5av/Zd 6Tc8v6kWbxJPFwmVWPYG56zXWzb7oqzQfIEH8r0CA2yRiXyH3EmrOkz9NBDYDaXXYO l/AbS478DpnPVcvj1BfDajFPSGIlMRUnCw5NXOocPiQFLODYxX4jzve9a02UUKaJH2 pFKLwMK7U20cg== Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 28 May 2024 01:31:33 +0300 Message-Id: Cc: , "David S. Miller" , "Stefan Berger" , Subject: Re: [PATCH] crypto: ecdsa: Fix the public key format description From: "Jarkko Sakkinen" To: "Jarkko Sakkinen" , "Herbert Xu" X-Mailer: aerc 0.17.0 References: <20240527202840.4818-1-jarkko@kernel.org> In-Reply-To: On Tue May 28, 2024 at 1:18 AM EEST, Jarkko Sakkinen wrote: > On Tue May 28, 2024 at 12:58 AM EEST, Jarkko Sakkinen wrote: > > On Tue May 28, 2024 at 12:05 AM EEST, Jarkko Sakkinen wrote: > > > On Mon May 27, 2024 at 11:28 PM EEST, Jarkko Sakkinen wrote: > > > > Public key blob is not just x and y concatenated. It follows RFC548= 0 > > > > section 2.2. Address this by re-documenting the function with the > > > > correct description of the format. > > > > > > > > Link: https://datatracker.ietf.org/doc/html/rfc5480 > > > > Fixes: 4e6602916bc6 ("crypto: ecdsa - Add support for ECDSA signatu= re verification") > > > > Signed-off-by: Jarkko Sakkinen > > > > --- > > > > It is a bug fix that does not really need a stable backport. Still > > > > categorizes as a bug because by following the existing documentatio= n > > > > you end up with an error code. > > > > crypto/ecdsa.c | 5 ++--- > > > > 1 file changed, 2 insertions(+), 3 deletions(-) > > > > > > > > diff --git a/crypto/ecdsa.c b/crypto/ecdsa.c > > > > index 258fffbf623d..55114146ff84 100644 > > > > --- a/crypto/ecdsa.c > > > > +++ b/crypto/ecdsa.c > > > > @@ -215,9 +215,8 @@ static int ecdsa_ecc_ctx_reset(struct ecc_ctx *= ctx) > > > > } > > > > =20 > > > > /* > > > > - * Set the public key given the raw uncompressed key data from an = X509 > > > > - * certificate. The key data contain the concatenated X and Y coor= dinates of > > > > - * the public key. > > > > + * Set the public ECC key as defined by RFC5480 section 2.2 "Subje= ct Public > > > > + * Key". Only the uncompressed format is supported. > > > > */ > > > > static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const vo= id *key, unsigned int keylen) > > > > { > > > > > > Based on this, is this now along the lines of correct format": > > > > > > *ptr++ =3D 0x04; /* uncompressed */ > > > ptr =3D asn1_encode_octet_string(&ptr[0], &in[sizeof(in)], &x[= 0], x_size); > > > ptr =3D asn1_encode_octet_string(&ptr[0], &in[sizeof(in)], &x[= x_size + 2], x_size); > > > in_len =3D ptr - in; > > > ret =3D crypto_akcipher_set_pub_key(tfm, in, in_len); > > > > > > I fixed up the above as it should be only single octect string to this: > > > > ptr =3D &in[0]; > > *ptr++ =3D 0x04; /* uncompressed */ > > ptr =3D asn1_encode_octet_string(&ptr[0], &in[sizeof(in)], > > &data[0], 2 * x_size); > > in_len =3D ptr - in; > > pr_info("in_len=3D%u\n", in_len); > > ret =3D crypto_akcipher_set_pub_key(tfm, in, in_len); > > crypto_free_akcipher(tfm); > > > > It fails in: > > > > ndigits =3D DIV_ROUND_UP(digitlen, sizeof(u64)); > > if (ndigits !=3D ctx->curve->g.ndigits) > > return -EINVAL; > > > > I checked that in_len=3D67. > > > > The tfm is deleted at instant because the above code is part of *_query= . > > TPM2 ECDSA asymmetric key that way that signature verification will wor= k > > when it is needed. The key type signs with TPM and verifies with the > > software cipher. > > 3rd trial, i.e. no octect encoding at all but with the undocumented > (in the current mainline) prefix byte: > > ptr =3D &data[0]; > *ptr++ =3D 0x04; /* uncompressed */ > memcpy(&ptr[0], &x[0], x_size); > memcpy(&ptr[x_size], &x[x_size + 2], x_size); > ret =3D crypto_akcipher_set_pub_key(tfm, data, 3 * x_size + 1); > crypto_free_akcipher(tfm); > > I added also kprobe: > > echo 'r:ecdsa ecdsa_set_pub_key $retval' >> /sys/kernel/tracing/kprobe_ev= ents > echo 1 > /sys/kernel/tracing/events/kprobes/enable > cat /sys/kernel/tracing/dynamic_events > > Results: > > ecdsa: (ecdsa_set_pub_key+0xc1/0xe0 <- ecc_is_pubkey_valid_full) arg1=3D0= xffffffea > > So I guess this is on a right track reverse engineering the format used > in this API at least :-) AFAIK TPM2 x and y are big-endian but I have no idea what ecc_digits_from_bytes() expects because it has no documentation. I guess it expect little-endian format, is this correct? BR, Jarkko