ALT Linux kernel packages development
 help / color / mirror / Atom feed
From: Alexey Shabalin <shaba@altlinux.org>
To: devel-kernel@altlinux.org
Subject: [d-kernel] [PATCH v2 6.12/sisyphus 10/29] crypto/krb5: Implement the Kerberos5 rfc3961 get_mic and verify_mic
Date: Wed, 18 Nov 2026 17:49:16 +0300
Message-ID: <20261118144935.3872281-11-shaba@altlinux.org> (raw)
In-Reply-To: <20261118144935.3872281-1-shaba@altlinux.org>

From: David Howells <dhowells@redhat.com>

Add functions that sign and verify a message according to rfc3961 sec 5.4,
using Kc to generate a checksum and insert it into the MIC field in the
skbuff in the sign phase then checksum the data and compare it to the MIC
in the verify phase.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Herbert Xu <herbert@gondor.apana.org.au>
cc: "David S. Miller" <davem@davemloft.net>
cc: Chuck Lever <chuck.lever@oracle.com>
cc: Marc Dionne <marc.dionne@auristor.com>
cc: Eric Dumazet <edumazet@google.com>
cc: Jakub Kicinski <kuba@kernel.org>
cc: Paolo Abeni <pabeni@redhat.com>
cc: Simon Horman <horms@kernel.org>
cc: linux-afs@lists.infradead.org
cc: linux-nfs@vger.kernel.org
cc: linux-crypto@vger.kernel.org
cc: netdev@vger.kernel.org
(cherry picked from commit 348f5669d1f6c1c210b9017ebb8b82282eca6f25)
Signed-off-by: Alexey Shabalin <shaba@altlinux.org>
---
 crypto/krb5/internal.h           |  10 +++
 crypto/krb5/rfc3961_simplified.c | 130 +++++++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)

diff --git a/crypto/krb5/internal.h b/crypto/krb5/internal.h
index c8deb112b604..07a47ddf3ea9 100644
--- a/crypto/krb5/internal.h
+++ b/crypto/krb5/internal.h
@@ -169,3 +169,13 @@ int krb5_aead_decrypt(const struct krb5_enctype *krb5,
 		      struct crypto_aead *aead,
 		      struct scatterlist *sg, unsigned int nr_sg,
 		      size_t *_offset, size_t *_len);
+ssize_t rfc3961_get_mic(const struct krb5_enctype *krb5,
+			struct crypto_shash *shash,
+			const struct krb5_buffer *metadata,
+			struct scatterlist *sg, unsigned int nr_sg, size_t sg_len,
+			size_t data_offset, size_t data_len);
+int rfc3961_verify_mic(const struct krb5_enctype *krb5,
+		       struct crypto_shash *shash,
+		       const struct krb5_buffer *metadata,
+		       struct scatterlist *sg, unsigned int nr_sg,
+		       size_t *_offset, size_t *_len);
diff --git a/crypto/krb5/rfc3961_simplified.c b/crypto/krb5/rfc3961_simplified.c
index 58323ce5838a..c1dcb0dd3a00 100644
--- a/crypto/krb5/rfc3961_simplified.c
+++ b/crypto/krb5/rfc3961_simplified.c
@@ -653,6 +653,134 @@ int krb5_aead_decrypt(const struct krb5_enctype *krb5,
 	return ret;
 }
 
