Skip to contentSkip to conversation

mptcp: simplify crypto.c

PATCH In review Davide Caratti 2 participants 4 messages Jul 23, 2019 – Jul 24, 2019

Subsystems (2)
lore
Share & tools
lore

Davide Caratti

author
#0 lore raw

v2 cover mptcp: simplify crypto.c

his series wants to simplify, and hopefully improve, random generation of keys and nonces in crypto.c, While at it, I removed some unneeded #includes on top of the file. Any feedbacks are welcome!

Note:
in case you think the code is ok, I will propose to squash it into commit a564283a1e38 ("mptcp: Add key generation and token tree")

Changes since v1:
- add a comment that suggests use of hash to improve performance, thanks to Matthieu
- remove useless #include <tcp.h>, thanks Mat

Changes since RFC:
- use get_random_bytes() instead of siphash, thanks to Florian / Mat

Davide Caratti (2):
net: mptcp: remove useless #includes in crypto.c
net: mptcp: randomness improvements for crypto.c

 net/mptcp/crypto.c   | 76 --------------------------------------------
 net/mptcp/protocol.c |  1 -
 net/mptcp/protocol.h | 24 +++++++-------
 net/mptcp/token.c    | 54 ++++---------------------------
 4 files changed, 19 insertions(+), 136 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v2 1/2 net: mptcp: remove useless #includes in crypto.c

- crypto.c doesn't need module.h (that was included twice)
- crypto.c does'nt need netdevice.h
- remove duplicate include of kernel.h

Signed-off-by Davide Caratti <dcaratti(a)redhat.com>
---
 net/mptcp/crypto.c | 4 ----
 1 file changed, 4 deletions(-)
net/mptcp/crypto.c +0 -4
index 26a68aa1933a..e8644590690f 100644
@@ -21,10 +21,6 @@
  */
 
 #include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/netdevice.h>
-#include <linux/kernel.h>
-#include <linux/module.h>
 #include <linux/cryptohash.h>
 #include <linux/random.h>
 #include <linux/siphash.h>
Signature · 1 line
2.20.1
author
#2 lore raw

PATCH v2 2/2 net: mptcp: randomness improvements for crypto.c

In reply to Davide Caratti

use get_random_bytes(), instead of siphash, for MPTCP keys and nonces. This should improve MPTCP key/nonce randomness, because seeds are no more initialized at a fixed time during the boot process, and we also get rid of 'static u32 crypto_seed' (that can theoretically clash in case sockets were requesting the key from different namespaces).

CC Mat Martineau <mathew.j.martineau(a)linux.intel.com>
CC Florian Westphal <fwestpha(a)redhat.com>
CC Paolo Abeni <pabeni(a)redhat.com>
Signed-off-by Davide Caratti <dcaratti(a)redhat.com>
---
 net/mptcp/crypto.c   | 72 --------------------------------------------
 net/mptcp/protocol.c |  1 -
 net/mptcp/protocol.h | 24 ++++++++-------
 net/mptcp/token.c    | 54 ++++-----------------------------
 4 files changed, 19 insertions(+), 132 deletions(-)
net/mptcp/crypto.c +0 -72
index e8644590690f..5b764798c536 100644
@@ -26,69 +26,6 @@
 #include <linux/siphash.h>
 #include <asm/unaligned.h>
 
