diff --git a/examples/mifare-classic-format.c b/examples/mifare-classic-format.c index a893e0b..30f34fd 100644 --- a/examples/mifare-classic-format.c +++ b/examples/mifare-classic-format.c @@ -167,7 +167,7 @@ main(int argc, char *argv[]) nfc_list_devices (devices, 8, &device_count); if (!device_count) - errx (EXIT_FAILURE, "No NFC device found."); + errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { device = nfc_connect (&(devices[d])); @@ -177,72 +177,72 @@ main(int argc, char *argv[]) continue; } - tags = freefare_get_tags (device); - if (!tags) { - nfc_disconnect (device); - errx (EXIT_FAILURE, "Error listing Mifare Classic tag."); - } - - for (int i = 0; (!error) && tags[i]; i++) { - switch (freefare_get_tag_type (tags[i])) { - case CLASSIC_1K: - case CLASSIC_4K: - break; - default: - continue; + tags = freefare_get_tags (device); + if (!tags) { + nfc_disconnect (device); + errx (EXIT_FAILURE, "Error listing Mifare Classic tag."); } - char *tag_uid = freefare_get_tag_uid (tags[i]); - char buffer[BUFSIZ]; - - printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); - bool format = true; - if (format_options.interactive) { - printf ("Format [yN] "); - fgets (buffer, BUFSIZ, stdin); - format = ((buffer[0] == 'y') || (buffer[0] == 'Y')); - } else { - printf ("\n"); - } - - if (format) { - enum mifare_tag_type tt = freefare_get_tag_type (tags[i]); - at_block = 0; - - if (format_options.fast) { - printf (START_FORMAT_N, (tt == CLASSIC_1K) ? 1 : 2); - if (!try_format_sector (tags[i], 0x00)) - break; - - if (tt == CLASSIC_4K) - if (!try_format_sector (tags[i], 0x10)) - break; - - printf (DONE_FORMAT); - continue; - } - switch (tt) { + for (int i = 0; (!error) && tags[i]; i++) { + switch (freefare_get_tag_type (tags[i])) { case CLASSIC_1K: - mod_block = 4; - if (!format_mifare_classic_1k (tags[i])) - error = 1; - break; case CLASSIC_4K: - mod_block = 10; - if (!format_mifare_classic_4k (tags[i])) - error = 1; break; default: - /* Keep compiler quiet */ - break; + continue; } + + char *tag_uid = freefare_get_tag_uid (tags[i]); + char buffer[BUFSIZ]; + + printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); + bool format = true; + if (format_options.interactive) { + printf ("Format [yN] "); + fgets (buffer, BUFSIZ, stdin); + format = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + } else { + printf ("\n"); + } + + if (format) { + enum mifare_tag_type tt = freefare_get_tag_type (tags[i]); + at_block = 0; + + if (format_options.fast) { + printf (START_FORMAT_N, (tt == CLASSIC_1K) ? 1 : 2); + if (!try_format_sector (tags[i], 0x00)) + break; + + if (tt == CLASSIC_4K) + if (!try_format_sector (tags[i], 0x10)) + break; + + printf (DONE_FORMAT); + continue; + } + switch (tt) { + case CLASSIC_1K: + mod_block = 4; + if (!format_mifare_classic_1k (tags[i])) + error = 1; + break; + case CLASSIC_4K: + mod_block = 10; + if (!format_mifare_classic_4k (tags[i])) + error = 1; + break; + default: + /* Keep compiler quiet */ + break; + } + } + + free (tag_uid); } - free (tag_uid); - } - - freefare_free_tags (tags); - nfc_disconnect (device); + freefare_free_tags (tags); + nfc_disconnect (device); } exit (error); diff --git a/examples/mifare-classic-write-ndef.c b/examples/mifare-classic-write-ndef.c index 8ff5b4e..fb1999f 100644 --- a/examples/mifare-classic-write-ndef.c +++ b/examples/mifare-classic-write-ndef.c @@ -132,7 +132,7 @@ main(int argc, char *argv[]) nfc_list_devices (devices, 8, &device_count); if (!device_count) - errx (EXIT_FAILURE, "No NFC device found."); + errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { device = nfc_connect (&(devices[d])); @@ -142,179 +142,179 @@ main(int argc, char *argv[]) continue; } - tags = freefare_get_tags (device); - if (!tags) { - nfc_disconnect (device); - errx (EXIT_FAILURE, "Error listing MIFARE classic tag."); - } - - for (int i = 0; (!error) && tags[i]; i++) { - switch (freefare_get_tag_type (tags[i])) { - case CLASSIC_1K: - case CLASSIC_4K: - break; - default: - continue; + tags = freefare_get_tags (device); + if (!tags) { + nfc_disconnect (device); + errx (EXIT_FAILURE, "Error listing MIFARE classic tag."); } - char *tag_uid = freefare_get_tag_uid (tags[i]); - char buffer[BUFSIZ]; - - printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); - fgets (buffer, BUFSIZ, stdin); - bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); - - if (write_ndef) { + for (int i = 0; (!error) && tags[i]; i++) { switch (freefare_get_tag_type (tags[i])) { - case CLASSIC_4K: - if (!search_sector_key (tags[i], 0x10, &(card_write_keys[0x10].key), &(card_write_keys[0x10].type))) { - error = 1; - goto error; - } - /* fallthrough */ case CLASSIC_1K: - if (!search_sector_key (tags[i], 0x00, &(card_write_keys[0x00].key), &(card_write_keys[0x00].type))) { - error = 1; - goto error; - } + case CLASSIC_4K: break; default: - /* Keep compiler quiet */ - break; + continue; } - if (!error) { - /* Ensure the auth key is always a B one. If not, change it! */ + char *tag_uid = freefare_get_tag_uid (tags[i]); + char buffer[BUFSIZ]; + + printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); + fgets (buffer, BUFSIZ, stdin); + bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + + if (write_ndef) { switch (freefare_get_tag_type (tags[i])) { case CLASSIC_4K: - if (card_write_keys[0x10].type != MFC_KEY_B) { - if( 0 != fix_mad_trailer_block( tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) { - error = 1; - goto error; - } - memcpy (&(card_write_keys[0x10].key), &default_keyb, sizeof (MifareClassicKey)); - card_write_keys[0x10].type = MFC_KEY_B; - } - /* fallthrough */ - case CLASSIC_1K: - if (card_write_keys[0x00].type != MFC_KEY_B) { - if( 0 != fix_mad_trailer_block( tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) { - error = 1; - goto error; - } - memcpy (&(card_write_keys[0x00].key), &default_keyb, sizeof (MifareClassicKey)); - card_write_keys[0x00].type = MFC_KEY_B; - } - break; - default: - /* Keep compiler quiet */ - break; - } - } - - size_t encoded_size; - uint8_t *tlv_data = tlv_encode (3, ndef_msg, sizeof (ndef_msg), &encoded_size); - - /* - * At his point, we should have collected all information needed to - * succeed. - */ - - // If the card already has a MAD, load it. - if ((mad = mad_read (tags[i]))) { - // If our application already exists, erase it. - mifare_application_free (mad, mad_nfcforum_aid); - } else { - - // Create a MAD and mark unaccessible sectors in the card - if (!(mad = mad_new ((freefare_get_tag_type (tags[i]) == CLASSIC_4K) ? 2 : 1))) { - perror ("mad_new"); - error = 1; - goto error; - } - - MifareClassicSectorNumber max_s; - switch (freefare_get_tag_type (tags[i])) { - case CLASSIC_1K: - max_s = 15; - break; - case CLASSIC_4K: - max_s = 39; - break; - default: - /* Keep compiler quiet */ - break; - } - - MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; - - // Mark unusable sectors as so - for (size_t s = max_s; s; s--) { - if (s == 0x10) continue; - if (!search_sector_key (tags[i], s, &(card_write_keys[s].key), &(card_write_keys[s].type))) { - mad_set_aid (mad, s, mad_defect_aid); - } else if ((memcmp (card_write_keys[s].key, transport_key, sizeof (transport_key)) != 0) && - (card_write_keys[s].type != MFC_KEY_A)) { - // Revert to transport configuration - if (mifare_classic_format_sector (tags[i], s) < 0) { - perror ("mifare_classic_format_sector"); + if (!search_sector_key (tags[i], 0x10, &(card_write_keys[0x10].key), &(card_write_keys[0x10].type))) { error = 1; goto error; } + /* fallthrough */ + case CLASSIC_1K: + if (!search_sector_key (tags[i], 0x00, &(card_write_keys[0x00].key), &(card_write_keys[0x00].type))) { + error = 1; + goto error; + } + break; + default: + /* Keep compiler quiet */ + break; + } + + if (!error) { + /* Ensure the auth key is always a B one. If not, change it! */ + switch (freefare_get_tag_type (tags[i])) { + case CLASSIC_4K: + if (card_write_keys[0x10].type != MFC_KEY_B) { + if( 0 != fix_mad_trailer_block( tags[i], 0x10, card_write_keys[0x10].key, card_write_keys[0x10].type)) { + error = 1; + goto error; + } + memcpy (&(card_write_keys[0x10].key), &default_keyb, sizeof (MifareClassicKey)); + card_write_keys[0x10].type = MFC_KEY_B; + } + /* fallthrough */ + case CLASSIC_1K: + if (card_write_keys[0x00].type != MFC_KEY_B) { + if( 0 != fix_mad_trailer_block( tags[i], 0x00, card_write_keys[0x00].key, card_write_keys[0x00].type)) { + error = 1; + goto error; + } + memcpy (&(card_write_keys[0x00].key), &default_keyb, sizeof (MifareClassicKey)); + card_write_keys[0x00].type = MFC_KEY_B; + } + break; + default: + /* Keep compiler quiet */ + break; } } - } - MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size); - if (!sectors) { - perror ("mifare_application_alloc"); - error = EXIT_FAILURE; - goto error; - } + size_t encoded_size; + uint8_t *tlv_data = tlv_encode (3, ndef_msg, sizeof (ndef_msg), &encoded_size); - if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) { - perror ("mad_write"); - error = EXIT_FAILURE; - goto error; - } + /* + * At his point, we should have collected all information needed to + * succeed. + */ - int s = 0; - while (sectors[s]) { - MifareClassicBlockNumber block = mifare_classic_sector_last_block (sectors[s]); - MifareClassicBlock block_data; - mifare_classic_trailer_block (&block_data, mifare_classic_nfcforum_public_key_a, 0x0, 0x0, 0x0, 0x6, 0x40, default_keyb); - if (mifare_classic_authenticate (tags[i], block, card_write_keys[sectors[s]].key, card_write_keys[sectors[s]].type) < 0) { - perror ("mifare_classic_authenticate"); + // If the card already has a MAD, load it. + if ((mad = mad_read (tags[i]))) { + // If our application already exists, erase it. + mifare_application_free (mad, mad_nfcforum_aid); + } else { + + // Create a MAD and mark unaccessible sectors in the card + if (!(mad = mad_new ((freefare_get_tag_type (tags[i]) == CLASSIC_4K) ? 2 : 1))) { + perror ("mad_new"); + error = 1; + goto error; + } + + MifareClassicSectorNumber max_s; + switch (freefare_get_tag_type (tags[i])) { + case CLASSIC_1K: + max_s = 15; + break; + case CLASSIC_4K: + max_s = 39; + break; + default: + /* Keep compiler quiet */ + break; + } + + MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; + + // Mark unusable sectors as so + for (size_t s = max_s; s; s--) { + if (s == 0x10) continue; + if (!search_sector_key (tags[i], s, &(card_write_keys[s].key), &(card_write_keys[s].type))) { + mad_set_aid (mad, s, mad_defect_aid); + } else if ((memcmp (card_write_keys[s].key, transport_key, sizeof (transport_key)) != 0) && + (card_write_keys[s].type != MFC_KEY_A)) { + // Revert to transport configuration + if (mifare_classic_format_sector (tags[i], s) < 0) { + perror ("mifare_classic_format_sector"); + error = 1; + goto error; + } + } + } + } + + MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, mad_nfcforum_aid, encoded_size); + if (!sectors) { + perror ("mifare_application_alloc"); error = EXIT_FAILURE; goto error; } - if (mifare_classic_write (tags[i], block, block_data) < 0) { - perror ("mifare_classic_write"); + + if (mad_write (tags[i], mad, card_write_keys[0x00].key, card_write_keys[0x10].key) < 0) { + perror ("mad_write"); error = EXIT_FAILURE; goto error; } - s++; + + int s = 0; + while (sectors[s]) { + MifareClassicBlockNumber block = mifare_classic_sector_last_block (sectors[s]); + MifareClassicBlock block_data; + mifare_classic_trailer_block (&block_data, mifare_classic_nfcforum_public_key_a, 0x0, 0x0, 0x0, 0x6, 0x40, default_keyb); + if (mifare_classic_authenticate (tags[i], block, card_write_keys[sectors[s]].key, card_write_keys[sectors[s]].type) < 0) { + perror ("mifare_classic_authenticate"); + error = EXIT_FAILURE; + goto error; + } + if (mifare_classic_write (tags[i], block, block_data) < 0) { + perror ("mifare_classic_write"); + error = EXIT_FAILURE; + goto error; + } + s++; + } + + if ((ssize_t) encoded_size != mifare_application_write (tags[i], mad, mad_nfcforum_aid, tlv_data, encoded_size, default_keyb, MCAB_WRITE_KEYB)) { + perror ("mifare_application_write"); + error = EXIT_FAILURE; + goto error; + } + + free (sectors); + + free (tlv_data); + + free (mad); } - if ((ssize_t) encoded_size != mifare_application_write (tags[i], mad, mad_nfcforum_aid, tlv_data, encoded_size, default_keyb, MCAB_WRITE_KEYB)) { - perror ("mifare_application_write"); - error = EXIT_FAILURE; - goto error; - } - - free (sectors); - - free (tlv_data); - - free (mad); - } - error: - free (tag_uid); - } + free (tag_uid); + } - freefare_free_tags (tags); - nfc_disconnect (device); + freefare_free_tags (tags); + nfc_disconnect (device); } free (card_write_keys); diff --git a/examples/mifare-desfire-access.c b/examples/mifare-desfire-access.c index f845918..06d920e 100644 --- a/examples/mifare-desfire-access.c +++ b/examples/mifare-desfire-access.c @@ -44,7 +44,7 @@ main(int argc, char *argv[]) nfc_list_devices (devices, 8, &device_count); if (!device_count) - errx (EXIT_FAILURE, "No NFC device found."); + errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { device = nfc_connect (&(devices[d])); @@ -54,81 +54,81 @@ main(int argc, char *argv[]) continue; } - tags = freefare_get_tags (device); - if (!tags) { - nfc_disconnect (device); - errx (EXIT_FAILURE, "Error listing tags."); - } - - for (int i = 0; (!error) && tags[i]; i++) { - if (DESFIRE != freefare_get_tag_type (tags[i])) - continue; - - int res; - char *tag_uid = freefare_get_tag_uid (tags[i]); - - res = mifare_desfire_connect (tags[i]); - if (res < 0) { - warnx ("Can't connect to Mifare DESFire target."); - error = EXIT_FAILURE; - break; + tags = freefare_get_tags (device); + if (!tags) { + nfc_disconnect (device); + errx (EXIT_FAILURE, "Error listing tags."); } + for (int i = 0; (!error) && tags[i]; i++) { + if (DESFIRE != freefare_get_tag_type (tags[i])) + continue; - MifareDESFireKey key = mifare_desfire_des_key_new_with_version (key_data_null); - res = mifare_desfire_authenticate (tags[i], 0, key); - if (res < 0) - errx (EXIT_FAILURE, "Authentication on master application failed"); + int res; + char *tag_uid = freefare_get_tag_uid (tags[i]); - MadAid mad_aid = { 0x12, 0x34 }; - MifareDESFireAID aid = mifare_desfire_aid_new_with_mad_aid (mad_aid, 0x5); - res = mifare_desfire_create_application (tags[i], aid, 0xFF, 0x1); - if (res < 0) - errx (EXIT_FAILURE, "Application creation failed"); + res = mifare_desfire_connect (tags[i]); + if (res < 0) { + warnx ("Can't connect to Mifare DESFire target."); + error = EXIT_FAILURE; + break; + } - res = mifare_desfire_select_application (tags[i], aid); - if (res < 0) - errx (EXIT_FAILURE, "Application selection failed"); - res = mifare_desfire_authenticate (tags[i], 0, key); - if (res < 0) - errx (EXIT_FAILURE, "Authentication on application failed"); + MifareDESFireKey key = mifare_desfire_des_key_new_with_version (key_data_null); + res = mifare_desfire_authenticate (tags[i], 0, key); + if (res < 0) + errx (EXIT_FAILURE, "Authentication on master application failed"); - res = mifare_desfire_create_std_data_file (tags[i], 1, MDCM_FULLDES, 0x0000, 20); - if (res < 0) - errx (EXIT_FAILURE, "File creation failed"); + MadAid mad_aid = { 0x12, 0x34 }; + MifareDESFireAID aid = mifare_desfire_aid_new_with_mad_aid (mad_aid, 0x5); + res = mifare_desfire_create_application (tags[i], aid, 0xFF, 0x1); + if (res < 0) + errx (EXIT_FAILURE, "Application creation failed"); - char *s= "Hello World"; - res = mifare_desfire_write_data (tags[i], 1, 0, strlen (s), s); - if (res < 0) - errx (EXIT_FAILURE, "File write failed"); + res = mifare_desfire_select_application (tags[i], aid); + if (res < 0) + errx (EXIT_FAILURE, "Application selection failed"); - char buffer[20]; - res = mifare_desfire_read_data (tags[i], 1, 0, 0, buffer); - if (res < 0) - errx (EXIT_FAILURE, "File read failed"); + res = mifare_desfire_authenticate (tags[i], 0, key); + if (res < 0) + errx (EXIT_FAILURE, "Authentication on application failed"); - res = mifare_desfire_select_application (tags[i], NULL); - if (res < 0) - errx (EXIT_FAILURE, "Master application selection failed"); + res = mifare_desfire_create_std_data_file (tags[i], 1, MDCM_FULLDES, 0x0000, 20); + if (res < 0) + errx (EXIT_FAILURE, "File creation failed"); - res = mifare_desfire_authenticate (tags[i], 0, key); - if (res < 0) - errx (EXIT_FAILURE, "Authentication on master application failed"); + char *s= "Hello World"; + res = mifare_desfire_write_data (tags[i], 1, 0, strlen (s), s); + if (res < 0) + errx (EXIT_FAILURE, "File write failed"); - res = mifare_desfire_format_picc (tags[i]); - if (res < 0) - errx (EXIT_FAILURE, "PICC format failed"); + char buffer[20]; + res = mifare_desfire_read_data (tags[i], 1, 0, 0, buffer); + if (res < 0) + errx (EXIT_FAILURE, "File read failed"); - mifare_desfire_key_free (key); - free (tag_uid); - free (aid); + res = mifare_desfire_select_application (tags[i], NULL); + if (res < 0) + errx (EXIT_FAILURE, "Master application selection failed"); - mifare_desfire_disconnect (tags[i]); - } + res = mifare_desfire_authenticate (tags[i], 0, key); + if (res < 0) + errx (EXIT_FAILURE, "Authentication on master application failed"); - freefare_free_tags (tags); - nfc_disconnect (device); + res = mifare_desfire_format_picc (tags[i]); + if (res < 0) + errx (EXIT_FAILURE, "PICC format failed"); + + mifare_desfire_key_free (key); + free (tag_uid); + free (aid); + + mifare_desfire_disconnect (tags[i]); + } + + freefare_free_tags (tags); + nfc_disconnect (device); } exit (error); diff --git a/examples/mifare-desfire-format.c b/examples/mifare-desfire-format.c index 4464287..7a9d51f 100644 --- a/examples/mifare-desfire-format.c +++ b/examples/mifare-desfire-format.c @@ -75,7 +75,7 @@ main(int argc, char *argv[]) nfc_list_devices (devices, 8, &device_count); if (!device_count) - errx (EXIT_FAILURE, "No NFC device found."); + errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { device = nfc_connect (&(devices[d])); @@ -85,62 +85,62 @@ main(int argc, char *argv[]) continue; } - tags = freefare_get_tags (device); - if (!tags) { + tags = freefare_get_tags (device); + if (!tags) { + nfc_disconnect (device); + errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); + } + + for (int i = 0; (!error) && tags[i]; i++) { + if (DESFIRE != freefare_get_tag_type (tags[i])) + continue; + + char *tag_uid = freefare_get_tag_uid (tags[i]); + char buffer[BUFSIZ]; + + printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); + bool format = true; + if (format_options.interactive) { + printf ("Format [yN] "); + fgets (buffer, BUFSIZ, stdin); + format = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + } else { + printf ("\n"); + } + + if (format) { + int res; + MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); + + res = mifare_desfire_connect (tags[i]); + if (res < 0) { + warnx ("Can't connect to Mifare DESFire target."); + error = EXIT_FAILURE; + break; + } + + res = mifare_desfire_authenticate (tags[i], 0, default_key); + if (res < 0) { + warnx ("Can't authenticate on Mifare DESFire target."); + error = EXIT_FAILURE; + break; + } + + res = mifare_desfire_format_picc (tags[i]); + if (res < 0) { + warn ("Can't format PICC."); + error = EXIT_FAILURE; + break; + } + + mifare_desfire_disconnect (tags[i]); + } + + free (tag_uid); + } + + freefare_free_tags (tags); nfc_disconnect (device); - errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); - } - - for (int i = 0; (!error) && tags[i]; i++) { - if (DESFIRE != freefare_get_tag_type (tags[i])) - continue; - - char *tag_uid = freefare_get_tag_uid (tags[i]); - char buffer[BUFSIZ]; - - printf ("Found %s with UID %s. ", freefare_get_tag_friendly_name (tags[i]), tag_uid); - bool format = true; - if (format_options.interactive) { - printf ("Format [yN] "); - fgets (buffer, BUFSIZ, stdin); - format = ((buffer[0] == 'y') || (buffer[0] == 'Y')); - } else { - printf ("\n"); - } - - if (format) { - int res; - MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data); - - res = mifare_desfire_connect (tags[i]); - if (res < 0) { - warnx ("Can't connect to Mifare DESFire target."); - error = EXIT_FAILURE; - break; - } - - res = mifare_desfire_authenticate (tags[i], 0, default_key); - if (res < 0) { - warnx ("Can't authenticate on Mifare DESFire target."); - error = EXIT_FAILURE; - break; - } - - res = mifare_desfire_format_picc (tags[i]); - if (res < 0) { - warn ("Can't format PICC."); - error = EXIT_FAILURE; - break; - } - - mifare_desfire_disconnect (tags[i]); - } - - free (tag_uid); - } - - freefare_free_tags (tags); - nfc_disconnect (device); } exit (error); diff --git a/examples/mifare-desfire-info.c b/examples/mifare-desfire-info.c index 1e26b95..88b622b 100644 --- a/examples/mifare-desfire-info.c +++ b/examples/mifare-desfire-info.c @@ -42,7 +42,7 @@ main(int argc, char *argv[]) nfc_list_devices (devices, 8, &device_count); if (!device_count) - errx (EXIT_FAILURE, "No NFC device found."); + errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { device = nfc_connect (&(devices[d])); @@ -52,76 +52,76 @@ main(int argc, char *argv[]) continue; } - tags = freefare_get_tags (device); - if (!tags) { - nfc_disconnect (device); - errx (EXIT_FAILURE, "Error listing tags."); - } - - for (int i = 0; (!error) && tags[i]; i++) { - if (DESFIRE != freefare_get_tag_type (tags[i])) - continue; - - int res; - char *tag_uid = freefare_get_tag_uid (tags[i]); - - struct mifare_desfire_version_info info; - - res = mifare_desfire_connect (tags[i]); - if (res < 0) { - warnx ("Can't connect to Mifare DESFire target."); - error = 1; - break; + tags = freefare_get_tags (device); + if (!tags) { + nfc_disconnect (device); + errx (EXIT_FAILURE, "Error listing tags."); } - res = mifare_desfire_get_version (tags[i], &info); - if (res < 0) { - warnx ("Can't get Mifare DESFire version information."); - error = 1; - break; + for (int i = 0; (!error) && tags[i]; i++) { + if (DESFIRE != freefare_get_tag_type (tags[i])) + continue; + + int res; + char *tag_uid = freefare_get_tag_uid (tags[i]); + + struct mifare_desfire_version_info info; + + res = mifare_desfire_connect (tags[i]); + if (res < 0) { + warnx ("Can't connect to Mifare DESFire target."); + error = 1; + break; + } + + res = mifare_desfire_get_version (tags[i], &info); + if (res < 0) { + warnx ("Can't get Mifare DESFire version information."); + error = 1; + break; + } + + printf ("===> Version information for tag %s:\n", tag_uid); + printf ("UID: 0x%02x%02x%02x%02x%02x%02x%02x\n", info.uid[0], info.uid[1], info.uid[2], info.uid[3], info.uid[4], info.uid[5], info.uid[6]); + printf ("Batch number: 0x%02x%02x%02x%02x%02x\n", info.batch_number[0], info.batch_number[1], info.batch_number[2], info.batch_number[3], info.batch_number[4]); + printf ("Production date: week %x, 20%02x\n", info.production_week, info.production_year); + printf ("Hardware Information:\n"); + printf (" Vendor ID: 0x%02x\n", info.hardware.vendor_id); + printf (" Type: 0x%02x\n", info.hardware.type); + printf (" Subtype: 0x%02x\n", info.hardware.subtype); + printf (" Version: %d.%d\n", info.hardware.version_major, info.hardware.version_minor); + printf (" Storage size: 0x%02x (%s%d bytes)\n", info.hardware.storage_size, (info.hardware.storage_size & 1) ? ">" : "=", (int)pow (2, info.hardware.storage_size >> 1)); + printf (" Protocol: 0x%02x\n", info.hardware.protocol); + printf ("Software Information:\n"); + printf (" Vendor ID: 0x%02x\n", info.software.vendor_id); + printf (" Type: 0x%02x\n", info.software.type); + printf (" Subtype: 0x%02x\n", info.software.subtype); + printf (" Version: %d.%d\n", info.software.version_major, info.software.version_minor); + printf (" Storage size: 0x%02x (%s%d bytes)\n", info.software.storage_size, (info.software.storage_size & 1) ? ">" : "=", (int)pow (2, info.software.storage_size >> 1)); + printf (" Protocol: 0x%02x\n", info.software.protocol); + + uint8_t settings; + uint8_t max_keys; + res = mifare_desfire_get_key_settings (tags[i], &settings, &max_keys); + if (res < 0) { + warnx ("Cant' get Mifare DESFire Master Key settings: %s", mifare_desfire_error_lookup (mifare_desfire_get_last_error (tags[i]))); + error = 1; + break; + } + + printf ("Master Key settings (0x%02x):\n", settings); + printf (" 0x%02x configuration changeable;\n", settings & 0x08); + printf (" 0x%02x PICC Master Key not required for create / delete;\n", settings & 0x04); + printf (" 0x%02x Free directory list access without PICC Master Key;\n", settings & 0x02); + printf (" 0x%02x Allow changing the Master Key;\n", settings & 0x01); + + free (tag_uid); + + mifare_desfire_disconnect (tags[i]); } - printf ("===> Version information for tag %s:\n", tag_uid); - printf ("UID: 0x%02x%02x%02x%02x%02x%02x%02x\n", info.uid[0], info.uid[1], info.uid[2], info.uid[3], info.uid[4], info.uid[5], info.uid[6]); - printf ("Batch number: 0x%02x%02x%02x%02x%02x\n", info.batch_number[0], info.batch_number[1], info.batch_number[2], info.batch_number[3], info.batch_number[4]); - printf ("Production date: week %x, 20%02x\n", info.production_week, info.production_year); - printf ("Hardware Information:\n"); - printf (" Vendor ID: 0x%02x\n", info.hardware.vendor_id); - printf (" Type: 0x%02x\n", info.hardware.type); - printf (" Subtype: 0x%02x\n", info.hardware.subtype); - printf (" Version: %d.%d\n", info.hardware.version_major, info.hardware.version_minor); - printf (" Storage size: 0x%02x (%s%d bytes)\n", info.hardware.storage_size, (info.hardware.storage_size & 1) ? ">" : "=", (int)pow (2, info.hardware.storage_size >> 1)); - printf (" Protocol: 0x%02x\n", info.hardware.protocol); - printf ("Software Information:\n"); - printf (" Vendor ID: 0x%02x\n", info.software.vendor_id); - printf (" Type: 0x%02x\n", info.software.type); - printf (" Subtype: 0x%02x\n", info.software.subtype); - printf (" Version: %d.%d\n", info.software.version_major, info.software.version_minor); - printf (" Storage size: 0x%02x (%s%d bytes)\n", info.software.storage_size, (info.software.storage_size & 1) ? ">" : "=", (int)pow (2, info.software.storage_size >> 1)); - printf (" Protocol: 0x%02x\n", info.software.protocol); - - uint8_t settings; - uint8_t max_keys; - res = mifare_desfire_get_key_settings (tags[i], &settings, &max_keys); - if (res < 0) { - warnx ("Cant' get Mifare DESFire Master Key settings: %s", mifare_desfire_error_lookup (mifare_desfire_get_last_error (tags[i]))); - error = 1; - break; - } - - printf ("Master Key settings (0x%02x):\n", settings); - printf (" 0x%02x configuration changeable;\n", settings & 0x08); - printf (" 0x%02x PICC Master Key not required for create / delete;\n", settings & 0x04); - printf (" 0x%02x Free directory list access without PICC Master Key;\n", settings & 0x02); - printf (" 0x%02x Allow changing the Master Key;\n", settings & 0x01); - - free (tag_uid); - - mifare_desfire_disconnect (tags[i]); - } - - freefare_free_tags (tags); - nfc_disconnect (device); + freefare_free_tags (tags); + nfc_disconnect (device); } exit (error); diff --git a/examples/mifare-desfire-write-ndef.c b/examples/mifare-desfire-write-ndef.c index 3edea5a..5dc9885 100644 --- a/examples/mifare-desfire-write-ndef.c +++ b/examples/mifare-desfire-write-ndef.c @@ -50,129 +50,130 @@ main(int argc, char *argv[]) MifareTag *tags = NULL; if (argc > 1) - errx (EXIT_FAILURE, "usage: %s", argv[0]); + errx (EXIT_FAILURE, "usage: %s", argv[0]); nfc_device_desc_t devices[8]; size_t device_count; nfc_list_devices (devices, 8, &device_count); if (!device_count) - errx (EXIT_FAILURE, "No NFC device found."); + errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); - error = EXIT_FAILURE; - continue; - } + device = nfc_connect (&(devices[d])); + if (!device) { + warnx ("nfc_connect() failed."); + error = EXIT_FAILURE; + continue; + } - tags = freefare_get_tags (device); - if (!tags) { - nfc_disconnect (device); - errx (EXIT_FAILURE, "Error listing tags."); - } + tags = freefare_get_tags (device); + if (!tags) { + nfc_disconnect (device); + errx (EXIT_FAILURE, "Error listing tags."); + } - for (int i = 0; (!error) && tags[i]; i++) { - if (DESFIRE != freefare_get_tag_type (tags[i])) - continue; + for (int i = 0; (!error) && tags[i]; i++) { + if (DESFIRE != freefare_get_tag_type (tags[i])) + continue; - char *tag_uid = freefare_get_tag_uid (tags[i]); - char buffer[BUFSIZ]; + char *tag_uid = freefare_get_tag_uid (tags[i]); + char buffer[BUFSIZ]; - printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); - fgets (buffer, BUFSIZ, stdin); - bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); + printf ("Found %s with UID %s. Write NDEF [yN] ", freefare_get_tag_friendly_name (tags[i]), tag_uid); + fgets (buffer, BUFSIZ, stdin); + bool write_ndef = ((buffer[0] == 'y') || (buffer[0] == 'Y')); - if (write_ndef) { - int res; + if (write_ndef) { + int res; - res = mifare_desfire_connect (tags[i]); - if (res < 0) { - warnx ("Can't connect to Mifare DESFire target."); - error = EXIT_FAILURE; - break; - } + res = mifare_desfire_connect (tags[i]); + if (res < 0) { + warnx ("Can't connect to Mifare DESFire target."); + error = EXIT_FAILURE; + break; + } - /* Initialised Formatting Procedure. See section 6.5.1 and 8.1 of Mifare DESFire as Type 4 Tag document*/ - // Send Mifare DESFire Select Application with AID equal to 000000h to select the PICC level - res = mifare_desfire_select_application(tags[i], NULL); - if (res < 0) - errx (EXIT_FAILURE, "Application selection failed"); + /* Initialised Formatting Procedure. See section 6.5.1 and 8.1 of Mifare DESFire as Type 4 Tag document*/ + // Send Mifare DESFire Select Application with AID equal to 000000h to select the PICC level + res = mifare_desfire_select_application(tags[i], NULL); + if (res < 0) + errx (EXIT_FAILURE, "Application selection failed"); - MifareDESFireKey key; - key = mifare_desfire_des_key_new_with_version (key_data_null); + MifareDESFireKey key; + key = mifare_desfire_des_key_new_with_version (key_data_null); - // Authentication with PICC master key MAY be needed to issue ChangeKeySettings command - res = mifare_desfire_authenticate (tags[i], 0, key); - if (res < 0) - errx (EXIT_FAILURE, "Authentication with PICC master key failed"); + // Authentication with PICC master key MAY be needed to issue ChangeKeySettings command + res = mifare_desfire_authenticate (tags[i], 0, key); + if (res < 0) + errx (EXIT_FAILURE, "Authentication with PICC master key failed"); - // Mifare DESFire Create Application with AID equal to EEEE10h, key settings equal to 09, NumOfKeys equal to 01h - MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10); - res = mifare_desfire_create_application (tags[i], aid, 0x09, 1); - if (res < 0) - errx (EXIT_FAILURE, "Application creation failed. Try mifare-desfire-format before running %s.", argv[0]); + // Mifare DESFire Create Application with AID equal to EEEE10h, key settings equal to 09, NumOfKeys equal to 01h + MifareDESFireAID aid = mifare_desfire_aid_new(0xEEEE10); + res = mifare_desfire_create_application (tags[i], aid, 0x09, 1); + if (res < 0) + errx (EXIT_FAILURE, "Application creation failed. Try mifare-desfire-format before running %s.", argv[0]); - // Mifare DESFire SelectApplication (Select previously creates application) - res = mifare_desfire_select_application(tags[i], aid); - if (res < 0) - errx (EXIT_FAILURE, "Application selection failed"); - free (aid); + // Mifare DESFire SelectApplication (Select previously creates application) + res = mifare_desfire_select_application(tags[i], aid); + if (res < 0) + errx (EXIT_FAILURE, "Application selection failed"); + free (aid); - // Authentication with NDEF Tag Application master key (Authentication with key 0) - res = mifare_desfire_authenticate (tags[i], 0, key); - if (res < 0) - errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed"); - // Mifare DESFire ChangeKeySetting with key settings equal to 00000000b - res = mifare_desfire_change_key_settings (tags[i],0x00); - if (res < 0) - errx (EXIT_FAILURE, "ChangeKeySettings failed"); + // Authentication with NDEF Tag Application master key (Authentication with key 0) + res = mifare_desfire_authenticate (tags[i], 0, key); + if (res < 0) + errx (EXIT_FAILURE, "Authentication with NDEF Tag Application master key failed"); + // Mifare DESFire ChangeKeySetting with key settings equal to 00000000b + res = mifare_desfire_change_key_settings (tags[i],0x00); + if (res < 0) + errx (EXIT_FAILURE, "ChangeKeySettings failed"); - // Mifare DESFire CreateStdDataFile with FileNo equal to 03h (CC File DESFire FID), ComSet equal to 00h, - // AccesRights equal to E000h, File Size bigger equal to 00000Fh - res = mifare_desfire_create_std_data_file(tags[i],0x03,0x00,0xE000,0x00000F); - if (res < 0) - errx (EXIT_FAILURE, "CreateStdDataFile failed"); + // Mifare DESFire CreateStdDataFile with FileNo equal to 03h (CC File DESFire FID), ComSet equal to 00h, + // AccesRights equal to E000h, File Size bigger equal to 00000Fh + res = mifare_desfire_create_std_data_file(tags[i],0x03,0x00,0xE000,0x00000F); + if (res < 0) + errx (EXIT_FAILURE, "CreateStdDataFile failed"); - // Mifare DESFire WriteData to write the content of the CC File with CClEN equal to 000Fh, - // Mapping Version equal to 10h,MLe equal to 003Bh, MLc equal to 0034h, and NDEF File Control TLV - // equal to T =04h, L=06h, V=E1 04 (NDEF ISO FID=E104h) 0E E0 (NDEF File size =3808 Bytes) 00 (free read access) - // 00 free write access - byte_t capability_container_file_content[15] = { 0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh - 0x10, // Mapping version - 0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh - 0x00, 0x34, // MLc: Maximum data size that can be sent using a single UpdateBinary command. MLc = 0001h-FFFFh - 0x04, 0x06, 0xE1, 0x04, // TLV - 0x0E, 0xE0, // NDEF File size - 0x00, // free read access - 0x00 // free write acces - }; - res = mifare_desfire_write_data(tags[i],0x03,0,sizeof(capability_container_file_content),capability_container_file_content); - if (res>0){ + // Mifare DESFire WriteData to write the content of the CC File with CClEN equal to 000Fh, + // Mapping Version equal to 10h,MLe equal to 003Bh, MLc equal to 0034h, and NDEF File Control TLV + // equal to T =04h, L=06h, V=E1 04 (NDEF ISO FID=E104h) 0E E0 (NDEF File size =3808 Bytes) 00 (free read access) + // 00 free write access + byte_t capability_container_file_content[15] = { + 0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh + 0x10, // Mapping version + 0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh + 0x00, 0x34, // MLc: Maximum data size that can be sent using a single UpdateBinary command. MLc = 0001h-FFFFh + 0x04, 0x06, 0xE1, 0x04, // TLV + 0x0E, 0xE0, // NDEF File size + 0x00, // free read access + 0x00 // free write acces + }; + res = mifare_desfire_write_data(tags[i],0x03,0,sizeof(capability_container_file_content),capability_container_file_content); + if (res>0){ - // Mifare DESFire CreateStdDataFile with FileNo equal to 04h (NDEF FileDESFire FID), CmmSet equal to 00h, AccessRigths - // equal to EEE0h, FileSize equal to 000EE0h (3808 Bytes) - res = mifare_desfire_create_std_data_file(tags[i],0x04,0x00,0xEEE0,0x000EE0); - if (res < 0) - errx (EXIT_FAILURE, "CreateStdDataFile failed"); + // Mifare DESFire CreateStdDataFile with FileNo equal to 04h (NDEF FileDESFire FID), CmmSet equal to 00h, AccessRigths + // equal to EEE0h, FileSize equal to 000EE0h (3808 Bytes) + res = mifare_desfire_create_std_data_file(tags[i],0x04,0x00,0xEEE0,0x000EE0); + if (res < 0) + errx (EXIT_FAILURE, "CreateStdDataFile failed"); - //Mifare DESFire WriteData to write the content of the NDEF File with NLEN equal to NDEF Message length and NDEF Message + //Mifare DESFire WriteData to write the content of the NDEF File with NLEN equal to NDEF Message length and NDEF Message - res = mifare_desfire_write_data(tags[i], 0x04, 0, sizeof(ndef_msg), (uint8_t *) ndef_msg); - if (res < 0) - errx (EXIT_FAILURE, " Write data failed"); - } else { - errx (EXIT_FAILURE, "Write CC file content failed"); - } - mifare_desfire_key_free (key); + res = mifare_desfire_write_data(tags[i], 0x04, 0, sizeof(ndef_msg), (uint8_t *) ndef_msg); + if (res < 0) + errx (EXIT_FAILURE, " Write data failed"); + } else { + errx (EXIT_FAILURE, "Write CC file content failed"); + } + mifare_desfire_key_free (key); - mifare_desfire_disconnect (tags[i]); - } - free (tag_uid); - } - freefare_free_tags (tags); - nfc_disconnect (device); + mifare_desfire_disconnect (tags[i]); + } + free (tag_uid); + } + freefare_free_tags (tags); + nfc_disconnect (device); } exit (error); } diff --git a/libfreefare/freefare.c b/libfreefare/freefare.c index 1770c20..302c247 100644 --- a/libfreefare/freefare.c +++ b/libfreefare/freefare.c @@ -49,9 +49,9 @@ freefare_tag_new (nfc_device_t *device, nfc_iso14443a_info_t nai) /* Ensure the target is supported */ for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) { if (((nai.szUidLen == 4) || (nai.abtUid[0] == NXP_MANUFACTURER_CODE)) && - (nai.btSak == supported_tags[i].SAK) && - (nai.szAtsLen == supported_tags[i].ATS_length) && - (0 == memcmp (nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_length))) { + (nai.btSak == supported_tags[i].SAK) && + (nai.szAtsLen == supported_tags[i].ATS_length) && + (0 == memcmp (nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_length))) { tag_info = &(supported_tags[i]); found = true; diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index ffb7853..7a6efe0 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -85,8 +85,9 @@ DEBUG_XFER (msg, __##msg##_n, "===> "); \ if (!(nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n))) { \ nfc_perror (tag->device, __FUNCTION__); \ - if (disconnect) \ + if (disconnect) { \ tag->active = false; \ + } \ return errno = EIO, -1; \ } \ DEBUG_XFER (res, __##res##_n, "<=== "); \ @@ -120,56 +121,56 @@ union mifare_classic_block { typedef unsigned char MifareClassicAccessBits; unsigned char mifare_data_access_permissions[] = { -/* - * [ Key A ] [ Key B ] - * | | - * ,----------- r(ead) | - * |,---------- w(rite) | - * ||,--------- d(ecrement) | - * |||,-------- i(ncrement) | - * |||| | - * |||| ,------------------------ r - * ,----- C3 |||| |,----------------------- w - * |,---- C2 |||| ||,---------------------- d - * ||,--- C1 |||| |||,--------------------- i - * ||| |||| |||| - * 0b000 0b 1111 1111 */ 0xff, /* Default (blank card) */ -/* 0b001 0b 1000 1100 */ 0x8c, -/* 0b010 0b 1000 1000 */ 0x88, -/* 0b011 0b 1010 1111 */ 0xaf, -/* 0b100 0b 1010 1010 */ 0xaa, -/* 0b101 0b 0000 1000 */ 0x08, -/* 0b110 0b 0000 1100 */ 0x0c, -/* 0b111 0b 0000 0000 */ 0x00 + /* + * [ Key A ] [ Key B ] + * | | + * ,----------- r(ead) | + * |,---------- w(rite) | + * ||,--------- d(ecrement) | + * |||,-------- i(ncrement) | + * |||| | + * |||| ,------------------------ r + * ,----- C3 |||| |,----------------------- w + * |,---- C2 |||| ||,---------------------- d + * ||,--- C1 |||| |||,--------------------- i + * ||| |||| |||| + * 0b000 0b 1111 1111 */ 0xff, /* Default (blank card) */ + /* 0b001 0b 1000 1100 */ 0x8c, + /* 0b010 0b 1000 1000 */ 0x88, + /* 0b011 0b 1010 1111 */ 0xaf, + /* 0b100 0b 1010 1010 */ 0xaa, + /* 0b101 0b 0000 1000 */ 0x08, + /* 0b110 0b 0000 1100 */ 0x0c, + /* 0b111 0b 0000 0000 */ 0x00 }; uint16_t mifare_trailer_access_permissions[] = { -/* - * [ Key A ] [ Access bits ] [ Key B ] - * | | | - * ,----------- read A | | - * |,---------- read B | | - * ||,--------- write A | | - * |||,-------- write B | | - * |||| | | - * |||| ,----------------------- read A | - * |||| |,---------------------- read B | - * |||| ||,--------------------- write A | - * |||| |||,-------------------- write B | - * |||| |||| | - * |||| |||| ,----------------------------------- read A - * ,----- C3 |||| |||| |,---------------------------------- read B - * |,---- C2 |||| |||| ||,--------------------------------- write A - * ||,--- C1 |||| |||| |||,-------------------------------- write B - * ||| |||| |||| |||| - * 0b000 0b 0010 1000 1010*/ 0x28a, -/* 0b001 0b 0001 1100 0001*/ 0x1c1, -/* 0b010 0b 0000 1000 1000*/ 0x088, -/* 0b011 0b 0000 1100 0000*/ 0x0c0, -/* 0b100 0b 0010 1010 1010*/ 0x2aa, /* Default (blank card) */ -/* 0b101 0b 0000 1101 0000*/ 0x0d0, -/* 0b110 0b 0001 1101 0001*/ 0x1d1, -/* 0b111 0b 0000 1100 0000*/ 0x0c0 + /* + * [ Key A ] [ Access bits ] [ Key B ] + * | | | + * ,----------- read A | | + * |,---------- read B | | + * ||,--------- write A | | + * |||,-------- write B | | + * |||| | | + * |||| ,----------------------- read A | + * |||| |,---------------------- read B | + * |||| ||,--------------------- write A | + * |||| |||,-------------------- write B | + * |||| |||| | + * |||| |||| ,----------------------------------- read A + * ,----- C3 |||| |||| |,---------------------------------- read B + * |,---- C2 |||| |||| ||,--------------------------------- write A + * ||,--- C1 |||| |||| |||,-------------------------------- write B + * ||| |||| |||| |||| + * 0b000 0b 0010 1000 1010*/ 0x28a, + /* 0b001 0b 0001 1100 0001*/ 0x1c1, + /* 0b010 0b 0000 1000 1000*/ 0x088, + /* 0b011 0b 0000 1100 0000*/ 0x0c0, + /* 0b100 0b 0010 1010 1010*/ 0x2aa, /* Default (blank card) */ + /* 0b101 0b 0000 1101 0000*/ 0x0d0, + /* 0b110 0b 0001 1101 0001*/ 0x1d1, + /* 0b111 0b 0000 1100 0000*/ 0x0c0 }; @@ -543,7 +544,7 @@ get_block_access_bits (MifareTag tag, const MifareClassicBlockNumber block, Mifa */ if (MIFARE_CLASSIC(tag)->cached_access_bits.sector_trailer_block_number == trailer) { /* cache hit! */ - sector_access_bits = MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits; + sector_access_bits = MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits; } else { MifareClassicBlock trailer_data; @@ -561,7 +562,7 @@ get_block_access_bits (MifareTag tag, const MifareClassicBlockNumber block, Mifa } MIFARE_CLASSIC(tag)->cached_access_bits.sector_trailer_block_number = trailer; MIFARE_CLASSIC(tag)->cached_access_bits.block_number = -1; - MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits = sector_access_bits; + MIFARE_CLASSIC(tag)->cached_access_bits.sector_access_bits = sector_access_bits; } /* @@ -730,7 +731,7 @@ MifareClassicBlockNumber mifare_classic_sector_last_block (MifareClassicSectorNumber sector) { return mifare_classic_sector_first_block (sector) + - mifare_classic_sector_block_count (sector) - 1; + mifare_classic_sector_block_count (sector) - 1; } /* diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index 9c33035..0a14825 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -93,8 +93,9 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ #define ASSERT_AUTHENTICATED(tag) \ do { \ - if (MIFARE_DESFIRE (tag)->authenticated_key_no == NOT_YET_AUTHENTICATED) \ + if (MIFARE_DESFIRE (tag)->authenticated_key_no == NOT_YET_AUTHENTICATED) { \ return errno = EINVAL, -1;\ + } \ } while (0) /* @@ -103,18 +104,20 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ */ #define ASSERT_CS(cs) \ do { \ - if (cs < 0) \ + if (cs < 0) { \ return errno = EINVAL, -1; \ - if (cs == 0x02) \ + } else if (cs == 0x02) { \ return errno = EINVAL, -1; \ - if (cs > 0x03) \ + } else if (cs > 0x03) { \ return errno = EINVAL, -1; \ + } \ } while (0) #define ASSERT_NOT_NULL(argument) \ do { \ - if (!argument) \ + if (!argument) { \ return errno = EINVAL, -1; \ + } \ } while (0) @@ -144,8 +147,9 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ errno = 0; \ MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; \ DEBUG_XFER (__##msg, __##msg##_n+1, "===> "); \ - if (!(nfc_initiator_transceive_bytes (tag->device, __##msg, __##msg##_n+1, __##res, &__##res##_n))) \ + if (!(nfc_initiator_transceive_bytes (tag->device, __##msg, __##msg##_n+1, __##res, &__##res##_n))) { \ return errno = EIO, -1; \ + } \ DEBUG_XFER (__##res, __##res##_n, "<=== "); \ __##res##_n -= 1; \ while (__##res[0] == 0xf2) { \ @@ -157,8 +161,9 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ DEBUG_XFER (__##res, __##res##_n, "<=== "); \ __##res##_n -= 1; \ } \ - if ((1 == __##res##_n) && (OPERATION_OK != res[0]) && (ADDITIONAL_FRAME != res[0])) \ + if ((1 == __##res##_n) && (OPERATION_OK != res[0]) && (ADDITIONAL_FRAME != res[0])) { \ return MIFARE_DESFIRE (tag)->last_picc_error = res[0], -1; \ + } \ } while (0) @@ -717,7 +722,7 @@ mifare_desfire_get_file_settings (MifareTag tag, uint8_t file_no, struct mifare_ settings->file_type = raw_settings.file_type; settings->communication_settings = raw_settings.communication_settings; settings->access_rights = le16toh (raw_settings.access_rights); - + switch (settings->file_type) { case MDFT_STANDARD_DATA_FILE: case MDFT_BACKUP_DATA_FILE: @@ -757,15 +762,15 @@ mifare_desfire_change_file_settings (MifareTag tag, uint8_t file_no, uint8_t com cached_file_settings_current[file_no] = false; if (MDAR_CHANGE_AR(settings.access_rights) == MDAR_FREE) { - BUFFER_INIT (cmd, 5); - BUFFER_INIT (res, 1); + BUFFER_INIT (cmd, 5); + BUFFER_INIT (res, 1); - BUFFER_APPEND (cmd, 0x5F); - BUFFER_APPEND (cmd, file_no); - BUFFER_APPEND (cmd, communication_settings); - BUFFER_APPEND_LE (cmd, access_rights, 2, sizeof (uint16_t)); + BUFFER_APPEND (cmd, 0x5F); + BUFFER_APPEND (cmd, file_no); + BUFFER_APPEND (cmd, communication_settings); + BUFFER_APPEND_LE (cmd, access_rights, 2, sizeof (uint16_t)); - DESFIRE_TRANSCEIVE (tag, cmd, res); + DESFIRE_TRANSCEIVE (tag, cmd, res); } else { BUFFER_INIT (cmd, 10); BUFFER_INIT (res, 1); @@ -946,8 +951,8 @@ read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_t offset, size_t return -1; } - BUFFER_CLEAR (cmd); - BUFFER_APPEND (cmd, 0xAF); + BUFFER_CLEAR (cmd); + BUFFER_APPEND (cmd, 0xAF); } } while (res[0] != 0x00); diff --git a/libfreefare/mifare_desfire_aid.c b/libfreefare/mifare_desfire_aid.c index 8af5343..99f0f18 100644 --- a/libfreefare/mifare_desfire_aid.c +++ b/libfreefare/mifare_desfire_aid.c @@ -56,7 +56,7 @@ mifare_desfire_aid_new (uint32_t aid) uint32_t aid_le = htole32 (aid); if ((res = malloc (sizeof (*res)))) { - memcpy(res->data, ((uint8_t*)&aid_le), 3); + memcpy(res->data, ((uint8_t*)&aid_le), 3); } return res; diff --git a/libfreefare/mifare_desfire_authenticate.c b/libfreefare/mifare_desfire_authenticate.c index ecd8f91..00d38cc 100644 --- a/libfreefare/mifare_desfire_authenticate.c +++ b/libfreefare/mifare_desfire_authenticate.c @@ -256,7 +256,7 @@ mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection if (mac) { DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT); } else { - DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); + DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); } memcpy (data, edata, 8); break; @@ -266,9 +266,9 @@ mifare_des (MifareDESFireKey key, uint8_t *data, uint8_t *ivect, MifareDirection DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_DECRYPT); DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_ENCRYPT); } else { - DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); - DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT); - DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); + DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); + DES_ecb_encrypt ((DES_cblock *) edata, (DES_cblock *) data, &(key->ks2), DES_ENCRYPT); + DES_ecb_encrypt ((DES_cblock *) data, (DES_cblock *) edata, &(key->ks1), DES_DECRYPT); } memcpy (data, edata, 8); break; diff --git a/libfreefare/tlv.c b/libfreefare/tlv.c index 305530a..27bfa54 100644 --- a/libfreefare/tlv.c +++ b/libfreefare/tlv.c @@ -73,7 +73,7 @@ tlv_encode (const uint8_t type, const uint8_t *istream, uint16_t isize, size_t * return NULL; if ((res = malloc (1 + ((isize > 254) ? 3 : 1) + isize + 1))) { - /* type + size + payload + terminator */ + /* type + size + payload + terminator */ res[n++] = type; if (isize > 254) { diff --git a/test/mifare_classic_fixture.c b/test/mifare_classic_fixture.c index 49f14cf..a7af1fe 100644 --- a/test/mifare_classic_fixture.c +++ b/test/mifare_classic_fixture.c @@ -47,7 +47,7 @@ cut_setup () tag = NULL; for (int i=0; tags[i]; i++) { if ((freefare_get_tag_type(tags[i]) == CLASSIC_1K) || - (freefare_get_tag_type(tags[i]) == CLASSIC_4K)) { + (freefare_get_tag_type(tags[i]) == CLASSIC_4K)) { tag = tags[i]; res = mifare_classic_connect (tag); cut_assert_equal_int (0, res, cut_message ("mifare_classic_connect() failed")); diff --git a/test/test_mifare_classic_mad.c b/test/test_mifare_classic_mad.c index 0127ea6..15f4b03 100644 --- a/test/test_mifare_classic_mad.c +++ b/test/test_mifare_classic_mad.c @@ -57,10 +57,14 @@ test_mifare_classic_mad (void) // Check the empty MAD - MifareClassicBlock ref_01 = { 0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - MifareClassicBlock ref_02 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + MifareClassicBlock ref_01 = { + 0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + MifareClassicBlock ref_02 = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; MifareClassicBlock data; res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A); @@ -78,12 +82,12 @@ test_mifare_classic_mad (void) cut_assert_not_null (mad2, cut_message ("mad_read() failed")); cut_assert_equal_memory (mad, sizeof (mad), mad2, sizeof (mad2), cut_message ("Wrong MAD")); - const char application_data[] = "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " - "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " - "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " - "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " - "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " - "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> "; + const char application_data[] = "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \ + "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \ + "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \ + "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \ + "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> " \ + "APPLICATION DATA >> APPLICATION DATA >> APPLICATION DATA >> "; MadAid aid = { .function_cluster_code = 0x01, @@ -164,12 +168,18 @@ test_mifare_classic_mad (void) cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_memory (ref_02, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data")); - MifareClassicBlock ref_40 = { 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - MifareClassicBlock ref_41 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - MifareClassicBlock ref_42 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + MifareClassicBlock ref_40 = { + 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + MifareClassicBlock ref_41 = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + MifareClassicBlock ref_42 = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; res = mifare_classic_authenticate (tag, 0x40, mad_public_key_a, MFC_KEY_A); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); diff --git a/test/test_mifare_desfire.c b/test/test_mifare_desfire.c index 74e0c0f..3111884 100644 --- a/test/test_mifare_desfire.c +++ b/test/test_mifare_desfire.c @@ -292,7 +292,7 @@ test_mifare_desfire (void) struct mifare_desfire_file_settings settings; res = mifare_desfire_get_file_settings (tag, files[i], &settings); cut_assert_success ("mifare_desfire_get_file_settings()"); - + switch (files[i]) { case 0: cut_assert_equal_int (MDFT_CYCLIC_RECORD_FILE_WITH_BACKUP, settings.file_type, cut_message ("Wrong file type")); diff --git a/test/test_tlv.c b/test/test_tlv.c index f446b31..7dd3009 100644 --- a/test/test_tlv.c +++ b/test/test_tlv.c @@ -24,42 +24,42 @@ const uint8_t shortdata[8] = "elephant"; const uint8_t eshortdata[11] = "\x03" "\x08" "elephant" "\xfe"; - /* - * Many thanks to Charles Baudelaire for helping me - * test things and helping you realize your f**king - * OS / compiler does not support UTF-8 ;-) - */ -const uint8_t longdata[660] = "Dans une terre grasse et pleine d'escargots\n" - "Je veux creuser moi-même une fosse profonde,\n" - "Où je puisse à loisir étaler mes vieux os\n" - "Et dormir dans l'oubli comme un requin dans l'onde.\n" - "Je hais les testaments et je hais les tombeaux;\n" - "Plutôt que d'implorer une larme du monde,\n" - "Vivant, j'aimerais mieux inviter les corbeaux\n" - "À saigner tous les bouts de ma carcasse immonde.\n" - "Ô vers! noirs compagnons sans oreille et sans yeux,\n" - "Voyez venir à vous un mort libre et joyeux;\n" - "Philosophes viveurs, fils de la pourriture,\n" - "À travers ma ruine allez donc sans remords,\n" - "Et dites-moi s'il est encor quelque torture\n" - "Pour ce vieux corps sans âme et mort parmi les morts!\n"; +/* + * Many thanks to Charles Baudelaire for helping me + * test things and helping you realize your f**king + * OS / compiler does not support UTF-8 ;-) + */ +const uint8_t longdata[660] = "Dans une terre grasse et pleine d'escargots\n" \ + "Je veux creuser moi-même une fosse profonde,\n" \ + "Où je puisse à loisir étaler mes vieux os\n" \ + "Et dormir dans l'oubli comme un requin dans l'onde.\n" \ + "Je hais les testaments et je hais les tombeaux;\n" \ + "Plutôt que d'implorer une larme du monde,\n" \ + "Vivant, j'aimerais mieux inviter les corbeaux\n" \ + "À saigner tous les bouts de ma carcasse immonde.\n" \ + "Ô vers! noirs compagnons sans oreille et sans yeux,\n" \ + "Voyez venir à vous un mort libre et joyeux;\n" \ + "Philosophes viveurs, fils de la pourriture,\n" \ + "À travers ma ruine allez donc sans remords,\n" \ + "Et dites-moi s'il est encor quelque torture\n" \ + "Pour ce vieux corps sans âme et mort parmi les morts!\n"; -const uint8_t elongdata[665] = "\x07" "\xff\x02\x94" - "Dans une terre grasse et pleine d'escargots\n" - "Je veux creuser moi-même une fosse profonde,\n" - "Où je puisse à loisir étaler mes vieux os\n" - "Et dormir dans l'oubli comme un requin dans l'onde.\n" - "Je hais les testaments et je hais les tombeaux;\n" - "Plutôt que d'implorer une larme du monde,\n" - "Vivant, j'aimerais mieux inviter les corbeaux\n" - "À saigner tous les bouts de ma carcasse immonde.\n" - "Ô vers! noirs compagnons sans oreille et sans yeux,\n" - "Voyez venir à vous un mort libre et joyeux;\n" - "Philosophes viveurs, fils de la pourriture,\n" - "À travers ma ruine allez donc sans remords,\n" - "Et dites-moi s'il est encor quelque torture\n" - "Pour ce vieux corps sans âme et mort parmi les morts!\n" - "\xfe"; +const uint8_t elongdata[665] = "\x07" "\xff\x02\x94" \ + "Dans une terre grasse et pleine d'escargots\n" \ + "Je veux creuser moi-même une fosse profonde,\n" \ + "Où je puisse à loisir étaler mes vieux os\n" \ + "Et dormir dans l'oubli comme un requin dans l'onde.\n" \ + "Je hais les testaments et je hais les tombeaux;\n" \ + "Plutôt que d'implorer une larme du monde,\n" \ + "Vivant, j'aimerais mieux inviter les corbeaux\n" \ + "À saigner tous les bouts de ma carcasse immonde.\n" \ + "Ô vers! noirs compagnons sans oreille et sans yeux,\n" \ + "Voyez venir à vous un mort libre et joyeux;\n" \ + "Philosophes viveurs, fils de la pourriture,\n" \ + "À travers ma ruine allez donc sans remords,\n" \ + "Et dites-moi s'il est encor quelque torture\n" \ + "Pour ce vieux corps sans âme et mort parmi les morts!\n" + "\xfe"; void test_tlv_encode_short (void)