Skip to contentSkip to conversation

libceph: CEPH_CRYPTO_AES256KRB5 (--key-type aes256k) support

PATCH In review Ilya Dryomov 2 participants 12 messages Jan 28 – Jan 29 Ceph common code (libceph)

lore
Share & tools
lore

Ilya Dryomov

author maintainer
#0 lore raw

cover libceph: CEPH_CRYPTO_AES256KRB5 (--key-type aes256k) support

Hello,

This series adds support for the upcoming CephX key type that is based on Kerberos 5.

Thanks,

Ilya

Ilya Dryomov (5):
libceph: define and enforce CEPH_MAX_KEY_LEN
libceph: generalize ceph_x_encrypt_offset() and
ceph_x_encrypt_buflen()
libceph: introduce ceph_crypto_key_prepare()
libceph: add support for CEPH_CRYPTO_AES256KRB5
libceph: adapt ceph_x_challenge_blob hashing and msgr1 message signing

 include/linux/ceph/ceph_fs.h |   5 +-
 net/ceph/Kconfig             |   1 +
 net/ceph/auth_x.c            | 195 ++++++++++++++++++-------
 net/ceph/auth_x_protocol.h   |  38 +++++
 net/ceph/crypto.c            | 275 ++++++++++++++++++++++++++++-------
 net/ceph/crypto.h            |  21 ++-
 net/ceph/messenger_v2.c      |  16 +-
 7 files changed, 433 insertions(+), 118 deletions(-)
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 1/5 libceph: define and enforce CEPH_MAX_KEY_LEN

When decoding the key, verify that the key material would fit into a fixed-size buffer in process_auth_done() and generally has a sane length.

The new CEPH_MAX_KEY_LEN check replaces the existing check for a key with no key material which is a) not universal since CEPH_CRYPTO_NONE has to be excluded and b) doesn't provide much value since a smaller than needed key is just as invalid as no key -- this has to be handled elsewhere anyway.

---
 net/ceph/crypto.c       | 8 +++++---
 net/ceph/crypto.h       | 2 +-
 net/ceph/messenger_v2.c | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)
net/ceph/crypto.c +5 -3
index 01b2ce1e8fc0..5601732cf4fa 100644
@@ -37,9 +37,6 @@ static int set_secret(struct ceph_crypto_key *key, void *buf)
 		return -ENOTSUPP;
 	}
 
-	if (!key->len)
-		return -EINVAL;
-
 	key->key = kmemdup(buf, key->len, GFP_NOIO);
 	if (!key->key) {
 		ret = -ENOMEM;
@@ -83,6 +80,11 @@ int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
 	ceph_decode_copy(p, &key->created, sizeof(key->created));
 	key->len = ceph_decode_16(p);
 	ceph_decode_need(p, end, key->len, bad);
+	if (key->len > CEPH_MAX_KEY_LEN) {
+		pr_err("secret too big %d\n", key->len);
+		return -EINVAL;
+	}
+
 	ret = set_secret(key, *p);
 	memzero_explicit(*p, key->len);
 	*p += key->len;
net/ceph/crypto.h +1 -1
index 23de29fc613c..a20bad6d1e96 100644
@@ -5,7 +5,7 @@
 #include <linux/ceph/types.h>
 #include <linux/ceph/buffer.h>
 
-#define CEPH_KEY_LEN			16
+#define CEPH_MAX_KEY_LEN		16
 #define CEPH_MAX_CON_SECRET_LEN		64
 
 /*
net/ceph/messenger_v2.c +1 -1
index c9d50c0dcd33..31e042dc1b3f 100644
@@ -2360,7 +2360,7 @@ static int process_auth_reply_more(struct ceph_connection *con,
  */
 static int process_auth_done(struct ceph_connection *con, void *p, void *end)
 {
-	u8 session_key_buf[CEPH_KEY_LEN + 16];
+	u8 session_key_buf[CEPH_MAX_KEY_LEN + 16];
 	u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16];
 	u8 *session_key = PTR_ALIGN(&session_key_buf[0], 16);
 	u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16);
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH 1/5] libceph: define and enforce CEPH_MAX_KEY_LEN

Quoted 22 lines · Ilya Dryomov

When decoding the key, verify that the key material would fit into a fixed-size buffer in process_auth_done() and generally has a sane length.

The new CEPH_MAX_KEY_LEN check replaces the existing check for a key with no key material which is a) not universal since CEPH_CRYPTO_NONE has to be excluded and b) doesn't provide much value since a smaller than needed key is just as invalid as no key -- this has to be handled elsewhere anyway.

---
 net/ceph/crypto.c       | 8 +++++---
 net/ceph/crypto.h       | 2 +-
 net/ceph/messenger_v2.c | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)
net/ceph/crypto.c +0 -0
index 01b2ce1e8fc0..5601732cf4fa 100644

net/ceph/crypto.c static int set_secret(struct ceph_crypto_key *key, void *buf) · line 41

Quoted 5 lines
net/ceph/crypto.c +0 -2
@@ -37,9 +37,6 @@ static int set_secret(struct ceph_crypto_key *key, void *buf)
 		return -ENOTSUPP;
 	}
 
-	if (!key->len)
-		return -EINVAL;

Are you sure that this check is not necessary? What is the motivation that we can remove this check?

Could be key->len bigger than CEPH_MAX_KEY_LEN?

Quoted 21 lines
net/ceph/crypto.c +5 -1
-
 	key->key = kmemdup(buf, key->len, GFP_NOIO);
 	if (!key->key) {
 		ret = -ENOMEM;
@@ -83,6 +80,11 @@ int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
 	ceph_decode_copy(p, &key->created, sizeof(key->created));
 	key->len = ceph_decode_16(p);
 	ceph_decode_need(p, end, key->len, bad);
+	if (key->len > CEPH_MAX_KEY_LEN) {
+		pr_err("secret too big %d\n", key->len);
+		return -EINVAL;
+	}
+
 	ret = set_secret(key, *p);
 	memzero_explicit(*p, key->len);
 	*p += key->len;
net/ceph/crypto.h +0 -0
index 23de29fc613c..a20bad6d1e96 100644

net/ceph/crypto.h line 8

Quoted 5 lines
net/ceph/crypto.h +1 -1
@@ -5,7 +5,7 @@
 #include <linux/ceph/types.h>
 #include <linux/ceph/buffer.h>
 
-#define CEPH_KEY_LEN			16
+#define CEPH_MAX_KEY_LEN		16

Frankly speaking, I don't quite follow what was the point to change the constant name?

Thanks,
Slava.

Quoted 16 lines
net/ceph/crypto.h +0 -0
 #define CEPH_MAX_CON_SECRET_LEN		64
 
 /*
net/ceph/messenger_v2.c +1 -1
index c9d50c0dcd33..31e042dc1b3f 100644
@@ -2360,7 +2360,7 @@ static int process_auth_reply_more(struct ceph_connection *con,
  */
 static int process_auth_done(struct ceph_connection *con, void *p, void *end)
 {
-	u8 session_key_buf[CEPH_KEY_LEN + 16];
+	u8 session_key_buf[CEPH_MAX_KEY_LEN + 16];
 	u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16];
 	u8 *session_key = PTR_ALIGN(&session_key_buf[0], 16);
 	u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16);
author maintainer
#3 lore raw

Re: [PATCH 1/5] libceph: define and enforce CEPH_MAX_KEY_LEN

Quoted 27 lines · Viacheslav Dubeyko
Quoted 25 lines · Ilya Dryomov

When decoding the key, verify that the key material would fit into a fixed-size buffer in process_auth_done() and generally has a sane length.

The new CEPH_MAX_KEY_LEN check replaces the existing check for a key with no key material which is a) not universal since CEPH_CRYPTO_NONE has to be excluded and b) doesn't provide much value since a smaller than needed key is just as invalid as no key -- this has to be handled elsewhere anyway.

---
 net/ceph/crypto.c       | 8 +++++---
 net/ceph/crypto.h       | 2 +-
 net/ceph/messenger_v2.c | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)
net/ceph/crypto.c +0 -0
index 01b2ce1e8fc0..5601732cf4fa 100644
@@ -37,9 +37,6 @@ static int set_secret(struct ceph_crypto_key *key, void *buf)
              return -ENOTSUPP;
      }
 
Quoted 5 lines

net/ceph/crypto.c static int set_secret(struct ceph_crypto_key *key, void *buf) · line 41

Quoted 2 lines
net/ceph/crypto.c +0 -2
-     if (!key->len)
-             return -EINVAL;

Are you sure that this check is not necessary? What is the motivation that we can remove this check?

Hi Slava,

The motivation is explained in the patch description:

The new CEPH_MAX_KEY_LEN check replaces the existing check for a key with no key material which is a) not universal since CEPH_CRYPTO_NONE has to be excluded and b) doesn't provide much value since a smaller than needed key is just as invalid as no key -- this has to be handled elsewhere anyway.

The important point is (b): I don't see much sense in explicitly denying 0-byte keys but accepting e.g. 1-byte keys. Both will be rejected by crypto_sync_skcipher_setkey() later anyway.

Quoted 2 lines

Could be key->len bigger than CEPH_MAX_KEY_LEN?

Not if everything is working as expected. But in theory anything is possible because the key that we start with is essentially user input and therefore can easily be malformed -- hence this change.

Quoted 25 lines
Quoted 25 lines
net/ceph/crypto.c +5 -1
-
      key->key = kmemdup(buf, key->len, GFP_NOIO);
      if (!key->key) {
              ret = -ENOMEM;
@@ -83,6 +80,11 @@ int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end)
      ceph_decode_copy(p, &key->created, sizeof(key->created));
      key->len = ceph_decode_16(p);
      ceph_decode_need(p, end, key->len, bad);
+     if (key->len > CEPH_MAX_KEY_LEN) {
+             pr_err("secret too big %d\n", key->len);
+             return -EINVAL;
+     }
+
      ret = set_secret(key, *p);
      memzero_explicit(*p, key->len);
      *p += key->len;
net/ceph/crypto.h +0 -0
index 23de29fc613c..a20bad6d1e96 100644
@@ -5,7 +5,7 @@
 #include <linux/ceph/types.h>
 #include <linux/ceph/buffer.h>
 
Quoted 5 lines

net/ceph/crypto.h line 8

Quoted 2 lines
net/ceph/crypto.h +1 -1
-#define CEPH_KEY_LEN                 16
+#define CEPH_MAX_KEY_LEN             16

Frankly speaking, I don't quite follow what was the point to change the constant name?

CEPH_CRYPTO_AES256KRB5 is going to operate on 32-byte keys. And, irrespective of that, having MAX in the name communicates the intended usage better.

Thanks,

Ilya

author maintainer
#4 lore raw

PATCH 2/5 libceph: generalize ceph_x_encrypt_offset() and ceph_x_encrypt_buflen()

In reply to Ilya Dryomov

- 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.

---
 net/ceph/auth_x.c | 44 +++++++++++++++++++++++++++++---------------
 net/ceph/crypto.c | 25 +++++++++++++++++++++++++
 net/ceph/crypto.h |  2 ++
 3 files changed, 56 insertions(+), 15 deletions(-)
