2 /* $KAME: ah_core.c,v 1.59 2003/07/25 10:17:14 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>
77 #include <netinet6/ah_aesxcbcmac.h>
79 #include <netinet6/esp.h>
81 #include <netinet6/esp6.h>
84 #include <net/pfkeyv2.h>
85 #include <netkey/keydb.h>
87 #define MD5_RESULTLEN 16
88 #include <crypto/sha1.h>
89 #include <crypto/sha2/sha2.h>
90 #include <opencrypto/rmd160.h>
91 #define RIPEMD160_RESULTLEN 20
93 #include <net/net_osdep.h>
95 static int ah_sumsiz_1216 __P((struct secasvar *));
96 static int ah_sumsiz_zero __P((struct secasvar *));
97 static int ah_common_mature __P((struct secasvar *));
98 static int ah_none_mature __P((struct secasvar *));
99 static int ah_none_init __P((struct ah_algorithm_state *, struct secasvar *));
100 static void ah_none_loop __P((struct ah_algorithm_state *, u_int8_t *, size_t));
101 static void ah_none_result __P((struct ah_algorithm_state *,
102 u_int8_t *, size_t));
103 static int ah_keyed_md5_mature __P((struct secasvar *));
104 static int ah_keyed_md5_init __P((struct ah_algorithm_state *,
106 static void ah_keyed_md5_loop __P((struct ah_algorithm_state *, u_int8_t *,
108 static void ah_keyed_md5_result __P((struct ah_algorithm_state *,
109 u_int8_t *, size_t));
110 static int ah_keyed_sha1_init __P((struct ah_algorithm_state *,
112 static void ah_keyed_sha1_loop __P((struct ah_algorithm_state *, u_int8_t *,
114 static void ah_keyed_sha1_result __P((struct ah_algorithm_state *, u_int8_t *,
116 static int ah_hmac_md5_init __P((struct ah_algorithm_state *,
118 static void ah_hmac_md5_loop __P((struct ah_algorithm_state *, u_int8_t *,
120 static void ah_hmac_md5_result __P((struct ah_algorithm_state *,
121 u_int8_t *, size_t));
122 static int ah_hmac_sha1_init __P((struct ah_algorithm_state *,
124 static void ah_hmac_sha1_loop __P((struct ah_algorithm_state *, u_int8_t *,
126 static void ah_hmac_sha1_result __P((struct ah_algorithm_state *,
127 u_int8_t *, size_t));
128 static int ah_hmac_sha2_256_init __P((struct ah_algorithm_state *,
130 static void ah_hmac_sha2_256_loop __P((struct ah_algorithm_state *, u_int8_t *,
132 static void ah_hmac_sha2_256_result __P((struct ah_algorithm_state *,
133 u_int8_t *, size_t));
134 static int ah_hmac_sha2_384_init __P((struct ah_algorithm_state *,
136 static void ah_hmac_sha2_384_loop __P((struct ah_algorithm_state *, u_int8_t *,
138 static void ah_hmac_sha2_384_result __P((struct ah_algorithm_state *,
139 u_int8_t *, size_t));
140 static int ah_hmac_sha2_512_init __P((struct ah_algorithm_state *,
142 static void ah_hmac_sha2_512_loop __P((struct ah_algorithm_state *, u_int8_t *,
144 static void ah_hmac_sha2_512_result __P((struct ah_algorithm_state *,
145 u_int8_t *, size_t));
146 static int ah_hmac_ripemd160_init __P((struct ah_algorithm_state *,
148 static void ah_hmac_ripemd160_loop __P((struct ah_algorithm_state *, u_int8_t *,
150 static void ah_hmac_ripemd160_result __P((struct ah_algorithm_state *,
151 u_int8_t *, size_t));
153 static void ah_update_mbuf __P((struct mbuf *, int, int,
154 const struct ah_algorithm *, struct ah_algorithm_state *));
156 /* checksum algorithms */
157 static const struct ah_algorithm ah_algorithms[] = {
158 { ah_sumsiz_1216, ah_common_mature, 128, 128, "hmac-md5",
159 ah_hmac_md5_init, ah_hmac_md5_loop,
160 ah_hmac_md5_result, },
161 { ah_sumsiz_1216, ah_common_mature, 160, 160, "hmac-sha1",
162 ah_hmac_sha1_init, ah_hmac_sha1_loop,
163 ah_hmac_sha1_result, },
164 { ah_sumsiz_1216, ah_keyed_md5_mature, 128, 128, "keyed-md5",
165 ah_keyed_md5_init, ah_keyed_md5_loop,
166 ah_keyed_md5_result, },
167 { ah_sumsiz_1216, ah_common_mature, 160, 160, "keyed-sha1",
168 ah_keyed_sha1_init, ah_keyed_sha1_loop,
169 ah_keyed_sha1_result, },
170 { ah_sumsiz_zero, ah_none_mature, 0, 2048, "none",
171 ah_none_init, ah_none_loop, ah_none_result, },
172 { ah_sumsiz_1216, ah_common_mature, 256, 256,
174 ah_hmac_sha2_256_init, ah_hmac_sha2_256_loop,
175 ah_hmac_sha2_256_result, },
176 { ah_sumsiz_1216, ah_common_mature, 384, 384,
178 ah_hmac_sha2_384_init, ah_hmac_sha2_384_loop,
179 ah_hmac_sha2_384_result, },
180 { ah_sumsiz_1216, ah_common_mature, 512, 512,
182 ah_hmac_sha2_512_init, ah_hmac_sha2_512_loop,
183 ah_hmac_sha2_512_result, },
184 { ah_sumsiz_1216, ah_common_mature, 160, 160,
186 ah_hmac_ripemd160_init, ah_hmac_ripemd160_loop,
187 ah_hmac_ripemd160_result, },
188 { ah_sumsiz_1216, ah_common_mature, 128, 128,
190 ah_aes_xcbc_mac_init, ah_aes_xcbc_mac_loop,
191 ah_aes_xcbc_mac_result, },
192 { ah_sumsiz_1216, ah_none_mature, 1, 80, /* TCP_KEYLEN_MIN/MAX */
194 ah_none_init, ah_none_loop,
198 const struct ah_algorithm *
199 ah_algorithm_lookup(idx)
204 case SADB_AALG_MD5HMAC:
205 return &ah_algorithms[0];
206 case SADB_AALG_SHA1HMAC:
207 return &ah_algorithms[1];
208 case SADB_X_AALG_MD5:
209 return &ah_algorithms[2];
210 case SADB_X_AALG_SHA:
211 return &ah_algorithms[3];
212 case SADB_X_AALG_NULL:
213 return &ah_algorithms[4];
214 case SADB_X_AALG_SHA2_256:
215 return &ah_algorithms[5];
216 case SADB_X_AALG_SHA2_384:
217 return &ah_algorithms[6];
218 case SADB_X_AALG_SHA2_512:
219 return &ah_algorithms[7];
220 case SADB_X_AALG_RIPEMD160HMAC:
221 return &ah_algorithms[8];
222 case SADB_X_AALG_AES_XCBC_MAC:
223 return &ah_algorithms[9];
224 case SADB_X_AALG_TCP_MD5:
225 return &ah_algorithms[10];
234 struct secasvar *sav;
237 panic("ah_sumsiz_1216: null pointer is passed");
238 if (sav->flags & SADB_X_EXT_OLD)
246 struct secasvar *sav;
249 panic("ah_sumsiz_zero: null pointer is passed");
254 ah_common_mature(sav)
255 struct secasvar *sav;
257 const struct ah_algorithm *algo;
259 if (!sav->key_auth) {
260 ipseclog((LOG_ERR, "ah_common_mature: no key is given.\n"));
264 algo = ah_algorithm_lookup(sav->alg_auth);
266 ipseclog((LOG_ERR, "ah_common_mature: unsupported algorithm.\n"));
270 if (sav->key_auth->sadb_key_bits < algo->keymin ||
271 algo->keymax < sav->key_auth->sadb_key_bits) {
273 "ah_common_mature: invalid key length %d for %s.\n",
274 sav->key_auth->sadb_key_bits, algo->name));
283 struct secasvar *sav;
285 if (sav->sah->saidx.proto == IPPROTO_AH) {
287 "ah_none_mature: protocol and algorithm mismatch.\n"));
294 ah_none_init(state, sav)
295 struct ah_algorithm_state *state;
296 struct secasvar *sav;
303 ah_none_loop(state, addr, len)
304 struct ah_algorithm_state *state;
311 ah_none_result(state, addr, l)
312 struct ah_algorithm_state *state;
319 ah_keyed_md5_mature(sav)
320 struct secasvar *sav;
322 /* anything is okay */
327 ah_keyed_md5_init(state, sav)
328 struct ah_algorithm_state *state;
329 struct secasvar *sav;
336 panic("ah_keyed_md5_init: what?");
339 state->foo = (void *)malloc(sizeof(MD5_CTX), M_TEMP, M_NOWAIT);
340 if (state->foo == NULL)
343 MD5Init((MD5_CTX *)state->foo);
345 MD5Update((MD5_CTX *)state->foo,
346 (u_int8_t *)_KEYBUF(state->sav->key_auth),
347 (u_int)_KEYLEN(state->sav->key_auth));
351 * We cannot simply use md5_pad() since the function
352 * won't update the total length.
354 if (_KEYLEN(state->sav->key_auth) < 56)
355 padlen = 64 - 8 - _KEYLEN(state->sav->key_auth);
357 padlen = 64 + 64 - 8 - _KEYLEN(state->sav->key_auth);
358 keybitlen = _KEYLEN(state->sav->key_auth);
362 MD5Update((MD5_CTX *)state->foo, &buf[0], 1);
365 bzero(buf, sizeof(buf));
366 while (sizeof(buf) < padlen) {
367 MD5Update((MD5_CTX *)state->foo, &buf[0], sizeof(buf));
368 padlen -= sizeof(buf);
371 MD5Update((MD5_CTX *)state->foo, &buf[0], padlen);
374 buf[0] = (keybitlen >> 0) & 0xff;
375 buf[1] = (keybitlen >> 8) & 0xff;
376 buf[2] = (keybitlen >> 16) & 0xff;
377 buf[3] = (keybitlen >> 24) & 0xff;
378 MD5Update((MD5_CTX *)state->foo, buf, 8);
385 ah_keyed_md5_loop(state, addr, len)
386 struct ah_algorithm_state *state;
391 panic("ah_keyed_md5_loop: what?");
393 MD5Update((MD5_CTX *)state->foo, addr, len);
397 ah_keyed_md5_result(state, addr, l)
398 struct ah_algorithm_state *state;
402 u_char digest[MD5_RESULTLEN];
405 panic("ah_keyed_md5_result: what?");
408 MD5Update((MD5_CTX *)state->foo,
409 (u_int8_t *)_KEYBUF(state->sav->key_auth),
410 (u_int)_KEYLEN(state->sav->key_auth));
412 MD5Final(digest, (MD5_CTX *)state->foo);
413 free(state->foo, M_TEMP);
414 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
418 ah_keyed_sha1_init(state, sav)
419 struct ah_algorithm_state *state;
420 struct secasvar *sav;
428 panic("ah_keyed_sha1_init: what?");
431 state->foo = (void *)malloc(sizeof(SHA1_CTX), M_TEMP, M_NOWAIT);
435 ctxt = (SHA1_CTX *)state->foo;
439 SHA1Update(ctxt, (u_int8_t *)_KEYBUF(state->sav->key_auth),
440 (u_int)_KEYLEN(state->sav->key_auth));
445 if (_KEYLEN(state->sav->key_auth) < 56)
446 padlen = 64 - 8 - _KEYLEN(state->sav->key_auth);
448 padlen = 64 + 64 - 8 - _KEYLEN(state->sav->key_auth);
449 keybitlen = _KEYLEN(state->sav->key_auth);
453 SHA1Update(ctxt, &buf[0], 1);
456 bzero(buf, sizeof(buf));
457 while (sizeof(buf) < padlen) {
458 SHA1Update(ctxt, &buf[0], sizeof(buf));
459 padlen -= sizeof(buf);
462 SHA1Update(ctxt, &buf[0], padlen);
465 buf[0] = (keybitlen >> 0) & 0xff;
466 buf[1] = (keybitlen >> 8) & 0xff;
467 buf[2] = (keybitlen >> 16) & 0xff;
468 buf[3] = (keybitlen >> 24) & 0xff;
469 SHA1Update(ctxt, buf, 8);
476 ah_keyed_sha1_loop(state, addr, len)
477 struct ah_algorithm_state *state;
483 if (!state || !state->foo)
484 panic("ah_keyed_sha1_loop: what?");
485 ctxt = (SHA1_CTX *)state->foo;
487 SHA1Update(ctxt, (u_int8_t *)addr, (size_t)len);
491 ah_keyed_sha1_result(state, addr, l)
492 struct ah_algorithm_state *state;
496 u_char digest[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
499 if (!state || !state->foo)
500 panic("ah_keyed_sha1_result: what?");
501 ctxt = (SHA1_CTX *)state->foo;
504 SHA1Update(ctxt, (u_int8_t *)_KEYBUF(state->sav->key_auth),
505 (u_int)_KEYLEN(state->sav->key_auth));
507 SHA1Final((u_int8_t *)digest, ctxt);
508 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
510 free(state->foo, M_TEMP);
514 ah_hmac_md5_init(state, sav)
515 struct ah_algorithm_state *state;
516 struct secasvar *sav;
520 u_char tk[MD5_RESULTLEN];
527 panic("ah_hmac_md5_init: what?");
530 state->foo = (void *)malloc(64 + 64 + sizeof(MD5_CTX), M_TEMP, M_NOWAIT);
534 ipad = (u_char *)state->foo;
535 opad = (u_char *)(ipad + 64);
536 ctxt = (MD5_CTX *)(opad + 64);
538 /* compress the key if necessery */
539 if (64 < _KEYLEN(state->sav->key_auth)) {
541 MD5Update(ctxt, _KEYBUF(state->sav->key_auth),
542 _KEYLEN(state->sav->key_auth));
543 MD5Final(&tk[0], ctxt);
547 key = _KEYBUF(state->sav->key_auth);
548 keylen = _KEYLEN(state->sav->key_auth);
553 bcopy(key, ipad, keylen);
554 bcopy(key, opad, keylen);
555 for (i = 0; i < 64; i++) {
561 MD5Update(ctxt, ipad, 64);
567 ah_hmac_md5_loop(state, addr, len)
568 struct ah_algorithm_state *state;
574 if (!state || !state->foo)
575 panic("ah_hmac_md5_loop: what?");
576 ctxt = (MD5_CTX *)(((u_int8_t *)state->foo) + 128);
577 MD5Update(ctxt, addr, len);
581 ah_hmac_md5_result(state, addr, l)
582 struct ah_algorithm_state *state;
586 u_char digest[MD5_RESULTLEN];
591 if (!state || !state->foo)
592 panic("ah_hmac_md5_result: what?");
594 ipad = (u_char *)state->foo;
595 opad = (u_char *)(ipad + 64);
596 ctxt = (MD5_CTX *)(opad + 64);
598 MD5Final(digest, ctxt);
601 MD5Update(ctxt, opad, 64);
602 MD5Update(ctxt, digest, sizeof(digest));
603 MD5Final(digest, ctxt);
605 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
607 free(state->foo, M_TEMP);
611 ah_hmac_sha1_init(state, sav)
612 struct ah_algorithm_state *state;
613 struct secasvar *sav;
618 u_char tk[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
624 panic("ah_hmac_sha1_init: what?");
627 state->foo = (void *)malloc(64 + 64 + sizeof(SHA1_CTX),
632 ipad = (u_char *)state->foo;
633 opad = (u_char *)(ipad + 64);
634 ctxt = (SHA1_CTX *)(opad + 64);
636 /* compress the key if necessery */
637 if (64 < _KEYLEN(state->sav->key_auth)) {
639 SHA1Update(ctxt, _KEYBUF(state->sav->key_auth),
640 _KEYLEN(state->sav->key_auth));
641 SHA1Final(&tk[0], ctxt);
643 keylen = SHA1_RESULTLEN;
645 key = _KEYBUF(state->sav->key_auth);
646 keylen = _KEYLEN(state->sav->key_auth);
651 bcopy(key, ipad, keylen);
652 bcopy(key, opad, keylen);
653 for (i = 0; i < 64; i++) {
659 SHA1Update(ctxt, ipad, 64);
665 ah_hmac_sha1_loop(state, addr, len)
666 struct ah_algorithm_state *state;
672 if (!state || !state->foo)
673 panic("ah_hmac_sha1_loop: what?");
675 ctxt = (SHA1_CTX *)(((u_char *)state->foo) + 128);
676 SHA1Update(ctxt, (u_int8_t *)addr, (size_t)len);
680 ah_hmac_sha1_result(state, addr, l)
681 struct ah_algorithm_state *state;
685 u_char digest[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
690 if (!state || !state->foo)
691 panic("ah_hmac_sha1_result: what?");
693 ipad = (u_char *)state->foo;
694 opad = (u_char *)(ipad + 64);
695 ctxt = (SHA1_CTX *)(opad + 64);
697 SHA1Final((u_int8_t *)digest, ctxt);
700 SHA1Update(ctxt, opad, 64);
701 SHA1Update(ctxt, (u_int8_t *)digest, sizeof(digest));
702 SHA1Final((u_int8_t *)digest, ctxt);
704 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
706 free(state->foo, M_TEMP);
710 ah_hmac_sha2_256_init(state, sav)
711 struct ah_algorithm_state *state;
712 struct secasvar *sav;
717 u_char tk[SHA256_DIGEST_LENGTH];
723 panic("ah_hmac_sha2_256_init: what?");
726 state->foo = (void *)malloc(64 + 64 + sizeof(SHA256_CTX),
731 ipad = (u_char *)state->foo;
732 opad = (u_char *)(ipad + 64);
733 ctxt = (SHA256_CTX *)(opad + 64);
735 /* compress the key if necessery */
736 if (64 < _KEYLEN(state->sav->key_auth)) {
737 bzero(tk, sizeof(tk));
738 bzero(ctxt, sizeof(*ctxt));
740 SHA256_Update(ctxt, _KEYBUF(state->sav->key_auth),
741 _KEYLEN(state->sav->key_auth));
742 SHA256_Final(&tk[0], ctxt);
744 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
746 key = _KEYBUF(state->sav->key_auth);
747 keylen = _KEYLEN(state->sav->key_auth);
752 bcopy(key, ipad, keylen);
753 bcopy(key, opad, keylen);
754 for (i = 0; i < 64; i++) {
759 bzero(ctxt, sizeof(*ctxt));
761 SHA256_Update(ctxt, ipad, 64);
767 ah_hmac_sha2_256_loop(state, addr, len)
768 struct ah_algorithm_state *state;
774 if (!state || !state->foo)
775 panic("ah_hmac_sha2_256_loop: what?");
777 ctxt = (SHA256_CTX *)(((u_char *)state->foo) + 128);
778 SHA256_Update(ctxt, (caddr_t)addr, (size_t)len);
782 ah_hmac_sha2_256_result(state, addr, l)
783 struct ah_algorithm_state *state;
787 u_char digest[SHA256_DIGEST_LENGTH];
792 if (!state || !state->foo)
793 panic("ah_hmac_sha2_256_result: what?");
795 ipad = (u_char *)state->foo;
796 opad = (u_char *)(ipad + 64);
797 ctxt = (SHA256_CTX *)(opad + 64);
799 SHA256_Final((caddr_t)digest, ctxt);
801 bzero(ctxt, sizeof(*ctxt));
803 SHA256_Update(ctxt, opad, 64);
804 SHA256_Update(ctxt, (caddr_t)digest, sizeof(digest));
805 SHA256_Final((caddr_t)digest, ctxt);
807 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
809 free(state->foo, M_TEMP);
813 ah_hmac_sha2_384_init(state, sav)
814 struct ah_algorithm_state *state;
815 struct secasvar *sav;
820 u_char tk[SHA384_DIGEST_LENGTH];
826 panic("ah_hmac_sha2_384_init: what?");
829 state->foo = (void *)malloc(64 + 64 + sizeof(SHA384_CTX),
833 bzero(state->foo, 64 + 64 + sizeof(SHA384_CTX));
835 ipad = (u_char *)state->foo;
836 opad = (u_char *)(ipad + 64);
837 ctxt = (SHA384_CTX *)(opad + 64);
839 /* compress the key if necessery */
840 if (64 < _KEYLEN(state->sav->key_auth)) {
841 bzero(tk, sizeof(tk));
842 bzero(ctxt, sizeof(*ctxt));
844 SHA384_Update(ctxt, _KEYBUF(state->sav->key_auth),
845 _KEYLEN(state->sav->key_auth));
846 SHA384_Final(&tk[0], ctxt);
848 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
850 key = _KEYBUF(state->sav->key_auth);
851 keylen = _KEYLEN(state->sav->key_auth);
856 bcopy(key, ipad, keylen);
857 bcopy(key, opad, keylen);
858 for (i = 0; i < 64; i++) {
863 bzero(ctxt, sizeof(*ctxt));
865 SHA384_Update(ctxt, ipad, 64);
871 ah_hmac_sha2_384_loop(state, addr, len)
872 struct ah_algorithm_state *state;
878 if (!state || !state->foo)
879 panic("ah_hmac_sha2_384_loop: what?");
881 ctxt = (SHA384_CTX *)(((u_char *)state->foo) + 128);
882 SHA384_Update(ctxt, (caddr_t)addr, (size_t)len);
886 ah_hmac_sha2_384_result(state, addr, l)
887 struct ah_algorithm_state *state;
891 u_char digest[SHA384_DIGEST_LENGTH];
896 if (!state || !state->foo)
897 panic("ah_hmac_sha2_384_result: what?");
899 ipad = (u_char *)state->foo;
900 opad = (u_char *)(ipad + 64);
901 ctxt = (SHA384_CTX *)(opad + 64);
903 SHA384_Final((caddr_t)digest, ctxt);
905 bzero(ctxt, sizeof(*ctxt));
907 SHA384_Update(ctxt, opad, 64);
908 SHA384_Update(ctxt, (caddr_t)digest, sizeof(digest));
909 SHA384_Final((caddr_t)digest, ctxt);
911 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
913 free(state->foo, M_TEMP);
917 ah_hmac_sha2_512_init(state, sav)
918 struct ah_algorithm_state *state;
919 struct secasvar *sav;
924 u_char tk[SHA512_DIGEST_LENGTH];
930 panic("ah_hmac_sha2_512_init: what?");
933 state->foo = (void *)malloc(64 + 64 + sizeof(SHA512_CTX),
937 bzero(state->foo, 64 + 64 + sizeof(SHA512_CTX));
939 ipad = (u_char *)state->foo;
940 opad = (u_char *)(ipad + 64);
941 ctxt = (SHA512_CTX *)(opad + 64);
943 /* compress the key if necessery */
944 if (64 < _KEYLEN(state->sav->key_auth)) {
945 bzero(tk, sizeof(tk));
946 bzero(ctxt, sizeof(*ctxt));
948 SHA512_Update(ctxt, _KEYBUF(state->sav->key_auth),
949 _KEYLEN(state->sav->key_auth));
950 SHA512_Final(&tk[0], ctxt);
952 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
954 key = _KEYBUF(state->sav->key_auth);
955 keylen = _KEYLEN(state->sav->key_auth);
960 bcopy(key, ipad, keylen);
961 bcopy(key, opad, keylen);
962 for (i = 0; i < 64; i++) {
967 bzero(ctxt, sizeof(*ctxt));
969 SHA512_Update(ctxt, ipad, 64);
975 ah_hmac_sha2_512_loop(state, addr, len)
976 struct ah_algorithm_state *state;
982 if (!state || !state->foo)
983 panic("ah_hmac_sha2_512_loop: what?");
985 ctxt = (SHA512_CTX *)(((u_char *)state->foo) + 128);
986 SHA512_Update(ctxt, (caddr_t)addr, (size_t)len);
990 ah_hmac_sha2_512_result(state, addr, l)
991 struct ah_algorithm_state *state;
995 u_char digest[SHA512_DIGEST_LENGTH];
1000 if (!state || !state->foo)
1001 panic("ah_hmac_sha2_512_result: what?");
1003 ipad = (u_char *)state->foo;
1004 opad = (u_char *)(ipad + 64);
1005 ctxt = (SHA512_CTX *)(opad + 64);
1007 SHA512_Final((caddr_t)digest, ctxt);
1009 bzero(ctxt, sizeof(*ctxt));
1011 SHA512_Update(ctxt, opad, 64);
1012 SHA512_Update(ctxt, (caddr_t)digest, sizeof(digest));
1013 SHA512_Final((caddr_t)digest, ctxt);
1015 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
1017 free(state->foo, M_TEMP);
1021 ah_hmac_ripemd160_init(state, sav)
1022 struct ah_algorithm_state *state;
1023 struct secasvar *sav;
1028 u_char tk[RIPEMD160_RESULTLEN];
1034 panic("ah_hmac_ripemd160_init: what?");
1037 state->foo = (void *)malloc(64 + 64 + sizeof(RMD160_CTX),
1041 bzero(state->foo, 64 + 64 + sizeof(RMD160_CTX));
1043 ipad = (u_char *)state->foo;
1044 opad = (u_char *)(ipad + 64);
1045 ctxt = (RMD160_CTX *)(opad + 64);
1047 /* compress the key if necessery */
1048 if (64 < _KEYLEN(state->sav->key_auth)) {
1049 bzero(tk, sizeof(tk));
1050 bzero(ctxt, sizeof(*ctxt));
1052 RMD160Update(ctxt, _KEYBUF(state->sav->key_auth),
1053 _KEYLEN(state->sav->key_auth));
1054 RMD160Final(&tk[0], ctxt);
1056 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
1058 key = _KEYBUF(state->sav->key_auth);
1059 keylen = _KEYLEN(state->sav->key_auth);
1064 bcopy(key, ipad, keylen);
1065 bcopy(key, opad, keylen);
1066 for (i = 0; i < 64; i++) {
1071 bzero(ctxt, sizeof(*ctxt));
1073 RMD160Update(ctxt, ipad, 64);
1079 ah_hmac_ripemd160_loop(state, addr, len)
1080 struct ah_algorithm_state *state;
1086 if (!state || !state->foo)
1087 panic("ah_hmac_ripemd160_loop: what?");
1089 ctxt = (RMD160_CTX *)(((u_char *)state->foo) + 128);
1090 RMD160Update(ctxt, (caddr_t)addr, (size_t)len);
1094 ah_hmac_ripemd160_result(state, addr, l)
1095 struct ah_algorithm_state *state;
1099 u_char digest[RIPEMD160_RESULTLEN];
1104 if (!state || !state->foo)
1105 panic("ah_hmac_ripemd160_result: what?");
1107 ipad = (u_char *)state->foo;
1108 opad = (u_char *)(ipad + 64);
1109 ctxt = (RMD160_CTX *)(opad + 64);
1111 RMD160Final((caddr_t)digest, ctxt);
1113 bzero(ctxt, sizeof(*ctxt));
1115 RMD160Update(ctxt, opad, 64);
1116 RMD160Update(ctxt, (caddr_t)digest, sizeof(digest));
1117 RMD160Final((caddr_t)digest, ctxt);
1119 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
1121 free(state->foo, M_TEMP);
1124 /*------------------------------------------------------------*/
1127 * go generate the checksum.
1130 ah_update_mbuf(m, off, len, algo, algos)
1134 const struct ah_algorithm *algo;
1135 struct ah_algorithm_state *algos;
1140 /* easy case first */
1141 if (off + len <= m->m_len) {
1142 (algo->update)(algos, mtod(m, u_int8_t *) + off, len);
1146 for (n = m; n; n = n->m_next) {
1154 panic("ah_update_mbuf: wrong offset specified");
1156 for (/* nothing */; n && len > 0; n = n->m_next) {
1159 if (n->m_len - off < len)
1160 tlen = n->m_len - off;
1164 (algo->update)(algos, mtod(n, u_int8_t *) + off, tlen);
1173 * Go generate the checksum. This function won't modify the mbuf chain
1176 * NOTE: the function does not free mbuf on failure.
1177 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1180 ah4_calccksum(m, ahdat, len, algo, sav)
1184 const struct ah_algorithm *algo;
1185 struct secasvar *sav;
1189 size_t advancewidth;
1190 struct ah_algorithm_state algos;
1191 u_char sumbuf[AH_MAXSUMSIZE];
1194 struct mbuf *n = NULL;
1196 if ((m->m_flags & M_PKTHDR) == 0)
1200 hdrtype = -1; /* dummy, it is called IPPROTO_IP */
1204 error = (algo->init)(&algos, sav);
1208 advancewidth = 0; /* safety */
1213 case -1: /* first one only */
1216 * copy ip hdr, modify to fit the AH checksum rule,
1217 * then take a checksum.
1222 m_copydata(m, off, sizeof(iphdr), (caddr_t)&iphdr);
1224 hlen = IP_VHL_HL(iphdr.ip_vhl) << 2;
1226 hlen = iphdr.ip_hl << 2;
1229 iphdr.ip_sum = htons(0);
1230 if (ip4_ah_cleartos)
1232 iphdr.ip_off = htons(ntohs(iphdr.ip_off) & ip4_ah_offsetmask);
1233 (algo->update)(&algos, (u_int8_t *)&iphdr, sizeof(struct ip));
1235 if (hlen != sizeof(struct ip)) {
1239 if (hlen > MCLBYTES) {
1243 MGET(n, M_DONTWAIT, MT_DATA);
1244 if (n && hlen > MLEN) {
1245 MCLGET(n, M_DONTWAIT);
1246 if ((n->m_flags & M_EXT) == 0) {
1255 m_copydata(m, off, hlen, mtod(n, caddr_t));
1258 * IP options processing.
1259 * See RFC2402 appendix A.
1261 p = mtod(n, u_char *);
1262 i = sizeof(struct ip);
1264 if (i + IPOPT_OPTVAL >= hlen) {
1265 ipseclog((LOG_ERR, "ah4_calccksum: "
1266 "invalid IP option\n"));
1270 if (p[i + IPOPT_OPTVAL] == IPOPT_EOL ||
1271 p[i + IPOPT_OPTVAL] == IPOPT_NOP ||
1272 i + IPOPT_OLEN < hlen)
1276 "ah4_calccksum: invalid IP option "
1278 p[i + IPOPT_OPTVAL]));
1284 switch (p[i + IPOPT_OPTVAL]) {
1290 case IPOPT_SECURITY: /* 0x82 */
1291 case 0x85: /* Extended security */
1292 case 0x86: /* Commercial security */
1293 case 0x94: /* Router alert */
1294 case 0x95: /* RFC1770 */
1295 l = p[i + IPOPT_OLEN];
1301 l = p[i + IPOPT_OLEN];
1307 if (l < 1 || hlen - i < l) {
1310 "ah4_calccksum: invalid IP option "
1311 "(type=%02x len=%02x)\n",
1312 p[i + IPOPT_OPTVAL],
1313 p[i + IPOPT_OLEN]));
1319 if (p[i + IPOPT_OPTVAL] == IPOPT_EOL)
1323 p = mtod(n, u_char *) + sizeof(struct ip);
1324 (algo->update)(&algos, p, hlen - sizeof(struct ip));
1330 hdrtype = (iphdr.ip_p) & 0xff;
1331 advancewidth = hlen;
1342 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1343 hdrsiz = (sav->flags & SADB_X_EXT_OLD)
1345 : sizeof(struct newah);
1346 siz = (*algo->sumsiz)(sav);
1347 totlen = (ah.ah_len + 2) << 2;
1350 * special treatment is necessary for the first one, not others
1353 if (totlen > m->m_pkthdr.len - off ||
1354 totlen > MCLBYTES) {
1358 MGET(n, M_DONTWAIT, MT_DATA);
1359 if (n && totlen > MLEN) {
1360 MCLGET(n, M_DONTWAIT);
1361 if ((n->m_flags & M_EXT) == 0) {
1370 m_copydata(m, off, totlen, mtod(n, caddr_t));
1372 bzero(mtod(n, u_int8_t *) + hdrsiz, siz);
1373 (algo->update)(&algos, mtod(n, u_int8_t *), n->m_len);
1377 ah_update_mbuf(m, off, totlen, algo, &algos);
1380 hdrtype = ah.ah_nxt;
1381 advancewidth = totlen;
1386 ah_update_mbuf(m, off, m->m_pkthdr.len - off, algo, &algos);
1387 advancewidth = m->m_pkthdr.len - off;
1391 off += advancewidth;
1392 if (off < m->m_pkthdr.len)
1395 if (len < (*algo->sumsiz)(sav)) {
1400 (algo->result)(&algos, sumbuf, sizeof(sumbuf));
1401 bcopy(&sumbuf[0], ahdat, (*algo->sumsiz)(sav));
1416 * Go generate the checksum. This function won't modify the mbuf chain
1419 * NOTE: the function does not free mbuf on failure.
1420 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1423 ah6_calccksum(m, ahdat, len, algo, sav)
1427 const struct ah_algorithm *algo;
1428 struct secasvar *sav;
1432 struct mbuf *n = NULL;
1435 struct ah_algorithm_state algos;
1436 u_char sumbuf[AH_MAXSUMSIZE];
1438 if ((m->m_flags & M_PKTHDR) == 0)
1441 error = (algo->init)(&algos, sav);
1446 proto = IPPROTO_IPV6;
1451 newoff = ip6_nexthdr(m, off, proto, &nxt);
1453 newoff = m->m_pkthdr.len;
1454 else if (newoff <= off) {
1462 * special treatment is necessary for the first one, not others
1465 struct ip6_hdr ip6copy;
1467 if (newoff - off != sizeof(struct ip6_hdr)) {
1472 m_copydata(m, off, newoff - off, (caddr_t)&ip6copy);
1474 ip6copy.ip6_flow = 0;
1475 ip6copy.ip6_vfc &= ~IPV6_VERSION_MASK;
1476 ip6copy.ip6_vfc |= IPV6_VERSION;
1477 ip6copy.ip6_hlim = 0;
1478 in6_clearscope(&ip6copy.ip6_src); /* XXX */
1479 in6_clearscope(&ip6copy.ip6_dst); /* XXX */
1480 (algo->update)(&algos, (u_int8_t *)&ip6copy,
1481 sizeof(struct ip6_hdr));
1483 newoff = m->m_pkthdr.len;
1484 ah_update_mbuf(m, off, m->m_pkthdr.len - off, algo,
1494 hdrsiz = (sav->flags & SADB_X_EXT_OLD)
1496 : sizeof(struct newah);
1497 siz = (*algo->sumsiz)(sav);
1500 * special treatment is necessary for the first one, not others
1503 if (newoff - off > MCLBYTES) {
1507 MGET(n, M_DONTWAIT, MT_DATA);
1508 if (n && newoff - off > MLEN) {
1509 MCLGET(n, M_DONTWAIT);
1510 if ((n->m_flags & M_EXT) == 0) {
1519 m_copydata(m, off, newoff - off, mtod(n, caddr_t));
1520 n->m_len = newoff - off;
1521 bzero(mtod(n, u_int8_t *) + hdrsiz, siz);
1522 (algo->update)(&algos, mtod(n, u_int8_t *), n->m_len);
1526 ah_update_mbuf(m, off, newoff - off, algo, &algos);
1531 case IPPROTO_HOPOPTS:
1532 case IPPROTO_DSTOPTS:
1534 struct ip6_ext *ip6e;
1536 u_int8_t *p, *optend, *optp;
1538 if (newoff - off > MCLBYTES) {
1542 MGET(n, M_DONTWAIT, MT_DATA);
1543 if (n && newoff - off > MLEN) {
1544 MCLGET(n, M_DONTWAIT);
1545 if ((n->m_flags & M_EXT) == 0) {
1554 m_copydata(m, off, newoff - off, mtod(n, caddr_t));
1555 n->m_len = newoff - off;
1557 ip6e = mtod(n, struct ip6_ext *);
1558 hdrlen = (ip6e->ip6e_len + 1) << 3;
1559 if (newoff - off < hdrlen) {
1565 p = mtod(n, u_int8_t *);
1566 optend = p + hdrlen;
1569 * ICV calculation for the options header including all
1570 * options. This part is a little tricky since there are
1571 * two type of options; mutable and immutable. We try to
1572 * null-out mutable ones here.
1575 while (optp < optend) {
1576 if (optp[0] == IP6OPT_PAD1)
1579 if (optp + 2 > optend) {
1585 optlen = optp[1] + 2;
1588 if (optp + optlen > optend) {
1595 if (optp[0] & IP6OPT_MUTABLE)
1596 bzero(optp + 2, optlen - 2);
1601 (algo->update)(&algos, mtod(n, u_int8_t *), n->m_len);
1607 case IPPROTO_ROUTING:
1609 * For an input packet, we can just calculate `as is'.
1610 * For an output packet, we assume ip6_output have already
1611 * made packet how it will be received at the final
1617 ah_update_mbuf(m, off, newoff - off, algo, &algos);
1621 if (newoff < m->m_pkthdr.len) {
1627 if (len < (*algo->sumsiz)(sav)) {
1632 (algo->result)(&algos, sumbuf, sizeof(sumbuf));
1633 bcopy(&sumbuf[0], ahdat, (*algo->sumsiz)(sav));