annotate simtool/a38.c @ 127:141489d31667

fc-simtool: a38 command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 29 Jan 2021 03:40:20 +0000
parents
children 250d172662ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
127
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the a38 command for exercising
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * the SIM's RUN GSM ALGORITHM operation.
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <pcsclite.h>
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <winscard.h>
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "globals.h"
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 static
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 hexarg_16bytes(arg, databuf)
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char *arg;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 u_char *databuf;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned count;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 for (count = 0; ; count++) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 while (isspace(*arg))
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 arg++;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 if (!*arg)
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 break;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 fprintf(stderr, "error: invalid hex string input\n");
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return(-1);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (count >= 16) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 fprintf(stderr,
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 "error: hex string is longer than required 16 bytes\n");
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return(-1);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 databuf[count] = (decode_hex_digit(arg[0]) << 4) |
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 decode_hex_digit(arg[1]);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 arg += 2;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (count < 16) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr,
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 "error: hex string is shorter than required 16 bytes\n");
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 return(-1);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return(0);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 cmd_a38(argc, argv)
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 char **argv;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 u_char cmd[21];
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 int rc;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 /* RUN GSM ALGORITHM command APDU */
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 cmd[0] = 0xA0;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 cmd[1] = 0x88;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 cmd[2] = 0;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 cmd[3] = 0;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 cmd[4] = 16;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 rc = hexarg_16bytes(argv[1], cmd + 5);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (rc < 0)
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 return(rc);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 rc = apdu_exchange(cmd, 21);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (rc < 0)
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 return(rc);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if (sim_resp_sw != 0x9F0C) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 fprintf(stderr,
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 "error or unexpected SW response to RUN GSM ALGO: %04X\n",
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 sim_resp_sw);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 return(-1);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 /* GET RESPONSE follow-up */
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 cmd[1] = 0xC0;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 cmd[4] = 12;
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 rc = apdu_exchange(cmd, 5);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (rc < 0)
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 return(rc);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (sim_resp_sw != 0x9000) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 fprintf(stderr, "bad SW resp to GET RESPONSE: %04X\n",
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 sim_resp_sw);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 return(-1);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (sim_resp_data_len != 12) {
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 fprintf(stderr,
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 "error: GET RESPONSE returned %u bytes, expected 12\n",
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 sim_resp_data_len);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 return(-1);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 printf("SRES: %02X %02X %02X %02X\n", sim_resp_data[0],
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 sim_resp_data[1], sim_resp_data[2], sim_resp_data[3]);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 printf("Kc: %02X %02X %02X %02X %02X %02X %02X %02X\n",
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 sim_resp_data[4], sim_resp_data[5], sim_resp_data[6],
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 sim_resp_data[7], sim_resp_data[8], sim_resp_data[9],
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 sim_resp_data[10], sim_resp_data[11]);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 return(0);
141489d31667 fc-simtool: a38 command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }