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 #ifdef SCTP_AUTH_DRAFT_04
458 /* key is key1 + shared + key2 */
459 if (sctp_get_keylen(key1)) {
460 bcopy(key1->key, key_ptr, key1->keylen);
461 key_ptr += key1->keylen;
463 if (sctp_get_keylen(shared)) {
464 bcopy(shared->key, key_ptr, shared->keylen);
465 key_ptr += shared->keylen;
467 if (sctp_get_keylen(key2)) {
468 bcopy(key2->key, key_ptr, key2->keylen);
469 key_ptr += key2->keylen;
472 /* key is shared + key1 + key2 */
473 if (sctp_get_keylen(shared)) {
474 bcopy(shared->key, key_ptr, shared->keylen);
475 key_ptr += shared->keylen;
477 if (sctp_get_keylen(key1)) {
478 bcopy(key1->key, key_ptr, key1->keylen);
479 key_ptr += key1->keylen;
481 if (sctp_get_keylen(key2)) {
482 bcopy(key2->key, key_ptr, key2->keylen);
483 key_ptr += key2->keylen;
487 #ifdef SCTP_AUTH_DRAFT_04
488 /* key is key2 + shared + key1 */
489 if (sctp_get_keylen(key2)) {
490 bcopy(key2->key, key_ptr, key2->keylen);
491 key_ptr += key2->keylen;
493 if (sctp_get_keylen(shared)) {
494 bcopy(shared->key, key_ptr, shared->keylen);
495 key_ptr += shared->keylen;
497 if (sctp_get_keylen(key1)) {
498 bcopy(key1->key, key_ptr, key1->keylen);
499 key_ptr += key1->keylen;
502 /* key is shared + key2 + key1 */
503 if (sctp_get_keylen(shared)) {
504 bcopy(shared->key, key_ptr, shared->keylen);
505 key_ptr += shared->keylen;
507 if (sctp_get_keylen(key2)) {
508 bcopy(key2->key, key_ptr, key2->keylen);
509 key_ptr += key2->keylen;
511 if (sctp_get_keylen(key1)) {
512 bcopy(key1->key, key_ptr, key1->keylen);
513 key_ptr += key1->keylen;
522 sctp_alloc_sharedkey(void)
524 sctp_sharedkey_t *new_key;
526 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
528 if (new_key == NULL) {
538 sctp_free_sharedkey(sctp_sharedkey_t * skey)
541 if (skey->key != NULL)
542 sctp_free_key(skey->key);
543 SCTP_FREE(skey, SCTP_M_AUTH_KY);
548 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
550 sctp_sharedkey_t *skey;
552 LIST_FOREACH(skey, shared_keys, next) {
553 if (skey->keyid == key_id)
560 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
561 sctp_sharedkey_t * new_skey)
563 sctp_sharedkey_t *skey;
565 if ((shared_keys == NULL) || (new_skey == NULL))
568 /* insert into an empty list? */
569 if (SCTP_LIST_EMPTY(shared_keys)) {
570 LIST_INSERT_HEAD(shared_keys, new_skey, next);
573 /* insert into the existing list, ordered by key id */
574 LIST_FOREACH(skey, shared_keys, next) {
575 if (new_skey->keyid < skey->keyid) {
576 /* insert it before here */
577 LIST_INSERT_BEFORE(skey, new_skey, next);
579 } else if (new_skey->keyid == skey->keyid) {
580 /* replace the existing key */
581 SCTPDBG(SCTP_DEBUG_AUTH1,
582 "replacing shared key id %u\n",
584 LIST_INSERT_BEFORE(skey, new_skey, next);
585 LIST_REMOVE(skey, next);
586 sctp_free_sharedkey(skey);
589 if (LIST_NEXT(skey, next) == NULL) {
590 /* belongs at the end of the list */
591 LIST_INSERT_AFTER(skey, new_skey, next);
597 static sctp_sharedkey_t *
598 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
600 sctp_sharedkey_t *new_skey;
604 new_skey = sctp_alloc_sharedkey();
605 if (new_skey == NULL)
607 if (skey->key != NULL)
608 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
610 new_skey->key = NULL;
611 new_skey->keyid = skey->keyid;
616 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
618 sctp_sharedkey_t *skey, *new_skey;
621 if ((src == NULL) || (dest == NULL))
623 LIST_FOREACH(skey, src, next) {
624 new_skey = sctp_copy_sharedkey(skey);
625 if (new_skey != NULL) {
626 sctp_insert_sharedkey(dest, new_skey);
635 sctp_alloc_hmaclist(uint8_t num_hmacs)
637 sctp_hmaclist_t *new_list;
640 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
641 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
643 if (new_list == NULL) {
647 new_list->max_algo = num_hmacs;
648 new_list->num_algo = 0;
653 sctp_free_hmaclist(sctp_hmaclist_t * list)
656 SCTP_FREE(list, SCTP_M_AUTH_HL);
662 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
668 if (list->num_algo == list->max_algo) {
669 SCTPDBG(SCTP_DEBUG_AUTH1,
670 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
673 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
675 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
678 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
679 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
680 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
682 (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
685 /* Now is it already in the list */
686 for (i = 0; i < list->num_algo; i++) {
687 if (list->hmac[i] == hmac_id) {
688 /* already in list */
692 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
693 list->hmac[list->num_algo++] = hmac_id;
698 sctp_copy_hmaclist(sctp_hmaclist_t * list)
700 sctp_hmaclist_t *new_list;
706 new_list = sctp_alloc_hmaclist(list->max_algo);
707 if (new_list == NULL)
710 new_list->max_algo = list->max_algo;
711 new_list->num_algo = list->num_algo;
712 for (i = 0; i < list->num_algo; i++)
713 new_list->hmac[i] = list->hmac[i];
718 sctp_default_supported_hmaclist(void)
720 sctp_hmaclist_t *new_list;
722 new_list = sctp_alloc_hmaclist(2);
723 if (new_list == NULL)
725 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
726 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
731 * HMAC algos are listed in priority/preference order find the best HMAC id
732 * to use for the peer based on local support
735 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
739 if ((local == NULL) || (peer == NULL))
740 return (SCTP_AUTH_HMAC_ID_RSVD);
742 for (i = 0; i < peer->num_algo; i++) {
743 for (j = 0; j < local->num_algo; j++) {
744 if (peer->hmac[i] == local->hmac[j]) {
745 #ifndef SCTP_AUTH_DRAFT_04
746 /* "skip" MD5 as it's been deprecated */
747 if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
751 /* found the "best" one */
752 SCTPDBG(SCTP_DEBUG_AUTH1,
753 "SCTP: negotiated peer HMAC id %u\n",
755 return (peer->hmac[i]);
759 /* didn't find one! */
760 return (SCTP_AUTH_HMAC_ID_RSVD);
764 * serialize the HMAC algo list and return space used caller must guarantee
765 * ptr has appropriate space
768 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
776 for (i = 0; i < list->num_algo; i++) {
777 hmac_id = htons(list->hmac[i]);
778 bcopy(&hmac_id, ptr, sizeof(hmac_id));
779 ptr += sizeof(hmac_id);
781 return (list->num_algo * sizeof(hmac_id));
785 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
789 uint32_t sha1_supported = 0;
791 for (i = 0; i < num_hmacs; i++) {
792 hmac_id = ntohs(hmacs->hmac_ids[i]);
793 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
796 /* all HMAC id's are supported */
797 if (sha1_supported == 0)
804 sctp_alloc_authinfo(void)
806 sctp_authinfo_t *new_authinfo;
808 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
811 if (new_authinfo == NULL) {
815 bzero(new_authinfo, sizeof(*new_authinfo));
816 return (new_authinfo);
820 sctp_free_authinfo(sctp_authinfo_t * authinfo)
822 if (authinfo == NULL)
825 if (authinfo->random != NULL)
826 sctp_free_key(authinfo->random);
827 if (authinfo->peer_random != NULL)
828 sctp_free_key(authinfo->peer_random);
829 if (authinfo->assoc_key != NULL)
830 sctp_free_key(authinfo->assoc_key);
831 if (authinfo->recv_key != NULL)
832 sctp_free_key(authinfo->recv_key);
834 /* We are NOT dynamically allocating authinfo's right now... */
835 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
840 sctp_get_auth_chunk_len(uint16_t hmac_algo)
844 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
845 return (SCTP_SIZE32(size));
849 sctp_get_hmac_digest_len(uint16_t hmac_algo)
852 case SCTP_AUTH_HMAC_ID_SHA1:
853 return (SCTP_AUTH_DIGEST_LEN_SHA1);
854 case SCTP_AUTH_HMAC_ID_MD5:
855 return (SCTP_AUTH_DIGEST_LEN_MD5);
857 case SCTP_AUTH_HMAC_ID_SHA224:
858 return (SCTP_AUTH_DIGEST_LEN_SHA224);
861 case SCTP_AUTH_HMAC_ID_SHA256:
862 return (SCTP_AUTH_DIGEST_LEN_SHA256);
863 case SCTP_AUTH_HMAC_ID_SHA384:
864 return (SCTP_AUTH_DIGEST_LEN_SHA384);
865 case SCTP_AUTH_HMAC_ID_SHA512:
866 return (SCTP_AUTH_DIGEST_LEN_SHA512);
869 /* unknown HMAC algorithm: can't do anything */
875 sctp_get_hmac_block_len(uint16_t hmac_algo)
878 case SCTP_AUTH_HMAC_ID_SHA1:
879 case SCTP_AUTH_HMAC_ID_MD5:
881 case SCTP_AUTH_HMAC_ID_SHA224:
885 case SCTP_AUTH_HMAC_ID_SHA256:
887 case SCTP_AUTH_HMAC_ID_SHA384:
888 case SCTP_AUTH_HMAC_ID_SHA512:
891 case SCTP_AUTH_HMAC_ID_RSVD:
893 /* unknown HMAC algorithm: can't do anything */
899 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
902 case SCTP_AUTH_HMAC_ID_SHA1:
903 SHA1_Init(&ctx->sha1);
905 case SCTP_AUTH_HMAC_ID_MD5:
909 case SCTP_AUTH_HMAC_ID_SHA224:
913 case SCTP_AUTH_HMAC_ID_SHA256:
914 SHA256_Init(&ctx->sha256);
916 case SCTP_AUTH_HMAC_ID_SHA384:
917 SHA384_Init(&ctx->sha384);
919 case SCTP_AUTH_HMAC_ID_SHA512:
920 SHA512_Init(&ctx->sha512);
923 case SCTP_AUTH_HMAC_ID_RSVD:
925 /* unknown HMAC algorithm: can't do anything */
931 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
932 uint8_t * text, uint32_t textlen)
935 case SCTP_AUTH_HMAC_ID_SHA1:
936 SHA1_Update(&ctx->sha1, text, textlen);
938 case SCTP_AUTH_HMAC_ID_MD5:
939 MD5_Update(&ctx->md5, text, textlen);
942 case SCTP_AUTH_HMAC_ID_SHA224:
946 case SCTP_AUTH_HMAC_ID_SHA256:
947 SHA256_Update(&ctx->sha256, text, textlen);
949 case SCTP_AUTH_HMAC_ID_SHA384:
950 SHA384_Update(&ctx->sha384, text, textlen);
952 case SCTP_AUTH_HMAC_ID_SHA512:
953 SHA512_Update(&ctx->sha512, text, textlen);
956 case SCTP_AUTH_HMAC_ID_RSVD:
958 /* unknown HMAC algorithm: can't do anything */
964 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
968 case SCTP_AUTH_HMAC_ID_SHA1:
969 SHA1_Final(digest, &ctx->sha1);
971 case SCTP_AUTH_HMAC_ID_MD5:
972 MD5_Final(digest, &ctx->md5);
975 case SCTP_AUTH_HMAC_ID_SHA224:
979 case SCTP_AUTH_HMAC_ID_SHA256:
980 SHA256_Final(digest, &ctx->sha256);
982 case SCTP_AUTH_HMAC_ID_SHA384:
983 /* SHA384 is truncated SHA512 */
984 SHA384_Final(digest, &ctx->sha384);
986 case SCTP_AUTH_HMAC_ID_SHA512:
987 SHA512_Final(digest, &ctx->sha512);
990 case SCTP_AUTH_HMAC_ID_RSVD:
992 /* unknown HMAC algorithm: can't do anything */
998 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
1000 * Compute the HMAC digest using the desired hash key, text, and HMAC
1001 * algorithm. Resulting digest is placed in 'digest' and digest length
1002 * is returned, if the HMAC was performed.
1004 * WARNING: it is up to the caller to supply sufficient space to hold the
1008 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1009 uint8_t * text, uint32_t textlen, uint8_t * digest)
1013 sctp_hash_context_t ctx;
1014 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1015 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1018 /* sanity check the material and length */
1019 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
1020 (textlen == 0) || (digest == NULL)) {
1021 /* can't do HMAC with empty key or text or digest store */
1024 /* validate the hmac algo and get the digest length */
1025 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1029 /* hash the key if it is longer than the hash block size */
1030 blocklen = sctp_get_hmac_block_len(hmac_algo);
1031 if (keylen > blocklen) {
1032 sctp_hmac_init(hmac_algo, &ctx);
1033 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1034 sctp_hmac_final(hmac_algo, &ctx, temp);
1035 /* set the hashed key as the key */
1039 /* initialize the inner/outer pads with the key and "append" zeroes */
1040 bzero(ipad, blocklen);
1041 bzero(opad, blocklen);
1042 bcopy(key, ipad, keylen);
1043 bcopy(key, opad, keylen);
1045 /* XOR the key with ipad and opad values */
1046 for (i = 0; i < blocklen; i++) {
1051 /* perform inner hash */
1052 sctp_hmac_init(hmac_algo, &ctx);
1053 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1054 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1055 sctp_hmac_final(hmac_algo, &ctx, temp);
1057 /* perform outer hash */
1058 sctp_hmac_init(hmac_algo, &ctx);
1059 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1060 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1061 sctp_hmac_final(hmac_algo, &ctx, digest);
1068 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1069 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
1073 sctp_hash_context_t ctx;
1074 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1075 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1079 /* sanity check the material and length */
1080 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1081 /* can't do HMAC with empty key or text or digest store */
1084 /* validate the hmac algo and get the digest length */
1085 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1089 /* hash the key if it is longer than the hash block size */
1090 blocklen = sctp_get_hmac_block_len(hmac_algo);
1091 if (keylen > blocklen) {
1092 sctp_hmac_init(hmac_algo, &ctx);
1093 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1094 sctp_hmac_final(hmac_algo, &ctx, temp);
1095 /* set the hashed key as the key */
1099 /* initialize the inner/outer pads with the key and "append" zeroes */
1100 bzero(ipad, blocklen);
1101 bzero(opad, blocklen);
1102 bcopy(key, ipad, keylen);
1103 bcopy(key, opad, keylen);
1105 /* XOR the key with ipad and opad values */
1106 for (i = 0; i < blocklen; i++) {
1111 /* perform inner hash */
1112 sctp_hmac_init(hmac_algo, &ctx);
1113 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1114 /* find the correct starting mbuf and offset (get start of text) */
1116 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1117 m_offset -= SCTP_BUF_LEN(m_tmp);
1118 m_tmp = SCTP_BUF_NEXT(m_tmp);
1120 /* now use the rest of the mbuf chain for the text */
1121 while (m_tmp != NULL) {
1122 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1123 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1124 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1126 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1127 SCTP_BUF_LEN(m_tmp) - m_offset);
1130 /* clear the offset since it's only for the first mbuf */
1132 m_tmp = SCTP_BUF_NEXT(m_tmp);
1134 sctp_hmac_final(hmac_algo, &ctx, temp);
1136 /* perform outer hash */
1137 sctp_hmac_init(hmac_algo, &ctx);
1138 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1139 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1140 sctp_hmac_final(hmac_algo, &ctx, digest);
1146 * verify the HMAC digest using the desired hash key, text, and HMAC
1147 * algorithm. Returns -1 on error, 0 on success.
1150 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1151 uint8_t * text, uint32_t textlen,
1152 uint8_t * digest, uint32_t digestlen)
1155 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1157 /* sanity check the material and length */
1158 if ((key == NULL) || (keylen == 0) ||
1159 (text == NULL) || (textlen == 0) || (digest == NULL)) {
1160 /* can't do HMAC with empty key or text or digest */
1163 len = sctp_get_hmac_digest_len(hmac_algo);
1164 if ((len == 0) || (digestlen != len))
1167 /* compute the expected hash */
1168 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1171 if (memcmp(digest, temp, digestlen) != 0)
1179 * computes the requested HMAC using a key struct (which may be modified if
1180 * the keylen exceeds the HMAC block len).
1183 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1184 uint32_t textlen, uint8_t * digest)
1188 sctp_hash_context_t ctx;
1189 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1192 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1194 /* can't do HMAC with empty key or text or digest store */
1197 /* validate the hmac algo and get the digest length */
1198 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1202 /* hash the key if it is longer than the hash block size */
1203 blocklen = sctp_get_hmac_block_len(hmac_algo);
1204 if (key->keylen > blocklen) {
1205 sctp_hmac_init(hmac_algo, &ctx);
1206 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1207 sctp_hmac_final(hmac_algo, &ctx, temp);
1208 /* save the hashed key as the new key */
1209 key->keylen = digestlen;
1210 bcopy(temp, key->key, key->keylen);
1212 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1218 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1219 uint32_t m_offset, uint8_t * digest)
1223 sctp_hash_context_t ctx;
1224 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1227 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1228 /* can't do HMAC with empty key or text or digest store */
1231 /* validate the hmac algo and get the digest length */
1232 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1236 /* hash the key if it is longer than the hash block size */
1237 blocklen = sctp_get_hmac_block_len(hmac_algo);
1238 if (key->keylen > blocklen) {
1239 sctp_hmac_init(hmac_algo, &ctx);
1240 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1241 sctp_hmac_final(hmac_algo, &ctx, temp);
1242 /* save the hashed key as the new key */
1243 key->keylen = digestlen;
1244 bcopy(temp, key->key, key->keylen);
1246 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1250 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1254 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1257 for (i = 0; i < list->num_algo; i++)
1258 if (list->hmac[i] == id)
1261 /* not in the list */
1267 * clear any cached key(s) if they match the given key id on an association
1268 * the cached key(s) will be recomputed and re-cached at next use. ASSUMES
1269 * TCB_LOCK is already held
1272 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1277 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1278 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1279 stcb->asoc.authinfo.assoc_key = NULL;
1281 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1282 sctp_free_key(stcb->asoc.authinfo.recv_key);
1283 stcb->asoc.authinfo.recv_key = NULL;
1288 * clear any cached key(s) if they match the given key id for all assocs on
1289 * an association ASSUMES INP_WLOCK is already held
1292 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1294 struct sctp_tcb *stcb;
1299 /* clear the cached keys on all assocs on this instance */
1300 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1301 SCTP_TCB_LOCK(stcb);
1302 sctp_clear_cachedkeys(stcb, keyid);
1303 SCTP_TCB_UNLOCK(stcb);
1308 * delete a shared key from an association ASSUMES TCB_LOCK is already held
1311 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1313 sctp_sharedkey_t *skey;
1318 /* is the keyid the assoc active sending key */
1319 if (keyid == stcb->asoc.authinfo.assoc_keyid)
1322 /* does the key exist? */
1323 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1328 LIST_REMOVE(skey, next);
1329 sctp_free_sharedkey(skey); /* frees skey->key as well */
1331 /* clear any cached keys */
1332 sctp_clear_cachedkeys(stcb, keyid);
1337 * deletes a shared key from the endpoint ASSUMES INP_WLOCK is already held
1340 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1342 sctp_sharedkey_t *skey;
1343 struct sctp_tcb *stcb;
1348 /* is the keyid the active sending key on the endpoint or any assoc */
1349 if (keyid == inp->sctp_ep.default_keyid)
1351 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1352 SCTP_TCB_LOCK(stcb);
1353 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1354 SCTP_TCB_UNLOCK(stcb);
1357 SCTP_TCB_UNLOCK(stcb);
1360 /* does the key exist? */
1361 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1366 LIST_REMOVE(skey, next);
1367 sctp_free_sharedkey(skey); /* frees skey->key as well */
1369 /* clear any cached keys */
1370 sctp_clear_cachedkeys_ep(inp, keyid);
1375 * set the active key on an association ASSUME TCB_LOCK is already held
1378 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1380 sctp_sharedkey_t *skey = NULL;
1381 sctp_key_t *key = NULL;
1382 int using_ep_key = 0;
1384 /* find the key on the assoc */
1385 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1387 /* if not on the assoc, find the key on the endpoint */
1388 atomic_add_int(&stcb->asoc.refcnt, 1);
1389 SCTP_TCB_UNLOCK(stcb);
1390 SCTP_INP_RLOCK(stcb->sctp_ep);
1391 SCTP_TCB_LOCK(stcb);
1392 atomic_add_int(&stcb->asoc.refcnt, -1);
1393 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1398 /* that key doesn't exist */
1400 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1404 /* get the shared key text */
1407 /* free any existing cached key */
1408 if (stcb->asoc.authinfo.assoc_key != NULL)
1409 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1410 /* compute a new assoc key and cache it */
1411 stcb->asoc.authinfo.assoc_key =
1412 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1413 stcb->asoc.authinfo.peer_random, key);
1414 stcb->asoc.authinfo.assoc_keyid = keyid;
1416 if (SCTP_AUTH_DEBUG)
1417 sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1421 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1427 * set the active key on an endpoint ASSUMES INP_WLOCK is already held
1430 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1432 sctp_sharedkey_t *skey;
1435 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1437 /* that key doesn't exist */
1440 inp->sctp_ep.default_keyid = keyid;
1445 * get local authentication parameters from cookie (from INIT-ACK)
1448 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1449 uint32_t offset, uint32_t length)
1451 struct sctp_paramhdr *phdr, tmp_param;
1452 uint16_t plen, ptype;
1453 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1454 struct sctp_auth_random *p_random = NULL;
1455 uint16_t random_len = 0;
1456 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1457 struct sctp_auth_hmac_algo *hmacs = NULL;
1458 uint16_t hmacs_len = 0;
1459 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1460 struct sctp_auth_chunk_list *chunks = NULL;
1461 uint16_t num_chunks = 0;
1462 sctp_key_t *new_key;
1465 /* convert to upper bound */
1468 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1469 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1470 while (phdr != NULL) {
1471 ptype = ntohs(phdr->param_type);
1472 plen = ntohs(phdr->param_length);
1474 if ((plen == 0) || (offset + plen > length))
1477 if (ptype == SCTP_RANDOM) {
1478 if (plen > sizeof(random_store))
1480 phdr = sctp_get_next_param(m, offset,
1481 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1484 /* save the random and length for the key */
1485 p_random = (struct sctp_auth_random *)phdr;
1486 random_len = plen - sizeof(*p_random);
1487 } else if (ptype == SCTP_HMAC_LIST) {
1491 if (plen > sizeof(hmacs_store))
1493 phdr = sctp_get_next_param(m, offset,
1494 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
1497 /* save the hmacs list and num for the key */
1498 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1499 hmacs_len = plen - sizeof(*hmacs);
1500 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1501 if (stcb->asoc.local_hmacs != NULL)
1502 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1503 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1504 if (stcb->asoc.local_hmacs != NULL) {
1505 for (i = 0; i < num_hmacs; i++) {
1506 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1507 ntohs(hmacs->hmac_ids[i]));
1510 } else if (ptype == SCTP_CHUNK_LIST) {
1513 if (plen > sizeof(chunks_store))
1515 phdr = sctp_get_next_param(m, offset,
1516 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
1519 chunks = (struct sctp_auth_chunk_list *)phdr;
1520 num_chunks = plen - sizeof(*chunks);
1521 /* save chunks list and num for the key */
1522 if (stcb->asoc.local_auth_chunks != NULL)
1523 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1525 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1526 for (i = 0; i < num_chunks; i++) {
1527 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1528 stcb->asoc.local_auth_chunks);
1531 /* get next parameter */
1532 offset += SCTP_SIZE32(plen);
1533 if (offset + sizeof(struct sctp_paramhdr) > length)
1535 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1536 (uint8_t *) & tmp_param);
1538 /* concatenate the full random key */
1539 #ifdef SCTP_AUTH_DRAFT_04
1540 keylen = random_len;
1541 new_key = sctp_alloc_key(keylen);
1542 if (new_key != NULL) {
1543 /* copy in the RANDOM */
1544 if (p_random != NULL)
1545 bcopy(p_random->random_data, new_key->key, random_len);
1548 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
1549 sizeof(*hmacs) + hmacs_len;
1550 new_key = sctp_alloc_key(keylen);
1551 if (new_key != NULL) {
1552 /* copy in the RANDOM */
1553 if (p_random != NULL) {
1554 keylen = sizeof(*p_random) + random_len;
1555 bcopy(p_random, new_key->key, keylen);
1557 /* append in the AUTH chunks */
1558 if (chunks != NULL) {
1559 bcopy(chunks, new_key->key + keylen,
1560 sizeof(*chunks) + num_chunks);
1561 keylen += sizeof(*chunks) + num_chunks;
1563 /* append in the HMACs */
1564 if (hmacs != NULL) {
1565 bcopy(hmacs, new_key->key + keylen,
1566 sizeof(*hmacs) + hmacs_len);
1570 if (stcb->asoc.authinfo.random != NULL)
1571 sctp_free_key(stcb->asoc.authinfo.random);
1572 stcb->asoc.authinfo.random = new_key;
1573 stcb->asoc.authinfo.random_len = random_len;
1574 #ifdef SCTP_AUTH_DRAFT_04
1575 /* don't include the chunks and hmacs for draft -04 */
1576 stcb->asoc.authinfo.random->keylen = random_len;
1578 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1579 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1581 /* negotiate what HMAC to use for the peer */
1582 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1583 stcb->asoc.local_hmacs);
1584 /* copy defaults from the endpoint */
1585 /* FIX ME: put in cookie? */
1586 stcb->asoc.authinfo.assoc_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1590 * compute and fill in the HMAC digest for a packet
1593 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1594 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb)
1597 sctp_sharedkey_t *skey;
1600 if ((stcb == NULL) || (auth == NULL))
1603 /* zero the digest + chunk padding */
1604 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1605 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1606 /* is an assoc key cached? */
1607 if (stcb->asoc.authinfo.assoc_key == NULL) {
1608 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1609 stcb->asoc.authinfo.assoc_keyid);
1611 /* not in the assoc list, so check the endpoint list */
1612 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1613 stcb->asoc.authinfo.assoc_keyid);
1615 /* the only way skey is NULL is if null key id 0 is used */
1620 /* compute a new assoc key and cache it */
1621 stcb->asoc.authinfo.assoc_key =
1622 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1623 stcb->asoc.authinfo.peer_random, key);
1624 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1625 stcb->asoc.authinfo.assoc_keyid);
1627 if (SCTP_AUTH_DEBUG)
1628 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1632 /* set in the active key id */
1633 auth->shared_key_id = htons(stcb->asoc.authinfo.assoc_keyid);
1635 /* compute and fill in the digest */
1636 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id,
1637 stcb->asoc.authinfo.assoc_key,
1638 m, auth_offset, auth->hmac);
1643 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1652 /* find the correct starting mbuf and offset (get start position) */
1654 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1655 m_offset -= SCTP_BUF_LEN(m_tmp);
1656 m_tmp = SCTP_BUF_NEXT(m_tmp);
1658 /* now use the rest of the mbuf chain */
1659 while ((m_tmp != NULL) && (size > 0)) {
1660 data = mtod(m_tmp, uint8_t *) + m_offset;
1661 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1662 bzero(data, SCTP_BUF_LEN(m_tmp));
1663 size -= SCTP_BUF_LEN(m_tmp);
1668 /* clear the offset since it's only for the first mbuf */
1670 m_tmp = SCTP_BUF_NEXT(m_tmp);
1675 * process the incoming Authentication chunk return codes: -1 on any
1676 * authentication error 0 on authentication verification
1679 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1680 struct mbuf *m, uint32_t offset)
1683 uint16_t shared_key_id;
1685 sctp_sharedkey_t *skey;
1687 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1688 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1690 /* auth is checked for NULL by caller */
1691 chunklen = ntohs(auth->ch.chunk_length);
1692 if (chunklen < sizeof(*auth)) {
1693 SCTP_STAT_INCR(sctps_recvauthfailed);
1696 SCTP_STAT_INCR(sctps_recvauth);
1698 /* get the auth params */
1699 shared_key_id = ntohs(auth->shared_key_id);
1700 hmac_id = ntohs(auth->hmac_id);
1701 SCTPDBG(SCTP_DEBUG_AUTH1,
1702 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1703 shared_key_id, hmac_id);
1705 /* is the indicated HMAC supported? */
1706 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1708 struct sctp_auth_invalid_hmac *err;
1710 SCTP_STAT_INCR(sctps_recvivalhmacid);
1711 SCTPDBG(SCTP_DEBUG_AUTH1,
1712 "SCTP Auth: unsupported HMAC id %u\n",
1715 * report this in an Error Chunk: Unsupported HMAC
1718 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
1720 if (m_err != NULL) {
1721 /* pre-reserve some space */
1722 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1723 /* fill in the error */
1724 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1725 bzero(err, sizeof(*err));
1726 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1727 err->ph.param_length = htons(sizeof(*err));
1728 err->hmac_id = ntohs(hmac_id);
1729 SCTP_BUF_LEN(m_err) = sizeof(*err);
1731 sctp_queue_op_err(stcb, m_err);
1735 /* get the indicated shared key, if available */
1736 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1737 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1738 /* find the shared key on the assoc first */
1739 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, shared_key_id);
1741 /* if not on the assoc, find it on the endpoint */
1742 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1745 /* if the shared key isn't found, discard the chunk */
1747 SCTP_STAT_INCR(sctps_recvivalkeyid);
1748 SCTPDBG(SCTP_DEBUG_AUTH1,
1749 "SCTP Auth: unknown key id %u\n",
1753 /* generate a notification if this is a new key id */
1754 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1756 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1757 * shared_key_id, (void
1758 * *)stcb->asoc.authinfo.recv_keyid);
1760 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1761 shared_key_id, stcb->asoc.authinfo.recv_keyid);
1762 /* compute a new recv assoc key and cache it */
1763 if (stcb->asoc.authinfo.recv_key != NULL)
1764 sctp_free_key(stcb->asoc.authinfo.recv_key);
1765 stcb->asoc.authinfo.recv_key =
1766 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1767 stcb->asoc.authinfo.peer_random, skey->key);
1768 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1770 if (SCTP_AUTH_DEBUG)
1771 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1774 /* validate the digest length */
1775 digestlen = sctp_get_hmac_digest_len(hmac_id);
1776 if (chunklen < (sizeof(*auth) + digestlen)) {
1777 /* invalid digest length */
1778 SCTP_STAT_INCR(sctps_recvauthfailed);
1779 SCTPDBG(SCTP_DEBUG_AUTH1,
1780 "SCTP Auth: chunk too short for HMAC\n");
1783 /* save a copy of the digest, zero the pseudo header, and validate */
1784 bcopy(auth->hmac, digest, digestlen);
1785 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1786 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1787 m, offset, computed_digest);
1789 /* compare the computed digest with the one in the AUTH chunk */
1790 if (memcmp(digest, computed_digest, digestlen) != 0) {
1791 SCTP_STAT_INCR(sctps_recvauthfailed);
1792 SCTPDBG(SCTP_DEBUG_AUTH1,
1793 "SCTP Auth: HMAC digest check failed\n");
1800 * Generate NOTIFICATION
1803 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1804 uint16_t keyid, uint16_t alt_keyid)
1806 struct mbuf *m_notify;
1807 struct sctp_authkey_event *auth;
1808 struct sctp_queued_to_read *control;
1810 if ((stcb == NULL) ||
1811 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1812 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1813 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1815 /* If the socket is gone we are out of here */
1818 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1819 /* event not enabled */
1822 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1823 0, M_DONTWAIT, 1, MT_HEADER);
1824 if (m_notify == NULL)
1828 SCTP_BUF_LEN(m_notify) = 0;
1829 auth = mtod(m_notify, struct sctp_authkey_event *);
1830 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1831 auth->auth_flags = 0;
1832 auth->auth_length = sizeof(*auth);
1833 auth->auth_keynumber = keyid;
1834 auth->auth_altkeynumber = alt_keyid;
1835 auth->auth_indication = indication;
1836 auth->auth_assoc_id = sctp_get_associd(stcb);
1838 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1839 SCTP_BUF_NEXT(m_notify) = NULL;
1841 /* append to socket */
1842 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1843 0, 0, 0, 0, 0, 0, m_notify);
1844 if (control == NULL) {
1846 sctp_m_freem(m_notify);
1849 control->spec_flags = M_NOTIFICATION;
1850 control->length = SCTP_BUF_LEN(m_notify);
1851 /* not that we need this */
1852 control->tail_mbuf = m_notify;
1853 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1854 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1859 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1860 * Note: currently only used for INIT as INIT-ACK is handled inline
1861 * with sctp_load_addresses_from_init()
1864 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1866 struct sctp_paramhdr *phdr, parm_buf;
1867 uint16_t ptype, plen;
1868 int peer_supports_asconf = 0;
1869 int peer_supports_auth = 0;
1870 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1871 uint8_t saw_asconf = 0;
1872 uint8_t saw_asconf_ack = 0;
1874 /* go through each of the params. */
1875 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1877 ptype = ntohs(phdr->param_type);
1878 plen = ntohs(phdr->param_length);
1880 if (offset + plen > limit) {
1883 if (plen < sizeof(struct sctp_paramhdr)) {
1886 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1887 /* A supported extension chunk */
1888 struct sctp_supported_chunk_types_param *pr_supported;
1889 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1892 phdr = sctp_get_next_param(m, offset,
1893 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
1897 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1898 num_ent = plen - sizeof(struct sctp_paramhdr);
1899 for (i = 0; i < num_ent; i++) {
1900 switch (pr_supported->chunk_types[i]) {
1902 case SCTP_ASCONF_ACK:
1903 peer_supports_asconf = 1;
1905 case SCTP_AUTHENTICATION:
1906 peer_supports_auth = 1;
1909 /* one we don't care about */
1913 } else if (ptype == SCTP_RANDOM) {
1915 /* enforce the random length */
1916 if (plen != (sizeof(struct sctp_auth_random) +
1917 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1918 SCTPDBG(SCTP_DEBUG_AUTH1,
1919 "SCTP: invalid RANDOM len\n");
1922 } else if (ptype == SCTP_HMAC_LIST) {
1923 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1924 struct sctp_auth_hmac_algo *hmacs;
1927 if (plen > sizeof(store))
1929 phdr = sctp_get_next_param(m, offset,
1930 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
1933 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1934 num_hmacs = (plen - sizeof(*hmacs)) /
1935 sizeof(hmacs->hmac_ids[0]);
1936 /* validate the hmac list */
1937 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1938 SCTPDBG(SCTP_DEBUG_AUTH1,
1939 "SCTP: invalid HMAC param\n");
1943 } else if (ptype == SCTP_CHUNK_LIST) {
1945 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1947 /* did the peer send a non-empty chunk list? */
1948 struct sctp_auth_chunk_list *chunks = NULL;
1950 phdr = sctp_get_next_param(m, offset,
1951 (struct sctp_paramhdr *)chunks_store,
1952 min(plen, sizeof(chunks_store)));
1957 * Flip through the list and mark that the
1958 * peer supports asconf/asconf_ack.
1960 chunks = (struct sctp_auth_chunk_list *)phdr;
1961 num_chunks = plen - sizeof(*chunks);
1962 for (i = 0; i < num_chunks; i++) {
1963 /* record asconf/asconf-ack if listed */
1964 if (chunks->chunk_types[i] == SCTP_ASCONF)
1966 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1973 offset += SCTP_SIZE32(plen);
1974 if (offset >= limit) {
1977 phdr = sctp_get_next_param(m, offset, &parm_buf,
1980 /* validate authentication required parameters */
1981 if (got_random && got_hmacs) {
1982 peer_supports_auth = 1;
1984 peer_supports_auth = 0;
1986 if (!peer_supports_auth && got_chklist) {
1987 SCTPDBG(SCTP_DEBUG_AUTH1,
1988 "SCTP: peer sent chunk list w/o AUTH\n");
1991 if (!sctp_asconf_auth_nochk && peer_supports_asconf &&
1992 !peer_supports_auth) {
1993 SCTPDBG(SCTP_DEBUG_AUTH1,
1994 "SCTP: peer supports ASCONF but not AUTH\n");
1996 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
1997 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
2004 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2006 uint16_t chunks_len = 0;
2007 uint16_t hmacs_len = 0;
2008 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
2009 sctp_key_t *new_key;
2012 /* initialize hmac list from endpoint */
2013 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
2014 if (stcb->asoc.local_hmacs != NULL) {
2015 hmacs_len = stcb->asoc.local_hmacs->num_algo *
2016 sizeof(stcb->asoc.local_hmacs->hmac[0]);
2018 /* initialize auth chunks list from endpoint */
2019 stcb->asoc.local_auth_chunks =
2020 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
2021 if (stcb->asoc.local_auth_chunks != NULL) {
2024 for (i = 0; i < 256; i++) {
2025 if (stcb->asoc.local_auth_chunks->chunks[i])
2029 /* copy defaults from the endpoint */
2030 stcb->asoc.authinfo.assoc_keyid = inp->sctp_ep.default_keyid;
2032 /* now set the concatenated key (random + chunks + hmacs) */
2033 #ifdef SCTP_AUTH_DRAFT_04
2034 /* don't include the chunks and hmacs for draft -04 */
2035 keylen = random_len;
2036 new_key = sctp_generate_random_key(keylen);
2038 /* key includes parameter headers */
2039 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2041 new_key = sctp_alloc_key(keylen);
2042 if (new_key != NULL) {
2043 struct sctp_paramhdr *ph;
2046 /* generate and copy in the RANDOM */
2047 ph = (struct sctp_paramhdr *)new_key->key;
2048 ph->param_type = htons(SCTP_RANDOM);
2049 plen = sizeof(*ph) + random_len;
2050 ph->param_length = htons(plen);
2051 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2054 /* append in the AUTH chunks */
2055 /* NOTE: currently we always have chunks to list */
2056 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2057 ph->param_type = htons(SCTP_CHUNK_LIST);
2058 plen = sizeof(*ph) + chunks_len;
2059 ph->param_length = htons(plen);
2060 keylen += sizeof(*ph);
2061 if (stcb->asoc.local_auth_chunks) {
2064 for (i = 0; i < 256; i++) {
2065 if (stcb->asoc.local_auth_chunks->chunks[i])
2066 new_key->key[keylen++] = i;
2069 /* append in the HMACs */
2070 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2071 ph->param_type = htons(SCTP_HMAC_LIST);
2072 plen = sizeof(*ph) + hmacs_len;
2073 ph->param_length = htons(plen);
2074 keylen += sizeof(*ph);
2075 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2076 new_key->key + keylen);
2079 if (stcb->asoc.authinfo.random != NULL)
2080 sctp_free_key(stcb->asoc.authinfo.random);
2081 stcb->asoc.authinfo.random = new_key;
2082 stcb->asoc.authinfo.random_len = random_len;
2086 #ifdef SCTP_HMAC_TEST
2088 * HMAC and key concatenation tests
2091 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2095 printf("\n%s: 0x", str);
2099 for (i = 0; i < digestlen; i++)
2100 printf("%02x", digest[i]);
2104 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2105 uint32_t keylen, uint8_t * text, uint32_t textlen,
2106 uint8_t * digest, uint32_t digestlen)
2108 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2110 printf("\n%s:", str);
2111 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2112 sctp_print_digest(digest, digestlen, "Expected digest");
2113 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2114 if (memcmp(digest, computed_digest, digestlen) != 0) {
2125 * RFC 2202: HMAC-SHA1 test cases
2128 sctp_test_hmac_sha1(void)
2135 uint32_t digestlen = 20;
2139 * test_case = 1 key =
2140 * 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b key_len = 20
2141 * data = "Hi There" data_len = 8 digest =
2142 * 0xb617318655057264e28bc0b6fb378c8ef146be00
2145 memset(key, 0x0b, keylen);
2147 strcpy(text, "Hi There");
2148 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2149 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2150 text, textlen, digest, digestlen) < 0)
2154 * test_case = 2 key = "Jefe" key_len = 4 data =
2155 * "what do ya want for nothing?" data_len = 28 digest =
2156 * 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2159 strcpy(key, "Jefe");
2161 strcpy(text, "what do ya want for nothing?");
2162 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2163 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2164 text, textlen, digest, digestlen) < 0)
2168 * test_case = 3 key =
2169 * 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa key_len = 20
2170 * data = 0xdd repeated 50 times data_len = 50 digest
2171 * = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2174 memset(key, 0xaa, keylen);
2176 memset(text, 0xdd, textlen);
2177 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2178 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2179 text, textlen, digest, digestlen) < 0)
2183 * test_case = 4 key =
2184 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2185 * data = 0xcd repeated 50 times data_len = 50 digest
2186 * = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2189 memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2191 memset(text, 0xcd, textlen);
2192 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2193 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2194 text, textlen, digest, digestlen) < 0)
2198 * test_case = 5 key =
2199 * 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c key_len = 20
2200 * data = "Test With Truncation" data_len = 20 digest
2201 * = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 digest-96 =
2202 * 0x4c1a03424b55e07fe7f27be1
2205 memset(key, 0x0c, keylen);
2207 strcpy(text, "Test With Truncation");
2208 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2209 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2210 text, textlen, digest, digestlen) < 0)
2214 * test_case = 6 key = 0xaa repeated 80 times key_len
2215 * = 80 data = "Test Using Larger Than Block-Size Key -
2216 * Hash Key First" data_len = 54 digest =
2217 * 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2220 memset(key, 0xaa, keylen);
2222 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2223 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2224 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2225 text, textlen, digest, digestlen) < 0)
2229 * test_case = 7 key = 0xaa repeated 80 times key_len
2230 * = 80 data = "Test Using Larger Than Block-Size Key and
2231 * Larger Than One Block-Size Data" data_len = 73 digest =
2232 * 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2235 memset(key, 0xaa, keylen);
2237 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2238 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2239 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2240 text, textlen, digest, digestlen) < 0)
2243 /* done with all tests */
2245 printf("\nSHA1 test results: %d cases failed", failed);
2247 printf("\nSHA1 test results: all test cases passed");
2251 * RFC 2202: HMAC-MD5 test cases
2254 sctp_test_hmac_md5(void)
2261 uint32_t digestlen = 16;
2265 * test_case = 1 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2266 * key_len = 16 data = "Hi There" data_len = 8 digest =
2267 * 0x9294727a3638bb1c13f48ef8158bfc9d
2270 memset(key, 0x0b, keylen);
2272 strcpy(text, "Hi There");
2273 digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2274 if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2275 text, textlen, digest, digestlen) < 0)
2279 * test_case = 2 key = "Jefe" key_len = 4 data =
2280 * "what do ya want for nothing?" data_len = 28 digest =
2281 * 0x750c783e6ab0b503eaa86e310a5db738
2284 strcpy(key, "Jefe");
2286 strcpy(text, "what do ya want for nothing?");
2287 digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2288 if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2289 text, textlen, digest, digestlen) < 0)
2293 * test_case = 3 key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2294 * key_len = 16 data = 0xdd repeated 50 times data_len = 50
2295 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2298 memset(key, 0xaa, keylen);
2300 memset(text, 0xdd, textlen);
2301 digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2302 if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2303 text, textlen, digest, digestlen) < 0)
2307 * test_case = 4 key =
2308 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2309 * data = 0xcd repeated 50 times data_len = 50 digest
2310 * = 0x697eaf0aca3a3aea3a75164746ffaa79
2313 memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2315 memset(text, 0xcd, textlen);
2316 digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2317 if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2318 text, textlen, digest, digestlen) < 0)
2322 * test_case = 5 key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2323 * key_len = 16 data = "Test With Truncation" data_len = 20
2324 * digest = 0x56461ef2342edc00f9bab995690efd4c digest-96
2325 * 0x56461ef2342edc00f9bab995
2328 memset(key, 0x0c, keylen);
2330 strcpy(text, "Test With Truncation");
2331 digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2332 if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2333 text, textlen, digest, digestlen) < 0)
2337 * test_case = 6 key = 0xaa repeated 80 times key_len
2338 * = 80 data = "Test Using Larger Than Block-Size Key -
2339 * Hash Key First" data_len = 54 digest =
2340 * 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2343 memset(key, 0xaa, keylen);
2345 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2346 digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2347 if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2348 text, textlen, digest, digestlen) < 0)
2352 * test_case = 7 key = 0xaa repeated 80 times key_len
2353 * = 80 data = "Test Using Larger Than Block-Size Key and
2354 * Larger Than One Block-Size Data" data_len = 73 digest =
2355 * 0x6f630fad67cda0ee1fb1f562db3aa53e
2358 memset(key, 0xaa, keylen);
2360 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2361 digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2362 if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2363 text, textlen, digest, digestlen) < 0)
2366 /* done with all tests */
2368 printf("\nMD5 test results: %d cases failed", failed);
2370 printf("\nMD5 test results: all test cases passed");
2374 * test assoc key concatenation
2377 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2378 sctp_key_t * expected_key)
2383 sctp_show_key(key1, "\nkey1");
2384 sctp_show_key(key2, "\nkey2");
2385 key = sctp_compute_hashkey(key1, key2, NULL);
2386 sctp_show_key(expected_key, "\nExpected");
2387 sctp_show_key(key, "\nComputed");
2388 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2395 sctp_free_key(key1);
2396 sctp_free_key(key2);
2397 sctp_free_key(expected_key);
2404 sctp_test_authkey(void)
2406 sctp_key_t *key1, *key2, *expected_key;
2410 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2411 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2412 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2413 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2417 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2418 key2 = sctp_set_key("\x02", 1);
2419 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2420 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2424 key1 = sctp_set_key("\x01", 1);
2425 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2426 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2427 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2431 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2432 key2 = sctp_set_key("\x01", 1);
2433 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2434 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2438 key1 = sctp_set_key("\x01", 1);
2439 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2440 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2441 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2445 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2446 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2447 expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2448 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2452 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2453 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2454 expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2455 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2458 /* done with all tests */
2460 printf("\nKey concatenation test results: %d cases failed", failed);
2462 printf("\nKey concatenation test results: all test cases passed");
2466 #if defined(STANDALONE_HMAC_TEST)
2470 sctp_test_hmac_sha1();
2471 sctp_test_hmac_md5();
2472 sctp_test_authkey();
2475 #endif /* STANDALONE_HMAC_TEST */
2477 #endif /* SCTP_HMAC_TEST */