From 7ed4561f74cfaf80f7595020cb846023894c9676 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Sat, 19 Dec 2009 06:10:44 +0000 Subject: [PATCH] New API function: mifare_classic_trailer_block(). --- mifare_classic.c | 30 ++++++++++++++++++++++++++++++ mifare_classic.h | 3 +-- test/Makefile | 5 +++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mifare_classic.c b/mifare_classic.c index 22da8a0..542e151 100644 --- a/mifare_classic.c +++ b/mifare_classic.c @@ -69,6 +69,12 @@ union mifare_classic_block { MifareClassicBlockNumber address__; MifareClassicBlockNumber address___; } value; + struct { + MifareClassicKey key_a; + uint8_t access_bits[3]; + uint8_t gpb; + MifareClassicKey key_b; + } trailer; }; typedef unsigned char MifareClassicAccessBits; @@ -677,3 +683,27 @@ mifare_classic_format_sector (MifareClassicTag tag, MifareClassicBlockNumber blo return 0; } + +/* + * Generates a MIFARE trailer block. + */ +void +mifare_classic_trailer_block (MifareClassicBlock *block, MifareClassicKey key_a, uint8_t ab_0, uint8_t ab_1, uint8_t ab_2, uint8_t ab_tb, uint8_t gpb, MifareClassicKey key_b) +{ + union mifare_classic_block *b = (union mifare_classic_block *)block; // *((union mifare_classic_block *)(&block)); + + memcpy (b->trailer.key_a, key_a, sizeof (MifareClassicKey)); + + uint32_t access_bits = ((((( ab_0 & 0x4) >> 2) << 8) | (((ab_0 & 0x2) >> 1) << 4) | (ab_0 & 0x1)) | + (((((ab_1 & 0x4) >> 2) << 8) | (((ab_1 & 0x2) >> 1) << 4) | (ab_1 & 0x1)) << 1) | + (((((ab_2 & 0x4) >> 2) << 8) | (((ab_2 & 0x2) >> 1) << 4) | (ab_2 & 0x1)) << 2) | + (((((ab_tb & 0x4) >> 2) << 8) | (((ab_tb & 0x2) >> 1) << 4) | (ab_tb & 0x1)) << 3)); + + uint32_t access_bits_ = ((~access_bits) & 0x00000fff); + + uint32_t ab = htole32(((access_bits << 12) | access_bits_)); + memcpy (&(b->trailer.access_bits), &ab, 3); + b->trailer.gpb = gpb; + + memcpy (b->trailer.key_b, key_b, sizeof (MifareClassicKey)); +} diff --git a/mifare_classic.h b/mifare_classic.h index 27d6d9e..a480f84 100644 --- a/mifare_classic.h +++ b/mifare_classic.h @@ -23,8 +23,6 @@ struct mifare_classic_tag; typedef struct mifare_classic_tag *MifareClassicTag; -// struct mifare_block; -// typedef struct mifare_block *MifareClassicBlock; typedef unsigned char MifareClassicBlock[16]; typedef unsigned char MifareClassicBlockNumber; @@ -54,6 +52,7 @@ int mifare_classic_restore (MifareClassicTag tag, MifareClassicBlockNumber bloc int mifare_classic_transfer (MifareClassicTag tag, MifareClassicBlockNumber block); int mifare_classic_format_sector (MifareClassicTag tag, MifareClassicBlockNumber block); +void mifare_classic_trailer_block (MifareClassicBlock *block, MifareClassicKey key_a, uint8_t ab_0, uint8_t ab_1, uint8_t ab_2, uint8_t ab_tb, uint8_t gpb, MifareClassicKey key_b); /* MIFARE Classic Access Bits */ #define MCAB_R 0x8 diff --git a/test/Makefile b/test/Makefile index 1c6ec87..72239db 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,7 +8,8 @@ TESTS= test_read_sector_0.c \ test_authenticate.c \ test_value_block.c \ test_access_bits.c \ - test_format.c + test_format.c \ + test_create_trailer_block.c SRCS= ${LMF_SRCS} \ ${TESTS} \ @@ -35,7 +36,7 @@ check test: libfreefare_test # list.h is just a list of all tests, as indicated by DEFINE_TEST macro lines list.h: ${TESTS} Makefile - (cd ${.CURDIR}; cat test_*.c) | grep DEFINE_TEST > list.h + (cd ${.CURDIR}; cat ${TESTS}) | grep DEFINE_TEST > list.h CLEANFILES+= list.h