xref: /freebsd/sys/netinet/sctp_auth.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <netinet/sctp_os.h>
35 #include <netinet/sctp.h>
36 #include <netinet/sctp_header.h>
37 #include <netinet/sctp_pcb.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp_indata.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_auth.h>
44 
45 #ifdef SCTP_DEBUG
46 #define SCTP_AUTH_DEBUG		(sctp_debug_on & SCTP_DEBUG_AUTH1)
47 #define SCTP_AUTH_DEBUG2	(sctp_debug_on & SCTP_DEBUG_AUTH2)
48 #endif				/* SCTP_DEBUG */
49 
50 
51 void
52 sctp_clear_chunklist(sctp_auth_chklist_t * chklist)
53 {
54 	bzero(chklist, sizeof(*chklist));
55 	/* chklist->num_chunks = 0; */
56 }
57 
58 sctp_auth_chklist_t *
59 sctp_alloc_chunklist(void)
60 {
61 	sctp_auth_chklist_t *chklist;
62 
63 	SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
64 	    SCTP_M_AUTH_CL);
65 	if (chklist == NULL) {
66 		SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
67 	} else {
68 		sctp_clear_chunklist(chklist);
69 	}
70 	return (chklist);
71 }
72 
73 void
74 sctp_free_chunklist(sctp_auth_chklist_t * list)
75 {
76 	if (list != NULL)
77 		SCTP_FREE(list, SCTP_M_AUTH_CL);
78 }
79 
80 sctp_auth_chklist_t *
81 sctp_copy_chunklist(sctp_auth_chklist_t * list)
82 {
83 	sctp_auth_chklist_t *new_list;
84 
85 	if (list == NULL)
86 		return (NULL);
87 
88 	/* get a new list */
89 	new_list = sctp_alloc_chunklist();
90 	if (new_list == NULL)
91 		return (NULL);
92 	/* copy it */
93 	bcopy(list, new_list, sizeof(*new_list));
94 
95 	return (new_list);
96 }
97 
98 
99 /*
100  * add a chunk to the required chunks list
101  */
102 int
103 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
104 {
105 	if (list == NULL)
106 		return (-1);
107 
108 	/* is chunk restricted? */
109 	if ((chunk == SCTP_INITIATION) ||
110 	    (chunk == SCTP_INITIATION_ACK) ||
111 	    (chunk == SCTP_SHUTDOWN_COMPLETE) ||
112 	    (chunk == SCTP_AUTHENTICATION)) {
113 		return (-1);
114 	}
115 	if (list->chunks[chunk] == 0) {
116 		list->chunks[chunk] = 1;
117 		list->num_chunks++;
118 		SCTPDBG(SCTP_DEBUG_AUTH1,
119 		    "SCTP: added chunk %u (0x%02x) to Auth list\n",
120 		    chunk, chunk);
121 	}
122 	return (0);
123 }
124 
125 /*
126  * delete a chunk from the required chunks list
127  */
128 int
129 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
130 {
131 	if (list == NULL)
132 		return (-1);
133 
134 	/* is chunk restricted? */
135 	if ((chunk == SCTP_ASCONF) ||
136 	    (chunk == SCTP_ASCONF_ACK)) {
137 		return (-1);
138 	}
139 	if (list->chunks[chunk] == 1) {
140 		list->chunks[chunk] = 0;
141 		list->num_chunks--;
142 		SCTPDBG(SCTP_DEBUG_AUTH1,
143 		    "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
144 		    chunk, chunk);
145 	}
146 	return (0);
147 }
148 
149 size_t
150 sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
151 {
152 	if (list == NULL)
153 		return (0);
154 	else
155 		return (list->num_chunks);
156 }
157 
158 /*
159  * set the default list of chunks requiring AUTH
160  */
161 void
162 sctp_auth_set_default_chunks(sctp_auth_chklist_t * list)
163 {
164 	(void)sctp_auth_add_chunk(SCTP_ASCONF, list);
165 	(void)sctp_auth_add_chunk(SCTP_ASCONF_ACK, list);
166 }
167 
168 /*
169  * return the current number and list of required chunks caller must
170  * guarantee ptr has space for up to 256 bytes
171  */
172 int
173 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
174 {
175 	int i, count = 0;
176 
177 	if (list == NULL)
178 		return (0);
179 
180 	for (i = 0; i < 256; i++) {
181 		if (list->chunks[i] != 0) {
182 			*ptr++ = i;
183 			count++;
184 		}
185 	}
186 	return (count);
187 }
188 
189 int
190 sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
191 {
192 	int i, size = 0;
193 
194 	if (list == NULL)
195 		return (0);
196 
197 	if (list->num_chunks <= 32) {
198 		/* just list them, one byte each */
199 		for (i = 0; i < 256; i++) {
200 			if (list->chunks[i] != 0) {
201 				*ptr++ = i;
202 				size++;
203 			}
204 		}
205 	} else {
206 		int index, offset;
207 
208 		/* pack into a 32 byte bitfield */
209 		for (i = 0; i < 256; i++) {
210 			if (list->chunks[i] != 0) {
211 				index = i / 8;
212 				offset = i % 8;
213 				ptr[index] |= (1 << offset);
214 			}
215 		}
216 		size = 32;
217 	}
218 	return (size);
219 }
220 
221 int
222 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
223     sctp_auth_chklist_t * list)
224 {
225 	int i;
226 	int size;
227 
228 	if (list == NULL)
229 		return (0);
230 
231 	if (num_chunks <= 32) {
232 		/* just pull them, one byte each */
233 		for (i = 0; i < num_chunks; i++) {
234 			(void)sctp_auth_add_chunk(*ptr++, list);
235 		}
236 		size = num_chunks;
237 	} else {
238 		int index, offset;
239 
240 		/* unpack from a 32 byte bitfield */
241 		for (index = 0; index < 32; index++) {
242 			for (offset = 0; offset < 8; offset++) {
243 				if (ptr[index] & (1 << offset)) {
244 					(void)sctp_auth_add_chunk((index * 8) + offset, list);
245 				}
246 			}
247 		}
248 		size = 32;
249 	}
250 	return (size);
251 }
252 
253 
254 /*
255  * allocate structure space for a key of length keylen
256  */
257 sctp_key_t *
258 sctp_alloc_key(uint32_t keylen)
259 {
260 	sctp_key_t *new_key;
261 
262 	SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
263 	    SCTP_M_AUTH_KY);
264 	if (new_key == NULL) {
265 		/* out of memory */
266 		return (NULL);
267 	}
268 	new_key->keylen = keylen;
269 	return (new_key);
270 }
271 
272 void
273 sctp_free_key(sctp_key_t * key)
274 {
275 	if (key != NULL)
276 		SCTP_FREE(key, SCTP_M_AUTH_KY);
277 }
278 
279 void
280 sctp_print_key(sctp_key_t * key, const char *str)
281 {
282 	uint32_t i;
283 
284 	if (key == NULL) {
285 		printf("%s: [Null key]\n", str);
286 		return;
287 	}
288 	printf("%s: len %u, ", str, key->keylen);
289 	if (key->keylen) {
290 		for (i = 0; i < key->keylen; i++)
291 			printf("%02x", key->key[i]);
292 		printf("\n");
293 	} else {
294 		printf("[Null key]\n");
295 	}
296 }
297 
298 void
299 sctp_show_key(sctp_key_t * key, const char *str)
300 {
301 	uint32_t i;
302 
303 	if (key == NULL) {
304 		printf("%s: [Null key]\n", str);
305 		return;
306 	}
307 	printf("%s: len %u, ", str, key->keylen);
308 	if (key->keylen) {
309 		for (i = 0; i < key->keylen; i++)
310 			printf("%02x", key->key[i]);
311 		printf("\n");
312 	} else {
313 		printf("[Null key]\n");
314 	}
315 }
316 
317 static uint32_t
318 sctp_get_keylen(sctp_key_t * key)
319 {
320 	if (key != NULL)
321 		return (key->keylen);
322 	else
323 		return (0);
324 }
325 
326 /*
327  * generate a new random key of length 'keylen'
328  */
329 sctp_key_t *
330 sctp_generate_random_key(uint32_t keylen)
331 {
332 	sctp_key_t *new_key;
333 
334 	/* validate keylen */
335 	if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX)
336 		keylen = SCTP_AUTH_RANDOM_SIZE_MAX;
337 
338 	new_key = sctp_alloc_key(keylen);
339 	if (new_key == NULL) {
340 		/* out of memory */
341 		return (NULL);
342 	}
343 	SCTP_READ_RANDOM(new_key->key, keylen);
344 	new_key->keylen = keylen;
345 	return (new_key);
346 }
347 
348 sctp_key_t *
349 sctp_set_key(uint8_t * key, uint32_t keylen)
350 {
351 	sctp_key_t *new_key;
352 
353 	new_key = sctp_alloc_key(keylen);
354 	if (new_key == NULL) {
355 		/* out of memory */
356 		return (NULL);
357 	}
358 	bcopy(key, new_key->key, keylen);
359 	return (new_key);
360 }
361 
362 /*
363  * given two keys of variable size, compute which key is "larger/smaller"
364  * returns: 1 if key1 > key2 -1 if key1 < key2 0 if key1 = key2
365  */
366 static int
367 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
368 {
369 	uint32_t maxlen;
370 	uint32_t i;
371 	uint32_t key1len, key2len;
372 	uint8_t *key_1, *key_2;
373 	uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
374 
375 	/* sanity/length check */
376 	key1len = sctp_get_keylen(key1);
377 	key2len = sctp_get_keylen(key2);
378 	if ((key1len == 0) && (key2len == 0))
379 		return (0);
380 	else if (key1len == 0)
381 		return (-1);
382 	else if (key2len == 0)
383 		return (1);
384 
385 	if (key1len != key2len) {
386 		if (key1len >= key2len)
387 			maxlen = key1len;
388 		else
389 			maxlen = key2len;
390 		bzero(temp, maxlen);
391 		if (key1len < maxlen) {
392 			/* prepend zeroes to key1 */
393 			bcopy(key1->key, temp + (maxlen - key1len), key1len);
394 			key_1 = temp;
395 			key_2 = key2->key;
396 		} else {
397 			/* prepend zeroes to key2 */
398 			bcopy(key2->key, temp + (maxlen - key2len), key2len);
399 			key_1 = key1->key;
400 			key_2 = temp;
401 		}
402 	} else {
403 		maxlen = key1len;
404 		key_1 = key1->key;
405 		key_2 = key2->key;
406 	}
407 
408 	for (i = 0; i < maxlen; i++) {
409 		if (*key_1 > *key_2)
410 			return (1);
411 		else if (*key_1 < *key_2)
412 			return (-1);
413 		key_1++;
414 		key_2++;
415 	}
416 
417 	/* keys are equal value, so check lengths */
418 	if (key1len == key2len)
419 		return (0);
420 	else if (key1len < key2len)
421 		return (-1);
422 	else
423 		return (1);
424 }
425 
426 /*
427  * generate the concatenated keying material based on the two keys and the
428  * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
429  * order for concatenation
430  */
431 sctp_key_t *
432 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
433 {
434 	uint32_t keylen;
435 	sctp_key_t *new_key;
436 	uint8_t *key_ptr;
437 
438 	keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
439 	    sctp_get_keylen(shared);
440 
441 	if (keylen > 0) {
442 		/* get space for the new key */
443 		new_key = sctp_alloc_key(keylen);
444 		if (new_key == NULL) {
445 			/* out of memory */
446 			return (NULL);
447 		}
448 		new_key->keylen = keylen;
449 		key_ptr = new_key->key;
450 	} else {
451 		/* all keys empty/null?! */
452 		return (NULL);
453 	}
454 
455 	/* concatenate the keys */
456 	if (sctp_compare_key(key1, key2) <= 0) {
457 #ifdef SCTP_AUTH_DRAFT_04
458 		/* key is key1 + shared + key2 */
459 		if (sctp_get_keylen(key1)) {
460 			bcopy(key1->key, key_ptr, key1->keylen);
461 			key_ptr += key1->keylen;
462 		}
463 		if (sctp_get_keylen(shared)) {
464 			bcopy(shared->key, key_ptr, shared->keylen);
465 			key_ptr += shared->keylen;
466 		}
467 		if (sctp_get_keylen(key2)) {
468 			bcopy(key2->key, key_ptr, key2->keylen);
469 			key_ptr += key2->keylen;
470 		}
471 #else
472 		/* key is shared + key1 + key2 */
473 		if (sctp_get_keylen(shared)) {
474 			bcopy(shared->key, key_ptr, shared->keylen);
475 			key_ptr += shared->keylen;
476 		}
477 		if (sctp_get_keylen(key1)) {
478 			bcopy(key1->key, key_ptr, key1->keylen);
479 			key_ptr += key1->keylen;
480 		}
481 		if (sctp_get_keylen(key2)) {
482 			bcopy(key2->key, key_ptr, key2->keylen);
483 			key_ptr += key2->keylen;
484 		}
485 #endif
486 	} else {
487 #ifdef SCTP_AUTH_DRAFT_04
488 		/* key is key2 + shared + key1 */
489 		if (sctp_get_keylen(key2)) {
490 			bcopy(key2->key, key_ptr, key2->keylen);
491 			key_ptr += key2->keylen;
492 		}
493 		if (sctp_get_keylen(shared)) {
494 			bcopy(shared->key, key_ptr, shared->keylen);
495 			key_ptr += shared->keylen;
496 		}
497 		if (sctp_get_keylen(key1)) {
498 			bcopy(key1->key, key_ptr, key1->keylen);
499 			key_ptr += key1->keylen;
500 		}
501 #else
502 		/* key is shared + key2 + key1 */
503 		if (sctp_get_keylen(shared)) {
504 			bcopy(shared->key, key_ptr, shared->keylen);
505 			key_ptr += shared->keylen;
506 		}
507 		if (sctp_get_keylen(key2)) {
508 			bcopy(key2->key, key_ptr, key2->keylen);
509 			key_ptr += key2->keylen;
510 		}
511 		if (sctp_get_keylen(key1)) {
512 			bcopy(key1->key, key_ptr, key1->keylen);
513 			key_ptr += key1->keylen;
514 		}
515 #endif
516 	}
517 	return (new_key);
518 }
519 
520 
521 sctp_sharedkey_t *
522 sctp_alloc_sharedkey(void)
523 {
524 	sctp_sharedkey_t *new_key;
525 
526 	SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
527 	    SCTP_M_AUTH_KY);
528 	if (new_key == NULL) {
529 		/* out of memory */
530 		return (NULL);
531 	}
532 	new_key->keyid = 0;
533 	new_key->key = NULL;
534 	return (new_key);
535 }
536 
537 void
538 sctp_free_sharedkey(sctp_sharedkey_t * skey)
539 {
540 	if (skey != NULL) {
541 		if (skey->key != NULL)
542 			sctp_free_key(skey->key);
543 		SCTP_FREE(skey, SCTP_M_AUTH_KY);
544 	}
545 }
546 
547 sctp_sharedkey_t *
548 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
549 {
550 	sctp_sharedkey_t *skey;
551 
552 	LIST_FOREACH(skey, shared_keys, next) {
553 		if (skey->keyid == key_id)
554 			return (skey);
555 	}
556 	return (NULL);
557 }
558 
559 void
560 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
561     sctp_sharedkey_t * new_skey)
562 {
563 	sctp_sharedkey_t *skey;
564 
565 	if ((shared_keys == NULL) || (new_skey == NULL))
566 		return;
567 
568 	/* insert into an empty list? */
569 	if (SCTP_LIST_EMPTY(shared_keys)) {
570 		LIST_INSERT_HEAD(shared_keys, new_skey, next);
571 		return;
572 	}
573 	/* insert into the existing list, ordered by key id */
574 	LIST_FOREACH(skey, shared_keys, next) {
575 		if (new_skey->keyid < skey->keyid) {
576 			/* insert it before here */
577 			LIST_INSERT_BEFORE(skey, new_skey, next);
578 			return;
579 		} else if (new_skey->keyid == skey->keyid) {
580 			/* replace the existing key */
581 			SCTPDBG(SCTP_DEBUG_AUTH1,
582 			    "replacing shared key id %u\n",
583 			    new_skey->keyid);
584 			LIST_INSERT_BEFORE(skey, new_skey, next);
585 			LIST_REMOVE(skey, next);
586 			sctp_free_sharedkey(skey);
587 			return;
588 		}
589 		if (LIST_NEXT(skey, next) == NULL) {
590 			/* belongs at the end of the list */
591 			LIST_INSERT_AFTER(skey, new_skey, next);
592 			return;
593 		}
594 	}
595 }
596 
597 static sctp_sharedkey_t *
598 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
599 {
600 	sctp_sharedkey_t *new_skey;
601 
602 	if (skey == NULL)
603 		return (NULL);
604 	new_skey = sctp_alloc_sharedkey();
605 	if (new_skey == NULL)
606 		return (NULL);
607 	if (skey->key != NULL)
608 		new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
609 	else
610 		new_skey->key = NULL;
611 	new_skey->keyid = skey->keyid;
612 	return (new_skey);
613 }
614 
615 int
616 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
617 {
618 	sctp_sharedkey_t *skey, *new_skey;
619 	int count = 0;
620 
621 	if ((src == NULL) || (dest == NULL))
622 		return (0);
623 	LIST_FOREACH(skey, src, next) {
624 		new_skey = sctp_copy_sharedkey(skey);
625 		if (new_skey != NULL) {
626 			sctp_insert_sharedkey(dest, new_skey);
627 			count++;
628 		}
629 	}
630 	return (count);
631 }
632 
633 
634 sctp_hmaclist_t *
635 sctp_alloc_hmaclist(uint8_t num_hmacs)
636 {
637 	sctp_hmaclist_t *new_list;
638 	int alloc_size;
639 
640 	alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
641 	SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
642 	    SCTP_M_AUTH_HL);
643 	if (new_list == NULL) {
644 		/* out of memory */
645 		return (NULL);
646 	}
647 	new_list->max_algo = num_hmacs;
648 	new_list->num_algo = 0;
649 	return (new_list);
650 }
651 
652 void
653 sctp_free_hmaclist(sctp_hmaclist_t * list)
654 {
655 	if (list != NULL) {
656 		SCTP_FREE(list, SCTP_M_AUTH_HL);
657 		list = NULL;
658 	}
659 }
660 
661 int
662 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
663 {
664 	int i;
665 
666 	if (list == NULL)
667 		return (-1);
668 	if (list->num_algo == list->max_algo) {
669 		SCTPDBG(SCTP_DEBUG_AUTH1,
670 		    "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
671 		return (-1);
672 	}
673 	if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
674 #ifdef HAVE_SHA224
675 	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
676 #endif
677 #ifdef HAVE_SHA2
678 	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
679 	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
680 	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
681 #endif
682 	    (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
683 		return (-1);
684 	}
685 	/* Now is it already in the list */
686 	for (i = 0; i < list->num_algo; i++) {
687 		if (list->hmac[i] == hmac_id) {
688 			/* already in list */
689 			return (-1);
690 		}
691 	}
692 	SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
693 	list->hmac[list->num_algo++] = hmac_id;
694 	return (0);
695 }
696 
697 sctp_hmaclist_t *
698 sctp_copy_hmaclist(sctp_hmaclist_t * list)
699 {
700 	sctp_hmaclist_t *new_list;
701 	int i;
702 
703 	if (list == NULL)
704 		return (NULL);
705 	/* get a new list */
706 	new_list = sctp_alloc_hmaclist(list->max_algo);
707 	if (new_list == NULL)
708 		return (NULL);
709 	/* copy it */
710 	new_list->max_algo = list->max_algo;
711 	new_list->num_algo = list->num_algo;
712 	for (i = 0; i < list->num_algo; i++)
713 		new_list->hmac[i] = list->hmac[i];
714 	return (new_list);
715 }
716 
717 sctp_hmaclist_t *
718 sctp_default_supported_hmaclist(void)
719 {
720 	sctp_hmaclist_t *new_list;
721 
722 	new_list = sctp_alloc_hmaclist(2);
723 	if (new_list == NULL)
724 		return (NULL);
725 	(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
726 	(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
727 	return (new_list);
728 }
729 
730 /*
731  * HMAC algos are listed in priority/preference order find the best HMAC id
732  * to use for the peer based on local support
733  */
734 uint16_t
735 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
736 {
737 	int i, j;
738 
739 	if ((local == NULL) || (peer == NULL))
740 		return (SCTP_AUTH_HMAC_ID_RSVD);
741 
742 	for (i = 0; i < peer->num_algo; i++) {
743 		for (j = 0; j < local->num_algo; j++) {
744 			if (peer->hmac[i] == local->hmac[j]) {
745 #ifndef SCTP_AUTH_DRAFT_04
746 				/* "skip" MD5 as it's been deprecated */
747 				if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
748 					continue;
749 #endif
750 
751 				/* found the "best" one */
752 				SCTPDBG(SCTP_DEBUG_AUTH1,
753 				    "SCTP: negotiated peer HMAC id %u\n",
754 				    peer->hmac[i]);
755 				return (peer->hmac[i]);
756 			}
757 		}
758 	}
759 	/* didn't find one! */
760 	return (SCTP_AUTH_HMAC_ID_RSVD);
761 }
762 
763 /*
764  * serialize the HMAC algo list and return space used caller must guarantee
765  * ptr has appropriate space
766  */
767 int
768 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
769 {
770 	int i;
771 	uint16_t hmac_id;
772 
773 	if (list == NULL)
774 		return (0);
775 
776 	for (i = 0; i < list->num_algo; i++) {
777 		hmac_id = htons(list->hmac[i]);
778 		bcopy(&hmac_id, ptr, sizeof(hmac_id));
779 		ptr += sizeof(hmac_id);
780 	}
781 	return (list->num_algo * sizeof(hmac_id));
782 }
783 
784 int
785 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
786 {
787 	uint32_t i;
788 	uint16_t hmac_id;
789 	uint32_t sha1_supported = 0;
790 
791 	for (i = 0; i < num_hmacs; i++) {
792 		hmac_id = ntohs(hmacs->hmac_ids[i]);
793 		if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
794 			sha1_supported = 1;
795 	}
796 	/* all HMAC id's are supported */
797 	if (sha1_supported == 0)
798 		return (-1);
799 	else
800 		return (0);
801 }
802 
803 sctp_authinfo_t *
804 sctp_alloc_authinfo(void)
805 {
806 	sctp_authinfo_t *new_authinfo;
807 
808 	SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
809 	    SCTP_M_AUTH_IF);
810 
811 	if (new_authinfo == NULL) {
812 		/* out of memory */
813 		return (NULL);
814 	}
815 	bzero(new_authinfo, sizeof(*new_authinfo));
816 	return (new_authinfo);
817 }
818 
819 void
820 sctp_free_authinfo(sctp_authinfo_t * authinfo)
821 {
822 	if (authinfo == NULL)
823 		return;
824 
825 	if (authinfo->random != NULL)
826 		sctp_free_key(authinfo->random);
827 	if (authinfo->peer_random != NULL)
828 		sctp_free_key(authinfo->peer_random);
829 	if (authinfo->assoc_key != NULL)
830 		sctp_free_key(authinfo->assoc_key);
831 	if (authinfo->recv_key != NULL)
832 		sctp_free_key(authinfo->recv_key);
833 
834 	/* We are NOT dynamically allocating authinfo's right now... */
835 	/* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
836 }
837 
838 
839 uint32_t
840 sctp_get_auth_chunk_len(uint16_t hmac_algo)
841 {
842 	int size;
843 
844 	size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
845 	return (SCTP_SIZE32(size));
846 }
847 
848 uint32_t
849 sctp_get_hmac_digest_len(uint16_t hmac_algo)
850 {
851 	switch (hmac_algo) {
852 	case SCTP_AUTH_HMAC_ID_SHA1:
853 		return (SCTP_AUTH_DIGEST_LEN_SHA1);
854 	case SCTP_AUTH_HMAC_ID_MD5:
855 		return (SCTP_AUTH_DIGEST_LEN_MD5);
856 #ifdef HAVE_SHA224
857 	case SCTP_AUTH_HMAC_ID_SHA224:
858 		return (SCTP_AUTH_DIGEST_LEN_SHA224);
859 #endif
860 #ifdef HAVE_SHA2
861 	case SCTP_AUTH_HMAC_ID_SHA256:
862 		return (SCTP_AUTH_DIGEST_LEN_SHA256);
863 	case SCTP_AUTH_HMAC_ID_SHA384:
864 		return (SCTP_AUTH_DIGEST_LEN_SHA384);
865 	case SCTP_AUTH_HMAC_ID_SHA512:
866 		return (SCTP_AUTH_DIGEST_LEN_SHA512);
867 #endif
868 	default:
869 		/* unknown HMAC algorithm: can't do anything */
870 		return (0);
871 	}			/* end switch */
872 }
873 
874 static inline int
875 sctp_get_hmac_block_len(uint16_t hmac_algo)
876 {
877 	switch (hmac_algo) {
878 		case SCTP_AUTH_HMAC_ID_SHA1:
879 		case SCTP_AUTH_HMAC_ID_MD5:
880 #ifdef HAVE_SHA224
881 		case SCTP_AUTH_HMAC_ID_SHA224:
882 #endif
883 		return (64);
884 #ifdef HAVE_SHA2
885 	case SCTP_AUTH_HMAC_ID_SHA256:
886 		return (64);
887 	case SCTP_AUTH_HMAC_ID_SHA384:
888 	case SCTP_AUTH_HMAC_ID_SHA512:
889 		return (128);
890 #endif
891 	case SCTP_AUTH_HMAC_ID_RSVD:
892 	default:
893 		/* unknown HMAC algorithm: can't do anything */
894 		return (0);
895 	}			/* end switch */
896 }
897 
898 static void
899 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
900 {
901 	switch (hmac_algo) {
902 		case SCTP_AUTH_HMAC_ID_SHA1:
903 		SHA1_Init(&ctx->sha1);
904 		break;
905 	case SCTP_AUTH_HMAC_ID_MD5:
906 		MD5_Init(&ctx->md5);
907 		break;
908 #ifdef HAVE_SHA224
909 	case SCTP_AUTH_HMAC_ID_SHA224:
910 		break;
911 #endif
912 #ifdef HAVE_SHA2
913 	case SCTP_AUTH_HMAC_ID_SHA256:
914 		SHA256_Init(&ctx->sha256);
915 		break;
916 	case SCTP_AUTH_HMAC_ID_SHA384:
917 		SHA384_Init(&ctx->sha384);
918 		break;
919 	case SCTP_AUTH_HMAC_ID_SHA512:
920 		SHA512_Init(&ctx->sha512);
921 		break;
922 #endif
923 	case SCTP_AUTH_HMAC_ID_RSVD:
924 	default:
925 		/* unknown HMAC algorithm: can't do anything */
926 		return;
927 	}			/* end switch */
928 }
929 
930 static void
931 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
932     uint8_t * text, uint32_t textlen)
933 {
934 	switch (hmac_algo) {
935 		case SCTP_AUTH_HMAC_ID_SHA1:
936 		SHA1_Update(&ctx->sha1, text, textlen);
937 		break;
938 	case SCTP_AUTH_HMAC_ID_MD5:
939 		MD5_Update(&ctx->md5, text, textlen);
940 		break;
941 #ifdef HAVE_SHA224
942 	case SCTP_AUTH_HMAC_ID_SHA224:
943 		break;
944 #endif
945 #ifdef HAVE_SHA2
946 	case SCTP_AUTH_HMAC_ID_SHA256:
947 		SHA256_Update(&ctx->sha256, text, textlen);
948 		break;
949 	case SCTP_AUTH_HMAC_ID_SHA384:
950 		SHA384_Update(&ctx->sha384, text, textlen);
951 		break;
952 	case SCTP_AUTH_HMAC_ID_SHA512:
953 		SHA512_Update(&ctx->sha512, text, textlen);
954 		break;
955 #endif
956 	case SCTP_AUTH_HMAC_ID_RSVD:
957 	default:
958 		/* unknown HMAC algorithm: can't do anything */
959 		return;
960 	}			/* end switch */
961 }
962 
963 static void
964 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
965     uint8_t * digest)
966 {
967 	switch (hmac_algo) {
968 		case SCTP_AUTH_HMAC_ID_SHA1:
969 		SHA1_Final(digest, &ctx->sha1);
970 		break;
971 	case SCTP_AUTH_HMAC_ID_MD5:
972 		MD5_Final(digest, &ctx->md5);
973 		break;
974 #ifdef HAVE_SHA224
975 	case SCTP_AUTH_HMAC_ID_SHA224:
976 		break;
977 #endif
978 #ifdef HAVE_SHA2
979 	case SCTP_AUTH_HMAC_ID_SHA256:
980 		SHA256_Final(digest, &ctx->sha256);
981 		break;
982 	case SCTP_AUTH_HMAC_ID_SHA384:
983 		/* SHA384 is truncated SHA512 */
984 		SHA384_Final(digest, &ctx->sha384);
985 		break;
986 	case SCTP_AUTH_HMAC_ID_SHA512:
987 		SHA512_Final(digest, &ctx->sha512);
988 		break;
989 #endif
990 	case SCTP_AUTH_HMAC_ID_RSVD:
991 	default:
992 		/* unknown HMAC algorithm: can't do anything */
993 		return;
994 	}			/* end switch */
995 }
996 
997 /*
998  * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
999  *
1000  * Compute the HMAC digest using the desired hash key, text, and HMAC
1001  * algorithm.  Resulting digest is placed in 'digest' and digest length
1002  * is returned, if the HMAC was performed.
1003  *
1004  * WARNING: it is up to the caller to supply sufficient space to hold the
1005  * resultant digest.
1006  */
1007 uint32_t
1008 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1009     uint8_t * text, uint32_t textlen, uint8_t * digest)
1010 {
1011 	uint32_t digestlen;
1012 	uint32_t blocklen;
1013 	sctp_hash_context_t ctx;
1014 	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
1015 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1016 	uint32_t i;
1017 
1018 	/* sanity check the material and length */
1019 	if ((key == NULL) || (keylen == 0) || (text == NULL) ||
1020 	    (textlen == 0) || (digest == NULL)) {
1021 		/* can't do HMAC with empty key or text or digest store */
1022 		return (0);
1023 	}
1024 	/* validate the hmac algo and get the digest length */
1025 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1026 	if (digestlen == 0)
1027 		return (0);
1028 
1029 	/* hash the key if it is longer than the hash block size */
1030 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1031 	if (keylen > blocklen) {
1032 		sctp_hmac_init(hmac_algo, &ctx);
1033 		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1034 		sctp_hmac_final(hmac_algo, &ctx, temp);
1035 		/* set the hashed key as the key */
1036 		keylen = digestlen;
1037 		key = temp;
1038 	}
1039 	/* initialize the inner/outer pads with the key and "append" zeroes */
1040 	bzero(ipad, blocklen);
1041 	bzero(opad, blocklen);
1042 	bcopy(key, ipad, keylen);
1043 	bcopy(key, opad, keylen);
1044 
1045 	/* XOR the key with ipad and opad values */
1046 	for (i = 0; i < blocklen; i++) {
1047 		ipad[i] ^= 0x36;
1048 		opad[i] ^= 0x5c;
1049 	}
1050 
1051 	/* perform inner hash */
1052 	sctp_hmac_init(hmac_algo, &ctx);
1053 	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1054 	sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1055 	sctp_hmac_final(hmac_algo, &ctx, temp);
1056 
1057 	/* perform outer hash */
1058 	sctp_hmac_init(hmac_algo, &ctx);
1059 	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1060 	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1061 	sctp_hmac_final(hmac_algo, &ctx, digest);
1062 
1063 	return (digestlen);
1064 }
1065 
1066 /* mbuf version */
1067 uint32_t
1068 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1069     struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
1070 {
1071 	uint32_t digestlen;
1072 	uint32_t blocklen;
1073 	sctp_hash_context_t ctx;
1074 	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
1075 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1076 	uint32_t i;
1077 	struct mbuf *m_tmp;
1078 
1079 	/* sanity check the material and length */
1080 	if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1081 		/* can't do HMAC with empty key or text or digest store */
1082 		return (0);
1083 	}
1084 	/* validate the hmac algo and get the digest length */
1085 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1086 	if (digestlen == 0)
1087 		return (0);
1088 
1089 	/* hash the key if it is longer than the hash block size */
1090 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1091 	if (keylen > blocklen) {
1092 		sctp_hmac_init(hmac_algo, &ctx);
1093 		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1094 		sctp_hmac_final(hmac_algo, &ctx, temp);
1095 		/* set the hashed key as the key */
1096 		keylen = digestlen;
1097 		key = temp;
1098 	}
1099 	/* initialize the inner/outer pads with the key and "append" zeroes */
1100 	bzero(ipad, blocklen);
1101 	bzero(opad, blocklen);
1102 	bcopy(key, ipad, keylen);
1103 	bcopy(key, opad, keylen);
1104 
1105 	/* XOR the key with ipad and opad values */
1106 	for (i = 0; i < blocklen; i++) {
1107 		ipad[i] ^= 0x36;
1108 		opad[i] ^= 0x5c;
1109 	}
1110 
1111 	/* perform inner hash */
1112 	sctp_hmac_init(hmac_algo, &ctx);
1113 	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1114 	/* find the correct starting mbuf and offset (get start of text) */
1115 	m_tmp = m;
1116 	while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1117 		m_offset -= SCTP_BUF_LEN(m_tmp);
1118 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1119 	}
1120 	/* now use the rest of the mbuf chain for the text */
1121 	while (m_tmp != NULL) {
1122 		if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1123 			sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1124 			    SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1125 		} else {
1126 			sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1127 			    SCTP_BUF_LEN(m_tmp) - m_offset);
1128 		}
1129 
1130 		/* clear the offset since it's only for the first mbuf */
1131 		m_offset = 0;
1132 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1133 	}
1134 	sctp_hmac_final(hmac_algo, &ctx, temp);
1135 
1136 	/* perform outer hash */
1137 	sctp_hmac_init(hmac_algo, &ctx);
1138 	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1139 	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1140 	sctp_hmac_final(hmac_algo, &ctx, digest);
1141 
1142 	return (digestlen);
1143 }
1144 
1145 /*
1146  * verify the HMAC digest using the desired hash key, text, and HMAC
1147  * algorithm. Returns -1 on error, 0 on success.
1148  */
1149 int
1150 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1151     uint8_t * text, uint32_t textlen,
1152     uint8_t * digest, uint32_t digestlen)
1153 {
1154 	uint32_t len;
1155 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1156 
1157 	/* sanity check the material and length */
1158 	if ((key == NULL) || (keylen == 0) ||
1159 	    (text == NULL) || (textlen == 0) || (digest == NULL)) {
1160 		/* can't do HMAC with empty key or text or digest */
1161 		return (-1);
1162 	}
1163 	len = sctp_get_hmac_digest_len(hmac_algo);
1164 	if ((len == 0) || (digestlen != len))
1165 		return (-1);
1166 
1167 	/* compute the expected hash */
1168 	if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1169 		return (-1);
1170 
1171 	if (memcmp(digest, temp, digestlen) != 0)
1172 		return (-1);
1173 	else
1174 		return (0);
1175 }
1176 
1177 
1178 /*
1179  * computes the requested HMAC using a key struct (which may be modified if
1180  * the keylen exceeds the HMAC block len).
1181  */
1182 uint32_t
1183 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1184     uint32_t textlen, uint8_t * digest)
1185 {
1186 	uint32_t digestlen;
1187 	uint32_t blocklen;
1188 	sctp_hash_context_t ctx;
1189 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1190 
1191 	/* sanity check */
1192 	if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1193 	    (digest == NULL)) {
1194 		/* can't do HMAC with empty key or text or digest store */
1195 		return (0);
1196 	}
1197 	/* validate the hmac algo and get the digest length */
1198 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1199 	if (digestlen == 0)
1200 		return (0);
1201 
1202 	/* hash the key if it is longer than the hash block size */
1203 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1204 	if (key->keylen > blocklen) {
1205 		sctp_hmac_init(hmac_algo, &ctx);
1206 		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1207 		sctp_hmac_final(hmac_algo, &ctx, temp);
1208 		/* save the hashed key as the new key */
1209 		key->keylen = digestlen;
1210 		bcopy(temp, key->key, key->keylen);
1211 	}
1212 	return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1213 	    digest));
1214 }
1215 
1216 /* mbuf version */
1217 uint32_t
1218 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1219     uint32_t m_offset, uint8_t * digest)
1220 {
1221 	uint32_t digestlen;
1222 	uint32_t blocklen;
1223 	sctp_hash_context_t ctx;
1224 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1225 
1226 	/* sanity check */
1227 	if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1228 		/* can't do HMAC with empty key or text or digest store */
1229 		return (0);
1230 	}
1231 	/* validate the hmac algo and get the digest length */
1232 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1233 	if (digestlen == 0)
1234 		return (0);
1235 
1236 	/* hash the key if it is longer than the hash block size */
1237 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1238 	if (key->keylen > blocklen) {
1239 		sctp_hmac_init(hmac_algo, &ctx);
1240 		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1241 		sctp_hmac_final(hmac_algo, &ctx, temp);
1242 		/* save the hashed key as the new key */
1243 		key->keylen = digestlen;
1244 		bcopy(temp, key->key, key->keylen);
1245 	}
1246 	return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1247 }
1248 
1249 int
1250 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1251 {
1252 	int i;
1253 
1254 	if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1255 		return (0);
1256 
1257 	for (i = 0; i < list->num_algo; i++)
1258 		if (list->hmac[i] == id)
1259 			return (1);
1260 
1261 	/* not in the list */
1262 	return (0);
1263 }
1264 
1265 
1266 /*
1267  * clear any cached key(s) if they match the given key id on an association
1268  * the cached key(s) will be recomputed and re-cached at next use. ASSUMES
1269  * TCB_LOCK is already held
1270  */
1271 void
1272 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1273 {
1274 	if (stcb == NULL)
1275 		return;
1276 
1277 	if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1278 		sctp_free_key(stcb->asoc.authinfo.assoc_key);
1279 		stcb->asoc.authinfo.assoc_key = NULL;
1280 	}
1281 	if (keyid == stcb->asoc.authinfo.recv_keyid) {
1282 		sctp_free_key(stcb->asoc.authinfo.recv_key);
1283 		stcb->asoc.authinfo.recv_key = NULL;
1284 	}
1285 }
1286 
1287 /*
1288  * clear any cached key(s) if they match the given key id for all assocs on
1289  * an association ASSUMES INP_WLOCK is already held
1290  */
1291 void
1292 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1293 {
1294 	struct sctp_tcb *stcb;
1295 
1296 	if (inp == NULL)
1297 		return;
1298 
1299 	/* clear the cached keys on all assocs on this instance */
1300 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1301 		SCTP_TCB_LOCK(stcb);
1302 		sctp_clear_cachedkeys(stcb, keyid);
1303 		SCTP_TCB_UNLOCK(stcb);
1304 	}
1305 }
1306 
1307 /*
1308  * delete a shared key from an association ASSUMES TCB_LOCK is already held
1309  */
1310 int
1311 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1312 {
1313 	sctp_sharedkey_t *skey;
1314 
1315 	if (stcb == NULL)
1316 		return (-1);
1317 
1318 	/* is the keyid the assoc active sending key */
1319 	if (keyid == stcb->asoc.authinfo.assoc_keyid)
1320 		return (-1);
1321 
1322 	/* does the key exist? */
1323 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1324 	if (skey == NULL)
1325 		return (-1);
1326 
1327 	/* remove it */
1328 	LIST_REMOVE(skey, next);
1329 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1330 
1331 	/* clear any cached keys */
1332 	sctp_clear_cachedkeys(stcb, keyid);
1333 	return (0);
1334 }
1335 
1336 /*
1337  * deletes a shared key from the endpoint ASSUMES INP_WLOCK is already held
1338  */
1339 int
1340 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1341 {
1342 	sctp_sharedkey_t *skey;
1343 	struct sctp_tcb *stcb;
1344 
1345 	if (inp == NULL)
1346 		return (-1);
1347 
1348 	/* is the keyid the active sending key on the endpoint or any assoc */
1349 	if (keyid == inp->sctp_ep.default_keyid)
1350 		return (-1);
1351 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1352 		SCTP_TCB_LOCK(stcb);
1353 		if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1354 			SCTP_TCB_UNLOCK(stcb);
1355 			return (-1);
1356 		}
1357 		SCTP_TCB_UNLOCK(stcb);
1358 	}
1359 
1360 	/* does the key exist? */
1361 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1362 	if (skey == NULL)
1363 		return (-1);
1364 
1365 	/* remove it */
1366 	LIST_REMOVE(skey, next);
1367 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1368 
1369 	/* clear any cached keys */
1370 	sctp_clear_cachedkeys_ep(inp, keyid);
1371 	return (0);
1372 }
1373 
1374 /*
1375  * set the active key on an association ASSUME TCB_LOCK is already held
1376  */
1377 int
1378 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1379 {
1380 	sctp_sharedkey_t *skey = NULL;
1381 	sctp_key_t *key = NULL;
1382 	int using_ep_key = 0;
1383 
1384 	/* find the key on the assoc */
1385 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1386 	if (skey == NULL) {
1387 		/* if not on the assoc, find the key on the endpoint */
1388 		atomic_add_int(&stcb->asoc.refcnt, 1);
1389 		SCTP_TCB_UNLOCK(stcb);
1390 		SCTP_INP_RLOCK(stcb->sctp_ep);
1391 		SCTP_TCB_LOCK(stcb);
1392 		atomic_add_int(&stcb->asoc.refcnt, -1);
1393 		skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1394 		    keyid);
1395 		using_ep_key = 1;
1396 	}
1397 	if (skey == NULL) {
1398 		/* that key doesn't exist */
1399 		if (using_ep_key) {
1400 			SCTP_INP_RUNLOCK(stcb->sctp_ep);
1401 		}
1402 		return (-1);
1403 	}
1404 	/* get the shared key text */
1405 	key = skey->key;
1406 
1407 	/* free any existing cached key */
1408 	if (stcb->asoc.authinfo.assoc_key != NULL)
1409 		sctp_free_key(stcb->asoc.authinfo.assoc_key);
1410 	/* compute a new assoc key and cache it */
1411 	stcb->asoc.authinfo.assoc_key =
1412 	    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1413 	    stcb->asoc.authinfo.peer_random, key);
1414 	stcb->asoc.authinfo.assoc_keyid = keyid;
1415 #ifdef SCTP_DEBUG
1416 	if (SCTP_AUTH_DEBUG)
1417 		sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1418 #endif
1419 
1420 	if (using_ep_key) {
1421 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
1422 	}
1423 	return (0);
1424 }
1425 
1426 /*
1427  * set the active key on an endpoint ASSUMES INP_WLOCK is already held
1428  */
1429 int
1430 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1431 {
1432 	sctp_sharedkey_t *skey;
1433 
1434 	/* find the key */
1435 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1436 	if (skey == NULL) {
1437 		/* that key doesn't exist */
1438 		return (-1);
1439 	}
1440 	inp->sctp_ep.default_keyid = keyid;
1441 	return (0);
1442 }
1443 
1444 /*
1445  * get local authentication parameters from cookie (from INIT-ACK)
1446  */
1447 void
1448 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1449     uint32_t offset, uint32_t length)
1450 {
1451 	struct sctp_paramhdr *phdr, tmp_param;
1452 	uint16_t plen, ptype;
1453 	uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1454 	struct sctp_auth_random *p_random = NULL;
1455 	uint16_t random_len = 0;
1456 	uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1457 	struct sctp_auth_hmac_algo *hmacs = NULL;
1458 	uint16_t hmacs_len = 0;
1459 	uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1460 	struct sctp_auth_chunk_list *chunks = NULL;
1461 	uint16_t num_chunks = 0;
1462 	sctp_key_t *new_key;
1463 	uint32_t keylen;
1464 
1465 	/* convert to upper bound */
1466 	length += offset;
1467 
1468 	phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1469 	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1470 	while (phdr != NULL) {
1471 		ptype = ntohs(phdr->param_type);
1472 		plen = ntohs(phdr->param_length);
1473 
1474 		if ((plen == 0) || (offset + plen > length))
1475 			break;
1476 
1477 		if (ptype == SCTP_RANDOM) {
1478 			if (plen > sizeof(random_store))
1479 				break;
1480 			phdr = sctp_get_next_param(m, offset,
1481 			    (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1482 			if (phdr == NULL)
1483 				return;
1484 			/* save the random and length for the key */
1485 			p_random = (struct sctp_auth_random *)phdr;
1486 			random_len = plen - sizeof(*p_random);
1487 		} else if (ptype == SCTP_HMAC_LIST) {
1488 			int num_hmacs;
1489 			int i;
1490 
1491 			if (plen > sizeof(hmacs_store))
1492 				break;
1493 			phdr = sctp_get_next_param(m, offset,
1494 			    (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
1495 			if (phdr == NULL)
1496 				return;
1497 			/* save the hmacs list and num for the key */
1498 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1499 			hmacs_len = plen - sizeof(*hmacs);
1500 			num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1501 			if (stcb->asoc.local_hmacs != NULL)
1502 				sctp_free_hmaclist(stcb->asoc.local_hmacs);
1503 			stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1504 			if (stcb->asoc.local_hmacs != NULL) {
1505 				for (i = 0; i < num_hmacs; i++) {
1506 					(void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1507 					    ntohs(hmacs->hmac_ids[i]));
1508 				}
1509 			}
1510 		} else if (ptype == SCTP_CHUNK_LIST) {
1511 			int i;
1512 
1513 			if (plen > sizeof(chunks_store))
1514 				break;
1515 			phdr = sctp_get_next_param(m, offset,
1516 			    (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
1517 			if (phdr == NULL)
1518 				return;
1519 			chunks = (struct sctp_auth_chunk_list *)phdr;
1520 			num_chunks = plen - sizeof(*chunks);
1521 			/* save chunks list and num for the key */
1522 			if (stcb->asoc.local_auth_chunks != NULL)
1523 				sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1524 			else
1525 				stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1526 			for (i = 0; i < num_chunks; i++) {
1527 				(void)sctp_auth_add_chunk(chunks->chunk_types[i],
1528 				    stcb->asoc.local_auth_chunks);
1529 			}
1530 		}
1531 		/* get next parameter */
1532 		offset += SCTP_SIZE32(plen);
1533 		if (offset + sizeof(struct sctp_paramhdr) > length)
1534 			break;
1535 		phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1536 		    (uint8_t *) & tmp_param);
1537 	}
1538 	/* concatenate the full random key */
1539 #ifdef SCTP_AUTH_DRAFT_04
1540 	keylen = random_len;
1541 	new_key = sctp_alloc_key(keylen);
1542 	if (new_key != NULL) {
1543 		/* copy in the RANDOM */
1544 		if (p_random != NULL)
1545 			bcopy(p_random->random_data, new_key->key, random_len);
1546 	}
1547 #else
1548 	keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
1549 	    sizeof(*hmacs) + hmacs_len;
1550 	new_key = sctp_alloc_key(keylen);
1551 	if (new_key != NULL) {
1552 		/* copy in the RANDOM */
1553 		if (p_random != NULL) {
1554 			keylen = sizeof(*p_random) + random_len;
1555 			bcopy(p_random, new_key->key, keylen);
1556 		}
1557 		/* append in the AUTH chunks */
1558 		if (chunks != NULL) {
1559 			bcopy(chunks, new_key->key + keylen,
1560 			    sizeof(*chunks) + num_chunks);
1561 			keylen += sizeof(*chunks) + num_chunks;
1562 		}
1563 		/* append in the HMACs */
1564 		if (hmacs != NULL) {
1565 			bcopy(hmacs, new_key->key + keylen,
1566 			    sizeof(*hmacs) + hmacs_len);
1567 		}
1568 	}
1569 #endif
1570 	if (stcb->asoc.authinfo.random != NULL)
1571 		sctp_free_key(stcb->asoc.authinfo.random);
1572 	stcb->asoc.authinfo.random = new_key;
1573 	stcb->asoc.authinfo.random_len = random_len;
1574 #ifdef SCTP_AUTH_DRAFT_04
1575 	/* don't include the chunks and hmacs for draft -04 */
1576 	stcb->asoc.authinfo.random->keylen = random_len;
1577 #endif
1578 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1579 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1580 
1581 	/* negotiate what HMAC to use for the peer */
1582 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1583 	    stcb->asoc.local_hmacs);
1584 	/* copy defaults from the endpoint */
1585 	/* FIX ME: put in cookie? */
1586 	stcb->asoc.authinfo.assoc_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1587 }
1588 
1589 /*
1590  * compute and fill in the HMAC digest for a packet
1591  */
1592 void
1593 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1594     struct sctp_auth_chunk *auth, struct sctp_tcb *stcb)
1595 {
1596 	uint32_t digestlen;
1597 	sctp_sharedkey_t *skey;
1598 	sctp_key_t *key;
1599 
1600 	if ((stcb == NULL) || (auth == NULL))
1601 		return;
1602 
1603 	/* zero the digest + chunk padding */
1604 	digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1605 	bzero(auth->hmac, SCTP_SIZE32(digestlen));
1606 	/* is an assoc key cached? */
1607 	if (stcb->asoc.authinfo.assoc_key == NULL) {
1608 		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1609 		    stcb->asoc.authinfo.assoc_keyid);
1610 		if (skey == NULL) {
1611 			/* not in the assoc list, so check the endpoint list */
1612 			skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1613 			    stcb->asoc.authinfo.assoc_keyid);
1614 		}
1615 		/* the only way skey is NULL is if null key id 0 is used */
1616 		if (skey != NULL)
1617 			key = skey->key;
1618 		else
1619 			key = NULL;
1620 		/* compute a new assoc key and cache it */
1621 		stcb->asoc.authinfo.assoc_key =
1622 		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1623 		    stcb->asoc.authinfo.peer_random, key);
1624 		SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1625 		    stcb->asoc.authinfo.assoc_keyid);
1626 #ifdef SCTP_DEBUG
1627 		if (SCTP_AUTH_DEBUG)
1628 			sctp_print_key(stcb->asoc.authinfo.assoc_key,
1629 			    "Assoc Key");
1630 #endif
1631 	}
1632 	/* set in the active key id */
1633 	auth->shared_key_id = htons(stcb->asoc.authinfo.assoc_keyid);
1634 
1635 	/* compute and fill in the digest */
1636 	(void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id,
1637 	    stcb->asoc.authinfo.assoc_key,
1638 	    m, auth_offset, auth->hmac);
1639 }
1640 
1641 
1642 static void
1643 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1644 {
1645 	struct mbuf *m_tmp;
1646 	uint8_t *data;
1647 
1648 	/* sanity check */
1649 	if (m == NULL)
1650 		return;
1651 
1652 	/* find the correct starting mbuf and offset (get start position) */
1653 	m_tmp = m;
1654 	while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1655 		m_offset -= SCTP_BUF_LEN(m_tmp);
1656 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1657 	}
1658 	/* now use the rest of the mbuf chain */
1659 	while ((m_tmp != NULL) && (size > 0)) {
1660 		data = mtod(m_tmp, uint8_t *) + m_offset;
1661 		if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1662 			bzero(data, SCTP_BUF_LEN(m_tmp));
1663 			size -= SCTP_BUF_LEN(m_tmp);
1664 		} else {
1665 			bzero(data, size);
1666 			size = 0;
1667 		}
1668 		/* clear the offset since it's only for the first mbuf */
1669 		m_offset = 0;
1670 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1671 	}
1672 }
1673 
1674 /*
1675  * process the incoming Authentication chunk return codes: -1 on any
1676  * authentication error 0 on authentication verification
1677  */
1678 int
1679 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1680     struct mbuf *m, uint32_t offset)
1681 {
1682 	uint16_t chunklen;
1683 	uint16_t shared_key_id;
1684 	uint16_t hmac_id;
1685 	sctp_sharedkey_t *skey;
1686 	uint32_t digestlen;
1687 	uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1688 	uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1689 
1690 	/* auth is checked for NULL by caller */
1691 	chunklen = ntohs(auth->ch.chunk_length);
1692 	if (chunklen < sizeof(*auth)) {
1693 		SCTP_STAT_INCR(sctps_recvauthfailed);
1694 		return (-1);
1695 	}
1696 	SCTP_STAT_INCR(sctps_recvauth);
1697 
1698 	/* get the auth params */
1699 	shared_key_id = ntohs(auth->shared_key_id);
1700 	hmac_id = ntohs(auth->hmac_id);
1701 	SCTPDBG(SCTP_DEBUG_AUTH1,
1702 	    "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1703 	    shared_key_id, hmac_id);
1704 
1705 	/* is the indicated HMAC supported? */
1706 	if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1707 		struct mbuf *m_err;
1708 		struct sctp_auth_invalid_hmac *err;
1709 
1710 		SCTP_STAT_INCR(sctps_recvivalhmacid);
1711 		SCTPDBG(SCTP_DEBUG_AUTH1,
1712 		    "SCTP Auth: unsupported HMAC id %u\n",
1713 		    hmac_id);
1714 		/*
1715 		 * report this in an Error Chunk: Unsupported HMAC
1716 		 * Identifier
1717 		 */
1718 		m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
1719 		    1, MT_HEADER);
1720 		if (m_err != NULL) {
1721 			/* pre-reserve some space */
1722 			SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1723 			/* fill in the error */
1724 			err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1725 			bzero(err, sizeof(*err));
1726 			err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1727 			err->ph.param_length = htons(sizeof(*err));
1728 			err->hmac_id = ntohs(hmac_id);
1729 			SCTP_BUF_LEN(m_err) = sizeof(*err);
1730 			/* queue it */
1731 			sctp_queue_op_err(stcb, m_err);
1732 		}
1733 		return (-1);
1734 	}
1735 	/* get the indicated shared key, if available */
1736 	if ((stcb->asoc.authinfo.recv_key == NULL) ||
1737 	    (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1738 		/* find the shared key on the assoc first */
1739 		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, shared_key_id);
1740 		if (skey == NULL) {
1741 			/* if not on the assoc, find it on the endpoint */
1742 			skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1743 			    shared_key_id);
1744 		}
1745 		/* if the shared key isn't found, discard the chunk */
1746 		if (skey == NULL) {
1747 			SCTP_STAT_INCR(sctps_recvivalkeyid);
1748 			SCTPDBG(SCTP_DEBUG_AUTH1,
1749 			    "SCTP Auth: unknown key id %u\n",
1750 			    shared_key_id);
1751 			return (-1);
1752 		}
1753 		/* generate a notification if this is a new key id */
1754 		if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1755 			/*
1756 			 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1757 			 * shared_key_id, (void
1758 			 * *)stcb->asoc.authinfo.recv_keyid);
1759 			 */
1760 			sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1761 			    shared_key_id, stcb->asoc.authinfo.recv_keyid);
1762 		/* compute a new recv assoc key and cache it */
1763 		if (stcb->asoc.authinfo.recv_key != NULL)
1764 			sctp_free_key(stcb->asoc.authinfo.recv_key);
1765 		stcb->asoc.authinfo.recv_key =
1766 		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1767 		    stcb->asoc.authinfo.peer_random, skey->key);
1768 		stcb->asoc.authinfo.recv_keyid = shared_key_id;
1769 #ifdef SCTP_DEBUG
1770 		if (SCTP_AUTH_DEBUG)
1771 			sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1772 #endif
1773 	}
1774 	/* validate the digest length */
1775 	digestlen = sctp_get_hmac_digest_len(hmac_id);
1776 	if (chunklen < (sizeof(*auth) + digestlen)) {
1777 		/* invalid digest length */
1778 		SCTP_STAT_INCR(sctps_recvauthfailed);
1779 		SCTPDBG(SCTP_DEBUG_AUTH1,
1780 		    "SCTP Auth: chunk too short for HMAC\n");
1781 		return (-1);
1782 	}
1783 	/* save a copy of the digest, zero the pseudo header, and validate */
1784 	bcopy(auth->hmac, digest, digestlen);
1785 	sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1786 	(void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1787 	    m, offset, computed_digest);
1788 
1789 	/* compare the computed digest with the one in the AUTH chunk */
1790 	if (memcmp(digest, computed_digest, digestlen) != 0) {
1791 		SCTP_STAT_INCR(sctps_recvauthfailed);
1792 		SCTPDBG(SCTP_DEBUG_AUTH1,
1793 		    "SCTP Auth: HMAC digest check failed\n");
1794 		return (-1);
1795 	}
1796 	return (0);
1797 }
1798 
1799 /*
1800  * Generate NOTIFICATION
1801  */
1802 void
1803 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1804     uint16_t keyid, uint16_t alt_keyid)
1805 {
1806 	struct mbuf *m_notify;
1807 	struct sctp_authkey_event *auth;
1808 	struct sctp_queued_to_read *control;
1809 
1810 	if ((stcb == NULL) ||
1811 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1812 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1813 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1814 	    ) {
1815 		/* If the socket is gone we are out of here */
1816 		return;
1817 	}
1818 	if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1819 		/* event not enabled */
1820 		return;
1821 
1822 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1823 	    0, M_DONTWAIT, 1, MT_HEADER);
1824 	if (m_notify == NULL)
1825 		/* no space left */
1826 		return;
1827 
1828 	SCTP_BUF_LEN(m_notify) = 0;
1829 	auth = mtod(m_notify, struct sctp_authkey_event *);
1830 	auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1831 	auth->auth_flags = 0;
1832 	auth->auth_length = sizeof(*auth);
1833 	auth->auth_keynumber = keyid;
1834 	auth->auth_altkeynumber = alt_keyid;
1835 	auth->auth_indication = indication;
1836 	auth->auth_assoc_id = sctp_get_associd(stcb);
1837 
1838 	SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1839 	SCTP_BUF_NEXT(m_notify) = NULL;
1840 
1841 	/* append to socket */
1842 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1843 	    0, 0, 0, 0, 0, 0, m_notify);
1844 	if (control == NULL) {
1845 		/* no memory */
1846 		sctp_m_freem(m_notify);
1847 		return;
1848 	}
1849 	control->spec_flags = M_NOTIFICATION;
1850 	control->length = SCTP_BUF_LEN(m_notify);
1851 	/* not that we need this */
1852 	control->tail_mbuf = m_notify;
1853 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1854 	    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1855 }
1856 
1857 
1858 /*
1859  * validates the AUTHentication related parameters in an INIT/INIT-ACK
1860  * Note: currently only used for INIT as INIT-ACK is handled inline
1861  * with sctp_load_addresses_from_init()
1862  */
1863 int
1864 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1865 {
1866 	struct sctp_paramhdr *phdr, parm_buf;
1867 	uint16_t ptype, plen;
1868 	int peer_supports_asconf = 0;
1869 	int peer_supports_auth = 0;
1870 	int got_random = 0, got_hmacs = 0, got_chklist = 0;
1871 	uint8_t saw_asconf = 0;
1872 	uint8_t saw_asconf_ack = 0;
1873 
1874 	/* go through each of the params. */
1875 	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1876 	while (phdr) {
1877 		ptype = ntohs(phdr->param_type);
1878 		plen = ntohs(phdr->param_length);
1879 
1880 		if (offset + plen > limit) {
1881 			break;
1882 		}
1883 		if (plen < sizeof(struct sctp_paramhdr)) {
1884 			break;
1885 		}
1886 		if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1887 			/* A supported extension chunk */
1888 			struct sctp_supported_chunk_types_param *pr_supported;
1889 			uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1890 			int num_ent, i;
1891 
1892 			phdr = sctp_get_next_param(m, offset,
1893 			    (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
1894 			if (phdr == NULL) {
1895 				return (-1);
1896 			}
1897 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1898 			num_ent = plen - sizeof(struct sctp_paramhdr);
1899 			for (i = 0; i < num_ent; i++) {
1900 				switch (pr_supported->chunk_types[i]) {
1901 				case SCTP_ASCONF:
1902 				case SCTP_ASCONF_ACK:
1903 					peer_supports_asconf = 1;
1904 					break;
1905 				case SCTP_AUTHENTICATION:
1906 					peer_supports_auth = 1;
1907 					break;
1908 				default:
1909 					/* one we don't care about */
1910 					break;
1911 				}
1912 			}
1913 		} else if (ptype == SCTP_RANDOM) {
1914 			got_random = 1;
1915 			/* enforce the random length */
1916 			if (plen != (sizeof(struct sctp_auth_random) +
1917 			    SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1918 				SCTPDBG(SCTP_DEBUG_AUTH1,
1919 				    "SCTP: invalid RANDOM len\n");
1920 				return (-1);
1921 			}
1922 		} else if (ptype == SCTP_HMAC_LIST) {
1923 			uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1924 			struct sctp_auth_hmac_algo *hmacs;
1925 			int num_hmacs;
1926 
1927 			if (plen > sizeof(store))
1928 				break;
1929 			phdr = sctp_get_next_param(m, offset,
1930 			    (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
1931 			if (phdr == NULL)
1932 				return (-1);
1933 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1934 			num_hmacs = (plen - sizeof(*hmacs)) /
1935 			    sizeof(hmacs->hmac_ids[0]);
1936 			/* validate the hmac list */
1937 			if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1938 				SCTPDBG(SCTP_DEBUG_AUTH1,
1939 				    "SCTP: invalid HMAC param\n");
1940 				return (-1);
1941 			}
1942 			got_hmacs = 1;
1943 		} else if (ptype == SCTP_CHUNK_LIST) {
1944 			int i, num_chunks;
1945 			uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1946 
1947 			/* did the peer send a non-empty chunk list? */
1948 			struct sctp_auth_chunk_list *chunks = NULL;
1949 
1950 			phdr = sctp_get_next_param(m, offset,
1951 			    (struct sctp_paramhdr *)chunks_store,
1952 			    min(plen, sizeof(chunks_store)));
1953 			if (phdr == NULL)
1954 				return (-1);
1955 
1956 			/*-
1957 			 * Flip through the list and mark that the
1958 			 * peer supports asconf/asconf_ack.
1959 			 */
1960 			chunks = (struct sctp_auth_chunk_list *)phdr;
1961 			num_chunks = plen - sizeof(*chunks);
1962 			for (i = 0; i < num_chunks; i++) {
1963 				/* record asconf/asconf-ack if listed */
1964 				if (chunks->chunk_types[i] == SCTP_ASCONF)
1965 					saw_asconf = 1;
1966 				if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1967 					saw_asconf_ack = 1;
1968 
1969 			}
1970 			if (num_chunks)
1971 				got_chklist = 1;
1972 		}
1973 		offset += SCTP_SIZE32(plen);
1974 		if (offset >= limit) {
1975 			break;
1976 		}
1977 		phdr = sctp_get_next_param(m, offset, &parm_buf,
1978 		    sizeof(parm_buf));
1979 	}
1980 	/* validate authentication required parameters */
1981 	if (got_random && got_hmacs) {
1982 		peer_supports_auth = 1;
1983 	} else {
1984 		peer_supports_auth = 0;
1985 	}
1986 	if (!peer_supports_auth && got_chklist) {
1987 		SCTPDBG(SCTP_DEBUG_AUTH1,
1988 		    "SCTP: peer sent chunk list w/o AUTH\n");
1989 		return (-1);
1990 	}
1991 	if (!sctp_asconf_auth_nochk && peer_supports_asconf &&
1992 	    !peer_supports_auth) {
1993 		SCTPDBG(SCTP_DEBUG_AUTH1,
1994 		    "SCTP: peer supports ASCONF but not AUTH\n");
1995 		return (-1);
1996 	} else if ((peer_supports_asconf) && (peer_supports_auth) &&
1997 	    ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1998 		return (-2);
1999 	}
2000 	return (0);
2001 }
2002 
2003 void
2004 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2005 {
2006 	uint16_t chunks_len = 0;
2007 	uint16_t hmacs_len = 0;
2008 	uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
2009 	sctp_key_t *new_key;
2010 	uint16_t keylen;
2011 
2012 	/* initialize hmac list from endpoint */
2013 	stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
2014 	if (stcb->asoc.local_hmacs != NULL) {
2015 		hmacs_len = stcb->asoc.local_hmacs->num_algo *
2016 		    sizeof(stcb->asoc.local_hmacs->hmac[0]);
2017 	}
2018 	/* initialize auth chunks list from endpoint */
2019 	stcb->asoc.local_auth_chunks =
2020 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
2021 	if (stcb->asoc.local_auth_chunks != NULL) {
2022 		int i;
2023 
2024 		for (i = 0; i < 256; i++) {
2025 			if (stcb->asoc.local_auth_chunks->chunks[i])
2026 				chunks_len++;
2027 		}
2028 	}
2029 	/* copy defaults from the endpoint */
2030 	stcb->asoc.authinfo.assoc_keyid = inp->sctp_ep.default_keyid;
2031 
2032 	/* now set the concatenated key (random + chunks + hmacs) */
2033 #ifdef SCTP_AUTH_DRAFT_04
2034 	/* don't include the chunks and hmacs for draft -04 */
2035 	keylen = random_len;
2036 	new_key = sctp_generate_random_key(keylen);
2037 #else
2038 	/* key includes parameter headers */
2039 	keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2040 	    hmacs_len;
2041 	new_key = sctp_alloc_key(keylen);
2042 	if (new_key != NULL) {
2043 		struct sctp_paramhdr *ph;
2044 		int plen;
2045 
2046 		/* generate and copy in the RANDOM */
2047 		ph = (struct sctp_paramhdr *)new_key->key;
2048 		ph->param_type = htons(SCTP_RANDOM);
2049 		plen = sizeof(*ph) + random_len;
2050 		ph->param_length = htons(plen);
2051 		SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2052 		keylen = plen;
2053 
2054 		/* append in the AUTH chunks */
2055 		/* NOTE: currently we always have chunks to list */
2056 		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2057 		ph->param_type = htons(SCTP_CHUNK_LIST);
2058 		plen = sizeof(*ph) + chunks_len;
2059 		ph->param_length = htons(plen);
2060 		keylen += sizeof(*ph);
2061 		if (stcb->asoc.local_auth_chunks) {
2062 			int i;
2063 
2064 			for (i = 0; i < 256; i++) {
2065 				if (stcb->asoc.local_auth_chunks->chunks[i])
2066 					new_key->key[keylen++] = i;
2067 			}
2068 		}
2069 		/* append in the HMACs */
2070 		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2071 		ph->param_type = htons(SCTP_HMAC_LIST);
2072 		plen = sizeof(*ph) + hmacs_len;
2073 		ph->param_length = htons(plen);
2074 		keylen += sizeof(*ph);
2075 		(void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2076 		    new_key->key + keylen);
2077 	}
2078 #endif
2079 	if (stcb->asoc.authinfo.random != NULL)
2080 		sctp_free_key(stcb->asoc.authinfo.random);
2081 	stcb->asoc.authinfo.random = new_key;
2082 	stcb->asoc.authinfo.random_len = random_len;
2083 }
2084 
2085 
2086 #ifdef SCTP_HMAC_TEST
2087 /*
2088  * HMAC and key concatenation tests
2089  */
2090 static void
2091 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2092 {
2093 	uint32_t i;
2094 
2095 	printf("\n%s: 0x", str);
2096 	if (digest == NULL)
2097 		return;
2098 
2099 	for (i = 0; i < digestlen; i++)
2100 		printf("%02x", digest[i]);
2101 }
2102 
2103 static int
2104 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2105     uint32_t keylen, uint8_t * text, uint32_t textlen,
2106     uint8_t * digest, uint32_t digestlen)
2107 {
2108 	uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2109 
2110 	printf("\n%s:", str);
2111 	sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2112 	sctp_print_digest(digest, digestlen, "Expected digest");
2113 	sctp_print_digest(computed_digest, digestlen, "Computed digest");
2114 	if (memcmp(digest, computed_digest, digestlen) != 0) {
2115 		printf("\nFAILED");
2116 		return (-1);
2117 	} else {
2118 		printf("\nPASSED");
2119 		return (0);
2120 	}
2121 }
2122 
2123 
2124 /*
2125  * RFC 2202: HMAC-SHA1 test cases
2126  */
2127 void
2128 sctp_test_hmac_sha1(void)
2129 {
2130 	uint8_t *digest;
2131 	uint8_t key[128];
2132 	uint32_t keylen;
2133 	uint8_t text[128];
2134 	uint32_t textlen;
2135 	uint32_t digestlen = 20;
2136 	int failed = 0;
2137 
2138 	/*
2139 	 * test_case =     1 key =
2140 	 * 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b key_len =       20
2141 	 * data =          "Hi There" data_len =      8 digest =
2142 	 * 0xb617318655057264e28bc0b6fb378c8ef146be00
2143 	 */
2144 	keylen = 20;
2145 	memset(key, 0x0b, keylen);
2146 	textlen = 8;
2147 	strcpy(text, "Hi There");
2148 	digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2149 	if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2150 	    text, textlen, digest, digestlen) < 0)
2151 		failed++;
2152 
2153 	/*
2154 	 * test_case =     2 key =           "Jefe" key_len =       4 data =
2155 	 * "what do ya want for nothing?" data_len =      28 digest =
2156 	 * 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2157 	 */
2158 	keylen = 4;
2159 	strcpy(key, "Jefe");
2160 	textlen = 28;
2161 	strcpy(text, "what do ya want for nothing?");
2162 	digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2163 	if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2164 	    text, textlen, digest, digestlen) < 0)
2165 		failed++;
2166 
2167 	/*
2168 	 * test_case =     3 key =
2169 	 * 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa key_len =       20
2170 	 * data =          0xdd repeated 50 times data_len =      50 digest
2171 	 * = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2172 	 */
2173 	keylen = 20;
2174 	memset(key, 0xaa, keylen);
2175 	textlen = 50;
2176 	memset(text, 0xdd, textlen);
2177 	digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2178 	if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2179 	    text, textlen, digest, digestlen) < 0)
2180 		failed++;
2181 
2182 	/*
2183 	 * test_case =     4 key =
2184 	 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2185 	 * data =          0xcd repeated 50 times data_len =      50 digest
2186 	 * =        0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2187 	 */
2188 	keylen = 25;
2189 	memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2190 	textlen = 50;
2191 	memset(text, 0xcd, textlen);
2192 	digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2193 	if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2194 	    text, textlen, digest, digestlen) < 0)
2195 		failed++;
2196 
2197 	/*
2198 	 * test_case =     5 key =
2199 	 * 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c key_len =       20
2200 	 * data =          "Test With Truncation" data_len =      20 digest
2201 	 * = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 digest-96 =
2202 	 * 0x4c1a03424b55e07fe7f27be1
2203 	 */
2204 	keylen = 20;
2205 	memset(key, 0x0c, keylen);
2206 	textlen = 20;
2207 	strcpy(text, "Test With Truncation");
2208 	digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2209 	if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2210 	    text, textlen, digest, digestlen) < 0)
2211 		failed++;
2212 
2213 	/*
2214 	 * test_case =     6 key =           0xaa repeated 80 times key_len
2215 	 * = 80 data =          "Test Using Larger Than Block-Size Key -
2216 	 * Hash Key First" data_len =      54 digest =
2217 	 * 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2218 	 */
2219 	keylen = 80;
2220 	memset(key, 0xaa, keylen);
2221 	textlen = 54;
2222 	strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2223 	digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2224 	if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2225 	    text, textlen, digest, digestlen) < 0)
2226 		failed++;
2227 
2228 	/*
2229 	 * test_case =     7 key =           0xaa repeated 80 times key_len
2230 	 * = 80 data =          "Test Using Larger Than Block-Size Key and
2231 	 * Larger Than One Block-Size Data" data_len =      73 digest =
2232 	 * 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2233 	 */
2234 	keylen = 80;
2235 	memset(key, 0xaa, keylen);
2236 	textlen = 73;
2237 	strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2238 	digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2239 	if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2240 	    text, textlen, digest, digestlen) < 0)
2241 		failed++;
2242 
2243 	/* done with all tests */
2244 	if (failed)
2245 		printf("\nSHA1 test results: %d cases failed", failed);
2246 	else
2247 		printf("\nSHA1 test results: all test cases passed");
2248 }
2249 
2250 /*
2251  * RFC 2202: HMAC-MD5 test cases
2252  */
2253 void
2254 sctp_test_hmac_md5(void)
2255 {
2256 	uint8_t *digest;
2257 	uint8_t key[128];
2258 	uint32_t keylen;
2259 	uint8_t text[128];
2260 	uint32_t textlen;
2261 	uint32_t digestlen = 16;
2262 	int failed = 0;
2263 
2264 	/*
2265 	 * test_case =     1 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2266 	 * key_len =       16 data = "Hi There" data_len =      8 digest =
2267 	 * 0x9294727a3638bb1c13f48ef8158bfc9d
2268 	 */
2269 	keylen = 16;
2270 	memset(key, 0x0b, keylen);
2271 	textlen = 8;
2272 	strcpy(text, "Hi There");
2273 	digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2274 	if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2275 	    text, textlen, digest, digestlen) < 0)
2276 		failed++;
2277 
2278 	/*
2279 	 * test_case =     2 key =           "Jefe" key_len =       4 data =
2280 	 * "what do ya want for nothing?" data_len =      28 digest =
2281 	 * 0x750c783e6ab0b503eaa86e310a5db738
2282 	 */
2283 	keylen = 4;
2284 	strcpy(key, "Jefe");
2285 	textlen = 28;
2286 	strcpy(text, "what do ya want for nothing?");
2287 	digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2288 	if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2289 	    text, textlen, digest, digestlen) < 0)
2290 		failed++;
2291 
2292 	/*
2293 	 * test_case =     3 key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2294 	 * key_len =       16 data = 0xdd repeated 50 times data_len = 50
2295 	 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2296 	 */
2297 	keylen = 16;
2298 	memset(key, 0xaa, keylen);
2299 	textlen = 50;
2300 	memset(text, 0xdd, textlen);
2301 	digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2302 	if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2303 	    text, textlen, digest, digestlen) < 0)
2304 		failed++;
2305 
2306 	/*
2307 	 * test_case =     4 key =
2308 	 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2309 	 * data =          0xcd repeated 50 times data_len =      50 digest
2310 	 * =        0x697eaf0aca3a3aea3a75164746ffaa79
2311 	 */
2312 	keylen = 25;
2313 	memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2314 	textlen = 50;
2315 	memset(text, 0xcd, textlen);
2316 	digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2317 	if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2318 	    text, textlen, digest, digestlen) < 0)
2319 		failed++;
2320 
2321 	/*
2322 	 * test_case =     5 key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2323 	 * key_len =       16 data = "Test With Truncation" data_len = 20
2324 	 * digest = 0x56461ef2342edc00f9bab995690efd4c digest-96
2325 	 * 0x56461ef2342edc00f9bab995
2326 	 */
2327 	keylen = 16;
2328 	memset(key, 0x0c, keylen);
2329 	textlen = 20;
2330 	strcpy(text, "Test With Truncation");
2331 	digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2332 	if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2333 	    text, textlen, digest, digestlen) < 0)
2334 		failed++;
2335 
2336 	/*
2337 	 * test_case =     6 key =           0xaa repeated 80 times key_len
2338 	 * = 80 data =          "Test Using Larger Than Block-Size Key -
2339 	 * Hash Key First" data_len =      54 digest =
2340 	 * 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2341 	 */
2342 	keylen = 80;
2343 	memset(key, 0xaa, keylen);
2344 	textlen = 54;
2345 	strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2346 	digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2347 	if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2348 	    text, textlen, digest, digestlen) < 0)
2349 		failed++;
2350 
2351 	/*
2352 	 * test_case =     7 key =           0xaa repeated 80 times key_len
2353 	 * = 80 data =          "Test Using Larger Than Block-Size Key and
2354 	 * Larger Than One Block-Size Data" data_len =      73 digest =
2355 	 * 0x6f630fad67cda0ee1fb1f562db3aa53e
2356 	 */
2357 	keylen = 80;
2358 	memset(key, 0xaa, keylen);
2359 	textlen = 73;
2360 	strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2361 	digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2362 	if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2363 	    text, textlen, digest, digestlen) < 0)
2364 		failed++;
2365 
2366 	/* done with all tests */
2367 	if (failed)
2368 		printf("\nMD5 test results: %d cases failed", failed);
2369 	else
2370 		printf("\nMD5 test results: all test cases passed");
2371 }
2372 
2373 /*
2374  * test assoc key concatenation
2375  */
2376 static int
2377 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2378     sctp_key_t * expected_key)
2379 {
2380 	sctp_key_t *key;
2381 	int ret_val;
2382 
2383 	sctp_show_key(key1, "\nkey1");
2384 	sctp_show_key(key2, "\nkey2");
2385 	key = sctp_compute_hashkey(key1, key2, NULL);
2386 	sctp_show_key(expected_key, "\nExpected");
2387 	sctp_show_key(key, "\nComputed");
2388 	if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2389 		printf("\nFAILED");
2390 		ret_val = -1;
2391 	} else {
2392 		printf("\nPASSED");
2393 		ret_val = 0;
2394 	}
2395 	sctp_free_key(key1);
2396 	sctp_free_key(key2);
2397 	sctp_free_key(expected_key);
2398 	sctp_free_key(key);
2399 	return (ret_val);
2400 }
2401 
2402 
2403 void
2404 sctp_test_authkey(void)
2405 {
2406 	sctp_key_t *key1, *key2, *expected_key;
2407 	int failed = 0;
2408 
2409 	/* test case 1 */
2410 	key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2411 	key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2412 	expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2413 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2414 		failed++;
2415 
2416 	/* test case 2 */
2417 	key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2418 	key2 = sctp_set_key("\x02", 1);
2419 	expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2420 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2421 		failed++;
2422 
2423 	/* test case 3 */
2424 	key1 = sctp_set_key("\x01", 1);
2425 	key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2426 	expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2427 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2428 		failed++;
2429 
2430 	/* test case 4 */
2431 	key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2432 	key2 = sctp_set_key("\x01", 1);
2433 	expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2434 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2435 		failed++;
2436 
2437 	/* test case 5 */
2438 	key1 = sctp_set_key("\x01", 1);
2439 	key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2440 	expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2441 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2442 		failed++;
2443 
2444 	/* test case 6 */
2445 	key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2446 	key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2447 	expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2448 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2449 		failed++;
2450 
2451 	/* test case 7 */
2452 	key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2453 	key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2454 	expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2455 	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2456 		failed++;
2457 
2458 	/* done with all tests */
2459 	if (failed)
2460 		printf("\nKey concatenation test results: %d cases failed", failed);
2461 	else
2462 		printf("\nKey concatenation test results: all test cases passed");
2463 }
2464 
2465 
2466 #if defined(STANDALONE_HMAC_TEST)
2467 int
2468 main(void)
2469 {
2470 	sctp_test_hmac_sha1();
2471 	sctp_test_hmac_md5();
2472 	sctp_test_authkey();
2473 }
2474 
2475 #endif				/* STANDALONE_HMAC_TEST */
2476 
2477 #endif				/* SCTP_HMAC_TEST */
2478