2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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>
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 */
52 sctp_clear_chunklist(sctp_auth_chklist_t * chklist)
54 bzero(chklist, sizeof(*chklist));
55 /* chklist->num_chunks = 0; */
59 sctp_alloc_chunklist(void)
61 sctp_auth_chklist_t *chklist;
63 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
65 if (chklist == NULL) {
66 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
68 sctp_clear_chunklist(chklist);
74 sctp_free_chunklist(sctp_auth_chklist_t * list)
77 SCTP_FREE(list, SCTP_M_AUTH_CL);
81 sctp_copy_chunklist(sctp_auth_chklist_t * list)
83 sctp_auth_chklist_t *new_list;
89 new_list = sctp_alloc_chunklist();
93 bcopy(list, new_list, sizeof(*new_list));
100 * add a chunk to the required chunks list
103 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
108 /* is chunk restricted? */
109 if ((chunk == SCTP_INITIATION) ||
110 (chunk == SCTP_INITIATION_ACK) ||
111 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
112 (chunk == SCTP_AUTHENTICATION)) {
115 if (list->chunks[chunk] == 0) {
116 list->chunks[chunk] = 1;
118 SCTPDBG(SCTP_DEBUG_AUTH1,
119 "SCTP: added chunk %u (0x%02x) to Auth list\n",
126 * delete a chunk from the required chunks list
129 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
134 /* is chunk restricted? */
135 if ((chunk == SCTP_ASCONF) ||
136 (chunk == SCTP_ASCONF_ACK)) {
139 if (list->chunks[chunk] == 1) {
140 list->chunks[chunk] = 0;
142 SCTPDBG(SCTP_DEBUG_AUTH1,
143 "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
150 sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
155 return (list->num_chunks);
159 * set the default list of chunks requiring AUTH
162 sctp_auth_set_default_chunks(sctp_auth_chklist_t * list)
164 (void)sctp_auth_add_chunk(SCTP_ASCONF, list);
165 (void)sctp_auth_add_chunk(SCTP_ASCONF_ACK, list);
169 * return the current number and list of required chunks caller must
170 * guarantee ptr has space for up to 256 bytes
173 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
180 for (i = 0; i < 256; i++) {
181 if (list->chunks[i] != 0) {
190 sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
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) {
208 /* pack into a 32 byte bitfield */
209 for (i = 0; i < 256; i++) {
210 if (list->chunks[i] != 0) {
213 ptr[index] |= (1 << offset);
222 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
223 sctp_auth_chklist_t * list)
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);
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);
255 * allocate structure space for a key of length keylen
258 sctp_alloc_key(uint32_t keylen)
262 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
264 if (new_key == NULL) {
268 new_key->keylen = keylen;
273 sctp_free_key(sctp_key_t * key)
276 SCTP_FREE(key, SCTP_M_AUTH_KY);
280 sctp_print_key(sctp_key_t * key, const char *str)
285 printf("%s: [Null key]\n", str);
288 printf("%s: len %u, ", str, key->keylen);
290 for (i = 0; i < key->keylen; i++)
291 printf("%02x", key->key[i]);
294 printf("[Null key]\n");
299 sctp_show_key(sctp_key_t * key, const char *str)
304 printf("%s: [Null key]\n", str);
307 printf("%s: len %u, ", str, key->keylen);
309 for (i = 0; i < key->keylen; i++)
310 printf("%02x", key->key[i]);
313 printf("[Null key]\n");
318 sctp_get_keylen(sctp_key_t * key)
321 return (key->keylen);
327 * generate a new random key of length 'keylen'
330 sctp_generate_random_key(uint32_t keylen)
334 /* validate keylen */
335 if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX)
336 keylen = SCTP_AUTH_RANDOM_SIZE_MAX;
338 new_key = sctp_alloc_key(keylen);
339 if (new_key == NULL) {
343 SCTP_READ_RANDOM(new_key->key, keylen);
344 new_key->keylen = keylen;
349 sctp_set_key(uint8_t * key, uint32_t keylen)
353 new_key = sctp_alloc_key(keylen);
354 if (new_key == NULL) {
358 bcopy(key, new_key->key, keylen);
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
367 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
371 uint32_t key1len, key2len;
372 uint8_t *key_1, *key_2;
373 uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
375 /* sanity/length check */
376 key1len = sctp_get_keylen(key1);
377 key2len = sctp_get_keylen(key2);
378 if ((key1len == 0) && (key2len == 0))
380 else if (key1len == 0)
382 else if (key2len == 0)
385 if (key1len != key2len) {
386 if (key1len >= key2len)
391 if (key1len < maxlen) {
392 /* prepend zeroes to key1 */
393 bcopy(key1->key, temp + (maxlen - key1len), key1len);
397 /* prepend zeroes to key2 */
398 bcopy(key2->key, temp + (maxlen - key2len), key2len);
408 for (i = 0; i < maxlen; i++) {
411 else if (*key_1 < *key_2)
417 /* keys are equal value, so check lengths */
418 if (key1len == key2len)
420 else if (key1len < key2len)
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
432 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
438 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
439 sctp_get_keylen(shared);
442 /* get space for the new key */
443 new_key = sctp_alloc_key(keylen);
444 if (new_key == NULL) {
448 new_key->keylen = keylen;
449 key_ptr = new_key->key;
451 /* all keys empty/null?! */
455 /* concatenate the keys */
456 if (sctp_compare_key(key1, key2) <= 0) {
457 /* key is key1 + shared + key2 */
458 if (sctp_get_keylen(key1)) {
459 bcopy(key1->key, key_ptr, key1->keylen);
460 key_ptr += key1->keylen;
462 if (sctp_get_keylen(shared)) {
463 bcopy(shared->key, key_ptr, shared->keylen);
464 key_ptr += shared->keylen;
466 if (sctp_get_keylen(key2)) {
467 bcopy(key2->key, key_ptr, key2->keylen);
468 key_ptr += key2->keylen;
471 /* key is key2 + shared + key1 */
472 if (sctp_get_keylen(key2)) {
473 bcopy(key2->key, key_ptr, key2->keylen);
474 key_ptr += key2->keylen;
476 if (sctp_get_keylen(shared)) {
477 bcopy(shared->key, key_ptr, shared->keylen);
478 key_ptr += shared->keylen;
480 if (sctp_get_keylen(key1)) {
481 bcopy(key1->key, key_ptr, key1->keylen);
482 key_ptr += key1->keylen;
490 sctp_alloc_sharedkey(void)
492 sctp_sharedkey_t *new_key;
494 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
496 if (new_key == NULL) {
506 sctp_free_sharedkey(sctp_sharedkey_t * skey)
509 if (skey->key != NULL)
510 sctp_free_key(skey->key);
511 SCTP_FREE(skey, SCTP_M_AUTH_KY);
516 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
518 sctp_sharedkey_t *skey;
520 LIST_FOREACH(skey, shared_keys, next) {
521 if (skey->keyid == key_id)
528 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
529 sctp_sharedkey_t * new_skey)
531 sctp_sharedkey_t *skey;
533 if ((shared_keys == NULL) || (new_skey == NULL))
536 /* insert into an empty list? */
537 if (SCTP_LIST_EMPTY(shared_keys)) {
538 LIST_INSERT_HEAD(shared_keys, new_skey, next);
541 /* insert into the existing list, ordered by key id */
542 LIST_FOREACH(skey, shared_keys, next) {
543 if (new_skey->keyid < skey->keyid) {
544 /* insert it before here */
545 LIST_INSERT_BEFORE(skey, new_skey, next);
547 } else if (new_skey->keyid == skey->keyid) {
548 /* replace the existing key */
549 SCTPDBG(SCTP_DEBUG_AUTH1,
550 "replacing shared key id %u\n",
552 LIST_INSERT_BEFORE(skey, new_skey, next);
553 LIST_REMOVE(skey, next);
554 sctp_free_sharedkey(skey);
557 if (LIST_NEXT(skey, next) == NULL) {
558 /* belongs at the end of the list */
559 LIST_INSERT_AFTER(skey, new_skey, next);
565 static sctp_sharedkey_t *
566 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
568 sctp_sharedkey_t *new_skey;
572 new_skey = sctp_alloc_sharedkey();
573 if (new_skey == NULL)
575 if (skey->key != NULL)
576 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
578 new_skey->key = NULL;
579 new_skey->keyid = skey->keyid;
584 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
586 sctp_sharedkey_t *skey, *new_skey;
589 if ((src == NULL) || (dest == NULL))
591 LIST_FOREACH(skey, src, next) {
592 new_skey = sctp_copy_sharedkey(skey);
593 if (new_skey != NULL) {
594 sctp_insert_sharedkey(dest, new_skey);
603 sctp_alloc_hmaclist(uint8_t num_hmacs)
605 sctp_hmaclist_t *new_list;
608 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
609 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
611 if (new_list == NULL) {
615 new_list->max_algo = num_hmacs;
616 new_list->num_algo = 0;
621 sctp_free_hmaclist(sctp_hmaclist_t * list)
624 SCTP_FREE(list, SCTP_M_AUTH_HL);
630 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
636 if (list->num_algo == list->max_algo) {
637 SCTPDBG(SCTP_DEBUG_AUTH1,
638 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
641 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
643 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
646 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
647 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
648 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
650 (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
653 /* Now is it already in the list */
654 for (i = 0; i < list->num_algo; i++) {
655 if (list->hmac[i] == hmac_id) {
656 /* already in list */
660 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
661 list->hmac[list->num_algo++] = hmac_id;
666 sctp_copy_hmaclist(sctp_hmaclist_t * list)
668 sctp_hmaclist_t *new_list;
674 new_list = sctp_alloc_hmaclist(list->max_algo);
675 if (new_list == NULL)
678 new_list->max_algo = list->max_algo;
679 new_list->num_algo = list->num_algo;
680 for (i = 0; i < list->num_algo; i++)
681 new_list->hmac[i] = list->hmac[i];
686 sctp_default_supported_hmaclist(void)
688 sctp_hmaclist_t *new_list;
690 new_list = sctp_alloc_hmaclist(2);
691 if (new_list == NULL)
693 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
694 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
699 * HMAC algos are listed in priority/preference order find the best HMAC id
700 * to use for the peer based on local support
703 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
707 if ((local == NULL) || (peer == NULL))
708 return (SCTP_AUTH_HMAC_ID_RSVD);
710 for (i = 0; i < peer->num_algo; i++) {
711 for (j = 0; j < local->num_algo; j++) {
712 if (peer->hmac[i] == local->hmac[j]) {
713 #ifndef SCTP_AUTH_DRAFT_04
714 /* "skip" MD5 as it's been deprecated */
715 if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
719 /* found the "best" one */
720 SCTPDBG(SCTP_DEBUG_AUTH1,
721 "SCTP: negotiated peer HMAC id %u\n",
723 return (peer->hmac[i]);
727 /* didn't find one! */
728 return (SCTP_AUTH_HMAC_ID_RSVD);
732 * serialize the HMAC algo list and return space used caller must guarantee
733 * ptr has appropriate space
736 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
744 for (i = 0; i < list->num_algo; i++) {
745 hmac_id = htons(list->hmac[i]);
746 bcopy(&hmac_id, ptr, sizeof(hmac_id));
747 ptr += sizeof(hmac_id);
749 return (list->num_algo * sizeof(hmac_id));
753 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
757 uint32_t sha1_supported = 0;
759 for (i = 0; i < num_hmacs; i++) {
760 hmac_id = ntohs(hmacs->hmac_ids[i]);
761 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
764 /* all HMAC id's are supported */
765 if (sha1_supported == 0)
772 sctp_alloc_authinfo(void)
774 sctp_authinfo_t *new_authinfo;
776 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
779 if (new_authinfo == NULL) {
783 bzero(&new_authinfo, sizeof(*new_authinfo));
784 return (new_authinfo);
788 sctp_free_authinfo(sctp_authinfo_t * authinfo)
790 if (authinfo == NULL)
793 if (authinfo->random != NULL)
794 sctp_free_key(authinfo->random);
795 if (authinfo->peer_random != NULL)
796 sctp_free_key(authinfo->peer_random);
797 if (authinfo->assoc_key != NULL)
798 sctp_free_key(authinfo->assoc_key);
799 if (authinfo->recv_key != NULL)
800 sctp_free_key(authinfo->recv_key);
802 /* We are NOT dynamically allocating authinfo's right now... */
803 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
808 sctp_get_auth_chunk_len(uint16_t hmac_algo)
812 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
813 return (SCTP_SIZE32(size));
817 sctp_get_hmac_digest_len(uint16_t hmac_algo)
820 case SCTP_AUTH_HMAC_ID_SHA1:
821 return (SCTP_AUTH_DIGEST_LEN_SHA1);
822 case SCTP_AUTH_HMAC_ID_MD5:
823 return (SCTP_AUTH_DIGEST_LEN_MD5);
825 case SCTP_AUTH_HMAC_ID_SHA224:
826 return (SCTP_AUTH_DIGEST_LEN_SHA224);
829 case SCTP_AUTH_HMAC_ID_SHA256:
830 return (SCTP_AUTH_DIGEST_LEN_SHA256);
831 case SCTP_AUTH_HMAC_ID_SHA384:
832 return (SCTP_AUTH_DIGEST_LEN_SHA384);
833 case SCTP_AUTH_HMAC_ID_SHA512:
834 return (SCTP_AUTH_DIGEST_LEN_SHA512);
837 /* unknown HMAC algorithm: can't do anything */
843 sctp_get_hmac_block_len(uint16_t hmac_algo)
846 case SCTP_AUTH_HMAC_ID_SHA1:
847 case SCTP_AUTH_HMAC_ID_MD5:
849 case SCTP_AUTH_HMAC_ID_SHA224:
853 case SCTP_AUTH_HMAC_ID_SHA256:
855 case SCTP_AUTH_HMAC_ID_SHA384:
856 case SCTP_AUTH_HMAC_ID_SHA512:
859 case SCTP_AUTH_HMAC_ID_RSVD:
861 /* unknown HMAC algorithm: can't do anything */
867 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
870 case SCTP_AUTH_HMAC_ID_SHA1:
871 SHA1_Init(&ctx->sha1);
873 case SCTP_AUTH_HMAC_ID_MD5:
877 case SCTP_AUTH_HMAC_ID_SHA224:
881 case SCTP_AUTH_HMAC_ID_SHA256:
882 SHA256_Init(&ctx->sha256);
884 case SCTP_AUTH_HMAC_ID_SHA384:
885 SHA384_Init(&ctx->sha384);
887 case SCTP_AUTH_HMAC_ID_SHA512:
888 SHA512_Init(&ctx->sha512);
891 case SCTP_AUTH_HMAC_ID_RSVD:
893 /* unknown HMAC algorithm: can't do anything */
899 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
900 uint8_t * text, uint32_t textlen)
903 case SCTP_AUTH_HMAC_ID_SHA1:
904 SHA1_Update(&ctx->sha1, text, textlen);
906 case SCTP_AUTH_HMAC_ID_MD5:
907 MD5_Update(&ctx->md5, text, textlen);
910 case SCTP_AUTH_HMAC_ID_SHA224:
914 case SCTP_AUTH_HMAC_ID_SHA256:
915 SHA256_Update(&ctx->sha256, text, textlen);
917 case SCTP_AUTH_HMAC_ID_SHA384:
918 SHA384_Update(&ctx->sha384, text, textlen);
920 case SCTP_AUTH_HMAC_ID_SHA512:
921 SHA512_Update(&ctx->sha512, text, textlen);
924 case SCTP_AUTH_HMAC_ID_RSVD:
926 /* unknown HMAC algorithm: can't do anything */
932 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
936 case SCTP_AUTH_HMAC_ID_SHA1:
937 SHA1_Final(digest, &ctx->sha1);
939 case SCTP_AUTH_HMAC_ID_MD5:
940 MD5_Final(digest, &ctx->md5);
943 case SCTP_AUTH_HMAC_ID_SHA224:
947 case SCTP_AUTH_HMAC_ID_SHA256:
948 SHA256_Final(digest, &ctx->sha256);
950 case SCTP_AUTH_HMAC_ID_SHA384:
951 /* SHA384 is truncated SHA512 */
952 SHA384_Final(digest, &ctx->sha384);
954 case SCTP_AUTH_HMAC_ID_SHA512:
955 SHA512_Final(digest, &ctx->sha512);
958 case SCTP_AUTH_HMAC_ID_RSVD:
960 /* unknown HMAC algorithm: can't do anything */
966 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
968 * Compute the HMAC digest using the desired hash key, text, and HMAC
969 * algorithm. Resulting digest is placed in 'digest' and digest length
970 * is returned, if the HMAC was performed.
972 * WARNING: it is up to the caller to supply sufficient space to hold the
976 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
977 uint8_t * text, uint32_t textlen, uint8_t * digest)
981 sctp_hash_context_t ctx;
982 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
983 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
986 /* sanity check the material and length */
987 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
988 (textlen == 0) || (digest == NULL)) {
989 /* can't do HMAC with empty key or text or digest store */
992 /* validate the hmac algo and get the digest length */
993 digestlen = sctp_get_hmac_digest_len(hmac_algo);
997 /* hash the key if it is longer than the hash block size */
998 blocklen = sctp_get_hmac_block_len(hmac_algo);
999 if (keylen > blocklen) {
1000 sctp_hmac_init(hmac_algo, &ctx);
1001 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1002 sctp_hmac_final(hmac_algo, &ctx, temp);
1003 /* set the hashed key as the key */
1007 /* initialize the inner/outer pads with the key and "append" zeroes */
1008 bzero(ipad, blocklen);
1009 bzero(opad, blocklen);
1010 bcopy(key, ipad, keylen);
1011 bcopy(key, opad, keylen);
1013 /* XOR the key with ipad and opad values */
1014 for (i = 0; i < blocklen; i++) {
1019 /* perform inner hash */
1020 sctp_hmac_init(hmac_algo, &ctx);
1021 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1022 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1023 sctp_hmac_final(hmac_algo, &ctx, temp);
1025 /* perform outer hash */
1026 sctp_hmac_init(hmac_algo, &ctx);
1027 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1028 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1029 sctp_hmac_final(hmac_algo, &ctx, digest);
1036 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1037 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
1041 sctp_hash_context_t ctx;
1042 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1043 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1047 /* sanity check the material and length */
1048 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1049 /* can't do HMAC with empty key or text or digest store */
1052 /* validate the hmac algo and get the digest length */
1053 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1057 /* hash the key if it is longer than the hash block size */
1058 blocklen = sctp_get_hmac_block_len(hmac_algo);
1059 if (keylen > blocklen) {
1060 sctp_hmac_init(hmac_algo, &ctx);
1061 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1062 sctp_hmac_final(hmac_algo, &ctx, temp);
1063 /* set the hashed key as the key */
1067 /* initialize the inner/outer pads with the key and "append" zeroes */
1068 bzero(ipad, blocklen);
1069 bzero(opad, blocklen);
1070 bcopy(key, ipad, keylen);
1071 bcopy(key, opad, keylen);
1073 /* XOR the key with ipad and opad values */
1074 for (i = 0; i < blocklen; i++) {
1079 /* perform inner hash */
1080 sctp_hmac_init(hmac_algo, &ctx);
1081 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1082 /* find the correct starting mbuf and offset (get start of text) */
1084 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1085 m_offset -= SCTP_BUF_LEN(m_tmp);
1086 m_tmp = SCTP_BUF_NEXT(m_tmp);
1088 /* now use the rest of the mbuf chain for the text */
1089 while (m_tmp != NULL) {
1090 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1091 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1092 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1094 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1095 SCTP_BUF_LEN(m_tmp) - m_offset);
1098 /* clear the offset since it's only for the first mbuf */
1100 m_tmp = SCTP_BUF_NEXT(m_tmp);
1102 sctp_hmac_final(hmac_algo, &ctx, temp);
1104 /* perform outer hash */
1105 sctp_hmac_init(hmac_algo, &ctx);
1106 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1107 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1108 sctp_hmac_final(hmac_algo, &ctx, digest);
1114 * verify the HMAC digest using the desired hash key, text, and HMAC
1115 * algorithm. Returns -1 on error, 0 on success.
1118 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1119 uint8_t * text, uint32_t textlen,
1120 uint8_t * digest, uint32_t digestlen)
1123 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1125 /* sanity check the material and length */
1126 if ((key == NULL) || (keylen == 0) ||
1127 (text == NULL) || (textlen == 0) || (digest == NULL)) {
1128 /* can't do HMAC with empty key or text or digest */
1131 len = sctp_get_hmac_digest_len(hmac_algo);
1132 if ((len == 0) || (digestlen != len))
1135 /* compute the expected hash */
1136 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1139 if (memcmp(digest, temp, digestlen) != 0)
1147 * computes the requested HMAC using a key struct (which may be modified if
1148 * the keylen exceeds the HMAC block len).
1151 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1152 uint32_t textlen, uint8_t * digest)
1156 sctp_hash_context_t ctx;
1157 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1160 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1162 /* can't do HMAC with empty key or text or digest store */
1165 /* validate the hmac algo and get the digest length */
1166 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1170 /* hash the key if it is longer than the hash block size */
1171 blocklen = sctp_get_hmac_block_len(hmac_algo);
1172 if (key->keylen > blocklen) {
1173 sctp_hmac_init(hmac_algo, &ctx);
1174 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1175 sctp_hmac_final(hmac_algo, &ctx, temp);
1176 /* save the hashed key as the new key */
1177 key->keylen = digestlen;
1178 bcopy(temp, key->key, key->keylen);
1180 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1186 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1187 uint32_t m_offset, uint8_t * digest)
1191 sctp_hash_context_t ctx;
1192 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1195 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1196 /* can't do HMAC with empty key or text or digest store */
1199 /* validate the hmac algo and get the digest length */
1200 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1204 /* hash the key if it is longer than the hash block size */
1205 blocklen = sctp_get_hmac_block_len(hmac_algo);
1206 if (key->keylen > blocklen) {
1207 sctp_hmac_init(hmac_algo, &ctx);
1208 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1209 sctp_hmac_final(hmac_algo, &ctx, temp);
1210 /* save the hashed key as the new key */
1211 key->keylen = digestlen;
1212 bcopy(temp, key->key, key->keylen);
1214 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1218 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1222 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1225 for (i = 0; i < list->num_algo; i++)
1226 if (list->hmac[i] == id)
1229 /* not in the list */
1235 * clear any cached key(s) if they match the given key id on an association
1236 * the cached key(s) will be recomputed and re-cached at next use. ASSUMES
1237 * TCB_LOCK is already held
1240 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1245 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1246 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1247 stcb->asoc.authinfo.assoc_key = NULL;
1249 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1250 sctp_free_key(stcb->asoc.authinfo.recv_key);
1251 stcb->asoc.authinfo.recv_key = NULL;
1256 * clear any cached key(s) if they match the given key id for all assocs on
1257 * an association ASSUMES INP_WLOCK is already held
1260 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1262 struct sctp_tcb *stcb;
1267 /* clear the cached keys on all assocs on this instance */
1268 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1269 SCTP_TCB_LOCK(stcb);
1270 sctp_clear_cachedkeys(stcb, keyid);
1271 SCTP_TCB_UNLOCK(stcb);
1276 * delete a shared key from an association ASSUMES TCB_LOCK is already held
1279 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1281 sctp_sharedkey_t *skey;
1286 /* is the keyid the assoc active sending key */
1287 if (keyid == stcb->asoc.authinfo.assoc_keyid)
1290 /* does the key exist? */
1291 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1296 LIST_REMOVE(skey, next);
1297 sctp_free_sharedkey(skey); /* frees skey->key as well */
1299 /* clear any cached keys */
1300 sctp_clear_cachedkeys(stcb, keyid);
1305 * deletes a shared key from the endpoint ASSUMES INP_WLOCK is already held
1308 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1310 sctp_sharedkey_t *skey;
1311 struct sctp_tcb *stcb;
1316 /* is the keyid the active sending key on the endpoint or any assoc */
1317 if (keyid == inp->sctp_ep.default_keyid)
1319 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1320 SCTP_TCB_LOCK(stcb);
1321 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1322 SCTP_TCB_UNLOCK(stcb);
1325 SCTP_TCB_UNLOCK(stcb);
1328 /* does the key exist? */
1329 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1334 LIST_REMOVE(skey, next);
1335 sctp_free_sharedkey(skey); /* frees skey->key as well */
1337 /* clear any cached keys */
1338 sctp_clear_cachedkeys_ep(inp, keyid);
1343 * set the active key on an association ASSUME TCB_LOCK is already held
1346 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1348 sctp_sharedkey_t *skey = NULL;
1349 sctp_key_t *key = NULL;
1350 int using_ep_key = 0;
1352 /* find the key on the assoc */
1353 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1355 /* if not on the assoc, find the key on the endpoint */
1356 atomic_add_int(&stcb->asoc.refcnt, 1);
1357 SCTP_TCB_UNLOCK(stcb);
1358 SCTP_INP_RLOCK(stcb->sctp_ep);
1359 SCTP_TCB_LOCK(stcb);
1360 atomic_add_int(&stcb->asoc.refcnt, -1);
1361 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1366 /* that key doesn't exist */
1368 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1372 /* get the shared key text */
1375 /* free any existing cached key */
1376 if (stcb->asoc.authinfo.assoc_key != NULL)
1377 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1378 /* compute a new assoc key and cache it */
1379 stcb->asoc.authinfo.assoc_key =
1380 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1381 stcb->asoc.authinfo.peer_random, key);
1382 stcb->asoc.authinfo.assoc_keyid = keyid;
1384 if (SCTP_AUTH_DEBUG)
1385 sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1389 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1395 * set the active key on an endpoint ASSUMES INP_WLOCK is already held
1398 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1400 sctp_sharedkey_t *skey;
1403 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1405 /* that key doesn't exist */
1408 inp->sctp_ep.default_keyid = keyid;
1413 * get local authentication parameters from cookie (from INIT-ACK)
1416 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1417 uint32_t offset, uint32_t length)
1419 struct sctp_paramhdr *phdr, tmp_param;
1420 uint16_t plen, ptype;
1421 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1422 struct sctp_auth_random *p_random = NULL;
1423 uint16_t random_len = 0;
1424 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1425 struct sctp_auth_hmac_algo *hmacs = NULL;
1426 uint16_t hmacs_len = 0;
1427 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1428 struct sctp_auth_chunk_list *chunks = NULL;
1429 uint16_t num_chunks = 0;
1430 sctp_key_t *new_key;
1433 /* convert to upper bound */
1436 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1437 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1438 while (phdr != NULL) {
1439 ptype = ntohs(phdr->param_type);
1440 plen = ntohs(phdr->param_length);
1442 if ((plen == 0) || (offset + plen > length))
1445 if (ptype == SCTP_RANDOM) {
1446 if (plen > sizeof(random_store))
1448 phdr = sctp_get_next_param(m, offset,
1449 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1452 /* save the random and length for the key */
1453 p_random = (struct sctp_auth_random *)phdr;
1454 random_len = plen - sizeof(*p_random);
1455 } else if (ptype == SCTP_HMAC_LIST) {
1459 if (plen > sizeof(hmacs_store))
1461 phdr = sctp_get_next_param(m, offset,
1462 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
1465 /* save the hmacs list and num for the key */
1466 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1467 hmacs_len = plen - sizeof(*hmacs);
1468 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1469 if (stcb->asoc.local_hmacs != NULL)
1470 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1471 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1472 if (stcb->asoc.local_hmacs != NULL) {
1473 for (i = 0; i < num_hmacs; i++) {
1474 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1475 ntohs(hmacs->hmac_ids[i]));
1478 } else if (ptype == SCTP_CHUNK_LIST) {
1481 if (plen > sizeof(chunks_store))
1483 phdr = sctp_get_next_param(m, offset,
1484 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
1487 chunks = (struct sctp_auth_chunk_list *)phdr;
1488 num_chunks = plen - sizeof(*chunks);
1489 /* save chunks list and num for the key */
1490 if (stcb->asoc.local_auth_chunks != NULL)
1491 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1493 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1494 for (i = 0; i < num_chunks; i++) {
1495 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1496 stcb->asoc.local_auth_chunks);
1499 /* get next parameter */
1500 offset += SCTP_SIZE32(plen);
1501 if (offset + sizeof(struct sctp_paramhdr) > length)
1503 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1504 (uint8_t *) & tmp_param);
1506 /* concatenate the full random key */
1507 #ifdef SCTP_AUTH_DRAFT_04
1508 keylen = random_len;
1509 new_key = sctp_alloc_key(keylen);
1510 if (new_key != NULL) {
1511 /* copy in the RANDOM */
1512 if (p_random != NULL)
1513 bcopy(p_random->random_data, new_key->key, random_len);
1516 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
1517 sizeof(*hmacs) + hmacs_len;
1518 new_key = sctp_alloc_key(keylen);
1519 if (new_key != NULL) {
1520 /* copy in the RANDOM */
1521 if (p_random != NULL) {
1522 keylen = sizeof(*p_random) + random_len;
1523 bcopy(p_random, new_key->key, keylen);
1525 /* append in the AUTH chunks */
1526 if (chunks != NULL) {
1527 bcopy(chunks, new_key->key + keylen,
1528 sizeof(*chunks) + num_chunks);
1529 keylen += sizeof(*chunks) + num_chunks;
1531 /* append in the HMACs */
1532 if (hmacs != NULL) {
1533 bcopy(hmacs, new_key->key + keylen,
1534 sizeof(*hmacs) + hmacs_len);
1538 if (stcb->asoc.authinfo.random != NULL)
1539 sctp_free_key(stcb->asoc.authinfo.random);
1540 stcb->asoc.authinfo.random = new_key;
1541 stcb->asoc.authinfo.random_len = random_len;
1542 #ifdef SCTP_AUTH_DRAFT_04
1543 /* don't include the chunks and hmacs for draft -04 */
1544 stcb->asoc.authinfo.random->keylen = random_len;
1546 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1547 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1549 /* negotiate what HMAC to use for the peer */
1550 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1551 stcb->asoc.local_hmacs);
1552 /* copy defaults from the endpoint */
1553 /* FIX ME: put in cookie? */
1554 stcb->asoc.authinfo.assoc_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1558 * compute and fill in the HMAC digest for a packet
1561 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1562 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb)
1565 sctp_sharedkey_t *skey;
1568 if ((stcb == NULL) || (auth == NULL))
1571 /* zero the digest + chunk padding */
1572 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1573 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1574 /* is an assoc key cached? */
1575 if (stcb->asoc.authinfo.assoc_key == NULL) {
1576 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1577 stcb->asoc.authinfo.assoc_keyid);
1579 /* not in the assoc list, so check the endpoint list */
1580 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1581 stcb->asoc.authinfo.assoc_keyid);
1583 /* the only way skey is NULL is if null key id 0 is used */
1588 /* compute a new assoc key and cache it */
1589 stcb->asoc.authinfo.assoc_key =
1590 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1591 stcb->asoc.authinfo.peer_random, key);
1592 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1593 stcb->asoc.authinfo.assoc_keyid);
1595 if (SCTP_AUTH_DEBUG)
1596 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1600 /* set in the active key id */
1601 auth->shared_key_id = htons(stcb->asoc.authinfo.assoc_keyid);
1603 /* compute and fill in the digest */
1604 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id,
1605 stcb->asoc.authinfo.assoc_key,
1606 m, auth_offset, auth->hmac);
1611 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1620 /* find the correct starting mbuf and offset (get start position) */
1622 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1623 m_offset -= SCTP_BUF_LEN(m_tmp);
1624 m_tmp = SCTP_BUF_NEXT(m_tmp);
1626 /* now use the rest of the mbuf chain */
1627 while ((m_tmp != NULL) && (size > 0)) {
1628 data = mtod(m_tmp, uint8_t *) + m_offset;
1629 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1630 bzero(data, SCTP_BUF_LEN(m_tmp));
1631 size -= SCTP_BUF_LEN(m_tmp);
1636 /* clear the offset since it's only for the first mbuf */
1638 m_tmp = SCTP_BUF_NEXT(m_tmp);
1643 * process the incoming Authentication chunk return codes: -1 on any
1644 * authentication error 0 on authentication verification
1647 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1648 struct mbuf *m, uint32_t offset)
1651 uint16_t shared_key_id;
1653 sctp_sharedkey_t *skey;
1655 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1656 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1658 /* auth is checked for NULL by caller */
1659 chunklen = ntohs(auth->ch.chunk_length);
1660 if (chunklen < sizeof(*auth)) {
1661 SCTP_STAT_INCR(sctps_recvauthfailed);
1664 SCTP_STAT_INCR(sctps_recvauth);
1666 /* get the auth params */
1667 shared_key_id = ntohs(auth->shared_key_id);
1668 hmac_id = ntohs(auth->hmac_id);
1669 SCTPDBG(SCTP_DEBUG_AUTH1,
1670 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1671 shared_key_id, hmac_id);
1673 /* is the indicated HMAC supported? */
1674 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1676 struct sctp_auth_invalid_hmac *err;
1678 SCTP_STAT_INCR(sctps_recvivalhmacid);
1679 SCTPDBG(SCTP_DEBUG_AUTH1,
1680 "SCTP Auth: unsupported HMAC id %u\n",
1683 * report this in an Error Chunk: Unsupported HMAC
1686 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
1688 if (m_err != NULL) {
1689 /* pre-reserve some space */
1690 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1691 /* fill in the error */
1692 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1693 bzero(err, sizeof(*err));
1694 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1695 err->ph.param_length = htons(sizeof(*err));
1696 err->hmac_id = ntohs(hmac_id);
1697 SCTP_BUF_LEN(m_err) = sizeof(*err);
1699 sctp_queue_op_err(stcb, m_err);
1703 /* get the indicated shared key, if available */
1704 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1705 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1706 /* find the shared key on the assoc first */
1707 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, shared_key_id);
1709 /* if not on the assoc, find it on the endpoint */
1710 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1713 /* if the shared key isn't found, discard the chunk */
1715 SCTP_STAT_INCR(sctps_recvivalkeyid);
1716 SCTPDBG(SCTP_DEBUG_AUTH1,
1717 "SCTP Auth: unknown key id %u\n",
1721 /* generate a notification if this is a new key id */
1722 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1724 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1725 * shared_key_id, (void
1726 * *)stcb->asoc.authinfo.recv_keyid);
1728 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1729 shared_key_id, stcb->asoc.authinfo.recv_keyid);
1730 /* compute a new recv assoc key and cache it */
1731 if (stcb->asoc.authinfo.recv_key != NULL)
1732 sctp_free_key(stcb->asoc.authinfo.recv_key);
1733 stcb->asoc.authinfo.recv_key =
1734 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1735 stcb->asoc.authinfo.peer_random, skey->key);
1736 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1738 if (SCTP_AUTH_DEBUG)
1739 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1742 /* validate the digest length */
1743 digestlen = sctp_get_hmac_digest_len(hmac_id);
1744 if (chunklen < (sizeof(*auth) + digestlen)) {
1745 /* invalid digest length */
1746 SCTP_STAT_INCR(sctps_recvauthfailed);
1747 SCTPDBG(SCTP_DEBUG_AUTH1,
1748 "SCTP Auth: chunk too short for HMAC\n");
1751 /* save a copy of the digest, zero the pseudo header, and validate */
1752 bcopy(auth->hmac, digest, digestlen);
1753 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1754 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1755 m, offset, computed_digest);
1757 /* compare the computed digest with the one in the AUTH chunk */
1758 if (memcmp(digest, computed_digest, digestlen) != 0) {
1759 SCTP_STAT_INCR(sctps_recvauthfailed);
1760 SCTPDBG(SCTP_DEBUG_AUTH1,
1761 "SCTP Auth: HMAC digest check failed\n");
1768 * Generate NOTIFICATION
1771 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1772 uint16_t keyid, uint16_t alt_keyid)
1774 struct mbuf *m_notify;
1775 struct sctp_authkey_event *auth;
1776 struct sctp_queued_to_read *control;
1778 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1779 /* event not enabled */
1782 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1783 0, M_DONTWAIT, 1, MT_HEADER);
1784 if (m_notify == NULL)
1788 SCTP_BUF_LEN(m_notify) = 0;
1789 auth = mtod(m_notify, struct sctp_authkey_event *);
1790 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1791 auth->auth_flags = 0;
1792 auth->auth_length = sizeof(*auth);
1793 auth->auth_keynumber = keyid;
1794 auth->auth_altkeynumber = alt_keyid;
1795 auth->auth_indication = indication;
1796 auth->auth_assoc_id = sctp_get_associd(stcb);
1798 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1799 SCTP_BUF_NEXT(m_notify) = NULL;
1801 /* append to socket */
1802 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1803 0, 0, 0, 0, 0, 0, m_notify);
1804 if (control == NULL) {
1806 sctp_m_freem(m_notify);
1809 control->spec_flags = M_NOTIFICATION;
1810 control->length = SCTP_BUF_LEN(m_notify);
1811 /* not that we need this */
1812 control->tail_mbuf = m_notify;
1813 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1814 &stcb->sctp_socket->so_rcv, 1);
1819 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1820 * Note: currently only used for INIT as INIT-ACK is handled inline
1821 * with sctp_load_addresses_from_init()
1824 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1826 struct sctp_paramhdr *phdr, parm_buf;
1827 uint16_t ptype, plen;
1828 int peer_supports_asconf = 0;
1829 int peer_supports_auth = 0;
1830 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1832 /* go through each of the params. */
1833 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1835 ptype = ntohs(phdr->param_type);
1836 plen = ntohs(phdr->param_length);
1838 if (offset + plen > limit) {
1844 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1845 /* A supported extension chunk */
1846 struct sctp_supported_chunk_types_param *pr_supported;
1847 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1850 phdr = sctp_get_next_param(m, offset,
1851 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
1855 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1856 num_ent = plen - sizeof(struct sctp_paramhdr);
1857 for (i = 0; i < num_ent; i++) {
1858 switch (pr_supported->chunk_types[i]) {
1860 case SCTP_ASCONF_ACK:
1861 peer_supports_asconf = 1;
1863 case SCTP_AUTHENTICATION:
1864 peer_supports_auth = 1;
1867 /* one we don't care about */
1871 } else if (ptype == SCTP_RANDOM) {
1873 /* enforce the random length */
1874 if (plen != (sizeof(struct sctp_auth_random) +
1875 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1876 SCTPDBG(SCTP_DEBUG_AUTH1,
1877 "SCTP: invalid RANDOM len\n");
1880 } else if (ptype == SCTP_HMAC_LIST) {
1881 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1882 struct sctp_auth_hmac_algo *hmacs;
1885 if (plen > sizeof(store))
1887 phdr = sctp_get_next_param(m, offset,
1888 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
1891 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1892 num_hmacs = (plen - sizeof(*hmacs)) /
1893 sizeof(hmacs->hmac_ids[0]);
1894 /* validate the hmac list */
1895 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1896 SCTPDBG(SCTP_DEBUG_AUTH1,
1897 "SCTP: invalid HMAC param\n");
1901 } else if (ptype == SCTP_CHUNK_LIST) {
1902 /* did the peer send a non-empty chunk list? */
1906 offset += SCTP_SIZE32(plen);
1907 if (offset >= limit) {
1910 phdr = sctp_get_next_param(m, offset, &parm_buf,
1913 /* validate authentication required parameters */
1914 if (got_random && got_hmacs) {
1915 peer_supports_auth = 1;
1917 peer_supports_auth = 0;
1919 if (!peer_supports_auth && got_chklist) {
1920 SCTPDBG(SCTP_DEBUG_AUTH1,
1921 "SCTP: peer sent chunk list w/o AUTH\n");
1924 if (!sctp_asconf_auth_nochk && peer_supports_asconf &&
1925 !peer_supports_auth) {
1926 SCTPDBG(SCTP_DEBUG_AUTH1,
1927 "SCTP: peer supports ASCONF but not AUTH\n");
1934 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1936 uint16_t chunks_len = 0;
1937 uint16_t hmacs_len = 0;
1938 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1939 sctp_key_t *new_key;
1942 /* initialize hmac list from endpoint */
1943 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1944 if (stcb->asoc.local_hmacs != NULL) {
1945 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1946 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1948 /* initialize auth chunks list from endpoint */
1949 stcb->asoc.local_auth_chunks =
1950 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1951 if (stcb->asoc.local_auth_chunks != NULL) {
1954 for (i = 0; i < 256; i++) {
1955 if (stcb->asoc.local_auth_chunks->chunks[i])
1959 /* copy defaults from the endpoint */
1960 stcb->asoc.authinfo.assoc_keyid = inp->sctp_ep.default_keyid;
1962 /* now set the concatenated key (random + chunks + hmacs) */
1963 #ifdef SCTP_AUTH_DRAFT_04
1964 /* don't include the chunks and hmacs for draft -04 */
1965 keylen = random_len;
1966 new_key = sctp_generate_random_key(keylen);
1968 /* key includes parameter headers */
1969 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
1971 new_key = sctp_alloc_key(keylen);
1972 if (new_key != NULL) {
1973 struct sctp_paramhdr *ph;
1976 /* generate and copy in the RANDOM */
1977 ph = (struct sctp_paramhdr *)new_key->key;
1978 ph->param_type = htons(SCTP_RANDOM);
1979 plen = sizeof(*ph) + random_len;
1980 ph->param_length = htons(plen);
1981 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
1984 /* append in the AUTH chunks */
1985 /* NOTE: currently we always have chunks to list */
1986 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
1987 ph->param_type = htons(SCTP_CHUNK_LIST);
1988 plen = sizeof(*ph) + chunks_len;
1989 ph->param_length = htons(plen);
1990 keylen += sizeof(*ph);
1991 if (stcb->asoc.local_auth_chunks) {
1994 for (i = 0; i < 256; i++) {
1995 if (stcb->asoc.local_auth_chunks->chunks[i])
1996 new_key->key[keylen++] = i;
1999 /* append in the HMACs */
2000 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2001 ph->param_type = htons(SCTP_HMAC_LIST);
2002 plen = sizeof(*ph) + hmacs_len;
2003 ph->param_length = htons(plen);
2004 keylen += sizeof(*ph);
2005 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2006 new_key->key + keylen);
2009 if (stcb->asoc.authinfo.random != NULL)
2010 sctp_free_key(stcb->asoc.authinfo.random);
2011 stcb->asoc.authinfo.random = new_key;
2012 stcb->asoc.authinfo.random_len = random_len;
2016 #ifdef SCTP_HMAC_TEST
2018 * HMAC and key concatenation tests
2021 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2025 printf("\n%s: 0x", str);
2029 for (i = 0; i < digestlen; i++)
2030 printf("%02x", digest[i]);
2034 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2035 uint32_t keylen, uint8_t * text, uint32_t textlen,
2036 uint8_t * digest, uint32_t digestlen)
2038 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2040 printf("\n%s:", str);
2041 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2042 sctp_print_digest(digest, digestlen, "Expected digest");
2043 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2044 if (memcmp(digest, computed_digest, digestlen) != 0) {
2055 * RFC 2202: HMAC-SHA1 test cases
2058 sctp_test_hmac_sha1(void)
2065 uint32_t digestlen = 20;
2069 * test_case = 1 key =
2070 * 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b key_len = 20
2071 * data = "Hi There" data_len = 8 digest =
2072 * 0xb617318655057264e28bc0b6fb378c8ef146be00
2075 memset(key, 0x0b, keylen);
2077 strcpy(text, "Hi There");
2078 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2079 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2080 text, textlen, digest, digestlen) < 0)
2084 * test_case = 2 key = "Jefe" key_len = 4 data =
2085 * "what do ya want for nothing?" data_len = 28 digest =
2086 * 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2089 strcpy(key, "Jefe");
2091 strcpy(text, "what do ya want for nothing?");
2092 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2093 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2094 text, textlen, digest, digestlen) < 0)
2098 * test_case = 3 key =
2099 * 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa key_len = 20
2100 * data = 0xdd repeated 50 times data_len = 50 digest
2101 * = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2104 memset(key, 0xaa, keylen);
2106 memset(text, 0xdd, textlen);
2107 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2108 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2109 text, textlen, digest, digestlen) < 0)
2113 * test_case = 4 key =
2114 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2115 * data = 0xcd repeated 50 times data_len = 50 digest
2116 * = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2119 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);
2121 memset(text, 0xcd, textlen);
2122 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2123 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2124 text, textlen, digest, digestlen) < 0)
2128 * test_case = 5 key =
2129 * 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c key_len = 20
2130 * data = "Test With Truncation" data_len = 20 digest
2131 * = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 digest-96 =
2132 * 0x4c1a03424b55e07fe7f27be1
2135 memset(key, 0x0c, keylen);
2137 strcpy(text, "Test With Truncation");
2138 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2139 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2140 text, textlen, digest, digestlen) < 0)
2144 * test_case = 6 key = 0xaa repeated 80 times key_len
2145 * = 80 data = "Test Using Larger Than Block-Size Key -
2146 * Hash Key First" data_len = 54 digest =
2147 * 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2150 memset(key, 0xaa, keylen);
2152 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2153 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2154 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2155 text, textlen, digest, digestlen) < 0)
2159 * test_case = 7 key = 0xaa repeated 80 times key_len
2160 * = 80 data = "Test Using Larger Than Block-Size Key and
2161 * Larger Than One Block-Size Data" data_len = 73 digest =
2162 * 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2165 memset(key, 0xaa, keylen);
2167 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2168 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2169 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2170 text, textlen, digest, digestlen) < 0)
2173 /* done with all tests */
2175 printf("\nSHA1 test results: %d cases failed", failed);
2177 printf("\nSHA1 test results: all test cases passed");
2181 * RFC 2202: HMAC-MD5 test cases
2184 sctp_test_hmac_md5(void)
2191 uint32_t digestlen = 16;
2195 * test_case = 1 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2196 * key_len = 16 data = "Hi There" data_len = 8 digest =
2197 * 0x9294727a3638bb1c13f48ef8158bfc9d
2200 memset(key, 0x0b, keylen);
2202 strcpy(text, "Hi There");
2203 digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2204 if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2205 text, textlen, digest, digestlen) < 0)
2209 * test_case = 2 key = "Jefe" key_len = 4 data =
2210 * "what do ya want for nothing?" data_len = 28 digest =
2211 * 0x750c783e6ab0b503eaa86e310a5db738
2214 strcpy(key, "Jefe");
2216 strcpy(text, "what do ya want for nothing?");
2217 digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2218 if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2219 text, textlen, digest, digestlen) < 0)
2223 * test_case = 3 key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2224 * key_len = 16 data = 0xdd repeated 50 times data_len = 50
2225 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2228 memset(key, 0xaa, keylen);
2230 memset(text, 0xdd, textlen);
2231 digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2232 if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2233 text, textlen, digest, digestlen) < 0)
2237 * test_case = 4 key =
2238 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2239 * data = 0xcd repeated 50 times data_len = 50 digest
2240 * = 0x697eaf0aca3a3aea3a75164746ffaa79
2243 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);
2245 memset(text, 0xcd, textlen);
2246 digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2247 if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2248 text, textlen, digest, digestlen) < 0)
2252 * test_case = 5 key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2253 * key_len = 16 data = "Test With Truncation" data_len = 20
2254 * digest = 0x56461ef2342edc00f9bab995690efd4c digest-96
2255 * 0x56461ef2342edc00f9bab995
2258 memset(key, 0x0c, keylen);
2260 strcpy(text, "Test With Truncation");
2261 digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2262 if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2263 text, textlen, digest, digestlen) < 0)
2267 * test_case = 6 key = 0xaa repeated 80 times key_len
2268 * = 80 data = "Test Using Larger Than Block-Size Key -
2269 * Hash Key First" data_len = 54 digest =
2270 * 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2273 memset(key, 0xaa, keylen);
2275 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2276 digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2277 if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2278 text, textlen, digest, digestlen) < 0)
2282 * test_case = 7 key = 0xaa repeated 80 times key_len
2283 * = 80 data = "Test Using Larger Than Block-Size Key and
2284 * Larger Than One Block-Size Data" data_len = 73 digest =
2285 * 0x6f630fad67cda0ee1fb1f562db3aa53e
2288 memset(key, 0xaa, keylen);
2290 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2291 digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2292 if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2293 text, textlen, digest, digestlen) < 0)
2296 /* done with all tests */
2298 printf("\nMD5 test results: %d cases failed", failed);
2300 printf("\nMD5 test results: all test cases passed");
2304 * test assoc key concatenation
2307 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2308 sctp_key_t * expected_key)
2313 sctp_show_key(key1, "\nkey1");
2314 sctp_show_key(key2, "\nkey2");
2315 key = sctp_compute_hashkey(key1, key2, NULL);
2316 sctp_show_key(expected_key, "\nExpected");
2317 sctp_show_key(key, "\nComputed");
2318 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2325 sctp_free_key(key1);
2326 sctp_free_key(key2);
2327 sctp_free_key(expected_key);
2334 sctp_test_authkey(void)
2336 sctp_key_t *key1, *key2, *expected_key;
2340 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2341 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2342 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2343 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2347 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2348 key2 = sctp_set_key("\x02", 1);
2349 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2350 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2354 key1 = sctp_set_key("\x01", 1);
2355 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2356 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2357 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2361 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2362 key2 = sctp_set_key("\x01", 1);
2363 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2364 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2368 key1 = sctp_set_key("\x01", 1);
2369 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2370 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2371 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2375 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2376 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2377 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);
2378 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2382 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2383 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2384 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);
2385 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2388 /* done with all tests */
2390 printf("\nKey concatenation test results: %d cases failed", failed);
2392 printf("\nKey concatenation test results: all test cases passed");
2396 #if defined(STANDALONE_HMAC_TEST)
2400 sctp_test_hmac_sha1();
2401 sctp_test_hmac_md5();
2402 sctp_test_authkey();
2405 #endif /* STANDALONE_HMAC_TEST */
2407 #endif /* SCTP_HMAC_TEST */