From 6124b49c9913e9b63ce451147553c1f1b4dd93c7 Mon Sep 17 00:00:00 2001
From: merlokk <807634+merlokk@users.noreply.github.com>
Date: Sun, 4 Jul 2021 12:47:09 +0300
Subject: [PATCH] DesfirePrintContext prints session state

---
 client/src/cmdhfmfdes.c            |  2 +-
 client/src/mifare/desfire_crypto.c | 14 +++++++++-----
 client/src/mifare/desfire_crypto.h |  1 +
 client/src/mifare/desfirecore.c    | 28 +++++++++++++++++++++++++---
 4 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c
index 1a3c82166..c7a195db5 100644
--- a/client/src/cmdhfmfdes.c
+++ b/client/src/cmdhfmfdes.c
@@ -5195,7 +5195,7 @@ static int CmdHF14ADesGetAIDs(const char *Cmd) {
         DropField();
         return PM3_ESOFT;
     }
-    
+  
     if (DesfireIsAuthenticated(&dctx)) {
         if (verbose)
             PrintAndLogEx(ERR, "Desfire  " _GREEN_("authenticated") , res);
diff --git a/client/src/mifare/desfire_crypto.c b/client/src/mifare/desfire_crypto.c
index 43c0ae3bf..079e7c897 100644
--- a/client/src/mifare/desfire_crypto.c
+++ b/client/src/mifare/desfire_crypto.c
@@ -390,12 +390,9 @@ void mifare_kdf_an10922(const desfirekey_t key, const uint8_t *data, size_t len)
     free(buffer);
 }
 
-size_t key_block_size(const desfirekey_t key) {
-    if (key == NULL) {
-        return 0;
-    }
+size_t desfire_get_key_block_length(enum DESFIRE_CRYPTOALGO key_type) {
     size_t block_size = 8;
-    switch (key->type) {
+    switch (key_type) {
         case T_DES:
         case T_3DES:
         case T_3K3DES:
@@ -408,6 +405,13 @@ size_t key_block_size(const desfirekey_t key) {
     return block_size;
 }
 
+size_t key_block_size(const desfirekey_t key) {
+    if (key == NULL) {
+        return 0;
+    }
+    return desfire_get_key_block_length(key->type);
+}
+
 size_t key_size(const enum DESFIRE_CRYPTOALGO algo) {
     switch (algo) {
         case T_DES:
diff --git a/client/src/mifare/desfire_crypto.h b/client/src/mifare/desfire_crypto.h
index 281b33978..1a1f6375c 100644
--- a/client/src/mifare/desfire_crypto.h
+++ b/client/src/mifare/desfire_crypto.h
@@ -82,6 +82,7 @@ enum DESFIRE_CRYPTOALGO {
 };
 
 int desfire_get_key_length(enum DESFIRE_CRYPTOALGO key_type);
+size_t desfire_get_key_block_length(enum DESFIRE_CRYPTOALGO key_type);
 
 enum DESFIRE_AUTH_SCHEME {
     AS_LEGACY,
diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c
index bab818a39..6fead2fdb 100644
--- a/client/src/mifare/desfirecore.c
+++ b/client/src/mifare/desfirecore.c
@@ -227,9 +227,31 @@ void DesfireClearSession(DesfireContext *ctx) {
 }
 
 void DesfirePrintContext(DesfireContext *ctx) {
-    //PrintAndLogEx(INFO, "algo: %s", CLIGetOptionListStr(algo_opts, ARRAY_LENGTH(algo_opts), algores));
-    PrintAndLogEx(INFO, "Key num: %d Key algo: %s Key[%d]: %s", ctx->keyNum, CLIGetOptionListStr(DesfireAlgoOpts, ctx->keyType), key_size(ctx->keyType), sprint_hex(ctx->key, key_size(ctx->keyType)));
-}
+    PrintAndLogEx(INFO, "Key num: %d Key algo: %s Key[%d]: %s", 
+                        ctx->keyNum, 
+                        CLIGetOptionListStr(DesfireAlgoOpts, ctx->keyType), 
+                        key_size(ctx->keyType), 
+                        sprint_hex(ctx->key, 
+                        key_size(ctx->keyType)));
+                        
+    if (ctx->kdfAlgo != MFDES_KDF_ALGO_NONE)
+        PrintAndLogEx(INFO, "KDF algo: %s KDF input[%d]: %s", CLIGetOptionListStr(DesfireKDFAlgoOpts, ctx->kdfAlgo), ctx->kdfInputLen, sprint_hex(ctx->kdfInput, ctx->kdfInputLen));
+    
+    PrintAndLogEx(INFO, "Secure channel: %s Command set: %s Communication mode: %s", 
+                        CLIGetOptionListStr(DesfireSecureChannelOpts, ctx->secureChannel),
+                        CLIGetOptionListStr(DesfireCommandSetOpts, ctx->cmdSet),
+                        CLIGetOptionListStr(DesfireCommunicationModeOpts, ctx->commMode));
+                        
+    if (DesfireIsAuthenticated(ctx)) {
+        PrintAndLogEx(INFO, "Session key MAC [%d]: %s ENC: %s IV [%d]: %s", 
+                            key_size(ctx->keyType), 
+                            sprint_hex(ctx->sessionKeyMAC, key_size(ctx->keyType)), 
+                            sprint_hex(ctx->sessionKeyEnc, key_size(ctx->keyType)),
+                            desfire_get_key_block_length(ctx->keyType),
+                            sprint_hex(ctx->sessionKeyEnc, desfire_get_key_block_length(ctx->keyType)));
+        
+    }
+ }
 
 void DesfireSetKey(DesfireContext *ctx, uint8_t keyNum, enum DESFIRE_CRYPTOALGO keyType, uint8_t *key) {
     DesfireClearContext(ctx);