-
- return 0;
-}
-
-int try3DesAuthentication( uint8_t *key, bool switch_off_field ){
-
- //uint32_t cuid = 0;
-
- des3_context ctx = { 0 };
-
- uint8_t random_a[8] = { 1,1,1,1,1,1,1,1 };
- uint8_t random_b[8] = { 0 };
- uint8_t enc_random_b[8] = { 0 };
- uint8_t rnd_ab[16] = { 0 };
- uint8_t iv[8] = { 0 };
-
- UsbCommand c = {CMD_MIFAREUC_AUTH1, {0x00}};
- SendCommand(&c);
- UsbCommand resp;
- if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500) ) return -1;
- if ( !(resp.arg[0] & 0xff) ) return -2;
-
- //cuid = resp.arg[1];
- memcpy(enc_random_b,resp.d.asBytes+1,8);
-
- des3_set2key_dec(&ctx, key);
- // context, mode, length, IV, input, output
- des3_crypt_cbc( &ctx, DES_DECRYPT, sizeof(random_b), iv , enc_random_b , random_b);
-
- rol(random_b,8);
- memcpy(rnd_ab ,random_a,8);
- memcpy(rnd_ab+8,random_b,8);
-
- des3_set2key_enc(&ctx, key);
- // context, mode, length, IV, input, output
- des3_crypt_cbc(&ctx, DES_ENCRYPT, sizeof(rnd_ab), enc_random_b, rnd_ab, rnd_ab);
-
- //Auth2
- c.cmd = CMD_MIFAREUC_AUTH2;
- c.arg[0] = switch_off_field;
- memcpy(c.d.asBytes, rnd_ab, 16);
- SendCommand(&c);
-
- if ( !WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
- if ( !(resp.arg[0] & 0xff)) return -2;
-
- uint8_t enc_resp[8] = { 0 };
- uint8_t resp_random_a[8] = { 0 };
- memcpy(enc_resp, resp.d.asBytes+1, 8);
-
- des3_set2key_dec(&ctx, key);
- // context, mode, length, IV, input, output
- des3_crypt_cbc( &ctx, DES_DECRYPT, 8, enc_random_b, enc_resp, resp_random_a);
-
- if ( !memcmp(resp_random_a, random_a, 8))
- return 1;