From 4eaa2fc5aabc0b1ddb928a5a47f54f222670bcc9 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 16 Apr 2019 23:02:40 +0200 Subject: [PATCH] move common util fcts to /common to avoid common files (e.g. crc) to depend on non-common files --- armsrc/Makefile | 1 + armsrc/mifareutil.h | 2 +- armsrc/util.c | 78 -------------------------------------- armsrc/util.h | 23 ------------ client/Makefile | 1 + client/emv/emvjson.c | 1 - client/emv/emvjson.h | 1 + client/fido/cose.c | 1 - client/fido/cose.h | 2 + client/util.c | 69 ---------------------------------- client/util.h | 30 --------------- common/commonutil.c | 89 ++++++++++++++++++++++++++++++++++++++++++++ common/commonutil.h | 55 +++++++++++++++++++++++++++ common/crc.h | 2 +- common/crc16.h | 4 +- common/i2c.h | 1 - common/lfdemod.h | 1 - common/tea.h | 2 +- common/wiegand.h | 1 - 19 files changed, 155 insertions(+), 209 deletions(-) create mode 100644 common/commonutil.c create mode 100644 common/commonutil.h diff --git a/armsrc/Makefile b/armsrc/Makefile index 5ec73b2b3..a3280a0b9 100644 --- a/armsrc/Makefile +++ b/armsrc/Makefile @@ -157,6 +157,7 @@ THUMBSRC = start.c \ $(SRC_FPC) \ appmain.c \ printf.c \ + commonutil.c \ util.c \ string.c \ BigBuf.c \ diff --git a/armsrc/mifareutil.h b/armsrc/mifareutil.h index 46f6fa81a..8bfb1c13f 100644 --- a/armsrc/mifareutil.h +++ b/armsrc/mifareutil.h @@ -15,7 +15,7 @@ #include "proxmark3.h" #include "apps.h" #include "parity.h" -#include "util.h" +#include "commonutil.h" #include "string.h" #include "iso14443a.h" #include "crapto1/crapto1.h" diff --git a/armsrc/util.c b/armsrc/util.c index 8ee91b464..60b4723b4 100644 --- a/armsrc/util.c +++ b/armsrc/util.c @@ -13,84 +13,6 @@ size_t nbytes(size_t nbits) { return (nbits >> 3) + ((nbits % 8) > 0); } -/* - ref http://www.csm.ornl.gov/~dunigan/crc.html - Returns the value v with the bottom b [0,32] bits reflected. - Example: reflect(0x3e23L,3) == 0x3e26 -*/ -uint32_t reflect(uint32_t v, int b) { - uint32_t t = v; - for (int i = 0; i < b; ++i) { - if (t & 1) - v |= BITMASK((b - 1) - i); - else - v &= ~BITMASK((b - 1) - i); - t >>= 1; - } - return v; -} - -uint8_t reflect8(uint8_t b) { - return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; -} -uint16_t reflect16(uint16_t b) { - uint16_t v = 0; - v |= (b & 0x8000) >> 15; - v |= (b & 0x4000) >> 13; - v |= (b & 0x2000) >> 11; - v |= (b & 0x1000) >> 9; - v |= (b & 0x0800) >> 7; - v |= (b & 0x0400) >> 5; - v |= (b & 0x0200) >> 3; - v |= (b & 0x0100) >> 1; - - v |= (b & 0x0080) << 1; - v |= (b & 0x0040) << 3; - v |= (b & 0x0020) << 5; - v |= (b & 0x0010) << 7; - v |= (b & 0x0008) << 9; - v |= (b & 0x0004) << 11; - v |= (b & 0x0002) << 13; - v |= (b & 0x0001) << 15; - return v; -} - -void num_to_bytes(uint64_t n, size_t len, uint8_t *dest) { - while (len--) { - dest[len] = (uint8_t) n; - n >>= 8; - } -} - -uint64_t bytes_to_num(uint8_t *src, size_t len) { - uint64_t num = 0; - while (len--) { - num = (num << 8) | (*src); - src++; - } - return num; -} - -// RotateLeft - Ultralight, Desfire -void rol(uint8_t *data, const size_t len) { - uint8_t first = data[0]; - for (size_t i = 0; i < len - 1; i++) { - data[i] = data[i + 1]; - } - data[len - 1] = first; -} - -void lsl(uint8_t *data, size_t len) { - for (size_t n = 0; n < len - 1; n++) { - data[n] = (data[n] << 1) | (data[n + 1] >> 7); - } - data[len - 1] <<= 1; -} - -int32_t le24toh(uint8_t data[3]) { - return (data[2] << 16) | (data[1] << 8) | data[0]; -} - //convert hex digit to integer uint8_t hex2int(char hexchar) { switch (hexchar) { diff --git a/armsrc/util.h b/armsrc/util.h index fc519fe43..9be593ad5 100644 --- a/armsrc/util.h +++ b/armsrc/util.h @@ -40,20 +40,6 @@ #define BUTTON_DOUBLE_CLICK -2 #define BUTTON_ERROR -99 -#ifndef BSWAP_16 -# define BSWAP_16(x) ((( ((x) & 0xFF00 ) >> 8))| ( (((x) & 0x00FF) << 8))) -#endif -#ifndef BITMASK -# define BITMASK(X) (1 << (X)) -#endif -#ifndef ARRAYLEN -# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0])) -#endif - -#ifndef NTIME -# define NTIME(n) for (int _index = 0; _index < n; _index++) -#endif - #ifndef REV8 #define REV8(x) ((((x)>>7)&1)+((((x)>>6)&1)<<1)+((((x)>>5)&1)<<2)+((((x)>>4)&1)<<3)+((((x)>>3)&1)<<4)+((((x)>>2)&1)<<5)+((((x)>>1)&1)<<6)+(((x)&1)<<7)) #endif @@ -84,15 +70,6 @@ size_t nbytes(size_t nbits); -uint32_t reflect(uint32_t v, int b); // used in crc.c ... -uint8_t reflect8(uint8_t b); // dedicated 8bit reversal -uint16_t reflect16(uint16_t b); // dedicated 16bit reversal - -void num_to_bytes(uint64_t n, size_t len, uint8_t *dest); -uint64_t bytes_to_num(uint8_t *src, size_t len); -void rol(uint8_t *data, const size_t len); -void lsl(uint8_t *data, size_t len); -int32_t le24toh(uint8_t data[3]); uint8_t hex2int(char hexchar); void LED(int led, int ms); diff --git a/client/Makefile b/client/Makefile index 7992b68b4..9d78ab125 100644 --- a/client/Makefile +++ b/client/Makefile @@ -116,6 +116,7 @@ POSTCOMPILE = $(MV) -f $(OBJDIR)/$*.Td $(OBJDIR)/$*.d CORESRCS = uart_posix.c \ uart_win32.c \ ui.c \ + commonutil.c \ util.c \ util_posix.c \ scandir.c \ diff --git a/client/emv/emvjson.c b/client/emv/emvjson.c index 59de9d37e..c9cffae93 100644 --- a/client/emv/emvjson.c +++ b/client/emv/emvjson.c @@ -14,7 +14,6 @@ #include #include #include -#include "util.h" #include "ui.h" #include "proxmark3.h" #include "emv_tags.h" diff --git a/client/emv/emvjson.h b/client/emv/emvjson.h index 6bcf8243d..edff18bcd 100644 --- a/client/emv/emvjson.h +++ b/client/emv/emvjson.h @@ -12,6 +12,7 @@ #include #include "tlv.h" +#include "commonutil.h" typedef struct { tlv_tag_t Tag; diff --git a/client/fido/cose.c b/client/fido/cose.c index a3fc9755c..6d2fa4faa 100644 --- a/client/fido/cose.c +++ b/client/fido/cose.c @@ -13,7 +13,6 @@ #include "cose.h" #include #include "cbortools.h" -#include "util.h" static const char COSEEmptyStr[] = ""; diff --git a/client/fido/cose.h b/client/fido/cose.h index f8f28aabf..3243a1632 100644 --- a/client/fido/cose.h +++ b/client/fido/cose.h @@ -16,6 +16,8 @@ #include #include #include +#include "commonutil.h" +#include "util.h" const char *GetCOSEAlgName(int id); const char *GetCOSEAlgDescription(int id); diff --git a/client/util.c b/client/util.c index b14023067..ebb534fdf 100644 --- a/client/util.c +++ b/client/util.c @@ -372,22 +372,6 @@ void print_blocks(uint32_t *data, size_t len) { } } -void num_to_bytes(uint64_t n, size_t len, uint8_t *dest) { - while (len--) { - dest[len] = n & 0xFF; - n >>= 8; - } -} - -uint64_t bytes_to_num(uint8_t *src, size_t len) { - uint64_t num = 0; - while (len--) { - num = (num << 8) | (*src); - src++; - } - return num; -} - // takes a number (uint64_t) and creates a binarray in dest. void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest) { while (len--) { @@ -773,9 +757,6 @@ void xor(unsigned char *dst, unsigned char *src, size_t len) { *dst ^= *src; } -int32_t le24toh(uint8_t data[3]) { - return (data[2] << 16) | (data[1] << 8) | data[0]; -} // Pack a bitarray into a uint32_t. uint32_t PackBits(uint8_t start, uint8_t len, uint8_t *bits) { @@ -791,16 +772,6 @@ uint32_t PackBits(uint8_t start, uint8_t len, uint8_t *bits) { return tmp; } -// RotateLeft - Ultralight, Desfire, works on byte level -// 00-01-02 >> 01-02-00 -void rol(uint8_t *data, const size_t len) { - uint8_t first = data[0]; - for (size_t i = 0; i < len - 1; i++) { - data[i] = data[i + 1]; - } - data[len - 1] = first; -} - /* uint8_t pw_rev_A(uint8_t b) { b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; @@ -809,46 +780,6 @@ uint8_t pw_rev_A(uint8_t b) { return b; } */ -uint8_t reflect8(uint8_t b) { - return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; -} -uint16_t reflect16(uint16_t b) { - uint16_t v = 0; - v |= (b & 0x8000) >> 15; - v |= (b & 0x4000) >> 13; - v |= (b & 0x2000) >> 11; - v |= (b & 0x1000) >> 9; - v |= (b & 0x0800) >> 7; - v |= (b & 0x0400) >> 5; - v |= (b & 0x0200) >> 3; - v |= (b & 0x0100) >> 1; - - v |= (b & 0x0080) << 1; - v |= (b & 0x0040) << 3; - v |= (b & 0x0020) << 5; - v |= (b & 0x0010) << 7; - v |= (b & 0x0008) << 9; - v |= (b & 0x0004) << 11; - v |= (b & 0x0002) << 13; - v |= (b & 0x0001) << 15; - return v; -} -/* - ref http://www.csm.ornl.gov/~dunigan/crc.html - Returns the value v with the bottom b [0,32] bits reflected. - Example: reflect(0x3e23L,3) == 0x3e26 -*/ -uint32_t reflect(uint32_t v, int b) { - uint32_t t = v; - for (int i = 0; i < b; ++i) { - if (t & 1) - v |= BITMASK((b - 1) - i); - else - v &= ~BITMASK((b - 1) - i); - t >>= 1; - } - return v; -} uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor) { uint64_t remaind = 0, quotient = 0, result = 0; diff --git a/client/util.h b/client/util.h index 5e60868bb..bd82237d0 100644 --- a/client/util.h +++ b/client/util.h @@ -24,9 +24,6 @@ #include #endif -#ifndef BITMASK -# define BITMASK(X) (1 << (X)) -#endif #ifndef ROTR # define ROTR(x,n) (((uintmax_t)(x) >> (n)) | ((uintmax_t)(x) << ((sizeof(x) * 8) - (n)))) #endif @@ -85,23 +82,6 @@ #endif #endif -// endian change for 16bit -#ifdef __GNUC__ -#ifndef BSWAP_16 -#define BSWAP_16(x) __builtin_bswap16(x) -#endif -#else -#ifdef _MSC_VER -#ifndef BSWAP_16 -#define BSWAP_16(x) _byteswap_ushort(x) -#endif -#else -#ifndef BSWAP_16 -# define BSWAP_16(x) ((( ((x) & 0xFF00 ) >> 8))| ( (((x) & 0x00FF) << 8))) -#endif -#endif -#endif - #define EVEN 0 #define ODD 1 @@ -132,10 +112,6 @@ # define FILE_PATH_SIZE 1000 #endif -#ifndef ARRAYLEN -# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0])) -#endif - #if defined(__linux__) || (__APPLE__) # define _BLUE_(s) "\x1b[34m" s "\x1b[0m " #else @@ -217,8 +193,6 @@ char *sprint_ascii_ex(const uint8_t *data, const size_t len, const size_t min_st void print_blocks(uint32_t *data, size_t len); -void num_to_bytes(uint64_t n, size_t len, uint8_t *dest); -uint64_t bytes_to_num(uint8_t *src, size_t len); void num_to_bytebits(uint64_t n, size_t len, uint8_t *dest); void num_to_bytebitsLSBF(uint64_t n, size_t len, uint8_t *dest); uint8_t *SwapEndian64(const uint8_t *src, const size_t len, const uint8_t blockSize); @@ -251,10 +225,6 @@ void xor(unsigned char *dst, unsigned char *src, size_t len); int32_t le24toh(uint8_t data[3]); uint32_t PackBits(uint8_t start, uint8_t len, uint8_t *bits); -void rol(uint8_t *data, const size_t len); -uint32_t reflect(uint32_t v, int b); -uint8_t reflect8(uint8_t b); // dedicated 8bit reversal -uint16_t reflect16(uint16_t b); // dedicated 16bit reversal uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor); int num_CPUs(void); // number of logical CPUs diff --git a/common/commonutil.c b/common/commonutil.c new file mode 100644 index 000000000..75ad41285 --- /dev/null +++ b/common/commonutil.c @@ -0,0 +1,89 @@ +//----------------------------------------------------------------------------- +// Jonathan Westhues, Sept 2005 +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// Utility functions used in many places, not specific to any piece of code. +//----------------------------------------------------------------------------- +#include "commonutil.h" + +/* + ref http://www.csm.ornl.gov/~dunigan/crc.html + Returns the value v with the bottom b [0,32] bits reflected. + Example: reflect(0x3e23L,3) == 0x3e26 +*/ +uint32_t reflect(uint32_t v, int b) { + uint32_t t = v; + for (int i = 0; i < b; ++i) { + if (t & 1) + v |= BITMASK((b - 1) - i); + else + v &= ~BITMASK((b - 1) - i); + t >>= 1; + } + return v; +} + +uint8_t reflect8(uint8_t b) { + return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32; +} +uint16_t reflect16(uint16_t b) { + uint16_t v = 0; + v |= (b & 0x8000) >> 15; + v |= (b & 0x4000) >> 13; + v |= (b & 0x2000) >> 11; + v |= (b & 0x1000) >> 9; + v |= (b & 0x0800) >> 7; + v |= (b & 0x0400) >> 5; + v |= (b & 0x0200) >> 3; + v |= (b & 0x0100) >> 1; + + v |= (b & 0x0080) << 1; + v |= (b & 0x0040) << 3; + v |= (b & 0x0020) << 5; + v |= (b & 0x0010) << 7; + v |= (b & 0x0008) << 9; + v |= (b & 0x0004) << 11; + v |= (b & 0x0002) << 13; + v |= (b & 0x0001) << 15; + return v; +} + +void num_to_bytes(uint64_t n, size_t len, uint8_t *dest) { + while (len--) { + dest[len] = (uint8_t) n; + n >>= 8; + } +} + +uint64_t bytes_to_num(uint8_t *src, size_t len) { + uint64_t num = 0; + while (len--) { + num = (num << 8) | (*src); + src++; + } + return num; +} + +// RotateLeft - Ultralight, Desfire +void rol(uint8_t *data, const size_t len) { + uint8_t first = data[0]; + for (size_t i = 0; i < len - 1; i++) { + data[i] = data[i + 1]; + } + data[len - 1] = first; +} + +void lsl(uint8_t *data, size_t len) { + for (size_t n = 0; n < len - 1; n++) { + data[n] = (data[n] << 1) | (data[n + 1] >> 7); + } + data[len - 1] <<= 1; +} + +int32_t le24toh(uint8_t data[3]) { + return (data[2] << 16) | (data[1] << 8) | data[0]; +} + diff --git a/common/commonutil.h b/common/commonutil.h new file mode 100644 index 000000000..e4b2178e5 --- /dev/null +++ b/common/commonutil.h @@ -0,0 +1,55 @@ +//----------------------------------------------------------------------------- +// Jonathan Westhues, Aug 2005 +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// Utility functions used in many places, not specific to any piece of code. +//----------------------------------------------------------------------------- + +#ifndef __COMMONUTIL_H +#define __COMMONUTIL_H + +#include +#include +// endian change for 16bit +#ifdef __GNUC__ +#ifndef BSWAP_16 +#define BSWAP_16(x) __builtin_bswap16(x) +#endif +#else +#ifdef _MSC_VER +#ifndef BSWAP_16 +#define BSWAP_16(x) _byteswap_ushort(x) +#endif +#else +#ifndef BSWAP_16 +# define BSWAP_16(x) ((( ((x) & 0xFF00 ) >> 8))| ( (((x) & 0x00FF) << 8))) +#endif +#endif +#endif + +#ifndef BITMASK +# define BITMASK(X) (1 << (X)) +#endif +#ifndef ARRAYLEN +# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0])) +#endif + +#ifndef NTIME +# define NTIME(n) for (int _index = 0; _index < n; _index++) +#endif + +uint32_t reflect(uint32_t v, int b); // used in crc.c ... +uint8_t reflect8(uint8_t b); // dedicated 8bit reversal +uint16_t reflect16(uint16_t b); // dedicated 16bit reversal + +void num_to_bytes(uint64_t n, size_t len, uint8_t *dest); +uint64_t bytes_to_num(uint8_t *src, size_t len); + +void rol(uint8_t *data, const size_t len); +void lsl(uint8_t *data, size_t len); +int32_t le24toh(uint8_t data[3]); + +#endif diff --git a/common/crc.h b/common/crc.h index 7cb9caff9..f0fa91194 100644 --- a/common/crc.h +++ b/common/crc.h @@ -10,7 +10,7 @@ #define __CRC_H #include "common.h" //stdint, stddef, stdbool -#include "util.h" // reflect, bswap_16 +#include "commonutil.h" // reflect, bswap_16 typedef struct crc_ctx { uint32_t state; diff --git a/common/crc16.h b/common/crc16.h index c5c486df9..cc29d3ee0 100644 --- a/common/crc16.h +++ b/common/crc16.h @@ -10,7 +10,9 @@ #include #include -#include "util.h" +#include +#include +#include "commonutil.h" #define CRC16_POLY_CCITT 0x1021 #define CRC16_POLY_LEGIC 0xc6c6 //0x6363 diff --git a/common/i2c.h b/common/i2c.h index df7f6698c..648d0d8d9 100644 --- a/common/i2c.h +++ b/common/i2c.h @@ -4,7 +4,6 @@ #include #include "proxmark3.h" #include "apps.h" -#include "util.h" #include "BigBuf.h" #include "mifare.h" diff --git a/common/lfdemod.h b/common/lfdemod.h index 75f18f3c6..a9fe3f70b 100644 --- a/common/lfdemod.h +++ b/common/lfdemod.h @@ -19,7 +19,6 @@ #include // for #include // for bool #include "parity.h" // for parity test -#include "util.h" // for ARRAYLEN //might not be high enough for noisy environments #define NOISE_AMPLITUDE_THRESHOLD 15 diff --git a/common/tea.h b/common/tea.h index 43ac93428..57be7e834 100644 --- a/common/tea.h +++ b/common/tea.h @@ -10,7 +10,7 @@ #ifndef __TEA_H #define __TEA_H -#include "util.h" +#include "commonutil.h" #include #include void tea_encrypt(uint8_t *v, uint8_t *key); diff --git a/common/wiegand.h b/common/wiegand.h index 1db50607c..ce219fad1 100644 --- a/common/wiegand.h +++ b/common/wiegand.h @@ -10,7 +10,6 @@ #define __WIEGAND_H #include "common.h" -#include "util.h" uint8_t getParity(uint8_t *bits, uint8_t len, uint8_t type); uint8_t checkParity(uint32_t bits, uint8_t len, uint8_t type);