net/ceph/auth_x.c +29 -15
index a21c157daf7d..5d7245884f95 100644
@@ -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,6 +98,7 @@ static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
 	if (ret)
 		return ret;
 
+	hdr = p + ceph_crypt_data_offset(secret);
 	if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
 		pr_err("%s bad magic\n", __func__);
 		return -EINVAL;
@@ -193,7 +206,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;
@@ -220,7 +233,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;
@@ -312,7 +325,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) {
@@ -368,7 +381,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);
@@ -507,7 +520,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;
@@ -634,7 +647,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;
@@ -804,7 +817,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 */
@@ -851,7 +864,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;
@@ -974,7 +987,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;
net/ceph/crypto.c +25 -0
index 5601732cf4fa..2b98daffe9af 100644
@@ -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;
net/ceph/crypto.h +2 -0
index a20bad6d1e96..736ec6d2fbcb 100644
@@ -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);
 
Signature · 1 line
2.49.0
#5 lore raw

Re: [PATCH 2/5] libceph: generalize ceph_x_encrypt_offset() and ceph_x_encrypt_buflen()

Quoted 27 lines · Ilya Dryomov

- 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.

---
 net/ceph/auth_x.c | 44 +++++++++++++++++++++++++++++---------------
 net/ceph/crypto.c | 25 +++++++++++++++++++++++++
 net/ceph/crypto.h |  2 ++
 3 files changed, 56 insertions(+), 15 deletions(-)
net/ceph/auth_x.c +1 -1
index a21c157daf7d..5d7245884f95 100644
@@ -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)
 {

net/ceph/auth_x.c static int ceph_x_should_authenticate(struct ceph_auth_client *ac) · line 55

Quoted 10 lines
net/ceph/auth_x.c +4 -3
-	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);

Frankly speaking, it's hard to follow why sizeof(u32) is used. Maybe, it makes sense to introduce a named constant? This could make the code more easy to understand.

net/ceph/auth_x.c static int ceph_x_should_authenticate(struct ceph_auth_client *ac) · line 65

Quoted 10 lines
net/ceph/auth_x.c +10 -0
+}
+
+/*
+ * 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);

Ditto.

net/ceph/auth_x.c static int ceph_x_should_authenticate(struct ceph_auth_client *ac) · line 75

Quoted 11 lines
net/ceph/auth_x.c +2 -1
 }
 
 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);

Ditto.

Quoted 108 lines
net/ceph/auth_x.c +12 -10
 	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,6 +98,7 @@ static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
 	if (ret)
 		return ret;
 
+	hdr = p + ceph_crypt_data_offset(secret);
 	if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
 		pr_err("%s bad magic\n", __func__);
 		return -EINVAL;
@@ -193,7 +206,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;
@@ -220,7 +233,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;
@@ -312,7 +325,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) {
@@ -368,7 +381,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);
@@ -507,7 +520,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;
@@ -634,7 +647,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;
@@ -804,7 +817,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 */
@@ -851,7 +864,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;
@@ -974,7 +987,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;
net/ceph/crypto.c +0 -0
index 5601732cf4fa..2b98daffe9af 100644
@@ -285,6 +285,31 @@ int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
 	}

net/ceph/crypto.c int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt, · line 295

Quoted 10 lines
net/ceph/crypto.c +8 -0
 }
 
+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();

Maybe, return error code or INT_MAX instead of BUG()?

Quoted 4 lines
net/ceph/crypto.c +4 -0
+	}
+}
+
+int ceph_crypt_buflen(const struct ceph_crypto_key *key, int data_len)

net/ceph/crypto.c int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt, · line 309

Quoted 10 lines
net/ceph/crypto.c +10 -0
+{
+	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();

Ditto.

Thanks,
Slava.

Quoted 19 lines
net/ceph/crypto.c +3 -0
+	}
+}
+
 static int ceph_key_preparse(struct key_preparsed_payload *prep)
 {
 	struct ceph_crypto_key *ckey;
net/ceph/crypto.h +2 -0
index a20bad6d1e96..736ec6d2fbcb 100644
@@ -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);
 
author maintainer
#6 lore raw

Re: [PATCH 2/5] libceph: generalize ceph_x_encrypt_offset() and ceph_x_encrypt_buflen()

Quoted 34 lines · Viacheslav Dubeyko
Quoted 32 lines · Ilya Dryomov

- 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.

---
 net/ceph/auth_x.c | 44 +++++++++++++++++++++++++++++---------------
 net/ceph/crypto.c | 25 +++++++++++++++++++++++++
 net/ceph/crypto.h |  2 ++
 3 files changed, 56 insertions(+), 15 deletions(-)
net/ceph/auth_x.c +3 -2
index a21c157daf7d..5d7245884f95 100644
@@ -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);
 }
 
Quoted 9 lines

net/ceph/auth_x.c static int ceph_x_should_authenticate(struct ceph_auth_client *ac) · line 55

Quoted 5 lines
net/ceph/auth_x.c +2 -2
-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);

Frankly speaking, it's hard to follow why sizeof(u32) is used. Maybe, it makes sense to introduce a named constant? This could make the code more easy to understand.

It's part of the on-wire format which dictates that ciphertext_len is encoded as a 32-bit integer. It's pre-existing code (I just moved it) and a meaningful name for "length of ciphertext_len in the encoding" would be significationly more verbose.

The comment that I have added just a couple of lines below attempts to fill in the blanks:

/*
* AES: ciphertext_len | hdr | data... | padding */

Quoted 138 lines

net/ceph/auth_x.c static int ceph_x_should_authenticate(struct ceph_auth_client *ac) · line 65

Quoted 11 lines
net/ceph/auth_x.c +10 -0
+}
+
+/*
+ * 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);

Ditto.

net/ceph/auth_x.c static int ceph_x_should_authenticate(struct ceph_auth_client *ac) · line 75

Quoted 11 lines
net/ceph/auth_x.c +2 -1
 }
 
 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);

Ditto.

Quoted 110 lines
net/ceph/auth_x.c +12 -10
      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,6 +98,7 @@ static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
      if (ret)
              return ret;
 
+     hdr = p + ceph_crypt_data_offset(secret);
      if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
              pr_err("%s bad magic\n", __func__);
              return -EINVAL;
@@ -193,7 +206,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;
@@ -220,7 +233,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;
@@ -312,7 +325,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) {
@@ -368,7 +381,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);
@@ -507,7 +520,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;
@@ -634,7 +647,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;
@@ -804,7 +817,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 */
@@ -851,7 +864,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;
@@ -974,7 +987,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;
net/ceph/crypto.c +0 -0
index 5601732cf4fa..2b98daffe9af 100644
@@ -285,6 +285,31 @@ int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
      }
 }
 
Quoted 10 lines

net/ceph/crypto.c int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt, · line 295

Quoted 8 lines
net/ceph/crypto.c +8 -0
+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();

Maybe, return error code or INT_MAX instead of BUG()?

Random sentinel values like that often backfire in my experience. A BUG is much more clear and acceptable here IMO because a key of an unsupported type is expected be rejected at prepare time.

Thanks,

Ilya

author maintainer
#7 lore raw

PATCH 3/5 libceph: introduce ceph_crypto_key_prepare()

In reply to Ilya Dryomov

In preparation for bringing in a new encryption scheme/key type, decouple decoding or cloning the key from allocating required crypto API objects and setting them up. The rationale is that a) in some cases a shallow clone is sufficient and b) ceph_crypto_key_prepare() may grow additional parameters that would be inconvenient to provide at the point the key is originally decoded.

---
 net/ceph/auth_x.c | 26 +++++++++++++----
 net/ceph/crypto.c | 71 +++++++++++++++++++++++------------------------
 net/ceph/crypto.h |  1 +
 3 files changed, 56 insertions(+), 42 deletions(-)
net/ceph/auth_x.c +20 -6
index 5d7245884f95..abdd35be263a 100644
@@ -221,6 +221,10 @@ static int process_one_ticket(struct ceph_auth_client *ac,
 	if (ret)
 		goto out;
 
+	ret = ceph_crypto_key_prepare(&new_session_key);
+	if (ret)
+		goto out;
+
 	ceph_decode_need(&dp, dend, sizeof(struct ceph_timespec), bad);
 	ceph_decode_timespec64(&validity, dp);
 	dp += sizeof(struct ceph_timespec);
@@ -380,6 +384,10 @@ static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
 	if (ret)
 		goto out_au;
 
+	ret = ceph_crypto_key_prepare(&au->session_key);
+	if (ret)
+		goto out_au;
+
 	maxlen = sizeof(*msg_a) + ticket_blob_len +
 		ceph_x_encrypt_buflen(&au->session_key, sizeof(*msg_b));
 	dout("  need len %d\n", maxlen);
@@ -1106,21 +1114,26 @@ int ceph_x_init(struct ceph_auth_client *ac)
 	int ret;
 
 	dout("ceph_x_init %p\n", ac);
-	ret = -ENOMEM;
 	xi = kzalloc(sizeof(*xi), GFP_NOFS);
 	if (!xi)
-		goto out;
+		return -ENOMEM;
 
 	ret = -EINVAL;
 	if (!ac->key) {
 		pr_err("no secret set (for auth_x protocol)\n");
-		goto out_nomem;
+		goto err_xi;
 	}
 
 	ret = ceph_crypto_key_clone(&xi->secret, ac->key);
 	if (ret < 0) {
 		pr_err("cannot clone key: %d\n", ret);
-		goto out_nomem;
+		goto err_xi;
+	}
+
+	ret = ceph_crypto_key_prepare(&xi->secret);
+	if (ret) {
+		pr_err("cannot prepare key: %d\n", ret);
+		goto err_secret;
 	}
 
 	xi->starting = true;
@@ -1131,8 +1144,9 @@ int ceph_x_init(struct ceph_auth_client *ac)
 	ac->ops = &ceph_x_ops;
 	return 0;
 
-out_nomem:
+err_secret:
+	ceph_crypto_key_destroy(&xi->secret);
+err_xi:
 	kfree(xi);
-out:
 	return ret;
 }
net/ceph/crypto.c +35 -36

117 lines not on this page · raw message

net/ceph/crypto.h +1 -0
index 736ec6d2fbcb..2b8f8f68ff7a 100644
@@ -19,6 +19,7 @@ struct ceph_crypto_key {
 	struct crypto_sync_skcipher *tfm;
 };
 
+int ceph_crypto_key_prepare(struct ceph_crypto_key *key);
 int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
 			  const struct ceph_crypto_key *src);
 int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end);
Signature · 1 line
2.49.0
author maintainer
#8 lore raw

PATCH 4/5 libceph: add support for CEPH_CRYPTO_AES256KRB5

In reply to Ilya Dryomov

This is based on AES256-CTS-HMAC384-192 crypto algorithm per RFC 8009 (i.e. Kerberos 5, hence the name) with custom-defined key usage numbers. The implementation allows a given key to have/be linked to between one and three usage numbers.

The existing CEPH_CRYPTO_AES remains in place and unchanged. The usage_slot parameter that needed to be added to ceph_crypt() and its wrappers is simply ignored there.

