From ab249d0d76a11a23aab65a818a9e42671eefda74 Mon Sep 17 00:00:00 2001
From: merlokk <807634+merlokk@users.noreply.github.com>
Date: Sat, 31 Jul 2021 16:46:16 +0300
Subject: [PATCH] make style

---
 client/src/cmddata.c                  |  2 +-
 client/src/cmdhfmfdes.c               | 30 +++++------
 client/src/mifare/desfirecore.c       | 72 +++++++++++++--------------
 client/src/mifare/desfirecore.h       |  4 +-
 client/src/mifare/desfirecrypto.c     | 14 +++---
 client/src/mifare/desfirecrypto.h     |  2 +-
 client/src/mifare/desfiresecurechan.c |  4 +-
 client/src/mifare/desfiretest.c       | 10 ++--
 doc/commands.json                     | 65 ++++++++++++++----------
 doc/commands.md                       | 66 ++++++++++++------------
 10 files changed, 141 insertions(+), 128 deletions(-)

diff --git a/client/src/cmddata.c b/client/src/cmddata.c
index a6f93592f..32e2600de 100644
--- a/client/src/cmddata.c
+++ b/client/src/cmddata.c
@@ -427,7 +427,7 @@ int ASKDemod_ext(int clk, int invert, int maxErr, size_t maxlen, bool amplify, b
     if (start_idx >= clk / 2) {
         start_idx -= clk / 2;
     }
-    if ( askType == 0 ) { // if not Manchester, clock width is halved
+    if (askType == 0) {   // if not Manchester, clock width is halved
         clk /= 2;
     }
     if (errCnt < 0 || bitlen < 16) { //if fatal error (or -1)
diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c
index 63e691aa3..e93531403 100644
--- a/client/src/cmdhfmfdes.c
+++ b/client/src/cmdhfmfdes.c
@@ -3069,7 +3069,7 @@ static int CmdHF14ADesSelectApp(const char *Cmd) {
         CLIParserFree(ctx);
         return res;
     }
-    
+
     uint8_t dfname[32] = {0};
     int dfnamelen = 16;
     CLIGetStrWithReturn(ctx, 12, dfname, &dfnamelen);
@@ -3092,22 +3092,22 @@ static int CmdHF14ADesSelectApp(const char *Cmd) {
 
         if (resplen > 0)
             PrintAndLogEx(FAILED, "Application " _CYAN_("FCI template") " [%zu]%s", resplen, sprint_hex(resp, resplen));
-        
+
         if (dfnamelen > 0)
             PrintAndLogEx(SUCCESS, "Application `%s` selected " _GREEN_("succesfully"), (char *)dfname);
         else
             PrintAndLogEx(SUCCESS, "PICC MF selected " _GREEN_("succesfully"));
-    } else {    
+    } else {
         res = DesfireSelectAndAuthenticateEx(&dctx, securechann, appid, true, verbose);
         if (res != PM3_SUCCESS) {
             DropField();
             PrintAndLogEx(FAILED, "Select application 0x%06x " _RED_("failed") " ", appid);
             return res;
         }
-        
+
         PrintAndLogEx(SUCCESS, "Application 0x%06x selected " _GREEN_("succesfully") " ", appid);
     }
-    
+
     DropField();
     return res;
 }
@@ -3146,9 +3146,9 @@ static int CmdHF14ADesBruteApps(const char *Cmd) {
     CLIGetHexWithReturn(ctx, 2, endAid, &endLen);
     uint32_t idIncrement = arg_get_int_def(ctx, 3, 1);
     bool mad = arg_get_lit(ctx, 4);
-    
+
     CLIParserFree(ctx);
-    
+
     // tru select PICC
     res = DesfireSelectAIDHex(&dctx, 0x000000, false, 0);
     if (res != PM3_SUCCESS) {
@@ -3156,7 +3156,7 @@ static int CmdHF14ADesBruteApps(const char *Cmd) {
         PrintAndLogEx(FAILED, "Desfire PICC level select " _RED_("failed") ". Maybe wrong card or no card in the field.");
         return res;
     }
-    
+
     // TODO: We need to check the tag version, EV1 should stop after 26 apps are found
     if (mad) {
         idIncrement = 0x10;
@@ -3174,18 +3174,18 @@ static int CmdHF14ADesBruteApps(const char *Cmd) {
     PrintAndLogEx(INFO, "Enumerating through all AIDs manually, this will take a while!");
     for (uint32_t id = idStart; id <= idEnd && id >= idStart; id += idIncrement) {
         if (kbd_enter_pressed()) break;
-        
+
         int progress = ((id - idStart) * 100) / ((idEnd - idStart));
         PrintAndLogEx(INPLACE, "Progress: %d %%, current AID: %06X", progress, id);
-        
+
         res = DesfireSelectAIDHexNoFieldOn(&dctx, id);
-        
+
         if (res == PM3_SUCCESS) {
             printf("\33[2K\r"); // clear current line before printing
             PrintAndLogEx(SUCCESS, "Got new APPID %06X", id);
         }
     }
-    
+
     PrintAndLogEx(NORMAL, "");
     PrintAndLogEx(SUCCESS, _GREEN_("Done"));
     DropField();
@@ -3250,7 +3250,7 @@ static int CmdHF14ADesAuth(const char *Cmd) {
         PrintAndLogEx(FAILED, "Select or authentication 0x%06x " _RED_("failed") ". Result [%d] %s", appid, res, DesfireAuthErrorToStr(res));
         return res;
     }
-    
+
     if (appid == 0x000000)
         PrintAndLogEx(SUCCESS, "PICC selected and authenticated " _GREEN_("succesfully"));
     else
@@ -3258,7 +3258,7 @@ static int CmdHF14ADesAuth(const char *Cmd) {
 
     PrintAndLogEx(SUCCESS, _CYAN_("Context: "));
     DesfirePrintContext(&dctx);
-    
+
     if (save) {
         defaultKeyNum = dctx.keyNum;
         defaultAlgoId = dctx.keyType;
@@ -3269,7 +3269,7 @@ static int CmdHF14ADesAuth(const char *Cmd) {
         defaultSecureChannel = securechann;
         defaultCommSet = dctx.cmdSet;
         defaultCommMode = dctx.commMode;
-        
+
         PrintAndLogEx(SUCCESS, "Context saved to defaults " _GREEN_("succesfully") ". You can check them by command " _YELLOW_("hf mfdes default"));
     }
 
diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c
index 51ad6172d..e60093317 100644
--- a/client/src/mifare/desfirecore.c
+++ b/client/src/mifare/desfirecore.c
@@ -673,10 +673,10 @@ static int DesfireExchangeISONative(bool activate_field, DesfireContext *ctx, ui
 static int DesfireExchangeISO(bool activate_field, DesfireContext *ctx, sAPDU apdu, uint16_t le, uint8_t *resp, size_t *resplen, uint16_t *sw) {
     uint32_t rlen = 0;
     int res = DESFIRESendApduEx(activate_field, apdu, le, resp, 255, &rlen, sw);
-    
+
     if (res == PM3_SUCCESS)
         *resplen = rlen;
-    
+
     return res;
 }
 
@@ -775,10 +775,10 @@ int DesfireSelectAID(DesfireContext *ctx, uint8_t *aid1, uint8_t *aid2) {
 
         DesfireClearSession(ctx);
         ctx->appSelected = (aid1[0] != 0x00 || aid1[1] != 0x00 || aid1[2] != 0x00);
-        
+
         return PM3_SUCCESS;
     }
-    
+
     return res;
 }
 
@@ -817,7 +817,7 @@ int DesfireSelectAIDHexNoFieldOn(DesfireContext *ctx, uint32_t aid) {
 int DesfireSelectAndAuthenticateEx(DesfireContext *dctx, DesfireSecureChannel secureChannel, uint32_t aid, bool noauth, bool verbose) {
     if (verbose)
         DesfirePrintContext(dctx);
-    
+
     bool isosw = false;
     if (dctx->cmdSet == DCCISO) {
         dctx->cmdSet = DCCNativeISO;
@@ -833,7 +833,7 @@ int DesfireSelectAndAuthenticateEx(DesfireContext *dctx, DesfireSecureChannel se
     }
     if (verbose)
         PrintAndLogEx(INFO, "App %06x " _GREEN_("selected"), aid);
-    
+
     if (isosw)
         dctx->cmdSet = DCCISO;
 
@@ -1154,7 +1154,7 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
     uint8_t RndB[CRYPTO_AES_BLOCK_SIZE] = {0};
     uint8_t encRndB[CRYPTO_AES_BLOCK_SIZE] = {0};
     uint8_t rotRndB[CRYPTO_AES_BLOCK_SIZE] = {0};   //RndB'
-    uint8_t both[CRYPTO_AES_BLOCK_SIZE * 2 + 1] = {0};  // ek/dk_keyNo(RndA+RndB')    
+    uint8_t both[CRYPTO_AES_BLOCK_SIZE * 2 + 1] = {0};  // ek/dk_keyNo(RndA+RndB')
 
     uint8_t subcommand = firstauth ? MFDES_AUTHENTICATE_EV2F : MFDES_AUTHENTICATE_EV2NF;
     uint8_t *key = dctx->key;
@@ -1162,7 +1162,7 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
     size_t recv_len = 0;
     uint8_t respcode = 0;
     uint8_t recv_data[256] = {0};
-    
+
     if (verbose)
         PrintAndLogEx(INFO, _CYAN_("Auth %s:") " cmd: 0x%02x keynum: 0x%02x key: %s", (firstauth) ? "first" : "non-first", subcommand, dctx->keyNum, sprint_hex(key, 16));
 
@@ -1191,7 +1191,7 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
     // Part 3
     if (aes_decode(IV, key, encRndB, RndB, CRYPTO_AES_BLOCK_SIZE))
         return 5;
-    
+
     if (g_debugMode > 1) {
         PrintAndLogEx(DEBUG, "encRndB: %s", sprint_hex(encRndB, CRYPTO_AES_BLOCK_SIZE));
         PrintAndLogEx(DEBUG, "RndB: %s", sprint_hex(RndB, CRYPTO_AES_BLOCK_SIZE));
@@ -1211,7 +1211,7 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
         PrintAndLogEx(DEBUG, "rotRndB: %s", sprint_hex(rotRndB, CRYPTO_AES_BLOCK_SIZE));
         PrintAndLogEx(DEBUG, "Both: %s", sprint_hex(tmp, CRYPTO_AES_BLOCK_SIZE * 2));
     }
-    
+
     if (aes_encode(IV, key, tmp, both, CRYPTO_AES_BLOCK_SIZE * 2))
         return 6;
     if (g_debugMode > 1) {
@@ -1232,7 +1232,7 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
     }
 
     // Part 4
-    memcpy(encRndA, recv_data, CRYPTO_AES_BLOCK_SIZE);    
+    memcpy(encRndA, recv_data, CRYPTO_AES_BLOCK_SIZE);
 
     uint8_t data[32] = {0};
 
@@ -1249,7 +1249,7 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
         }
         return 11;
     }
-    
+
     if (firstauth) {
         dctx->cmdCntr = 0;
         memcpy(dctx->TI, data, 4);
@@ -1270,38 +1270,38 @@ static int DesfireAuthenticateEV2(DesfireContext *dctx, DesfireSecureChannel sec
         PrintAndLogEx(INFO, "session key ENC: %s", sprint_hex(dctx->sessionKeyEnc, 16));
         PrintAndLogEx(INFO, "session key MAC: %s", sprint_hex(dctx->sessionKeyMAC, 16));
     }
-    
+
     return PM3_SUCCESS;
 }
 
 static int DesfireAuthenticateISO(DesfireContext *dctx, DesfireSecureChannel secureChannel, bool verbose) {
     uint8_t rndlen = DesfireGetRndLenForKey(dctx->keyType);
-    
+
     uint8_t hostrnd[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
     uint8_t hostrnd2[] = {0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01};
-    
+
     uint8_t piccrnd[64];
     size_t xlen = 0;
     int res = DesfireISOGetChallenge(dctx, dctx->keyType, piccrnd, &xlen);
     if (res != PM3_SUCCESS)
         return 301;
-    
+
     if (xlen != rndlen)
         return 302;
-    
+
     uint8_t both[32] = {0};
     memcpy(both, hostrnd, rndlen);
     memcpy(&both[rndlen], piccrnd, rndlen);
-    
+
     // encode
     DesfireClearIV(dctx);
     DesfireCryptoEncDec(dctx, false, both, rndlen * 2, both, true); // error 303
-    
+
     // external authenticate
     res = DesfireISOExternalAuth(dctx, dctx->appSelected, dctx->keyNum, dctx->keyType, both);
     if (res != PM3_SUCCESS)
         return 304;
-    
+
     // internal authenticate
     uint8_t rnddata[64] = {0};
     xlen = 0;
@@ -1311,36 +1311,36 @@ static int DesfireAuthenticateISO(DesfireContext *dctx, DesfireSecureChannel sec
 
     if (xlen != rndlen * 2)
         return 306;
-    
+
     // decode rnddata
     uint8_t piccrnd2[64] = {0};
     DesfireCryptoEncDec(dctx, false, rnddata, rndlen * 2, piccrnd2, false); // error 307
-    
+
     // check
     if (memcmp(hostrnd2, &piccrnd2[rndlen], rndlen) != 0)
         return 308;
-        
+
     DesfireGenSessionKeyEV1(hostrnd, piccrnd2, dctx->keyType, dctx->sessionKeyEnc);
     DesfireClearIV(dctx);
     memcpy(dctx->sessionKeyMAC, dctx->sessionKeyEnc, desfire_get_key_length(dctx->keyType));
     dctx->secureChannel = secureChannel;
-    
+
     if (verbose)
         PrintAndLogEx(INFO, "session key: %s", sprint_hex(dctx->sessionKeyEnc, desfire_get_key_length(dctx->keyType)));
-    
+
     return PM3_SUCCESS;
 }
 
 int DesfireAuthenticate(DesfireContext *dctx, DesfireSecureChannel secureChannel, bool verbose) {
     if (dctx->cmdSet == DCCISO && secureChannel != DACEV2)
         return DesfireAuthenticateISO(dctx, secureChannel, verbose);
-    
+
     if (secureChannel == DACd40 || secureChannel == DACEV1)
         return DesfireAuthenticateEV1(dctx, secureChannel, verbose);
 
     if (secureChannel == DACEV2)
         return DesfireAuthenticateEV2(dctx, secureChannel, (DesfireIsAuthenticated(dctx) == false), verbose); // non first auth if there is a working secure channel
-    
+
     return 100;
 }
 
@@ -2227,15 +2227,15 @@ int DesfireISOSelect(DesfireContext *dctx, DesfireISOSelectControl cntr, uint8_t
     int res = DesfireExchangeISO(true, dctx, (sAPDU) {0x00, ISO7816_SELECT_FILE, cntr, ((resp == NULL) ? 0x0C : 0x00), datalen, data}, APDU_INCLUDE_LE_00, xresp, &xresplen, &sw);
     if (res == PM3_SUCCESS && sw != 0x9000)
         return PM3_ESOFT;
-    
+
     if (resp != NULL && resplen != NULL) {
         *resplen = xresplen;
         memcpy(resp, xresp, xresplen);
     }
-    
+
     DesfireClearSession(dctx);
-    dctx->appSelected = !( (cntr == ISSMFDFEF && datalen == 0) || (cntr == ISSEFByFileID && datalen == 2 && data[0] == 0 && data[1] == 0) );
-    
+    dctx->appSelected = !((cntr == ISSMFDFEF && datalen == 0) || (cntr == ISSEFByFileID && datalen == 2 && data[0] == 0 && data[1] == 0));
+
     return res;
 }
 
@@ -2248,22 +2248,22 @@ int DesfireISOGetChallenge(DesfireContext *dctx, DesfireCryptoAlgorythm keytype,
     int res = DesfireExchangeISO(false, dctx, (sAPDU) {0x00, ISO7816_GET_CHALLENGE, 0x00, 0x00, 0x00, NULL}, DesfireGetRndLenForKey(keytype), resp, resplen, &sw);
     if (res == PM3_SUCCESS && sw != 0x9000)
         return PM3_ESOFT;
-    
+
     return res;
 }
 
 int DesfireISOExternalAuth(DesfireContext *dctx, bool app_level, uint8_t keynum, DesfireCryptoAlgorythm keytype, uint8_t *data) {
     uint8_t p1 = DesfireKeyToISOKey(keytype);
     uint8_t p2 = ((app_level) ? 0x80 : 0x00) | keynum;
-    
+
     uint8_t resp[250] = {0};
     size_t resplen = 0;
-    
+
     uint16_t sw = 0;
     int res = DesfireExchangeISO(false, dctx, (sAPDU) {0x00, ISO7816_EXTERNAL_AUTHENTICATION, p1, p2, DesfireGetRndLenForKey(keytype) * 2, data}, 0, resp, &resplen, &sw);
     if (res == PM3_SUCCESS && sw != 0x9000)
         return PM3_ESOFT;
-    
+
     return res;
 }
 
@@ -2276,7 +2276,7 @@ int DesfireISOInternalAuth(DesfireContext *dctx, bool app_level, uint8_t keynum,
     int res = DesfireExchangeISO(false, dctx, (sAPDU) {0x00, ISO7816_INTERNAL_AUTHENTICATION, p1, p2, keylen, data}, keylen * 2, resp, resplen, &sw);
     if (res == PM3_SUCCESS && sw != 0x9000)
         return PM3_ESOFT;
-    
+
     return res;
 }
 
diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h
index 655995e5b..a149ba36c 100644
--- a/client/src/mifare/desfirecore.h
+++ b/client/src/mifare/desfirecore.h
@@ -23,9 +23,9 @@
 
 enum DesfireISOSelectControlEnum {
     ISSMFDFEF     = 0x00,
-    ISSChildDF    = 0x01, 
+    ISSChildDF    = 0x01,
     ISSEFByFileID = 0x02,
-    ISSParentDF   = 0x03, 
+    ISSParentDF   = 0x03,
     ISSDFName     = 0x04
 };
 typedef enum DesfireISOSelectControlEnum DesfireISOSelectControl;
diff --git a/client/src/mifare/desfirecrypto.c b/client/src/mifare/desfirecrypto.c
index e1670ada2..ba82cdae0 100644
--- a/client/src/mifare/desfirecrypto.c
+++ b/client/src/mifare/desfirecrypto.c
@@ -41,7 +41,7 @@ void DesfireClearContext(DesfireContext *ctx) {
     ctx->secureChannel = DACNone;
     ctx->cmdSet = DCCNative;
     ctx->commMode = DCMNone;
-    
+
     ctx->appSelected = false;
 
     ctx->kdfAlgo = 0;
@@ -224,7 +224,7 @@ void DesfireCryptoEncDecEx(DesfireContext *ctx, bool use_session_key, uint8_t *s
     if (ctx->secureChannel == DACd40) {
         memset(ctx->IV, 0, DESFIRE_MAX_CRYPTO_BLOCK_SIZE);
     }
-    
+
     size_t block_size = desfire_get_key_block_length(ctx->keyType);
 
     if (iv == NULL)
@@ -425,7 +425,7 @@ void DesfireGenSessionKeyEV1(const uint8_t rnda[], const uint8_t rndb[], Desfire
 void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool enckey, uint8_t *sessionkey) {
     uint8_t data[64] = {0};
     memset(sessionkey, 0, CRYPTO_AES_BLOCK_SIZE);
-    
+
     if (enckey) {
         data[0] = 0xa5;
         data[1] = 0x5a;
@@ -435,7 +435,7 @@ void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool en
     }
     data[3] = 0x01;
     data[5] = 0x80;
-    
+
     // data+6 - start of rnd part
     memcpy(data + 6, rndA, 8);
     bin_xor(data + 8, rndB, 6); // xor rndb 6b
@@ -453,7 +453,7 @@ void DesfireGenSessionKeyEV2(uint8_t *key, uint8_t *rndA, uint8_t *rndB, bool en
 
 void DesfireEV2FillIV(DesfireContext *ctx, bool ivforcommand, uint8_t *iv) {
     uint8_t xiv[CRYPTO_AES_BLOCK_SIZE] = {0};
-    
+
     if (ivforcommand) {
         xiv[0] = 0xa5;
         xiv[1] = 0x5a;
@@ -464,10 +464,10 @@ void DesfireEV2FillIV(DesfireContext *ctx, bool ivforcommand, uint8_t *iv) {
 
     memcpy(xiv + 2, ctx->TI, 4);
     Uint2byteToMemLe(xiv + 2 + 4, ctx->cmdCntr);
-    
+
     if (aes_encode(NULL, ctx->sessionKeyEnc, xiv, xiv, CRYPTO_AES_BLOCK_SIZE))
         return;
-    
+
     if (iv == NULL)
         memcpy(ctx->IV, xiv, CRYPTO_AES_BLOCK_SIZE);
     else
diff --git a/client/src/mifare/desfirecrypto.h b/client/src/mifare/desfirecrypto.h
index b24b8a3ac..a336f9e3f 100644
--- a/client/src/mifare/desfirecrypto.h
+++ b/client/src/mifare/desfirecrypto.h
@@ -75,7 +75,7 @@ typedef struct DesfireContextS {
     DesfireSecureChannel secureChannel; // none/d40/ev1/ev2
     DesfireCommandSet cmdSet;           // native/nativeiso/iso
     DesfireCommunicationMode commMode;  // plain/mac/enc
-    
+
     bool appSelected; // for iso auth
 
     uint8_t IV[DESFIRE_MAX_KEY_SIZE];
diff --git a/client/src/mifare/desfiresecurechan.c b/client/src/mifare/desfiresecurechan.c
index 586c95285..73aba69a6 100644
--- a/client/src/mifare/desfiresecurechan.c
+++ b/client/src/mifare/desfiresecurechan.c
@@ -117,7 +117,7 @@ static const AllowedChannelModesS AllowedChannelModes[] = {
 
     {MFDES_CHANGE_KEY,                DACEV1,  DCCNative,    DCMEncryptedPlain},
     {MFDES_CHANGE_KEY_EV2,            DACEV1,  DCCNative,    DCMEncryptedPlain},
-    
+
     {MFDES_AUTHENTICATE_EV2F,         DACEV2,  DCCNative,    DCMPlain},
     {MFDES_AUTHENTICATE_EV2NF,        DACEV2,  DCCNative,    DCMPlain},
 };
@@ -246,7 +246,7 @@ static void DesfireSecureChannelEncodeEV1(DesfireContext *ctx, uint8_t cmd, uint
 static void DesfireSecureChannelEncodeEV2(DesfireContext *ctx, uint8_t cmd, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, size_t *dstdatalen) {
     memcpy(dstdata, srcdata, srcdatalen);
     *dstdatalen = srcdatalen;
-    
+
 }
 
 void DesfireSecureChannelEncode(DesfireContext *ctx, uint8_t cmd, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, size_t *dstdatalen) {
diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c
index 5de1b312d..c08d7600d 100644
--- a/client/src/mifare/desfiretest.c
+++ b/client/src/mifare/desfiretest.c
@@ -216,7 +216,7 @@ static bool TestEV2SessionKeys(void) {
     uint8_t rndb[] = {0xFA, 0x65, 0x9A, 0xD0, 0xDC, 0xA7, 0x38, 0xDD, 0x65, 0xDC, 0x7D, 0xC3, 0x86, 0x12, 0xAD, 0x81};
     uint8_t sessionkeyauth[] = {0x63, 0xDC, 0x07, 0x28, 0x62, 0x89, 0xA7, 0xA6, 0xC0, 0x33, 0x4C, 0xA3, 0x1C, 0x31, 0x4A, 0x04};
     uint8_t sessionkeymac[] = {0x77, 0x4F, 0x26, 0x74, 0x3E, 0xCE, 0x6A, 0xF5, 0x03, 0x3B, 0x6A, 0xE8, 0x52, 0x29, 0x46, 0xF6};
-    
+
     uint8_t sessionkey[16] = {0};
     DesfireGenSessionKeyEV2(key, rnda, rndb, true, sessionkey);
     res = res && (memcmp(sessionkey, sessionkeyauth, sizeof(sessionkeyauth)) == 0);
@@ -224,7 +224,7 @@ static bool TestEV2SessionKeys(void) {
     memset(sessionkey, 0, sizeof(sessionkey));
     DesfireGenSessionKeyEV2(key, rnda, rndb, false, sessionkey);
     res = res && (memcmp(sessionkey, sessionkeymac, sizeof(sessionkeymac)) == 0);
-    
+
     if (res)
         PrintAndLogEx(INFO, "EV2 session keys.. " _GREEN_("passed"));
     else
@@ -239,17 +239,17 @@ static bool TestEV2IVEncode(void) {
     uint8_t key[] = {0x66, 0xA8, 0xCB, 0x93, 0x26, 0x9D, 0xC9, 0xBC, 0x28, 0x85, 0xB7, 0xA9, 0x1B, 0x9C, 0x69, 0x7B};
     uint8_t ti[] = {0xED, 0x56, 0xF6, 0xE6};
     uint8_t ivres[] = {0xDA, 0x0F, 0x64, 0x4A, 0x49, 0x86, 0x27, 0x59, 0x57, 0xCF, 0x1E, 0xC3, 0xAF, 0x4C, 0xCE, 0x53};
-    
+
     DesfireContext ctx = {0};
     ctx.keyType = T_AES;
     memcpy(ctx.sessionKeyEnc, key, 16);
     memcpy(ctx.TI, ti, 4);
     ctx.cmdCntr = 0;
-    
+
     uint8_t iv[16] = {0};
     DesfireEV2FillIV(&ctx, true, iv);
     res = res && (memcmp(iv, ivres, sizeof(ivres)) == 0);
-    
+
     uint8_t key2[] = {0x44, 0x5A, 0x86, 0x26, 0xB3, 0x33, 0x84, 0x59, 0x32, 0x12, 0x32, 0xfA, 0xDf, 0x6a, 0xDe, 0x2B};
     uint8_t ti2[] = {0x11, 0x22, 0x33, 0x44};
     uint8_t ivres2[] = {0x17, 0x74, 0x94, 0xFC, 0xC4, 0xF1, 0xDA, 0xB2, 0xAF, 0xBE, 0x8F, 0xAE, 0x20, 0x57, 0xA9, 0xD2};
diff --git a/doc/commands.json b/doc/commands.json
index 689d90c49..103ead0b0 100644
--- a/doc/commands.json
+++ b/doc/commands.json
@@ -973,7 +973,7 @@
         },
         "help": {
             "command": "help",
-            "description": "help use `<command> help` for details of a command prefs { edit client/device preferences... } -------- ----------------------- technology ----------------------- analyse { analyse utils... } data { plot window / data buffer manipulation... } emv { emv iso-14443 / iso-7816... } hf { high frequency commands... } hw { hardware commands... } lf { low frequency commands... } nfc { nfc commands... } reveng { crc calculations from reveng software... } smart { smart card iso-7816 commands... } script { scripting commands... } trace { trace manipulation... } wiegand { wiegand format manipulation... } -------- ----------------------- general ----------------------- clear clear screen hints turn hints on / off msleep add a pause in milliseconds rem add a text line in log file quit exit exit program [=] session log e:\\proxspace\\pm3/.proxmark3/logs/log_20210729.txt --------------------------------------------------------------------------------------- auto available offline: no run lf search / hf search / data plot / data save",
+            "description": "help use `<command> help` for details of a command prefs { edit client/device preferences... } -------- ----------------------- technology ----------------------- analyse { analyse utils... } data { plot window / data buffer manipulation... } emv { emv iso-14443 / iso-7816... } hf { high frequency commands... } hw { hardware commands... } lf { low frequency commands... } nfc { nfc commands... } reveng { crc calculations from reveng software... } smart { smart card iso-7816 commands... } script { scripting commands... } trace { trace manipulation... } wiegand { wiegand format manipulation... } -------- ----------------------- general ----------------------- clear clear screen hints turn hints on / off msleep add a pause in milliseconds rem add a text line in log file quit exit exit program [=] session log e:\\proxspace\\pm3/.proxmark3/logs/log_20210731.txt --------------------------------------------------------------------------------------- auto available offline: no run lf search / hf search / data plot / data save",
             "notes": [
                 "auto"
             ],
@@ -3944,32 +3944,34 @@
         },
         "hf mfdes auth": {
             "command": "hf mfdes auth",
-            "description": "authenticates mifare desfire using key",
+            "description": "select application on the card. it selects app if it is a valid one or returns an error.",
             "notes": [
-                "hf mfdes auth -m 3 -t 4 -a 808301 -n 0 -k 00000000000000000000000000000000 -> aes,keynumber 0, aid 0x803201",
-                "hf mfdes auth -m 2 -t 2 -a 000000 -n 1 -k 00000000000000000000000000000000 -> 3des,keynumber 1, aid 0x000000",
-                "hf mfdes auth -m 1 -t 1 -a 000000 -n 2 -k 0000000000000000 -> des,keynumber 2, aid 0x000000",
-                "hf mfdes auth -m 1 -t 1 -a 000000 -n 0 -> des, defaultkey, aid 0x000000",
-                "hf mfdes auth -m 2 -t 2 -a 000000 -n 0 -> 3des, defaultkey, aid 0x000000",
-                "hf mfdes auth -m 3 -t 4 -a 000000 -n 0 -> 3k3des, defaultkey, aid 0x000000",
-                "hf mfdes auth -m 3 -t 4 -a 000000 -n 0 -> aes, defaultkey, aid 0x000000"
+                "hf mfdes auth -n 0 -t des -k 0000000000000000 -f none -> select picc level and authenticate with key num=0, key type=des, key=00..00 and key derivation = none",
+                "hf mfdes auth -n 0 -t aes -k 00000000000000000000000000000000 -> select picc level and authenticate with key num=0, key type=aes, key=00..00 and key derivation = none",
+                "hf mfdes auth -n 0 -t des -k 0000000000000000 --save -> select picc level and authenticate and in case of successful authentication - save channel parameters to defaults",
+                "hf mfdes auth --aid 123456 -> select application 123456 and authenticate via parameters from `default` command"
             ],
             "offline": false,
             "options": [
                 "-h, --help this help",
-                "-m, --type <type> auth type (1=normal, 2=iso, 3=aes)",
-                "-t, --algo <algo> crypt algo (1=des, 2=3des(2k2des), 3=3k3des, 4=aes)",
-                "-a, --aid <aid> aid used for authentification (hex 3 bytes)",
-                "-n, --keyno <keyno> key number used for authentification",
-                "-k, --key <key> key for checking (hex 8-24 bytes)",
-                "-d, --kdf <kdf> key derivation function (kdf) (0=none, 1=an10922, 2=gallagher)",
-                "-i, --kdfi <kdfi> kdf input (hex 1-31 bytes)"
+                "-a, --apdu show apdu requests and responses",
+                "-v, --verbose show technical data",
+                "-n, --keyno <keyno> key number",
+                "-t, --algo <des/2tdea/3tdea/aes> crypt algo: des, 2tdea, 3tdea, aes",
+                "-k, --key <key> key for authenticate (hex 8(des), 16(2tdea or aes) or 24(3tdea) bytes)",
+                "-f, --kdf <none/an10922/gallagher> key derivation function (kdf): none, an10922, gallagher",
+                "-i, --kdfi <kdfi> kdf input (hex 1-31 bytes)",
+                "-m, --cmode <plain/mac/encrypt> communicaton mode: plain/mac/encrypt",
+                "-c, --ccset <native/niso/iso> communicaton command set: native/niso/iso",
+                "-s, --schann <d40/ev1/ev2> secure channel: d40/ev1/ev2",
+                "--aid <app id hex> application id of application for some parameters (3 hex bytes, big endian)",
+                "--save saves channels parameters to defaults if authentication succeeds"
             ],
-            "usage": "hf mfdes auth [-h] [-m <type>] [-t <algo>] [-a <aid>]... [-n <keyno>] [-k <key>] [-d <kdf>] [-i <kdfi>]"
+            "usage": "hf mfdes auth [-hav] [-n <keyno>] [-t <des/2tdea/3tdea/aes>] [-k <key>] [-f <none/an10922/gallagher>] [-i <kdfi>] [-m <plain/mac/encrypt>] [-c <native/niso/iso>] [-s <d40/ev1/ev2>] [--aid <app id hex>] [--save]"
         },
         "hf mfdes bruteaid": {
             "command": "hf mfdes bruteaid",
-            "description": "recover aids by bruteforce. warning: this command takes a long time",
+            "description": "recover aids by bruteforce. warning: this command takes a loooong time",
             "notes": [
                 "hf mfdes bruteaid -> search all apps",
                 "hf mfdes bruteaid -s f0000f -i 16 -> search mad range manually"
@@ -4771,18 +4773,29 @@
             ],
             "usage": "hf mfdes read [-hav] [-n <keyno>] [-t <des/2tdea/3tdea/aes>] [-k <key>] [-f <none/an10922/gallagher>] [-i <kdfi>] [-m <plain/mac/encrypt>] [-c <native/niso/iso>] [-s <d40/ev1/ev2>] [--aid <app id hex>] [--fid <file id hex>] [--no-auth] [--type <auto/data/value/record/mac>] [-o <hex>] [-l <hex>]"
         },
-        "hf mfdes selectaid": {
-            "command": "hf mfdes selectaid",
-            "description": "select application id",
+        "hf mfdes selectapp": {
+            "command": "hf mfdes selectapp",
+            "description": "select application on the card. it selects app if it is a valid one or returns an error.",
             "notes": [
-                "hf mfdes selectaid -a 123456"
+                "hf mfdes selectapp --aid 123456 -> select application 123456"
             ],
             "offline": false,
             "options": [
                 "-h, --help this help",
-                "-a, --aid <hex> app id to select as hex bytes (3 bytes, big endian)"
+                "-a, --apdu show apdu requests and responses",
+                "-v, --verbose show technical data",
+                "-n, --keyno <keyno> key number",
+                "-t, --algo <des/2tdea/3tdea/aes> crypt algo: des, 2tdea, 3tdea, aes",
+                "-k, --key <key> key for authenticate (hex 8(des), 16(2tdea or aes) or 24(3tdea) bytes)",
+                "-f, --kdf <none/an10922/gallagher> key derivation function (kdf): none, an10922, gallagher",
+                "-i, --kdfi <kdfi> kdf input (hex 1-31 bytes)",
+                "-m, --cmode <plain/mac/encrypt> communicaton mode: plain/mac/encrypt",
+                "-c, --ccset <native/niso/iso> communicaton command set: native/niso/iso",
+                "-s, --schann <d40/ev1/ev2> secure channel: d40/ev1/ev2",
+                "--aid <app id hex> application id of application for some parameters (3 hex bytes, big endian)",
+                "--dfname <df name str> application df name (string, max 16 chars). selects application via iso select command"
             ],
-            "usage": "hf mfdes selectaid [-h] [-a <hex>]..."
+            "usage": "hf mfdes selectapp [-hav] [-n <keyno>] [-t <des/2tdea/3tdea/aes>] [-k <key>] [-f <none/an10922/gallagher>] [-i <kdfi>] [-m <plain/mac/encrypt>] [-c <native/niso/iso>] [-s <d40/ev1/ev2>] [--aid <app id hex>] [--dfname <df name str>]"
         },
         "hf mfdes setconfig": {
             "command": "hf mfdes setconfig",
@@ -4812,7 +4825,7 @@
         },
         "hf mfdes test": {
             "command": "hf mfdes test",
-            "description": "[=] ------ desfire tests ------ [!] no space for crc. pos: 1 [=] crc16............ passed [!] no space for crc. pos: 2 [=] crc32............ passed [=] cmac 3tdea....... passed [=] cmac 2tdea....... passed [=] cmac des......... passed [=] --------------------------- [+] tests [ ok ] ======================================================================================= hf seos { seos rfids... } --------------------------------------------------------------------------------------- hf seos help available offline: yes help this help list list seos history --------------------------------------------------------------------------------------- hf seos info available offline: no get info from seos tags",
+            "description": "[=] ------ desfire tests ------ [!] no space for crc. pos: 1 [=] crc16............. passed [!] no space for crc. pos: 2 [=] crc32............. passed [=] cmac 3tdea........ passed [=] cmac 2tdea........ passed [=] cmac des.......... passed [=] ev2 session keys.. passed [=] ev2 iv calc....... passed [=] --------------------------- [+] tests [ ok ] ======================================================================================= hf seos { seos rfids... } --------------------------------------------------------------------------------------- hf seos help available offline: yes help this help list list seos history --------------------------------------------------------------------------------------- hf seos info available offline: no get info from seos tags",
             "notes": [
                 "hf seos info"
             ],
@@ -9826,6 +9839,6 @@
     "metadata": {
         "commands_extracted": 587,
         "extracted_by": "PM3Help2JSON v1.00",
-        "extracted_on": "2021-07-29T15:18:59"
+        "extracted_on": "2021-07-31T13:44:52"
     }
 }
\ No newline at end of file
diff --git a/doc/commands.md b/doc/commands.md
index 3796bfd98..830fab232 100644
--- a/doc/commands.md
+++ b/doc/commands.md
@@ -503,41 +503,41 @@ Check column "offline" for their availability.
 |command                  |offline |description
 |-------                  |------- |-----------
 |`hf mfdes help          `|Y       |`This help`
-|`hf mfdes default       `|N       |`[new]Set defaults for all the commands`
-|`hf mfdes auth          `|N       |`Tries a MIFARE DesFire Authentication`
-|`hf mfdes chk           `|N       |`Check keys`
-|`hf mfdes enum          `|N       |`Tries enumerate all applications`
-|`hf mfdes formatpicc    `|N       |`[new]Format PICC`
-|`hf mfdes freemem       `|N       |`[new]Get free memory size`
-|`hf mfdes getuid        `|N       |`[new]Get uid from card`
-|`hf mfdes setconfig     `|N       |`[new]Set card configuration`
-|`hf mfdes info          `|N       |`Tag information`
+|`hf mfdes default       `|N       |`Set defaults for all the commands`
+|`hf mfdes auth          `|N       |`MIFARE DesFire Authentication`
+|`hf mfdes chk           `|N       |`[old]Check keys`
+|`hf mfdes enum          `|N       |`[old]Tries enumerate all applications`
+|`hf mfdes formatpicc    `|N       |`Format PICC`
+|`hf mfdes freemem       `|N       |`Get free memory size`
+|`hf mfdes getuid        `|N       |`Get uid from card`
+|`hf mfdes setconfig     `|N       |`Set card configuration`
+|`hf mfdes info          `|N       |`[old]Tag information`
 |`hf mfdes list          `|Y       |`List DESFire (ISO 14443A) history`
-|`hf mfdes changekey     `|N       |`[new]Change Key`
-|`hf mfdes chkeysettings `|N       |`[new]Change Key Settings`
-|`hf mfdes getkeysettings`|N       |`[new]Get Key Settings`
-|`hf mfdes getkeyversions`|N       |`[new]Get Key Versions`
+|`hf mfdes changekey     `|N       |`Change Key`
+|`hf mfdes chkeysettings `|N       |`Change Key Settings`
+|`hf mfdes getkeysettings`|N       |`Get Key Settings`
+|`hf mfdes getkeyversions`|N       |`Get Key Versions`
 |`hf mfdes bruteaid      `|N       |`Recover AIDs by bruteforce`
-|`hf mfdes createapp     `|N       |`[new]Create Application`
-|`hf mfdes deleteapp     `|N       |`[new]Delete Application`
-|`hf mfdes selectaid     `|N       |`Select Application ID`
-|`hf mfdes getaids       `|N       |`[new]Get Application IDs list`
-|`hf mfdes getappnames   `|N       |`[new]Get Applications list`
-|`hf mfdes getfileids    `|N       |`[new]Get File IDs list`
-|`hf mfdes getfileisoids `|N       |`[new]Get File ISO IDs list`
-|`hf mfdes lsfiles       `|N       |`[new]Show all files list`
-|`hf mfdes dump          `|N       |`[new]Dump all files`
-|`hf mfdes createfile    `|N       |`[new]Create Standard/Backup File`
-|`hf mfdes createvaluefile`|N       |`[new]Create Value File`
-|`hf mfdes createrecordfile`|N       |`[new]Create Linear/Cyclic Record File`
-|`hf mfdes createmacfile `|N       |`[new]Create Transaction MAC File`
-|`hf mfdes deletefile    `|N       |`[new]Delete File`
-|`hf mfdes getfilesettings`|N       |`[new]Get file settings`
-|`hf mfdes chfilesettings`|N       |`[new]Change file settings`
-|`hf mfdes read          `|N       |`[new]Read data from standard/backup/record/value/mac file`
-|`hf mfdes write         `|N       |`[new]Write data to standard/backup/record/value file`
-|`hf mfdes value         `|N       |`[new]Operations with value file (get/credit/limited credit/debit/clear)`
-|`hf mfdes clearrecfile  `|N       |`[new]Clear record File`
+|`hf mfdes createapp     `|N       |`Create Application`
+|`hf mfdes deleteapp     `|N       |`Delete Application`
+|`hf mfdes selectapp     `|N       |`Select Application ID`
+|`hf mfdes getaids       `|N       |`Get Application IDs list`
+|`hf mfdes getappnames   `|N       |`Get Applications list`
+|`hf mfdes getfileids    `|N       |`Get File IDs list`
+|`hf mfdes getfileisoids `|N       |`Get File ISO IDs list`
+|`hf mfdes lsfiles       `|N       |`Show all files list`
+|`hf mfdes dump          `|N       |`Dump all files`
+|`hf mfdes createfile    `|N       |`Create Standard/Backup File`
+|`hf mfdes createvaluefile`|N       |`Create Value File`
+|`hf mfdes createrecordfile`|N       |`Create Linear/Cyclic Record File`
+|`hf mfdes createmacfile `|N       |`Create Transaction MAC File`
+|`hf mfdes deletefile    `|N       |`Delete File`
+|`hf mfdes getfilesettings`|N       |`Get file settings`
+|`hf mfdes chfilesettings`|N       |`Change file settings`
+|`hf mfdes read          `|N       |`Read data from standard/backup/record/value/mac file`
+|`hf mfdes write         `|N       |`Write data to standard/backup/record/value file`
+|`hf mfdes value         `|N       |`Operations with value file (get/credit/limited credit/debit/clear)`
+|`hf mfdes clearrecfile  `|N       |`Clear record File`
 |`hf mfdes test          `|Y       |`Test crypto`