Skip to content

Commit bd69722

Browse files
Todd Malsbarydavem330
authored andcommitted
mptcp: use untruncated hash in ADD_ADDR HMAC
There is some ambiguity in the RFC as to whether the ADD_ADDR HMAC is the rightmost 64 bits of the entire hash or of the leftmost 160 bits of the hash. The intention, as clarified with the author of the RFC, is the entire hash. This change returns the entire hash from mptcp_crypto_hmac_sha (instead of only the first 160 bits), and moves any truncation/selection operation on the hash to the caller. Fixes: 12555a2 ("mptcp: use rightmost 64 bits in ADD_ADDR HMAC") Reviewed-by: Christoph Paasch <[email protected]> Reviewed-by: Mat Martineau <[email protected]> Signed-off-by: Todd Malsbary <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a765421 commit bd69722

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

net/mptcp/crypto.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
4747
void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
4848
{
4949
u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
50-
__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
51-
__be32 *hash_out = (__force __be32 *)hmac;
5250
struct sha256_state state;
5351
u8 key1be[8];
5452
u8 key2be[8];
@@ -86,11 +84,7 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
8684

8785
sha256_init(&state);
8886
sha256_update(&state, input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE);
89-
sha256_final(&state, (u8 *)mptcp_hashed_key);
90-
91-
/* takes only first 160 bits */
92-
for (i = 0; i < 5; i++)
93-
hash_out[i] = mptcp_hashed_key[i];
87+
sha256_final(&state, (u8 *)hmac);
9488
}
9589

9690
#ifdef CONFIG_MPTCP_HMAC_TEST
@@ -101,29 +95,29 @@ struct test_cast {
10195
};
10296

10397
/* we can't reuse RFC 4231 test vectors, as we have constraint on the
104-
* input and key size, and we truncate the output.
98+
* input and key size.
10599
*/
106100
static struct test_cast tests[] = {
107101
{
108102
.key = "0b0b0b0b0b0b0b0b",
109103
.msg = "48692054",
110-
.result = "8385e24fb4235ac37556b6b886db106284a1da67",
104+
.result = "8385e24fb4235ac37556b6b886db106284a1da671699f46db1f235ec622dcafa",
111105
},
112106
{
113107
.key = "aaaaaaaaaaaaaaaa",
114108
.msg = "dddddddd",
115-
.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492",
109+
.result = "2c5e219164ff1dca1c4a92318d847bb6b9d44492984e1eb71aff9022f71046e9",
116110
},
117111
{
118112
.key = "0102030405060708",
119113
.msg = "cdcdcdcd",
120-
.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6",
114+
.result = "e73b9ba9969969cefb04aa0d6df18ec2fcc075b6f23b4d8c4da736a5dbbc6e7d",
121115
},
122116
};
123117