---
 include/linux/ceph/ceph_fs.h |   5 +-
 net/ceph/Kconfig             |   1 +
 net/ceph/auth_x.c            |  88 ++++++++++++++------
 net/ceph/auth_x_protocol.h   |  38 +++++++++
 net/ceph/crypto.c            | 157 +++++++++++++++++++++++++++++++----
 net/ceph/crypto.h            |  16 +++-
 6 files changed, 257 insertions(+), 48 deletions(-)
include/linux/ceph/ceph_fs.h +3 -2
index c7f2c63b3bc3..4e81dea14b7b 100644
@@ -83,8 +83,9 @@ struct ceph_dir_layout {
 } __attribute__ ((packed));
 
 /* crypto algorithms */
-#define CEPH_CRYPTO_NONE 0x0
-#define CEPH_CRYPTO_AES  0x1
+#define CEPH_CRYPTO_NONE       0x0
+#define CEPH_CRYPTO_AES        0x1
+#define CEPH_CRYPTO_AES256KRB5 0x2 /* AES256-CTS-HMAC384-192 */
 
 #define CEPH_AES_IV "cephsageyudagreg"
 
net/ceph/Kconfig +1 -0
index ea60e3ef0834..7e2528cde4b9 100644
@@ -6,6 +6,7 @@ config CEPH_LIB
 	select CRYPTO_AES
 	select CRYPTO_CBC
 	select CRYPTO_GCM
+	select CRYPTO_KRB5
 	select CRYPTO_LIB_SHA256
 	select CRYPTO
 	select KEYS
net/ceph/auth_x.c +61 -27

212 lines not on this page · raw message

net/ceph/auth_x_protocol.h +38 -0
index 9c60feeb1bcb..d097b3651c99 100644
@@ -6,6 +6,44 @@
 #define CEPHX_GET_PRINCIPAL_SESSION_KEY 0x0200
 #define CEPHX_GET_ROTATING_KEY          0x0400
 
+/* Client <-> AuthMonitor */
+/*
+ * The AUTH session's connection secret: encrypted with the AUTH
+ * ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET	0x03
+/*
+ * The ticket's blob for the client ("blob for me", contains the
+ * session key): encrypted with the client's secret key in case of
+ * the AUTH ticket and the AUTH ticket session key in case of other
+ * service tickets
+ */
+#define CEPHX_KEY_USAGE_TICKET_SESSION_KEY	0x04
+/*
+ * The ticket's blob for the service (ceph_x_ticket_blob): possibly
+ * encrypted with the old AUTH ticket session key in case of the AUTH
+ * ticket and not encrypted in case of other service tickets
+ */
+#define CEPHX_KEY_USAGE_TICKET_BLOB		0x05
+
+/* Client <-> Service */
+/*
+ * The client's authorization request (ceph_x_authorize_b):
+ * encrypted with the service ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE		0x10
+/*
+ * The service's challenge (ceph_x_authorize_challenge):
+ * encrypted with the service ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE	0x11
+/*
+ * The service's final reply (ceph_x_authorize_reply + the service
+ * session's connection secret): encrypted with the service ticket
+ * session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE_REPLY		0x12
+
 /* common bits */
 struct ceph_x_ticket_blob {
 	__u8 struct_v;
net/ceph/crypto.c +142 -15

227 lines not on this page · raw message

net/ceph/crypto.h +12 -4
index 2b8f8f68ff7a..2c37c54d0f56 100644
@@ -5,7 +5,7 @@
 #include <linux/ceph/types.h>
 #include <linux/ceph/buffer.h>
 
-#define CEPH_MAX_KEY_LEN		16
+#define CEPH_MAX_KEY_LEN		32
 #define CEPH_MAX_CON_SECRET_LEN		64
 
 /*
@@ -16,10 +16,18 @@ struct ceph_crypto_key {
 	struct ceph_timespec created;
 	int len;
 	void *key;
-	struct crypto_sync_skcipher *tfm;
+
+	union {
+		struct crypto_sync_skcipher *aes_tfm;
+		struct {
+			const struct krb5_enctype *krb5_type;
+			struct crypto_aead *krb5_tfms[3];
+		};
+	};
 };
 
-int ceph_crypto_key_prepare(struct ceph_crypto_key *key);
+int ceph_crypto_key_prepare(struct ceph_crypto_key *key,
+			    const u32 *key_usages, int key_usage_cnt);
 int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
 			  const struct ceph_crypto_key *src);
 int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end);
@@ -27,7 +35,7 @@ int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *in);
 void ceph_crypto_key_destroy(struct ceph_crypto_key *key);
 
 /* crypto.c */
-int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
+int ceph_crypt(const struct ceph_crypto_key *key, int usage_slot, 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);
Signature · 1 line
2.49.0
#9 lore raw

Re: [PATCH 4/5] libceph: add support for CEPH_CRYPTO_AES256KRB5

Quoted 87 lines · Ilya Dryomov

This is based on AES256-CTS-HMAC384-192 crypto algorithm per RFC 8009 (i.e. Kerberos 5, hence the name) with custom-defined key usage numbers. The implementation allows a given key to have/be linked to between one and three usage numbers.

The existing CEPH_CRYPTO_AES remains in place and unchanged. The usage_slot parameter that needed to be added to ceph_crypt() and its wrappers is simply ignored there.

---
 include/linux/ceph/ceph_fs.h |   5 +-
 net/ceph/Kconfig             |   1 +
 net/ceph/auth_x.c            |  88 ++++++++++++++------
 net/ceph/auth_x_protocol.h   |  38 +++++++++
 net/ceph/crypto.c            | 157 +++++++++++++++++++++++++++++++----
 net/ceph/crypto.h            |  16 +++-
 6 files changed, 257 insertions(+), 48 deletions(-)
include/linux/ceph/ceph_fs.h +3 -2
index c7f2c63b3bc3..4e81dea14b7b 100644
@@ -83,8 +83,9 @@ struct ceph_dir_layout {
 } __attribute__ ((packed));
 
 /* crypto algorithms */
-#define CEPH_CRYPTO_NONE 0x0
-#define CEPH_CRYPTO_AES  0x1
+#define CEPH_CRYPTO_NONE       0x0
+#define CEPH_CRYPTO_AES        0x1
+#define CEPH_CRYPTO_AES256KRB5 0x2 /* AES256-CTS-HMAC384-192 */
 
 #define CEPH_AES_IV "cephsageyudagreg"
 
net/ceph/Kconfig +1 -0
index ea60e3ef0834..7e2528cde4b9 100644
@@ -6,6 +6,7 @@ config CEPH_LIB
 	select CRYPTO_AES
 	select CRYPTO_CBC
 	select CRYPTO_GCM
+	select CRYPTO_KRB5
 	select CRYPTO_LIB_SHA256
 	select CRYPTO
 	select KEYS
net/ceph/auth_x.c +17 -1
index abdd35be263a..decd2867f8f1 100644
@@ -17,6 +17,22 @@
 #include "auth_x.h"
 #include "auth_x_protocol.h"
 
+static const u32 ticket_key_usages[] = {
+	CEPHX_KEY_USAGE_TICKET_SESSION_KEY,
+	CEPHX_KEY_USAGE_TICKET_BLOB,
+	CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET
+};
+
+static const u32 authorizer_key_usages[] = {
+	CEPHX_KEY_USAGE_AUTHORIZE,
+	CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE,
+	CEPHX_KEY_USAGE_AUTHORIZE_REPLY
+};
+
+static const u32 client_key_usages[] = {
+	CEPHX_KEY_USAGE_TICKET_SESSION_KEY
+};
+
 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
 
 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
@@ -57,6 +73,7 @@ static int ceph_x_encrypt_offset(const struct ceph_crypto_key *key)
 
 /*
  * AES: ciphertext_len | hdr | data... | padding
+ * AES256KRB5: ciphertext_len | confounder | hdr | data... | hmac
  */
 static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key,
 				 int data_len)
@@ -65,19 +82,19 @@ static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key,
 	return sizeof(u32) + ceph_crypt_buflen(key, encrypt_len);
 }
 
-static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,

net/ceph/auth_x.c static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key, · line 92

Quoted 10 lines
net/ceph/auth_x.c +3 -2
-			  int buf_len, int plaintext_len)
+static int ceph_x_encrypt(const struct ceph_crypto_key *key, int usage_slot,
+			  void *buf, int buf_len, int plaintext_len)
 {
 	struct ceph_x_encrypt_header *hdr;
 	int ciphertext_len;
 	int ret;
 
-	hdr = buf + sizeof(u32) + ceph_crypt_data_offset(secret);
+	hdr = buf + sizeof(u32) + ceph_crypt_data_offset(key);

What about to have a named constant for this sizeof(u32)?

net/ceph/auth_x.c static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key, · line 97

Quoted 7 lines
net/ceph/auth_x.c +2 -2
 	hdr->struct_v = 1;
 	hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
 
-	ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
-			 plaintext_len + sizeof(struct ceph_x_encrypt_header),
+	ret = ceph_crypt(key, usage_slot, true, buf + sizeof(u32),
+			 buf_len - sizeof(u32), plaintext_len + sizeof(*hdr),

Ditto.

Quoted 48 lines
net/ceph/auth_x.c +8 -7
 			 &ciphertext_len);
 	if (ret)
 		return ret;
@@ -86,19 +103,19 @@ static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
 	return sizeof(u32) + ciphertext_len;
 }
 
-static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
-			    int ciphertext_len)
+static int __ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot,
+			    void *p, int ciphertext_len)
 {
 	struct ceph_x_encrypt_header *hdr;
 	int plaintext_len;
 	int ret;
 
-	ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
-			 &plaintext_len);
+	ret = ceph_crypt(key, usage_slot, false, p, ciphertext_len,
+			 ciphertext_len, &plaintext_len);
 	if (ret)
 		return ret;
 
-	hdr = p + ceph_crypt_data_offset(secret);
+	hdr = p + ceph_crypt_data_offset(key);
 	if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
 		pr_err("%s bad magic\n", __func__);
 		return -EINVAL;
@@ -107,7 +124,8 @@ static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
 	return plaintext_len - sizeof(*hdr);
 }
 
