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 <netinet6/scope6_var.h>
67 #include <netinet/icmp6.h>
70 #include <netinet6/ipsec.h>
72 #include <netinet6/ipsec6.h>
74 #include <netinet6/ah.h>
76 #include <netinet6/ah6.h>
78 #include <netinet6/ah_aesxcbcmac.h>
80 #include <netinet6/esp.h>
82 #include <netinet6/esp6.h>
85 #include <net/pfkeyv2.h>
86 #include <netkey/keydb.h>
88 #define MD5_RESULTLEN 16
89 #include <crypto/sha1.h>
90 #include <crypto/sha2/sha2.h>
91 #include <opencrypto/rmd160.h>
92 #define RIPEMD160_RESULTLEN 20
94 #include <net/net_osdep.h>
96 static int ah_sumsiz_1216 __P((struct secasvar *));
97 static int ah_sumsiz_zero __P((struct secasvar *));
98 static int ah_common_mature __P((struct secasvar *));
99 static int ah_none_mature __P((struct secasvar *));
100 static int ah_none_init __P((struct ah_algorithm_state *, struct secasvar *));
101 static void ah_none_loop __P((struct ah_algorithm_state *, u_int8_t *, size_t));
102 static void ah_none_result __P((struct ah_algorithm_state *,
103 u_int8_t *, size_t));
104 static int ah_keyed_md5_mature __P((struct secasvar *));
105 static int ah_keyed_md5_init __P((struct ah_algorithm_state *,
107 static void ah_keyed_md5_loop __P((struct ah_algorithm_state *, u_int8_t *,
109 static void ah_keyed_md5_result __P((struct ah_algorithm_state *,
110 u_int8_t *, size_t));
111 static int ah_keyed_sha1_init __P((struct ah_algorithm_state *,
113 static void ah_keyed_sha1_loop __P((struct ah_algorithm_state *, u_int8_t *,
115 static void ah_keyed_sha1_result __P((struct ah_algorithm_state *, u_int8_t *,
117 static int ah_hmac_md5_init __P((struct ah_algorithm_state *,
119 static void ah_hmac_md5_loop __P((struct ah_algorithm_state *, u_int8_t *,
121 static void ah_hmac_md5_result __P((struct ah_algorithm_state *,
122 u_int8_t *, size_t));
123 static int ah_hmac_sha1_init __P((struct ah_algorithm_state *,
125 static void ah_hmac_sha1_loop __P((struct ah_algorithm_state *, u_int8_t *,
127 static void ah_hmac_sha1_result __P((struct ah_algorithm_state *,
128 u_int8_t *, size_t));
129 static int ah_hmac_sha2_256_init __P((struct ah_algorithm_state *,
131 static void ah_hmac_sha2_256_loop __P((struct ah_algorithm_state *, u_int8_t *,
133 static void ah_hmac_sha2_256_result __P((struct ah_algorithm_state *,
134 u_int8_t *, size_t));
135 static int ah_hmac_sha2_384_init __P((struct ah_algorithm_state *,
137 static void ah_hmac_sha2_384_loop __P((struct ah_algorithm_state *, u_int8_t *,
139 static void ah_hmac_sha2_384_result __P((struct ah_algorithm_state *,
140 u_int8_t *, size_t));
141 static int ah_hmac_sha2_512_init __P((struct ah_algorithm_state *,
143 static void ah_hmac_sha2_512_loop __P((struct ah_algorithm_state *, u_int8_t *,
145 static void ah_hmac_sha2_512_result __P((struct ah_algorithm_state *,
146 u_int8_t *, size_t));
147 static int ah_hmac_ripemd160_init __P((struct ah_algorithm_state *,
149 static void ah_hmac_ripemd160_loop __P((struct ah_algorithm_state *, u_int8_t *,
151 static void ah_hmac_ripemd160_result __P((struct ah_algorithm_state *,
152 u_int8_t *, size_t));
154 static void ah_update_mbuf __P((struct mbuf *, int, int,
155 const struct ah_algorithm *, struct ah_algorithm_state *));
157 /* checksum algorithms */
158 static const struct ah_algorithm ah_algorithms[] = {
159 { ah_sumsiz_1216, ah_common_mature, 128, 128, "hmac-md5",
160 ah_hmac_md5_init, ah_hmac_md5_loop,
161 ah_hmac_md5_result, },
162 { ah_sumsiz_1216, ah_common_mature, 160, 160, "hmac-sha1",
163 ah_hmac_sha1_init, ah_hmac_sha1_loop,
164 ah_hmac_sha1_result, },
165 { ah_sumsiz_1216, ah_keyed_md5_mature, 128, 128, "keyed-md5",
166 ah_keyed_md5_init, ah_keyed_md5_loop,
167 ah_keyed_md5_result, },
168 { ah_sumsiz_1216, ah_common_mature, 160, 160, "keyed-sha1",
169 ah_keyed_sha1_init, ah_keyed_sha1_loop,
170 ah_keyed_sha1_result, },
171 { ah_sumsiz_zero, ah_none_mature, 0, 2048, "none",
172 ah_none_init, ah_none_loop, ah_none_result, },
173 { ah_sumsiz_1216, ah_common_mature, 256, 256,
175 ah_hmac_sha2_256_init, ah_hmac_sha2_256_loop,
176 ah_hmac_sha2_256_result, },
177 { ah_sumsiz_1216, ah_common_mature, 384, 384,
179 ah_hmac_sha2_384_init, ah_hmac_sha2_384_loop,
180 ah_hmac_sha2_384_result, },
181 { ah_sumsiz_1216, ah_common_mature, 512, 512,
183 ah_hmac_sha2_512_init, ah_hmac_sha2_512_loop,
184 ah_hmac_sha2_512_result, },
185 { ah_sumsiz_1216, ah_common_mature, 160, 160,
187 ah_hmac_ripemd160_init, ah_hmac_ripemd160_loop,
188 ah_hmac_ripemd160_result, },
189 { ah_sumsiz_1216, ah_common_mature, 128, 128,
191 ah_aes_xcbc_mac_init, ah_aes_xcbc_mac_loop,
192 ah_aes_xcbc_mac_result, },
193 { ah_sumsiz_1216, ah_none_mature, 1, 80, /* TCP_KEYLEN_MIN/MAX */
195 ah_none_init, ah_none_loop,
199 const struct ah_algorithm *
200 ah_algorithm_lookup(idx)
205 case SADB_AALG_MD5HMAC:
206 return &ah_algorithms[0];
207 case SADB_AALG_SHA1HMAC:
208 return &ah_algorithms[1];
209 case SADB_X_AALG_MD5:
210 return &ah_algorithms[2];
211 case SADB_X_AALG_SHA:
212 return &ah_algorithms[3];
213 case SADB_X_AALG_NULL:
214 return &ah_algorithms[4];
215 case SADB_X_AALG_SHA2_256:
216 return &ah_algorithms[5];
217 case SADB_X_AALG_SHA2_384:
218 return &ah_algorithms[6];
219 case SADB_X_AALG_SHA2_512:
220 return &ah_algorithms[7];
221 case SADB_X_AALG_RIPEMD160HMAC:
222 return &ah_algorithms[8];
223 case SADB_X_AALG_AES_XCBC_MAC:
224 return &ah_algorithms[9];
225 case SADB_X_AALG_TCP_MD5:
226 return &ah_algorithms[10];
235 struct secasvar *sav;
238 panic("ah_sumsiz_1216: null pointer is passed");
239 if (sav->flags & SADB_X_EXT_OLD)
247 struct secasvar *sav;
250 panic("ah_sumsiz_zero: null pointer is passed");
255 ah_common_mature(sav)
256 struct secasvar *sav;
258 const struct ah_algorithm *algo;
260 if (!sav->key_auth) {
261 ipseclog((LOG_ERR, "ah_common_mature: no key is given.\n"));
265 algo = ah_algorithm_lookup(sav->alg_auth);
267 ipseclog((LOG_ERR, "ah_common_mature: unsupported algorithm.\n"));
271 if (sav->key_auth->sadb_key_bits < algo->keymin ||
272 algo->keymax < sav->key_auth->sadb_key_bits) {
274 "ah_common_mature: invalid key length %d for %s.\n",
275 sav->key_auth->sadb_key_bits, algo->name));
284 struct secasvar *sav;
286 if (sav->sah->saidx.proto == IPPROTO_AH) {
288 "ah_none_mature: protocol and algorithm mismatch.\n"));
295 ah_none_init(state, sav)
296 struct ah_algorithm_state *state;
297 struct secasvar *sav;
304 ah_none_loop(state, addr, len)
305 struct ah_algorithm_state *state;
312 ah_none_result(state, addr, l)
313 struct ah_algorithm_state *state;
320 ah_keyed_md5_mature(sav)
321 struct secasvar *sav;
323 /* anything is okay */
328 ah_keyed_md5_init(state, sav)
329 struct ah_algorithm_state *state;
330 struct secasvar *sav;
337 panic("ah_keyed_md5_init: what?");
340 state->foo = (void *)malloc(sizeof(MD5_CTX), M_TEMP, M_NOWAIT);
341 if (state->foo == NULL)
344 MD5Init((MD5_CTX *)state->foo);
346 MD5Update((MD5_CTX *)state->foo,
347 (u_int8_t *)_KEYBUF(state->sav->key_auth),
348 (u_int)_KEYLEN(state->sav->key_auth));
352 * We cannot simply use md5_pad() since the function
353 * won't update the total length.
355 if (_KEYLEN(state->sav->key_auth) < 56)
356 padlen = 64 - 8 - _KEYLEN(state->sav->key_auth);
358 padlen = 64 + 64 - 8 - _KEYLEN(state->sav->key_auth);
359 keybitlen = _KEYLEN(state->sav->key_auth);
363 MD5Update((MD5_CTX *)state->foo, &buf[0], 1);
366 bzero(buf, sizeof(buf));
367 while (sizeof(buf) < padlen) {
368 MD5Update((MD5_CTX *)state->foo, &buf[0], sizeof(buf));
369 padlen -= sizeof(buf);
372 MD5Update((MD5_CTX *)state->foo, &buf[0], padlen);
375 buf[0] = (keybitlen >> 0) & 0xff;
376 buf[1] = (keybitlen >> 8) & 0xff;
377 buf[2] = (keybitlen >> 16) & 0xff;
378 buf[3] = (keybitlen >> 24) & 0xff;
379 MD5Update((MD5_CTX *)state->foo, buf, 8);
386 ah_keyed_md5_loop(state, addr, len)
387 struct ah_algorithm_state *state;
392 panic("ah_keyed_md5_loop: what?");
394 MD5Update((MD5_CTX *)state->foo, addr, len);
398 ah_keyed_md5_result(state, addr, l)
399 struct ah_algorithm_state *state;
403 u_char digest[MD5_RESULTLEN];
406 panic("ah_keyed_md5_result: what?");
409 MD5Update((MD5_CTX *)state->foo,
410 (u_int8_t *)_KEYBUF(state->sav->key_auth),
411 (u_int)_KEYLEN(state->sav->key_auth));
413 MD5Final(digest, (MD5_CTX *)state->foo);
414 free(state->foo, M_TEMP);
415 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
419 ah_keyed_sha1_init(state, sav)
420 struct ah_algorithm_state *state;
421 struct secasvar *sav;
429 panic("ah_keyed_sha1_init: what?");
432 state->foo = (void *)malloc(sizeof(SHA1_CTX), M_TEMP, M_NOWAIT);
436 ctxt = (SHA1_CTX *)state->foo;
440 SHA1Update(ctxt, (u_int8_t *)_KEYBUF(state->sav->key_auth),
441 (u_int)_KEYLEN(state->sav->key_auth));
446 if (_KEYLEN(state->sav->key_auth) < 56)
447 padlen = 64 - 8 - _KEYLEN(state->sav->key_auth);
449 padlen = 64 + 64 - 8 - _KEYLEN(state->sav->key_auth);
450 keybitlen = _KEYLEN(state->sav->key_auth);
454 SHA1Update(ctxt, &buf[0], 1);
457 bzero(buf, sizeof(buf));
458 while (sizeof(buf) < padlen) {
459 SHA1Update(ctxt, &buf[0], sizeof(buf));
460 padlen -= sizeof(buf);
463 SHA1Update(ctxt, &buf[0], padlen);
466 buf[0] = (keybitlen >> 0) & 0xff;
467 buf[1] = (keybitlen >> 8) & 0xff;
468 buf[2] = (keybitlen >> 16) & 0xff;
469 buf[3] = (keybitlen >> 24) & 0xff;
470 SHA1Update(ctxt, buf, 8);
477 ah_keyed_sha1_loop(state, addr, len)
478 struct ah_algorithm_state *state;
484 if (!state || !state->foo)
485 panic("ah_keyed_sha1_loop: what?");
486 ctxt = (SHA1_CTX *)state->foo;
488 SHA1Update(ctxt, (u_int8_t *)addr, (size_t)len);
492 ah_keyed_sha1_result(state, addr, l)
493 struct ah_algorithm_state *state;
497 u_char digest[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
500 if (!state || !state->foo)
501 panic("ah_keyed_sha1_result: what?");
502 ctxt = (SHA1_CTX *)state->foo;
505 SHA1Update(ctxt, (u_int8_t *)_KEYBUF(state->sav->key_auth),
506 (u_int)_KEYLEN(state->sav->key_auth));
508 SHA1Final((u_int8_t *)digest, ctxt);
509 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
511 free(state->foo, M_TEMP);
515 ah_hmac_md5_init(state, sav)
516 struct ah_algorithm_state *state;
517 struct secasvar *sav;
521 u_char tk[MD5_RESULTLEN];
528 panic("ah_hmac_md5_init: what?");
531 state->foo = (void *)malloc(64 + 64 + sizeof(MD5_CTX), M_TEMP, M_NOWAIT);
535 ipad = (u_char *)state->foo;
536 opad = (u_char *)(ipad + 64);
537 ctxt = (MD5_CTX *)(opad + 64);
539 /* compress the key if necessery */
540 if (64 < _KEYLEN(state->sav->key_auth)) {
542 MD5Update(ctxt, _KEYBUF(state->sav->key_auth),
543 _KEYLEN(state->sav->key_auth));
544 MD5Final(&tk[0], ctxt);
548 key = _KEYBUF(state->sav->key_auth);
549 keylen = _KEYLEN(state->sav->key_auth);
554 bcopy(key, ipad, keylen);
555 bcopy(key, opad, keylen);
556 for (i = 0; i < 64; i++) {
562 MD5Update(ctxt, ipad, 64);
568 ah_hmac_md5_loop(state, addr, len)
569 struct ah_algorithm_state *state;
575 if (!state || !state->foo)
576 panic("ah_hmac_md5_loop: what?");
577 ctxt = (MD5_CTX *)(((u_int8_t *)state->foo) + 128);
578 MD5Update(ctxt, addr, len);
582 ah_hmac_md5_result(state, addr, l)
583 struct ah_algorithm_state *state;
587 u_char digest[MD5_RESULTLEN];
592 if (!state || !state->foo)
593 panic("ah_hmac_md5_result: what?");
595 ipad = (u_char *)state->foo;
596 opad = (u_char *)(ipad + 64);
597 ctxt = (MD5_CTX *)(opad + 64);
599 MD5Final(digest, ctxt);
602 MD5Update(ctxt, opad, 64);
603 MD5Update(ctxt, digest, sizeof(digest));
604 MD5Final(digest, ctxt);
606 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
608 free(state->foo, M_TEMP);
612 ah_hmac_sha1_init(state, sav)
613 struct ah_algorithm_state *state;
614 struct secasvar *sav;
619 u_char tk[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
625 panic("ah_hmac_sha1_init: what?");
628 state->foo = (void *)malloc(64 + 64 + sizeof(SHA1_CTX),
633 ipad = (u_char *)state->foo;
634 opad = (u_char *)(ipad + 64);
635 ctxt = (SHA1_CTX *)(opad + 64);
637 /* compress the key if necessery */
638 if (64 < _KEYLEN(state->sav->key_auth)) {
640 SHA1Update(ctxt, _KEYBUF(state->sav->key_auth),
641 _KEYLEN(state->sav->key_auth));
642 SHA1Final(&tk[0], ctxt);
644 keylen = SHA1_RESULTLEN;
646 key = _KEYBUF(state->sav->key_auth);
647 keylen = _KEYLEN(state->sav->key_auth);
652 bcopy(key, ipad, keylen);
653 bcopy(key, opad, keylen);
654 for (i = 0; i < 64; i++) {
660 SHA1Update(ctxt, ipad, 64);
666 ah_hmac_sha1_loop(state, addr, len)
667 struct ah_algorithm_state *state;
673 if (!state || !state->foo)
674 panic("ah_hmac_sha1_loop: what?");
676 ctxt = (SHA1_CTX *)(((u_char *)state->foo) + 128);
677 SHA1Update(ctxt, (u_int8_t *)addr, (size_t)len);
681 ah_hmac_sha1_result(state, addr, l)
682 struct ah_algorithm_state *state;
686 u_char digest[SHA1_RESULTLEN]; /* SHA-1 generates 160 bits */
691 if (!state || !state->foo)
692 panic("ah_hmac_sha1_result: what?");
694 ipad = (u_char *)state->foo;
695 opad = (u_char *)(ipad + 64);
696 ctxt = (SHA1_CTX *)(opad + 64);
698 SHA1Final((u_int8_t *)digest, ctxt);
701 SHA1Update(ctxt, opad, 64);
702 SHA1Update(ctxt, (u_int8_t *)digest, sizeof(digest));
703 SHA1Final((u_int8_t *)digest, ctxt);
705 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
707 free(state->foo, M_TEMP);
711 ah_hmac_sha2_256_init(state, sav)
712 struct ah_algorithm_state *state;
713 struct secasvar *sav;
718 u_char tk[SHA256_DIGEST_LENGTH];
724 panic("ah_hmac_sha2_256_init: what?");
727 state->foo = (void *)malloc(64 + 64 + sizeof(SHA256_CTX),
732 ipad = (u_char *)state->foo;
733 opad = (u_char *)(ipad + 64);
734 ctxt = (SHA256_CTX *)(opad + 64);
736 /* compress the key if necessery */
737 if (64 < _KEYLEN(state->sav->key_auth)) {
738 bzero(tk, sizeof(tk));
739 bzero(ctxt, sizeof(*ctxt));
741 SHA256_Update(ctxt, _KEYBUF(state->sav->key_auth),
742 _KEYLEN(state->sav->key_auth));
743 SHA256_Final(&tk[0], ctxt);
745 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
747 key = _KEYBUF(state->sav->key_auth);
748 keylen = _KEYLEN(state->sav->key_auth);
753 bcopy(key, ipad, keylen);
754 bcopy(key, opad, keylen);
755 for (i = 0; i < 64; i++) {
760 bzero(ctxt, sizeof(*ctxt));
762 SHA256_Update(ctxt, ipad, 64);
768 ah_hmac_sha2_256_loop(state, addr, len)
769 struct ah_algorithm_state *state;
775 if (!state || !state->foo)
776 panic("ah_hmac_sha2_256_loop: what?");
778 ctxt = (SHA256_CTX *)(((u_char *)state->foo) + 128);
779 SHA256_Update(ctxt, (caddr_t)addr, (size_t)len);
783 ah_hmac_sha2_256_result(state, addr, l)
784 struct ah_algorithm_state *state;
788 u_char digest[SHA256_DIGEST_LENGTH];
793 if (!state || !state->foo)
794 panic("ah_hmac_sha2_256_result: what?");
796 ipad = (u_char *)state->foo;
797 opad = (u_char *)(ipad + 64);
798 ctxt = (SHA256_CTX *)(opad + 64);
800 SHA256_Final((caddr_t)digest, ctxt);
802 bzero(ctxt, sizeof(*ctxt));
804 SHA256_Update(ctxt, opad, 64);
805 SHA256_Update(ctxt, (caddr_t)digest, sizeof(digest));
806 SHA256_Final((caddr_t)digest, ctxt);
808 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
810 free(state->foo, M_TEMP);
814 ah_hmac_sha2_384_init(state, sav)
815 struct ah_algorithm_state *state;
816 struct secasvar *sav;
821 u_char tk[SHA384_DIGEST_LENGTH];
827 panic("ah_hmac_sha2_384_init: what?");
830 state->foo = (void *)malloc(64 + 64 + sizeof(SHA384_CTX),
834 bzero(state->foo, 64 + 64 + sizeof(SHA384_CTX));
836 ipad = (u_char *)state->foo;
837 opad = (u_char *)(ipad + 64);
838 ctxt = (SHA384_CTX *)(opad + 64);
840 /* compress the key if necessery */
841 if (64 < _KEYLEN(state->sav->key_auth)) {
842 bzero(tk, sizeof(tk));
843 bzero(ctxt, sizeof(*ctxt));
845 SHA384_Update(ctxt, _KEYBUF(state->sav->key_auth),
846 _KEYLEN(state->sav->key_auth));
847 SHA384_Final(&tk[0], ctxt);
849 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
851 key = _KEYBUF(state->sav->key_auth);
852 keylen = _KEYLEN(state->sav->key_auth);
857 bcopy(key, ipad, keylen);
858 bcopy(key, opad, keylen);
859 for (i = 0; i < 64; i++) {
864 bzero(ctxt, sizeof(*ctxt));
866 SHA384_Update(ctxt, ipad, 64);
872 ah_hmac_sha2_384_loop(state, addr, len)
873 struct ah_algorithm_state *state;
879 if (!state || !state->foo)
880 panic("ah_hmac_sha2_384_loop: what?");
882 ctxt = (SHA384_CTX *)(((u_char *)state->foo) + 128);
883 SHA384_Update(ctxt, (caddr_t)addr, (size_t)len);
887 ah_hmac_sha2_384_result(state, addr, l)
888 struct ah_algorithm_state *state;
892 u_char digest[SHA384_DIGEST_LENGTH];
897 if (!state || !state->foo)
898 panic("ah_hmac_sha2_384_result: what?");
900 ipad = (u_char *)state->foo;
901 opad = (u_char *)(ipad + 64);
902 ctxt = (SHA384_CTX *)(opad + 64);
904 SHA384_Final((caddr_t)digest, ctxt);
906 bzero(ctxt, sizeof(*ctxt));
908 SHA384_Update(ctxt, opad, 64);
909 SHA384_Update(ctxt, (caddr_t)digest, sizeof(digest));
910 SHA384_Final((caddr_t)digest, ctxt);
912 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
914 free(state->foo, M_TEMP);
918 ah_hmac_sha2_512_init(state, sav)
919 struct ah_algorithm_state *state;
920 struct secasvar *sav;
925 u_char tk[SHA512_DIGEST_LENGTH];
931 panic("ah_hmac_sha2_512_init: what?");
934 state->foo = (void *)malloc(64 + 64 + sizeof(SHA512_CTX),
938 bzero(state->foo, 64 + 64 + sizeof(SHA512_CTX));
940 ipad = (u_char *)state->foo;
941 opad = (u_char *)(ipad + 64);
942 ctxt = (SHA512_CTX *)(opad + 64);
944 /* compress the key if necessery */
945 if (64 < _KEYLEN(state->sav->key_auth)) {
946 bzero(tk, sizeof(tk));
947 bzero(ctxt, sizeof(*ctxt));
949 SHA512_Update(ctxt, _KEYBUF(state->sav->key_auth),
950 _KEYLEN(state->sav->key_auth));
951 SHA512_Final(&tk[0], ctxt);
953 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
955 key = _KEYBUF(state->sav->key_auth);
956 keylen = _KEYLEN(state->sav->key_auth);
961 bcopy(key, ipad, keylen);
962 bcopy(key, opad, keylen);
963 for (i = 0; i < 64; i++) {
968 bzero(ctxt, sizeof(*ctxt));
970 SHA512_Update(ctxt, ipad, 64);
976 ah_hmac_sha2_512_loop(state, addr, len)
977 struct ah_algorithm_state *state;
983 if (!state || !state->foo)
984 panic("ah_hmac_sha2_512_loop: what?");
986 ctxt = (SHA512_CTX *)(((u_char *)state->foo) + 128);
987 SHA512_Update(ctxt, (caddr_t)addr, (size_t)len);
991 ah_hmac_sha2_512_result(state, addr, l)
992 struct ah_algorithm_state *state;
996 u_char digest[SHA512_DIGEST_LENGTH];
1001 if (!state || !state->foo)
1002 panic("ah_hmac_sha2_512_result: what?");
1004 ipad = (u_char *)state->foo;
1005 opad = (u_char *)(ipad + 64);
1006 ctxt = (SHA512_CTX *)(opad + 64);
1008 SHA512_Final((caddr_t)digest, ctxt);
1010 bzero(ctxt, sizeof(*ctxt));
1012 SHA512_Update(ctxt, opad, 64);
1013 SHA512_Update(ctxt, (caddr_t)digest, sizeof(digest));
1014 SHA512_Final((caddr_t)digest, ctxt);
1016 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
1018 free(state->foo, M_TEMP);
1022 ah_hmac_ripemd160_init(state, sav)
1023 struct ah_algorithm_state *state;
1024 struct secasvar *sav;
1029 u_char tk[RIPEMD160_RESULTLEN];
1035 panic("ah_hmac_ripemd160_init: what?");
1038 state->foo = (void *)malloc(64 + 64 + sizeof(RMD160_CTX),
1042 bzero(state->foo, 64 + 64 + sizeof(RMD160_CTX));
1044 ipad = (u_char *)state->foo;
1045 opad = (u_char *)(ipad + 64);
1046 ctxt = (RMD160_CTX *)(opad + 64);
1048 /* compress the key if necessery */
1049 if (64 < _KEYLEN(state->sav->key_auth)) {
1050 bzero(tk, sizeof(tk));
1051 bzero(ctxt, sizeof(*ctxt));
1053 RMD160Update(ctxt, _KEYBUF(state->sav->key_auth),
1054 _KEYLEN(state->sav->key_auth));
1055 RMD160Final(&tk[0], ctxt);
1057 keylen = sizeof(tk) < 64 ? sizeof(tk) : 64;
1059 key = _KEYBUF(state->sav->key_auth);
1060 keylen = _KEYLEN(state->sav->key_auth);
1065 bcopy(key, ipad, keylen);
1066 bcopy(key, opad, keylen);
1067 for (i = 0; i < 64; i++) {
1072 bzero(ctxt, sizeof(*ctxt));
1074 RMD160Update(ctxt, ipad, 64);
1080 ah_hmac_ripemd160_loop(state, addr, len)
1081 struct ah_algorithm_state *state;
1087 if (!state || !state->foo)
1088 panic("ah_hmac_ripemd160_loop: what?");
1090 ctxt = (RMD160_CTX *)(((u_char *)state->foo) + 128);
1091 RMD160Update(ctxt, (caddr_t)addr, (size_t)len);
1095 ah_hmac_ripemd160_result(state, addr, l)
1096 struct ah_algorithm_state *state;
1100 u_char digest[RIPEMD160_RESULTLEN];
1105 if (!state || !state->foo)
1106 panic("ah_hmac_ripemd160_result: what?");
1108 ipad = (u_char *)state->foo;
1109 opad = (u_char *)(ipad + 64);
1110 ctxt = (RMD160_CTX *)(opad + 64);
1112 RMD160Final((caddr_t)digest, ctxt);
1114 bzero(ctxt, sizeof(*ctxt));
1116 RMD160Update(ctxt, opad, 64);
1117 RMD160Update(ctxt, (caddr_t)digest, sizeof(digest));
1118 RMD160Final((caddr_t)digest, ctxt);
1120 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
1122 free(state->foo, M_TEMP);
1125 /*------------------------------------------------------------*/
1128 * go generate the checksum.
1131 ah_update_mbuf(m, off, len, algo, algos)
1135 const struct ah_algorithm *algo;
1136 struct ah_algorithm_state *algos;
1141 /* easy case first */
1142 if (off + len <= m->m_len) {
1143 (algo->update)(algos, mtod(m, u_int8_t *) + off, len);
1147 for (n = m; n; n = n->m_next) {
1155 panic("ah_update_mbuf: wrong offset specified");
1157 for (/* nothing */; n && len > 0; n = n->m_next) {
1160 if (n->m_len - off < len)
1161 tlen = n->m_len - off;
1165 (algo->update)(algos, mtod(n, u_int8_t *) + off, tlen);
1174 * Go generate the checksum. This function won't modify the mbuf chain
1177 * NOTE: the function does not free mbuf on failure.
1178 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1181 ah4_calccksum(m, ahdat, len, algo, sav)
1185 const struct ah_algorithm *algo;
1186 struct secasvar *sav;
1190 size_t advancewidth;
1191 struct ah_algorithm_state algos;
1192 u_char sumbuf[AH_MAXSUMSIZE];
1195 struct mbuf *n = NULL;
1197 if ((m->m_flags & M_PKTHDR) == 0)
1201 hdrtype = -1; /* dummy, it is called IPPROTO_IP */
1205 error = (algo->init)(&algos, sav);
1209 advancewidth = 0; /* safety */
1214 case -1: /* first one only */
1217 * copy ip hdr, modify to fit the AH checksum rule,
1218 * then take a checksum.
1223 m_copydata(m, off, sizeof(iphdr), (caddr_t)&iphdr);
1225 hlen = IP_VHL_HL(iphdr.ip_vhl) << 2;
1227 hlen = iphdr.ip_hl << 2;
1230 iphdr.ip_sum = htons(0);
1231 if (ip4_ah_cleartos)
1233 iphdr.ip_off = htons(ntohs(iphdr.ip_off) & ip4_ah_offsetmask);
1234 (algo->update)(&algos, (u_int8_t *)&iphdr, sizeof(struct ip));
1236 if (hlen != sizeof(struct ip)) {
1240 if (hlen > MCLBYTES) {
1244 MGET(n, M_DONTWAIT, MT_DATA);
1245 if (n && hlen > MLEN) {
1246 MCLGET(n, M_DONTWAIT);
1247 if ((n->m_flags & M_EXT) == 0) {
1256 m_copydata(m, off, hlen, mtod(n, caddr_t));
1259 * IP options processing.
1260 * See RFC2402 appendix A.
1262 p = mtod(n, u_char *);
1263 i = sizeof(struct ip);
1265 if (i + IPOPT_OPTVAL >= hlen) {
1266 ipseclog((LOG_ERR, "ah4_calccksum: "
1267 "invalid IP option\n"));
1271 if (p[i + IPOPT_OPTVAL] == IPOPT_EOL ||
1272 p[i + IPOPT_OPTVAL] == IPOPT_NOP ||
1273 i + IPOPT_OLEN < hlen)
1277 "ah4_calccksum: invalid IP option "
1279 p[i + IPOPT_OPTVAL]));
1285 switch (p[i + IPOPT_OPTVAL]) {
1291 case IPOPT_SECURITY: /* 0x82 */
1292 case 0x85: /* Extended security */
1293 case 0x86: /* Commercial security */
1294 case 0x94: /* Router alert */
1295 case 0x95: /* RFC1770 */
1296 l = p[i + IPOPT_OLEN];
1302 l = p[i + IPOPT_OLEN];
1308 if (l < 1 || hlen - i < l) {
1311 "ah4_calccksum: invalid IP option "
1312 "(type=%02x len=%02x)\n",
1313 p[i + IPOPT_OPTVAL],
1314 p[i + IPOPT_OLEN]));
1320 if (p[i + IPOPT_OPTVAL] == IPOPT_EOL)
1324 p = mtod(n, u_char *) + sizeof(struct ip);
1325 (algo->update)(&algos, p, hlen - sizeof(struct ip));
1331 hdrtype = (iphdr.ip_p) & 0xff;
1332 advancewidth = hlen;
1343 m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
1344 hdrsiz = (sav->flags & SADB_X_EXT_OLD)
1346 : sizeof(struct newah);
1347 siz = (*algo->sumsiz)(sav);
1348 totlen = (ah.ah_len + 2) << 2;
1351 * special treatment is necessary for the first one, not others
1354 if (totlen > m->m_pkthdr.len - off ||
1355 totlen > MCLBYTES) {
1359 MGET(n, M_DONTWAIT, MT_DATA);
1360 if (n && totlen > MLEN) {
1361 MCLGET(n, M_DONTWAIT);
1362 if ((n->m_flags & M_EXT) == 0) {
1371 m_copydata(m, off, totlen, mtod(n, caddr_t));
1373 bzero(mtod(n, u_int8_t *) + hdrsiz, siz);
1374 (algo->update)(&algos, mtod(n, u_int8_t *), n->m_len);
1378 ah_update_mbuf(m, off, totlen, algo, &algos);
1381 hdrtype = ah.ah_nxt;
1382 advancewidth = totlen;
1387 ah_update_mbuf(m, off, m->m_pkthdr.len - off, algo, &algos);
1388 advancewidth = m->m_pkthdr.len - off;
1392 off += advancewidth;
1393 if (off < m->m_pkthdr.len)
1396 if (len < (*algo->sumsiz)(sav)) {
1401 (algo->result)(&algos, sumbuf, sizeof(sumbuf));
1402 bcopy(&sumbuf[0], ahdat, (*algo->sumsiz)(sav));
1417 * Go generate the checksum. This function won't modify the mbuf chain
1420 * NOTE: the function does not free mbuf on failure.
1421 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1424 ah6_calccksum(m, ahdat, len, algo, sav)
1428 const struct ah_algorithm *algo;
1429 struct secasvar *sav;
1433 struct mbuf *n = NULL;
1436 struct ah_algorithm_state algos;
1437 u_char sumbuf[AH_MAXSUMSIZE];
1439 if ((m->m_flags & M_PKTHDR) == 0)
1442 error = (algo->init)(&algos, sav);
1447 proto = IPPROTO_IPV6;
1452 newoff = ip6_nexthdr(m, off, proto, &nxt);
1454 newoff = m->m_pkthdr.len;
1455 else if (newoff <= off) {
1463 * special treatment is necessary for the first one, not others
1466 struct ip6_hdr ip6copy;
1468 if (newoff - off != sizeof(struct ip6_hdr)) {
1473 m_copydata(m, off, newoff - off, (caddr_t)&ip6copy);
1475 ip6copy.ip6_flow = 0;
1476 ip6copy.ip6_vfc &= ~IPV6_VERSION_MASK;
1477 ip6copy.ip6_vfc |= IPV6_VERSION;
1478 ip6copy.ip6_hlim = 0;
1479 in6_clearscope(&ip6copy.ip6_src); /* XXX */
1480 in6_clearscope(&ip6copy.ip6_dst); /* XXX */
1481 (algo->update)(&algos, (u_int8_t *)&ip6copy,
1482 sizeof(struct ip6_hdr));
1484 newoff = m->m_pkthdr.len;
1485 ah_update_mbuf(m, off, m->m_pkthdr.len - off, algo,
1495 hdrsiz = (sav->flags & SADB_X_EXT_OLD)
1497 : sizeof(struct newah);
1498 siz = (*algo->sumsiz)(sav);
1501 * special treatment is necessary for the first one, not others
1504 if (newoff - off > MCLBYTES) {
1508 MGET(n, M_DONTWAIT, MT_DATA);
1509 if (n && newoff - off > MLEN) {
1510 MCLGET(n, M_DONTWAIT);
1511 if ((n->m_flags & M_EXT) == 0) {
1520 m_copydata(m, off, newoff - off, mtod(n, caddr_t));
1521 n->m_len = newoff - off;
1522 bzero(mtod(n, u_int8_t *) + hdrsiz, siz);
1523 (algo->update)(&algos, mtod(n, u_int8_t *), n->m_len);
1527 ah_update_mbuf(m, off, newoff - off, algo, &algos);
1532 case IPPROTO_HOPOPTS:
1533 case IPPROTO_DSTOPTS:
1535 struct ip6_ext *ip6e;
1537 u_int8_t *p, *optend, *optp;
1539 if (newoff - off > MCLBYTES) {
1543 MGET(n, M_DONTWAIT, MT_DATA);
1544 if (n && newoff - off > MLEN) {
1545 MCLGET(n, M_DONTWAIT);
1546 if ((n->m_flags & M_EXT) == 0) {
1555 m_copydata(m, off, newoff - off, mtod(n, caddr_t));
1556 n->m_len = newoff - off;
1558 ip6e = mtod(n, struct ip6_ext *);
1559 hdrlen = (ip6e->ip6e_len + 1) << 3;
1560 if (newoff - off < hdrlen) {
1566 p = mtod(n, u_int8_t *);
1567 optend = p + hdrlen;
1570 * ICV calculation for the options header including all
1571 * options. This part is a little tricky since there are
1572 * two type of options; mutable and immutable. We try to
1573 * null-out mutable ones here.
1576 while (optp < optend) {
1577 if (optp[0] == IP6OPT_PAD1)
1580 if (optp + 2 > optend) {
1586 optlen = optp[1] + 2;
1589 if (optp + optlen > optend) {
1596 if (optp[0] & IP6OPT_MUTABLE)
1597 bzero(optp + 2, optlen - 2);
1602 (algo->update)(&algos, mtod(n, u_int8_t *), n->m_len);
1608 case IPPROTO_ROUTING:
1610 * For an input packet, we can just calculate `as is'.
1611 * For an output packet, we assume ip6_output have already
1612 * made packet how it will be received at the final
1618 ah_update_mbuf(m, off, newoff - off, algo, &algos);
1622 if (newoff < m->m_pkthdr.len) {
1628 if (len < (*algo->sumsiz)(sav)) {
1633 (algo->result)(&algos, sumbuf, sizeof(sumbuf));
1634 bcopy(&sumbuf[0], ahdat, (*algo->sumsiz)(sav));