Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 2 | |
Harald Welte | c368b54 | 2017-10-16 16:00:06 +0200 | [diff] [blame] | 3 | /*! \defgroup auth GSM/GPRS/3G Authentication |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 4 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | * \file auth.h */ |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 6 | |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <osmocom/core/linuxlist.h> |
Neels Hofmeyr | 26e30b1 | 2017-10-07 04:41:22 +0200 | [diff] [blame] | 10 | #include <osmocom/core/utils.h> |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 11 | |
Max | 483cdff | 2017-08-28 10:38:59 +0200 | [diff] [blame] | 12 | #define OSMO_A5_MAX_KEY_LEN_BYTES (128/8) |
Neels Hofmeyr | 1a02e36 | 2017-10-07 04:44:08 +0200 | [diff] [blame] | 13 | #define OSMO_MILENAGE_IND_BITLEN_MAX 28 |
Max | 483cdff | 2017-08-28 10:38:59 +0200 | [diff] [blame] | 14 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 15 | /*! Authentication Type (GSM/UMTS) */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 16 | enum osmo_sub_auth_type { |
| 17 | OSMO_AUTH_TYPE_NONE = 0x00, |
| 18 | OSMO_AUTH_TYPE_GSM = 0x01, |
| 19 | OSMO_AUTH_TYPE_UMTS = 0x02, |
| 20 | }; |
| 21 | |
Neels Hofmeyr | 26e30b1 | 2017-10-07 04:41:22 +0200 | [diff] [blame] | 22 | extern const struct value_string osmo_sub_auth_type_names[]; |
| 23 | static inline const char *osmo_sub_auth_type_name(enum osmo_sub_auth_type val) |
| 24 | { return get_value_string(osmo_sub_auth_type_names, val); } |
| 25 | |
| 26 | /*! Authentication Algorithm. |
| 27 | * See also osmo_auth_alg_name() and osmo_auth_alg_parse(). */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 28 | enum osmo_auth_algo { |
| 29 | OSMO_AUTH_ALG_NONE, |
| 30 | OSMO_AUTH_ALG_COMP128v1, |
| 31 | OSMO_AUTH_ALG_COMP128v2, |
| 32 | OSMO_AUTH_ALG_COMP128v3, |
Harald Welte | 9b7c9ae | 2023-02-21 22:24:15 +0100 | [diff] [blame] | 33 | OSMO_AUTH_ALG_XOR_3G, |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 34 | OSMO_AUTH_ALG_MILENAGE, |
Harald Welte | e93c5e9 | 2023-02-21 22:18:11 +0100 | [diff] [blame] | 35 | OSMO_AUTH_ALG_XOR_2G, |
Harald Welte | 8bd9d5d | 2023-05-28 09:36:50 +0200 | [diff] [blame] | 36 | OSMO_AUTH_ALG_TUAK, |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 37 | _OSMO_AUTH_ALG_NUM, |
| 38 | }; |
Harald Welte | 9b7c9ae | 2023-02-21 22:24:15 +0100 | [diff] [blame] | 39 | /* Backwards-compatibility. We used to call XOR-3G just "XOR" which became ambiguous when |
| 40 | * we started to add XOR-2G support. */ |
| 41 | #define OSMO_AUTH_ALG_XOR OSMO_AUTH_ALG_XOR_3G |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 42 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 43 | /*! permanent (secret) subscriber auth data */ |
Harald Welte | 08450c9 | 2023-05-30 10:55:37 +0200 | [diff] [blame] | 44 | struct osmo_sub_auth_data2 { |
| 45 | enum osmo_sub_auth_type type; |
| 46 | enum osmo_auth_algo algo; |
| 47 | union { |
| 48 | struct { |
| 49 | /* See 3GPP TS 33.102 Section 9.3.7 Length of authentication parameters */ |
| 50 | uint8_t opc[32]; /*!< operator invariant value */ |
| 51 | uint8_t opc_len; /*!< OPc length (in bytes): 16 or 32 */ |
| 52 | uint8_t k[32]; /*!< secret key of the subscriber */ |
| 53 | uint8_t k_len; /*!< K length (in bytes): 16 or 32 */ |
| 54 | uint8_t amf[2]; |
| 55 | uint64_t sqn; /*!< sequence number (in: prev sqn; out: used sqn) */ |
| 56 | int opc_is_op; /*!< is the OPC field OPC (0) or OP (1) ? */ |
| 57 | unsigned int ind_bitlen; /*!< nr of bits not in SEQ, only SQN */ |
| 58 | unsigned int ind; /*!< which IND slot to use an SQN from */ |
| 59 | uint64_t sqn_ms; /*!< sqn from AUTS (output value only) */ |
| 60 | } umts; |
| 61 | struct { |
| 62 | uint8_t ki[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< secret key */ |
| 63 | } gsm; |
| 64 | } u; |
| 65 | }; |
| 66 | |
| 67 | /* deprecated older structure without support for 32-byte K/OP[c] */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 68 | struct osmo_sub_auth_data { |
| 69 | enum osmo_sub_auth_type type; |
| 70 | enum osmo_auth_algo algo; |
| 71 | union { |
| 72 | struct { |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 73 | uint8_t opc[16]; /*!< operator invariant value */ |
Max | af25c37 | 2018-12-19 20:12:19 +0100 | [diff] [blame] | 74 | uint8_t k[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< secret key of the subscriber */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 75 | uint8_t amf[2]; |
Neels Hofmeyr | 95500c8 | 2017-08-26 22:38:08 +0200 | [diff] [blame] | 76 | uint64_t sqn; /*!< sequence number (in: prev sqn; out: used sqn) */ |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 77 | int opc_is_op; /*!< is the OPC field OPC (0) or OP (1) ? */ |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 78 | unsigned int ind_bitlen; /*!< nr of bits not in SEQ, only SQN */ |
Neels Hofmeyr | 95500c8 | 2017-08-26 22:38:08 +0200 | [diff] [blame] | 79 | unsigned int ind; /*!< which IND slot to use an SQN from */ |
Neels Hofmeyr | 2066a42 | 2017-08-26 22:43:50 +0200 | [diff] [blame] | 80 | uint64_t sqn_ms; /*!< sqn from AUTS (output value only) */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 81 | } umts; |
| 82 | struct { |
Max | 483cdff | 2017-08-28 10:38:59 +0200 | [diff] [blame] | 83 | uint8_t ki[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< secret key */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 84 | } gsm; |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 85 | } u; |
Vadim Yanitskiy | 3ada0bd | 2023-06-05 01:42:36 +0700 | [diff] [blame] | 86 | }; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 87 | |
| 88 | /* data structure describing a computed auth vector, generated by AuC */ |
| 89 | struct osmo_auth_vector { |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 90 | uint8_t rand[16]; /*!< random challenge */ |
| 91 | uint8_t autn[16]; /*!< authentication nonce */ |
Max | af25c37 | 2018-12-19 20:12:19 +0100 | [diff] [blame] | 92 | uint8_t ck[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< ciphering key */ |
| 93 | uint8_t ik[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< integrity key */ |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 94 | uint8_t res[16]; /*!< authentication result */ |
Harald Welte | d8e5309 | 2023-05-30 15:01:38 +0200 | [diff] [blame] | 95 | uint8_t res_len; /*!< length (in bytes) of res: 4..16 bytes */ |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 96 | uint8_t kc[8]; /*!< Kc for GSM encryption (A5) */ |
| 97 | uint8_t sres[4]; /*!< authentication result for GSM */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 98 | uint32_t auth_types; /*!< bitmask of OSMO_AUTH_TYPE_* */ |
| 99 | }; |
| 100 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 101 | /* An implementation of an authentication algorithm */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 102 | struct osmo_auth_impl { |
| 103 | struct llist_head list; |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 104 | enum osmo_auth_algo algo; /*!< algorithm we implement */ |
| 105 | const char *name; /*!< name of the implementation */ |
| 106 | unsigned int priority; /*!< priority value (resp. othe implementations */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 107 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 108 | /*! callback for generate authentication vectors */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 109 | int (*gen_vec)(struct osmo_auth_vector *vec, |
Harald Welte | 08450c9 | 2023-05-30 10:55:37 +0200 | [diff] [blame] | 110 | struct osmo_sub_auth_data2 *aud, |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 111 | const uint8_t *_rand); |
| 112 | |
Harald Welte | 08450c9 | 2023-05-30 10:55:37 +0200 | [diff] [blame] | 113 | /*! callback for generating auth vectors + re-sync */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 114 | int (*gen_vec_auts)(struct osmo_auth_vector *vec, |
Harald Welte | 08450c9 | 2023-05-30 10:55:37 +0200 | [diff] [blame] | 115 | struct osmo_sub_auth_data2 *aud, |
Neels Hofmeyr | 03ab9a6 | 2017-02-03 05:00:24 +0100 | [diff] [blame] | 116 | const uint8_t *auts, const uint8_t *rand_auts, |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 117 | const uint8_t *_rand); |
| 118 | }; |
| 119 | |
| 120 | int osmo_auth_gen_vec(struct osmo_auth_vector *vec, |
Harald Welte | 08450c9 | 2023-05-30 10:55:37 +0200 | [diff] [blame] | 121 | struct osmo_sub_auth_data *aud, const uint8_t *_rand) |
| 122 | OSMO_DEPRECATED_OUTSIDE("Use osmo_auth_gen_vec2 instead"); |
| 123 | |
| 124 | int osmo_auth_gen_vec2(struct osmo_auth_vector *vec, |
| 125 | struct osmo_sub_auth_data2 *aud, const uint8_t *_rand); |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 126 | |
| 127 | int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec, |
| 128 | struct osmo_sub_auth_data *aud, |
Neels Hofmeyr | 03ab9a6 | 2017-02-03 05:00:24 +0100 | [diff] [blame] | 129 | const uint8_t *auts, const uint8_t *rand_auts, |
Harald Welte | 08450c9 | 2023-05-30 10:55:37 +0200 | [diff] [blame] | 130 | const uint8_t *_rand) |
| 131 | OSMO_DEPRECATED_OUTSIDE("Use osmo_auth_gen_vec_auts2 instead"); |
| 132 | |
| 133 | int osmo_auth_gen_vec_auts2(struct osmo_auth_vector *vec, |
| 134 | struct osmo_sub_auth_data2 *aud, |
| 135 | const uint8_t *auts, const uint8_t *rand_auts, |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 136 | const uint8_t *_rand); |
| 137 | |
| 138 | int osmo_auth_register(struct osmo_auth_impl *impl); |
| 139 | |
| 140 | int osmo_auth_load(const char *path); |
| 141 | |
| 142 | int osmo_auth_supported(enum osmo_auth_algo algo); |
Max | ceae123 | 2016-06-27 18:12:49 +0200 | [diff] [blame] | 143 | void osmo_c4(uint8_t *ck, const uint8_t *kc); |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 144 | const char *osmo_auth_alg_name(enum osmo_auth_algo alg); |
| 145 | enum osmo_auth_algo osmo_auth_alg_parse(const char *name); |
| 146 | |
Neels Hofmeyr | aa84b71 | 2017-12-18 03:12:01 +0100 | [diff] [blame] | 147 | void osmo_auth_c3(uint8_t kc[], const uint8_t ck[], const uint8_t ik[]); |
Harald Welte | 76f4c5c | 2023-05-30 15:57:08 +0200 | [diff] [blame] | 148 | void osmo_auth_c2(uint8_t sres[4], const uint8_t *res, size_t res_len, uint8_t sres_deriv_func); |
Neels Hofmeyr | aa84b71 | 2017-12-18 03:12:01 +0100 | [diff] [blame] | 149 | |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 150 | /* @} */ |