From patchwork Tue Oct 17 10:52:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark O'Donovan X-Patchwork-Id: 154064 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4045893vqb; Tue, 17 Oct 2023 03:53:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IERCrTyks8U91ueAMnCQ1M8P0eEvYI4/naNLb9Y65WNvfIyuRW3LbpNkQXVsgKQmHBppJWJ X-Received: by 2002:a05:6a21:8cc5:b0:14e:2c56:7b02 with SMTP id ta5-20020a056a218cc500b0014e2c567b02mr1577084pzb.0.1697540011532; Tue, 17 Oct 2023 03:53:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697540011; cv=none; d=google.com; s=arc-20160816; b=on5uAD8mPK/tyrtQAIiy6gslZkrGjgTXdO0OUd2FJcZerVhc3hRKmkcJlEJAYSzXWm F1Rhr7K3VJ8JdVvTq1e0fFhFvZMo7CGYZypv+nLHuJ9botsyPPGFRVVPanB3t4F5HlpM aFNI+Pzc1x7vNTNe28nhQZg/MparoGFtr7/GSH+v+GUXxP4akHpl6gr4lfEldPhbXwr+ VBwf3QTvpleyjwlNTLyFPBFYGcW6WRKdH3oanp2FD4z9vDozzjf9pww4IxcxrzY6GO11 nayacU/E82oJewfuEKDyGO2kuUOkot9AhMihXI1c06HEQ9jKl5lWkfi5iZxEXSmSPfui 1PHA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uvw+oIq57GPIC6aTHMxWkMEl8ZoflyFtv2q8e9lVB7k=; fh=OK6NlIuxAObXnbB+YsmCJIFQ9spAlQ71xiCElZ04Vio=; b=v8fpY50Ij+98FECZLvyLK2hU+XBQ0FnSMbSFxbK3DSR6FYaXSbAThYQgcMCR2BsKI5 QYok0bStQl6kO7oZyyY9M/asivX5xitxJXVT6q86pq+IxgetY2E2b/WUIPmMx2Bkk+6G 6zrgCBUraGLqLOwbB6KzCpurBN18DbffYxSJezsgLXlcMVP+/xSxb74sMebtL1yN7omu Fs6ATDiP5NF0hrvV1DjLD3x+tP7V1UU+gnitMeB7w34geRItsJEDehq0+e7o8u2bxuIC cM0i9iONJMlgMes5oNw3iqqFkysK3wqpDt11DeU2vlUJAePGPLuN39kzmINTj81Km3kV 1WcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=DCoPY1uM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id l6-20020a170902f68600b001ca8e7953b3si1606041plg.538.2023.10.17.03.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 03:53:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=DCoPY1uM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 3807D802A919; Tue, 17 Oct 2023 03:53:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343710AbjJQKxP (ORCPT + 19 others); Tue, 17 Oct 2023 06:53:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234622AbjJQKxL (ORCPT ); Tue, 17 Oct 2023 06:53:11 -0400 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5ACAB0 for ; Tue, 17 Oct 2023 03:53:09 -0700 (PDT) Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 5F72A24002B for ; Tue, 17 Oct 2023 12:53:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1697539988; bh=0PnFmgZ0qQizc+TAU9NqyXD9j4ftHguqoGr3flF/G5M=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version: Content-Transfer-Encoding:From; b=DCoPY1uMJD7CcNFO4Yj5q4pemFNlMBCRNzxut435TguUDCXM9RjPmxRsgHEQdE9XX fuFUekNiVu/10XLYV1MRu5vH5MEZ2htxu0LefIVEukWVR5o3YxHCTQowNOxHwOLqf4 K0pUQqm57bxt/e0XXduYxZqauZS5v143eTJwtMKsDIM4m00lnpYQT/RYJpdlNbdlpZ WW0Gwa7Uf4dQNMjF05ka3K2HAfS2gCYKbALF1pKs+kANwD6OhFJDaUaBzjeVpo3Gxm KNmVp8w5tViCBTmY3hR1u2nMIExdTpprL/T271+4cNSrYyx+tbLzhYVe3do54LJbUN MBtqzXrg3+w0Q== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4S8rRl0Tyhz6tvd; Tue, 17 Oct 2023 12:53:07 +0200 (CEST) From: Mark O'Donovan To: linux-kernel@vger.kernel.org Cc: linux-nvme@lists.infradead.org, sagi@grimberg.me, hch@lst.de, axboe@kernel.dk, kbusch@kernel.org, hare@suse.de, Mark O'Donovan , Akash Appaiah Subject: [PATCH v4 1/3] nvme-auth: alloc nvme_dhchap_key as single buffer Date: Tue, 17 Oct 2023 10:52:49 +0000 Message-Id: <20231017105251.3274652-2-shiftee@posteo.net> In-Reply-To: <20231017105251.3274652-1-shiftee@posteo.net> References: <20231017105251.3274652-1-shiftee@posteo.net> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 17 Oct 2023 03:53:25 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779999715115453899 X-GMAIL-MSGID: 1779999715115453899 Co-developed-by: Akash Appaiah Signed-off-by: Akash Appaiah Signed-off-by: Mark O'Donovan Reviewed-by: Hannes Reinecke --- V2 -> V3: initial version V3 -> V4: added function to get size of key struct drivers/nvme/common/auth.c | 35 ++++++++++++++++++++++++----------- include/linux/nvme-auth.h | 4 +++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c index d90e4f0c08b7..984f4320aca3 100644 --- a/drivers/nvme/common/auth.c +++ b/drivers/nvme/common/auth.c @@ -150,6 +150,14 @@ size_t nvme_auth_hmac_hash_len(u8 hmac_id) } EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len); +u32 nvme_auth_key_struct_size(u32 key_len) +{ + struct nvme_dhchap_key key; + + return struct_size(&key, key, key_len); +} +EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size); + struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, u8 key_hash) { @@ -163,14 +171,9 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, p = strrchr(secret, ':'); if (p) allocated_len = p - secret; - key = kzalloc(sizeof(*key), GFP_KERNEL); + key = nvme_auth_alloc_key(allocated_len, 0); if (!key) return ERR_PTR(-ENOMEM); - key->key = kzalloc(allocated_len, GFP_KERNEL); - if (!key->key) { - ret = -ENOMEM; - goto out_free_key; - } key_len = base64_decode(secret, allocated_len, key->key); if (key_len < 0) { @@ -213,19 +216,29 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, key->hash = key_hash; return key; out_free_secret: - kfree_sensitive(key->key); -out_free_key: - kfree(key); + nvme_auth_free_key(key); return ERR_PTR(ret); } EXPORT_SYMBOL_GPL(nvme_auth_extract_key); +struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash) +{ + u32 num_bytes = nvme_auth_key_struct_size(len); + struct nvme_dhchap_key *key = kzalloc(num_bytes, GFP_KERNEL); + + if (key) { + key->len = len; + key->hash = hash; + } + return key; +} +EXPORT_SYMBOL_GPL(nvme_auth_alloc_key); + void nvme_auth_free_key(struct nvme_dhchap_key *key) { if (!key) return; - kfree_sensitive(key->key); - kfree(key); + kfree_sensitive(key); } EXPORT_SYMBOL_GPL(nvme_auth_free_key); diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h index dcb8030062dd..a5ae9abe1ef6 100644 --- a/include/linux/nvme-auth.h +++ b/include/linux/nvme-auth.h @@ -9,9 +9,9 @@ #include struct nvme_dhchap_key { - u8 *key; size_t len; u8 hash; + u8 key[]; }; u32 nvme_auth_get_seqnum(void); @@ -24,9 +24,11 @@ const char *nvme_auth_digest_name(u8 hmac_id); size_t nvme_auth_hmac_hash_len(u8 hmac_id); u8 nvme_auth_hmac_id(const char *hmac_name); +u32 nvme_auth_key_struct_size(u32 key_len); struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, u8 key_hash); void nvme_auth_free_key(struct nvme_dhchap_key *key); +struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash); u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn); int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key); int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len, From patchwork Tue Oct 17 10:52:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark O'Donovan X-Patchwork-Id: 154063 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4045854vqb; Tue, 17 Oct 2023 03:53:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgpzpTAhveMPInsjHIJ/TqLTxuT15L9Y9KVmijP9TP645qMZj6KnGG/A2EAJACxsJ4uPOL X-Received: by 2002:a05:6a00:278c:b0:6b5:523e:9e9 with SMTP id bd12-20020a056a00278c00b006b5523e09e9mr1825694pfb.3.1697540006130; Tue, 17 Oct 2023 03:53:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697540006; cv=none; d=google.com; s=arc-20160816; b=UYCJ7/zLVhx4D6uzy+M20uFkiojf8D/elDw7QFTCbossvz5p+N738MOCTfxuTog9Kq 3Hx/gpCpM2QJxy2Liaozip2UpdmF2r6g2JjsMLgssSC9uFV15DMjehf7w+pFdhITCNRf AmYUgjvwqiTMEJCvOMgsu8zk9LAirJpsBjEPCDBXTJKsQ1uszj+QVvPrFJkFbCv31skl qhAQmYjdFG7t3DybY52IIhRDPaDlws68Y8LVs50WLYJTIaHeizapVhDeU8Swh5vVdKW8 IrzVVgY/HHYORIE8ljrIYvt8gTBA94UjUOS+8eXyeNm8WrNrjhQbB/Co4ouzqDXOtt5m lMCg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IDt+mgVfuhyxZ5dvfBdvzae2yr69d+KBrhUPcgyPjVs=; fh=OK6NlIuxAObXnbB+YsmCJIFQ9spAlQ71xiCElZ04Vio=; b=QaaTOg65xu8BXJQTWkrlXZBrKhNWeeEBT7ngyO7DvJlo0kenWIsaouz2s+DRJrVDpP X12iBs9v1qgsglssR/pEDgAuIJWhfAfSR9kwatEp0HYUmyoz+jkvmn0kaZ/c9CBqlpCQ h1RQECRmixcBNVS/a1Q8VLHogUtXKNpLGKBa3pu1UdxpuQ0gj+KZyYgRXN/cKCr7ULt9 +aAgJs4LK4BdkxrNmJJxs5KWi2oTEvT2FgjUpQJ9gzM9V9j1lz3cGKMx0+6MMwAm2dBt 9JVoIi6yGyLkSkARuDwhBsWwhAw8aK7RDF7OnM/0rX4QU4va+Mw8Ba6yBJP9CiWb9ZME p4pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b="e/kUvJBr"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x9-20020a634a09000000b0059f0cebd054si1553070pga.731.2023.10.17.03.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 03:53:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b="e/kUvJBr"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 3FA06801D4A8; Tue, 17 Oct 2023 03:53:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343728AbjJQKxQ (ORCPT + 19 others); Tue, 17 Oct 2023 06:53:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343667AbjJQKxN (ORCPT ); Tue, 17 Oct 2023 06:53:13 -0400 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DCEFEA for ; Tue, 17 Oct 2023 03:53:11 -0700 (PDT) Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id B98E724002A for ; Tue, 17 Oct 2023 12:53:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1697539989; bh=lsy+EhwM0egrnNd594n9n/ct9m5wTrzR71bElFpZ94U=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version: Content-Transfer-Encoding:From; b=e/kUvJBrE9XQjkWg4mB+G8loffnIDHUGXNACJOHKMvtWuInUcnqR2GVUd6Uapssyo 4ia7d7ncpNFG9OMO/wVHMKEFzfTb0CS4tvlD8SlKHEb/2CUlYdD6q44jN3ULMkcbq+ cY8kuXWPPqOShOfEkUFMxe5rKKcVE8/P9evvY3OsgEftfnVwOF2uGqIt5Mt1LslW+E iodVSBOd7q141g3QfxSXJGhBdxWgy0k+u+niEt7vIKSARKKoG/Nk62CtGn5btdqMyo Li8dBk7RpT1hdcf9U16bXVGN6B6zR5gCYN6dVLrqN8q2qEz8CCQZV4DjR2/odfE3ss wIfbrSz6EYcHQ== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4S8rRm1HLHz6ty1; Tue, 17 Oct 2023 12:53:08 +0200 (CEST) From: Mark O'Donovan To: linux-kernel@vger.kernel.org Cc: linux-nvme@lists.infradead.org, sagi@grimberg.me, hch@lst.de, axboe@kernel.dk, kbusch@kernel.org, hare@suse.de, Mark O'Donovan , Akash Appaiah Subject: [PATCH v4 2/3] nvme-auth: use transformed key size to create resp Date: Tue, 17 Oct 2023 10:52:50 +0000 Message-Id: <20231017105251.3274652-3-shiftee@posteo.net> In-Reply-To: <20231017105251.3274652-1-shiftee@posteo.net> References: <20231017105251.3274652-1-shiftee@posteo.net> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 17 Oct 2023 03:53:25 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779999709637512079 X-GMAIL-MSGID: 1779999709637512079 This does not change current behaviour as the driver currently verifies that the secret size is the same size as the length of the transformation hash. Co-developed-by: Akash Appaiah Signed-off-by: Akash Appaiah Signed-off-by: Mark O'Donovan Reviewed-by: Hannes Reinecke --- V1 -> V2: support target implementation and controller secrets also V2 -> V3: return key from nvme_auth_transform_key V3 -> V4: use helper to get key struct len, added ERR_CAST drivers/nvme/common/auth.c | 23 ++++++++++++++--------- drivers/nvme/host/auth.c | 30 +++++++++++++++--------------- drivers/nvme/target/auth.c | 31 +++++++++++++++++-------------- include/linux/nvme-auth.h | 3 ++- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c index 984f4320aca3..f9e7ed8e729d 100644 --- a/drivers/nvme/common/auth.c +++ b/drivers/nvme/common/auth.c @@ -242,21 +242,25 @@ void nvme_auth_free_key(struct nvme_dhchap_key *key) } EXPORT_SYMBOL_GPL(nvme_auth_free_key); -u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) +struct nvme_dhchap_key *nvme_auth_transform_key( + struct nvme_dhchap_key *key, char *nqn) { const char *hmac_name; struct crypto_shash *key_tfm; struct shash_desc *shash; - u8 *transformed_key; - int ret; + struct nvme_dhchap_key *transformed_key; + int ret, key_len; if (!key || !key->key) { pr_warn("No key specified\n"); return ERR_PTR(-ENOKEY); } if (key->hash == 0) { - transformed_key = kmemdup(key->key, key->len, GFP_KERNEL); - return transformed_key ? transformed_key : ERR_PTR(-ENOMEM); + key_len = nvme_auth_key_struct_size(key->len); + transformed_key = kmemdup(key, key_len, GFP_KERNEL); + if (!transformed_key) + return ERR_PTR(-ENOMEM); + return transformed_key; } hmac_name = nvme_auth_hmac_name(key->hash); if (!hmac_name) { @@ -266,7 +270,7 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) key_tfm = crypto_alloc_shash(hmac_name, 0, 0); if (IS_ERR(key_tfm)) - return (u8 *)key_tfm; + return ERR_CAST(key_tfm); shash = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(key_tfm), @@ -276,7 +280,8 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) goto out_free_key; } - transformed_key = kzalloc(crypto_shash_digestsize(key_tfm), GFP_KERNEL); + key_len = crypto_shash_digestsize(key_tfm); + transformed_key = nvme_auth_alloc_key(key_len, key->hash); if (!transformed_key) { ret = -ENOMEM; goto out_free_shash; @@ -295,7 +300,7 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) ret = crypto_shash_update(shash, "NVMe-over-Fabrics", 17); if (ret < 0) goto out_free_transformed_key; - ret = crypto_shash_final(shash, transformed_key); + ret = crypto_shash_final(shash, transformed_key->key); if (ret < 0) goto out_free_transformed_key; @@ -305,7 +310,7 @@ u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn) return transformed_key; out_free_transformed_key: - kfree_sensitive(transformed_key); + nvme_auth_free_key(transformed_key); out_free_shash: kfree(shash); out_free_key: diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c index daf5d144a8ea..de1390d705dc 100644 --- a/drivers/nvme/host/auth.c +++ b/drivers/nvme/host/auth.c @@ -23,6 +23,7 @@ struct nvme_dhchap_queue_context { struct nvme_ctrl *ctrl; struct crypto_shash *shash_tfm; struct crypto_kpp *dh_tfm; + struct nvme_dhchap_key *transformed_key; void *buf; int qid; int error; @@ -36,7 +37,6 @@ struct nvme_dhchap_queue_context { u8 c1[64]; u8 c2[64]; u8 response[64]; - u8 *host_response; u8 *ctrl_key; u8 *host_key; u8 *sess_key; @@ -428,12 +428,12 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl, dev_dbg(ctrl->device, "%s: qid %d host response seq %u transaction %d\n", __func__, chap->qid, chap->s1, chap->transaction); - if (!chap->host_response) { - chap->host_response = nvme_auth_transform_key(ctrl->host_key, + if (!chap->transformed_key) { + chap->transformed_key = nvme_auth_transform_key(ctrl->host_key, ctrl->opts->host->nqn); - if (IS_ERR(chap->host_response)) { - ret = PTR_ERR(chap->host_response); - chap->host_response = NULL; + if (IS_ERR(chap->transformed_key)) { + ret = PTR_ERR(chap->transformed_key); + chap->transformed_key = NULL; return ret; } } else { @@ -442,7 +442,7 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl, } ret = crypto_shash_setkey(chap->shash_tfm, - chap->host_response, ctrl->host_key->len); + chap->transformed_key->key, chap->transformed_key->len); if (ret) { dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n", chap->qid, ret); @@ -508,19 +508,19 @@ static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl, struct nvme_dhchap_queue_context *chap) { SHASH_DESC_ON_STACK(shash, chap->shash_tfm); - u8 *ctrl_response; + struct nvme_dhchap_key *transformed_key; u8 buf[4], *challenge = chap->c2; int ret; - ctrl_response = nvme_auth_transform_key(ctrl->ctrl_key, + transformed_key = nvme_auth_transform_key(ctrl->ctrl_key, ctrl->opts->subsysnqn); - if (IS_ERR(ctrl_response)) { - ret = PTR_ERR(ctrl_response); + if (IS_ERR(transformed_key)) { + ret = PTR_ERR(transformed_key); return ret; } ret = crypto_shash_setkey(chap->shash_tfm, - ctrl_response, ctrl->ctrl_key->len); + transformed_key->key, transformed_key->len); if (ret) { dev_warn(ctrl->device, "qid %d: failed to set key, error %d\n", chap->qid, ret); @@ -586,7 +586,7 @@ static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl, out: if (challenge != chap->c2) kfree(challenge); - kfree(ctrl_response); + nvme_auth_free_key(transformed_key); return ret; } @@ -648,8 +648,8 @@ static int nvme_auth_dhchap_exponential(struct nvme_ctrl *ctrl, static void nvme_auth_reset_dhchap(struct nvme_dhchap_queue_context *chap) { - kfree_sensitive(chap->host_response); - chap->host_response = NULL; + nvme_auth_free_key(chap->transformed_key); + chap->transformed_key = NULL; kfree_sensitive(chap->host_key); chap->host_key = NULL; chap->host_key_len = 0; diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c index 4dcddcf95279..3ddbc3880cac 100644 --- a/drivers/nvme/target/auth.c +++ b/drivers/nvme/target/auth.c @@ -267,7 +267,8 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, struct shash_desc *shash; struct nvmet_ctrl *ctrl = req->sq->ctrl; const char *hash_name; - u8 *challenge = req->sq->dhchap_c1, *host_response; + u8 *challenge = req->sq->dhchap_c1; + struct nvme_dhchap_key *transformed_key; u8 buf[4]; int ret; @@ -291,14 +292,15 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, goto out_free_tfm; } - host_response = nvme_auth_transform_key(ctrl->host_key, ctrl->hostnqn); - if (IS_ERR(host_response)) { - ret = PTR_ERR(host_response); + transformed_key = nvme_auth_transform_key(ctrl->host_key, + ctrl->hostnqn); + if (IS_ERR(transformed_key)) { + ret = PTR_ERR(transformed_key); goto out_free_tfm; } - ret = crypto_shash_setkey(shash_tfm, host_response, - ctrl->host_key->len); + ret = crypto_shash_setkey(shash_tfm, transformed_key->key, + transformed_key->len); if (ret) goto out_free_response; @@ -365,7 +367,7 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response, kfree(challenge); kfree(shash); out_free_response: - kfree_sensitive(host_response); + nvme_auth_free_key(transformed_key); out_free_tfm: crypto_free_shash(shash_tfm); return 0; @@ -378,7 +380,8 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, struct shash_desc *shash; struct nvmet_ctrl *ctrl = req->sq->ctrl; const char *hash_name; - u8 *challenge = req->sq->dhchap_c2, *ctrl_response; + u8 *challenge = req->sq->dhchap_c2; + struct nvme_dhchap_key *transformed_key; u8 buf[4]; int ret; @@ -402,15 +405,15 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, goto out_free_tfm; } - ctrl_response = nvme_auth_transform_key(ctrl->ctrl_key, + transformed_key = nvme_auth_transform_key(ctrl->ctrl_key, ctrl->subsysnqn); - if (IS_ERR(ctrl_response)) { - ret = PTR_ERR(ctrl_response); + if (IS_ERR(transformed_key)) { + ret = PTR_ERR(transformed_key); goto out_free_tfm; } - ret = crypto_shash_setkey(shash_tfm, ctrl_response, - ctrl->ctrl_key->len); + ret = crypto_shash_setkey(shash_tfm, transformed_key->key, + transformed_key->len); if (ret) goto out_free_response; @@ -474,7 +477,7 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response, kfree(challenge); kfree(shash); out_free_response: - kfree_sensitive(ctrl_response); + nvme_auth_free_key(transformed_key); out_free_tfm: crypto_free_shash(shash_tfm); return 0; diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h index a5ae9abe1ef6..c1d0bc5d9624 100644 --- a/include/linux/nvme-auth.h +++ b/include/linux/nvme-auth.h @@ -29,7 +29,8 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, u8 key_hash); void nvme_auth_free_key(struct nvme_dhchap_key *key); struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash); -u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn); +struct nvme_dhchap_key *nvme_auth_transform_key( + struct nvme_dhchap_key *key, char *nqn); int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key); int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len, u8 *challenge, u8 *aug, size_t hlen); From patchwork Tue Oct 17 10:52:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark O'Donovan X-Patchwork-Id: 154065 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4045960vqb; Tue, 17 Oct 2023 03:53:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+o/HuIP23eCejIkm0DAZwdFJnRKv7lh17dzuLUwBiUSz5n24QJsjVTHa2us9jBhxcZR7T X-Received: by 2002:a17:90b:3510:b0:27d:6268:b75c with SMTP id ls16-20020a17090b351000b0027d6268b75cmr1899815pjb.4.1697540024462; Tue, 17 Oct 2023 03:53:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697540024; cv=none; d=google.com; s=arc-20160816; b=D2z3H8KtMqr1YFqVWf50sO4H9dfKz6r+khkUeVGXBzlwEjUGLdCyelC3kD6jjhspu0 1J2NWsLEo64F/oMZqbNQ0mK7Xrh37iCcvJ3IZfauzm5l1BejqCIQRFVfJ1fDpcGkuxHu OX/WZnf7Li1sFg2mqoOSSjBN+FrcEdSQ5z4umNCtoDX2YlxGeOjhIMG92qR5q8ops0H9 06EpcR7Yc+GoqII7BuntDzGOe1nQEUe2ljMEA22/nZtY4LvPNydntrP889/XbPAAQEHD zLYDdcRdvDzH7OXMI2u82DaO3luDyvK+sq9UELOGptoqeqrYEg/ZxykfK8qViaatFd3Z UwNw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MgYSQUsaCKTBOjvnSrqGBHDLXVdDU+83yjZTXDijhpU=; fh=OK6NlIuxAObXnbB+YsmCJIFQ9spAlQ71xiCElZ04Vio=; b=1BL27kN1Exv6Nb9fz9EL4ledeyKMMwWySbCq8i2cQhXZPWvgbKnyhLryC/0T2drAPP FTICUf1UAB6+DT+0cfdx1NoOgH+Tl0Eg3AyXv2dYbfQkISQtt62BatwFXi/NoxfuGgOl 03+1mN3NGQ+IVRF82mfPyBvOxV+TP92KwQhW5mzfq9GeYYlnEV7stNBXFMgrGJDiVb2F Aqz6qGv9+0UTjebv5Lpx0NC2CsCxiIEqZFY32Cmw5c5FGUZj63JHCGwJqzu3JaWx9aCQ JnV246fyy1CMDlmAMZ80TW1ivtJymiK3CImQaJi+lcrf/UEneiDnLFSXSkPFF92nAgad 4u7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=pEcE4rLH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id v23-20020a17090ae99700b0027740192bc9si1460406pjy.152.2023.10.17.03.53.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 03:53:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=pEcE4rLH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 9A602802F548; Tue, 17 Oct 2023 03:53:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343747AbjJQKxT (ORCPT + 19 others); Tue, 17 Oct 2023 06:53:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343680AbjJQKxN (ORCPT ); Tue, 17 Oct 2023 06:53:13 -0400 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 237FCA2 for ; Tue, 17 Oct 2023 03:53:12 -0700 (PDT) Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id BD1AB24002B for ; Tue, 17 Oct 2023 12:53:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1697539990; bh=QzwVfBdifB51edMzB/+yD4kt6tOaLkGIgcIPJIModxs=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version: Content-Transfer-Encoding:From; b=pEcE4rLHEDQ1ldQlDn9UAH6zsBnG37g3Ze2AQBI6PpnTYdFEeardtvkqkUL9v3pJi cM80Zgch2X8y0Hd/PB/p7+JBH82s+UAXfLJeH1SWXt8JlprhonIoz+Osjy5IuDE37k 3mIz2sIZJgXuHDJ5ajybp0zgEjznvQqt+jiufTsH/kLdP7N1dmNhQMEKwN05h9p2+9 Z1TybwjxgdK+RS8R3tepSpjvcE/seF9JMvYg0Wp7faQxgWAqLOqGmv0eegutk6lwH/ C2oFqxK6ZD94lpdwOdvbPq/O4xNgfo4hpgPkIUJj0S+17WKP04HeSuE9GDqR2QfhxY vS78FKCqaVBdA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4S8rRn34mlz6tvp; Tue, 17 Oct 2023 12:53:09 +0200 (CEST) From: Mark O'Donovan To: linux-kernel@vger.kernel.org Cc: linux-nvme@lists.infradead.org, sagi@grimberg.me, hch@lst.de, axboe@kernel.dk, kbusch@kernel.org, hare@suse.de, Mark O'Donovan , Akash Appaiah Subject: [PATCH v4 3/3] nvme-auth: allow mixing of secret and hash lengths Date: Tue, 17 Oct 2023 10:52:51 +0000 Message-Id: <20231017105251.3274652-4-shiftee@posteo.net> In-Reply-To: <20231017105251.3274652-1-shiftee@posteo.net> References: <20231017105251.3274652-1-shiftee@posteo.net> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 17 Oct 2023 03:53:42 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779999728918965557 X-GMAIL-MSGID: 1779999728918965557 We can now use any of the secret transformation hashes with a secret, regardless of the secret size. e.g. a 32 byte key with the SHA-512(64 byte) hash. The example secret from the spec should now be permitted with any of the following: DHHC-1:00:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n: DHHC-1:01:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n: DHHC-1:02:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n: DHHC-1:03:ia6zGodOr4SEG0Zzaw398rpY0wqipUWj4jWjUh4HWUz6aQ2n: Note: Secrets are still restricted to 32,48 or 64 bits. Co-developed-by: Akash Appaiah Signed-off-by: Akash Appaiah Signed-off-by: Mark O'Donovan Reviewed-by: Hannes Reinecke --- drivers/nvme/common/auth.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c index f9e7ed8e729d..ddfcc3889fec 100644 --- a/drivers/nvme/common/auth.c +++ b/drivers/nvme/common/auth.c @@ -190,14 +190,6 @@ struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret, goto out_free_secret; } - if (key_hash > 0 && - (key_len - 4) != nvme_auth_hmac_hash_len(key_hash)) { - pr_err("Mismatched key len %d for %s\n", key_len, - nvme_auth_hmac_name(key_hash)); - ret = -EINVAL; - goto out_free_secret; - } - /* The last four bytes is the CRC in little-endian format */ key_len -= 4; /*