diff --git a/src/pixiewps.c b/src/pixiewps.c index 03cab56..f4760c1 100644 --- a/src/pixiewps.c +++ b/src/pixiewps.c @@ -46,17 +46,21 @@ #include #include + +#include "utils.h" + #ifdef __MACH__ # include # define be32(x) OSSwapBigToHostInt32(x) +#elif __ANDROID__ +# define be32(x) h32tbe(x) #else # include # define be32(x) __be32_to_cpu(x) -#endif /* __MACH__ */ +#endif #include "pixiewps.h" #include "random_r.h" -#include "utils.h" int32_t rand_r(uint32_t *seed); diff --git a/src/pixiewps.h b/src/pixiewps.h index e93ceb4..152826b 100644 --- a/src/pixiewps.h +++ b/src/pixiewps.h @@ -145,7 +145,6 @@ void hmac_sha256(const void *key, int key_len, const unsigned char *data, const /* Key Derivation Function */ void kdf(const void *key, const size_t key_len, unsigned char *res) { - uint32_t i = 1; uint32_t kdk_len = key_len * 8; uint_fast8_t j = 0; @@ -154,7 +153,7 @@ void kdf(const void *key, const size_t key_len, unsigned char *res) { unsigned char *buffer = malloc(WPS_KDF_SALT_LEN + 4 * 2); - for (i = 1; i < 4; i++) { + for (uint32_t i = 1; i < 4; i++) { uint32_t be = be32(i); memcpy(buffer, &be, 4); memcpy(buffer + 4, salt, WPS_KDF_SALT_LEN); diff --git a/src/utils.h b/src/utils.h index 23737b2..1abd186 100644 --- a/src/utils.h +++ b/src/utils.h @@ -106,4 +106,24 @@ void byte_array_print(const unsigned char *buffer, const unsigned int length) { } } +/* Converts a 32 Little Endian bit number to its Big Endian representation */ +unsigned int h32tbe(uint32_t num) { + uint32_t temp = num; + uint32_t res; + uint32_t b0, b1, b2, b3; + unsigned int i = 1; + char *p = (char *) &i; + + if (p[0] == 1) { /* LE */ + b0 = (num & 0x000000ff) << 24; + b1 = (num & 0x0000ff00) << 8; + b2 = (num & 0x00ff0000) >> 8; + b3 = (num & 0xff000000) >> 24; + res = b0 | b1 | b2 | b3; + } else { /* BE */ + res = num; + } + return res; +} + #endif /* UTILS_H */