Commit 5a7a0d94 authored by Eric Biggers's avatar Eric Biggers Committed by Herbert Xu

mptcp: use sha256() instead of open coding

Now that there's a function that calculates the SHA-256 digest of a
buffer in one step, use it instead of sha256_init() + sha256_update() +
sha256_final().
Reviewed-by: default avatarArd Biesheuvel <ardb@kernel.org>
Acked-by: default avatarMatthieu Baerts <matthieu.baerts@tessares.net>
Cc: mptcp@lists.01.org
Cc: Mat Martineau <mathew.j.martineau@linux.intel.com>
Cc: Matthieu Baerts <matthieu.baerts@tessares.net>
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 2164960d
...@@ -32,11 +32,8 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn) ...@@ -32,11 +32,8 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
{ {
__be32 mptcp_hashed_key[SHA256_DIGEST_WORDS]; __be32 mptcp_hashed_key[SHA256_DIGEST_WORDS];
__be64 input = cpu_to_be64(key); __be64 input = cpu_to_be64(key);
struct sha256_state state;
sha256_init(&state); sha256((__force u8 *)&input, sizeof(input), (u8 *)mptcp_hashed_key);
sha256_update(&state, (__force u8 *)&input, sizeof(input));
sha256_final(&state, (u8 *)mptcp_hashed_key);
if (token) if (token)
*token = be32_to_cpu(mptcp_hashed_key[0]); *token = be32_to_cpu(mptcp_hashed_key[0]);
...@@ -47,7 +44,6 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn) ...@@ -47,7 +44,6 @@ void mptcp_crypto_key_sha(u64 key, u32 *token, u64 *idsn)
void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac) void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
{ {
u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE]; u8 input[SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE];
struct sha256_state state;
u8 key1be[8]; u8 key1be[8];
u8 key2be[8]; u8 key2be[8];
int i; int i;
...@@ -67,13 +63,10 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac) ...@@ -67,13 +63,10 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
memcpy(&input[SHA256_BLOCK_SIZE], msg, len); memcpy(&input[SHA256_BLOCK_SIZE], msg, len);
sha256_init(&state);
sha256_update(&state, input, SHA256_BLOCK_SIZE + len);
/* emit sha256(K1 || msg) on the second input block, so we can /* emit sha256(K1 || msg) on the second input block, so we can
* reuse 'input' for the last hashing * reuse 'input' for the last hashing
*/ */
sha256_final(&state, &input[SHA256_BLOCK_SIZE]); sha256(input, SHA256_BLOCK_SIZE + len, &input[SHA256_BLOCK_SIZE]);
/* Prepare second part of hmac */ /* Prepare second part of hmac */
memset(input, 0x5C, SHA256_BLOCK_SIZE); memset(input, 0x5C, SHA256_BLOCK_SIZE);
...@@ -82,9 +75,7 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac) ...@@ -82,9 +75,7 @@ void mptcp_crypto_hmac_sha(u64 key1, u64 key2, u8 *msg, int len, void *hmac)
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
input[i + 8] ^= key2be[i]; input[i + 8] ^= key2be[i];
sha256_init(&state); sha256(input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE, hmac);
sha256_update(&state, input, SHA256_BLOCK_SIZE + SHA256_DIGEST_SIZE);
sha256_final(&state, (u8 *)hmac);
} }
#ifdef CONFIG_MPTCP_HMAC_TEST #ifdef CONFIG_MPTCP_HMAC_TEST
......
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