1/* SCTP kernel implementation
2 * (C) Copyright 2007 Hewlett-Packard Development Company, L.P.
3 *
4 * This file is part of the SCTP kernel implementation
5 *
6 * This SCTP implementation is free software;
7 * you can redistribute it and/or modify it under the terms of
8 * the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This SCTP implementation is distributed in the hope that it
13 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 *                 ************************
15 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU CC; see the file COPYING.  If not, write to
20 * the Free Software Foundation, 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *
23 * Please send any bug reports or fixes you make to the
24 * email address(es):
25 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
26 *
27 * Or submit a bug report through the following website:
28 *    http://www.sf.net/projects/lksctp
29 *
30 * Written or modified by:
31 *   Vlad Yasevich     <vladislav.yasevich@hp.com>
32 *
33 * Any bugs reported given to us we will try to fix... any fixes shared will
34 * be incorporated into the next SCTP release.
35 */
36
37#include <linux/slab.h>
38#include <linux/types.h>
39#include <linux/crypto.h>
40#include <linux/scatterlist.h>
41#include <net/sctp/sctp.h>
42#include <net/sctp/auth.h>
43
44static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
45	{
46		/* id 0 is reserved.  as all 0 */
47		.hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
48	},
49	{
50		.hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
51		.hmac_name="hmac(sha1)",
52		.hmac_len = SCTP_SHA1_SIG_SIZE,
53	},
54	{
55		/* id 2 is reserved as well */
56		.hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
57	},
58#if defined (CONFIG_CRYPTO_SHA256) || defined (CONFIG_CRYPTO_SHA256_MODULE)
59	{
60		.hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
61		.hmac_name="hmac(sha256)",
62		.hmac_len = SCTP_SHA256_SIG_SIZE,
63	}
64#endif
65};
66
67
68void sctp_auth_key_put(struct sctp_auth_bytes *key)
69{
70	if (!key)
71		return;
72
73	if (atomic_dec_and_test(&key->refcnt)) {
74		kzfree(key);
75		SCTP_DBG_OBJCNT_DEC(keys);
76	}
77}
78
79/* Create a new key structure of a given length */
80static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
81{
82	struct sctp_auth_bytes *key;
83
84	/* Verify that we are not going to overflow INT_MAX */
85	if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
86		return NULL;
87
88	/* Allocate the shared key */
89	key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
90	if (!key)
91		return NULL;
92
93	key->len = key_len;
94	atomic_set(&key->refcnt, 1);
95	SCTP_DBG_OBJCNT_INC(keys);
96
97	return key;
98}
99
100/* Create a new shared key container with a give key id */
101struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
102{
103	struct sctp_shared_key *new;
104
105	/* Allocate the shared key container */
106	new = kzalloc(sizeof(struct sctp_shared_key), gfp);
107	if (!new)
108		return NULL;
109
110	INIT_LIST_HEAD(&new->key_list);
111	new->key_id = key_id;
112
113	return new;
114}
115
116/* Free the shared key structure */
117static void sctp_auth_shkey_free(struct sctp_shared_key *sh_key)
118{
119	BUG_ON(!list_empty(&sh_key->key_list));
120	sctp_auth_key_put(sh_key->key);
121	sh_key->key = NULL;
122	kfree(sh_key);
123}
124
125/* Destroy the entire key list.  This is done during the
126 * associon and endpoint free process.
127 */
128void sctp_auth_destroy_keys(struct list_head *keys)
129{
130	struct sctp_shared_key *ep_key;
131	struct sctp_shared_key *tmp;
132
133	if (list_empty(keys))
134		return;
135
136	key_for_each_safe(ep_key, tmp, keys) {
137		list_del_init(&ep_key->key_list);
138		sctp_auth_shkey_free(ep_key);
139	}
140}
141
142/* Compare two byte vectors as numbers.  Return values
143 * are:
144 * 	  0 - vectors are equal
145 * 	< 0 - vector 1 is smaller than vector2
146 * 	> 0 - vector 1 is greater than vector2
147 *
148 * Algorithm is:
149 * 	This is performed by selecting the numerically smaller key vector...
150 *	If the key vectors are equal as numbers but differ in length ...
151 *	the shorter vector is considered smaller
152 *
153 * Examples (with small values):
154 * 	000123456789 > 123456789 (first number is longer)
155 * 	000123456789 < 234567891 (second number is larger numerically)
156 * 	123456789 > 2345678 	 (first number is both larger & longer)
157 */
158static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
159			      struct sctp_auth_bytes *vector2)
160{
161	int diff;
162	int i;
163	const __u8 *longer;
164
165	diff = vector1->len - vector2->len;
166	if (diff) {
167		longer = (diff > 0) ? vector1->data : vector2->data;
168
169		/* Check to see if the longer number is
170		 * lead-zero padded.  If it is not, it
171		 * is automatically larger numerically.
172		 */
173		for (i = 0; i < abs(diff); i++ ) {
174			if (longer[i] != 0)
175				return diff;
176		}
177	}
178
179	/* lengths are the same, compare numbers */
180	return memcmp(vector1->data, vector2->data, vector1->len);
181}
182
183/*
184 * Create a key vector as described in SCTP-AUTH, Section 6.1
185 *    The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
186 *    parameter sent by each endpoint are concatenated as byte vectors.
187 *    These parameters include the parameter type, parameter length, and
188 *    the parameter value, but padding is omitted; all padding MUST be
189 *    removed from this concatenation before proceeding with further
190 *    computation of keys.  Parameters which were not sent are simply
191 *    omitted from the concatenation process.  The resulting two vectors
192 *    are called the two key vectors.
193 */
194static struct sctp_auth_bytes *sctp_auth_make_key_vector(
195			sctp_random_param_t *random,
196			sctp_chunks_param_t *chunks,
197			sctp_hmac_algo_param_t *hmacs,
198			gfp_t gfp)
199{
200	struct sctp_auth_bytes *new;
201	__u32	len;
202	__u32	offset = 0;
203	__u16	random_len, hmacs_len, chunks_len = 0;
204
205	random_len = ntohs(random->param_hdr.length);
206	hmacs_len = ntohs(hmacs->param_hdr.length);
207	if (chunks)
208		chunks_len = ntohs(chunks->param_hdr.length);
209
210	len = random_len + hmacs_len + chunks_len;
211
212	new = sctp_auth_create_key(len, gfp);
213	if (!new)
214		return NULL;
215
216	memcpy(new->data, random, random_len);
217	offset += random_len;
218
219	if (chunks) {
220		memcpy(new->data + offset, chunks, chunks_len);
221		offset += chunks_len;
222	}
223
224	memcpy(new->data + offset, hmacs, hmacs_len);
225
226	return new;
227}
228
229
230/* Make a key vector based on our local parameters */
231static struct sctp_auth_bytes *sctp_auth_make_local_vector(
232				    const struct sctp_association *asoc,
233				    gfp_t gfp)
234{
235	return sctp_auth_make_key_vector(
236				    (sctp_random_param_t*)asoc->c.auth_random,
237				    (sctp_chunks_param_t*)asoc->c.auth_chunks,
238				    (sctp_hmac_algo_param_t*)asoc->c.auth_hmacs,
239				    gfp);
240}
241
242/* Make a key vector based on peer's parameters */
243static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
244				    const struct sctp_association *asoc,
245				    gfp_t gfp)
246{
247	return sctp_auth_make_key_vector(asoc->peer.peer_random,
248					 asoc->peer.peer_chunks,
249					 asoc->peer.peer_hmacs,
250					 gfp);
251}
252
253
254/* Set the value of the association shared key base on the parameters
255 * given.  The algorithm is:
256 *    From the endpoint pair shared keys and the key vectors the
257 *    association shared keys are computed.  This is performed by selecting
258 *    the numerically smaller key vector and concatenating it to the
259 *    endpoint pair shared key, and then concatenating the numerically
260 *    larger key vector to that.  The result of the concatenation is the
261 *    association shared key.
262 */
263static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
264			struct sctp_shared_key *ep_key,
265			struct sctp_auth_bytes *first_vector,
266			struct sctp_auth_bytes *last_vector,
267			gfp_t gfp)
268{
269	struct sctp_auth_bytes *secret;
270	__u32 offset = 0;
271	__u32 auth_len;
272
273	auth_len = first_vector->len + last_vector->len;
274	if (ep_key->key)
275		auth_len += ep_key->key->len;
276
277	secret = sctp_auth_create_key(auth_len, gfp);
278	if (!secret)
279		return NULL;
280
281	if (ep_key->key) {
282		memcpy(secret->data, ep_key->key->data, ep_key->key->len);
283		offset += ep_key->key->len;
284	}
285
286	memcpy(secret->data + offset, first_vector->data, first_vector->len);
287	offset += first_vector->len;
288
289	memcpy(secret->data + offset, last_vector->data, last_vector->len);
290
291	return secret;
292}
293
294/* Create an association shared key.  Follow the algorithm
295 * described in SCTP-AUTH, Section 6.1
296 */
297static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
298				 const struct sctp_association *asoc,
299				 struct sctp_shared_key *ep_key,
300				 gfp_t gfp)
301{
302	struct sctp_auth_bytes *local_key_vector;
303	struct sctp_auth_bytes *peer_key_vector;
304	struct sctp_auth_bytes	*first_vector,
305				*last_vector;
306	struct sctp_auth_bytes	*secret = NULL;
307	int	cmp;
308
309
310	/* Now we need to build the key vectors
311	 * SCTP-AUTH , Section 6.1
312	 *    The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
313	 *    parameter sent by each endpoint are concatenated as byte vectors.
314	 *    These parameters include the parameter type, parameter length, and
315	 *    the parameter value, but padding is omitted; all padding MUST be
316	 *    removed from this concatenation before proceeding with further
317	 *    computation of keys.  Parameters which were not sent are simply
318	 *    omitted from the concatenation process.  The resulting two vectors
319	 *    are called the two key vectors.
320	 */
321
322	local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
323	peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
324
325	if (!peer_key_vector || !local_key_vector)
326		goto out;
327
328	/* Figure out the order in which the key_vectors will be
329	 * added to the endpoint shared key.
330	 * SCTP-AUTH, Section 6.1:
331	 *   This is performed by selecting the numerically smaller key
332	 *   vector and concatenating it to the endpoint pair shared
333	 *   key, and then concatenating the numerically larger key
334	 *   vector to that.  If the key vectors are equal as numbers
335	 *   but differ in length, then the concatenation order is the
336	 *   endpoint shared key, followed by the shorter key vector,
337	 *   followed by the longer key vector.  Otherwise, the key
338	 *   vectors are identical, and may be concatenated to the
339	 *   endpoint pair key in any order.
340	 */
341	cmp = sctp_auth_compare_vectors(local_key_vector,
342					peer_key_vector);
343	if (cmp < 0) {
344		first_vector = local_key_vector;
345		last_vector = peer_key_vector;
346	} else {
347		first_vector = peer_key_vector;
348		last_vector = local_key_vector;
349	}
350
351	secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
352					    gfp);
353out:
354	sctp_auth_key_put(local_key_vector);
355	sctp_auth_key_put(peer_key_vector);
356
357	return secret;
358}
359
360/*
361 * Populate the association overlay list with the list
362 * from the endpoint.
363 */
364int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
365				struct sctp_association *asoc,
366				gfp_t gfp)
367{
368	struct sctp_shared_key *sh_key;
369	struct sctp_shared_key *new;
370
371	BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
372
373	key_for_each(sh_key, &ep->endpoint_shared_keys) {
374		new = sctp_auth_shkey_create(sh_key->key_id, gfp);
375		if (!new)
376			goto nomem;
377
378		new->key = sh_key->key;
379		sctp_auth_key_hold(new->key);
380		list_add(&new->key_list, &asoc->endpoint_shared_keys);
381	}
382
383	return 0;
384
385nomem:
386	sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
387	return -ENOMEM;
388}
389
390
391/* Public interface to creat the association shared key.
392 * See code above for the algorithm.
393 */
394int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
395{
396	struct net *net = sock_net(asoc->base.sk);
397	struct sctp_auth_bytes	*secret;
398	struct sctp_shared_key *ep_key;
399
400	/* If we don't support AUTH, or peer is not capable
401	 * we don't need to do anything.
402	 */
403	if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
404		return 0;
405
406	/* If the key_id is non-zero and we couldn't find an
407	 * endpoint pair shared key, we can't compute the
408	 * secret.
409	 * For key_id 0, endpoint pair shared key is a NULL key.
410	 */
411	ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
412	BUG_ON(!ep_key);
413
414	secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
415	if (!secret)
416		return -ENOMEM;
417
418	sctp_auth_key_put(asoc->asoc_shared_key);
419	asoc->asoc_shared_key = secret;
420
421	return 0;
422}
423
424
425/* Find the endpoint pair shared key based on the key_id */
426struct sctp_shared_key *sctp_auth_get_shkey(
427				const struct sctp_association *asoc,
428				__u16 key_id)
429{
430	struct sctp_shared_key *key;
431
432	/* First search associations set of endpoint pair shared keys */
433	key_for_each(key, &asoc->endpoint_shared_keys) {
434		if (key->key_id == key_id)
435			return key;
436	}
437
438	return NULL;
439}
440
441/*
442 * Initialize all the possible digest transforms that we can use.  Right now
443 * now, the supported digests are SHA1 and SHA256.  We do this here once
444 * because of the restrictiong that transforms may only be allocated in
445 * user context.  This forces us to pre-allocated all possible transforms
446 * at the endpoint init time.
447 */
448int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
449{
450	struct net *net = sock_net(ep->base.sk);
451	struct crypto_hash *tfm = NULL;
452	__u16   id;
453
454	/* if the transforms are already allocted, we are done */
455	if (!net->sctp.auth_enable) {
456		ep->auth_hmacs = NULL;
457		return 0;
458	}
459
460	if (ep->auth_hmacs)
461		return 0;
462
463	/* Allocated the array of pointers to transorms */
464	ep->auth_hmacs = kzalloc(
465			    sizeof(struct crypto_hash *) * SCTP_AUTH_NUM_HMACS,
466			    gfp);
467	if (!ep->auth_hmacs)
468		return -ENOMEM;
469
470	for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
471
472		/* See is we support the id.  Supported IDs have name and
473		 * length fields set, so that we can allocated and use
474		 * them.  We can safely just check for name, for without the
475		 * name, we can't allocate the TFM.
476		 */
477		if (!sctp_hmac_list[id].hmac_name)
478			continue;
479
480		/* If this TFM has been allocated, we are all set */
481		if (ep->auth_hmacs[id])
482			continue;
483
484		/* Allocate the ID */
485		tfm = crypto_alloc_hash(sctp_hmac_list[id].hmac_name, 0,
486					CRYPTO_ALG_ASYNC);
487		if (IS_ERR(tfm))
488			goto out_err;
489
490		ep->auth_hmacs[id] = tfm;
491	}
492
493	return 0;
494
495out_err:
496	/* Clean up any successful allocations */
497	sctp_auth_destroy_hmacs(ep->auth_hmacs);
498	return -ENOMEM;
499}
500
501/* Destroy the hmac tfm array */
502void sctp_auth_destroy_hmacs(struct crypto_hash *auth_hmacs[])
503{
504	int i;
505
506	if (!auth_hmacs)
507		return;
508
509	for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++)
510	{
511		if (auth_hmacs[i])
512			crypto_free_hash(auth_hmacs[i]);
513	}
514	kfree(auth_hmacs);
515}
516
517
518struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
519{
520	return &sctp_hmac_list[hmac_id];
521}
522
523/* Get an hmac description information that we can use to build
524 * the AUTH chunk
525 */
526struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
527{
528	struct sctp_hmac_algo_param *hmacs;
529	__u16 n_elt;
530	__u16 id = 0;
531	int i;
532
533	/* If we have a default entry, use it */
534	if (asoc->default_hmac_id)
535		return &sctp_hmac_list[asoc->default_hmac_id];
536
537	/* Since we do not have a default entry, find the first entry
538	 * we support and return that.  Do not cache that id.
539	 */
540	hmacs = asoc->peer.peer_hmacs;
541	if (!hmacs)
542		return NULL;
543
544	n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1;
545	for (i = 0; i < n_elt; i++) {
546		id = ntohs(hmacs->hmac_ids[i]);
547
548		/* Check the id is in the supported range */
549		if (id > SCTP_AUTH_HMAC_ID_MAX) {
550			id = 0;
551			continue;
552		}
553
554		/* See is we support the id.  Supported IDs have name and
555		 * length fields set, so that we can allocated and use
556		 * them.  We can safely just check for name, for without the
557		 * name, we can't allocate the TFM.
558		 */
559		if (!sctp_hmac_list[id].hmac_name) {
560			id = 0;
561			continue;
562		}
563
564		break;
565	}
566
567	if (id == 0)
568		return NULL;
569
570	return &sctp_hmac_list[id];
571}
572
573static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
574{
575	int  found = 0;
576	int  i;
577
578	for (i = 0; i < n_elts; i++) {
579		if (hmac_id == hmacs[i]) {
580			found = 1;
581			break;
582		}
583	}
584
585	return found;
586}
587
588/* See if the HMAC_ID is one that we claim as supported */
589int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
590				    __be16 hmac_id)
591{
592	struct sctp_hmac_algo_param *hmacs;
593	__u16 n_elt;
594
595	if (!asoc)
596		return 0;
597
598	hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
599	n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1;
600
601	return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);
602}
603
604
605/* Cache the default HMAC id.  This to follow this text from SCTP-AUTH:
606 * Section 6.1:
607 *   The receiver of a HMAC-ALGO parameter SHOULD use the first listed
608 *   algorithm it supports.
609 */
610void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc,
611				     struct sctp_hmac_algo_param *hmacs)
612{
613	struct sctp_endpoint *ep;
614	__u16   id;
615	int	i;
616	int	n_params;
617
618	/* if the default id is already set, use it */
619	if (asoc->default_hmac_id)
620		return;
621
622	n_params = (ntohs(hmacs->param_hdr.length)
623				- sizeof(sctp_paramhdr_t)) >> 1;
624	ep = asoc->ep;
625	for (i = 0; i < n_params; i++) {
626		id = ntohs(hmacs->hmac_ids[i]);
627
628		/* Check the id is in the supported range */
629		if (id > SCTP_AUTH_HMAC_ID_MAX)
630			continue;
631
632		/* If this TFM has been allocated, use this id */
633		if (ep->auth_hmacs[id]) {
634			asoc->default_hmac_id = id;
635			break;
636		}
637	}
638}
639
640
641/* Check to see if the given chunk is supposed to be authenticated */
642static int __sctp_auth_cid(sctp_cid_t chunk, struct sctp_chunks_param *param)
643{
644	unsigned short len;
645	int found = 0;
646	int i;
647
648	if (!param || param->param_hdr.length == 0)
649		return 0;
650
651	len = ntohs(param->param_hdr.length) - sizeof(sctp_paramhdr_t);
652
653	/* SCTP-AUTH, Section 3.2
654	 *    The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH
655	 *    chunks MUST NOT be listed in the CHUNKS parameter.  However, if
656	 *    a CHUNKS parameter is received then the types for INIT, INIT-ACK,
657	 *    SHUTDOWN-COMPLETE and AUTH chunks MUST be ignored.
658	 */
659	for (i = 0; !found && i < len; i++) {
660		switch (param->chunks[i]) {
661		    case SCTP_CID_INIT:
662		    case SCTP_CID_INIT_ACK:
663		    case SCTP_CID_SHUTDOWN_COMPLETE:
664		    case SCTP_CID_AUTH:
665			break;
666
667		    default:
668			if (param->chunks[i] == chunk)
669			    found = 1;
670			break;
671		}
672	}
673
674	return found;
675}
676
677/* Check if peer requested that this chunk is authenticated */
678int sctp_auth_send_cid(sctp_cid_t chunk, const struct sctp_association *asoc)
679{
680	struct net  *net;
681	if (!asoc)
682		return 0;
683
684	net = sock_net(asoc->base.sk);
685	if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
686		return 0;
687
688	return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
689}
690
691/* Check if we requested that peer authenticate this chunk. */
692int sctp_auth_recv_cid(sctp_cid_t chunk, const struct sctp_association *asoc)
693{
694	struct net *net;
695	if (!asoc)
696		return 0;
697
698	net = sock_net(asoc->base.sk);
699	if (!net->sctp.auth_enable)
700		return 0;
701
702	return __sctp_auth_cid(chunk,
703			      (struct sctp_chunks_param *)asoc->c.auth_chunks);
704}
705
706/* SCTP-AUTH: Section 6.2:
707 *    The sender MUST calculate the MAC as described in RFC2104 [2] using
708 *    the hash function H as described by the MAC Identifier and the shared
709 *    association key K based on the endpoint pair shared key described by
710 *    the shared key identifier.  The 'data' used for the computation of
711 *    the AUTH-chunk is given by the AUTH chunk with its HMAC field set to
712 *    zero (as shown in Figure 6) followed by all chunks that are placed
713 *    after the AUTH chunk in the SCTP packet.
714 */
715void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
716			      struct sk_buff *skb,
717			      struct sctp_auth_chunk *auth,
718			      gfp_t gfp)
719{
720	struct scatterlist sg;
721	struct hash_desc desc;
722	struct sctp_auth_bytes *asoc_key;
723	__u16 key_id, hmac_id;
724	__u8 *digest;
725	unsigned char *end;
726	int free_key = 0;
727
728	/* Extract the info we need:
729	 * - hmac id
730	 * - key id
731	 */
732	key_id = ntohs(auth->auth_hdr.shkey_id);
733	hmac_id = ntohs(auth->auth_hdr.hmac_id);
734
735	if (key_id == asoc->active_key_id)
736		asoc_key = asoc->asoc_shared_key;
737	else {
738		struct sctp_shared_key *ep_key;
739
740		ep_key = sctp_auth_get_shkey(asoc, key_id);
741		if (!ep_key)
742			return;
743
744		asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
745		if (!asoc_key)
746			return;
747
748		free_key = 1;
749	}
750
751	/* set up scatter list */
752	end = skb_tail_pointer(skb);
753	sg_init_one(&sg, auth, end - (unsigned char *)auth);
754
755	desc.tfm = asoc->ep->auth_hmacs[hmac_id];
756	desc.flags = 0;
757
758	digest = auth->auth_hdr.hmac;
759	if (crypto_hash_setkey(desc.tfm, &asoc_key->data[0], asoc_key->len))
760		goto free;
761
762	crypto_hash_digest(&desc, &sg, sg.length, digest);
763
764free:
765	if (free_key)
766		sctp_auth_key_put(asoc_key);
767}
768
769/* API Helpers */
770
771/* Add a chunk to the endpoint authenticated chunk list */
772int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
773{
774	struct sctp_chunks_param *p = ep->auth_chunk_list;
775	__u16 nchunks;
776	__u16 param_len;
777
778	/* If this chunk is already specified, we are done */
779	if (__sctp_auth_cid(chunk_id, p))
780		return 0;
781
782	/* Check if we can add this chunk to the array */
783	param_len = ntohs(p->param_hdr.length);
784	nchunks = param_len - sizeof(sctp_paramhdr_t);
785	if (nchunks == SCTP_NUM_CHUNK_TYPES)
786		return -EINVAL;
787
788	p->chunks[nchunks] = chunk_id;
789	p->param_hdr.length = htons(param_len + 1);
790	return 0;
791}
792
793/* Add hmac identifires to the endpoint list of supported hmac ids */
794int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
795			   struct sctp_hmacalgo *hmacs)
796{
797	int has_sha1 = 0;
798	__u16 id;
799	int i;
800
801	/* Scan the list looking for unsupported id.  Also make sure that
802	 * SHA1 is specified.
803	 */
804	for (i = 0; i < hmacs->shmac_num_idents; i++) {
805		id = hmacs->shmac_idents[i];
806
807		if (id > SCTP_AUTH_HMAC_ID_MAX)
808			return -EOPNOTSUPP;
809
810		if (SCTP_AUTH_HMAC_ID_SHA1 == id)
811			has_sha1 = 1;
812
813		if (!sctp_hmac_list[id].hmac_name)
814			return -EOPNOTSUPP;
815	}
816
817	if (!has_sha1)
818		return -EINVAL;
819
820	memcpy(ep->auth_hmacs_list->hmac_ids, &hmacs->shmac_idents[0],
821		hmacs->shmac_num_idents * sizeof(__u16));
822	ep->auth_hmacs_list->param_hdr.length = htons(sizeof(sctp_paramhdr_t) +
823				hmacs->shmac_num_idents * sizeof(__u16));
824	return 0;
825}
826
827/* Set a new shared key on either endpoint or association.  If the
828 * the key with a same ID already exists, replace the key (remove the
829 * old key and add a new one).
830 */
831int sctp_auth_set_key(struct sctp_endpoint *ep,
832		      struct sctp_association *asoc,
833		      struct sctp_authkey *auth_key)
834{
835	struct sctp_shared_key *cur_key = NULL;
836	struct sctp_auth_bytes *key;
837	struct list_head *sh_keys;
838	int replace = 0;
839
840	/* Try to find the given key id to see if
841	 * we are doing a replace, or adding a new key
842	 */
843	if (asoc)
844		sh_keys = &asoc->endpoint_shared_keys;
845	else
846		sh_keys = &ep->endpoint_shared_keys;
847
848	key_for_each(cur_key, sh_keys) {
849		if (cur_key->key_id == auth_key->sca_keynumber) {
850			replace = 1;
851			break;
852		}
853	}
854
855	/* If we are not replacing a key id, we need to allocate
856	 * a shared key.
857	 */
858	if (!replace) {
859		cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber,
860						 GFP_KERNEL);
861		if (!cur_key)
862			return -ENOMEM;
863	}
864
865	/* Create a new key data based on the info passed in */
866	key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
867	if (!key)
868		goto nomem;
869
870	memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
871
872	/* If we are replacing, remove the old keys data from the
873	 * key id.  If we are adding new key id, add it to the
874	 * list.
875	 */
876	if (replace)
877		sctp_auth_key_put(cur_key->key);
878	else
879		list_add(&cur_key->key_list, sh_keys);
880
881	cur_key->key = key;
882	sctp_auth_key_hold(key);
883
884	return 0;
885nomem:
886	if (!replace)
887		sctp_auth_shkey_free(cur_key);
888
889	return -ENOMEM;
890}
891
892int sctp_auth_set_active_key(struct sctp_endpoint *ep,
893			     struct sctp_association *asoc,
894			     __u16  key_id)
895{
896	struct sctp_shared_key *key;
897	struct list_head *sh_keys;
898	int found = 0;
899
900	/* The key identifier MUST correst to an existing key */
901	if (asoc)
902		sh_keys = &asoc->endpoint_shared_keys;
903	else
904		sh_keys = &ep->endpoint_shared_keys;
905
906	key_for_each(key, sh_keys) {
907		if (key->key_id == key_id) {
908			found = 1;
909			break;
910		}
911	}
912
913	if (!found)
914		return -EINVAL;
915
916	if (asoc) {
917		asoc->active_key_id = key_id;
918		sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
919	} else
920		ep->active_key_id = key_id;
921
922	return 0;
923}
924
925int sctp_auth_del_key_id(struct sctp_endpoint *ep,
926			 struct sctp_association *asoc,
927			 __u16  key_id)
928{
929	struct sctp_shared_key *key;
930	struct list_head *sh_keys;
931	int found = 0;
932
933	/* The key identifier MUST NOT be the current active key
934	 * The key identifier MUST correst to an existing key
935	 */
936	if (asoc) {
937		if (asoc->active_key_id == key_id)
938			return -EINVAL;
939
940		sh_keys = &asoc->endpoint_shared_keys;
941	} else {
942		if (ep->active_key_id == key_id)
943			return -EINVAL;
944
945		sh_keys = &ep->endpoint_shared_keys;
946	}
947
948	key_for_each(key, sh_keys) {
949		if (key->key_id == key_id) {
950			found = 1;
951			break;
952		}
953	}
954
955	if (!found)
956		return -EINVAL;
957
958	/* Delete the shared key */
959	list_del_init(&key->key_list);
960	sctp_auth_shkey_free(key);
961
962	return 0;
963}
964