From 7ce190593c54a0ed253e469aff6815d38808b0ef Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Mon, 16 Aug 2021 17:08:33 +0300 Subject: [PATCH] make style --- client/src/mifare/desfiretest.c | 100 +++++++++++++++++--------------- client/src/mifare/lrpcrypto.c | 50 ++++++++-------- client/src/mifare/lrpcrypto.h | 4 +- doc/commands.json | 8 +-- 4 files changed, 85 insertions(+), 77 deletions(-) diff --git a/client/src/mifare/desfiretest.c b/client/src/mifare/desfiretest.c index 1e91d5586..8ebb66428 100644 --- a/client/src/mifare/desfiretest.c +++ b/client/src/mifare/desfiretest.c @@ -482,11 +482,11 @@ static bool TestTransSessionKeys(void) { // page 10 static bool TestLRPPlaintexts(void) { bool res = true; - + uint8_t key[] = {0x56, 0x78, 0x26, 0xB8, 0xDA, 0x8E, 0x76, 0x84, 0x32, 0xA9, 0x54, 0x8D, 0xBE, 0x4A, 0xA3, 0xA0}; LRPContext ctx = {0}; LRPSetKey(&ctx, key, 0, false); - + uint8_t pt0[] = {0xAC, 0x20, 0xD3, 0x9F, 0x53, 0x41, 0xFE, 0x98, 0xDF, 0xCA, 0x21, 0xDA, 0x86, 0xBA, 0x79, 0x14}; res = res && (memcmp(ctx.plaintexts[0], pt0, sizeof(pt0)) == 0); @@ -515,7 +515,7 @@ static bool TestLRPUpdatedKeys(void) { uint8_t key[] = {0x56, 0x78, 0x26, 0xB8, 0xDA, 0x8E, 0x76, 0x84, 0x32, 0xA9, 0x54, 0x8D, 0xBE, 0x4A, 0xA3, 0xA0}; LRPContext ctx = {0}; LRPSetKey(&ctx, key, 0, false); - + uint8_t key0[] = {0x16, 0x3D, 0x14, 0xED, 0x24, 0xED, 0x93, 0x53, 0x73, 0x56, 0x8E, 0xC5, 0x21, 0xE9, 0x6C, 0xF4}; res = res && (memcmp(ctx.updatedKeys[0], key0, sizeof(key0)) == 0); @@ -545,15 +545,15 @@ static bool TestLRPEval(void) { uint8_t iv[] = {0x13, 0x59}; LRPSetKey(&ctx, key, 2, false); LRPEvalLRP(&ctx, iv, sizeof(iv) * 2, true, y); - + uint8_t y1[] = {0x1B, 0xA2, 0xC0, 0xC5, 0x78, 0x99, 0x6B, 0xC4, 0x97, 0xDD, 0x18, 0x1C, 0x68, 0x85, 0xA9, 0xDD}; res = res && (memcmp(y, y1, sizeof(y1)) == 0); - + uint8_t key2[] = {0xB6, 0x55, 0x57, 0xCE, 0x0E, 0x9B, 0x4C, 0x58, 0x86, 0xF2, 0x32, 0x20, 0x01, 0x13, 0x56, 0x2B}; uint8_t iv2[] = {0xBB, 0x4F, 0xCF, 0x27, 0xC9, 0x40, 0x76, 0xF7, 0x56, 0xAB, 0x03, 0x0D}; LRPSetKey(&ctx, key2, 1, false); LRPEvalLRP(&ctx, iv2, sizeof(iv2) * 2, false, y); - + uint8_t y2[] = {0x6F, 0xDF, 0xA8, 0xD2, 0xA6, 0xAA, 0x84, 0x76, 0xBF, 0x94, 0xE7, 0x1F, 0x25, 0x63, 0x7F, 0x96}; res = res && (memcmp(y, y2, sizeof(y2)) == 0); @@ -561,7 +561,7 @@ static bool TestLRPEval(void) { uint8_t iv3[] = {0x1F, 0x0B, 0x7C, 0x0D, 0xB1, 0x28, 0x89, 0xCA, 0x43, 0x6C, 0xAB, 0xB7, 0x8B, 0xE4, 0x2F, 0x90}; LRPSetKey(&ctx, key3, 3, false); LRPEvalLRP(&ctx, iv3, sizeof(iv3) * 2 - 1, true, y); - + uint8_t y3[] = {0x51, 0x29, 0x6B, 0x5E, 0x6D, 0x3B, 0x8D, 0xB8, 0xA1, 0xA7, 0x39, 0x97, 0x60, 0xA1, 0x91, 0x89}; res = res && (memcmp(y, y3, sizeof(y3)) == 0); @@ -569,7 +569,7 @@ static bool TestLRPEval(void) { uint8_t iv4[] = {0x47, 0x5B, 0xB4, 0x18, 0x78, 0xEB, 0x17, 0x46, 0x8F, 0x7A, 0x68, 0x84, 0x7D, 0xDD, 0x3B, 0xAC}; LRPSetKey(&ctx, key4, 3, false); LRPEvalLRP(&ctx, iv4, sizeof(iv4) * 2, true, y); - + uint8_t y4[] = {0xC3, 0xB5, 0xEE, 0x74, 0xA7, 0x22, 0xE7, 0x84, 0x88, 0x7C, 0x4C, 0x9F, 0xDB, 0x49, 0x78, 0x55}; res = res && (memcmp(y, y4, sizeof(y4)) == 0); @@ -577,7 +577,7 @@ static bool TestLRPEval(void) { uint8_t iv5[] = {0xD4, 0x13, 0x77, 0x64, 0x71, 0x6D, 0xBC, 0x8C, 0x57, 0x9B, 0xEA, 0xB7, 0xE7, 0x67, 0x54, 0xE0}; LRPSetKey(&ctx, key5, 3, false); LRPEvalLRP(&ctx, iv5, sizeof(iv5) * 2 - 1, false, y); - + uint8_t y5[] = {0xCF, 0x99, 0x13, 0x92, 0xF0, 0x36, 0x93, 0x50, 0xA7, 0xE2, 0x1B, 0xE5, 0x2F, 0x74, 0x88, 0x21}; res = res && (memcmp(y, y5, sizeof(y5)) == 0); @@ -606,7 +606,7 @@ static bool TestLRPIncCounter(void) { LRPIncCounter(ctr3, 3); uint8_t ctrr3[] = {0x00, 0x00}; res = res && (memcmp(ctr3, ctrr3, sizeof(ctrr3)) == 0); - + uint8_t ctr4[] = {0xf0}; LRPIncCounter(ctr4, 1); uint8_t ctrr4[] = {0x00}; @@ -622,8 +622,8 @@ static bool TestLRPIncCounter(void) { static bool TestLRPEncode(void) { bool res = true; - - uint8_t resp[100] = {0}; + + uint8_t resp[100] = {0}; size_t resplen = 0; LRPContext ctx = {0}; @@ -632,9 +632,10 @@ static bool TestLRPEncode(void) { uint8_t iv1[] = {0xC3, 0x31, 0x5D, 0xBF}; LRPSetKeyEx(&ctx, key1, iv1, sizeof(iv1) * 2, 0, true); uint8_t data1[] = {0x01, 0x2D, 0x7F, 0x16, 0x53, 0xCA, 0xF6, 0x50, 0x3C, 0x6A, 0xB0, 0xC1, 0x01, 0x0E, 0x8C, 0xB0}; - LRPEncode(&ctx, data1, sizeof (data1), resp, &resplen); - uint8_t res1[] = {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}; + LRPEncode(&ctx, data1, sizeof(data1), resp, &resplen); + uint8_t res1[] = {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 + }; res = res && (resplen == sizeof(res1)); res = res && (memcmp(resp, res1, sizeof(res1)) == 0); @@ -642,7 +643,7 @@ static bool TestLRPEncode(void) { uint8_t iv2[] = {0x90, 0x36, 0xFF, 0xFF}; LRPSetKeyEx(&ctx, key2, iv2, sizeof(iv2) * 2, 0, false); uint8_t data2[] = {0xE7, 0xF6, 0x1E, 0x01, 0x2F, 0x4F, 0x32, 0x55, 0x31, 0x2B, 0xA6, 0x8B, 0x1D, 0x2F, 0xDA, 0xBF}; - LRPEncode(&ctx, data2, sizeof (data2), resp, &resplen); + LRPEncode(&ctx, data2, sizeof(data2), resp, &resplen); uint8_t res2[] = {0xEA, 0x6E, 0x09, 0xAC, 0x2F, 0xB9, 0x7E, 0x10, 0x2D, 0x8C, 0xA6, 0x4C, 0x1C, 0xBC, 0x0C, 0x0C}; res = res && (resplen == sizeof(res2)); res = res && (memcmp(resp, res2, sizeof(res2)) == 0); @@ -651,7 +652,7 @@ static bool TestLRPEncode(void) { uint8_t iv3[] = {0xFF, 0xFF, 0xFF, 0xFF}; LRPSetKeyEx(&ctx, key3, iv3, sizeof(iv3) * 2, 0, true); uint8_t data3[] = {0x27}; - LRPEncode(&ctx, data3, sizeof (data3), resp, &resplen); + LRPEncode(&ctx, data3, sizeof(data3), resp, &resplen); uint8_t res3[] = {0xF5, 0x83, 0x3F, 0xC3, 0x97, 0x35, 0x6E, 0xA3, 0xD9, 0xEC, 0xAD, 0xBB, 0x9F, 0x6F, 0xE4, 0x40}; res = res && (resplen == sizeof(res3)); res = res && (memcmp(resp, res3, sizeof(res3)) == 0); @@ -667,13 +668,15 @@ static bool TestLRPEncode(void) { 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[] = {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}; - LRPEncode(&ctx, data5, sizeof (data5), resp, &resplen); - uint8_t res5[] = {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}; + uint8_t data5[] = {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 + }; + LRPEncode(&ctx, data5, sizeof(data5), resp, &resplen); + uint8_t res5[] = {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 + }; res = res && (resplen == sizeof(res5)); res = res && (memcmp(resp, res5, sizeof(res5)) == 0); @@ -687,8 +690,8 @@ static bool TestLRPEncode(void) { static bool TestLRPDecode(void) { bool res = true; - - uint8_t resp[100] = {0}; + + uint8_t resp[100] = {0}; size_t resplen = 0; LRPContext ctx = {0}; @@ -696,9 +699,10 @@ static bool TestLRPDecode(void) { 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 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); @@ -707,7 +711,7 @@ static bool TestLRPDecode(void) { 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); + 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); @@ -716,7 +720,7 @@ static bool TestLRPDecode(void) { 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); + LRPDecode(&ctx, data3, sizeof(data3), resp, &resplen); uint8_t res3[] = {0x27}; res = res && (resplen == sizeof(res3)); res = res && (memcmp(resp, res3, sizeof(res3)) == 0); @@ -731,13 +735,15 @@ static bool TestLRPDecode(void) { 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}; + 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); @@ -760,15 +766,15 @@ static bool TestLRPSubkeys(void) { uint8_t key1[] = {0x81, 0x95, 0x08, 0x8C, 0xE6, 0xC3, 0x93, 0x70, 0x8E, 0xBB, 0xE6, 0xC7, 0x91, 0x4E, 0xCB, 0x0B}; uint8_t sk1r1[] = {0x16, 0x91, 0x2B, 0x8D, 0x19, 0xD9, 0x4B, 0x2D, 0x4D, 0xA4, 0xFF, 0xA1, 0xCA, 0xD2, 0x18, 0x23}; uint8_t sk2r1[] = {0x2D, 0x22, 0x57, 0x1A, 0x33, 0xB2, 0x96, 0x5A, 0x9B, 0x49, 0xFF, 0x43, 0x95, 0xA4, 0x30, 0x46}; - + LRPGenSubkeys(key1, sk1, sk2); res = res && (memcmp(sk1, sk1r1, sizeof(sk1r1)) == 0); res = res && (memcmp(sk2, sk2r1, sizeof(sk2r1)) == 0); - + uint8_t key2[] = {0x11, 0xED, 0x02, 0x02, 0x25, 0x70, 0xCB, 0x10, 0x50, 0x2B, 0xC1, 0xDA, 0xCF, 0x64, 0xB2, 0x1F}; uint8_t sk1r2[] = {0x5B, 0x5D, 0x85, 0x36, 0x61, 0xE5, 0x1B, 0xC9, 0x13, 0x77, 0xED, 0xCE, 0xB6, 0x22, 0xBF, 0x6E}; uint8_t sk2r2[] = {0xB6, 0xBB, 0x0A, 0x6C, 0xC3, 0xCA, 0x37, 0x92, 0x26, 0xEF, 0xDB, 0x9D, 0x6C, 0x45, 0x7E, 0xDC}; - + LRPGenSubkeys(key2, sk1, sk2); res = res && (memcmp(sk1, sk1r2, sizeof(sk1r2)) == 0); res = res && (memcmp(sk2, sk2r2, sizeof(sk2r2)) == 0); @@ -776,7 +782,7 @@ static bool TestLRPSubkeys(void) { uint8_t key3[] = {0x5A, 0xA9, 0xF6, 0xC6, 0xDE, 0x51, 0x38, 0x11, 0x3D, 0xF5, 0xD6, 0xB6, 0xC7, 0x7D, 0x5D, 0x52}; uint8_t sk1r3[] = {0x2A, 0xE0, 0xEB, 0xD3, 0x76, 0xBC, 0xD4, 0xA2, 0x7B, 0x1C, 0xD4, 0x06, 0xD2, 0x43, 0x1C, 0xF9}; uint8_t sk2r3[] = {0x55, 0xC1, 0xD7, 0xA6, 0xED, 0x79, 0xA9, 0x44, 0xF6, 0x39, 0xA8, 0x0D, 0xA4, 0x86, 0x39, 0xF2}; - + LRPGenSubkeys(key3, sk1, sk2); res = res && (memcmp(sk1, sk1r3, sizeof(sk1r3)) == 0); res = res && (memcmp(sk2, sk2r3, sizeof(sk2r3)) == 0); @@ -803,7 +809,7 @@ static bool TestLRPCMAC(void) { LRPCMAC(&ctx, data1, sizeof(data1), cmac); uint8_t cmacres1[] = {0xAD, 0x85, 0x95, 0xE0, 0xB4, 0x9C, 0x5C, 0x0D, 0xB1, 0x8E, 0x77, 0x35, 0x5F, 0x5A, 0xAF, 0xF6}; res = res && (memcmp(cmac, cmacres1, sizeof(cmacres1)) == 0); - + uint8_t key2[] = {0x5A, 0xA9, 0xF6, 0xC6, 0xDE, 0x51, 0x38, 0x11, 0x3D, 0xF5, 0xD6, 0xB6, 0xC7, 0x7D, 0x5D, 0x52}; LRPSetKey(&ctx, key2, 0, true); uint8_t data2[] = {0xA4, 0x43, 0x4D, 0x74, 0x0C, 0x2C, 0xB6, 0x65, 0xFE, 0x53, 0x96, 0x95, 0x91, 0x89, 0x38, 0x3F}; @@ -822,7 +828,8 @@ static bool TestLRPCMAC(void) { LRPSetKey(&ctx, key4, 0, true); uint8_t data4[] = {0xC2, 0xAC, 0x3D, 0x72, 0x50, 0xEE, 0xF0, 0x23, 0x18, 0xBC, 0x08, 0x4F, 0x29, 0x4B, 0x1A, 0xC7, 0x22, 0x91, 0xEE, 0x1D, 0xC0, 0x2A, 0xF4, 0x24, 0x94, 0x1C, 0xAA, 0xC6, 0x85, 0xFC, 0xA5, 0x9D, - 0x90, 0x08, 0x67, 0x9B, 0x00, 0xC5, 0x6A, 0x05, 0x62, 0x58, 0x3B, 0xDA, 0xEC, 0x0B, 0xBA}; + 0x90, 0x08, 0x67, 0x9B, 0x00, 0xC5, 0x6A, 0x05, 0x62, 0x58, 0x3B, 0xDA, 0xEC, 0x0B, 0xBA + }; LRPCMAC(&ctx, data4, sizeof(data4), cmac); uint8_t cmacres4[] = {0x66, 0xDC, 0x2B, 0xCE, 0x26, 0x9B, 0x79, 0x3B, 0x4A, 0xCA, 0x1A, 0x4D, 0x04, 0xDD, 0xD6, 0x68}; res = res && (memcmp(cmac, cmacres4, sizeof(cmacres4)) == 0); @@ -832,11 +839,12 @@ static bool TestLRPCMAC(void) { LRPCMAC(&ctx, NULL, 0, cmac); uint8_t cmacres5[] = {0x0E, 0x07, 0xC6, 0x01, 0x97, 0x08, 0x14, 0xA4, 0x17, 0x6F, 0xDA, 0x63, 0x3C, 0x6F, 0xC3, 0xDE}; res = res && (memcmp(cmac, cmacres5, sizeof(cmacres5)) == 0); - + uint8_t key6[] = {0x95, 0x2F, 0xDE, 0x83, 0x93, 0xC4, 0x5D, 0x23, 0x0A, 0x5B, 0xE9, 0xB3, 0x86, 0x36, 0xD1, 0x54}; LRPSetKey(&ctx, key6, 0, true); uint8_t data6[] = {0xD7, 0x80, 0x0E, 0x25, 0x70, 0x01, 0xA7, 0x74, 0xAE, 0x7B, 0xCF, 0xB2, 0xCE, 0x13, 0x07, 0xB5, - 0xB0, 0x44}; + 0xB0, 0x44 + }; LRPCMAC(&ctx, data6, sizeof(data6), cmac); uint8_t cmacres6[] = {0x05, 0xF1, 0xCE, 0x30, 0x45, 0x1A, 0x03, 0xA6, 0xE4, 0x68, 0xB3, 0xA5, 0x90, 0x33, 0xA5, 0x54}; res = res && (memcmp(cmac, cmacres6, sizeof(cmacres6)) == 0); diff --git a/client/src/mifare/lrpcrypto.c b/client/src/mifare/lrpcrypto.c index 113191162..71ddce201 100644 --- a/client/src/mifare/lrpcrypto.c +++ b/client/src/mifare/lrpcrypto.c @@ -35,7 +35,7 @@ static uint8_t const00[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 void LRPClearContext(LRPContext *ctx) { memset(ctx->key, 0, CRYPTO_AES128_KEY_SIZE); - + ctx->useBitPadding = false; ctx->plaintextsCount = 0; memset(ctx->plaintexts, 0, LRP_MAX_PLAINTEXTS_SIZE * CRYPTO_AES128_KEY_SIZE); @@ -46,15 +46,15 @@ void LRPClearContext(LRPContext *ctx) { void LRPSetKey(LRPContext *ctx, uint8_t *key, size_t updatedKeyNum, bool useBitPadding) { LRPClearContext(ctx); - + memcpy(ctx->key, key, CRYPTO_AES128_KEY_SIZE); - + LRPGeneratePlaintexts(ctx, 16); LRPGenerateUpdatedKeys(ctx, 4); - + ctx->useUpdatedKeyNum = updatedKeyNum; ctx->useBitPadding = useBitPadding; - + memcpy(ctx->counter, const00, CRYPTO_AES128_KEY_SIZE); ctx->counterLenNibbles = CRYPTO_AES128_KEY_SIZE; } @@ -64,7 +64,7 @@ void LRPSetCounter(LRPContext *ctx, uint8_t *counter, size_t counterLenNibbles) ctx->counterLenNibbles = counterLenNibbles; } -void LRPSetKeyEx(LRPContext *ctx, uint8_t *key, uint8_t *counter, size_t counterLenNibbles, size_t updatedKeyNum, bool useBitPadding){ +void LRPSetKeyEx(LRPContext *ctx, uint8_t *key, uint8_t *counter, size_t counterLenNibbles, size_t updatedKeyNum, bool useBitPadding) { LRPSetKey(ctx, key, updatedKeyNum, useBitPadding); LRPSetCounter(ctx, counter, counterLenNibbles); } @@ -75,15 +75,15 @@ void LRPSetKeyEx(LRPContext *ctx, uint8_t *key, uint8_t *counter, size_t counter void LRPGeneratePlaintexts(LRPContext *ctx, size_t plaintextsCount) { if (plaintextsCount > LRP_MAX_PLAINTEXTS_SIZE) return; - + uint8_t h[CRYPTO_AES128_KEY_SIZE] = {0}; memcpy(h, ctx->key, CRYPTO_AES128_KEY_SIZE); - + for (int i = 0; i < plaintextsCount; i++) { aes_encode(NULL, h, const55, h, CRYPTO_AES128_KEY_SIZE); - aes_encode(NULL, h, constAA, ctx->plaintexts[i], CRYPTO_AES128_KEY_SIZE); + aes_encode(NULL, h, constAA, ctx->plaintexts[i], CRYPTO_AES128_KEY_SIZE); } - + ctx->plaintextsCount = plaintextsCount; } @@ -97,10 +97,10 @@ void LRPGenerateUpdatedKeys(LRPContext *ctx, size_t updatedKeysCount) { aes_encode(NULL, ctx->key, constAA, h, CRYPTO_AES128_KEY_SIZE); for (int i = 0; i < updatedKeysCount; i++) { - aes_encode(NULL, h, constAA, ctx->updatedKeys[i], CRYPTO_AES128_KEY_SIZE); + aes_encode(NULL, h, constAA, ctx->updatedKeys[i], CRYPTO_AES128_KEY_SIZE); aes_encode(NULL, h, const55, h, CRYPTO_AES128_KEY_SIZE); } - + ctx->updatedKeysCount = updatedKeysCount; } @@ -109,12 +109,12 @@ void LRPGenerateUpdatedKeys(LRPContext *ctx, size_t updatedKeysCount) { void LRPEvalLRP(LRPContext *ctx, uint8_t *iv, size_t ivlen, bool final, uint8_t *y) { uint8_t ry[CRYPTO_AES128_KEY_SIZE] = {0}; memcpy(ry, ctx->updatedKeys[ctx->useUpdatedKeyNum], CRYPTO_AES128_KEY_SIZE); - + for (int i = 0; i < ivlen; i++) { uint8_t nk = (i % 2) ? iv[i / 2] & 0x0f : (iv[i / 2] >> 4) & 0x0f; aes_encode(NULL, ry, ctx->plaintexts[nk], ry, CRYPTO_AES128_KEY_SIZE); } - + if (final) aes_encode(NULL, ry, const00, ry, CRYPTO_AES128_KEY_SIZE); memcpy(y, ry, CRYPTO_AES128_KEY_SIZE); @@ -133,8 +133,8 @@ void LRPIncCounter(uint8_t *ctr, size_t ctrlen) { ctr[i / 2] = (ctr[i / 2] & 0xf0) | (nk & 0x0f); else ctr[i / 2] = (ctr[i / 2] & 0x0f) | ((nk << 4) & 0xf0); - - if(!carry) + + if (!carry) break; } } @@ -143,17 +143,17 @@ void LRPIncCounter(uint8_t *ctr, size_t ctrlen) { // Algorithm 4 void LRPEncode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, size_t *resplen) { *resplen = 0; - + uint8_t xdata[1024] = {0}; memcpy(xdata, data, datalen); if (ctx->useBitPadding) { xdata[datalen] = 0x80; datalen++; } - + if (datalen % CRYPTO_AES128_KEY_SIZE) datalen = datalen + CRYPTO_AES128_KEY_SIZE - (datalen % CRYPTO_AES128_KEY_SIZE); - + if (datalen == 0) return; @@ -172,7 +172,7 @@ void LRPDecode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, si *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); @@ -180,7 +180,7 @@ void LRPDecode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, si *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--) { @@ -195,7 +195,7 @@ void LRPDecode(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *resp, si static bool shiftLeftBe(uint8_t *data, size_t length) { if (length == 0) return false; - + bool carry = false; for (int i = length - 1; i >= 0; i--) { uint8_t val = data[i]; @@ -234,7 +234,7 @@ void LRPCMAC(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *cmac) { uint8_t sk1[CRYPTO_AES128_KEY_SIZE] = {0}; uint8_t sk2[CRYPTO_AES128_KEY_SIZE] = {0}; LRPGenSubkeys(ctx->key, sk1, sk2); - + uint8_t y[CRYPTO_AES128_KEY_SIZE] = {0}; size_t clen = 0; for (int i = 0; i < datalen / CRYPTO_AES128_KEY_SIZE; i++) { @@ -253,7 +253,7 @@ void LRPCMAC(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *cmac) { // last block if (bllen == 16) { bin_xor(y, bl, CRYPTO_AES128_KEY_SIZE); - + bin_xor(y, sk1, CRYPTO_AES128_KEY_SIZE); } else { // padding @@ -262,6 +262,6 @@ void LRPCMAC(LRPContext *ctx, uint8_t *data, size_t datalen, uint8_t *cmac) { bin_xor(y, sk2, CRYPTO_AES128_KEY_SIZE); } - + LRPEvalLRP(ctx, y, CRYPTO_AES128_KEY_SIZE * 2, true, cmac); } diff --git a/client/src/mifare/lrpcrypto.h b/client/src/mifare/lrpcrypto.h index 19a811137..cf1653865 100644 --- a/client/src/mifare/lrpcrypto.h +++ b/client/src/mifare/lrpcrypto.h @@ -32,14 +32,14 @@ typedef struct { uint8_t key[CRYPTO_AES128_KEY_SIZE]; - + bool useBitPadding; size_t plaintextsCount; uint8_t plaintexts[LRP_MAX_PLAINTEXTS_SIZE][CRYPTO_AES128_KEY_SIZE]; size_t updatedKeysCount; uint8_t updatedKeys[LRP_MAX_UPDATED_KEYS_SIZE][CRYPTO_AES128_KEY_SIZE]; size_t useUpdatedKeyNum; - + uint8_t counter[LRP_MAX_COUNTER_SIZE]; size_t counterLenNibbles; // len in bytes * 2 (or * 2 - 1) } LRPContext; diff --git a/doc/commands.json b/doc/commands.json index ff0e51657..27558f75f 100644 --- a/doc/commands.json +++ b/doc/commands.json @@ -973,7 +973,7 @@ }, "help": { "command": "help", - "description": "help use ` 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_20210813.txt --------------------------------------------------------------------------------------- auto available offline: no run lf search / hf search / data plot / data save", + "description": "help use ` 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_20210816.txt --------------------------------------------------------------------------------------- auto available offline: no run lf search / hf search / data plot / data save", "notes": [ "auto" ], @@ -4936,7 +4936,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 subkeys...... passed [=] an10922 aes....... passed [=] an10922 2tdea..... passed [=] an10922 3tdea..... passed [=] cmac 3tdea........ passed [=] cmac 2tdea........ passed [=] cmac des.......... passed [=] ev2 session keys.. passed [=] ev2 iv calc....... passed [=] ev2 mac calc...... passed [=] trans session key. 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 subkeys...... passed [=] an10922 aes....... passed [=] an10922 2tdea..... passed [=] an10922 3tdea..... passed [=] cmac 3tdea........ passed [=] cmac 2tdea........ passed [=] cmac des.......... passed [=] ev2 session keys.. passed [=] ev2 iv calc....... passed [=] ev2 mac calc...... passed [=] trans session key. passed [=] lrp plaintexts.... passed [=] lrp updated keys.. passed [=] lrp eval.......... passed [=] lrp inc counter... passed [=] lrp encode........ passed [=] lrp decode........ passed [=] lrp subkeys....... passed [=] lrp cmac.......... 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" ], @@ -4991,7 +4991,7 @@ "hf mfdes write --aid 123456 --fid 01 --type record -d 01020304 --updaterec 0 -> update record in the record file. record 0 - lastest record.", "hf mfdes write --aid 123456 --fid 01 --type record --offset 000000 -d 11223344 -> write record to record file. use default channel settings from `default` command", "hf mfdes write --appisoid 1234 --fileisoid 1000 --type data -c iso -d 01020304 -> write data to std/backup file via iso commandset", - "hf mfdes write --appisoid 1234 --fileisoid 2000 --type record -c iso -d 01020304 -> aend record to record file via iso commandset", + "hf mfdes write --appisoid 1234 --fileisoid 2000 --type record -c iso -d 01020304 -> send record to record file via iso commandset", "hf mfdes write --aid 123456 --fid 01 -d 01020304 --readerid 010203 -> write data to file with commitreaderid command before write and committransaction after write" ], "offline": false, @@ -9959,6 +9959,6 @@ "metadata": { "commands_extracted": 589, "extracted_by": "PM3Help2JSON v1.00", - "extracted_on": "2021-08-13T09:38:47" + "extracted_on": "2021-08-16T14:02:15" } } \ No newline at end of file