Skip to content

Commit 27c0a7b

Browse files
Eric Biggersidryomov
authored andcommitted
libceph: Use HMAC-SHA256 library instead of crypto_shash
Use the HMAC-SHA256 library functions instead of crypto_shash. This is simpler and faster. Signed-off-by: Eric Biggers <[email protected]> Reviewed-by: Viacheslav Dubeyko <[email protected]> Signed-off-by: Ilya Dryomov <[email protected]>
1 parent e5f0a69 commit 27c0a7b

3 files changed

Lines changed: 26 additions & 58 deletions

File tree

include/linux/ceph/messenger.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __FS_CEPH_MESSENGER_H
33
#define __FS_CEPH_MESSENGER_H
44

5+
#include <crypto/sha2.h>
56
#include <linux/bvec.h>
67
#include <linux/crypto.h>
78
#include <linux/kref.h>
@@ -412,7 +413,8 @@ struct ceph_connection_v2_info {
412413
struct ceph_msg_data_cursor in_cursor;
413414
struct ceph_msg_data_cursor out_cursor;
414415

415-
struct crypto_shash *hmac_tfm; /* post-auth signature */
416+
struct hmac_sha256_key hmac_key; /* post-auth signature */
417+
bool hmac_key_set;
416418
struct crypto_aead *gcm_tfm; /* on-wire encryption */
417419
struct aead_request *gcm_req;
418420
struct crypto_wait gcm_wait;

net/ceph/Kconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ config CEPH_LIB
66
select CRYPTO_AES
77
select CRYPTO_CBC
88
select CRYPTO_GCM
9-
select CRYPTO_HMAC
10-
select CRYPTO_SHA256
9+
select CRYPTO_LIB_SHA256
1110
select CRYPTO
1211
select KEYS
1312
default n

net/ceph/messenger_v2.c

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ static int setup_crypto(struct ceph_connection *con,
709709

710710
dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n",
711711
__func__, con, con->v2.con_mode, session_key_len, con_secret_len);
712-
WARN_ON(con->v2.hmac_tfm || con->v2.gcm_tfm || con->v2.gcm_req);
712+
WARN_ON(con->v2.hmac_key_set || con->v2.gcm_tfm || con->v2.gcm_req);
713713

714714
if (con->v2.con_mode != CEPH_CON_MODE_CRC &&
715715
con->v2.con_mode != CEPH_CON_MODE_SECURE) {
@@ -723,22 +723,8 @@ static int setup_crypto(struct ceph_connection *con,
723723
return 0; /* auth_none */
724724
}
725725

726-
noio_flag = memalloc_noio_save();
727-
con->v2.hmac_tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
728-
memalloc_noio_restore(noio_flag);
729-
if (IS_ERR(con->v2.hmac_tfm)) {
730-
ret = PTR_ERR(con->v2.hmac_tfm);
731-
con->v2.hmac_tfm = NULL;
732-
pr_err("failed to allocate hmac tfm context: %d\n", ret);
733-
return ret;
734-
}
735-
736-
ret = crypto_shash_setkey(con->v2.hmac_tfm, session_key,
737-
session_key_len);
738-
if (ret) {
739-
pr_err("failed to set hmac key: %d\n", ret);
740-
return ret;
741-
}
726+
hmac_sha256_preparekey(&con->v2.hmac_key, session_key, session_key_len);
727+
con->v2.hmac_key_set = true;
742728

743729
if (con->v2.con_mode == CEPH_CON_MODE_CRC) {
744730
WARN_ON(con_secret_len);
@@ -793,38 +779,26 @@ static int setup_crypto(struct ceph_connection *con,
793779
return 0; /* auth_x, secure mode */
794780
}
795781

796-
static int ceph_hmac_sha256(struct ceph_connection *con,
797-
const struct kvec *kvecs, int kvec_cnt, u8 *hmac)
782+
static void ceph_hmac_sha256(struct ceph_connection *con,
783+
const struct kvec *kvecs, int kvec_cnt,
784+
u8 hmac[SHA256_DIGEST_SIZE])
798785
{
799-
SHASH_DESC_ON_STACK(desc, con->v2.hmac_tfm); /* tfm arg is ignored */
800-
int ret;
786+
struct hmac_sha256_ctx ctx;
801787
int i;
802788

803-
dout("%s con %p hmac_tfm %p kvec_cnt %d\n", __func__, con,
804-
con->v2.hmac_tfm, kvec_cnt);
789+
dout("%s con %p hmac_key_set %d kvec_cnt %d\n", __func__, con,
790+
con->v2.hmac_key_set, kvec_cnt);
805791

806-
if (!con->v2.hmac_tfm) {
792+
if (!con->v2.hmac_key_set) {
807793
memset(hmac, 0, SHA256_DIGEST_SIZE);
808-
return 0; /* auth_none */
794+
return; /* auth_none */
809795
}
810796

811-
desc->tfm = con->v2.hmac_tfm;
812-
ret = crypto_shash_init(desc);
813-
if (ret)
814-
goto out;
815-
816-
for (i = 0; i < kvec_cnt; i++) {
817-
ret = crypto_shash_update(desc, kvecs[i].iov_base,
818-
kvecs[i].iov_len);
819-
if (ret)
820-
goto out;
821-
}
822-
823-
ret = crypto_shash_final(desc, hmac);
824-
825-
out:
826-
shash_desc_zero(desc);
827-
return ret; /* auth_x, both plain and secure modes */
797+
/* auth_x, both plain and secure modes */
798+
hmac_sha256_init(&ctx, &con->v2.hmac_key);
799+
for (i = 0; i < kvec_cnt; i++)
800+
hmac_sha256_update(&ctx, kvecs[i].iov_base, kvecs[i].iov_len);
801+
hmac_sha256_final(&ctx, hmac);
828802
}
829803

830804
static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce)
@@ -1455,17 +1429,14 @@ static int prepare_auth_request_more(struct ceph_connection *con,
14551429
static int prepare_auth_signature(struct ceph_connection *con)
14561430
{
14571431
void *buf;
1458-
int ret;
14591432

14601433
buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE,
14611434
con_secure(con)));
14621435
if (!buf)
14631436
return -ENOMEM;
14641437

1465-
ret = ceph_hmac_sha256(con, con->v2.in_sign_kvecs,
1466-
con->v2.in_sign_kvec_cnt, CTRL_BODY(buf));
1467-
if (ret)
1468-
return ret;
1438+
ceph_hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
1439+
CTRL_BODY(buf));
14691440

14701441
return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf,
14711442
SHA256_DIGEST_SIZE);
@@ -2460,10 +2431,8 @@ static int process_auth_signature(struct ceph_connection *con,
24602431
return -EINVAL;
24612432
}
24622433

2463-
ret = ceph_hmac_sha256(con, con->v2.out_sign_kvecs,
2464-
con->v2.out_sign_kvec_cnt, hmac);
2465-
if (ret)
2466-
return ret;
2434+
ceph_hmac_sha256(con, con->v2.out_sign_kvecs, con->v2.out_sign_kvec_cnt,
2435+
hmac);
24672436

24682437
ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad);
24692438
if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) {
@@ -3814,10 +3783,8 @@ void ceph_con_v2_reset_protocol(struct ceph_connection *con)
38143783
memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN);
38153784
memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN);
38163785

3817-
if (con->v2.hmac_tfm) {
3818-
crypto_free_shash(con->v2.hmac_tfm);
3819-
con->v2.hmac_tfm = NULL;
3820-
}
3786+
memzero_explicit(&con->v2.hmac_key, sizeof(con->v2.hmac_key));
3787+
con->v2.hmac_key_set = false;
38213788
if (con->v2.gcm_req) {
38223789
aead_request_free(con->v2.gcm_req);
38233790
con->v2.gcm_req = NULL;

0 commit comments

Comments
 (0)