1 /*
2 * WPA Supplicant - ASCII passphrase to WPA PSK tool
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10 #include <termios.h>
11
12 #include "common.h"
13 #include "crypto/sha1.h"
14
15
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 struct termios term;
19 unsigned char psk[32];
20 int i;
21 char *ssid, *passphrase, buf[64], *pos;
22 size_t len;
23
24 if (argc < 2) {
25 printf("usage: wpa_passphrase <ssid> [passphrase]\n"
26 "\nIf passphrase is left out, it will be read from "
27 "stdin\n");
28 return 1;
29 }
30
31 ssid = argv[1];
32
33 if (argc > 2) {
34 passphrase = argv[2];
35 } else {
36 bool ctrl_echo;
37
38 fprintf(stderr, "# reading passphrase from stdin\n");
39 if (tcgetattr(STDIN_FILENO, &term) < 0) {
40 perror("tcgetattr");
41 return 1;
42 }
43 ctrl_echo = term.c_lflag & ECHO;
44 term.c_lflag &= ~ECHO;
45 if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
46 perror("tcsetattr:error disabling echo");
47 return 1;
48 }
49 if (fgets(buf, sizeof(buf), stdin) == NULL) {
50 fprintf(stderr, "Failed to read passphrase\n");
51 return 1;
52 }
53 term.c_lflag |= ECHO;
54 if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
55 perror("tcsetattr:error enabling echo");
56 return 1;
57 }
58 buf[sizeof(buf) - 1] = '\0';
59 pos = buf;
60 while (*pos != '\0') {
61 if (*pos == '\r' || *pos == '\n') {
62 *pos = '\0';
63 break;
64 }
65 pos++;
66 }
67 passphrase = buf;
68 }
69
70 len = os_strlen(passphrase);
71 if (len < 8 || len > 63) {
72 fprintf(stderr, "Passphrase must be 8..63 characters\n");
73 return 1;
74 }
75 if (has_ctrl_char((u8 *) passphrase, len)) {
76 fprintf(stderr, "Invalid passphrase character\n");
77 return 1;
78 }
79
80 if (pbkdf2_sha1(passphrase, (u8 *) ssid, os_strlen(ssid), 4096, psk, 32)
81 != 0) {
82 fprintf(stderr, "Error in pbkdf2_sha1()\n");
83 return 1;
84 }
85
86 printf("network={\n");
87 printf("\tssid=\"%s\"\n", ssid);
88 printf("\t#psk=\"%s\"\n", passphrase);
89 printf("\tpsk=");
90 for (i = 0; i < 32; i++)
91 printf("%02x", psk[i]);
92 printf("\n");
93 printf("}\n");
94
95 return 0;
96 }
97