Commit 7cd23e53 authored by Jason A. Donenfeld's avatar Jason A. Donenfeld Committed by David S. Miller

secure_seq: use SipHash in place of MD5

This gives a clear speed and security improvement. Siphash is both
faster and is more solid crypto than the aging MD5.

Rather than manually filling MD5 buffers, for IPv6, we simply create
a layout by a simple anonymous struct, for which gcc generates
rather efficient code. For IPv4, we pass the values directly to the
short input convenience functions.

64-bit x86_64:
[    1.683628] secure_tcpv6_sequence_number_md5# cycles: 99563527
[    1.717350] secure_tcp_sequence_number_md5# cycles: 92890502
[    1.741968] secure_tcpv6_sequence_number_siphash# cycles: 67825362
[    1.762048] secure_tcp_sequence_number_siphash# cycles: 67485526

32-bit x86:
[    1.600012] secure_tcpv6_sequence_number_md5# cycles: 103227892
[    1.634219] secure_tcp_sequence_number_md5# cycles: 94732544
[    1.669102] secure_tcpv6_sequence_number_siphash# cycles: 96299384
[    1.700165] secure_tcp_sequence_number_siphash# cycles: 86015473
Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: David Miller <davem@davemloft.net>
Cc: David Laight <David.Laight@aculab.com>
Cc: Tom Herbert <tom@herbertland.com>
Cc: Hannes Frederic Sowa <hannes@stressinduktion.org>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1ae2324f
/*
* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/cryptohash.h> #include <linux/cryptohash.h>
...@@ -8,18 +12,18 @@ ...@@ -8,18 +12,18 @@
#include <linux/ktime.h> #include <linux/ktime.h>
#include <linux/string.h> #include <linux/string.h>
#include <linux/net.h> #include <linux/net.h>
#include <linux/siphash.h>
#include <net/secure_seq.h> #include <net/secure_seq.h>
#if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET) #if IS_ENABLED(CONFIG_IPV6) || IS_ENABLED(CONFIG_INET)
#include <linux/in6.h>
#include <net/tcp.h> #include <net/tcp.h>
#define NET_SECRET_SIZE (MD5_MESSAGE_BYTES / 4)
static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned; static siphash_key_t net_secret __read_mostly;
static __always_inline void net_secret_init(void) static __always_inline void net_secret_init(void)
{ {
net_get_random_once(net_secret, sizeof(net_secret)); net_get_random_once(&net_secret, sizeof(net_secret));
} }
#endif #endif
...@@ -44,80 +48,70 @@ static u32 seq_scale(u32 seq) ...@@ -44,80 +48,70 @@ static u32 seq_scale(u32 seq)
u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr, u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
__be16 sport, __be16 dport, u32 *tsoff) __be16 sport, __be16 dport, u32 *tsoff)
{ {
u32 secret[MD5_MESSAGE_BYTES / 4]; const struct {
u32 hash[MD5_DIGEST_WORDS]; struct in6_addr saddr;
u32 i; struct in6_addr daddr;
__be16 sport;
__be16 dport;
} __aligned(SIPHASH_ALIGNMENT) combined = {
.saddr = *(struct in6_addr *)saddr,
.daddr = *(struct in6_addr *)daddr,
.sport = sport,
.dport = dport
};
u64 hash;
net_secret_init(); net_secret_init();
memcpy(hash, saddr, 16); hash = siphash(&combined, offsetofend(typeof(combined), dport),
for (i = 0; i < 4; i++) &net_secret);
secret[i] = net_secret[i] + (__force u32)daddr[i]; *tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
secret[4] = net_secret[4] + return seq_scale(hash);
(((__force u16)sport << 16) + (__force u16)dport);
for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
secret[i] = net_secret[i];
md5_transform(hash, secret);
*tsoff = sysctl_tcp_timestamps == 1 ? hash[1] : 0;
return seq_scale(hash[0]);
} }
EXPORT_SYMBOL(secure_tcpv6_sequence_number); EXPORT_SYMBOL(secure_tcpv6_sequence_number);
u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr, u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
__be16 dport) __be16 dport)
{ {
u32 secret[MD5_MESSAGE_BYTES / 4]; const struct {
u32 hash[MD5_DIGEST_WORDS]; struct in6_addr saddr;
u32 i; struct in6_addr daddr;
__be16 dport;
} __aligned(SIPHASH_ALIGNMENT) combined = {
.saddr = *(struct in6_addr *)saddr,
.daddr = *(struct in6_addr *)daddr,
.dport = dport
};
net_secret_init(); net_secret_init();
memcpy(hash, saddr, 16); return siphash(&combined, offsetofend(typeof(combined), dport),
for (i = 0; i < 4; i++) &net_secret);
secret[i] = net_secret[i] + (__force u32) daddr[i];
secret[4] = net_secret[4] + (__force u32)dport;
for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
secret[i] = net_secret[i];
md5_transform(hash, secret);
return hash[0];
} }
EXPORT_SYMBOL(secure_ipv6_port_ephemeral); EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
#endif #endif
#ifdef CONFIG_INET #ifdef CONFIG_INET
/* secure_tcp_sequence_number(a, b, 0, d) == secure_ipv4_port_ephemeral(a, b, d),
* but fortunately, `sport' cannot be 0 in any circumstances. If this changes,
* it would be easy enough to have the former function use siphash_4u32, passing
* the arguments as separate u32.
*/
u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr, u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
__be16 sport, __be16 dport, u32 *tsoff) __be16 sport, __be16 dport, u32 *tsoff)
{ {
u32 hash[MD5_DIGEST_WORDS]; u64 hash;
net_secret_init(); net_secret_init();
hash[0] = (__force u32)saddr; hash = siphash_3u32((__force u32)saddr, (__force u32)daddr,
hash[1] = (__force u32)daddr; (__force u32)sport << 16 | (__force u32)dport,
hash[2] = ((__force u16)sport << 16) + (__force u16)dport; &net_secret);
hash[3] = net_secret[15]; *tsoff = sysctl_tcp_timestamps == 1 ? (hash >> 32) : 0;
return seq_scale(hash);
md5_transform(hash, net_secret);
*tsoff = sysctl_tcp_timestamps == 1 ? hash[1] : 0;
return seq_scale(hash[0]);
} }
u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport) u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
{ {
u32 hash[MD5_DIGEST_WORDS];
net_secret_init(); net_secret_init();
hash[0] = (__force u32)saddr; return siphash_3u32((__force u32)saddr, (__force u32)daddr,
hash[1] = (__force u32)daddr; (__force u16)dport, &net_secret);
hash[2] = (__force u32)dport ^ net_secret[14];
hash[3] = net_secret[15];
md5_transform(hash, net_secret);
return hash[0];
} }
EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral); EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
#endif #endif
...@@ -126,21 +120,11 @@ EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral); ...@@ -126,21 +120,11 @@ EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);
u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr, u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
__be16 sport, __be16 dport) __be16 sport, __be16 dport)
{ {
u32 hash[MD5_DIGEST_WORDS];
u64 seq; u64 seq;
net_secret_init(); net_secret_init();
hash[0] = (__force u32)saddr; seq = siphash_3u32(saddr, daddr, (u32)sport << 16 | dport, &net_secret);
hash[1] = (__force u32)daddr;
hash[2] = ((__force u16)sport << 16) + (__force u16)dport;
hash[3] = net_secret[15];
md5_transform(hash, net_secret);
seq = hash[0] | (((u64)hash[1]) << 32);
seq += ktime_get_real_ns(); seq += ktime_get_real_ns();
seq &= (1ull << 48) - 1; seq &= (1ull << 48) - 1;
return seq; return seq;
} }
EXPORT_SYMBOL(secure_dccp_sequence_number); EXPORT_SYMBOL(secure_dccp_sequence_number);
...@@ -149,26 +133,23 @@ EXPORT_SYMBOL(secure_dccp_sequence_number); ...@@ -149,26 +133,23 @@ EXPORT_SYMBOL(secure_dccp_sequence_number);
u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr, u64 secure_dccpv6_sequence_number(__be32 *saddr, __be32 *daddr,
__be16 sport, __be16 dport) __be16 sport, __be16 dport)
{ {
u32 secret[MD5_MESSAGE_BYTES / 4]; const struct {
u32 hash[MD5_DIGEST_WORDS]; struct in6_addr saddr;
struct in6_addr daddr;
__be16 sport;
__be16 dport;
} __aligned(SIPHASH_ALIGNMENT) combined = {
.saddr = *(struct in6_addr *)saddr,
.daddr = *(struct in6_addr *)daddr,
.sport = sport,
.dport = dport
};
u64 seq; u64 seq;
u32 i;
net_secret_init(); net_secret_init();
memcpy(hash, saddr, 16); seq = siphash(&combined, offsetofend(typeof(combined), dport),
for (i = 0; i < 4; i++) &net_secret);
secret[i] = net_secret[i] + (__force u32)daddr[i];
secret[4] = net_secret[4] +
(((__force u16)sport << 16) + (__force u16)dport);
for (i = 5; i < MD5_MESSAGE_BYTES / 4; i++)
secret[i] = net_secret[i];
md5_transform(hash, secret);
seq = hash[0] | (((u64)hash[1]) << 32);
seq += ktime_get_real_ns(); seq += ktime_get_real_ns();
seq &= (1ull << 48) - 1; seq &= (1ull << 48) - 1;
return seq; return seq;
} }
EXPORT_SYMBOL(secure_dccpv6_sequence_number); EXPORT_SYMBOL(secure_dccpv6_sequence_number);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment