Ok i got it that is my mistake
Here Crypto1Win32.exe, Below i just test Crypto1 code running on Win32.
I have create with Visual Studio2005 everything based on win32 environment.
Using
C:\Crypto1Win32.exe UID Tag_challenge nr_enc Reader_resonse Tag_response
Try to play this
// Crypto1Win32.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "crapto1.h"
#include <stdio.h>
#include <windows.h>
LPSTR UnicodeToAnsi(LPCWSTR s);
uint32_t HexStrToUINT32(const PBYTE inBuf, unsigned int uiLen);
int _tmain(int argc, wchar_t* argv[])
{
struct Crypto1State *revstate;
uint64_t lfsr;
unsigned char* plfsr = (unsigned char*)&lfsr;
uint32_t uid = 0xc108416a; //UID
uint32_t tag_challenge = 0xabcd1949; //Nt
uint32_t nr_enc = 0x59d5920f; //[Nr,
uint32_t reader_resonse = 0x15b9d553; // Nt']
uint32_t tag_response = 0xa79a3fee; //[Nt'']
uint32_t arguments[5];
char name[5][20] = {"UID \t\t","Tag Challenge","[Reader Challenge]","[Reader response]","[Tag response]"};
if(argc == 6)
{
for(int i=1; i<=5; i++)
{
LPCSTR tmp = UnicodeToAnsi(argv[i]);
printf("%s : \t\t 0x%s, len:%d\n", &name[i-1], tmp, strlen(tmp));
arguments[i-1] = HexStrToUINT32( (const PBYTE)tmp, (int)strlen(tmp) );
delete[] tmp;
}
uid = arguments[0];
tag_challenge = arguments[1];
nr_enc = arguments[2];
reader_resonse = arguments[3];
tag_response = arguments[4];
}
else
{
printf("This cmd requires a set of arguments :\n");
printf("UID Tag_challenge nr_enc Reader_resonse Tag_response\n");
return 0;
}
uint32_t ks2 = reader_resonse ^ prng_successor(tag_challenge, 64);
uint32_t ks3 = tag_response ^ prng_successor(tag_challenge, 96);
printf("nt': %08x\t",prng_successor(tag_challenge, 64));
printf("nt'': %08x\t",prng_successor(tag_challenge, 96));
printf("ks2: %08x\t",ks2);
printf("ks3: %08x\n",ks3);
revstate = lfsr_recovery(ks2, ks3);
lfsr_rollback(revstate, 0, 0);
lfsr_rollback(revstate, 0, 0);
lfsr_rollback(revstate, nr_enc, 1);
lfsr_rollback(revstate, uid ^ tag_challenge, 0);
crypto1_get_lfsr(revstate, &lfsr);
printf("\nFound Key: [%02x %02x %02x %02x %02x %02x]\n",plfsr[0],plfsr[1],plfsr[2],plfsr[3],plfsr[4],plfsr[5]);
return 0;
}
uint32_t HexStrToUINT32(const PBYTE inBuf, unsigned int uiLen)
{
int niblePair=0x0;
int j,k,bytecount=0;
uint32_t rtn = 0x0;
for( UINT c=0 ; c<uiLen-1 ; c++ )
{
// check character pair, loop if either j,k is over range
do
{
j = toupper(inBuf[c]);
k = toupper(inBuf[c+1]);
++c;
// interpretor and matching for parenthesis ignorance.
if(j == '(')do{
j = toupper(inBuf[c]);
k = toupper(inBuf[c+1]);
++c;
}while(j != ')');
// NOT[(Is-j-valid)AND(Is-k-valid) OR Is-c-in-range]
} while( !(((j>='A')&&(j<='F')||(j>='0')&&(j<='9')) &&
((k>='A')&&(k<='F')||(k>='0')&&(k<='9')) || (c>=uiLen)));
/*** After a pair is ok!, proceed the step belows. ***/
// convert char of MSB' nibble to hex
if ((j>='A') && (j<='F'))
niblePair = 10+j-'A';
else if(j>='0' && j<='9')
niblePair = j-'0';
else
continue;
niblePair<<=4;
// convert char of LSB' nibble to hex
if ((k>='A') && (k<='F'))
niblePair|= (10+k-'A');
else if(k>='0' && k<='9')
niblePair|= (k-'0');
else
continue;
// store int32
rtn |= (uint32_t)niblePair;
if(c < uiLen-2)rtn<<=8;
}
return rtn;
}
LPSTR UnicodeToAnsi(LPCWSTR s)
{
if (s==NULL) return NULL;
int cw=lstrlenW(s);
if (cw==0) {CHAR *psz=new CHAR[1];*psz='\0';return psz;}
int cc=WideCharToMultiByte(CP_ACP,0,s,cw,NULL,0,NULL,NULL);
if (cc==0) return NULL;
CHAR *psz=new CHAR[cc+1];
cc=WideCharToMultiByte(CP_ACP,0,s,cw,psz,cc,NULL,NULL);
if (cc==0) {delete[] psz;return NULL;}
psz[cc]='\0';
return psz;
};