From 20a3bca52476b2c1b6f1b3b59885e97be165a407 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 14 Aug 2021 13:24:59 +0300 Subject: [PATCH] LRPDecode --- client/src/mifare/desfiretest.c | 65 +++++++++++++++++++++++++++++++++ client/src/mifare/lrpcrypto.c | 24 ++++++++++++ client/src/mifare/lrpcrypto.h | 1 + 3 files changed, 90 insertions(+) diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c index bd6ba8945..c615b70c3 100644 --- a/client/src/mifare/desfiretest.c +++ b/client/src/mifare/desfiretest.c @@ -682,6 +682,70 @@ static bool TestLRPEncode(void) { return res; } +static bool TestLRPDecode(void) { + bool res = true; + + uint8_t resp[100] = {0}; + size_t resplen = 0; + + LRPContext ctx = {0}; + + uint8_t key1[] = {0xE0, 0xC4, 0x93, 0x5F, 0xF0, 0xC2, 0x54, 0xCD, 0x2C, 0xEF, 0x8F, 0xDD, 0xC3, 0x24, 0x60, 0xCF}; + uint8_t iv1[] = {0xC3, 0x31, 0x5D, 0xBF}; + LRPSetKeyEx(&ctx, key1, iv1, sizeof(iv1) * 2, 0, true); + uint8_t data1[] = {0xFC, 0xBB, 0xAC, 0xAA, 0x4F, 0x29, 0x18, 0x24, 0x64, 0xF9, 0x9D, 0xE4, 0x10, 0x85, 0x26, 0x6F, + 0x48, 0x0E, 0x86, 0x3E, 0x48, 0x7B, 0xAA, 0xF6, 0x87, 0xB4, 0x3E, 0xD1, 0xEC, 0xE0, 0xD6, 0x23}; + LRPDecode(&ctx, data1, sizeof (data1), resp, &resplen); + uint8_t res1[] = {0x01, 0x2D, 0x7F, 0x16, 0x53, 0xCA, 0xF6, 0x50, 0x3C, 0x6A, 0xB0, 0xC1, 0x01, 0x0E, 0x8C, 0xB0}; + res = res && (resplen == sizeof(res1)); + res = res && (memcmp(resp, res1, sizeof(res1)) == 0); + + uint8_t key2[] = {0xEF, 0xA5, 0xB7, 0x42, 0x9C, 0xD1, 0x53, 0xBF, 0x00, 0x86, 0xDE, 0xF9, 0x00, 0xC0, 0xF2, 0x35}; + uint8_t iv2[] = {0x90, 0x36, 0xFF, 0xFF}; + LRPSetKeyEx(&ctx, key2, iv2, sizeof(iv2) * 2, 0, false); + uint8_t data2[] = {0xEA, 0x6E, 0x09, 0xAC, 0x2F, 0xB9, 0x7E, 0x10, 0x2D, 0x8C, 0xA6, 0x4C, 0x1C, 0xBC, 0x0C, 0x0C}; + LRPDecode(&ctx, data2, sizeof (data2), resp, &resplen); + uint8_t res2[] = {0xE7, 0xF6, 0x1E, 0x01, 0x2F, 0x4F, 0x32, 0x55, 0x31, 0x2B, 0xA6, 0x8B, 0x1D, 0x2F, 0xDA, 0xBF}; + res = res && (resplen == sizeof(res2)); + res = res && (memcmp(resp, res2, sizeof(res2)) == 0); + + uint8_t key3[] = {0x9D, 0x81, 0x31, 0x34, 0xCF, 0xDE, 0xE9, 0xD5, 0x87, 0x55, 0xDE, 0xAC, 0xD4, 0xAF, 0x72, 0xA7}; + uint8_t iv3[] = {0xFF, 0xFF, 0xFF, 0xFF}; + LRPSetKeyEx(&ctx, key3, iv3, sizeof(iv3) * 2, 0, true); + uint8_t data3[] = {0xF5, 0x83, 0x3F, 0xC3, 0x97, 0x35, 0x6E, 0xA3, 0xD9, 0xEC, 0xAD, 0xBB, 0x9F, 0x6F, 0xE4, 0x40}; + LRPDecode(&ctx, data3, sizeof (data3), resp, &resplen); + uint8_t res3[] = {0x27}; + res = res && (resplen == sizeof(res3)); + res = res && (memcmp(resp, res3, sizeof(res3)) == 0); + + uint8_t key4[] = {0xF5, 0xC3, 0xE9, 0x9F, 0xB7, 0x5E, 0x31, 0x6B, 0x76, 0x68, 0x9F, 0xC5, 0x46, 0x42, 0x60, 0xCD}; + uint8_t iv4[] = {0x07, 0x97, 0xF6, 0xB7}; + LRPSetKeyEx(&ctx, key4, iv4, sizeof(iv4) * 2, 0, true); + uint8_t data4[] = {0x93, 0xDC, 0x3E, 0xE1, 0x4B, 0x61, 0x2B, 0xE6, 0xA3, 0xE9, 0xE2, 0xE8, 0x04, 0x0C, 0xDF, 0xCB}; + LRPDecode(&ctx, data4, sizeof(data4), resp, &resplen); + res = res && (resplen == 0); + + uint8_t key5[] = {0x9B, 0x1E, 0x41, 0x8D, 0xF9, 0x75, 0x2F, 0x37, 0xEB, 0xBD, 0x8E, 0xE8, 0x33, 0xBD, 0xF2, 0xD7}; + uint8_t iv5[] = {0x24, 0xFF, 0xFF, 0xFF}; + LRPSetKeyEx(&ctx, key5, iv5, sizeof(iv5) * 2, 0, true); + uint8_t data5[] = {0x15, 0x8B, 0x3B, 0x9C, 0x61, 0x36, 0xFB, 0x71, 0x5C, 0xCF, 0x43, 0x5C, 0xA4, 0xCA, 0xDE, 0x80, + 0x8D, 0x1F, 0x98, 0x43, 0x13, 0x27, 0x06, 0x1A, 0x9A, 0x64, 0xD5, 0x2A, 0x5F, 0xE7, 0xB2, 0x74, + 0x6D, 0x7F, 0x5A, 0x63, 0x3F, 0xC0, 0xCF, 0xE7, 0x85, 0x56, 0x56, 0xAD, 0x3C, 0x6B, 0x94, 0xCF}; + LRPDecode(&ctx, data5, sizeof (data5), resp, &resplen); + uint8_t res5[] = {0x55, 0x53, 0x4E, 0x15, 0x9F, 0x14, 0xDD, 0x77, 0x31, 0x36, 0x89, 0x88, 0xEE, 0x6D, 0xD7, 0xC6, + 0x11, 0x4E, 0x74, 0x7F, 0x9C, 0x17, 0xA9, 0x1B, 0xBC, 0x12, 0xD6, 0x8C, 0x26, 0x53, 0x1F, 0x2F, + 0xFC, 0xFC}; + res = res && (resplen == sizeof(res5)); + res = res && (memcmp(resp, res5, sizeof(res5)) == 0); + + if (res) + PrintAndLogEx(INFO, "LRP decode........ " _GREEN_("passed")); + else + PrintAndLogEx(ERR, "LRP decode........ " _RED_("fail")); + + return res; +} + bool DesfireTest(bool verbose) { bool res = true; @@ -705,6 +769,7 @@ bool DesfireTest(bool verbose) { res = res && TestLRPEval(); res = res && TestLRPIncCounter(); res = res && TestLRPEncode(); + res = res && TestLRPDecode(); PrintAndLogEx(INFO, "---------------------------"); if (res) diff --git a/client/src/mifare/lrpcrypto.c b/client/src/mifare/lrpcrypto.c index bff85d289..5f5432487 100644 --- a/client/src/mifare/lrpcrypto.c +++ b/client/src/mifare/lrpcrypto.c @@ -157,3 +157,27 @@ void LRPEncode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, si LRPIncCounter(ctx->counter, ctx->counterLenNibbles); } } + +void LRPDecode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen) { + *resplen = 0; + if (datalen % CRYPTO_AES128_KEY_SIZE) + return; + + uint8_t y[CRYPTO_AES128_KEY_SIZE] = {0}; + for (int i = 0; i < datalen / CRYPTO_AES128_KEY_SIZE; i++) { + LRPEvalLRP(ctx, ctx->counter, ctx->counterLenNibbles, true, y); + aes_decode(NULL, y, &data[i * CRYPTO_AES128_KEY_SIZE], &resp[i * CRYPTO_AES128_KEY_SIZE], CRYPTO_AES128_KEY_SIZE); + *resplen += CRYPTO_AES128_KEY_SIZE; + LRPIncCounter(ctx->counter, ctx->counterLenNibbles); + } + + // search padding + if (ctx->useBitPadding) { + for (int i = *resplen - 1; i >= *resplen - CRYPTO_AES128_KEY_SIZE; i--) { + if (resp[i] == 0x80) + *resplen = i; + if (resp[i] != 0x00) + break; + } + } +} diff --git a/client/src/mifare/lrpcrypto.h b/client/src/mifare/lrpcrypto.h index 17e0858f0..cccf72437 100644 --- a/client/src/mifare/lrpcrypto.h +++ b/client/src/mifare/lrpcrypto.h @@ -50,5 +50,6 @@ void LRPGenerateUpdatedKeys(LRPContext *ctx, size_t updatedKeysCount); void LRPEvalLRP(LRPContext *ctx, uint8_t *iv, size_t ivlen, bool final, uint8_t *y); void LRPIncCounter(uint8_t *ctr, size_t ctrlen); void LRPEncode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen); +void LRPDecode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen); #endif // __LRPCRYPTO_H