mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2026-05-12 11:18:11 -07:00
tools: fix mix of spaces & tabs
This commit is contained in:
+356
-356
File diff suppressed because it is too large
Load Diff
+29
-29
@@ -43,18 +43,18 @@ 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)
|
||||
(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;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ parity(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
else
|
||||
uint32_t __n = 0,__M = 0, N = 0;\
|
||||
int __i;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ parity(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
else
|
||||
|
||||
#define LF_POLY_ODD (0x29CE5C)
|
||||
#define LF_POLY_EVEN (0x870804)
|
||||
@@ -63,31 +63,31 @@ int nonce_distance(uint32_t from, uint32_t to);
|
||||
static inline int parity(uint32_t x)
|
||||
{
|
||||
#if !defined __i386__ || !defined __GNUC__
|
||||
x ^= x >> 16;
|
||||
x ^= x >> 8;
|
||||
x ^= x >> 4;
|
||||
return BIT(0x6996, x & 0xf);
|
||||
x ^= x >> 16;
|
||||
x ^= x >> 8;
|
||||
x ^= x >> 4;
|
||||
return BIT(0x6996, x & 0xf);
|
||||
#else
|
||||
__asm__( "movl %1, %%eax\n"
|
||||
"mov %%ax, %%cx\n"
|
||||
"shrl $0x10, %%eax\n"
|
||||
"xor %%ax, %%cx\n"
|
||||
"xor %%ch, %%cl\n"
|
||||
"setpo %%al\n"
|
||||
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax","ecx");
|
||||
return x;
|
||||
__asm__( "movl %1, %%eax\n"
|
||||
"mov %%ax, %%cx\n"
|
||||
"shrl $0x10, %%eax\n"
|
||||
"xor %%ax, %%cx\n"
|
||||
"xor %%ch, %%cl\n"
|
||||
"setpo %%al\n"
|
||||
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax","ecx");
|
||||
return x;
|
||||
#endif
|
||||
}
|
||||
static inline int filter(uint32_t const x)
|
||||
{
|
||||
uint32_t f;
|
||||
uint32_t f;
|
||||
|
||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||
return BIT(0xEC57E80A, f);
|
||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||
return BIT(0xEC57E80A, f);
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+85
-85
@@ -22,115 +22,115 @@
|
||||
|
||||
struct Crypto1State * crypto1_create(uint64_t key)
|
||||
{
|
||||
struct Crypto1State *s = malloc(sizeof(*s));
|
||||
if ( !s ) return NULL;
|
||||
struct Crypto1State *s = malloc(sizeof(*s));
|
||||
if ( !s ) return NULL;
|
||||
|
||||
s->odd = s->even = 0;
|
||||
s->odd = s->even = 0;
|
||||
|
||||
int i;
|
||||
//for(i = 47;s && i > 0; i -= 2) {
|
||||
for(i = 47; i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return s;
|
||||
int i;
|
||||
//for(i = 47;s && i > 0; i -= 2) {
|
||||
for(i = 47; i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
void crypto1_destroy(struct Crypto1State *state)
|
||||
{
|
||||
free(state);
|
||||
free(state);
|
||||
}
|
||||
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr)
|
||||
{
|
||||
int i;
|
||||
for(*lfsr = 0, i = 23; i >= 0; --i) {
|
||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||
}
|
||||
int i;
|
||||
for(*lfsr = 0, i = 23; i >= 0; --i) {
|
||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||
}
|
||||
}
|
||||
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
||||
{
|
||||
uint32_t feedin;
|
||||
uint32_t tmp;
|
||||
uint8_t ret = filter(s->odd);
|
||||
uint32_t feedin;
|
||||
uint32_t tmp;
|
||||
uint8_t ret = filter(s->odd);
|
||||
|
||||
feedin = ret & !!is_encrypted;
|
||||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | parity(feedin);
|
||||
feedin = ret & !!is_encrypted;
|
||||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | parity(feedin);
|
||||
|
||||
tmp = s->odd;
|
||||
s->odd = s->even;
|
||||
s->even = tmp;
|
||||
tmp = s->odd;
|
||||
s->odd = s->even;
|
||||
s->even = tmp;
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
||||
{
|
||||
/*
|
||||
uint8_t i, ret = 0;
|
||||
/*
|
||||
uint8_t i, ret = 0;
|
||||
|
||||
for (i = 0; i < 8; ++i)
|
||||
ret |= crypto1_bit(s, BIT(in, i), is_encrypted) << i;
|
||||
*/
|
||||
for (i = 0; i < 8; ++i)
|
||||
ret |= crypto1_bit(s, BIT(in, i), is_encrypted) << i;
|
||||
*/
|
||||
// unfold loop 20161012
|
||||
uint8_t ret = 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
|
||||
ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
|
||||
ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
|
||||
ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
|
||||
ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
|
||||
ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
|
||||
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
|
||||
return ret;
|
||||
uint8_t ret = 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
|
||||
ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
|
||||
ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
|
||||
ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
|
||||
ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
|
||||
ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
|
||||
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
|
||||
return ret;
|
||||
}
|
||||
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
||||
{
|
||||
/*
|
||||
uint32_t i, ret = 0;
|
||||
/*
|
||||
uint32_t i, ret = 0;
|
||||
|
||||
for (i = 0; i < 32; ++i)
|
||||
ret |= crypto1_bit(s, BEBIT(in, i), is_encrypted) << (i ^ 24);
|
||||
for (i = 0; i < 32; ++i)
|
||||
ret |= crypto1_bit(s, BEBIT(in, i), is_encrypted) << (i ^ 24);
|
||||
*/
|
||||
//unfold loop 2016012
|
||||
uint32_t ret = 0;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (2 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (3 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (4 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (5 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (6 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (7 ^ 24);
|
||||
uint32_t ret = 0;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (2 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (3 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (4 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (5 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (6 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (7 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (8 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (9 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (10 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (11 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (12 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (13 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (14 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (15 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (8 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (9 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (10 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (11 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (12 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (13 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (14 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (15 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (16 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (17 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (18 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (19 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (20 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (21 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (22 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (23 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (16 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (17 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (18 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (19 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (20 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (21 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (22 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (23 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (25 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (26 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (27 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (28 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (29 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (30 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (31 ^ 24);
|
||||
return ret;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (25 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (26 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (27 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (28 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (29 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (30 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (31 ^ 24);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* prng_successor
|
||||
@@ -138,9 +138,9 @@ uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
||||
*/
|
||||
uint32_t prng_successor(uint32_t x, uint32_t n)
|
||||
{
|
||||
SWAPENDIAN(x);
|
||||
while(n--)
|
||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
SWAPENDIAN(x);
|
||||
while(n--)
|
||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
|
||||
return SWAPENDIAN(x);
|
||||
return SWAPENDIAN(x);
|
||||
}
|
||||
|
||||
+51
-51
@@ -5,63 +5,63 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
struct Crypto1State *s,*t;
|
||||
uint64_t key; // recovered key
|
||||
uint32_t uid; // serial number
|
||||
uint32_t nt; // tag challenge
|
||||
uint32_t nr0_enc; // first encrypted reader challenge
|
||||
uint32_t ar0_enc; // first encrypted reader response
|
||||
uint32_t nr1_enc; // second encrypted reader challenge
|
||||
uint32_t ar1_enc; // second encrypted reader response
|
||||
uint32_t ks2; // keystream used to encrypt reader response
|
||||
struct Crypto1State *s,*t;
|
||||
uint64_t key; // recovered key
|
||||
uint32_t uid; // serial number
|
||||
uint32_t nt; // tag challenge
|
||||
uint32_t nr0_enc; // first encrypted reader challenge
|
||||
uint32_t ar0_enc; // first encrypted reader response
|
||||
uint32_t nr1_enc; // second encrypted reader challenge
|
||||
uint32_t ar1_enc; // second encrypted reader response
|
||||
uint32_t ks2; // keystream used to encrypt reader response
|
||||
|
||||
printf("MIFARE Classic key recovery - based 32 bits of keystream\n");
|
||||
printf("Recover key from two 32-bit reader authentication answers only!\n\n");
|
||||
printf("MIFARE Classic key recovery - based 32 bits of keystream\n");
|
||||
printf("Recover key from two 32-bit reader authentication answers only!\n\n");
|
||||
|
||||
if (argc < 7) {
|
||||
printf(" syntax: %s <uid> <nt> <nr_0> <ar_0> <nr_1> <ar_1>\n\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc < 7) {
|
||||
printf(" syntax: %s <uid> <nt> <nr_0> <ar_0> <nr_1> <ar_1>\n\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sscanf(argv[1],"%x",&uid);
|
||||
sscanf(argv[2],"%x",&nt);
|
||||
sscanf(argv[3],"%x",&nr0_enc);
|
||||
sscanf(argv[4],"%x",&ar0_enc);
|
||||
sscanf(argv[5],"%x",&nr1_enc);
|
||||
sscanf(argv[6],"%x",&ar1_enc);
|
||||
sscanf(argv[1],"%x",&uid);
|
||||
sscanf(argv[2],"%x",&nt);
|
||||
sscanf(argv[3],"%x",&nr0_enc);
|
||||
sscanf(argv[4],"%x",&ar0_enc);
|
||||
sscanf(argv[5],"%x",&nr1_enc);
|
||||
sscanf(argv[6],"%x",&ar1_enc);
|
||||
|
||||
printf("Recovering key for:\n");
|
||||
printf(" uid: %08x\n",uid);
|
||||
printf(" nt: %08x\n",nt);
|
||||
printf(" {nr_0}: %08x\n",nr0_enc);
|
||||
printf(" {ar_0}: %08x\n",ar0_enc);
|
||||
printf(" {nr_1}: %08x\n",nr1_enc);
|
||||
printf(" {ar_1}: %08x\n",ar1_enc);
|
||||
printf("Recovering key for:\n");
|
||||
printf(" uid: %08x\n",uid);
|
||||
printf(" nt: %08x\n",nt);
|
||||
printf(" {nr_0}: %08x\n",nr0_enc);
|
||||
printf(" {ar_0}: %08x\n",ar0_enc);
|
||||
printf(" {nr_1}: %08x\n",nr1_enc);
|
||||
printf(" {ar_1}: %08x\n",ar1_enc);
|
||||
|
||||
// Generate lfsr succesors of the tag challenge
|
||||
printf("\nLFSR succesors of the tag challenge:\n");
|
||||
uint32_t p64 = prng_successor(nt, 64);
|
||||
printf(" nt': %08x\n", p64);
|
||||
printf(" nt'': %08x\n", prng_successor(p64, 32));
|
||||
// Generate lfsr succesors of the tag challenge
|
||||
printf("\nLFSR succesors of the tag challenge:\n");
|
||||
uint32_t p64 = prng_successor(nt, 64);
|
||||
printf(" nt': %08x\n", p64);
|
||||
printf(" nt'': %08x\n", prng_successor(p64, 32));
|
||||
|
||||
// Extract the keystream from the messages
|
||||
printf("\nKeystream used to generate {ar} and {at}:\n");
|
||||
ks2 = ar0_enc ^ p64;
|
||||
printf(" ks2: %08x\n", ks2);
|
||||
// Extract the keystream from the messages
|
||||
printf("\nKeystream used to generate {ar} and {at}:\n");
|
||||
ks2 = ar0_enc ^ p64;
|
||||
printf(" ks2: %08x\n", ks2);
|
||||
|
||||
s = lfsr_recovery32(ar0_enc ^ p64, 0);
|
||||
s = lfsr_recovery32(ar0_enc ^ p64, 0);
|
||||
|
||||
for(t = s; t->odd | t->even; ++t) {
|
||||
lfsr_rollback_word(t, 0, 0);
|
||||
lfsr_rollback_word(t, nr0_enc, 1);
|
||||
lfsr_rollback_word(t, uid ^ nt, 0);
|
||||
crypto1_get_lfsr(t, &key);
|
||||
crypto1_word(t, uid ^ nt, 0);
|
||||
crypto1_word(t, nr1_enc, 1);
|
||||
if (ar1_enc == (crypto1_word(t, 0, 0) ^ p64)) {
|
||||
printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
|
||||
break;}
|
||||
}
|
||||
free(s);
|
||||
return 0;
|
||||
for(t = s; t->odd | t->even; ++t) {
|
||||
lfsr_rollback_word(t, 0, 0);
|
||||
lfsr_rollback_word(t, nr0_enc, 1);
|
||||
lfsr_rollback_word(t, uid ^ nt, 0);
|
||||
crypto1_get_lfsr(t, &key);
|
||||
crypto1_word(t, uid ^ nt, 0);
|
||||
crypto1_word(t, nr1_enc, 1);
|
||||
if (ar1_enc == (crypto1_word(t, 0, 0) ^ p64)) {
|
||||
printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
|
||||
break;}
|
||||
}
|
||||
free(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+56
-56
@@ -5,70 +5,70 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
struct Crypto1State *s,*t;
|
||||
uint64_t key; // recovered key
|
||||
uint32_t uid; // serial number
|
||||
uint32_t nt0; // tag challenge first
|
||||
uint32_t nt1; // tag challenge second
|
||||
uint32_t nr0_enc; // first encrypted reader challenge
|
||||
uint32_t ar0_enc; // first encrypted reader response
|
||||
uint32_t nr1_enc; // second encrypted reader challenge
|
||||
uint32_t ar1_enc; // second encrypted reader response
|
||||
uint32_t ks2; // keystream used to encrypt reader response
|
||||
struct Crypto1State *s,*t;
|
||||
uint64_t key; // recovered key
|
||||
uint32_t uid; // serial number
|
||||
uint32_t nt0; // tag challenge first
|
||||
uint32_t nt1; // tag challenge second
|
||||
uint32_t nr0_enc; // first encrypted reader challenge
|
||||
uint32_t ar0_enc; // first encrypted reader response
|
||||
uint32_t nr1_enc; // second encrypted reader challenge
|
||||
uint32_t ar1_enc; // second encrypted reader response
|
||||
uint32_t ks2; // keystream used to encrypt reader response
|
||||
|
||||
printf("MIFARE Classic key recovery - based 32 bits of keystream VERSION2\n");
|
||||
printf("Recover key from two 32-bit reader authentication answers only\n");
|
||||
printf("This version implements Moebius two different nonce solution (like the supercard)\n\n");
|
||||
printf("MIFARE Classic key recovery - based 32 bits of keystream VERSION2\n");
|
||||
printf("Recover key from two 32-bit reader authentication answers only\n");
|
||||
printf("This version implements Moebius two different nonce solution (like the supercard)\n\n");
|
||||
|
||||
if (argc < 8) {
|
||||
printf("syntax: %s <uid> <nt> <nr_0> <ar_0> <nt1> <nr_1> <ar_1>\n\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc < 8) {
|
||||
printf("syntax: %s <uid> <nt> <nr_0> <ar_0> <nt1> <nr_1> <ar_1>\n\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
sscanf(argv[1],"%x",&uid);
|
||||
sscanf(argv[2],"%x",&nt0);
|
||||
sscanf(argv[3],"%x",&nr0_enc);
|
||||
sscanf(argv[4],"%x",&ar0_enc);
|
||||
sscanf(argv[5],"%x",&nt1);
|
||||
sscanf(argv[6],"%x",&nr1_enc);
|
||||
sscanf(argv[7],"%x",&ar1_enc);
|
||||
sscanf(argv[1],"%x",&uid);
|
||||
sscanf(argv[2],"%x",&nt0);
|
||||
sscanf(argv[3],"%x",&nr0_enc);
|
||||
sscanf(argv[4],"%x",&ar0_enc);
|
||||
sscanf(argv[5],"%x",&nt1);
|
||||
sscanf(argv[6],"%x",&nr1_enc);
|
||||
sscanf(argv[7],"%x",&ar1_enc);
|
||||
|
||||
printf("Recovering key for:\n");
|
||||
printf(" uid: %08x\n",uid);
|
||||
printf(" nt_0: %08x\n",nt0);
|
||||
printf(" {nr_0}: %08x\n",nr0_enc);
|
||||
printf(" {ar_0}: %08x\n",ar0_enc);
|
||||
printf(" nt_1: %08x\n",nt1);
|
||||
printf(" {nr_1}: %08x\n",nr1_enc);
|
||||
printf(" {ar_1}: %08x\n",ar1_enc);
|
||||
printf("Recovering key for:\n");
|
||||
printf(" uid: %08x\n",uid);
|
||||
printf(" nt_0: %08x\n",nt0);
|
||||
printf(" {nr_0}: %08x\n",nr0_enc);
|
||||
printf(" {ar_0}: %08x\n",ar0_enc);
|
||||
printf(" nt_1: %08x\n",nt1);
|
||||
printf(" {nr_1}: %08x\n",nr1_enc);
|
||||
printf(" {ar_1}: %08x\n",ar1_enc);
|
||||
|
||||
// Generate lfsr succesors of the tag challenge
|
||||
printf("\nLFSR succesors of the tag challenge:\n");
|
||||
uint32_t p64 = prng_successor(nt0, 64);
|
||||
uint32_t p64b = prng_successor(nt1, 64);
|
||||
// Generate lfsr succesors of the tag challenge
|
||||
printf("\nLFSR succesors of the tag challenge:\n");
|
||||
uint32_t p64 = prng_successor(nt0, 64);
|
||||
uint32_t p64b = prng_successor(nt1, 64);
|
||||
|
||||
printf(" nt': %08x\n", p64);
|
||||
printf(" nt'': %08x\n", prng_successor(p64, 32));
|
||||
printf(" nt': %08x\n", p64);
|
||||
printf(" nt'': %08x\n", prng_successor(p64, 32));
|
||||
|
||||
// Extract the keystream from the messages
|
||||
printf("\nKeystream used to generate {ar} and {at}:\n");
|
||||
ks2 = ar0_enc ^ p64;
|
||||
printf(" ks2: %08x\n",ks2);
|
||||
// Extract the keystream from the messages
|
||||
printf("\nKeystream used to generate {ar} and {at}:\n");
|
||||
ks2 = ar0_enc ^ p64;
|
||||
printf(" ks2: %08x\n",ks2);
|
||||
|
||||
s = lfsr_recovery32(ar0_enc ^ p64, 0);
|
||||
s = lfsr_recovery32(ar0_enc ^ p64, 0);
|
||||
|
||||
for(t = s; t->odd | t->even; ++t) {
|
||||
lfsr_rollback_word(t, 0, 0);
|
||||
lfsr_rollback_word(t, nr0_enc, 1);
|
||||
lfsr_rollback_word(t, uid ^ nt0, 0);
|
||||
crypto1_get_lfsr(t, &key);
|
||||
for(t = s; t->odd | t->even; ++t) {
|
||||
lfsr_rollback_word(t, 0, 0);
|
||||
lfsr_rollback_word(t, nr0_enc, 1);
|
||||
lfsr_rollback_word(t, uid ^ nt0, 0);
|
||||
crypto1_get_lfsr(t, &key);
|
||||
|
||||
crypto1_word(t, uid ^ nt1, 0);
|
||||
crypto1_word(t, nr1_enc, 1);
|
||||
if (ar1_enc == (crypto1_word(t, 0, 0) ^ p64b)) {
|
||||
printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
|
||||
break;}
|
||||
}
|
||||
free(s);
|
||||
return 0;
|
||||
crypto1_word(t, uid ^ nt1, 0);
|
||||
crypto1_word(t, nr1_enc, 1);
|
||||
if (ar1_enc == (crypto1_word(t, 0, 0) ^ p64b)) {
|
||||
printf("\nFound Key: [%012" PRIx64 "]\n\n",key);
|
||||
break;}
|
||||
}
|
||||
free(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+78
-78
@@ -6,94 +6,94 @@
|
||||
#include "crapto1.h"
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
struct Crypto1State *revstate;
|
||||
uint64_t key; // recovered key
|
||||
uint32_t uid; // serial number
|
||||
uint32_t nt; // tag challenge
|
||||
uint32_t nr_enc; // encrypted reader challenge
|
||||
uint32_t ar_enc; // encrypted reader response
|
||||
uint32_t at_enc; // encrypted tag response
|
||||
uint32_t ks2; // keystream used to encrypt reader response
|
||||
uint32_t ks3; // keystream used to encrypt tag response
|
||||
struct Crypto1State *revstate;
|
||||
uint64_t key; // recovered key
|
||||
uint32_t uid; // serial number
|
||||
uint32_t nt; // tag challenge
|
||||
uint32_t nr_enc; // encrypted reader challenge
|
||||
uint32_t ar_enc; // encrypted reader response
|
||||
uint32_t at_enc; // encrypted tag response
|
||||
uint32_t ks2; // keystream used to encrypt reader response
|
||||
uint32_t ks3; // keystream used to encrypt tag response
|
||||
|
||||
printf("MIFARE Classic key recovery - based 64 bits of keystream\n");
|
||||
printf("Recover key from only one complete authentication!\n\n");
|
||||
printf("MIFARE Classic key recovery - based 64 bits of keystream\n");
|
||||
printf("Recover key from only one complete authentication!\n\n");
|
||||
|
||||
if (argc < 6) {
|
||||
printf(" syntax: %s <uid> <nt> <{nr}> <{ar}> <{at}> [enc...]\n\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc < 6) {
|
||||
printf(" syntax: %s <uid> <nt> <{nr}> <{ar}> <{at}> [enc...]\n\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int encc = argc - 6;
|
||||
int enclen[encc];
|
||||
uint8_t enc[encc][120];
|
||||
int encc = argc - 6;
|
||||
int enclen[encc];
|
||||
uint8_t enc[encc][120];
|
||||
|
||||
sscanf(argv[1],"%x",&uid);
|
||||
sscanf(argv[2],"%x",&nt);
|
||||
sscanf(argv[3],"%x",&nr_enc);
|
||||
sscanf(argv[4],"%x",&ar_enc);
|
||||
sscanf(argv[5],"%x",&at_enc);
|
||||
for (int i = 0; i < encc; i++) {
|
||||
enclen[i] = strlen(argv[i + 6]) / 2;
|
||||
for (int i2 = 0; i2 < enclen[i]; i2++) {
|
||||
sscanf(argv[i+6] + i2*2, "%2x", (unsigned int *)&enc[i][i2]);
|
||||
}
|
||||
}
|
||||
sscanf(argv[1],"%x",&uid);
|
||||
sscanf(argv[2],"%x",&nt);
|
||||
sscanf(argv[3],"%x",&nr_enc);
|
||||
sscanf(argv[4],"%x",&ar_enc);
|
||||
sscanf(argv[5],"%x",&at_enc);
|
||||
for (int i = 0; i < encc; i++) {
|
||||
enclen[i] = strlen(argv[i + 6]) / 2;
|
||||
for (int i2 = 0; i2 < enclen[i]; i2++) {
|
||||
sscanf(argv[i+6] + i2*2, "%2x", (unsigned int *)&enc[i][i2]);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Recovering key for:\n");
|
||||
printf("Recovering key for:\n");
|
||||
|
||||
printf(" uid: %08x\n",uid);
|
||||
printf(" nt: %08x\n",nt);
|
||||
printf(" {nr}: %08x\n",nr_enc);
|
||||
printf(" {ar}: %08x\n",ar_enc);
|
||||
printf(" {at}: %08x\n",at_enc);
|
||||
printf(" uid: %08x\n",uid);
|
||||
printf(" nt: %08x\n",nt);
|
||||
printf(" {nr}: %08x\n",nr_enc);
|
||||
printf(" {ar}: %08x\n",ar_enc);
|
||||
printf(" {at}: %08x\n",at_enc);
|
||||
|
||||
for (int i = 0; i < encc; i++) {
|
||||
printf("{enc%d}: ", i);
|
||||
for (int i2 = 0; i2 < enclen[i]; i2++) {
|
||||
printf("%02x", enc[i][i2]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for (int i = 0; i < encc; i++) {
|
||||
printf("{enc%d}: ", i);
|
||||
for (int i2 = 0; i2 < enclen[i]; i2++) {
|
||||
printf("%02x", enc[i][i2]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Generate lfsr succesors of the tag challenge
|
||||
printf("\nLFSR succesors of the tag challenge:\n");
|
||||
printf(" nt': %08x\n",prng_successor(nt, 64));
|
||||
printf(" nt'': %08x\n",prng_successor(nt, 96));
|
||||
// Generate lfsr succesors of the tag challenge
|
||||
printf("\nLFSR succesors of the tag challenge:\n");
|
||||
printf(" nt': %08x\n",prng_successor(nt, 64));
|
||||
printf(" nt'': %08x\n",prng_successor(nt, 96));
|
||||
|
||||
// Extract the keystream from the messages
|
||||
printf("\nKeystream used to generate {ar} and {at}:\n");
|
||||
ks2 = ar_enc ^ prng_successor(nt, 64);
|
||||
ks3 = at_enc ^ prng_successor(nt, 96);
|
||||
printf(" ks2: %08x\n",ks2);
|
||||
printf(" ks3: %08x\n",ks3);
|
||||
// Extract the keystream from the messages
|
||||
printf("\nKeystream used to generate {ar} and {at}:\n");
|
||||
ks2 = ar_enc ^ prng_successor(nt, 64);
|
||||
ks3 = at_enc ^ prng_successor(nt, 96);
|
||||
printf(" ks2: %08x\n",ks2);
|
||||
printf(" ks3: %08x\n",ks3);
|
||||
|
||||
revstate = lfsr_recovery64(ks2, ks3);
|
||||
revstate = lfsr_recovery64(ks2, ks3);
|
||||
|
||||
// Decrypting communication using keystream if presented
|
||||
if (argc > 6 ) {
|
||||
printf("\nDecrypted communication:\n");
|
||||
uint8_t ks4;
|
||||
int rollb = 0;
|
||||
for (int i = 0; i < encc; i++) {
|
||||
printf("{dec%d}: ", i);
|
||||
for (int i2 = 0; i2 < enclen[i]; i2++) {
|
||||
ks4 = crypto1_byte(revstate, 0, 0);
|
||||
printf("%02x", ks4 ^ enc[i][i2]);
|
||||
rollb += 1;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for (int i = 0; i < rollb; i++)
|
||||
lfsr_rollback_byte(revstate, 0, 0);
|
||||
}
|
||||
// Decrypting communication using keystream if presented
|
||||
if (argc > 6 ) {
|
||||
printf("\nDecrypted communication:\n");
|
||||
uint8_t ks4;
|
||||
int rollb = 0;
|
||||
for (int i = 0; i < encc; i++) {
|
||||
printf("{dec%d}: ", i);
|
||||
for (int i2 = 0; i2 < enclen[i]; i2++) {
|
||||
ks4 = crypto1_byte(revstate, 0, 0);
|
||||
printf("%02x", ks4 ^ enc[i][i2]);
|
||||
rollb += 1;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for (int i = 0; i < rollb; i++)
|
||||
lfsr_rollback_byte(revstate, 0, 0);
|
||||
}
|
||||
|
||||
lfsr_rollback_word(revstate, 0, 0);
|
||||
lfsr_rollback_word(revstate, 0, 0);
|
||||
lfsr_rollback_word(revstate, nr_enc, 1);
|
||||
lfsr_rollback_word(revstate, uid ^ nt, 0);
|
||||
crypto1_get_lfsr(revstate, &key);
|
||||
printf("\nFound Key: [%012" PRIx64 "]\n\n", key);
|
||||
crypto1_destroy(revstate);
|
||||
return 0;
|
||||
lfsr_rollback_word(revstate, 0, 0);
|
||||
lfsr_rollback_word(revstate, 0, 0);
|
||||
lfsr_rollback_word(revstate, nr_enc, 1);
|
||||
lfsr_rollback_word(revstate, uid ^ nt, 0);
|
||||
crypto1_get_lfsr(revstate, &key);
|
||||
printf("\nFound Key: [%012" PRIx64 "]\n\n", key);
|
||||
crypto1_destroy(revstate);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+356
-356
File diff suppressed because it is too large
Load Diff
+25
-25
@@ -43,18 +43,18 @@ 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)
|
||||
(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;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ parity(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
else
|
||||
uint32_t __n = 0,__M = 0, N = 0;\
|
||||
int __i;\
|
||||
for(; __n < 1 << 16; N = prng_successor(__M = ++__n, 16))\
|
||||
for(__i = FSIZE - 1; __i >= 0; __i--)\
|
||||
if(BIT(FILTER, __i) ^ parity(__M & 0xFF01))\
|
||||
break;\
|
||||
else if(__i)\
|
||||
__M = prng_successor(__M, (__i == 7) ? 48 : 8);\
|
||||
else
|
||||
|
||||
#define LF_POLY_ODD (0x29CE5C)
|
||||
#define LF_POLY_EVEN (0x870804)
|
||||
@@ -63,31 +63,31 @@ int nonce_distance(uint32_t from, uint32_t to);
|
||||
static inline int parity(uint32_t x)
|
||||
{
|
||||
#if !defined __i386__ || !defined __GNUC__
|
||||
x ^= x >> 16;
|
||||
x ^= x >> 8;
|
||||
x ^= x >> 4;
|
||||
return BIT(0x6996, x & 0xf);
|
||||
x ^= x >> 16;
|
||||
x ^= x >> 8;
|
||||
x ^= x >> 4;
|
||||
return BIT(0x6996, x & 0xf);
|
||||
#else
|
||||
__asm__( "movl %1, %%eax\n"
|
||||
"mov %%ax, %%cx\n"
|
||||
"shrl $0x10, %%eax\n"
|
||||
"xor %%ax, %%cx\n"
|
||||
"mov %%ax, %%cx\n"
|
||||
"shrl $0x10, %%eax\n"
|
||||
"xor %%ax, %%cx\n"
|
||||
"xor %%ch, %%cl\n"
|
||||
"setpo %%al\n"
|
||||
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax","ecx");
|
||||
return x;
|
||||
return x;
|
||||
#endif
|
||||
}
|
||||
static inline int filter(uint32_t const x)
|
||||
{
|
||||
uint32_t f;
|
||||
uint32_t f;
|
||||
|
||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||
return BIT(0xEC57E80A, f);
|
||||
f = 0xf22c0 >> (x & 0xf) & 16;
|
||||
f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
|
||||
f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
|
||||
f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
|
||||
f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
|
||||
return BIT(0xEC57E80A, f);
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+85
-85
@@ -22,115 +22,115 @@
|
||||
|
||||
struct Crypto1State * crypto1_create(uint64_t key)
|
||||
{
|
||||
struct Crypto1State *s = malloc(sizeof(*s));
|
||||
if ( !s ) return NULL;
|
||||
struct Crypto1State *s = malloc(sizeof(*s));
|
||||
if ( !s ) return NULL;
|
||||
|
||||
s->odd = s->even = 0;
|
||||
s->odd = s->even = 0;
|
||||
|
||||
int i;
|
||||
//for(i = 47;s && i > 0; i -= 2) {
|
||||
for(i = 47; i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return s;
|
||||
int i;
|
||||
//for(i = 47;s && i > 0; i -= 2) {
|
||||
for(i = 47; i > 0; i -= 2) {
|
||||
s->odd = s->odd << 1 | BIT(key, (i - 1) ^ 7);
|
||||
s->even = s->even << 1 | BIT(key, i ^ 7);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
void crypto1_destroy(struct Crypto1State *state)
|
||||
{
|
||||
free(state);
|
||||
free(state);
|
||||
}
|
||||
void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr)
|
||||
{
|
||||
int i;
|
||||
for(*lfsr = 0, i = 23; i >= 0; --i) {
|
||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||
}
|
||||
int i;
|
||||
for(*lfsr = 0, i = 23; i >= 0; --i) {
|
||||
*lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
|
||||
*lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
|
||||
}
|
||||
}
|
||||
uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
||||
{
|
||||
uint32_t feedin;
|
||||
uint32_t tmp;
|
||||
uint8_t ret = filter(s->odd);
|
||||
uint32_t feedin;
|
||||
uint32_t tmp;
|
||||
uint8_t ret = filter(s->odd);
|
||||
|
||||
feedin = ret & !!is_encrypted;
|
||||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | parity(feedin);
|
||||
feedin = ret & !!is_encrypted;
|
||||
feedin ^= !!in;
|
||||
feedin ^= LF_POLY_ODD & s->odd;
|
||||
feedin ^= LF_POLY_EVEN & s->even;
|
||||
s->even = s->even << 1 | parity(feedin);
|
||||
|
||||
tmp = s->odd;
|
||||
s->odd = s->even;
|
||||
s->even = tmp;
|
||||
tmp = s->odd;
|
||||
s->odd = s->even;
|
||||
s->even = tmp;
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted)
|
||||
{
|
||||
/*
|
||||
uint8_t i, ret = 0;
|
||||
/*
|
||||
uint8_t i, ret = 0;
|
||||
|
||||
for (i = 0; i < 8; ++i)
|
||||
ret |= crypto1_bit(s, BIT(in, i), is_encrypted) << i;
|
||||
*/
|
||||
for (i = 0; i < 8; ++i)
|
||||
ret |= crypto1_bit(s, BIT(in, i), is_encrypted) << i;
|
||||
*/
|
||||
// unfold loop 20161012
|
||||
uint8_t ret = 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
|
||||
ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
|
||||
ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
|
||||
ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
|
||||
ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
|
||||
ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
|
||||
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
|
||||
return ret;
|
||||
uint8_t ret = 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
|
||||
ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
|
||||
ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
|
||||
ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
|
||||
ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
|
||||
ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
|
||||
ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
|
||||
ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
|
||||
return ret;
|
||||
}
|
||||
uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
||||
{
|
||||
/*
|
||||
uint32_t i, ret = 0;
|
||||
/*
|
||||
uint32_t i, ret = 0;
|
||||
|
||||
for (i = 0; i < 32; ++i)
|
||||
ret |= crypto1_bit(s, BEBIT(in, i), is_encrypted) << (i ^ 24);
|
||||
for (i = 0; i < 32; ++i)
|
||||
ret |= crypto1_bit(s, BEBIT(in, i), is_encrypted) << (i ^ 24);
|
||||
*/
|
||||
//unfold loop 2016012
|
||||
uint32_t ret = 0;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (2 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (3 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (4 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (5 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (6 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (7 ^ 24);
|
||||
uint32_t ret = 0;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (0 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (1 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (2 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (3 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (4 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (5 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (6 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (7 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (8 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (9 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (10 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (11 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (12 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (13 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (14 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (15 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (8 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (9 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (10 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (11 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (12 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (13 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (14 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (15 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (16 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (17 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (18 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (19 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (20 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (21 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (22 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (23 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (16 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (17 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (18 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (19 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (20 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (21 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (22 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (23 ^ 24);
|
||||
|
||||
ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (25 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (26 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (27 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (28 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (29 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (30 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (31 ^ 24);
|
||||
return ret;
|
||||
ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (25 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (26 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (27 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (28 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (29 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (30 ^ 24);
|
||||
ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (31 ^ 24);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* prng_successor
|
||||
@@ -138,9 +138,9 @@ uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted)
|
||||
*/
|
||||
uint32_t prng_successor(uint32_t x, uint32_t n)
|
||||
{
|
||||
SWAPENDIAN(x);
|
||||
while(n--)
|
||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
SWAPENDIAN(x);
|
||||
while(n--)
|
||||
x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
|
||||
|
||||
return SWAPENDIAN(x);
|
||||
return SWAPENDIAN(x);
|
||||
}
|
||||
|
||||
+41
-41
@@ -4,54 +4,54 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(const int argc, const char* argv[]) {
|
||||
struct Crypto1State *state;
|
||||
uint32_t pos, uid, nt, nr, rr, nr_diff;
|
||||
uint8_t bt, i, ks3x[8], par[8][8];
|
||||
uint64_t key_recovered;
|
||||
uint64_t par_info;
|
||||
uint64_t ks_info;
|
||||
nr = rr = 0;
|
||||
struct Crypto1State *state;
|
||||
uint32_t pos, uid, nt, nr, rr, nr_diff;
|
||||
uint8_t bt, i, ks3x[8], par[8][8];
|
||||
uint64_t key_recovered;
|
||||
uint64_t par_info;
|
||||
uint64_t ks_info;
|
||||
nr = rr = 0;
|
||||
|
||||
if (argc < 5) {
|
||||
printf("\nsyntax: %s <uid> <nt> <par> <ks>\n\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
sscanf(argv[1],"%08x", &uid);
|
||||
sscanf(argv[2],"%08x", &nt);
|
||||
sscanf(argv[3],"%016" SCNx64 ,&par_info);
|
||||
sscanf(argv[4],"%016" SCNx64 ,&ks_info);
|
||||
if (argc < 5) {
|
||||
printf("\nsyntax: %s <uid> <nt> <par> <ks>\n\n",argv[0]);
|
||||
return 1;
|
||||
}
|
||||
sscanf(argv[1],"%08x", &uid);
|
||||
sscanf(argv[2],"%08x", &nt);
|
||||
sscanf(argv[3],"%016" SCNx64 ,&par_info);
|
||||
sscanf(argv[4],"%016" SCNx64 ,&ks_info);
|
||||
|
||||
// Reset the last three significant bits of the reader nonce
|
||||
nr &= 0xffffff1f;
|
||||
// Reset the last three significant bits of the reader nonce
|
||||
nr &= 0xffffff1f;
|
||||
|
||||
printf("\nuid(%08x) nt(%08x) par(%016" PRIx64 ") ks(%016" PRIx64 ")\n\n", uid, nt, par_info, ks_info);
|
||||
printf("\nuid(%08x) nt(%08x) par(%016" PRIx64 ") ks(%016" PRIx64 ")\n\n", uid, nt, par_info, ks_info);
|
||||
|
||||
for ( pos = 0; pos < 8; pos++ ) {
|
||||
ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;
|
||||
bt = (par_info >> (pos*8)) & 0xff;
|
||||
for ( pos = 0; pos < 8; pos++ ) {
|
||||
ks3x[7-pos] = (ks_info >> (pos*8)) & 0x0f;
|
||||
bt = (par_info >> (pos*8)) & 0xff;
|
||||
|
||||
for ( i = 0; i < 8; i++) {
|
||||
par[7-pos][i] = (bt >> i) & 0x01;
|
||||
}
|
||||
}
|
||||
for ( i = 0; i < 8; i++) {
|
||||
par[7-pos][i] = (bt >> i) & 0x01;
|
||||
}
|
||||
}
|
||||
|
||||
printf("|diff|{nr} |ks3|ks3^5|parity |\n");
|
||||
printf("+----+--------+---+-----+---------------+\n");
|
||||
printf("|diff|{nr} |ks3|ks3^5|parity |\n");
|
||||
printf("+----+--------+---+-----+---------------+\n");
|
||||
|
||||
for ( i = 0; i < 8; i++) {
|
||||
nr_diff = nr | i << 5;
|
||||
printf("| %02x |%08x| %01x | %01x |", i << 5, nr_diff, ks3x[i], ks3x[i]^5);
|
||||
for ( i = 0; i < 8; i++) {
|
||||
nr_diff = nr | i << 5;
|
||||
printf("| %02x |%08x| %01x | %01x |", i << 5, nr_diff, ks3x[i], ks3x[i]^5);
|
||||
|
||||
for ( pos = 0; pos < 7; pos++)
|
||||
printf("%01x,", par[i][pos]);
|
||||
printf("%01x|\n", par[i][7]);
|
||||
}
|
||||
printf("+----+--------+---+-----+---------------+\n");
|
||||
for ( pos = 0; pos < 7; pos++)
|
||||
printf("%01x,", par[i][pos]);
|
||||
printf("%01x|\n", par[i][7]);
|
||||
}
|
||||
printf("+----+--------+---+-----+---------------+\n");
|
||||
|
||||
state = lfsr_common_prefix(nr,rr,ks3x,par);
|
||||
lfsr_rollback_word(state,uid^nt,0);
|
||||
crypto1_get_lfsr(state,&key_recovered);
|
||||
printf("\nkey recovered: %012" PRIx64 "\n\n", key_recovered);
|
||||
crypto1_destroy(state);
|
||||
return 0;
|
||||
state = lfsr_common_prefix(nr,rr,ks3x,par);
|
||||
lfsr_rollback_word(state,uid^nt,0);
|
||||
crypto1_get_lfsr(state,&key_recovered);
|
||||
printf("\nkey recovered: %012" PRIx64 "\n\n", key_recovered);
|
||||
crypto1_destroy(state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user