Renamed classes
[YkNeoCR.git] / src / org / average / nfcauthcr / YkNeo.java
1 package org.average.nfcauthcr;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5
6 import android.nfc.NfcAdapter;
7 import android.nfc.Tag;
8 import android.nfc.TagLostException;
9 import android.nfc.tech.IsoDep;
10 //import android.util.Log;
11
12 public class YkNeo {
13
14         // This is a CCID APDU, ISO 7816-4.
15         // 00 A4 04 00 xx AID - GlobalPlatform - SELECT
16         // Lc, send data = 07: A0 00 00 05 27 20 01
17         // Le, recv data = 00
18         private static final byte[] selectApdu =
19                 {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
20                  0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
21
22         private static final byte SLOT_CHAL_HMAC1 = 0x30;
23         private static final byte SLOT_CHAL_HMAC2 = 0x38;
24
25         public static final byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
26                                                 byte[] challenge) {
27                 try {
28                         isoTag.connect();
29                         byte[] resp = isoTag.transceive(selectApdu);
30                         int length = resp.length;
31                         //Log.v(TAG, "response to select length is " + length);
32                         if (resp[length - 2] != (byte)0x90 ||
33                             resp[length - 1] != 0x00) {
34                                 //Log.v(TAG, "Wrong response to select");
35                                 //Toast.makeText(this, R.string.tag_error,
36                                 //              Toast.LENGTH_LONG).show();
37                                 return null;
38                         }
39                         byte[] crApdu = new byte[69];
40                         crApdu[0] = 0x00; // CLA
41                         crApdu[1] = 0x01; // INS
42                         switch (slot) {
43                         case 1: crApdu[2] = SLOT_CHAL_HMAC1; break; // P1
44                         case 2: crApdu[2] = SLOT_CHAL_HMAC2; break; // P1
45                         }
46                         crApdu[3] = 0x00; // P2
47                         crApdu[4] = 63;   // Lc
48                         System.arraycopy(challenge, 0, crApdu, 5,
49                                                 challenge.length); // Payload
50                         crApdu[crApdu.length-1] = 22; // Le
51                         resp = isoTag.transceive(crApdu);
52                         length = resp.length;
53                         if (resp[length - 2] != (byte)0x90 ||
54                             resp[length - 1] != 0x00) {
55                                 //Log.v(TAG, "Wrong response to challenge");
56                                 //Toast.makeText(this, R.string.tag_error,
57                                 //              Toast.LENGTH_LONG).show();
58                                 return null;
59                         }
60                         //Log.v(TAG, "response to challenge length is " + length);
61                         return Arrays.copyOf(resp, length-2);
62                 } catch (TagLostException e) {
63                         //Log.v(TAG, e.getMessage());
64                         //Toast.makeText(this,
65                         //      R.string.tag_lost, Toast.LENGTH_LONG).show();
66                 } catch (IOException e) {
67                         //Log.v(TAG, e.getMessage());
68                         //Toast.makeText(this,
69                         //      getText(R.string.tag_error) +
70                         //      e.getMessage(),
71                         //      Toast.LENGTH_LONG).show();
72                 }
73                 return null;
74         }
75 }