Skip to content

Commit

Permalink
createapp fully works
Browse files Browse the repository at this point in the history
  • Loading branch information
merlokk committed Jul 13, 2021
1 parent 2f2942c commit 1b3e34c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
27 changes: 17 additions & 10 deletions client/src/cmdhfmfdes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5034,7 +5034,7 @@ static int CmdHF14ADesDefault(const char *Cmd) {

static int CmdHF14ADesCreateApp(const char *Cmd) {
CLIParserContext *ctx;
CLIParserInit(&ctx, "hf mfdes createid",
CLIParserInit(&ctx, "hf mfdes createaid",
"Create application. Master key needs to be provided.",
"option rawdata have priority over the rest settings, and options ks1 and ks2 have priority over corresponded key settings\n"
"\n"\
Expand Down Expand Up @@ -5065,9 +5065,9 @@ static int CmdHF14ADesCreateApp(const char *Cmd) {
" 6E = with FID, 3TDEA, 14 keys\n"\
" AE = with FID, AES, 14 keys\n"\
"\n"\
"hf mfdes createid --rawdata 123456 -> execute create by rawdata\n"\
"hf mfdes createid --aid 123456 --fid 2345 --dfname aid123456 -> app aid, iso file id, and iso df name is specified\n"
"hf mfdes createid --aid 123456 --fid 2345 --dfname aid123456 --dstalgo aes -> with algorithm for key AES");
"hf mfdes createaid --rawdata 123456 -> execute create by rawdata\n"\
"hf mfdes createaid --aid 123456 --fid 2345 --dfname aid123456 -> app aid, iso file id, and iso df name is specified\n"
"hf mfdes createaid --aid 123456 --fid 2345 --dfname aid123456 --dstalgo aes -> with algorithm for key AES");

void *argtable[] = {
arg_param_begin,
Expand Down Expand Up @@ -5156,9 +5156,14 @@ static int CmdHF14ADesCreateApp(const char *Cmd) {
}

if (keycount > 0x0e || keycount < 1) {
PrintAndLogEx(ERR, "Key count must be in the range 0x01..0x0e");
PrintAndLogEx(ERR, "Key count must be in the range 1..14");
return PM3_ESOFT;
}

if (dfnamelen > 16) {
PrintAndLogEx(ERR, "DF name must be a maximum of 16 bytes in length");
return PM3_EINVARG;
}

res = DesfireSelectAndAuthenticate(&dctx, securechann, 0x000000, verbose);
if (res != PM3_SUCCESS) {
Expand All @@ -5178,19 +5183,21 @@ static int CmdHF14ADesCreateApp(const char *Cmd) {

if (!ks2present) {
if (keycount > 0) {
//data[4] keycount
data[4] &= 0xf0;
data[4] |= keycount & 0x0f;
}
//data[4] dstalgo
}

uint8_t kt = DesfireKeyAlgoToType(dstalgo);
data[4] &= 0x3f;
data[4] |= (kt & 0x03) << 6;
}

datalen = 5;
if (fileidpresent || (data[4] & 0x20) != 0) {
data[5] = fileid & 0xff;
data[6] = (fileid >> 8) & 0xff;
data[4] |= 0x20; // set bit FileID in the ks2
memcpy(&data[7], dfname, dfnamelen);
datalen = 7 + 16;
datalen = 7 + dfnamelen;
}
}

Expand Down
9 changes: 9 additions & 0 deletions client/src/mifare/desfirecore.c
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,15 @@ int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len) {
return DesfireCommandTxData(dctx, MFDES_CHANGE_KEY_SETTINGS, data, len);
}

uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType) {
switch(keyType) {
case T_DES: return 0x00;
case T_3DES: return 0x00;
case T_3K3DES: return 0x01;
case T_AES: return 0x02;
}
return 0;
}
static void PrintKeyType(uint8_t keytype) {
switch (keytype) {
case 00:
Expand Down
1 change: 1 addition & 0 deletions client/src/mifare/desfirecore.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ int DesfireGetKeyVersion(DesfireContext *dctx, uint8_t *data, size_t len, uint8_
int DesfireGetKeySettings(DesfireContext *dctx, uint8_t *resp, size_t *resplen);
int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len);
void PrintKeySettings(uint8_t keysettings, uint8_t numkeys, bool applevel, bool print2ndbyte);
uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType);

#endif // __DESFIRECORE_H

0 comments on commit 1b3e34c

Please sign in to comment.