Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2127949yba; Mon, 15 Apr 2019 05:37:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6b3x6bd5q5Wds7kIkILpU2tr/MDTkCvmdRF187fLsMX5+HQEW0lcADYx89CwPdw/oLPDa X-Received: by 2002:a17:902:8bc3:: with SMTP id r3mr17425753plo.53.1555331865659; Mon, 15 Apr 2019 05:37:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555331865; cv=none; d=google.com; s=arc-20160816; b=JbBkKsPyEx8RYfHxppYsueqaPatG05YrJnPfjYtitfkahgah5SLvTxnOVxs3KNc655 U2v3IrgyurFsFfj8tHmMc2jL1yrruC4FoCUY5sXb6VFGyOqRZdaKnZrcz2BzLQnusHIK scm/TZx0yyM1x2u0cYAeOnQyytEaH299Ws++PY6ctmsb7K9dox5L2j6/L77Orkdar31W hAH0J/azH6c6x5w/Gpnt0K+X11m0U7WBzKiW+rrQqmFE2YI0W6Ai5uX/C3NCQw7vw4rc bv00e912MkK2tbhSjXLngMSQIKYoFqtukyCsiGP6qvTMWVkcT8e6iY35hUHpu/GY6Efn rzZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=yY1gQN6/TzQsnmu6EKWqqwbQF/G9dO8vcwRmU7Mil2I=; b=AmZcf65i3wQzBMfAgzI6Rxg7k2aoeB9ZEsms33zLu8TZv11L669CvV0JilzBHWMdi7 dLpOlfw72NaZ3vOUcaFDQtn91BTu8kVGslIv/HEqb2nAZgx68K+Km+p1uVkUvEn/+tpI yXql863XYsmTbq3R0CVKxPl+Wn+bW9a/ILhKWfi2V8G5qk1P6MwT3GKR2DtQLNoILAxF NNrqqAhVgoLK4ZeJoRIko+0kSP+PzfYHEFPpaZisgAmmY7EV8j48H4ITyetLi+s1Qso1 qVwD2JO6QkkfotM6lOZpY57/Xg7qVG8Fpx8xEIlkPWLclLoEDxARZQ9hFRX9RdgoMnF5 8FjA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hygon.cn Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m15si45861508pgj.126.2019.04.15.05.37.29; Mon, 15 Apr 2019 05:37:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=hygon.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727335AbfDOMga (ORCPT + 99 others); Mon, 15 Apr 2019 08:36:30 -0400 Received: from [110.188.70.11] ([110.188.70.11]:51454 "EHLO spam2.hygon.cn" rhost-flags-FAIL-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727025AbfDOMga (ORCPT ); Mon, 15 Apr 2019 08:36:30 -0400 Received: from spam2.hygon.cn (localhost [127.0.0.2] (may be forged)) by spam2.hygon.cn with ESMTP id x3FC9jZf059355; Mon, 15 Apr 2019 20:09:45 +0800 (GMT-8) (envelope-from fenghao@hygon.cn) Received: from MK-FE.hygon.cn ([172.23.18.61]) by spam2.hygon.cn with ESMTP id x3FC7PM4059208; Mon, 15 Apr 2019 20:07:25 +0800 (GMT-8) (envelope-from fenghao@hygon.cn) Received: from cncheex02.Hygon.cn ([172.23.18.12]) by MK-FE.hygon.cn with ESMTP id x3FC6op0022121; Mon, 15 Apr 2019 20:06:50 +0800 (GMT-8) (envelope-from fenghao@hygon.cn) Received: from harry-Inspiron-5675.higon.com (172.23.18.44) by cncheex02.Hygon.cn (172.23.18.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1466.3; Mon, 15 Apr 2019 20:07:20 +0800 From: Hao Feng To: "'Tom Lendacky '" , "'Gary Hook '" , "'Herbert Xu '" , "' David S. Miller '" , "'Janakarajan Natarajan '" CC: "'Zhaohui Du '" , "'Zhiwei Ying '" , "'Wen Pu '" , Hao Feng , , Subject: [PATCH 3/6] crypto: ccp: Implement SEV_GM_PUBKEY_GEN ioctl command Date: Mon, 15 Apr 2019 20:04:25 +0800 Message-ID: <1555329868-17895-4-git-send-email-fenghao@hygon.cn> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1555329868-17895-1-git-send-email-fenghao@hygon.cn> References: <1555329868-17895-1-git-send-email-fenghao@hygon.cn> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [172.23.18.44] X-ClientProxiedBy: cncheex02.Hygon.cn (172.23.18.12) To cncheex02.Hygon.cn (172.23.18.12) X-MAIL: spam2.hygon.cn x3FC7PM4059208 X-DNSRBL: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The SEV_GM_PUBKEY_GEN command is used to get SM2 random public key from SEV firmware to start SM2 key exchange, guest owner will use the random public key to compute share key. Signed-off-by: Hao Feng --- drivers/crypto/ccp/psp-dev.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 17 +++++++++ 2 files changed, 100 insertions(+) diff --git a/drivers/crypto/ccp/psp-dev.c b/drivers/crypto/ccp/psp-dev.c index fafebf4..c165847c 100644 --- a/drivers/crypto/ccp/psp-dev.c +++ b/drivers/crypto/ccp/psp-dev.c @@ -720,6 +720,86 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp) return ret; } +static int sev_ioctl_do_gm_pubkey_gen(struct sev_issue_cmd *argp) +{ + struct sev_user_data_gm_pubkey_gen input; + void *key_id_blob = NULL, *pubkey_blob = NULL; + struct sev_data_gm_pubkey_gen *data; + int ret; + + if (copy_from_user(&input, (void __user *)argp->data, sizeof(input))) + return -EFAULT; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + /* Userspace wants to query the public key length. */ + if (!input.pubkey_address || + !input.pubkey_len) + goto cmd; + + /* Copy key id blob from userspace. */ + key_id_blob = psp_copy_user_blob(input.key_id_address, input.key_id_len); + if (IS_ERR(key_id_blob)) { + ret = PTR_ERR(key_id_blob); + goto e_free; + } + + data->key_id_address = __psp_pa(key_id_blob); + data->key_id_len = input.key_id_len; + + /* Allocate a physically contiguous buffer to store the public key blob. */ + if ((input.pubkey_len > SEV_FW_BLOB_MAX_SIZE) || + !access_ok(input.pubkey_address, input.pubkey_len)) { + ret = -EFAULT; + goto e_free_key_id; + } + + pubkey_blob = kmalloc(input.pubkey_len, GFP_KERNEL); + if (!pubkey_blob) { + ret = -ENOMEM; + goto e_free_key_id; + } + + data->pubkey_address = __psp_pa(pubkey_blob); + data->pubkey_len = input.pubkey_len; + +cmd: + /* If platform is not in INIT state then transition it to INIT. */ + if (psp_master->sev_state != SEV_STATE_INIT) { + ret = __sev_platform_init_locked(&argp->error); + if (ret) + goto e_free_pubkey; + } + + ret = __sev_do_cmd_locked(SEV_CMD_GM_PUBKEY_GEN, data, &argp->error); + + /* If we query the length, FW responded with expected data. */ + input.pubkey_len = data->pubkey_len; + + if (copy_to_user((void __user *)argp->data, &input, sizeof(input))) { + ret = -EFAULT; + goto e_free_pubkey; + } + + if (pubkey_blob) { + if (copy_to_user((void __user *)input.pubkey_address, + pubkey_blob, input.pubkey_len)) { + ret = -EFAULT; + goto e_free_pubkey; + } + } + +e_free_pubkey: + kfree(pubkey_blob); +e_free_key_id: + kfree(key_id_blob); +e_free: + kfree(data); + return ret; +} + static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { void __user *argp = (void __user *)arg; @@ -766,6 +846,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) case SEV_GET_ID: ret = sev_ioctl_do_get_id(&input); break; + case SEV_GM_PUBKEY_GEN: + ret = sev_ioctl_do_gm_pubkey_gen(&input); + break; default: ret = -EINVAL; goto out; diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index ac8c60b..7482cbd 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -32,6 +32,8 @@ enum { SEV_PEK_CERT_IMPORT, SEV_GET_ID, + SEV_GM_PUBKEY_GEN, + SEV_MAX, }; @@ -136,6 +138,21 @@ struct sev_user_data_get_id { } __packed; /** + * struct sev_user_data_gm_pubkey_gen - GM_PUBKEY_GEN command parameters + * + * @key_id_address: address of key id + * @key_id_len: len of key id + * @pubkey_address: address of GM public key + * @pubkey_len: len of GM public key + */ +struct sev_user_data_gm_pubkey_gen { + __u64 key_id_address; /* In */ + __u32 key_id_len; /* In */ + __u64 pubkey_address; /* In */ + __u32 pubkey_len; /* In/Out */ +} __packed; + +/** * struct sev_issue_cmd - SEV ioctl parameters * * @cmd: SEV commands to execute -- 2.7.4