1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
package simpleapdu;
import applets.SimpleECCApplet;
import static applets.SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE;
import static applets.SimpleECCApplet.ECTEST_SET_INVALIDCURVE;
import javacard.framework.ISO7816;
import javacard.security.CryptoException;
import javacard.security.KeyPair;
import javax.smartcardio.ResponseAPDU;
import org.bouncycastle.util.Arrays;
/**
*
* @author Petr Svenda petr@svenda.com
*/
public class SimpleAPDU {
static CardMngr cardManager = new CardMngr();
private final static byte SELECT_ECTESTERAPPLET[] = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0a,
(byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31};
private static byte TESTECSUPPORTALL_FP[] = {(byte) 0xB0, (byte) 0x5E, (byte) 0x00, (byte) 0x00, (byte) 0x00};
private static byte TESTECSUPPORTALL_F2M[] = {(byte) 0xB0, (byte) 0x5F, (byte) 0x00, (byte) 0x00, (byte) 0x00};
private static byte TESTECSUPPORTALL_LASTUSEDPARAMS[] = {(byte) 0xB0, (byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00};
private static byte TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB[] = {(byte) 0xB0, (byte) 0x70, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
static short getShort(byte[] array, int offset) {
return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF));
}
static void setShort(byte[] array, int offset, short value) {
array[offset + 1] = (byte) (value & 0xFF);
array[offset] = (byte) ((value >> 8) & 0xFF);
}
static void testFPkeyGen_setNumRepeats(byte[] apduArray, short numRepeats) {
// num repeats starts at index 5
setShort(apduArray, 5, numRepeats);
}
static void testFPkeyGen_rewindOnSuccess(byte[] apduArray, boolean bRewind) {
// rewind info at index 7
apduArray[7] = bRewind ? (byte) 1 : (byte) 0;
}
static void testFPkeyGen_setCorruptionType(byte[] apduArray, short corruptionType) {
// corruptionType starts at index 7
setShort(apduArray, 7, corruptionType);
}
public static void main(String[] args) {
try {
//
// REAL CARDS
//
if (cardManager.ConnectToCard()) {
// Select our application on card
cardManager.sendAPDU(SELECT_ECTESTERAPPLET);
// Test setting invalid parameter B of curev
byte[] testAPDU = Arrays.clone(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB);
//testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_LASTBYTEINCREMENT);
testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_ONEBYTERANDOM);
//testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_FULLRANDOM);
testFPkeyGen_setNumRepeats(testAPDU, (short) 1000);
testFPkeyGen_rewindOnSuccess(testAPDU, false);
ResponseAPDU resp_fp_keygen = cardManager.sendAPDU(testAPDU);
ResponseAPDU resp_keygen_params = cardManager.sendAPDU(TESTECSUPPORTALL_LASTUSEDPARAMS);
PrintECKeyGenInvalidCurveB(resp_fp_keygen);
PrintECKeyGenInvalidCurveB_lastUserParams(resp_keygen_params);
// Test support for different types of curves
ResponseAPDU resp_fp = cardManager.sendAPDU(TESTECSUPPORTALL_FP);
ResponseAPDU resp_f2m = cardManager.sendAPDU(TESTECSUPPORTALL_F2M);
PrintECSupport(resp_fp);
PrintECSupport(resp_f2m);
cardManager.DisconnectFromCard();
} else {
System.out.println("Failed to connect to card");
}
} catch (Exception ex) {
System.out.println("Exception : " + ex);
}
}
static String getPrintError(short code) {
if (code == ISO7816.SW_NO_ERROR) {
return "OK\t(0x9000)";
}
else {
String codeStr = "unknown";
if (code == CryptoException.ILLEGAL_VALUE) {
codeStr = "ILLEGAL_VALUE";
}
if (code == CryptoException.UNINITIALIZED_KEY) {
codeStr = "UNINITIALIZED_KEY";
}
if (code == CryptoException.NO_SUCH_ALGORITHM) {
codeStr = "NO_SUCH_ALG";
}
if (code == CryptoException.INVALID_INIT) {
codeStr = "INVALID_INIT";
}
if (code == CryptoException.ILLEGAL_USE) {
codeStr = "ILLEGAL_USE";
}
if (code == SimpleECCApplet.SW_SKIPPED) {
codeStr = "skipped";
}
if (code == SimpleECCApplet.SW_KEYPAIR_GENERATED_INVALID) {
codeStr = "SW_KEYPAIR_GENERATED_INVALID";
}
if (code == SimpleECCApplet.SW_INVALID_CORRUPTION_TYPE) {
codeStr = "SW_INVALID_CORRUPTION_TYPE";
}
return String.format("fail\t(%s,\t0x%4x)", codeStr, code);
}
}
enum ExpResult {
SHOULD_SUCCEDD,
MAY_FAIL,
MUST_FAIL
}
static int VerifyPrintResult(String message, byte expectedTag, byte[] buffer, int bufferOffset, ExpResult expRes) {
if (bufferOffset >= buffer.length) {
System.out.println("No more data returned");
}
else {
if (buffer[bufferOffset] != expectedTag) {
System.out.println("ERROR: mismatched tag");
assert(buffer[bufferOffset] == expectedTag);
}
bufferOffset++;
short resCode = getShort(buffer, bufferOffset);
bufferOffset += 2;
boolean bHiglight = false;
if ((expRes == ExpResult.MUST_FAIL) && (resCode == ISO7816.SW_NO_ERROR)) {
bHiglight = true;
}
if ((expRes == ExpResult.SHOULD_SUCCEDD) && (resCode != ISO7816.SW_NO_ERROR)) {
bHiglight = true;
}
if (bHiglight) {
System.out.println(String.format("!! %-50s%s", message, getPrintError(resCode)));
}
else {
System.out.println(String.format(" %-50s%s", message, getPrintError(resCode)));
}
}
return bufferOffset;
}
static void PrintECSupport(ResponseAPDU resp) {
byte[] buffer = resp.getData();
System.out.println();System.out.println();
int bufferOffset = 0;
while (bufferOffset < buffer.length) {
assert(buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR);
bufferOffset++;
String ecType = "unknown";
if (buffer[bufferOffset] == KeyPair.ALG_EC_FP) {
ecType = "ALG_EC_FP";
}
if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) {
ecType = "ALG_EC_F2M";
}
System.out.println(String.format("%-53s%s", "EC type:", ecType));
bufferOffset++;
short keyLen = getShort(buffer, bufferOffset);
System.out.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen));
bufferOffset += 2;
bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
bufferOffset = VerifyPrintResult("Generate key with def curve (fails if no def):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_DEFCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
bufferOffset = VerifyPrintResult("ECDH agreement with valid point:", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_VALID_POINT, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_INVALID_POINT, buffer, bufferOffset, ExpResult.MUST_FAIL);
bufferOffset = VerifyPrintResult("Set invalid custom curve (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL);
bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL);
System.out.println();
}
}
static void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) {
byte[] buffer = resp.getData();
System.out.println();
System.out.println();
int bufferOffset = 0;
while (bufferOffset < buffer.length) {
assert (buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR);
bufferOffset++;
String ecType = "unknown";
if (buffer[bufferOffset] == KeyPair.ALG_EC_FP) {
ecType = "ALG_EC_FP";
}
if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) {
ecType = "ALG_EC_F2M";
}
System.out.println(String.format("%-53s%s", "EC type:", ecType));
bufferOffset++;
short keyLen = getShort(buffer, bufferOffset);
System.out.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen));
bufferOffset += 2;
short numRepeats = getShort(buffer, bufferOffset);
bufferOffset += 2;
System.out.println(String.format("Executed repeats before unexpected error: %d times", numRepeats));
bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
while (bufferOffset < buffer.length) {
bufferOffset = VerifyPrintResult("Set invalid custom curve:", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL);
bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD);
}
System.out.println();
}
}
static void PrintECKeyGenInvalidCurveB_lastUserParams(ResponseAPDU resp) {
byte[] buffer = resp.getData();
short offset = 0;
System.out.print("Last used value of B: ");
while (offset < buffer.length) {
System.out.print(String.format("%x ", buffer[offset]));
offset++;
}
}
}
|