-static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
+static int ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot,
+			  void **p, void *end)
 {
 	int ciphertext_len;
 	int ret;
@@ -115,7 +133,7 @@ static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
 	ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
 	ceph_decode_need(p, end, ciphertext_len, e_inval);
 
-	ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
+	ret = __ceph_x_decrypt(key, usage_slot, *p, ciphertext_len);
 	if (ret < 0)
 		return ret;
 

net/ceph/auth_x.c static int process_one_ticket(struct ceph_auth_client *ac, · line 229

Quoted 6 lines
net/ceph/auth_x.c +2 -1
@@ -207,7 +225,9 @@ static int process_one_ticket(struct ceph_auth_client *ac,
 
 	/* blob for me */
 	dp = *p + ceph_x_encrypt_offset(secret);
-	ret = ceph_x_decrypt(secret, p, end);
+	ret = ceph_x_decrypt(secret,
+			     0 /* CEPHX_KEY_USAGE_TICKET_SESSION_KEY */,

This looks really confusing. If we have the named constant already, then what's the point to comment it instead of using it?

Quoted 15 lines
net/ceph/auth_x.c +3 -1
+			     p, end);
 	if (ret < 0)
 		goto out;
 	dout(" decrypted %d bytes\n", ret);
@@ -221,7 +241,8 @@ static int process_one_ticket(struct ceph_auth_client *ac,
 	if (ret)
 		goto out;
 
-	ret = ceph_crypto_key_prepare(&new_session_key);
+	ret = ceph_crypto_key_prepare(&new_session_key, ticket_key_usages,
+				      ARRAY_SIZE(ticket_key_usages));
 	if (ret)
 		goto out;
 

net/ceph/auth_x.c static int process_one_ticket(struct ceph_auth_client *ac, · line 263

Quoted 6 lines
net/ceph/auth_x.c +2 -1
@@ -238,7 +259,9 @@ static int process_one_ticket(struct ceph_auth_client *ac,
 	if (is_enc) {
 		/* encrypted */
 		tp = *p + ceph_x_encrypt_offset(&th->session_key);
-		ret = ceph_x_decrypt(&th->session_key, p, end);
+		ret = ceph_x_decrypt(&th->session_key,
+				     1 /* CEPHX_KEY_USAGE_TICKET_BLOB */,

Ditto.

net/ceph/auth_x.c static int encrypt_authorizer(struct ceph_x_authorizer *au, · line 368

Quoted 11 lines
net/ceph/auth_x.c +3 -1
+				     p, end);
 		if (ret < 0)
 			goto out;
 		dout(" encrypted ticket, decrypted %d bytes\n", ret);
@@ -341,7 +364,9 @@ static int encrypt_authorizer(struct ceph_x_authorizer *au,
 		msg_b->server_challenge_plus_one = 0;
 	}
 
-	ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
+	ret = ceph_x_encrypt(&au->session_key,
+			     0 /* CEPHX_KEY_USAGE_AUTHORIZE */,

Ditto.

Quoted 15 lines
net/ceph/auth_x.c +3 -1
+			     p, end - p, sizeof(*msg_b));
 	if (ret < 0)
 		return ret;
 
@@ -384,7 +409,8 @@ static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
 	if (ret)
 		goto out_au;
 
-	ret = ceph_crypto_key_prepare(&au->session_key);
+	ret = ceph_crypto_key_prepare(&au->session_key, authorizer_key_usages,
+				      ARRAY_SIZE(authorizer_key_usages));
 	if (ret)
 		goto out_au;
 

net/ceph/auth_x.c static int ceph_x_build_request(struct ceph_auth_client *ac, · line 571

Quoted 5 lines
net/ceph/auth_x.c +1 -1
@@ -542,7 +568,8 @@ static int ceph_x_build_request(struct ceph_auth_client *ac,
 		get_random_bytes(&auth->client_challenge, sizeof(u64));
 		blob->client_challenge = auth->client_challenge;
 		blob->server_challenge = cpu_to_le64(xi->server_challenge);
-		ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
+		ret = ceph_x_encrypt(&xi->secret, 0 /* dummy */,

Ditto.

net/ceph/auth_x.c static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id, · line 687

Quoted 11 lines
net/ceph/auth_x.c +3 -1
+				     enc_buf, CEPHX_AU_ENC_BUF_LEN,
 				     sizeof(*blob));
 		if (ret < 0)
 			return ret;
@@ -656,7 +683,9 @@ static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id,
 	dout("%s connection secret blob len %d\n", __func__, len);
 	if (len > 0) {
 		dp = *p + ceph_x_encrypt_offset(&th->session_key);
-		ret = ceph_x_decrypt(&th->session_key, p, *p + len);
+		ret = ceph_x_decrypt(&th->session_key,
+				     2 /* CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET */,

Ditto.

net/ceph/auth_x.c static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret, · line 853

Quoted 11 lines
net/ceph/auth_x.c +3 -1
+				     p, *p + len);
 		if (ret < 0)
 			return ret;
 
@@ -820,7 +849,9 @@ static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret,
 	int ret;
 
 	/* no leading len */
-	ret = __ceph_x_decrypt(secret, challenge, challenge_len);
+	ret = __ceph_x_decrypt(secret,
+			       1 /* CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE */,

Ditto.

net/ceph/auth_x.c static int decrypt_authorizer_reply(struct ceph_crypto_key *secret, · line 907

Quoted 10 lines
net/ceph/auth_x.c +2 -1
+			       challenge, challenge_len);
 	if (ret < 0)
 		return ret;
 
@@ -873,7 +904,8 @@ static int decrypt_authorizer_reply(struct ceph_crypto_key *secret,
 	int ret;
 
 	dp = *p + ceph_x_encrypt_offset(secret);
-	ret = ceph_x_decrypt(secret, p, end);
+	ret = ceph_x_decrypt(secret, 2 /* CEPHX_KEY_USAGE_AUTHORIZE_REPLY */,

Ditto.

net/ceph/auth_x.c static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, · line 1039

Quoted 11 lines
net/ceph/auth_x.c +2 -2
+			     p, end);
 	if (ret < 0)
 		return ret;
 
@@ -1004,8 +1036,9 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
 		sigblock->middle_crc = msg->footer.middle_crc;
 		sigblock->data_crc =  msg->footer.data_crc;
 
-		ret = ceph_x_encrypt(&au->session_key, enc_buf,
-				     CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock));
+		ret = ceph_x_encrypt(&au->session_key, 0 /* dummy */,

Ditto.

net/ceph/auth_x.c static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, · line 1072

Quoted 13 lines
net/ceph/auth_x.c +3 -3
+				     enc_buf, CEPHX_AU_ENC_BUF_LEN,
+				     sizeof(*sigblock));
 		if (ret < 0)
 			return ret;
 
@@ -1036,9 +1069,9 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
 		sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
 
 		/* no leading len, no ceph_x_encrypt_header */
-		ret = ceph_crypt(&au->session_key, true, enc_buf,
-				 CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock),
-				 &ciphertext_len);
+		ret = ceph_crypt(&au->session_key, 0 /* dummy */,

Ditto.

Quoted 158 lines
net/ceph/auth_x.c +4 -1
+				 true, enc_buf, CEPHX_AU_ENC_BUF_LEN,
+				 sizeof(*sigblock), &ciphertext_len);
 		if (ret)
 			return ret;
 
@@ -1130,7 +1163,8 @@ int ceph_x_init(struct ceph_auth_client *ac)
 		goto err_xi;
 	}
 
-	ret = ceph_crypto_key_prepare(&xi->secret);
+	ret = ceph_crypto_key_prepare(&xi->secret, client_key_usages,
+				      ARRAY_SIZE(client_key_usages));
 	if (ret) {
 		pr_err("cannot prepare key: %d\n", ret);
 		goto err_secret;
net/ceph/auth_x_protocol.h +38 -0
index 9c60feeb1bcb..d097b3651c99 100644
@@ -6,6 +6,44 @@
 #define CEPHX_GET_PRINCIPAL_SESSION_KEY 0x0200
 #define CEPHX_GET_ROTATING_KEY          0x0400
 
+/* Client <-> AuthMonitor */
+/*
+ * The AUTH session's connection secret: encrypted with the AUTH
+ * ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET	0x03
+/*
+ * The ticket's blob for the client ("blob for me", contains the
+ * session key): encrypted with the client's secret key in case of
+ * the AUTH ticket and the AUTH ticket session key in case of other
+ * service tickets
+ */
+#define CEPHX_KEY_USAGE_TICKET_SESSION_KEY	0x04
+/*
+ * The ticket's blob for the service (ceph_x_ticket_blob): possibly
+ * encrypted with the old AUTH ticket session key in case of the AUTH
+ * ticket and not encrypted in case of other service tickets
+ */
+#define CEPHX_KEY_USAGE_TICKET_BLOB		0x05
+
+/* Client <-> Service */
+/*
+ * The client's authorization request (ceph_x_authorize_b):
+ * encrypted with the service ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE		0x10
+/*
+ * The service's challenge (ceph_x_authorize_challenge):
+ * encrypted with the service ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE	0x11
+/*
+ * The service's final reply (ceph_x_authorize_reply + the service
+ * session's connection secret): encrypted with the service ticket
+ * session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE_REPLY		0x12
+
 /* common bits */
 struct ceph_x_ticket_blob {
 	__u8 struct_v;
net/ceph/crypto.c +47 -9
index 3453dc303315..b54085d8d5f0 100644
@@ -7,6 +7,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <crypto/aes.h>
+#include <crypto/krb5.h>
 #include <crypto/skcipher.h>
 #include <linux/key-type.h>
 #include <linux/sched/mm.h>
@@ -22,28 +23,68 @@ static int set_aes_tfm(struct ceph_crypto_key *key)
 	int ret;
 
 	noio_flag = memalloc_noio_save();
-	key->tfm = crypto_alloc_sync_skcipher("cbc(aes)", 0, 0);
+	key->aes_tfm = crypto_alloc_sync_skcipher("cbc(aes)", 0, 0);
 	memalloc_noio_restore(noio_flag);
-	if (IS_ERR(key->tfm)) {
-		ret = PTR_ERR(key->tfm);
-		key->tfm = NULL;
+	if (IS_ERR(key->aes_tfm)) {
+		ret = PTR_ERR(key->aes_tfm);
+		key->aes_tfm = NULL;
 		return ret;
 	}
 
-	ret = crypto_sync_skcipher_setkey(key->tfm, key->key, key->len);
+	ret = crypto_sync_skcipher_setkey(key->aes_tfm, key->key, key->len);
 	if (ret)
 		return ret;
 
 	return 0;
 }
 
-int ceph_crypto_key_prepare(struct ceph_crypto_key *key)
+static int set_krb5_tfms(struct ceph_crypto_key *key, const u32 *key_usages,
+			 int key_usage_cnt)
+{
+	struct krb5_buffer TK = { .len = key->len, .data = key->key };
+	unsigned int noio_flag;
+	int ret = 0;
+	int i;
+
+	if (WARN_ON_ONCE(key_usage_cnt > ARRAY_SIZE(key->krb5_tfms)))
+		return -EINVAL;
+
+	key->krb5_type = crypto_krb5_find_enctype(
+			     KRB5_ENCTYPE_AES256_CTS_HMAC_SHA384_192);
+	if (!key->krb5_type)
+		return -ENOPKG;
+
+	/*
+	 * Despite crypto_krb5_prepare_encryption() taking a gfp mask,
+	 * crypto_alloc_aead() inside of it allocates with GFP_KERNEL.
+	 */
+	noio_flag = memalloc_noio_save();
+	for (i = 0; i < key_usage_cnt; i++) {
+		key->krb5_tfms[i] = crypto_krb5_prepare_encryption(
+					key->krb5_type, &TK, key_usages[i],
+					GFP_NOIO);
+		if (IS_ERR(key->krb5_tfms[i])) {
+			ret = PTR_ERR(key->krb5_tfms[i]);
+			key->krb5_tfms[i] = NULL;
+			goto out_flag;
+		}
+	}
+
+out_flag:
+	memalloc_noio_restore(noio_flag);
+	return ret;
+}
+
+int ceph_crypto_key_prepare(struct ceph_crypto_key *key,
+			    const u32 *key_usages, int key_usage_cnt)
 {
 	switch (key->type) {
 	case CEPH_CRYPTO_NONE:
 		return 0; /* nothing to do */
 	case CEPH_CRYPTO_AES:
 		return set_aes_tfm(key);
+	case CEPH_CRYPTO_AES256KRB5:
+		return set_krb5_tfms(key, key_usages, key_usage_cnt);
 	default:
 		return -ENOTSUPP;
 	}
@@ -123,12 +164,25 @@ int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
 
 void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
 {
-	if (key) {
-		kfree_sensitive(key->key);
-		key->key = NULL;

net/ceph/crypto.c int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey) · line 173

Quoted 10 lines
net/ceph/crypto.c +7 -3
-		if (key->tfm) {
-			crypto_free_sync_skcipher(key->tfm);
-			key->tfm = NULL;
+	int i;
+
+	if (!key)
+		return;
+
+	kfree_sensitive(key->key);
+	key->key = NULL;

The key->key looks strange. :) Maybe, another name like crypto_key?

Quoted 34 lines
net/ceph/crypto.c +14 -2
+
+	if (key->type == CEPH_CRYPTO_AES) {
+		if (key->aes_tfm) {
+			crypto_free_sync_skcipher(key->aes_tfm);
+			key->aes_tfm = NULL;
+		}
+	} else if (key->type == CEPH_CRYPTO_AES256KRB5) {
+		for (i = 0; i < ARRAY_SIZE(key->krb5_tfms); i++) {
+			if (key->krb5_tfms[i]) {
+				crypto_free_aead(key->krb5_tfms[i]);
+				key->krb5_tfms[i] = NULL;
+			}
 		}
 	}
 }
@@ -208,7 +262,7 @@ static void teardown_sgtable(struct sg_table *sgt)
 static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
 			  void *buf, int buf_len, int in_len, int *pout_len)
 {
-	SYNC_SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
+	SYNC_SKCIPHER_REQUEST_ON_STACK(req, key->aes_tfm);
 	struct sg_table sgt;
 	struct scatterlist prealloc_sg;
 	char iv[AES_BLOCK_SIZE] __aligned(8);
@@ -224,7 +278,7 @@ static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
 		return ret;
 
 	memcpy(iv, aes_iv, AES_BLOCK_SIZE);
-	skcipher_request_set_sync_tfm(req, key->tfm);
+	skcipher_request_set_sync_tfm(req, key->aes_tfm);
 	skcipher_request_set_callback(req, 0, NULL, NULL);
 	skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
 

net/ceph/crypto.c static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt, · line 327

Quoted 6 lines
net/ceph/crypto.c +2 -1
@@ -269,7 +323,68 @@ static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
 	return ret;
 }
 
-int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
+static int ceph_krb5_encrypt(const struct ceph_crypto_key *key, int usage_slot,
+			     void *buf, int buf_len, int in_len, int *pout_len)

If we have really many function's arguments, then, maybe, we need to introduce some structure?

Quoted 77 lines
net/ceph/crypto.c +66 -0
+{
+	struct sg_table sgt;
+	struct scatterlist prealloc_sg;
+	int ret;
+
+	if (WARN_ON_ONCE(usage_slot >= ARRAY_SIZE(key->krb5_tfms)))
+		return -EINVAL;
+
+	ret = setup_sgtable(&sgt, &prealloc_sg, buf, buf_len);
+	if (ret)
+		return ret;
+
+	ret = crypto_krb5_encrypt(key->krb5_type, key->krb5_tfms[usage_slot],
+				  sgt.sgl, sgt.nents, buf_len, AES_BLOCK_SIZE,
+				  in_len, false);
+	if (ret < 0) {
+		pr_err("%s encrypt failed: %d\n", __func__, ret);
+		goto out_sgt;
+	}
+
+	*pout_len = ret;
+	ret = 0;
+
+out_sgt:
+	teardown_sgtable(&sgt);
+	return ret;
+}
+
+static int ceph_krb5_decrypt(const struct ceph_crypto_key *key, int usage_slot,
+			     void *buf, int buf_len, int in_len, int *pout_len)
+{
+	struct sg_table sgt;
+	struct scatterlist prealloc_sg;
+	size_t data_off = 0;
+	size_t data_len = in_len;
+	int ret;
+
+	if (WARN_ON_ONCE(usage_slot >= ARRAY_SIZE(key->krb5_tfms)))
+		return -EINVAL;
+
+	ret = setup_sgtable(&sgt, &prealloc_sg, buf, in_len);
+	if (ret)
+		return ret;
+
+	ret = crypto_krb5_decrypt(key->krb5_type, key->krb5_tfms[usage_slot],
+				  sgt.sgl, sgt.nents, &data_off, &data_len);
+	if (ret) {
+		pr_err("%s decrypt failed: %d\n", __func__, ret);
+		goto out_sgt;
+	}
+
+	WARN_ON(data_off != AES_BLOCK_SIZE);
+	*pout_len = data_len;
+
+out_sgt:
+	teardown_sgtable(&sgt);
+	return ret;
+}
+
+int ceph_crypt(const struct ceph_crypto_key *key, int usage_slot, bool encrypt,
 	       void *buf, int buf_len, int in_len, int *pout_len)
 {
 	switch (key->type) {
@@ -279,6 +394,12 @@ int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
 	case CEPH_CRYPTO_AES:
 		return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
 				      pout_len);
+	case CEPH_CRYPTO_AES256KRB5:
+		return encrypt ?
+		    ceph_krb5_encrypt(key, usage_slot, buf, buf_len, in_len,
+				      pout_len) :
+		    ceph_krb5_decrypt(key, usage_slot, buf, buf_len, in_len,
+				      pout_len);
 	default:
 		return -ENOTSUPP;
 	}

net/ceph/crypto.c int ceph_crypt_data_offset(const struct ceph_crypto_key *key) · line 415

Quoted 5 lines
net/ceph/crypto.c +2 -0
@@ -290,6 +411,9 @@ int ceph_crypt_data_offset(const struct ceph_crypto_key *key)
 	case CEPH_CRYPTO_NONE:
 	case CEPH_CRYPTO_AES:
 		return 0;
+	case CEPH_CRYPTO_AES256KRB5:
+		/* confounder */

Frankly speaking, I don't follow what this comment imply. :)

Thanks,
Slava.

Quoted 57 lines
net/ceph/crypto.c +4 -0
+		return AES_BLOCK_SIZE;
 	default:
 		BUG();
 	}
@@ -304,6 +428,9 @@ int ceph_crypt_buflen(const struct ceph_crypto_key *key, int data_len)
 		/* PKCS#7 padding at the end */
 		return data_len + AES_BLOCK_SIZE -
 		       (data_len & (AES_BLOCK_SIZE - 1));
+	case CEPH_CRYPTO_AES256KRB5:
+		/* confounder at the beginning and 192-bit HMAC at the end */
+		return AES_BLOCK_SIZE + data_len + 24;
 	default:
 		BUG();
 	}
net/ceph/crypto.h +12 -4
index 2b8f8f68ff7a..2c37c54d0f56 100644
@@ -5,7 +5,7 @@
 #include <linux/ceph/types.h>
 #include <linux/ceph/buffer.h>
 
-#define CEPH_MAX_KEY_LEN		16
+#define CEPH_MAX_KEY_LEN		32
 #define CEPH_MAX_CON_SECRET_LEN		64
 
 /*
@@ -16,10 +16,18 @@ struct ceph_crypto_key {
 	struct ceph_timespec created;
 	int len;
 	void *key;
-	struct crypto_sync_skcipher *tfm;
+
+	union {
+		struct crypto_sync_skcipher *aes_tfm;
+		struct {
+			const struct krb5_enctype *krb5_type;
+			struct crypto_aead *krb5_tfms[3];
+		};
+	};
 };
 
-int ceph_crypto_key_prepare(struct ceph_crypto_key *key);
+int ceph_crypto_key_prepare(struct ceph_crypto_key *key,
+			    const u32 *key_usages, int key_usage_cnt);
 int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
 			  const struct ceph_crypto_key *src);
 int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end);
@@ -27,7 +35,7 @@ int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *in);
 void ceph_crypto_key_destroy(struct ceph_crypto_key *key);
 
 /* crypto.c */
-int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
+int ceph_crypt(const struct ceph_crypto_key *key, int usage_slot, 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);
author maintainer
#10 lore raw

Re: [PATCH 4/5] libceph: add support for CEPH_CRYPTO_AES256KRB5

Quoted 97 lines · Viacheslav Dubeyko
Quoted 95 lines · Ilya Dryomov

This is based on AES256-CTS-HMAC384-192 crypto algorithm per RFC 8009 (i.e. Kerberos 5, hence the name) with custom-defined key usage numbers. The implementation allows a given key to have/be linked to between one and three usage numbers.

The existing CEPH_CRYPTO_AES remains in place and unchanged. The usage_slot parameter that needed to be added to ceph_crypt() and its wrappers is simply ignored there.

---
 include/linux/ceph/ceph_fs.h |   5 +-
 net/ceph/Kconfig             |   1 +
 net/ceph/auth_x.c            |  88 ++++++++++++++------
 net/ceph/auth_x_protocol.h   |  38 +++++++++
 net/ceph/crypto.c            | 157 +++++++++++++++++++++++++++++++----
 net/ceph/crypto.h            |  16 +++-
 6 files changed, 257 insertions(+), 48 deletions(-)
include/linux/ceph/ceph_fs.h +3 -2
index c7f2c63b3bc3..4e81dea14b7b 100644
@@ -83,8 +83,9 @@ struct ceph_dir_layout {
 } __attribute__ ((packed));
 
 /* crypto algorithms */
-#define CEPH_CRYPTO_NONE 0x0
-#define CEPH_CRYPTO_AES  0x1
+#define CEPH_CRYPTO_NONE       0x0
+#define CEPH_CRYPTO_AES        0x1
+#define CEPH_CRYPTO_AES256KRB5 0x2 /* AES256-CTS-HMAC384-192 */
 
 #define CEPH_AES_IV "cephsageyudagreg"
 
net/ceph/Kconfig +1 -0
index ea60e3ef0834..7e2528cde4b9 100644
@@ -6,6 +6,7 @@ config CEPH_LIB
      select CRYPTO_AES
      select CRYPTO_CBC
      select CRYPTO_GCM
+     select CRYPTO_KRB5
      select CRYPTO_LIB_SHA256
      select CRYPTO
      select KEYS
net/ceph/auth_x.c +19 -2
index abdd35be263a..decd2867f8f1 100644
@@ -17,6 +17,22 @@
 #include "auth_x.h"
 #include "auth_x_protocol.h"
 
+static const u32 ticket_key_usages[] = {
+     CEPHX_KEY_USAGE_TICKET_SESSION_KEY,
+     CEPHX_KEY_USAGE_TICKET_BLOB,
+     CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET
+};
+
+static const u32 authorizer_key_usages[] = {
+     CEPHX_KEY_USAGE_AUTHORIZE,
+     CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE,
+     CEPHX_KEY_USAGE_AUTHORIZE_REPLY
+};
+
+static const u32 client_key_usages[] = {
+     CEPHX_KEY_USAGE_TICKET_SESSION_KEY
+};
+
 static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
 
 static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
@@ -57,6 +73,7 @@ static int ceph_x_encrypt_offset(const struct ceph_crypto_key *key)
 
 /*
  * AES: ciphertext_len | hdr | data... | padding
+ * AES256KRB5: ciphertext_len | confounder | hdr | data... | hmac
  */
 static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key,
                               int data_len)
@@ -65,19 +82,19 @@ static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key,
      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)
+static int ceph_x_encrypt(const struct ceph_crypto_key *key, int usage_slot,
+                       void *buf, int buf_len, int plaintext_len)
 {
      struct ceph_x_encrypt_header *hdr;
      int ciphertext_len;
      int ret;
 
Quoted 4 lines

net/ceph/auth_x.c static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key, · line 92

Quoted 2 lines
net/ceph/auth_x.c +1 -1
-     hdr = buf + sizeof(u32) + ceph_crypt_data_offset(secret);
+     hdr = buf + sizeof(u32) + ceph_crypt_data_offset(key);

What about to have a named constant for this sizeof(u32)?

Already covered on another patch -- this is the ciphertext_len thing, all pre-existing code.

Quoted 60 lines

net/ceph/auth_x.c static int ceph_x_encrypt_buflen(const struct ceph_crypto_key *key, · line 97

Quoted 8 lines
net/ceph/auth_x.c +2 -2
      hdr->struct_v = 1;
      hdr->magic = cpu_to_le64(CEPHX_ENC_MAGIC);
 
-     ret = ceph_crypt(secret, true, buf + sizeof(u32), buf_len - sizeof(u32),
-                      plaintext_len + sizeof(struct ceph_x_encrypt_header),
+     ret = ceph_crypt(key, usage_slot, true, buf + sizeof(u32),
+                      buf_len - sizeof(u32), plaintext_len + sizeof(*hdr),

Ditto.

Quoted 49 lines
net/ceph/auth_x.c +8 -7
                       &ciphertext_len);
      if (ret)
              return ret;
@@ -86,19 +103,19 @@ static int ceph_x_encrypt(struct ceph_crypto_key *secret, void *buf,
      return sizeof(u32) + ciphertext_len;
 }
 
-static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
-                         int ciphertext_len)
+static int __ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot,
+                         void *p, int ciphertext_len)
 {
      struct ceph_x_encrypt_header *hdr;
      int plaintext_len;
      int ret;
 
-     ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
-                      &plaintext_len);
+     ret = ceph_crypt(key, usage_slot, false, p, ciphertext_len,
+                      ciphertext_len, &plaintext_len);
      if (ret)
              return ret;
 
-     hdr = p + ceph_crypt_data_offset(secret);
+     hdr = p + ceph_crypt_data_offset(key);
      if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
              pr_err("%s bad magic\n", __func__);
              return -EINVAL;
@@ -107,7 +124,8 @@ static int __ceph_x_decrypt(struct ceph_crypto_key *secret, void *p,
      return plaintext_len - sizeof(*hdr);
 }
 
-static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
+static int ceph_x_decrypt(const struct ceph_crypto_key *key, int usage_slot,
+                       void **p, void *end)
 {
      int ciphertext_len;
      int ret;
@@ -115,7 +133,7 @@ static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
      ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
      ceph_decode_need(p, end, ciphertext_len, e_inval);
 
-     ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
+     ret = __ceph_x_decrypt(key, usage_slot, *p, ciphertext_len);
      if (ret < 0)
              return ret;
 
@@ -207,7 +225,9 @@ static int process_one_ticket(struct ceph_auth_client *ac,
 
Quoted 8 lines

net/ceph/auth_x.c static int process_one_ticket(struct ceph_auth_client *ac, · line 229

Quoted 5 lines
net/ceph/auth_x.c +2 -1
      /* blob for me */
      dp = *p + ceph_x_encrypt_offset(secret);
-     ret = ceph_x_decrypt(secret, p, end);
+     ret = ceph_x_decrypt(secret,
+                          0 /* CEPHX_KEY_USAGE_TICKET_SESSION_KEY */,

This looks really confusing. If we have the named constant already, then what's the point to comment it instead of using it?

The named constant in question has a different value:

  #define CEPHX_KEY_USAGE_TICKET_SESSION_KEY   0x04

Notice that the respective ceph_x_decrypt() parameter is named usage_slot, not key_usage or something like that. The 0 here selects the first of the three available key usage slots -- the one that gets set up to correspond to CEPHX_KEY_USAGE_TICKET_SESSION_KEY at prepare time.

Quoted 53 lines
Quoted 16 lines
net/ceph/auth_x.c +3 -1
+                          p, end);
      if (ret < 0)
              goto out;
      dout(" decrypted %d bytes\n", ret);
@@ -221,7 +241,8 @@ static int process_one_ticket(struct ceph_auth_client *ac,
      if (ret)
              goto out;
 
-     ret = ceph_crypto_key_prepare(&new_session_key);
+     ret = ceph_crypto_key_prepare(&new_session_key, ticket_key_usages,
+                                   ARRAY_SIZE(ticket_key_usages));
      if (ret)
              goto out;
 

net/ceph/auth_x.c static int process_one_ticket(struct ceph_auth_client *ac, · line 263

Quoted 6 lines
net/ceph/auth_x.c +2 -1
@@ -238,7 +259,9 @@ static int process_one_ticket(struct ceph_auth_client *ac,
      if (is_enc) {
              /* encrypted */
              tp = *p + ceph_x_encrypt_offset(&th->session_key);
-             ret = ceph_x_decrypt(&th->session_key, p, end);
+             ret = ceph_x_decrypt(&th->session_key,
+                                  1 /* CEPHX_KEY_USAGE_TICKET_BLOB */,

Ditto.

net/ceph/auth_x.c static int encrypt_authorizer(struct ceph_x_authorizer *au, · line 368

Quoted 11 lines
net/ceph/auth_x.c +3 -1
+                                  p, end);
              if (ret < 0)
                      goto out;
              dout(" encrypted ticket, decrypted %d bytes\n", ret);
@@ -341,7 +364,9 @@ static int encrypt_authorizer(struct ceph_x_authorizer *au,
              msg_b->server_challenge_plus_one = 0;
      }
 
-     ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
+     ret = ceph_x_encrypt(&au->session_key,
+                          0 /* CEPHX_KEY_USAGE_AUTHORIZE */,

Ditto.

Quoted 14 lines
net/ceph/auth_x.c +3 -1
+                          p, end - p, sizeof(*msg_b));
      if (ret < 0)
              return ret;
 
@@ -384,7 +409,8 @@ static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
      if (ret)
              goto out_au;
 
-     ret = ceph_crypto_key_prepare(&au->session_key);
+     ret = ceph_crypto_key_prepare(&au->session_key, authorizer_key_usages,
+                                   ARRAY_SIZE(authorizer_key_usages));
      if (ret)
              goto out_au;
 
Quoted 8 lines

net/ceph/auth_x.c static int ceph_x_build_request(struct ceph_auth_client *ac, · line 571

Quoted 6 lines
net/ceph/auth_x.c +1 -1
@@ -542,7 +568,8 @@ static int ceph_x_build_request(struct ceph_auth_client *ac,
              get_random_bytes(&auth->client_challenge, sizeof(u64));
              blob->client_challenge = auth->client_challenge;
              blob->server_challenge = cpu_to_le64(xi->server_challenge);
-             ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
+             ret = ceph_x_encrypt(&xi->secret, 0 /* dummy */,

Ditto.

The "dummy" aspect is noted in the patch description:

The existing CEPH_CRYPTO_AES remains in place and unchanged. The usage_slot parameter that needed to be added to ceph_crypt() and its wrappers is simply ignored there.

Quoted 234 lines

net/ceph/auth_x.c static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id, · line 687

Quoted 12 lines
net/ceph/auth_x.c +3 -1
+                                  enc_buf, CEPHX_AU_ENC_BUF_LEN,
                                   sizeof(*blob));
              if (ret < 0)
                      return ret;
@@ -656,7 +683,9 @@ static int handle_auth_session_key(struct ceph_auth_client *ac, u64 global_id,
      dout("%s connection secret blob len %d\n", __func__, len);
      if (len > 0) {
              dp = *p + ceph_x_encrypt_offset(&th->session_key);
-             ret = ceph_x_decrypt(&th->session_key, p, *p + len);
+             ret = ceph_x_decrypt(&th->session_key,
+                                  2 /* CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET */,

Ditto.

net/ceph/auth_x.c static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret, · line 853

Quoted 11 lines
net/ceph/auth_x.c +3 -1
+                                  p, *p + len);
              if (ret < 0)
                      return ret;
 
@@ -820,7 +849,9 @@ static int decrypt_authorizer_challenge(struct ceph_crypto_key *secret,
      int ret;
 
      /* no leading len */
-     ret = __ceph_x_decrypt(secret, challenge, challenge_len);
+     ret = __ceph_x_decrypt(secret,
+                            1 /* CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE */,

Ditto.

net/ceph/auth_x.c static int decrypt_authorizer_reply(struct ceph_crypto_key *secret, · line 907

Quoted 10 lines
net/ceph/auth_x.c +2 -1
+                            challenge, challenge_len);
      if (ret < 0)
              return ret;
 
@@ -873,7 +904,8 @@ static int decrypt_authorizer_reply(struct ceph_crypto_key *secret,
      int ret;
 
      dp = *p + ceph_x_encrypt_offset(secret);
-     ret = ceph_x_decrypt(secret, p, end);
+     ret = ceph_x_decrypt(secret, 2 /* CEPHX_KEY_USAGE_AUTHORIZE_REPLY */,

Ditto.

net/ceph/auth_x.c static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, · line 1039

Quoted 11 lines
net/ceph/auth_x.c +2 -2
+                          p, end);
      if (ret < 0)
              return ret;
 
@@ -1004,8 +1036,9 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
              sigblock->middle_crc = msg->footer.middle_crc;
              sigblock->data_crc =  msg->footer.data_crc;
 
-             ret = ceph_x_encrypt(&au->session_key, enc_buf,
-                                  CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock));
+             ret = ceph_x_encrypt(&au->session_key, 0 /* dummy */,

Ditto.

net/ceph/auth_x.c static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg, · line 1072

Quoted 13 lines
net/ceph/auth_x.c +3 -3
+                                  enc_buf, CEPHX_AU_ENC_BUF_LEN,
+                                  sizeof(*sigblock));
              if (ret < 0)
                      return ret;
 
@@ -1036,9 +1069,9 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
              sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
 
              /* no leading len, no ceph_x_encrypt_header */
-             ret = ceph_crypt(&au->session_key, true, enc_buf,
-                              CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock),
-                              &ciphertext_len);
+             ret = ceph_crypt(&au->session_key, 0 /* dummy */,

Ditto.

Quoted 162 lines
net/ceph/auth_x.c +4 -1
+                              true, enc_buf, CEPHX_AU_ENC_BUF_LEN,
+                              sizeof(*sigblock), &ciphertext_len);
              if (ret)
                      return ret;
 
@@ -1130,7 +1163,8 @@ int ceph_x_init(struct ceph_auth_client *ac)
              goto err_xi;
      }
 
-     ret = ceph_crypto_key_prepare(&xi->secret);
+     ret = ceph_crypto_key_prepare(&xi->secret, client_key_usages,
+                                   ARRAY_SIZE(client_key_usages));
      if (ret) {
              pr_err("cannot prepare key: %d\n", ret);
              goto err_secret;
net/ceph/auth_x_protocol.h +38 -0
index 9c60feeb1bcb..d097b3651c99 100644
@@ -6,6 +6,44 @@
 #define CEPHX_GET_PRINCIPAL_SESSION_KEY 0x0200
 #define CEPHX_GET_ROTATING_KEY          0x0400
 
+/* Client <-> AuthMonitor */
+/*
+ * The AUTH session's connection secret: encrypted with the AUTH
+ * ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTH_CONNECTION_SECRET       0x03
+/*
+ * The ticket's blob for the client ("blob for me", contains the
+ * session key): encrypted with the client's secret key in case of
+ * the AUTH ticket and the AUTH ticket session key in case of other
+ * service tickets
+ */
+#define CEPHX_KEY_USAGE_TICKET_SESSION_KEY   0x04
+/*
+ * The ticket's blob for the service (ceph_x_ticket_blob): possibly
+ * encrypted with the old AUTH ticket session key in case of the AUTH
+ * ticket and not encrypted in case of other service tickets
+ */
+#define CEPHX_KEY_USAGE_TICKET_BLOB          0x05
+
+/* Client <-> Service */
+/*
+ * The client's authorization request (ceph_x_authorize_b):
+ * encrypted with the service ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE            0x10
+/*
+ * The service's challenge (ceph_x_authorize_challenge):
+ * encrypted with the service ticket session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE_CHALLENGE  0x11
+/*
+ * The service's final reply (ceph_x_authorize_reply + the service
+ * session's connection secret): encrypted with the service ticket
+ * session key
+ */
+#define CEPHX_KEY_USAGE_AUTHORIZE_REPLY              0x12
+
 /* common bits */
 struct ceph_x_ticket_blob {
      __u8 struct_v;
net/ceph/crypto.c +48 -12
index 3453dc303315..b54085d8d5f0 100644
@@ -7,6 +7,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <crypto/aes.h>
+#include <crypto/krb5.h>
 #include <crypto/skcipher.h>
 #include <linux/key-type.h>
 #include <linux/sched/mm.h>
@@ -22,28 +23,68 @@ static int set_aes_tfm(struct ceph_crypto_key *key)
      int ret;
 
      noio_flag = memalloc_noio_save();
-     key->tfm = crypto_alloc_sync_skcipher("cbc(aes)", 0, 0);
+     key->aes_tfm = crypto_alloc_sync_skcipher("cbc(aes)", 0, 0);
      memalloc_noio_restore(noio_flag);
-     if (IS_ERR(key->tfm)) {
-             ret = PTR_ERR(key->tfm);
-             key->tfm = NULL;
+     if (IS_ERR(key->aes_tfm)) {
+             ret = PTR_ERR(key->aes_tfm);
+             key->aes_tfm = NULL;
              return ret;
      }
 
-     ret = crypto_sync_skcipher_setkey(key->tfm, key->key, key->len);
+     ret = crypto_sync_skcipher_setkey(key->aes_tfm, key->key, key->len);
      if (ret)
              return ret;
 
      return 0;
 }
 
-int ceph_crypto_key_prepare(struct ceph_crypto_key *key)
+static int set_krb5_tfms(struct ceph_crypto_key *key, const u32 *key_usages,
+                      int key_usage_cnt)
+{
+     struct krb5_buffer TK = { .len = key->len, .data = key->key };
+     unsigned int noio_flag;
+     int ret = 0;
+     int i;
+
+     if (WARN_ON_ONCE(key_usage_cnt > ARRAY_SIZE(key->krb5_tfms)))
+             return -EINVAL;
+
+     key->krb5_type = crypto_krb5_find_enctype(
+                          KRB5_ENCTYPE_AES256_CTS_HMAC_SHA384_192);
+     if (!key->krb5_type)
+             return -ENOPKG;
+
+     /*
+      * Despite crypto_krb5_prepare_encryption() taking a gfp mask,
+      * crypto_alloc_aead() inside of it allocates with GFP_KERNEL.
+      */
+     noio_flag = memalloc_noio_save();
+     for (i = 0; i < key_usage_cnt; i++) {
+             key->krb5_tfms[i] = crypto_krb5_prepare_encryption(
+                                     key->krb5_type, &TK, key_usages[i],
+                                     GFP_NOIO);
+             if (IS_ERR(key->krb5_tfms[i])) {
+                     ret = PTR_ERR(key->krb5_tfms[i]);
+                     key->krb5_tfms[i] = NULL;
+                     goto out_flag;
+             }
+     }
+
+out_flag:
+     memalloc_noio_restore(noio_flag);
+     return ret;
+}
+
+int ceph_crypto_key_prepare(struct ceph_crypto_key *key,
+                         const u32 *key_usages, int key_usage_cnt)
 {
      switch (key->type) {
      case CEPH_CRYPTO_NONE:
              return 0; /* nothing to do */
      case CEPH_CRYPTO_AES:
              return set_aes_tfm(key);
+     case CEPH_CRYPTO_AES256KRB5:
+             return set_krb5_tfms(key, key_usages, key_usage_cnt);
      default:
              return -ENOTSUPP;
      }
@@ -123,12 +164,25 @@ int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey)
 
 void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
 {
-     if (key) {
-             kfree_sensitive(key->key);
-             key->key = NULL;
-             if (key->tfm) {
-                     crypto_free_sync_skcipher(key->tfm);
-                     key->tfm = NULL;
+     int i;
Quoted 8 lines

net/ceph/crypto.c int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey) · line 173

Quoted 6 lines
net/ceph/crypto.c +6 -0
+
+     if (!key)
+             return;
+
+     kfree_sensitive(key->key);
+     key->key = NULL;

The key->key looks strange. :) Maybe, another name like crypto_key?

It could certainly be named differently, but this name goes back to 2010. I don't think a wholesale rename is in scope for this series.

Quoted 38 lines
Quoted 38 lines
net/ceph/crypto.c +14 -2
+
+     if (key->type == CEPH_CRYPTO_AES) {
+             if (key->aes_tfm) {
+                     crypto_free_sync_skcipher(key->aes_tfm);
+                     key->aes_tfm = NULL;
+             }
+     } else if (key->type == CEPH_CRYPTO_AES256KRB5) {
+             for (i = 0; i < ARRAY_SIZE(key->krb5_tfms); i++) {
+                     if (key->krb5_tfms[i]) {
+                             crypto_free_aead(key->krb5_tfms[i]);
+                             key->krb5_tfms[i] = NULL;
+                     }
              }
      }
 }
@@ -208,7 +262,7 @@ static void teardown_sgtable(struct sg_table *sgt)
 static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
                        void *buf, int buf_len, int in_len, int *pout_len)
 {
-     SYNC_SKCIPHER_REQUEST_ON_STACK(req, key->tfm);
+     SYNC_SKCIPHER_REQUEST_ON_STACK(req, key->aes_tfm);
      struct sg_table sgt;
      struct scatterlist prealloc_sg;
      char iv[AES_BLOCK_SIZE] __aligned(8);
@@ -224,7 +278,7 @@ static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
              return ret;
 
      memcpy(iv, aes_iv, AES_BLOCK_SIZE);
-     skcipher_request_set_sync_tfm(req, key->tfm);
+     skcipher_request_set_sync_tfm(req, key->aes_tfm);
      skcipher_request_set_callback(req, 0, NULL, NULL);
      skcipher_request_set_crypt(req, sgt.sgl, sgt.sgl, crypt_len, iv);
 
@@ -269,7 +323,68 @@ static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt,
      return ret;
 }
 
Quoted 6 lines

net/ceph/crypto.c static int ceph_aes_crypt(const struct ceph_crypto_key *key, bool encrypt, · line 327

Quoted 3 lines
net/ceph/crypto.c +2 -1
-int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
+static int ceph_krb5_encrypt(const struct ceph_crypto_key *key, int usage_slot,
+                          void *buf, int buf_len, int in_len, int *pout_len)

If we have really many function's arguments, then, maybe, we need to introduce some structure?

I wouldn't say that six arguments is "really many" -- on x86-64 they all are likely to be passed in registers and it's a rather manageable number in general IMO. See how the crypto API method that this is built upon takes eight arguments, for example.

Quoted 77 lines
Quoted 77 lines
net/ceph/crypto.c +66 -0
+{
+     struct sg_table sgt;
+     struct scatterlist prealloc_sg;
+     int ret;
+
+     if (WARN_ON_ONCE(usage_slot >= ARRAY_SIZE(key->krb5_tfms)))
+             return -EINVAL;
+
+     ret = setup_sgtable(&sgt, &prealloc_sg, buf, buf_len);
+     if (ret)
+             return ret;
+
+     ret = crypto_krb5_encrypt(key->krb5_type, key->krb5_tfms[usage_slot],
+                               sgt.sgl, sgt.nents, buf_len, AES_BLOCK_SIZE,
+                               in_len, false);
+     if (ret < 0) {
+             pr_err("%s encrypt failed: %d\n", __func__, ret);
+             goto out_sgt;
+     }
+
+     *pout_len = ret;
+     ret = 0;
+
+out_sgt:
+     teardown_sgtable(&sgt);
+     return ret;
+}
+
+static int ceph_krb5_decrypt(const struct ceph_crypto_key *key, int usage_slot,
+                          void *buf, int buf_len, int in_len, int *pout_len)
+{
+     struct sg_table sgt;
+     struct scatterlist prealloc_sg;
+     size_t data_off = 0;
+     size_t data_len = in_len;
+     int ret;
+
+     if (WARN_ON_ONCE(usage_slot >= ARRAY_SIZE(key->krb5_tfms)))
+             return -EINVAL;
+
+     ret = setup_sgtable(&sgt, &prealloc_sg, buf, in_len);
+     if (ret)
+             return ret;
+
+     ret = crypto_krb5_decrypt(key->krb5_type, key->krb5_tfms[usage_slot],
+                               sgt.sgl, sgt.nents, &data_off, &data_len);
+     if (ret) {
+             pr_err("%s decrypt failed: %d\n", __func__, ret);
+             goto out_sgt;
+     }
+
+     WARN_ON(data_off != AES_BLOCK_SIZE);
+     *pout_len = data_len;
+
+out_sgt:
+     teardown_sgtable(&sgt);
+     return ret;
+}
+
+int ceph_crypt(const struct ceph_crypto_key *key, int usage_slot, bool encrypt,
             void *buf, int buf_len, int in_len, int *pout_len)
 {
      switch (key->type) {
@@ -279,6 +394,12 @@ int ceph_crypt(const struct ceph_crypto_key *key, bool encrypt,
      case CEPH_CRYPTO_AES:
              return ceph_aes_crypt(key, encrypt, buf, buf_len, in_len,
                                    pout_len);
+     case CEPH_CRYPTO_AES256KRB5:
+             return encrypt ?
+                 ceph_krb5_encrypt(key, usage_slot, buf, buf_len, in_len,
+                                   pout_len) :
+                 ceph_krb5_decrypt(key, usage_slot, buf, buf_len, in_len,
+                                   pout_len);
      default:
              return -ENOTSUPP;
      }
Quoted 8 lines

net/ceph/crypto.c int ceph_crypt_data_offset(const struct ceph_crypto_key *key) · line 415

Quoted 6 lines
net/ceph/crypto.c +2 -0
@@ -290,6 +411,9 @@ int ceph_crypt_data_offset(const struct ceph_crypto_key *key)
      case CEPH_CRYPTO_NONE:
      case CEPH_CRYPTO_AES:
              return 0;
+     case CEPH_CRYPTO_AES256KRB5:
+             /* confounder */

Frankly speaking, I don't follow what this comment imply. :)

Confounder is a random string that the plaintext is prepended with before being encrypted. This is an established term, it's used in RFC 8009 and some other places. Think of it as a kind of initialization vector (IV) in the context of CBC encryption.

The comment then implies that in case of CEPH_CRYPTO_AES256KRB5 the space that ceph_crypt_data_offset() accounts for gets taken up by the confounder.

Thanks,

Ilya

author maintainer
#11 lore raw

PATCH 5/5 libceph: adapt ceph_x_challenge_blob hashing and msgr1 message signing

In reply to Ilya Dryomov

The existing approach where ceph_x_challenge_blob is encrypted with the client's secret key and then the digest derived from the ciphertext is used for the test doesn't work with CEPH_CRYPTO_AES256KRB5 because the confounder randomizes the ciphertext: the client and the server get two different ciphertexts and therefore two different digests.

msgr1 signatures are affected the same way: a digest derived from the ciphertext for the message's "sigblock" is what becomes a signature and the two sides disagree on the expected value.

For CEPH_CRYPTO_AES256KRB5 (and potential future encryption schemes), switch to HMAC-SHA256 function keyed in the same way as the existing encryption. For CEPH_CRYPTO_AES, everything is preserved as is.

---
 net/ceph/auth_x.c       | 59 ++++++++++++++++++++++++++++++-----------
 net/ceph/crypto.c       | 18 +++++++++++++
 net/ceph/crypto.h       |  4 +++
 net/ceph/messenger_v2.c | 14 +++++-----
 4 files changed, 72 insertions(+), 23 deletions(-)
net/ceph/auth_x.c +43 -16
index decd2867f8f1..13b3df9af0ac 100644
@@ -553,8 +553,7 @@ static int ceph_x_build_request(struct ceph_auth_client *ac,
 	if (need & CEPH_ENTITY_TYPE_AUTH) {
 		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(&xi->secret);
+		struct ceph_x_challenge_blob *blob;
 		u64 *u;
 
 		p = auth + 1;
@@ -564,15 +563,29 @@ static int ceph_x_build_request(struct ceph_auth_client *ac,
 		dout(" get_auth_session_key\n");
 		head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
 
-		/* encrypt and hash */
+		if (xi->secret.type == CEPH_CRYPTO_AES) {
+			blob = enc_buf + ceph_x_encrypt_offset(&xi->secret);
+		} else {
+			BUILD_BUG_ON(SHA256_DIGEST_SIZE + sizeof(*blob) >
+				     CEPHX_AU_ENC_BUF_LEN);
+			blob = enc_buf + SHA256_DIGEST_SIZE;
+		}
+
 		get_random_bytes(&auth->client_challenge, sizeof(u64));
 		blob->client_challenge = auth->client_challenge;
 		blob->server_challenge = cpu_to_le64(xi->server_challenge);
-		ret = ceph_x_encrypt(&xi->secret, 0 /* dummy */,
-				     enc_buf, CEPHX_AU_ENC_BUF_LEN,
-				     sizeof(*blob));
-		if (ret < 0)
-			return ret;
+
+		if (xi->secret.type == CEPH_CRYPTO_AES) {
+			ret = ceph_x_encrypt(&xi->secret, 0 /* dummy */,
+					     enc_buf, CEPHX_AU_ENC_BUF_LEN,
+					     sizeof(*blob));
+			if (ret < 0)
+				return ret;
+		} else {
+			ceph_hmac_sha256(&xi->secret, blob, sizeof(*blob),
+					 enc_buf);
+			ret = SHA256_DIGEST_SIZE;
+		}
 
 		auth->struct_v = 3;  /* nautilus+ */
 		auth->key = 0;
@@ -1053,11 +1066,19 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
 			__le32 data_crc;
 			__le32 data_len;
 			__le32 seq_lower_word;
-		} __packed *sigblock = enc_buf;
+		} __packed *sigblock;
 		struct {
 			__le64 a, b, c, d;
 		} __packed *penc = enc_buf;
-		int ciphertext_len;
+
+		if (au->session_key.type == CEPH_CRYPTO_AES) {
+			/* no leading len, no ceph_x_encrypt_header */
+			sigblock = enc_buf;
+		} else {
+			BUILD_BUG_ON(SHA256_DIGEST_SIZE + sizeof(*sigblock) >
+				     CEPHX_AU_ENC_BUF_LEN);
+			sigblock = enc_buf + SHA256_DIGEST_SIZE;
+		}
 
 		sigblock->header_crc = msg->hdr.crc;
 		sigblock->front_crc = msg->footer.front_crc;
@@ -1068,12 +1089,18 @@ static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
 		sigblock->data_len = msg->hdr.data_len;
 		sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
 
-		/* no leading len, no ceph_x_encrypt_header */
-		ret = ceph_crypt(&au->session_key, 0 /* dummy */,
-				 true, enc_buf, CEPHX_AU_ENC_BUF_LEN,
-				 sizeof(*sigblock), &ciphertext_len);
-		if (ret)
-			return ret;
+		if (au->session_key.type == CEPH_CRYPTO_AES) {
+			int ciphertext_len; /* unused */
+
+			ret = ceph_crypt(&au->session_key, 0 /* dummy */,
+					 true, enc_buf, CEPHX_AU_ENC_BUF_LEN,
+					 sizeof(*sigblock), &ciphertext_len);
+			if (ret)
+				return ret;
+		} else {
+			ceph_hmac_sha256(&au->session_key, sigblock,
+					 sizeof(*sigblock), enc_buf);
+		}
 
 		*psig = penc->a ^ penc->b ^ penc->c ^ penc->d;
 	}
net/ceph/crypto.c +18 -0
index b54085d8d5f0..b2067ea6c38a 100644
@@ -84,6 +84,7 @@ int ceph_crypto_key_prepare(struct ceph_crypto_key *key,
 	case CEPH_CRYPTO_AES:
 		return set_aes_tfm(key);
 	case CEPH_CRYPTO_AES256KRB5:
+		hmac_sha256_preparekey(&key->hmac_key, key->key, key->len);
 		return set_krb5_tfms(key, key_usages, key_usage_cnt);
 	default:
 		return -ENOTSUPP;
@@ -178,6 +179,7 @@ void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
 			key->aes_tfm = NULL;
 		}
 	} else if (key->type == CEPH_CRYPTO_AES256KRB5) {
+		memzero_explicit(&key->hmac_key, sizeof(key->hmac_key));
 		for (i = 0; i < ARRAY_SIZE(key->krb5_tfms); i++) {
 			if (key->krb5_tfms[i]) {
 				crypto_free_aead(key->krb5_tfms[i]);
@@ -436,6 +438,22 @@ int ceph_crypt_buflen(const struct ceph_crypto_key *key, int data_len)
 	}
 }
 
+void ceph_hmac_sha256(const struct ceph_crypto_key *key, const void *buf,
+		      int buf_len, u8 hmac[SHA256_DIGEST_SIZE])
+{
+	switch (key->type) {
+	case CEPH_CRYPTO_NONE:
+	case CEPH_CRYPTO_AES:
+		memset(hmac, 0, SHA256_DIGEST_SIZE);
+		return;
+	case CEPH_CRYPTO_AES256KRB5:
+		hmac_sha256(&key->hmac_key, buf, buf_len, hmac);
+		return;
+	default:
+		BUG();
+	}
+}
+
 static int ceph_key_preparse(struct key_preparsed_payload *prep)
 {
 	struct ceph_crypto_key *ckey;
net/ceph/crypto.h +4 -0
index 2c37c54d0f56..3a2ade15abbc 100644
@@ -2,6 +2,7 @@
 #ifndef _FS_CEPH_CRYPTO_H
 #define _FS_CEPH_CRYPTO_H
 
+#include <crypto/sha2.h>
 #include <linux/ceph/types.h>
 #include <linux/ceph/buffer.h>
 
@@ -20,6 +21,7 @@ struct ceph_crypto_key {
 	union {
 		struct crypto_sync_skcipher *aes_tfm;
 		struct {
+			struct hmac_sha256_key hmac_key;
 			const struct krb5_enctype *krb5_type;
 			struct crypto_aead *krb5_tfms[3];
 		};
@@ -39,6 +41,8 @@ int ceph_crypt(const struct ceph_crypto_key *key, int usage_slot, 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);
+void ceph_hmac_sha256(const struct ceph_crypto_key *key, const void *buf,
+		      int buf_len, u8 hmac[SHA256_DIGEST_SIZE]);
 int ceph_crypto_init(void);
 void ceph_crypto_shutdown(void);
 
net/ceph/messenger_v2.c +7 -7
index 31e042dc1b3f..5ec3272cd2dd 100644
@@ -779,9 +779,9 @@ static int setup_crypto(struct ceph_connection *con,
 	return 0;  /* auth_x, secure mode */
 }
 
-static void ceph_hmac_sha256(struct ceph_connection *con,
-			     const struct kvec *kvecs, int kvec_cnt,
-			     u8 hmac[SHA256_DIGEST_SIZE])
+static void con_hmac_sha256(struct ceph_connection *con,
+			    const struct kvec *kvecs, int kvec_cnt,
+			    u8 hmac[SHA256_DIGEST_SIZE])
 {
 	struct hmac_sha256_ctx ctx;
 	int i;
@@ -1438,8 +1438,8 @@ static int prepare_auth_signature(struct ceph_connection *con)
 	if (!buf)
 		return -ENOMEM;
 
-	ceph_hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
-			 CTRL_BODY(buf));
+	con_hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
+			CTRL_BODY(buf));
 
 	return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf,
 			       SHA256_DIGEST_SIZE);
@@ -2436,8 +2436,8 @@ static int process_auth_signature(struct ceph_connection *con,
 		return -EINVAL;
 	}
 
-	ceph_hmac_sha256(con, con->v2.out_sign_kvecs, con->v2.out_sign_kvec_cnt,
-			 hmac);
+	con_hmac_sha256(con, con->v2.out_sign_kvecs, con->v2.out_sign_kvec_cnt,
+			hmac);
 
 	ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad);
 	if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) {
Signature · 1 line
2.49.0