xref: /openbmc/linux/net/sctp/auth.c (revision 2ab399a9)
147505b8bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
260c778b2SVlad Yasevich /* SCTP kernel implementation
31f485649SVlad Yasevich  * (C) Copyright 2007 Hewlett-Packard Development Company, L.P.
41f485649SVlad Yasevich  *
560c778b2SVlad Yasevich  * This file is part of the SCTP kernel implementation
61f485649SVlad Yasevich  *
71f485649SVlad Yasevich  * Please send any bug reports or fixes you make to the
81f485649SVlad Yasevich  * email address(es):
991705c61SDaniel Borkmann  *    lksctp developers <linux-sctp@vger.kernel.org>
101f485649SVlad Yasevich  *
111f485649SVlad Yasevich  * Written or modified by:
121f485649SVlad Yasevich  *   Vlad Yasevich     <vladislav.yasevich@hp.com>
131f485649SVlad Yasevich  */
141f485649SVlad Yasevich 
155821c769SHerbert Xu #include <crypto/hash.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171f485649SVlad Yasevich #include <linux/types.h>
181f485649SVlad Yasevich #include <linux/scatterlist.h>
191f485649SVlad Yasevich #include <net/sctp/sctp.h>
201f485649SVlad Yasevich #include <net/sctp/auth.h>
211f485649SVlad Yasevich 
221f485649SVlad Yasevich static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
231f485649SVlad Yasevich 	{
241f485649SVlad Yasevich 		/* id 0 is reserved.  as all 0 */
251f485649SVlad Yasevich 		.hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
261f485649SVlad Yasevich 	},
271f485649SVlad Yasevich 	{
281f485649SVlad Yasevich 		.hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
291f485649SVlad Yasevich 		.hmac_name = "hmac(sha1)",
301f485649SVlad Yasevich 		.hmac_len = SCTP_SHA1_SIG_SIZE,
311f485649SVlad Yasevich 	},
321f485649SVlad Yasevich 	{
331f485649SVlad Yasevich 		/* id 2 is reserved as well */
341f485649SVlad Yasevich 		.hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
351f485649SVlad Yasevich 	},
36aebf5de0SJavier Martinez Canillas #if IS_ENABLED(CONFIG_CRYPTO_SHA256)
371f485649SVlad Yasevich 	{
381f485649SVlad Yasevich 		.hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
391f485649SVlad Yasevich 		.hmac_name = "hmac(sha256)",
401f485649SVlad Yasevich 		.hmac_len = SCTP_SHA256_SIG_SIZE,
411f485649SVlad Yasevich 	}
42b7e0fe9fSVlad Yasevich #endif
431f485649SVlad Yasevich };
441f485649SVlad Yasevich 
451f485649SVlad Yasevich 
sctp_auth_key_put(struct sctp_auth_bytes * key)461f485649SVlad Yasevich void sctp_auth_key_put(struct sctp_auth_bytes *key)
471f485649SVlad Yasevich {
481f485649SVlad Yasevich 	if (!key)
491f485649SVlad Yasevich 		return;
501f485649SVlad Yasevich 
516871584aSReshetova, Elena 	if (refcount_dec_and_test(&key->refcnt)) {
52453431a5SWaiman Long 		kfree_sensitive(key);
531f485649SVlad Yasevich 		SCTP_DBG_OBJCNT_DEC(keys);
541f485649SVlad Yasevich 	}
551f485649SVlad Yasevich }
561f485649SVlad Yasevich 
571f485649SVlad Yasevich /* Create a new key structure of a given length */
sctp_auth_create_key(__u32 key_len,gfp_t gfp)581f485649SVlad Yasevich static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
591f485649SVlad Yasevich {
601f485649SVlad Yasevich 	struct sctp_auth_bytes *key;
611f485649SVlad Yasevich 
6230c2235cSVlad Yasevich 	/* Verify that we are not going to overflow INT_MAX */
63c89304b8SXi Wang 	if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
6430c2235cSVlad Yasevich 		return NULL;
6530c2235cSVlad Yasevich 
661f485649SVlad Yasevich 	/* Allocate the shared key */
671f485649SVlad Yasevich 	key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
681f485649SVlad Yasevich 	if (!key)
691f485649SVlad Yasevich 		return NULL;
701f485649SVlad Yasevich 
711f485649SVlad Yasevich 	key->len = key_len;
726871584aSReshetova, Elena 	refcount_set(&key->refcnt, 1);
731f485649SVlad Yasevich 	SCTP_DBG_OBJCNT_INC(keys);
741f485649SVlad Yasevich 
751f485649SVlad Yasevich 	return key;
761f485649SVlad Yasevich }
771f485649SVlad Yasevich 
781f485649SVlad Yasevich /* Create a new shared key container with a give key id */
sctp_auth_shkey_create(__u16 key_id,gfp_t gfp)791f485649SVlad Yasevich struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
801f485649SVlad Yasevich {
811f485649SVlad Yasevich 	struct sctp_shared_key *new;
821f485649SVlad Yasevich 
831f485649SVlad Yasevich 	/* Allocate the shared key container */
841f485649SVlad Yasevich 	new = kzalloc(sizeof(struct sctp_shared_key), gfp);
851f485649SVlad Yasevich 	if (!new)
861f485649SVlad Yasevich 		return NULL;
871f485649SVlad Yasevich 
881f485649SVlad Yasevich 	INIT_LIST_HEAD(&new->key_list);
891b1e0bc9SXin Long 	refcount_set(&new->refcnt, 1);
901f485649SVlad Yasevich 	new->key_id = key_id;
911f485649SVlad Yasevich 
921f485649SVlad Yasevich 	return new;
931f485649SVlad Yasevich }
941f485649SVlad Yasevich 
9525985edcSLucas De Marchi /* Free the shared key structure */
sctp_auth_shkey_destroy(struct sctp_shared_key * sh_key)961b1e0bc9SXin Long static void sctp_auth_shkey_destroy(struct sctp_shared_key *sh_key)
971f485649SVlad Yasevich {
981f485649SVlad Yasevich 	BUG_ON(!list_empty(&sh_key->key_list));
991f485649SVlad Yasevich 	sctp_auth_key_put(sh_key->key);
1001f485649SVlad Yasevich 	sh_key->key = NULL;
1011f485649SVlad Yasevich 	kfree(sh_key);
1021f485649SVlad Yasevich }
1031f485649SVlad Yasevich 
sctp_auth_shkey_release(struct sctp_shared_key * sh_key)1041b1e0bc9SXin Long void sctp_auth_shkey_release(struct sctp_shared_key *sh_key)
1051b1e0bc9SXin Long {
1061b1e0bc9SXin Long 	if (refcount_dec_and_test(&sh_key->refcnt))
1071b1e0bc9SXin Long 		sctp_auth_shkey_destroy(sh_key);
1081b1e0bc9SXin Long }
1091b1e0bc9SXin Long 
sctp_auth_shkey_hold(struct sctp_shared_key * sh_key)1101b1e0bc9SXin Long void sctp_auth_shkey_hold(struct sctp_shared_key *sh_key)
1111b1e0bc9SXin Long {
1121b1e0bc9SXin Long 	refcount_inc(&sh_key->refcnt);
1131b1e0bc9SXin Long }
1141b1e0bc9SXin Long 
11525985edcSLucas De Marchi /* Destroy the entire key list.  This is done during the
1161f485649SVlad Yasevich  * associon and endpoint free process.
1171f485649SVlad Yasevich  */
sctp_auth_destroy_keys(struct list_head * keys)1181f485649SVlad Yasevich void sctp_auth_destroy_keys(struct list_head *keys)
1191f485649SVlad Yasevich {
1201f485649SVlad Yasevich 	struct sctp_shared_key *ep_key;
1211f485649SVlad Yasevich 	struct sctp_shared_key *tmp;
1221f485649SVlad Yasevich 
1231f485649SVlad Yasevich 	if (list_empty(keys))
1241f485649SVlad Yasevich 		return;
1251f485649SVlad Yasevich 
1261f485649SVlad Yasevich 	key_for_each_safe(ep_key, tmp, keys) {
1271f485649SVlad Yasevich 		list_del_init(&ep_key->key_list);
1281b1e0bc9SXin Long 		sctp_auth_shkey_release(ep_key);
1291f485649SVlad Yasevich 	}
1301f485649SVlad Yasevich }
1311f485649SVlad Yasevich 
1321f485649SVlad Yasevich /* Compare two byte vectors as numbers.  Return values
1331f485649SVlad Yasevich  * are:
1341f485649SVlad Yasevich  * 	  0 - vectors are equal
135025dfdafSFrederik Schwarzer  * 	< 0 - vector 1 is smaller than vector2
136025dfdafSFrederik Schwarzer  * 	> 0 - vector 1 is greater than vector2
1371f485649SVlad Yasevich  *
1381f485649SVlad Yasevich  * Algorithm is:
1391f485649SVlad Yasevich  * 	This is performed by selecting the numerically smaller key vector...
1401f485649SVlad Yasevich  *	If the key vectors are equal as numbers but differ in length ...
1411f485649SVlad Yasevich  *	the shorter vector is considered smaller
1421f485649SVlad Yasevich  *
1431f485649SVlad Yasevich  * Examples (with small values):
1441f485649SVlad Yasevich  * 	000123456789 > 123456789 (first number is longer)
1451f485649SVlad Yasevich  * 	000123456789 < 234567891 (second number is larger numerically)
1461f485649SVlad Yasevich  * 	123456789 > 2345678 	 (first number is both larger & longer)
1471f485649SVlad Yasevich  */
sctp_auth_compare_vectors(struct sctp_auth_bytes * vector1,struct sctp_auth_bytes * vector2)1481f485649SVlad Yasevich static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
1491f485649SVlad Yasevich 			      struct sctp_auth_bytes *vector2)
1501f485649SVlad Yasevich {
1511f485649SVlad Yasevich 	int diff;
1521f485649SVlad Yasevich 	int i;
1531f485649SVlad Yasevich 	const __u8 *longer;
1541f485649SVlad Yasevich 
1551f485649SVlad Yasevich 	diff = vector1->len - vector2->len;
1561f485649SVlad Yasevich 	if (diff) {
1571f485649SVlad Yasevich 		longer = (diff > 0) ? vector1->data : vector2->data;
1581f485649SVlad Yasevich 
1591f485649SVlad Yasevich 		/* Check to see if the longer number is
1601f485649SVlad Yasevich 		 * lead-zero padded.  If it is not, it
1611f485649SVlad Yasevich 		 * is automatically larger numerically.
1621f485649SVlad Yasevich 		 */
1631f485649SVlad Yasevich 		for (i = 0; i < abs(diff); i++) {
1641f485649SVlad Yasevich 			if (longer[i] != 0)
1651f485649SVlad Yasevich 				return diff;
1661f485649SVlad Yasevich 		}
1671f485649SVlad Yasevich 	}
1681f485649SVlad Yasevich 
1691f485649SVlad Yasevich 	/* lengths are the same, compare numbers */
1701f485649SVlad Yasevich 	return memcmp(vector1->data, vector2->data, vector1->len);
1711f485649SVlad Yasevich }
1721f485649SVlad Yasevich 
1731f485649SVlad Yasevich /*
1741f485649SVlad Yasevich  * Create a key vector as described in SCTP-AUTH, Section 6.1
1751f485649SVlad Yasevich  *    The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
1761f485649SVlad Yasevich  *    parameter sent by each endpoint are concatenated as byte vectors.
1771f485649SVlad Yasevich  *    These parameters include the parameter type, parameter length, and
1781f485649SVlad Yasevich  *    the parameter value, but padding is omitted; all padding MUST be
1791f485649SVlad Yasevich  *    removed from this concatenation before proceeding with further
1801f485649SVlad Yasevich  *    computation of keys.  Parameters which were not sent are simply
1811f485649SVlad Yasevich  *    omitted from the concatenation process.  The resulting two vectors
1821f485649SVlad Yasevich  *    are called the two key vectors.
1831f485649SVlad Yasevich  */
sctp_auth_make_key_vector(struct sctp_random_param * random,struct sctp_chunks_param * chunks,struct sctp_hmac_algo_param * hmacs,gfp_t gfp)1841f485649SVlad Yasevich static struct sctp_auth_bytes *sctp_auth_make_key_vector(
185b02db702SXin Long 			struct sctp_random_param *random,
186a762a9d9SXin Long 			struct sctp_chunks_param *chunks,
1871474774aSXin Long 			struct sctp_hmac_algo_param *hmacs,
1881f485649SVlad Yasevich 			gfp_t gfp)
1891f485649SVlad Yasevich {
1901f485649SVlad Yasevich 	struct sctp_auth_bytes *new;
1911f485649SVlad Yasevich 	__u32	len;
1921f485649SVlad Yasevich 	__u32	offset = 0;
193241448c2SDaniel Borkmann 	__u16	random_len, hmacs_len, chunks_len = 0;
1941f485649SVlad Yasevich 
195241448c2SDaniel Borkmann 	random_len = ntohs(random->param_hdr.length);
196241448c2SDaniel Borkmann 	hmacs_len = ntohs(hmacs->param_hdr.length);
1971f485649SVlad Yasevich 	if (chunks)
198241448c2SDaniel Borkmann 		chunks_len = ntohs(chunks->param_hdr.length);
199241448c2SDaniel Borkmann 
200241448c2SDaniel Borkmann 	len = random_len + hmacs_len + chunks_len;
2011f485649SVlad Yasevich 
20203536e23SDaniel Borkmann 	new = sctp_auth_create_key(len, gfp);
2031f485649SVlad Yasevich 	if (!new)
2041f485649SVlad Yasevich 		return NULL;
2051f485649SVlad Yasevich 
206241448c2SDaniel Borkmann 	memcpy(new->data, random, random_len);
207241448c2SDaniel Borkmann 	offset += random_len;
2081f485649SVlad Yasevich 
2091f485649SVlad Yasevich 	if (chunks) {
210241448c2SDaniel Borkmann 		memcpy(new->data + offset, chunks, chunks_len);
211241448c2SDaniel Borkmann 		offset += chunks_len;
2121f485649SVlad Yasevich 	}
2131f485649SVlad Yasevich 
214241448c2SDaniel Borkmann 	memcpy(new->data + offset, hmacs, hmacs_len);
2151f485649SVlad Yasevich 
2161f485649SVlad Yasevich 	return new;
2171f485649SVlad Yasevich }
2181f485649SVlad Yasevich 
2191f485649SVlad Yasevich 
2201f485649SVlad Yasevich /* Make a key vector based on our local parameters */
sctp_auth_make_local_vector(const struct sctp_association * asoc,gfp_t gfp)2218ad7c62bSAdrian Bunk static struct sctp_auth_bytes *sctp_auth_make_local_vector(
2221f485649SVlad Yasevich 				    const struct sctp_association *asoc,
2231f485649SVlad Yasevich 				    gfp_t gfp)
2241f485649SVlad Yasevich {
2251f485649SVlad Yasevich 	return sctp_auth_make_key_vector(
226b02db702SXin Long 			(struct sctp_random_param *)asoc->c.auth_random,
227a762a9d9SXin Long 			(struct sctp_chunks_param *)asoc->c.auth_chunks,
2281474774aSXin Long 			(struct sctp_hmac_algo_param *)asoc->c.auth_hmacs, gfp);
2291f485649SVlad Yasevich }
2301f485649SVlad Yasevich 
2311f485649SVlad Yasevich /* Make a key vector based on peer's parameters */
sctp_auth_make_peer_vector(const struct sctp_association * asoc,gfp_t gfp)2328ad7c62bSAdrian Bunk static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
2331f485649SVlad Yasevich 				    const struct sctp_association *asoc,
2341f485649SVlad Yasevich 				    gfp_t gfp)
2351f485649SVlad Yasevich {
2361f485649SVlad Yasevich 	return sctp_auth_make_key_vector(asoc->peer.peer_random,
2371f485649SVlad Yasevich 					 asoc->peer.peer_chunks,
2381f485649SVlad Yasevich 					 asoc->peer.peer_hmacs,
2391f485649SVlad Yasevich 					 gfp);
2401f485649SVlad Yasevich }
2411f485649SVlad Yasevich 
2421f485649SVlad Yasevich 
2431f485649SVlad Yasevich /* Set the value of the association shared key base on the parameters
2441f485649SVlad Yasevich  * given.  The algorithm is:
2451f485649SVlad Yasevich  *    From the endpoint pair shared keys and the key vectors the
2461f485649SVlad Yasevich  *    association shared keys are computed.  This is performed by selecting
2471f485649SVlad Yasevich  *    the numerically smaller key vector and concatenating it to the
2481f485649SVlad Yasevich  *    endpoint pair shared key, and then concatenating the numerically
2491f485649SVlad Yasevich  *    larger key vector to that.  The result of the concatenation is the
2501f485649SVlad Yasevich  *    association shared key.
2511f485649SVlad Yasevich  */
sctp_auth_asoc_set_secret(struct sctp_shared_key * ep_key,struct sctp_auth_bytes * first_vector,struct sctp_auth_bytes * last_vector,gfp_t gfp)2521f485649SVlad Yasevich static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
2531f485649SVlad Yasevich 			struct sctp_shared_key *ep_key,
2541f485649SVlad Yasevich 			struct sctp_auth_bytes *first_vector,
2551f485649SVlad Yasevich 			struct sctp_auth_bytes *last_vector,
2561f485649SVlad Yasevich 			gfp_t gfp)
2571f485649SVlad Yasevich {
2581f485649SVlad Yasevich 	struct sctp_auth_bytes *secret;
2591f485649SVlad Yasevich 	__u32 offset = 0;
2601f485649SVlad Yasevich 	__u32 auth_len;
2611f485649SVlad Yasevich 
2621f485649SVlad Yasevich 	auth_len = first_vector->len + last_vector->len;
2631f485649SVlad Yasevich 	if (ep_key->key)
2641f485649SVlad Yasevich 		auth_len += ep_key->key->len;
2651f485649SVlad Yasevich 
2661f485649SVlad Yasevich 	secret = sctp_auth_create_key(auth_len, gfp);
2671f485649SVlad Yasevich 	if (!secret)
2681f485649SVlad Yasevich 		return NULL;
2691f485649SVlad Yasevich 
2701f485649SVlad Yasevich 	if (ep_key->key) {
2711f485649SVlad Yasevich 		memcpy(secret->data, ep_key->key->data, ep_key->key->len);
2721f485649SVlad Yasevich 		offset += ep_key->key->len;
2731f485649SVlad Yasevich 	}
2741f485649SVlad Yasevich 
2751f485649SVlad Yasevich 	memcpy(secret->data + offset, first_vector->data, first_vector->len);
2761f485649SVlad Yasevich 	offset += first_vector->len;
2771f485649SVlad Yasevich 
2781f485649SVlad Yasevich 	memcpy(secret->data + offset, last_vector->data, last_vector->len);
2791f485649SVlad Yasevich 
2801f485649SVlad Yasevich 	return secret;
2811f485649SVlad Yasevich }
2821f485649SVlad Yasevich 
2831f485649SVlad Yasevich /* Create an association shared key.  Follow the algorithm
2841f485649SVlad Yasevich  * described in SCTP-AUTH, Section 6.1
2851f485649SVlad Yasevich  */
sctp_auth_asoc_create_secret(const struct sctp_association * asoc,struct sctp_shared_key * ep_key,gfp_t gfp)2861f485649SVlad Yasevich static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
2871f485649SVlad Yasevich 				 const struct sctp_association *asoc,
2881f485649SVlad Yasevich 				 struct sctp_shared_key *ep_key,
2891f485649SVlad Yasevich 				 gfp_t gfp)
2901f485649SVlad Yasevich {
2911f485649SVlad Yasevich 	struct sctp_auth_bytes *local_key_vector;
2921f485649SVlad Yasevich 	struct sctp_auth_bytes *peer_key_vector;
2931f485649SVlad Yasevich 	struct sctp_auth_bytes	*first_vector,
2941f485649SVlad Yasevich 				*last_vector;
2951f485649SVlad Yasevich 	struct sctp_auth_bytes	*secret = NULL;
2961f485649SVlad Yasevich 	int	cmp;
2971f485649SVlad Yasevich 
2981f485649SVlad Yasevich 
2991f485649SVlad Yasevich 	/* Now we need to build the key vectors
3001f485649SVlad Yasevich 	 * SCTP-AUTH , Section 6.1
3011f485649SVlad Yasevich 	 *    The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
3021f485649SVlad Yasevich 	 *    parameter sent by each endpoint are concatenated as byte vectors.
3031f485649SVlad Yasevich 	 *    These parameters include the parameter type, parameter length, and
3041f485649SVlad Yasevich 	 *    the parameter value, but padding is omitted; all padding MUST be
3051f485649SVlad Yasevich 	 *    removed from this concatenation before proceeding with further
3061f485649SVlad Yasevich 	 *    computation of keys.  Parameters which were not sent are simply
3071f485649SVlad Yasevich 	 *    omitted from the concatenation process.  The resulting two vectors
3081f485649SVlad Yasevich 	 *    are called the two key vectors.
3091f485649SVlad Yasevich 	 */
3101f485649SVlad Yasevich 
3111f485649SVlad Yasevich 	local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
3121f485649SVlad Yasevich 	peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
3131f485649SVlad Yasevich 
3141f485649SVlad Yasevich 	if (!peer_key_vector || !local_key_vector)
3151f485649SVlad Yasevich 		goto out;
3161f485649SVlad Yasevich 
31725985edcSLucas De Marchi 	/* Figure out the order in which the key_vectors will be
3181f485649SVlad Yasevich 	 * added to the endpoint shared key.
3191f485649SVlad Yasevich 	 * SCTP-AUTH, Section 6.1:
3201f485649SVlad Yasevich 	 *   This is performed by selecting the numerically smaller key
3211f485649SVlad Yasevich 	 *   vector and concatenating it to the endpoint pair shared
3221f485649SVlad Yasevich 	 *   key, and then concatenating the numerically larger key
3231f485649SVlad Yasevich 	 *   vector to that.  If the key vectors are equal as numbers
3241f485649SVlad Yasevich 	 *   but differ in length, then the concatenation order is the
3251f485649SVlad Yasevich 	 *   endpoint shared key, followed by the shorter key vector,
3261f485649SVlad Yasevich 	 *   followed by the longer key vector.  Otherwise, the key
3271f485649SVlad Yasevich 	 *   vectors are identical, and may be concatenated to the
3281f485649SVlad Yasevich 	 *   endpoint pair key in any order.
3291f485649SVlad Yasevich 	 */
3301f485649SVlad Yasevich 	cmp = sctp_auth_compare_vectors(local_key_vector,
3311f485649SVlad Yasevich 					peer_key_vector);
3321f485649SVlad Yasevich 	if (cmp < 0) {
3331f485649SVlad Yasevich 		first_vector = local_key_vector;
3341f485649SVlad Yasevich 		last_vector = peer_key_vector;
3351f485649SVlad Yasevich 	} else {
3361f485649SVlad Yasevich 		first_vector = peer_key_vector;
3371f485649SVlad Yasevich 		last_vector = local_key_vector;
3381f485649SVlad Yasevich 	}
3391f485649SVlad Yasevich 
3401f485649SVlad Yasevich 	secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
3411f485649SVlad Yasevich 					    gfp);
3421f485649SVlad Yasevich out:
34303536e23SDaniel Borkmann 	sctp_auth_key_put(local_key_vector);
34403536e23SDaniel Borkmann 	sctp_auth_key_put(peer_key_vector);
3451f485649SVlad Yasevich 
3461f485649SVlad Yasevich 	return secret;
3471f485649SVlad Yasevich }
3481f485649SVlad Yasevich 
3491f485649SVlad Yasevich /*
3501f485649SVlad Yasevich  * Populate the association overlay list with the list
3511f485649SVlad Yasevich  * from the endpoint.
3521f485649SVlad Yasevich  */
sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint * ep,struct sctp_association * asoc,gfp_t gfp)3531f485649SVlad Yasevich int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
3541f485649SVlad Yasevich 				struct sctp_association *asoc,
3551f485649SVlad Yasevich 				gfp_t gfp)
3561f485649SVlad Yasevich {
3571f485649SVlad Yasevich 	struct sctp_shared_key *sh_key;
3581f485649SVlad Yasevich 	struct sctp_shared_key *new;
3591f485649SVlad Yasevich 
3601f485649SVlad Yasevich 	BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
3611f485649SVlad Yasevich 
3621f485649SVlad Yasevich 	key_for_each(sh_key, &ep->endpoint_shared_keys) {
3631f485649SVlad Yasevich 		new = sctp_auth_shkey_create(sh_key->key_id, gfp);
3641f485649SVlad Yasevich 		if (!new)
3651f485649SVlad Yasevich 			goto nomem;
3661f485649SVlad Yasevich 
3671f485649SVlad Yasevich 		new->key = sh_key->key;
3681f485649SVlad Yasevich 		sctp_auth_key_hold(new->key);
3691f485649SVlad Yasevich 		list_add(&new->key_list, &asoc->endpoint_shared_keys);
3701f485649SVlad Yasevich 	}
3711f485649SVlad Yasevich 
3721f485649SVlad Yasevich 	return 0;
3731f485649SVlad Yasevich 
3741f485649SVlad Yasevich nomem:
3751f485649SVlad Yasevich 	sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
3761f485649SVlad Yasevich 	return -ENOMEM;
3771f485649SVlad Yasevich }
3781f485649SVlad Yasevich 
3791f485649SVlad Yasevich 
380ae36806aSMarcelo Ricardo Leitner /* Public interface to create the association shared key.
3811f485649SVlad Yasevich  * See code above for the algorithm.
3821f485649SVlad Yasevich  */
sctp_auth_asoc_init_active_key(struct sctp_association * asoc,gfp_t gfp)3831f485649SVlad Yasevich int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
3841f485649SVlad Yasevich {
3851f485649SVlad Yasevich 	struct sctp_auth_bytes	*secret;
3861f485649SVlad Yasevich 	struct sctp_shared_key *ep_key;
387ae36806aSMarcelo Ricardo Leitner 	struct sctp_chunk *chunk;
3881f485649SVlad Yasevich 
3891f485649SVlad Yasevich 	/* If we don't support AUTH, or peer is not capable
3901f485649SVlad Yasevich 	 * we don't need to do anything.
3911f485649SVlad Yasevich 	 */
392219f9ea4SXin Long 	if (!asoc->peer.auth_capable)
3931f485649SVlad Yasevich 		return 0;
3941f485649SVlad Yasevich 
3951f485649SVlad Yasevich 	/* If the key_id is non-zero and we couldn't find an
3961f485649SVlad Yasevich 	 * endpoint pair shared key, we can't compute the
3971f485649SVlad Yasevich 	 * secret.
3981f485649SVlad Yasevich 	 * For key_id 0, endpoint pair shared key is a NULL key.
3991f485649SVlad Yasevich 	 */
4001f485649SVlad Yasevich 	ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
4011f485649SVlad Yasevich 	BUG_ON(!ep_key);
4021f485649SVlad Yasevich 
4031f485649SVlad Yasevich 	secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
4041f485649SVlad Yasevich 	if (!secret)
4051f485649SVlad Yasevich 		return -ENOMEM;
4061f485649SVlad Yasevich 
4071f485649SVlad Yasevich 	sctp_auth_key_put(asoc->asoc_shared_key);
4081f485649SVlad Yasevich 	asoc->asoc_shared_key = secret;
4091b1e0bc9SXin Long 	asoc->shkey = ep_key;
4101f485649SVlad Yasevich 
411ae36806aSMarcelo Ricardo Leitner 	/* Update send queue in case any chunk already in there now
412ae36806aSMarcelo Ricardo Leitner 	 * needs authenticating
413ae36806aSMarcelo Ricardo Leitner 	 */
414ae36806aSMarcelo Ricardo Leitner 	list_for_each_entry(chunk, &asoc->outqueue.out_chunk_list, list) {
4151b1e0bc9SXin Long 		if (sctp_auth_send_cid(chunk->chunk_hdr->type, asoc)) {
416ae36806aSMarcelo Ricardo Leitner 			chunk->auth = 1;
4171b1e0bc9SXin Long 			if (!chunk->shkey) {
4181b1e0bc9SXin Long 				chunk->shkey = asoc->shkey;
4191b1e0bc9SXin Long 				sctp_auth_shkey_hold(chunk->shkey);
4201b1e0bc9SXin Long 			}
4211b1e0bc9SXin Long 		}
422ae36806aSMarcelo Ricardo Leitner 	}
423ae36806aSMarcelo Ricardo Leitner 
4241f485649SVlad Yasevich 	return 0;
4251f485649SVlad Yasevich }
4261f485649SVlad Yasevich 
4271f485649SVlad Yasevich 
4281f485649SVlad Yasevich /* Find the endpoint pair shared key based on the key_id */
sctp_auth_get_shkey(const struct sctp_association * asoc,__u16 key_id)4291f485649SVlad Yasevich struct sctp_shared_key *sctp_auth_get_shkey(
4301f485649SVlad Yasevich 				const struct sctp_association *asoc,
4311f485649SVlad Yasevich 				__u16 key_id)
4321f485649SVlad Yasevich {
4337cc08b55SWei Yongjun 	struct sctp_shared_key *key;
4341f485649SVlad Yasevich 
4351f485649SVlad Yasevich 	/* First search associations set of endpoint pair shared keys */
4361f485649SVlad Yasevich 	key_for_each(key, &asoc->endpoint_shared_keys) {
437601590ecSXin Long 		if (key->key_id == key_id) {
438601590ecSXin Long 			if (!key->deactivated)
4397cc08b55SWei Yongjun 				return key;
440601590ecSXin Long 			break;
441601590ecSXin Long 		}
4421f485649SVlad Yasevich 	}
4431f485649SVlad Yasevich 
4447cc08b55SWei Yongjun 	return NULL;
4451f485649SVlad Yasevich }
4461f485649SVlad Yasevich 
4471f485649SVlad Yasevich /*
448861e7021SRandy Dunlap  * Initialize all the possible digest transforms that we can use.  Right
4491f485649SVlad Yasevich  * now, the supported digests are SHA1 and SHA256.  We do this here once
4501f485649SVlad Yasevich  * because of the restrictiong that transforms may only be allocated in
4511f485649SVlad Yasevich  * user context.  This forces us to pre-allocated all possible transforms
4521f485649SVlad Yasevich  * at the endpoint init time.
4531f485649SVlad Yasevich  */
sctp_auth_init_hmacs(struct sctp_endpoint * ep,gfp_t gfp)4541f485649SVlad Yasevich int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
4551f485649SVlad Yasevich {
4565821c769SHerbert Xu 	struct crypto_shash *tfm = NULL;
4571f485649SVlad Yasevich 	__u16   id;
4581f485649SVlad Yasevich 
459b14878ccSVlad Yasevich 	/* If the transforms are already allocated, we are done */
4601f485649SVlad Yasevich 	if (ep->auth_hmacs)
4611f485649SVlad Yasevich 		return 0;
4621f485649SVlad Yasevich 
4631f485649SVlad Yasevich 	/* Allocated the array of pointers to transorms */
4646396bb22SKees Cook 	ep->auth_hmacs = kcalloc(SCTP_AUTH_NUM_HMACS,
4656396bb22SKees Cook 				 sizeof(struct crypto_shash *),
4666396bb22SKees Cook 				 gfp);
4671f485649SVlad Yasevich 	if (!ep->auth_hmacs)
4681f485649SVlad Yasevich 		return -ENOMEM;
4691f485649SVlad Yasevich 
4701f485649SVlad Yasevich 	for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
4711f485649SVlad Yasevich 
4721f485649SVlad Yasevich 		/* See is we support the id.  Supported IDs have name and
4731f485649SVlad Yasevich 		 * length fields set, so that we can allocated and use
4741f485649SVlad Yasevich 		 * them.  We can safely just check for name, for without the
4751f485649SVlad Yasevich 		 * name, we can't allocate the TFM.
4761f485649SVlad Yasevich 		 */
4771f485649SVlad Yasevich 		if (!sctp_hmac_list[id].hmac_name)
4781f485649SVlad Yasevich 			continue;
4791f485649SVlad Yasevich 
4801f485649SVlad Yasevich 		/* If this TFM has been allocated, we are all set */
4811f485649SVlad Yasevich 		if (ep->auth_hmacs[id])
4821f485649SVlad Yasevich 			continue;
4831f485649SVlad Yasevich 
4841f485649SVlad Yasevich 		/* Allocate the ID */
4855821c769SHerbert Xu 		tfm = crypto_alloc_shash(sctp_hmac_list[id].hmac_name, 0, 0);
4861f485649SVlad Yasevich 		if (IS_ERR(tfm))
4871f485649SVlad Yasevich 			goto out_err;
4881f485649SVlad Yasevich 
4891f485649SVlad Yasevich 		ep->auth_hmacs[id] = tfm;
4901f485649SVlad Yasevich 	}
4911f485649SVlad Yasevich 
4921f485649SVlad Yasevich 	return 0;
4931f485649SVlad Yasevich 
4941f485649SVlad Yasevich out_err:
49573ac36eaSColy Li 	/* Clean up any successful allocations */
4961f485649SVlad Yasevich 	sctp_auth_destroy_hmacs(ep->auth_hmacs);
497d42ee76eSEric Dumazet 	ep->auth_hmacs = NULL;
4981f485649SVlad Yasevich 	return -ENOMEM;
4991f485649SVlad Yasevich }
5001f485649SVlad Yasevich 
5011f485649SVlad Yasevich /* Destroy the hmac tfm array */
sctp_auth_destroy_hmacs(struct crypto_shash * auth_hmacs[])5025821c769SHerbert Xu void sctp_auth_destroy_hmacs(struct crypto_shash *auth_hmacs[])
5031f485649SVlad Yasevich {
5041f485649SVlad Yasevich 	int i;
5051f485649SVlad Yasevich 
5061f485649SVlad Yasevich 	if (!auth_hmacs)
5071f485649SVlad Yasevich 		return;
5081f485649SVlad Yasevich 
5098d72651dSwangweidong 	for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++) {
5105821c769SHerbert Xu 		crypto_free_shash(auth_hmacs[i]);
5111f485649SVlad Yasevich 	}
5121f485649SVlad Yasevich 	kfree(auth_hmacs);
5131f485649SVlad Yasevich }
5141f485649SVlad Yasevich 
5151f485649SVlad Yasevich 
sctp_auth_get_hmac(__u16 hmac_id)5161f485649SVlad Yasevich struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
5171f485649SVlad Yasevich {
5181f485649SVlad Yasevich 	return &sctp_hmac_list[hmac_id];
5191f485649SVlad Yasevich }
5201f485649SVlad Yasevich 
5211f485649SVlad Yasevich /* Get an hmac description information that we can use to build
5221f485649SVlad Yasevich  * the AUTH chunk
5231f485649SVlad Yasevich  */
sctp_auth_asoc_get_hmac(const struct sctp_association * asoc)5241f485649SVlad Yasevich struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
5251f485649SVlad Yasevich {
5261f485649SVlad Yasevich 	struct sctp_hmac_algo_param *hmacs;
5271f485649SVlad Yasevich 	__u16 n_elt;
5281f485649SVlad Yasevich 	__u16 id = 0;
5291f485649SVlad Yasevich 	int i;
5301f485649SVlad Yasevich 
5311f485649SVlad Yasevich 	/* If we have a default entry, use it */
5321f485649SVlad Yasevich 	if (asoc->default_hmac_id)
5331f485649SVlad Yasevich 		return &sctp_hmac_list[asoc->default_hmac_id];
5341f485649SVlad Yasevich 
5351f485649SVlad Yasevich 	/* Since we do not have a default entry, find the first entry
5361f485649SVlad Yasevich 	 * we support and return that.  Do not cache that id.
5371f485649SVlad Yasevich 	 */
5381f485649SVlad Yasevich 	hmacs = asoc->peer.peer_hmacs;
5391f485649SVlad Yasevich 	if (!hmacs)
5401f485649SVlad Yasevich 		return NULL;
5411f485649SVlad Yasevich 
5423c918704SXin Long 	n_elt = (ntohs(hmacs->param_hdr.length) -
5433c918704SXin Long 		 sizeof(struct sctp_paramhdr)) >> 1;
5441f485649SVlad Yasevich 	for (i = 0; i < n_elt; i++) {
5451f485649SVlad Yasevich 		id = ntohs(hmacs->hmac_ids[i]);
5461f485649SVlad Yasevich 
547747edc0fSwangweidong 		/* Check the id is in the supported range. And
548747edc0fSwangweidong 		 * see if we support the id.  Supported IDs have name and
549747edc0fSwangweidong 		 * length fields set, so that we can allocate and use
5501f485649SVlad Yasevich 		 * them.  We can safely just check for name, for without the
5511f485649SVlad Yasevich 		 * name, we can't allocate the TFM.
5521f485649SVlad Yasevich 		 */
553747edc0fSwangweidong 		if (id > SCTP_AUTH_HMAC_ID_MAX ||
554747edc0fSwangweidong 		    !sctp_hmac_list[id].hmac_name) {
55551e97a12SDan Rosenberg 			id = 0;
5561f485649SVlad Yasevich 			continue;
55751e97a12SDan Rosenberg 		}
5581f485649SVlad Yasevich 
5591f485649SVlad Yasevich 		break;
5601f485649SVlad Yasevich 	}
5611f485649SVlad Yasevich 
5621f485649SVlad Yasevich 	if (id == 0)
5631f485649SVlad Yasevich 		return NULL;
5641f485649SVlad Yasevich 
5651f485649SVlad Yasevich 	return &sctp_hmac_list[id];
5661f485649SVlad Yasevich }
5671f485649SVlad Yasevich 
__sctp_auth_find_hmacid(__be16 * hmacs,int n_elts,__be16 hmac_id)568d06f6082SAl Viro static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
5691f485649SVlad Yasevich {
5701f485649SVlad Yasevich 	int  found = 0;
5711f485649SVlad Yasevich 	int  i;
5721f485649SVlad Yasevich 
5731f485649SVlad Yasevich 	for (i = 0; i < n_elts; i++) {
5741f485649SVlad Yasevich 		if (hmac_id == hmacs[i]) {
5751f485649SVlad Yasevich 			found = 1;
5761f485649SVlad Yasevich 			break;
5771f485649SVlad Yasevich 		}
5781f485649SVlad Yasevich 	}
5791f485649SVlad Yasevich 
5801f485649SVlad Yasevich 	return found;
5811f485649SVlad Yasevich }
5821f485649SVlad Yasevich 
5831f485649SVlad Yasevich /* See if the HMAC_ID is one that we claim as supported */
sctp_auth_asoc_verify_hmac_id(const struct sctp_association * asoc,__be16 hmac_id)5841f485649SVlad Yasevich int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
585d06f6082SAl Viro 				    __be16 hmac_id)
5861f485649SVlad Yasevich {
5871f485649SVlad Yasevich 	struct sctp_hmac_algo_param *hmacs;
5881f485649SVlad Yasevich 	__u16 n_elt;
5891f485649SVlad Yasevich 
5901f485649SVlad Yasevich 	if (!asoc)
5911f485649SVlad Yasevich 		return 0;
5921f485649SVlad Yasevich 
5931f485649SVlad Yasevich 	hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
5943c918704SXin Long 	n_elt = (ntohs(hmacs->param_hdr.length) -
5953c918704SXin Long 		 sizeof(struct sctp_paramhdr)) >> 1;
5961f485649SVlad Yasevich 
5971f485649SVlad Yasevich 	return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);
5981f485649SVlad Yasevich }
5991f485649SVlad Yasevich 
6001f485649SVlad Yasevich 
6011f485649SVlad Yasevich /* Cache the default HMAC id.  This to follow this text from SCTP-AUTH:
6021f485649SVlad Yasevich  * Section 6.1:
6031f485649SVlad Yasevich  *   The receiver of a HMAC-ALGO parameter SHOULD use the first listed
6041f485649SVlad Yasevich  *   algorithm it supports.
6051f485649SVlad Yasevich  */
sctp_auth_asoc_set_default_hmac(struct sctp_association * asoc,struct sctp_hmac_algo_param * hmacs)6061f485649SVlad Yasevich void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc,
6071f485649SVlad Yasevich 				     struct sctp_hmac_algo_param *hmacs)
6081f485649SVlad Yasevich {
6091f485649SVlad Yasevich 	struct sctp_endpoint *ep;
6101f485649SVlad Yasevich 	__u16   id;
6111f485649SVlad Yasevich 	int	i;
6121f485649SVlad Yasevich 	int	n_params;
6131f485649SVlad Yasevich 
6141f485649SVlad Yasevich 	/* if the default id is already set, use it */
6151f485649SVlad Yasevich 	if (asoc->default_hmac_id)
6161f485649SVlad Yasevich 		return;
6171f485649SVlad Yasevich 
6183c918704SXin Long 	n_params = (ntohs(hmacs->param_hdr.length) -
6193c918704SXin Long 		    sizeof(struct sctp_paramhdr)) >> 1;
6201f485649SVlad Yasevich 	ep = asoc->ep;
6211f485649SVlad Yasevich 	for (i = 0; i < n_params; i++) {
6221f485649SVlad Yasevich 		id = ntohs(hmacs->hmac_ids[i]);
6231f485649SVlad Yasevich 
6241f485649SVlad Yasevich 		/* Check the id is in the supported range */
6251f485649SVlad Yasevich 		if (id > SCTP_AUTH_HMAC_ID_MAX)
6261f485649SVlad Yasevich 			continue;
6271f485649SVlad Yasevich 
6281f485649SVlad Yasevich 		/* If this TFM has been allocated, use this id */
6291f485649SVlad Yasevich 		if (ep->auth_hmacs[id]) {
6301f485649SVlad Yasevich 			asoc->default_hmac_id = id;
6311f485649SVlad Yasevich 			break;
6321f485649SVlad Yasevich 		}
6331f485649SVlad Yasevich 	}
6341f485649SVlad Yasevich }
6351f485649SVlad Yasevich 
6361f485649SVlad Yasevich 
6371f485649SVlad Yasevich /* Check to see if the given chunk is supposed to be authenticated */
__sctp_auth_cid(enum sctp_cid chunk,struct sctp_chunks_param * param)6386d85e68fSXin Long static int __sctp_auth_cid(enum sctp_cid chunk, struct sctp_chunks_param *param)
6391f485649SVlad Yasevich {
6401f485649SVlad Yasevich 	unsigned short len;
6411f485649SVlad Yasevich 	int found = 0;
6421f485649SVlad Yasevich 	int i;
6431f485649SVlad Yasevich 
644555d3d5dSVlad Yasevich 	if (!param || param->param_hdr.length == 0)
6451f485649SVlad Yasevich 		return 0;
6461f485649SVlad Yasevich 
6473c918704SXin Long 	len = ntohs(param->param_hdr.length) - sizeof(struct sctp_paramhdr);
6481f485649SVlad Yasevich 
6491f485649SVlad Yasevich 	/* SCTP-AUTH, Section 3.2
6501f485649SVlad Yasevich 	 *    The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH
6511f485649SVlad Yasevich 	 *    chunks MUST NOT be listed in the CHUNKS parameter.  However, if
6521f485649SVlad Yasevich 	 *    a CHUNKS parameter is received then the types for INIT, INIT-ACK,
6531f485649SVlad Yasevich 	 *    SHUTDOWN-COMPLETE and AUTH chunks MUST be ignored.
6541f485649SVlad Yasevich 	 */
6551f485649SVlad Yasevich 	for (i = 0; !found && i < len; i++) {
6561f485649SVlad Yasevich 		switch (param->chunks[i]) {
6571f485649SVlad Yasevich 		case SCTP_CID_INIT:
6581f485649SVlad Yasevich 		case SCTP_CID_INIT_ACK:
6591f485649SVlad Yasevich 		case SCTP_CID_SHUTDOWN_COMPLETE:
6601f485649SVlad Yasevich 		case SCTP_CID_AUTH:
6611f485649SVlad Yasevich 			break;
6621f485649SVlad Yasevich 
6631f485649SVlad Yasevich 		default:
6641f485649SVlad Yasevich 			if (param->chunks[i] == chunk)
6651f485649SVlad Yasevich 				found = 1;
6661f485649SVlad Yasevich 			break;
6671f485649SVlad Yasevich 		}
6681f485649SVlad Yasevich 	}
6691f485649SVlad Yasevich 
6701f485649SVlad Yasevich 	return found;
6711f485649SVlad Yasevich }
6721f485649SVlad Yasevich 
6731f485649SVlad Yasevich /* Check if peer requested that this chunk is authenticated */
sctp_auth_send_cid(enum sctp_cid chunk,const struct sctp_association * asoc)6746d85e68fSXin Long int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
6751f485649SVlad Yasevich {
676e1fc3b14SEric W. Biederman 	if (!asoc)
677e1fc3b14SEric W. Biederman 		return 0;
678e1fc3b14SEric W. Biederman 
679219f9ea4SXin Long 	if (!asoc->peer.auth_capable)
6801f485649SVlad Yasevich 		return 0;
6811f485649SVlad Yasevich 
6821f485649SVlad Yasevich 	return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
6831f485649SVlad Yasevich }
6841f485649SVlad Yasevich 
6851f485649SVlad Yasevich /* Check if we requested that peer authenticate this chunk. */
sctp_auth_recv_cid(enum sctp_cid chunk,const struct sctp_association * asoc)6866d85e68fSXin Long int sctp_auth_recv_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
6871f485649SVlad Yasevich {
688e1fc3b14SEric W. Biederman 	if (!asoc)
689e1fc3b14SEric W. Biederman 		return 0;
690e1fc3b14SEric W. Biederman 
691219f9ea4SXin Long 	if (!asoc->peer.auth_capable)
6921f485649SVlad Yasevich 		return 0;
6931f485649SVlad Yasevich 
6941f485649SVlad Yasevich 	return __sctp_auth_cid(chunk,
6951f485649SVlad Yasevich 			      (struct sctp_chunks_param *)asoc->c.auth_chunks);
6961f485649SVlad Yasevich }
6971f485649SVlad Yasevich 
6981f485649SVlad Yasevich /* SCTP-AUTH: Section 6.2:
6991f485649SVlad Yasevich  *    The sender MUST calculate the MAC as described in RFC2104 [2] using
7001f485649SVlad Yasevich  *    the hash function H as described by the MAC Identifier and the shared
7011f485649SVlad Yasevich  *    association key K based on the endpoint pair shared key described by
7021f485649SVlad Yasevich  *    the shared key identifier.  The 'data' used for the computation of
7031f485649SVlad Yasevich  *    the AUTH-chunk is given by the AUTH chunk with its HMAC field set to
7041f485649SVlad Yasevich  *    zero (as shown in Figure 6) followed by all chunks that are placed
7051f485649SVlad Yasevich  *    after the AUTH chunk in the SCTP packet.
7061f485649SVlad Yasevich  */
sctp_auth_calculate_hmac(const struct sctp_association * asoc,struct sk_buff * skb,struct sctp_auth_chunk * auth,struct sctp_shared_key * ep_key,gfp_t gfp)7071f485649SVlad Yasevich void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
7081b1e0bc9SXin Long 			      struct sk_buff *skb, struct sctp_auth_chunk *auth,
7091b1e0bc9SXin Long 			      struct sctp_shared_key *ep_key, gfp_t gfp)
7101f485649SVlad Yasevich {
7111f485649SVlad Yasevich 	struct sctp_auth_bytes *asoc_key;
7121b1e0bc9SXin Long 	struct crypto_shash *tfm;
7131f485649SVlad Yasevich 	__u16 key_id, hmac_id;
7141f485649SVlad Yasevich 	unsigned char *end;
7151f485649SVlad Yasevich 	int free_key = 0;
7161b1e0bc9SXin Long 	__u8 *digest;
7171f485649SVlad Yasevich 
7181f485649SVlad Yasevich 	/* Extract the info we need:
7191f485649SVlad Yasevich 	 * - hmac id
7201f485649SVlad Yasevich 	 * - key id
7211f485649SVlad Yasevich 	 */
7221f485649SVlad Yasevich 	key_id = ntohs(auth->auth_hdr.shkey_id);
7231f485649SVlad Yasevich 	hmac_id = ntohs(auth->auth_hdr.hmac_id);
7241f485649SVlad Yasevich 
7251f485649SVlad Yasevich 	if (key_id == asoc->active_key_id)
7261f485649SVlad Yasevich 		asoc_key = asoc->asoc_shared_key;
7271f485649SVlad Yasevich 	else {
7281b1e0bc9SXin Long 		/* ep_key can't be NULL here */
7291f485649SVlad Yasevich 		asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
7301f485649SVlad Yasevich 		if (!asoc_key)
7311f485649SVlad Yasevich 			return;
7321f485649SVlad Yasevich 
7331f485649SVlad Yasevich 		free_key = 1;
7341f485649SVlad Yasevich 	}
7351f485649SVlad Yasevich 
7361f485649SVlad Yasevich 	/* set up scatter list */
7371f485649SVlad Yasevich 	end = skb_tail_pointer(skb);
7381f485649SVlad Yasevich 
7395821c769SHerbert Xu 	tfm = asoc->ep->auth_hmacs[hmac_id];
7401f485649SVlad Yasevich 
741*2ab399a9SXin Long 	digest = (u8 *)(&auth->auth_hdr + 1);
7425821c769SHerbert Xu 	if (crypto_shash_setkey(tfm, &asoc_key->data[0], asoc_key->len))
7431f485649SVlad Yasevich 		goto free;
7441f485649SVlad Yasevich 
74575b93c63SEric Biggers 	crypto_shash_tfm_digest(tfm, (u8 *)auth, end - (unsigned char *)auth,
74675b93c63SEric Biggers 				digest);
7471f485649SVlad Yasevich 
7481f485649SVlad Yasevich free:
7491f485649SVlad Yasevich 	if (free_key)
7501f485649SVlad Yasevich 		sctp_auth_key_put(asoc_key);
7511f485649SVlad Yasevich }
75265b07e5dSVlad Yasevich 
75365b07e5dSVlad Yasevich /* API Helpers */
75465b07e5dSVlad Yasevich 
75565b07e5dSVlad Yasevich /* Add a chunk to the endpoint authenticated chunk list */
sctp_auth_ep_add_chunkid(struct sctp_endpoint * ep,__u8 chunk_id)75665b07e5dSVlad Yasevich int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
75765b07e5dSVlad Yasevich {
75865b07e5dSVlad Yasevich 	struct sctp_chunks_param *p = ep->auth_chunk_list;
75965b07e5dSVlad Yasevich 	__u16 nchunks;
76065b07e5dSVlad Yasevich 	__u16 param_len;
76165b07e5dSVlad Yasevich 
76265b07e5dSVlad Yasevich 	/* If this chunk is already specified, we are done */
76365b07e5dSVlad Yasevich 	if (__sctp_auth_cid(chunk_id, p))
76465b07e5dSVlad Yasevich 		return 0;
76565b07e5dSVlad Yasevich 
76665b07e5dSVlad Yasevich 	/* Check if we can add this chunk to the array */
76765b07e5dSVlad Yasevich 	param_len = ntohs(p->param_hdr.length);
7683c918704SXin Long 	nchunks = param_len - sizeof(struct sctp_paramhdr);
76965b07e5dSVlad Yasevich 	if (nchunks == SCTP_NUM_CHUNK_TYPES)
77065b07e5dSVlad Yasevich 		return -EINVAL;
77165b07e5dSVlad Yasevich 
77265b07e5dSVlad Yasevich 	p->chunks[nchunks] = chunk_id;
77365b07e5dSVlad Yasevich 	p->param_hdr.length = htons(param_len + 1);
77465b07e5dSVlad Yasevich 	return 0;
77565b07e5dSVlad Yasevich }
77665b07e5dSVlad Yasevich 
77765b07e5dSVlad Yasevich /* Add hmac identifires to the endpoint list of supported hmac ids */
sctp_auth_ep_set_hmacs(struct sctp_endpoint * ep,struct sctp_hmacalgo * hmacs)77865b07e5dSVlad Yasevich int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
77965b07e5dSVlad Yasevich 			   struct sctp_hmacalgo *hmacs)
78065b07e5dSVlad Yasevich {
78165b07e5dSVlad Yasevich 	int has_sha1 = 0;
78265b07e5dSVlad Yasevich 	__u16 id;
78365b07e5dSVlad Yasevich 	int i;
78465b07e5dSVlad Yasevich 
78565b07e5dSVlad Yasevich 	/* Scan the list looking for unsupported id.  Also make sure that
78665b07e5dSVlad Yasevich 	 * SHA1 is specified.
78765b07e5dSVlad Yasevich 	 */
78865b07e5dSVlad Yasevich 	for (i = 0; i < hmacs->shmac_num_idents; i++) {
78965b07e5dSVlad Yasevich 		id = hmacs->shmac_idents[i];
79065b07e5dSVlad Yasevich 
791d9724055SVlad Yasevich 		if (id > SCTP_AUTH_HMAC_ID_MAX)
792d9724055SVlad Yasevich 			return -EOPNOTSUPP;
793d9724055SVlad Yasevich 
79465b07e5dSVlad Yasevich 		if (SCTP_AUTH_HMAC_ID_SHA1 == id)
79565b07e5dSVlad Yasevich 			has_sha1 = 1;
79665b07e5dSVlad Yasevich 
79765b07e5dSVlad Yasevich 		if (!sctp_hmac_list[id].hmac_name)
79865b07e5dSVlad Yasevich 			return -EOPNOTSUPP;
79965b07e5dSVlad Yasevich 	}
80065b07e5dSVlad Yasevich 
80165b07e5dSVlad Yasevich 	if (!has_sha1)
80265b07e5dSVlad Yasevich 		return -EINVAL;
80365b07e5dSVlad Yasevich 
804ed5a377dSlucien 	for (i = 0; i < hmacs->shmac_num_idents; i++)
8053c918704SXin Long 		ep->auth_hmacs_list->hmac_ids[i] =
8063c918704SXin Long 				htons(hmacs->shmac_idents[i]);
8073c918704SXin Long 	ep->auth_hmacs_list->param_hdr.length =
8083c918704SXin Long 			htons(sizeof(struct sctp_paramhdr) +
80965b07e5dSVlad Yasevich 			hmacs->shmac_num_idents * sizeof(__u16));
81065b07e5dSVlad Yasevich 	return 0;
81165b07e5dSVlad Yasevich }
81265b07e5dSVlad Yasevich 
81365b07e5dSVlad Yasevich /* Set a new shared key on either endpoint or association.  If the
814861e7021SRandy Dunlap  * key with a same ID already exists, replace the key (remove the
81565b07e5dSVlad Yasevich  * old key and add a new one).
81665b07e5dSVlad Yasevich  */
sctp_auth_set_key(struct sctp_endpoint * ep,struct sctp_association * asoc,struct sctp_authkey * auth_key)81765b07e5dSVlad Yasevich int sctp_auth_set_key(struct sctp_endpoint *ep,
81865b07e5dSVlad Yasevich 		      struct sctp_association *asoc,
81965b07e5dSVlad Yasevich 		      struct sctp_authkey *auth_key)
82065b07e5dSVlad Yasevich {
8211b1e0bc9SXin Long 	struct sctp_shared_key *cur_key, *shkey;
82265b07e5dSVlad Yasevich 	struct sctp_auth_bytes *key;
82365b07e5dSVlad Yasevich 	struct list_head *sh_keys;
82465b07e5dSVlad Yasevich 	int replace = 0;
82565b07e5dSVlad Yasevich 
82665b07e5dSVlad Yasevich 	/* Try to find the given key id to see if
82765b07e5dSVlad Yasevich 	 * we are doing a replace, or adding a new key
82865b07e5dSVlad Yasevich 	 */
829219f9ea4SXin Long 	if (asoc) {
830219f9ea4SXin Long 		if (!asoc->peer.auth_capable)
831219f9ea4SXin Long 			return -EACCES;
83265b07e5dSVlad Yasevich 		sh_keys = &asoc->endpoint_shared_keys;
833219f9ea4SXin Long 	} else {
834219f9ea4SXin Long 		if (!ep->auth_enable)
835219f9ea4SXin Long 			return -EACCES;
83665b07e5dSVlad Yasevich 		sh_keys = &ep->endpoint_shared_keys;
837219f9ea4SXin Long 	}
83865b07e5dSVlad Yasevich 
8391b1e0bc9SXin Long 	key_for_each(shkey, sh_keys) {
8401b1e0bc9SXin Long 		if (shkey->key_id == auth_key->sca_keynumber) {
84165b07e5dSVlad Yasevich 			replace = 1;
84265b07e5dSVlad Yasevich 			break;
84365b07e5dSVlad Yasevich 		}
84465b07e5dSVlad Yasevich 	}
84565b07e5dSVlad Yasevich 
8461b1e0bc9SXin Long 	cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL);
84765b07e5dSVlad Yasevich 	if (!cur_key)
84865b07e5dSVlad Yasevich 		return -ENOMEM;
84965b07e5dSVlad Yasevich 
85065b07e5dSVlad Yasevich 	/* Create a new key data based on the info passed in */
8517e8616d8SVlad Yasevich 	key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
8521b1e0bc9SXin Long 	if (!key) {
8531b1e0bc9SXin Long 		kfree(cur_key);
8541b1e0bc9SXin Long 		return -ENOMEM;
8551b1e0bc9SXin Long 	}
85665b07e5dSVlad Yasevich 
8577e8616d8SVlad Yasevich 	memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
8581b1e0bc9SXin Long 	cur_key->key = key;
85965b07e5dSVlad Yasevich 
860ae954bbcSXin Long 	if (!replace) {
861ae954bbcSXin Long 		list_add(&cur_key->key_list, sh_keys);
862ae954bbcSXin Long 		return 0;
863ae954bbcSXin Long 	}
864ae954bbcSXin Long 
8651b1e0bc9SXin Long 	list_del_init(&shkey->key_list);
866ae954bbcSXin Long 	list_add(&cur_key->key_list, sh_keys);
867ae954bbcSXin Long 
868022152aaSXin Long 	if (asoc && asoc->active_key_id == auth_key->sca_keynumber &&
869022152aaSXin Long 	    sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) {
870022152aaSXin Long 		list_del_init(&cur_key->key_list);
871022152aaSXin Long 		sctp_auth_shkey_release(cur_key);
872022152aaSXin Long 		list_add(&shkey->key_list, sh_keys);
873022152aaSXin Long 		return -ENOMEM;
874022152aaSXin Long 	}
87565b07e5dSVlad Yasevich 
876022152aaSXin Long 	sctp_auth_shkey_release(shkey);
87765b07e5dSVlad Yasevich 	return 0;
87865b07e5dSVlad Yasevich }
87965b07e5dSVlad Yasevich 
sctp_auth_set_active_key(struct sctp_endpoint * ep,struct sctp_association * asoc,__u16 key_id)88065b07e5dSVlad Yasevich int sctp_auth_set_active_key(struct sctp_endpoint *ep,
88165b07e5dSVlad Yasevich 			     struct sctp_association *asoc,
88265b07e5dSVlad Yasevich 			     __u16  key_id)
88365b07e5dSVlad Yasevich {
88465b07e5dSVlad Yasevich 	struct sctp_shared_key *key;
88565b07e5dSVlad Yasevich 	struct list_head *sh_keys;
88665b07e5dSVlad Yasevich 	int found = 0;
88765b07e5dSVlad Yasevich 
88865b07e5dSVlad Yasevich 	/* The key identifier MUST correst to an existing key */
889219f9ea4SXin Long 	if (asoc) {
890219f9ea4SXin Long 		if (!asoc->peer.auth_capable)
891219f9ea4SXin Long 			return -EACCES;
89265b07e5dSVlad Yasevich 		sh_keys = &asoc->endpoint_shared_keys;
893219f9ea4SXin Long 	} else {
894219f9ea4SXin Long 		if (!ep->auth_enable)
895219f9ea4SXin Long 			return -EACCES;
89665b07e5dSVlad Yasevich 		sh_keys = &ep->endpoint_shared_keys;
897219f9ea4SXin Long 	}
89865b07e5dSVlad Yasevich 
89965b07e5dSVlad Yasevich 	key_for_each(key, sh_keys) {
90065b07e5dSVlad Yasevich 		if (key->key_id == key_id) {
90165b07e5dSVlad Yasevich 			found = 1;
90265b07e5dSVlad Yasevich 			break;
90365b07e5dSVlad Yasevich 		}
90465b07e5dSVlad Yasevich 	}
90565b07e5dSVlad Yasevich 
906601590ecSXin Long 	if (!found || key->deactivated)
90765b07e5dSVlad Yasevich 		return -EINVAL;
90865b07e5dSVlad Yasevich 
90965b07e5dSVlad Yasevich 	if (asoc) {
910022152aaSXin Long 		__u16  active_key_id = asoc->active_key_id;
911022152aaSXin Long 
91265b07e5dSVlad Yasevich 		asoc->active_key_id = key_id;
913022152aaSXin Long 		if (sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) {
914022152aaSXin Long 			asoc->active_key_id = active_key_id;
915022152aaSXin Long 			return -ENOMEM;
916022152aaSXin Long 		}
91765b07e5dSVlad Yasevich 	} else
91865b07e5dSVlad Yasevich 		ep->active_key_id = key_id;
91965b07e5dSVlad Yasevich 
92065b07e5dSVlad Yasevich 	return 0;
92165b07e5dSVlad Yasevich }
92265b07e5dSVlad Yasevich 
sctp_auth_del_key_id(struct sctp_endpoint * ep,struct sctp_association * asoc,__u16 key_id)92365b07e5dSVlad Yasevich int sctp_auth_del_key_id(struct sctp_endpoint *ep,
92465b07e5dSVlad Yasevich 			 struct sctp_association *asoc,
92565b07e5dSVlad Yasevich 			 __u16  key_id)
92665b07e5dSVlad Yasevich {
92765b07e5dSVlad Yasevich 	struct sctp_shared_key *key;
92865b07e5dSVlad Yasevich 	struct list_head *sh_keys;
92965b07e5dSVlad Yasevich 	int found = 0;
93065b07e5dSVlad Yasevich 
93165b07e5dSVlad Yasevich 	/* The key identifier MUST NOT be the current active key
93265b07e5dSVlad Yasevich 	 * The key identifier MUST correst to an existing key
93365b07e5dSVlad Yasevich 	 */
93465b07e5dSVlad Yasevich 	if (asoc) {
935219f9ea4SXin Long 		if (!asoc->peer.auth_capable)
936219f9ea4SXin Long 			return -EACCES;
93765b07e5dSVlad Yasevich 		if (asoc->active_key_id == key_id)
93865b07e5dSVlad Yasevich 			return -EINVAL;
93965b07e5dSVlad Yasevich 
94065b07e5dSVlad Yasevich 		sh_keys = &asoc->endpoint_shared_keys;
94165b07e5dSVlad Yasevich 	} else {
942219f9ea4SXin Long 		if (!ep->auth_enable)
943219f9ea4SXin Long 			return -EACCES;
94465b07e5dSVlad Yasevich 		if (ep->active_key_id == key_id)
94565b07e5dSVlad Yasevich 			return -EINVAL;
94665b07e5dSVlad Yasevich 
94765b07e5dSVlad Yasevich 		sh_keys = &ep->endpoint_shared_keys;
94865b07e5dSVlad Yasevich 	}
94965b07e5dSVlad Yasevich 
95065b07e5dSVlad Yasevich 	key_for_each(key, sh_keys) {
95165b07e5dSVlad Yasevich 		if (key->key_id == key_id) {
95265b07e5dSVlad Yasevich 			found = 1;
95365b07e5dSVlad Yasevich 			break;
95465b07e5dSVlad Yasevich 		}
95565b07e5dSVlad Yasevich 	}
95665b07e5dSVlad Yasevich 
95765b07e5dSVlad Yasevich 	if (!found)
95865b07e5dSVlad Yasevich 		return -EINVAL;
95965b07e5dSVlad Yasevich 
96065b07e5dSVlad Yasevich 	/* Delete the shared key */
96165b07e5dSVlad Yasevich 	list_del_init(&key->key_list);
9621b1e0bc9SXin Long 	sctp_auth_shkey_release(key);
96365b07e5dSVlad Yasevich 
96465b07e5dSVlad Yasevich 	return 0;
96565b07e5dSVlad Yasevich }
966601590ecSXin Long 
sctp_auth_deact_key_id(struct sctp_endpoint * ep,struct sctp_association * asoc,__u16 key_id)967601590ecSXin Long int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
968601590ecSXin Long 			   struct sctp_association *asoc, __u16  key_id)
969601590ecSXin Long {
970601590ecSXin Long 	struct sctp_shared_key *key;
971601590ecSXin Long 	struct list_head *sh_keys;
972601590ecSXin Long 	int found = 0;
973601590ecSXin Long 
974601590ecSXin Long 	/* The key identifier MUST NOT be the current active key
975601590ecSXin Long 	 * The key identifier MUST correst to an existing key
976601590ecSXin Long 	 */
977601590ecSXin Long 	if (asoc) {
978219f9ea4SXin Long 		if (!asoc->peer.auth_capable)
979219f9ea4SXin Long 			return -EACCES;
980601590ecSXin Long 		if (asoc->active_key_id == key_id)
981601590ecSXin Long 			return -EINVAL;
982601590ecSXin Long 
983601590ecSXin Long 		sh_keys = &asoc->endpoint_shared_keys;
984601590ecSXin Long 	} else {
985219f9ea4SXin Long 		if (!ep->auth_enable)
986219f9ea4SXin Long 			return -EACCES;
987601590ecSXin Long 		if (ep->active_key_id == key_id)
988601590ecSXin Long 			return -EINVAL;
989601590ecSXin Long 
990601590ecSXin Long 		sh_keys = &ep->endpoint_shared_keys;
991601590ecSXin Long 	}
992601590ecSXin Long 
993601590ecSXin Long 	key_for_each(key, sh_keys) {
994601590ecSXin Long 		if (key->key_id == key_id) {
995601590ecSXin Long 			found = 1;
996601590ecSXin Long 			break;
997601590ecSXin Long 		}
998601590ecSXin Long 	}
999601590ecSXin Long 
1000601590ecSXin Long 	if (!found)
1001601590ecSXin Long 		return -EINVAL;
1002601590ecSXin Long 
1003ec2e506cSXin Long 	/* refcnt == 1 and !list_empty mean it's not being used anywhere
1004ec2e506cSXin Long 	 * and deactivated will be set, so it's time to notify userland
1005ec2e506cSXin Long 	 * that this shkey can be freed.
1006ec2e506cSXin Long 	 */
1007ec2e506cSXin Long 	if (asoc && !list_empty(&key->key_list) &&
1008ec2e506cSXin Long 	    refcount_read(&key->refcnt) == 1) {
1009ec2e506cSXin Long 		struct sctp_ulpevent *ev;
1010ec2e506cSXin Long 
1011ec2e506cSXin Long 		ev = sctp_ulpevent_make_authkey(asoc, key->key_id,
1012ec2e506cSXin Long 						SCTP_AUTH_FREE_KEY, GFP_KERNEL);
1013ec2e506cSXin Long 		if (ev)
1014ec2e506cSXin Long 			asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
1015ec2e506cSXin Long 	}
1016ec2e506cSXin Long 
1017601590ecSXin Long 	key->deactivated = 1;
1018601590ecSXin Long 
1019601590ecSXin Long 	return 0;
1020601590ecSXin Long }
102103f96127SXin Long 
sctp_auth_init(struct sctp_endpoint * ep,gfp_t gfp)102203f96127SXin Long int sctp_auth_init(struct sctp_endpoint *ep, gfp_t gfp)
102303f96127SXin Long {
102403f96127SXin Long 	int err = -ENOMEM;
102503f96127SXin Long 
102603f96127SXin Long 	/* Allocate space for HMACS and CHUNKS authentication
102703f96127SXin Long 	 * variables.  There are arrays that we encode directly
102803f96127SXin Long 	 * into parameters to make the rest of the operations easier.
102903f96127SXin Long 	 */
103003f96127SXin Long 	if (!ep->auth_hmacs_list) {
103103f96127SXin Long 		struct sctp_hmac_algo_param *auth_hmacs;
103203f96127SXin Long 
103303f96127SXin Long 		auth_hmacs = kzalloc(struct_size(auth_hmacs, hmac_ids,
103403f96127SXin Long 						 SCTP_AUTH_NUM_HMACS), gfp);
103503f96127SXin Long 		if (!auth_hmacs)
103603f96127SXin Long 			goto nomem;
103703f96127SXin Long 		/* Initialize the HMACS parameter.
103803f96127SXin Long 		 * SCTP-AUTH: Section 3.3
103903f96127SXin Long 		 *    Every endpoint supporting SCTP chunk authentication MUST
104003f96127SXin Long 		 *    support the HMAC based on the SHA-1 algorithm.
104103f96127SXin Long 		 */
104203f96127SXin Long 		auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
104303f96127SXin Long 		auth_hmacs->param_hdr.length =
104403f96127SXin Long 				htons(sizeof(struct sctp_paramhdr) + 2);
104503f96127SXin Long 		auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
104603f96127SXin Long 		ep->auth_hmacs_list = auth_hmacs;
104703f96127SXin Long 	}
104803f96127SXin Long 
104903f96127SXin Long 	if (!ep->auth_chunk_list) {
105003f96127SXin Long 		struct sctp_chunks_param *auth_chunks;
105103f96127SXin Long 
105203f96127SXin Long 		auth_chunks = kzalloc(sizeof(*auth_chunks) +
105303f96127SXin Long 				      SCTP_NUM_CHUNK_TYPES, gfp);
105403f96127SXin Long 		if (!auth_chunks)
105503f96127SXin Long 			goto nomem;
105603f96127SXin Long 		/* Initialize the CHUNKS parameter */
105703f96127SXin Long 		auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
105803f96127SXin Long 		auth_chunks->param_hdr.length =
105903f96127SXin Long 				htons(sizeof(struct sctp_paramhdr));
106003f96127SXin Long 		ep->auth_chunk_list = auth_chunks;
106103f96127SXin Long 	}
106203f96127SXin Long 
106303f96127SXin Long 	/* Allocate and initialize transorms arrays for supported
106403f96127SXin Long 	 * HMACs.
106503f96127SXin Long 	 */
106603f96127SXin Long 	err = sctp_auth_init_hmacs(ep, gfp);
106703f96127SXin Long 	if (err)
106803f96127SXin Long 		goto nomem;
106903f96127SXin Long 
107003f96127SXin Long 	return 0;
107103f96127SXin Long 
107203f96127SXin Long nomem:
107303f96127SXin Long 	/* Free all allocations */
107403f96127SXin Long 	kfree(ep->auth_hmacs_list);
107503f96127SXin Long 	kfree(ep->auth_chunk_list);
107603f96127SXin Long 	ep->auth_hmacs_list = NULL;
107703f96127SXin Long 	ep->auth_chunk_list = NULL;
107803f96127SXin Long 	return err;
107903f96127SXin Long }
108003f96127SXin Long 
sctp_auth_free(struct sctp_endpoint * ep)108103f96127SXin Long void sctp_auth_free(struct sctp_endpoint *ep)
108203f96127SXin Long {
108303f96127SXin Long 	kfree(ep->auth_hmacs_list);
108403f96127SXin Long 	kfree(ep->auth_chunk_list);
108503f96127SXin Long 	ep->auth_hmacs_list = NULL;
108603f96127SXin Long 	ep->auth_chunk_list = NULL;
108703f96127SXin Long 	sctp_auth_destroy_hmacs(ep->auth_hmacs);
108803f96127SXin Long 	ep->auth_hmacs = NULL;
108903f96127SXin Long }
1090