commit 42e083cf9b73942817bd9625d642a50a9c286535
parent 7a9891dbef5b7b32426541cb2970997a6f5f02f9
Author: James Prestwood <prestwoj@gmail.com>
Date: Wed, 18 Sep 2019 09:02:57 -0700
crypto: add crypto_passphrase_is_valid
Move this check out of crypto_psk_from_passphrase so other modules
can validate a passphrase.
Diffstat:
2 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/src/crypto.c b/src/crypto.c
@@ -479,20 +479,10 @@ int crypto_cipher_tk_bits(enum crypto_cipher cipher)
return crypto_cipher_key_len(cipher) * 8;
}
-int crypto_psk_from_passphrase(const char *passphrase,
- const unsigned char *ssid, size_t ssid_len,
- unsigned char *out_psk)
+bool crypto_passphrase_is_valid(const char *passphrase)
{
size_t passphrase_len;
size_t i;
- bool result;
- unsigned char psk[32];
-
- if (!passphrase)
- return -EINVAL;
-
- if (!ssid)
- return -EINVAL;
/*
* IEEE 802.11, Annex M, Section M.4.1:
@@ -503,10 +493,7 @@ int crypto_psk_from_passphrase(const char *passphrase,
*/
passphrase_len = strlen(passphrase);
if (passphrase_len < 8 || passphrase_len > 63)
- return -ERANGE;
-
- if (ssid_len == 0 || ssid_len > 32)
- return -ERANGE;
+ return false;
/* IEEE 802.11, Annex M, Section M.4.1:
* "Each character in the pass-phrase must have an encoding in the
@@ -518,9 +505,31 @@ int crypto_psk_from_passphrase(const char *passphrase,
if (l_ascii_isprint(passphrase[i]))
continue;
- return -EINVAL;
+ return false;
}
+ return true;
+}
+
+int crypto_psk_from_passphrase(const char *passphrase,
+ const unsigned char *ssid, size_t ssid_len,
+ unsigned char *out_psk)
+{
+ bool result;
+ unsigned char psk[32];
+
+ if (!passphrase)
+ return -EINVAL;
+
+ if (!ssid)
+ return -EINVAL;
+
+ if (!crypto_passphrase_is_valid(passphrase))
+ return -ERANGE;
+
+ if (ssid_len == 0 || ssid_len > 32)
+ return -ERANGE;
+
result = l_pkcs5_pbkdf2(L_CHECKSUM_SHA1, passphrase, ssid, ssid_len,
4096, psk, sizeof(psk));
if (!result)
diff --git a/src/crypto.h b/src/crypto.h
@@ -91,6 +91,8 @@ bool aes_siv_decrypt(const uint8_t *key, size_t key_len, const uint8_t *in,
int crypto_cipher_key_len(enum crypto_cipher cipher);
int crypto_cipher_tk_bits(enum crypto_cipher cipher);
+bool crypto_passphrase_is_valid(const char *passphrase);
+
int crypto_psk_from_passphrase(const char *passphrase,
const unsigned char *ssid, size_t ssid_len,
unsigned char *out_psk);