changeset 127:141489d31667

fc-simtool: a38 command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 29 Jan 2021 03:40:20 +0000
parents f18b87115cca
children 95c2a67e1219
files simtool/Makefile simtool/a38.c simtool/dispatch.c
diffstat 3 files changed, 98 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Fri Jan 29 03:18:17 2021 +0000
+++ b/simtool/Makefile	Fri Jan 29 03:40:20 2021 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-simtool
-OBJS=	alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chv.o \
+OBJS=	a38.o alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chv.o \
 	dispatch.o exit.o globals.o hexdump.o hexread.o hlread.o main.o names.o\
 	pbcommon.o pbdump.o pberase.o pbupdate.o readcmd.o readops.o \
 	saverestore.o script.o select.o sysmo.o telsum.o writecmd.o writeops.o
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/a38.c	Fri Jan 29 03:40:20 2021 +0000
@@ -0,0 +1,95 @@
+/*
+ * This module implements the a38 command for exercising
+ * the SIM's RUN GSM ALGORITHM operation.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+
+static
+hexarg_16bytes(arg, databuf)
+	char *arg;
+	u_char *databuf;
+{
+	unsigned count;
+
+	for (count = 0; ; count++) {
+		while (isspace(*arg))
+			arg++;
+		if (!*arg)
+			break;
+		if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
+			fprintf(stderr, "error: invalid hex string input\n");
+			return(-1);
+		}
+		if (count >= 16) {
+			fprintf(stderr,
+			"error: hex string is longer than required 16 bytes\n");
+			return(-1);
+		}
+		databuf[count] = (decode_hex_digit(arg[0]) << 4) |
+				 decode_hex_digit(arg[1]);
+		arg += 2;
+	}
+	if (count < 16) {
+		fprintf(stderr,
+		"error: hex string is shorter than required 16 bytes\n");
+		return(-1);
+	}
+	return(0);
+}
+
+cmd_a38(argc, argv)
+	char **argv;
+{
+	u_char cmd[21];
+	int rc;
+
+	/* RUN GSM ALGORITHM command APDU */
+	cmd[0] = 0xA0;
+	cmd[1] = 0x88;
+	cmd[2] = 0;
+	cmd[3] = 0;
+	cmd[4] = 16;
+	rc = hexarg_16bytes(argv[1], cmd + 5);
+	if (rc < 0)
+		return(rc);
+	rc = apdu_exchange(cmd, 21);
+	if (rc < 0)
+		return(rc);
+	if (sim_resp_sw != 0x9F0C) {
+		fprintf(stderr,
+		"error or unexpected SW response to RUN GSM ALGO: %04X\n",
+			sim_resp_sw);
+		return(-1);
+	}
+	/* GET RESPONSE follow-up */
+	cmd[1] = 0xC0;
+	cmd[4] = 12;
+	rc = apdu_exchange(cmd, 5);
+	if (rc < 0)
+		return(rc);
+	if (sim_resp_sw != 0x9000) {
+		fprintf(stderr, "bad SW resp to GET RESPONSE: %04X\n",
+			sim_resp_sw);
+		return(-1);
+	}
+	if (sim_resp_data_len != 12) {
+		fprintf(stderr,
+			"error: GET RESPONSE returned %u bytes, expected 12\n",
+			sim_resp_data_len);
+		return(-1);
+	}
+	printf("SRES: %02X %02X %02X %02X\n", sim_resp_data[0],
+		sim_resp_data[1], sim_resp_data[2], sim_resp_data[3]);
+	printf("Kc: %02X %02X %02X %02X %02X %02X %02X %02X\n",
+		sim_resp_data[4], sim_resp_data[5], sim_resp_data[6],
+		sim_resp_data[7], sim_resp_data[8], sim_resp_data[9],
+		sim_resp_data[10], sim_resp_data[11]);
+	return(0);
+}
--- a/simtool/dispatch.c	Fri Jan 29 03:18:17 2021 +0000
+++ b/simtool/dispatch.c	Fri Jan 29 03:40:20 2021 +0000
@@ -8,6 +8,7 @@
 #include <strings.h>
 #include <stdlib.h>
 
+extern int cmd_a38();
 extern int cmd_change_chv();
 extern int cmd_disable_chv();
 extern int cmd_enable_chv();
@@ -47,6 +48,7 @@
 	int maxargs;
 	int (*func)();
 } cmdtab[] = {
+	{"a38", 1, 1, cmd_a38},
 	{"change-chv1", 2, 2, cmd_change_chv},
 	{"change-chv2", 2, 2, cmd_change_chv},
 	{"change-pin1", 2, 2, cmd_change_chv},