mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-04-20 18:36:31 +02:00
Added: Authenticate
This commit is contained in:
parent
c943a51d9c
commit
6a5f521800
@ -4,6 +4,7 @@ using PCSC.Iso7816;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -81,7 +82,7 @@ namespace NFC.Mifare_DESFire
|
|||||||
/// <param name="key"></param>
|
/// <param name="key"></param>
|
||||||
public void Authenticate(byte key_id, byte[] key)
|
public void Authenticate(byte key_id, byte[] key)
|
||||||
{
|
{
|
||||||
APDUCommand cmd_getchallange = new APDUCommand(IsoCase.Case4Short)
|
APDUCommand cmd_challange_request = new APDUCommand(IsoCase.Case4Short)
|
||||||
{
|
{
|
||||||
CLA = 0x90,
|
CLA = 0x90,
|
||||||
INS = (byte)0x1A,
|
INS = (byte)0x1A,
|
||||||
@ -90,55 +91,82 @@ namespace NFC.Mifare_DESFire
|
|||||||
key_id
|
key_id
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
APDUResponse response = _Card.Transmit(cmd_getchallange);
|
APDUResponse response = _Card.Transmit(cmd_challange_request);
|
||||||
|
|
||||||
byte[] challenge = response.Body;
|
byte[] rndB_enc = response.Body;
|
||||||
Console.WriteLine("Challange: {0}", toHexString(challenge));
|
Console.WriteLine("rndB_enc: {0}", toHexString(rndB_enc));
|
||||||
|
|
||||||
|
DES des = new DES();
|
||||||
|
byte[] rndB = des.Decrypt(rndB_enc, key, GenerateDefaultKey(8));
|
||||||
|
Console.WriteLine("rndB: {0}", toHexString(rndB));
|
||||||
|
|
||||||
|
byte[] iv = new byte[8];
|
||||||
|
rndB.CopyTo(iv, 0);
|
||||||
|
|
||||||
|
byte[] rndB_rl = rotateLeft(rndB);
|
||||||
|
Console.WriteLine("rndB_enc: {0}", toHexString(rndB_rl));
|
||||||
|
|
||||||
byte[] rndA = new byte[]
|
byte[] rndA = new byte[]
|
||||||
{
|
{
|
||||||
0x92, 0x31, 0x34, 0x8B, 0x66, 0x35, 0xA8, 0xAF
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
|
||||||
};
|
};
|
||||||
Console.WriteLine("rndA: {0}", toHexString(rndA));
|
Console.WriteLine("rndA: {0}", toHexString(rndA));
|
||||||
|
|
||||||
TripleDES des = new TripleDES();
|
byte[] rndAB = concatenate(rndA, rndB_rl);
|
||||||
byte[] rndB = des.Decrypt(challenge, key, GenerateDefaultKey(8));
|
Console.WriteLine("rndAB: {0}", toHexString(rndAB));
|
||||||
Console.WriteLine("rndB: {0}", toHexString(rndB));
|
|
||||||
|
|
||||||
byte[] leftRotatedRndB = rotateLeft(rndB);
|
byte[] rndAB_enc = des.Encrypt(rndAB, key, rndB_enc);
|
||||||
Console.WriteLine("leftRotatedRndB: {0}", toHexString(leftRotatedRndB));
|
Console.WriteLine("rndA_rndB_enc: {0}", toHexString(rndAB_enc));
|
||||||
|
iv = lastBlock(rndAB_enc);
|
||||||
|
|
||||||
byte[] rndA_rndB = concatenate(rndA, leftRotatedRndB);
|
APDUCommand cmd_challange_response = new APDUCommand(IsoCase.Case4Short)
|
||||||
Console.WriteLine("rndA_rndB: {0}", toHexString(rndA_rndB));
|
|
||||||
|
|
||||||
byte[] challengeAnswer = des.Encrypt(rndA_rndB, key, GenerateDefaultKey(8));
|
|
||||||
Console.WriteLine("challengeAnswer: {0}", toHexString(challengeAnswer));
|
|
||||||
|
|
||||||
APDUCommand cmd_answerchallange = new APDUCommand(IsoCase.Case4Short)
|
|
||||||
{
|
{
|
||||||
CLA = 0x90,
|
CLA = 0x90,
|
||||||
INS = (byte)0xAF,
|
INS = (byte)0xAF,
|
||||||
Data = challengeAnswer
|
Data = rndAB_enc
|
||||||
};
|
};
|
||||||
Console.WriteLine("cmd_answerchallange: {0}", toHexString(cmd_answerchallange.ToArray()));
|
Console.WriteLine("cmd_challange_response: {0}", toHexString(cmd_challange_response.ToArray()));
|
||||||
|
|
||||||
response = _Card.Transmit(cmd_answerchallange);
|
response = _Card.Transmit(cmd_challange_response);
|
||||||
|
|
||||||
byte[] encryptedRndAFromCard = response.Body;
|
byte[] encryptedRndAFromCard = response.Body;
|
||||||
Console.WriteLine("encryptedRndAFromCard: {0}", toHexString(encryptedRndAFromCard));
|
Console.WriteLine("encryptedRndAFromCard: {0}", toHexString(encryptedRndAFromCard));
|
||||||
|
|
||||||
byte[] rotatedRndAFromCard = des.Decrypt(encryptedRndAFromCard, key, GenerateDefaultKey(8));
|
byte[] rotatedRndAFromCard = des.Decrypt(encryptedRndAFromCard, key, iv);
|
||||||
Console.WriteLine("rotatedRndAFromCard: {0}", toHexString(rotatedRndAFromCard));
|
Console.WriteLine("rotatedRndAFromCard: {0}", toHexString(rotatedRndAFromCard));
|
||||||
|
|
||||||
byte[] rndAFromCard = rotateRight(rotatedRndAFromCard);
|
byte[] rndAFromCard = rotateRight(rotatedRndAFromCard);
|
||||||
Console.WriteLine("rndAFromCard: {0}", toHexString(rndAFromCard));
|
Console.WriteLine("rndAFromCard: {0}", toHexString(rndAFromCard));
|
||||||
|
|
||||||
if (!rndA.Equals(rndAFromCard))
|
if (!rndA.SequenceEqual(rndAFromCard))
|
||||||
{
|
{
|
||||||
throw new Exception("???");
|
throw new Exception("???");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] lastBlock(byte[] data)
|
||||||
|
{
|
||||||
|
byte[] block = new byte[8];
|
||||||
|
|
||||||
|
for(int i = 0; i < block.Length; i++)
|
||||||
|
{
|
||||||
|
block[i] = data[data.Length - block.Length + i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
public byte[] exclusiveOR(byte[] a, byte[] b)
|
||||||
|
{
|
||||||
|
byte[] c = new byte[a.Length];
|
||||||
|
|
||||||
|
for (int i = 0; i < a.Length; i++)
|
||||||
|
{
|
||||||
|
c[i] = (byte)(a[i] ^ b[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
private String toHexString(byte[] data)
|
private String toHexString(byte[] data)
|
||||||
{
|
{
|
||||||
return BitConverter.ToString(data).Replace("-", string.Empty);
|
return BitConverter.ToString(data).Replace("-", string.Empty);
|
||||||
@ -165,7 +193,7 @@ namespace NFC.Mifare_DESFire
|
|||||||
data.CopyTo(rotate, 0);
|
data.CopyTo(rotate, 0);
|
||||||
|
|
||||||
byte temp = rotate[rotate.Length - 1];
|
byte temp = rotate[rotate.Length - 1];
|
||||||
for (var i = rotate.Length - 1; i > 1; i--)
|
for (var i = rotate.Length - 1; i > 0; i--)
|
||||||
{
|
{
|
||||||
rotate[i] = rotate[i - 1];
|
rotate[i] = rotate[i - 1];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user