New unit test for XOR-2G authentication

Let's test if the XOR-2G algorithm does what it's supposed to do.

Change-Id: I7014258751624ff18c51912b6348c3cd876bb23f
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c637c16..eeefd3e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -49,6 +49,7 @@
 		 smscb/smscb_test                                       \
 		 smscb/gsm0341_test                                     \
 		 smscb/cbsp_test                                        \
+		 auth/xor2g_test                                        \
 		 $(NULL)
 
 if ENABLE_MSGFILE
@@ -110,6 +111,9 @@
 auth_milenage_test_SOURCES = auth/milenage_test.c
 auth_milenage_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
 
+auth_xor2g_test_SOURCES = auth/xor2g_test.c
+auth_xor2g_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
+
 abis_abis_test_SOURCES = abis/abis_test.c
 abis_abis_test_LDADD = $(LDADD) $(top_builddir)/src/gsm/libosmogsm.la
 
@@ -357,6 +361,7 @@
              timer/timer_test.ok sms/sms_test.ok ussd/ussd_test.ok	\
              bits/bitrev_test.ok a5/a5_test.ok				\
              conv/conv_test.ok auth/milenage_test.ok ctrl/ctrl_test.ok	\
+             auth/xor2g_test.ok						\
              lapd/lapd_test.ok						\
              gsm0408/gsm0408_test.ok gsm0408/gsm0408_test.err		\
              gsm0808/gsm0808_test.ok gb/bssgp_fc_tests.err		\
diff --git a/tests/auth/xor2g_test.c b/tests/auth/xor2g_test.c
new file mode 100644
index 0000000..82ab25a
--- /dev/null
+++ b/tests/auth/xor2g_test.c
@@ -0,0 +1,77 @@
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include <osmocom/crypt/auth.h>
+#include <osmocom/core/utils.h>
+
+static void dump_auth_vec(struct osmo_auth_vector *vec)
+{
+	printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand)));
+
+	if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
+		printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn)));
+		printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik)));
+		printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck)));
+		printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len));
+	}
+
+	if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
+		printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres)));
+		/* According to 3GPP TS 55.205 Sec. 4 the GSM-MILENAGE output is limited to 64 bits.
+		   According to 3GPP TS 33.102 Annex. B5 in UMTS security context Kc can be 128 bits.
+		   Here we test the former, so make sure we only print interesting Kc bits. */
+		printf("Kc:\t%s\n", osmo_hexdump(vec->kc, OSMO_A5_MAX_KEY_LEN_BYTES/2));
+	}
+}
+
+static struct osmo_sub_auth_data test_aud = {
+	.type = OSMO_AUTH_TYPE_GSM,
+	.algo = OSMO_AUTH_ALG_XOR_2G,
+	.u.gsm = {
+		.ki =  { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+			 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
+	},
+};
+
+int main(int argc, char **argv)
+{
+	struct osmo_auth_vector _vec;
+	struct osmo_auth_vector *vec = &_vec;
+	uint8_t _rand[16];
+	int rc;
+
+#if 0
+	srand(time(NULL));
+	*(uint32_t *)&_rand[0] = rand();
+	*(uint32_t *)(&_rand[4]) = rand();
+	*(uint32_t *)(&_rand[8]) = rand();
+	*(uint32_t *)(&_rand[12]) = rand();
+#else
+	memset(_rand, 0, sizeof(_rand));
+#endif
+	memset(vec, 0, sizeof(*vec));
+
+	rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
+	if (rc < 0) {
+		fprintf(stderr, "error generating auth vector\n");
+		exit(1);
+	}
+	dump_auth_vec(vec);
+
+	/* test once more with non-zero RAND to see it show in result */
+	for (int i = 0; i < sizeof(_rand); i++)
+		_rand[i] = i << 4;
+
+	rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
+	if (rc < 0) {
+		fprintf(stderr, "error generating auth vector\n");
+		exit(1);
+	}
+	dump_auth_vec(vec);
+
+	exit(0);
+}
diff --git a/tests/auth/xor2g_test.ok b/tests/auth/xor2g_test.ok
new file mode 100644
index 0000000..58becf6
--- /dev/null
+++ b/tests/auth/xor2g_test.ok
@@ -0,0 +1,6 @@
+RAND:	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
+SRES:	00 01 02 03 
+Kc:	04 05 06 07 08 09 0a 0b 
+RAND:	00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 
+SRES:	00 11 22 33 
+Kc:	44 55 66 77 88 99 aa bb 
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 6b2e75f..e447cdc 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -126,6 +126,12 @@
 AT_CHECK([$abs_top_builddir/tests/auth/milenage_test], [0], [expout], [ignore])
 AT_CLEANUP
 
+AT_SETUP([auth_xor2g])
+AT_KEYWORDS([auth_xor2g])
+cat $abs_srcdir/auth/xor2g_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/auth/xor2g_test], [0], [expout], [ignore])
+AT_CLEANUP
+
 AT_SETUP([comp128])
 AT_KEYWORDS([comp128])
 cat $abs_srcdir/comp128/comp128_test.ok > expout