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(*hmacs) + hmacs_len;
1649 if (chunks != NULL) {
1650 keylen += sizeof(*chunks) + num_chunks;
1652 new_key = sctp_alloc_key(keylen);
1653 if (new_key != NULL) {
1654 /* copy in the RANDOM */
1655 if (p_random != NULL) {
1656 keylen = sizeof(*p_random) + random_len;
1657 bcopy(p_random, new_key->key, keylen);
1659 /* append in the AUTH chunks */
1660 if (chunks != NULL) {
1661 bcopy(chunks, new_key->key + keylen,
1662 sizeof(*chunks) + num_chunks);
1663 keylen += sizeof(*chunks) + num_chunks;
1665 /* append in the HMACs */
1666 if (hmacs != NULL) {
1667 bcopy(hmacs, new_key->key + keylen,
1668 sizeof(*hmacs) + hmacs_len);
1672 if (stcb->asoc.authinfo.random != NULL)
1673 sctp_free_key(stcb->asoc.authinfo.random);
1674 stcb->asoc.authinfo.random = new_key;
1675 stcb->asoc.authinfo.random_len = random_len;
1676 #ifdef SCTP_AUTH_DRAFT_04
1677 /* don't include the chunks and hmacs for draft -04 */
1678 stcb->asoc.authinfo.random->keylen = random_len;
1680 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1681 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1683 /* negotiate what HMAC to use for the peer */
1684 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1685 stcb->asoc.local_hmacs);
1687 /* copy defaults from the endpoint */
1688 /* FIX ME: put in cookie? */
1689 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1690 /* copy out the shared key list (by reference) from the endpoint */
1691 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1692 &stcb->asoc.shared_keys);
1696 * compute and fill in the HMAC digest for a packet
1699 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1700 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1703 sctp_sharedkey_t *skey;
1706 if ((stcb == NULL) || (auth == NULL))
1709 /* zero the digest + chunk padding */
1710 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1711 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1713 /* is the desired key cached? */
1714 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1715 (stcb->asoc.authinfo.assoc_key == NULL)) {
1716 if (stcb->asoc.authinfo.assoc_key != NULL) {
1717 /* free the old cached key */
1718 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1720 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1721 /* the only way skey is NULL is if null key id 0 is used */
1726 /* compute a new assoc key and cache it */
1727 stcb->asoc.authinfo.assoc_key =
1728 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1729 stcb->asoc.authinfo.peer_random, key);
1730 stcb->asoc.authinfo.assoc_keyid = keyid;
1731 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1732 stcb->asoc.authinfo.assoc_keyid);
1734 if (SCTP_AUTH_DEBUG)
1735 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1739 /* set in the active key id */
1740 auth->shared_key_id = htons(keyid);
1742 /* compute and fill in the digest */
1743 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1744 m, auth_offset, auth->hmac);
1749 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1758 /* find the correct starting mbuf and offset (get start position) */
1760 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1761 m_offset -= SCTP_BUF_LEN(m_tmp);
1762 m_tmp = SCTP_BUF_NEXT(m_tmp);
1764 /* now use the rest of the mbuf chain */
1765 while ((m_tmp != NULL) && (size > 0)) {
1766 data = mtod(m_tmp, uint8_t *) + m_offset;
1767 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1768 bzero(data, SCTP_BUF_LEN(m_tmp));
1769 size -= SCTP_BUF_LEN(m_tmp);
1774 /* clear the offset since it's only for the first mbuf */
1776 m_tmp = SCTP_BUF_NEXT(m_tmp);
1781 * process the incoming Authentication chunk
1783 * -1 on any authentication error
1784 * 0 on authentication verification
1787 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1788 struct mbuf *m, uint32_t offset)
1791 uint16_t shared_key_id;
1793 sctp_sharedkey_t *skey;
1795 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1796 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1798 /* auth is checked for NULL by caller */
1799 chunklen = ntohs(auth->ch.chunk_length);
1800 if (chunklen < sizeof(*auth)) {
1801 SCTP_STAT_INCR(sctps_recvauthfailed);
1804 SCTP_STAT_INCR(sctps_recvauth);
1806 /* get the auth params */
1807 shared_key_id = ntohs(auth->shared_key_id);
1808 hmac_id = ntohs(auth->hmac_id);
1809 SCTPDBG(SCTP_DEBUG_AUTH1,
1810 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1811 shared_key_id, hmac_id);
1813 /* is the indicated HMAC supported? */
1814 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1816 struct sctp_auth_invalid_hmac *err;
1818 SCTP_STAT_INCR(sctps_recvivalhmacid);
1819 SCTPDBG(SCTP_DEBUG_AUTH1,
1820 "SCTP Auth: unsupported HMAC id %u\n",
1823 * report this in an Error Chunk: Unsupported HMAC
1826 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
1828 if (m_err != NULL) {
1829 /* pre-reserve some space */
1830 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1831 /* fill in the error */
1832 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1833 bzero(err, sizeof(*err));
1834 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1835 err->ph.param_length = htons(sizeof(*err));
1836 err->hmac_id = ntohs(hmac_id);
1837 SCTP_BUF_LEN(m_err) = sizeof(*err);
1839 sctp_queue_op_err(stcb, m_err);
1843 /* get the indicated shared key, if available */
1844 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1845 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1846 /* find the shared key on the assoc first */
1847 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1849 /* if the shared key isn't found, discard the chunk */
1851 SCTP_STAT_INCR(sctps_recvivalkeyid);
1852 SCTPDBG(SCTP_DEBUG_AUTH1,
1853 "SCTP Auth: unknown key id %u\n",
1857 /* generate a notification if this is a new key id */
1858 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1860 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1861 * shared_key_id, (void
1862 * *)stcb->asoc.authinfo.recv_keyid);
1864 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1865 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1866 SCTP_SO_NOT_LOCKED);
1867 /* compute a new recv assoc key and cache it */
1868 if (stcb->asoc.authinfo.recv_key != NULL)
1869 sctp_free_key(stcb->asoc.authinfo.recv_key);
1870 stcb->asoc.authinfo.recv_key =
1871 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1872 stcb->asoc.authinfo.peer_random, skey->key);
1873 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1875 if (SCTP_AUTH_DEBUG)
1876 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1879 /* validate the digest length */
1880 digestlen = sctp_get_hmac_digest_len(hmac_id);
1881 if (chunklen < (sizeof(*auth) + digestlen)) {
1882 /* invalid digest length */
1883 SCTP_STAT_INCR(sctps_recvauthfailed);
1884 SCTPDBG(SCTP_DEBUG_AUTH1,
1885 "SCTP Auth: chunk too short for HMAC\n");
1888 /* save a copy of the digest, zero the pseudo header, and validate */
1889 bcopy(auth->hmac, digest, digestlen);
1890 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1891 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1892 m, offset, computed_digest);
1894 /* compare the computed digest with the one in the AUTH chunk */
1895 if (memcmp(digest, computed_digest, digestlen) != 0) {
1896 SCTP_STAT_INCR(sctps_recvauthfailed);
1897 SCTPDBG(SCTP_DEBUG_AUTH1,
1898 "SCTP Auth: HMAC digest check failed\n");
1905 * Generate NOTIFICATION
1908 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1909 uint16_t keyid, uint16_t alt_keyid, int so_locked
1910 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1915 struct mbuf *m_notify;
1916 struct sctp_authkey_event *auth;
1917 struct sctp_queued_to_read *control;
1919 if ((stcb == NULL) ||
1920 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1921 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1922 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1924 /* If the socket is gone we are out of here */
1927 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1928 /* event not enabled */
1931 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1932 0, M_DONTWAIT, 1, MT_HEADER);
1933 if (m_notify == NULL)
1937 SCTP_BUF_LEN(m_notify) = 0;
1938 auth = mtod(m_notify, struct sctp_authkey_event *);
1939 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1940 auth->auth_flags = 0;
1941 auth->auth_length = sizeof(*auth);
1942 auth->auth_keynumber = keyid;
1943 auth->auth_altkeynumber = alt_keyid;
1944 auth->auth_indication = indication;
1945 auth->auth_assoc_id = sctp_get_associd(stcb);
1947 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1948 SCTP_BUF_NEXT(m_notify) = NULL;
1950 /* append to socket */
1951 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1952 0, 0, 0, 0, 0, 0, m_notify);
1953 if (control == NULL) {
1955 sctp_m_freem(m_notify);
1958 control->spec_flags = M_NOTIFICATION;
1959 control->length = SCTP_BUF_LEN(m_notify);
1960 /* not that we need this */
1961 control->tail_mbuf = m_notify;
1962 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1963 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1968 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1969 * Note: currently only used for INIT as INIT-ACK is handled inline
1970 * with sctp_load_addresses_from_init()
1973 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1975 struct sctp_paramhdr *phdr, parm_buf;
1976 uint16_t ptype, plen;
1977 int peer_supports_asconf = 0;
1978 int peer_supports_auth = 0;
1979 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1980 uint8_t saw_asconf = 0;
1981 uint8_t saw_asconf_ack = 0;
1983 /* go through each of the params. */
1984 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1986 ptype = ntohs(phdr->param_type);
1987 plen = ntohs(phdr->param_length);
1989 if (offset + plen > limit) {
1992 if (plen < sizeof(struct sctp_paramhdr)) {
1995 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1996 /* A supported extension chunk */
1997 struct sctp_supported_chunk_types_param *pr_supported;
1998 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
2001 phdr = sctp_get_next_param(m, offset,
2002 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
2006 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
2007 num_ent = plen - sizeof(struct sctp_paramhdr);
2008 for (i = 0; i < num_ent; i++) {
2009 switch (pr_supported->chunk_types[i]) {
2011 case SCTP_ASCONF_ACK:
2012 peer_supports_asconf = 1;
2014 case SCTP_AUTHENTICATION:
2015 peer_supports_auth = 1;
2018 /* one we don't care about */
2022 } else if (ptype == SCTP_RANDOM) {
2024 /* enforce the random length */
2025 if (plen != (sizeof(struct sctp_auth_random) +
2026 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
2027 SCTPDBG(SCTP_DEBUG_AUTH1,
2028 "SCTP: invalid RANDOM len\n");
2031 } else if (ptype == SCTP_HMAC_LIST) {
2032 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
2033 struct sctp_auth_hmac_algo *hmacs;
2036 if (plen > sizeof(store))
2038 phdr = sctp_get_next_param(m, offset,
2039 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
2042 hmacs = (struct sctp_auth_hmac_algo *)phdr;
2043 num_hmacs = (plen - sizeof(*hmacs)) /
2044 sizeof(hmacs->hmac_ids[0]);
2045 /* validate the hmac list */
2046 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
2047 SCTPDBG(SCTP_DEBUG_AUTH1,
2048 "SCTP: invalid HMAC param\n");
2052 } else if (ptype == SCTP_CHUNK_LIST) {
2054 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
2056 /* did the peer send a non-empty chunk list? */
2057 struct sctp_auth_chunk_list *chunks = NULL;
2059 phdr = sctp_get_next_param(m, offset,
2060 (struct sctp_paramhdr *)chunks_store,
2061 min(plen, sizeof(chunks_store)));
2066 * Flip through the list and mark that the
2067 * peer supports asconf/asconf_ack.
2069 chunks = (struct sctp_auth_chunk_list *)phdr;
2070 num_chunks = plen - sizeof(*chunks);
2071 for (i = 0; i < num_chunks; i++) {
2072 /* record asconf/asconf-ack if listed */
2073 if (chunks->chunk_types[i] == SCTP_ASCONF)
2075 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
2082 offset += SCTP_SIZE32(plen);
2083 if (offset >= limit) {
2086 phdr = sctp_get_next_param(m, offset, &parm_buf,
2089 /* validate authentication required parameters */
2090 if (got_random && got_hmacs) {
2091 peer_supports_auth = 1;
2093 peer_supports_auth = 0;
2095 if (!peer_supports_auth && got_chklist) {
2096 SCTPDBG(SCTP_DEBUG_AUTH1,
2097 "SCTP: peer sent chunk list w/o AUTH\n");
2100 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && peer_supports_asconf &&
2101 !peer_supports_auth) {
2102 SCTPDBG(SCTP_DEBUG_AUTH1,
2103 "SCTP: peer supports ASCONF but not AUTH\n");
2105 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
2106 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
2113 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2115 uint16_t chunks_len = 0;
2116 uint16_t hmacs_len = 0;
2117 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
2118 sctp_key_t *new_key;
2121 /* initialize hmac list from endpoint */
2122 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
2123 if (stcb->asoc.local_hmacs != NULL) {
2124 hmacs_len = stcb->asoc.local_hmacs->num_algo *
2125 sizeof(stcb->asoc.local_hmacs->hmac[0]);
2127 /* initialize auth chunks list from endpoint */
2128 stcb->asoc.local_auth_chunks =
2129 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
2130 if (stcb->asoc.local_auth_chunks != NULL) {
2133 for (i = 0; i < 256; i++) {
2134 if (stcb->asoc.local_auth_chunks->chunks[i])
2138 /* copy defaults from the endpoint */
2139 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
2141 /* copy out the shared key list (by reference) from the endpoint */
2142 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
2143 &stcb->asoc.shared_keys);
2145 /* now set the concatenated key (random + chunks + hmacs) */
2146 #ifdef SCTP_AUTH_DRAFT_04
2147 /* don't include the chunks and hmacs for draft -04 */
2148 keylen = random_len;
2149 new_key = sctp_generate_random_key(keylen);
2151 /* key includes parameter headers */
2152 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2154 new_key = sctp_alloc_key(keylen);
2155 if (new_key != NULL) {
2156 struct sctp_paramhdr *ph;
2159 /* generate and copy in the RANDOM */
2160 ph = (struct sctp_paramhdr *)new_key->key;
2161 ph->param_type = htons(SCTP_RANDOM);
2162 plen = sizeof(*ph) + random_len;
2163 ph->param_length = htons(plen);
2164 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2167 /* append in the AUTH chunks */
2168 /* NOTE: currently we always have chunks to list */
2169 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2170 ph->param_type = htons(SCTP_CHUNK_LIST);
2171 plen = sizeof(*ph) + chunks_len;
2172 ph->param_length = htons(plen);
2173 keylen += sizeof(*ph);
2174 if (stcb->asoc.local_auth_chunks) {
2177 for (i = 0; i < 256; i++) {
2178 if (stcb->asoc.local_auth_chunks->chunks[i])
2179 new_key->key[keylen++] = i;
2182 /* append in the HMACs */
2183 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2184 ph->param_type = htons(SCTP_HMAC_LIST);
2185 plen = sizeof(*ph) + hmacs_len;
2186 ph->param_length = htons(plen);
2187 keylen += sizeof(*ph);
2188 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2189 new_key->key + keylen);
2192 if (stcb->asoc.authinfo.random != NULL)
2193 sctp_free_key(stcb->asoc.authinfo.random);
2194 stcb->asoc.authinfo.random = new_key;
2195 stcb->asoc.authinfo.random_len = random_len;
2199 #ifdef SCTP_HMAC_TEST
2201 * HMAC and key concatenation tests
2204 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2208 printf("\n%s: 0x", str);
2212 for (i = 0; i < digestlen; i++)
2213 printf("%02x", digest[i]);
2217 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2218 uint32_t keylen, uint8_t * text, uint32_t textlen,
2219 uint8_t * digest, uint32_t digestlen)
2221 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2223 printf("\n%s:", str);
2224 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2225 sctp_print_digest(digest, digestlen, "Expected digest");
2226 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2227 if (memcmp(digest, computed_digest, digestlen) != 0) {
2238 * RFC 2202: HMAC-SHA1 test cases
2241 sctp_test_hmac_sha1(void)
2248 uint32_t digestlen = 20;
2253 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2257 * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
2260 memset(key, 0x0b, keylen);
2262 strcpy(text, "Hi There");
2263 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2264 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2265 text, textlen, digest, digestlen) < 0)
2272 * data = "what do ya want for nothing?"
2274 * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2277 strcpy(key, "Jefe");
2279 strcpy(text, "what do ya want for nothing?");
2280 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2281 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2282 text, textlen, digest, digestlen) < 0)
2287 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2289 * data = 0xdd repeated 50 times
2291 * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2294 memset(key, 0xaa, keylen);
2296 memset(text, 0xdd, textlen);
2297 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2298 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2299 text, textlen, digest, digestlen) < 0)
2304 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2306 * data = 0xcd repeated 50 times
2308 * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2311 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);
2313 memset(text, 0xcd, textlen);
2314 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2315 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2316 text, textlen, digest, digestlen) < 0)
2321 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2323 * data = "Test With Truncation"
2325 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
2326 * digest-96 = 0x4c1a03424b55e07fe7f27be1
2329 memset(key, 0x0c, keylen);
2331 strcpy(text, "Test With Truncation");
2332 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2333 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2334 text, textlen, digest, digestlen) < 0)
2339 * key = 0xaa repeated 80 times
2341 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2343 * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2346 memset(key, 0xaa, keylen);
2348 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2349 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2350 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2351 text, textlen, digest, digestlen) < 0)
2356 * key = 0xaa repeated 80 times
2358 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2360 * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2363 memset(key, 0xaa, keylen);
2365 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2366 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2367 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2368 text, textlen, digest, digestlen) < 0)
2371 /* done with all tests */
2373 printf("\nSHA1 test results: %d cases failed", failed);
2375 printf("\nSHA1 test results: all test cases passed");
2379 * RFC 2202: HMAC-MD5 test cases
2382 sctp_test_hmac_md5(void)
2389 uint32_t digestlen = 16;
2394 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2398 * digest = 0x9294727a3638bb1c13f48ef8158bfc9d
2401 memset(key, 0x0b, keylen);
2403 strcpy(text, "Hi There");
2404 digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2405 if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2406 text, textlen, digest, digestlen) < 0)
2413 * data = "what do ya want for nothing?"
2415 * digest = 0x750c783e6ab0b503eaa86e310a5db738
2418 strcpy(key, "Jefe");
2420 strcpy(text, "what do ya want for nothing?");
2421 digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2422 if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2423 text, textlen, digest, digestlen) < 0)
2428 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2430 * data = 0xdd repeated 50 times
2432 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2435 memset(key, 0xaa, keylen);
2437 memset(text, 0xdd, textlen);
2438 digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2439 if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2440 text, textlen, digest, digestlen) < 0)
2445 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2447 * data = 0xcd repeated 50 times
2449 * digest = 0x697eaf0aca3a3aea3a75164746ffaa79
2452 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);
2454 memset(text, 0xcd, textlen);
2455 digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2456 if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2457 text, textlen, digest, digestlen) < 0)
2462 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2464 * data = "Test With Truncation"
2466 * digest = 0x56461ef2342edc00f9bab995690efd4c
2467 * digest-96 = 0x56461ef2342edc00f9bab995
2470 memset(key, 0x0c, keylen);
2472 strcpy(text, "Test With Truncation");
2473 digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2474 if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2475 text, textlen, digest, digestlen) < 0)
2480 * key = 0xaa repeated 80 times
2482 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2484 * digest = 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2487 memset(key, 0xaa, keylen);
2489 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2490 digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2491 if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2492 text, textlen, digest, digestlen) < 0)
2497 * key = 0xaa repeated 80 times
2499 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2501 * digest = 0x6f630fad67cda0ee1fb1f562db3aa53e
2504 memset(key, 0xaa, keylen);
2506 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2507 digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2508 if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2509 text, textlen, digest, digestlen) < 0)
2512 /* done with all tests */
2514 printf("\nMD5 test results: %d cases failed", failed);
2516 printf("\nMD5 test results: all test cases passed");
2520 * test assoc key concatenation
2523 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2524 sctp_key_t * expected_key)
2529 sctp_show_key(key1, "\nkey1");
2530 sctp_show_key(key2, "\nkey2");
2531 key = sctp_compute_hashkey(key1, key2, NULL);
2532 sctp_show_key(expected_key, "\nExpected");
2533 sctp_show_key(key, "\nComputed");
2534 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2541 sctp_free_key(key1);
2542 sctp_free_key(key2);
2543 sctp_free_key(expected_key);
2550 sctp_test_authkey(void)
2552 sctp_key_t *key1, *key2, *expected_key;
2556 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2557 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2558 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2559 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2563 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2564 key2 = sctp_set_key("\x02", 1);
2565 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2566 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2570 key1 = sctp_set_key("\x01", 1);
2571 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2572 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2573 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2577 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2578 key2 = sctp_set_key("\x01", 1);
2579 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2580 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2584 key1 = sctp_set_key("\x01", 1);
2585 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2586 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2587 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2591 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2592 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2593 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);
2594 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2598 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2599 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2600 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);
2601 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2604 /* done with all tests */
2606 printf("\nKey concatenation test results: %d cases failed", failed);
2608 printf("\nKey concatenation test results: all test cases passed");
2612 #if defined(STANDALONE_HMAC_TEST)
2616 sctp_test_hmac_sha1();
2617 sctp_test_hmac_md5();
2618 sctp_test_authkey();
2621 #endif /* STANDALONE_HMAC_TEST */
2623 #endif /* SCTP_HMAC_TEST */