From: iceman1001 <iceman@iuse.se>
Date: Tue, 19 Jan 2016 15:19:59 +0000 (+0100)
Subject: CHG:  Crapto1 v3.3 for the ARMSRC
X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/417f4ae12711f28d8b27dcd29786a1cc9300aaad?hp=a1afa550ea5b602b6d8bc69bdc6f018696b21ff0

CHG:  Crapto1 v3.3 for the ARMSRC
---

diff --git a/armsrc/crapto1.c b/armsrc/crapto1.c
index 87213757..4b4e240e 100644
--- a/armsrc/crapto1.c
+++ b/armsrc/crapto1.c
@@ -33,8 +33,7 @@ static void __attribute__((constructor)) fill_lut()
 
 static void quicksort(uint32_t* const start, uint32_t* const stop)
 {
-	uint32_t *it = start + 1, *rit = stop;
-	uint32_t tmp;
+	uint32_t *it = start + 1, *rit = stop, t;
 
 	if(it > rit)
 		return;
@@ -44,19 +43,13 @@ static void quicksort(uint32_t* const start, uint32_t* const stop)
 			++it;
 		else if(*rit > *start)
 			--rit;
-		else {
-			tmp = *it;
-			*it = *rit;
-			*rit = tmp;
-		}
+		else
+			t = *it,  *it = *rit, *rit = t;
 
 	if(*rit >= *start)
 		--rit;
-	if(rit != start) {
-		tmp = *rit;
-		*rit = *start;
-		*start = tmp;
-	}
+	if(rit != start)
+		t = *rit,  *rit = *start, *start = t;
 
 	quicksort(start, rit - 1);
 	quicksort(rit + 1, stop);
@@ -325,12 +318,10 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
 {
 	int out;
 	uint8_t ret;
-	uint32_t tmp;
+	uint32_t t;
 
 	s->odd &= 0xffffff;
-	tmp = s->odd;
-	s->odd = s->even;
-	s->even = tmp;
+	t = s->odd, s->odd = s->even, s->even = t;
 
 	out = s->even & 1;
 	out ^= LF_POLY_EVEN & (s->even >>= 1);
@@ -346,7 +337,8 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
  */
 uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb)
 {
-/*	int i, ret = 0;
+	/*
+	int i, ret = 0;
 	for (i = 7; i >= 0; --i)
 		ret |= lfsr_rollback_bit(s, BIT(in, i), fb) << i;
 */
@@ -367,7 +359,8 @@ uint8_t lfsr_rollback_byte(struct Crypto1State *s, uint32_t in, int fb)
  */
 uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb)
 {
-/*	int i;
+	/*
+	int i;
 	uint32_t ret = 0;
 	for (i = 31; i >= 0; --i)
 		ret |= lfsr_rollback_bit(s, BEBIT(in, i), fb) << (i ^ 24);
@@ -435,6 +428,8 @@ int nonce_distance(uint32_t from, uint32_t to)
 static uint32_t fastfwd[2][8] = {
 	{ 0, 0x4BC53, 0xECB1, 0x450E2, 0x25E29, 0x6E27A, 0x2B298, 0x60ECB},
 	{ 0, 0x1D962, 0x4BC53, 0x56531, 0xECB1, 0x135D3, 0x450E2, 0x58980}};
+
+
 /** lfsr_prefix_ks
  *
  * Is an exported helper function from the common prefix attack
@@ -444,10 +439,12 @@ static uint32_t fastfwd[2][8] = {
  * encrypt the NACK which is observed when varying only the 3 last bits of Nr
  * only correct iff [NR_3] ^ NR_3 does not depend on Nr_3
  */
+ // TO VERIFY 
 uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd)
 {
-	uint32_t c, entry, *candidates = malloc(4 << 10);
-	int i, size = 0, good;
+	uint32_t *candidates = malloc(4 << 10);
+	uint32_t c,  entry;
+	int size = 0, i, good;
 
 	if(!candidates)
 		return 0;
@@ -503,6 +500,12 @@ check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
 
 /** lfsr_common_prefix
  * Implentation of the common prefix attack.
+ * Requires the 28 bit constant prefix used as reader nonce (pfx)
+ * The reader response used (rr)
+ * The keystream used to encrypt the observed NACK's (ks)
+ * The parity bits (par)
+ * It returns a zero terminated list of possible cipher states after the
+ * tag nonce was fed in
  */
 struct Crypto1State*
 lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8])
@@ -516,8 +519,9 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8])
 	s = statelist = malloc((sizeof *statelist) << 20);
 	if(!s || !odd || !even) {
 		free(statelist);
-		statelist = 0;
-		goto out;
+		free(odd);
+		free(even);
+		return 0;
 	}
 
 	for(o = odd; *o + 1; ++o)
@@ -529,8 +533,9 @@ lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8])
 			}
 
 	s->odd = s->even = 0;
-out:
+
 	free(odd);
 	free(even);
+
 	return statelist;
 }
diff --git a/armsrc/crapto1.h b/armsrc/crapto1.h
index b144853c..50654db9 100644
--- a/armsrc/crapto1.h
+++ b/armsrc/crapto1.h
@@ -15,7 +15,7 @@
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     MA  02110-1301, US$
 
-    Copyright (C) 2008-2008 bla <blapost@gmail.com>
+    Copyright (C) 2008-2014 bla <blapost@gmail.com>
 */
 #ifndef CRAPTO1_INCLUDED
 #define CRAPTO1_INCLUDED
@@ -43,6 +43,9 @@ uint8_t lfsr_rollback_bit(struct Crypto1State* s, uint32_t in, int fb);
 uint8_t lfsr_rollback_byte(struct Crypto1State* s, uint32_t in, int fb);
 uint32_t lfsr_rollback_word(struct Crypto1State* s, uint32_t in, int fb);
 int nonce_distance(uint32_t from, uint32_t to);
+#define SWAPENDIAN(x)\
+	(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
+	
 #define FOREACH_VALID_NONCE(N, FILTER, FSIZE)\
 	uint32_t __n = 0,__M = 0, N = 0;\
 	int __i;\
diff --git a/armsrc/crypto1.c b/armsrc/crypto1.c
index db864113..b218af19 100644
--- a/armsrc/crypto1.c
+++ b/armsrc/crypto1.c
@@ -20,8 +20,6 @@
 #include "crapto1.h"
 #include <stdlib.h>
 
-#define SWAPENDIAN(x)\
-	(x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
 
 void crypto1_create(struct Crypto1State *s, uint64_t key)
 {