From: Alexey Shabalin <shaba@altlinux.org>
To: devel-kernel@altlinux.org
Subject: [d-kernel] [PATCH v2 6.18/sisyphus 1/5] libceph: generalize ceph_x_encrypt_offset() and ceph_x_encrypt_buflen()
Date: Wed, 18 Nov 2026 17:42:36 +0300
Message-ID: <20261118144240.3865115-2-shaba@altlinux.org> (raw)
In-Reply-To: <20261118144240.3865115-1-shaba@altlinux.org>
From: Ilya Dryomov <idryomov@gmail.com>
- introduce the notion of a data offset for ceph_x_encrypt_offset()
to allow for e.g. confounder to be prepended before the encryption
header in the future. For CEPH_CRYPTO_AES, the data offset is 0
(i.e. nothing is prepended).
- adjust ceph_x_encrypt_buflen() accordingly and make it account for
PKCS#7 padding that is used by CEPH_CRYPTO_AES precisely instead of
just always adding 16.
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
(cherry picked from commit 0ee8bccf7396d50726c9c8dd3135fb64a9fe8426)
Signed-off-by: Alexey Shabalin <shaba@altlinux.org>
---
net/ceph/auth_x.c | 46 ++++++++++++++++++++++++++++++----------------
net/ceph/crypto.c | 25 +++++++++++++++++++++++++
net/ceph/crypto.h | 2 ++
3 files changed, 57 insertions(+), 16 deletions(-)
diff --git a/net/ceph/auth_x.c b/net/ceph/auth_x.c
index f805a813308f..5c32045c390d 100644
--- a/net/ceph/auth_x.c
+++ b/net/ceph/auth_x.c
@@ -44,23 +44,35 @@ static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
return !!need;
}
-static int ceph_x_encrypt_offset(void)
+static int __ceph_x_encrypt_offset(const struct ceph_crypto_key *key)
{
- return sizeof(u32) + sizeof(struct ceph_x_encrypt_header);
+ return ceph_crypt_data_offset(key) +
+ sizeof(struct ceph_x_encrypt_header);
}
-static int ceph_x_encrypt_buflen(int ilen)
+static int ceph_x_encrypt_offset(const struct ceph_crypto_key *key)
{
- return ceph_x_encrypt_offset() + ilen + 16;
+ return sizeof(u32) + __ceph_x_encrypt_offset(key);
+}
+
+/*
+ * AES: ciphertext_len | hdr | data... | padding
+ */
+static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key,
+ int data_len)
+{
+ int encrypt_len = sizeof(struct ceph_x_encrypt_header) + data_len;
+ return sizeof(u32) + ceph_crypt_buflen(key, encrypt_len);
}
static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
int buf_len, int plaintext_len)
{
- struct ceph_x_encrypt_header *hdr = buf + sizeof(u32);
+ struct ceph_x_encrypt_header *hdr;
int ciphertext_len;
int ret;
+ hdr = buf + sizeof(u32) + ceph_crypt_data_offset(secret);
hdr->struct_v = 1;
hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
@@ -77,7 +89,7 @@ static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
int ciphertext_len)
{
- struct ceph_x_encrypt_header *hdr = p;
+ struct ceph_x_encrypt_header *hdr;
int plaintext_len;
int ret;
@@ -86,7 +98,8 @@ static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
if (ret)
return ret;
- if (plaintext_len < sizeof(*hdr)) {
+ hdr = p + ceph_crypt_data_offset(secret);
+ if (plaintext_len < ceph_crypt_data_offset(secret) + sizeof(*hdr)) {
pr_err("%s plaintext too small %d\n", __func__, plaintext_len);
return -EINVAL;
}
@@ -198,7 +211,7 @@ static int process_one_ticket(struct ceph_auth_client *ac,
}
/* blob for me */
- dp = *p + ceph_x_encrypt_offset();
+ dp = *p + ceph_x_encrypt_offset(secret);
ret = ceph_x_decrypt(secret, p, end);
if (ret < 0)
goto out;
@@ -225,7 +238,7 @@ static int process_one_ticket(struct ceph_auth_client *ac,
ceph_decode_8_safe(p, end, is_enc, bad);
if (is_enc) {
/* encrypted */
- tp = *p + ceph_x_encrypt_offset();
+ tp = *p + ceph_x_encrypt_offset(&th->session_key);
ret = ceph_x_decrypt(&th->session_key, p, end);
if (ret < 0)
goto out;
@@ -317,7 +330,7 @@ static int encrypt_authorizer(struct ceph_x_authorizer *au,
p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
end = au->buf->vec.iov_base + au->buf->vec.iov_len;
- msg_b = p + ceph_x_encrypt_offset();
+ msg_b = p + ceph_x_encrypt_offset(&au->session_key);
msg_b->struct_v = 2;
msg_b->nonce = cpu_to_le64(au->nonce);
if (server_challenge) {
@@ -373,7 +386,7 @@ static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
goto out_au;
maxlen = sizeof(*msg_a) + ticket_blob_len +
- ceph_x_encrypt_buflen(sizeof(*msg_b));
+ ceph_x_encrypt_buflen(&au->session_key, sizeof(*msg_b));
dout(" need len %d\n", maxlen);
if (au->buf && au->buf->alloc_len < maxlen) {
ceph_buffer_put(au->buf);
@@ -512,7 +525,7 @@ static int ceph_x_build_request(struct ceph_auth_client *ac,
struct ceph_x_authenticate *auth = (void *)(head + 1);
void *enc_buf = xi->auth_authorizer.enc_buf;
struct ceph_x_challenge_blob *blob = enc_buf +
- ceph_x_encrypt_offset();
+ ceph_x_encrypt_offset(&xi->secret);
u64 *u;
p = auth + 1;
@@ -639,7 +652,7 @@ static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id,
ceph_decode_need(p, end, len, e_inval);
dout("%s connection secret blob len %d\n", __func__, len);
if (len > 0) {
- dp = *p + ceph_x_encrypt_offset();
+ dp = *p + ceph_x_encrypt_offset(&th->session_key);
ret = ceph_x_decrypt(&th->session_key, p, *p + len);
if (ret < 0)
return ret;
@@ -816,7 +829,7 @@ static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret,
return ret;
dout("%s decrypted %d bytes\n", __func__, ret);
- dp = challenge + sizeof(struct ceph_x_encrypt_header);
+ dp = challenge + __ceph_x_encrypt_offset(secret);
dend = dp + ret;
ceph_decode_skip_8(&dp, dend, e_inval); /* struct_v */
@@ -863,7 +876,7 @@ static int decrypt_authorizer_reply(struct ceph_crypto_key *secret,
u8 struct_v;
int ret;
- dp = *p + ceph_x_encrypt_offset();
+ dp = *p + ceph_x_encrypt_offset(secret);
ret = ceph_x_decrypt(secret, p, end);
if (ret < 0)
return ret;
@@ -986,7 +999,8 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
__le32 front_crc;
__le32 middle_crc;
__le32 data_crc;
- } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
+ } __packed *sigblock = enc_buf +
+ ceph_x_encrypt_offset(&au->session_key);
sigblock->len = cpu_to_le32(4*sizeof(u32));
sigblock->header_crc = msg->hdr.crc;
diff --git a/net/ceph/crypto.c b/net/ceph/crypto.c
index 5601732cf4fa..2b98daffe9af 100644
--- a/net/ceph/crypto.c
+++ b/net/ceph/crypto.c
@@ -285,6 +285,31 @@ int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
}
}
+int ceph_crypt_data_offset(const struct ceph_crypto_key *key)
+{
+ switch (key->type) {
+ case CEPH_CRYPTO_NONE:
+ case CEPH_CRYPTO_AES:
+ return 0;
+ default:
+ BUG();
+ }
+}
+
+int ceph_crypt_buflen(const struct ceph_crypto_key *key, int data_len)
+{
+ switch (key->type) {
+ case CEPH_CRYPTO_NONE:
+ return data_len;
+ case CEPH_CRYPTO_AES:
+ /* PKCS#7 padding at the end */
+ return data_len + AES_BLOCK_SIZE -
+ (data_len & (AES_BLOCK_SIZE - 1));
+ default:
+ BUG();
+ }
+}
+
static int ceph_key_preparse(struct key_preparsed_payload *prep)
{
struct ceph_crypto_key *ckey;
diff --git a/net/ceph/crypto.h b/net/ceph/crypto.h
index a20bad6d1e96..736ec6d2fbcb 100644
--- a/net/ceph/crypto.h
+++ b/net/ceph/crypto.h
@@ -28,6 +28,8 @@ void ceph_crypto_key_destroy(struct ceph_crypto_key *key);
/* crypto.c */
int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
void *buf, int buf_len, int in_len, int *pout_len);
+int ceph_crypt_data_offset(const struct ceph_crypto_key *key);
+int ceph_crypt_buflen(const struct ceph_crypto_key *key, int data_len);
int ceph_crypto_init(void);
void ceph_crypto_shutdown(void);
--
2.50.1
next prev parent reply other threads:[~2026-11-18 14:42 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 14:42 [d-kernel] [PATCH v2 6.18/sisyphus 0/5] libceph: add support for AES256KRB5 (aes256k) cephx keys Alexey Shabalin
2026-11-18 14:42 ` Alexey Shabalin [this message]
2026-11-18 14:42 ` [d-kernel] [PATCH v2 6.18/sisyphus 2/5] libceph: introduce ceph_crypto_key_prepare() Alexey Shabalin
2026-11-18 14:42 ` [d-kernel] [PATCH v2 6.18/sisyphus 3/5] libceph: add support for CEPH_CRYPTO_AES256KRB5 Alexey Shabalin
2026-11-18 14:42 ` [d-kernel] [PATCH v2 6.18/sisyphus 4/5] libceph: adapt ceph_x_challenge_blob hashing and msgr1 message signing Alexey Shabalin
2026-11-18 14:42 ` [d-kernel] [PATCH v2 6.18/sisyphus 5/5] config: enable CONFIG_CRYPTO_KRB5 Alexey Shabalin
2026-11-20 12:59 ` [d-kernel] [PATCH v2 6.18/sisyphus 0/5] libceph: add support for AES256KRB5 (aes256k) cephx keys Ivan A. Melnikov
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=20261118144240.3865115-2-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