wpa_supplicant vendor branch: Update version 0.6.10 => 2.1
[dragonfly.git] / contrib / wpa_supplicant / wpa_supplicant / wpa_passphrase.c
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
11 #include "common.h"
12 #include "crypto/sha1.h"
13
14
15 int main(int argc, char *argv[])
16 {
17         unsigned char psk[32];
18         int i;
19         char *ssid, *passphrase, buf[64], *pos;
20
21         if (argc < 2) {
22                 printf("usage: wpa_passphrase <ssid> [passphrase]\n"
23                         "\nIf passphrase is left out, it will be read from "
24                         "stdin\n");
25                 return 1;
26         }
27
28         ssid = argv[1];
29
30         if (argc > 2) {
31                 passphrase = argv[2];
32         } else {
33                 printf("# reading passphrase from stdin\n");
34                 if (fgets(buf, sizeof(buf), stdin) == NULL) {
35                         printf("Failed to read passphrase\n");
36                         return 1;
37                 }
38                 buf[sizeof(buf) - 1] = '\0';
39                 pos = buf;
40                 while (*pos != '\0') {
41                         if (*pos == '\r' || *pos == '\n') {
42                                 *pos = '\0';
43                                 break;
44                         }
45                         pos++;
46                 }
47                 passphrase = buf;
48         }
49
50         if (os_strlen(passphrase) < 8 || os_strlen(passphrase) > 63) {
51                 printf("Passphrase must be 8..63 characters\n");
52                 return 1;
53         }
54
55         pbkdf2_sha1(passphrase, (u8 *) ssid, os_strlen(ssid), 4096, psk, 32);
56
57         printf("network={\n");
58         printf("\tssid=\"%s\"\n", ssid);
59         printf("\t#psk=\"%s\"\n", passphrase);
60         printf("\tpsk=");
61         for (i = 0; i < 32; i++)
62                 printf("%02x", psk[i]);
63         printf("\n");
64         printf("}\n");
65
66         return 0;
67 }