2 * Copyright (c) 2001-2007, 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) {
67 if (sctp_debug_on & SCTP_AUTH_DEBUG) {
68 printf("sctp_alloc_chunklist: failed to get memory!\n");
70 #endif /* SCTP_DEBUG */
72 sctp_clear_chunklist(chklist);
78 sctp_free_chunklist(sctp_auth_chklist_t * list)
85 sctp_copy_chunklist(sctp_auth_chklist_t * list)
87 sctp_auth_chklist_t *new_list;
93 new_list = sctp_alloc_chunklist();
97 bcopy(list, new_list, sizeof(*new_list));
104 * add a chunk to the required chunks list
107 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
112 /* is chunk restricted? */
113 if ((chunk == SCTP_INITIATION) ||
114 (chunk == SCTP_INITIATION_ACK) ||
115 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
116 (chunk == SCTP_AUTHENTICATION)) {
119 if (list->chunks[chunk] == 0) {
120 list->chunks[chunk] = 1;
124 printf("SCTP: added chunk %u (0x%02x) to Auth list\n",
132 * delete a chunk from the required chunks list
135 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
140 /* is chunk restricted? */
141 if ((chunk == SCTP_ASCONF) ||
142 (chunk == SCTP_ASCONF_ACK)) {
145 if (list->chunks[chunk] == 1) {
146 list->chunks[chunk] = 0;
150 printf("SCTP: deleted chunk %u (0x%02x) from Auth list\n",
158 sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
163 return (list->num_chunks);
167 * set the default list of chunks requiring AUTH
170 sctp_auth_set_default_chunks(sctp_auth_chklist_t * list)
172 sctp_auth_add_chunk(SCTP_ASCONF, list);
173 sctp_auth_add_chunk(SCTP_ASCONF_ACK, list);
177 * return the current number and list of required chunks caller must
178 * guarantee ptr has space for up to 256 bytes
181 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
188 for (i = 0; i < 256; i++) {
189 if (list->chunks[i] != 0) {
198 sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
205 if (list->num_chunks <= 32) {
206 /* just list them, one byte each */
207 for (i = 0; i < 256; i++) {
208 if (list->chunks[i] != 0) {
216 /* pack into a 32 byte bitfield */
217 for (i = 0; i < 256; i++) {
218 if (list->chunks[i] != 0) {
221 ptr[index] |= (1 << offset);
230 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
231 sctp_auth_chklist_t * list)
239 if (num_chunks <= 32) {
240 /* just pull them, one byte each */
241 for (i = 0; i < num_chunks; i++) {
242 sctp_auth_add_chunk(*ptr++, list);
248 /* unpack from a 32 byte bitfield */
249 for (index = 0; index < 32; index++) {
250 for (offset = 0; offset < 8; offset++) {
251 if (ptr[index] & (1 << offset)) {
252 sctp_auth_add_chunk((index * 8) + offset, list);
263 * allocate structure space for a key of length keylen
266 sctp_alloc_key(uint32_t keylen)
270 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
272 if (new_key == NULL) {
276 new_key->keylen = keylen;
281 sctp_free_key(sctp_key_t * key)
288 sctp_print_key(sctp_key_t * key, const char *str)
293 printf("%s: [Null key]\n", str);
296 printf("%s: len %u, ", str, key->keylen);
298 for (i = 0; i < key->keylen; i++)
299 printf("%02x", key->key[i]);
302 printf("[Null key]\n");
307 sctp_show_key(sctp_key_t * key, const char *str)
312 printf("%s: [Null key]\n", str);
315 printf("%s: len %u, ", str, key->keylen);
317 for (i = 0; i < key->keylen; i++)
318 printf("%02x", key->key[i]);
321 printf("[Null key]\n");
325 static inline uint32_t
326 sctp_get_keylen(sctp_key_t * key)
329 return (key->keylen);
335 * generate a new random key of length 'keylen'
338 sctp_generate_random_key(uint32_t keylen)
342 /* validate keylen */
343 if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX)
344 keylen = SCTP_AUTH_RANDOM_SIZE_MAX;
346 new_key = sctp_alloc_key(keylen);
347 if (new_key == NULL) {
351 SCTP_READ_RANDOM(new_key->key, keylen);
352 new_key->keylen = keylen;
357 sctp_set_key(uint8_t * key, uint32_t keylen)
361 new_key = sctp_alloc_key(keylen);
362 if (new_key == NULL) {
366 bcopy(key, new_key->key, keylen);
371 * given two keys of variable size, compute which key is "larger/smaller"
372 * returns: 1 if key1 > key2 -1 if key1 < key2 0 if key1 = key2
375 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
379 uint32_t key1len, key2len;
380 uint8_t *key_1, *key_2;
381 uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
383 /* sanity/length check */
384 key1len = sctp_get_keylen(key1);
385 key2len = sctp_get_keylen(key2);
386 if ((key1len == 0) && (key2len == 0))
388 else if (key1len == 0)
390 else if (key2len == 0)
393 if (key1len != key2len) {
394 if (key1len >= key2len)
399 if (key1len < maxlen) {
400 /* prepend zeroes to key1 */
401 bcopy(key1->key, temp + (maxlen - key1len), key1len);
405 /* prepend zeroes to key2 */
406 bcopy(key2->key, temp + (maxlen - key2len), key2len);
416 for (i = 0; i < maxlen; i++) {
419 else if (*key_1 < *key_2)
425 /* keys are equal value, so check lengths */
426 if (key1len == key2len)
428 else if (key1len < key2len)
435 * generate the concatenated keying material based on the two keys and the
436 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
437 * order for concatenation
440 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
446 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
447 sctp_get_keylen(shared);
450 /* get space for the new key */
451 new_key = sctp_alloc_key(keylen);
452 if (new_key == NULL) {
456 new_key->keylen = keylen;
457 key_ptr = new_key->key;
459 /* all keys empty/null?! */
463 /* concatenate the keys */
464 if (sctp_compare_key(key1, key2) <= 0) {
465 /* key is key1 + shared + key2 */
466 if (sctp_get_keylen(key1)) {
467 bcopy(key1->key, key_ptr, key1->keylen);
468 key_ptr += key1->keylen;
470 if (sctp_get_keylen(shared)) {
471 bcopy(shared->key, key_ptr, shared->keylen);
472 key_ptr += shared->keylen;
474 if (sctp_get_keylen(key2)) {
475 bcopy(key2->key, key_ptr, key2->keylen);
476 key_ptr += key2->keylen;
479 /* key is key2 + shared + key1 */
480 if (sctp_get_keylen(key2)) {
481 bcopy(key2->key, key_ptr, key2->keylen);
482 key_ptr += key2->keylen;
484 if (sctp_get_keylen(shared)) {
485 bcopy(shared->key, key_ptr, shared->keylen);
486 key_ptr += shared->keylen;
488 if (sctp_get_keylen(key1)) {
489 bcopy(key1->key, key_ptr, key1->keylen);
490 key_ptr += key1->keylen;
498 sctp_alloc_sharedkey(void)
500 sctp_sharedkey_t *new_key;
502 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
504 if (new_key == NULL) {
514 sctp_free_sharedkey(sctp_sharedkey_t * skey)
517 if (skey->key != NULL)
518 sctp_free_key(skey->key);
524 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
526 sctp_sharedkey_t *skey;
528 LIST_FOREACH(skey, shared_keys, next) {
529 if (skey->keyid == key_id)
536 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
537 sctp_sharedkey_t * new_skey)
539 sctp_sharedkey_t *skey;
541 if ((shared_keys == NULL) || (new_skey == NULL))
544 /* insert into an empty list? */
545 if (SCTP_LIST_EMPTY(shared_keys)) {
546 LIST_INSERT_HEAD(shared_keys, new_skey, next);
549 /* insert into the existing list, ordered by key id */
550 LIST_FOREACH(skey, shared_keys, next) {
551 if (new_skey->keyid < skey->keyid) {
552 /* insert it before here */
553 LIST_INSERT_BEFORE(skey, new_skey, next);
555 } else if (new_skey->keyid == skey->keyid) {
556 /* replace the existing key */
559 printf("replacing shared key id %u\n", new_skey->keyid);
561 LIST_INSERT_BEFORE(skey, new_skey, next);
562 LIST_REMOVE(skey, next);
563 sctp_free_sharedkey(skey);
566 if (LIST_NEXT(skey, next) == NULL) {
567 /* belongs at the end of the list */
568 LIST_INSERT_AFTER(skey, new_skey, next);
574 static sctp_sharedkey_t *
575 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
577 sctp_sharedkey_t *new_skey;
581 new_skey = sctp_alloc_sharedkey();
582 if (new_skey == NULL)
584 if (skey->key != NULL)
585 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
587 new_skey->key = NULL;
588 new_skey->keyid = skey->keyid;
593 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
595 sctp_sharedkey_t *skey, *new_skey;
598 if ((src == NULL) || (dest == NULL))
600 LIST_FOREACH(skey, src, next) {
601 new_skey = sctp_copy_sharedkey(skey);
602 if (new_skey != NULL) {
603 sctp_insert_sharedkey(dest, new_skey);
612 sctp_alloc_hmaclist(uint8_t num_hmacs)
614 sctp_hmaclist_t *new_list;
617 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
618 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
620 if (new_list == NULL) {
624 new_list->max_algo = num_hmacs;
625 new_list->num_algo = 0;
630 sctp_free_hmaclist(sctp_hmaclist_t * list)
639 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
643 if (list->num_algo == list->max_algo) {
646 printf("SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
650 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
652 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
655 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
656 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
657 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
659 (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
664 printf("SCTP: add HMAC id %u to list\n", hmac_id);
666 list->hmac[list->num_algo++] = hmac_id;
671 sctp_copy_hmaclist(sctp_hmaclist_t * list)
673 sctp_hmaclist_t *new_list;
679 new_list = sctp_alloc_hmaclist(list->max_algo);
680 if (new_list == NULL)
683 new_list->max_algo = list->max_algo;
684 new_list->num_algo = list->num_algo;
685 for (i = 0; i < list->num_algo; i++)
686 new_list->hmac[i] = list->hmac[i];
691 sctp_default_supported_hmaclist(void)
693 sctp_hmaclist_t *new_list;
695 new_list = sctp_alloc_hmaclist(2);
696 if (new_list == NULL)
698 sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
699 sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
704 * HMAC algos are listed in priority/preference order find the best HMAC id
705 * to use for the peer based on local support
708 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
712 if ((local == NULL) || (peer == NULL))
713 return (SCTP_AUTH_HMAC_ID_RSVD);
715 for (i = 0; i < peer->num_algo; i++) {
716 for (j = 0; j < local->num_algo; j++) {
717 if (peer->hmac[i] == local->hmac[j]) {
718 #ifndef SCTP_AUTH_DRAFT_04
719 /* "skip" MD5 as it's been deprecated */
720 if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
724 /* found the "best" one */
727 printf("SCTP: negotiated peer HMAC id %u\n", peer->hmac[i]);
729 return (peer->hmac[i]);
733 /* didn't find one! */
734 return (SCTP_AUTH_HMAC_ID_RSVD);
738 * serialize the HMAC algo list and return space used caller must guarantee
739 * ptr has appropriate space
742 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
750 for (i = 0; i < list->num_algo; i++) {
751 hmac_id = htons(list->hmac[i]);
752 bcopy(&hmac_id, ptr, sizeof(hmac_id));
753 ptr += sizeof(hmac_id);
755 return (list->num_algo * sizeof(hmac_id));
759 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
763 uint32_t sha1_supported = 0;
765 for (i = 0; i < num_hmacs; i++) {
766 hmac_id = ntohs(hmacs->hmac_ids[i]);
767 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
770 /* all HMAC id's are supported */
771 if (sha1_supported == 0)
778 sctp_alloc_authinfo(void)
780 sctp_authinfo_t *new_authinfo;
782 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
784 if (new_authinfo == NULL) {
788 bzero(&new_authinfo, sizeof(*new_authinfo));
789 return (new_authinfo);
793 sctp_free_authinfo(sctp_authinfo_t * authinfo)
795 if (authinfo == NULL)
798 if (authinfo->random != NULL)
799 sctp_free_key(authinfo->random);
800 if (authinfo->peer_random != NULL)
801 sctp_free_key(authinfo->peer_random);
802 if (authinfo->assoc_key != NULL)
803 sctp_free_key(authinfo->assoc_key);
804 if (authinfo->recv_key != NULL)
805 sctp_free_key(authinfo->recv_key);
807 /* We are NOT dynamically allocating authinfo's right now... */
808 /* SCTP_FREE(authinfo); */
813 sctp_get_auth_chunk_len(uint16_t hmac_algo)
817 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
818 return (SCTP_SIZE32(size));
822 sctp_get_hmac_digest_len(uint16_t hmac_algo)
825 case SCTP_AUTH_HMAC_ID_SHA1:
826 return (SCTP_AUTH_DIGEST_LEN_SHA1);
827 case SCTP_AUTH_HMAC_ID_MD5:
828 return (SCTP_AUTH_DIGEST_LEN_MD5);
830 case SCTP_AUTH_HMAC_ID_SHA224:
831 return (SCTP_AUTH_DIGEST_LEN_SHA224);
834 case SCTP_AUTH_HMAC_ID_SHA256:
835 return (SCTP_AUTH_DIGEST_LEN_SHA256);
836 case SCTP_AUTH_HMAC_ID_SHA384:
837 return (SCTP_AUTH_DIGEST_LEN_SHA384);
838 case SCTP_AUTH_HMAC_ID_SHA512:
839 return (SCTP_AUTH_DIGEST_LEN_SHA512);
842 /* unknown HMAC algorithm: can't do anything */
848 sctp_get_hmac_block_len(uint16_t hmac_algo)
851 case SCTP_AUTH_HMAC_ID_SHA1:
852 case SCTP_AUTH_HMAC_ID_MD5:
854 case SCTP_AUTH_HMAC_ID_SHA224:
858 case SCTP_AUTH_HMAC_ID_SHA256:
860 case SCTP_AUTH_HMAC_ID_SHA384:
861 case SCTP_AUTH_HMAC_ID_SHA512:
864 case SCTP_AUTH_HMAC_ID_RSVD:
866 /* unknown HMAC algorithm: can't do anything */
872 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
875 case SCTP_AUTH_HMAC_ID_SHA1:
876 SHA1_Init(&ctx->sha1);
878 case SCTP_AUTH_HMAC_ID_MD5:
882 case SCTP_AUTH_HMAC_ID_SHA224:
886 case SCTP_AUTH_HMAC_ID_SHA256:
887 SHA256_Init(&ctx->sha256);
889 case SCTP_AUTH_HMAC_ID_SHA384:
890 SHA384_Init(&ctx->sha384);
892 case SCTP_AUTH_HMAC_ID_SHA512:
893 SHA512_Init(&ctx->sha512);
896 case SCTP_AUTH_HMAC_ID_RSVD:
898 /* unknown HMAC algorithm: can't do anything */
904 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
905 uint8_t * text, uint32_t textlen)
908 case SCTP_AUTH_HMAC_ID_SHA1:
909 SHA1_Update(&ctx->sha1, text, textlen);
911 case SCTP_AUTH_HMAC_ID_MD5:
912 MD5_Update(&ctx->md5, text, textlen);
915 case SCTP_AUTH_HMAC_ID_SHA224:
919 case SCTP_AUTH_HMAC_ID_SHA256:
920 SHA256_Update(&ctx->sha256, text, textlen);
922 case SCTP_AUTH_HMAC_ID_SHA384:
923 SHA384_Update(&ctx->sha384, text, textlen);
925 case SCTP_AUTH_HMAC_ID_SHA512:
926 SHA512_Update(&ctx->sha512, text, textlen);
929 case SCTP_AUTH_HMAC_ID_RSVD:
931 /* unknown HMAC algorithm: can't do anything */
937 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
941 case SCTP_AUTH_HMAC_ID_SHA1:
942 SHA1_Final(digest, &ctx->sha1);
944 case SCTP_AUTH_HMAC_ID_MD5:
945 MD5_Final(digest, &ctx->md5);
948 case SCTP_AUTH_HMAC_ID_SHA224:
952 case SCTP_AUTH_HMAC_ID_SHA256:
953 SHA256_Final(digest, &ctx->sha256);
955 case SCTP_AUTH_HMAC_ID_SHA384:
956 /* SHA384 is truncated SHA512 */
957 SHA384_Final(digest, &ctx->sha384);
959 case SCTP_AUTH_HMAC_ID_SHA512:
960 SHA512_Final(digest, &ctx->sha512);
963 case SCTP_AUTH_HMAC_ID_RSVD:
965 /* unknown HMAC algorithm: can't do anything */
971 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
973 * Compute the HMAC digest using the desired hash key, text, and HMAC
974 * algorithm. Resulting digest is placed in 'digest' and digest length
975 * is returned, if the HMAC was performed.
977 * WARNING: it is up to the caller to supply sufficient space to hold the
981 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
982 uint8_t * text, uint32_t textlen, uint8_t * digest)
986 sctp_hash_context_t ctx;
987 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
988 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
991 /* sanity check the material and length */
992 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
993 (textlen == 0) || (digest == NULL)) {
994 /* can't do HMAC with empty key or text or digest store */
997 /* validate the hmac algo and get the digest length */
998 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1002 /* hash the key if it is longer than the hash block size */
1003 blocklen = sctp_get_hmac_block_len(hmac_algo);
1004 if (keylen > blocklen) {
1005 sctp_hmac_init(hmac_algo, &ctx);
1006 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1007 sctp_hmac_final(hmac_algo, &ctx, temp);
1008 /* set the hashed key as the key */
1012 /* initialize the inner/outer pads with the key and "append" zeroes */
1013 bzero(ipad, blocklen);
1014 bzero(opad, blocklen);
1015 bcopy(key, ipad, keylen);
1016 bcopy(key, opad, keylen);
1018 /* XOR the key with ipad and opad values */
1019 for (i = 0; i < blocklen; i++) {
1024 /* perform inner hash */
1025 sctp_hmac_init(hmac_algo, &ctx);
1026 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1027 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1028 sctp_hmac_final(hmac_algo, &ctx, temp);
1030 /* perform outer hash */
1031 sctp_hmac_init(hmac_algo, &ctx);
1032 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1033 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1034 sctp_hmac_final(hmac_algo, &ctx, digest);
1041 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1042 struct mbuf *m, uint32_t m_offset, uint8_t * digest)
1046 sctp_hash_context_t ctx;
1047 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1048 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1052 /* sanity check the material and length */
1053 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1054 /* can't do HMAC with empty key or text or digest store */
1057 /* validate the hmac algo and get the digest length */
1058 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1062 /* hash the key if it is longer than the hash block size */
1063 blocklen = sctp_get_hmac_block_len(hmac_algo);
1064 if (keylen > blocklen) {
1065 sctp_hmac_init(hmac_algo, &ctx);
1066 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1067 sctp_hmac_final(hmac_algo, &ctx, temp);
1068 /* set the hashed key as the key */
1072 /* initialize the inner/outer pads with the key and "append" zeroes */
1073 bzero(ipad, blocklen);
1074 bzero(opad, blocklen);
1075 bcopy(key, ipad, keylen);
1076 bcopy(key, opad, keylen);
1078 /* XOR the key with ipad and opad values */
1079 for (i = 0; i < blocklen; i++) {
1084 /* perform inner hash */
1085 sctp_hmac_init(hmac_algo, &ctx);
1086 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1087 /* find the correct starting mbuf and offset (get start of text) */
1089 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1090 m_offset -= SCTP_BUF_LEN(m_tmp);
1091 m_tmp = SCTP_BUF_NEXT(m_tmp);
1093 /* now use the rest of the mbuf chain for the text */
1094 while (m_tmp != NULL) {
1095 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1096 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));
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 SCTP_INP_RLOCK(stcb->sctp_ep);
1357 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1362 /* that key doesn't exist */
1364 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1367 /* get the shared key text */
1370 /* free any existing cached key */
1371 if (stcb->asoc.authinfo.assoc_key != NULL)
1372 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1373 /* compute a new assoc key and cache it */
1374 stcb->asoc.authinfo.assoc_key =
1375 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1376 stcb->asoc.authinfo.peer_random, key);
1377 stcb->asoc.authinfo.assoc_keyid = keyid;
1379 if (SCTP_AUTH_DEBUG)
1380 sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1384 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1389 * set the active key on an endpoint ASSUMES INP_WLOCK is already held
1392 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1394 sctp_sharedkey_t *skey;
1397 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1399 /* that key doesn't exist */
1402 inp->sctp_ep.default_keyid = keyid;
1407 * get local authentication parameters from cookie (from INIT-ACK)
1410 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1411 uint32_t offset, uint32_t length)
1413 struct sctp_paramhdr *phdr, tmp_param;
1414 uint16_t plen, ptype;
1415 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1416 struct sctp_auth_random *random = NULL;
1417 uint16_t random_len = 0;
1418 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1419 struct sctp_auth_hmac_algo *hmacs = NULL;
1420 uint16_t hmacs_len = 0;
1421 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1422 struct sctp_auth_chunk_list *chunks = NULL;
1423 uint16_t num_chunks = 0;
1424 sctp_key_t *new_key;
1427 /* convert to upper bound */
1430 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1431 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1432 while (phdr != NULL) {
1433 ptype = ntohs(phdr->param_type);
1434 plen = ntohs(phdr->param_length);
1436 if ((plen == 0) || (offset + plen > length))
1439 if (ptype == SCTP_RANDOM) {
1440 if (plen > sizeof(random_store))
1442 phdr = sctp_get_next_param(m, offset,
1443 (struct sctp_paramhdr *)random_store, plen);
1446 /* save the random and length for the key */
1447 random = (struct sctp_auth_random *)phdr;
1448 random_len = plen - sizeof(*random);
1449 } else if (ptype == SCTP_HMAC_LIST) {
1453 if (plen > sizeof(hmacs_store))
1455 phdr = sctp_get_next_param(m, offset,
1456 (struct sctp_paramhdr *)hmacs_store, plen);
1459 /* save the hmacs list and num for the key */
1460 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1461 hmacs_len = plen - sizeof(*hmacs);
1462 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1463 if (stcb->asoc.local_hmacs != NULL)
1464 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1465 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1466 if (stcb->asoc.local_hmacs != NULL) {
1467 for (i = 0; i < num_hmacs; i++) {
1468 sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1469 ntohs(hmacs->hmac_ids[i]));
1472 } else if (ptype == SCTP_CHUNK_LIST) {
1475 if (plen > sizeof(chunks_store))
1477 phdr = sctp_get_next_param(m, offset,
1478 (struct sctp_paramhdr *)chunks_store, plen);
1481 chunks = (struct sctp_auth_chunk_list *)phdr;
1482 num_chunks = plen - sizeof(*chunks);
1483 /* save chunks list and num for the key */
1484 if (stcb->asoc.local_auth_chunks != NULL)
1485 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1487 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1488 for (i = 0; i < num_chunks; i++) {
1489 sctp_auth_add_chunk(chunks->chunk_types[i],
1490 stcb->asoc.local_auth_chunks);
1493 /* get next parameter */
1494 offset += SCTP_SIZE32(plen);
1495 if (offset + sizeof(struct sctp_paramhdr) > length)
1497 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1498 (uint8_t *) & tmp_param);
1500 /* concatenate the full random key */
1501 #ifdef SCTP_AUTH_DRAFT_04
1502 keylen = random_len;
1503 new_key = sctp_alloc_key(keylen);
1504 if (new_key != NULL) {
1505 /* copy in the RANDOM */
1507 bcopy(random->random_data, new_key->key, random_len);
1510 keylen = sizeof(*random) + random_len + sizeof(*chunks) + num_chunks +
1511 sizeof(*hmacs) + hmacs_len;
1512 new_key = sctp_alloc_key(keylen);
1513 if (new_key != NULL) {
1514 /* copy in the RANDOM */
1515 if (random != NULL) {
1516 keylen = sizeof(*random) + random_len;
1517 bcopy(random, new_key->key, keylen);
1519 /* append in the AUTH chunks */
1520 if (chunks != NULL) {
1521 bcopy(chunks, new_key->key + keylen,
1522 sizeof(*chunks) + num_chunks);
1523 keylen += sizeof(*chunks) + num_chunks;
1525 /* append in the HMACs */
1526 if (hmacs != NULL) {
1527 bcopy(hmacs, new_key->key + keylen,
1528 sizeof(*hmacs) + hmacs_len);
1532 if (stcb->asoc.authinfo.random != NULL)
1533 sctp_free_key(stcb->asoc.authinfo.random);
1534 stcb->asoc.authinfo.random = new_key;
1535 stcb->asoc.authinfo.random_len = random_len;
1536 #ifdef SCTP_AUTH_DRAFT_04
1537 /* don't include the chunks and hmacs for draft -04 */
1538 stcb->asoc.authinfo.random->keylen = random_len;
1540 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1541 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1543 /* negotiate what HMAC to use for the peer */
1544 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1545 stcb->asoc.local_hmacs);
1546 /* copy defaults from the endpoint */
1547 /* FIX ME: put in cookie? */
1548 stcb->asoc.authinfo.assoc_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1552 * compute and fill in the HMAC digest for a packet
1555 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1556 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb)
1559 sctp_sharedkey_t *skey;
1562 if ((stcb == NULL) || (auth == NULL))
1565 /* zero the digest + chunk padding */
1566 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1567 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1568 /* is an assoc key cached? */
1569 if (stcb->asoc.authinfo.assoc_key == NULL) {
1570 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1571 stcb->asoc.authinfo.assoc_keyid);
1573 /* not in the assoc list, so check the endpoint list */
1574 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1575 stcb->asoc.authinfo.assoc_keyid);
1577 /* the only way skey is NULL is if null key id 0 is used */
1582 /* compute a new assoc key and cache it */
1583 stcb->asoc.authinfo.assoc_key =
1584 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1585 stcb->asoc.authinfo.peer_random, key);
1587 if (SCTP_AUTH_DEBUG) {
1588 printf("caching key id %u\n",
1589 stcb->asoc.authinfo.assoc_keyid);
1590 sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1594 /* set in the active key id */
1595 auth->shared_key_id = htons(stcb->asoc.authinfo.assoc_keyid);
1597 /* compute and fill in the digest */
1598 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id,
1599 stcb->asoc.authinfo.assoc_key,
1600 m, auth_offset, auth->hmac);
1605 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1614 /* find the correct starting mbuf and offset (get start position) */
1616 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1617 m_offset -= SCTP_BUF_LEN(m_tmp);
1618 m_tmp = SCTP_BUF_NEXT(m_tmp);
1620 /* now use the rest of the mbuf chain */
1621 while ((m_tmp != NULL) && (size > 0)) {
1622 data = mtod(m_tmp, uint8_t *) + m_offset;
1623 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1624 bzero(data, SCTP_BUF_LEN(m_tmp));
1625 size -= SCTP_BUF_LEN(m_tmp);
1630 /* clear the offset since it's only for the first mbuf */
1632 m_tmp = SCTP_BUF_NEXT(m_tmp);
1637 * process the incoming Authentication chunk return codes: -1 on any
1638 * authentication error 0 on authentication verification
1641 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1642 struct mbuf *m, uint32_t offset)
1645 uint16_t shared_key_id;
1647 sctp_sharedkey_t *skey;
1649 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1650 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1652 /* auth is checked for NULL by caller */
1653 chunklen = ntohs(auth->ch.chunk_length);
1654 if (chunklen < sizeof(*auth)) {
1655 SCTP_STAT_INCR(sctps_recvauthfailed);
1658 SCTP_STAT_INCR(sctps_recvauth);
1660 /* get the auth params */
1661 shared_key_id = ntohs(auth->shared_key_id);
1662 hmac_id = ntohs(auth->hmac_id);
1664 if (SCTP_AUTH_DEBUG)
1665 printf("SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1666 shared_key_id, hmac_id);
1669 /* is the indicated HMAC supported? */
1670 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1672 struct sctp_auth_invalid_hmac *err;
1674 SCTP_STAT_INCR(sctps_recvivalhmacid);
1676 if (SCTP_AUTH_DEBUG)
1677 printf("SCTP Auth: unsupported HMAC id %u\n", hmac_id);
1680 * report this in an Error Chunk: Unsupported HMAC
1683 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT, 1, MT_HEADER);
1684 if (m_err != NULL) {
1685 /* pre-reserve some space */
1686 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1687 /* fill in the error */
1688 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1689 bzero(err, sizeof(*err));
1690 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1691 err->ph.param_length = htons(sizeof(*err));
1692 err->hmac_id = ntohs(hmac_id);
1693 SCTP_BUF_LEN(m_err) = sizeof(*err);
1695 sctp_queue_op_err(stcb, m_err);
1699 /* get the indicated shared key, if available */
1700 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1701 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1702 /* find the shared key on the assoc first */
1703 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, shared_key_id);
1705 /* if not on the assoc, find it on the endpoint */
1706 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1709 /* if the shared key isn't found, discard the chunk */
1711 SCTP_STAT_INCR(sctps_recvivalkeyid);
1713 if (SCTP_AUTH_DEBUG)
1714 printf("SCTP Auth: unknown key id %u\n",
1719 /* generate a notification if this is a new key id */
1720 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1722 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1723 * shared_key_id, (void
1724 * *)stcb->asoc.authinfo.recv_keyid);
1726 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1727 shared_key_id, stcb->asoc.authinfo.recv_keyid);
1728 /* compute a new recv assoc key and cache it */
1729 if (stcb->asoc.authinfo.recv_key != NULL)
1730 sctp_free_key(stcb->asoc.authinfo.recv_key);
1731 stcb->asoc.authinfo.recv_key =
1732 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1733 stcb->asoc.authinfo.peer_random, skey->key);
1734 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1736 if (SCTP_AUTH_DEBUG)
1737 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1740 /* validate the digest length */
1741 digestlen = sctp_get_hmac_digest_len(hmac_id);
1742 if (chunklen < (sizeof(*auth) + digestlen)) {
1743 /* invalid digest length */
1744 SCTP_STAT_INCR(sctps_recvauthfailed);
1746 if (SCTP_AUTH_DEBUG)
1747 printf("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);
1761 if (SCTP_AUTH_DEBUG)
1762 printf("SCTP Auth: HMAC digest check failed\n");
1770 * Generate NOTIFICATION
1773 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1774 uint16_t keyid, uint16_t alt_keyid)
1776 struct mbuf *m_notify;
1777 struct sctp_authkey_event *auth;
1778 struct sctp_queued_to_read *control;
1780 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1781 /* event not enabled */
1784 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1785 0, M_DONTWAIT, 1, MT_HEADER);
1786 if (m_notify == NULL)
1790 SCTP_BUF_LEN(m_notify) = 0;
1791 auth = mtod(m_notify, struct sctp_authkey_event *);
1792 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1793 auth->auth_flags = 0;
1794 auth->auth_length = sizeof(*auth);
1795 auth->auth_keynumber = keyid;
1796 auth->auth_altkeynumber = alt_keyid;
1797 auth->auth_indication = indication;
1798 auth->auth_assoc_id = sctp_get_associd(stcb);
1800 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1801 SCTP_BUF_NEXT(m_notify) = NULL;
1803 /* append to socket */
1804 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1805 0, 0, 0, 0, 0, 0, m_notify);
1806 if (control == NULL) {
1808 sctp_m_freem(m_notify);
1811 control->spec_flags = M_NOTIFICATION;
1812 control->length = SCTP_BUF_LEN(m_notify);
1813 /* not that we need this */
1814 control->tail_mbuf = m_notify;
1815 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1816 &stcb->sctp_socket->so_rcv, 1);
1821 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1822 * Note: currently only used for INIT as INIT-ACK is handled inline
1823 * with sctp_load_addresses_from_init()
1826 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1828 struct sctp_paramhdr *phdr, parm_buf;
1829 uint16_t ptype, plen;
1830 int peer_supports_asconf = 0;
1831 int peer_supports_auth = 0;
1832 int got_random = 0, got_hmacs = 0;
1834 /* go through each of the params. */
1835 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1837 ptype = ntohs(phdr->param_type);
1838 plen = ntohs(phdr->param_length);
1840 if (offset + plen > limit) {
1846 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1847 /* A supported extension chunk */
1848 struct sctp_supported_chunk_types_param *pr_supported;
1849 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1852 phdr = sctp_get_next_param(m, offset,
1853 (struct sctp_paramhdr *)&local_store, plen);
1857 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1858 num_ent = plen - sizeof(struct sctp_paramhdr);
1859 for (i = 0; i < num_ent; i++) {
1860 switch (pr_supported->chunk_types[i]) {
1862 case SCTP_ASCONF_ACK:
1863 peer_supports_asconf = 1;
1865 case SCTP_AUTHENTICATION:
1866 peer_supports_auth = 1;
1869 /* one we don't care about */
1873 } else if (ptype == SCTP_RANDOM) {
1875 /* enforce the random length */
1876 if (plen != (sizeof(struct sctp_auth_random) +
1877 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1879 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1880 printf("SCTP: invalid RANDOM len\n");
1884 } else if (ptype == SCTP_HMAC_LIST) {
1885 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1886 struct sctp_auth_hmac_algo *hmacs;
1889 if (plen > sizeof(store))
1891 phdr = sctp_get_next_param(m, offset,
1892 (struct sctp_paramhdr *)store, plen);
1895 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1896 num_hmacs = (plen - sizeof(*hmacs)) /
1897 sizeof(hmacs->hmac_ids[0]);
1898 /* validate the hmac list */
1899 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1901 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1902 printf("SCTP: invalid HMAC param\n");
1908 offset += SCTP_SIZE32(plen);
1909 if (offset >= limit) {
1912 phdr = sctp_get_next_param(m, offset, &parm_buf,
1915 /* validate authentication required parameters */
1916 if (got_random && got_hmacs) {
1917 peer_supports_auth = 1;
1919 peer_supports_auth = 0;
1921 if (!sctp_asconf_auth_nochk && peer_supports_asconf &&
1922 !peer_supports_auth) {
1924 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1925 printf("SCTP: peer supports ASCONF but not AUTH\n");
1933 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1935 uint16_t chunks_len = 0;
1936 uint16_t hmacs_len = 0;
1937 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1938 sctp_key_t *new_key;
1941 /* initialize hmac list from endpoint */
1942 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1943 if (stcb->asoc.local_hmacs != NULL) {
1944 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1945 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1947 /* initialize auth chunks list from endpoint */
1948 stcb->asoc.local_auth_chunks =
1949 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1950 if (stcb->asoc.local_auth_chunks != NULL) {
1953 for (i = 0; i < 256; i++) {
1954 if (stcb->asoc.local_auth_chunks->chunks[i])
1958 /* copy defaults from the endpoint */
1959 stcb->asoc.authinfo.assoc_keyid = inp->sctp_ep.default_keyid;
1961 /* now set the concatenated key (random + chunks + hmacs) */
1962 #ifdef SCTP_AUTH_DRAFT_04
1963 /* don't include the chunks and hmacs for draft -04 */
1964 keylen = random_len;
1965 new_key = sctp_generate_random_key(keylen);
1967 /* key includes parameter headers */
1968 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
1970 new_key = sctp_alloc_key(keylen);
1971 if (new_key != NULL) {
1972 struct sctp_paramhdr *ph;
1975 /* generate and copy in the RANDOM */
1976 ph = (struct sctp_paramhdr *)new_key->key;
1977 ph->param_type = htons(SCTP_RANDOM);
1978 plen = sizeof(*ph) + random_len;
1979 ph->param_length = htons(plen);
1980 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
1983 /* append in the AUTH chunks */
1984 /* NOTE: currently we always have chunks to list */
1985 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
1986 ph->param_type = htons(SCTP_CHUNK_LIST);
1987 plen = sizeof(*ph) + chunks_len;
1988 ph->param_length = htons(plen);
1989 keylen += sizeof(*ph);
1990 if (stcb->asoc.local_auth_chunks) {
1993 for (i = 0; i < 256; i++) {
1994 if (stcb->asoc.local_auth_chunks->chunks[i])
1995 new_key->key[keylen++] = i;
1998 /* append in the HMACs */
1999 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2000 ph->param_type = htons(SCTP_HMAC_LIST);
2001 plen = sizeof(*ph) + hmacs_len;
2002 ph->param_length = htons(plen);
2003 keylen += sizeof(*ph);
2004 sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2005 new_key->key + keylen);
2008 if (stcb->asoc.authinfo.random != NULL)
2009 sctp_free_key(stcb->asoc.authinfo.random);
2010 stcb->asoc.authinfo.random = new_key;
2011 stcb->asoc.authinfo.random_len = random_len;
2015 #ifdef SCTP_HMAC_TEST
2017 * HMAC and key concatenation tests
2020 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2024 printf("\n%s: 0x", str);
2028 for (i = 0; i < digestlen; i++)
2029 printf("%02x", digest[i]);
2033 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2034 uint32_t keylen, uint8_t * text, uint32_t textlen,
2035 uint8_t * digest, uint32_t digestlen)
2037 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2039 printf("\n%s:", str);
2040 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2041 sctp_print_digest(digest, digestlen, "Expected digest");
2042 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2043 if (memcmp(digest, computed_digest, digestlen) != 0) {
2054 * RFC 2202: HMAC-SHA1 test cases
2057 sctp_test_hmac_sha1(void)
2064 uint32_t digestlen = 20;
2068 * test_case = 1 key =
2069 * 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b key_len = 20
2070 * data = "Hi There" data_len = 8 digest =
2071 * 0xb617318655057264e28bc0b6fb378c8ef146be00
2074 memset(key, 0x0b, keylen);
2076 strcpy(text, "Hi There");
2077 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2078 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2079 text, textlen, digest, digestlen) < 0)
2083 * test_case = 2 key = "Jefe" key_len = 4 data =
2084 * "what do ya want for nothing?" data_len = 28 digest =
2085 * 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2088 strcpy(key, "Jefe");
2090 strcpy(text, "what do ya want for nothing?");
2091 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2092 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2093 text, textlen, digest, digestlen) < 0)
2097 * test_case = 3 key =
2098 * 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa key_len = 20
2099 * data = 0xdd repeated 50 times data_len = 50 digest
2100 * = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2103 memset(key, 0xaa, keylen);
2105 memset(text, 0xdd, textlen);
2106 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2107 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2108 text, textlen, digest, digestlen) < 0)
2112 * test_case = 4 key =
2113 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2114 * data = 0xcd repeated 50 times data_len = 50 digest
2115 * = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2118 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);
2120 memset(text, 0xcd, textlen);
2121 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2122 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2123 text, textlen, digest, digestlen) < 0)
2127 * test_case = 5 key =
2128 * 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c key_len = 20
2129 * data = "Test With Truncation" data_len = 20 digest
2130 * = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 digest-96 =
2131 * 0x4c1a03424b55e07fe7f27be1
2134 memset(key, 0x0c, keylen);
2136 strcpy(text, "Test With Truncation");
2137 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2138 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2139 text, textlen, digest, digestlen) < 0)
2143 * test_case = 6 key = 0xaa repeated 80 times key_len
2144 * = 80 data = "Test Using Larger Than Block-Size Key -
2145 * Hash Key First" data_len = 54 digest =
2146 * 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2149 memset(key, 0xaa, keylen);
2151 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2152 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2153 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2154 text, textlen, digest, digestlen) < 0)
2158 * test_case = 7 key = 0xaa repeated 80 times key_len
2159 * = 80 data = "Test Using Larger Than Block-Size Key and
2160 * Larger Than One Block-Size Data" data_len = 73 digest =
2161 * 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2164 memset(key, 0xaa, keylen);
2166 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2167 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2168 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2169 text, textlen, digest, digestlen) < 0)
2172 /* done with all tests */
2174 printf("\nSHA1 test results: %d cases failed", failed);
2176 printf("\nSHA1 test results: all test cases passed");
2180 * RFC 2202: HMAC-MD5 test cases
2183 sctp_test_hmac_md5(void)
2190 uint32_t digestlen = 16;
2194 * test_case = 1 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2195 * key_len = 16 data = "Hi There" data_len = 8 digest =
2196 * 0x9294727a3638bb1c13f48ef8158bfc9d
2199 memset(key, 0x0b, keylen);
2201 strcpy(text, "Hi There");
2202 digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2203 if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2204 text, textlen, digest, digestlen) < 0)
2208 * test_case = 2 key = "Jefe" key_len = 4 data =
2209 * "what do ya want for nothing?" data_len = 28 digest =
2210 * 0x750c783e6ab0b503eaa86e310a5db738
2213 strcpy(key, "Jefe");
2215 strcpy(text, "what do ya want for nothing?");
2216 digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2217 if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2218 text, textlen, digest, digestlen) < 0)
2222 * test_case = 3 key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2223 * key_len = 16 data = 0xdd repeated 50 times data_len = 50
2224 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2227 memset(key, 0xaa, keylen);
2229 memset(text, 0xdd, textlen);
2230 digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2231 if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2232 text, textlen, digest, digestlen) < 0)
2236 * test_case = 4 key =
2237 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2238 * data = 0xcd repeated 50 times data_len = 50 digest
2239 * = 0x697eaf0aca3a3aea3a75164746ffaa79
2242 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);
2244 memset(text, 0xcd, textlen);
2245 digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2246 if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2247 text, textlen, digest, digestlen) < 0)
2251 * test_case = 5 key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2252 * key_len = 16 data = "Test With Truncation" data_len = 20
2253 * digest = 0x56461ef2342edc00f9bab995690efd4c digest-96
2254 * 0x56461ef2342edc00f9bab995
2257 memset(key, 0x0c, keylen);
2259 strcpy(text, "Test With Truncation");
2260 digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2261 if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2262 text, textlen, digest, digestlen) < 0)
2266 * test_case = 6 key = 0xaa repeated 80 times key_len
2267 * = 80 data = "Test Using Larger Than Block-Size Key -
2268 * Hash Key First" data_len = 54 digest =
2269 * 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2272 memset(key, 0xaa, keylen);
2274 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2275 digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2276 if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2277 text, textlen, digest, digestlen) < 0)
2281 * test_case = 7 key = 0xaa repeated 80 times key_len
2282 * = 80 data = "Test Using Larger Than Block-Size Key and
2283 * Larger Than One Block-Size Data" data_len = 73 digest =
2284 * 0x6f630fad67cda0ee1fb1f562db3aa53e
2287 memset(key, 0xaa, keylen);
2289 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2290 digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2291 if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2292 text, textlen, digest, digestlen) < 0)
2295 /* done with all tests */
2297 printf("\nMD5 test results: %d cases failed", failed);
2299 printf("\nMD5 test results: all test cases passed");
2303 * test assoc key concatenation
2306 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2307 sctp_key_t * expected_key)
2312 sctp_show_key(key1, "\nkey1");
2313 sctp_show_key(key2, "\nkey2");
2314 key = sctp_compute_hashkey(key1, key2, NULL);
2315 sctp_show_key(expected_key, "\nExpected");
2316 sctp_show_key(key, "\nComputed");
2317 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2324 sctp_free_key(key1);
2325 sctp_free_key(key2);
2326 sctp_free_key(expected_key);
2333 sctp_test_authkey(void)
2335 sctp_key_t *key1, *key2, *expected_key;
2339 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2340 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2341 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2342 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2346 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2347 key2 = sctp_set_key("\x02", 1);
2348 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2349 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2353 key1 = sctp_set_key("\x01", 1);
2354 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2355 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2356 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2360 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2361 key2 = sctp_set_key("\x01", 1);
2362 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2363 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2367 key1 = sctp_set_key("\x01", 1);
2368 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2369 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2370 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2374 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2375 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2376 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);
2377 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2381 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2382 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2383 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);
2384 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2387 /* done with all tests */
2389 printf("\nKey concatenation test results: %d cases failed", failed);
2391 printf("\nKey concatenation test results: all test cases passed");
2395 #if defined(STANDALONE_HMAC_TEST)
2399 sctp_test_hmac_sha1();
2400 sctp_test_hmac_md5();
2401 sctp_test_authkey();
2404 #endif /* STANDALONE_HMAC_TEST */
2406 #endif /* SCTP_HMAC_TEST */