From 8efb31cf2d95b4f33fac4032f693b1711747527e Mon Sep 17 00:00:00 2001 From: dave Date: Mon, 21 Apr 2008 14:04:00 -0700 Subject: [PATCH] Add standalone crypto implementation. Thanks wpa_supplicant! --- Makefile | 8 +++-- eyefi-config.c | 79 ++++++++++++++------------------------------------ 2 files changed, 27 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index f9edffc..27bc34c 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ CC=gcc CFLAGS=-g -Wall -eyefi-config: eyefi-config.c - $(CC) $(CFLAGS) $< -o $@ +OBJS = eyefi-config.o sha1.o md5.o + +eyefi-config: $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $@ clean: - rm eyefi-config core + rm eyefi-config core $(OBJS) diff --git a/eyefi-config.c b/eyefi-config.c index 69b8d4b..c19f9ec 100755 --- a/eyefi-config.c +++ b/eyefi-config.c @@ -19,6 +19,8 @@ #include #include +#include "eyefi-config.h" + #define O_DIRECT 00040000 /* direct disk access hint */ enum eyefi_file { @@ -64,23 +66,6 @@ int debug_level = 1; fprintf(stderr, ## args); \ } while(0) -/* - * Do some kernel-style types to make - * definitions shorter. - */ -typedef unsigned long u32; -typedef unsigned char u8; - -static inline u32 swap_bytes(u32 src) -{ - unsigned int dest = 0; - dest |= (src & 0xff000000) >> 24; - dest |= (src & 0x00ff0000) >> 8; - dest |= (src & 0x0000ff00) << 8; - dest |= (src & 0x000000ff) << 24; - return dest; -} - /* * Just a few functions so that I can't easily forget about * endinness. @@ -495,13 +480,15 @@ int atoh(char c) else if ((c >= 'a') && (c <= 'z')) return (c - 'a') + 10; debug_printf(5, "non-hex character: '%c'/'%c'\n", c, lc); - return 0; + return -1; } /* * Take a string like "0ab1" and make it * a series of bytes: { 0x0a, 0xb1 } * + * @len is the strlen() of the ascii + * * Destroys the original string. */ char *convert_ascii_to_hex(char *ascii, int len) @@ -513,9 +500,11 @@ char *convert_ascii_to_hex(char *ascii, int len) exit(2); } for (i=0; i < len; i+=2) { - u8 high = atoh(ascii[i]); - u8 low = atoh(ascii[i+1]); + int high = atoh(ascii[i]); + int low = atoh(ascii[i+1]); u8 byte = (high<<4 | low); + if (high < 0 || low < 0) + return NULL; debug_printf(6, "high: %02x low: %02x, both: %02x\n", high, low, byte); ascii[i/2] = byte; } @@ -528,44 +517,20 @@ char *convert_ascii_to_hex(char *ascii, int len) struct wpa_key *make_wpa_key(char *essid, char *pass) { - char program[] = PASSPHRASE_PROG; - // 7 for 2 spaces, 4 quotes and a \0 - char redirect[] = " 2> /dev/null"; - char *cmdbuf = malloc(strlen(essid) + strlen(pass) + strlen(program) + 7 + - strlen(redirect)); - - if (!cmdbuf) - return NULL; - - sprintf(cmdbuf, "%s '%s' '%s' %s", program, essid, pass, redirect); - FILE *pipe = popen(cmdbuf, "r"); - if (!pipe) { - perror("\nunable to execute " PASSPHRASE_PROG); - return NULL; - } - - int key_chars = WPA_KEY_BYTES*2; - char hex_key_in_ascii[key_chars+1]; - char line[1024]; - int read = 0; - while (fgets(&line[0], 1023, pipe)) { - debug_printf(4, "read from %s: '%s'\n", PASSPHRASE_PROG, line); - read = sscanf(&line[0], " psk=%64s", &hex_key_in_ascii[0]); - if (read == 0) - continue; - break; - } - int exit_code = pclose(pipe); - if (!read || exit_code) { - fprintf(stderr, "\nunable to read wpa key from %s\n", PASSPHRASE_PROG); - fprintf(stderr, "Is wpa_supplicant installed?\n"); - exit(4); - } - debug_printf(4, "ascii key: '%s'\n", hex_key_in_ascii); - char *hex_key = convert_ascii_to_hex(hex_key_in_ascii, key_chars); struct wpa_key *key = malloc(sizeof(*key)); - memcpy(&key->key[0], hex_key, WPA_KEY_BYTES); - free(cmdbuf); + + if (strlen(pass) == WPA_KEY_BYTES*2) { + char *hex_pass; + debug_printf(2, "Interpreting password as hex WPA key\n"); + hex_pass = convert_ascii_to_hex(pass, WPA_KEY_BYTES*2); + if (!hex_pass) + return NULL; + memcpy(&key->key[0], pass, WPA_KEY_BYTES); + } else { + debug_printf(2, "Interpreting password as ASCII WPA key\n"); + pbkdf2_sha1(pass, essid, strlen(essid), 4096, + &key->key[0], WPA_KEY_BYTES); + } return key; } -- 2.45.0