wpa_passphrase.c 1.5 KB

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