Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3018250pxb; Sun, 28 Feb 2021 22:25:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxj/Wm0Srd8dJe+Q30V3frFpwy9Ov+tqKO4+7lV0nVkvufSKZX63otMO7gtz61zOq0Uk2C X-Received: by 2002:a17:906:5d06:: with SMTP id g6mr6970221ejt.216.1614579914899; Sun, 28 Feb 2021 22:25:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614579914; cv=none; d=google.com; s=arc-20160816; b=W789gSlnxU8W/H4lKsqEvLiNGx3i8ty8XzZvNC+pMSHohABztHCANYMDgmQUUNd0m1 CRSnsANALcmXPAHmJcEheVlmTR2n8dyiq7CamdPdrstyiuQ9vxr/wxehRgOly6nmFgIu qfrZ6CKKzdn8Mw1LTB8lmLnTxmzvA21XVhSwWe/53M69FOYQFWaQbkHYTRb4DXui8+00 qpMbe/ui1/HWmV5lASiOA/6QDrqEDUkJqXETamgBGSoII43bzDLxk2WHnaGzH+iphCIJ OFuQnbMOlzMFK5BiEudNMSeEDheYxwAMRrBSFqEI/aiEcWDPmrszccPd5bozuKSZQUw/ /jlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=tgpIaYYAc+SMNXatSiZYoL96qHruVkXsnJhyjA/pCmo=; b=f/AZsScjFRB3fZqS8GByLslw5vcntLohdYUG29BKYEZPF/bGAgow+86xPAY1azmjR/ 3m3tjbSetLvUjBGlkZ/RVSXGVWgqOlE2iGtd4jO1qGVWRWPNMRTLHwK7bw+FIGNGxBgc hF4Ou0WvK4jWq9xpdfzgbUg38b/SV8QIelsIGVdZfpJXoJ4oP+Yb+piqkoBLy1csFiVb 4PZ/EKBPWdpn118HuODglW3oFOLk0ed3jNrAz82Mm9auSSo+fFlrl+/8g+l3AsGGjxyG qiLeGDWpVowJINNwGRK2gnuIut4A9lMAAAw0W1GXHeq59jWNrf9X3f42bvfrLGlKNxlD BpOw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qw16si8116604ejb.673.2021.02.28.22.24.50; Sun, 28 Feb 2021 22:25:14 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233452AbhCAFTW (ORCPT + 99 others); Mon, 1 Mar 2021 00:19:22 -0500 Received: from out30-56.freemail.mail.aliyun.com ([115.124.30.56]:53026 "EHLO out30-56.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233439AbhCAFTV (ORCPT ); Mon, 1 Mar 2021 00:19:21 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04426;MF=tianjia.zhang@linux.alibaba.com;NM=1;PH=DS;RN=13;SR=0;TI=SMTPD_---0UPtRs.4_1614575916; Received: from localhost(mailfrom:tianjia.zhang@linux.alibaba.com fp:SMTPD_---0UPtRs.4_1614575916) by smtp.aliyun-inc.com(127.0.0.1); Mon, 01 Mar 2021 13:18:36 +0800 From: Tianjia Zhang To: Jarkko Sakkinen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Sean Christopherson , Shuah Khan , x86@kernel.org, linux-sgx@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Jia Zhang Cc: Tianjia Zhang Subject: [PATCH] selftests/sgx: fix EINIT failure dueto SGX_INVALID_SIGNATURE Date: Mon, 1 Mar 2021 13:18:36 +0800 Message-Id: <20210301051836.30738-1-tianjia.zhang@linux.alibaba.com> X-Mailer: git-send-email 2.19.1.3.ge56e4f7 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org q2 is not always 384-byte length. Sometimes it only has 383-byte. In this case, the valid portion of q2 is reordered reversely for little endian order, and the remaining portion is filled with zero. Signed-off-by: Tianjia Zhang --- tools/testing/selftests/sgx/sigstruct.c | 41 +++++++++++++------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/sgx/sigstruct.c b/tools/testing/selftests/sgx/sigstruct.c index dee7a3d6c5a5..92bbc5a15c39 100644 --- a/tools/testing/selftests/sgx/sigstruct.c +++ b/tools/testing/selftests/sgx/sigstruct.c @@ -55,10 +55,27 @@ static bool alloc_q1q2_ctx(const uint8_t *s, const uint8_t *m, return true; } +static void reverse_bytes(void *data, int length) +{ + int i = 0; + int j = length - 1; + uint8_t temp; + uint8_t *ptr = data; + + while (i < j) { + temp = ptr[i]; + ptr[i] = ptr[j]; + ptr[j] = temp; + i++; + j--; + } +} + static bool calc_q1q2(const uint8_t *s, const uint8_t *m, uint8_t *q1, uint8_t *q2) { struct q1q2_ctx ctx; + int len; if (!alloc_q1q2_ctx(s, m, &ctx)) { fprintf(stderr, "Not enough memory for Q1Q2 calculation\n"); @@ -89,8 +106,10 @@ static bool calc_q1q2(const uint8_t *s, const uint8_t *m, uint8_t *q1, goto out; } - BN_bn2bin(ctx.q1, q1); - BN_bn2bin(ctx.q2, q2); + len = BN_bn2bin(ctx.q1, q1); + reverse_bytes(q1, len); + len = BN_bn2bin(ctx.q2, q2); + reverse_bytes(q2, len); free_q1q2_ctx(&ctx); return true; @@ -152,22 +171,6 @@ static RSA *gen_sign_key(void) return key; } -static void reverse_bytes(void *data, int length) -{ - int i = 0; - int j = length - 1; - uint8_t temp; - uint8_t *ptr = data; - - while (i < j) { - temp = ptr[i]; - ptr[i] = ptr[j]; - ptr[j] = temp; - i++; - j--; - } -} - enum mrtags { MRECREATE = 0x0045544145524345, MREADD = 0x0000000044444145, @@ -367,8 +370,6 @@ bool encl_measure(struct encl *encl) /* BE -> LE */ reverse_bytes(sigstruct->signature, SGX_MODULUS_SIZE); reverse_bytes(sigstruct->modulus, SGX_MODULUS_SIZE); - reverse_bytes(sigstruct->q1, SGX_MODULUS_SIZE); - reverse_bytes(sigstruct->q2, SGX_MODULUS_SIZE); EVP_MD_CTX_destroy(ctx); RSA_free(key); -- 2.19.1.3.ge56e4f7