Format source code with "make style"

This commit is contained in:
Romuald Conty 2013-01-20 15:36:59 +01:00
parent 2596aeac80
commit 6048309a13
5 changed files with 1221 additions and 1209 deletions

View file

@ -126,8 +126,7 @@ extend_table_simple(uint32_t *tbl, uint32_t **end, int bit)
static struct Crypto1State * static struct Crypto1State *
recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks, recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
uint32_t *e_head, uint32_t *e_tail, uint32_t eks, int rem, uint32_t *e_head, uint32_t *e_tail, uint32_t eks, int rem,
struct Crypto1State *sl, uint32_t in) struct Crypto1State *sl, uint32_t in) {
{
uint32_t *o, *e, i; uint32_t *o, *e, i;
if (rem == -1) { if (rem == -1) {
@ -163,8 +162,7 @@ recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
e_tail = binsearch(e_head, e = e_tail); e_tail = binsearch(e_head, e = e_tail);
sl = recover(o_tail--, o, oks, sl = recover(o_tail--, o, oks,
e_tail--, e, eks, rem, sl, in); e_tail--, e, eks, rem, sl, in);
} } else if (*o_tail > *e_tail)
else if(*o_tail > *e_tail)
o_tail = binsearch(o_head, o_tail) - 1; o_tail = binsearch(o_head, o_tail) - 1;
else else
e_tail = binsearch(e_head, e_tail) - 1; e_tail = binsearch(e_head, e_tail) - 1;
@ -176,8 +174,7 @@ recover(uint32_t *o_head, uint32_t *o_tail, uint32_t oks,
* additionally you can use the in parameter to specify the value * additionally you can use the in parameter to specify the value
* that was fed into the lfsr at the time the keystream was generated * that was fed into the lfsr at the time the keystream was generated
*/ */
struct Crypto1State* lfsr_recovery32(uint32_t ks2, uint32_t in) struct Crypto1State *lfsr_recovery32(uint32_t ks2, uint32_t in) {
{
struct Crypto1State *statelist; struct Crypto1State *statelist;
uint32_t *odd_head = 0, *odd_tail = 0, oks = 0; uint32_t *odd_head = 0, *odd_tail = 0, oks = 0;
uint32_t *even_head = 0, *even_tail = 0, eks = 0; uint32_t *even_head = 0, *even_tail = 0, eks = 0;
@ -220,29 +217,32 @@ out:
static const uint32_t S1[] = { 0x62141, 0x310A0, 0x18850, 0x0C428, 0x06214, static const uint32_t S1[] = { 0x62141, 0x310A0, 0x18850, 0x0C428, 0x06214,
0x0310A, 0x85E30, 0xC69AD, 0x634D6, 0xB5CDE, 0xDE8DA, 0x6F46D, 0xB3C83, 0x0310A, 0x85E30, 0xC69AD, 0x634D6, 0xB5CDE, 0xDE8DA, 0x6F46D, 0xB3C83,
0x59E41, 0xA8995, 0xD027F, 0x6813F, 0x3409F, 0x9E6FA}; 0x59E41, 0xA8995, 0xD027F, 0x6813F, 0x3409F, 0x9E6FA
};
static const uint32_t S2[] = { 0x3A557B00, 0x5D2ABD80, 0x2E955EC0, 0x174AAF60, static const uint32_t S2[] = { 0x3A557B00, 0x5D2ABD80, 0x2E955EC0, 0x174AAF60,
0x0BA557B0, 0x05D2ABD8, 0x0449DE68, 0x048464B0, 0x42423258, 0x278192A8, 0x0BA557B0, 0x05D2ABD8, 0x0449DE68, 0x048464B0, 0x42423258, 0x278192A8,
0x156042D0, 0x0AB02168, 0x43F89B30, 0x61FC4D98, 0x765EAD48, 0x7D8FDD20, 0x156042D0, 0x0AB02168, 0x43F89B30, 0x61FC4D98, 0x765EAD48, 0x7D8FDD20,
0x7EC7EE90, 0x7F63F748, 0x79117020}; 0x7EC7EE90, 0x7F63F748, 0x79117020
};
static const uint32_t T1[] = { static const uint32_t T1[] = {
0x4F37D, 0x279BE, 0x97A6A, 0x4BD35, 0x25E9A, 0x12F4D, 0x097A6, 0x80D66, 0x4F37D, 0x279BE, 0x97A6A, 0x4BD35, 0x25E9A, 0x12F4D, 0x097A6, 0x80D66,
0xC4006, 0x62003, 0xB56B4, 0x5AB5A, 0xA9318, 0xD0F39, 0x6879C, 0xB057B, 0xC4006, 0x62003, 0xB56B4, 0x5AB5A, 0xA9318, 0xD0F39, 0x6879C, 0xB057B,
0x582BD, 0x2C15E, 0x160AF, 0x8F6E2, 0xC3DC4, 0xE5857, 0x72C2B, 0x39615, 0x582BD, 0x2C15E, 0x160AF, 0x8F6E2, 0xC3DC4, 0xE5857, 0x72C2B, 0x39615,
0x98DBF, 0xC806A, 0xE0680, 0x70340, 0x381A0, 0x98665, 0x4C332, 0xA272C}; 0x98DBF, 0xC806A, 0xE0680, 0x70340, 0x381A0, 0x98665, 0x4C332, 0xA272C
};
static const uint32_t T2[] = { 0x3C88B810, 0x5E445C08, 0x2982A580, 0x14C152C0, static const uint32_t T2[] = { 0x3C88B810, 0x5E445C08, 0x2982A580, 0x14C152C0,
0x4A60A960, 0x253054B0, 0x52982A58, 0x2FEC9EA8, 0x1156C4D0, 0x08AB6268, 0x4A60A960, 0x253054B0, 0x52982A58, 0x2FEC9EA8, 0x1156C4D0, 0x08AB6268,
0x42F53AB0, 0x217A9D58, 0x161DC528, 0x0DAE6910, 0x46D73488, 0x25CB11C0, 0x42F53AB0, 0x217A9D58, 0x161DC528, 0x0DAE6910, 0x46D73488, 0x25CB11C0,
0x52E588E0, 0x6972C470, 0x34B96238, 0x5CFC3A98, 0x28DE96C8, 0x12CFC0E0, 0x52E588E0, 0x6972C470, 0x34B96238, 0x5CFC3A98, 0x28DE96C8, 0x12CFC0E0,
0x4967E070, 0x64B3F038, 0x74F97398, 0x7CDC3248, 0x38CE92A0, 0x1C674950, 0x4967E070, 0x64B3F038, 0x74F97398, 0x7CDC3248, 0x38CE92A0, 0x1C674950,
0x0E33A4A8, 0x01B959D0, 0x40DCACE8, 0x26CEDDF0}; 0x0E33A4A8, 0x01B959D0, 0x40DCACE8, 0x26CEDDF0
};
static const uint32_t C1[] = { 0x846B5, 0x4235A, 0x211AD}; static const uint32_t C1[] = { 0x846B5, 0x4235A, 0x211AD};
static const uint32_t C2[] = { 0x1A822E0, 0x21A822E0, 0x21A822E0}; static const uint32_t C2[] = { 0x1A822E0, 0x21A822E0, 0x21A822E0};
/** Reverse 64 bits of keystream into possible cipher states /** Reverse 64 bits of keystream into possible cipher states
* Variation mentioned in the paper. Somewhat optimized version * Variation mentioned in the paper. Somewhat optimized version
*/ */
struct Crypto1State* lfsr_recovery64(uint32_t ks2, uint32_t ks3) struct Crypto1State *lfsr_recovery64(uint32_t ks2, uint32_t ks3) {
{
struct Crypto1State *statelist, *sl; struct Crypto1State *statelist, *sl;
uint8_t oks[32], eks[32], hi[32]; uint8_t oks[32], eks[32], hi[32];
uint32_t low = 0, win = 0; uint32_t low = 0, win = 0;
@ -302,7 +302,8 @@ struct Crypto1State* lfsr_recovery64(uint32_t ks2, uint32_t ks3)
sl->even = win; sl->even = win;
++sl; ++sl;
sl->odd = sl->even = 0; sl->odd = sl->even = 0;
continue2:; continue2:
;
} }
} }
return statelist; return statelist;
@ -378,7 +379,8 @@ int nonce_distance(uint32_t from, uint32_t to)
static uint32_t fastfwd[2][8] = { static uint32_t fastfwd[2][8] = {
{ 0, 0x4BC53, 0xECB1, 0x450E2, 0x25E29, 0x6E27A, 0x2B298, 0x60ECB}, { 0, 0x4BC53, 0xECB1, 0x450E2, 0x25E29, 0x6E27A, 0x2B298, 0x60ECB},
{ 0, 0x1D962, 0x4BC53, 0x56531, 0xECB1, 0x135D3, 0x450E2, 0x58980}}; { 0, 0x1D962, 0x4BC53, 0x56531, 0xECB1, 0x135D3, 0x450E2, 0x58980}
};
/** lfsr_prefix_ks /** lfsr_prefix_ks
@ -420,8 +422,7 @@ uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
*/ */
static struct Crypto1State * static struct Crypto1State *
check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8], check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
uint32_t odd, uint32_t even, struct Crypto1State* sl) uint32_t odd, uint32_t even, struct Crypto1State *sl) {
{
uint32_t ks1, nr, ks2, rr, ks3, c, good = 1; uint32_t ks1, nr, ks2, rr, ks3, c, good = 1;
for (c = 0; good && c < 8; ++c) { for (c = 0; good && c < 8; ++c) {
@ -461,8 +462,7 @@ struct Crypto1State* lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8]
* tag nonce was fed in * tag nonce was fed in
*/ */
struct Crypto1State * struct Crypto1State *
lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]) lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]) {
{
struct Crypto1State *statelist, *s; struct Crypto1State *statelist, *s;
uint32_t *odd, *even, *o, *e, top; uint32_t *odd, *even, *o, *e, top;

View file

@ -23,8 +23,7 @@
#define SWAPENDIAN(x)\ #define SWAPENDIAN(x)\
(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16) (x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
struct Crypto1State * crypto1_create(uint64_t key) struct Crypto1State *crypto1_create(uint64_t key) {
{
struct Crypto1State *s = malloc(sizeof(*s)); struct Crypto1State *s = malloc(sizeof(*s));
int i; int i;

View file

@ -52,7 +52,8 @@
nfc_context *context; nfc_context *context;
int main(int argc, char * const argv[]) { int main(int argc, char *const argv[])
{
const nfc_modulation nm = { const nfc_modulation nm = {
.nmt = NMT_ISO14443A, .nmt = NMT_ISO14443A,
.nbr = NBR_106, .nbr = NBR_106,
@ -122,8 +123,7 @@ int main(int argc, char * const argv[]) {
} }
// fprintf(stdout, "Number of probes: %d\n", probes); // fprintf(stdout, "Number of probes: %d\n", probes);
break; break;
case 'T': case 'T': {
{
int res; int res;
// Nonce tolerance range // Nonce tolerance range
if (((res = atoi(optarg)) < 0)) { if (((res = atoi(optarg)) < 0)) {
@ -525,7 +525,8 @@ error:
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void usage(FILE * stream, int errno) { void usage(FILE *stream, int errno)
{
fprintf(stream, "Usage: mfoc [-h] [-k key]... [-P probnum] [-T tolerance] [-O output]\n"); fprintf(stream, "Usage: mfoc [-h] [-k key]... [-P probnum] [-T tolerance] [-O output]\n");
fprintf(stream, "\n"); fprintf(stream, "\n");
fprintf(stream, " h print this help and exit\n"); fprintf(stream, " h print this help and exit\n");
@ -547,7 +548,8 @@ void usage(FILE * stream, int errno) {
exit(errno); exit(errno);
} }
void mf_init(mfreader *r) { void mf_init(mfreader *r)
{
// Connect to the first NFC device // Connect to the first NFC device
nfc_init(&context); nfc_init(&context);
r->pdi = nfc_open(context, NULL); r->pdi = nfc_open(context, NULL);
@ -557,7 +559,8 @@ void mf_init(mfreader *r) {
} }
} }
void mf_configure(nfc_device* pdi) { void mf_configure(nfc_device *pdi)
{
if (nfc_initiator_init(pdi) < 0) { if (nfc_initiator_init(pdi) < 0) {
nfc_perror(pdi, "nfc_initiator_init"); nfc_perror(pdi, "nfc_initiator_init");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -588,7 +591,8 @@ void mf_configure(nfc_device* pdi) {
} }
} }
void mf_select_tag(nfc_device* pdi, nfc_target* pnt) { void mf_select_tag(nfc_device *pdi, nfc_target *pnt)
{
// Poll for a ISO14443A (MIFARE) tag // Poll for a ISO14443A (MIFARE) tag
const nfc_modulation nm = { const nfc_modulation nm = {
.nmt = NMT_ISO14443A, .nmt = NMT_ISO14443A,
@ -609,7 +613,8 @@ int trailer_block(uint32_t block)
} }
// Return position of sector if it is encrypted with the default key otherwise exit.. // Return position of sector if it is encrypted with the default key otherwise exit..
int find_exploit_sector(mftag t) { int find_exploit_sector(mftag t)
{
int i; int i;
bool interesting = false; bool interesting = false;
@ -633,7 +638,8 @@ int find_exploit_sector(mftag t) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void mf_anticollision(mftag t, mfreader r) { void mf_anticollision(mftag t, mfreader r)
{
const nfc_modulation nm = { const nfc_modulation nm = {
.nmt = NMT_ISO14443A, .nmt = NMT_ISO14443A,
.nbr = NBR_106, .nbr = NBR_106,
@ -645,7 +651,8 @@ void mf_anticollision(mftag t, mfreader r) {
} }
} }
int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d, pKeys *pk, char mode, bool dumpKeysA) { int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d, pKeys *pk, char mode, bool dumpKeysA)
{
struct Crypto1State *pcs; struct Crypto1State *pcs;
struct Crypto1State *revstate; struct Crypto1State *revstate;
struct Crypto1State *revstate_start; struct Crypto1State *revstate_start;
@ -902,7 +909,8 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
} }
// Return the median value from the nonce distances array // Return the median value from the nonce distances array
uint32_t median(denonce d) { uint32_t median(denonce d)
{
int middle = (int) d.num_distances / 2; int middle = (int) d.num_distances / 2;
qsort(d.distances, d.num_distances, sizeof(uint32_t), compar_int); qsort(d.distances, d.num_distances, sizeof(uint32_t), compar_int);
@ -915,16 +923,19 @@ uint32_t median(denonce d) {
} }
} }
int compar_int(const void * a, const void * b) { int compar_int(const void *a, const void *b)
{
return (*(uint64_t *)b - * (uint64_t *)a); return (*(uint64_t *)b - * (uint64_t *)a);
} }
// Compare countKeys structure // Compare countKeys structure
int compar_special_int(const void * a, const void * b) { int compar_special_int(const void *a, const void *b)
{
return (((countKeys *)b)->count - ((countKeys *)a)->count); return (((countKeys *)b)->count - ((countKeys *)a)->count);
} }
countKeys * uniqsort(uint64_t * possibleKeys, uint32_t size) { countKeys *uniqsort(uint64_t *possibleKeys, uint32_t size)
{
unsigned int i, j = 0; unsigned int i, j = 0;
int count = 0; int count = 0;
countKeys *our_counts; countKeys *our_counts;
@ -953,23 +964,25 @@ countKeys * uniqsort(uint64_t * possibleKeys, uint32_t size) {
// Return 1 if the nonce is invalid else return 0 // Return 1 if the nonce is invalid else return 0
int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t * parity) { int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity)
{
return ((odd_parity((Nt >> 24) & 0xFF) == ((parity[0]) ^ odd_parity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1, 16))) & \ return ((odd_parity((Nt >> 24) & 0xFF) == ((parity[0]) ^ odd_parity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1, 16))) & \
(odd_parity((Nt >> 16) & 0xFF) == ((parity[1]) ^ odd_parity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1, 8))) & \ (odd_parity((Nt >> 16) & 0xFF) == ((parity[1]) ^ odd_parity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1, 8))) & \
(odd_parity((Nt >> 8) & 0xFF) == ((parity[2]) ^ odd_parity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1, 0)))) ? 1 : 0; (odd_parity((Nt >> 8) & 0xFF) == ((parity[2]) ^ odd_parity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1, 0)))) ? 1 : 0;
} }
void num_to_bytes(uint64_t n, uint32_t len, uint8_t* dest) { void num_to_bytes(uint64_t n, uint32_t len, uint8_t *dest)
{
while (len--) { while (len--) {
dest[len] = (uint8_t) n; dest[len] = (uint8_t) n;
n >>= 8; n >>= 8;
} }
} }
long long unsigned int bytes_to_num(uint8_t* src, uint32_t len) { long long unsigned int bytes_to_num(uint8_t *src, uint32_t len)
uint64_t num = 0;
while (len--)
{ {
uint64_t num = 0;
while (len--) {
num = (num << 8) | (*src); num = (num << 8) | (*src);
src++; src++;
} }