static char *ibping_serv(void) { void *umad; void *mad; char *data; DEBUG("starting to serve..."); while ((umad = mad_receive_via(0, -1, srcport))) { if (umad_status(umad) == 0) { mad = umad_get_mad(umad); data = (char *)mad + IB_VENDOR_RANGE2_DATA_OFFS; memcpy(data, host_and_domain, IB_VENDOR_RANGE2_DATA_SIZE); DEBUG("Pong: %s", data); if (mad_respond_via(umad, 0, 0, srcport) < 0) DEBUG("respond failed"); } mad_free(umad); } DEBUG("server out"); return 0; }
void test_mifare_application (void) { /* Card publisher part */ MadAid aid = { 0x22, 0x42 }; Mad mad = mad_new (2); int i; cut_assert_not_null (mad, cut_message ("mad_new() failed")); MifareClassicSectorNumber *s_alloc = mifare_application_alloc (mad, aid, 3*3*16); cut_assert_not_null (s_alloc, cut_message ("mifare_application_alloc() failed")); MifareClassicSectorNumber *s_found = mifare_application_find (mad, aid); cut_assert_not_null (s_found, cut_message ("mifare_application_alloc() failed")); for (i = 0; s_alloc[i]; i++) { cut_assert_equal_int (s_alloc[i], s_found[i], cut_message ("Allocated and found blocks don't match at position %d", i)); } cut_assert_equal_int (0, s_alloc[i], cut_message ("Invalid size")); cut_assert_equal_int (0, s_found[i], cut_message ("Invalid size")); mifare_application_free (mad, aid); free (s_alloc); free (s_found); s_found = mifare_application_find (mad, aid); cut_assert_null (s_found, cut_message ("mifare_application_free() failed")); s_alloc = mifare_application_alloc (mad, aid, 15*16 + 1*16 + 1); cut_assert_not_null (s_alloc, cut_message ("mifare_application_alloc() failed")); s_found = mifare_application_find (mad, aid); cut_assert_not_null (s_found, cut_message ("mifare_application_alloc() failed")); for (i = 0; s_alloc[i]; i++) { cut_assert_equal_int (s_alloc[i], s_found[i], cut_message ("Allocated and found blocks don't match at position %d", i)); } cut_assert_equal_int (0, s_alloc[i], cut_message ("Invalid size")); cut_assert_equal_int (0, s_found[i], cut_message ("Invalid size")); mifare_application_free (mad, aid); free (s_alloc); free (s_found); s_found = mifare_application_find (mad, aid); cut_assert_null (s_found, cut_message ("mifare_application_free() failed")); mad_free (mad); }
void test_mad_sector_0x00_crc8 (void) { int res; Mad mad = mad_new (1); cut_assert_not_null (mad, cut_message ("mad_new() failed")); res = mad_set_card_publisher_sector (mad, 0x01); /* Block 1 */ MadAid aid1 = { 0x01, 0x08 }; mad_set_aid (mad, 1, aid1); mad_set_aid (mad, 2, aid1); mad_set_aid (mad, 3, aid1); /* Block 2 */ MadAid empty_aid = { 0x00, 0x00 }; mad_set_aid (mad, 4, empty_aid); mad_set_aid (mad, 5, empty_aid); mad_set_aid (mad, 6, empty_aid); MadAid aid2 = { 0x04, 0x00 }; mad_set_aid (mad, 7, aid2); /* Block 3 */ MadAid aid3 = { 0x03, 0x10 }; mad_set_aid (mad, 8, aid3); mad_set_aid (mad, 9, aid3); MadAid aid4 = { 0x02, 0x10 }; mad_set_aid (mad, 10, aid4); mad_set_aid (mad, 11, aid4); mad_set_aid (mad, 12, empty_aid); mad_set_aid (mad, 13, empty_aid); mad_set_aid (mad, 14, empty_aid); MadAid aid5 = { 0x11, 0x30 }; mad_set_aid (mad, 15, aid5); res = sector_0x00_crc8 (mad); cut_assert_equal_int(0x89, res, cut_message ("Sample CRC should match")); mad_free (mad); }
//-------------------------------------------------------------------------------------------- mad_t * mad_reconstruct( mad_t * pmad ) { /// @details BB@> initialize the character data to safe values /// since we use memset(..., 0, ...), all = 0, = false, and = 0.0f /// statements are redundant int action; if ( NULL == pmad ) return NULL; mad_free( pmad ); memset( pmad, 0, sizeof( *pmad ) ); strncpy( pmad->name, "*NONE*", SDL_arraysize( pmad->name ) ); // Clear out all actions and reset to invalid for ( action = 0; action < ACTION_COUNT; action++ ) { pmad->action_map[action] = ACTION_COUNT; } return pmad; }
void mad_desc_del(D *d) { assert(d); mad_free(d->var_ords); mad_free(d->map_ords); mad_free(d->monos); mad_free(d->ords); mad_free(d->To); mad_free(d->Tv); mad_free(d->ord2idx); mad_free(d->tv2to); mad_free(d->to2tv); mad_free(d->H); if (d->var_names_) { for (int i = 0; i < d->nmv; ++i) mad_free(d->var_names_[i]); mad_free(d->var_names_); } if (d->L) { // if L exists, then L_idx exists too for (int i = 0; i < 1 + d->mo * d->mo/2; ++i) { mad_free(d->L[i]); if (d->L_idx[i]) { mad_free(*d->L_idx[i]); // allocated as single block mad_free( d->L_idx[i]); } } mad_free(d->L); mad_free(d->L_idx); } if (d->ocs) { int nb_threads = omp_get_num_procs(); for (int t = 0; t < nb_threads; ++t) mad_free(d->ocs[t]); mad_free(d->ocs); } for(int i=0; i < desc_max_temps; i++) { mad_tpsa_del (d-> t[i]); mad_ctpsa_del(d->ct[i]); } // remove descriptor from global array Ds[d->id] = NULL; mad_free(d); }
void test_mifare_classic_mad (void) { MifareClassicKey key_a_transport = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; MifareClassicKey key_b_sector_00 = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; MifareClassicKey key_b_sector_10 = { 0x1a, 0x98, 0x2c, 0x7e, 0x45 ,0x9a }; MifareClassicBlock tb; Mad mad; int res; /* __ __ _ ___ _ * | \/ | /_\ | \__ _/ | * | |\/| |/ _ \| |) \ V / | * |_| |_/_/ \_\___/ \_/|_| */ mad = mad_new (1); cut_assert_not_null (mad, cut_message ("mad_new() failed")); // Prepare sector 0x00 for writing a MAD. res = mifare_classic_authenticate (tag, 0x00, key_a_transport, MFC_KEY_A); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); mifare_classic_trailer_block (&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_00); res = mifare_classic_write (tag, 0x03, tb); cut_assert_equal_int (0, res, cut_message ("mifare_classic_write() failed")); // Write the empty MAD res = mad_write (tag, mad, key_b_sector_00, NULL); cut_assert_equal_int (0, res, cut_message ("mad_write() failed")); // 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 data; res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); res = mifare_classic_read (tag, 0x01, &data); cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_memory (ref_01, sizeof (ref_01), data, sizeof (data), cut_message ("Wrong data")); res = mifare_classic_read (tag, 0x02, &data); 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")); Mad mad2 = mad_read (tag); 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 >> "; MadAid aid = { .function_cluster_code = 0x01, .application_code = 0x12 }; // Write some data in the application MifareClassicSectorNumber *sectors = mifare_application_alloc (mad, aid, sizeof (application_data)); cut_assert_not_null (sectors, cut_message ("mifare_application_alloc() failed")); free (sectors); res = mad_write (tag, mad, key_b_sector_00, NULL); cut_assert_equal_int (0, res, cut_message ("mad_write() failed")); ssize_t s = mifare_application_write (tag, mad, aid, &application_data, sizeof (application_data), key_a_transport, MFC_KEY_A); cut_assert_equal_int (sizeof (application_data), s, cut_message ("mifare_application_write() failed")); char read_buf[500]; // Read it again s = mifare_application_read (tag, mad, aid, read_buf, sizeof (application_data), key_a_transport, MFC_KEY_A); cut_assert_equal_int (sizeof (application_data), s, cut_message ("mifare_application_read() failed")); cut_assert_equal_memory (application_data, sizeof (application_data), read_buf, s, cut_message ("Wrong application data")); mad_free (mad); mad_free (mad2); // Revert to the transport configuration res = mifare_classic_authenticate (tag, 0x00, key_b_sector_00, MFC_KEY_B); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); res = mifare_classic_format_sector (tag, 0x00); cut_assert_equal_int (0, res, cut_message ("mifare_classic_format_sector() failed")); /* __ __ _ ___ ___ * | \/ | /_\ | \__ _|_ ) * | |\/| |/ _ \| |) \ V // / * |_| |_/_/ \_\___/ \_//___| */ if (freefare_get_tag_type (tag) != CLASSIC_4K) { cut_omit ("MADv2 requires a MIFARE Classic 4K to be tested"); } mad = mad_new (2); cut_assert_not_null (mad, cut_message ("mad_new() failed")); // Prepare sector 0x00 for writing a MAD. res = mifare_classic_authenticate (tag, 0x00, key_a_transport, MFC_KEY_A); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); mifare_classic_trailer_block (&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_00); res = mifare_classic_write (tag, 0x03, tb); cut_assert_equal_int (0, res, cut_message ("mifare_classic_write() failed")); // Prepare sector 0x10 for writing a MAD. res = mifare_classic_authenticate (tag, 0x40, key_a_transport, MFC_KEY_A); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); mifare_classic_trailer_block (&tb, key_a_transport, 00, 00, 00, 06, 0x00, key_b_sector_10); res = mifare_classic_write (tag, 0x43, tb); cut_assert_equal_int (0, res, cut_message ("mifare_classic_write() failed")); // Write the empty MAD res = mad_write (tag, mad, key_b_sector_00, key_b_sector_10); cut_assert_equal_int (0, res, cut_message ("mad_write() failed")); // Check the empty MAD res = mifare_classic_authenticate (tag, 0x01, mad_public_key_a, MFC_KEY_A); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); res = mifare_classic_read (tag, 0x01, &data); cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_memory (ref_01, sizeof (ref_01), data, sizeof (data), cut_message ("Wrong data")); res = mifare_classic_read (tag, 0x02, &data); 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 }; 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")); res = mifare_classic_read (tag, 0x40, &data); cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_memory (ref_40, sizeof (ref_01), data, sizeof (data), cut_message ("Wrong data")); res = mifare_classic_read (tag, 0x41, &data); cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_memory (ref_41, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data")); res = mifare_classic_read (tag, 0x42, &data); cut_assert_equal_int (0, res, cut_message ("mad_read() failed")); cut_assert_equal_memory (ref_42, sizeof (ref_02), data, sizeof (data), cut_message ("Wrong data")); mad2 = mad_read (tag); cut_assert_not_null (mad2, cut_message ("mad_read() failed")); cut_assert_equal_memory (mad, sizeof (mad), mad2, sizeof (mad2), cut_message ("Wrong MAD")); // Write some data in the application sectors = mifare_application_alloc (mad, aid, sizeof (application_data)); cut_assert_not_null (sectors, cut_message ("mifare_application_alloc() failed")); free (sectors); res = mad_write (tag, mad, key_b_sector_00, key_b_sector_10); cut_assert_equal_int (0, res, cut_message ("mad_write() failed")); s = mifare_application_write (tag, mad, aid, &application_data, sizeof (application_data), key_a_transport, MFC_KEY_A); cut_assert_equal_int (sizeof (application_data), s, cut_message ("mifare_application_write() failed")); // Read it again s = mifare_application_read (tag, mad, aid, read_buf, sizeof (application_data), key_a_transport, MFC_KEY_A); cut_assert_equal_int (sizeof (application_data), s, cut_message ("mifare_application_read() failed")); cut_assert_equal_memory (application_data, sizeof (application_data), read_buf, s, cut_message ("Wrong application data")); mad_free (mad); mad_free (mad2); // Revert to the transport configuration res = mifare_classic_authenticate (tag, 0x00, key_b_sector_00, MFC_KEY_B); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); res = mifare_classic_format_sector (tag, 0x00); cut_assert_equal_int (0, res, cut_message ("mifare_classic_format_sector() failed")); res = mifare_classic_authenticate (tag, 0x40, key_b_sector_10, MFC_KEY_B); cut_assert_equal_int (0, res, cut_message ("mifare_classic_authenticate() failed")); res = mifare_classic_format_sector (tag, 0x10); cut_assert_equal_int (0, res, cut_message ("mifare_classic_format_sector() failed")); }
void test_mad (void) { int res; Mad mad = mad_new (1); cut_assert_not_null (mad, cut_message ("Can create a new MAD")); cut_assert_equal_int (1, mad_get_version (mad), cut_message ("Wrong default MAD version")); mad_set_version (mad, 2); cut_assert_equal_int (2, mad_get_version (mad), cut_message ("Can't change MAD version")); cut_assert_equal_int (0, mad_get_card_publisher_sector (mad), cut_message ("Wrong default MAD publisher")); res = mad_set_card_publisher_sector (mad, 13); cut_assert_equal_int (0, res, cut_message ("mad_set_card_publisher_sector() returned an error.")); cut_assert_equal_int (13, mad_get_card_publisher_sector (mad), cut_message ("Wrong publisher sector")); res = mad_set_card_publisher_sector (mad, 0xff); cut_assert_equal_int (-1, res, cut_message ("Invalid sector")); cut_assert_equal_int (13, mad_get_card_publisher_sector (mad), cut_message ("Previous publisher sector value")); MadAid aid = { .function_cluster_code = 0, .application_code = 0 }; res = mad_get_aid (mad, 3, &aid); cut_assert_equal_int (0, res, cut_message ("mad_get_aid() failed")); cut_assert_equal_int (0, aid.function_cluster_code, cut_message ("Invalid default value")); cut_assert_equal_int (0, aid.application_code, cut_message ("Invalid default value")); aid.function_cluster_code = 0xc0; aid.application_code = 0x42; res = mad_set_aid (mad, 3, aid); cut_assert_equal_int (0, res, cut_message ("mad_set_aid() failed")); res = mad_get_aid (mad, 3, &aid); cut_assert_equal_int (0, res, cut_message ("mad_get_aid() failed")); cut_assert_equal_int (0xC0, aid.function_cluster_code, cut_message ("Invalid value")); cut_assert_equal_int (0x42, aid.application_code, cut_message ("Invalid value")); mad_free (mad); } #define CRC_PRESET 0xc7 void test_mad_crc8_basic (void) { uint8_t crc; const uint8_t crc_value = 0x42; /* Check integrity */ crc = CRC_PRESET; nxp_crc(&crc, crc_value); uint8_t save = crc; crc = CRC_PRESET; nxp_crc(&crc, crc_value); nxp_crc(&crc, save); cut_assert_equal_int (0x00, crc, cut_message ("CRC should verify crc(message + crc(message)) = 0")); }