blob: cfab4f1b55512d40d686d89cecbaaf35dbdc5a31 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - ASCII passphrase to WPA PSK tool
3 * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000010#include <termios.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011
12#include "common.h"
13#include "crypto/sha1.h"
14
15
16int main(int argc, char *argv[])
17{
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000018 struct termios term;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019 unsigned char psk[32];
20 int i;
21 char *ssid, *passphrase, buf[64], *pos;
Dmitry Shmidt29333592017-01-09 12:27:11 -080022 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023
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 {
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000036 bool ctrl_echo;
37
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080038 fprintf(stderr, "# reading passphrase from stdin\n");
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000039 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 if (fgets(buf, sizeof(buf), stdin) == NULL) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080050 fprintf(stderr, "Failed to read passphrase\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 return 1;
52 }
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000053 term.c_lflag |= ECHO;
54 if (ctrl_echo && tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0) {
55 perror("tcsetattr:error enabling echo");
56 return 1;
57 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 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
Dmitry Shmidt29333592017-01-09 12:27:11 -080070 len = os_strlen(passphrase);
71 if (len < 8 || len > 63) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080072 fprintf(stderr, "Passphrase must be 8..63 characters\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 return 1;
74 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080075 if (has_ctrl_char((u8 *) passphrase, len)) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080076 fprintf(stderr, "Invalid passphrase character\n");
Dmitry Shmidt29333592017-01-09 12:27:11 -080077 return 1;
78 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079
Sunil Ravia04bd252022-05-02 22:54:18 -070080 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085
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}