crypto: Add passphrase generation utility

This commit is contained in:
Denis Kenzior 2014-11-14 11:37:01 -06:00
parent d0cb0aedc6
commit d87d7d469f
3 changed files with 115 additions and 0 deletions

View File

@ -47,6 +47,7 @@ src_iwd_SOURCES = src/main.c linux/nl80211.h linux/kdbus.h \
src/ie.h src/ie.c \
src/dbus.h src/dbus.c \
src/manager.h src/manager.c \
src/crypto.h src/crypto.c \
iwd.h
src_iwd_LDADD = ell/libell-internal.la

87
src/crypto.c Normal file
View File

@ -0,0 +1,87 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <ell/ell.h>
#include "sha1.h"
#include "crypto.h"
int crypto_psk_from_passphrase(const char *passphrase,
const unsigned char *ssid, size_t ssid_len,
unsigned char *out_psk)
{
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:
* "A pass-phrase is a sequence of between 8 and 63 ASCII-encoded
* characters. The limit of 63 comes from the desire to distinguish
* between a pass-phrase and a PSK displayed as 64 hexadecimal
* characters."
*/
passphrase_len = strlen(passphrase);
if (passphrase_len < 8 || passphrase_len > 63)
return -ERANGE;
if (ssid_len == 0 || ssid_len > 32)
return -ERANGE;
/* IEEE 802.11, Annex M, Section M.4.1:
* "Each character in the pass-phrase must have an encoding in the
* range of 32 to 126 (decimal), inclusive."
*
* This corresponds to printable characters only
*/
for (i = 0; i < passphrase_len; i++) {
if (l_ascii_isprint(passphrase[i]))
continue;
return -EINVAL;
}
result = pbkdf2_sha1(passphrase, passphrase_len, ssid, ssid_len,
4096, psk, sizeof(psk));
if (!result)
return -ENOKEY;
if (out_psk)
memcpy(out_psk, psk, sizeof(psk));
return 0;
}

27
src/crypto.h Normal file
View File

@ -0,0 +1,27 @@
/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stddef.h>
int crypto_psk_from_passphrase(const char *passphrase,
const unsigned char *ssid, size_t ssid_len,
unsigned char *out_psk);