Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <ebiggers@kernel.org>
Reviewed-by: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com>
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>

authored by

Eric Biggers and committed by
Ilya Dryomov
27c0a7b0 e5f0a698

+26 -58
+3 -1
include/linux/ceph/messenger.h
··· 2 2 #ifndef __FS_CEPH_MESSENGER_H 3 3 #define __FS_CEPH_MESSENGER_H 4 4 5 + #include <crypto/sha2.h> 5 6 #include <linux/bvec.h> 6 7 #include <linux/crypto.h> 7 8 #include <linux/kref.h> ··· 413 412 struct ceph_msg_data_cursor in_cursor; 414 413 struct ceph_msg_data_cursor out_cursor; 415 414 416 - struct crypto_shash *hmac_tfm; /* post-auth signature */ 415 + struct hmac_sha256_key hmac_key; /* post-auth signature */ 416 + bool hmac_key_set; 417 417 struct crypto_aead *gcm_tfm; /* on-wire encryption */ 418 418 struct aead_request *gcm_req; 419 419 struct crypto_wait gcm_wait;
+1 -2
net/ceph/Kconfig
··· 6 6 select CRYPTO_AES 7 7 select CRYPTO_CBC 8 8 select CRYPTO_GCM 9 - select CRYPTO_HMAC 10 - select CRYPTO_SHA256 9 + select CRYPTO_LIB_SHA256 11 10 select CRYPTO 12 11 select KEYS 13 12 default n
+22 -55
net/ceph/messenger_v2.c
··· 709 709 710 710 dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n", 711 711 __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); 713 713 714 714 if (con->v2.con_mode != CEPH_CON_MODE_CRC && 715 715 con->v2.con_mode != CEPH_CON_MODE_SECURE) { ··· 723 723 return 0; /* auth_none */ 724 724 } 725 725 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; 742 728 743 729 if (con->v2.con_mode == CEPH_CON_MODE_CRC) { 744 730 WARN_ON(con_secret_len); ··· 779 793 return 0; /* auth_x, secure mode */ 780 794 } 781 795 782 - static int ceph_hmac_sha256(struct ceph_connection *con, 783 - const struct kvec *kvecs, int kvec_cnt, u8 *hmac) 796 + static void ceph_hmac_sha256(struct ceph_connection *con, 797 + const struct kvec *kvecs, int kvec_cnt, 798 + u8 hmac[SHA256_DIGEST_SIZE]) 784 799 { 785 - SHASH_DESC_ON_STACK(desc, con->v2.hmac_tfm); /* tfm arg is ignored */ 786 - int ret; 800 + struct hmac_sha256_ctx ctx; 787 801 int i; 788 802 789 - dout("%s con %p hmac_tfm %p kvec_cnt %d\n", __func__, con, 790 - con->v2.hmac_tfm, kvec_cnt); 803 + dout("%s con %p hmac_key_set %d kvec_cnt %d\n", __func__, con, 804 + con->v2.hmac_key_set, kvec_cnt); 791 805 792 - if (!con->v2.hmac_tfm) { 806 + if (!con->v2.hmac_key_set) { 793 807 memset(hmac, 0, SHA256_DIGEST_SIZE); 794 - return 0; /* auth_none */ 808 + return; /* auth_none */ 795 809 } 796 810 797 - desc->tfm = con->v2.hmac_tfm; 798 - ret = crypto_shash_init(desc); 799 - if (ret) 800 - goto out; 801 - 802 - for (i = 0; i < kvec_cnt; i++) { 803 - ret = crypto_shash_update(desc, kvecs[i].iov_base, 804 - kvecs[i].iov_len); 805 - if (ret) 806 - goto out; 807 - } 808 - 809 - ret = crypto_shash_final(desc, hmac); 810 - 811 - out: 812 - shash_desc_zero(desc); 813 - return ret; /* auth_x, both plain and secure modes */ 811 + /* auth_x, both plain and secure modes */ 812 + hmac_sha256_init(&ctx, &con->v2.hmac_key); 813 + for (i = 0; i < kvec_cnt; i++) 814 + hmac_sha256_update(&ctx, kvecs[i].iov_base, kvecs[i].iov_len); 815 + hmac_sha256_final(&ctx, hmac); 814 816 } 815 817 816 818 static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce) ··· 1429 1455 static int prepare_auth_signature(struct ceph_connection *con) 1430 1456 { 1431 1457 void *buf; 1432 - int ret; 1433 1458 1434 1459 buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE, 1435 1460 con_secure(con))); 1436 1461 if (!buf) 1437 1462 return -ENOMEM; 1438 1463 1439 - ret = ceph_hmac_sha256(con, con->v2.in_sign_kvecs, 1440 - con->v2.in_sign_kvec_cnt, CTRL_BODY(buf)); 1441 - if (ret) 1442 - return ret; 1464 + ceph_hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt, 1465 + CTRL_BODY(buf)); 1443 1466 1444 1467 return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf, 1445 1468 SHA256_DIGEST_SIZE); ··· 2431 2460 return -EINVAL; 2432 2461 } 2433 2462 2434 - ret = ceph_hmac_sha256(con, con->v2.out_sign_kvecs, 2435 - con->v2.out_sign_kvec_cnt, hmac); 2436 - if (ret) 2437 - return ret; 2463 + ceph_hmac_sha256(con, con->v2.out_sign_kvecs, con->v2.out_sign_kvec_cnt, 2464 + hmac); 2438 2465 2439 2466 ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad); 2440 2467 if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) { ··· 3783 3814 memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN); 3784 3815 memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN); 3785 3816 3786 - if (con->v2.hmac_tfm) { 3787 - crypto_free_shash(con->v2.hmac_tfm); 3788 - con->v2.hmac_tfm = NULL; 3789 - } 3817 + memzero_explicit(&con->v2.hmac_key, sizeof(con->v2.hmac_key)); 3818 + con->v2.hmac_key_set = false; 3790 3819 if (con->v2.gcm_req) { 3791 3820 aead_request_free(con->v2.gcm_req); 3792 3821 con->v2.gcm_req = NULL;