CHG: Crapto1 v3.3 for the ARMSRC

This commit is contained in:
iceman1001 2016-01-19 16:19:59 +01:00
commit 417f4ae127
3 changed files with 32 additions and 26 deletions

View file

@ -33,8 +33,7 @@ static void __attribute__((constructor)) fill_lut()
static void quicksort(uint32_t* const start, uint32_t* const stop) static void quicksort(uint32_t* const start, uint32_t* const stop)
{ {
uint32_t *it = start + 1, *rit = stop; uint32_t *it = start + 1, *rit = stop, t;
uint32_t tmp;
if(it > rit) if(it > rit)
return; return;
@ -44,19 +43,13 @@ static void quicksort(uint32_t* const start, uint32_t* const stop)
++it; ++it;
else if(*rit > *start) else if(*rit > *start)
--rit; --rit;
else { else
tmp = *it; t = *it, *it = *rit, *rit = t;
*it = *rit;
*rit = tmp;
}
if(*rit >= *start) if(*rit >= *start)
--rit; --rit;
if(rit != start) { if(rit != start)
tmp = *rit; t = *rit, *rit = *start, *start = t;
*rit = *start;
*start = tmp;
}
quicksort(start, rit - 1); quicksort(start, rit - 1);
quicksort(rit + 1, stop); quicksort(rit + 1, stop);
@ -325,12 +318,10 @@ uint8_t lfsr_rollback_bit(struct Crypto1State *s, uint32_t in, int fb)
{ {
int out; int out;
uint8_t ret; uint8_t ret;
uint32_t tmp; uint32_t t;
s->odd &= 0xffffff; s->odd &= 0xffffff;
tmp = s->odd; t = s->odd, s->odd = s->even, s->even = t;
s->odd = s->even;
s->even = tmp;
out = s->even & 1; out = s->even & 1;
out ^= LF_POLY_EVEN & (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) 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) for (i = 7; i >= 0; --i)
ret |= lfsr_rollback_bit(s, BIT(in, i), fb) << 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) uint32_t lfsr_rollback_word(struct Crypto1State *s, uint32_t in, int fb)
{ {
/* int i; /*
int i;
uint32_t ret = 0; uint32_t ret = 0;
for (i = 31; i >= 0; --i) for (i = 31; i >= 0; --i)
ret |= lfsr_rollback_bit(s, BEBIT(in, i), fb) << (i ^ 24); 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] = { 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
* *
* Is an exported helper function from the common prefix attack * 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 * 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 * 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 *lfsr_prefix_ks(uint8_t ks[8], int isodd)
{ {
uint32_t c, entry, *candidates = malloc(4 << 10); uint32_t *candidates = malloc(4 << 10);
int i, size = 0, good; uint32_t c, entry;
int size = 0, i, good;
if(!candidates) if(!candidates)
return 0; return 0;
@ -503,6 +500,12 @@ check_pfx_parity(uint32_t prefix, uint32_t rresp, uint8_t parities[8][8],
/** lfsr_common_prefix /** lfsr_common_prefix
* Implentation of the common prefix attack. * 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* 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])
@ -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); s = statelist = malloc((sizeof *statelist) << 20);
if(!s || !odd || !even) { if(!s || !odd || !even) {
free(statelist); free(statelist);
statelist = 0; free(odd);
goto out; free(even);
return 0;
} }
for(o = odd; *o + 1; ++o) 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; s->odd = s->even = 0;
out:
free(odd); free(odd);
free(even); free(even);
return statelist; return statelist;
} }

View file

@ -15,7 +15,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, US$ MA 02110-1301, US$
Copyright (C) 2008-2008 bla <blapost@gmail.com> Copyright (C) 2008-2014 bla <blapost@gmail.com>
*/ */
#ifndef CRAPTO1_INCLUDED #ifndef CRAPTO1_INCLUDED
#define 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); 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); uint32_t lfsr_rollback_word(struct Crypto1State* s, uint32_t in, int fb);
int nonce_distance(uint32_t from, uint32_t to); 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)\ #define FOREACH_VALID_NONCE(N, FILTER, FSIZE)\
uint32_t __n = 0,__M = 0, N = 0;\ uint32_t __n = 0,__M = 0, N = 0;\
int __i;\ int __i;\

View file

@ -20,8 +20,6 @@
#include "crapto1.h" #include "crapto1.h"
#include <stdlib.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) void crypto1_create(struct Crypto1State *s, uint64_t key)
{ {