mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Steve French <sfrench@samba.org>
Cc: David Howells <dhowells@redhat.com>,
	Paulo Alcantara <pc@manguebit.org>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Tom Talpey <tom@talpey.com>,
	Wang Zhaolong <wangzhaolong@huaweicloud.com>,
	Stefan Metzmacher <metze@samba.org>,
	Mina Almasry <almasrymina@google.com>,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
	netfs@lists.linux.dev, linux-fsdevel@vger.kernel.org
Subject: [RFC PATCH 14/31] cifs: Split crypt_message() into encrypt and decrypt variants
Date: Wed,  6 Aug 2025 21:36:35 +0100	[thread overview]
Message-ID: <20250806203705.2560493-15-dhowells@redhat.com> (raw)
In-Reply-To: <20250806203705.2560493-1-dhowells@redhat.com>

Split crypt_message() into encrypt and decrypt variants so that the encrypt
variant can be substantially changed.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.org>
cc: Shyam Prasad N <sprasad@microsoft.com>
cc: Tom Talpey <tom@talpey.com>
cc: linux-cifs@vger.kernel.org
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
---
 fs/smb/client/smb2ops.c | 100 ++++++++++++++++++++++++++++++++++------
 1 file changed, 85 insertions(+), 15 deletions(-)

diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 7b714e50f681..0ad4a2a012a0 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -4297,16 +4297,17 @@ smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
 
 	return -EAGAIN;
 }
+
 /*
- * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
+ * Encrypt @rqst message. @rqst[0] has the following format:
  * iov[0]   - transform header (associate data),
  * iov[1-N] - SMB2 header and pages - data to encrypt.
  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
  * untouched.
  */
 static int
-crypt_message(struct TCP_Server_Info *server, int num_rqst,
-	      struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
+encrypt_message(struct TCP_Server_Info *server, int num_rqst,
+		struct smb_rqst *rqst, struct crypto_aead *tfm)
 {
 	struct smb2_transform_hdr *tr_hdr =
 		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
@@ -4321,10 +4322,10 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	void *creq;
 	size_t sensitive_size;
 
-	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
+	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), 1, key);
 	if (rc) {
-		cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
-			 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
+		cifs_server_dbg(FYI, "%s: Could not get encryption key. sid: 0x%llx\n",
+				__func__, le64_to_cpu(tr_hdr->SessionId));
 		return rc;
 	}
 
@@ -4350,11 +4351,6 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	if (IS_ERR(creq))
 		return PTR_ERR(creq);
 
-	if (!enc) {
-		memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
-		crypt_len += SMB2_SIGNATURE_SIZE;
-	}
-
 	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
 	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
 		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
@@ -4367,15 +4363,89 @@ crypt_message(struct TCP_Server_Info *server, int num_rqst,
 	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
 	aead_request_set_ad(req, assoc_data_len);
 
-	rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
+	rc = crypto_aead_encrypt(req);
 
-	if (!rc && enc)
+	if (!rc)
 		memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
 
 	kvfree_sensitive(creq, sensitive_size);
 	return rc;
 }
 
+/*
+ * Decrypt @rqst message. @rqst[0] has the following format:
+ * iov[0]   - transform header (associate data),
+ * iov[1-N] - SMB2 header and pages - data to decrypt.
+ * On success return encrypted data in iov[1-N] and pages, leave iov[0]
+ * untouched.
+ */
+static int
+decrypt_message(struct TCP_Server_Info *server, int num_rqst,
+		struct smb_rqst *rqst, struct crypto_aead *tfm)
+{
+	struct smb2_transform_hdr *tr_hdr =
+		(struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
+	unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
+	int rc = 0;
+	struct scatterlist *sg;
+	u8 sign[SMB2_SIGNATURE_SIZE] = {};
+	u8 key[SMB3_ENC_DEC_KEY_SIZE];
+	struct aead_request *req;
+	u8 *iv;
+	unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
+	void *creq;
+	size_t sensitive_size;
+
+	rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), 0, key);
+	if (rc) {
+		cifs_server_dbg(FYI, "%s: Could not get decryption key. sid: 0x%llx\n",
+				__func__, le64_to_cpu(tr_hdr->SessionId));
+		return rc;
+	}
+
+	if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
+		(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
+	else
+		rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
+
+	if (rc) {
+		cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
+		return rc;
+	}
+
+	rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
+	if (rc) {
+		cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
+		return rc;
+	}
+
+	creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg,
+				 &sensitive_size);
+	if (IS_ERR(creq))
+		return PTR_ERR(creq);
+
+	memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
+	crypt_len += SMB2_SIGNATURE_SIZE;
+
+	if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
+	    (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
+		memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
+	else {
+		iv[0] = 3;
+		memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
+	}
+
+	aead_request_set_tfm(req, tfm);
+	aead_request_set_crypt(req, sg, sg, crypt_len, iv);
+	aead_request_set_ad(req, assoc_data_len);
+
+	rc = crypto_aead_decrypt(req);
+
+	kvfree_sensitive(creq, sensitive_size);
+	return rc;
+}
+
 /*
  * Clear a read buffer, discarding the folios which have the 1st mark set.
  */
@@ -4509,7 +4579,7 @@ smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
 	/* fill the 1st iov with a transform header */
 	fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
 
-	rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
+	rc = encrypt_message(server, num_rqst, new_rq, server->secmech.enc);
 	cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
 	if (rc)
 		goto err_free;
@@ -4571,7 +4641,7 @@ decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
 		tfm = server->secmech.dec;
 	}
 
