wpa_supplicant: update vendor branch to 0.6.10
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / wpa_passphrase.c
1 /*
2  * WPA Supplicant - ASCII passphrase to WPA PSK tool
3  * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "common.h"
19 #include "sha1.h"
20
21
22 int main(int argc, char *argv[])
23 {
24         unsigned char psk[32];
25         int i;
26         char *ssid, *passphrase, buf[64], *pos;
27
28         if (argc < 2) {
29                 printf("usage: wpa_passphrase <ssid> [passphrase]\n"
30                         "\nIf passphrase is left out, it will be read from "
31                         "stdin\n");
32                 return 1;
33         }
34
35         ssid = argv[1];
36
37         if (argc > 2) {
38                 passphrase = argv[2];
39         } else {
40                 printf("# reading passphrase from stdin\n");
41                 if (fgets(buf, sizeof(buf), stdin) == NULL) {
42                         printf("Failed to read passphrase\n");
43                         return 1;
44                 }
45                 buf[sizeof(buf) - 1] = '\0';
46                 pos = buf;
47                 while (*pos != '\0') {
48                         if (*pos == '\r' || *pos == '\n') {
49                                 *pos = '\0';
50                                 break;
51                         }
52                         pos++;
53                 }
54                 passphrase = buf;
55         }
56
57         if (strlen(passphrase) < 8 || strlen(passphrase) > 63) {
58                 printf("Passphrase must be 8..63 characters\n");
59                 return 1;
60         }
61
62         pbkdf2_sha1(passphrase, ssid, strlen(ssid), 4096, psk, 32);
63
64         printf("network={\n");
65         printf("\tssid=\"%s\"\n", ssid);
66         printf("\t#psk=\"%s\"\n", passphrase);
67         printf("\tpsk=");
68         for (i = 0; i < 32; i++)
69                 printf("%02x", psk[i]);
70         printf("\n");
71         printf("}\n");
72
73         return 0;
74 }