-static siphash_key_t crypto_key_secret __read_mostly;
-static hsiphash_key_t crypto_nonce_secret __read_mostly;
-static u32 crypto_seed;
-
-u32 crypto_v4_get_nonce(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport)
-{
-	return hsiphash_4u32((__force u32)saddr, (__force u32)daddr,
-			    (__force u32)sport << 16 | (__force u32)dport,
-			    crypto_seed++, &crypto_nonce_secret);
-}
-
-u64 crypto_v4_get_key(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport)
-{
-	pr_debug("src=%x:%d, dst=%x:%d", saddr, sport, daddr, dport);
-	return siphash_4u32((__force u32)saddr, (__force u32)daddr,
-			    (__force u32)sport << 16 | (__force u32)dport,
-			    crypto_seed++, &crypto_key_secret);
-}
-
-u32 crypto_v6_get_nonce(const struct in6_addr *saddr,
-			const struct in6_addr *daddr,
-			__be16 sport, __be16 dport)
-{
-	const struct {
-		struct in6_addr saddr;
-		struct in6_addr daddr;
-		u32 seed;
-		__be16 sport;
-		__be16 dport;
-	} __aligned(SIPHASH_ALIGNMENT) combined = {
-		.saddr = *saddr,
-		.daddr = *daddr,
-		.seed = crypto_seed++,
-		.sport = sport,
-		.dport = dport,
-	};
-
-	return hsiphash(&combined, offsetofend(typeof(combined), dport),
-			&crypto_nonce_secret);
-}
-
-u64 crypto_v6_get_key(const struct in6_addr *saddr,
-		      const struct in6_addr *daddr,
-		      __be16 sport, __be16 dport)
-{
-	const struct {
-		struct in6_addr saddr;
-		struct in6_addr daddr;
-		u32 seed;
-		__be16 sport;
-		__be16 dport;
-	} __aligned(SIPHASH_ALIGNMENT) combined = {
-		.saddr = *saddr,
-		.daddr = *daddr,
-		.seed = crypto_seed++,
-		.sport = sport,
-		.dport = dport,
-	};
-
-	return siphash(&combined, offsetofend(typeof(combined), dport),
-		       &crypto_key_secret);
-}
-
 void crypto_key_sha1(u64 key, u32 *token, u64 *idsn)
 {
 	u32 workspace[SHA_WORKSPACE_WORDS];
@@ -191,12 +128,3 @@ void crypto_hmac_sha1(u64 key1, u64 key2, u32 *hash_out,
 	for (i = 0; i < 5; i++)
 		hash_out[i] = (__force u32)cpu_to_be32(hash_out[i]);
 }
-
-void crypto_init(void)
-{
-	get_random_bytes((void *)&crypto_key_secret,
-			 sizeof(crypto_key_secret));
-	get_random_bytes((void *)&crypto_nonce_secret,
-			 sizeof(crypto_nonce_secret));
-	crypto_seed = 0;
-}
net/mptcp/protocol.c +0 -1
index 774ed25d3b6d..7347da1e7a39 100644
@@ -1006,7 +1006,6 @@ void __init mptcp_init(void)
 	mptcp_stream_ops.shutdown = mptcp_shutdown;
 
 	token_init();
-	crypto_init();
 	subflow_init();
 
 	if (proto_register(&mptcp_prot, 1) != 0)
net/mptcp/protocol.h +13 -11
index 7f15f6aab93d..a28022451c87 100644
@@ -9,6 +9,7 @@
 
 #include <linux/spinlock.h>
 #include <net/tcp.h>
+#include <linux/random.h>
 
 /* MPTCP option bits */
 #define OPTION_MPTCP_MPC_SYN	BIT(0)
@@ -207,21 +208,22 @@ void token_update_accept(struct sock *sk, struct sock *conn);
 void token_release(u32 token);
 void token_destroy(u32 token);
 
-void crypto_init(void);
-u32 crypto_v4_get_nonce(__be32 saddr, __be32 daddr,
-			__be16 sport, __be16 dport);
-u64 crypto_v4_get_key(__be32 saddr, __be32 daddr,
-		      __be16 sport, __be16 dport);
-u64 crypto_v6_get_key(const struct in6_addr *saddr,
-		      const struct in6_addr *daddr,
-		      __be16 sport, __be16 dport);
-u32 crypto_v6_get_nonce(const struct in6_addr *saddr,
-			const struct in6_addr *daddr,
-			__be16 sport, __be16 dport);
 void crypto_key_sha1(u64 key, u32 *token, u64 *idsn);
 void crypto_hmac_sha1(u64 key1, u64 key2, u32 *hash_out,
 		      int arg_num, ...);
 
+static inline void crypto_key_gen_sha1(u64 *key, u32 *token, u64 *idsn)
+{
+	/* we might consider a faster version that computes the key as a
+	 * hash of some information available in the MPTCP socket. Use
+	 * random data at the moment, as it's probably the safest option
+	 * in case multiple sockets are opened in different namespaces at
+	 * the same time.
+	 */
+	get_random_bytes(key, sizeof(u64));
+	crypto_key_sha1(*key, token, idsn);
+}
+
 void pm_new_connection(struct mptcp_sock *msk);
 void pm_fully_established(struct mptcp_sock *msk);
 void pm_connection_closed(struct mptcp_sock *msk);
net/mptcp/token.c +6 -48

73 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw
In reply to Davide Caratti

Hi Davide, Mat, Peter, Florian,

Quoted 13 lines · Davide Caratti

his series wants to simplify, and hopefully improve, random generation of keys and nonces in crypto.c, While at it, I removed some unneeded #includes on top of the file. Any feedbacks are welcome!

Note:
in case you think the code is ok, I will propose to squash it into commit a564283a1e38 ("mptcp: Add key generation and token tree")

Changes since v1:
- add a comment that suggests use of hash to improve performance, thanks to Matthieu
- remove useless #include <tcp.h>, thanks Mat

Quoted 7 lines

Changes since RFC:
- use get_random_bytes() instead of siphash, thanks to Florian / Mat

Davide Caratti (2):
net: mptcp: remove useless #includes in crypto.c
net: mptcp: randomness improvements for crypto.c

Thank you for the patch, the new version and the reviews!

- 830d99ebce23: first patch squashed in "mptcp: Add key generation and token tree"
- 1395553e7bbb: signed-off
- 3957d8f5d7dc: second patch partly squashed in "mptcp: Add key generation and token tree" (with conflicts)
- 1e46a4a65f49: conflict in t/mptcp-Write-MPTCP-DSS-headers-to-outgoing-data-packets
- 6d9bd00afae6: conflict in t/mptcp-Make-connection_list-a-real-list-of-subflows
- 14b72baa0fbf: second patch partly squashed in "mptcp: Add handling of incoming MP_JOIN requests"
- 49e73e4934b4..7b0096b174c5: result

Tests are still OK!

Cheers,
Matt

Signature · 5 lines
Matthieu Baerts | R&D Engineer
matthieu.baerts(a)tessares.net
Tessares SA | Hybrid Access Solutions
www.tessares.net
1 Avenue Jean Monnet, 1348 Louvain-la-Neuve, Belgium