Skip to content

Mifare DESFire EV1 examples

Adrien JUND edited this page Aug 10, 2017 · 1 revision

Found bellow some code examples on how to use Mifare DESFire EV1 chip functionality with this sdk.\ All examples assume you already have a valid chip object. Please see How to Use Liblogicalaccess first.

As DESFire EV1 inherits a lot from DESFire chip, please also see this mifare-desfire-examples.

Configure your project

Using specific chip functionality means you cannot use the generic abstraction layer / plug-in architecture provided by the core library to achieve your goal.\ Your project has to include DESFire EV1 plug-in headers and link with it directly.

Add the following include to your code file:

#include <desfire/desfireev1chip.hpp>

On Windows, link with ''DESFireCards.lib''.\ On Linux, link with ''libDESFireCards.so''.

Examples

std::shared_ptr<logicalaccess::DESFireEV1Chip> desfireev1Chip = std::dynamic_pointer_cast<logicalaccess::DESFireEV1Chip>(chip);

if (desfireev1Chip)
{
    std::shared_ptr<logicalaccess::DESFireEV1Commands> desfireev1cmd = desfireev1cp->getDESFireEV1Commands();
        
    // DO DESFIRE STUFF HERE
    // DO DESFIRE STUFF HERE
    // DO DESFIRE STUFF HERE
}
else
{
    std::cout << "Not a DESFire EV1 chip." << std::endl;
}

Read / Write data using the generic way

All chip memory can be addressed on a generic way using Location and Access Information.\ It is recommended to use this method as it do all required stuff (key loading, authentication, structure check and creation if missing, data alignment ...).

// The excepted memory tree
std::shared_ptr<logicalaccess::DESFireEV1Location> dlocation(new logicalaccess::DESFireEV1Location());
// The Application ID to use
dlocation->aid = 0x000521;
// File 0 into this application
dlocation->file = 0;
// File communication requires encryption
dlocation->securityLevel = logicalaccess::CM_ENCRYPT;
dlocation->useEV1 = true;
dlocation->cryptoMethod = logicalaccess::DF_KEY_AES;
location = dlocation;
 
// Keys to use for authentication
std::shared_ptr<logicalaccess::DESFireAccessInfo> daiToUse(new logicalaccess::DESFireAccessInfo());
daiToUse->masterCardKey->fromString("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
aiToUse = daiToUse;
 
// Change keys with the following ones
std::shared_ptr<logicalaccess::DESFireAccessInfo> daiToWrite(new logicalaccess::DESFireAccessInfo());
daiToWrite->masterCardKey->fromString("ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff");
daiToWrite->masterApplicationKey->fromString("aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa");
daiToWrite->masterApplicationKey->setKeyType(logicalaccess::DF_KEY_AES);
daiToWrite->readKey->fromString("22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22");
daiToWrite->readKey->setKeyType(logicalaccess::DF_KEY_AES);
daiToWrite->readKeyno = 2;
daiToWrite->writeKey->fromString("11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11");
daiToWrite->writeKey->setKeyType(logicalaccess::DF_KEY_AES);
daiToWrite->writeKeyno = 1;
aiToWrite = daiToWrite;

Then see read write data on chip to learn how to use it.

Create a new application requiring AES crypto

logicalaccess::DESFireKeySettings settings = logicalaccess::KS_DEFAULT;
int maxNbkeys = 3;
desfirecmd->createApplication(0x000521, settings, maxNbkeys, logicalaccess::FIDS_NO_ISO_FID, logicalaccess::DF_KEY_AES):
std::cout << "Application 0x000521 created successfully." << std::endl;

You need to be authenticated with the PICC master key, or the chip must be configured to allow application creation without authentication.