Mifare Classic Authenticate Sector with KeyB

Forum / MIFARE SDK / Mifare Classic Authenticate Sector with KeyB

  • 2. September 2015 at 12:53
    Hi there,
    So, I previously had a problem with Mifare classic cards when trying to read them using the Mifare Lite Android SDK. With lots of help from NXP and this forum, I proved that it was my phone which NFC controller could "not speak the Classic language" ;-).

    So I got a Samsung GS3 with NFC on it and retried some code. So now, the phone does actually pick up the Classic card using the NXP lite api calls (On Intent ..... On Classic card Detected).

    Now I have another problem, and I don't know exactly why I am having it.
    I am trying to authenticate with the card, but the auth is failing .
    I am using the "classic.authenticateSectorWithKeyB(sectorNo, Key2);" api from the Lite lirary. however I'm getting Smartcard exception (Value -1) which is not any of the exceptions below: EXCEPTIONTYPE_TAG_RESPONSE = 0;
    EXCEPTIONTYPE_SECURITY = 1;
    EXCEPTIONTYPE_USAGE = 2;
    EXCEPTIONTYPE_UNKNOWN = 3;


    Let me add this for clarity:
    1) I can 100% say with certainty that my Classic card is 100% functional.
    2) The Value of KeyB I am using for this card is 100% correct, because I can authenticate to Sector 2 with this Key when I use another C++ app and a PCSC reader on my laptop.

    Can anyone please help me here?

    My whole Auth code method will now follow:

    private void testClassicauthenticateSectorKeyB() {
    boolean res = false;
    String errMessage="";
    try {
    NxpLogUtils.d(TAG, "testClassicauthenticate Key b Sec 2, start");
    byte sectorNo = 2;

    byte bkeyVal1,bkeyVal2,bkeyVal3,bkeyVal4,bkeyVal5,bkeyVal6;
    bkeyVal1 = (byte)0x08;
    bkeyVal2 = (byte)0x9B;
    bkeyVal3 = (byte)0x07;
    bkeyVal4 = (byte)0x08;
    bkeyVal5 = (byte)0xEC;
    bkeyVal6 = (byte)0x62;
    //089B0708EC62
    byte Key2[] = {bkeyVal1,bkeyVal2,bkeyVal3,bkeyVal4,bkeyVal5,bkeyVal6};



    classic.authenticateSectorWithKeyB(sectorNo, Key2);
    res = true;
    } catch (IOException e) {
    e.printStackTrace();
    }catch (ClassicException e){
    String eCode="";
    /*
    EXCEPTIONTYPE_TAG_RESPONSE = 0;
    EXCEPTIONTYPE_SECURITY = 1;
    EXCEPTIONTYPE_USAGE = 2;
    EXCEPTIONTYPE_UNKNOWN = 3;
    */

    switch (e.getErrorCode()){
    case ClassicException.EXCEPTIONTYPE_SECURITY:
    eCode = "EXCEPTIONTYPE_SECURITY";
    break;
    case ClassicException.EXCEPTIONTYPE_TAG_RESPONSE:
    eCode = "EXCEPTIONTYPE_SECURITY";
    break;
    case ClassicException.EXCEPTIONTYPE_UNKNOWN:
    eCode = "EXCEPTIONTYPE_SECURITY";
    break;
    case ClassicException.EXCEPTIONTYPE_USAGE:
    eCode = "EXCEPTIONTYPE_SECURITY";
    break;
    case -1 :
    eCode = "ERR_UNKNOWN";
    break;

    }

    errMessage = e.getMessage() + "\nCode:" + e.getErrorCode() + "," + eCode;
    e.printStackTrace();
    }

    showMessage("authenticate Sec 2 Key B:" + res + "\nCLassic ERR:" + errMessage, 'd');
    NxpLogUtils.d(TAG, "testClassicauthenticate, result is " + res);
    NxpLogUtils.d(TAG, "testClassicauthenticate, End");
    }

    + 0  |  - 1

    Re: Mifare Classic Authenticate Sector with KeyB

    2. September 2015 at 14:40
    Hi Philip,

    I did not check you code, but from reading your post, first thing that came to my mind is, that your NFC Controller inside your phone does not recognize your MF Classic as classic card and confuses it with some other TAG / CARD, and throws you that error.

    But this is a wide guess, when I will have more time, I will try to look into this one.

    Best,
    David
    + 1  |  - 0

    Re: Mifare Classic Authenticate Sector with KeyB

    3. September 2015 at 10:32
    Hi David,
    Thank you for replying.
    I'm not sure you understood my first message completely.
    At first try, I did actually have another type of phone (Prestigio) on which I tested. This phone actually had the issue you are referring to, meaning, this phone's NFC controller did not recognize the Classic card.

    So my next step was to get a new phone, Samsung Galaxy S3. On this phone, when I use the SampleNxpNfcLibLite app and the NXP TagInfo app's (For your reference I put the output from Taginfo right at the end of this message in RED ,both these apps indicated that the device supports Classic Card.
    Also the SampleNxpNfcLibLite app returns "true"

    So I copied the SampleNxpNfcLibLite app and added another function specifically for the authenticate sector with keyB, which is the code in my first message above

    The code below in blue is the actual code from the SampleNxpNfcLibLite APP, this code executes and always returns true - no exceptions. My code above which I placed Just below the original code in the same app, is giving the -1 Exception.

    /** Classic Authenticate Operations. */
    private void testClassicauthenticate() {
    boolean res = false;
    try {
    NxpLogUtils.d(TAG, "testClassicauthenticate, start");
    byte sectorNo = 2;
    byte[] appId = new byte[] { 0x11, 0x11, 0x11 };
    byte[] key = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
    (byte) 0xFF, (byte) 0xFF };

    classic.authenticate(sectorNo, appId, key);
    res = true;
    } catch (SmartCardException e) {
    e.printStackTrace();
    }
    showMessage("authenticate :" + res, 'd');
    NxpLogUtils.d(TAG, "testClassicauthenticate, result is " + res);
    NxpLogUtils.d(TAG, "testClassicauthenticate, End");
    }



    For your reference:



    3.0
    1970-01-20 21:05:29
    <title>NXP Semiconductors MIFARE Classic (MF1S50) tag</title>
    9B:62:EC:07
    false
    <section>


    NXP Semiconductors




    MIFARE Classic (MF1S50)


    </section>
    <section>


    Maximum NDEF storage size after format: 716 bytes


    </section>
    <section>


    1 kB
    ► 16 sectors, with 4 blocks per sector
    ► 64 blocks, with 16 bytes per block


    </section>
    <section>


    MIFARE Classic compatible
    ISO/IEC 14443-3 (Type A) compatible
    ISO/IEC 14443-2 (Type A) compatible




    Tag description:
    ► TAG: Tech [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
    android.nfc.tech.NdefFormatable
    android.nfc.tech.MifareClassic
    android.nfc.tech.NfcA
    ► Maximum transceive length: 253 bytes
    ► Default maximum transceive time-out: 618 ms




    ID: 9B:62:EC:07
    ATQA: 0x0400
    SAK: 0x08




    <address>0</address>



    <address>1</address>



    <address>2</address>



    <address>3</address>





    <address>4</address>
    20 20 20 20 43 72 65 64 69 74 20 32 20 20 20 20


    <address>5</address>
    12 34 36 29 11 14 11 30 01 14 27 01 00 10 00 10


    <address>6</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01


    <address>7</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>8</address>
    11 11 11 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>9</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>10</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>11</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>12</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>13</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>14</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>15</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>16</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>17</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>18</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>19</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>20</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>21</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>22</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>23</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>24</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>25</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>26</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>27</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>28</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>29</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>30</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>31</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>32</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>33</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>34</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>35</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>36</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>37</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>38</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>39</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>40</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>41</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>42</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>43</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>44</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>45</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>46</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>47</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>48</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>49</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>50</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>51</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>52</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>53</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>54</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>55</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>56</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>57</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>58</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>59</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF


    <address>60</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>61</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>62</address>
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


    <address>63</address>
    00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF
    FF FF FF FF FF FF
    FF FF FF FF FF FF



    r/R=read, w/W=write, i/I=increment,
    d=decr/transfer/restore, x=r+w, X=R+W
    data block: r/w/i/d:key A|B, R/W/I:key B only,
    I/i implies d, *=value block
    trailer (order: key A, AC, key B): r/w:key A,
    W:key B, R:key A|B, (r)=readable key
    AC: W implies R+r, R implies r


    </

    + 0  |  - 0

    Re: Mifare Classic Authenticate Sector with KeyB

    15. October 2015 at 13:18
    Does any one know why the NXP Classic library will return a -1 ClassicException? Is the -1 code documented for the function below?

    classic.authenticateSectorWithKeyB(sectorNo, key);
    + 0  |  - 0
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.