wpa_passphrase.c revision 189261
1290001Sglebius/*
2290001Sglebius * WPA Supplicant - ASCII passphrase to WPA PSK tool
3290001Sglebius * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4290001Sglebius *
5290001Sglebius * This program is free software; you can redistribute it and/or modify
6290001Sglebius * it under the terms of the GNU General Public License version 2 as
7290001Sglebius * published by the Free Software Foundation.
8290001Sglebius *
9290001Sglebius * Alternatively, this software may be distributed under the terms of BSD
10290001Sglebius * license.
11290001Sglebius *
12290001Sglebius * See README and COPYING for more details.
13290001Sglebius */
14290001Sglebius
15290001Sglebius#include "includes.h"
16290001Sglebius
17290001Sglebius#include "common.h"
18290001Sglebius#include "sha1.h"
19290001Sglebius
20290001Sglebius
21290001Sglebiusint main(int argc, char *argv[])
22290001Sglebius{
23290001Sglebius	unsigned char psk[32];
24290001Sglebius	int i;
25290001Sglebius	char *ssid, *passphrase, buf[64], *pos;
26290001Sglebius
27290001Sglebius	if (argc < 2) {
28290001Sglebius		printf("usage: wpa_passphrase <ssid> [passphrase]\n"
29290001Sglebius			"\nIf passphrase is left out, it will be read from "
30290001Sglebius			"stdin\n");
31290001Sglebius		return 1;
32290001Sglebius	}
33290001Sglebius
34290001Sglebius	ssid = argv[1];
35290001Sglebius
36290001Sglebius	if (argc > 2) {
37290001Sglebius		passphrase = argv[2];
38290001Sglebius	} else {
39290001Sglebius		printf("# reading passphrase from stdin\n");
40290001Sglebius		if (fgets(buf, sizeof(buf), stdin) == NULL) {
41290001Sglebius			printf("Failed to read passphrase\n");
42290001Sglebius			return 1;
43290001Sglebius		}
44290001Sglebius		buf[sizeof(buf) - 1] = '\0';
45290001Sglebius		pos = buf;
46290001Sglebius		while (*pos != '\0') {
47290001Sglebius			if (*pos == '\r' || *pos == '\n') {
48290001Sglebius				*pos = '\0';
49290001Sglebius				break;
50290001Sglebius			}
51290001Sglebius			pos++;
52290001Sglebius		}
53290001Sglebius		passphrase = buf;
54290001Sglebius	}
55290001Sglebius
56290001Sglebius	if (os_strlen(passphrase) < 8 || os_strlen(passphrase) > 63) {
57290001Sglebius		printf("Passphrase must be 8..63 characters\n");
58290001Sglebius		return 1;
59290001Sglebius	}
60290001Sglebius
61290001Sglebius	pbkdf2_sha1(passphrase, ssid, os_strlen(ssid), 4096, psk, 32);
62290001Sglebius
63290001Sglebius	printf("network={\n");
64290001Sglebius	printf("\tssid=\"%s\"\n", ssid);
65290001Sglebius	printf("\t#psk=\"%s\"\n", passphrase);
66290001Sglebius	printf("\tpsk=");
67290001Sglebius	for (i = 0; i < 32; i++)
68290001Sglebius		printf("%02x", psk[i]);
69290001Sglebius	printf("\n");
70290001Sglebius	printf("}\n");
71290001Sglebius
72290001Sglebius	return 0;
73290001Sglebius}
74290001Sglebius