mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-13 07:11:56 +01:00
212 lines
6.3 KiB
C#
212 lines
6.3 KiB
C#
using NFC;
|
|
using NFC.Crypto;
|
|
using NFC.ISO7816_4;
|
|
using NFC.Mifare_DESFire;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using PCSC.Iso7816;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace NFC_Test
|
|
{
|
|
public class AuthCrypto_Test
|
|
{
|
|
public byte[] GenerateDefaultKey(int size)
|
|
{
|
|
List<byte> key = new List<byte>();
|
|
for (int i = 0; i < size; i++)
|
|
{
|
|
key.Add(0);
|
|
}
|
|
|
|
return key.ToArray();
|
|
}
|
|
|
|
[Test]
|
|
// https://www.linkedin.com/pulse/mifare-desfire-introduction-david-coelho
|
|
public void AuthExample()
|
|
{
|
|
DES des = new DES();
|
|
|
|
byte[] challenge = new byte[]
|
|
{
|
|
0x93 ,0x9d ,0x2c ,0x2e ,0xa1 ,0x65 ,0x75 ,0xd5
|
|
};
|
|
|
|
byte[] key = GenerateDefaultKey(8);
|
|
byte[] iv = GenerateDefaultKey(8);
|
|
|
|
byte[] rndA = new byte[]
|
|
{
|
|
0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07
|
|
};
|
|
|
|
byte[] rndB = des.Decrypt(challenge, key, iv);
|
|
|
|
byte[] rndB_expected = new byte[]
|
|
{
|
|
0xea ,0x48 ,0x50 ,0x13 ,0xd8 ,0x0a ,0x05 ,0x67
|
|
};
|
|
|
|
Assert.AreEqual(rndB_expected, rndB);
|
|
}
|
|
|
|
[Test]
|
|
public void concatenate()
|
|
{
|
|
MIFARE_DESFire mifareDESFire = new MIFARE_DESFire(null);
|
|
|
|
byte[] rndA = new byte[]
|
|
{
|
|
0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07
|
|
};
|
|
|
|
byte[] rndB = new byte[]
|
|
{
|
|
0x48 ,0x50 ,0x13 ,0xd8 ,0x0a ,0x05 ,0x67 ,0xea
|
|
};
|
|
|
|
byte[] rndA_rndB_expected = new byte[]
|
|
{
|
|
0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07, 0x48 ,0x50 ,0x13 ,0xd8 ,0x0a ,0x05 ,0x67 ,0xea
|
|
};
|
|
|
|
byte[] rndA_rndB = mifareDESFire.concatenate(rndA, rndB);
|
|
|
|
Assert.AreEqual(rndA_rndB_expected, rndA_rndB);
|
|
}
|
|
|
|
[Test]
|
|
public void Auth()
|
|
{
|
|
APDUCommand cmd_getchallange = new APDUCommand(IsoCase.Case4Short)
|
|
{
|
|
CLA = 0x90,
|
|
INS = (byte)0x1A,
|
|
Data = new byte[]
|
|
{
|
|
0x00
|
|
}
|
|
};
|
|
|
|
APDUCommand cmd_answerchallange = new APDUCommand(IsoCase.Case4Short)
|
|
{
|
|
CLA = 0x90,
|
|
INS = (byte)0xAF,
|
|
Data = new byte[]
|
|
{
|
|
0x69, 0x17, 0x8b, 0x93, 0x8c, 0x03, 0xed, 0xf1, 0x86, 0xd3, 0x05, 0x6b, 0xed, 0xc8, 0xd6, 0xcf
|
|
}
|
|
};
|
|
|
|
APDUResponse response = new APDUResponse()
|
|
{
|
|
Body = new byte[]
|
|
{
|
|
0xB8, 0x90, 0x04, 0x7F, 0x2D, 0xC8, 0xD6, 0x8B
|
|
}
|
|
};
|
|
|
|
APDUResponse response2 = new APDUResponse()
|
|
{
|
|
Body = new byte[]
|
|
{
|
|
0x04, 0x79, 0xed, 0x6c, 0x4f, 0x74, 0xda, 0x4a
|
|
}
|
|
};
|
|
|
|
ICard card = Substitute.For<ICard>();
|
|
|
|
card.Transmit(cmd_getchallange).ReturnsForAnyArgs(response);
|
|
card.Transmit(cmd_answerchallange).Returns(response2);
|
|
|
|
MIFARE_DESFire mifareDESFire = new MIFARE_DESFire(card);
|
|
|
|
mifareDESFire.AuthenticateDES(0x00, mifareDESFire.GenerateDefaultKey(16));
|
|
}
|
|
|
|
[Test]
|
|
public void ChangeKey()
|
|
{
|
|
ICard card = Substitute.For<ICard>();
|
|
MIFARE_DESFire mifareDESFire = new MIFARE_DESFire(card);
|
|
|
|
mifareDESFire._SessionKey = new byte[]
|
|
{
|
|
0xDC, 0xB0, 0x96, 0xC2, 0xA4, 0x0E, 0x78, 0xE0, 0xA0, 0xE4, 0x7A, 0x96, 0xF4, 0x2E, 0x62, 0xAE
|
|
};
|
|
mifareDESFire._IV = new byte[]
|
|
{
|
|
0x33, 0x45 , 0xAA , 0x95 , 0xF2 , 0xD9 , 0x56 , 0xCF
|
|
};
|
|
|
|
mifareDESFire.ChangeKeyDES(0x00, GenerateDefaultKey(16), GenerateDefaultKey(16));
|
|
}
|
|
|
|
[Test]
|
|
public void CRC()
|
|
{
|
|
byte[] data = StringToByteArrayFastest("c40045eeb8338ae8f49a032e85bb1114353010");
|
|
|
|
CRC32 crc32 = new CRC32();
|
|
|
|
byte[] crc = crc32.Calculate(data);
|
|
|
|
MIFARE_DESFire dESFire = new MIFARE_DESFire(null);
|
|
|
|
Console.WriteLine("data: {0}", dESFire.ConvertToHexString(data));
|
|
Console.WriteLine("crc: {0}", dESFire.ConvertToHexString(crc));
|
|
|
|
byte[] data_crc = dESFire.concatenate(data, crc);
|
|
Console.WriteLine("data_crc: {0}", dESFire.ConvertToHexString(data_crc));
|
|
}
|
|
|
|
public static byte[] StringToByteArrayFastest(string hex)
|
|
{
|
|
if (hex.Length % 2 == 1)
|
|
throw new Exception("The binary key cannot have an odd number of digits");
|
|
|
|
byte[] arr = new byte[hex.Length >> 1];
|
|
|
|
for (int i = 0; i < hex.Length >> 1; ++i)
|
|
{
|
|
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
public static int GetHexVal(char hex)
|
|
{
|
|
int val = (int)hex;
|
|
//For uppercase A-F letters:
|
|
//return val - (val < 58 ? 48 : 55);
|
|
//For lowercase a-f letters:
|
|
//return val - (val < 58 ? 48 : 87);
|
|
//Or the two combined, but a bit slower:
|
|
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
|
|
}
|
|
|
|
[Test]
|
|
public void AES()
|
|
{
|
|
byte[] data = StringToByteArrayFastest("45eeb8338ae8f49a032e85bb111435301095c3894b0000000000000000000000");
|
|
byte[] key = StringToByteArrayFastest("d99aca2b5b4de3a949fa2cf12b0eb673");
|
|
byte[] iv = StringToByteArrayFastest("00000000000000000000000000000000");
|
|
|
|
MIFARE_DESFire dESFire = new MIFARE_DESFire(null);
|
|
|
|
Console.WriteLine("data: {0}", dESFire.ConvertToHexString(data));
|
|
Console.WriteLine("key: {0}", dESFire.ConvertToHexString(key));
|
|
Console.WriteLine("iv: {0}", dESFire.ConvertToHexString(iv));
|
|
|
|
AES aes = new AES();
|
|
|
|
byte[] data_enc = aes.Encrypt(data, key, iv);
|
|
Console.WriteLine("data_enc: {0}", dESFire.ConvertToHexString(data_enc));
|
|
}
|
|
}
|
|
}
|