Harald Welte | 4cc60a1 | 2023-02-21 22:35:29 +0100 | [diff] [blame] | 1 | |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <errno.h> |
| 5 | #include <string.h> |
| 6 | #include <inttypes.h> |
| 7 | |
| 8 | #include <osmocom/crypt/auth.h> |
| 9 | #include <osmocom/core/utils.h> |
| 10 | |
| 11 | static void dump_auth_vec(struct osmo_auth_vector *vec) |
| 12 | { |
| 13 | printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand))); |
| 14 | |
| 15 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 16 | printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn))); |
| 17 | printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik))); |
| 18 | printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck))); |
| 19 | printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len)); |
| 20 | } |
| 21 | |
| 22 | if (vec->auth_types & OSMO_AUTH_TYPE_GSM) { |
| 23 | printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres))); |
| 24 | /* According to 3GPP TS 55.205 Sec. 4 the GSM-MILENAGE output is limited to 64 bits. |
| 25 | According to 3GPP TS 33.102 Annex. B5 in UMTS security context Kc can be 128 bits. |
| 26 | Here we test the former, so make sure we only print interesting Kc bits. */ |
| 27 | printf("Kc:\t%s\n", osmo_hexdump(vec->kc, OSMO_A5_MAX_KEY_LEN_BYTES/2)); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static struct osmo_sub_auth_data test_aud = { |
| 32 | .type = OSMO_AUTH_TYPE_GSM, |
| 33 | .algo = OSMO_AUTH_ALG_XOR_2G, |
| 34 | .u.gsm = { |
| 35 | .ki = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 36 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
| 37 | }, |
| 38 | }; |
| 39 | |
| 40 | int main(int argc, char **argv) |
| 41 | { |
| 42 | struct osmo_auth_vector _vec; |
| 43 | struct osmo_auth_vector *vec = &_vec; |
| 44 | uint8_t _rand[16]; |
| 45 | int rc; |
| 46 | |
| 47 | #if 0 |
| 48 | srand(time(NULL)); |
| 49 | *(uint32_t *)&_rand[0] = rand(); |
| 50 | *(uint32_t *)(&_rand[4]) = rand(); |
| 51 | *(uint32_t *)(&_rand[8]) = rand(); |
| 52 | *(uint32_t *)(&_rand[12]) = rand(); |
| 53 | #else |
| 54 | memset(_rand, 0, sizeof(_rand)); |
| 55 | #endif |
| 56 | memset(vec, 0, sizeof(*vec)); |
| 57 | |
| 58 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 59 | if (rc < 0) { |
| 60 | fprintf(stderr, "error generating auth vector\n"); |
| 61 | exit(1); |
| 62 | } |
| 63 | dump_auth_vec(vec); |
| 64 | |
| 65 | /* test once more with non-zero RAND to see it show in result */ |
| 66 | for (int i = 0; i < sizeof(_rand); i++) |
| 67 | _rand[i] = i << 4; |
| 68 | |
| 69 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 70 | if (rc < 0) { |
| 71 | fprintf(stderr, "error generating auth vector\n"); |
| 72 | exit(1); |
| 73 | } |
| 74 | dump_auth_vec(vec); |
| 75 | |
| 76 | exit(0); |
| 77 | } |