MIFARE DESFire EV1 Authentication Issue

admin

Administrator
Staff member
I've been trying to authenticate with a MIFARE DESFire EV1 card with the default key (00000000h) for the last week to no avail. I have followed <a href="http://ridrix.wordpress.com/2009/09/19/mifare-desfire-communication-example/" rel="nofollow noreferrer">this blog</a>'s instructions to the letter. I implemented
Code:
Send mode CBC
and
Code:
Receive mode CBC
like this:

Code:
var
  SendVector, ReceiveVector: UInt64;

procedure ResetVectors;
begin
  SendVector := 0;
  ReceiveVector := 0;
end;

procedure Encrypt(var Data: TBytes; Key: TBytes);
var
  iData, iKey: UInt64;
  i: Integer;
begin
  if Length(Data) mod 8 &gt; 0 then
    SetLength(Data, Length(Data) + (8 - Length(Data) mod 8));

  Move(Key[0], iKey, 8);
  for i := 0 to (Length(Data) - 1) div 8 do
  begin
    Move(Data[i * 8], iData, 8);
    EncryptInt64(iData, iKey);
    Move(iData, Data[i * 8], 8);
  end;
end;

procedure EncryptInt64(var Data, Key: Int64);
begin
  Data := Data xor SendVector;
  DESEncrypt(@Data, @Key);
  SendVector := Data;
end;

procedure Decrypt(var Data: TBytes; Key: TBytes);
var
  iData, iKey: UInt64;
  i: Integer;
begin
  Move(Key[0], iKey, 8);
  for i := 0 to (Length(Data) - 1) div 8 do
  begin
    Move(Data[i * 8], iData, 8);
    DecryptInt64(iData, iKey);
    Move(iData, Data[i * 8], 8);
  end;
end;

procedure DecryptInt64(var Data, Key: Int64);
var
  Tmp: UInt64;
begin
  Tmp := ReceiveVector;
  ReceiveVector := Data;
  DESDecrypt(@Data, @Key);
  Data := Data xor Tmp;
end;

This is the log of APDU commands I sent to the card, and their corresponding responses:

Code:
--&gt;90 6A 00 00 00 // List Applications
&lt;--01 02 03 
&lt;--9100 (OK)

--&gt;90 5A 00 00 03 00 00 00 00 // Select PICC
&lt;--9100 (OK)

--&gt;90 1A 00 00 01 00 00 // ISO Authenticate with master key (00000000h)
&lt;--91AF

--&gt;90 AF 00 00 00 // Retreive RndB
&lt;--A4 4C 2B D1 EB 6F 64 0C 
&lt;--9100 (OK)

--&gt;90 AF 00 00 10 0D 9F 27 9B A5 D8 72 60 25 DD 7A 19 63 0F 26 2D 00 // Send DES(RndA + RndB')
&lt;--91AE (AUTHENTICATION_FAILURE)

Here is the whole code of my
Code:
Authenticate
method:

Code:
procedure Authenticate;
var
  Key, Data: TBytes;
  s: string;
  b: Byte;
  RndA: UInt64;
  i: Integer;
begin
  ResetVectors;
  Key := HexStringToBuffer('00 00 00 00 00 00 00 00');
  s := '90 1A 00 00 01 00 00';
  s := SendAPDU(s, False);
  Data := HexStringToBuffer(s);
  Decrypt(Data, Key);

  b := Data[0];
  for i := 0 to 6 do
    Data[i] := Data[i + 1];
  Data[7] := b;

  RndA := 1; // not very wise

  SetLength(Data, 16);
  Move(Data[0], Data[8], 8);
  Move(RndA, Data[0], 8);

  Encrypt(Data, Key);
  s := '90 AF 00 00 10 ' + BufferToHexString(Data) + ' 00';
  SendAPDU(s, False);
end;

I'm lost as to why the card is rejecting my authentication attempt flatly. Any thoughts?

<hr>

Here's the diagram of CBC Send and CBC Receive algorithms as per DESFire EV1 manufacturer instructions:
<img src=" " alt="CBC Send">
<img src=" " alt="CBC Receive">