Forum › MIFARE SmartCard IC`s › MIFARE DESFire › I have problem in AUTHENTICATION › Re: I have problem in AUTHENTICATION
Please tell me how to create session key . my problem may be session key.
I can't underestand when I must get RandA and RandB to create session key.
after create session key , i must create CMAC. if is true , how to make cmac ?
thanks a lot
void Create_session_key(const uint8_t rnda[], const uint8_t rndb[])
{
memcpy (session_key, rnda, 4);
memcpy (session_key+4, rndb, 4);
memcpy (session_key+8, rnda+4, 4);
memcpy (session_key+12, rndb+4, 4);
}
bool DesFire_Authenticate(uint8_t KeyNo,const uint8_t *key)
{
uint8_t randA[8],randB[8];
desInit(&des,key,8);
send_buffer[0] = 0x0A; // cmd
send_buffer[1] = KeyNo; // key number
if(PN532_transfer(send_buffer,2,get_buffer,&get_len)== true)
{
}
else
{
return false;
}
if(get_buffer[0] != 0xAF)
return false;
/////////////////////////////////////////////////////////////////////////////
// get encrypted(randB) from the response
uint8_t b0[8] ;
for(int i = 0; i < 8; i++)
{
b0 = get_buffer;
//randB = b0;
}
// decrypt encoded(randB)
uint8_t r0[8];
desDecryptBlock(&des,b0, r0);
for(int i = 0; i < 8; i++)
{
randB = r0;
}
// generate randA (integer 0-7 for trying, should randomize for real-life use)
uint8_t nr[8];
for(int i = 0; i < 8; i++)
{
nr = rand()/0x808080;
randA = nr;
}
Create_session_key(randA,randB);
// decrypt randA, should XOR with IV, but IV is all 0's, not necessary
uint8_t b1[8];
desDecryptBlock(&des,nr,b1);
// shift randB one byte left and get randB'
uint8_t r1[8];
for(int i = 0; i < 7; i++) {
r1 = r0;
}
r1[7]=r0[0];
// xor randB' with randA and decrypt
uint8_t b2[8];
for(int i = 0; i < 8; i++) {
b2 = (uint8_t) (b1 ^ r1);
}
desDecryptBlock(&des,b2,b2);
// concat (randA + randB')
uint8_t b1b2[16];
for (int i = 0; i < sizeof(b1b2); i++) {
if(i <= 7) {
b1b2 = b1;
} else {
b1b2=b2;
}
}
/////////////////////////////////////////////////////////////////////////////
send_buffer[0] = 0xAF;
for(int i=1 ; i<17 ; i++)
send_buffer = b1b2;
if(PN532_transfer(send_buffer,17,get_buffer,&get_len)== true)
{
if(get_buffer[0] == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
return false;
}
I can't underestand when I must get RandA and RandB to create session key.
after create session key , i must create CMAC. if is true , how to make cmac ?
thanks a lot
void Create_session_key(const uint8_t rnda[], const uint8_t rndb[])
{
memcpy (session_key, rnda, 4);
memcpy (session_key+4, rndb, 4);
memcpy (session_key+8, rnda+4, 4);
memcpy (session_key+12, rndb+4, 4);
}
bool DesFire_Authenticate(uint8_t KeyNo,const uint8_t *key)
{
uint8_t randA[8],randB[8];
desInit(&des,key,8);
send_buffer[0] = 0x0A; // cmd
send_buffer[1] = KeyNo; // key number
if(PN532_transfer(send_buffer,2,get_buffer,&get_len)== true)
{
}
else
{
return false;
}
if(get_buffer[0] != 0xAF)
return false;
/////////////////////////////////////////////////////////////////////////////
// get encrypted(randB) from the response
uint8_t b0[8] ;
for(int i = 0; i < 8; i++)
{
b0 = get_buffer;
//randB = b0;
}
// decrypt encoded(randB)
uint8_t r0[8];
desDecryptBlock(&des,b0, r0);
for(int i = 0; i < 8; i++)
{
randB = r0;
}
// generate randA (integer 0-7 for trying, should randomize for real-life use)
uint8_t nr[8];
for(int i = 0; i < 8; i++)
{
nr = rand()/0x808080;
randA = nr;
}
Create_session_key(randA,randB);
// decrypt randA, should XOR with IV, but IV is all 0's, not necessary
uint8_t b1[8];
desDecryptBlock(&des,nr,b1);
// shift randB one byte left and get randB'
uint8_t r1[8];
for(int i = 0; i < 7; i++) {
r1 = r0;
}
r1[7]=r0[0];
// xor randB' with randA and decrypt
uint8_t b2[8];
for(int i = 0; i < 8; i++) {
b2 = (uint8_t) (b1 ^ r1);
}
desDecryptBlock(&des,b2,b2);
// concat (randA + randB')
uint8_t b1b2[16];
for (int i = 0; i < sizeof(b1b2); i++) {
if(i <= 7) {
b1b2 = b1;
} else {
b1b2=b2;
}
}
/////////////////////////////////////////////////////////////////////////////
send_buffer[0] = 0xAF;
for(int i=1 ; i<17 ; i++)
send_buffer = b1b2;
if(PN532_transfer(send_buffer,17,get_buffer,&get_len)== true)
{
if(get_buffer[0] == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
return false;
}
+ 0
|
- 0