-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(wpa_supplicant): Add optimized PSK impl (IDFGH-14280) #15073
Open
ivq
wants to merge
1
commit into
espressif:master
Choose a base branch
from
ivq:psk_opt_pr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
components/wpa_supplicant/esp_supplicant/src/crypto/fastpsk.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* Specialized and optimized PBKDF2-SHA1 implementation for Wi-Fi PSK | ||
* | ||
* Initially authored by Chien Wong([email protected]). | ||
*/ | ||
|
||
#include "fastpsk.h" | ||
|
||
#include <string.h> | ||
|
||
#include <sha/sha_dma.h> | ||
#include <hal/sha_hal.h> | ||
|
||
#ifndef PUT_UINT32_BE | ||
#define PUT_UINT32_BE(n,b,i) \ | ||
{ \ | ||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ | ||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ | ||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ | ||
(b)[(i) + 3] = (unsigned char) ( (n) ); \ | ||
} | ||
#endif | ||
|
||
#define FAST_PSK_SHA1_BLOCKS 2 | ||
#define SHA1_BLOCK_SZ 64 | ||
#define SHA1_BLOCK_SZ_WORDS 16 | ||
#define SHA1_OUTPUT_SZ 20 | ||
#define SHA1_OUTPUT_SZ_WORDS 5 | ||
#define FAST_PSK_SHA1_BLOCKS_BUF_BYTES (FAST_PSK_SHA1_BLOCKS * SHA1_BLOCK_SZ) | ||
#define FAST_PSK_SHA1_BLOCKS_BUF_WORDS (FAST_PSK_SHA1_BLOCKS * SHA1_BLOCK_SZ / 4) | ||
|
||
union hmac_block { | ||
union { | ||
uint32_t words[SHA1_BLOCK_SZ / 4]; | ||
uint8_t bytes[SHA1_BLOCK_SZ]; | ||
} block[FAST_PSK_SHA1_BLOCKS]; | ||
uint8_t whole_bytes[FAST_PSK_SHA1_BLOCKS_BUF_BYTES]; | ||
uint32_t whole_words[FAST_PSK_SHA1_BLOCKS_BUF_WORDS]; | ||
}; | ||
_Static_assert(sizeof(union hmac_block) == 128, "Incorrect layout of hmac_block"); | ||
|
||
struct fast_psk_context { | ||
union hmac_block inner, outer; | ||
uint32_t sum[SHA1_OUTPUT_SZ_WORDS]; | ||
}; | ||
|
||
static inline void sha1_setup(void) | ||
{ | ||
esp_sha_acquire_hardware(); | ||
} | ||
|
||
static inline void sha1_teardown(void) | ||
{ | ||
esp_sha_release_hardware(); | ||
} | ||
|
||
static void pad_blocks(union hmac_block *ctx, size_t len) | ||
{ | ||
size_t bits = len << 3; | ||
uint8_t *bytes = ctx->whole_bytes; | ||
bytes[len] = 0x80; | ||
// Set all remaining bytes to 0 | ||
memset(&bytes[len + 1], 0, FAST_PSK_SHA1_BLOCKS_BUF_BYTES - (len + 1)); | ||
|
||
/* | ||
* Simplified PUT_UINT64_BE(bits, bytes, FAST_PSK_SHA1_BLOCKS_BUF_BYTES - 8). | ||
* Since len < 128 => bits < 1024, we only need to update the two least significant | ||
* bytes, actually. | ||
*/ | ||
PUT_UINT32_BE(bits, bytes, FAST_PSK_SHA1_BLOCKS_BUF_BYTES - 4); | ||
} | ||
|
||
void sha1_op(uint32_t blocks[FAST_PSK_SHA1_BLOCKS_BUF_WORDS], uint32_t output[SHA1_OUTPUT_SZ_WORDS]) | ||
{ | ||
sha_hal_hash_block(SHA1, blocks, SHA1_BLOCK_SZ_WORDS, true); | ||
sha_hal_hash_block(SHA1, &blocks[SHA1_BLOCK_SZ_WORDS], SHA1_BLOCK_SZ_WORDS, false); | ||
sha_hal_read_digest(SHA1, output); | ||
} | ||
|
||
void fast_psk_f(const char *password, size_t password_len, const uint8_t *ssid, size_t ssid_len, uint32_t count, uint8_t digest[SHA1_OUTPUT_SZ]) | ||
{ | ||
struct fast_psk_context ctx_, *ctx = &ctx_; | ||
size_t i; | ||
|
||
memset(ctx, 0, sizeof(*ctx)); | ||
|
||
memset(ctx->outer.block[0].bytes, 0x5c, SHA1_BLOCK_SZ); | ||
memset(ctx->inner.block[0].bytes, 0x36, SHA1_BLOCK_SZ); | ||
|
||
for (i = 0; i < password_len; ++i) { | ||
ctx->outer.block[0].bytes[i] ^= password[i]; | ||
ctx->inner.block[0].bytes[i] ^= password[i]; | ||
} | ||
|
||
// U1 = PRF(P, S || i) | ||
memcpy(ctx->inner.block[1].bytes, ssid, ssid_len); | ||
PUT_UINT32_BE(count, ctx->inner.block[1].bytes, ssid_len); | ||
pad_blocks(&ctx->inner, SHA1_BLOCK_SZ + ssid_len + 4); | ||
|
||
sha1_setup(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calling task will hold the SHA1 lock for a long time so other tasks may suffer from starvation. If this could be an issue, we could mitigate. |
||
|
||
uint32_t *pi, *po; | ||
pi = ctx->inner.whole_words; | ||
po = ctx->outer.whole_words; | ||
// T1 = SHA1(K ^ ipad, S || i) | ||
sha1_op(pi, ctx->outer.block[1].words); | ||
|
||
// U1 = SHA1(K ^ opad, T1) | ||
pad_blocks(&ctx->outer, SHA1_BLOCK_SZ + SHA1_OUTPUT_SZ); | ||
uint32_t *inner_blk1 = ctx->inner.block[1].words; | ||
uint32_t *outer_blk1 = ctx->outer.block[1].words; | ||
uint32_t *sum = ctx->sum; | ||
|
||
sha1_op(po, inner_blk1); | ||
memcpy(sum, inner_blk1, SHA1_OUTPUT_SZ); | ||
pad_blocks(&ctx->inner, SHA1_BLOCK_SZ + SHA1_OUTPUT_SZ); | ||
|
||
for (i = 1; i < 4096; ++i) { | ||
// Tn = SHA1(K ^ ipad, Un-1) | ||
sha1_op(pi, outer_blk1); | ||
|
||
// Un = SHA1(K ^ opad, Tn) | ||
sha1_op(po, inner_blk1); | ||
|
||
// F = U1 ^ U2 ^ ... Un | ||
for (size_t j = 0; j < SHA1_OUTPUT_SZ_WORDS; ++j) { | ||
sum[j] ^= inner_blk1[j]; | ||
} | ||
} | ||
|
||
sha1_teardown(); | ||
|
||
memcpy(digest, sum, SHA1_OUTPUT_SZ); | ||
|
||
memset(ctx, 0, sizeof(*ctx)); | ||
} | ||
|
||
int esp_fast_psk(const char *password, size_t password_len, const uint8_t *ssid, size_t ssid_len, size_t iterations, uint8_t *output, size_t output_len) | ||
{ | ||
if (!(ssid_len <= 32 && password_len <= 63 && iterations == 4096 && output_len == 32)) { | ||
return -1; | ||
} | ||
fast_psk_f(password, password_len, ssid, ssid_len, 2, output); | ||
memcpy(output + SHA1_OUTPUT_SZ, output, 32 - SHA1_OUTPUT_SZ); | ||
fast_psk_f(password, password_len, ssid, ssid_len, 1, output); | ||
|
||
return 0; | ||
} |
32 changes: 32 additions & 0 deletions
32
components/wpa_supplicant/esp_supplicant/src/crypto/fastpsk.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Calculate PSK | ||
* | ||
* @param password Password | ||
* @param password_len Length of password, it must be <= 63 | ||
* @param ssid SSID | ||
* @param ssid_len Length of SSID, it must be <= 32 | ||
* @param iterations Iterations of the PBKDF2-SHA1, this is a dummy param and it must be 4096 | ||
* @param output Buffer for calculated PSK, it must be at least 32 bytes | ||
* @param output_len Length of output to return, this is a dummy param and it must be 32 | ||
* @return 0 on success, non-zero on failure | ||
*/ | ||
int esp_fast_psk(const char *password, size_t password_len, const uint8_t *ssid, size_t ssid_len, size_t iterations, uint8_t *output, size_t output_len); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big stack usage(276 bytes). Maybe replace with malloc?