-	rc = crypt_message(server, 1, &rqst, 0, tfm);
+	rc = decrypt_message(server, 1, &rqst, tfm);
 	cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
 
 	if (is_offloaded)


  parent reply	other threads:[~2025-08-06 20:38 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-06 20:36 [RFC PATCH 00/31] netfs: [WIP] Allow the use of MSG_SPLICE_PAGES and use netmem allocator David Howells
2025-08-06 20:36 ` [RFC PATCH 01/31] iov_iter: Move ITER_DISCARD and ITER_XARRAY iteration out-of-line David Howells
2025-08-06 20:36 ` [RFC PATCH 02/31] iov_iter: Add a segmented queue of bio_vec[] David Howells
2025-08-06 20:36 ` [RFC PATCH 03/31] netfs: Provide facility to alloc buffer in a bvecq David Howells
2025-08-06 20:36 ` [RFC PATCH 04/31] cifs, nls: Provide unicode size determination func David Howells
2025-08-06 20:36 ` [RFC PATCH 05/31] cifs: Introduce an ALIGN8() macro David Howells
2025-08-06 20:36 ` [RFC PATCH 06/31] cifs: Move the SMB1 transport code out of transport.c David Howells
2025-08-06 20:36 ` [RFC PATCH 07/31] cifs: Rename mid_q_entry to smb_message David Howells
2025-08-06 20:36 ` [RFC PATCH 08/31] cifs: Keep the CPU-endian command ID around David Howells
2025-08-06 20:36 ` [RFC PATCH 09/31] cifs: Rename SMB2_xxxx_HE to SMB2_xxxx David Howells
2025-08-06 20:36 ` [RFC PATCH 10/31] cifs: Make smb1's SendReceive() wrap cifs_send_recv() David Howells
2025-08-06 20:36 ` [RFC PATCH 11/31] cifs: Fix SMB1 to not require separate kvec for the rfc1002 header David Howells
2025-08-06 20:36 ` [RFC PATCH 12/31] cifs: Replace SendReceiveBlockingLock() with SendReceive() plus flags David Howells
2025-08-06 20:36 ` [RFC PATCH 13/31] cifs: Institute message managing struct David Howells
2025-08-06 20:36 ` David Howells [this message]
2025-08-06 20:36 ` [RFC PATCH 15/31] cifs: Use netfs_alloc/free_folioq_buffer() David Howells
2025-08-06 20:36 ` [RFC PATCH 16/31] cifs: Rewrite base TCP transmission David Howells
2025-08-07  5:40   ` Stefan Metzmacher
2025-08-07 10:12   ` David Howells
2025-08-07 10:14   ` David Howells
2025-08-06 20:36 ` [RFC PATCH 17/31] cifs: Rework smb2 decryption David Howells
2025-08-06 20:36 ` [RFC PATCH 18/31] cifs: Pass smb_message structs down into the transport layer David Howells
2025-08-08 14:21   ` Enzo Matsumiya
2025-08-06 20:36 ` [RFC PATCH 19/31] cifs: Clean up mid->callback_data and kill off mid->creator David Howells
2025-08-06 20:36 ` [RFC PATCH 20/31] cifs: Don't need state locking in smb2_get_mid_entry() David Howells
2025-08-06 20:36 ` [RFC PATCH 21/31] cifs: [DEBUG] smb_message refcounting David Howells
2025-08-06 20:36 ` [RFC PATCH 22/31] cifs: Add netmem allocation functions David Howells
2025-08-06 20:36 ` [RFC PATCH 23/31] cifs: Add more pieces to smb_message David Howells
2025-08-06 20:36 ` [RFC PATCH 24/31] cifs: Convert SMB2 Negotiate Protocol request David Howells
2025-08-08 14:44   ` Enzo Matsumiya
2025-08-08 15:10   ` David Howells
2025-08-06 20:36 ` [RFC PATCH 25/31] cifs: Convert SMB2 Session Setup request David Howells
2025-08-06 20:36 ` [RFC PATCH 26/31] cifs: Convert SMB2 Logoff request David Howells
2025-08-06 20:36 ` [RFC PATCH 27/31] cifs: Convert SMB2 Tree Connect request David Howells
2025-08-06 20:36 ` [RFC PATCH 28/31] cifs: Convert SMB2 Tree Disconnect request David Howells
2025-08-06 20:36 ` [RFC PATCH 29/31] cifs: Rearrange Create request subfuncs David Howells
2025-08-06 20:36 ` [RFC PATCH 30/31] cifs: Convert SMB2 Posix Mkdir request David Howells
2025-08-06 20:36 ` [RFC PATCH 31/31] cifs: Convert SMB2 Open request David Howells
2025-08-07  5:23 ` [RFC PATCH 00/31] netfs: [WIP] Allow the use of MSG_SPLICE_PAGES and use netmem allocator Stefan Metzmacher
2025-08-07  6:24 ` David Howells
2025-08-07  6:54   ` Stefan Metzmacher
2025-08-07  7:12   ` David Howells
2025-08-08 14:15 ` Enzo Matsumiya
2025-08-08 17:25 ` David Howells
2025-08-08 19:58   ` Enzo Matsumiya
2025-08-08 20:33   ` David Howells
2025-08-10 23:29   ` David Howells
2025-08-11 12:25     ` Enzo Matsumiya

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250806203705.2560493-15-dhowells@redhat.com \
    --to=dhowells@redhat.com \
    --cc=almasrymina@google.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=metze@samba.org \
    --cc=netfs@lists.linux.dev \
    --cc=pc@manguebit.org \
    --cc=sfrench@samba.org \
    --cc=sprasad@microsoft.com \
    --cc=tom@talpey.com \
    --cc=wangzhaolong@huaweicloud.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®