2021-07-02 00:53:57 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 2010 Romain Tartiere.
|
|
|
|
// Copyright (C) 2014 Iceman
|
|
|
|
// Copyright (C) 2021 Merlok
|
|
|
|
//
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// High frequency Desfire core functions
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifndef __DESFIRECORE_H
|
|
|
|
#define __DESFIRECORE_H
|
|
|
|
|
|
|
|
#include "common.h"
|
2021-07-04 17:20:09 +08:00
|
|
|
#include "cliparser.h"
|
2021-07-02 00:53:57 +08:00
|
|
|
#include "mifare/desfire_crypto.h"
|
|
|
|
#include "mifare/mifare4.h"
|
|
|
|
|
|
|
|
#define DESF_MAX_KEY_LEN 24
|
|
|
|
|
|
|
|
#define DESFIRE_GET_ISO_STATUS(x) ( ((uint16_t)(0x91<<8)) + (uint16_t)x )
|
|
|
|
|
2021-07-03 05:08:45 +08:00
|
|
|
typedef enum DESFIRE_CRYPTOALGO DesfireCryptoAlgorythm;
|
|
|
|
|
2021-07-02 00:53:57 +08:00
|
|
|
typedef enum {
|
|
|
|
DACNone,
|
|
|
|
DACd40,
|
|
|
|
DACEV1,
|
|
|
|
DACEV2
|
2021-07-03 22:31:12 +08:00
|
|
|
} DesfireSecureChannel;
|
2021-07-02 00:53:57 +08:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
DCCNative,
|
|
|
|
DCCNativeISO,
|
|
|
|
DCCISO
|
2021-07-03 22:31:12 +08:00
|
|
|
} DesfireCommandSet;
|
2021-07-02 00:53:57 +08:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
DCMNone,
|
|
|
|
DCMPlain,
|
|
|
|
DCMMACed,
|
|
|
|
DCMEncrypted
|
|
|
|
} DesfireCommunicationMode;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct DesfireContextS {
|
|
|
|
uint8_t keyNum;
|
2021-07-02 22:37:15 +08:00
|
|
|
enum DESFIRE_CRYPTOALGO keyType; // des/2tdea/3tdea/aes
|
2021-07-02 00:53:57 +08:00
|
|
|
uint8_t key[DESF_MAX_KEY_LEN];
|
2021-07-06 17:16:19 +08:00
|
|
|
|
2021-07-02 00:53:57 +08:00
|
|
|
// KDF finction
|
2021-07-02 22:37:15 +08:00
|
|
|
uint8_t kdfAlgo;
|
|
|
|
uint8_t kdfInputLen;
|
|
|
|
uint8_t kdfInput[31];
|
2021-07-06 17:16:19 +08:00
|
|
|
|
2021-07-03 22:31:12 +08:00
|
|
|
DesfireSecureChannel secureChannel; // none/d40/ev1/ev2
|
|
|
|
DesfireCommandSet cmdSet; // native/nativeiso/iso
|
|
|
|
DesfireCommunicationMode commMode; // plain/mac/enc
|
2021-07-02 00:53:57 +08:00
|
|
|
|
2021-07-05 03:16:43 +08:00
|
|
|
uint8_t IV[DESF_MAX_KEY_LEN];
|
2021-07-02 00:53:57 +08:00
|
|
|
uint8_t sessionKeyMAC[DESF_MAX_KEY_LEN];
|
|
|
|
uint8_t sessionKeyEnc[DESF_MAX_KEY_LEN]; // look at mifare4.h - mf4Session_t
|
|
|
|
uint8_t lastIV[DESF_MAX_KEY_LEN];
|
|
|
|
//mf4Session_t AESSession;
|
|
|
|
uint16_t cntrTx; // for AES
|
|
|
|
uint16_t cntrRx; // for AES
|
|
|
|
uint8_t TI[4]; // for AES
|
|
|
|
} DesfireContext;
|
|
|
|
|
2021-07-04 17:20:09 +08:00
|
|
|
extern const CLIParserOption DesfireAlgoOpts[];
|
|
|
|
extern const CLIParserOption DesfireKDFAlgoOpts[];
|
|
|
|
extern const CLIParserOption DesfireCommunicationModeOpts[];
|
|
|
|
extern const CLIParserOption DesfireCommandSetOpts[];
|
|
|
|
extern const CLIParserOption DesfireSecureChannelOpts[];
|
|
|
|
|
2021-07-02 00:53:57 +08:00
|
|
|
void DesfireClearContext(DesfireContext *ctx);
|
2021-07-03 22:31:12 +08:00
|
|
|
void DesfirePrintContext(DesfireContext *ctx);
|
2021-07-02 22:37:15 +08:00
|
|
|
void DesfireClearSession(DesfireContext *ctx);
|
2021-07-02 00:53:57 +08:00
|
|
|
void DesfireSetKey(DesfireContext *ctx, uint8_t keyNum, enum DESFIRE_CRYPTOALGO keyType, uint8_t *key);
|
2021-07-03 22:31:12 +08:00
|
|
|
void DesfireSetCommandSet(DesfireContext *ctx, DesfireCommandSet cmdSet);
|
|
|
|
void DesfireSetCommMode(DesfireContext *ctx, DesfireCommunicationMode commMode);
|
2021-07-06 17:16:19 +08:00
|
|
|
void DesfireSetKdf(DesfireContext *ctx, uint8_t kdfAlgo, uint8_t *kdfInput, uint8_t kdfInputLen);
|
2021-07-02 00:53:57 +08:00
|
|
|
|
2021-07-02 01:47:02 +08:00
|
|
|
const char *DesfireGetErrorString(int res, uint16_t *sw);
|
2021-07-02 23:06:13 +08:00
|
|
|
uint32_t DesfireAIDByteToUint(uint8_t *data);
|
|
|
|
void DesfireAIDUintToByte(uint32_t aid, uint8_t *data);
|
2021-07-02 01:47:02 +08:00
|
|
|
|
2021-07-02 01:32:04 +08:00
|
|
|
int DesfireExchange(DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen);
|
2021-07-03 18:11:17 +08:00
|
|
|
int DesfireExchangeEx(bool activate_field, DesfireContext *ctx, uint8_t cmd, uint8_t *data, size_t datalen, uint8_t *respcode, uint8_t *resp, size_t *resplen, bool enable_chaining, size_t splitbysize);
|
2021-07-02 00:53:57 +08:00
|
|
|
|
2021-07-02 22:37:15 +08:00
|
|
|
int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2);
|
|
|
|
int DesfireSelectAIDHex(DesfireContext *ctx, uint32_t aid1, bool select_two, uint32_t aid2);
|
2021-07-02 23:06:13 +08:00
|
|
|
|
2021-07-03 22:31:12 +08:00
|
|
|
int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel);
|
2021-07-02 22:37:15 +08:00
|
|
|
bool DesfireIsAuthenticated(DesfireContext *dctx);
|
|
|
|
|
2021-07-02 23:06:13 +08:00
|
|
|
int DesfireGetAIDList(DesfireContext *dctx, uint8_t *resp, size_t *resplen);
|
|
|
|
int DesfireGetDFList(DesfireContext *dctx, uint8_t *resp, size_t *resplen);
|
|
|
|
|
2021-07-02 00:53:57 +08:00
|
|
|
|
|
|
|
#endif // __DESFIRECORE_H
|