2 * Copyright (c) 2001-2008, 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_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
47 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(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
369 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
373 uint32_t key1len, key2len;
374 uint8_t *key_1, *key_2;
375 uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
377 /* sanity/length check */
378 key1len = sctp_get_keylen(key1);
379 key2len = sctp_get_keylen(key2);
380 if ((key1len == 0) && (key2len == 0))
382 else if (key1len == 0)
384 else if (key2len == 0)
387 if (key1len != key2len) {
388 if (key1len >= key2len)
393 if (key1len < maxlen) {
394 /* prepend zeroes to key1 */
395 bcopy(key1->key, temp + (maxlen - key1len), key1len);
399 /* prepend zeroes to key2 */
400 bcopy(key2->key, temp + (maxlen - key2len), key2len);
410 for (i = 0; i < maxlen; i++) {
413 else if (*key_1 < *key_2)
419 /* keys are equal value, so check lengths */
420 if (key1len == key2len)
422 else if (key1len < key2len)
429 * generate the concatenated keying material based on the two keys and the
430 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
431 * order for concatenation
434 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
440 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
441 sctp_get_keylen(shared);
444 /* get space for the new key */
445 new_key = sctp_alloc_key(keylen);
446 if (new_key == NULL) {
450 new_key->keylen = keylen;
451 key_ptr = new_key->key;
453 /* all keys empty/null?! */
457 /* concatenate the keys */
458 if (sctp_compare_key(key1, key2) <= 0) {
459 #ifdef SCTP_AUTH_DRAFT_04
460 /* key is key1 + shared + key2 */
461 if (sctp_get_keylen(key1)) {
462 bcopy(key1->key, key_ptr, key1->keylen);
463 key_ptr += key1->keylen;
465 if (sctp_get_keylen(shared)) {
466 bcopy(shared->key, key_ptr, shared->keylen);
467 key_ptr += shared->keylen;
469 if (sctp_get_keylen(key2)) {
470 bcopy(key2->key, key_ptr, key2->keylen);
471 key_ptr += key2->keylen;
474 /* key is shared + key1 + key2 */
475 if (sctp_get_keylen(shared)) {
476 bcopy(shared->key, key_ptr, shared->keylen);
477 key_ptr += shared->keylen;
479 if (sctp_get_keylen(key1)) {
480 bcopy(key1->key, key_ptr, key1->keylen);
481 key_ptr += key1->keylen;
483 if (sctp_get_keylen(key2)) {
484 bcopy(key2->key, key_ptr, key2->keylen);
485 key_ptr += key2->keylen;
489 #ifdef SCTP_AUTH_DRAFT_04
490 /* key is key2 + shared + key1 */
491 if (sctp_get_keylen(key2)) {
492 bcopy(key2->key, key_ptr, key2->keylen);
493 key_ptr += key2->keylen;
495 if (sctp_get_keylen(shared)) {
496 bcopy(shared->key, key_ptr, shared->keylen);
497 key_ptr += shared->keylen;
499 if (sctp_get_keylen(key1)) {
500 bcopy(key1->key, key_ptr, key1->keylen);
501 key_ptr += key1->keylen;
504 /* key is shared + key2 + key1 */
505 if (sctp_get_keylen(shared)) {
506 bcopy(shared->key, key_ptr, shared->keylen);
507 key_ptr += shared->keylen;
509 if (sctp_get_keylen(key2)) {
510 bcopy(key2->key, key_ptr, key2->keylen);
511 key_ptr += key2->keylen;
513 if (sctp_get_keylen(key1)) {
514 bcopy(key1->key, key_ptr, key1->keylen);
515 key_ptr += key1->keylen;
524 sctp_alloc_sharedkey(void)
526 sctp_sharedkey_t *new_key;
528 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
530 if (new_key == NULL) {
536 new_key->refcount = 1;
537 new_key->deactivated = 0;
542 sctp_free_sharedkey(sctp_sharedkey_t * skey)
547 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
548 if (skey->key != NULL)
549 sctp_free_key(skey->key);
550 SCTP_FREE(skey, SCTP_M_AUTH_KY);
555 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
557 sctp_sharedkey_t *skey;
559 LIST_FOREACH(skey, shared_keys, next) {
560 if (skey->keyid == key_id)
567 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
568 sctp_sharedkey_t * new_skey)
570 sctp_sharedkey_t *skey;
572 if ((shared_keys == NULL) || (new_skey == NULL))
575 /* insert into an empty list? */
576 if (SCTP_LIST_EMPTY(shared_keys)) {
577 LIST_INSERT_HEAD(shared_keys, new_skey, next);
580 /* insert into the existing list, ordered by key id */
581 LIST_FOREACH(skey, shared_keys, next) {
582 if (new_skey->keyid < skey->keyid) {
583 /* insert it before here */
584 LIST_INSERT_BEFORE(skey, new_skey, next);
586 } else if (new_skey->keyid == skey->keyid) {
587 /* replace the existing key */
588 /* verify this key *can* be replaced */
589 if ((skey->deactivated) && (skey->refcount > 1)) {
590 SCTPDBG(SCTP_DEBUG_AUTH1,
591 "can't replace shared key id %u\n",
595 SCTPDBG(SCTP_DEBUG_AUTH1,
596 "replacing shared key id %u\n",
598 LIST_INSERT_BEFORE(skey, new_skey, next);
599 LIST_REMOVE(skey, next);
600 sctp_free_sharedkey(skey);
603 if (LIST_NEXT(skey, next) == NULL) {
604 /* belongs at the end of the list */
605 LIST_INSERT_AFTER(skey, new_skey, next);
609 /* shouldn't reach here */
614 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
616 sctp_sharedkey_t *skey;
618 /* find the shared key */
619 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
621 /* bump the ref count */
623 atomic_add_int(&skey->refcount, 1);
624 SCTPDBG(SCTP_DEBUG_AUTH2,
625 "%s: stcb %p key %u refcount acquire to %d\n",
626 __FUNCTION__, stcb, key_id, skey->refcount);
631 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id)
633 sctp_sharedkey_t *skey;
635 /* find the shared key */
636 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
638 /* decrement the ref count */
640 sctp_free_sharedkey(skey);
641 SCTPDBG(SCTP_DEBUG_AUTH2,
642 "%s: stcb %p key %u refcount release to %d\n",
643 __FUNCTION__, stcb, key_id, skey->refcount);
645 /* see if a notification should be generated */
646 if ((skey->refcount <= 1) && (skey->deactivated)) {
647 /* notify ULP that key is no longer used */
648 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
649 key_id, 0, SCTP_SO_NOT_LOCKED);
650 SCTPDBG(SCTP_DEBUG_AUTH2,
651 "%s: stcb %p key %u no longer used, %d\n",
652 __FUNCTION__, stcb, key_id, skey->refcount);
657 static sctp_sharedkey_t *
658 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
660 sctp_sharedkey_t *new_skey;
664 new_skey = sctp_alloc_sharedkey();
665 if (new_skey == NULL)
667 if (skey->key != NULL)
668 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
670 new_skey->key = NULL;
671 new_skey->keyid = skey->keyid;
676 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
678 sctp_sharedkey_t *skey, *new_skey;
681 if ((src == NULL) || (dest == NULL))
683 LIST_FOREACH(skey, src, next) {
684 new_skey = sctp_copy_sharedkey(skey);
685 if (new_skey != NULL) {
686 (void)sctp_insert_sharedkey(dest, new_skey);
695 sctp_alloc_hmaclist(uint8_t num_hmacs)
697 sctp_hmaclist_t *new_list;
700 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
701 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
703 if (new_list == NULL) {
707 new_list->max_algo = num_hmacs;
708 new_list->num_algo = 0;
713 sctp_free_hmaclist(sctp_hmaclist_t * list)
716 SCTP_FREE(list, SCTP_M_AUTH_HL);
722 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
728 if (list->num_algo == list->max_algo) {
729 SCTPDBG(SCTP_DEBUG_AUTH1,
730 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
733 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
735 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
738 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
739 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
740 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
742 (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
745 /* Now is it already in the list */
746 for (i = 0; i < list->num_algo; i++) {
747 if (list->hmac[i] == hmac_id) {
748 /* already in list */
752 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
753 list->hmac[list->num_algo++] = hmac_id;
758 sctp_copy_hmaclist(sctp_hmaclist_t * list)
760 sctp_hmaclist_t *new_list;
766 new_list = sctp_alloc_hmaclist(list->max_algo);
767 if (new_list == NULL)
770 new_list->max_algo = list->max_algo;
771 new_list->num_algo = list->num_algo;
772 for (i = 0; i < list->num_algo; i++)
773 new_list->hmac[i] = list->hmac[i];
778 sctp_default_supported_hmaclist(void)
780 sctp_hmaclist_t *new_list;
782 new_list = sctp_alloc_hmaclist(2);
783 if (new_list == NULL)
785 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
786 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
791 * HMAC algos are listed in priority/preference order
792 * find the best HMAC id to use for the peer based on local support
795 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
799 if ((local == NULL) || (peer == NULL))
800 return (SCTP_AUTH_HMAC_ID_RSVD);
802 for (i = 0; i < peer->num_algo; i++) {
803 for (j = 0; j < local->num_algo; j++) {
804 if (peer->hmac[i] == local->hmac[j]) {
805 #ifndef SCTP_AUTH_DRAFT_04
806 /* "skip" MD5 as it's been deprecated */
807 if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
811 /* found the "best" one */
812 SCTPDBG(SCTP_DEBUG_AUTH1,
813 "SCTP: negotiated peer HMAC id %u\n",
815 return (peer->hmac[i]);
819 /* didn't find one! */
820 return (SCTP_AUTH_HMAC_ID_RSVD);
824 * serialize the HMAC algo list and return space used
825 * caller must guarantee ptr has appropriate space
828 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
836 for (i = 0; i < list->num_algo; i++) {
837 hmac_id = htons(list->hmac[i]);
838 bcopy(&hmac_id, ptr, sizeof(hmac_id));
839 ptr += sizeof(hmac_id);
841 return (list->num_algo * sizeof(hmac_id));
845 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
849 uint32_t sha1_supported = 0;
851 for (i = 0; i < num_hmacs; i++) {
852 hmac_id = ntohs(hmacs->hmac_ids[i]);
853 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
856 /* all HMAC id's are supported */
857 if (sha1_supported == 0)
864 sctp_alloc_authinfo(void)
866 sctp_authinfo_t *new_authinfo;
868 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
871 if (new_authinfo == NULL) {
875 bzero(new_authinfo, sizeof(*new_authinfo));
876 return (new_authinfo);
880 sctp_free_authinfo(sctp_authinfo_t * authinfo)
882 if (authinfo == NULL)
885 if (authinfo->random != NULL)
886 sctp_free_key(authinfo->random);
887 if (authinfo->peer_random != NULL)
888 sctp_free_key(authinfo->peer_random);
889 if (authinfo->assoc_key != NULL)
890 sctp_free_key(authinfo->assoc_key);
891 if (authinfo->recv_key != NULL)
892 sctp_free_key(authinfo->recv_key);
894 /* We are NOT dynamically allocating authinfo's right now... */
895 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
900 sctp_get_auth_chunk_len(uint16_t hmac_algo)
904 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
905 return (SCTP_SIZE32(size));
909 sctp_get_hmac_digest_len(uint16_t hmac_algo)
912 case SCTP_AUTH_HMAC_ID_SHA1:
913 return (SCTP_AUTH_DIGEST_LEN_SHA1);
914 case SCTP_AUTH_HMAC_ID_MD5:
915 return (SCTP_AUTH_DIGEST_LEN_MD5);
917 case SCTP_AUTH_HMAC_ID_SHA224:
918 return (SCTP_AUTH_DIGEST_LEN_SHA224);
921 case SCTP_AUTH_HMAC_ID_SHA256:
922 return (SCTP_AUTH_DIGEST_LEN_SHA256);
923 case SCTP_AUTH_HMAC_ID_SHA384:
924 return (SCTP_AUTH_DIGEST_LEN_SHA384);
925 case SCTP_AUTH_HMAC_ID_SHA512:
926 return (SCTP_AUTH_DIGEST_LEN_SHA512);
929 /* unknown HMAC algorithm: can't do anything */
935 sctp_get_hmac_block_len(uint16_t hmac_algo)
938 case SCTP_AUTH_HMAC_ID_SHA1:
939 case SCTP_AUTH_HMAC_ID_MD5:
941 case SCTP_AUTH_HMAC_ID_SHA224:
945 case SCTP_AUTH_HMAC_ID_SHA256:
947 case SCTP_AUTH_HMAC_ID_SHA384:
948 case SCTP_AUTH_HMAC_ID_SHA512:
951 case SCTP_AUTH_HMAC_ID_RSVD:
953 /* unknown HMAC algorithm: can't do anything */
959 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
962 case SCTP_AUTH_HMAC_ID_SHA1:
963 SHA1_Init(&ctx->sha1);
965 case SCTP_AUTH_HMAC_ID_MD5:
969 case SCTP_AUTH_HMAC_ID_SHA224:
973 case SCTP_AUTH_HMAC_ID_SHA256:
974 SHA256_Init(&ctx->sha256);
976 case SCTP_AUTH_HMAC_ID_SHA384:
977 SHA384_Init(&ctx->sha384);
979 case SCTP_AUTH_HMAC_ID_SHA512:
980 SHA512_Init(&ctx->sha512);
983 case SCTP_AUTH_HMAC_ID_RSVD:
985 /* unknown HMAC algorithm: can't do anything */
991 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
992 uint8_t * text, uint32_t textlen)
995 case SCTP_AUTH_HMAC_ID_SHA1:
996 SHA1_Update(&ctx->sha1, text, textlen);
998 case SCTP_AUTH_HMAC_ID_MD5:
999 MD5_Update(&ctx->md5, text, textlen);
1002 case SCTP_AUTH_HMAC_ID_SHA224:
1006 case SCTP_AUTH_HMAC_ID_SHA256:
1007 SHA256_Update(&ctx->sha256, text, textlen);
1009 case SCTP_AUTH_HMAC_ID_SHA384:
1010 SHA384_Update(&ctx->sha384, text, textlen);
1012 case SCTP_AUTH_HMAC_ID_SHA512:
1013 SHA512_Update(&ctx->sha512, text, textlen);
1016 case SCTP_AUTH_HMAC_ID_RSVD:
1018 /* unknown HMAC algorithm: can't do anything */
1024 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
1027 switch (hmac_algo) {
1028 case SCTP_AUTH_HMAC_ID_SHA1:
1029 SHA1_Final(digest, &ctx->sha1);
1031 case SCTP_AUTH_HMAC_ID_MD5:
1032 MD5_Final(digest, &ctx->md5);
1035 case SCTP_AUTH_HMAC_ID_SHA224:
1039 case SCTP_AUTH_HMAC_ID_SHA256:
1040 SHA256_Final(digest, &ctx->sha256);
1042 case SCTP_AUTH_HMAC_ID_SHA384:
1043 /* SHA384 is truncated SHA512 */
1044 SHA384_Final(digest, &ctx->sha384);
1046 case SCTP_AUTH_HMAC_ID_SHA512:
1047 SHA512_Final(digest, &ctx->sha512);
1050 case SCTP_AUTH_HMAC_ID_RSVD:
1052 /* unknown HMAC algorithm: can't do anything */
1058 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
1060 * Compute the HMAC digest using the desired hash key, text, and HMAC
1061 * algorithm. Resulting digest is placed in 'digest' and digest length
1062 * is returned, if the HMAC was performed.
1064 * WARNING: it is up to the caller to supply sufficient space to hold the
1068 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1069 uint8_t * text, uint32_t textlen, uint8_t * digest)
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];
1078 /* sanity check the material and length */
1079 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
1080 (textlen == 0) || (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 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1115 sctp_hmac_final(hmac_algo, &ctx, temp);
1117 /* perform outer hash */
1118 sctp_hmac_init(hmac_algo, &ctx);
1119 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1120 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1121 sctp_hmac_final(hmac_algo, &ctx, digest);
1128 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1129 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
1133 sctp_hash_context_t ctx;
1134 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1135 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1139 /* sanity check the material and length */
1140 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1141 /* can't do HMAC with empty key or text or digest store */
1144 /* validate the hmac algo and get the digest length */
1145 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1149 /* hash the key if it is longer than the hash block size */
1150 blocklen = sctp_get_hmac_block_len(hmac_algo);
1151 if (keylen > blocklen) {
1152 sctp_hmac_init(hmac_algo, &ctx);
1153 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1154 sctp_hmac_final(hmac_algo, &ctx, temp);
1155 /* set the hashed key as the key */
1159 /* initialize the inner/outer pads with the key and "append" zeroes */
1160 bzero(ipad, blocklen);
1161 bzero(opad, blocklen);
1162 bcopy(key, ipad, keylen);
1163 bcopy(key, opad, keylen);
1165 /* XOR the key with ipad and opad values */
1166 for (i = 0; i < blocklen; i++) {
1171 /* perform inner hash */
1172 sctp_hmac_init(hmac_algo, &ctx);
1173 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1174 /* find the correct starting mbuf and offset (get start of text) */
1176 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1177 m_offset -= SCTP_BUF_LEN(m_tmp);
1178 m_tmp = SCTP_BUF_NEXT(m_tmp);
1180 /* now use the rest of the mbuf chain for the text */
1181 while (m_tmp != NULL) {
1182 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1183 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1184 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1186 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1187 SCTP_BUF_LEN(m_tmp) - m_offset);
1190 /* clear the offset since it's only for the first mbuf */
1192 m_tmp = SCTP_BUF_NEXT(m_tmp);
1194 sctp_hmac_final(hmac_algo, &ctx, temp);
1196 /* perform outer hash */
1197 sctp_hmac_init(hmac_algo, &ctx);
1198 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1199 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1200 sctp_hmac_final(hmac_algo, &ctx, digest);
1206 * verify the HMAC digest using the desired hash key, text, and HMAC
1208 * Returns -1 on error, 0 on success.
1211 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1212 uint8_t * text, uint32_t textlen,
1213 uint8_t * digest, uint32_t digestlen)
1216 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1218 /* sanity check the material and length */
1219 if ((key == NULL) || (keylen == 0) ||
1220 (text == NULL) || (textlen == 0) || (digest == NULL)) {
1221 /* can't do HMAC with empty key or text or digest */
1224 len = sctp_get_hmac_digest_len(hmac_algo);
1225 if ((len == 0) || (digestlen != len))
1228 /* compute the expected hash */
1229 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1232 if (memcmp(digest, temp, digestlen) != 0)
1240 * computes the requested HMAC using a key struct (which may be modified if
1241 * the keylen exceeds the HMAC block len).
1244 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1245 uint32_t textlen, uint8_t * digest)
1249 sctp_hash_context_t ctx;
1250 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1253 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1255 /* can't do HMAC with empty key or text or digest store */
1258 /* validate the hmac algo and get the digest length */
1259 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1263 /* hash the key if it is longer than the hash block size */
1264 blocklen = sctp_get_hmac_block_len(hmac_algo);
1265 if (key->keylen > blocklen) {
1266 sctp_hmac_init(hmac_algo, &ctx);
1267 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1268 sctp_hmac_final(hmac_algo, &ctx, temp);
1269 /* save the hashed key as the new key */
1270 key->keylen = digestlen;
1271 bcopy(temp, key->key, key->keylen);
1273 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1279 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1280 uint32_t m_offset, uint8_t * digest)
1284 sctp_hash_context_t ctx;
1285 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1288 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1289 /* can't do HMAC with empty key or text or digest store */
1292 /* validate the hmac algo and get the digest length */
1293 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1297 /* hash the key if it is longer than the hash block size */
1298 blocklen = sctp_get_hmac_block_len(hmac_algo);
1299 if (key->keylen > blocklen) {
1300 sctp_hmac_init(hmac_algo, &ctx);
1301 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1302 sctp_hmac_final(hmac_algo, &ctx, temp);
1303 /* save the hashed key as the new key */
1304 key->keylen = digestlen;
1305 bcopy(temp, key->key, key->keylen);
1307 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1311 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1315 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1318 for (i = 0; i < list->num_algo; i++)
1319 if (list->hmac[i] == id)
1322 /* not in the list */
1328 * clear any cached key(s) if they match the given key id on an association.
1329 * the cached key(s) will be recomputed and re-cached at next use.
1330 * ASSUMES TCB_LOCK is already held
1333 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1338 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1339 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1340 stcb->asoc.authinfo.assoc_key = NULL;
1342 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1343 sctp_free_key(stcb->asoc.authinfo.recv_key);
1344 stcb->asoc.authinfo.recv_key = NULL;
1349 * clear any cached key(s) if they match the given key id for all assocs on
1351 * ASSUMES INP_WLOCK is already held
1354 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1356 struct sctp_tcb *stcb;
1361 /* clear the cached keys on all assocs on this instance */
1362 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1363 SCTP_TCB_LOCK(stcb);
1364 sctp_clear_cachedkeys(stcb, keyid);
1365 SCTP_TCB_UNLOCK(stcb);
1370 * delete a shared key from an association
1371 * ASSUMES TCB_LOCK is already held
1374 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1376 sctp_sharedkey_t *skey;
1381 /* is the keyid the assoc active sending key */
1382 if (keyid == stcb->asoc.authinfo.active_keyid)
1385 /* does the key exist? */
1386 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1390 /* are there other refcount holders on the key? */
1391 if (skey->refcount > 1)
1395 LIST_REMOVE(skey, next);
1396 sctp_free_sharedkey(skey); /* frees skey->key as well */
1398 /* clear any cached keys */
1399 sctp_clear_cachedkeys(stcb, keyid);
1404 * deletes a shared key from the endpoint
1405 * ASSUMES INP_WLOCK is already held
1408 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1410 sctp_sharedkey_t *skey;
1415 /* is the keyid the active sending key on the endpoint */
1416 if (keyid == inp->sctp_ep.default_keyid)
1419 /* does the key exist? */
1420 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1424 /* endpoint keys are not refcounted */
1427 LIST_REMOVE(skey, next);
1428 sctp_free_sharedkey(skey); /* frees skey->key as well */
1430 /* clear any cached keys */
1431 sctp_clear_cachedkeys_ep(inp, keyid);
1436 * set the active key on an association
1437 * ASSUMES TCB_LOCK is already held
1440 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1442 sctp_sharedkey_t *skey = NULL;
1444 /* find the key on the assoc */
1445 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1447 /* that key doesn't exist */
1450 if ((skey->deactivated) && (skey->refcount > 1)) {
1451 /* can't reactivate a deactivated key with other refcounts */
1454 /* set the (new) active key */
1455 stcb->asoc.authinfo.active_keyid = keyid;
1456 /* reset the deactivated flag */
1457 skey->deactivated = 0;
1463 * set the active key on an endpoint
1464 * ASSUMES INP_WLOCK is already held
1467 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1469 sctp_sharedkey_t *skey;
1472 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1474 /* that key doesn't exist */
1477 inp->sctp_ep.default_keyid = keyid;
1482 * deactivates a shared key from the association
1483 * ASSUMES INP_WLOCK is already held
1486 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1488 sctp_sharedkey_t *skey;
1493 /* is the keyid the assoc active sending key */
1494 if (keyid == stcb->asoc.authinfo.active_keyid)
1497 /* does the key exist? */
1498 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1502 /* are there other refcount holders on the key? */
1503 if (skey->refcount == 1) {
1504 /* no other users, send a notification for this key */
1505 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1508 /* mark the key as deactivated */
1509 skey->deactivated = 1;
1515 * deactivates a shared key from the endpoint
1516 * ASSUMES INP_WLOCK is already held
1519 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1521 sctp_sharedkey_t *skey;
1526 /* is the keyid the active sending key on the endpoint */
1527 if (keyid == inp->sctp_ep.default_keyid)
1530 /* does the key exist? */
1531 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1535 /* endpoint keys are not refcounted */
1538 LIST_REMOVE(skey, next);
1539 sctp_free_sharedkey(skey); /* frees skey->key as well */
1545 * get local authentication parameters from cookie (from INIT-ACK)
1548 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1549 uint32_t offset, uint32_t length)
1551 struct sctp_paramhdr *phdr, tmp_param;
1552 uint16_t plen, ptype;
1553 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1554 struct sctp_auth_random *p_random = NULL;
1555 uint16_t random_len = 0;
1556 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1557 struct sctp_auth_hmac_algo *hmacs = NULL;
1558 uint16_t hmacs_len = 0;
1559 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1560 struct sctp_auth_chunk_list *chunks = NULL;
1561 uint16_t num_chunks = 0;
1562 sctp_key_t *new_key;
1565 /* convert to upper bound */
1568 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1569 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1570 while (phdr != NULL) {
1571 ptype = ntohs(phdr->param_type);
1572 plen = ntohs(phdr->param_length);
1574 if ((plen == 0) || (offset + plen > length))
1577 if (ptype == SCTP_RANDOM) {
1578 if (plen > sizeof(random_store))
1580 phdr = sctp_get_next_param(m, offset,
1581 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1584 /* save the random and length for the key */
1585 p_random = (struct sctp_auth_random *)phdr;
1586 random_len = plen - sizeof(*p_random);
1587 } else if (ptype == SCTP_HMAC_LIST) {
1591 if (plen > sizeof(hmacs_store))
1593 phdr = sctp_get_next_param(m, offset,
1594 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
1597 /* save the hmacs list and num for the key */
1598 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1599 hmacs_len = plen - sizeof(*hmacs);
1600 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1601 if (stcb->asoc.local_hmacs != NULL)
1602 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1603 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1604 if (stcb->asoc.local_hmacs != NULL) {
1605 for (i = 0; i < num_hmacs; i++) {
1606 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1607 ntohs(hmacs->hmac_ids[i]));
1610 } else if (ptype == SCTP_CHUNK_LIST) {
1613 if (plen > sizeof(chunks_store))
1615 phdr = sctp_get_next_param(m, offset,
1616 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
1619 chunks = (struct sctp_auth_chunk_list *)phdr;
1620 num_chunks = plen - sizeof(*chunks);
1621 /* save chunks list and num for the key */
1622 if (stcb->asoc.local_auth_chunks != NULL)
1623 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1625 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1626 for (i = 0; i < num_chunks; i++) {
1627 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1628 stcb->asoc.local_auth_chunks);
1631 /* get next parameter */
1632 offset += SCTP_SIZE32(plen);
1633 if (offset + sizeof(struct sctp_paramhdr) > length)
1635 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1636 (uint8_t *) & tmp_param);
1638 /* concatenate the full random key */
1639 #ifdef SCTP_AUTH_DRAFT_04
1640 keylen = random_len;
1641 new_key = sctp_alloc_key(keylen);
1642 if (new_key != NULL) {
1643 /* copy in the RANDOM */
1644 if (p_random != NULL)
1645 bcopy(p_random->random_data, new_key->key, random_len);
1648 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
1649 sizeof(*hmacs) + hmacs_len;
1650 new_key = sctp_alloc_key(keylen);
1651 if (new_key != NULL) {
1652 /* copy in the RANDOM */
1653 if (p_random != NULL) {
1654 keylen = sizeof(*p_random) + random_len;
1655 bcopy(p_random, new_key->key, keylen);
1657 /* append in the AUTH chunks */
1658 if (chunks != NULL) {
1659 bcopy(chunks, new_key->key + keylen,
1660 sizeof(*chunks) + num_chunks);
1661 keylen += sizeof(*chunks) + num_chunks;
1663 /* append in the HMACs */
1664 if (hmacs != NULL) {
1665 bcopy(hmacs, new_key->key + keylen,
1666 sizeof(*hmacs) + hmacs_len);
1670 if (stcb->asoc.authinfo.random != NULL)
1671 sctp_free_key(stcb->asoc.authinfo.random);
1672 stcb->asoc.authinfo.random = new_key;
1673 stcb->asoc.authinfo.random_len = random_len;
1674 #ifdef SCTP_AUTH_DRAFT_04
1675 /* don't include the chunks and hmacs for draft -04 */
1676 stcb->asoc.authinfo.random->keylen = random_len;
1678 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1679 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1681 /* negotiate what HMAC to use for the peer */
1682 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1683 stcb->asoc.local_hmacs);
1685 /* copy defaults from the endpoint */
1686 /* FIX ME: put in cookie? */
1687 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1688 /* copy out the shared key list (by reference) from the endpoint */
1689 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1690 &stcb->asoc.shared_keys);
1694 * compute and fill in the HMAC digest for a packet
1697 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1698 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1701 sctp_sharedkey_t *skey;
1704 if ((stcb == NULL) || (auth == NULL))
1707 /* zero the digest + chunk padding */
1708 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1709 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1711 /* is the desired key cached? */
1712 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1713 (stcb->asoc.authinfo.assoc_key == NULL)) {
1714 if (stcb->asoc.authinfo.assoc_key != NULL) {
1715 /* free the old cached key */
1716 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1718 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1719 /* the only way skey is NULL is if null key id 0 is used */
1724 /* compute a new assoc key and cache it */
1725 stcb->asoc.authinfo.assoc_key =
1726 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1727 stcb->asoc.authinfo.peer_random, key);
1728 stcb->asoc.authinfo.assoc_keyid = keyid;
1729 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1730 stcb->asoc.authinfo.assoc_keyid);
1732 if (SCTP_AUTH_DEBUG)
1733 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1737 /* set in the active key id */
1738 auth->shared_key_id = htons(keyid);
1740 /* compute and fill in the digest */
1741 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1742 m, auth_offset, auth->hmac);
1747 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1756 /* find the correct starting mbuf and offset (get start position) */
1758 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1759 m_offset -= SCTP_BUF_LEN(m_tmp);
1760 m_tmp = SCTP_BUF_NEXT(m_tmp);
1762 /* now use the rest of the mbuf chain */
1763 while ((m_tmp != NULL) && (size > 0)) {
1764 data = mtod(m_tmp, uint8_t *) + m_offset;
1765 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1766 bzero(data, SCTP_BUF_LEN(m_tmp));
1767 size -= SCTP_BUF_LEN(m_tmp);
1772 /* clear the offset since it's only for the first mbuf */
1774 m_tmp = SCTP_BUF_NEXT(m_tmp);
1779 * process the incoming Authentication chunk
1781 * -1 on any authentication error
1782 * 0 on authentication verification
1785 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1786 struct mbuf *m, uint32_t offset)
1789 uint16_t shared_key_id;
1791 sctp_sharedkey_t *skey;
1793 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1794 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1796 /* auth is checked for NULL by caller */
1797 chunklen = ntohs(auth->ch.chunk_length);
1798 if (chunklen < sizeof(*auth)) {
1799 SCTP_STAT_INCR(sctps_recvauthfailed);
1802 SCTP_STAT_INCR(sctps_recvauth);
1804 /* get the auth params */
1805 shared_key_id = ntohs(auth->shared_key_id);
1806 hmac_id = ntohs(auth->hmac_id);
1807 SCTPDBG(SCTP_DEBUG_AUTH1,
1808 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1809 shared_key_id, hmac_id);
1811 /* is the indicated HMAC supported? */
1812 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1814 struct sctp_auth_invalid_hmac *err;
1816 SCTP_STAT_INCR(sctps_recvivalhmacid);
1817 SCTPDBG(SCTP_DEBUG_AUTH1,
1818 "SCTP Auth: unsupported HMAC id %u\n",
1821 * report this in an Error Chunk: Unsupported HMAC
1824 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
1826 if (m_err != NULL) {
1827 /* pre-reserve some space */
1828 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1829 /* fill in the error */
1830 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1831 bzero(err, sizeof(*err));
1832 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1833 err->ph.param_length = htons(sizeof(*err));
1834 err->hmac_id = ntohs(hmac_id);
1835 SCTP_BUF_LEN(m_err) = sizeof(*err);
1837 sctp_queue_op_err(stcb, m_err);
1841 /* get the indicated shared key, if available */
1842 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1843 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1844 /* find the shared key on the assoc first */
1845 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1847 /* if the shared key isn't found, discard the chunk */
1849 SCTP_STAT_INCR(sctps_recvivalkeyid);
1850 SCTPDBG(SCTP_DEBUG_AUTH1,
1851 "SCTP Auth: unknown key id %u\n",
1855 /* generate a notification if this is a new key id */
1856 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1858 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1859 * shared_key_id, (void
1860 * *)stcb->asoc.authinfo.recv_keyid);
1862 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1863 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1864 SCTP_SO_NOT_LOCKED);
1865 /* compute a new recv assoc key and cache it */
1866 if (stcb->asoc.authinfo.recv_key != NULL)
1867 sctp_free_key(stcb->asoc.authinfo.recv_key);
1868 stcb->asoc.authinfo.recv_key =
1869 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1870 stcb->asoc.authinfo.peer_random, skey->key);
1871 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1873 if (SCTP_AUTH_DEBUG)
1874 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1877 /* validate the digest length */
1878 digestlen = sctp_get_hmac_digest_len(hmac_id);
1879 if (chunklen < (sizeof(*auth) + digestlen)) {
1880 /* invalid digest length */
1881 SCTP_STAT_INCR(sctps_recvauthfailed);
1882 SCTPDBG(SCTP_DEBUG_AUTH1,
1883 "SCTP Auth: chunk too short for HMAC\n");
1886 /* save a copy of the digest, zero the pseudo header, and validate */
1887 bcopy(auth->hmac, digest, digestlen);
1888 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1889 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1890 m, offset, computed_digest);
1892 /* compare the computed digest with the one in the AUTH chunk */
1893 if (memcmp(digest, computed_digest, digestlen) != 0) {
1894 SCTP_STAT_INCR(sctps_recvauthfailed);
1895 SCTPDBG(SCTP_DEBUG_AUTH1,
1896 "SCTP Auth: HMAC digest check failed\n");
1903 * Generate NOTIFICATION
1906 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1907 uint16_t keyid, uint16_t alt_keyid, int so_locked
1908 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1913 struct mbuf *m_notify;
1914 struct sctp_authkey_event *auth;
1915 struct sctp_queued_to_read *control;
1917 if ((stcb == NULL) ||
1918 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1919 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1920 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1922 /* If the socket is gone we are out of here */
1925 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1926 /* event not enabled */
1929 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1930 0, M_DONTWAIT, 1, MT_HEADER);
1931 if (m_notify == NULL)
1935 SCTP_BUF_LEN(m_notify) = 0;
1936 auth = mtod(m_notify, struct sctp_authkey_event *);
1937 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1938 auth->auth_flags = 0;
1939 auth->auth_length = sizeof(*auth);
1940 auth->auth_keynumber = keyid;
1941 auth->auth_altkeynumber = alt_keyid;
1942 auth->auth_indication = indication;
1943 auth->auth_assoc_id = sctp_get_associd(stcb);
1945 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1946 SCTP_BUF_NEXT(m_notify) = NULL;
1948 /* append to socket */
1949 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1950 0, 0, 0, 0, 0, 0, m_notify);
1951 if (control == NULL) {
1953 sctp_m_freem(m_notify);
1956 control->spec_flags = M_NOTIFICATION;
1957 control->length = SCTP_BUF_LEN(m_notify);
1958 /* not that we need this */
1959 control->tail_mbuf = m_notify;
1960 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1961 &stcb->sctp_socket->so_rcv, 1, so_locked);
1966 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1967 * Note: currently only used for INIT as INIT-ACK is handled inline
1968 * with sctp_load_addresses_from_init()
1971 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1973 struct sctp_paramhdr *phdr, parm_buf;
1974 uint16_t ptype, plen;
1975 int peer_supports_asconf = 0;
1976 int peer_supports_auth = 0;
1977 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1978 uint8_t saw_asconf = 0;
1979 uint8_t saw_asconf_ack = 0;
1981 /* go through each of the params. */
1982 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1984 ptype = ntohs(phdr->param_type);
1985 plen = ntohs(phdr->param_length);
1987 if (offset + plen > limit) {
1990 if (plen < sizeof(struct sctp_paramhdr)) {
1993 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1994 /* A supported extension chunk */
1995 struct sctp_supported_chunk_types_param *pr_supported;
1996 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1999 phdr = sctp_get_next_param(m, offset,
2000 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
2004 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
2005 num_ent = plen - sizeof(struct sctp_paramhdr);
2006 for (i = 0; i < num_ent; i++) {
2007 switch (pr_supported->chunk_types[i]) {
2009 case SCTP_ASCONF_ACK:
2010 peer_supports_asconf = 1;
2012 case SCTP_AUTHENTICATION:
2013 peer_supports_auth = 1;
2016 /* one we don't care about */
2020 } else if (ptype == SCTP_RANDOM) {
2022 /* enforce the random length */
2023 if (plen != (sizeof(struct sctp_auth_random) +
2024 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
2025 SCTPDBG(SCTP_DEBUG_AUTH1,
2026 "SCTP: invalid RANDOM len\n");
2029 } else if (ptype == SCTP_HMAC_LIST) {
2030 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
2031 struct sctp_auth_hmac_algo *hmacs;
2034 if (plen > sizeof(store))
2036 phdr = sctp_get_next_param(m, offset,
2037 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
2040 hmacs = (struct sctp_auth_hmac_algo *)phdr;
2041 num_hmacs = (plen - sizeof(*hmacs)) /
2042 sizeof(hmacs->hmac_ids[0]);
2043 /* validate the hmac list */
2044 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
2045 SCTPDBG(SCTP_DEBUG_AUTH1,
2046 "SCTP: invalid HMAC param\n");
2050 } else if (ptype == SCTP_CHUNK_LIST) {
2052 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
2054 /* did the peer send a non-empty chunk list? */
2055 struct sctp_auth_chunk_list *chunks = NULL;
2057 phdr = sctp_get_next_param(m, offset,
2058 (struct sctp_paramhdr *)chunks_store,
2059 min(plen, sizeof(chunks_store)));
2064 * Flip through the list and mark that the
2065 * peer supports asconf/asconf_ack.
2067 chunks = (struct sctp_auth_chunk_list *)phdr;
2068 num_chunks = plen - sizeof(*chunks);
2069 for (i = 0; i < num_chunks; i++) {
2070 /* record asconf/asconf-ack if listed */
2071 if (chunks->chunk_types[i] == SCTP_ASCONF)
2073 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
2080 offset += SCTP_SIZE32(plen);
2081 if (offset >= limit) {
2084 phdr = sctp_get_next_param(m, offset, &parm_buf,
2087 /* validate authentication required parameters */
2088 if (got_random && got_hmacs) {
2089 peer_supports_auth = 1;
2091 peer_supports_auth = 0;
2093 if (!peer_supports_auth && got_chklist) {
2094 SCTPDBG(SCTP_DEBUG_AUTH1,
2095 "SCTP: peer sent chunk list w/o AUTH\n");
2098 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && peer_supports_asconf &&
2099 !peer_supports_auth) {
2100 SCTPDBG(SCTP_DEBUG_AUTH1,
2101 "SCTP: peer supports ASCONF but not AUTH\n");
2103 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
2104 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
2111 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2113 uint16_t chunks_len = 0;
2114 uint16_t hmacs_len = 0;
2115 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
2116 sctp_key_t *new_key;
2119 /* initialize hmac list from endpoint */
2120 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
2121 if (stcb->asoc.local_hmacs != NULL) {
2122 hmacs_len = stcb->asoc.local_hmacs->num_algo *
2123 sizeof(stcb->asoc.local_hmacs->hmac[0]);
2125 /* initialize auth chunks list from endpoint */
2126 stcb->asoc.local_auth_chunks =
2127 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
2128 if (stcb->asoc.local_auth_chunks != NULL) {
2131 for (i = 0; i < 256; i++) {
2132 if (stcb->asoc.local_auth_chunks->chunks[i])
2136 /* copy defaults from the endpoint */
2137 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
2139 /* copy out the shared key list (by reference) from the endpoint */
2140 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
2141 &stcb->asoc.shared_keys);
2143 /* now set the concatenated key (random + chunks + hmacs) */
2144 #ifdef SCTP_AUTH_DRAFT_04
2145 /* don't include the chunks and hmacs for draft -04 */
2146 keylen = random_len;
2147 new_key = sctp_generate_random_key(keylen);
2149 /* key includes parameter headers */
2150 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2152 new_key = sctp_alloc_key(keylen);
2153 if (new_key != NULL) {
2154 struct sctp_paramhdr *ph;
2157 /* generate and copy in the RANDOM */
2158 ph = (struct sctp_paramhdr *)new_key->key;
2159 ph->param_type = htons(SCTP_RANDOM);
2160 plen = sizeof(*ph) + random_len;
2161 ph->param_length = htons(plen);
2162 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2165 /* append in the AUTH chunks */
2166 /* NOTE: currently we always have chunks to list */
2167 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2168 ph->param_type = htons(SCTP_CHUNK_LIST);
2169 plen = sizeof(*ph) + chunks_len;
2170 ph->param_length = htons(plen);
2171 keylen += sizeof(*ph);
2172 if (stcb->asoc.local_auth_chunks) {
2175 for (i = 0; i < 256; i++) {
2176 if (stcb->asoc.local_auth_chunks->chunks[i])
2177 new_key->key[keylen++] = i;
2180 /* append in the HMACs */
2181 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2182 ph->param_type = htons(SCTP_HMAC_LIST);
2183 plen = sizeof(*ph) + hmacs_len;
2184 ph->param_length = htons(plen);
2185 keylen += sizeof(*ph);
2186 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2187 new_key->key + keylen);
2190 if (stcb->asoc.authinfo.random != NULL)
2191 sctp_free_key(stcb->asoc.authinfo.random);
2192 stcb->asoc.authinfo.random = new_key;
2193 stcb->asoc.authinfo.random_len = random_len;
2197 #ifdef SCTP_HMAC_TEST
2199 * HMAC and key concatenation tests
2202 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2206 printf("\n%s: 0x", str);
2210 for (i = 0; i < digestlen; i++)
2211 printf("%02x", digest[i]);
2215 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2216 uint32_t keylen, uint8_t * text, uint32_t textlen,
2217 uint8_t * digest, uint32_t digestlen)
2219 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2221 printf("\n%s:", str);
2222 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2223 sctp_print_digest(digest, digestlen, "Expected digest");
2224 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2225 if (memcmp(digest, computed_digest, digestlen) != 0) {
2236 * RFC 2202: HMAC-SHA1 test cases
2239 sctp_test_hmac_sha1(void)
2246 uint32_t digestlen = 20;
2251 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2255 * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
2258 memset(key, 0x0b, keylen);
2260 strcpy(text, "Hi There");
2261 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2262 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2263 text, textlen, digest, digestlen) < 0)
2270 * data = "what do ya want for nothing?"
2272 * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2275 strcpy(key, "Jefe");
2277 strcpy(text, "what do ya want for nothing?");
2278 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2279 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2280 text, textlen, digest, digestlen) < 0)
2285 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2287 * data = 0xdd repeated 50 times
2289 * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2292 memset(key, 0xaa, keylen);
2294 memset(text, 0xdd, textlen);
2295 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2296 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2297 text, textlen, digest, digestlen) < 0)
2302 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2304 * data = 0xcd repeated 50 times
2306 * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2309 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);
2311 memset(text, 0xcd, textlen);
2312 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2313 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2314 text, textlen, digest, digestlen) < 0)
2319 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2321 * data = "Test With Truncation"
2323 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
2324 * digest-96 = 0x4c1a03424b55e07fe7f27be1
2327 memset(key, 0x0c, keylen);
2329 strcpy(text, "Test With Truncation");
2330 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2331 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2332 text, textlen, digest, digestlen) < 0)
2337 * key = 0xaa repeated 80 times
2339 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2341 * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2344 memset(key, 0xaa, keylen);
2346 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2347 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2348 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2349 text, textlen, digest, digestlen) < 0)
2354 * key = 0xaa repeated 80 times
2356 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2358 * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2361 memset(key, 0xaa, keylen);
2363 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2364 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2365 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2366 text, textlen, digest, digestlen) < 0)
2369 /* done with all tests */
2371 printf("\nSHA1 test results: %d cases failed", failed);
2373 printf("\nSHA1 test results: all test cases passed");
2377 * RFC 2202: HMAC-MD5 test cases
2380 sctp_test_hmac_md5(void)
2387 uint32_t digestlen = 16;
2392 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2396 * digest = 0x9294727a3638bb1c13f48ef8158bfc9d
2399 memset(key, 0x0b, keylen);
2401 strcpy(text, "Hi There");
2402 digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2403 if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2404 text, textlen, digest, digestlen) < 0)
2411 * data = "what do ya want for nothing?"
2413 * digest = 0x750c783e6ab0b503eaa86e310a5db738
2416 strcpy(key, "Jefe");
2418 strcpy(text, "what do ya want for nothing?");
2419 digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2420 if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2421 text, textlen, digest, digestlen) < 0)
2426 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2428 * data = 0xdd repeated 50 times
2430 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2433 memset(key, 0xaa, keylen);
2435 memset(text, 0xdd, textlen);
2436 digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2437 if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2438 text, textlen, digest, digestlen) < 0)
2443 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2445 * data = 0xcd repeated 50 times
2447 * digest = 0x697eaf0aca3a3aea3a75164746ffaa79
2450 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);
2452 memset(text, 0xcd, textlen);
2453 digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2454 if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2455 text, textlen, digest, digestlen) < 0)
2460 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2462 * data = "Test With Truncation"
2464 * digest = 0x56461ef2342edc00f9bab995690efd4c
2465 * digest-96 = 0x56461ef2342edc00f9bab995
2468 memset(key, 0x0c, keylen);
2470 strcpy(text, "Test With Truncation");
2471 digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2472 if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2473 text, textlen, digest, digestlen) < 0)
2478 * key = 0xaa repeated 80 times
2480 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2482 * digest = 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2485 memset(key, 0xaa, keylen);
2487 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2488 digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2489 if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2490 text, textlen, digest, digestlen) < 0)
2495 * key = 0xaa repeated 80 times
2497 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2499 * digest = 0x6f630fad67cda0ee1fb1f562db3aa53e
2502 memset(key, 0xaa, keylen);
2504 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2505 digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2506 if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2507 text, textlen, digest, digestlen) < 0)
2510 /* done with all tests */
2512 printf("\nMD5 test results: %d cases failed", failed);
2514 printf("\nMD5 test results: all test cases passed");
2518 * test assoc key concatenation
2521 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2522 sctp_key_t * expected_key)
2527 sctp_show_key(key1, "\nkey1");
2528 sctp_show_key(key2, "\nkey2");
2529 key = sctp_compute_hashkey(key1, key2, NULL);
2530 sctp_show_key(expected_key, "\nExpected");
2531 sctp_show_key(key, "\nComputed");
2532 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2539 sctp_free_key(key1);
2540 sctp_free_key(key2);
2541 sctp_free_key(expected_key);
2548 sctp_test_authkey(void)
2550 sctp_key_t *key1, *key2, *expected_key;
2554 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2555 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2556 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2557 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2561 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2562 key2 = sctp_set_key("\x02", 1);
2563 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2564 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2568 key1 = sctp_set_key("\x01", 1);
2569 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2570 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2571 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2575 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2576 key2 = sctp_set_key("\x01", 1);
2577 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2578 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2582 key1 = sctp_set_key("\x01", 1);
2583 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2584 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2585 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2589 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2590 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2591 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);
2592 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2596 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2597 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2598 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);
2599 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2602 /* done with all tests */
2604 printf("\nKey concatenation test results: %d cases failed", failed);
2606 printf("\nKey concatenation test results: all test cases passed");
2610 #if defined(STANDALONE_HMAC_TEST)
2614 sctp_test_hmac_sha1();
2615 sctp_test_hmac_md5();
2616 sctp_test_authkey();
2619 #endif /* STANDALONE_HMAC_TEST */
2621 #endif /* SCTP_HMAC_TEST */