blob: 2833ed39b424ddc8a3d3b9ac1f9f719407fc1316 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welted82e0eb2011-12-06 21:53:42 +01002
Harald Weltec368b542017-10-16 16:00:06 +02003/*! \defgroup auth GSM/GPRS/3G Authentication
Harald Welte007a71e2012-07-18 19:47:56 +02004 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005 * \file auth.h */
Harald Welte007a71e2012-07-18 19:47:56 +02006
Harald Welted82e0eb2011-12-06 21:53:42 +01007#include <stdint.h>
8
9#include <osmocom/core/linuxlist.h>
Neels Hofmeyr26e30b12017-10-07 04:41:22 +020010#include <osmocom/core/utils.h>
Harald Welted82e0eb2011-12-06 21:53:42 +010011
Max483cdff2017-08-28 10:38:59 +020012#define OSMO_A5_MAX_KEY_LEN_BYTES (128/8)
Neels Hofmeyr1a02e362017-10-07 04:44:08 +020013#define OSMO_MILENAGE_IND_BITLEN_MAX 28
Max483cdff2017-08-28 10:38:59 +020014
Neels Hofmeyr87e45502017-06-20 00:17:59 +020015/*! Authentication Type (GSM/UMTS) */
Harald Welted82e0eb2011-12-06 21:53:42 +010016enum 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 Hofmeyr26e30b12017-10-07 04:41:22 +020022extern const struct value_string osmo_sub_auth_type_names[];
23static 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 Welted82e0eb2011-12-06 21:53:42 +010028enum osmo_auth_algo {
29 OSMO_AUTH_ALG_NONE,
30 OSMO_AUTH_ALG_COMP128v1,
31 OSMO_AUTH_ALG_COMP128v2,
32 OSMO_AUTH_ALG_COMP128v3,
Harald Welte9b7c9ae2023-02-21 22:24:15 +010033 OSMO_AUTH_ALG_XOR_3G,
Harald Welted82e0eb2011-12-06 21:53:42 +010034 OSMO_AUTH_ALG_MILENAGE,
Harald Weltee93c5e92023-02-21 22:18:11 +010035 OSMO_AUTH_ALG_XOR_2G,
Harald Welted82e0eb2011-12-06 21:53:42 +010036 _OSMO_AUTH_ALG_NUM,
37};
Harald Welte9b7c9ae2023-02-21 22:24:15 +010038/* Backwards-compatibility. We used to call XOR-3G just "XOR" which became ambiguous when
39 * we started to add XOR-2G support. */
40#define OSMO_AUTH_ALG_XOR OSMO_AUTH_ALG_XOR_3G
Harald Welted82e0eb2011-12-06 21:53:42 +010041
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042/*! permanent (secret) subscriber auth data */
Harald Welte08450c92023-05-30 10:55:37 +020043struct osmo_sub_auth_data2 {
44 enum osmo_sub_auth_type type;
45 enum osmo_auth_algo algo;
46 union {
47 struct {
48 /* See 3GPP TS 33.102 Section 9.3.7 Length of authentication parameters */
49 uint8_t opc[32]; /*!< operator invariant value */
50 uint8_t opc_len; /*!< OPc length (in bytes): 16 or 32 */
51 uint8_t k[32]; /*!< secret key of the subscriber */
52 uint8_t k_len; /*!< K length (in bytes): 16 or 32 */
53 uint8_t amf[2];
54 uint64_t sqn; /*!< sequence number (in: prev sqn; out: used sqn) */
55 int opc_is_op; /*!< is the OPC field OPC (0) or OP (1) ? */
56 unsigned int ind_bitlen; /*!< nr of bits not in SEQ, only SQN */
57 unsigned int ind; /*!< which IND slot to use an SQN from */
58 uint64_t sqn_ms; /*!< sqn from AUTS (output value only) */
59 } umts;
60 struct {
61 uint8_t ki[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< secret key */
62 } gsm;
63 } u;
64};
65
66/* deprecated older structure without support for 32-byte K/OP[c] */
Harald Welted82e0eb2011-12-06 21:53:42 +010067struct osmo_sub_auth_data {
68 enum osmo_sub_auth_type type;
69 enum osmo_auth_algo algo;
70 union {
71 struct {
Harald Welte007a71e2012-07-18 19:47:56 +020072 uint8_t opc[16]; /*!< operator invariant value */
Maxaf25c372018-12-19 20:12:19 +010073 uint8_t k[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< secret key of the subscriber */
Harald Welted82e0eb2011-12-06 21:53:42 +010074 uint8_t amf[2];
Neels Hofmeyr95500c82017-08-26 22:38:08 +020075 uint64_t sqn; /*!< sequence number (in: prev sqn; out: used sqn) */
Harald Welte007a71e2012-07-18 19:47:56 +020076 int opc_is_op; /*!< is the OPC field OPC (0) or OP (1) ? */
Neels Hofmeyrbb6f7b72017-03-13 17:27:17 +010077 unsigned int ind_bitlen; /*!< nr of bits not in SEQ, only SQN */
Neels Hofmeyr95500c82017-08-26 22:38:08 +020078 unsigned int ind; /*!< which IND slot to use an SQN from */
Neels Hofmeyr2066a422017-08-26 22:43:50 +020079 uint64_t sqn_ms; /*!< sqn from AUTS (output value only) */
Harald Welted82e0eb2011-12-06 21:53:42 +010080 } umts;
81 struct {
Max483cdff2017-08-28 10:38:59 +020082 uint8_t ki[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< secret key */
Harald Welted82e0eb2011-12-06 21:53:42 +010083 } gsm;
Harald Welteaae23622011-12-07 11:35:02 +010084 } u;
Harald Welte08450c92023-05-30 10:55:37 +020085} OSMO_DEPRECATED_OUTSIDE("Use osmo_sub_auth_data2 instead");
Harald Welted82e0eb2011-12-06 21:53:42 +010086
87/* data structure describing a computed auth vector, generated by AuC */
88struct osmo_auth_vector {
Harald Welte007a71e2012-07-18 19:47:56 +020089 uint8_t rand[16]; /*!< random challenge */
90 uint8_t autn[16]; /*!< authentication nonce */
Maxaf25c372018-12-19 20:12:19 +010091 uint8_t ck[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< ciphering key */
92 uint8_t ik[OSMO_A5_MAX_KEY_LEN_BYTES]; /*!< integrity key */
Harald Welte007a71e2012-07-18 19:47:56 +020093 uint8_t res[16]; /*!< authentication result */
Harald Welte08450c92023-05-30 10:55:37 +020094 uint8_t res_len; /*!< length (in bytes) of res: 8..16 bytes */
Harald Welte007a71e2012-07-18 19:47:56 +020095 uint8_t kc[8]; /*!< Kc for GSM encryption (A5) */
96 uint8_t sres[4]; /*!< authentication result for GSM */
Harald Welted82e0eb2011-12-06 21:53:42 +010097 uint32_t auth_types; /*!< bitmask of OSMO_AUTH_TYPE_* */
98};
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100/* An implementation of an authentication algorithm */
Harald Welted82e0eb2011-12-06 21:53:42 +0100101struct osmo_auth_impl {
102 struct llist_head list;
Harald Welte007a71e2012-07-18 19:47:56 +0200103 enum osmo_auth_algo algo; /*!< algorithm we implement */
104 const char *name; /*!< name of the implementation */
105 unsigned int priority; /*!< priority value (resp. othe implementations */
Harald Welted82e0eb2011-12-06 21:53:42 +0100106
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107 /*! callback for generate authentication vectors */
Harald Welted82e0eb2011-12-06 21:53:42 +0100108 int (*gen_vec)(struct osmo_auth_vector *vec,
Harald Welte08450c92023-05-30 10:55:37 +0200109 struct osmo_sub_auth_data2 *aud,
Harald Welted82e0eb2011-12-06 21:53:42 +0100110 const uint8_t *_rand);
111
Harald Welte08450c92023-05-30 10:55:37 +0200112 /*! callback for generating auth vectors + re-sync */
Harald Welted82e0eb2011-12-06 21:53:42 +0100113 int (*gen_vec_auts)(struct osmo_auth_vector *vec,
Harald Welte08450c92023-05-30 10:55:37 +0200114 struct osmo_sub_auth_data2 *aud,
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100115 const uint8_t *auts, const uint8_t *rand_auts,
Harald Welted82e0eb2011-12-06 21:53:42 +0100116 const uint8_t *_rand);
117};
118
119int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
Harald Welte08450c92023-05-30 10:55:37 +0200120 struct osmo_sub_auth_data *aud, const uint8_t *_rand)
121 OSMO_DEPRECATED_OUTSIDE("Use osmo_auth_gen_vec2 instead");
122
123int osmo_auth_gen_vec2(struct osmo_auth_vector *vec,
124 struct osmo_sub_auth_data2 *aud, const uint8_t *_rand);
Harald Welted82e0eb2011-12-06 21:53:42 +0100125
126int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
127 struct osmo_sub_auth_data *aud,
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100128 const uint8_t *auts, const uint8_t *rand_auts,
Harald Welte08450c92023-05-30 10:55:37 +0200129 const uint8_t *_rand)
130 OSMO_DEPRECATED_OUTSIDE("Use osmo_auth_gen_vec_auts2 instead");
131
132int osmo_auth_gen_vec_auts2(struct osmo_auth_vector *vec,
133 struct osmo_sub_auth_data2 *aud,
134 const uint8_t *auts, const uint8_t *rand_auts,
Harald Welted82e0eb2011-12-06 21:53:42 +0100135 const uint8_t *_rand);
136
137int osmo_auth_register(struct osmo_auth_impl *impl);
138
139int osmo_auth_load(const char *path);
140
141int osmo_auth_supported(enum osmo_auth_algo algo);
Maxceae1232016-06-27 18:12:49 +0200142void osmo_c4(uint8_t *ck, const uint8_t *kc);
Harald Weltea5ab1622011-12-07 02:33:11 +0100143const char *osmo_auth_alg_name(enum osmo_auth_algo alg);
144enum osmo_auth_algo osmo_auth_alg_parse(const char *name);
145
Neels Hofmeyraa84b712017-12-18 03:12:01 +0100146void osmo_auth_c3(uint8_t kc[], const uint8_t ck[], const uint8_t ik[]);
147
Harald Welte007a71e2012-07-18 19:47:56 +0200148/* @} */