New API function: mifare_classic_trailer_block().

This commit is contained in:
Romain Tartiere 2009-12-19 06:10:44 +00:00
parent b97012ac05
commit 7ed4561f74
3 changed files with 34 additions and 4 deletions

View file

@ -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));
}

View file

@ -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

View file

@ -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