Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp31920pxf; Wed, 10 Mar 2021 19:44:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+ZuO0n7wr2mgoRrhaQjuj4JESAKTlxFoOXoHu+UdZR9bhbcko237QDYr00zgzkq67hX7i X-Received: by 2002:a17:906:f210:: with SMTP id gt16mr1042787ejb.206.1615434275217; Wed, 10 Mar 2021 19:44:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615434275; cv=none; d=google.com; s=arc-20160816; b=w/Q+10wR4yumKE1qPRmXdHRBqPrcXae50g/pFfXWLXLv6xewRytPu8j2oyevaqmEa8 EmGNtZKnyqdE1NlC38dHIqFW+z0FE+NOGvltbGNRmjZvbSxroO7vY/nCdk+TvFmeHkAS v8p5ne3/74zsI8CfKXaztiVmV54SO65xQr+DIWC3kXqSCAQD/HDMmyv+556XqsumUoD9 wfZdeQnzyEF16xsZJ0e42fo461OxsYv+4mND6zI/XRJO8Ttbg+NCBNBjb/SPhvFILdsq oQzz/OFmGb8ytTJ5Z4F8qeCdJWxH3M5FD/1JoXzYNKQvKLOXc+TbqY1m1A1/xTdMIwWe NGeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=HwDYb44qb1A/Qo+gMrWOrvtieCbxHBu0YAho4TfPICw=; b=o1+JJoEkR5CaQgM/rT8bXHOpSM/v4kpX6vQ7mhQ36Lhyb8QucR9LCN1qUsVU2xq6cA AiiklUdlFvrvEPNrmzGhn4H58bLQsbDaJev+y2g6+Rf6nJqAhb1+BPe/WIK/SiwbDWcH o3YBbpg08Pc9CDHuvVUur6o9dQNE2bIuI9EpHLPL/OhjON2HfkCPiC8PyhWlXPTWw1cX v8T4DPsEhQH/b4jPeXdaXGQf+9M026yZDV88A4VfyS/u2w0wxiNV9tjBVmF+V+A01uZJ YcEhF7TU6YfqgZlIlu1uiwjAhYKrc/vgvD4uxAfEfClIc6hWmjJuAL+OG4o9p2QcY7hZ pS4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=K4jGvwDh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u10si915276edj.431.2021.03.10.19.44.12; Wed, 10 Mar 2021 19:44:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=K4jGvwDh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230480AbhCKDnF (ORCPT + 99 others); Wed, 10 Mar 2021 22:43:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:52400 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229739AbhCKDmi (ORCPT ); Wed, 10 Mar 2021 22:42:38 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4BB3864E77; Thu, 11 Mar 2021 03:42:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1615434157; bh=k8OzmRwgOFwV8GHVfIcQNSa+kPtmotoHlN41AgoZI10=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=K4jGvwDhYpwbncoCFrUFbOof5b/5f5pH710z+H5rUxW9z71Y3X6QgbGBucw86Ao/V Jr5iOrRO3YJs8f3XrM2677ZQqu4AsKhhENWWmTUi1wm/iUepY1EPZg2mpZcOyV8mQi vUot//wgLdsm1V9RAMA6DIzlC+1xS+ST3xiobNwo2ah+/UDtqC83NdhdICi0RAD4PQ sUrUxEYCAJ3I+nTbQ2ayUjqafyNwlVleeLQNAdQu6YITCDfyiSFHNFqAELYqlKKoSD bs2PPtBMily/Y9UO1qJgWioS84m8ogf2oQmMrAoWO7qksKYB2ykyfxjX1TTMYDWZJI Fp6qWKVXZ3xGQ== Date: Thu, 11 Mar 2021 05:42:13 +0200 From: Jarkko Sakkinen To: Jia Zhang Cc: Andy Lutomirski , Tianjia Zhang , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Sean Christopherson , Shuah Khan , X86 ML , linux-sgx@vger.kernel.org, "open list:KERNEL SELFTEST FRAMEWORK" , LKML Subject: Re: [PATCH] selftests/sgx: fix EINIT failure dueto SGX_INVALID_SIGNATURE Message-ID: References: <20210301051836.30738-1-tianjia.zhang@linux.alibaba.com> <3bcdcf04-4bed-ed95-84b6-790675f18240@linux.alibaba.com> <1f5c2375-39e2-65a8-3ad3-8dc43422f568@linux.alibaba.com> <20ef1254-007d-04ce-8df5-5122ffd4d8d3@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20ef1254-007d-04ce-8df5-5122ffd4d8d3@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 11, 2021 at 10:47:50AM +0800, Jia Zhang wrote: > > > On 2021/3/11 上午5:39, Jarkko Sakkinen wrote: > > On Wed, Mar 10, 2021 at 08:44:44PM +0800, Jia Zhang wrote: > >> > >> > >> On 2021/3/2 下午9:47, Jarkko Sakkinen wrote: > >>> On Mon, Mar 01, 2021 at 09:54:37PM -0800, Andy Lutomirski wrote: > >>>> On Mon, Mar 1, 2021 at 9:06 PM Tianjia Zhang > >>>> wrote: > >>>>> > >>>>> > >>>>> > >>>>> On 3/1/21 5:54 PM, Jarkko Sakkinen wrote: > >>>>>> On Mon, Mar 01, 2021 at 01:18:36PM +0800, Tianjia Zhang wrote: > >>>>>>> q2 is not always 384-byte length. Sometimes it only has 383-byte. > >>>>>> > >>>>>> What does determine this? > >>>>>> > >>>>>>> In this case, the valid portion of q2 is reordered reversely for > >>>>>>> little endian order, and the remaining portion is filled with zero. > >>>>>> > >>>>>> I'm presuming that you want to say "In this case, q2 needs to be reversed because...". > >>>>>> > >>>>>> I'm lacking these details: > >>>>>> > >>>>>> 1. Why the length of Q2 can vary? > >>>>>> 2. Why reversing the bytes is the correct measure to counter-measure > >>>>>> this variation? > >>>>>> > >>>>>> /Jarkko > >>>>>> > >>>>> > >>>>> When use openssl to generate a key instead of using the built-in > >>>>> sign_key.pem, there is a probability that will encounter this problem. > >>>>> > >>>>> Here is a problematic key I encountered. The calculated q1 and q2 of > >>>>> this key are both 383 bytes, If the length is not processed, the > >>>>> hardware signature will fail. > >>>> > >>>> Presumably the issue is that some keys have parameters that have > >>>> enough leading 0 bits to be effectively shorter. The openssl API > >>>> (and, sadly, a bunch of the ASN.1 stuff) treats these parameters as > >>>> variable-size integers. > >>> > >>> But the test uses a static key. It used to generate a key on fly but > >> > >> IMO even though the test code, it comes from the linux kernel, meaning > >> that its quality has a certain guarantee and it is a good reference, so > >> the test code still needs to ensure its correctness. > > > > Hmm... what is working incorrectly then? > > In current implementation, it is working well, after all the static key > can derive the full 384-byte length of q1 and q2. As mentioned above, if > someone refers to the design of signing tool from selftest code, it is > quite possible that the actual implementation will use dynamical or > external signing key deriving shorter q1 and/or q2 in length. A self-test needs is not meant to be generic to be directly used in 3rd party code. With the current key there is not issue => there is no issue. > > Going back the technical background, I'm not a crypto expert, so I > choose to check the code, doc and make experiment. > > Accorindg to intel sdm vol3 37.14: > > ``` > SIGSTRUCT includes four 3072-bit integers (MODULUS, SIGNATURE, Q1, Q2). > Each such integer is represented as a byte strings of length 384, with > the most significant byte at the position “offset + 383”, and the least > significant byte at position “offset”. > > ... > > The 3072-bit integers Q1 and Q2 are defined by: > q1 = floor(Signature^2 / Modulus); > q2 = floor((Signature^3 - q1 * Signature * Modulus) / Modulus); > ``` > > and the implementation of singing tool from Intel SGX SDK > (https://github.com/intel/linux-sgx/blob/master/sdk/sign_tool/SignTool/sign_tool.cpp#L381 > ), the most significant byte shuld be at the position “offset + > q1/q2_actuall_len”, and the padding portion should be filled with zero, > and don't involve the order reverse, but the selftest code always does. > This is the root cause of SGX_INVALID_SIGNATURE. > > Just simplily enforce size_q1 and size_q2 to SE_KEY_SIZE, and generate > randome siging key with `openssl genrsa -3 -out signing_key.pem 3072`, > the SGX_INVALID_SIGNATURE error will appear up quickly. > > Jia > > > > > /Jarkko > > > /Jarkko