1 /* $FreeBSD: src/sys/netinet6/ah_core.c,v 1.2.2.5 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: ah_core.c,v 1.44 2001/03/12 11:24:39 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * RFC1826/2402 authentication header.
37 /* TODO: have shared routines for hmac-* algorithms */
40 #include "opt_inet6.h"
41 #include "opt_ipsec.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/errno.h>
53 #include <sys/syslog.h>
56 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_var.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet/icmp6.h>
69 #include <netinet6/ipsec.h>
71 #include <netinet6/ipsec6.h>
73 #include <netinet6/ah.h>
75 #include <netinet6/ah6.h>
78 #include <netinet6/esp.h>
80 #include <netinet6/esp6.h>
83 #include <net/pfkeyv2.h>
84 #include <netkey/keydb.h>
86 #include <crypto/sha1.h>
87 #include <crypto/sha2/sha2.h>
89 #include <net/net_osdep.h>
93 static int ah_sumsiz_1216 __P((struct secasvar *));
94 static int ah_sumsiz_zero __P((struct secasvar *));
95 static int ah_none_mature __P((struct secasvar *));
96 static int ah_none_init __P((struct ah_algorithm_state *, struct secasvar *));
97 static void ah_none_loop __P((struct ah_algorithm_state *, caddr_t, size_t));
98 static void ah_none_result __P((struct ah_algorithm_state *, caddr_t));
99 static int ah_keyed_md5_mature __P((struct secasvar *));
100 static int ah_keyed_md5_init __P((struct ah_algorithm_state *,
102 static void ah_keyed_md5_loop __P((struct ah_algorithm_state *, caddr_t,
104 static void ah_keyed_md5_result __P((struct ah_algorithm_state *, caddr_t));
105 static int ah_keyed_sha1_mature __P((struct secasvar *));
106 static int ah_keyed_sha1_init __P((struct ah_algorithm_state *,
108 static void ah_keyed_sha1_loop __P((struct ah_algorithm_state *, caddr_t,
110 static void ah_keyed_sha1_result __P((struct ah_algorithm_state *, caddr_t));
111 static int ah_hmac_md5_mature __P((struct secasvar *));
112 static int ah_hmac_md5_init __P((struct ah_algorithm_state *,
114 static void ah_hmac_md5_loop __P((struct ah_algorithm_state *, caddr_t,
116 static void ah_hmac_md5_result __P((struct ah_algorithm_state *, caddr_t));
117 static int ah_hmac_sha1_mature __P((struct secasvar *));
118 static int ah_hmac_sha1_init __P((struct ah_algorithm_state *,
120 static void ah_hmac_sha1_loop __P((struct ah_algorithm_state *, caddr_t,
122 static void ah_hmac_sha1_result __P((struct ah_algorithm_state *, caddr_t));
123 static int ah_hmac_sha2_256_mature __P((struct secasvar *));
124 static int ah_hmac_sha2_256_init __P((struct ah_algorithm_state *,
126 static void ah_hmac_sha2_256_loop __P((struct ah_algorithm_state *, caddr_t,
128 static void ah_hmac_sha2_256_result __P((struct ah_algorithm_state *, caddr_t));
129 static int ah_hmac_sha2_384_mature __P((struct secasvar *));
130 static int ah_hmac_sha2_384_init __P((struct ah_algorithm_state *,
132 static void ah_hmac_sha2_384_loop __P((struct ah_algorithm_state *, caddr_t,
134 static void ah_hmac_sha2_384_result __P((struct ah_algorithm_state *, caddr_t));
135 static int ah_hmac_sha2_512_mature __P((struct secasvar *));
136 static int ah_hmac_sha2_512_init __P((struct ah_algorithm_state *,
138 static void ah_hmac_sha2_512_loop __P((struct ah_algorithm_state *, caddr_t,
140 static void ah_hmac_sha2_512_result __P((struct ah_algorithm_state *, caddr_t));
142 static void ah_update_mbuf __P((struct mbuf *, int, int,
143 const struct ah_algorithm *, struct ah_algorithm_state *));
145 const struct ah_algorithm *
146 ah_algorithm_lookup(idx)
149 /* checksum algorithms */
150 static struct ah_algorithm ah_algorithms[] = {
151 { ah_sumsiz_1216, ah_hmac_md5_mature, 128, 128, "hmac-md5",
152 ah_hmac_md5_init, ah_hmac_md5_loop,
153 ah_hmac_md5_result, },
154 { ah_sumsiz_1216, ah_hmac_sha1_mature, 160, 160, "hmac-sha1",
155 ah_hmac_sha1_init, ah_hmac_sha1_loop,
156 ah_hmac_sha1_result, },
157 { ah_sumsiz_1216, ah_keyed_md5_mature, 128, 128, "keyed-md5",
158 ah_keyed_md5_init, ah_keyed_md5_loop,
159 ah_keyed_md5_result, },
160 { ah_sumsiz_1216, ah_keyed_sha1_mature, 160, 160, "keyed-sha1",
161 ah_keyed_sha1_init, ah_keyed_sha1_loop,
162 ah_keyed_sha1_result, },
163 { ah_sumsiz_zero, ah_none_mature, 0, 2048, "none",
164 ah_none_init, ah_none_loop, ah_none_result, },
165 { ah_sumsiz_1216, ah_hmac_sha2_256_mature, 256, 256,
167 ah_hmac_sha2_256_init, ah_hmac_sha2_256_loop,
168 ah_hmac_sha2_256_result, },
169 { ah_sumsiz_1216, ah_hmac_sha2_384_mature, 384, 384,
171 ah_hmac_sha2_384_init, ah_hmac_sha2_384_loop,
172 ah_hmac_sha2_384_result, },
173 { ah_sumsiz_1216, ah_hmac_sha2_512_mature, 512, 512,
175 ah_hmac_sha2_512_init, ah_hmac_sha2_512_loop,
176 ah_hmac_sha2_512_result, },
180 case SADB_AALG_MD5HMAC:
181 return &ah_algorithms[0];
182 case SADB_AALG_SHA1HMAC:
183 return &ah_algorithms[1];
184 case SADB_X_AALG_MD5:
185 return &ah_algorithms[2];
186 case SADB_X_AALG_SHA:
187 return &ah_algorithms[3];
188 case SADB_X_AALG_NULL:
189 return &ah_algorithms[4];
190 case SADB_X_AALG_SHA2_256:
191 return &ah_algorithms[5];
192 case SADB_X_AALG_SHA2_384:
193 return &ah_algorithms[6];
194 case SADB_X_AALG_SHA2_512:
195 return &ah_algorithms[7];
204 struct secasvar *sav;
208 if (sav->flags & SADB_X_EXT_OLD)
216 struct secasvar *sav;
225 struct secasvar *sav;
227 if (sav->sah->saidx.proto == IPPROTO_AH) {
229 "ah_none_mature: protocol and algorithm mismatch.\n"));
236 ah_none_init(state, sav)
237 struct ah_algorithm_state *state;
238 struct secasvar *sav;
245 ah_none_loop(state, addr, len)
246 struct ah_algorithm_state *state;
253 ah_none_result(state, addr)
254 struct ah_algorithm_state *state;
260 ah_keyed_md5_mature(sav)
261 struct secasvar *sav;
263 /* anything is okay */
268 ah_keyed_md5_init(state, sav)
269 struct ah_algorithm_state *state;
270 struct secasvar *sav;
277 panic("ah_keyed_md5_init: what?");
280 state->foo = (void *)malloc(sizeof(MD5_CTX), M_TEMP, M_NOWAIT);
281 if (state->foo == NULL)
284 MD5Init((MD5_CTX *)state->foo);
286 MD5Update((MD5_CTX *)state->foo,
287 (u_int8_t *)_KEYBUF(state->sav->key_auth),
288 (u_int)_KEYLEN(state->sav->key_auth));
292 * We cannot simply use md5_pad() since the function
293 * won't update the total length.
295 if (_KEYLEN(state->sav->key_auth) < 56)
296 padlen = 64 - 8 - _KEYLEN(state->sav->key_auth);
298 padlen = 64 + 64 - 8 - _KEYLEN(state->sav->key_auth);
299 keybitlen = _KEYLEN(state->sav->key_auth);
303 MD5Update((MD5_CTX *)state->foo, &buf[0], 1);
306 bzero(buf, sizeof(buf));
307 while (sizeof(buf) < padlen) {
308 MD5Update((MD5_CTX *)state->foo, &buf[0], sizeof(buf));
309 padlen -= sizeof(buf);
312 MD5Update((MD5_CTX *)state->foo, &buf[0], padlen);
315 buf[0] = (keybitlen >> 0) & 0xff;
316 buf[1] = (keybitlen >> 8) & 0xff;
317 buf[2] = (keybitlen >> 16) & 0xff;
318 buf[3] = (keybitlen >> 24) & 0xff;
319 MD5Update((MD5_CTX *)state->foo, buf, 8);
326 ah_keyed_md5_loop(state, addr, len)
327 struct ah_algorithm_state *state;
332 panic("ah_keyed_md5_loop: what?");
334 MD5Update((MD5_CTX *)state->foo, addr, len);
338 ah_keyed_md5_result(state, addr)
339 struct ah_algorithm_state *state;
345 panic("ah_keyed_md5_result: what?");
348 MD5Update((MD5_CTX *)state->foo,
349 (u_int8_t *)_KEYBUF(state->sav->key_auth),
350 (u_int)_KEYLEN(state->sav->key_auth));
352 MD5Final(&digest[0], (MD5_CTX *)state->foo);
353 free(state->foo, M_TEMP);
354 bcopy(&digest[0], (void *)addr, sizeof(digest));
358 ah_keyed_sha1_mature(sav)
359 struct secasvar *sav;
361 const struct ah_algorithm *algo;
363 if (!sav->key_auth) {
364 ipseclog((LOG_ERR, "ah_keyed_sha1_mature: no key is given.\n"));
368 algo = ah_algorithm_lookup(sav->alg_auth);
370 ipseclog((LOG_ERR, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
374 if (sav->key_auth->sadb_key_bits < algo->keymin
375 || algo->keymax < sav->key_auth->sadb_key_bits) {
377 "ah_keyed_sha1_mature: invalid key length %d.\n",
378 sav->key_auth->sadb_key_bits));
386 ah_keyed_sha1_init(state, sav)
387 struct ah_algorithm_state *state;
388 struct secasvar *sav;
396 panic("ah_keyed_sha1_init: what?");
399 state->foo = (void *)malloc(sizeof(SHA1_CTX), M_TEMP, M_NOWAIT);
403 ctxt = (SHA1_CTX *)state->foo;
407 SHA1Update(ctxt, (u_int8_t *)_KEYBUF(state->sav->key_auth),
408 (u_int)_KEYLEN(state->sav->key_auth));
413 if (_KEYLEN(state->sav->key_auth) < 56)
414 padlen = 64 - 8 - _KEYLEN(state->sav->key_auth);
416 padlen = 64 + 64 - 8 - _KEYLEN(state->sav->key_auth);
417 keybitlen = _KEYLEN(state->sav->key_auth);
421 SHA1Update(ctxt, &buf[0], 1);
424 bzero(buf, sizeof(buf));
425 while (sizeof(buf) < padlen) {
426 SHA1Update(ctxt, &buf[0], sizeof(buf));
427 padlen -= sizeof(buf);
430 SHA1Update(ctxt, &buf[0], padlen);
433 buf[0] = (keybitlen >> 0) & 0xff;
434 buf[1] = (keybitlen >> 8) & 0xff;
435 buf[2] = (keybitlen >> 16) & 0xff;
436 buf[3] = (keybitlen >> 24) & 0xff;
437 SHA1Update(ctxt, buf, 8);
444 ah_keyed_sha1_loop(state, addr, len)
445 struct ah_algorithm_state *state;
451 if (!state || !state->foo)
452 panic("ah_keyed_sha1_loop: what?");
453 ctxt = (SHA1_CTX *)state->foo;
455 SHA1Update(ctxt, (caddr_t)addr, (size_t)len);
459 ah_keyed_sha1_result(state, addr)
460 struct ah_algorithm_state *state;
463 u_char digest[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
466 if (!state || !state->foo)
467 panic("ah_keyed_sha1_result: what?");
468 ctxt = (SHA1_CTX *)state->foo;
471 SHA1Update(ctxt, (u_int8_t *)_KEYBUF(state->sav->key_auth),
472 (u_int)_KEYLEN(state->sav->key_auth));
474 SHA1Final((caddr_t)&digest[0], ctxt);
475 bcopy(&digest[0], (void *)addr, HMACSIZE);
477 free(state->foo, M_TEMP);
481 ah_hmac_md5_mature(sav)
482 struct secasvar *sav;
484 const struct ah_algorithm *algo;
486 if (!sav->key_auth) {
487 ipseclog((LOG_ERR, "ah_hmac_md5_mature: no key is given.\n"));
491 algo = ah_algorithm_lookup(sav->alg_auth);
493 ipseclog((LOG_ERR, "ah_hmac_md5_mature: unsupported algorithm.\n"));
497 if (sav->key_auth->sadb_key_bits < algo->keymin
498 || algo->keymax < sav->key_auth->sadb_key_bits) {
500 "ah_hmac_md5_mature: invalid key length %d.\n",
501 sav->key_auth->sadb_key_bits));
509 ah_hmac_md5_init(state, sav)
510 struct ah_algorithm_state *state;
511 struct secasvar *sav;
522 panic("ah_hmac_md5_init: what?");
525 state->foo = (void *)malloc(64 + 64 + sizeof(MD5_CTX), M_TEMP, M_NOWAIT);
529 ipad = (u_char *)state->foo;
530 opad = (u_char *)(ipad + 64);
531 ctxt = (MD5_CTX *)(opad + 64);
533 /* compress the key if necessery */
534 if (64 < _KEYLEN(state->sav->key_auth)) {
536 MD5Update(ctxt, _KEYBUF(state->sav->key_auth),
537 _KEYLEN(state->sav->key_auth));
538 MD5Final(&tk[0], ctxt);
542 key = _KEYBUF(state->sav->key_auth);
543 keylen = _KEYLEN(state->sav->key_auth);
548 bcopy(key, ipad, keylen);
549 bcopy(key, opad, keylen);
550 for (i = 0; i < 64; i++) {
556 MD5Update(ctxt, ipad, 64);
562 ah_hmac_md5_loop(state, addr, len)
563 struct ah_algorithm_state *state;
569 if (!state || !state->foo)
570 panic("ah_hmac_md5_loop: what?");
571 ctxt = (MD5_CTX *)(((caddr_t)state->foo) + 128);
572 MD5Update(ctxt, addr, len);
576 ah_hmac_md5_result(state, addr)
577 struct ah_algorithm_state *state;
585 if (!state || !state->foo)
586 panic("ah_hmac_md5_result: what?");
588 ipad = (u_char *)state->foo;
589 opad = (u_char *)(ipad + 64);
590 ctxt = (MD5_CTX *)(opad + 64);
592 MD5Final(&digest[0], ctxt);
595 MD5Update(ctxt, opad, 64);
596 MD5Update(ctxt, &digest[0], sizeof(digest));
597 MD5Final(&digest[0], ctxt);
599 bcopy(&digest[0], (void *)addr, HMACSIZE);
601 free(state->foo, M_TEMP);
605 ah_hmac_sha1_mature(sav)
606 struct secasvar *sav;
608 const struct ah_algorithm *algo;
610 if (!sav->key_auth) {
611 ipseclog((LOG_ERR, "ah_hmac_sha1_mature: no key is given.\n"));
615 algo = ah_algorithm_lookup(sav->alg_auth);
617 ipseclog((LOG_ERR, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
621 if (sav->key_auth->sadb_key_bits < algo->keymin
622 || algo->keymax < sav->key_auth->sadb_key_bits) {
624 "ah_hmac_sha1_mature: invalid key length %d.\n",
625 sav->key_auth->sadb_key_bits));
633 ah_hmac_sha1_init(state, sav)
634 struct ah_algorithm_state *state;
635 struct secasvar *sav;
640 u_char tk[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
646 panic("ah_hmac_sha1_init: what?");
649 state->foo = (void *)malloc(64 + 64 + sizeof(SHA1_CTX),
654 ipad = (u_char *)state->foo;
655 opad = (u_char *)(ipad + 64);
656 ctxt = (SHA1_CTX *)(opad + 64);
658 /* compress the key if necessery */
659 if (64 < _KEYLEN(state->sav->key_auth)) {
661 SHA1Update(ctxt, _KEYBUF(state->sav->key_auth),
662 _KEYLEN(state->sav->key_auth));
663 SHA1Final(&tk[0], ctxt);
665 keylen = SHA1_RESULTLEN;
667 key = _KEYBUF(state->sav->key_auth);
668 keylen = _KEYLEN(state->sav->key_auth);
673 bcopy(key, ipad, keylen);
674 bcopy(key, opad, keylen);
675 for (i = 0; i < 64; i++) {
681 SHA1Update(ctxt, ipad, 64);
687 ah_hmac_sha1_loop(state, addr, len)
688 struct ah_algorithm_state *state;
694 if (!state || !state->foo)
695 panic("ah_hmac_sha1_loop: what?");
697 ctxt = (SHA1_CTX *)(((u_char *)state->foo) + 128);
698 SHA1Update(ctxt, (caddr_t)addr, (size_t)len);
702 ah_hmac_sha1_result(state, addr)
703 struct ah_algorithm_state *state;
706 u_char digest[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
711 if (!state || !state->foo)
712 panic("ah_hmac_sha1_result: what?");
714 ipad = (u_char *)state->foo;
715 opad = (u_char *)(ipad + 64);
716 ctxt = (SHA1_CTX *)(opad + 64);
718 SHA1Final((caddr_t)&digest[0], ctxt);
721 SHA1Update(ctxt, opad, 64);
722 SHA1Update(ctxt, (caddr_t)&digest[0], sizeof(digest));
723 SHA1Final((caddr_t)&digest[0], ctxt);
725 bcopy(&digest[0], (void *)addr, HMACSIZE);
727 free(state->foo, M_TEMP);
731 ah_hmac_sha2_256_mature(sav)
732 struct secasvar *sav;
734 const struct ah_algorithm *algo;
736 if (!sav->key_auth) {
738 "ah_hmac_sha2_256_mature: no key is given.\n"));
742 algo = ah_algorithm_lookup(sav->alg_auth);
745 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
749 if (sav->key_auth->sadb_key_bits < algo->keymin ||
750 algo->keymax < sav->key_auth->sadb_key_bits) {
752 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
753 sav->key_auth->sadb_key_bits));
761 ah_hmac_sha2_256_init(state, sav)
762 struct ah_algorithm_state *state;
763 struct secasvar *sav;
768 u_char tk[SHA256_DIGEST_LENGTH];
774 panic("ah_hmac_sha2_256_init: what?");
777 state->foo = (void *)malloc(64 + 64 + sizeof(SHA256_CTX),
782 ipad = (u_char *)state->foo;
783 opad = (u_char *)(ipad + 64);
784 ctxt = (SHA256_CTX *)(opad + 64);
786 /* compress the key if necessery */
787 if (64 < _KEYLEN(state->sav->key_auth)) {
788 bzero(tk, sizeof(tk));
789 bzero(ctxt, sizeof(*ctxt));
791 SHA256_Update(ctxt, _KEYBUF(state->sav->key_auth),
792 _KEYLEN(state->sav->key_auth));
793 SHA256_Final(&tk[0], ctxt);
795 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
797 key = _KEYBUF(state->sav->key_auth);
798 keylen = _KEYLEN(state->sav->key_auth);
803 bcopy(key, ipad, keylen);
804 bcopy(key, opad, keylen);
805 for (i = 0; i < 64; i++) {
810 bzero(ctxt, sizeof(*ctxt));
812 SHA256_Update(ctxt, ipad, 64);
818 ah_hmac_sha2_256_loop(state, addr, len)
819 struct ah_algorithm_state *state;
825 if (!state || !state->foo)
826 panic("ah_hmac_sha2_256_loop: what?");
828 ctxt = (SHA256_CTX *)(((u_char *)state->foo) + 128);
829 SHA256_Update(ctxt, (caddr_t)addr, (size_t)len);
833 ah_hmac_sha2_256_result(state, addr)
834 struct ah_algorithm_state *state;
837 u_char digest[SHA256_DIGEST_LENGTH];
842 if (!state || !state->foo)
843 panic("ah_hmac_sha2_256_result: what?");
845 ipad = (u_char *)state->foo;
846 opad = (u_char *)(ipad + 64);
847 ctxt = (SHA256_CTX *)(opad + 64);
849 SHA256_Final((caddr_t)&digest[0], ctxt);
851 bzero(ctxt, sizeof(*ctxt));
853 SHA256_Update(ctxt, opad, 64);
854 SHA256_Update(ctxt, (caddr_t)&digest[0], sizeof(digest));
855 SHA256_Final((caddr_t)&digest[0], ctxt);
857 bcopy(&digest[0], (void *)addr, HMACSIZE);
859 free(state->foo, M_TEMP);
863 ah_hmac_sha2_384_mature(sav)
864 struct secasvar *sav;
866 const struct ah_algorithm *algo;
868 if (!sav->key_auth) {
870 "ah_hmac_sha2_384_mature: no key is given.\n"));
874 algo = ah_algorithm_lookup(sav->alg_auth);
877 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
881 if (sav->key_auth->sadb_key_bits < algo->keymin ||
882 algo->keymax < sav->key_auth->sadb_key_bits) {
884 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
885 sav->key_auth->sadb_key_bits));
893 ah_hmac_sha2_384_init(state, sav)
894 struct ah_algorithm_state *state;
895 struct secasvar *sav;
900 u_char tk[SHA384_DIGEST_LENGTH];
906 panic("ah_hmac_sha2_384_init: what?");
909 state->foo = (void *)malloc(64 + 64 + sizeof(SHA384_CTX),
913 bzero(state->foo, 64 + 64 + sizeof(SHA384_CTX));
915 ipad = (u_char *)state->foo;
916 opad = (u_char *)(ipad + 64);
917 ctxt = (SHA384_CTX *)(opad + 64);
919 /* compress the key if necessery */
920 if (64 < _KEYLEN(state->sav->key_auth)) {
921 bzero(tk, sizeof(tk));
922 bzero(ctxt, sizeof(*ctxt));
924 SHA384_Update(ctxt, _KEYBUF(state->sav->key_auth),
925 _KEYLEN(state->sav->key_auth));
926 SHA384_Final(&tk[0], ctxt);
928 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
930 key = _KEYBUF(state->sav->key_auth);
931 keylen = _KEYLEN(state->sav->key_auth);
936 bcopy(key, ipad, keylen);
937 bcopy(key, opad, keylen);
938 for (i = 0; i < 64; i++) {
943 bzero(ctxt, sizeof(*ctxt));
945 SHA384_Update(ctxt, ipad, 64);
951 ah_hmac_sha2_384_loop(state, addr, len)
952 struct ah_algorithm_state *state;
958 if (!state || !state->foo)
959 panic("ah_hmac_sha2_384_loop: what?");
961 ctxt = (SHA384_CTX *)(((u_char *)state->foo) + 128);
962 SHA384_Update(ctxt, (caddr_t)addr, (size_t)len);
966 ah_hmac_sha2_384_result(state, addr)
967 struct ah_algorithm_state *state;
970 u_char digest[SHA384_DIGEST_LENGTH];
975 if (!state || !state->foo)
976 panic("ah_hmac_sha2_384_result: what?");
978 ipad = (u_char *)state->foo;
979 opad = (u_char *)(ipad + 64);
980 ctxt = (SHA384_CTX *)(opad + 64);
982 SHA384_Final((caddr_t)&digest[0], ctxt);
984 bzero(ctxt, sizeof(*ctxt));
986 SHA384_Update(ctxt, opad, 64);
987 SHA384_Update(ctxt, (caddr_t)&digest[0], sizeof(digest));
988 SHA384_Final((caddr_t)&digest[0], ctxt);
990 bcopy(&digest[0], (void *)addr, HMACSIZE);
992 free(state->foo, M_TEMP);
996 ah_hmac_sha2_512_mature(sav)
997 struct secasvar *sav;
999 const struct ah_algorithm *algo;
1001 if (!sav->key_auth) {
1003 "ah_hmac_sha2_512_mature: no key is given.\n"));
1007 algo = ah_algorithm_lookup(sav->alg_auth);
1010 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
1014 if (sav->key_auth->sadb_key_bits < algo->keymin ||
1015 algo->keymax < sav->key_auth->sadb_key_bits) {
1017 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1018 sav->key_auth->sadb_key_bits));
1026 ah_hmac_sha2_512_init(state, sav)
1027 struct ah_algorithm_state *state;
1028 struct secasvar *sav;
1033 u_char tk[SHA512_DIGEST_LENGTH];
1039 panic("ah_hmac_sha2_512_init: what?");
1042 state->foo = (void *)malloc(64 + 64 + sizeof(SHA512_CTX),
1046 bzero(state->foo, 64 + 64 + sizeof(SHA512_CTX));
1048 ipad = (u_char *)state->foo;
1049 opad = (u_char *)(ipad + 64);
1050 ctxt = (SHA512_CTX *)(opad + 64);
1052 /* compress the key if necessery */
1053 if (64 < _KEYLEN(state->sav->key_auth)) {
1054 bzero(tk, sizeof(tk));
1055 bzero(ctxt, sizeof(*ctxt));
1057 SHA512_Update(ctxt, _KEYBUF(state->sav->key_auth),
1058 _KEYLEN(state->sav->key_auth));
1059 SHA512_Final(&tk[0], ctxt);
1061 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
1063 key = _KEYBUF(state->sav->key_auth);
1064 keylen = _KEYLEN(state->sav->key_auth);
1069 bcopy(key, ipad, keylen);
1070 bcopy(key, opad, keylen);
1071 for (i = 0; i < 64; i++) {
1076 bzero(ctxt, sizeof(*ctxt));
1078 SHA512_Update(ctxt, ipad, 64);
1084 ah_hmac_sha2_512_loop(state, addr, len)
1085 struct ah_algorithm_state *state;
1091 if (!state || !state->foo)
1092 panic("ah_hmac_sha2_512_loop: what?");
1094 ctxt = (SHA512_CTX *)(((u_char *)state->foo) + 128);
1095 SHA512_Update(ctxt, (caddr_t)addr, (size_t)len);
1099 ah_hmac_sha2_512_result(state, addr)
1100 struct ah_algorithm_state *state;
1103 u_char digest[SHA512_DIGEST_LENGTH];
1108 if (!state || !state->foo)
1109 panic("ah_hmac_sha2_512_result: what?");
1111 ipad = (u_char *)state->foo;
1112 opad = (u_char *)(ipad + 64);
1113 ctxt = (SHA512_CTX *)(opad + 64);
1115 SHA512_Final((caddr_t)&digest[0], ctxt);
1117 bzero(ctxt, sizeof(*ctxt));
1119 SHA512_Update(ctxt, opad, 64);
1120 SHA512_Update(ctxt, (caddr_t)&digest[0], sizeof(digest));
1121 SHA512_Final((caddr_t)&digest[0], ctxt);
1123 bcopy(&digest[0], (void *)addr, HMACSIZE);
1125 free(state->foo, M_TEMP);
1128 /*------------------------------------------------------------*/
1131 * go generate the checksum.
1134 ah_update_mbuf(m, off, len, algo, algos)
1138 const struct ah_algorithm *algo;
1139 struct ah_algorithm_state *algos;
1144 /* easy case first */
1145 if (off + len <= m->m_len) {
1146 (algo->update)(algos, mtod(m, caddr_t) + off, len);
1150 for (n = m; n; n = n->m_next) {
1158 panic("ah_update_mbuf: wrong offset specified");
1160 for (/* nothing */; n && len > 0; n = n->m_next) {
1163 if (n->m_len - off < len)
1164 tlen = n->m_len - off;
1168 (algo->update)(algos, mtod(n, caddr_t) + off, tlen);
1177 * Go generate the checksum. This function won't modify the mbuf chain
1180 * NOTE: the function does not free mbuf on failure.
1181 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1184 ah4_calccksum(m, ahdat, len, algo, sav)
1188 const struct ah_algorithm *algo;
1189 struct secasvar *sav;
1193 size_t advancewidth;
1194 struct ah_algorithm_state algos;
1195 u_char sumbuf[AH_MAXSUMSIZE];
1198 struct mbuf *n = NULL;
1200 if ((m->m_flags & M_PKTHDR) == 0)
1204 hdrtype = -1; /* dummy, it is called IPPROTO_IP */
1208 error = (algo->init)(&algos, sav);
1212 advancewidth = 0; /* safety */
1217 case -1: /* first one only */
1220 * copy ip hdr, modify to fit the AH checksum rule,
1221 * then take a checksum.
1226 m_copydata(m, off, sizeof(iphdr), (caddr_t)&iphdr);
1228 hlen = IP_VHL_HL(iphdr.ip_vhl) << 2;
1230 hlen = iphdr.ip_hl << 2;
1233 iphdr.ip_sum = htons(0);
1234 if (ip4_ah_cleartos)
1236 iphdr.ip_off = htons(ntohs(iphdr.ip_off) & ip4_ah_offsetmask);
1237 (algo->update)(&algos, (caddr_t)&iphdr, sizeof(struct ip));
1239 if (hlen != sizeof(struct ip)) {
1243 if (hlen > MCLBYTES) {
1247 MGET(n, M_DONTWAIT, MT_DATA);
1248 if (n && hlen > MLEN) {
1249 MCLGET(n, M_DONTWAIT);
1250 if ((n->m_flags & M_EXT) == 0) {
1259 m_copydata(m, off, hlen, mtod(n, caddr_t));
1262 * IP options processing.
1263 * See RFC2402 appendix A.
1265 p = mtod(n, u_char *);
1266 i = sizeof(struct ip);
1268 if (i + IPOPT_OPTVAL >= hlen) {
1269 ipseclog((LOG_ERR, "ah4_calccksum: "
1270 "invalid IP option\n"));
1274 if (p[i + IPOPT_OPTVAL] == IPOPT_EOL ||
1275 p[i + IPOPT_OPTVAL] == IPOPT_NOP ||
1276 i + IPOPT_OLEN < hlen)
1280 "ah4_calccksum: invalid IP option "
1282 p[i + IPOPT_OPTVAL]));
1288 switch (p[i + IPOPT_OPTVAL]) {
1294 case IPOPT_SECURITY: /* 0x82 */
1295 case 0x85: /* Extended security */
1296 case 0x86: /* Commercial security */
1297 case 0x94: /* Router alert */
1298 case 0x95: /* RFC1770 */
1299 l = p[i + IPOPT_OLEN];
1305 l = p[i + IPOPT_OLEN];
1311 if (l < 1 || hlen - i < l) {
1314 "ah4_calccksum: invalid IP option "
1315 "(type=%02x len=%02x)\n",
1316 p[i + IPOPT_OPTVAL],
1317 p[i + IPOPT_OLEN]));
1323 if (p[i + IPOPT_OPTVAL] == IPOPT_EOL)
1327 p = mtod(n, u_char *) + sizeof(struct ip);
1328 (algo->update)(&algos, p, hlen - sizeof(struct ip));
1334 hdrtype = (iphdr.ip_p) & 0xff;
1335 advancewidth = hlen;
1346 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1347 hdrsiz = (sav->flags & SADB_X_EXT_OLD)
1349 : sizeof(struct newah);
1350 siz = (*algo->sumsiz)(sav);
1351 totlen = (ah.ah_len + 2) << 2;
1354 * special treatment is necessary for the first one, not others
1357 if (totlen > m->m_pkthdr.len - off ||
1358 totlen > MCLBYTES) {
1362 MGET(n, M_DONTWAIT, MT_DATA);
1363 if (n && totlen > MLEN) {
1364 MCLGET(n, M_DONTWAIT);
1365 if ((n->m_flags & M_EXT) == 0) {
1374 m_copydata(m, off, totlen, mtod(n, caddr_t));
1376 bzero(mtod(n, caddr_t) + hdrsiz, siz);
1377 (algo->update)(&algos, mtod(n, caddr_t), n->m_len);
1381 ah_update_mbuf(m, off, totlen, algo, &algos);
1384 hdrtype = ah.ah_nxt;
1385 advancewidth = totlen;
1390 ah_update_mbuf(m, off, m->m_pkthdr.len - off, algo, &algos);
1391 advancewidth = m->m_pkthdr.len - off;
1395 off += advancewidth;
1396 if (off < m->m_pkthdr.len)
1399 if (len < (*algo->sumsiz)(sav)) {
1404 (algo->result)(&algos, &sumbuf[0]);
1405 bcopy(&sumbuf[0], ahdat, (*algo->sumsiz)(sav));
1420 * Go generate the checksum. This function won't modify the mbuf chain
1423 * NOTE: the function does not free mbuf on failure.
1424 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1427 ah6_calccksum(m, ahdat, len, algo, sav)
1431 const struct ah_algorithm *algo;
1432 struct secasvar *sav;
1436 struct mbuf *n = NULL;
1439 struct ah_algorithm_state algos;
1440 u_char sumbuf[AH_MAXSUMSIZE];
1442 if ((m->m_flags & M_PKTHDR) == 0)
1445 error = (algo->init)(&algos, sav);
1450 proto = IPPROTO_IPV6;
1455 newoff = ip6_nexthdr(m, off, proto, &nxt);
1457 newoff = m->m_pkthdr.len;
1458 else if (newoff <= off) {
1466 * special treatment is necessary for the first one, not others
1469 struct ip6_hdr ip6copy;
1471 if (newoff - off != sizeof(struct ip6_hdr)) {
1476 m_copydata(m, off, newoff - off, (caddr_t)&ip6copy);
1478 ip6copy.ip6_flow = 0;
1479 ip6copy.ip6_vfc &= ~IPV6_VERSION_MASK;
1480 ip6copy.ip6_vfc |= IPV6_VERSION;
1481 ip6copy.ip6_hlim = 0;
1482 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy.ip6_src))
1483 ip6copy.ip6_src.s6_addr16[1] = 0x0000;
1484 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy.ip6_dst))
1485 ip6copy.ip6_dst.s6_addr16[1] = 0x0000;
1486 (algo->update)(&algos, (caddr_t)&ip6copy,
1487 sizeof(struct ip6_hdr));
1489 newoff = m->m_pkthdr.len;
1490 ah_update_mbuf(m, off, m->m_pkthdr.len - off, algo,
1500 hdrsiz = (sav->flags & SADB_X_EXT_OLD)
1502 : sizeof(struct newah);
1503 siz = (*algo->sumsiz)(sav);
1506 * special treatment is necessary for the first one, not others
1509 if (newoff - off > MCLBYTES) {
1513 MGET(n, M_DONTWAIT, MT_DATA);
1514 if (n && newoff - off > MLEN) {
1515 MCLGET(n, M_DONTWAIT);
1516 if ((n->m_flags & M_EXT) == 0) {
1525 m_copydata(m, off, newoff - off, mtod(n, caddr_t));
1526 n->m_len = newoff - off;
1527 bzero(mtod(n, caddr_t) + hdrsiz, siz);
1528 (algo->update)(&algos, mtod(n, caddr_t), n->m_len);
1532 ah_update_mbuf(m, off, newoff - off, algo, &algos);
1537 case IPPROTO_HOPOPTS:
1538 case IPPROTO_DSTOPTS:
1540 struct ip6_ext *ip6e;
1542 u_int8_t *p, *optend, *optp;
1544 if (newoff - off > MCLBYTES) {
1548 MGET(n, M_DONTWAIT, MT_DATA);
1549 if (n && newoff - off > MLEN) {
1550 MCLGET(n, M_DONTWAIT);
1551 if ((n->m_flags & M_EXT) == 0) {
1560 m_copydata(m, off, newoff - off, mtod(n, caddr_t));
1561 n->m_len = newoff - off;
1563 ip6e = mtod(n, struct ip6_ext *);
1564 hdrlen = (ip6e->ip6e_len + 1) << 3;
1565 if (newoff - off < hdrlen) {
1571 p = mtod(n, u_int8_t *);
1572 optend = p + hdrlen;
1575 * ICV calculation for the options header including all
1576 * options. This part is a little tricky since there are
1577 * two type of options; mutable and immutable. We try to
1578 * null-out mutable ones here.
1581 while (optp < optend) {
1582 if (optp[0] == IP6OPT_PAD1)
1585 if (optp + 2 > optend) {
1591 optlen = optp[1] + 2;
1593 if (optp[0] & IP6OPT_MUTABLE)
1594 bzero(optp + 2, optlen - 2);
1600 (algo->update)(&algos, mtod(n, caddr_t), n->m_len);
1606 case IPPROTO_ROUTING:
1608 * For an input packet, we can just calculate `as is'.
1609 * For an output packet, we assume ip6_output have already
1610 * made packet how it will be received at the final
1616 ah_update_mbuf(m, off, newoff - off, algo, &algos);
1620 if (newoff < m->m_pkthdr.len) {
1626 if (len < (*algo->sumsiz)(sav)) {
1631 (algo->result)(&algos, &sumbuf[0]);
1632 bcopy(&sumbuf[0], ahdat, (*algo->sumsiz)(sav));