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