[英]MIFARE DESFire EV1 Authentication Issue
I've been trying to authenticate with a MIFARE DESFire EV1 card with the default key (00000000h) for the last week to no avail. 我一直在尝试使用带有默认密钥(00000000h)的MIFARE DESFire EV1卡进行身份验证,在上周没有任何帮助。 I have followed this blog 's instructions to the letter. 我已按照该博客的说明进行操作。 I implemented Send mode CBC
and Receive mode CBC
like this: 我实现了Send mode CBC
和Receive mode CBC
如下所示:
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 > 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: 这是我发送到卡的APDU命令的日志及其相应的响应:
-->90 6A 00 00 00 // List Applications
<--01 02 03
<--9100 (OK)
-->90 5A 00 00 03 00 00 00 00 // Select PICC
<--9100 (OK)
-->90 1A 00 00 01 00 00 // ISO Authenticate with master key (00000000h)
<--91AF
-->90 AF 00 00 00 // Retreive RndB
<--A4 4C 2B D1 EB 6F 64 0C
<--9100 (OK)
-->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')
<--91AE (AUTHENTICATION_FAILURE)
Here is the whole code of my Authenticate
method: 这是我的Authenticate
方法的完整代码:
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? 有什么想法吗?
Here's the diagram of CBC Send and CBC Receive algorithms as per DESFire EV1 manufacturer instructions: 这是根据DESFire EV1制造商说明的CBC发送和CBC接收算法图:
Try replace encrypt to decrypt in DES cipher. 尝试替换加密以DES密码解密。 Card ALWAYS uses DES ENCRYPT mode (both when recieving and sending data). 卡始终使用DES加密模式(在接收和发送数据时)。 And the host ALWAYS uses DECRYPT mode. 并且主机始终使用DECRYPT模式。
For more info: https://ridrix.wordpress.com/2009/09/19/mifare-desfire-communication-example/#comment-30 有关更多信息: https : //ridrix.wordpress.com/2009/09/19/mifare-desfire-communication-example/#comment-30
During an ISO or AES authentication the following scheme is used: 在ISO或AES认证期间,使用以下方案:
IMPORTANT : ALL encryption goes through CBC. 重要提示 :所有加密均通过CBC进行。 The IV of the key used for CBC encryption/decryption is only reset ONCE at the beginning. 用于CBC加密/解密的密钥的IV仅在开始时复位一次。 Then it must be maintained up to date during ALL the following commands. 然后必须在所有以下命令期间将其保持最新。
If you use ISO or AES mode, after a successfull authentication, you MUST also calulcate the CMAC over sent commands and received data, otherwise your IV will not be in sync with the card and you get an Integrity Error each time you use the Session key! 如果您使用ISO或AES模式,则在成功通过身份验证之后,还必须通过发送的命令和接收到的数据来计算CMAC,否则IV不会与卡同步,并且每次使用Session密钥时都会收到Integrity错误。 !
As I was struggeling with the same problems I posted some communication examples that will help you much in testing your code. 当我在同样的问题中挣扎时,我张贴了一些交流示例,这些示例将极大地帮助您测试代码。 You find them here on Stackoverflow: Desfire EV1 communication examples There you also find a link to my source code that I recommnd you to study. 您可以在Stackoverflow上找到它们: Desfire EV1通信示例在这里您还可以找到我建议您学习的源代码链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.