+/*
+ * Generate a checksum over some metadata and part of an skbuff and insert the
+ * MIC into the skbuff immediately prior to the data.
+ */
+ssize_t rfc3961_get_mic(const struct krb5_enctype *krb5,
+			struct crypto_shash *shash,
+			const struct krb5_buffer *metadata,
+			struct scatterlist *sg, unsigned int nr_sg, size_t sg_len,
+			size_t data_offset, size_t data_len)
+{
+	struct shash_desc *desc;
+	ssize_t ret, done;
+	size_t bsize;
+	void *buffer, *digest;
+
+	if (WARN_ON(data_offset != krb5->cksum_len))
+		return -EMSGSIZE;
+
+	bsize = krb5_shash_size(shash) +
+		krb5_digest_size(shash);
+	buffer = kzalloc(bsize, GFP_NOFS);
+	if (!buffer)
+		return -ENOMEM;
+
+	/* Calculate the MIC with key Kc and store it into the skb */
+	desc = buffer;
+	desc->tfm = shash;
+	ret = crypto_shash_init(desc);
+	if (ret < 0)
+		goto error;
+
+	if (metadata) {
+		ret = crypto_shash_update(desc, metadata->data, metadata->len);
+		if (ret < 0)
+			goto error;
+	}
+
+	ret = crypto_shash_update_sg(desc, sg, data_offset, data_len);
+	if (ret < 0)
+		goto error;
+
+	digest = buffer + krb5_shash_size(shash);
+	ret = crypto_shash_final(desc, digest);
+	if (ret < 0)
+		goto error;
+
+	ret = -EFAULT;
+	done = sg_pcopy_from_buffer(sg, nr_sg, digest, krb5->cksum_len,
+				    data_offset - krb5->cksum_len);
+	if (done != krb5->cksum_len)
+		goto error;
+
+	ret = krb5->cksum_len + data_len;
+
+error:
+	kfree_sensitive(buffer);
+	return ret;
+}
+
+/*
+ * Check the MIC on a region of an skbuff.  The offset and length are updated
+ * to reflect the actual content of the secure region.
+ */
+int rfc3961_verify_mic(const struct krb5_enctype *krb5,
+		       struct crypto_shash *shash,
+		       const struct krb5_buffer *metadata,
+		       struct scatterlist *sg, unsigned int nr_sg,
+		       size_t *_offset, size_t *_len)
+{
+	struct shash_desc *desc;
+	ssize_t done;
+	size_t bsize, data_offset, data_len, offset = *_offset, len = *_len;
+	void *buffer = NULL;
+	int ret;
+	u8 *cksum, *cksum2;
+
+	if (len < krb5->cksum_len)
+		return -EPROTO;
+	data_offset = offset + krb5->cksum_len;
+	data_len = len - krb5->cksum_len;
+
+	bsize = krb5_shash_size(shash) +
+		krb5_digest_size(shash) * 2;
+	buffer = kzalloc(bsize, GFP_NOFS);
+	if (!buffer)
+		return -ENOMEM;
+
+	cksum = buffer +
+		krb5_shash_size(shash);
+	cksum2 = buffer +
+		krb5_shash_size(shash) +
+		krb5_digest_size(shash);
+
+	/* Calculate the MIC */
+	desc = buffer;
+	desc->tfm = shash;
+	ret = crypto_shash_init(desc);
+	if (ret < 0)
+		goto error;
+
+	if (metadata) {
+		ret = crypto_shash_update(desc, metadata->data, metadata->len);
+		if (ret < 0)
+			goto error;
+	}
+
+	crypto_shash_update_sg(desc, sg, data_offset, data_len);
+	crypto_shash_final(desc, cksum);
+
+	ret = -EFAULT;
+	done = sg_pcopy_to_buffer(sg, nr_sg, cksum2, krb5->cksum_len, offset);
+	if (done != krb5->cksum_len)
+		goto error;
+
+	if (memcmp(cksum, cksum2, krb5->cksum_len) != 0) {
+		ret = -EBADMSG;
+		goto error;
+	}
+
+	*_offset += krb5->cksum_len;
+	*_len -= krb5->cksum_len;
+	ret = 0;
+
+error:
+	kfree_sensitive(buffer);
+	return ret;
+}
+
 const struct krb5_crypto_profile rfc3961_simplified_profile = {
 	.calc_PRF		= rfc3961_calc_PRF,
 	.calc_Kc		= rfc3961_calc_DK,
@@ -664,4 +792,6 @@ const struct krb5_crypto_profile rfc3961_simplified_profile = {
 	.load_checksum_key	= rfc3961_load_checksum_key,
 	.encrypt		= krb5_aead_encrypt,
 	.decrypt		= krb5_aead_decrypt,
+	.get_mic		= rfc3961_get_mic,
+	.verify_mic		= rfc3961_verify_mic,
 };
-- 
2.50.1



  parent reply	other threads:[~2026-11-18 14:49 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 14:49 [d-kernel] [PATCH v2 6.12/sisyphus 0/29] libceph: add support for AES256KRB5 (aes256k) cephx keys Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 01/29] crypto/krb5: Add some constants out of sunrpc headers Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 02/29] crypto/krb5: Implement Kerberos crypto core Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 03/29] crypto/krb5: Add an API to query the layout of the crypto section Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 04/29] crypto/krb5: Add an API to alloc and prepare a crypto object Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 05/29] crypto/krb5: Add an API to perform requests Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 06/29] crypto/krb5: Provide infrastructure and key derivation Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 07/29] crypto/krb5: Implement the Kerberos5 rfc3961 " Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 08/29] crypto/krb5: Provide RFC3961 setkey packaging functions Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 09/29] crypto/krb5: Implement the Kerberos5 rfc3961 encrypt and decrypt functions Alexey Shabalin
2026-11-18 14:49 ` Alexey Shabalin [this message]
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 11/29] crypto/krb5: Implement the AES enctypes from rfc3962 Alexey Shabalin
2026-11-20 16:45   ` Ivan A. Melnikov
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 12/29] crypto/krb5: Implement the AES enctypes from rfc8009 Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 13/29] crypto/krb5: Implement the Camellia enctypes from rfc6803 Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 14/29] crypto/krb5: Implement crypto self-testing Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 15/29] lib/scatterlist: Add SG_MITER_LOCAL and use it Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 16/29] crypto: krb5 - Use SG miter instead of doing it by hand Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 17/29] crypto/krb5: Fix change to use SG miter to use offset Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 18/29] crypto: krb5 - Fix memory leak in krb5_test_one_prf() Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 19/29] crypto/krb5: Add API Documentation Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 20/29] crypto/krb5, rxrpc: Fix lack of pre-decrypt/pre-verify length checks Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 21/29] crypto: krb5 - filter out async aead implementations at alloc Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 22/29] crypto: krb5 - use kfree_sensitive() for derived key buffers Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 23/29] libceph: Rename hmac_sha256() to ceph_hmac_sha256() Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 24/29] libceph: generalize ceph_x_encrypt_offset() and ceph_x_encrypt_buflen() Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 25/29] libceph: Remove unused ceph_crypto_key_encode Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 26/29] libceph: introduce ceph_crypto_key_prepare() Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 27/29] libceph: add support for CEPH_CRYPTO_AES256KRB5 Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 28/29] libceph: adapt ceph_x_challenge_blob hashing and msgr1 message signing Alexey Shabalin
2026-11-18 14:49 ` [d-kernel] [PATCH v2 6.12/sisyphus 29/29] config: enable CONFIG_CRYPTO_KRB5 Alexey Shabalin

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=20261118144935.3872281-11-shaba@altlinux.org \
    --to=shaba@altlinux.org \
    --cc=devel-kernel@altlinux.org \
    --cc=devel-kernel@lists.altlinux.org \
    /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

ALT Linux kernel packages development

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/devel-kernel/0 devel-kernel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 devel-kernel devel-kernel/ http://lore.altlinux.org/devel-kernel \
		devel-kernel@altlinux.org devel-kernel@altlinux.ru devel-kernel@altlinux.com
	public-inbox-index devel-kernel

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.devel-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git