124118
static int __init test_mptcp_crypto(void)
125119
{
126-
char hmac[20], hmac_hex[41];
120+
char hmac[32], hmac_hex[65];
127121
u32 nonce1, nonce2;
128122
u64 key1, key2;
129123
u8 msg[8];
@@ -140,11 +134,11 @@ static int __init test_mptcp_crypto(void)
140134
put_unaligned_be32(nonce2, &msg[4]);
141135

142136
mptcp_crypto_hmac_sha(key1, key2, msg, 8, hmac);
143-
for (j = 0; j < 20; ++j)
137+
for (j = 0; j < 32; ++j)
144138
sprintf(&hmac_hex[j << 1], "%02x", hmac[j] & 0xff);
145-
hmac_hex[40] = 0;
139+
hmac_hex[64] = 0;
146140

147-
if (memcmp(hmac_hex, tests[i].result, 40))
141+
if (memcmp(hmac_hex, tests[i].result, 64))
148142
pr_err("test %d failed, got %s expected %s", i,
149143
hmac_hex, tests[i].result);
150144
else

net/mptcp/options.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define pr_fmt(fmt) "MPTCP: " fmt
88

99
#include <linux/kernel.h>
10+
#include <crypto/sha.h>
1011
#include <net/tcp.h>
1112
#include <net/mptcp.h>
1213
#include "protocol.h"
@@ -535,7 +536,7 @@ static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
535536
static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
536537
struct in_addr *addr)
537538
{
538-
u8 hmac[MPTCP_ADDR_HMAC_LEN];
539+
u8 hmac[SHA256_DIGEST_SIZE];
539540
u8 msg[7];
540541

541542
msg[0] = addr_id;
@@ -545,14 +546,14 @@ static u64 add_addr_generate_hmac(u64 key1, u64 key2, u8 addr_id,
545546

546547
mptcp_crypto_hmac_sha(key1, key2, msg, 7, hmac);
547548

548-
return get_unaligned_be64(&hmac[MPTCP_ADDR_HMAC_LEN - sizeof(u64)]);
549+
return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
549550
}
550551

551552
#if IS_ENABLED(CONFIG_MPTCP_IPV6)
552553
static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
553554
struct in6_addr *addr)
554555
{
555-
u8 hmac[MPTCP_ADDR_HMAC_LEN];
556+
u8 hmac[SHA256_DIGEST_SIZE];
556557
u8 msg[19];
557558

558559
msg[0] = addr_id;
@@ -562,7 +563,7 @@ static u64 add_addr6_generate_hmac(u64 key1, u64 key2, u8 addr_id,
562563

563564
mptcp_crypto_hmac_sha(key1, key2, msg, 19, hmac);
564565

565-
return get_unaligned_be64(&hmac[MPTCP_ADDR_HMAC_LEN - sizeof(u64)]);
566+
return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
566567
}
567568
#endif
568569

net/mptcp/protocol.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181

8282
/* MPTCP ADD_ADDR flags */
8383
#define MPTCP_ADDR_ECHO BIT(0)
84-
#define MPTCP_ADDR_HMAC_LEN 20
8584
#define MPTCP_ADDR_IPVERSION_4 4
8685
#define MPTCP_ADDR_IPVERSION_6 6
8786

net/mptcp/subflow.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/module.h>
1111
#include <linux/netdevice.h>
1212
#include <crypto/algapi.h>
13+
#include <crypto/sha.h>
1314
#include <net/sock.h>
1415
#include <net/inet_common.h>
1516
#include <net/inet_hashtables.h>
@@ -89,7 +90,7 @@ static bool subflow_token_join_request(struct request_sock *req,
8990
const struct sk_buff *skb)
9091
{
9192
struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
92-
u8 hmac[MPTCPOPT_HMAC_LEN];
93+
u8 hmac[SHA256_DIGEST_SIZE];
9394
struct mptcp_sock *msk;
9495
int local_id;
9596

@@ -201,7 +202,7 @@ static void subflow_v6_init_req(struct request_sock *req,
201202
/* validate received truncated hmac and create hmac for third ACK */
202203
static bool subflow_thmac_valid(struct mptcp_subflow_context *subflow)
203204
{
204-
u8 hmac[MPTCPOPT_HMAC_LEN];
205+
u8 hmac[SHA256_DIGEST_SIZE];
205206
u64 thmac;
206207

207208
subflow_generate_hmac(subflow->remote_key, subflow->local_key,
@@ -267,6 +268,8 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
267268
subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
268269
}
269270
} else if (subflow->mp_join) {
271+
u8 hmac[SHA256_DIGEST_SIZE];
272+
270273
pr_debug("subflow=%p, thmac=%llu, remote_nonce=%u",
271274
subflow, subflow->thmac,
272275
subflow->remote_nonce);
@@ -279,7 +282,9 @@ static void subflow_finish_connect(struct sock *sk, const struct sk_buff *skb)
279282
subflow_generate_hmac(subflow->local_key, subflow->remote_key,
280283
subflow->local_nonce,
281284
subflow->remote_nonce,
282-
subflow->hmac);
285+
hmac);
286+
287+
memcpy(subflow->hmac, hmac, MPTCPOPT_HMAC_LEN);
283288

284289
if (skb)
285290
subflow->ssn_offset = TCP_SKB_CB(skb)->seq;
@@ -347,7 +352,7 @@ static bool subflow_hmac_valid(const struct request_sock *req,
347352
const struct mptcp_options_received *mp_opt)
348353
{
349354
const struct mptcp_subflow_request_sock *subflow_req;
350-
u8 hmac[MPTCPOPT_HMAC_LEN];
355+
u8 hmac[SHA256_DIGEST_SIZE];
351356
struct mptcp_sock *msk;
352357
bool ret;
353358

@@ -361,7 +366,7 @@ static bool subflow_hmac_valid(const struct request_sock *req,
361366
subflow_req->local_nonce, hmac);
362367

363368
ret = true;
364-
if (crypto_memneq(hmac, mp_opt->hmac, sizeof(hmac)))
369+
if (crypto_memneq(hmac, mp_opt->hmac, MPTCPOPT_HMAC_LEN))
365370
ret = false;
366371

367372
sock_put((struct sock *)msk);

0 commit comments

Comments
 (0)